-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShader.cpp
More file actions
545 lines (449 loc) · 20.4 KB
/
Copy pathShader.cpp
File metadata and controls
545 lines (449 loc) · 20.4 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
// ===========================================================================
/// <summary>
/// Shader.cpp
/// DirectXIntroduction
/// created by Mehrdad Soleimanimajd on 27.08.2019
/// </summary>
/// <created>ʆϒʅ, 27.08.2019</created>
/// <changed>ʆϒʅ, 04.07.2023</changed>
// ===========================================================================
#include "Shader.h"
#include "Shared.h"
Shader::Shader (ID3D10Device1* dev, std::wstring entry) :
device (dev), entryPoint (entry)
{
// shaders, introduced in several different types,
// are used to actually render vertices or pixels to the screen in the render process,
// controlled by the graphics rendering pipeline, which is programmed by these shaders,
// and each of them is a small program that controls one step of the pipeline.
// the process in nature takes vertices as input and results in fully rendered images,
// in which each type of shader is run many times based on its different nature.
// note that shaders are written in HLSL (High Level Shader Language),
// Visual Studio is able to create HLSL programs, and after compiling,
// the HLSL file is compiled into CSO (Compiled Shader Objects), usable by the running program.
// note that it is essential to write high efficient vertex shaders: http://www.rastertek.com/dx10s2tut04.html
vertexBuffer = nullptr;
pixelBuffer = nullptr;
errorMsg = nullptr;
vertexShader = nullptr;
pixelShader = nullptr;
inputLayout = nullptr;
samplerState = nullptr;
};
Shader::Buffer::Buffer (void)
{
buffer = nullptr;
size = 0;
};
Shader::Buffer::~Buffer (void)
{
if (buffer)
release ();
};
void Shader::Buffer::release (void)
{
if (buffer)
{
delete buffer;
buffer = nullptr;
}
};
void Shader::loadCompiled (std::string& fileName, Buffer* csoBuffer)
{
try
{
// load shaders from .cso compiled files
std::string path {""};
#ifndef _NOT_DEBUGGING
path = {".//x64//Debug//"};
#else
path = {".//x64//Release//"};
#endif // !_NOT_DEBUGGING
path += fileName;
std::ifstream csoFile (path, std::ios::binary | std::ios::ate);
if (csoFile.is_open ())
{
csoBuffer->size = csoFile.tellg (); // shader object size
csoBuffer->buffer = new (std::nothrow) byte [csoBuffer->size];
if (csoBuffer->buffer)
{
csoFile.seekg (0, std::ios::beg);
csoFile.read (reinterpret_cast<char*>(csoBuffer->buffer), csoBuffer->size);
} else
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
L"Shader buffer allocation for compiled file failed!" + entryPoint);
}
csoFile.close ();
} else
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
L"Loading shader form compiled file failed!" + entryPoint);
}
} catch (const std::exception& ex)
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
Converter::strConverter (ex.what ()) + entryPoint);
}
};
bool Shader::initializeCompiled (std::string* filePaths,
D3D10_INPUT_ELEMENT_DESC* polygonLayout,
unsigned short elmCount)
{
try
{
HRESULT hR;
// load and encapsulate .cso shaders (compiled by VisualStudio) into usable shader objects
Buffer vertexBuf; // vertex shader buffer
loadCompiled (filePaths [0], &vertexBuf); // load process
Buffer pixelBuf; // pixel shader buffer
loadCompiled (filePaths [1], &pixelBuf);
// Direct3D interface for vertex shaders: vertex shader creation
// purpose: invoking the HLSL shaders for drawing the 3D models already on the GPU
if (vertexBuf.buffer && pixelBuf.buffer)
{
hR = device->CreateVertexShader (vertexBuf.buffer,
vertexBuf.size, &vertexShader);
if (FAILED (hR))
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
L"Creation of vertex shader failed!" + entryPoint);
return false;
}
// Direct3D interface for pixel shaders: pixel shader creation
hR = device->CreatePixelShader (pixelBuf.buffer,
pixelBuf.size, &pixelShader);
if (FAILED (hR))
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
L"Creation of pixel shader failed!" + entryPoint);
return false;
}
// input layout creation (how to handle the defined vertices)
// the interface holds definition of how to feed vertex data into the input-assembler stage of the graphics rendering pipeline
hR = device->CreateInputLayout (polygonLayout, elmCount,
vertexBuf.buffer,
vertexBuf.size, &inputLayout);
if (FAILED (hR))
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
L"Creation of input layout failed!" + entryPoint);
return false;
}
}
vertexBuf.release ();
pixelBuf.release ();
return true;
} catch (const std::exception& ex)
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
Converter::strConverter (ex.what ()) + entryPoint);
return false;
}
};
bool Shader::compile (LPCWSTR* files)
{
try
{
HRESULT hR;
std::string errorStr; // HLSL compilation errors in string
// directly compile the shader into buffer (DirectX APIs)
hR = D3DCompileFromFile (files [0], nullptr, nullptr,
"main", "vs_4_1", D3D10_SHADER_ENABLE_STRICTNESS,
0, &vertexBuffer, &errorMsg);
if (FAILED (hR))
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
L"Compilation of texture vertex shader file failed!" + entryPoint);
if (errorMsg)
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
Converter::strConverter ((char*) errorMsg->GetBufferPointer ())
+ entryPoint);
errorStr = (char*) errorMsg->GetBufferPointer ();
errorMsg->Release ();
errorMsg = nullptr;
}
return false;
}
hR = D3DCompileFromFile (files [1], nullptr, nullptr,
"main", "ps_4_1", D3D10_SHADER_ENABLE_STRICTNESS,
0, &pixelBuffer, &errorMsg);
if (FAILED (hR))
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
L"Compilation of texture pixel shader file failed!" + entryPoint);
if (errorMsg)
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
Converter::strConverter ((char*) errorMsg->GetBufferPointer ())
+ entryPoint);
errorStr = (char*) errorMsg->GetBufferPointer ();
errorMsg->Release ();
errorMsg = nullptr;
}
return false;
}
return true;
} catch (const std::exception& ex)
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
Converter::strConverter (ex.what ()) + entryPoint);
return false;
}
};
bool Shader::initialize (D3D10_INPUT_ELEMENT_DESC* polygonLayout,
unsigned short elmCount,
D3D10_SAMPLER_DESC* sampler)
{
try
{
HRESULT hR;
// purpose: invoking the HLSL shaders for drawing the 3D models already on the GPU
hR = device->CreateVertexShader (vertexBuffer->GetBufferPointer (),
vertexBuffer->GetBufferSize (), &vertexShader);
if (FAILED (hR))
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
L"Creation of texture vertex shader failed!" + entryPoint);
return false;
}
hR = device->CreatePixelShader (pixelBuffer->GetBufferPointer (),
pixelBuffer->GetBufferSize (), &pixelShader);
if (FAILED (hR))
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
L"Creation of texture pixel shader failed!" + entryPoint);
return false;
}
// input layout creation (how to handle the defined vertices)
// the interface holds definition of how to feed vertex data into the input-assembler stage of the graphics rendering pipeline
hR = device->CreateInputLayout (polygonLayout, elmCount,
vertexBuffer->GetBufferPointer (),
vertexBuffer->GetBufferSize (), &inputLayout);
if (FAILED (hR))
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
L"Creation of texture input layout failed!" + entryPoint);
return false;
}
vertexBuffer->Release ();
vertexBuffer = nullptr;
pixelBuffer->Release ();
pixelBuffer = nullptr;
if (sampler)
{
// testure sampler state creation
hR = device->CreateSamplerState (sampler, &samplerState);
if (FAILED (hR))
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
L"Creation of texture sampler state failed!" + entryPoint);
return false;
}
}
return true;
} catch (const std::exception& ex)
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
Converter::strConverter (ex.what ()) + entryPoint);
return false;
}
};
ID3D10VertexShader* const Shader::getVertexShader (void)
{
return vertexShader;
};
ID3D10PixelShader* const Shader::getPixelShader (void)
{
return pixelShader;
};
ID3D10InputLayout* const Shader::getInputLayout (void)
{
return inputLayout;
};
ID3D10SamplerState** const Shader::getSamplerState (void)
{
return &samplerState;
};
void Shader::release (void)
{
try
{
if (vertexShader)
{
vertexShader->Release ();
vertexShader = nullptr;
}
if (pixelShader)
{
pixelShader->Release ();
pixelShader = nullptr;
}
if (inputLayout)
{
inputLayout->Release ();
inputLayout = nullptr;
}
if (samplerState)
{
samplerState->Release ();
samplerState = nullptr;
}
device = nullptr;
} catch (const std::exception& ex)
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
Converter::strConverter (ex.what ()));
}
};
ShaderColour::ShaderColour (ID3D10Device1* dev) :
Shader (dev, L"ColourShader"), initialized (false)
{
try
{
// input layout description (passed into the shader)
// note that the description must match to the vertex types defined in game and shader
// teaching the GPU how to read a custom vertex structure.
// note that to improve the rendering speed,
// the GPU can be told what information with each vertex needs to be stored.
// note flag D3D10_APPEND_ALIGNED_ELEMENT: the elements are one after each other,
// therefore automatically figure the spacing out. (no need to define the offset)
polygonLayoutDesc [0].SemanticName = "POSITION"; // what a certain value is used for
polygonLayoutDesc [0].SemanticIndex = 0; // modifies the semantic with an integer index (multiple elements with same semantic)
polygonLayoutDesc [0].Format = DXGI_FORMAT_R32G32B32_FLOAT; // 32 bits for each x, y and z
polygonLayoutDesc [0].InputSlot = 0; // imput - assembler or input slot through which data is fed to GPU ( Direct3D supports sixteen input slots )
polygonLayoutDesc [0].AlignedByteOffset = 0; // the offset between each element in the structure
polygonLayoutDesc [0].InputSlotClass = D3D10_INPUT_PER_VERTEX_DATA; // input data class for a single input slot
polygonLayoutDesc [0].InstanceDataStepRate = 0; // for now
polygonLayoutDesc [1].SemanticName = "COLOR"; // colour semantic
polygonLayoutDesc [1].SemanticIndex = 0;
polygonLayoutDesc [1].Format = DXGI_FORMAT_R32G32B32A32_FLOAT; // 32 bits for each x, y and z
polygonLayoutDesc [1].InputSlot = 0;
polygonLayoutDesc [1].AlignedByteOffset = 12; // offset: 3*4 byte of float type
polygonLayoutDesc [1].InputSlotClass = D3D10_INPUT_PER_VERTEX_DATA;
polygonLayoutDesc [1].InstanceDataStepRate = 0;
elementsCount = 2;
// compiled by VisualStudio
files [0] = "vertex.cso";
files [1] = "pixel.cso";
initialized = initializeCompiled (files, polygonLayoutDesc, elementsCount);
} catch (const std::exception& ex)
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
Converter::strConverter (ex.what ()));
}
};
const bool& ShaderColour::isInitialized (void)
{
return initialized;
};
ShaderTexture::ShaderTexture (ID3D10Device1* dev) :
Shader (dev, L"TextureShader"), initialized (false)
{
try
{
polygonLayoutDesc [0].SemanticName = "POSITION";
polygonLayoutDesc [0].SemanticIndex = 0;
polygonLayoutDesc [0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
polygonLayoutDesc [0].InputSlot = 0;
polygonLayoutDesc [0].AlignedByteOffset = 0;
polygonLayoutDesc [0].InputSlotClass = D3D10_INPUT_PER_VERTEX_DATA;
polygonLayoutDesc [0].InstanceDataStepRate = 0;
polygonLayoutDesc [1].SemanticName = "TEXCOORD"; // texture coordinate semantic
// note that numbered semantics are introduced here without any numbers
polygonLayoutDesc [1].SemanticIndex = 0;
polygonLayoutDesc [1].Format = DXGI_FORMAT_R32G32_FLOAT; // 32 bits for each U (width) and V (height)
polygonLayoutDesc [1].InputSlot = 0;
polygonLayoutDesc [1].AlignedByteOffset = 12; //D3D10_APPEND_ALIGNED_ELEMENT
polygonLayoutDesc [1].InputSlotClass = D3D10_INPUT_PER_VERTEX_DATA;
polygonLayoutDesc [1].InstanceDataStepRate = 0;
// tecture sampler state description (to interface with texture shader):
// which pixel or what combination of them to use when drawing (near or far away polygon)
// liner option: the most expensive in processing and the best visual result,
// unisng linear interpretation for manification, magnification and mip-level sampling.
samplerDesc.Filter = D3D10_FILTER_MIN_MAG_MIP_LINEAR; // the most important one
// wrap: ensures the coordinates stay between 0.0f and 1.0f by wrapping anything already outside,
// around and within these values
samplerDesc.AddressU = D3D10_TEXTURE_ADDRESS_WRAP;
samplerDesc.AddressV = D3D10_TEXTURE_ADDRESS_WRAP;
samplerDesc.AddressW = D3D10_TEXTURE_ADDRESS_WRAP;
samplerDesc.MipLODBias = 0.0f;
samplerDesc.MaxAnisotropy = 1;
samplerDesc.ComparisonFunc = D3D10_COMPARISON_ALWAYS;
samplerDesc.BorderColor [0] = 0.0f;
samplerDesc.BorderColor [1] = 0.0f;
samplerDesc.BorderColor [2] = 0.0f;
samplerDesc.BorderColor [3] = 0.0f;
samplerDesc.MinLOD = 0.0f;
samplerDesc.MaxLOD = D3D10_FLOAT32_MAX;
elementsCount = 2;
files [0] = L"./graphics/vertexT.hlsl";
files [1] = L"./graphics/pixelT.hlsl";
if (compile (files))
initialized = initialize (polygonLayoutDesc, elementsCount, &samplerDesc);
} catch (const std::exception& ex)
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
Converter::strConverter (ex.what ()));
}
};
const bool& ShaderTexture::isInitialized (void)
{
return initialized;
};
ShaderDiffuseLight::ShaderDiffuseLight (ID3D10Device1* dev) :
Shader (dev, L"DiffuseLightShader"), initialized (false)
{
try
{
polygonLayoutDesc [0].SemanticName = "POSITION";
polygonLayoutDesc [0].SemanticIndex = 0;
polygonLayoutDesc [0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
polygonLayoutDesc [0].InputSlot = 0;
polygonLayoutDesc [0].AlignedByteOffset = 0;
polygonLayoutDesc [0].InputSlotClass = D3D10_INPUT_PER_VERTEX_DATA;
polygonLayoutDesc [0].InstanceDataStepRate = 0;
polygonLayoutDesc [1].SemanticName = "TEXCOORD";
polygonLayoutDesc [1].SemanticIndex = 0;
polygonLayoutDesc [1].Format = DXGI_FORMAT_R32G32_FLOAT;
polygonLayoutDesc [1].InputSlot = 0;
polygonLayoutDesc [1].AlignedByteOffset = 12; //D3D10_APPEND_ALIGNED_ELEMENT
polygonLayoutDesc [1].InputSlotClass = D3D10_INPUT_PER_VERTEX_DATA;
polygonLayoutDesc [1].InstanceDataStepRate = 0;
polygonLayoutDesc [2].SemanticName = "NORMAL"; // normal light semantic
polygonLayoutDesc [2].SemanticIndex = 0;
polygonLayoutDesc [2].Format = DXGI_FORMAT_R32G32B32_FLOAT; // each x, y and z of the normal vector 32 bits
polygonLayoutDesc [2].InputSlot = 0;
polygonLayoutDesc [2].AlignedByteOffset = 20; //D3D10_APPEND_ALIGNED_ELEMENT
polygonLayoutDesc [2].InputSlotClass = D3D10_INPUT_PER_VERTEX_DATA;
polygonLayoutDesc [2].InstanceDataStepRate = 0;
samplerDesc.Filter = D3D10_FILTER_MIN_MAG_MIP_LINEAR;
samplerDesc.AddressU = D3D10_TEXTURE_ADDRESS_WRAP;
samplerDesc.AddressV = D3D10_TEXTURE_ADDRESS_WRAP;
samplerDesc.AddressW = D3D10_TEXTURE_ADDRESS_WRAP;
samplerDesc.MipLODBias = 0.0f;
samplerDesc.MaxAnisotropy = 1;
samplerDesc.ComparisonFunc = D3D10_COMPARISON_ALWAYS;
samplerDesc.BorderColor [0] = 0.0f;
samplerDesc.BorderColor [1] = 0.0f;
samplerDesc.BorderColor [2] = 0.0f;
samplerDesc.BorderColor [3] = 0.0f;
samplerDesc.MinLOD = 0.0f;
samplerDesc.MaxLOD = D3D10_FLOAT32_MAX;
elementsCount = 3;
files [0] = L"./graphics/vertexL.hlsl";
files [1] = L"./graphics/pixelL.hlsl";
if (compile (files))
initialized = initialize (polygonLayoutDesc, elementsCount, &samplerDesc);
} catch (const std::exception& ex)
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
Converter::strConverter (ex.what ()));
}
};
const bool& ShaderDiffuseLight::isInitialized (void)
{
return initialized;
};