Przeglądaj źródła

ComputeShader 리소스 추가 (1024x1024 텍스쳐)

ComputeShader ThreadingGroup 적용.
Hongtae Kim 5 lat temu
rodzic
commit
a69fa338ef

+ 41
- 73
Samples/ComputeShader/ComputeShader.cpp Wyświetl plik

@@ -13,10 +13,10 @@ private:
13 13
 
14 14
     DKArray<UVQuad::Vertex> vertices =
15 15
     {
16
-        { { 1.0f, 1.0f, 0.0f }, { 1.0f, 0.0f } },
17
-        { { -1.0f, 1.0f, 0.0f }, { 0.0f, 0.0f } },
16
+        { {  1.0f,  1.0f, 0.0f }, { 1.0f, 0.0f } },
17
+        { { -1.0f,  1.0f, 0.0f }, { 0.0f, 0.0f } },
18 18
         { { -1.0f, -1.0f, 0.0f }, { 0.0f, 1.0f } },
19
-        { { 1.0f, -1.0f, 0.0f }, { 1.0f, 1.0f } }
19
+        { {  1.0f, -1.0f, 0.0f }, { 1.0f, 1.0f } }
20 20
     };
21 21
 
22 22
     DKArray<uint32_t> indices = { 0,1,2,2,3,0 };
@@ -54,45 +54,6 @@ public:
54 54
     }
55 55
 };
56 56
 
57
-class TextureComputeTarget
58
-{
59
-private:
60
-    DKObject<DKTexture> textureTarget = nullptr;
61
-
62
-public:
63
-    TextureComputeTarget() = default;
64
-
65
-    DKTexture* ComputeTarget(DKCommandQueue* queue, int w, int h)
66
-    {
67
-        auto device = queue->Device();
68
-
69
-        if (textureTarget)
70
-        {
71
-            if (textureTarget->Width() != w ||
72
-                textureTarget->Height() != h)
73
-                textureTarget = nullptr;
74
-        }
75
-
76
-        if (textureTarget == nullptr)
77
-        {
78
-            DKTextureDescriptor texDesc = {};
79
-            texDesc.textureType = DKTexture::Type2D;
80
-            texDesc.pixelFormat = DKPixelFormat::BGRA8Unorm;
81
-            texDesc.width = w;
82
-            texDesc.height = h;
83
-            texDesc.depth = 1;
84
-            texDesc.mipmapLevels = 1;
85
-            texDesc.sampleCount = 1;
86
-            texDesc.arrayLength = 1;
87
-            texDesc.usage = DKTexture::UsageStorage  // For Compute Shader
88
-                | DKTexture::UsageSampled | DKTexture::UsageShaderRead;// For FragmentShader
89
-            textureTarget = device->CreateTexture(texDesc);
90
-        }
91
-
92
-        return textureTarget;
93
-    }
94
-};
95
-
96 57
 class GPUShader
97 58
 {
98 59
 private:
@@ -100,7 +61,10 @@ private:
100 61
     DKObject<DKShaderModule> shaderModule = nullptr;
101 62
     DKObject<DKShaderFunction> shaderFunc = nullptr;
102 63
 public:
103
-    GPUShader(DKData* data) : shaderData(data)
64
+
65
+    struct { uint32_t x, y, z; } threadgroupSize;
66
+
67
+    GPUShader(DKData* data) : shaderData(data), threadgroupSize{1,1,1}
104 68
     {
105 69
     }
106 70
 
@@ -112,6 +76,12 @@ public:
112 76
             DKShader shader(shaderData);
113 77
             shaderModule = device->CreateShaderModule(&shader);
114 78
             shaderFunc = shaderModule->CreateFunction(shaderModule->FunctionNames().Value(0));
79
+            if (shaderFunc)
80
+            {
81
+                threadgroupSize = { shader.ThreadgroupSize().x,
82
+                                    shader.ThreadgroupSize().y,
83
+                                    shader.ThreadgroupSize().z };
84
+            }
115 85
         }
116 86
     }
117 87
 
