DKGL2 sample codes

Triangle.cpp 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include "app.h"
  2. #include "util.h"
  3. class TriangleDemo : public SampleApp
  4. {
  5. DKObject<DKWindow> window;
  6. DKObject<DKThread> renderThread;
  7. DKAtomicNumber32 runningRenderThread;
  8. public:
  9. void RenderThread(void)
  10. {
  11. DKObject<DKData> vertData = resourcePool.LoadResourceData("triangle.vert.spv");
  12. DKObject<DKData> fragData = resourcePool.LoadResourceData("triangle.frag.spv");
  13. DKShader vertShader(vertData);
  14. DKShader fragShader(fragData);
  15. DKObject<DKGraphicsDevice> device = DKGraphicsDevice::SharedInstance();
  16. DKObject<DKShaderModule> vertShaderModule = device->CreateShaderModule(&vertShader);
  17. DKObject<DKShaderModule> fragShaderModule = device->CreateShaderModule(&fragShader);
  18. DKObject<DKShaderFunction> vertShaderFunction = vertShaderModule->CreateFunction(vertShaderModule->FunctionNames().Value(0));
  19. DKObject<DKShaderFunction> fragShaderFunction = fragShaderModule->CreateFunction(fragShaderModule->FunctionNames().Value(0));
  20. DKObject<DKCommandQueue> queue = device->CreateCommandQueue(DKCommandQueue::Graphics);
  21. DKObject<DKSwapChain> swapChain = queue->CreateSwapChain(window);
  22. DKLog("VertexFunction.VertexAttributes: %d", vertShaderFunction->StageInputAttributes().Count());
  23. for (int i = 0; i < vertShaderFunction->StageInputAttributes().Count(); ++i)
  24. {
  25. const DKShaderAttribute& attr = vertShaderFunction->StageInputAttributes().Value(i);
  26. DKLog(" --> VertexAttribute[%d]: \"%ls\" (location:%u)", i, (const wchar_t*)attr.name, attr.location);
  27. }
  28. struct Vertex
  29. {
  30. DKVector3 position;
  31. DKVector3 color;
  32. };
  33. DKArray<Vertex> vertexData =
  34. {
  35. { { 0.0f, -0.5f, 0.0f },{ 1.0f, 1.0f, 1.0f } },
  36. { { 0.5f, 0.5f, 0.0f },{ 0.0f, 1.0f, 0.0f } },
  37. { { -0.5f, 0.5f, 0.0f },{ 0.0f, 0.0f, 1.0f } }
  38. };
  39. uint32_t vertexBufferSize = static_cast<uint32_t>(vertexData.Count()) * sizeof(Vertex);
  40. DKArray<uint32_t> indexData = { 0, 1, 2 };
  41. uint32_t indexBufferSize = indexData.Count() * sizeof(uint32_t);
  42. DKObject<DKGpuBuffer> vertexBuffer = device->CreateBuffer(vertexBufferSize, DKGpuBuffer::StorageModeShared, DKCpuCacheModeDefault);
  43. memcpy(vertexBuffer->Lock(), vertexData, vertexBufferSize);
  44. vertexBuffer->Unlock();
  45. DKObject<DKGpuBuffer> indexBuffer = device->CreateBuffer(indexBufferSize, DKGpuBuffer::StorageModeShared, DKCpuCacheModeDefault);
  46. memcpy(indexBuffer->Lock(), indexData, indexBufferSize);
  47. indexBuffer->Unlock();
  48. DKRenderPipelineDescriptor pipelineDescriptor;
  49. pipelineDescriptor.vertexFunction = vertShaderFunction;
  50. pipelineDescriptor.fragmentFunction = fragShaderFunction;
  51. pipelineDescriptor.colorAttachments.Resize(1);
  52. pipelineDescriptor.colorAttachments.Value(0).pixelFormat = swapChain->ColorPixelFormat();
  53. pipelineDescriptor.depthStencilAttachmentPixelFormat = DKPixelFormat::Invalid; // no depth buffer
  54. pipelineDescriptor.vertexDescriptor.attributes = {
  55. { DKVertexFormat::Float3, 0, 0, 0 },
  56. { DKVertexFormat::Float3, sizeof(DKVector3), 0, 1 },
  57. };
  58. pipelineDescriptor.vertexDescriptor.layouts = {
  59. { DKVertexStepRate::Vertex, sizeof(Vertex), 0 },
  60. };
  61. pipelineDescriptor.primitiveTopology = DKPrimitiveType::Triangle;
  62. pipelineDescriptor.frontFace = DKFrontFace::CCW;
  63. pipelineDescriptor.triangleFillMode = DKTriangleFillMode::Fill;
  64. pipelineDescriptor.depthClipMode = DKDepthClipMode::Clip;
  65. pipelineDescriptor.cullMode = DKCullMode::None;
  66. pipelineDescriptor.rasterizationEnabled = true;
  67. DKPipelineReflection reflection;
  68. DKObject<DKRenderPipelineState> pipelineState = device->CreateRenderPipeline(pipelineDescriptor, &reflection);
  69. if (pipelineState)
  70. {
  71. PrintPipelineReflection(&reflection, DKLogCategory::Verbose);
  72. }
  73. DKShaderBindingSetLayout layout;
  74. if (1)
  75. {
  76. DKShaderBinding binding = {
  77. 0,
  78. DKShader::DescriptorTypeUniformBuffer,
  79. 1,
  80. nullptr
  81. };
  82. layout.bindings.Add(binding);
  83. }
  84. DKObject<DKShaderBindingSet> bindSet = device->CreateShaderBindingSet(layout);
  85. if (bindSet)
  86. {
  87. struct
  88. {
  89. DKMatrix4 projectionMatrix;
  90. DKMatrix4 modelMatrix;
  91. DKMatrix4 viewMatrix;
  92. } ubo;
  93. DKObject<DKGpuBuffer> uboBuffer = device->CreateBuffer(sizeof(ubo), DKGpuBuffer::StorageModeShared, DKCpuCacheModeDefault);
  94. if (uboBuffer)
  95. {
  96. ubo.projectionMatrix = DKMatrix4::identity;
  97. ubo.modelMatrix = DKMatrix4::identity;
  98. ubo.viewMatrix = DKMatrix4::identity;
  99. void* p = uboBuffer->Lock(0);
  100. if (p)
  101. {
  102. memcpy(p, &ubo, sizeof(ubo));
  103. uboBuffer->Unlock();
  104. bindSet->SetBuffer(0, uboBuffer, 0, sizeof(ubo));
  105. }
  106. else
  107. {
  108. DKLogE("GpuBuffer Lock failed!");
  109. }
  110. }
  111. }
  112. DKTimer timer;
  113. timer.Reset();
  114. DKLog("Render thread begin");
  115. while (!runningRenderThread.CompareAndSet(0, 0))
  116. {
  117. DKRenderPassDescriptor rpd = swapChain->CurrentRenderPassDescriptor();
  118. double t = timer.Elapsed();
  119. t = (cos(t) + 1.0) * 0.5;
  120. rpd.colorAttachments.Value(0).clearColor = DKColor(t, 0.0, 0.0, 0.0);
  121. DKObject<DKCommandBuffer> buffer = queue->CreateCommandBuffer();
  122. DKObject<DKRenderCommandEncoder> encoder = buffer->CreateRenderCommandEncoder(rpd);
  123. if (encoder)
  124. {
  125. encoder->SetRenderPipelineState(pipelineState);
  126. encoder->SetVertexBuffer(vertexBuffer, 0, 0);
  127. encoder->SetIndexBuffer(indexBuffer, 0, DKIndexType::UInt32);
  128. encoder->SetResources(0, bindSet);
  129. // draw scene!
  130. encoder->DrawIndexed(indexData.Count(), 1, 0, 0, 1);
  131. encoder->EndEncoding();
  132. buffer->Commit();
  133. swapChain->Present();
  134. }
  135. else
  136. {
  137. }
  138. DKThread::Sleep(0.01);
  139. }
  140. DKLog("RenderThread terminating...");
  141. }
  142. void OnInitialize(void) override
  143. {
  144. SampleApp::OnInitialize();
  145. DKLogD("%s", DKGL_FUNCTION_NAME);
  146. // create window
  147. window = DKWindow::Create("DefaultWindow");
  148. window->SetOrigin({ 0, 0 });
  149. window->Resize({ 320, 240 });
  150. window->Activate();
  151. window->AddEventHandler(this, DKFunction([this](const DKWindow::WindowEvent& e)
  152. {
  153. if (e.type == DKWindow::WindowEvent::WindowClosed)
  154. DKApplication::Instance()->Terminate(0);
  155. }), NULL, NULL);
  156. runningRenderThread = 1;
  157. renderThread = DKThread::Create(DKFunction(this, &TriangleDemo::RenderThread)->Invocation());
  158. }
  159. void OnTerminate(void) override
  160. {
  161. DKLogD("%s", DKGL_FUNCTION_NAME);
  162. runningRenderThread = 0;
  163. renderThread->WaitTerminate();
  164. renderThread = NULL;
  165. window = NULL;
  166. SampleApp::OnTerminate();
  167. }
  168. };
  169. #ifdef _WIN32
  170. int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
  171. _In_opt_ HINSTANCE hPrevInstance,
  172. _In_ LPWSTR lpCmdLine,
  173. _In_ int nCmdShow)
  174. #else
  175. int main(int argc, const char * argv[])
  176. #endif
  177. {
  178. TriangleDemo app;
  179. DKPropertySet::SystemConfig().SetValue("AppDelegate", "AppDelegate");
  180. DKPropertySet::SystemConfig().SetValue("GraphicsAPI", "Vulkan");
  181. return app.Run();
  182. }