No Description

TestApp1.cpp 11KB

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