/
Rendering.cpp
358 lines (284 loc) · 13.4 KB
/
Rendering.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
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
/*****************************************************************************/
/*
CS391 Project 3 Profiler Scenario
This is adapted from Cody Pritchard's PHY350/399 Fluid Simulation Work
This sample provides a nice, juicy, CPU intensive application to test
your profiler on for project 3.
*/
/*****************************************************************************/
#include "DXUT.h"
#include "DXUTgui.h"
#include "SDKmisc.h"
#include "Fluid.h"
#include "render_vs.h"
#include "render_gs.h"
#include "render_ps.h"
#pragma warning(disable:4100) // warning C4100: 'x' : unreferenced formal parameter
extern Fluid* FLUID;
struct ParticleVertex {
DirectX::XMFLOAT2 position;
DirectX::XMFLOAT4 color;
};
struct ConstantBuffer {
DirectX::XMFLOAT4X4A mViewProjection;
FLOAT fParticleSize;
};
//--------------------------------------------------------------------------------------
// Global variables
//--------------------------------------------------------------------------------------
CDXUTDialogResourceManager* g_pDialogResourceManager = NULL;
CDXUTTextHelper* g_pTxtHelper = NULL;
CDXUTDialog* g_pHUD = NULL;
const float PARTICLE_SIZE = 0.005f;
const unsigned int FluidVertexBufferSize = 4096;
ID3D11VertexShader* g_pVertexShader = NULL;
ID3D11GeometryShader* g_pGeometryShader = NULL;
ID3D11PixelShader* g_pPixelShader = NULL;
ID3D11Buffer* g_pVertexBuffer = NULL;
ID3D11InputLayout* g_pVertexLayout = NULL;
ID3D11Buffer* g_pConstantBuffer = NULL;
//--------------------------------------------------------------------------------------
// UI control IDs
//--------------------------------------------------------------------------------------
#define IDC_TOGGLEFULLSCREEN 1
#define IDC_RESETSIMULATION 2
#define IDC_PAUSESIMULATION 3
#define IDC_PAUSESIMULATIONONSTEP600 4
#define IDC_SIMULATIONSIZE 5
#define IDC_SIMULATIONSIZE_TEXT 6
//--------------------------------------------------------------------------------------
// Forward declarations
//--------------------------------------------------------------------------------------
HRESULT CALLBACK OnCreateDevice( ID3D11Device* pd3dDevice, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext );
HRESULT CALLBACK OnResizedSwapChain( ID3D11Device* pd3dDevice, IDXGISwapChain* pSwapChain, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext );
void CALLBACK OnFrameMove( double fTime, float fElapsedTime, void* pUserContext );
void CALLBACK OnFrameRender( ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3dImmediateContext, double fTime, float fElapsedTime, void* pUserContext );
void CALLBACK OnDestroyDevice( void* pUserContext );
LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, bool* pbNoFurtherProcessing, void* pUserContext );
void CALLBACK OnGUIEvent( UINT nEvent, int nControlID, CDXUTControl* pControl, void* pUserContext );
void InitApp();
void DestroyApp();
void RenderFluid( ID3D11DeviceContext* pd3dImmediateContext );
void RenderText( double fTime );
//--------------------------------------------------------------------------------------
// Initialization
//--------------------------------------------------------------------------------------
void InitializeDirectX( )
{
DXUTSetCallbackD3D11DeviceCreated( OnCreateDevice );
DXUTSetCallbackD3D11FrameRender( OnFrameRender );
DXUTSetCallbackD3D11DeviceDestroyed( OnDestroyDevice );
DXUTSetCallbackD3D11SwapChainResized( OnResizedSwapChain );
DXUTSetCallbackMsgProc( MsgProc );
DXUTSetCallbackFrameMove( OnFrameMove );
DXUTSetCursorSettings( true, true );
InitApp();
DXUTInit( true, true );
DXUTCreateWindow( L"CS391: Project 3" );
DXUTCreateDevice( D3D_FEATURE_LEVEL_10_0, true, 640, 480 );
}
//--------------------------------------------------------------------------------------
// Initialization
//--------------------------------------------------------------------------------------
int RunDirectX( )
{
DXUTMainLoop();
DestroyApp();
return DXUTGetExitCode();
}
//--------------------------------------------------------------------------------------
// Initialization
//--------------------------------------------------------------------------------------
void InitApp()
{
// Initialize dialogs
g_pDialogResourceManager = new CDXUTDialogResourceManager();
g_pHUD = new CDXUTDialog();
g_pHUD->Init( g_pDialogResourceManager );
// HUD
g_pHUD->SetCallback( OnGUIEvent );
int iY = 10;
g_pHUD->AddButton( IDC_TOGGLEFULLSCREEN, L"Toggle full screen", 35, iY, 155, 22 );
g_pHUD->AddButton( IDC_RESETSIMULATION, L"Reset Simulation", 35, iY += 24, 155, 22 );
g_pHUD->AddCheckBox( IDC_PAUSESIMULATION, L"Pause Simulation", 35, iY += 24, 155, 22 );
g_pHUD->AddCheckBox( IDC_PAUSESIMULATIONONSTEP600, L"Pause On Step 600", 35, iY += 24, 155, 22 );
g_pHUD->AddStatic( IDC_SIMULATIONSIZE_TEXT, L"Simulation Size:", 35, iY += 24, 155, 22 );
g_pHUD->AddSlider( IDC_SIMULATIONSIZE, 35, iY += 24, 155, 22, 1, 8, 5 );
}
//--------------------------------------------------------------------------------------
// Initialization
//--------------------------------------------------------------------------------------
HRESULT CALLBACK OnCreateDevice( ID3D11Device* pd3dDevice, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
{
HRESULT hr;
ID3D11DeviceContext* pd3dImmediateContext = DXUTGetD3D11DeviceContext();
V_RETURN( g_pDialogResourceManager->OnD3D11CreateDevice( pd3dDevice, pd3dImmediateContext ) );
g_pTxtHelper = new CDXUTTextHelper( pd3dDevice, pd3dImmediateContext, g_pDialogResourceManager, 15 );
V_RETURN( pd3dDevice->CreateVertexShader( g_ParticleVS, sizeof(g_ParticleVS), NULL, &g_pVertexShader ) );
V_RETURN( pd3dDevice->CreateGeometryShader( g_ParticleGS, sizeof(g_ParticleGS), NULL, &g_pGeometryShader ) );
V_RETURN( pd3dDevice->CreatePixelShader( g_ParticlePS, sizeof(g_ParticlePS), NULL, &g_pPixelShader ) );
// Create VB
D3D11_BUFFER_DESC bufferDesc;
ZeroMemory( &bufferDesc, sizeof(bufferDesc) );
bufferDesc.ByteWidth = sizeof(ParticleVertex) * FluidVertexBufferSize;
bufferDesc.Usage = D3D11_USAGE_DYNAMIC;
bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
V_RETURN( pd3dDevice->CreateBuffer( &bufferDesc, NULL, &g_pVertexBuffer ) );
bufferDesc.ByteWidth = sizeof(ConstantBuffer);
bufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
V_RETURN( pd3dDevice->CreateBuffer( &bufferDesc, NULL, &g_pConstantBuffer ) );
// Create the IA layout
D3D11_INPUT_ELEMENT_DESC layout[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 8, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
V_RETURN( pd3dDevice->CreateInputLayout( layout, _countof(layout), g_ParticleVS, sizeof(g_ParticleVS), &g_pVertexLayout ) );
return S_OK;
}
//--------------------------------------------------------------------------------------
// Create any D3D11 resources that depend on the back buffer
//--------------------------------------------------------------------------------------
HRESULT CALLBACK OnResizedSwapChain( ID3D11Device* pd3dDevice, IDXGISwapChain* pSwapChain, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
{
HRESULT hr;
V_RETURN( g_pDialogResourceManager->OnD3D11ResizedSwapChain( pd3dDevice, pBackBufferSurfaceDesc ) );
g_pHUD->SetLocation( pBackBufferSurfaceDesc->Width - 200, 0 );
g_pHUD->SetSize( 200, 170 );
return S_OK;
}
//--------------------------------------------------------------------------------------
// Render
//--------------------------------------------------------------------------------------
void CALLBACK OnFrameRender( ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3dImmediateContext, double fTime, float fElapsedTime, void* pUserContext )
{
// Clear the render target and depth stencil
auto pRTV = DXUTGetD3D11RenderTargetView();
pd3dImmediateContext->ClearRenderTargetView( pRTV, DirectX::Colors::Gray );
auto pDSV = DXUTGetD3D11DepthStencilView();
pd3dImmediateContext->ClearDepthStencilView( pDSV, D3D11_CLEAR_DEPTH, 1.0, 0 );
pd3dImmediateContext->OMSetRenderTargets( 1, &pRTV, pDSV );
// Fluid
RenderFluid( pd3dImmediateContext );
// HUD
g_pHUD->OnRender( fElapsedTime );
// FPS and Statistics
RenderText( fTime );
}
//--------------------------------------------------------------------------------------
// Render the Fluid
//--------------------------------------------------------------------------------------
void RenderFluid( ID3D11DeviceContext* pd3dImmediateContext )
{
DirectX::XMMATRIX proj = DirectX::XMMatrixOrthographicOffCenterLH(0, FLUID->Width(), FLUID->Height(), 0, 0, 1);
proj = DirectX::XMMatrixTranspose(proj);
D3D11_MAPPED_SUBRESOURCE MappedResource = {};
pd3dImmediateContext->Map( g_pConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource );
ConstantBuffer* pCB = reinterpret_cast<ConstantBuffer*>(MappedResource.pData);
DirectX::XMStoreFloat4x4A( &pCB->mViewProjection, proj );
pCB->fParticleSize = PARTICLE_SIZE;
pd3dImmediateContext->Unmap( g_pConstantBuffer, 0 );
pd3dImmediateContext->VSSetShader( g_pVertexShader, NULL, 0 );
pd3dImmediateContext->GSSetShader( g_pGeometryShader, NULL, 0 );
pd3dImmediateContext->PSSetShader( g_pPixelShader, NULL, 0 );
pd3dImmediateContext->GSSetConstantBuffers(0, 1, &g_pConstantBuffer);
pd3dImmediateContext->IASetInputLayout( g_pVertexLayout );
pd3dImmediateContext->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_POINTLIST );
for (unsigned int i = 0; i < FLUID->Size(); i += FluidVertexBufferSize)
{
pd3dImmediateContext->Map(g_pVertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource);
ParticleVertex* pVertices = reinterpret_cast<ParticleVertex*>(MappedResource.pData);
// Render each particle
unsigned int j = 0;
for (; j < FluidVertexBufferSize && i + j < FLUID->Size(); j++) {
float color = (float)(0.5f * FLUID->density[i + j] / FluidRestDensity);
color = (color > 1.0f) ? 1.0f : color;
DirectX::XMFLOAT4 vColor(1.0f - color, 1.0f - color, 1.0f, 1.0f);
DirectX::XMFLOAT2 vPosition(FLUID->pos[i + j].x, FLUID->pos[i + j].y);
pVertices[j].position = vPosition;
pVertices[j].color = vColor;
}
pd3dImmediateContext->Unmap(g_pVertexBuffer, 0);
UINT stride = sizeof(ParticleVertex);
UINT offset = 0;
pd3dImmediateContext->IASetVertexBuffers(0, 1, &g_pVertexBuffer, &stride, &offset);
pd3dImmediateContext->Draw(j, 0);
}
}
//--------------------------------------------------------------------------------------
// Render the FPS and statistics text
//--------------------------------------------------------------------------------------
void RenderText( double fTime )
{
// Output statistics
g_pTxtHelper->Begin();
g_pTxtHelper->SetInsertionPos( 2, 0 );
g_pTxtHelper->SetForegroundColor( DirectX::Colors::White );
g_pTxtHelper->DrawTextLine( DXUTGetFrameStats( DXUTIsVsyncEnabled() ) );
g_pTxtHelper->DrawTextLine( DXUTGetDeviceStats() );
g_pTxtHelper->SetForegroundColor( DirectX::Colors::White );
g_pTxtHelper->DrawFormattedTextLine( L"Particles: %i", FLUID->Size() );
g_pTxtHelper->DrawFormattedTextLine( L"Step: %i", FLUID->Step() );
g_pTxtHelper->End();
}
//--------------------------------------------------------------------------------------
// Handles the input events
//--------------------------------------------------------------------------------------
LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, bool* pbNoFurtherProcessing, void* pUserContext )
{
// Always allow dialog resource manager calls to handle global messages
// so GUI state is updated correctly
*pbNoFurtherProcessing = g_pDialogResourceManager->MsgProc( hWnd, uMsg, wParam, lParam );
if( *pbNoFurtherProcessing )
return 0;
*pbNoFurtherProcessing = g_pHUD->MsgProc( hWnd, uMsg, wParam, lParam );
if( *pbNoFurtherProcessing )
return 0;
return 0;
}
//--------------------------------------------------------------------------------------
// Handles the GUI events
//--------------------------------------------------------------------------------------
void CALLBACK OnGUIEvent( UINT nEvent, int nControlID, CDXUTControl* pControl, void* pUserContext )
{
switch( nControlID )
{
case IDC_TOGGLEFULLSCREEN:
DXUTToggleFullScreen(); break;
case IDC_PAUSESIMULATION:
FLUID->Pause( g_pHUD->GetCheckBox( IDC_PAUSESIMULATION )->GetChecked() ); break;
case IDC_PAUSESIMULATIONONSTEP600:
FLUID->PauseOnStep( (g_pHUD->GetCheckBox( IDC_PAUSESIMULATIONONSTEP600 )->GetChecked())? 600 : 0xFFFFFFFF ); break;
case IDC_SIMULATIONSIZE:
// fallthrough
case IDC_RESETSIMULATION:
FLUID->Fill( g_pHUD->GetSlider( IDC_SIMULATIONSIZE )->GetValue() / 10.0f );
break;
}
}
//--------------------------------------------------------------------------------------
// Cleanup
//--------------------------------------------------------------------------------------
void CALLBACK OnDestroyDevice( void* pUserContext )
{
if( g_pDialogResourceManager )
g_pDialogResourceManager->OnD3D11DestroyDevice();
SAFE_DELETE( g_pTxtHelper );
SAFE_RELEASE( g_pVertexShader );
SAFE_RELEASE( g_pGeometryShader );
SAFE_RELEASE( g_pPixelShader );
SAFE_RELEASE( g_pVertexBuffer );
SAFE_RELEASE( g_pVertexLayout );
SAFE_RELEASE( g_pConstantBuffer );
}
//--------------------------------------------------------------------------------------
// Cleanup
//--------------------------------------------------------------------------------------
void DestroyApp()
{
if( g_pDialogResourceManager )
g_pDialogResourceManager->OnD3D11DestroyDevice();
SAFE_DELETE( g_pDialogResourceManager );
SAFE_DELETE( g_pHUD );
}