DKGL2 sample codes

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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;
  199. DKObject<DKCommandQueue> computeQueue;
  200. bool useSingleQueue = false;
  201. if (useSingleQueue)
  202. {
  203. graphicsQueue = device->CreateCommandQueue(DKCommandQueue::Graphics | DKCommandQueue::Compute);
  204. computeQueue = graphicsQueue;
  205. }
  206. else
  207. {
  208. graphicsQueue = device->CreateCommandQueue(DKCommandQueue::Graphics);
  209. computeQueue = device->CreateCommandQueue(DKCommandQueue::Compute);
  210. }
  211. // Geometry Initialzie
  212. quad->InitializeGpuResource(graphicsQueue);
  213. // create shaders
  214. DKObject<DKData> vertData = resourcePool.LoadResourceData("shaders/ComputeShader/texture.vert.spv");
  215. DKObject<DKData> fragData = resourcePool.LoadResourceData("shaders/ComputeShader/texture.frag.spv");
  216. DKObject<DKData> embossData = resourcePool.LoadResourceData("shaders/ComputeShader/emboss.comp.spv");
  217. DKObject<DKData> edgedetectData = resourcePool.LoadResourceData("shaders/ComputeShader/edgedetect.comp.spv");
  218. DKObject<DKData> sharpenData = resourcePool.LoadResourceData("shaders/ComputeShader/sharpen.comp.spv");
  219. DKObject<GPUShader> vs = DKOBJECT_NEW GPUShader(vertData);
  220. DKObject<GPUShader> fs = DKOBJECT_NEW GPUShader(fragData);
  221. DKObject<GPUShader> cs_e = DKOBJECT_NEW GPUShader(embossData);
  222. DKObject<GPUShader> cs_ed = DKOBJECT_NEW GPUShader(edgedetectData);
  223. DKObject<GPUShader> cs_sh = DKOBJECT_NEW GPUShader(sharpenData);
  224. vs->InitializeGpuResource(graphicsQueue);
  225. fs->InitializeGpuResource(graphicsQueue);
  226. cs_e->InitializeGpuResource(computeQueue);
  227. cs_ed->InitializeGpuResource(computeQueue);
  228. cs_sh->InitializeGpuResource(computeQueue);
  229. auto vsf = vs->Function();
  230. auto fsf = fs->Function();
  231. auto cs_ef = cs_e->Function();
  232. auto cs_edf = cs_ed->Function();
  233. auto cs_shf = cs_sh->Function();
  234. // Texture Resource Initialize
  235. DKObject<DKTexture> sourceTexture = LoadTexture2D(graphicsQueue, resourcePool.LoadResourceData("textures/Vulkan_1024.png"));
  236. DKObject<DKTexture> targetTexture = [](DKGraphicsDevice* device, int width, int height) {
  237. DKTextureDescriptor texDesc = {};
  238. texDesc.textureType = DKTexture::Type2D;
  239. texDesc.pixelFormat = DKPixelFormat::BGRA8Unorm;
  240. texDesc.width = width;
  241. texDesc.height = height;
  242. texDesc.depth = 1;
  243. texDesc.mipmapLevels = 1;
  244. texDesc.sampleCount = 1;
  245. texDesc.arrayLength = 1;
  246. texDesc.usage = DKTexture::UsageStorage | // For Compute Shader
  247. DKTexture::UsageSampled; // For FragmentShader
  248. return device->CreateTexture(texDesc);
  249. }(graphicsQueue->Device(), sourceTexture->Width(), sourceTexture->Height());
  250. // create sampler for fragment-shader
  251. DKSamplerDescriptor samplerDesc = {};
  252. samplerDesc.magFilter = DKSamplerDescriptor::MinMagFilterLinear;
  253. samplerDesc.minFilter = DKSamplerDescriptor::MinMagFilterLinear;
  254. samplerDesc.addressModeU = DKSamplerDescriptor::AddressModeClampToEdge;
  255. samplerDesc.addressModeV = DKSamplerDescriptor::AddressModeClampToEdge;
  256. samplerDesc.addressModeW = DKSamplerDescriptor::AddressModeClampToEdge;
  257. samplerDesc.maxAnisotropy = 1;
  258. DKObject<DKSamplerState> sampler = device->CreateSamplerState(samplerDesc);
  259. DKObject<DKSwapChain> swapChain = graphicsQueue->CreateSwapChain(window);
  260. DKLog("VertexFunction.VertexAttributes: %d", vsf->StageInputAttributes().Count());
  261. for (int i = 0; i < vsf->StageInputAttributes().Count(); ++i)
  262. {
  263. const DKShaderAttribute& attr = vsf->StageInputAttributes().Value(i);
  264. DKLog(" --> VertexAttribute[%d]: \"%ls\" (location:%u)", i, (const wchar_t*)attr.name, attr.location);
  265. }
  266. DKRenderPipelineDescriptor pipelineDescriptor;
  267. // setup shader
  268. pipelineDescriptor.vertexFunction = vsf;
  269. pipelineDescriptor.fragmentFunction = fsf;
  270. // setup color-attachment render-targets
  271. pipelineDescriptor.colorAttachments.Resize(1);
  272. pipelineDescriptor.colorAttachments.Value(0).pixelFormat = swapChain->ColorPixelFormat();
  273. pipelineDescriptor.colorAttachments.Value(0).blendingEnabled = false;
  274. pipelineDescriptor.colorAttachments.Value(0).sourceRGBBlendFactor = DKBlendFactor::SourceAlpha;
  275. pipelineDescriptor.colorAttachments.Value(0).destinationRGBBlendFactor = DKBlendFactor::OneMinusSourceAlpha;
  276. // setup depth-stencil
  277. pipelineDescriptor.depthStencilAttachmentPixelFormat = DKPixelFormat::D32Float;
  278. pipelineDescriptor.depthStencilDescriptor.depthWriteEnabled = true;
  279. pipelineDescriptor.depthStencilDescriptor.depthCompareFunction = DKCompareFunctionLessEqual;
  280. // setup vertex buffer and attributes
  281. pipelineDescriptor.vertexDescriptor = quad->VertexDescriptor();
  282. // setup topology and rasterization
  283. pipelineDescriptor.primitiveTopology = DKPrimitiveType::Triangle;
  284. pipelineDescriptor.frontFace = DKFrontFace::CCW;
  285. pipelineDescriptor.triangleFillMode = DKTriangleFillMode::Fill;
  286. pipelineDescriptor.depthClipMode = DKDepthClipMode::Clip;
  287. pipelineDescriptor.cullMode = DKCullMode::Back;
  288. pipelineDescriptor.rasterizationEnabled = true;
  289. DKPipelineReflection reflection;
  290. DKObject<DKRenderPipelineState> pipelineState = device->CreateRenderPipeline(pipelineDescriptor, &reflection);
  291. if (pipelineState)
  292. {
  293. PrintPipelineReflection(&reflection, DKLogCategory::Verbose);
  294. }
  295. ///
  296. graphicShaderBindingSet = DKOBJECT_NEW GraphicShaderBindingSet();
  297. graphicShaderBindingSet->InitializeGpuResource(device);
  298. auto uboBuffer = graphicShaderBindingSet->UniformBuffer();
  299. auto ubo = graphicShaderBindingSet->UniformBufferO();
  300. // ComputerBuffer Layout
  301. DKShaderBindingSetLayout ComputeLayout;
  302. if (1)
  303. {
  304. DKShaderBinding bindings[2] = {
  305. {
  306. 0,
  307. DKShader::DescriptorTypeStorageTexture,
  308. 1,
  309. nullptr
  310. }, // Input Image (read-only)
  311. {
  312. 1,
  313. DKShader::DescriptorTypeStorageTexture,
  314. 1,
  315. nullptr
  316. }, // Output image (write)
  317. };
  318. ComputeLayout.bindings.Add(bindings, 2);
  319. }
  320. DKObject<DKShaderBindingSet> computebindSet = device->CreateShaderBindingSet(ComputeLayout);
  321. //auto CS_EF = CS_E->Function();
  322. //auto CS_EDF = CS_ED->Function();
  323. //auto CS_SHF = CS_SH->Function();
  324. DKComputePipelineDescriptor embossComputePipelineDescriptor;
  325. embossComputePipelineDescriptor.computeFunction = cs_ef;
  326. DKObject<DKComputePipelineState> emboss = device->CreateComputePipeline(embossComputePipelineDescriptor);
  327. DKObject<DKTexture> depthBuffer = nullptr;
  328. DKTimer timer;
  329. timer.Reset();
  330. DKLog("Render thread begin");
  331. while (!runningRenderThread.CompareAndSet(0, 0))
  332. {
  333. DKRenderPassDescriptor rpd = swapChain->CurrentRenderPassDescriptor();
  334. double t = timer.Elapsed();
  335. double waveT = (cos(t) + 1.0) * 0.5;
  336. rpd.colorAttachments.Value(0).clearColor = DKColor(waveT, 0.0, 0.0, 0.0);
  337. int width = rpd.colorAttachments.Value(0).renderTarget->Width();
  338. int height = rpd.colorAttachments.Value(0).renderTarget->Height();
  339. if (depthBuffer)
  340. {
  341. if (depthBuffer->Width() != width ||
  342. depthBuffer->Height() != height )
  343. depthBuffer = nullptr;
  344. }
  345. if (depthBuffer == nullptr)
  346. {
  347. // create depth buffer
  348. DKTextureDescriptor texDesc = {};
  349. texDesc.textureType = DKTexture::Type2D;
  350. texDesc.pixelFormat = DKPixelFormat::D32Float;
  351. texDesc.width = width;
  352. texDesc.height = height;
  353. texDesc.depth = 1;
  354. texDesc.mipmapLevels = 1;
  355. texDesc.sampleCount = 1;
  356. texDesc.arrayLength = 1;
  357. texDesc.usage = DKTexture::UsageRenderTarget;
  358. depthBuffer = device->CreateTexture(texDesc);
  359. }
  360. rpd.depthStencilAttachment.renderTarget = depthBuffer;
  361. rpd.depthStencilAttachment.loadAction = DKRenderPassAttachmentDescriptor::LoadActionClear;
  362. rpd.depthStencilAttachment.storeAction = DKRenderPassAttachmentDescriptor::StoreActionDontCare;
  363. DKObject<DKCommandBuffer> computeCmdbuffer = computeQueue->CreateCommandBuffer();
  364. DKObject<DKComputeCommandEncoder> computeEncoder = computeCmdbuffer->CreateComputeCommandEncoder();
  365. if (computeEncoder)
  366. {
  367. if (computebindSet)
  368. {
  369. computebindSet->SetTexture(0, sourceTexture);
  370. computebindSet->SetTexture(1, targetTexture);
  371. }
  372. computeEncoder->SetComputePipelineState(emboss);
  373. computeEncoder->SetResources(0, computebindSet);
  374. computeEncoder->Dispatch(targetTexture->Width() / cs_e->threadgroupSize.x,
  375. targetTexture->Height() / cs_e->threadgroupSize.y,
  376. 1);
  377. computeEncoder->EndEncoding();
  378. }
  379. DKObject<DKCommandBuffer> buffer = graphicsQueue->CreateCommandBuffer();
  380. DKObject<DKRenderCommandEncoder> encoder = buffer->CreateRenderCommandEncoder(rpd);
  381. if (encoder)
  382. {
  383. if (graphicShaderBindingSet->PostcomputeDescSet() && ubo)
  384. {
  385. graphicShaderBindingSet->PostcomputeDescSet()->SetBuffer(0, uboBuffer, 0, sizeof(GraphicShaderBindingSet::UBO));
  386. graphicShaderBindingSet->PostcomputeDescSet()->SetTexture(1, targetTexture);
  387. graphicShaderBindingSet->PostcomputeDescSet()->SetSamplerState(1, sampler);
  388. }
  389. encoder->SetRenderPipelineState(pipelineState);
  390. encoder->SetVertexBuffer(quad->VertexBuffer(), 0, 0);
  391. encoder->SetIndexBuffer(quad->IndexBuffer(), 0, DKIndexType::UInt32);
  392. encoder->SetResources(0, graphicShaderBindingSet->PostcomputeDescSet());
  393. // draw scene!
  394. encoder->DrawIndexed(quad->IndicesCount(), 1, 0, 0, 0);
  395. encoder->EndEncoding();
  396. if (computeCmdbuffer)
  397. computeCmdbuffer->Commit();
  398. buffer->Commit();
  399. swapChain->Present();
  400. }
  401. else
  402. {
  403. }
  404. DKThread::Sleep(0.01);
  405. }
  406. DKLog("RenderThread terminating...");
  407. }
  408. void OnInitialize(void) override
  409. {
  410. SampleApp::OnInitialize();
  411. DKLogD("%s", DKGL_FUNCTION_NAME);
  412. // create window
  413. window = DKWindow::Create("DefaultWindow");
  414. window->SetOrigin({ 0, 0 });
  415. window->Resize({ 512, 512 });
  416. window->Activate();
  417. window->AddEventHandler(this, DKFunction([this](const DKWindow::WindowEvent& e)
  418. {
  419. if (e.type == DKWindow::WindowEvent::WindowClosed)
  420. DKApplication::Instance()->Terminate(0);
  421. }), NULL, NULL);
  422. quad = DKOBJECT_NEW UVQuad();
  423. runningRenderThread = 1;
  424. renderThread = DKThread::Create(DKFunction(this, &ComputeShaderDemo::RenderThread)->Invocation());
  425. }
  426. void OnTerminate(void) override
  427. {
  428. DKLogD("%s", DKGL_FUNCTION_NAME);
  429. runningRenderThread = 0;
  430. renderThread->WaitTerminate();
  431. renderThread = NULL;
  432. window = NULL;
  433. SampleApp::OnTerminate();
  434. }
  435. };
  436. #ifdef _WIN32
  437. int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
  438. _In_opt_ HINSTANCE hPrevInstance,
  439. _In_ LPWSTR lpCmdLine,
  440. _In_ int nCmdShow)
  441. #else
  442. int main(int argc, const char * argv[])
  443. #endif
  444. {
  445. ComputeShaderDemo app;
  446. DKPropertySet::SystemConfig().SetValue("AppDelegate", "AppDelegate");
  447. DKPropertySet::SystemConfig().SetValue("GraphicsAPI", "Vulkan");
  448. return app.Run();
  449. }