DKGL2 sample codes

Triangle.cpp 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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("shaders/triangle.vert.spv");
  12. DKObject<DKData> fragData = resourcePool.LoadResourceData("shaders/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, DKCpuCacheModeReadWrite);
  43. memcpy(vertexBuffer->Contents(), vertexData, vertexBufferSize);
  44. DKObject<DKGpuBuffer> indexBuffer = device->CreateBuffer(indexBufferSize, DKGpuBuffer::StorageModeShared, DKCpuCacheModeReadWrite);
  45. memcpy(indexBuffer->Contents(), indexData, indexBufferSize);
  46. DKRenderPipelineDescriptor pipelineDescriptor;
  47. pipelineDescriptor.vertexFunction = vertShaderFunction;
  48. pipelineDescriptor.fragmentFunction = fragShaderFunction;
  49. pipelineDescriptor.colorAttachments.Resize(1);
  50. pipelineDescriptor.colorAttachments.Value(0).pixelFormat = swapChain->ColorPixelFormat();
  51. pipelineDescriptor.depthStencilAttachmentPixelFormat = DKPixelFormat::Invalid; // no depth buffer
  52. pipelineDescriptor.vertexDescriptor.attributes = {
  53. { DKVertexFormat::Float3, 0, 0, 0 },
  54. { DKVertexFormat::Float3, sizeof(DKVector3), 0, 1 },
  55. };
  56. pipelineDescriptor.vertexDescriptor.layouts = {
  57. { DKVertexStepRate::Vertex, sizeof(Vertex), 0 },
  58. };
  59. pipelineDescriptor.primitiveTopology = DKPrimitiveType::Triangle;
  60. pipelineDescriptor.frontFace = DKFrontFace::CCW;
  61. pipelineDescriptor.triangleFillMode = DKTriangleFillMode::Fill;
  62. pipelineDescriptor.depthClipMode = DKDepthClipMode::Clip;
  63. pipelineDescriptor.cullMode = DKCullMode::None;
  64. pipelineDescriptor.rasterizationEnabled = true;
  65. DKPipelineReflection reflection;
  66. DKObject<DKRenderPipelineState> pipelineState = device->CreateRenderPipeline(pipelineDescriptor, &reflection);
  67. if (pipelineState)
  68. {
  69. PrintPipelineReflection(&reflection, DKLogCategory::Verbose);
  70. }
  71. DKShaderBindingSetLayout layout;
  72. if (1)
  73. {
  74. DKShaderBinding binding = {
  75. 0,
  76. DKShader::DescriptorTypeUniformBuffer,
  77. 1,
  78. nullptr
  79. };
  80. layout.bindings.Add(binding);
  81. }
  82. DKObject<DKShaderBindingSet> bindSet = device->CreateShaderBindingSet(layout);
  83. if (bindSet)
  84. {
  85. struct
  86. {
  87. DKMatrix4 projectionMatrix;
  88. DKMatrix4 modelMatrix;
  89. DKMatrix4 viewMatrix;
  90. } ubo;
  91. DKObject<DKGpuBuffer> uboBuffer = device->CreateBuffer(sizeof(ubo), DKGpuBuffer::StorageModeShared, DKCpuCacheModeReadWrite);
  92. if (uboBuffer)
  93. {
  94. ubo.projectionMatrix = DKMatrix4::identity;
  95. ubo.modelMatrix = DKMatrix4::identity;
  96. ubo.viewMatrix = DKMatrix4::identity;
  97. memcpy(uboBuffer->Contents(), &ubo, sizeof(ubo));
  98. bindSet->SetBuffer(0, uboBuffer, 0, sizeof(ubo));
  99. }
  100. }
  101. DKTimer timer;
  102. timer.Reset();
  103. DKLog("Render thread begin");
  104. while (!runningRenderThread.CompareAndSet(0, 0))
  105. {
  106. DKRenderPassDescriptor rpd = swapChain->CurrentRenderPassDescriptor();
  107. double t = timer.Elapsed();
  108. t = (cos(t) + 1.0) * 0.5;
  109. rpd.colorAttachments.Value(0).clearColor = DKColor(t, 0.0, 0.0, 0.0);
  110. DKObject<DKCommandBuffer> buffer = queue->CreateCommandBuffer();
  111. DKObject<DKRenderCommandEncoder> encoder = buffer->CreateRenderCommandEncoder(rpd);
  112. if (encoder)
  113. {
  114. encoder->SetRenderPipelineState(pipelineState);
  115. encoder->SetVertexBuffer(vertexBuffer, 0, 0);
  116. encoder->SetIndexBuffer(indexBuffer, 0, DKIndexType::UInt32);
  117. encoder->SetResources(0, bindSet);
  118. // draw scene!
  119. encoder->DrawIndexed(indexData.Count(), 1, 0, 0, 0);
  120. encoder->EndEncoding();
  121. buffer->Commit();
  122. swapChain->Present();
  123. }
  124. else
  125. {
  126. }
  127. DKThread::Sleep(0.01);
  128. }
  129. DKLog("RenderThread terminating...");
  130. }
  131. void OnInitialize(void) override
  132. {
  133. SampleApp::OnInitialize();
  134. DKLogD("%s", DKGL_FUNCTION_NAME);
  135. // create window
  136. window = DKWindow::Create("DefaultWindow");
  137. window->SetOrigin({ 0, 0 });
  138. window->Resize({ 320, 240 });
  139. window->Activate();
  140. window->AddEventHandler(this, DKFunction([this](const DKWindow::WindowEvent& e)
  141. {
  142. if (e.type == DKWindow::WindowEvent::WindowClosed)
  143. DKApplication::Instance()->Terminate(0);
  144. }), NULL, NULL);
  145. runningRenderThread = 1;
  146. renderThread = DKThread::Create(DKFunction(this, &TriangleDemo::RenderThread)->Invocation());
  147. }
  148. void OnTerminate(void) override
  149. {
  150. DKLogD("%s", DKGL_FUNCTION_NAME);
  151. runningRenderThread = 0;
  152. renderThread->WaitTerminate();
  153. renderThread = NULL;
  154. window = NULL;
  155. SampleApp::OnTerminate();
  156. }
  157. };
  158. #ifdef _WIN32
  159. int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
  160. _In_opt_ HINSTANCE hPrevInstance,
  161. _In_ LPWSTR lpCmdLine,
  162. _In_ int nCmdShow)
  163. #else
  164. int main(int argc, const char * argv[])
  165. #endif
  166. {
  167. TriangleDemo app;
  168. DKPropertySet::SystemConfig().SetValue("AppDelegate", "AppDelegate");
  169. DKPropertySet::SystemConfig().SetValue("GraphicsAPI", "Vulkan");
  170. return app.Run();
  171. }