DKGL2 sample codes

Texture.cpp 8.1KB

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