-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstance_buffer_windows.cpp
More file actions
194 lines (175 loc) · 5.7 KB
/
Copy pathinstance_buffer_windows.cpp
File metadata and controls
194 lines (175 loc) · 5.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "detect_os.hpp"
#ifdef PAZ_WINDOWS
#include "PAZ_Graphics"
#include "util_windows.hpp"
#include "internal_data.hpp"
#include "common.hpp"
static constexpr int TypeSize = 4;
paz::InstanceBuffer::Data::~Data()
{
for(auto& n : _buffers)
{
if(n)
{
n->Release();
}
}
}
void paz::InstanceBuffer::Data::addAttribute(int dim, DataType type, const void*
data, std::size_t size)
{
checkSize(dim, size);
_buffers.emplace_back();
_strides.push_back(TypeSize*dim);
if(size)
{
D3D11_BUFFER_DESC bufDescriptor = {};
bufDescriptor.Usage = D3D11_USAGE_DEFAULT;
bufDescriptor.ByteWidth = TypeSize*size;
bufDescriptor.BindFlags = D3D11_BIND_VERTEX_BUFFER;
D3D11_SUBRESOURCE_DATA srData = {};
srData.pSysMem = data;
const auto hr = d3d_device()->CreateBuffer(&bufDescriptor, &srData,
&_buffers.back());
if(hr)
{
throw std::runtime_error("Failed to create instance buffer (" +
format_hresult(hr) + ").");
}
}
const unsigned int slot = _inputElemDescriptors.size();
D3D11_INPUT_ELEMENT_DESC inputDescriptor = {};
inputDescriptor.SemanticName = "INST";
inputDescriptor.SemanticIndex = slot;
inputDescriptor.Format = dxgi_format(dim, type);
inputDescriptor.InputSlot = slot;
inputDescriptor.AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
inputDescriptor.InputSlotClass = D3D11_INPUT_PER_INSTANCE_DATA;
inputDescriptor.InstanceDataStepRate = 1;
_inputElemDescriptors.push_back(inputDescriptor);
}
paz::InstanceBuffer::InstanceBuffer()
{
initialize();
_data = std::make_shared<Data>();
}
paz::InstanceBuffer::InstanceBuffer(std::size_t size) : InstanceBuffer()
{
_data->_numInstances = size;
}
void paz::InstanceBuffer::Data::checkSize(int dim, std::size_t size)
{
if(dim != 1 && dim != 2 && dim != 4)
{
throw std::runtime_error("Instance attribute dimensions must be 1, 2, o"
"r 4.");
}
const std::size_t m = size/dim;
if(!_numInstances)
{
_numInstances = m;
}
else if(m != _numInstances)
{
throw std::runtime_error("Number of instances for each attribute must m"
"atch.");
}
}
void paz::InstanceBuffer::addAttribute(int dim, DataType type)
{
if(!_data->_numInstances)
{
throw std::runtime_error("Instance buffer size has not been set.");
}
_data->_buffers.emplace_back();
_data->_strides.push_back(TypeSize*dim);
D3D11_BUFFER_DESC bufDescriptor = {};
bufDescriptor.Usage = D3D11_USAGE_DYNAMIC;
bufDescriptor.ByteWidth = TypeSize*dim*_data->_numInstances;
bufDescriptor.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bufDescriptor.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
const auto hr = d3d_device()->CreateBuffer(&bufDescriptor, nullptr, &_data->
_buffers.back());
if(hr)
{
throw std::runtime_error("Failed to create instance buffer (" +
format_hresult(hr) + ").");
}
const unsigned int slot = _data->_inputElemDescriptors.size();
D3D11_INPUT_ELEMENT_DESC inputDescriptor = {};
inputDescriptor.SemanticName = "INST";
inputDescriptor.SemanticIndex = slot;
inputDescriptor.Format = dxgi_format(dim, type);
inputDescriptor.InputSlot = slot;
inputDescriptor.AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
inputDescriptor.InputSlotClass = D3D11_INPUT_PER_INSTANCE_DATA;
inputDescriptor.InstanceDataStepRate = 1;
_data->_inputElemDescriptors.push_back(inputDescriptor);
}
void paz::InstanceBuffer::addAttribute(int dim, const float* data, std::size_t
size)
{
_data->addAttribute(dim, DataType::Float, data, size);
}
void paz::InstanceBuffer::addAttribute(int dim, const unsigned int* data, std::
size_t size)
{
_data->addAttribute(dim, DataType::UInt, data, size);
}
void paz::InstanceBuffer::addAttribute(int dim, const int* data, std::size_t
size)
{
_data->addAttribute(dim, DataType::SInt, data, size);
}
void paz::InstanceBuffer::subAttribute(std::size_t idx, const float* data, std::
size_t size)
{
D3D11_MAPPED_SUBRESOURCE mappedSr;
const auto hr = d3d_context()->Map(_data->_buffers[idx], 0,
D3D11_MAP_WRITE_DISCARD, 0, &mappedSr);
if(hr)
{
throw std::runtime_error("Failed to map instance buffer (" +
format_hresult(hr) + ").");
}
std::copy(data, data + size, reinterpret_cast<float*>(mappedSr.pData));
d3d_context()->Unmap(_data->_buffers[idx], 0);
}
void paz::InstanceBuffer::subAttribute(std::size_t idx, const unsigned int*
data, std::size_t size)
{
D3D11_MAPPED_SUBRESOURCE mappedSr;
const auto hr = d3d_context()->Map(_data->_buffers[idx], 0,
D3D11_MAP_WRITE_DISCARD, 0, &mappedSr);
if(hr)
{
throw std::runtime_error("Failed to map instance buffer (" +
format_hresult(hr) + ").");
}
std::copy(data, data + size, reinterpret_cast<unsigned int*>(mappedSr.
pData));
d3d_context()->Unmap(_data->_buffers[idx], 0);
}
void paz::InstanceBuffer::subAttribute(std::size_t idx, const int* data, std::
size_t size)
{
D3D11_MAPPED_SUBRESOURCE mappedSr;
const auto hr = d3d_context()->Map(_data->_buffers[idx], 0,
D3D11_MAP_WRITE_DISCARD, 0, &mappedSr);
if(hr)
{
throw std::runtime_error("Failed to map instance buffer (" +
format_hresult(hr) + ").");
}
std::copy(data, data + size, reinterpret_cast<int*>(mappedSr.pData));
d3d_context()->Unmap(_data->_buffers[idx], 0);
}
bool paz::InstanceBuffer::empty() const
{
return !_data || !_data->_numInstances;
}
std::size_t paz::InstanceBuffer::size() const
{
return _data ? _data->_numInstances : 0;
}
#endif