-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticleManager.cpp
More file actions
295 lines (249 loc) · 8.75 KB
/
ParticleManager.cpp
File metadata and controls
295 lines (249 loc) · 8.75 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include "Defines.h"
#include "ParticleManager.h"
#include "Shaders.h"
ParticleManager::ParticleManager(ID3D11Device* device, ID3D11ShaderResourceView* SRV, int numberOfParticles, Camera& camera, vector_3 orgin, vector_3 sizeofArea):
movement()
{
this->numberOfParticles = numberOfParticles;
//if not a multiple of 8, make it
if (numberOfParticles % 8 != 0)
{
numberOfParticles += (8 - (numberOfParticles % 8));
}
createShaders(device);
//load a Compute shader
loadComputeShader("ParticleCS.cso", device, computeShaderUpdate);
trpConstantBuffer.time.element = 0;
for (int i = 0; i < numberOfParticles; i++)
{
particles.push_back(point(vector_3(
Randomizer(orgin.x - sizeofArea.x, orgin.x + sizeofArea.x),
Randomizer(orgin.y - sizeofArea.y, orgin.y + sizeofArea.y),
Randomizer(orgin.z - sizeofArea.z, orgin.z + sizeofArea.z))
)
);
}
this->SRV = SRV;
CreateVertexConstantBuffer(device, this->VertexConstantBuffer, camera); // change names of buffers in this class?
CreateGeometryConstantBuffer(device, this->GeoConstantBuffer);
CreatePixelConstantBuffer(device, this->PixelConstantBuffer);
CreateConstBuffer(device, this->CConstantBuffer, sizeof(CB_stuff), &trpConstantBuffer);
D3D11_BUFFER_DESC bufferDesc;
bufferDesc.ByteWidth = sizeof(point) * numberOfParticles;
bufferDesc.Usage = D3D11_USAGE_DEFAULT;
bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER | D3D11_BIND_UNORDERED_ACCESS;
bufferDesc.CPUAccessFlags = 0;
bufferDesc.StructureByteStride = 0;
bufferDesc.MiscFlags = 0;
D3D11_SUBRESOURCE_DATA data;
data.pSysMem = particles.data();
data.SysMemPitch = 0;
data.SysMemSlicePitch = 0;
if (FAILED(device->CreateBuffer(&bufferDesc, &data, &bufferManager)))
{
std::cout << "ERROR! CANNOT CEATE BUFFERMANAGER" << std::endl;
return;
}
D3D11_UNORDERED_ACCESS_VIEW_DESC UavDescription;
UavDescription.Format = DXGI_FORMAT_R32_FLOAT;
UavDescription.ViewDimension = D3D11_UAV_DIMENSION_BUFFER;
UavDescription.Buffer.FirstElement = 0;
UavDescription.Buffer.NumElements = numberOfParticles * 3;
UavDescription.Buffer.Flags = 0;
if (FAILED(device->CreateUnorderedAccessView(bufferManager, &UavDescription, &particleUAV)))
{
std::cout << "ERROR! CANNOT CREATE BUFFER FROM UAV" << std::endl;
return;
}
}
ParticleManager::~ParticleManager()
{
if (computeShaderUpdate != nullptr)
{
computeShaderUpdate->Release();
}
if (particleUAV != nullptr)
{
particleUAV->Release();
}
if (bufferManager != nullptr)
{
bufferManager->Release();
}
if (GeoConstantBuffer != nullptr)
{
GeoConstantBuffer->Release();
}
if (PixelConstantBuffer != nullptr)
{
PixelConstantBuffer->Release();
}
if (VertexConstantBuffer != nullptr)
{
VertexConstantBuffer->Release();
}
if (CConstantBuffer != nullptr)
{
CConstantBuffer->Release();
}
if (pShader != nullptr)
{
pShader->Release();
}
if (vShader != nullptr)
{
vShader->Release();
}
if (geoShader != nullptr)
{
geoShader->Release();
}
}
void ParticleManager::Update(float dt, ID3D11DeviceContext* context)
{
this->trpConstantBuffer.time.element = dt;
this->trpConstantBuffer.random.element = Randomizer(-1.0f, 1.0f);
//time to dispatch stuff
context->CSSetShader(computeShaderUpdate, nullptr, 0);
context->CSSetConstantBuffers(0, 1, &CConstantBuffer);
context->CSSetUnorderedAccessViews(0, 1, &particleUAV, nullptr);
context->Dispatch((UINT)this->particles.size() / 8, 1, 1);//calculate how many groups are needed
//nullify uav bruv
ID3D11UnorderedAccessView* uavNull = nullptr;
context->CSSetUnorderedAccessViews(0, 1, &uavNull, nullptr);
}
void ParticleManager::ShaderUpdater(ID3D11DeviceContext* context, Camera& camera)
{
vector_3 scale(1, 1, 1);
XMMATRIX XMscale(
scale.x, 0.0f, 0.0f, 0.0f,
0.0f, scale.y, 0.0f, 0.0f,
0.0f, 0.0f, scale.z, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
);
//transform positions with matrix
XMMATRIX XMtrans(
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
position.x, position.y, position.z, 1.0f
);
XMMATRIX transScale = (XMscale * XMtrans);
D3D11_SUBRESOURCE_DATA InitData;
VertexCb vcb = {};
vcb.projection.element = camera.GetPerspectiveMatrix();
vcb.view.element = camera.GetViewMatrix();
vcb.transform.element = transScale;
//time to change the VS cBuffer
D3D11_MAPPED_SUBRESOURCE resource;
context->Map(VertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &resource);
memcpy(resource.pData, &vcb, sizeof(VertexCb));
context->Unmap(VertexConstantBuffer, 0);
ZeroMemory(&resource, sizeof(D3D11_MAPPED_SUBRESOURCE));
//GeometryConstBuffer
GeoCb geoConstantBuffer = {
{0,0,0,0},
{0,0,0,0},
};
//uv schtuff
geoConstantBuffer.uvCords.element[0] = movement.uv().xyz.x;
geoConstantBuffer.uvCords.element[1] = movement.uv().xyz.y;
geoConstantBuffer.uvCords.element[2] = movement.uv().xyz.z;
geoConstantBuffer.uvCords.element[3] = movement.uv().w;
context->Map(GeoConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &resource);
memcpy(resource.pData, &geoConstantBuffer, sizeof(GeoCb));
context->Unmap(GeoConstantBuffer, 0);
ZeroMemory(&resource, sizeof(D3D11_MAPPED_SUBRESOURCE));
//update computeshader constant buffer
context->Map(CConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &resource);
memcpy(resource.pData, &trpConstantBuffer, sizeof(CB_stuff));
context->Unmap(CConstantBuffer, 0);
ZeroMemory(&resource, sizeof(D3D11_MAPPED_SUBRESOURCE));
}
void ParticleManager::Draw(ID3D11DeviceContext* context, ID3D11InputLayout* layout)
{
UINT offset = 0;
static UINT strid = sizeof(point);
context->IASetInputLayout(layout);
context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_POINTLIST);
context->VSSetShader(vShader, nullptr, 0);
context->GSSetShader(geoShader, nullptr, 0);
context->PSSetShader(pShader, nullptr, 0);
context->HSSetShader(nullptr, nullptr, 0);
context->DSSetShader(nullptr, nullptr, 0);
context->PSSetShaderResources(0, 1, &SRV);
context->VSSetConstantBuffers(0, 1, &VertexConstantBuffer);
context->GSSetConstantBuffers(0, 1, &GeoConstantBuffer);
context->PSSetConstantBuffers(0, 1, &PixelConstantBuffer);
context->IASetVertexBuffers(0, 1, &this->bufferManager, &strid, &offset);
context->Draw(this->numberOfParticles, 0);
}
std::string ParticleManager::GetVShaderData()
{
return this->vertexShaderByteCode;
}
void ParticleManager::createShaders(ID3D11Device* device)
{
std::string shaderData;
std::ifstream reader;
std::string shaderByteCode;
// Geometry shader
reader.open("x64/Debug/ParticleGS.cso", std::ios::binary | std::ios::beg);
if (!reader.is_open())
{
std::cout << "ERROR! COULD NOT OPEN FILE!: x64/Debug/ParticleGS.cso" << std::endl;
return;
}
reader.seekg(0, std::ios::end);
shaderData.reserve(static_cast<unsigned int>(reader.tellg()));
reader.seekg(0, std::ios::beg);
shaderData.assign((std::istreambuf_iterator<char>(reader)), std::istreambuf_iterator<char>());
HRESULT hr = device->CreateGeometryShader(shaderData.c_str(), shaderData.length(), nullptr, &geoShader);
if FAILED(hr)
{
MessageBox(NULL, L"Couldn't create geometry shader!", L"SHADER ERROR", MB_OK);
return;
}
shaderData.clear();
reader.close();
//vertex shader
reader.open("x64/Debug/ParticleVS.cso", std::ios::binary | std::ios::beg);
if (!reader.is_open())
{
std::cout << "ERROR! COULD NOT OPEN FILE!: x64/Debug/ParticleVS.cso" << std::endl;
return;
}
reader.seekg(0, std::ios::end);
shaderData.reserve(static_cast<unsigned int>(reader.tellg()));
reader.seekg(0, std::ios::beg);
shaderData.assign((std::istreambuf_iterator<char>(reader)), std::istreambuf_iterator<char>());
hr = device->CreateVertexShader(shaderData.c_str(), shaderData.length(), nullptr, &vShader);
if FAILED(hr)
{
MessageBox(NULL, L"Couldn't create vertex shader!", L"SHADER ERROR", MB_OK);
return;
}
vertexShaderByteCode = shaderData;
shaderData.clear();
reader.close();
//pixel shader
//reader.open("x64/Debug/ParticlePS.cso", std::ios::binary | std::ios::beg);
reader.open("x64/Debug/DeferredPS.cso", std::ios::binary | std::ios::beg);
if (!reader.is_open())
{
std::cout << "ERROR! COULD NOT OPEN FILE!: x64/Debug/DeferredPS.cso" << std::endl;
return;
}
reader.seekg(0, std::ios::end);
shaderData.reserve(static_cast<unsigned int>(reader.tellg()));
reader.seekg(0, std::ios::beg);
shaderData.assign((std::istreambuf_iterator<char>(reader)), std::istreambuf_iterator<char>());
hr = device->CreatePixelShader(shaderData.c_str(), shaderData.length(), nullptr, &pShader);
if FAILED(hr)
{
MessageBox(NULL, L"Couldn't create pixel shader!", L"SHADER ERROR", MB_OK);
return;
}
shaderData.clear();
reader.close();
}