Pārlūkot izejas kodu

매 프레임 bindSet (ubo) 갱신

Hongtae Kim 5 gadus atpakaļ
vecāks
revīzija
656f5ca2c2
1 mainītis faili ar 71 papildinājumiem un 20 dzēšanām
  1. 71
    20
      Samples/Mesh/Mesh.cpp

+ 71
- 20
Samples/Mesh/Mesh.cpp Parādīt failu

@@ -293,37 +293,46 @@ public:
293 293
             layout.bindings.Add(bindings, 2);
294 294
         }
295 295
         DKObject<DKShaderBindingSet> bindSet = device->CreateShaderBindingSet(layout);
296
+
297
+        struct UBO
298
+        {
299
+            DKMatrix4 projectionMatrix;
300
+            DKMatrix4 modelMatrix;
301
+            DKMatrix4 viewMatrix;
302
+        };
303
+        DKObject<DKGpuBuffer> uboBuffer = device->CreateBuffer(sizeof(UBO), DKGpuBuffer::StorageModeShared, DKCpuCacheModeReadWrite);
304
+        UBO* ubo = nullptr;
296 305
         if (bindSet)
297 306
         {
298
-            struct
299
-            {
300
-                DKMatrix4 projectionMatrix;
301
-                DKMatrix4 modelMatrix;
302
-                DKMatrix4 viewMatrix;
303
-            } ubo;
304
-
305
-            DKObject<DKGpuBuffer> uboBuffer = device->CreateBuffer(sizeof(ubo), DKGpuBuffer::StorageModeShared, DKCpuCacheModeReadWrite);
306 307
             if (uboBuffer)
307 308
             {
308
-                ubo.projectionMatrix = DKMatrix4::identity;
309
-                ubo.modelMatrix = DKMatrix4::identity;
310
-                ubo.viewMatrix = DKMatrix4::identity;
309
+                ubo = reinterpret_cast<UBO*>(uboBuffer->Contents());
310
+                ubo->projectionMatrix = DKMatrix4::identity;
311
+                ubo->modelMatrix = DKMatrix4::identity;
312
+                ubo->viewMatrix = DKMatrix4::identity;
313
+                uboBuffer->Flush();
311 314
 
312
-                DKAffineTransform3 trans;
313
-                trans.Multiply(DKLinearTransform3().Scale(0.25).Rotate(DKVector3(1,1,1), DKGL_PI * 0.25));
314
-                ubo.modelMatrix.Multiply(trans.Matrix4());
315
+                //DKAffineTransform3 trans;
316
+                //trans.Multiply(DKLinearTransform3().Scale(0.5).Rotate(DKVector3(0,1,0), DKGL_PI * 0.5));
317
+                //ubo.modelMatrix.Multiply(trans.Matrix4());
315 318
 
316
-                memcpy(uboBuffer->Contents(), &ubo, sizeof(ubo));
319
+                //memcpy(uboBuffer->Contents(), &ubo, sizeof(ubo));
317 320
                 bindSet->SetBuffer(0, uboBuffer, 0, sizeof(ubo));
318
-                uboBuffer->Flush();
319 321
             }
320 322
 			         
321
-
322 323
             bindSet->SetTexture(1, texture);
323 324
             bindSet->SetSamplerState(1, sampler);
324 325
         }
325 326
 
326
-		DKTimer timer;
327
+        DKObject<DKTexture> depthBuffer = nullptr;
328
+
329
+        DKCamera camera;
330
+        DKVector3 cameraPosition = { 0, 0, 10 };
331
+        DKVector3 cameraTartget = { 0, 0, 0 };
332
+
333
+        DKAffineTransform3 tm(DKLinearTransform3().Scale(4));
334
+
335
+        DKTimer timer;
327 336
 		timer.Reset();
328 337
 
329 338
 		DKLog("Render thread begin");
@@ -331,13 +340,55 @@ public:
331 340
 		{
332 341
 			DKRenderPassDescriptor rpd = swapChain->CurrentRenderPassDescriptor();
333 342
 			double t = timer.Elapsed();
334
-			t = (cos(t) + 1.0) * 0.5;
335
-			rpd.colorAttachments.Value(0).clearColor = DKColor(t, 0.0, 0.0, 0.0);
343
+			double waveT = (cos(t) + 1.0) * 0.5;
344
+			rpd.colorAttachments.Value(0).clearColor = DKColor(waveT, 0.0, 0.0, 0.0);
345
+
346
+            int width = rpd.colorAttachments.Value(0).renderTarget->Width();
347
+            int height = rpd.colorAttachments.Value(0).renderTarget->Height();
348
+            if (depthBuffer)
349
+            {
350
+                if (depthBuffer->Width() !=  width ||
351
+                    depthBuffer->Height() != height )
352
+                    depthBuffer = nullptr;
353
+            }
354
+            if (depthBuffer == nullptr)
355
+            {
356
+                // create depth buffer
357
+                DKTextureDescriptor texDesc = {};
358
+                texDesc.textureType = DKTexture::Type2D;
359
+                texDesc.pixelFormat = DKPixelFormat::D32Float;
360
+                texDesc.width = width;
361
+                texDesc.height = height;
362
+                texDesc.depth = 1;
363
+                texDesc.mipmapLevels = 1;
364
+                texDesc.sampleCount = 1;
365
+                texDesc.arrayLength = 1;
366
+                texDesc.usage = DKTexture::UsageShaderWrite | DKTexture::UsageRenderTarget;
367
+                depthBuffer = device->CreateTexture(texDesc);
368
+            }
369
+            rpd.depthStencilAttachment.renderTarget = depthBuffer;
370
+            rpd.depthStencilAttachment.loadAction = DKRenderPassAttachmentDescriptor::LoadActionClear;
371
+            rpd.depthStencilAttachment.storeAction = DKRenderPassAttachmentDescriptor::StoreActionDontCare;
336 372
 
337 373
 			DKObject<DKCommandBuffer> buffer = queue->CreateCommandBuffer();
338 374
 			DKObject<DKRenderCommandEncoder> encoder = buffer->CreateRenderCommandEncoder(rpd);
339 375
 			if (encoder)
340 376
 			{
377
+                if (bindSet && ubo)
378
+                {
379
+                    camera.SetView(cameraPosition, cameraTartget - cameraPosition, DKVector3(0, 1, 0));
380
+                    camera.SetPerspective(DKGL_DEGREE_TO_RADIAN(90), float(width)/float(height), 1, 1000);
381
+
382
+                    ubo->projectionMatrix = camera.ProjectionMatrix();
383
+                    ubo->viewMatrix = camera.ViewMatrix();
384
+
385
+                    DKQuaternion quat(DKVector3(0, 1, 0), t);
386
+                    DKAffineTransform3 trans = tm * DKAffineTransform3(quat);
387
+                    ubo->modelMatrix = trans.Matrix4();
388
+                    uboBuffer->Flush();
389
+                    bindSet->SetBuffer(0, uboBuffer, 0, sizeof(ubo));
390
+                }
391
+
341 392
 				encoder->SetRenderPipelineState(pipelineState);
342 393
 				encoder->SetVertexBuffer(vertexBuffer, 0, 0);
343 394
 				encoder->SetIndexBuffer(indexBuffer, 0, DKIndexType::UInt32);