forked from GameTechDev/CloudsGPUPro6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRenderTechnique.cpp
292 lines (250 loc) · 10.1 KB
/
RenderTechnique.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
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
/////////////////////////////////////////////////////////////////////////////////////////////
// Copyright 2017 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or imlied.
// See the License for the specific language governing permissions and
// limitations under the License.
/////////////////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "RenderTechnique.h"
#include <D3DX11.h>
#include <D3Dcompiler.h>
#include <fstream>
CRenderTechnique::CRenderTechnique(void)
{
}
CRenderTechnique::~CRenderTechnique(void)
{
}
void CRenderTechnique::Release()
{
m_pVS.Release();
m_pGS.Release();
m_pPS.Release();
m_pCS.Release();
m_pRS.Release();
m_pDS.Release();
m_pBS.Release();
m_pVSByteCode.Release();
m_pContext.Release();
m_pDevice.Release();
}
static
HRESULT CompileShaderFromFile(LPCTSTR strFilePath,
LPCSTR strFunctionName,
const D3D_SHADER_MACRO* pDefines,
LPCSTR profile,
ID3DBlob **ppBlobOut)
{
DWORD dwShaderFlags = D3D10_SHADER_ENABLE_STRICTNESS;
#if defined( DEBUG ) || defined( _DEBUG )
// Set the D3D10_SHADER_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 |= D3D10_SHADER_DEBUG;
#else
// Warning: do not use this flag as it causes shader compiler to fail the compilation and
// report strange errors:
// dwShaderFlags |= D3D10_SHADER_OPTIMIZATION_LEVEL3;
#endif
HRESULT hr;
do
{
CComPtr<ID3DBlob> errors;
hr = D3DX11CompileFromFile(strFilePath, pDefines, NULL, strFunctionName, profile, dwShaderFlags, 0, NULL, ppBlobOut, &errors, NULL);
if( errors )
{
OutputDebugStringA((char*) errors->GetBufferPointer());
if( FAILED(hr) &&
IDRETRY != MessageBoxA(NULL, (char*) errors->GetBufferPointer(), "FX Error", MB_ICONERROR|MB_ABORTRETRYIGNORE) )
{
break;
}
}
} while( FAILED(hr) );
return hr;
}
HRESULT CRenderTechnique::CreateVertexShaderFromFile(LPCTSTR strFilePath,
LPCSTR strFunctionName,
const D3D_SHADER_MACRO* pDefines)
{
HRESULT hr;
m_pVSByteCode.Release();
hr = CompileShaderFromFile( strFilePath, strFunctionName, pDefines, "vs_5_0", &m_pVSByteCode );
if(FAILED(hr))return hr;
m_pVS.Release();
if( m_pDevice )
hr = m_pDevice->CreateVertexShader( m_pVSByteCode->GetBufferPointer(), m_pVSByteCode->GetBufferSize(), NULL, &m_pVS );
else
return E_FAIL;
return hr;
}
HRESULT CRenderTechnique::CreateGeometryShaderFromFile(LPCTSTR strFilePath,
LPCSTR strFunctionName,
const D3D_SHADER_MACRO* pDefines)
{
CComPtr<ID3DBlob> pShaderByteCode;
HRESULT hr;
hr = CompileShaderFromFile( strFilePath, strFunctionName, pDefines, "gs_5_0", &pShaderByteCode );
if(FAILED(hr))return hr;
m_pGS.Release();
if( m_pDevice )
hr = m_pDevice->CreateGeometryShader( pShaderByteCode->GetBufferPointer(), pShaderByteCode->GetBufferSize(), NULL, &m_pGS );
else
return E_FAIL;
return hr;
}
HRESULT CRenderTechnique::CreateGSWithSOFromFile(LPCTSTR strFilePath,
LPCSTR strFunctionName,
const D3D_SHADER_MACRO* pDefines,
D3D11_SO_DECLARATION_ENTRY *pDecl,
UINT NumDeclEntries,
UINT *pStrides,
UINT NumStrides,
UINT RasterizedStream)
{
CComPtr<ID3DBlob> pShaderByteCode;
HRESULT hr;
hr = CompileShaderFromFile( strFilePath, strFunctionName, pDefines, "gs_5_0", &pShaderByteCode );
if(FAILED(hr))return hr;
m_pGS.Release();
if( m_pDevice )
hr = m_pDevice->CreateGeometryShaderWithStreamOutput( pShaderByteCode->GetBufferPointer(), pShaderByteCode->GetBufferSize(), pDecl, NumDeclEntries, pStrides, NumStrides, RasterizedStream, NULL, &m_pGS );
else
return E_FAIL;
return hr;
}
HRESULT CRenderTechnique::CreatePixelShaderFromFile(LPCTSTR strFilePath,
LPCSTR strFunctionName,
const D3D_SHADER_MACRO* pDefines)
{
CComPtr<ID3DBlob> pShaderByteCode;
HRESULT hr;
hr = CompileShaderFromFile( strFilePath, strFunctionName, pDefines, "ps_5_0", &pShaderByteCode );
if(FAILED(hr))return hr;
m_pPS.Release();
if( m_pDevice )
hr = m_pDevice->CreatePixelShader( pShaderByteCode->GetBufferPointer(), pShaderByteCode->GetBufferSize(), NULL, &m_pPS );
else
return E_FAIL;
return hr;
}
HRESULT CRenderTechnique::CreateComputeShaderFromFile(LPCTSTR strFilePath,
LPCSTR strFunctionName,
const D3D_SHADER_MACRO* pDefines)
{
CComPtr<ID3DBlob> pShaderByteCode;
HRESULT hr;
hr = CompileShaderFromFile( strFilePath, strFunctionName, pDefines, "cs_5_0", &pShaderByteCode );
if(FAILED(hr))return hr;
m_pCS.Release();
if( m_pDevice )
hr = m_pDevice->CreateComputeShader( pShaderByteCode->GetBufferPointer(), pShaderByteCode->GetBufferSize(), NULL, &m_pCS );
else
return E_FAIL;
return hr;
}
HRESULT CRenderTechnique::CreateVGPShadersFromFile(LPCTSTR strFilePath,
LPSTR strVSFunctionName,
LPSTR strGSFunctionName,
LPSTR strPSFunctionName,
const D3D_SHADER_MACRO* pDefines)
{
HRESULT hr = S_OK;
if( strVSFunctionName )
{
hr = CreateVertexShaderFromFile(strFilePath, strVSFunctionName, pDefines);
if( FAILED(hr) )return hr;
}
if( strPSFunctionName )
{
hr = CreatePixelShaderFromFile(strFilePath, strPSFunctionName, pDefines);
if( FAILED(hr) )return hr;
}
if( strGSFunctionName )
{
hr = CreateGeometryShaderFromFile(strFilePath, strGSFunctionName, pDefines);
if( FAILED(hr) )return hr;
}
return hr;
}
void CRenderTechnique::Apply()
{
m_pContext->HSSetShader(NULL, NULL, 0);
m_pContext->DSSetShader(NULL, NULL, 0);
m_pContext->VSSetShader(m_pVS, NULL, 0);
m_pContext->GSSetShader(m_pGS, NULL, 0);
m_pContext->PSSetShader(m_pPS, NULL, 0);
m_pContext->CSSetShader(m_pCS, NULL, 0);
m_pContext->RSSetState(m_pRS);
m_pContext->OMSetDepthStencilState(m_pDS, m_uiSampleRef);
float fBlendFactor[] = {0, 0, 0, 0};
m_pContext->OMSetBlendState(m_pBS, fBlendFactor, 0xFFFFFFFF);
}
HRESULT CRenderTechnique::CreateDefaultBlendState()
{
HRESULT hr;
D3D11_BLEND_DESC DefaultBlendStateDesc;
ZeroMemory(&DefaultBlendStateDesc, sizeof(DefaultBlendStateDesc));
DefaultBlendStateDesc.IndependentBlendEnable = FALSE;
for(int i=0; i< _countof(DefaultBlendStateDesc.RenderTarget); i++)
DefaultBlendStateDesc.RenderTarget[i].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
V( m_pDevice->CreateBlendState( &DefaultBlendStateDesc, &m_pBS) );
return hr;
}
HRESULT CRenderTechnique::CreateDefaultDepthState(BOOL bEnableDepth,
D3D11_DEPTH_WRITE_MASK WriteMask)
{
HRESULT hr;
D3D11_DEPTH_STENCIL_DESC DSDesc;
ZeroMemory(&DSDesc, sizeof(DSDesc));
DSDesc.DepthEnable = bEnableDepth;
DSDesc.DepthWriteMask = WriteMask;
DSDesc.DepthFunc = D3D11_COMPARISON_GREATER;
CComPtr<ID3D11DepthStencilState> pDisableDepthTestDS;
V( m_pDevice->CreateDepthStencilState( &DSDesc, &m_pDS) );
return hr;
}
HRESULT CRenderTechnique::CreateDefaultRasterizerState(D3D11_FILL_MODE FillMode,
D3D11_CULL_MODE CullMode,
BOOL bIsFrontCCW)
{
HRESULT hr;
D3D11_RASTERIZER_DESC RSDesc;
ZeroMemory(&RSDesc, sizeof(RSDesc));
RSDesc.FillMode = FillMode;
RSDesc.CullMode = CullMode;
RSDesc.FrontCounterClockwise = bIsFrontCCW;
CComPtr<ID3D11RasterizerState> pRSSolidFillNoCull;
V( m_pDevice->CreateRasterizerState( &RSDesc, &m_pRS) );
return hr;
}
HRESULT CRenderTechnique::CreateSampler(ID3D11SamplerState **ppSamplerState,
D3D11_FILTER Filter,
D3D11_TEXTURE_ADDRESS_MODE AddressMode)
{
D3D11_SAMPLER_DESC SamDesc =
{
Filter,
AddressMode,
AddressMode,
AddressMode,
0, //FLOAT MipLODBias;
0, //UINT MaxAnisotropy;
D3D11_COMPARISON_NEVER, // D3D11_COMPARISON_FUNC ComparisonFunc;
{0.f, 0.f, 0.f, 0.f}, //FLOAT BorderColor[ 4 ];
-FLT_MAX, //FLOAT MinLOD;
+FLT_MAX //FLOAT MaxLOD;
};
HRESULT hr = m_pDevice->CreateSamplerState(&SamDesc, ppSamplerState);
return hr;
}