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