shaderc_dx11.cpp 11 KB

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