No Description

TestApp1.cpp 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. { { 1.0f, 1.0f, 0.0f },{ 1.0f, 0.0f, 0.0f } },
  35. { { -1.0f, 1.0f, 0.0f },{ 0.0f, 1.0f, 0.0f } },
  36. { { 0.0f, -1.0f, 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.rasterizationEnabled = true;
  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. DKObject<DKRenderPipelineState> pipelineState = device->CreateRenderPipeline(pipelineDescriptor, NULL);
  61. DKTimer timer;
  62. timer.Reset();
  63. DKLog("Render thread begin");
  64. while (!runningRenderThread.CompareAndSet(0, 0))
  65. {
  66. DKRenderPassDescriptor rpd = swapChain->CurrentRenderPassDescriptor();
  67. double t = timer.Elapsed();
  68. t = (cos(t) + 1.0) * 0.5;
  69. rpd.colorAttachments.Value(0).clearColor = DKColor(t, 0.0, 0.0, 0.0);
  70. DKObject<DKCommandBuffer> buffer = queue->CreateCommandBuffer();
  71. DKObject<DKRenderCommandEncoder> encoder = buffer->CreateRenderCommandEncoder(rpd);
  72. if (encoder)
  73. {
  74. // draw scene!
  75. encoder->EndEncoding();
  76. buffer->Commit();
  77. swapChain->Present();
  78. }
  79. else
  80. {
  81. }
  82. DKThread::Sleep(0.01);
  83. }
  84. DKLog("RenderThread terminating...");
  85. }
  86. void OnInitialize(void) override
  87. {
  88. DKLogD("%s", DKGL_FUNCTION_NAME);
  89. DKString resPath = DefaultPath(SystemPath::AppResource);
  90. resPath = resPath.FilePathStringByAppendingPath("Data");
  91. DKLog("resPath: %ls", (const wchar_t*)resPath);
  92. resourcePool.AddLocatorForPath(resPath);
  93. window = DKWindow::Create("DefaultWindow");
  94. window->SetOrigin({ 0, 0 });
  95. window->Resize({ 320, 240 });
  96. window->Activate();
  97. window->AddEventHandler(this,
  98. DKFunction([this](const DKWindow::WindowEvent& e) {
  99. if (e.type == DKWindow::WindowEvent::WindowClosed)
  100. DKApplication::Instance()->Terminate(0);
  101. }),
  102. NULL, NULL);
  103. runningRenderThread = 1;
  104. renderThread = DKThread::Create(DKFunction(this, &TestApp1::RenderThread)->Invocation());
  105. }
  106. void OnTerminate(void) override
  107. {
  108. DKLogD("%s", DKGL_FUNCTION_NAME);
  109. runningRenderThread = 0;
  110. renderThread->WaitTerminate();
  111. renderThread = NULL;
  112. window = NULL;
  113. DKLogI("Memory Pool Statistics");
  114. size_t numBuckets = DKMemoryPoolNumberOfBuckets();
  115. DKMemoryPoolBucketStatus* buckets = new DKMemoryPoolBucketStatus[numBuckets];
  116. DKMemoryPoolQueryAllocationStatus(buckets, numBuckets);
  117. size_t usedBytes = 0;
  118. for (int i = 0; i < numBuckets; ++i)
  119. {
  120. if (buckets[i].totalChunks > 0)
  121. {
  122. DKLogI("--> %5lu: %5lu/%5lu, usage: %.1f%%, used: %.1fKB, total: %.1fKB",
  123. buckets[i].chunkSize,
  124. buckets[i].usedChunks, buckets[i].totalChunks,
  125. double(buckets[i].usedChunks) / double(buckets[i].totalChunks) * 100.0,
  126. double(buckets[i].chunkSize * buckets[i].usedChunks) / 1024.0,
  127. double(buckets[i].chunkSize * buckets[i].totalChunks) / 1024.0
  128. );
  129. usedBytes += buckets[i].chunkSize * buckets[i].usedChunks;
  130. }
  131. }
  132. DKLogI("MemoryPool Usage: %.1fMB / %.1fMB", double(usedBytes) / (1024 * 1024), double(DKMemoryPoolSize()) / (1024 * 1024));
  133. delete[] buckets;
  134. }
  135. };
  136. #ifdef _WIN32
  137. int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
  138. _In_opt_ HINSTANCE hPrevInstance,
  139. _In_ LPWSTR lpCmdLine,
  140. _In_ int nCmdShow)
  141. #else
  142. int main(int argc, const char * argv[])
  143. #endif
  144. {
  145. TestApp1 app;
  146. DKPropertySet::SystemConfig().SetValue("AppDelegate", "AppDelegate");
  147. DKPropertySet::SystemConfig().SetValue("GraphicsAPI", "Vulkan");
  148. return app.Run();
  149. }