-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScene.cpp
More file actions
312 lines (269 loc) · 12.4 KB
/
Scene.cpp
File metadata and controls
312 lines (269 loc) · 12.4 KB
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
#include "Scene.h"
#include "Vulkan.h"
#include <stdio.h>
#include "matrix4.h"
#include "quaternion.h"
#include "RenderPass.h"
#include "StaticMesh.h"
#include "SceneNode.h"
#define _4MB 4194304
//NOTO:Road
//init args/visBuffer64
//node and cluster cull
//cluster cull
//hw/sw
//cluster color(rgba32float)
//swapChain
static float sLODScale, sLODScaleHW;
static float4 sCameraPositionWS(-330.f, 330.f, -330.f), sCameraTargetPositionWS(0.f, 80.f ,0.f);
static matrix4 sProjectionMatrix, sViewMatrix, sModelMatrix;
static GlobalConstants sGlobalConstantsData;
static Buffer* sGlobalConstantsBuffer,* sBVHBuffer, *sEchoBuffer, *sWorkArgsBuffer[2], * sMainAndPostNodeAndClusterBatches,
*sVisBuffer64, *sNaniteMesh, *sVisiableClusterSHWH;
static Texture2D* sVisualizationTexture;
static RenderPass* sInitPass, *sNodeAndClusterCullPass[4], *sClusterCullPass, *sHWRasterizePass, *sVisualizePass;
static SceneNode* sFSQNode;
static int sCurrentMipLevelIndex = 0;
static int sAvaliableMipLevels[] = { 0 ,1, 2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 };
unsigned char* LoadFileContent(const char* inFilePath, size_t& outFileSize) {
FILE* file = nullptr;
errno_t err = fopen_s(&file, inFilePath, "rb");
if (err != 0) {
return nullptr;
}
fseek(file, 0, SEEK_END);
outFileSize = ftell(file);
fseek(file, 0, SEEK_SET);
unsigned char* fileContent = new unsigned char[outFileSize];
fread(fileContent, 1, outFileSize, file);
fclose(file);
return fileContent;
}
void InitScene(int inCanvasWidth, int inCanvasHeight) {
StaticMesh::Init();
sProjectionMatrix.Perspective(90.f, float(inCanvasWidth) / float(inCanvasHeight), 10.f, 10000.f);
sViewMatrix.LookAt(sCameraPositionWS, sCameraTargetPositionWS, float4(0.f, 1.f, 0.f));
matrix3 scaleMatrix;
scaleMatrix.LoadIdentity();
matrix3 lt3x3 = scaleMatrix * quaternion(180.f, 0.f, 0.f).toMatrix3();
sModelMatrix.LoadIdentity();
sModelMatrix.SetLeftTop3x3(lt3x3);
float ViewToPixels = 0.5f * sProjectionMatrix.v[5] * float(inCanvasHeight);
sLODScale = ViewToPixels / 1.f;
sLODScaleHW = ViewToPixels / 32.f;
sGlobalConstantsData.SetProjectionMatrix(sProjectionMatrix.v);
sGlobalConstantsData.SetViewMatrix(sViewMatrix.v);
sGlobalConstantsData.SetModelMatrix(sModelMatrix.v);
sGlobalConstantsData.SetMisc0(sAvaliableMipLevels[sCurrentMipLevelIndex], 0, 0, 0);
float4 viewDirection = sCameraTargetPositionWS - sCameraPositionWS;
viewDirection.Normalize();
sGlobalConstantsData.SetCameraPositionWS(sCameraPositionWS.x, sCameraPositionWS.y, sCameraPositionWS.z, sLODScale);
sGlobalConstantsData.SetCameraViewDirectionWS(viewDirection.x, viewDirection.y, viewDirection.z, sLODScaleHW);
sGlobalConstantsBuffer = GenBufferObject(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, nullptr, 65536);
SetObjectName(VK_OBJECT_TYPE_BUFFER, sGlobalConstantsBuffer->mBuffer, "GlobalConstants");
{
sVisualizationTexture = new Texture2D;
sVisualizationTexture->mFormat = VK_FORMAT_R32G32B32A32_SFLOAT;
sVisualizationTexture->mImageAspectFlag = VK_IMAGE_ASPECT_COLOR_BIT;
GenImage(sVisualizationTexture, inCanvasWidth, inCanvasHeight,
VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
sVisualizationTexture->mImageView = GenImageView2D(sVisualizationTexture->mImage, sVisualizationTexture->mFormat, sVisualizationTexture->mImageAspectFlag);
sVisualizationTexture->mWidth = inCanvasWidth;
sVisualizationTexture->mHeight = inCanvasHeight;
sVisualizationTexture->mChannelCount = 4;
SetObjectName(VK_OBJECT_TYPE_IMAGE, sVisualizationTexture->mImage, "VisualizationTexture");
}
sEchoBuffer = GenBufferObject(VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, nullptr, _4MB);
SetObjectName(VK_OBJECT_TYPE_BUFFER, sEchoBuffer->mBuffer, "EchoBuffer");
sVisBuffer64 = GenBufferObject(VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, nullptr, inCanvasWidth*inCanvasHeight*sizeof(uint64_t));
SetObjectName(VK_OBJECT_TYPE_BUFFER, sVisBuffer64->mBuffer, "VisBuffer64");
sWorkArgsBuffer[0] = GenBufferObject(
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, nullptr, _4MB);
SetObjectName(VK_OBJECT_TYPE_BUFFER, sWorkArgsBuffer[0]->mBuffer, "WorkArgsBuffer[0]");
sWorkArgsBuffer[1] = GenBufferObject(
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, nullptr, _4MB);
SetObjectName(VK_OBJECT_TYPE_BUFFER, sWorkArgsBuffer[1]->mBuffer, "WorkArgsBuffer[1]");
sMainAndPostNodeAndClusterBatches = GenBufferObject(VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, nullptr, _4MB);
SetObjectName(VK_OBJECT_TYPE_BUFFER, sMainAndPostNodeAndClusterBatches->mBuffer, "MainAndPostNodeAndClusterBatches");
sVisiableClusterSHWH = GenBufferObject(VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, nullptr, _4MB);
SetObjectName(VK_OBJECT_TYPE_BUFFER, sVisiableClusterSHWH->mBuffer, "VisiableClusterSHWH");
{
size_t fileSize = 0;
unsigned char* fileContent = LoadFileContent("Res/mitsuba.bvh", fileSize);
sBVHBuffer = GenBufferObject(VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, fileContent, fileSize);
delete[] fileContent;
SetObjectName(VK_OBJECT_TYPE_BUFFER, sBVHBuffer->mBuffer, "HierarchyBuffer");
fileContent = LoadFileContent("Res/mitsuba.nanitemesh", fileSize);
sNaniteMesh = GenBufferObject(VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, fileContent, fileSize);
delete[] fileContent;
SetObjectName(VK_OBJECT_TYPE_BUFFER, sBVHBuffer->mBuffer, "NaniteMesh");
}
//InitPass ->(Unreal) RasterClear
{
sInitPass = new RenderPass(ERenderPassType::ERPT_COMPUTE, "Init");
sInitPass->SetSSBO(0, sWorkArgsBuffer[0], true);
sInitPass->SetSSBO(1, sWorkArgsBuffer[1], true);
sInitPass->SetSSBO(2, sMainAndPostNodeAndClusterBatches, true);
sInitPass->SetSSBO(3, sVisBuffer64, true);
sInitPass->SetCS("Res/Shaders/Init.spv");
sInitPass->SetComputeDispatchArgs(int(ceilf(float(inCanvasWidth) / 8.f)), int(ceilf(float(inCanvasHeight) / 8.f)), 1);
sInitPass->Build();
}
//node and cluster cull
for (int i = 0; i < 4; i++) {
char szName[128] = { 0 };
sprintf_s(szName, "NodeAndClusterCull_%d", i);
int inputWorkArgsIndex = i % 2;
int outputWorkArgsIndex = (i + 1) % 2;
sNodeAndClusterCullPass[i] = new RenderPass(ERenderPassType::ERPT_COMPUTE, szName);
sNodeAndClusterCullPass[i]->SetSSBO(0, sBVHBuffer);
sNodeAndClusterCullPass[i]->SetSSBO(1, sEchoBuffer, true);
sNodeAndClusterCullPass[i]->SetSSBO(2, sMainAndPostNodeAndClusterBatches, true);
sNodeAndClusterCullPass[i]->SetSSBO(3, sWorkArgsBuffer[inputWorkArgsIndex]);
sNodeAndClusterCullPass[i]->SetSSBO(4, sWorkArgsBuffer[outputWorkArgsIndex], true);
sNodeAndClusterCullPass[i]->SetUniformBufferObject(5, sGlobalConstantsBuffer);
sNodeAndClusterCullPass[i]->SetSSBO(6, sNaniteMesh);
sNodeAndClusterCullPass[i]->SetCS("Res/Shaders/NodeAndClusterCull.spv");
sNodeAndClusterCullPass[i]->SetComputeDispatchArgs(1, 1, 1);
sNodeAndClusterCullPass[i]->Build();
}
//ClusterCull Pass
{
sClusterCullPass = new RenderPass(ERenderPassType::ERPT_COMPUTE, "ClusterCull");
sClusterCullPass->SetUniformBufferObject(0, sGlobalConstantsBuffer);
sClusterCullPass->SetSSBO(1, sMainAndPostNodeAndClusterBatches);
sClusterCullPass->SetSSBO(2, sVisiableClusterSHWH, true);
sClusterCullPass->SetSSBO(3, sWorkArgsBuffer[0]);
sClusterCullPass->SetSSBO(4, sNaniteMesh);
sClusterCullPass->SetCS("Res/Shaders/ClusterCull.spv");
sClusterCullPass->SetComputeDispatchArgs(1, 1, 1);
sClusterCullPass->Build();
}
//HWRasterize Pass
{
sHWRasterizePass = new RenderPass(ERenderPassType::ERPT_GRAPHICS, "HWRasterize");
sHWRasterizePass->SetUniformBufferObject(0, sGlobalConstantsBuffer);
sHWRasterizePass->SetSSBO(1, sNaniteMesh);
sHWRasterizePass->SetSSBO(2, sVisiableClusterSHWH);
sHWRasterizePass->SetSSBO(3, sVisBuffer64, true);
sHWRasterizePass->SetVSPS(
"Res/Shaders/HWRasterizeVS.spv",
"Res/Shaders/HWRasterizeFS.spv");
sHWRasterizePass->Build(inCanvasWidth, inCanvasHeight);
}
//Visualize Pass
{
sVisualizePass = new RenderPass(ERenderPassType::ERPT_COMPUTE, "Visualize");
sVisualizePass->SetSSBO(0, sVisBuffer64);
sVisualizePass->SetComputeImage(1, sVisualizationTexture, true);
sVisualizePass->SetCS("Res/Shaders/Visualize.spv");
sVisualizePass->SetComputeDispatchArgs(int(ceilf(float(inCanvasWidth) / 8.f)), int(ceilf(float(inCanvasHeight) / 8.f)), 1);
sVisualizePass->Build();
}
{
sFSQNode = new SceneNode;
StaticMesh* staticMesh = new StaticMesh;
staticMesh->SetVertexCount(4);
staticMesh->SetPosition(0, -1.f, -1.f, 0.f, 1.f);
staticMesh->SetTexcoord(0, 0.f, 0.f, 0.f, 0.f);
staticMesh->SetPosition(1, 1.f, -1.f, 0.f, 1.f);
staticMesh->SetTexcoord(1, 1.f, 0.f, 0.f, 0.f);
staticMesh->SetPosition(2, -1.f, 1.f, 0.f, 1.f);
staticMesh->SetTexcoord(2, 0.f, 1.f, 0.f, 0.f);
staticMesh->SetPosition(3, 1.f, 1.f, 0.f, 1.f);
staticMesh->SetTexcoord(3, 1.f, 1.f, 0.f, 0.f);
staticMesh->mVBO = GenBufferObject(VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, staticMesh->mVertexData, sizeof(StaticMeshVertexData) * 4);
sFSQNode->mStaticMesh = staticMesh;
staticMesh->mMaterial.Init("Res/Shaders/SwapChainVS.spv","Res/Shaders/SwapChainFS.spv");
staticMesh->mMaterial.mPrimitiveType = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
staticMesh->mMaterial.SetTexture2D(2, 0,sVisualizationTexture->mImageView, GenSampler());
}
}
void RenderOneFrame(float inFrameTimeInSecond) {
float speed = 400.0f;
float dt = inFrameTimeInSecond;
float worldUp[3] = { 0,1,0 };
// Right = normalize(cross(ViewDir, Up))
float right[3] = {
sGlobalConstantsData.mViewDirectionWS[1] * worldUp[2] -
sGlobalConstantsData.mViewDirectionWS[2] * worldUp[1],
sGlobalConstantsData.mViewDirectionWS[2] * worldUp[0] -
sGlobalConstantsData.mViewDirectionWS[0] * worldUp[2],
sGlobalConstantsData.mViewDirectionWS[0] * worldUp[1] -
sGlobalConstantsData.mViewDirectionWS[1] * worldUp[0],
};
float len = sqrtf(right[0] * right[0] + right[1] * right[1] + right[2] * right[2]);
right[0] /= len; right[1] /= len; right[2] /= len;
if (GetAsyncKeyState('W'))
for (int i = 0; i < 3; i++)
sGlobalConstantsData.mCameraPositionWS[i] += sGlobalConstantsData.mViewDirectionWS[i] * speed * dt;
if (GetAsyncKeyState('S'))
for (int i = 0; i < 3; i++)
sGlobalConstantsData.mCameraPositionWS[i] -= sGlobalConstantsData.mViewDirectionWS[i] * speed * dt;
if (GetAsyncKeyState('A'))
for (int i = 0; i < 3; i++)
sGlobalConstantsData.mCameraPositionWS[i] -= right[i] * speed * dt;
if (GetAsyncKeyState('D'))
for (int i = 0; i < 3; i++)
sGlobalConstantsData.mCameraPositionWS[i] += right[i] * speed * dt;
BufferSubData(sGlobalConstantsBuffer, &sGlobalConstantsData, sizeof(GlobalConstants));
sInitPass->Execute();
for (int i = 0; i < 4; i++) {
sNodeAndClusterCullPass[i]->Execute();
}
sClusterCullPass->Execute();
sHWRasterizePass->ExecuteIndirect(sWorkArgsBuffer[0]);
sVisualizePass->Execute();
VkCommandBuffer commandBuffer = CreateCommandBuffer();
BeginCommandBuffer(commandBuffer, VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT);
{
static bool bIsConverted = false;
if (bIsConverted == false)
{
bIsConverted = true;
VkImageSubresourceRange imageSubresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT ,0,1,0,1 };
TransferImageLayout(commandBuffer, sVisualizationTexture->mImage, imageSubresourceRange,
VK_IMAGE_LAYOUT_UNDEFINED, VK_ACCESS_NONE,
VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_ACCESS_SHADER_READ_BIT,
VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
}
SCOPED_EVENT(commandBuffer, "SwapChain");
BeginSwapChainRenderPass(commandBuffer);
//ShaderParameterDescription* parameterDescription = GetUberPassShaderParameterDescription();
sFSQNode->Draw(commandBuffer, GetSwapChainRenderPass(), sProjectionMatrix, sViewMatrix);
}
EndSwapChainRenderPass(commandBuffer);
}
void OnKeyUp(unsigned int inKeyCode)
{
if (inKeyCode == VK_UP)
{
printf("up arrow\n");
sCurrentMipLevelIndex++;
if (sCurrentMipLevelIndex == _countof(sAvaliableMipLevels))
{
sCurrentMipLevelIndex = 0;
}
}
else if (inKeyCode == VK_DOWN)
{
printf("down arrow\n");
sCurrentMipLevelIndex--;
if (sCurrentMipLevelIndex < 0)
{
sCurrentMipLevelIndex = _countof(sAvaliableMipLevels) - 1;
}
}
sGlobalConstantsData.SetMisc0(sAvaliableMipLevels[sCurrentMipLevelIndex], 0, 0, 0);
}