DKGL2 sample codes

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #ifdef _WIN32
  2. #include "Win32/stdafx.h"
  3. #endif
  4. #include <DK.h>
  5. DKString ShaderStageNames(uint32_t s)
  6. {
  7. DKArray<const char*> stages;
  8. if (s & (uint32_t)DKShaderStage::Vertex)
  9. stages.Add("Vertex");
  10. if (s & (uint32_t)DKShaderStage::TessellationControl)
  11. stages.Add("TessCtrl");
  12. if (s & (uint32_t)DKShaderStage::TessellationEvaluation)
  13. stages.Add("TessEval");
  14. if (s & (uint32_t)DKShaderStage::Geometry)
  15. stages.Add("Geometry");
  16. if (s & (uint32_t)DKShaderStage::Fragment)
  17. stages.Add("Fragment");
  18. if (s & (uint32_t)DKShaderStage::Compute)
  19. stages.Add("Compute");
  20. if (stages.IsEmpty())
  21. return "";
  22. DKString str = stages.Value(0);
  23. for (int i = 1; i < stages.Count(); ++i)
  24. str += DKString::Format(", %ls", stages.Value(i));
  25. return str;
  26. }
  27. const char* ShaderDataTypeStr(DKShaderDataType t)
  28. {
  29. switch (t) {
  30. case DKShaderDataType::Unknown: return "Unknown";
  31. case DKShaderDataType::None: return "None";
  32. case DKShaderDataType::Struct: return "Struct";
  33. case DKShaderDataType::Texture: return "Texture";
  34. case DKShaderDataType::Sampler: return "Sampler";
  35. case DKShaderDataType::Float: return "Float";
  36. case DKShaderDataType::Float2: return "Float2";
  37. case DKShaderDataType::Float3: return "Float3";
  38. case DKShaderDataType::Float4: return "Float4";
  39. case DKShaderDataType::Float2x2: return "Float2x2";
  40. case DKShaderDataType::Float2x3: return "Float2x3";
  41. case DKShaderDataType::Float2x4: return "Float2x4";
  42. case DKShaderDataType::Float3x2: return "Float3x2";
  43. case DKShaderDataType::Float3x3: return "Float3x3";
  44. case DKShaderDataType::Float3x4: return "Float3x4";
  45. case DKShaderDataType::Float4x2: return "Float4x2";
  46. case DKShaderDataType::Float4x3: return "Float4x3";
  47. case DKShaderDataType::Float4x4: return "Float4x4";
  48. case DKShaderDataType::Half: return "Half";
  49. case DKShaderDataType::Half2: return "Half2";
  50. case DKShaderDataType::Half3: return "Half3";
  51. case DKShaderDataType::Half4: return "Half4";
  52. case DKShaderDataType::Half2x2: return "Half2x2";
  53. case DKShaderDataType::Half2x3: return "Half2x3";
  54. case DKShaderDataType::Half2x4: return "Half2x4";
  55. case DKShaderDataType::Half3x2: return "Half3x2";
  56. case DKShaderDataType::Half3x3: return "Half3x3";
  57. case DKShaderDataType::Half3x4: return "Half3x4";
  58. case DKShaderDataType::Half4x2: return "Half4x2";
  59. case DKShaderDataType::Half4x3: return "Half4x3";
  60. case DKShaderDataType::Half4x4: return "Half4x4";
  61. case DKShaderDataType::Int: return "Int";
  62. case DKShaderDataType::Int2: return "Int2";
  63. case DKShaderDataType::Int3: return "Int3";
  64. case DKShaderDataType::Int4: return "Int4";
  65. case DKShaderDataType::UInt: return "UInt";
  66. case DKShaderDataType::UInt2: return "UInt2";
  67. case DKShaderDataType::UInt3: return "UInt3";
  68. case DKShaderDataType::UInt4: return "UInt4";
  69. case DKShaderDataType::Short: return "Short";
  70. case DKShaderDataType::Short2: return "Short2";
  71. case DKShaderDataType::Short3: return "Short3";
  72. case DKShaderDataType::Short4: return "Short4";
  73. case DKShaderDataType::UShort: return "UShort";
  74. case DKShaderDataType::UShort2: return "UShort2";
  75. case DKShaderDataType::UShort3: return "UShort3";
  76. case DKShaderDataType::UShort4: return "UShort4";
  77. case DKShaderDataType::Char: return "Char";
  78. case DKShaderDataType::Char2: return "Char2";
  79. case DKShaderDataType::Char3: return "Char3";
  80. case DKShaderDataType::Char4: return "Char4";
  81. case DKShaderDataType::UChar: return "UChar";
  82. case DKShaderDataType::UChar2: return "UChar2";
  83. case DKShaderDataType::UChar3: return "UChar3";
  84. case DKShaderDataType::UChar4: return "UChar4";
  85. case DKShaderDataType::Bool: return "Bool";
  86. case DKShaderDataType::Bool2: return "Bool2";
  87. case DKShaderDataType::Bool3: return "Bool3";
  88. case DKShaderDataType::Bool4: return "Bool4";
  89. }
  90. return "Error";
  91. }
  92. void PrintShaderResource(const DKShaderResource& res, DKLogCategory c = DKLogCategory::Info)
  93. {
  94. struct MemberPrinter
  95. {
  96. const DKShaderResource& res;
  97. int indent;
  98. DKLogCategory c;
  99. void operator()(const DKShaderResourceStruct& str) const
  100. {
  101. DKString indentStr = "";
  102. for (int i = 0; i < indent; ++i)
  103. {
  104. indentStr += " ";
  105. }
  106. for (const DKShaderResourceStructMember& mem : str.members)
  107. {
  108. if (mem.count > 1)
  109. {
  110. DKLog(c, " %ls+ %ls[%d] (%s, Offset: %d, Stride: %d)",
  111. (const wchar_t*)indentStr,
  112. (const wchar_t*)mem.name,
  113. mem.count,
  114. ShaderDataTypeStr(mem.dataType),
  115. mem.offset, mem.stride);
  116. }
  117. else
  118. {
  119. DKLog(c, " %ls+ %ls (%s, Offset: %d)",
  120. (const wchar_t*)indentStr,
  121. (const wchar_t*)mem.name,
  122. ShaderDataTypeStr(mem.dataType),
  123. mem.offset);
  124. }
  125. auto* p = res.structTypeMemberMap.Find(mem.typeInfoKey);
  126. if (p)
  127. {
  128. DKLog(c, " %ls Struct \"%ls\"",
  129. (const wchar_t*)indentStr,
  130. (const wchar_t*)mem.typeInfoKey);
  131. MemberPrinter{ res, indent + 1, c }.operator()(p->value);
  132. }
  133. }
  134. }
  135. };
  136. if (res.count > 1)
  137. DKLog(c, "ShaderResource: %ls[%d] (set=%d, binding=%d, stages=%ls)",
  138. (const wchar_t*)res.name, res.count, res.set, res.binding,
  139. (const wchar_t*)ShaderStageNames(res.stages));
  140. else
  141. DKLog(c, "ShaderResource: %ls (set=%d, binding=%d, stages=%ls)",
  142. (const wchar_t*)res.name, res.set, res.binding,
  143. (const wchar_t*)ShaderStageNames(res.stages));
  144. const char* type = "Unknown (ERROR)";
  145. switch (res.type)
  146. {
  147. case DKShaderResource::TypeBuffer: type = "Buffer"; break;
  148. case DKShaderResource::TypeTexture: type = "Texture"; break;
  149. case DKShaderResource::TypeSampler: type = "Sampler"; break;
  150. case DKShaderResource::TypeTextureSampler: type = "SampledTexture"; break;
  151. }
  152. const char* access = "Unknown (ERROR)";
  153. switch (res.access)
  154. {
  155. case DKShaderResource::AccessReadOnly: access = "ReadOnly"; break;
  156. case DKShaderResource::AccessWriteOnly: access = "WriteOnly"; break;
  157. case DKShaderResource::AccessReadWrite: access = "ReadWrite"; break;
  158. }
  159. if (res.type == DKShaderResource::TypeBuffer)
  160. {
  161. DKLog(c, " Type:%s, Access:%s, Enabled:%d, Size:%d",
  162. type,
  163. access,
  164. int(res.enabled),
  165. res.typeInfo.buffer.size);
  166. }
  167. else
  168. {
  169. DKLog(c, " Type:%s, Access:%s, Enabled:%d",
  170. type,
  171. access,
  172. int(res.enabled));
  173. }
  174. if (res.typeInfoKey.Length() > 0)
  175. DKLog(c, " Struct \"%ls\"", (const wchar_t*)res.typeInfoKey);
  176. if (res.type == DKShaderResource::TypeBuffer)
  177. {
  178. auto p = res.structTypeMemberMap.Find(res.typeInfoKey);
  179. if (p)
  180. MemberPrinter{ res, 1 , c }.operator()(p->value);
  181. }
  182. }
  183. void PrintShaderReflection(const DKShader* shader, DKLogCategory c = DKLogCategory::Warning)
  184. {
  185. DKString stage = ShaderStageNames((uint32_t)shader->Stage());
  186. if (stage.Length() == 0)
  187. stage = "Unknown";
  188. DKLog(c, "=========================================================");
  189. DKLog(c, "Shader<%ls.SPIR-V>.InputAttributes: %d",
  190. (const wchar_t*)stage, shader->InputAttributes().Count());
  191. for (int i = 0; i < shader->InputAttributes().Count(); ++i)
  192. {
  193. const DKShaderAttribute& attr = shader->InputAttributes().Value(i);
  194. DKLog(c, " [in] ShaderAttribute[%d]: \"%ls\" (type:%s, location:%u)",
  195. i, (const wchar_t*)attr.name, ShaderDataTypeStr(attr.type), attr.location);
  196. }
  197. DKLog(c, "---------------------------------------------------------");
  198. DKLog(c, "Shader<%ls.SPIR-V>.OutputAttributes: %d",
  199. (const wchar_t*)stage, shader->OutputAttributes().Count());
  200. for (int i = 0; i < shader->OutputAttributes().Count(); ++i)
  201. {
  202. const DKShaderAttribute& attr = shader->OutputAttributes().Value(i);
  203. DKLog(c, " [out] ShaderAttribute[%d]: \"%ls\" (type:%s, location:%u)",
  204. i, (const wchar_t*)attr.name, ShaderDataTypeStr(attr.type), attr.location);
  205. }
  206. DKLog(c, "---------------------------------------------------------");
  207. DKLog(c, "Shader<%ls.SPIR-V>.Resources: %d",
  208. (const wchar_t*)stage, shader->Resources().Count());
  209. for (auto& arg : shader->Resources())
  210. PrintShaderResource(arg, c);
  211. for (int i = 0; i < shader->PushConstantBufferLayouts().Count(); ++i)
  212. {
  213. const DKShaderPushConstantLayout& layout = shader->PushConstantBufferLayouts().Value(i);
  214. DKLog(c, " PushConstant:%d \"%ls\" (offset:%u, size:%u, stages:%ls)",
  215. i, (const wchar_t*)layout.name, layout.offset, layout.size,
  216. (const wchar_t*)ShaderStageNames(layout.stages));
  217. }
  218. DKLog(c, "=========================================================");
  219. }
  220. void PrintPipelineReflection(const DKPipelineReflection* reflection, DKLogCategory c = DKLogCategory::Error)
  221. {
  222. DKLog(c, "=========================================================");
  223. DKLog(c, "PipelineReflection.InputAttributes: %d", reflection->inputAttributes.Count());
  224. for (int i = 0; i < reflection->inputAttributes.Count(); ++i)
  225. {
  226. const DKShaderAttribute& attr = reflection->inputAttributes.Value(i);
  227. DKLog(c, " [in] ShaderAttribute[%d]: \"%ls\" (type:%s, location:%u)",
  228. i, (const wchar_t*)attr.name, ShaderDataTypeStr(attr.type), attr.location);
  229. }
  230. DKLog(c, "---------------------------------------------------------");
  231. DKLog(c, "PipelineReflection.Resources: %d", reflection->resources.Count());
  232. for (auto& arg : reflection->resources)
  233. PrintShaderResource(arg, c);
  234. for (int i = 0; i < reflection->pushConstantLayouts.Count(); ++i)
  235. {
  236. const DKShaderPushConstantLayout& layout = reflection->pushConstantLayouts.Value(i);
  237. DKLog(c, " PushConstant:%d \"%ls\" (offset:%u, size:%u, stages:%ls)",
  238. i, (const wchar_t*)layout.name, layout.offset, layout.size,
  239. (const wchar_t*)ShaderStageNames(layout.stages));
  240. }
  241. DKLog(c, "=========================================================");
  242. }