No Description

TestApp1.cpp 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // TestApp1.cpp : Defines the entry point for the application.
  2. //
  3. #ifdef _WIN32
  4. #include "Win32/stdafx.h"
  5. #endif
  6. #include <DK.h>
  7. class TestApp1 : public DKApplication
  8. {
  9. DKObject<DKWindow> window;
  10. DKObject<DKThread> renderThread;
  11. DKResourcePool resourcePool;
  12. DKAtomicNumber32 runningRenderThread;
  13. public:
  14. void RenderThread(void)
  15. {
  16. DKObject<DKData> vertData = resourcePool.LoadResourceData("triangle.vert.spv");
  17. DKObject<DKData> fragData = resourcePool.LoadResourceData("triangle.frag.spv");
  18. DKShader vertShader(vertData, DKShader::Vertex);
  19. DKShader fragShader(fragData, DKShader::Fragment);
  20. DKObject<DKGraphicsDevice> device = DKGraphicsDevice::SharedInstance();
  21. DKObject<DKShaderModule> vertShaderModule = device->CreateShaderModule(&vertShader);
  22. DKObject<DKShaderModule> fragShaderModule = device->CreateShaderModule(&fragShader);
  23. DKObject<DKShaderFunction> vertShaderFunction = vertShaderModule->CreateFunction(vertShaderModule->FunctionNames().Value(0));
  24. DKObject<DKShaderFunction> fragShaderFunction = fragShaderModule->CreateFunction(fragShaderModule->FunctionNames().Value(0));
  25. DKObject<DKCommandQueue> queue = device->CreateCommandQueue(DKCommandQueue::Graphics);
  26. DKObject<DKSwapChain> swapChain = queue->CreateSwapChain(window);
  27. DKLog("VertexFunction.VertexAttributes: %d", vertShaderFunction->VertexAttributes().Count());
  28. for (int i = 0; i < vertShaderFunction->VertexAttributes().Count(); ++i)
  29. {
  30. const DKVertexAttribute& attr = vertShaderFunction->VertexAttributes().Value(i);
  31. DKLog(" --> VertexAttribute[%d]: \"%ls\" (location:%u)", i, (const wchar_t*)attr.name, attr.location);
  32. }
  33. struct Vertex
  34. {
  35. DKVector3 position;
  36. DKVector3 color;
  37. };
  38. DKArray<Vertex> vertexData =
  39. {
  40. { { 0.0f, -0.5f, 0.0f },{ 1.0f, 1.0f, 1.0f } },
  41. { { 0.5f, 0.5f, 0.0f },{ 0.0f, 1.0f, 0.0f } },
  42. { { -0.5f, 0.5f, 0.0f },{ 0.0f, 0.0f, 1.0f } }
  43. };
  44. uint32_t vertexBufferSize = static_cast<uint32_t>(vertexData.Count()) * sizeof(Vertex);
  45. DKArray<uint32_t> indexData = { 0, 1, 2 };
  46. uint32_t indexBufferSize = indexData.Count() * sizeof(uint32_t);
  47. DKObject<DKGpuBuffer> vertexBuffer = device->CreateBuffer(vertexBufferSize, DKGpuBuffer::StorageModeShared, DKCpuCacheModeDefault);
  48. memcpy(vertexBuffer->Lock(), vertexData, vertexBufferSize);
  49. vertexBuffer->Unlock();
  50. DKObject<DKGpuBuffer> indexBuffer = device->CreateBuffer(indexBufferSize, DKGpuBuffer::StorageModeShared, DKCpuCacheModeDefault);
  51. memcpy(indexBuffer->Lock(), indexData, indexBufferSize);
  52. indexBuffer->Unlock();
  53. DKRenderPipelineDescriptor pipelineDescriptor;
  54. pipelineDescriptor.vertexFunction = vertShaderFunction;
  55. pipelineDescriptor.fragmentFunction = fragShaderFunction;
  56. pipelineDescriptor.colorAttachments.Resize(1);
  57. pipelineDescriptor.colorAttachments.Value(0).pixelFormat = swapChain->ColorPixelFormat();
  58. pipelineDescriptor.depthStencilAttachmentPixelFormat = DKPixelFormat::Invalid; // no depth buffer
  59. pipelineDescriptor.vertexDescriptor.attributes = {
  60. { DKVertexFormat::Float3, 0, 0, 0 },
  61. { DKVertexFormat::Float3, sizeof(DKVector3), 0, 1 },
  62. };
  63. pipelineDescriptor.vertexDescriptor.layouts = {
  64. { DKVertexStepRate::Vertex, sizeof(Vertex), 0 },
  65. };
  66. pipelineDescriptor.primitiveTopology = DKPrimitiveType::Triangle;
  67. pipelineDescriptor.frontFace = DKFrontFace::CCW;
  68. pipelineDescriptor.triangleFillMode = DKTriangleFillMode::Fill;
  69. pipelineDescriptor.depthClipMode = DKDepthClipMode::Clip;
  70. pipelineDescriptor.cullMode = DKCullMode::None;
  71. pipelineDescriptor.rasterizationEnabled = true;
  72. DKRenderPipelineReflection reflection;
  73. DKObject<DKRenderPipelineState> pipelineState = device->CreateRenderPipeline(pipelineDescriptor, &reflection);
  74. if (pipelineState)
  75. {
  76. auto printShaderArg = [](const DKShaderResource& arg)
  77. {
  78. const char* argType = "Unknown";
  79. switch (arg.type)
  80. {
  81. case DKShaderResource::TypeBuffer: argType = "Buffer"; break;
  82. case DKShaderResource::TypeTexture: argType = "Texture"; break;
  83. case DKShaderResource::TypeSampler: argType = "Sampler"; break;
  84. case DKShaderResource::TypeThreadgroupMemory: argType = "ThreadMemory"; break;
  85. }
  86. DKLog(" --> \"%ls\"[%u] binding:%u:%u type:%s", (const wchar_t*)arg.name, uint32_t(arg.count), arg.set, arg.binding, argType);
  87. };
  88. DKLog("PipelineReflection.VertexResources: %d", reflection.vertexResources.Count());
  89. for (auto& arg : reflection.vertexResources)
  90. printShaderArg(arg);
  91. DKLog("PipelineReflection.FragmentResources: %d", reflection.fragmentResources.Count());
  92. for (auto& arg : reflection.fragmentResources)
  93. printShaderArg(arg);
  94. }
  95. DKTimer timer;
  96. timer.Reset();
  97. DKLog("Render thread begin");
  98. while (!runningRenderThread.CompareAndSet(0, 0))
  99. {
  100. DKRenderPassDescriptor rpd = swapChain->CurrentRenderPassDescriptor();
  101. double t = timer.Elapsed();
  102. t = (cos(t) + 1.0) * 0.5;
  103. rpd.colorAttachments.Value(0).clearColor = DKColor(t, 0.0, 0.0, 0.0);
  104. DKObject<DKCommandBuffer> buffer = queue->CreateCommandBuffer();
  105. DKObject<DKRenderCommandEncoder> encoder = buffer->CreateRenderCommandEncoder(rpd);
  106. if (encoder)
  107. {
  108. encoder->SetRenderPipelineState(pipelineState);
  109. encoder->SetVertexBuffer(vertexBuffer, 0, 0);
  110. encoder->SetIndexBuffer(indexBuffer, 0, DKIndexType::UInt32);
  111. // draw scene!
  112. encoder->DrawIndexed(indexData.Count(), 1, 0, 0, 1);
  113. encoder->EndEncoding();
  114. buffer->Commit();
  115. swapChain->Present();
  116. }
  117. else
  118. {
  119. }
  120. DKThread::Sleep(0.01);
  121. }
  122. DKLog("RenderThread terminating...");
  123. }
  124. void OnInitialize(void) override
  125. {
  126. DKLogD("%s", DKGL_FUNCTION_NAME);
  127. DKString resPath = DefaultPath(SystemPath::AppResource);
  128. resPath = resPath.FilePathStringByAppendingPath("Data");
  129. DKLog("resPath: %ls", (const wchar_t*)resPath);
  130. resourcePool.AddLocatorForPath(resPath);
  131. window = DKWindow::Create("DefaultWindow");
  132. window->SetOrigin({ 0, 0 });
  133. window->Resize({ 320, 240 });
  134. window->Activate();
  135. window->AddEventHandler(this,
  136. DKFunction([this](const DKWindow::WindowEvent& e) {
  137. if (e.type == DKWindow::WindowEvent::WindowClosed)
  138. DKApplication::Instance()->Terminate(0);
  139. }),
  140. NULL, NULL);
  141. runningRenderThread = 1;
  142. renderThread = DKThread::Create(DKFunction(this, &TestApp1::RenderThread)->Invocation());
  143. }
  144. void OnTerminate(void) override
  145. {
  146. DKLogD("%s", DKGL_FUNCTION_NAME);
  147. runningRenderThread = 0;
  148. renderThread->WaitTerminate();
  149. renderThread = NULL;
  150. window = NULL;
  151. DKLogI("Memory Pool Statistics");
  152. size_t numBuckets = DKMemoryPoolNumberOfBuckets();
  153. DKMemoryPoolBucketStatus* buckets = new DKMemoryPoolBucketStatus[numBuckets];
  154. DKMemoryPoolQueryAllocationStatus(buckets, numBuckets);
  155. size_t usedBytes = 0;
  156. for (int i = 0; i < numBuckets; ++i)
  157. {
  158. if (buckets[i].totalChunks > 0)
  159. {
  160. DKLogI("--> %5lu: %5lu/%5lu, usage: %.1f%%, used: %.1fKB, total: %.1fKB",
  161. buckets[i].chunkSize,
  162. buckets[i].usedChunks, buckets[i].totalChunks,
  163. double(buckets[i].usedChunks) / double(buckets[i].totalChunks) * 100.0,
  164. double(buckets[i].chunkSize * buckets[i].usedChunks) / 1024.0,
  165. double(buckets[i].chunkSize * buckets[i].totalChunks) / 1024.0
  166. );
  167. usedBytes += buckets[i].chunkSize * buckets[i].usedChunks;
  168. }
  169. }
  170. DKLogI("MemoryPool Usage: %.1fMB / %.1fMB", double(usedBytes) / (1024 * 1024), double(DKMemoryPoolSize()) / (1024 * 1024));
  171. delete[] buckets;
  172. }
  173. };
  174. #ifdef _WIN32
  175. int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
  176. _In_opt_ HINSTANCE hPrevInstance,
  177. _In_ LPWSTR lpCmdLine,
  178. _In_ int nCmdShow)
  179. #else
  180. int main(int argc, const char * argv[])
  181. #endif
  182. {
  183. TestApp1 app;
  184. DKPropertySet::SystemConfig().SetValue("AppDelegate", "AppDelegate");
  185. DKPropertySet::SystemConfig().SetValue("GraphicsAPI", "Vulkan");
  186. return app.Run();
  187. }