Browse Source

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

ComputeShader ThreadingGroup 적용.
Hongtae Kim 5 years ago
parent
commit
a69fa338ef
2 changed files with 44 additions and 73 deletions
  1. 41
    73
      Samples/ComputeShader/ComputeShader.cpp
  2. BIN
      Samples/Data/textures/Vulkan_1024.png

+ 41
- 73
Samples/ComputeShader/ComputeShader.cpp View File

13
 
13
 
14
     DKArray<UVQuad::Vertex> vertices =
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
         { { -1.0f, -1.0f, 0.0f }, { 0.0f, 1.0f } },
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
     DKArray<uint32_t> indices = { 0,1,2,2,3,0 };
22
     DKArray<uint32_t> indices = { 0,1,2,2,3,0 };
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
 class GPUShader
57
 class GPUShader
97
 {
58
 {
98
 private:
59
 private:
100
     DKObject<DKShaderModule> shaderModule = nullptr;
61
     DKObject<DKShaderModule> shaderModule = nullptr;
101
     DKObject<DKShaderFunction> shaderFunc = nullptr;
62
     DKObject<DKShaderFunction> shaderFunc = nullptr;
102
 public:
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
             DKShader shader(shaderData);
76
             DKShader shader(shaderData);
113
             shaderModule = device->CreateShaderModule(&shader);
77
             shaderModule = device->CreateShaderModule(&shader);
114
             shaderFunc = shaderModule->CreateFunction(shaderModule->FunctionNames().Value(0));
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
 	DKObject<UVQuad> quad;
171
 	DKObject<UVQuad> quad;
202
     DKObject<DKTexture> textureColorMap;
172
     DKObject<DKTexture> textureColorMap;
203
 
173
 
204
-    DKObject<TextureComputeTarget> computeTarget;
205
     DKObject<DKSamplerState> sampleState = nullptr;;
174
     DKObject<DKSamplerState> sampleState = nullptr;;
206
 
175
 
207
     DKObject<GraphicShaderBindingSet> graphicShaderBindingSet = nullptr;
176
     DKObject<GraphicShaderBindingSet> graphicShaderBindingSet = nullptr;
261
     {
230
     {
262
         // Device and Queue Preperation
231
         // Device and Queue Preperation
263
         DKObject<DKGraphicsDevice> device = DKGraphicsDevice::SharedInstance();
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
         // Geometry Initialzie
237
         // Geometry Initialzie
268
         quad->InitializeGpuResource(graphicsQueue);
238
         quad->InitializeGpuResource(graphicsQueue);
296
         auto cs_shf = cs_sh->Function();
266
         auto cs_shf = cs_sh->Function();
297
 
267
 
298
         // Texture Resource Initialize
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
 		DKSamplerDescriptor samplerDesc = {};
286
 		DKSamplerDescriptor samplerDesc = {};
318
 		samplerDesc.magFilter = DKSamplerDescriptor::MinMagFilterLinear;
287
 		samplerDesc.magFilter = DKSamplerDescriptor::MinMagFilterLinear;
319
 		samplerDesc.minFilter = DKSamplerDescriptor::MinMagFilterLinear;
288
 		samplerDesc.minFilter = DKSamplerDescriptor::MinMagFilterLinear;
401
 
370
 
402
         DKComputePipelineDescriptor embossComputePipelineDescriptor;
371
         DKComputePipelineDescriptor embossComputePipelineDescriptor;
403
         embossComputePipelineDescriptor.computeFunction = cs_ef;
372
         embossComputePipelineDescriptor.computeFunction = cs_ef;
404
-        auto emboss = device->CreateComputePipeline(embossComputePipelineDescriptor);
373
+        DKObject<DKComputePipelineState> emboss = device->CreateComputePipeline(embossComputePipelineDescriptor);
405
         
374
         
406
         DKObject<DKTexture> depthBuffer = nullptr;
375
         DKObject<DKTexture> depthBuffer = nullptr;
407
-        DKObject<DKTexture> targettex = nullptr;
408
-
376
+      
409
         DKTimer timer;
377
         DKTimer timer;
410
 		timer.Reset();
378
 		timer.Reset();
411
 
379
 
444
             rpd.depthStencilAttachment.loadAction = DKRenderPassAttachmentDescriptor::LoadActionClear;
412
             rpd.depthStencilAttachment.loadAction = DKRenderPassAttachmentDescriptor::LoadActionClear;
445
             rpd.depthStencilAttachment.storeAction = DKRenderPassAttachmentDescriptor::StoreActionDontCare;
413
             rpd.depthStencilAttachment.storeAction = DKRenderPassAttachmentDescriptor::StoreActionDontCare;
446
 
414
 
447
-            targettex = computeTarget->ComputeTarget(computeQueue, width, height);
448
-
449
             DKObject<DKCommandBuffer> computeCmdbuffer = computeQueue->CreateCommandBuffer();
415
             DKObject<DKCommandBuffer> computeCmdbuffer = computeQueue->CreateCommandBuffer();
450
             DKObject<DKComputeCommandEncoder> computeEncoder = computeCmdbuffer->CreateComputeCommandEncoder();
416
             DKObject<DKComputeCommandEncoder> computeEncoder = computeCmdbuffer->CreateComputeCommandEncoder();
451
             if (computeEncoder)
417
             if (computeEncoder)
452
             {
418
             {
453
                 if (computebindSet)
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
                 computeEncoder->SetComputePipelineState(emboss);
424
                 computeEncoder->SetComputePipelineState(emboss);
459
                 computeEncoder->SetResources(0, computebindSet);
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
                 computeEncoder->EndEncoding();
429
                 computeEncoder->EndEncoding();
462
             }
430
             }
463
 
431
 
469
                 if (graphicShaderBindingSet->PostcomputeDescSet() && ubo)
437
                 if (graphicShaderBindingSet->PostcomputeDescSet() && ubo)
470
                 {
438
                 {
471
                     graphicShaderBindingSet->PostcomputeDescSet()->SetBuffer(0, uboBuffer, 0, sizeof(GraphicShaderBindingSet::UBO));
439
                     graphicShaderBindingSet->PostcomputeDescSet()->SetBuffer(0, uboBuffer, 0, sizeof(GraphicShaderBindingSet::UBO));
472
-                    graphicShaderBindingSet->PostcomputeDescSet()->SetTexture(1, targettex);
440
+                    graphicShaderBindingSet->PostcomputeDescSet()->SetTexture(1, targetTexture);
473
                     graphicShaderBindingSet->PostcomputeDescSet()->SetSamplerState(1, sampler);
441
                     graphicShaderBindingSet->PostcomputeDescSet()->SetSamplerState(1, sampler);
474
                 }
442
                 }
475
 
443
 
504
         // create window
472
         // create window
505
         window = DKWindow::Create("DefaultWindow");
473
         window = DKWindow::Create("DefaultWindow");
506
         window->SetOrigin({ 0, 0 });
474
         window->SetOrigin({ 0, 0 });
507
-        window->Resize({ 320, 240 });
475
+        window->Resize({ 512, 512 });
508
         window->Activate();
476
         window->Activate();
509
 
477
 
510
         window->AddEventHandler(this, DKFunction([this](const DKWindow::WindowEvent& e)
478
         window->AddEventHandler(this, DKFunction([this](const DKWindow::WindowEvent& e)

BIN
Samples/Data/textures/Vulkan_1024.png (Stored with Git LFS) View File

2
+oid sha256:ea36d4110bfa06f6e30af833e2c5047b937aa0471a5bfb0fd19ed9013dd85b2f
3
+size 495747