Khaotic Engine Reborn
Loading...
Searching...
No Matches
celshade_class.cpp
1
2// Filename: lightshaderclass.cpp
4#include "celshade_class.h"
5
6
7celshade_class::celshade_class()
8{
9 vertex_shader_ = 0;
10 pixel_shader_ = 0;
11 layout_ = 0;
12 sample_state_ = 0;
13 matrix_buffer_ = 0;
14 camera_buffer_ = 0;
15 sunlight_buffer_ = 0;
16 sunlight_color_buffer_ = 0;
17 sunlight_position_buffer_ = 0;
18}
19
20
21celshade_class::celshade_class(const celshade_class& other)
22{
23}
24
25
26celshade_class::~celshade_class()
27{
28}
29
30
31bool celshade_class::initialize(ID3D11Device* device, HWND hwnd)
32{
33 Logger::Get().Log("Initializing LightShaderClass", __FILE__, __LINE__, Logger::LogLevel::Initialize);
34
35 wchar_t vsFilename[128];
36 wchar_t psFilename[128];
37 int error;
38 bool result;
39
40 // Set the filename of the vertex shader.
41 error = wcscpy_s(vsFilename, 128, L"src/hlsl/celshading.vs");
42 if (error != 0)
43 {
44 Logger::Get().Log("Failed to copy string", __FILE__, __LINE__, Logger::LogLevel::Error);
45 return false;
46 }
47
48 // Set the filename of the pixel shader.
49 error = wcscpy_s(psFilename, 128, L"src/hlsl/celshading.ps");
50 if (error != 0)
51 {
52 Logger::Get().Log("Failed to copy string", __FILE__, __LINE__, Logger::LogLevel::Error);
53 return false;
54 }
55 // initialize the vertex and pixel shaders.
56 result = initialize_shader(device, hwnd, vsFilename, psFilename);
57 if (!result)
58 {
59 Logger::Get().Log("Failed to initialize shader", __FILE__, __LINE__, Logger::LogLevel::Error);
60 return false;
61 }
62
63 Logger::Get().Log("SunLightShaderClass initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
64
65 return true;
66}
67
68
69void celshade_class::shutdown()
70{
71 // shutdown the vertex and pixel shaders as well as the related objects.
72 shutdown_shader();
73
74 return;
75}
76
77bool celshade_class::render(ID3D11DeviceContext* deviceContext, int index_count, XMMATRIX world_matrix, XMMATRIX view_matrix, XMMATRIX projection_matrix,
78 ID3D11ShaderResourceView* texture, XMFLOAT4 diffuse_color, XMFLOAT4 ambient_color, XMFLOAT3 sun_direction, float sunIntensity)
79{
80 bool result;
81
82
83 // Set the shader parameters that it will use for rendering.
84 result = set_shader_parameters(deviceContext, world_matrix, view_matrix, projection_matrix, texture, diffuse_color, ambient_color, sun_direction, sunIntensity);
85 if (!result)
86 {
87 Logger::Get().Log("Failed to set shader parameters", __FILE__, __LINE__, Logger::LogLevel::Error);
88 return false;
89 }
90
91 // Now render the prepared buffers with the shader.
92 render_shader(deviceContext, index_count);
93
94 return true;
95}
96
97
98bool celshade_class::initialize_shader(ID3D11Device* device, HWND hwnd, WCHAR* vsFilename, WCHAR* psFilename)
99{
100 Logger::Get().Log("Initializing shader", __FILE__, __LINE__, Logger::LogLevel::Initialize);
101
102 HRESULT result;
103 ID3D10Blob* errorMessage;
104 ID3D10Blob* vertexShaderBuffer;
105 ID3D10Blob* pixelShaderBuffer;
106 D3D11_INPUT_ELEMENT_DESC polygonLayout[3];
107 unsigned int numElements;
108 D3D11_SAMPLER_DESC samplerDesc;
109 D3D11_BUFFER_DESC matrixBufferDesc;
110 D3D11_BUFFER_DESC sunlightBufferDesc;
111
112 // initialize the pointers this function will use to null.
113 errorMessage = 0;
114 vertexShaderBuffer = 0;
115 pixelShaderBuffer = 0;
116
117 // Compile the vertex shader code.
118 result = D3DCompileFromFile(vsFilename, NULL, NULL, "SunLightVertexShader", "vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, &vertexShaderBuffer, &errorMessage);
119 if (FAILED(result))
120 {
121 if (errorMessage)
122 {
123 output_shader_error_message(errorMessage, hwnd, vsFilename);
124 }
125 else
126 {
127 Logger::Get().Log("Failed to compile shader", __FILE__, __LINE__, Logger::LogLevel::Error);
128 }
129 return false;
130 }
131
132 // Compile the pixel shader code.
133 result = D3DCompileFromFile(psFilename, NULL, NULL, "SunLightPixelShader", "ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, &pixelShaderBuffer, &errorMessage);
134 if (FAILED(result))
135 {
136 if (errorMessage)
137 {
138 output_shader_error_message(errorMessage, hwnd, psFilename);
139 }
140 else
141 {
142 Logger::Get().Log("Failed to compile shader", __FILE__, __LINE__, Logger::LogLevel::Error);
143 }
144 return false;
145 }
146
147 // Create the vertex shader from the buffer.
148 result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &vertex_shader_);
149 if (FAILED(result))
150 {
151 Logger::Get().Log("Failed to create vertex shader", __FILE__, __LINE__, Logger::LogLevel::Error);
152 return false;
153 }
154
155 // Create the pixel shader from the buffer.
156 result = device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL, &pixel_shader_);
157 if (FAILED(result))
158 {
159 Logger::Get().Log("Failed to create pixel shader", __FILE__, __LINE__, Logger::LogLevel::Error);
160 return false;
161 }
162
163 // Create the vertex input layout description.
164 polygonLayout[0].SemanticName = "POSITION";
165 polygonLayout[0].SemanticIndex = 0;
166 polygonLayout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
167 polygonLayout[0].InputSlot = 0;
168 polygonLayout[0].AlignedByteOffset = 0;
169 polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
170 polygonLayout[0].InstanceDataStepRate = 0;
171
172 polygonLayout[1].SemanticName = "TEXCOORD";
173 polygonLayout[1].SemanticIndex = 0;
174 polygonLayout[1].Format = DXGI_FORMAT_R32G32_FLOAT;
175 polygonLayout[1].InputSlot = 0;
176 polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
177 polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
178 polygonLayout[1].InstanceDataStepRate = 0;
179
180 polygonLayout[2].SemanticName = "NORMAL";
181 polygonLayout[2].SemanticIndex = 0;
182 polygonLayout[2].Format = DXGI_FORMAT_R32G32B32_FLOAT;
183 polygonLayout[2].InputSlot = 0;
184 polygonLayout[2].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
185 polygonLayout[2].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
186 polygonLayout[2].InstanceDataStepRate = 0;
187
188 // Get a count of the elements in the layout.
189 numElements = sizeof(polygonLayout) / sizeof(polygonLayout[0]);
190
191 // Create the vertex input layout.
192 result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), &layout_);
193 if (FAILED(result))
194 {
195 Logger::Get().Log("Failed to create input layout", __FILE__, __LINE__, Logger::LogLevel::Error);
196 return false;
197 }
198
199 // Release the vertex shader buffer and pixel shader buffer since they are no longer needed.
200 vertexShaderBuffer->Release();
201 vertexShaderBuffer = 0;
202
203 pixelShaderBuffer->Release();
204 pixelShaderBuffer = 0;
205
206 // Create a texture sampler state description.
207 samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
208 samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
209 samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
210 samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
211 samplerDesc.MipLODBias = 0.0f;
212 samplerDesc.MaxAnisotropy = 1;
213 samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
214 samplerDesc.BorderColor[0] = 0;
215 samplerDesc.BorderColor[1] = 0;
216 samplerDesc.BorderColor[2] = 0;
217 samplerDesc.BorderColor[3] = 0;
218 samplerDesc.MinLOD = 0;
219 samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
220
221 // Create the texture sampler state.
222 result = device->CreateSamplerState(&samplerDesc, &sample_state_);
223 if (FAILED(result))
224 {
225 Logger::Get().Log("Failed to create sampler state", __FILE__, __LINE__, Logger::LogLevel::Error);
226 return false;
227 }
228
229 // Setup the description of the dynamic matrix constant buffer that is in the vertex shader.
230 matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
231 matrixBufferDesc.ByteWidth = sizeof(matrix_buffer_type);
232 matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
233 matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
234 matrixBufferDesc.MiscFlags = 0;
235 matrixBufferDesc.StructureByteStride = 0;
236
237 // Create the constant buffer pointer so we can access the vertex shader constant buffer from within this class.
238 result = device->CreateBuffer(&matrixBufferDesc, NULL, &matrix_buffer_);
239 if (FAILED(result))
240 {
241 Logger::Get().Log("Failed to create matrix buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
242 return false;
243 }
244
245 // Setup the description of the dynamic sunlight constant buffer that is in the pixel shader.
246 sunlightBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
247 sunlightBufferDesc.ByteWidth = sizeof(sun_light_buffer_type);
248 sunlightBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
249 sunlightBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
250 sunlightBufferDesc.MiscFlags = 0;
251 sunlightBufferDesc.StructureByteStride = 0;
252
253 // Create the constant buffer pointer so we can access the pixel shader constant buffer from within this class.
254 result = device->CreateBuffer(&sunlightBufferDesc, NULL, &sunlight_buffer_);
255 if (FAILED(result))
256 {
257 Logger::Get().Log("Failed to create sunlight buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
258 return false;
259 }
260
261 Logger::Get().Log("Shader initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
262
263 return true;
264}
265
266
267
268
269void celshade_class::shutdown_shader()
270{
271 Logger::Get().Log("Shutting down SunLightShaderClass", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
272
273 // Release the light constant buffers.
274 if (sunlight_color_buffer_)
275 {
276 sunlight_color_buffer_->Release();
277 sunlight_color_buffer_ = 0;
278 }
279
280 if (sunlight_position_buffer_)
281 {
282 sunlight_position_buffer_->Release();
283 sunlight_position_buffer_ = 0;
284 }
285
286 // Release the light constant buffer.
287 if (sunlight_buffer_)
288 {
289 sunlight_buffer_->Release();
290 sunlight_buffer_ = 0;
291 }
292
293 // Release the camera constant buffer.
294 if (camera_buffer_)
295 {
296 camera_buffer_->Release();
297 camera_buffer_ = 0;
298 }
299
300 // Release the matrix constant buffer.
301 if (matrix_buffer_)
302 {
303 matrix_buffer_->Release();
304 matrix_buffer_ = 0;
305 }
306
307 // Release the sampler state.
308 if (sample_state_)
309 {
310 sample_state_->Release();
311 sample_state_ = 0;
312 }
313
314 // Release the layout.
315 if (layout_)
316 {
317 layout_->Release();
318 layout_ = 0;
319 }
320
321 // Release the pixel shader.
322 if (pixel_shader_)
323 {
324 pixel_shader_->Release();
325 pixel_shader_ = 0;
326 }
327
328 // Release the vertex shader.
329 if (vertex_shader_)
330 {
331 vertex_shader_->Release();
332 vertex_shader_ = 0;
333 }
334
335 Logger::Get().Log("SunLightShaderClass shut down", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
336
337 return;
338}
339
340
341void celshade_class::output_shader_error_message(ID3D10Blob* errorMessage, HWND hwnd, WCHAR* shaderFilename)
342{
343 char* compileErrors;
344 unsigned __int64 bufferSize, i;
345 ofstream fout;
346
347
348 // Get a pointer to the error message text buffer.
349 compileErrors = (char*)(errorMessage->GetBufferPointer());
350
351 // Get the length of the message.
352 bufferSize = errorMessage->GetBufferSize();
353
354 // Open a file to write the error message to.
355 fout.open("shader-error.txt");
356
357 // Write out the error message.
358 for (i = 0; i < bufferSize; i++)
359 {
360 fout << compileErrors[i];
361 }
362
363 // Close the file.
364 fout.close();
365
366 // Release the error message.
367 errorMessage->Release();
368 errorMessage = 0;
369
370 // Pop a message up on the screen to notify the user to check the text file for compile errors.
371 MessageBox(hwnd, L"Error compiling shader. Check shader-error.txt for message.", shaderFilename, MB_OK);
372
373 return;
374}
375
376
377bool celshade_class::set_shader_parameters(
378 ID3D11DeviceContext* device_context,
379 XMMATRIX world_matrix,
380 XMMATRIX view_matrix,
381 XMMATRIX projection_matrix,
382 ID3D11ShaderResourceView* texture,
383 XMFLOAT4 ambient_color,
384 XMFLOAT4 diffuse_color,
385 XMFLOAT3 light_direction,
386 float sun_intensity
387 )
388{
389 HRESULT result;
390 D3D11_MAPPED_SUBRESOURCE mappedResource;
391 matrix_buffer_type* dataPtr;
392 camera_buffer_type* dataPtr2;
393 sun_light_buffer_type* dataPtr3;
394 unsigned int bufferNumber;
395
396 // Transpose the matrices to prepare them for the shader.
397 world_matrix = XMMatrixTranspose(world_matrix);
398 view_matrix = XMMatrixTranspose(view_matrix);
399 projection_matrix = XMMatrixTranspose(projection_matrix);
400
401 // Lock the constant buffer so it can be written to.
402 result = device_context->Map(matrix_buffer_, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
403 if (FAILED(result))
404 {
405 return false;
406 }
407
408 // Get a pointer to the data in the constant buffer.
409 dataPtr = (matrix_buffer_type*)mappedResource.pData;
410
411 // Copy the matrices into the constant buffer.
412 dataPtr->world = world_matrix;
413 dataPtr->view = view_matrix;
414 dataPtr->projection = projection_matrix;
415
416 // Unlock the constant buffer.
417 device_context->Unmap(matrix_buffer_, 0);
418
419 // Set the position of the constant buffer in the vertex shader.
420 bufferNumber = 0;
421
422 // Finally set the constant buffer in the vertex shader with the updated values.
423 device_context->VSSetConstantBuffers(bufferNumber, 1, &matrix_buffer_);
424
425 // Lock the sunlight constant buffer so it can be written to.
426 result = device_context->Map(sunlight_buffer_, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
427 if (FAILED(result))
428 {
429 return false;
430 }
431
432 // Get a pointer to the data in the constant buffer.
433 dataPtr3 = (sun_light_buffer_type*)mappedResource.pData;
434
435 // Copy the lighting variables into the constant buffer.
436 dataPtr3->ambient_color = ambient_color;
437 dataPtr3->diffuse_color = diffuse_color;
438 dataPtr3->sun_direction = light_direction;
439 dataPtr3->intensity = sun_intensity;
440
441 // Unlock the constant buffer.
442 device_context->Unmap(sunlight_buffer_, 0);
443
444 // Set the position of the sunlight constant buffer in the pixel shader.
445 bufferNumber = 0;
446
447 // Finally set the sunlight constant buffer in the pixel shader with the updated values.
448 device_context->PSSetConstantBuffers(bufferNumber, 1, &sunlight_buffer_);
449
450 // Set shader texture resource in the pixel shader.
451 device_context->PSSetShaderResources(0, 1, &texture);
452
453 return true;
454}
455
456void celshade_class::render_shader(ID3D11DeviceContext* deviceContext, int indexCount)
457{
458 // Set the vertex input layout.
459 deviceContext->IASetInputLayout(layout_);
460
461 // Set the vertex and pixel shaders that will be used to render this triangle.
462 deviceContext->VSSetShader(vertex_shader_, NULL, 0);
463 deviceContext->PSSetShader(pixel_shader_, NULL, 0);
464
465 // Set the sampler state in the pixel shader.
466 deviceContext->PSSetSamplers(0, 1, &sample_state_);
467
468 // render the triangle.
469 deviceContext->DrawIndexed(indexCount, 0, 0);
470
471 return;
472}
static Logger & Get()
Definition Logger.h:20
void Log(const std::string &message, const std::string &fileName, int lineNumber, LogLevel level=LogLevel::Info)
Definition Logger.h:158