forked from GameTechDev/CloudsGPUPro6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCloudsController.h
280 lines (224 loc) · 12.2 KB
/
CloudsController.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
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
/////////////////////////////////////////////////////////////////////////////////////////////
// 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.
/////////////////////////////////////////////////////////////////////////////////////////////
#pragma once
#include "RenderTechnique.h"
#include "Structures.fxh"
struct SPrecomputedOpticalDepthTexDim
{
const int iNumStartPosZenithAngles;
const int iNumStartPosAzimuthAngles;
const int iNumDirectionZenithAngles;
const int iNumDirectionAzimuthAngles;
SPrecomputedOpticalDepthTexDim() :
iNumStartPosZenithAngles(32/2),
iNumStartPosAzimuthAngles(64/2),
iNumDirectionZenithAngles(32/2),
iNumDirectionAzimuthAngles(64/2)
{
}
};
struct SPrecomputedScatteringInParticleTexDim
{
const int iNumStartPosZenithAngles;
const int iNumViewDirAzimuthAngles;
const int iNumViewDirZenithAngles;
const int iNumDistancesFromCenter;
SPrecomputedScatteringInParticleTexDim() :
iNumStartPosZenithAngles(32),
iNumViewDirAzimuthAngles(64),
iNumViewDirZenithAngles(32),
iNumDistancesFromCenter(32)
{
}
};
class CCloudsController
{
public:
CCloudsController();
~CCloudsController();
HRESULT OnCreateDevice(ID3D11Device *pDevice, ID3D11DeviceContext *pDeviceContext);
void OnDestroyDevice();
void Update( const SGlobalCloudAttribs &NewAttribs,
const D3DXVECTOR3 &CameraPos,
const D3DXVECTOR3 &LightDir,
ID3D11Device *pDevice,
ID3D11DeviceContext *pDeviceContext,
ID3D11Buffer *pcbCameraAttribs,
ID3D11Buffer *pcbLightAttribs,
ID3D11Buffer *pcMediaScatteringParams );
struct SRenderAttribs
{
ID3D11Device *pDevice;
ID3D11DeviceContext *pDeviceContext;
D3DXMATRIX ViewProjMatr;
ID3D11Buffer *pcbCameraAttribs;
ID3D11Buffer *pcbLightAttribs;
ID3D11Buffer *pcMediaScatteringParams;
ID3D11ShaderResourceView *pPrecomputedNetDensitySRV;
ID3D11ShaderResourceView *pAmbientSkylightSRV;
ID3D11ShaderResourceView *pDepthBufferSRV;
ID3D11DepthStencilView *pShadowMapDSV;
ID3D11ShaderResourceView *pLiSpCloudTransparencySRV;
ID3D11ShaderResourceView *pLiSpCloudMinMaxDepthSRV;
D3DXVECTOR3 f3CameraPos;
D3DXVECTOR3 f3ViewDir;
int iCascadeIndex;
float fCurrTime;
UINT uiLiSpCloudDensityDim;
bool bLightSpacePass;
const SCameraAttribs *m_pCameraAttribs;
const SShadowMapAttribs *m_pSMAttribs;
SRenderAttribs(){memset(this, 0, sizeof(*this));}
};
void RenderScreenSpaceDensityAndColor(SRenderAttribs &RenderAttribs);
void RenderLightSpaceDensity(SRenderAttribs &RenderAttribs);
void MergeLiSpDensityWithShadowMap(SRenderAttribs &RenderAttribs);
void CombineWithBackBuffer(ID3D11Device *pDevice,
ID3D11DeviceContext *pDeviceContext,
ID3D11ShaderResourceView *pDepthBufferSRV,
ID3D11ShaderResourceView *pBackBufferSRV);
void OnResize(ID3D11Device *pDevice,
UINT uiWidth, UINT uiHeight);
ID3D11ShaderResourceView *GetScrSpaceCloudColor(){return m_ptex2DScreenCloudColorSRV;}
ID3D11ShaderResourceView *GetScrSpaceCloudTransparency(){return m_ptex2DScrSpaceCloudTransparencySRV;}
ID3D11ShaderResourceView *GetScrSpaceCloudMinMaxDist(){return m_ptex2DScrSpaceDistToCloudSRV;}
const SGlobalCloudAttribs &GetCloudAttribs(){return m_CloudAttribs;}
bool IsPSOrderingAvailable(){return m_bPSOrderingAvailable;}
private:
CCloudsController(const CCloudsController&);
const CCloudsController& operator =(const CCloudsController&);
void DefineMacros(class CD3DShaderMacroHelper &Macros);
void RenderMaxDensityMip(ID3D11Device *pDevice,
ID3D11DeviceContext *pDeviceContext,
ID3D11Texture2D *ptex2DMaxDensityMipMap,
ID3D11Texture2D *ptex2DTmpMaxDensityMipMap,
const D3D11_TEXTURE2D_DESC &MaxCloudDensityMipDesc);
void RenderFlatClouds(SRenderAttribs &RenderAttribs);
void RenderParticles(SRenderAttribs &RenderAttribs);
void GenerateParticles(SRenderAttribs &RenderAttribs);
void PrepareDispatchArgsBuffer(SRenderAttribs &RenderAttribs, ID3D11ShaderResourceView *pCounterSRV, int iTechInd);
void SortVisibileParticles(SRenderAttribs &RenderAttribs);
HRESULT CreateBufferAndViews(ID3D11Device *pDevice,
const D3D11_BUFFER_DESC &BuffDesc,
D3D11_SUBRESOURCE_DATA *pInitData,
ID3D11Buffer **ppBuffer,
ID3D11ShaderResourceView **ppSRV = nullptr,
ID3D11UnorderedAccessView **ppUAV = nullptr,
UINT UAVFlags = 0);
HRESULT CreateParticleDataBuffer(ID3D11Device *pDevice);
HRESULT PrecomputParticleDensity(ID3D11Device *pDevice, ID3D11DeviceContext *pDeviceContext);
HRESULT PrecomputeScatteringInParticle(ID3D11Device *pDevice, ID3D11DeviceContext *pDeviceContext);
HRESULT ComputeExitance(ID3D11Device *pDevice, ID3D11DeviceContext *pDeviceContext);
HRESULT Create3DNoise(ID3D11Device *pDevice);
void ClearCellDensityAndAttenuationTextures(SRenderAttribs &RenderAttribs);
static const int sm_iCSThreadGroupSize = 128;
LPCTSTR m_strEffectPath;
LPCTSTR m_strPreprocessingEffectPath;
static const int sm_iTileSize = 16;
bool m_bPSOrderingAvailable;
SPrecomputedOpticalDepthTexDim m_PrecomputedOpticalDepthTexDim;
SPrecomputedScatteringInParticleTexDim m_PrecomputedSctrInParticleLUTDim;
D3DXVECTOR3 m_f3PrevLightDir;
UINT m_uiCloudDensityTexWidth, m_uiCloudDensityTexHeight;
UINT m_uiBackBufferWidth, m_uiBackBufferHeight;
CComPtr<ID3D11SamplerState> m_psamLinearWrap, m_psamPointWrap, m_psamLinearClamp;
SGlobalCloudAttribs m_CloudAttribs;
// Packed locations for all cells and particles
std::vector<UINT> m_PackedCellLocations, m_PackedParticleLocations;
CComPtr<ID3D11Buffer> m_pcbGlobalCloudAttribs;
// Render techniques
CRenderTechnique m_RenderCloudsTech[2], m_RenderFlatCloudsTech[2], m_CombineWithBBTech, m_RenderCloudDetphToShadowMap;
CRenderTechnique m_ProcessCloudGridTech, m_ComputeParticleVisibilityTech[2];
CRenderTechnique m_GenerateVisibleParticlesTech, m_ProcessVisibleParticlesTech;
CRenderTechnique m_EvaluateDensityTech, m_ComputeLightAttenuatingMass, m_Clear3DTexTech;
CRenderTechnique m_ComputeDispatchArgsTech[2];
CRenderTechnique m_ComputeOpticalDepthTech;
CRenderTechnique m_ApplyParticleLayersTech;
CRenderTechnique m_ComputeSingleSctrInParticleTech;
CRenderTechnique m_GatherPrevSctrOrderTech;
CRenderTechnique m_ComputeScatteringOrderTech;
CRenderTechnique m_AccumulateInscatteringTech;
CRenderTechnique m_RenderScatteringLUTSliceTech;
CRenderTechnique m_SortSubsequenceBitonicTech;
CRenderTechnique m_WriteSortedPariclesToVBTech;
CRenderTechnique m_MergeSubsequencesTech;
// States
CComPtr<ID3D11DepthStencilState> m_pdsEnableDepth, m_pdsDisableDepth;
CComPtr<ID3D11RasterizerState> m_prsSolidFillCullFront, m_prsSolidFillNoCull;
CComPtr<ID3D11BlendState> m_pbsDefault;
CComPtr<ID3D11BlendState> m_pbsRT0MulRT1MinRT2Over;
// 2D cloud density and noise textures
CComPtr<ID3D11ShaderResourceView> m_ptex2DCloudDensitySRV, m_ptex2DWhiteNoiseSRV;
// Maximum mip map pyramid
CComPtr<ID3D11ShaderResourceView> m_ptex2DMaxDensityMipMapSRV;
// 3D noise texture
CComPtr<ID3D11ShaderResourceView> m_ptex3DNoiseSRV;
// SRV and UAV for cloud grid
CComPtr<ID3D11ShaderResourceView> m_pbufCloudGridSRV;
CComPtr<ID3D11UnorderedAccessView> m_pbufCloudGridUAV;
// SRV and UAV for particle lattice
CComPtr<ID3D11UnorderedAccessView> m_pbufCloudParticlesUAV;
CComPtr<ID3D11ShaderResourceView> m_pbufCloudParticlesSRV;
CComPtr<ID3D11ShaderResourceView> m_pbufParticlesLightingSRV;
CComPtr<ID3D11UnorderedAccessView> m_pbufParticlesLightingUAV;
// Buffer containing unordered list of all valid cells
CComPtr<ID3D11Buffer> m_pbufValidCellsUnorderedList;
CComPtr<ID3D11UnorderedAccessView> m_pbufValidCellsUnorderedListUAV;
CComPtr<ID3D11ShaderResourceView> m_pbufValidCellsUnorderedListSRV;
CComPtr<ID3D11UnorderedAccessView> m_pbufVisibleCellsUnorderedListUAV;
CComPtr<ID3D11ShaderResourceView> m_pbufVisibleCellsUnorderedListSRV;
// Buffer containing number of valis cells or particles
CComPtr<ID3D11Buffer> m_pbufValidCellsCounter;
CComPtr<ID3D11ShaderResourceView> m_pbufValidCellsCounterSRV;
CComPtr<ID3D11Buffer> m_pbufVisibleParticlesCounter;
CComPtr<ID3D11ShaderResourceView> m_pbufVisibleParticlesCounterSRV;
CComPtr<ID3D11ShaderResourceView> m_ptex3DCellDensitySRV;
CComPtr<ID3D11UnorderedAccessView> m_ptex3DCellDensityUAV;
CComPtr<ID3D11ShaderResourceView> m_ptex3DLightAttenuatingMassSRV;
CComPtr<ID3D11UnorderedAccessView> m_ptex3DLightAttenuatingMassUAV;
CComPtr<ID3D11UnorderedAccessView> m_pbufVisibleParticlesUnorderedListUAV;
CComPtr<ID3D11ShaderResourceView> m_pbufVisibleParticlesUnorderedListSRV;
CComPtr<ID3D11UnorderedAccessView> m_pbufVisibleParticlesSortedListUAV, m_pbufVisibleParticlesMergedListUAV;
CComPtr<ID3D11ShaderResourceView> m_pbufVisibleParticlesSortedListSRV, m_pbufVisibleParticlesMergedListSRV;
// Buffer containing sorted list of VISIBLE particles only
CComPtr<ID3D11Buffer> m_pbufSerializedVisibleParticles;
CComPtr<ID3D11UnorderedAccessView> m_pbufSerializedVisibleParticlesUAV;
// Buffer used to store DispatchIndirect() arguments
CComPtr<ID3D11Buffer> m_pbufDispatchArgs;
CComPtr<ID3D11UnorderedAccessView> m_pbufDispatchArgsUAV;
// Buffer used to store DrawIndirect() arguments
CComPtr<ID3D11Buffer> m_pbufDrawIndirectArgs;
// SRV for the buffer containing packed cell locations
CComPtr<ID3D11ShaderResourceView> m_pbufPackedCellLocationsSRV;
// Cloud color, transparancy and distance buffer for camera space
CComPtr<ID3D11ShaderResourceView> m_ptex2DScreenCloudColorSRV;
CComPtr<ID3D11RenderTargetView> m_ptex2DScreenCloudColorRTV;
CComPtr<ID3D11ShaderResourceView> m_ptex2DScrSpaceCloudTransparencySRV, m_ptex2DScrSpaceDistToCloudSRV;
CComPtr<ID3D11RenderTargetView> m_ptex2DScrSpaceCloudTransparencyRTV, m_ptex2DScrSpaceDistToCloudRTV;
// Downscaled cloud color, transparancy and distance buffer for camera space
CComPtr<ID3D11ShaderResourceView> m_ptex2DDownscaledScrCloudColorSRV;
CComPtr<ID3D11RenderTargetView> m_ptex2DDownscaledScrCloudColorRTV;
CComPtr<ID3D11ShaderResourceView> m_ptex2DDownscaledScrCloudTransparencySRV, m_ptex2DDownscaledScrDistToCloudSRV;
CComPtr<ID3D11RenderTargetView> m_ptex2DDownscaledScrCloudTransparencyRTV, m_ptex2DDownscaledScrDistToCloudRTV;
CComPtr<ID3D11ShaderResourceView> m_pbufParticleLayersSRV;
CComPtr<ID3D11UnorderedAccessView> m_pbufParticleLayersUAV;
CComPtr<ID3D11Buffer> m_pbufClearParticleLayers;
CComPtr<ID3D11ShaderResourceView> m_ptex3DPrecomputedParticleDensitySRV;
CComPtr<ID3D11ShaderResourceView> m_ptex3DSingleSctrInParticleLUT_SRV;
CComPtr<ID3D11ShaderResourceView> m_ptex3DMultipleSctrInParticleLUT_SRV;
// Inpute layout for streamed out particles
CComPtr<ID3D11InputLayout> m_pRenderCloudsInputLayout;
};