shaderc_dx11.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * Copyright 2011-2015 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include "shaderc.h"
  6. #if SHADERC_CONFIG_DIRECT3D11
  7. #include <d3dcompiler.h>
  8. #include <d3d11shader.h>
  9. #ifndef D3D_SVF_USED
  10. # define D3D_SVF_USED 2
  11. #endif // D3D_SVF_USED
  12. static const GUID GUID_ID3D11ShaderReflection = { 0x0a233719, 0x3960, 0x4578, { 0x9d, 0x7c, 0x20, 0x3b, 0x8b, 0x1d, 0x9c, 0xc1 } };
  13. struct RemapInputSemantic
  14. {
  15. bgfx::Attrib::Enum m_attr;
  16. const char* m_name;
  17. uint8_t m_index;
  18. };
  19. static const RemapInputSemantic s_remapInputSemantic[bgfx::Attrib::Count+1] =
  20. {
  21. { bgfx::Attrib::Position, "POSITION", 0 },
  22. { bgfx::Attrib::Normal, "NORMAL", 0 },
  23. { bgfx::Attrib::Tangent, "TANGENT", 0 },
  24. { bgfx::Attrib::Bitangent, "BITANGENT", 0 },
  25. { bgfx::Attrib::Color0, "COLOR", 0 },
  26. { bgfx::Attrib::Color1, "COLOR", 1 },
  27. { bgfx::Attrib::Indices, "BLENDINDICES", 0 },
  28. { bgfx::Attrib::Weight, "BLENDWEIGHT", 0 },
  29. { bgfx::Attrib::TexCoord0, "TEXCOORD", 0 },
  30. { bgfx::Attrib::TexCoord1, "TEXCOORD", 1 },
  31. { bgfx::Attrib::TexCoord2, "TEXCOORD", 2 },
  32. { bgfx::Attrib::TexCoord3, "TEXCOORD", 3 },
  33. { bgfx::Attrib::TexCoord4, "TEXCOORD", 4 },
  34. { bgfx::Attrib::TexCoord5, "TEXCOORD", 5 },
  35. { bgfx::Attrib::TexCoord6, "TEXCOORD", 6 },
  36. { bgfx::Attrib::TexCoord7, "TEXCOORD", 7 },
  37. { bgfx::Attrib::Count, "", 0 },
  38. };
  39. const RemapInputSemantic& findInputSemantic(const char* _name, uint8_t _index)
  40. {
  41. for (uint32_t ii = 0; ii < bgfx::Attrib::Count; ++ii)
  42. {
  43. const RemapInputSemantic& ris = s_remapInputSemantic[ii];
  44. if (0 == strcmp(ris.m_name, _name)
  45. && ris.m_index == _index)
  46. {
  47. return ris;
  48. }
  49. }
  50. return s_remapInputSemantic[bgfx::Attrib::Count];
  51. }
  52. struct UniformRemapDx11
  53. {
  54. UniformType::Enum id;
  55. D3D_SHADER_VARIABLE_CLASS paramClass;
  56. D3D_SHADER_VARIABLE_TYPE paramType;
  57. uint8_t columns;
  58. uint8_t rows;
  59. };
  60. static const UniformRemapDx11 s_constRemapDx11[7] =
  61. {
  62. { UniformType::Uniform1iv, D3D_SVC_SCALAR, D3D_SVT_INT, 0, 0 },
  63. { UniformType::Uniform1fv, D3D_SVC_SCALAR, D3D_SVT_FLOAT, 0, 0 },
  64. { UniformType::Uniform2fv, D3D_SVC_VECTOR, D3D_SVT_FLOAT, 0, 0 },
  65. { UniformType::Uniform3fv, D3D_SVC_VECTOR, D3D_SVT_FLOAT, 0, 0 },
  66. { UniformType::Uniform4fv, D3D_SVC_VECTOR, D3D_SVT_FLOAT, 0, 0 },
  67. { UniformType::Uniform3x3fv, D3D_SVC_MATRIX_COLUMNS, D3D_SVT_FLOAT, 3, 3 },
  68. { UniformType::Uniform4x4fv, D3D_SVC_MATRIX_COLUMNS, D3D_SVT_FLOAT, 4, 4 },
  69. };
  70. UniformType::Enum findUniformTypeDx11(const D3D11_SHADER_TYPE_DESC& constDesc)
  71. {
  72. for (uint32_t ii = 0; ii < BX_COUNTOF(s_constRemapDx11); ++ii)
  73. {
  74. const UniformRemapDx11& remap = s_constRemapDx11[ii];
  75. if (remap.paramClass == constDesc.Class
  76. && remap.paramType == constDesc.Type)
  77. {
  78. if (D3D_SVC_MATRIX_COLUMNS != constDesc.Class)
  79. {
  80. return remap.id;
  81. }
  82. if (remap.columns == constDesc.Columns
  83. && remap.rows == constDesc.Rows)
  84. {
  85. return remap.id;
  86. }
  87. }
  88. }
  89. return UniformType::Count;
  90. }
  91. static uint32_t s_optimizationLevelDx11[4] =
  92. {
  93. D3DCOMPILE_OPTIMIZATION_LEVEL0,
  94. D3DCOMPILE_OPTIMIZATION_LEVEL1,
  95. D3DCOMPILE_OPTIMIZATION_LEVEL2,
  96. D3DCOMPILE_OPTIMIZATION_LEVEL3,
  97. };
  98. bool compileHLSLShaderDx11(bx::CommandLine& _cmdLine, const std::string& _code, bx::WriterI* _writer)
  99. {
  100. BX_TRACE("DX11");
  101. const char* profile = _cmdLine.findOption('p', "profile");
  102. if (NULL == profile)
  103. {
  104. fprintf(stderr, "Shader profile must be specified.\n");
  105. return false;
  106. }
  107. bool debug = _cmdLine.hasArg('\0', "debug");
  108. uint32_t flags = D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY;
  109. flags |= debug ? D3DCOMPILE_DEBUG : 0;
  110. flags |= _cmdLine.hasArg('\0', "avoid-flow-control") ? D3DCOMPILE_AVOID_FLOW_CONTROL : 0;
  111. flags |= _cmdLine.hasArg('\0', "no-preshader") ? D3DCOMPILE_NO_PRESHADER : 0;
  112. flags |= _cmdLine.hasArg('\0', "partial-precision") ? D3DCOMPILE_PARTIAL_PRECISION : 0;
  113. flags |= _cmdLine.hasArg('\0', "prefer-flow-control") ? D3DCOMPILE_PREFER_FLOW_CONTROL : 0;
  114. flags |= _cmdLine.hasArg('\0', "backwards-compatibility") ? D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY : 0;
  115. bool werror = _cmdLine.hasArg('\0', "Werror");
  116. if (werror)
  117. {
  118. flags |= D3DCOMPILE_WARNINGS_ARE_ERRORS;
  119. }
  120. uint32_t optimization = 3;
  121. if (_cmdLine.hasArg(optimization, 'O') )
  122. {
  123. optimization = bx::uint32_min(optimization, BX_COUNTOF(s_optimizationLevelDx11)-1);
  124. flags |= s_optimizationLevelDx11[optimization];
  125. }
  126. else
  127. {
  128. flags |= D3DCOMPILE_SKIP_OPTIMIZATION;
  129. }
  130. BX_TRACE("Profile: %s", profile);
  131. BX_TRACE("Flags: 0x%08x", flags);
  132. ID3DBlob* code;
  133. ID3DBlob* errorMsg;
  134. // Output preprocessed shader so that HLSL can be debugged via GPA
  135. // or PIX. Compiling through memory won't embed preprocessed shader
  136. // file path.
  137. std::string hlslfp;
  138. if (debug)
  139. {
  140. hlslfp = _cmdLine.findOption('o');
  141. hlslfp += ".hlsl";
  142. writeFile(hlslfp.c_str(), _code.c_str(), (int32_t)_code.size() );
  143. }
  144. HRESULT hr = D3DCompile(_code.c_str()
  145. , _code.size()
  146. , hlslfp.c_str()
  147. , NULL
  148. , NULL
  149. , "main"
  150. , profile
  151. , flags
  152. , 0
  153. , &code
  154. , &errorMsg
  155. );
  156. if (FAILED(hr)
  157. || (werror && NULL != errorMsg) )
  158. {
  159. const char* log = (char*)errorMsg->GetBufferPointer();
  160. int32_t line = 0;
  161. int32_t column = 0;
  162. int32_t start = 0;
  163. int32_t end = INT32_MAX;
  164. if (2 == sscanf(log, "(%u,%u):", &line, &column)
  165. && 0 != line)
  166. {
  167. start = bx::uint32_imax(1, line-10);
  168. end = start + 20;
  169. }
  170. printCode(_code.c_str(), line, start, end);
  171. fprintf(stderr, "Error: 0x%08x %s\n", (uint32_t)hr, log);
  172. errorMsg->Release();
  173. return false;
  174. }
  175. UniformArray uniforms;
  176. ID3D11ShaderReflection* reflect = NULL;
  177. hr = D3DReflect(code->GetBufferPointer()
  178. , code->GetBufferSize()
  179. , GUID_ID3D11ShaderReflection
  180. , (void**)&reflect
  181. );
  182. if (FAILED(hr) )
  183. {
  184. fprintf(stderr, "Error: 0x%08x\n", (uint32_t)hr);
  185. return false;
  186. }
  187. D3D11_SHADER_DESC desc;
  188. hr = reflect->GetDesc(&desc);
  189. if (FAILED(hr) )
  190. {
  191. fprintf(stderr, BX_FILE_LINE_LITERAL "Error: 0x%08x\n", (uint32_t)hr);
  192. return false;
  193. }
  194. BX_TRACE("Creator: %s 0x%08x", desc.Creator, desc.Version);
  195. BX_TRACE("Num constant buffers: %d", desc.ConstantBuffers);
  196. BX_TRACE("Input:");
  197. uint8_t numAttrs = 0;
  198. uint16_t attrs[bgfx::Attrib::Count];
  199. if (profile[0] == 'v') // Only care about input semantic on vertex shaders
  200. {
  201. for (uint32_t ii = 0; ii < desc.InputParameters; ++ii)
  202. {
  203. D3D11_SIGNATURE_PARAMETER_DESC spd;
  204. reflect->GetInputParameterDesc(ii, &spd);
  205. BX_TRACE("\t%2d: %s%d, vt %d, ct %d, mask %x, reg %d"
  206. , ii
  207. , spd.SemanticName
  208. , spd.SemanticIndex
  209. , spd.SystemValueType
  210. , spd.ComponentType
  211. , spd.Mask
  212. , spd.Register
  213. );
  214. const RemapInputSemantic& ris = findInputSemantic(spd.SemanticName, spd.SemanticIndex);
  215. if (ris.m_attr != bgfx::Attrib::Count)
  216. {
  217. attrs[numAttrs] = bgfx::attribToId(ris.m_attr);
  218. ++numAttrs;
  219. }
  220. }
  221. }
  222. BX_TRACE("Output:");
  223. for (uint32_t ii = 0; ii < desc.OutputParameters; ++ii)
  224. {
  225. D3D11_SIGNATURE_PARAMETER_DESC spd;
  226. reflect->GetOutputParameterDesc(ii, &spd);
  227. BX_TRACE("\t%2d: %s%d, %d, %d", ii, spd.SemanticName, spd.SemanticIndex, spd.SystemValueType, spd.ComponentType);
  228. }
  229. uint16_t size = 0;
  230. for (uint32_t ii = 0; ii < bx::uint32_min(1, desc.ConstantBuffers); ++ii)
  231. {
  232. ID3D11ShaderReflectionConstantBuffer* cbuffer = reflect->GetConstantBufferByIndex(ii);
  233. D3D11_SHADER_BUFFER_DESC bufferDesc;
  234. hr = cbuffer->GetDesc(&bufferDesc);
  235. size = (uint16_t)bufferDesc.Size;
  236. if (SUCCEEDED(hr) )
  237. {
  238. BX_TRACE("%s, %d, vars %d, size %d"
  239. , bufferDesc.Name
  240. , bufferDesc.Type
  241. , bufferDesc.Variables
  242. , bufferDesc.Size
  243. );
  244. for (uint32_t jj = 0; jj < bufferDesc.Variables; ++jj)
  245. {
  246. ID3D11ShaderReflectionVariable* var = cbuffer->GetVariableByIndex(jj);
  247. ID3D11ShaderReflectionType* type = var->GetType();
  248. D3D11_SHADER_VARIABLE_DESC varDesc;
  249. hr = var->GetDesc(&varDesc);
  250. if (SUCCEEDED(hr) )
  251. {
  252. D3D11_SHADER_TYPE_DESC constDesc;
  253. hr = type->GetDesc(&constDesc);
  254. if (SUCCEEDED(hr) )
  255. {
  256. UniformType::Enum type = findUniformTypeDx11(constDesc);
  257. if (UniformType::Count != type
  258. && 0 != (varDesc.uFlags & D3D_SVF_USED) )
  259. {
  260. Uniform un;
  261. un.name = varDesc.Name;
  262. un.type = type;
  263. un.num = constDesc.Elements;
  264. un.regIndex = varDesc.StartOffset;
  265. un.regCount = BX_ALIGN_16(varDesc.Size)/16;
  266. uniforms.push_back(un);
  267. BX_TRACE("\t%s, %d, size %d, flags 0x%08x, %d"
  268. , varDesc.Name
  269. , varDesc.StartOffset
  270. , varDesc.Size
  271. , varDesc.uFlags
  272. , type
  273. );
  274. }
  275. else
  276. {
  277. BX_TRACE("\t%s, unknown type", varDesc.Name);
  278. }
  279. }
  280. }
  281. }
  282. }
  283. }
  284. BX_TRACE("Bound:");
  285. for (uint32_t ii = 0; ii < desc.BoundResources; ++ii)
  286. {
  287. D3D11_SHADER_INPUT_BIND_DESC bindDesc;
  288. hr = reflect->GetResourceBindingDesc(ii, &bindDesc);
  289. if (SUCCEEDED(hr) )
  290. {
  291. // if (bindDesc.Type == D3D_SIT_SAMPLER)
  292. {
  293. BX_TRACE("\t%s, %d, %d, %d"
  294. , bindDesc.Name
  295. , bindDesc.Type
  296. , bindDesc.BindPoint
  297. , bindDesc.BindCount
  298. );
  299. }
  300. }
  301. }
  302. uint16_t count = (uint16_t)uniforms.size();
  303. bx::write(_writer, count);
  304. uint32_t fragmentBit = profile[0] == 'p' ? BGFX_UNIFORM_FRAGMENTBIT : 0;
  305. for (UniformArray::const_iterator it = uniforms.begin(); it != uniforms.end(); ++it)
  306. {
  307. const Uniform& un = *it;
  308. uint8_t nameSize = (uint8_t)un.name.size();
  309. bx::write(_writer, nameSize);
  310. bx::write(_writer, un.name.c_str(), nameSize);
  311. uint8_t type = un.type|fragmentBit;
  312. bx::write(_writer, type);
  313. bx::write(_writer, un.num);
  314. bx::write(_writer, un.regIndex);
  315. bx::write(_writer, un.regCount);
  316. BX_TRACE("%s, %s, %d, %d, %d"
  317. , un.name.c_str()
  318. , getUniformTypeName(un.type)
  319. , un.num
  320. , un.regIndex
  321. , un.regCount
  322. );
  323. }
  324. {
  325. ID3DBlob* stripped;
  326. hr = D3DStripShader(code->GetBufferPointer()
  327. , code->GetBufferSize()
  328. , D3DCOMPILER_STRIP_REFLECTION_DATA
  329. | D3DCOMPILER_STRIP_TEST_BLOBS
  330. , &stripped
  331. );
  332. if (SUCCEEDED(hr) )
  333. {
  334. code->Release();
  335. code = stripped;
  336. }
  337. }
  338. uint16_t shaderSize = (uint16_t)code->GetBufferSize();
  339. bx::write(_writer, shaderSize);
  340. bx::write(_writer, code->GetBufferPointer(), shaderSize);
  341. uint8_t nul = 0;
  342. bx::write(_writer, nul);
  343. bx::write(_writer, numAttrs);
  344. bx::write(_writer, attrs, numAttrs*sizeof(uint16_t) );
  345. bx::write(_writer, size);
  346. if (_cmdLine.hasArg('\0', "disasm") )
  347. {
  348. ID3DBlob* disasm;
  349. D3DDisassemble(code->GetBufferPointer()
  350. , code->GetBufferSize()
  351. , 0
  352. , NULL
  353. , &disasm
  354. );
  355. if (NULL != disasm)
  356. {
  357. std::string disasmfp = _cmdLine.findOption('o');
  358. disasmfp += ".disasm";
  359. writeFile(disasmfp.c_str(), disasm->GetBufferPointer(), (uint32_t)disasm->GetBufferSize() );
  360. disasm->Release();
  361. }
  362. }
  363. if (NULL != reflect)
  364. {
  365. reflect->Release();
  366. }
  367. if (NULL != errorMsg)
  368. {
  369. errorMsg->Release();
  370. }
  371. code->Release();
  372. return true;
  373. }
  374. #else
  375. bool compileHLSLShaderDx11(bx::CommandLine& _cmdLine, const std::string& _code, bx::WriterI* _writer)
  376. {
  377. BX_UNUSED(_cmdLine, _code, _writer);
  378. fprintf(stderr, "HLSL compiler is not supported on this platform.\n");
  379. return false;
  380. }
  381. #endif // SHADERC_CONFIG_DIRECT3D11