/
ShaderPair.cpp
120 lines (102 loc) · 4.11 KB
/
ShaderPair.cpp
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
#include "ShaderPair.h"
#include "SDKmisc.h"
#include "EventManager.h"
#include "GameContext.h"
#include "ObjMesh.h"
#include "Utils.h"
ShaderPair::ShaderPair()
{
// subscribe to onFrameMove
__hook(&EventManager::OnFrameMove, gameContext->eventManager, &ShaderPair::onFrameMove);
}
ShaderPair::~ShaderPair()
{
// release resources
SAFE_RELEASE(vertexLayout11);
SAFE_RELEASE(vertexShader);
SAFE_RELEASE(pixelShader);
// unsubscribe events
__unhook(&EventManager::OnFrameMove, gameContext->eventManager, &ShaderPair::onFrameMove);
}
// input layout has to be the same as in the vertex shader
// currently we have same for each shader...?
const D3D11_INPUT_ELEMENT_DESC layout[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
// ok I got rid of the V_RETURN macros because they were breaking it for no reason
// but I probably shouldn't have
void ShaderPair::constructShaders(ID3D11Device* pd3dDevice)
{
// compile shaders
ID3DBlob* vertexShaderBuffer = NULL;
compileShaderFromFile(vsFilePath(), vsName(), "vs_5_0", &vertexShaderBuffer);
ID3DBlob* pixelShaderBuffer = NULL;
compileShaderFromFile(psFilePath(), psName(), "ps_5_0", &pixelShaderBuffer);
// Create the pixel and vertex shaders
pd3dDevice->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &vertexShader);
DXUT_SetDebugName(vertexShader, "VERTEX_SHADER");
pd3dDevice->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL, &pixelShader);
DXUT_SetDebugName(pixelShader, "PIXEL_SHADER");
// create vertex layout
pd3dDevice->CreateInputLayout(layout, ARRAYSIZE(layout), vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), &vertexLayout11);
DXUT_SetDebugName(vertexLayout11, "VERTEX_LAYOUT");
// clean up
SAFE_RELEASE(vertexShaderBuffer);
SAFE_RELEASE(pixelShaderBuffer);
// create constant buffers
createBuffers(pd3dDevice);
}
//**************************************************************************//
// Compile the shader file. These files aren't pre-compiled (well, not //
// here, and are compiled on he fly). //
//**************************************************************************//
HRESULT ShaderPair::compileShaderFromFile(WCHAR* szFileName, // File Name
LPCSTR szEntryPoint, // Namee of shader
LPCSTR szShaderModel, // Shader model
ID3DBlob** ppBlobOut) // Blob returned
{
HRESULT hr = S_OK;
// find the file
WCHAR str[MAX_PATH];
V_RETURN(DXUTFindDXSDKMediaFileCch(str, MAX_PATH, szFileName));
DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS;
#if defined( DEBUG ) || defined( _DEBUG )
// Set the D3DCOMPILE_DEBUG flag to embed debug information in the shaders.
// Setting this flag improves the shader debugging experience, but still allows
// the shaders to be optimized and to run exactly the way they will run in
// the release configuration of this program.
dwShaderFlags |= D3DCOMPILE_DEBUG;
#endif
ID3DBlob* pErrorBlob;
hr = D3DX11CompileFromFile(str, NULL, NULL, szEntryPoint, szShaderModel,
dwShaderFlags, 0, NULL, ppBlobOut, &pErrorBlob, NULL);
if (FAILED(hr))
{
WCHAR errorCharsW[200];
if (pErrorBlob != NULL)
{
Utils::charStrToWideChar(errorCharsW, static_cast<char *>(pErrorBlob->GetBufferPointer()));
MessageBox(0, errorCharsW, L"Error", 0);
}
if (pErrorBlob) pErrorBlob->Release();
return hr;
}
SAFE_RELEASE(pErrorBlob);
return S_OK;
}
// doesn't actually 'render' anything...
void ShaderPair::renderPerFrame(ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3dImmediateContext)
{
updateBuffersPerFrame(pd3dImmediateContext);
}
// set as the current shader
void ShaderPair::setShader(ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3dImmediateContext)
{
pd3dImmediateContext->IASetInputLayout(vertexLayout11);
pd3dImmediateContext->VSSetShader(vertexShader, NULL, 0);
pd3dImmediateContext->PSSetShader(pixelShader, NULL, 0);
setConstantBuffers(pd3dImmediateContext);
}