-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShaders.h
168 lines (132 loc) · 7.04 KB
/
Shaders.h
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
#pragma once
#include <D3Dcompiler.h>
#include <d3d11shader.h>
#include "DXCore.h"
#include "ShaderReflection.h"
#include <string>
#include <fstream>
#include <sstream>
#include <map>
#pragma comment(lib, "dxguid.lib")
class Shader {
public:
std::string id;
ID3D11VertexShader* vertexShader;
ID3D11PixelShader* pixelShader;
std::vector<ConstantBuffer> psConstantBuffers;
std::vector<ConstantBuffer> vsConstantBuffers;
std::map<std::string, int> textureBindPointsVS;
std::map<std::string, int> textureBindPointsPS;
ID3D11InputLayout* layout;
std::string readFile(std::string fileName) {
std::ifstream file(fileName);
std::stringstream buffer;
buffer << file.rdbuf();
return buffer.str();
}
void compileVS(DXCore* core, std::string vsHLSL, unsigned int type) {
ID3DBlob* compiledVertexShader;
ID3DBlob* status;
HRESULT hr = D3DCompile(vsHLSL.c_str(), strlen(vsHLSL.c_str()), NULL, NULL, NULL, "VS", "vs_5_0", 0, 0, &compiledVertexShader, &status);
if (FAILED(hr)) {
MessageBoxA(NULL, (char*)status->GetBufferPointer(), "Vertex Shader Error", 0);
exit(0);
}
//D3DWriteBlobToFile(compiledVertexShader, L"shadername.cso", false);
core->device->CreateVertexShader(compiledVertexShader->GetBufferPointer(), compiledVertexShader->GetBufferSize(), NULL, &vertexShader);
/*D3D11_INPUT_ELEMENT_DESC layoutDesc[] = //Layout Description for basic triangle
{
{ "POS", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "COLOR", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};*/
if (type == 1) { //Shader input layout for Animated object
D3D11_INPUT_ELEMENT_DESC layoutDesc[] = {
{ "POS", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "TANGENT", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "BONEIDS", 0, DXGI_FORMAT_R32G32B32A32_UINT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "BONEWEIGHTS", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
core->device->CreateInputLayout(layoutDesc, 6, compiledVertexShader->GetBufferPointer(), compiledVertexShader->GetBufferSize(), &layout);
} else if (type == 2) { //Shader input latyout for Static Shadowmap Pass
D3D11_INPUT_ELEMENT_DESC layoutDesc[] = {
{ "POS", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
core->device->CreateInputLayout(layoutDesc, 2, compiledVertexShader->GetBufferPointer(), compiledVertexShader->GetBufferSize(), &layout);
}
else if (type == 3) { //Shader input latyout for Animated Shadowmap Pass
D3D11_INPUT_ELEMENT_DESC layoutDesc[] = {
{ "POS", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "BONEIDS", 0, DXGI_FORMAT_R32G32B32A32_UINT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "BONEWEIGHTS", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
core->device->CreateInputLayout(layoutDesc, 4, compiledVertexShader->GetBufferPointer(), compiledVertexShader->GetBufferSize(), &layout);
} else { //Shader input layout for Static Object
D3D11_INPUT_ELEMENT_DESC layoutDesc[] = {
{ "POS", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "TANGENT", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
core->device->CreateInputLayout(layoutDesc, 4, compiledVertexShader->GetBufferPointer(), compiledVertexShader->GetBufferSize(), &layout);
}
ConstantBufferReflection reflection;
reflection.build(core, compiledVertexShader, vsConstantBuffers, textureBindPointsVS, ShaderStage::VertexShader);
}
void compilePS(DXCore* core, std::string psHLSL) {
ID3DBlob* compiledPixelShader;
ID3DBlob* status;
HRESULT hr = D3DCompile(psHLSL.c_str(), strlen(psHLSL.c_str()), NULL, NULL, NULL, "PS", "ps_5_0", 0, 0, &compiledPixelShader, &status);
if (FAILED(hr)) {
MessageBoxA(NULL, (char*)status->GetBufferPointer(), "Pixel Shader Error", 0);
exit(0);
}
core->device->CreatePixelShader(compiledPixelShader->GetBufferPointer(), compiledPixelShader->GetBufferSize(), NULL, &pixelShader);
ConstantBufferReflection reflection;
reflection.build(core, compiledPixelShader, psConstantBuffers, textureBindPointsPS, ShaderStage::PixelShader);
}
void apply(DXCore* core) {
core->devicecontext->IASetInputLayout(layout);
core->devicecontext->VSSetShader(vertexShader, NULL, 0);
core->devicecontext->PSSetShader(pixelShader, NULL, 0);
for (int i = 0; i < vsConstantBuffers.size(); i++) {
vsConstantBuffers[i].upload(core);
}
for (int i = 0; i < psConstantBuffers.size(); i++) {
psConstantBuffers[i].upload(core);
}
}
void updateConstant(std::string constantBufferName, std::string variableName, void* data, std::vector<ConstantBuffer>& buffers) {
for (int i = 0; i < buffers.size(); i++) {
if (buffers[i].name == constantBufferName) {
buffers[i].update(variableName, data);
return;
}
}
}
void updateConstantVS(std::string constantBufferName, std::string variableName, void* data) {
updateConstant(constantBufferName, variableName, data, vsConstantBuffers);
}
void updateConstantPS(std::string constantBufferName, std::string variableName, void* data) {
updateConstant(constantBufferName, variableName, data, psConstantBuffers);
}
int getBindPoint(std::string name) {
return textureBindPointsPS[name];
}
void bindTexturePS(DXCore* core, std::string name, ID3D11ShaderResourceView* srv) {
core->devicecontext->PSSetShaderResources(textureBindPointsPS[name], 1, &srv);
}
void bindTextureVS(DXCore* core, std::string name, ID3D11ShaderResourceView* srv) {
core->devicecontext->VSSetShaderResources(textureBindPointsVS[name], 1, &srv);
}
void init(DXCore* core, std::string vsFileName, std::string psFileName, unsigned int type) {
std::string vs = readFile(vsFileName);
std::string ps = readFile(psFileName);
compileVS(core, vs, type);
compilePS(core, ps);
apply(core);
}
};