shaderc_d3d11.cpp 11 KB

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