DKGL2 sample codes

ComputeShader.cpp 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. #include <cstddef>
  2. #include "app.h"
  3. #include "util.h"
  4. class UVQuad : public GPUGeometry
  5. {
  6. private:
  7. struct Vertex
  8. {
  9. DKVector3 Pos;
  10. DKVector2 UV;
  11. };
  12. DKArray<UVQuad::Vertex> vertices =
  13. {
  14. { { 1.0f, 1.0f, 0.0f }, { 1.0f, 0.0f } },
  15. { { -1.0f, 1.0f, 0.0f }, { 0.0f, 0.0f } },
  16. { { -1.0f, -1.0f, 0.0f }, { 0.0f, 1.0f } },
  17. { { 1.0f, -1.0f, 0.0f }, { 1.0f, 1.0f } }
  18. };
  19. DKArray<uint32_t> indices = { 0,1,2,2,3,0 };
  20. public:
  21. UVQuad() = default;
  22. size_t VerticesCount() const { return vertices.Count(); }
  23. size_t IndicesCount() const { return indices.Count(); }
  24. UVQuad::Vertex* VerticesData() { return vertices; }
  25. uint32_t* IndicesData() { return indices; }
  26. void InitializeGpuResource(DKCommandQueue* queue)
  27. {
  28. DKGraphicsDevice* device = queue->Device();
  29. uint32_t vertexBufferSize = static_cast<uint32_t>(VerticesCount()) * sizeof(UVQuad::Vertex);
  30. uint32_t indexBufferSize = IndicesCount() * sizeof(uint32_t);
  31. vertexBuffer = device->CreateBuffer(vertexBufferSize, DKGpuBuffer::StorageModeShared, DKCpuCacheModeReadWrite);
  32. memcpy(vertexBuffer->Contents(), VerticesData(), vertexBufferSize);
  33. vertexBuffer->Flush();
  34. indexBuffer = device->CreateBuffer(indexBufferSize, DKGpuBuffer::StorageModeShared, DKCpuCacheModeReadWrite);
  35. memcpy(indexBuffer->Contents(), IndicesData(), indexBufferSize);
  36. indexBuffer->Flush();
  37. // setup vertex buffer and attributes
  38. vertexDesc.attributes = {
  39. { DKVertexFormat::Float3, offsetof(UVQuad::Vertex, Pos), 0, 0 },
  40. { DKVertexFormat::Float2, offsetof(UVQuad::Vertex, UV), 0, 1 },
  41. };
  42. vertexDesc.layouts = {
  43. { DKVertexStepRate::Vertex, sizeof(UVQuad::Vertex), 0 },
  44. };
  45. }
  46. };
  47. class GPUShader
  48. {
  49. private:
  50. DKObject<DKData> shaderData = nullptr;
  51. DKObject<DKShaderModule> shaderModule = nullptr;
  52. DKObject<DKShaderFunction> shaderFunc = nullptr;
  53. public:
  54. struct { uint32_t x, y, z; } threadgroupSize;
  55. GPUShader(DKData* data) : shaderData(data), threadgroupSize{1,1,1}
  56. {
  57. }
  58. void InitializeGpuResource(DKCommandQueue* queue)
  59. {
  60. if (shaderData)
  61. {
  62. DKGraphicsDevice* device = queue->Device();
  63. DKShader shader(shaderData);
  64. shaderModule = device->CreateShaderModule(&shader);
  65. shaderFunc = shaderModule->CreateFunction(shaderModule->FunctionNames().Value(0));
  66. if (shaderFunc)
  67. {
  68. threadgroupSize = { shader.ThreadgroupSize().x,
  69. shader.ThreadgroupSize().y,
  70. shader.ThreadgroupSize().z };
  71. }
  72. }
  73. }
  74. DKShaderFunction* Function() { return shaderFunc; }
  75. };
  76. class GraphicShaderBindingSet
  77. {
  78. public:
  79. struct UBO
  80. {
  81. DKMatrix4 projectionMatrix;
  82. DKMatrix4 modelMatrix;
  83. };
  84. private:
  85. DKShaderBindingSetLayout descriptorSetLayout;
  86. DKObject<DKShaderBindingSet> descriptorSetPreCompute;
  87. DKObject<DKShaderBindingSet> descriptorSetPostCompute;
  88. DKObject<DKRenderPipelineState> pipelineState;
  89. DKObject<DKGpuBuffer> uniformBuffer;
  90. UBO* ubo = nullptr;
  91. public:
  92. GraphicShaderBindingSet() = default;
  93. DKShaderBindingSet* PrecomputeDescSet() { return descriptorSetPreCompute; }
  94. DKShaderBindingSet* PostcomputeDescSet() { return descriptorSetPostCompute; }
  95. DKRenderPipelineState* GraphicPipelineState() { return pipelineState; }
  96. void InitializeGpuResource(DKGraphicsDevice* device)
  97. {
  98. if (1)
  99. {
  100. DKShaderBinding bindings[2] = {
  101. {
  102. 0,
  103. DKShader::DescriptorTypeUniformBuffer,
  104. 1,
  105. nullptr
  106. },
  107. {
  108. 1,
  109. DKShader::DescriptorTypeTextureSampler,
  110. 1,
  111. nullptr
  112. },
  113. };
  114. descriptorSetLayout.bindings.Add(bindings, 2);
  115. }
  116. descriptorSetPreCompute = device->CreateShaderBindingSet(descriptorSetLayout);
  117. descriptorSetPostCompute = device->CreateShaderBindingSet(descriptorSetLayout);
  118. uniformBuffer = device->CreateBuffer(sizeof(GraphicShaderBindingSet::UBO), DKGpuBuffer::StorageModeShared, DKCpuCacheModeReadWrite);
  119. if (descriptorSetPreCompute)
  120. {
  121. if (uniformBuffer)
  122. {
  123. ubo = reinterpret_cast<UBO*>(uniformBuffer->Contents());
  124. ubo->projectionMatrix = DKMatrix4::identity;
  125. ubo->modelMatrix = DKMatrix4::identity;
  126. uniformBuffer->Flush();
  127. descriptorSetPreCompute->SetBuffer(0, uniformBuffer, 0, sizeof(UBO));
  128. }
  129. }
  130. if (descriptorSetPostCompute)
  131. {
  132. if (uniformBuffer && ubo)
  133. {
  134. descriptorSetPostCompute->SetBuffer(0, uniformBuffer, 0, sizeof(UBO));
  135. }
  136. }
  137. }
  138. DKGpuBuffer* UniformBuffer() { return uniformBuffer; }
  139. UBO* UniformBufferO() { return ubo; }
  140. };
  141. class ComputeShaderDemo : public SampleApp
  142. {
  143. DKObject<DKWindow> window;
  144. DKObject<DKThread> renderThread;
  145. DKAtomicNumber32 runningRenderThread;
  146. //Resource
  147. DKObject<UVQuad> quad;
  148. DKObject<DKTexture> textureColorMap;
  149. DKObject<DKSamplerState> sampleState = nullptr;;
  150. DKObject<GraphicShaderBindingSet> graphicShaderBindingSet = nullptr;
  151. public:
  152. DKObject<DKTexture> LoadTexture2D(DKCommandQueue* queue, DKData* data)
  153. {
  154. DKObject<DKImage> image = DKImage::Create(data);
  155. if (image)
  156. {
  157. DKGraphicsDevice* device = queue->Device();
  158. DKTextureDescriptor texDesc = {};
  159. texDesc.textureType = DKTexture::Type2D;
  160. texDesc.pixelFormat = DKPixelFormat::RGBA8Unorm;
  161. texDesc.width = image->Width();
  162. texDesc.height = image->Height();
  163. texDesc.depth = 1;
  164. texDesc.mipmapLevels = 1;
  165. texDesc.sampleCount = 1;
  166. texDesc.arrayLength = 1;
  167. texDesc.usage = DKTexture::UsageStorage | DKTexture::UsageShaderRead | DKTexture::UsageCopyDestination | DKTexture::UsageSampled;
  168. DKObject<DKTexture> tex = device->CreateTexture(texDesc);
  169. if (tex)
  170. {
  171. size_t bytesPerPixel = image->BytesPerPixel();
  172. DKASSERT_DESC(bytesPerPixel == DKPixelFormatBytesPerPixel(texDesc.pixelFormat), "BytesPerPixel mismatch!");
  173. uint32_t width = image->Width();
  174. uint32_t height = image->Height();
  175. size_t bufferLength = bytesPerPixel * width * height;
  176. DKObject<DKGpuBuffer> stagingBuffer = device->CreateBuffer(bufferLength, DKGpuBuffer::StorageModeShared, DKCpuCacheModeReadWrite);
  177. memcpy(stagingBuffer->Contents(), image->Contents(), bufferLength);
  178. stagingBuffer->Flush();
  179. DKObject<DKCommandBuffer> cb = queue->CreateCommandBuffer();
  180. DKObject<DKCopyCommandEncoder> encoder = cb->CreateCopyCommandEncoder();
  181. encoder->CopyFromBufferToTexture(stagingBuffer,
  182. { 0, width, height },
  183. tex,
  184. { 0,0, 0,0,0 },
  185. { width,height,1 });
  186. encoder->EndEncoding();
  187. cb->Commit();
  188. DKLog("Texture created!");
  189. return tex;
  190. }
  191. }
  192. return nullptr;
  193. }
  194. void RenderThread(void)
  195. {
  196. // Device and Queue Preperation
  197. DKObject<DKGraphicsDevice> device = DKGraphicsDevice::SharedInstance();
  198. DKObject<DKCommandQueue> graphicsQueue = device->CreateCommandQueue(DKCommandQueue::Graphics| DKCommandQueue::Compute);
  199. //DKObject<DKCommandQueue> computeQueue = device->CreateCommandQueue(DKCommandQueue::Compute);
  200. DKObject<DKCommandQueue> computeQueue = graphicsQueue;
  201. // Geometry Initialzie
  202. quad->InitializeGpuResource(graphicsQueue);
  203. // create shaders
  204. DKObject<DKData> vertData = resourcePool.LoadResourceData("shaders/ComputeShader/texture.vert.spv");
  205. DKObject<DKData> fragData = resourcePool.LoadResourceData("shaders/ComputeShader/texture.frag.spv");
  206. DKObject<DKData> embossData = resourcePool.LoadResourceData("shaders/ComputeShader/emboss.comp.spv");
  207. DKObject<DKData> edgedetectData = resourcePool.LoadResourceData("shaders/ComputeShader/edgedetect.comp.spv");
  208. DKObject<DKData> sharpenData = resourcePool.LoadResourceData("shaders/ComputeShader/sharpen.comp.spv");
  209. DKObject<GPUShader> vs = DKOBJECT_NEW GPUShader(vertData);
  210. DKObject<GPUShader> fs = DKOBJECT_NEW GPUShader(fragData);
  211. DKObject<GPUShader> cs_e = DKOBJECT_NEW GPUShader(embossData);
  212. DKObject<GPUShader> cs_ed = DKOBJECT_NEW GPUShader(edgedetectData);
  213. DKObject<GPUShader> cs_sh = DKOBJECT_NEW GPUShader(sharpenData);
  214. vs->InitializeGpuResource(graphicsQueue);
  215. fs->InitializeGpuResource(graphicsQueue);
  216. cs_e->InitializeGpuResource(computeQueue);
  217. cs_ed->InitializeGpuResource(computeQueue);
  218. cs_sh->InitializeGpuResource(computeQueue);
  219. auto vsf = vs->Function();
  220. auto fsf = fs->Function();
  221. auto cs_ef = cs_e->Function();
  222. auto cs_edf = cs_ed->Function();
  223. auto cs_shf = cs_sh->Function();
  224. // Texture Resource Initialize
  225. DKObject<DKTexture> sourceTexture = LoadTexture2D(graphicsQueue, resourcePool.LoadResourceData("textures/Vulkan_1024.png"));
  226. DKObject<DKTexture> targetTexture = [](DKGraphicsDevice* device, int width, int height) {
  227. DKTextureDescriptor texDesc = {};
  228. texDesc.textureType = DKTexture::Type2D;
  229. texDesc.pixelFormat = DKPixelFormat::BGRA8Unorm;
  230. texDesc.width = width;
  231. texDesc.height = height;
  232. texDesc.depth = 1;
  233. texDesc.mipmapLevels = 1;
  234. texDesc.sampleCount = 1;
  235. texDesc.arrayLength = 1;
  236. texDesc.usage = DKTexture::UsageStorage | // For Compute Shader
  237. DKTexture::UsageSampled; // For FragmentShader
  238. return device->CreateTexture(texDesc);
  239. }(graphicsQueue->Device(), sourceTexture->Width(), sourceTexture->Height());
  240. // create sampler for fragment-shader
  241. DKSamplerDescriptor samplerDesc = {};
  242. samplerDesc.magFilter = DKSamplerDescriptor::MinMagFilterLinear;
  243. samplerDesc.minFilter = DKSamplerDescriptor::MinMagFilterLinear;
  244. samplerDesc.addressModeU = DKSamplerDescriptor::AddressModeClampToEdge;
  245. samplerDesc.addressModeV = DKSamplerDescriptor::AddressModeClampToEdge;
  246. samplerDesc.addressModeW = DKSamplerDescriptor::AddressModeClampToEdge;
  247. samplerDesc.maxAnisotropy = 1;
  248. DKObject<DKSamplerState> sampler = device->CreateSamplerState(samplerDesc);
  249. DKObject<DKSwapChain> swapChain = graphicsQueue->CreateSwapChain(window);
  250. DKLog("VertexFunction.VertexAttributes: %d", vsf->StageInputAttributes().Count());
  251. for (int i = 0; i < vsf->StageInputAttributes().Count(); ++i)
  252. {
  253. const DKShaderAttribute& attr = vsf->StageInputAttributes().Value(i);
  254. DKLog(" --> VertexAttribute[%d]: \"%ls\" (location:%u)", i, (const wchar_t*)attr.name, attr.location);
  255. }
  256. DKRenderPipelineDescriptor pipelineDescriptor;
  257. // setup shader
  258. pipelineDescriptor.vertexFunction = vsf;
  259. pipelineDescriptor.fragmentFunction = fsf;
  260. // setup color-attachment render-targets
  261. pipelineDescriptor.colorAttachments.Resize(1);
  262. pipelineDescriptor.colorAttachments.Value(0).pixelFormat = swapChain->ColorPixelFormat();
  263. pipelineDescriptor.colorAttachments.Value(0).blendingEnabled = false;
  264. pipelineDescriptor.colorAttachments.Value(0).sourceRGBBlendFactor = DKBlendFactor::SourceAlpha;
  265. pipelineDescriptor.colorAttachments.Value(0).destinationRGBBlendFactor = DKBlendFactor::OneMinusSourceAlpha;
  266. // setup depth-stencil
  267. pipelineDescriptor.depthStencilAttachmentPixelFormat = DKPixelFormat::D32Float;
  268. pipelineDescriptor.depthStencilDescriptor.depthWriteEnabled = true;
  269. pipelineDescriptor.depthStencilDescriptor.depthCompareFunction = DKCompareFunctionLessEqual;
  270. // setup vertex buffer and attributes
  271. pipelineDescriptor.vertexDescriptor = quad->VertexDescriptor();
  272. // setup topology and rasterization
  273. pipelineDescriptor.primitiveTopology = DKPrimitiveType::Triangle;
  274. pipelineDescriptor.frontFace = DKFrontFace::CCW;
  275. pipelineDescriptor.triangleFillMode = DKTriangleFillMode::Fill;
  276. pipelineDescriptor.depthClipMode = DKDepthClipMode::Clip;
  277. pipelineDescriptor.cullMode = DKCullMode::Back;
  278. pipelineDescriptor.rasterizationEnabled = true;
  279. DKPipelineReflection reflection;
  280. DKObject<DKRenderPipelineState> pipelineState = device->CreateRenderPipeline(pipelineDescriptor, &reflection);
  281. if (pipelineState)
  282. {
  283. PrintPipelineReflection(&reflection, DKLogCategory::Verbose);
  284. }
  285. ///
  286. graphicShaderBindingSet = DKOBJECT_NEW GraphicShaderBindingSet();
  287. graphicShaderBindingSet->InitializeGpuResource(device);
  288. auto uboBuffer = graphicShaderBindingSet->UniformBuffer();
  289. auto ubo = graphicShaderBindingSet->UniformBufferO();
  290. // ComputerBuffer Layout
  291. DKShaderBindingSetLayout ComputeLayout;
  292. if (1)
  293. {
  294. DKShaderBinding bindings[2] = {
  295. {
  296. 0,
  297. DKShader::DescriptorTypeStorageTexture,
  298. 1,
  299. nullptr
  300. }, // Input Image (read-only)
  301. {
  302. 1,
  303. DKShader::DescriptorTypeStorageTexture,
  304. 1,
  305. nullptr
  306. }, // Output image (write)
  307. };
  308. ComputeLayout.bindings.Add(bindings, 2);
  309. }
  310. DKObject<DKShaderBindingSet> computebindSet = device->CreateShaderBindingSet(ComputeLayout);
  311. //auto CS_EF = CS_E->Function();
  312. //auto CS_EDF = CS_ED->Function();
  313. //auto CS_SHF = CS_SH->Function();
  314. DKComputePipelineDescriptor embossComputePipelineDescriptor;
  315. embossComputePipelineDescriptor.computeFunction = cs_ef;
  316. DKObject<DKComputePipelineState> emboss = device->CreateComputePipeline(embossComputePipelineDescriptor);
  317. DKObject<DKTexture> depthBuffer = nullptr;
  318. DKTimer timer;
  319. timer.Reset();
  320. DKLog("Render thread begin");
  321. while (!runningRenderThread.CompareAndSet(0, 0))
  322. {
  323. DKRenderPassDescriptor rpd = swapChain->CurrentRenderPassDescriptor();
  324. double t = timer.Elapsed();
  325. double waveT = (cos(t) + 1.0) * 0.5;
  326. rpd.colorAttachments.Value(0).clearColor = DKColor(waveT, 0.0, 0.0, 0.0);
  327. int width = rpd.colorAttachments.Value(0).renderTarget->Width();
  328. int height = rpd.colorAttachments.Value(0).renderTarget->Height();
  329. if (depthBuffer)
  330. {
  331. if (depthBuffer->Width() != width ||
  332. depthBuffer->Height() != height )
  333. depthBuffer = nullptr;
  334. }
  335. if (depthBuffer == nullptr)
  336. {
  337. // create depth buffer
  338. DKTextureDescriptor texDesc = {};
  339. texDesc.textureType = DKTexture::Type2D;
  340. texDesc.pixelFormat = DKPixelFormat::D32Float;
  341. texDesc.width = width;
  342. texDesc.height = height;
  343. texDesc.depth = 1;
  344. texDesc.mipmapLevels = 1;
  345. texDesc.sampleCount = 1;
  346. texDesc.arrayLength = 1;
  347. texDesc.usage = DKTexture::UsageRenderTarget;
  348. depthBuffer = device->CreateTexture(texDesc);
  349. }
  350. rpd.depthStencilAttachment.renderTarget = depthBuffer;
  351. rpd.depthStencilAttachment.loadAction = DKRenderPassAttachmentDescriptor::LoadActionClear;
  352. rpd.depthStencilAttachment.storeAction = DKRenderPassAttachmentDescriptor::StoreActionDontCare;
  353. DKObject<DKCommandBuffer> computeCmdbuffer = computeQueue->CreateCommandBuffer();
  354. DKObject<DKComputeCommandEncoder> computeEncoder = computeCmdbuffer->CreateComputeCommandEncoder();
  355. if (computeEncoder)
  356. {
  357. if (computebindSet)
  358. {
  359. computebindSet->SetTexture(0, sourceTexture);
  360. computebindSet->SetTexture(1, targetTexture);
  361. }
  362. computeEncoder->SetComputePipelineState(emboss);
  363. computeEncoder->SetResources(0, computebindSet);
  364. computeEncoder->Dispatch(targetTexture->Width() / cs_e->threadgroupSize.x,
  365. targetTexture->Height() / cs_e->threadgroupSize.y,
  366. 1);
  367. computeEncoder->EndEncoding();
  368. }
  369. DKObject<DKCommandBuffer> buffer = graphicsQueue->CreateCommandBuffer();
  370. DKObject<DKRenderCommandEncoder> encoder = buffer->CreateRenderCommandEncoder(rpd);
  371. if (encoder)
  372. {
  373. if (graphicShaderBindingSet->PostcomputeDescSet() && ubo)
  374. {
  375. graphicShaderBindingSet->PostcomputeDescSet()->SetBuffer(0, uboBuffer, 0, sizeof(GraphicShaderBindingSet::UBO));
  376. graphicShaderBindingSet->PostcomputeDescSet()->SetTexture(1, targetTexture);
  377. graphicShaderBindingSet->PostcomputeDescSet()->SetSamplerState(1, sampler);
  378. }
  379. encoder->SetRenderPipelineState(pipelineState);
  380. encoder->SetVertexBuffer(quad->VertexBuffer(), 0, 0);
  381. encoder->SetIndexBuffer(quad->IndexBuffer(), 0, DKIndexType::UInt32);
  382. encoder->SetResources(0, graphicShaderBindingSet->PostcomputeDescSet());
  383. // draw scene!
  384. encoder->DrawIndexed(quad->IndicesCount(), 1, 0, 0, 0);
  385. encoder->EndEncoding();
  386. if (computeCmdbuffer)
  387. computeCmdbuffer->Commit();
  388. buffer->Commit();
  389. swapChain->Present();
  390. }
  391. else
  392. {
  393. }
  394. DKThread::Sleep(0.01);
  395. }
  396. DKLog("RenderThread terminating...");
  397. }
  398. void OnInitialize(void) override
  399. {
  400. SampleApp::OnInitialize();
  401. DKLogD("%s", DKGL_FUNCTION_NAME);
  402. // create window
  403. window = DKWindow::Create("DefaultWindow");
  404. window->SetOrigin({ 0, 0 });
  405. window->Resize({ 512, 512 });
  406. window->Activate();
  407. window->AddEventHandler(this, DKFunction([this](const DKWindow::WindowEvent& e)
  408. {
  409. if (e.type == DKWindow::WindowEvent::WindowClosed)
  410. DKApplication::Instance()->Terminate(0);
  411. }), NULL, NULL);
  412. quad = DKOBJECT_NEW UVQuad();
  413. runningRenderThread = 1;
  414. renderThread = DKThread::Create(DKFunction(this, &ComputeShaderDemo::RenderThread)->Invocation());
  415. }
  416. void OnTerminate(void) override
  417. {
  418. DKLogD("%s", DKGL_FUNCTION_NAME);
  419. runningRenderThread = 0;
  420. renderThread->WaitTerminate();
  421. renderThread = NULL;
  422. window = NULL;
  423. SampleApp::OnTerminate();
  424. }
  425. };
  426. #ifdef _WIN32
  427. int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
  428. _In_opt_ HINSTANCE hPrevInstance,
  429. _In_ LPWSTR lpCmdLine,
  430. _In_ int nCmdShow)
  431. #else
  432. int main(int argc, const char * argv[])
  433. #endif
  434. {
  435. ComputeShaderDemo app;
  436. DKPropertySet::SystemConfig().SetValue("AppDelegate", "AppDelegate");
  437. DKPropertySet::SystemConfig().SetValue("GraphicsAPI", "Vulkan");
  438. return app.Run();
  439. }