No Description

TestApp1.cpp 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. DKRenderPipelineDescriptor pipelineDescriptor;
  28. pipelineDescriptor.vertexFunction = vertShaderFunction;
  29. pipelineDescriptor.fragmentFunction = fragShaderFunction;
  30. pipelineDescriptor.colorAttachments.Resize(1);
  31. pipelineDescriptor.colorAttachments.Value(0).pixelFormat = swapChain->ColorPixelFormat();
  32. pipelineDescriptor.rasterizationEnabled = true;
  33. DKObject<DKRenderPipelineState> pipelineState = device->CreateRenderPipeline(pipelineDescriptor, NULL);
  34. DKTimer timer;
  35. timer.Reset();
  36. DKLog("Render thread begin");
  37. while (!runningRenderThread.CompareAndSet(0, 0))
  38. {
  39. DKRenderPassDescriptor rpd = swapChain->CurrentRenderPassDescriptor();
  40. double t = timer.Elapsed();
  41. t = (cos(t) + 1.0) * 0.5;
  42. rpd.colorAttachments.Value(0).clearColor = DKColor(t, 0.0, 0.0, 0.0);
  43. DKObject<DKCommandBuffer> buffer = queue->CreateCommandBuffer();
  44. DKObject<DKRenderCommandEncoder> encoder = buffer->CreateRenderCommandEncoder(rpd);
  45. if (encoder)
  46. {
  47. // draw scene!
  48. encoder->EndEncoding();
  49. buffer->Commit();
  50. //buffer->WaitUntilCompleted();
  51. swapChain->Present();
  52. }
  53. else
  54. {
  55. }
  56. DKThread::Sleep(0.01);
  57. }
  58. DKLog("RenderThread terminating...");
  59. }
  60. void OnInitialize(void) override
  61. {
  62. DKLogD("%s", DKGL_FUNCTION_NAME);
  63. DKString resPath = DefaultPath(SystemPath::AppResource);
  64. resPath = resPath.FilePathStringByAppendingPath("Data");
  65. DKLog("resPath: %ls", (const wchar_t*)resPath);
  66. resourcePool.AddLocatorForPath(resPath);
  67. window = DKWindow::Create("DefaultWindow");
  68. window->SetOrigin({ 0, 0 });
  69. window->Resize({ 320, 240 });
  70. window->Activate();
  71. window->AddEventHandler(this,
  72. DKFunction([this](const DKWindow::WindowEvent& e) {
  73. if (e.type == DKWindow::WindowEvent::WindowClosed)
  74. DKApplication::Instance()->Terminate(0);
  75. }),
  76. NULL, NULL);
  77. runningRenderThread = 1;
  78. renderThread = DKThread::Create(DKFunction(this, &TestApp1::RenderThread)->Invocation());
  79. }
  80. void OnTerminate(void) override
  81. {
  82. DKLogD("%s", DKGL_FUNCTION_NAME);
  83. runningRenderThread = 0;
  84. renderThread->WaitTerminate();
  85. renderThread = NULL;
  86. window = NULL;
  87. DKLogI("Memory Pool Statistics");
  88. size_t numBuckets = DKMemoryPoolNumberOfBuckets();
  89. DKMemoryPoolBucketStatus* buckets = new DKMemoryPoolBucketStatus[numBuckets];
  90. DKMemoryPoolQueryAllocationStatus(buckets, numBuckets);
  91. size_t usedBytes = 0;
  92. for (int i = 0; i < numBuckets; ++i)
  93. {
  94. if (buckets[i].totalChunks > 0)
  95. {
  96. DKLogI("--> %5lu: %5lu/%5lu, usage: %.1f%%, used: %.1fKB, total: %.1fKB",
  97. buckets[i].chunkSize,
  98. buckets[i].usedChunks, buckets[i].totalChunks,
  99. double(buckets[i].usedChunks) / double(buckets[i].totalChunks) * 100.0,
  100. double(buckets[i].chunkSize * buckets[i].usedChunks) / 1024.0,
  101. double(buckets[i].chunkSize * buckets[i].totalChunks) / 1024.0
  102. );
  103. usedBytes += buckets[i].chunkSize * buckets[i].usedChunks;
  104. }
  105. }
  106. DKLogI("MemoryPool Usage: %.1fMB / %.1fMB", double(usedBytes) / (1024 * 1024), double(DKMemoryPoolSize()) / (1024 * 1024));
  107. delete[] buckets;
  108. }
  109. };
  110. #ifdef _WIN32
  111. int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
  112. _In_opt_ HINSTANCE hPrevInstance,
  113. _In_ LPWSTR lpCmdLine,
  114. _In_ int nCmdShow)
  115. #else
  116. int main(int argc, const char * argv[])
  117. #endif
  118. {
  119. TestApp1 app;
  120. DKPropertySet::SystemConfig().SetValue("AppDelegate", "AppDelegate");
  121. DKPropertySet::SystemConfig().SetValue("GraphicsAPI", "Vulkan");
  122. return app.Run();
  123. }