No Description

TestApp1.cpp 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. struct Vertex
  28. {
  29. DKVector3 position;
  30. DKVector3 color;
  31. };
  32. DKArray<Vertex> vertexData =
  33. {
  34. { { 0.0f, -0.5f, 0.0f },{ 1.0f, 1.0f, 1.0f } },
  35. { { 0.5f, 0.5f, 0.0f },{ 0.0f, 1.0f, 0.0f } },
  36. { { -0.5f, 0.5f, 0.0f },{ 0.0f, 0.0f, 1.0f } }
  37. };
  38. uint32_t vertexBufferSize = static_cast<uint32_t>(vertexData.Count()) * sizeof(Vertex);
  39. DKArray<uint32_t> indexData = { 0, 1, 2 };
  40. uint32_t indexBufferSize = indexData.Count() * sizeof(uint32_t);
  41. DKObject<DKGpuBuffer> vertexBuffer = device->CreateBuffer(vertexBufferSize, DKGpuBuffer::StorageModeShared, DKCpuCacheModeDefault);
  42. memcpy(vertexBuffer->Lock(), vertexData, vertexBufferSize);
  43. vertexBuffer->Unlock();
  44. DKObject<DKGpuBuffer> indexBuffer = device->CreateBuffer(indexBufferSize, DKGpuBuffer::StorageModeShared, DKCpuCacheModeDefault);
  45. memcpy(indexBuffer->Lock(), indexData, indexBufferSize);
  46. indexBuffer->Unlock();
  47. DKRenderPipelineDescriptor pipelineDescriptor;
  48. pipelineDescriptor.vertexFunction = vertShaderFunction;
  49. pipelineDescriptor.fragmentFunction = fragShaderFunction;
  50. pipelineDescriptor.colorAttachments.Resize(1);
  51. pipelineDescriptor.colorAttachments.Value(0).pixelFormat = swapChain->ColorPixelFormat();
  52. pipelineDescriptor.depthStencilAttachmentPixelFormat = DKPixelFormat::Invalid; // no depth buffer
  53. pipelineDescriptor.vertexDescriptor.attributes = {
  54. { DKVertexFormat::Float3, 0, 0, 0 },
  55. { DKVertexFormat::Float3, sizeof(DKVector3), 0, 1 },
  56. };
  57. pipelineDescriptor.vertexDescriptor.layouts = {
  58. { DKVertexStepRate::Vertex, sizeof(Vertex), 0 },
  59. };
  60. pipelineDescriptor.primitiveTopology = DKPrimitiveType::Triangle;
  61. pipelineDescriptor.frontFace = DKFrontFace::CCW;
  62. pipelineDescriptor.triangleFillMode = DKTriangleFillMode::Fill;
  63. pipelineDescriptor.depthClipMode = DKDepthClipMode::Clip;
  64. pipelineDescriptor.cullMode = DKCullMode::None;
  65. pipelineDescriptor.rasterizationEnabled = true;
  66. DKPipelineReflection reflection;
  67. DKObject<DKRenderPipelineState> pipelineState = device->CreateRenderPipeline(pipelineDescriptor, &reflection);
  68. DKTimer timer;
  69. timer.Reset();
  70. DKLog("Render thread begin");
  71. while (!runningRenderThread.CompareAndSet(0, 0))
  72. {
  73. DKRenderPassDescriptor rpd = swapChain->CurrentRenderPassDescriptor();
  74. double t = timer.Elapsed();
  75. t = (cos(t) + 1.0) * 0.5;
  76. rpd.colorAttachments.Value(0).clearColor = DKColor(t, 0.0, 0.0, 0.0);
  77. DKObject<DKCommandBuffer> buffer = queue->CreateCommandBuffer();
  78. DKObject<DKRenderCommandEncoder> encoder = buffer->CreateRenderCommandEncoder(rpd);
  79. if (encoder)
  80. {
  81. encoder->SetRenderPipelineState(pipelineState);
  82. encoder->SetVertexBuffer(vertexBuffer, 0, 0);
  83. encoder->SetIndexBuffer(indexBuffer, 0, DKIndexType::UInt32);
  84. // draw scene!
  85. encoder->DrawIndexed(indexData.Count(), 1, 0, 0, 1);
  86. encoder->EndEncoding();
  87. buffer->Commit();
  88. swapChain->Present();
  89. }
  90. else
  91. {
  92. }
  93. DKThread::Sleep(0.01);
  94. }
  95. DKLog("RenderThread terminating...");
  96. }
  97. void OnInitialize(void) override
  98. {
  99. DKLogD("%s", DKGL_FUNCTION_NAME);
  100. DKString resPath = DefaultPath(SystemPath::AppResource);
  101. resPath = resPath.FilePathStringByAppendingPath("Data");
  102. DKLog("resPath: %ls", (const wchar_t*)resPath);
  103. resourcePool.AddLocatorForPath(resPath);
  104. window = DKWindow::Create("DefaultWindow");
  105. window->SetOrigin({ 0, 0 });
  106. window->Resize({ 320, 240 });
  107. window->Activate();
  108. window->AddEventHandler(this,
  109. DKFunction([this](const DKWindow::WindowEvent& e) {
  110. if (e.type == DKWindow::WindowEvent::WindowClosed)
  111. DKApplication::Instance()->Terminate(0);
  112. }),
  113. NULL, NULL);
  114. runningRenderThread = 1;
  115. renderThread = DKThread::Create(DKFunction(this, &TestApp1::RenderThread)->Invocation());
  116. }
  117. void OnTerminate(void) override
  118. {
  119. DKLogD("%s", DKGL_FUNCTION_NAME);
  120. runningRenderThread = 0;
  121. renderThread->WaitTerminate();
  122. renderThread = NULL;
  123. window = NULL;
  124. DKLogI("Memory Pool Statistics");
  125. size_t numBuckets = DKMemoryPoolNumberOfBuckets();
  126. DKMemoryPoolBucketStatus* buckets = new DKMemoryPoolBucketStatus[numBuckets];
  127. DKMemoryPoolQueryAllocationStatus(buckets, numBuckets);
  128. size_t usedBytes = 0;
  129. for (int i = 0; i < numBuckets; ++i)
  130. {
  131. if (buckets[i].totalChunks > 0)
  132. {
  133. DKLogI("--> %5lu: %5lu/%5lu, usage: %.1f%%, used: %.1fKB, total: %.1fKB",
  134. buckets[i].chunkSize,
  135. buckets[i].usedChunks, buckets[i].totalChunks,
  136. double(buckets[i].usedChunks) / double(buckets[i].totalChunks) * 100.0,
  137. double(buckets[i].chunkSize * buckets[i].usedChunks) / 1024.0,
  138. double(buckets[i].chunkSize * buckets[i].totalChunks) / 1024.0
  139. );
  140. usedBytes += buckets[i].chunkSize * buckets[i].usedChunks;
  141. }
  142. }
  143. DKLogI("MemoryPool Usage: %.1fMB / %.1fMB", double(usedBytes) / (1024 * 1024), double(DKMemoryPoolSize()) / (1024 * 1024));
  144. delete[] buckets;
  145. }
  146. };
  147. #ifdef _WIN32
  148. int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
  149. _In_opt_ HINSTANCE hPrevInstance,
  150. _In_ LPWSTR lpCmdLine,
  151. _In_ int nCmdShow)
  152. #else
  153. int main(int argc, const char * argv[])
  154. #endif
  155. {
  156. TestApp1 app;
  157. DKPropertySet::SystemConfig().SetValue("AppDelegate", "AppDelegate");
  158. DKPropertySet::SystemConfig().SetValue("GraphicsAPI", "Vulkan");
  159. return app.Run();
  160. }