DKGL2 sample codes

Texture.cpp 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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, DKCpuCacheModeDefault);
  71. memcpy(vertexBuffer->Lock(), vertexData, vertexBufferSize);
  72. vertexBuffer->Unlock();
  73. DKObject<DKGpuBuffer> indexBuffer = device->CreateBuffer(indexBufferSize, DKGpuBuffer::StorageModeShared, DKCpuCacheModeDefault);
  74. memcpy(indexBuffer->Lock(), indexData, indexBufferSize);
  75. indexBuffer->Unlock();
  76. DKRenderPipelineDescriptor pipelineDescriptor;
  77. pipelineDescriptor.vertexFunction = vertShaderFunction;
  78. pipelineDescriptor.fragmentFunction = fragShaderFunction;
  79. pipelineDescriptor.colorAttachments.Resize(1);
  80. pipelineDescriptor.colorAttachments.Value(0).pixelFormat = swapChain->ColorPixelFormat();
  81. pipelineDescriptor.depthStencilAttachmentPixelFormat = DKPixelFormat::Invalid; // no depth buffer
  82. pipelineDescriptor.vertexDescriptor.attributes = {
  83. { DKVertexFormat::Float3, 0, 0, 0 },
  84. { DKVertexFormat::Float2, offsetof(Vertex, inUV), 0, 1 },
  85. { DKVertexFormat::Float3, offsetof(Vertex, inNormal), 0, 2 },
  86. };
  87. pipelineDescriptor.vertexDescriptor.layouts = {
  88. { DKVertexStepRate::Vertex, sizeof(Vertex), 0 },
  89. };
  90. pipelineDescriptor.primitiveTopology = DKPrimitiveType::Triangle;
  91. pipelineDescriptor.frontFace = DKFrontFace::CCW;
  92. pipelineDescriptor.triangleFillMode = DKTriangleFillMode::Fill;
  93. pipelineDescriptor.depthClipMode = DKDepthClipMode::Clip;
  94. pipelineDescriptor.cullMode = DKCullMode::None;
  95. pipelineDescriptor.rasterizationEnabled = true;
  96. DKPipelineReflection reflection;
  97. DKObject<DKRenderPipelineState> pipelineState = device->CreateRenderPipeline(pipelineDescriptor, &reflection);
  98. if (pipelineState)
  99. {
  100. PrintPipelineReflection(&reflection, DKLogCategory::Verbose);
  101. }
  102. DKShaderBindingSetLayout layout;
  103. if (1)
  104. {
  105. DKShaderBinding binding = {
  106. 0,
  107. DKShader::DescriptorTypeUniformBuffer,
  108. 1,
  109. nullptr
  110. };
  111. layout.bindings.Add(binding);
  112. }
  113. DKObject<DKShaderBindingSet> bindSet = device->CreateShaderBindingSet(layout);
  114. if (bindSet)
  115. {
  116. struct
  117. {
  118. DKMatrix4 projectionMatrix;
  119. DKMatrix4 modelMatrix;
  120. DKMatrix4 viewMatrix;
  121. } ubo;
  122. DKObject<DKGpuBuffer> uboBuffer = device->CreateBuffer(sizeof(ubo), DKGpuBuffer::StorageModeShared, DKCpuCacheModeDefault);
  123. if (uboBuffer)
  124. {
  125. ubo.projectionMatrix = DKMatrix4::identity;
  126. ubo.modelMatrix = DKMatrix4::identity;
  127. ubo.viewMatrix = DKMatrix4::identity;
  128. void* p = uboBuffer->Lock(0);
  129. if (p)
  130. {
  131. memcpy(p, &ubo, sizeof(ubo));
  132. uboBuffer->Unlock();
  133. bindSet->SetBuffer(0, uboBuffer, 0, sizeof(ubo));
  134. }
  135. else
  136. {
  137. DKLogE("GpuBuffer Lock failed!");
  138. }
  139. }
  140. }
  141. DKTimer timer;
  142. timer.Reset();
  143. DKLog("Render thread begin");
  144. while (!runningRenderThread.CompareAndSet(0, 0))
  145. {
  146. DKRenderPassDescriptor rpd = swapChain->CurrentRenderPassDescriptor();
  147. double t = timer.Elapsed();
  148. t = (cos(t) + 1.0) * 0.5;
  149. rpd.colorAttachments.Value(0).clearColor = DKColor(t, 0.0, 0.0, 0.0);
  150. DKObject<DKCommandBuffer> buffer = queue->CreateCommandBuffer();
  151. DKObject<DKRenderCommandEncoder> encoder = buffer->CreateRenderCommandEncoder(rpd);
  152. if (encoder)
  153. {
  154. encoder->SetRenderPipelineState(pipelineState);
  155. encoder->SetVertexBuffer(vertexBuffer, 0, 0);
  156. encoder->SetIndexBuffer(indexBuffer, 0, DKIndexType::UInt32);
  157. encoder->SetResources(0, bindSet);
  158. // draw scene!
  159. encoder->DrawIndexed(indexData.Count(), 1, 0, 0, 0);
  160. encoder->EndEncoding();
  161. buffer->Commit();
  162. swapChain->Present();
  163. }
  164. else
  165. {
  166. }
  167. DKThread::Sleep(0.01);
  168. }
  169. DKLog("RenderThread terminating...");
  170. }
  171. void OnInitialize(void) override
  172. {
  173. SampleApp::OnInitialize();
  174. DKLogD("%s", DKGL_FUNCTION_NAME);
  175. // create window
  176. window = DKWindow::Create("DefaultWindow");
  177. window->SetOrigin({ 0, 0 });
  178. window->Resize({ 320, 240 });
  179. window->Activate();
  180. window->AddEventHandler(this, DKFunction([this](const DKWindow::WindowEvent& e)
  181. {
  182. if (e.type == DKWindow::WindowEvent::WindowClosed)
  183. DKApplication::Instance()->Terminate(0);
  184. }), NULL, NULL);
  185. runningRenderThread = 1;
  186. renderThread = DKThread::Create(DKFunction(this, &TextureDemo::RenderThread)->Invocation());
  187. }
  188. void OnTerminate(void) override
  189. {
  190. DKLogD("%s", DKGL_FUNCTION_NAME);
  191. runningRenderThread = 0;
  192. renderThread->WaitTerminate();
  193. renderThread = NULL;
  194. window = NULL;
  195. SampleApp::OnTerminate();
  196. }
  197. };
  198. #ifdef _WIN32
  199. int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
  200. _In_opt_ HINSTANCE hPrevInstance,
  201. _In_ LPWSTR lpCmdLine,
  202. _In_ int nCmdShow)
  203. #else
  204. int main(int argc, const char * argv[])
  205. #endif
  206. {
  207. TextureDemo app;
  208. DKPropertySet::SystemConfig().SetValue("AppDelegate", "AppDelegate");
  209. DKPropertySet::SystemConfig().SetValue("GraphicsAPI", "Vulkan");
  210. return app.Run();
  211. }