No Description

TestApp1.cpp 4.6KB

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