4#include "light_shader_class.h"
7light_shader_class::light_shader_class()
16 light_color_buffer_ = 0;
17 light_position_buffer_ = 0;
26light_shader_class::~light_shader_class()
31bool light_shader_class::initialize(ID3D11Device* device, HWND hwnd)
33 Logger::Get().
Log(
"Initializing light_shader_class", __FILE__, __LINE__, Logger::LogLevel::Initialize);
35 wchar_t vsFilename[128];
36 wchar_t psFilename[128];
41 error = wcscpy_s(vsFilename, 128, L
"src/hlsl/light.vs");
44 Logger::Get().
Log(
"Failed to copy string", __FILE__, __LINE__, Logger::LogLevel::Error);
49 error = wcscpy_s(psFilename, 128, L
"src/hlsl/light.ps");
52 Logger::Get().
Log(
"Failed to copy string", __FILE__, __LINE__, Logger::LogLevel::Error);
56 result = initialize_shader(device, hwnd, vsFilename, psFilename);
59 Logger::Get().
Log(
"Failed to initialize shader", __FILE__, __LINE__, Logger::LogLevel::Error);
63 Logger::Get().
Log(
"light_shader_class initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
69void light_shader_class::shutdown()
77bool light_shader_class::render(ID3D11DeviceContext* deviceContext,
int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
78 ID3D11ShaderResourceView* texture, XMFLOAT4 diffuseColor[], XMFLOAT4 lightPosition[], XMFLOAT4 ambientClor[])
84 result = set_shader_parameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, texture, diffuseColor, lightPosition, ambientClor);
87 Logger::Get().
Log(
"Failed to set shader parameters", __FILE__, __LINE__, Logger::LogLevel::Error);
92 render_shader(deviceContext, indexCount);
98bool light_shader_class::initialize_shader(ID3D11Device* device, HWND hwnd, WCHAR* vsFilename, WCHAR* psFilename)
100 Logger::Get().
Log(
"Initializing shader", __FILE__, __LINE__, Logger::LogLevel::Initialize);
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 cameraBufferDesc;
111 D3D11_BUFFER_DESC lightColorBufferDesc;
112 D3D11_BUFFER_DESC lightPositionBufferDesc;
117 vertexShaderBuffer = 0;
118 pixelShaderBuffer = 0;
121 result = D3DCompileFromFile(vsFilename, NULL, NULL,
"LightVertexShader",
"vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, &vertexShaderBuffer, &errorMessage);
127 output_shader_error_message(errorMessage, hwnd, vsFilename);
132 Logger::Get().
Log(
"Failed to compile shader", __FILE__, __LINE__, Logger::LogLevel::Error);
139 result = D3DCompileFromFile(psFilename, NULL, NULL,
"LightPixelShader",
"ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, &pixelShaderBuffer, &errorMessage);
145 output_shader_error_message(errorMessage, hwnd, psFilename);
150 Logger::Get().
Log(
"Failed to compile shader", __FILE__, __LINE__, Logger::LogLevel::Error);
157 result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &vertex_shader_);
160 Logger::Get().
Log(
"Failed to create vertex shader", __FILE__, __LINE__, Logger::LogLevel::Error);
165 result = device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL, &pixel_shader_);
168 Logger::Get().
Log(
"Failed to create pixel shader", __FILE__, __LINE__, Logger::LogLevel::Error);
174 polygonLayout[0].SemanticName =
"POSITION";
175 polygonLayout[0].SemanticIndex = 0;
176 polygonLayout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
177 polygonLayout[0].InputSlot = 0;
178 polygonLayout[0].AlignedByteOffset = 0;
179 polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
180 polygonLayout[0].InstanceDataStepRate = 0;
182 polygonLayout[1].SemanticName =
"TEXCOORD";
183 polygonLayout[1].SemanticIndex = 0;
184 polygonLayout[1].Format = DXGI_FORMAT_R32G32_FLOAT;
185 polygonLayout[1].InputSlot = 0;
186 polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
187 polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
188 polygonLayout[1].InstanceDataStepRate = 0;
190 polygonLayout[2].SemanticName =
"NORMAL";
191 polygonLayout[2].SemanticIndex = 0;
192 polygonLayout[2].Format = DXGI_FORMAT_R32G32B32_FLOAT;
193 polygonLayout[2].InputSlot = 0;
194 polygonLayout[2].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
195 polygonLayout[2].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
196 polygonLayout[2].InstanceDataStepRate = 0;
199 numElements =
sizeof(polygonLayout) /
sizeof(polygonLayout[0]);
202 result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(),
206 Logger::Get().
Log(
"Failed to create input layout", __FILE__, __LINE__, Logger::LogLevel::Error);
211 vertexShaderBuffer->Release();
212 vertexShaderBuffer = 0;
214 pixelShaderBuffer->Release();
215 pixelShaderBuffer = 0;
218 samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
219 samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
220 samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
221 samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
222 samplerDesc.MipLODBias = 0.0f;
223 samplerDesc.MaxAnisotropy = 1;
224 samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
225 samplerDesc.BorderColor[0] = 0;
226 samplerDesc.BorderColor[1] = 0;
227 samplerDesc.BorderColor[2] = 0;
228 samplerDesc.BorderColor[3] = 0;
229 samplerDesc.MinLOD = 0;
230 samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
233 result = device->CreateSamplerState(&samplerDesc, &sample_state_);
236 Logger::Get().
Log(
"Failed to create sampler state", __FILE__, __LINE__, Logger::LogLevel::Error);
241 matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
242 matrixBufferDesc.ByteWidth =
sizeof(matrix_buffer_type);
243 matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
244 matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
245 matrixBufferDesc.MiscFlags = 0;
246 matrixBufferDesc.StructureByteStride = 0;
249 result = device->CreateBuffer(&matrixBufferDesc, NULL, &matrix_buffer_);
252 Logger::Get().
Log(
"Failed to create matrix buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
259 cameraBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
260 cameraBufferDesc.ByteWidth =
sizeof(camera_buffer_type);
261 cameraBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
262 cameraBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
263 cameraBufferDesc.MiscFlags = 0;
264 cameraBufferDesc.StructureByteStride = 0;
267 result = device->CreateBuffer(&cameraBufferDesc, NULL, &camera_buffer_);
270 Logger::Get().
Log(
"Failed to create camera buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
275 lightColorBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
276 lightColorBufferDesc.ByteWidth =
sizeof(light_color_buffer_type);
277 lightColorBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
278 lightColorBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
279 lightColorBufferDesc.MiscFlags = 0;
280 lightColorBufferDesc.StructureByteStride = 0;
283 result = device->CreateBuffer(&lightColorBufferDesc, NULL, &light_color_buffer_);
286 Logger::Get().
Log(
"Failed to create light color buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
291 lightPositionBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
292 lightPositionBufferDesc.ByteWidth =
sizeof(light_position_buffer_type);
293 lightPositionBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
294 lightPositionBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
295 lightPositionBufferDesc.MiscFlags = 0;
296 lightPositionBufferDesc.StructureByteStride = 0;
299 result = device->CreateBuffer(&lightPositionBufferDesc, NULL, &light_position_buffer_);
302 Logger::Get().
Log(
"Failed to create light position buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
306 Logger::Get().
Log(
"Shader initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
312void light_shader_class::shutdown_shader()
314 Logger::Get().
Log(
"Shutting down light_shader_class", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
317 if (light_color_buffer_)
319 light_color_buffer_->Release();
320 light_color_buffer_ = 0;
323 if (light_position_buffer_)
325 light_position_buffer_->Release();
326 light_position_buffer_ = 0;
332 light_buffer_->Release();
339 camera_buffer_->Release();
346 matrix_buffer_->Release();
353 sample_state_->Release();
367 pixel_shader_->Release();
374 vertex_shader_->Release();
378 Logger::Get().
Log(
"light_shader_class shut down", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
384void light_shader_class::output_shader_error_message(ID3D10Blob* errorMessage, HWND hwnd, WCHAR* shaderFilename)
387 unsigned __int64 bufferSize, i;
392 compileErrors = (
char*)(errorMessage->GetBufferPointer());
395 bufferSize = errorMessage->GetBufferSize();
398 fout.open(
"shader-error.txt");
401 for (i = 0; i < bufferSize; i++)
403 fout << compileErrors[i];
410 errorMessage->Release();
414 MessageBox(hwnd, L
"Error compiling shader. Check shader-error.txt for message.", shaderFilename, MB_OK);
420bool light_shader_class::set_shader_parameters(ID3D11DeviceContext* deviceContext, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
421 ID3D11ShaderResourceView* texture, XMFLOAT4 diffuseColor[], XMFLOAT4 lightPosition[], XMFLOAT4 ambientColor[])
424 D3D11_MAPPED_SUBRESOURCE mappedResource;
425 unsigned int bufferNumber;
426 matrix_buffer_type* dataPtr;
427 light_position_buffer_type* dataPtr2;
428 light_color_buffer_type* dataPtr3;
431 worldMatrix = XMMatrixTranspose(worldMatrix);
432 viewMatrix = XMMatrixTranspose(viewMatrix);
433 projectionMatrix = XMMatrixTranspose(projectionMatrix);
436 result = deviceContext->Map(matrix_buffer_, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
439 Logger::Get().
Log(
"Failed to map matrix buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
444 dataPtr = (matrix_buffer_type*)mappedResource.pData;
447 dataPtr->world = worldMatrix;
448 dataPtr->view = viewMatrix;
449 dataPtr->projection = projectionMatrix;
452 deviceContext->Unmap(matrix_buffer_, 0);
458 deviceContext->VSSetConstantBuffers(bufferNumber, 1, &matrix_buffer_);
461 result = deviceContext->Map(camera_buffer_, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
464 Logger::Get().
Log(
"Failed to map camera buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
469 result = deviceContext->Map(light_position_buffer_, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
472 Logger::Get().
Log(
"Failed to map light position buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
477 dataPtr2 = (light_position_buffer_type*)mappedResource.pData;
480 for (
int i = 0; i < num_lights; i++)
482 dataPtr2->lightPosition[i] = lightPosition[i];
486 deviceContext->Unmap(light_position_buffer_, 0);
492 deviceContext->VSSetConstantBuffers(bufferNumber, 1, &light_position_buffer_);
495 deviceContext->PSSetShaderResources(0, 1, &texture);
498 result = deviceContext->Map(light_color_buffer_, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
501 Logger::Get().
Log(
"Failed to map light color buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
506 dataPtr3 = (light_color_buffer_type*)mappedResource.pData;
509 for (
int i = 0; i < num_lights; i++)
511 dataPtr3->diffuseColor[i] = diffuseColor[i];
515 deviceContext->Unmap(light_color_buffer_, 0);
521 deviceContext->PSSetConstantBuffers(bufferNumber, 1, &light_color_buffer_);
528void light_shader_class::render_shader(ID3D11DeviceContext* deviceContext,
int indexCount)
531 deviceContext->IASetInputLayout(layout_);
534 deviceContext->VSSetShader(vertex_shader_, NULL, 0);
535 deviceContext->PSSetShader(pixel_shader_, NULL, 0);
538 deviceContext->PSSetSamplers(0, 1, &sample_state_);
541 deviceContext->DrawIndexed(indexCount, 0, 0);
void Log(const std::string &message, const std::string &fileName, int lineNumber, LogLevel level=LogLevel::Info)