123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- // TestApp1.cpp : Defines the entry point for the application.
- //
-
- #ifdef _WIN32
- #include "Win32/stdafx.h"
- #endif
-
- #include <DK.h>
-
-
- class TestApp1 : public DKApplication
- {
- DKObject<DKWindow> window;
- DKObject<DKThread> renderThread;
- DKResourcePool resourcePool;
-
- DKAtomicNumber32 runningRenderThread;
- public:
- void RenderThread(void)
- {
- DKObject<DKData> vertData = resourcePool.LoadResourceData("triangle.vert.spv");
- DKObject<DKData> fragData = resourcePool.LoadResourceData("triangle.frag.spv");
- DKShader vertShader(vertData, DKShader::StageType::Vertex);
- DKShader fragShader(fragData, DKShader::StageType::Fragment);
-
- DKObject<DKGraphicsDevice> device = DKGraphicsDevice::SharedInstance();
- DKObject<DKShaderModule> vertShaderModule = device->CreateShaderModule(&vertShader);
- DKObject<DKShaderModule> fragShaderModule = device->CreateShaderModule(&fragShader);
-
- DKObject<DKShaderFunction> vertShaderFunction = vertShaderModule->CreateFunction(vertShaderModule->FunctionNames().Value(0));
- DKObject<DKShaderFunction> fragShaderFunction = fragShaderModule->CreateFunction(fragShaderModule->FunctionNames().Value(0));
-
- DKObject<DKCommandQueue> queue = device->CreateCommandQueue();
- DKObject<DKSwapChain> swapChain = queue->CreateSwapChain(window);
-
-
- DKRenderPipelineDescriptor pipelineDescriptor;
- pipelineDescriptor.vertexFunction = vertShaderFunction;
- pipelineDescriptor.fragmentFunction = fragShaderFunction;
- pipelineDescriptor.colorAttachments.Resize(1);
- pipelineDescriptor.colorAttachments.Value(0).pixelFormat = swapChain->ColorPixelFormat();
-
-
- DKObject<DKRenderPipelineState> pipelineState = device->CreateRenderPipeline(pipelineDescriptor, NULL);
-
- DKTimer timer;
- timer.Reset();
-
- DKLog("Render thread begin");
- while (!runningRenderThread.CompareAndSet(0, 0))
- {
- DKRenderPassDescriptor rpd = swapChain->CurrentRenderPassDescriptor();
- double t = timer.Elapsed();
- t = (cos(t) + 1.0) * 0.5;
- rpd.colorAttachments.Value(0).clearColor = DKColor(t, 0.0, 0.0, 0.0);
-
- DKObject<DKCommandBuffer> buffer = queue->CreateCommandBuffer();
- DKObject<DKRenderCommandEncoder> encoder = buffer->CreateRenderCommandEncoder(rpd);
- if (encoder)
- {
- // draw scene!
-
- encoder->EndEncoding();
- buffer->Commit();
- //buffer->WaitUntilCompleted();
- swapChain->Present();
- }
- else
- {
- }
-
- DKThread::Sleep(0.01);
- }
- DKLog("RenderThread terminating...");
- }
-
- void OnInitialize(void) override
- {
- DKLogD("%s", DKGL_FUNCTION_NAME);
-
- DKString resPath = DefaultPath(SystemPath::AppResource);
- resPath = resPath.FilePathStringByAppendingPath("Data");
- DKLog("resPath: %ls", (const wchar_t*)resPath);
- resourcePool.AddLocatorForPath(resPath);
-
- window = DKWindow::Create("DefaultWindow");
- window->SetOrigin({ 0, 0 });
- window->Resize({ 320, 240 });
- window->Activate();
-
- window->AddEventHandler(this,
- DKFunction([this](const DKWindow::WindowEvent& e) {
- if (e.type == DKWindow::WindowEvent::WindowClosed)
- DKApplication::Instance()->Terminate(0);
- }),
- NULL, NULL);
-
- runningRenderThread = 1;
- renderThread = DKThread::Create(DKFunction(this, &TestApp1::RenderThread)->Invocation());
- }
- void OnTerminate(void) override
- {
- DKLogD("%s", DKGL_FUNCTION_NAME);
-
- runningRenderThread = 0;
- renderThread->WaitTerminate();
- renderThread = NULL;
- window = NULL;
-
- DKLogI("Memory Pool Statistics");
- size_t numBuckets = DKMemoryPoolNumberOfBuckets();
- DKMemoryPoolBucketStatus* buckets = new DKMemoryPoolBucketStatus[numBuckets];
- DKMemoryPoolQueryAllocationStatus(buckets, numBuckets);
- size_t usedBytes = 0;
- for (int i = 0; i < numBuckets; ++i)
- {
- if (buckets[i].totalChunks > 0)
- {
- DKLogI("--> %5lu: %5lu/%5lu, usage: %.1f%%, used: %.1fKB, total: %.1fKB",
- buckets[i].chunkSize,
- buckets[i].usedChunks, buckets[i].totalChunks,
- double(buckets[i].usedChunks) / double(buckets[i].totalChunks) * 100.0,
- double(buckets[i].chunkSize * buckets[i].usedChunks) / 1024.0,
- double(buckets[i].chunkSize * buckets[i].totalChunks) / 1024.0
- );
- usedBytes += buckets[i].chunkSize * buckets[i].usedChunks;
- }
- }
- DKLogI("MemoryPool Usage: %.1fMB / %.1fMB", double(usedBytes) / (1024 * 1024), double(DKMemoryPoolSize()) / (1024 * 1024));
- delete[] buckets;
- }
- };
-
-
- #ifdef _WIN32
- int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
- _In_opt_ HINSTANCE hPrevInstance,
- _In_ LPWSTR lpCmdLine,
- _In_ int nCmdShow)
- #else
- int main(int argc, const char * argv[])
- #endif
- {
- TestApp1 app;
- DKPropertySet::SystemConfig().SetValue("AppDelegate", "AppDelegate");
- DKPropertySet::SystemConfig().SetValue("GraphicsAPI", "Vulkan");
- return app.Run();
- }
|