Rise
The Vieneo Province
Loading...
Searching...
No Matches
Geometry11.cpp
Go to the documentation of this file.
1#include "Geometry11.h"
2#include "FrameProfiler11.h"
3#include <algorithm>
4#include <cstring>
5#include <tuple>
6
7using Microsoft::WRL::ComPtr;
8namespace Rendering
9{
11 {
12 if(!stride || position==Absent)return false;
13 const auto fits=[&](UINT offset,UINT size){return offset==Absent || (offset<=stride && size<=stride-offset);};
14 if(!fits(position,transformed ? 16 : 12) || !fits(normal,12) || !fits(pointSize,4)
15 || !fits(diffuse,4) || !fits(specular,4))return false;
16 for(unsigned i=0;i<2;++i)if(uvDimensions[i]<1 || uvDimensions[i]>3 || !fits(uv[i],4*uvDimensions[i]))return false;
17 return true;
18 }
20 {
22 <std::tie(other.stride,other.position,other.normal,other.pointSize,other.diffuse,other.specular,other.uv[0],other.uv[1],other.uvDimensions[0],other.uvDimensions[1],other.transformed);
23 }
24 std::vector<D3D11_INPUT_ELEMENT_DESC> VertexLayout11::Elements() const
25 {
26 std::vector<D3D11_INPUT_ELEMENT_DESC> result;
27 const auto add=[&](const char* semantic,UINT ordinal,DXGI_FORMAT format,UINT offset) {
28 if(offset!=Absent)result.push_back({semantic,ordinal,format,0,offset,D3D11_INPUT_PER_VERTEX_DATA,0});
29 };
30 add("POSITION",0,transformed ? DXGI_FORMAT_R32G32B32A32_FLOAT : DXGI_FORMAT_R32G32B32_FLOAT,position);
31 add("NORMAL",0,DXGI_FORMAT_R32G32B32_FLOAT,normal);
32 add("PSIZE",0,DXGI_FORMAT_R32_FLOAT,pointSize);
33 add("COLOR",0,DXGI_FORMAT_R8G8B8A8_UNORM,diffuse);
34 add("COLOR",1,DXGI_FORMAT_R8G8B8A8_UNORM,specular);
35 const DXGI_FORMAT formats[]={DXGI_FORMAT_UNKNOWN,DXGI_FORMAT_R32_FLOAT,DXGI_FORMAT_R32G32_FLOAT,DXGI_FORMAT_R32G32B32_FLOAT};
36 for(unsigned i=0;i<2;++i)if(uvDimensions[i]>=1 && uvDimensions[i]<=3)add("TEXCOORD",i,formats[uvDimensions[i]],uv[i]);
37 return result;
38 }
40 {
41 ReleaseDevice();std::vector<uint8_t>().swap(bytes);revision=1;
43 }
44 HRESULT Geometry11::Create(ID3D11Device* nativeDevice,UINT size,bool streaming,bool isIndex,bool wideIndices)
45 {
46 Reset();if(!nativeDevice || !size || (isIndex && size%(wideIndices ? 4 : 2)))return E_INVALIDARG;
47 device=nativeDevice;dynamic=streaming;index=isIndex;indices32=wideIndices;
48 try {bytes.resize(size);}catch(const std::bad_alloc&){Reset();return E_OUTOFMEMORY;}
49 const HRESULT hr=RestoreDevice(nativeDevice);if(FAILED(hr))Reset();return hr;
50 }
52 {
53 converted.clear();native.Reset();device.Reset();uploaded=0;locked=readOnly=false;
54 }
55 HRESULT Geometry11::RestoreDevice(ID3D11Device* nativeDevice)
56 {
57 ReleaseDevice();if(!nativeDevice || bytes.empty())return E_INVALIDARG;
58 D3D11_BUFFER_DESC desc{};desc.ByteWidth=Size();desc.Usage=dynamic ? D3D11_USAGE_DYNAMIC : D3D11_USAGE_DEFAULT;
59 desc.BindFlags=index ? D3D11_BIND_INDEX_BUFFER : D3D11_BIND_VERTEX_BUFFER;
60 desc.CPUAccessFlags=dynamic ? D3D11_CPU_ACCESS_WRITE : 0;
61 const HRESULT hr=nativeDevice->CreateBuffer(&desc,nullptr,&native);
62 if(SUCCEEDED(hr))device=nativeDevice;
63 return hr;
64 }
65 HRESULT Geometry11::Lock(UINT offset,UINT size,void** destination,bool reading,bool discard)
66 {
67 if(destination)*destination=nullptr;
68 if(!destination || !native || locked || offset>bytes.size())return E_INVALIDARG;
69 if(!size)size=UINT(bytes.size())-offset;
70 if(size>bytes.size()-offset || !size || (reading && discard))return E_INVALIDARG;
71 // Discard is a GPU lifetime hint. Keep untouched CPU bytes for callers
72 // that edit a subrange or read geometry back for collision detection.
73 locked=true;readOnly=reading;*destination=bytes.data()+offset;return S_OK;
74 }
76 {
77 if(!locked)return E_UNEXPECTED;
78 locked=false;if(!readOnly)++revision;return S_OK;
79 }
80 HRESULT Geometry11::Upload(ID3D11DeviceContext* context)
81 {
82 if(!native || !context || locked)return E_UNEXPECTED;
83 ComPtr<ID3D11Device> caller;context->GetDevice(&caller);if(caller!=device)return E_INVALIDARG;
84 if(uploaded==revision)return S_OK;
85 if(dynamic)
86 {
87 D3D11_MAPPED_SUBRESOURCE map{};const HRESULT hr=context->Map(native.Get(),0,D3D11_MAP_WRITE_DISCARD,0,&map);
88 if(FAILED(hr))return hr;memcpy(map.pData,bytes.data(),bytes.size());context->Unmap(native.Get(),0);
89 }
90 else context->UpdateSubresource(native.Get(),0,nullptr,bytes.data(),0,0);
92 uploaded=revision;return S_OK;
93 }
94 HRESULT Geometry11::NativeBuffer(ID3D11DeviceContext* context,ID3D11Buffer** result)
95 {
96 if(!result)return E_INVALIDARG;*result=nullptr;
97 const HRESULT hr=Upload(context);if(FAILED(hr))return hr;native.CopyTo(result);return S_OK;
98 }
99 HRESULT Geometry11::BindIndices(ID3D11DeviceContext* context)
100 {
101 if(!index)return E_INVALIDARG;const HRESULT hr=Upload(context);if(FAILED(hr))return hr;
102 context->IASetIndexBuffer(native.Get(),indices32 ? DXGI_FORMAT_R32_UINT : DXGI_FORMAT_R16_UINT,0);return S_OK;
103 }
104 HRESULT Geometry11::BindVertices(ID3D11DeviceContext* context,const VertexLayout11& layout,UINT byteOffset,bool canonical)
105 {
106 if(index || !layout.Valid() || byteOffset>bytes.size() || layout.stride>bytes.size()-byteOffset)return E_INVALIDARG;
107 HRESULT hr=Upload(context);if(FAILED(hr))return hr;
108 if(!canonical)
109 {
110 auto* vb=native.Get();context->IASetVertexBuffers(0,1,&vb,&layout.stride,&byteOffset);return S_OK;
111 }
112 if(byteOffset%layout.stride)return E_INVALIDARG;
113 auto& cache=converted[layout];
114 const size_t count=bytes.size()/layout.stride;
115 if(count>UINT_MAX/sizeof(FixedVertex11))return E_INVALIDARG;
116 if(!cache.buffer)
117 {
118 D3D11_BUFFER_DESC desc{};desc.ByteWidth=UINT(count*sizeof(FixedVertex11));desc.Usage=D3D11_USAGE_DYNAMIC;
119 desc.BindFlags=D3D11_BIND_VERTEX_BUFFER;desc.CPUAccessFlags=D3D11_CPU_ACCESS_WRITE;
120 if(FAILED(hr=device->CreateBuffer(&desc,nullptr,&cache.buffer)))return hr;
121 }
122 if(cache.revision!=revision)
123 {
124 D3D11_MAPPED_SUBRESOURCE map{};if(FAILED(hr=context->Map(cache.buffer.Get(),0,D3D11_MAP_WRITE_DISCARD,0,&map)))return hr;
125 auto* destination=static_cast<FixedVertex11*>(map.pData);
126 for(size_t i=0;i<count;++i)
127 {
128 FixedVertex11 value;const auto* source=bytes.data()+i*layout.stride;
129 const auto copy=[&](void* out,UINT offset,UINT size){if(offset!=VertexLayout11::Absent)memcpy(out,source+offset,size);};
130 copy(&value.position,layout.position,layout.transformed ? 16 : 12);
131 copy(&value.normal,layout.normal,12);copy(&value.pointSize,layout.pointSize,4);
132 copy(&value.diffuse,layout.diffuse,4);copy(&value.specular,layout.specular,4);
133 for(unsigned uv=0;uv<2;++uv)copy(&value.uv[uv],layout.uv[uv],layout.uvDimensions[uv]*4);
134 destination[i]=value;
135 }
136 context->Unmap(cache.buffer.Get(),0);cache.revision=revision;
138 }
139 auto* vb=cache.buffer.Get();const UINT stride=sizeof(FixedVertex11),offset=byteOffset/layout.stride*stride;
140 context->IASetVertexBuffers(0,1,&vb,&stride,&offset);return S_OK;
141 }
142}
HRESULT(WINAPI *CUniBuffer::_ScriptApplyDigitSubstitution)(const SCRIPT_DIGITSUBSTITUTE *
static FrameProfiler11 & Get()
void BufferUpload(UINT64 bytes)
HRESULT Create(ID3D11Device *device, UINT bytes, bool dynamic, bool index=false, bool indices32=false)
HRESULT NativeBuffer(ID3D11DeviceContext *context, ID3D11Buffer **result)
HRESULT RestoreDevice(ID3D11Device *device)
Microsoft::WRL::ComPtr< ID3D11Device > device
Definition Geometry11.h:53
HRESULT BindIndices(ID3D11DeviceContext *context)
std::map< VertexLayout11, Converted > converted
Definition Geometry11.h:56
HRESULT Lock(UINT offset, UINT size, void **destination, bool readOnly=false, bool discard=false)
Microsoft::WRL::ComPtr< ID3D11Buffer > native
Definition Geometry11.h:54
std::vector< uint8_t > bytes
Definition Geometry11.h:55
HRESULT Upload(ID3D11DeviceContext *context)
UINT Size() const
Definition Geometry11.h:38
HRESULT BindVertices(ID3D11DeviceContext *context, const VertexLayout11 &layout, UINT byteOffset, bool canonical)
DirectX::XMFLOAT3 uv[2]
DirectX::XMFLOAT3 normal
DirectX::XMFLOAT4 position
bool operator<(const VertexLayout11 &other) const
static constexpr UINT Absent
Definition Geometry11.h:13
std::vector< D3D11_INPUT_ELEMENT_DESC > Elements() const