@@ -201,7 +171,6 @@ class ComputeShaderDemo : public SampleApp
201 171
 	DKObject<UVQuad> quad;
202 172
     DKObject<DKTexture> textureColorMap;
203 173
 
204
-    DKObject<TextureComputeTarget> computeTarget;
205 174
     DKObject<DKSamplerState> sampleState = nullptr;;
206 175
 
207 176
     DKObject<GraphicShaderBindingSet> graphicShaderBindingSet = nullptr;
@@ -261,8 +230,9 @@ public:
261 230
     {
262 231
         // Device and Queue Preperation
263 232
         DKObject<DKGraphicsDevice> device = DKGraphicsDevice::SharedInstance();
264
-        DKObject<DKCommandQueue> graphicsQueue = device->CreateCommandQueue(DKCommandQueue::Graphics);
265
-        DKObject<DKCommandQueue> computeQueue = device->CreateCommandQueue(DKCommandQueue::Compute);
233
+        DKObject<DKCommandQueue> graphicsQueue = device->CreateCommandQueue(DKCommandQueue::Graphics| DKCommandQueue::Compute);
234
+        //DKObject<DKCommandQueue> computeQueue = device->CreateCommandQueue(DKCommandQueue::Compute);
235
+        DKObject<DKCommandQueue> computeQueue = graphicsQueue;
266 236
 
267 237
         // Geometry Initialzie
268 238
         quad->InitializeGpuResource(graphicsQueue);
@@ -296,24 +266,23 @@ public:
296 266
         auto cs_shf = cs_sh->Function();
297 267
 
298 268
         // Texture Resource Initialize
299
-        
300
-        computeTarget = DKOBJECT_NEW TextureComputeTarget();
301
-        
302
-        DKSamplerDescriptor computeSamplerDesc = {};
303
-        computeSamplerDesc.magFilter = DKSamplerDescriptor::MinMagFilterLinear;
304
-        computeSamplerDesc.minFilter = DKSamplerDescriptor::MinMagFilterLinear;
305
-        computeSamplerDesc.mipFilter = DKSamplerDescriptor::MipFilterLinear;
306
-        computeSamplerDesc.addressModeU = DKSamplerDescriptor::AddressModeClampToEdge;
307
-        computeSamplerDesc.addressModeV = DKSamplerDescriptor::AddressModeClampToEdge;
308
-        computeSamplerDesc.addressModeW = DKSamplerDescriptor::AddressModeClampToEdge;
309
-        computeSamplerDesc.maxAnisotropy = 1.0f;
310
-        computeSamplerDesc.compareFunction = DKCompareFunctionNever;
311
-        DKObject<DKSamplerState> computeSampler = device->CreateSamplerState(computeSamplerDesc);
312
-
313
-        // create texture
314
-		DKObject<DKTexture> texture = LoadTexture2D(graphicsQueue, resourcePool.LoadResourceData("textures/deathstar3.png"));
269
+		DKObject<DKTexture> sourceTexture = LoadTexture2D(graphicsQueue, resourcePool.LoadResourceData("textures/Vulkan_1024.png"));
270
+        DKObject<DKTexture> targetTexture = [](DKGraphicsDevice* device, int width, int height) {
271
+            DKTextureDescriptor texDesc = {};
272
+            texDesc.textureType = DKTexture::Type2D;
273
+            texDesc.pixelFormat = DKPixelFormat::BGRA8Unorm;
274
+            texDesc.width = width;
275
+            texDesc.height = height;
276
+            texDesc.depth = 1;
277
+            texDesc.mipmapLevels = 1;
278
+            texDesc.sampleCount = 1;
279
+            texDesc.arrayLength = 1;
280
+            texDesc.usage = DKTexture::UsageStorage |   // For Compute Shader
281
+                            DKTexture::UsageSampled;    // For FragmentShader
282
+            return device->CreateTexture(texDesc);
283
+        }(graphicsQueue->Device(), sourceTexture->Width(), sourceTexture->Height());
315 284
 		
316
-        // create sampler
285
+        // create sampler for fragment-shader
317 286
 		DKSamplerDescriptor samplerDesc = {};
318 287
 		samplerDesc.magFilter = DKSamplerDescriptor::MinMagFilterLinear;
319 288
 		samplerDesc.minFilter = DKSamplerDescriptor::MinMagFilterLinear;
@@ -401,11 +370,10 @@ public:
401 370
 
402 371
         DKComputePipelineDescriptor embossComputePipelineDescriptor;
403 372
         embossComputePipelineDescriptor.computeFunction = cs_ef;
404
-        auto emboss = device->CreateComputePipeline(embossComputePipelineDescriptor);
373
+        DKObject<DKComputePipelineState> emboss = device->CreateComputePipeline(embossComputePipelineDescriptor);
405 374
         
406 375
         DKObject<DKTexture> depthBuffer = nullptr;
407
-        DKObject<DKTexture> targettex = nullptr;
408
-
376
+      
409 377
         DKTimer timer;
410 378
 		timer.Reset();
411 379
 
@@ -444,20 +412,20 @@ public:
444 412
             rpd.depthStencilAttachment.loadAction = DKRenderPassAttachmentDescriptor::LoadActionClear;
445 413
             rpd.depthStencilAttachment.storeAction = DKRenderPassAttachmentDescriptor::StoreActionDontCare;
446 414
 
447
-            targettex = computeTarget->ComputeTarget(computeQueue, width, height);
448
-
449 415
             DKObject<DKCommandBuffer> computeCmdbuffer = computeQueue->CreateCommandBuffer();
450 416
             DKObject<DKComputeCommandEncoder> computeEncoder = computeCmdbuffer->CreateComputeCommandEncoder();
451 417
             if (computeEncoder)
452 418
             {
453 419
                 if (computebindSet)
454 420
                 {
455
-                    computebindSet->SetTexture(0, texture);
456
-                    computebindSet->SetTexture(1, targettex);
421
+                    computebindSet->SetTexture(0, sourceTexture);
422
+                    computebindSet->SetTexture(1, targetTexture);
457 423
                 }
458 424
                 computeEncoder->SetComputePipelineState(emboss);
459 425
                 computeEncoder->SetResources(0, computebindSet);
460
-                computeEncoder->Dispatch(width / 16, height / 16, 1);
426
+                computeEncoder->Dispatch(targetTexture->Width() / cs_e->threadgroupSize.x,
427
+                                         targetTexture->Height() / cs_e->threadgroupSize.y,
428
+                                         1);
461 429
                 computeEncoder->EndEncoding();
462 430
             }
463 431
 
@@ -469,7 +437,7 @@ public:
469 437
                 if (graphicShaderBindingSet->PostcomputeDescSet() && ubo)
470 438
                 {
471 439
                     graphicShaderBindingSet->PostcomputeDescSet()->SetBuffer(0, uboBuffer, 0, sizeof(GraphicShaderBindingSet::UBO));
472
-                    graphicShaderBindingSet->PostcomputeDescSet()->SetTexture(1, targettex);
440
+                    graphicShaderBindingSet->PostcomputeDescSet()->SetTexture(1, targetTexture);
473 441
                     graphicShaderBindingSet->PostcomputeDescSet()->SetSamplerState(1, sampler);
474 442
                 }
475 443
 
@@ -504,7 +472,7 @@ public:
504 472
         // create window
505 473
         window = DKWindow::Create("DefaultWindow");
506 474
         window->SetOrigin({ 0, 0 });
507
-        window->Resize({ 320, 240 });
475
+        window->Resize({ 512, 512 });
508 476
         window->Activate();
509 477
 
510 478
         window->AddEventHandler(this, DKFunction([this](const DKWindow::WindowEvent& e)

BIN
Samples/Data/textures/Vulkan_1024.png (Przechowane za pomocą Git LFS) Wyświetl plik

2
+oid sha256:ea36d4110bfa06f6e30af833e2c5047b937aa0471a5bfb0fd19ed9013dd85b2f
3
+size 495747