No Description

TestApp1.cpp 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. DKAtomicNumber32 runningRenderThread;
  12. public:
  13. void RenderThread(void)
  14. {
  15. DKObject<DKGraphicsDevice> device = DKGraphicsDevice::SharedInstance();
  16. DKObject<DKCommandQueue> queue = device->CreateCommandQueue();
  17. DKObject<DKSwapChain> swapChain = queue->CreateSwapChain(window);
  18. DKTimer timer;
  19. timer.Reset();
  20. DKLog("Render thread begin");
  21. while (!runningRenderThread.CompareAndSet(0, 0))
  22. {
  23. DKRenderPassDescriptor rpd = swapChain->CurrentRenderPassDescriptor();
  24. double t = timer.Elapsed();
  25. t = (cos(t) + 1.0) * 0.5;
  26. rpd.colorAttachments.Value(0).clearColor = DKColor(t, 0.0, 0.0, 0.0);
  27. DKObject<DKCommandBuffer> buffer = queue->CreateCommandBuffer();
  28. DKObject<DKRenderCommandEncoder> encoder = buffer->CreateRenderCommandEncoder(rpd);
  29. if (encoder)
  30. {
  31. // draw scene!
  32. encoder->EndEncoding();
  33. buffer->Commit();
  34. //buffer->WaitUntilCompleted();
  35. swapChain->Present();
  36. }
  37. else
  38. {
  39. }
  40. DKThread::Sleep(0.01);
  41. }
  42. DKLog("RenderThread terminating...");
  43. }
  44. void OnInitialize(void) override
  45. {
  46. DKLog("%s", DKGL_FUNCTION_NAME);
  47. try {
  48. window = DKWindow::Create("DefaultWindow");
  49. }
  50. catch (DKError& e)
  51. {
  52. DKLog("error? :%ls", (const wchar_t*)e.Description());
  53. }
  54. window->Activate();
  55. window->AddEventHandler(this,
  56. DKFunction([this](const DKWindow::WindowEvent& e) {
  57. if (e.type == DKWindow::WindowEvent::WindowClosed)
  58. DKApplication::Instance()->Terminate(0);
  59. }),
  60. NULL, NULL);
  61. runningRenderThread = 1;
  62. renderThread = DKThread::Create(DKFunction(this, &TestApp1::RenderThread)->Invocation());
  63. }
  64. void OnTerminate(void) override
  65. {
  66. DKLog("%s", DKGL_FUNCTION_NAME);
  67. runningRenderThread = 0;
  68. renderThread->WaitTerminate();
  69. renderThread = NULL;
  70. window = NULL;
  71. DKLog("Memory Pool Statistics");
  72. size_t numBuckets = DKMemoryPoolNumberOfBuckets();
  73. DKMemoryPoolBucketStatus* buckets = new DKMemoryPoolBucketStatus[numBuckets];
  74. DKMemoryPoolQueryAllocationStatus(buckets, numBuckets);
  75. size_t usedBytes = 0;
  76. for (int i = 0; i < numBuckets; ++i)
  77. {
  78. if (buckets[i].totalChunks > 0)
  79. {
  80. DKLog("--> %5lu: %5lu/%5lu, usage: %.1f%%, used: %.1fKB, total: %.1fKB",
  81. buckets[i].chunkSize,
  82. buckets[i].usedChunks, buckets[i].totalChunks,
  83. double(buckets[i].usedChunks) / double(buckets[i].totalChunks) * 100.0,
  84. double(buckets[i].chunkSize * buckets[i].usedChunks) / 1024.0,
  85. double(buckets[i].chunkSize * buckets[i].totalChunks) / 1024.0
  86. );
  87. usedBytes += buckets[i].chunkSize * buckets[i].usedChunks;
  88. }
  89. }
  90. DKLog("MemoryPool Usage: %.1fMB / %.1fMB", double(usedBytes) / (1024 * 1024), double(DKMemoryPoolSize()) / (1024 * 1024));
  91. delete[] buckets;
  92. }
  93. };
  94. #ifdef _WIN32
  95. int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
  96. _In_opt_ HINSTANCE hPrevInstance,
  97. _In_ LPWSTR lpCmdLine,
  98. _In_ int nCmdShow)
  99. #else
  100. int main(int argc, const char * argv[])
  101. #endif
  102. {
  103. TestApp1 app;
  104. DKPropertySet::SystemConfig().SetValue("AppDelegate", "AppDelegate");
  105. DKPropertySet::SystemConfig().SetValue("GraphicsAPI", "Vulkan");
  106. return app.Run();
  107. }