No Description

TestApp1.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. const char* ShaderDataTypeStr(DKShaderDataType t)
  8. {
  9. switch (t) {
  10. case DKShaderDataType::Unknown: return "Unknown";
  11. case DKShaderDataType::None: return "None";
  12. case DKShaderDataType::Struct: return "Struct";
  13. case DKShaderDataType::Texture: return "Texture";
  14. case DKShaderDataType::Sampler: return "Sampler";
  15. case DKShaderDataType::Float: return "Float";
  16. case DKShaderDataType::Float2: return "Float2";
  17. case DKShaderDataType::Float3: return "Float3";
  18. case DKShaderDataType::Float4: return "Float4";
  19. case DKShaderDataType::Float2x2: return "Float2x2";
  20. case DKShaderDataType::Float2x3: return "Float2x3";
  21. case DKShaderDataType::Float2x4: return "Float2x4";
  22. case DKShaderDataType::Float3x2: return "Float3x2";
  23. case DKShaderDataType::Float3x3: return "Float3x3";
  24. case DKShaderDataType::Float3x4: return "Float3x4";
  25. case DKShaderDataType::Float4x2: return "Float4x2";
  26. case DKShaderDataType::Float4x3: return "Float4x3";
  27. case DKShaderDataType::Float4x4: return "Float4x4";
  28. case DKShaderDataType::Half: return "Half";
  29. case DKShaderDataType::Half2: return "Half2";
  30. case DKShaderDataType::Half3: return "Half3";
  31. case DKShaderDataType::Half4: return "Half4";
  32. case DKShaderDataType::Half2x2: return "Half2x2";
  33. case DKShaderDataType::Half2x3: return "Half2x3";
  34. case DKShaderDataType::Half2x4: return "Half2x4";
  35. case DKShaderDataType::Half3x2: return "Half3x2";
  36. case DKShaderDataType::Half3x3: return "Half3x3";
  37. case DKShaderDataType::Half3x4: return "Half3x4";
  38. case DKShaderDataType::Half4x2: return "Half4x2";
  39. case DKShaderDataType::Half4x3: return "Half4x3";
  40. case DKShaderDataType::Half4x4: return "Half4x4";
  41. case DKShaderDataType::Int: return "Int";
  42. case DKShaderDataType::Int2: return "Int2";
  43. case DKShaderDataType::Int3: return "Int3";
  44. case DKShaderDataType::Int4: return "Int4";
  45. case DKShaderDataType::UInt: return "UInt";
  46. case DKShaderDataType::UInt2: return "UInt2";
  47. case DKShaderDataType::UInt3: return "UInt3";
  48. case DKShaderDataType::UInt4: return "UInt4";
  49. case DKShaderDataType::Short: return "Short";
  50. case DKShaderDataType::Short2: return "Short2";
  51. case DKShaderDataType::Short3: return "Short3";
  52. case DKShaderDataType::Short4: return "Short4";
  53. case DKShaderDataType::UShort: return "UShort";
  54. case DKShaderDataType::UShort2: return "UShort2";
  55. case DKShaderDataType::UShort3: return "UShort3";
  56. case DKShaderDataType::UShort4: return "UShort4";
  57. case DKShaderDataType::Char: return "Char";
  58. case DKShaderDataType::Char2: return "Char2";
  59. case DKShaderDataType::Char3: return "Char3";
  60. case DKShaderDataType::Char4: return "Char4";
  61. case DKShaderDataType::UChar: return "UChar";
  62. case DKShaderDataType::UChar2: return "UChar2";
  63. case DKShaderDataType::UChar3: return "UChar3";
  64. case DKShaderDataType::UChar4: return "UChar4";
  65. case DKShaderDataType::Bool: return "Bool";
  66. case DKShaderDataType::Bool2: return "Bool2";
  67. case DKShaderDataType::Bool3: return "Bool3";
  68. case DKShaderDataType::Bool4: return "Bool4";
  69. }
  70. return "Error";
  71. }
  72. void PrintShaderResource(const DKShaderResource& res)
  73. {
  74. struct MemberPrinter
  75. {
  76. const DKShaderResource& res;
  77. int indent;
  78. void operator()(const DKShaderResourceStruct& str) const
  79. {
  80. DKString indentStr = "";
  81. for (int i = 0; i < indent; ++i)
  82. {
  83. indentStr += " ";
  84. }
  85. for (const DKShaderResourceStructMember& mem : str.members)
  86. {
  87. if (mem.count > 1)
  88. {
  89. DKLogI(" %ls+ %ls[%d] (%s, Offset: %d, Size: %d, Stride: %d)", (const wchar_t*)indentStr,
  90. (const wchar_t*)mem.name,
  91. mem.count,
  92. ShaderDataTypeStr(mem.dataType),
  93. mem.offset, mem.size, mem.stride);
  94. }
  95. else
  96. {
  97. DKLogI(" %ls+ %ls (%s, Offset: %d, Size: %d)", (const wchar_t*)indentStr,
  98. (const wchar_t*)mem.name,
  99. ShaderDataTypeStr(mem.dataType),
  100. mem.offset, mem.size);
  101. }
  102. auto* p = res.structTypeMemberMap.Find(mem.typeInfoKey);
  103. if (p)
  104. {
  105. MemberPrinter{ res, indent + 1 }.operator()(p->value);
  106. }
  107. }
  108. }
  109. };
  110. if (res.count > 1)
  111. DKLogI("ShaderResource: %ls[%d] (set=%d, binding=%d)",
  112. (const wchar_t*)res.name, res.count, res.set, res.binding);
  113. else
  114. DKLogI("ShaderResource: %ls (set=%d, binding=%d)", (const wchar_t*)res.name, res.set, res.binding);
  115. const char* type = "Unknown (ERROR)";
  116. switch (res.type)
  117. {
  118. case DKShaderResource::TypeBuffer: type = "Buffer"; break;
  119. case DKShaderResource::TypeTexture: type = "Texture"; break;
  120. case DKShaderResource::TypeSampler: type = "Sampler"; break;
  121. case DKShaderResource::TypeThreadgroupMemory: type = "Threadinggroup"; break;
  122. }
  123. DKLogI(" +Type: %s, Writable: %d, Enabled: %d Alignment: %d, Size: %d",
  124. type, int(res.writable), int(res.enabled),
  125. res.typeInfo.buffer.alignment,
  126. res.typeInfo.buffer.size);
  127. if (res.type == DKShaderResource::TypeBuffer)
  128. {
  129. auto p = res.structTypeMemberMap.Find(res.typeInfoKey);
  130. if (p)
  131. MemberPrinter{ res, 1 }.operator()(p->value);
  132. }
  133. }
  134. class TestApp1 : public DKApplication
  135. {
  136. DKObject<DKWindow> window;
  137. DKObject<DKThread> renderThread;
  138. DKResourcePool resourcePool;
  139. DKAtomicNumber32 runningRenderThread;
  140. public:
  141. void RenderThread(void)
  142. {
  143. DKObject<DKData> vertData = resourcePool.LoadResourceData("triangle.vert.spv");
  144. DKObject<DKData> fragData = resourcePool.LoadResourceData("triangle.frag.spv");
  145. DKShader vertShader(vertData, DKShader::Vertex);
  146. DKShader fragShader(fragData, DKShader::Fragment);
  147. DKObject<DKGraphicsDevice> device = DKGraphicsDevice::SharedInstance();
  148. DKObject<DKShaderModule> vertShaderModule = device->CreateShaderModule(&vertShader);
  149. DKObject<DKShaderModule> fragShaderModule = device->CreateShaderModule(&fragShader);
  150. DKObject<DKShaderFunction> vertShaderFunction = vertShaderModule->CreateFunction(vertShaderModule->FunctionNames().Value(0));
  151. DKObject<DKShaderFunction> fragShaderFunction = fragShaderModule->CreateFunction(fragShaderModule->FunctionNames().Value(0));
  152. DKObject<DKCommandQueue> queue = device->CreateCommandQueue(DKCommandQueue::Graphics);
  153. DKObject<DKSwapChain> swapChain = queue->CreateSwapChain(window);
  154. DKLog("VertexFunction.VertexAttributes: %d", vertShaderFunction->VertexAttributes().Count());
  155. for (int i = 0; i < vertShaderFunction->VertexAttributes().Count(); ++i)
  156. {
  157. const DKVertexAttribute& attr = vertShaderFunction->VertexAttributes().Value(i);
  158. DKLog(" --> VertexAttribute[%d]: \"%ls\" (location:%u)", i, (const wchar_t*)attr.name, attr.location);
  159. }
  160. struct Vertex
  161. {
  162. DKVector3 position;
  163. DKVector3 color;
  164. };
  165. DKArray<Vertex> vertexData =
  166. {
  167. { { 0.0f, -0.5f, 0.0f },{ 1.0f, 1.0f, 1.0f } },
  168. { { 0.5f, 0.5f, 0.0f },{ 0.0f, 1.0f, 0.0f } },
  169. { { -0.5f, 0.5f, 0.0f },{ 0.0f, 0.0f, 1.0f } }
  170. };
  171. uint32_t vertexBufferSize = static_cast<uint32_t>(vertexData.Count()) * sizeof(Vertex);
  172. DKArray<uint32_t> indexData = { 0, 1, 2 };
  173. uint32_t indexBufferSize = indexData.Count() * sizeof(uint32_t);
  174. DKObject<DKGpuBuffer> vertexBuffer = device->CreateBuffer(vertexBufferSize, DKGpuBuffer::StorageModeShared, DKCpuCacheModeDefault);
  175. memcpy(vertexBuffer->Lock(), vertexData, vertexBufferSize);
  176. vertexBuffer->Unlock();
  177. DKObject<DKGpuBuffer> indexBuffer = device->CreateBuffer(indexBufferSize, DKGpuBuffer::StorageModeShared, DKCpuCacheModeDefault);
  178. memcpy(indexBuffer->Lock(), indexData, indexBufferSize);
  179. indexBuffer->Unlock();
  180. DKRenderPipelineDescriptor pipelineDescriptor;
  181. pipelineDescriptor.vertexFunction = vertShaderFunction;
  182. pipelineDescriptor.fragmentFunction = fragShaderFunction;
  183. pipelineDescriptor.colorAttachments.Resize(1);
  184. pipelineDescriptor.colorAttachments.Value(0).pixelFormat = swapChain->ColorPixelFormat();
  185. pipelineDescriptor.depthStencilAttachmentPixelFormat = DKPixelFormat::Invalid; // no depth buffer
  186. pipelineDescriptor.vertexDescriptor.attributes = {
  187. { DKVertexFormat::Float3, 0, 0, 0 },
  188. { DKVertexFormat::Float3, sizeof(DKVector3), 0, 1 },
  189. };
  190. pipelineDescriptor.vertexDescriptor.layouts = {
  191. { DKVertexStepRate::Vertex, sizeof(Vertex), 0 },
  192. };
  193. pipelineDescriptor.primitiveTopology = DKPrimitiveType::Triangle;
  194. pipelineDescriptor.frontFace = DKFrontFace::CCW;
  195. pipelineDescriptor.triangleFillMode = DKTriangleFillMode::Fill;
  196. pipelineDescriptor.depthClipMode = DKDepthClipMode::Clip;
  197. pipelineDescriptor.cullMode = DKCullMode::None;
  198. pipelineDescriptor.rasterizationEnabled = true;
  199. DKRenderPipelineReflection reflection;
  200. DKObject<DKRenderPipelineState> pipelineState = device->CreateRenderPipeline(pipelineDescriptor, &reflection);
  201. if (pipelineState)
  202. {
  203. DKLog("PipelineReflection.VertexResources: %d", reflection.vertexResources.Count());
  204. for (auto& arg : reflection.vertexResources)
  205. PrintShaderResource(arg);
  206. DKLog("PipelineReflection.FragmentResources: %d", reflection.fragmentResources.Count());
  207. for (auto& arg : reflection.fragmentResources)
  208. PrintShaderResource(arg);
  209. }
  210. DKTimer timer;
  211. timer.Reset();
  212. DKLog("Render thread begin");
  213. while (!runningRenderThread.CompareAndSet(0, 0))
  214. {
  215. DKRenderPassDescriptor rpd = swapChain->CurrentRenderPassDescriptor();
  216. double t = timer.Elapsed();
  217. t = (cos(t) + 1.0) * 0.5;
  218. rpd.colorAttachments.Value(0).clearColor = DKColor(t, 0.0, 0.0, 0.0);
  219. DKObject<DKCommandBuffer> buffer = queue->CreateCommandBuffer();
  220. DKObject<DKRenderCommandEncoder> encoder = buffer->CreateRenderCommandEncoder(rpd);
  221. if (encoder)
  222. {
  223. encoder->SetRenderPipelineState(pipelineState);
  224. encoder->SetVertexBuffer(vertexBuffer, 0, 0);
  225. encoder->SetIndexBuffer(indexBuffer, 0, DKIndexType::UInt32);
  226. // draw scene!
  227. encoder->DrawIndexed(indexData.Count(), 1, 0, 0, 1);
  228. encoder->EndEncoding();
  229. buffer->Commit();
  230. swapChain->Present();
  231. }
  232. else
  233. {
  234. }
  235. DKThread::Sleep(0.01);
  236. }
  237. DKLog("RenderThread terminating...");
  238. }
  239. void OnInitialize(void) override
  240. {
  241. DKLogD("%s", DKGL_FUNCTION_NAME);
  242. DKString resPath = DefaultPath(SystemPath::AppResource);
  243. resPath = resPath.FilePathStringByAppendingPath("Data");
  244. DKLog("resPath: %ls", (const wchar_t*)resPath);
  245. resourcePool.AddLocatorForPath(resPath);
  246. window = DKWindow::Create("DefaultWindow");
  247. window->SetOrigin({ 0, 0 });
  248. window->Resize({ 320, 240 });
  249. window->Activate();
  250. window->AddEventHandler(this, DKFunction([this](const DKWindow::WindowEvent& e)
  251. {
  252. if (e.type == DKWindow::WindowEvent::WindowClosed)
  253. DKApplication::Instance()->Terminate(0);
  254. }), NULL, NULL);
  255. runningRenderThread = 1;
  256. renderThread = DKThread::Create(DKFunction(this, &TestApp1::RenderThread)->Invocation());
  257. }
  258. void OnTerminate(void) override
  259. {
  260. DKLogD("%s", DKGL_FUNCTION_NAME);
  261. runningRenderThread = 0;
  262. renderThread->WaitTerminate();
  263. renderThread = NULL;
  264. window = NULL;
  265. DKLogI("Memory Pool Statistics");
  266. size_t numBuckets = DKMemoryPoolNumberOfBuckets();
  267. DKMemoryPoolBucketStatus* buckets = new DKMemoryPoolBucketStatus[numBuckets];
  268. DKMemoryPoolQueryAllocationStatus(buckets, numBuckets);
  269. size_t usedBytes = 0;
  270. for (int i = 0; i < numBuckets; ++i)
  271. {
  272. if (buckets[i].totalChunks > 0)
  273. {
  274. DKLogI("--> %5lu: %5lu/%5lu, usage: %.1f%%, used: %.1fKB, total: %.1fKB",
  275. buckets[i].chunkSize,
  276. buckets[i].usedChunks, buckets[i].totalChunks,
  277. double(buckets[i].usedChunks) / double(buckets[i].totalChunks) * 100.0,
  278. double(buckets[i].chunkSize * buckets[i].usedChunks) / 1024.0,
  279. double(buckets[i].chunkSize * buckets[i].totalChunks) / 1024.0
  280. );
  281. usedBytes += buckets[i].chunkSize * buckets[i].usedChunks;
  282. }
  283. }
  284. DKLogI("MemoryPool Usage: %.1fMB / %.1fMB", double(usedBytes) / (1024 * 1024), double(DKMemoryPoolSize()) / (1024 * 1024));
  285. delete[] buckets;
  286. }
  287. };
  288. #ifdef _WIN32
  289. int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
  290. _In_opt_ HINSTANCE hPrevInstance,
  291. _In_ LPWSTR lpCmdLine,
  292. _In_ int nCmdShow)
  293. #else
  294. int main(int argc, const char * argv[])
  295. #endif
  296. {
  297. TestApp1 app;
  298. DKPropertySet::SystemConfig().SetValue("AppDelegate", "AppDelegate");
  299. DKPropertySet::SystemConfig().SetValue("GraphicsAPI", "Vulkan");
  300. return app.Run();
  301. }