Khaotic Engine Reborn
Loading...
Searching...
No Matches
skybox_shader_class.cpp
1
2// Filename: lightshaderclass.cpp
4#include "skybox_shader_class.h"
5
6
7skybox_shader_class::skybox_shader_class()
8{
9 m_vertexShader = 0;
10 m_pixelShader = 0;
11 m_layout = 0;
12 m_sampleState = 0;
13 m_matrixBuffer = 0;
14 m_cameraBuffer = 0;
15 m_sunlightBuffer = 0;
16 m_sunlightColorBuffer = 0;
17 m_sunlightPositionBuffer = 0;
18}
19
20
21skybox_shader_class::skybox_shader_class(const skybox_shader_class& other)
22{
23}
24
25
26skybox_shader_class::~skybox_shader_class()
27{
28}
29
30
31bool skybox_shader_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/skybox.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/skybox.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 = InitializeShader(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("skybox_shader_class initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
64
65 return true;
66}
67
68
69void skybox_shader_class::Shutdown()
70{
71 // Shutdown the vertex and pixel shaders as well as the related objects.
72 ShutdownShader();
73
74 return;
75}
76
77bool skybox_shader_class::Render(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
78 ID3D11ShaderResourceView* texture, XMFLOAT4 diffuseColor, XMFLOAT4 ambientColor, XMFLOAT3 sunDirection, float sunIntensity)
79{
80 bool result;
81
82
83 // Set the shader parameters that it will use for rendering.
84 result = SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, texture, diffuseColor, ambientColor, sunDirection, 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 RenderShader(deviceContext, indexCount);
93
94 return true;
95}
96
97
98bool skybox_shader_class::InitializeShader(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, "SkyboxVertexShader", "vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, &vertexShaderBuffer, &errorMessage);
119 if (FAILED(result))
120 {
121 if (errorMessage)
122 {
123 OutputShaderErrorMessage(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, "SkyboxPixelShader", "ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, &pixelShaderBuffer, &errorMessage);
134 if (FAILED(result))
135 {
136 if (errorMessage)
137 {
138 OutputShaderErrorMessage(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, &m_vertexShader);
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, &m_pixelShader);
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(), &m_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, &m_sampleState);
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(MatrixBufferType);
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, &m_matrixBuffer);
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(SkyboxBufferType);
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, &m_sunlightBuffer);
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 skybox_shader_class::ShutdownShader()
270{
271 Logger::Get().Log("Shutting down SunLightShaderClass", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
272
273 // Release the light constant buffers.
274 if (m_sunlightColorBuffer)
275 {
276 m_sunlightColorBuffer->Release();
277 m_sunlightColorBuffer = 0;
278 }
279
280 if (m_sunlightPositionBuffer)
281 {
282 m_sunlightPositionBuffer->Release();
283 m_sunlightPositionBuffer = 0;
284 }
285
286 // Release the light constant buffer.
287 if (m_sunlightBuffer)
288 {
289 m_sunlightBuffer->Release();
290 m_sunlightBuffer = 0;
291 }
292
293 // Release the camera constant buffer.
294 if (m_cameraBuffer)
295 {
296 m_cameraBuffer->Release();
297 m_cameraBuffer = 0;
298 }
299
300 // Release the matrix constant buffer.
301 if (m_matrixBuffer)
302 {
303 m_matrixBuffer->Release();
304 m_matrixBuffer = 0;
305 }
306
307 // Release the sampler state.
308 if (m_sampleState)
309 {
310 m_sampleState->Release();
311 m_sampleState = 0;
312 }
313
314 // Release the layout.
315 if (m_layout)
316 {
317 m_layout->Release();
318 m_layout = 0;
319 }
320
321 // Release the pixel shader.
322 if (m_pixelShader)
323 {
324 m_pixelShader->Release();
325 m_pixelShader = 0;
326 }
327
328 // Release the vertex shader.
329 if (m_vertexShader)
330 {
331 m_vertexShader->Release();
332 m_vertexShader = 0;
333 }
334
335 Logger::Get().Log("SunLightShaderClass shut down", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
336
337 return;
338}
339
340
341void skybox_shader_class::OutputShaderErrorMessage(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 skybox_shader_class::SetShaderParameters(ID3D11DeviceContext* deviceContext, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix, ID3D11ShaderResourceView* texture, XMFLOAT4 ambientColor, XMFLOAT4 diffuseColor, XMFLOAT3 lightDirection, float sunIntensity)
378{
379 HRESULT result;
380 D3D11_MAPPED_SUBRESOURCE mappedResource;
381 MatrixBufferType* dataPtr;
382 CameraBufferType* dataPtr2;
383 SkyboxBufferType* dataPtr3;
384 unsigned int bufferNumber;
385
386 // Transpose the matrices to prepare them for the shader.
387 worldMatrix = XMMatrixTranspose(worldMatrix);
388 viewMatrix = XMMatrixTranspose(viewMatrix);
389 projectionMatrix = XMMatrixTranspose(projectionMatrix);
390
391 // Lock the constant buffer so it can be written to.
392 result = deviceContext->Map(m_matrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
393 if (FAILED(result))
394 {
395 return false;
396 }
397
398 // Get a pointer to the data in the constant buffer.
399 dataPtr = (MatrixBufferType*)mappedResource.pData;
400
401 // Copy the matrices into the constant buffer.
402 dataPtr->world = worldMatrix;
403 dataPtr->view = viewMatrix;
404 dataPtr->projection = projectionMatrix;
405
406 // Unlock the constant buffer.
407 deviceContext->Unmap(m_matrixBuffer, 0);
408
409 // Set the position of the constant buffer in the vertex shader.
410 bufferNumber = 0;
411
412 // Finally set the constant buffer in the vertex shader with the updated values.
413 deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_matrixBuffer);
414
415 // Lock the sunlight constant buffer so it can be written to.
416 result = deviceContext->Map(m_sunlightBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
417 if (FAILED(result))
418 {
419 return false;
420 }
421
422 // Get a pointer to the data in the constant buffer.
423 dataPtr3 = (SkyboxBufferType*)mappedResource.pData;
424
425 // Copy the lighting variables into the constant buffer.
426 dataPtr3->ambientColor = ambientColor;
427 dataPtr3->diffuseColor = diffuseColor;
428 dataPtr3->sunDirection = lightDirection;
429 dataPtr3->intensity = sunIntensity;
430
431 // Unlock the constant buffer.
432 deviceContext->Unmap(m_sunlightBuffer, 0);
433
434 // Set the position of the sunlight constant buffer in the pixel shader.
435 bufferNumber = 0;
436
437 // Finally set the sunlight constant buffer in the pixel shader with the updated values.
438 deviceContext->PSSetConstantBuffers(bufferNumber, 1, &m_sunlightBuffer);
439
440 // Set shader texture resource in the pixel shader.
441 deviceContext->PSSetShaderResources(0, 1, &texture);
442
443 return true;
444}
445
446void skybox_shader_class::RenderShader(ID3D11DeviceContext* deviceContext, int indexCount)
447{
448 // Set the vertex input layout.
449 deviceContext->IASetInputLayout(m_layout);
450
451 // Set the vertex and pixel shaders that will be used to render this triangle.
452 deviceContext->VSSetShader(m_vertexShader, NULL, 0);
453 deviceContext->PSSetShader(m_pixelShader, NULL, 0);
454
455 // Set the sampler state in the pixel shader.
456 deviceContext->PSSetSamplers(0, 1, &m_sampleState);
457
458 // Render the triangle.
459 deviceContext->DrawIndexed(indexCount, 0, 0);
460
461 return;
462}
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