Rise
The Vieneo Province
Toggle main menu visibility
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
7
using
Microsoft::WRL::ComPtr;
8
namespace
Rendering
9
{
10
bool
VertexLayout11::Valid
()
const
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
}
19
bool
VertexLayout11::operator<
(
const
VertexLayout11
& other)
const
20
{
21
return
std::tie(
stride
,
position
,
normal
,
pointSize
,
diffuse
,
specular
,
uv
[0],
uv
[1],
uvDimensions
[0],
uvDimensions
[1],
transformed
)
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
}
39
void
Geometry11::Reset
()
40
{
41
ReleaseDevice
();std::vector<uint8_t>().swap(
bytes
);
revision
=1;
42
dynamic
=
index
=
indices32
=
locked
=
readOnly
=
false
;
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
}
51
void
Geometry11::ReleaseDevice
()
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
}
75
HRESULT
Geometry11::Unlock
()
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);
91
FrameProfiler11::Get
().
BufferUpload
(
bytes
.size());
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
;
137
FrameProfiler11::Get
().
BufferUpload
(count*
sizeof
(
FixedVertex11
));
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
HRESULT(WINAPI *CUniBuffer::_ScriptApplyDigitSubstitution)(const SCRIPT_DIGITSUBSTITUTE *
FrameProfiler11.h
Geometry11.h
Rendering::FrameProfiler11::Get
static FrameProfiler11 & Get()
Definition
FrameProfiler11.h:499
Rendering::FrameProfiler11::BufferUpload
void BufferUpload(UINT64 bytes)
Definition
FrameProfiler11.h:587
Rendering::Geometry11::Create
HRESULT Create(ID3D11Device *device, UINT bytes, bool dynamic, bool index=false, bool indices32=false)
Definition
Geometry11.cpp:44
Rendering::Geometry11::ReleaseDevice
void ReleaseDevice()
Definition
Geometry11.cpp:51
Rendering::Geometry11::revision
uint64_t revision
Definition
Geometry11.h:57
Rendering::Geometry11::NativeBuffer
HRESULT NativeBuffer(ID3D11DeviceContext *context, ID3D11Buffer **result)
Definition
Geometry11.cpp:94
Rendering::Geometry11::RestoreDevice
HRESULT RestoreDevice(ID3D11Device *device)
Definition
Geometry11.cpp:55
Rendering::Geometry11::Reset
void Reset()
Definition
Geometry11.cpp:39
Rendering::Geometry11::device
Microsoft::WRL::ComPtr< ID3D11Device > device
Definition
Geometry11.h:53
Rendering::Geometry11::BindIndices
HRESULT BindIndices(ID3D11DeviceContext *context)
Definition
Geometry11.cpp:99
Rendering::Geometry11::converted
std::map< VertexLayout11, Converted > converted
Definition
Geometry11.h:56
Rendering::Geometry11::locked
bool locked
Definition
Geometry11.h:58
Rendering::Geometry11::indices32
bool indices32
Definition
Geometry11.h:58
Rendering::Geometry11::Lock
HRESULT Lock(UINT offset, UINT size, void **destination, bool readOnly=false, bool discard=false)
Definition
Geometry11.cpp:65
Rendering::Geometry11::native
Microsoft::WRL::ComPtr< ID3D11Buffer > native
Definition
Geometry11.h:54
Rendering::Geometry11::bytes
std::vector< uint8_t > bytes
Definition
Geometry11.h:55
Rendering::Geometry11::Upload
HRESULT Upload(ID3D11DeviceContext *context)
Definition
Geometry11.cpp:80
Rendering::Geometry11::index
bool index
Definition
Geometry11.h:58
Rendering::Geometry11::Size
UINT Size() const
Definition
Geometry11.h:38
Rendering::Geometry11::BindVertices
HRESULT BindVertices(ID3D11DeviceContext *context, const VertexLayout11 &layout, UINT byteOffset, bool canonical)
Definition
Geometry11.cpp:104
Rendering::Geometry11::Unlock
HRESULT Unlock()
Definition
Geometry11.cpp:75
Rendering::Geometry11::dynamic
bool dynamic
Definition
Geometry11.h:58
Rendering::Geometry11::readOnly
bool readOnly
Definition
Geometry11.h:58
Rendering::Geometry11::uploaded
uint64_t uploaded
Definition
Geometry11.h:57
Rendering
Definition
AdDiagnostics11.cpp:16
Rendering::FixedVertex11
Definition
FixedFunction11.h:12
Rendering::FixedVertex11::uv
DirectX::XMFLOAT3 uv[2]
Definition
FixedFunction11.h:17
Rendering::FixedVertex11::pointSize
float pointSize
Definition
FixedFunction11.h:15
Rendering::FixedVertex11::normal
DirectX::XMFLOAT3 normal
Definition
FixedFunction11.h:14
Rendering::FixedVertex11::specular
uint32_t specular
Definition
FixedFunction11.h:16
Rendering::FixedVertex11::diffuse
uint32_t diffuse
Definition
FixedFunction11.h:16
Rendering::FixedVertex11::position
DirectX::XMFLOAT4 position
Definition
FixedFunction11.h:13
Rendering::VertexLayout11
Definition
Geometry11.h:12
Rendering::VertexLayout11::operator<
bool operator<(const VertexLayout11 &other) const
Definition
Geometry11.cpp:19
Rendering::VertexLayout11::uvDimensions
UINT uvDimensions[2]
Definition
Geometry11.h:15
Rendering::VertexLayout11::transformed
bool transformed
Definition
Geometry11.h:16
Rendering::VertexLayout11::uv
UINT uv[2]
Definition
Geometry11.h:15
Rendering::VertexLayout11::position
UINT position
Definition
Geometry11.h:14
Rendering::VertexLayout11::normal
UINT normal
Definition
Geometry11.h:14
Rendering::VertexLayout11::Absent
static constexpr UINT Absent
Definition
Geometry11.h:13
Rendering::VertexLayout11::Valid
bool Valid() const
Definition
Geometry11.cpp:10
Rendering::VertexLayout11::diffuse
UINT diffuse
Definition
Geometry11.h:14
Rendering::VertexLayout11::stride
UINT stride
Definition
Geometry11.h:14
Rendering::VertexLayout11::specular
UINT specular
Definition
Geometry11.h:14
Rendering::VertexLayout11::Elements
std::vector< D3D11_INPUT_ELEMENT_DESC > Elements() const
Definition
Geometry11.cpp:24
Rendering::VertexLayout11::pointSize
UINT pointSize
Definition
Geometry11.h:14
Rendering
Geometry11.cpp
Generated on
for Rise by
1.18.0