No Description

TestApp1.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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)",
  90. (const wchar_t*)indentStr,
  91. (const wchar_t*)mem.name,
  92. mem.count,
  93. ShaderDataTypeStr(mem.dataType),
  94. mem.offset, mem.size, mem.stride);
  95. }
  96. else
  97. {
  98. DKLogI(" %ls+ %ls (%s, Offset: %d, Size: %d)",
  99. (const wchar_t*)indentStr,
  100. (const wchar_t*)mem.name,
  101. ShaderDataTypeStr(mem.dataType),
  102. mem.offset, mem.size);
  103. }
  104. auto* p = res.structTypeMemberMap.Find(mem.typeInfoKey);
  105. if (p)
  106. {
  107. DKLogI(" %ls+TypeKey: %ls (struct)",
  108. (const wchar_t*)indentStr,
  109. (const wchar_t*)res.typeInfoKey);
  110. MemberPrinter{ res, indent + 1 }.operator()(p->value);
  111. }
  112. }
  113. }
  114. };
  115. if (res.count > 1)
  116. DKLogI("ShaderResource: %ls[%d] (set=%d, binding=%d)",
  117. (const wchar_t*)res.name, res.count, res.set, res.binding);
  118. else
  119. DKLogI("ShaderResource: %ls (set=%d, binding=%d)", (const wchar_t*)res.name, res.set, res.binding);
  120. const char* type = "Unknown (ERROR)";
  121. switch (res.type)
  122. {
  123. case DKShaderResource::TypeBuffer: type = "Buffer"; break;
  124. case DKShaderResource::TypeTexture: type = "Texture"; break;
  125. case DKShaderResource::TypeSampler: type = "Sampler"; break;
  126. case DKShaderResource::TypeThreadgroupMemory: type = "Threadinggroup"; break;
  127. }
  128. const char* access = "Unknown (ERROR)";
  129. switch (res.access)
  130. {
  131. case DKShaderResource::AccessReadOnly: access = "ReadOnly"; break;
  132. case DKShaderResource::AccessWriteOnly: access = "WriteOnly"; break;
  133. case DKShaderResource::AccessReadWrite: access = "ReadWrite"; break;
  134. }
  135. DKLogI(" +Type:%s, Access:%s, Enabled:%d Alignment:%d, Size:%d",
  136. type,
  137. access,
  138. int(res.enabled),
  139. res.typeInfo.buffer.alignment,
  140. res.typeInfo.buffer.size);
  141. if (res.typeInfoKey.Length() > 0)
  142. DKLogI(" +TypeKey: %ls (struct)", (const wchar_t*)res.typeInfoKey);
  143. if (res.type == DKShaderResource::TypeBuffer)
  144. {
  145. auto p = res.structTypeMemberMap.Find(res.typeInfoKey);
  146. if (p)
  147. MemberPrinter{ res, 1 }.operator()(p->value);
  148. }
  149. }
  150. class TestApp1 : public DKApplication
  151. {
  152. DKResourcePool resourcePool;
  153. public:
  154. void OnInitialize(void) override
  155. {
  156. DKLogD("%s", DKGL_FUNCTION_NAME);
  157. DKString resPath = DefaultPath(SystemPath::AppResource);
  158. resPath = resPath.FilePathStringByAppendingPath("Data");
  159. DKLog("resPath: %ls", (const wchar_t*)resPath);
  160. resourcePool.AddLocatorForPath(resPath);
  161. DKVariant vulkanProps;
  162. vulkanProps.NewValueAtKeyPath("pipelineCacheFilePath",
  163. DefaultPath(SystemPath::AppExecutable).FilePathStringByAppendingPath("pipeline.cache"));
  164. DKPropertySet::SystemConfig().SetValue("Vulkan", vulkanProps);
  165. DKObject<DKData> vertData = resourcePool.LoadResourceData("shaders/texture/texture.vert.spv");
  166. DKObject<DKData> fragData = resourcePool.LoadResourceData("shaders/texture/texture.frag.spv");
  167. struct Vertex
  168. {
  169. DKVector3 position;
  170. DKVector2 uv;
  171. DKVector3 normal;
  172. };
  173. DKVertexDescriptor vertexDescriptor;
  174. vertexDescriptor.attributes = {
  175. { DKVertexFormat::Float3, 0, 0, 0 },
  176. { DKVertexFormat::Float2, sizeof(DKVector2), 0, 1 },
  177. { DKVertexFormat::Float3, sizeof(DKVector3), 0, 2 },
  178. };
  179. vertexDescriptor.layouts = {
  180. { DKVertexStepRate::Vertex, sizeof(Vertex), 0 },
  181. };
  182. DKShader vertShader(vertData, DKShader::Vertex);
  183. DKShader fragShader(fragData, DKShader::Fragment);
  184. DKObject<DKGraphicsDevice> device = DKGraphicsDevice::SharedInstance();
  185. DKObject<DKShaderModule> vertShaderModule = device->CreateShaderModule(&vertShader);
  186. DKObject<DKShaderModule> fragShaderModule = device->CreateShaderModule(&fragShader);
  187. DKObject<DKShaderFunction> vertShaderFunction = vertShaderModule->CreateFunction(vertShaderModule->FunctionNames().Value(0));
  188. DKObject<DKShaderFunction> fragShaderFunction = fragShaderModule->CreateFunction(fragShaderModule->FunctionNames().Value(0));
  189. DKObject<DKCommandQueue> queue = device->CreateCommandQueue(DKCommandQueue::Graphics);
  190. DKLog("VertexFunction.VertexAttributes: %d", vertShaderFunction->VertexAttributes().Count());
  191. for (int i = 0; i < vertShaderFunction->VertexAttributes().Count(); ++i)
  192. {
  193. const DKVertexAttribute& attr = vertShaderFunction->VertexAttributes().Value(i);
  194. DKLog(" --> VertexAttribute[%d]: \"%ls\" (location:%u)", i, (const wchar_t*)attr.name, attr.location);
  195. }
  196. DKRenderPipelineDescriptor pipelineDescriptor;
  197. pipelineDescriptor.vertexFunction = vertShaderFunction;
  198. pipelineDescriptor.fragmentFunction = fragShaderFunction;
  199. pipelineDescriptor.colorAttachments.Resize(1);
  200. pipelineDescriptor.colorAttachments.Value(0).pixelFormat = DKPixelFormat::RGBA8Unorm;
  201. pipelineDescriptor.depthStencilAttachmentPixelFormat = DKPixelFormat::Invalid; // no depth buffer
  202. pipelineDescriptor.vertexDescriptor = vertexDescriptor;
  203. pipelineDescriptor.primitiveTopology = DKPrimitiveType::Triangle;
  204. pipelineDescriptor.frontFace = DKFrontFace::CCW;
  205. pipelineDescriptor.triangleFillMode = DKTriangleFillMode::Fill;
  206. pipelineDescriptor.depthClipMode = DKDepthClipMode::Clip;
  207. pipelineDescriptor.cullMode = DKCullMode::None;
  208. pipelineDescriptor.rasterizationEnabled = true;
  209. DKRenderPipelineReflection reflection;
  210. DKObject<DKRenderPipelineState> pipelineState = device->CreateRenderPipeline(pipelineDescriptor, &reflection);
  211. if (pipelineState)
  212. {
  213. DKLog("PipelineReflection.VertexResources: %d", reflection.vertexResources.Count());
  214. for (auto& arg : reflection.vertexResources)
  215. PrintShaderResource(arg);
  216. DKLog("PipelineReflection.FragmentResources: %d", reflection.fragmentResources.Count());
  217. for (auto& arg : reflection.fragmentResources)
  218. PrintShaderResource(arg);
  219. }
  220. Terminate(0);
  221. }
  222. void OnTerminate(void) override
  223. {
  224. DKLogD("%s", DKGL_FUNCTION_NAME);
  225. DKLogI("Memory Pool Statistics");
  226. size_t numBuckets = DKMemoryPoolNumberOfBuckets();
  227. DKMemoryPoolBucketStatus* buckets = new DKMemoryPoolBucketStatus[numBuckets];
  228. DKMemoryPoolQueryAllocationStatus(buckets, numBuckets);
  229. size_t usedBytes = 0;
  230. for (int i = 0; i < numBuckets; ++i)
  231. {
  232. if (buckets[i].totalChunks > 0)
  233. {
  234. DKLogI("--> %5lu: %5lu/%5lu, usage: %.1f%%, used: %.1fKB, total: %.1fKB",
  235. buckets[i].chunkSize,
  236. buckets[i].usedChunks, buckets[i].totalChunks,
  237. double(buckets[i].usedChunks) / double(buckets[i].totalChunks) * 100.0,
  238. double(buckets[i].chunkSize * buckets[i].usedChunks) / 1024.0,
  239. double(buckets[i].chunkSize * buckets[i].totalChunks) / 1024.0
  240. );
  241. usedBytes += buckets[i].chunkSize * buckets[i].usedChunks;
  242. }
  243. }
  244. DKLogI("MemoryPool Usage: %.1fMB / %.1fMB", double(usedBytes) / (1024 * 1024), double(DKMemoryPoolSize()) / (1024 * 1024));
  245. delete[] buckets;
  246. }
  247. };
  248. #ifdef _WIN32
  249. int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
  250. _In_opt_ HINSTANCE hPrevInstance,
  251. _In_ LPWSTR lpCmdLine,
  252. _In_ int nCmdShow)
  253. #else
  254. int main(int argc, const char * argv[])
  255. #endif
  256. {
  257. TestApp1 app;
  258. DKPropertySet::SystemConfig().SetValue("AppDelegate", "AppDelegate");
  259. DKPropertySet::SystemConfig().SetValue("GraphicsAPI", "Vulkan");
  260. return app.Run();
  261. }