shaderc_dx9.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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_DIRECT3D9
  7. #include <sal.h>
  8. #define __D3DX9MATH_INL__ // not used and MinGW complains about type-punning
  9. BX_PRAGMA_DIAGNOSTIC_PUSH();
  10. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wundef");
  11. #include <d3dx9.h>
  12. BX_PRAGMA_DIAGNOSTIC_POP();
  13. struct UniformRemapDx9
  14. {
  15. UniformType::Enum id;
  16. D3DXPARAMETER_CLASS paramClass;
  17. D3DXPARAMETER_TYPE paramType;
  18. uint8_t columns;
  19. uint8_t rows;
  20. };
  21. static const UniformRemapDx9 s_constRemapDx9[7] =
  22. {
  23. { UniformType::Uniform1iv, D3DXPC_SCALAR, D3DXPT_INT, 0, 0 },
  24. { UniformType::Uniform1fv, D3DXPC_SCALAR, D3DXPT_FLOAT, 0, 0 },
  25. { UniformType::Uniform2fv, D3DXPC_VECTOR, D3DXPT_FLOAT, 0, 0 },
  26. { UniformType::Uniform3fv, D3DXPC_VECTOR, D3DXPT_FLOAT, 0, 0 },
  27. { UniformType::Uniform4fv, D3DXPC_VECTOR, D3DXPT_FLOAT, 0, 0 },
  28. { UniformType::Uniform3x3fv, D3DXPC_MATRIX_COLUMNS, D3DXPT_FLOAT, 3, 3 },
  29. { UniformType::Uniform4x4fv, D3DXPC_MATRIX_COLUMNS, D3DXPT_FLOAT, 4, 4 },
  30. };
  31. UniformType::Enum findUniformTypeDx9(const D3DXCONSTANT_DESC& constDesc)
  32. {
  33. for (uint32_t ii = 0; ii < BX_COUNTOF(s_constRemapDx9); ++ii)
  34. {
  35. const UniformRemapDx9& remap = s_constRemapDx9[ii];
  36. if (remap.paramClass == constDesc.Class
  37. && remap.paramType == constDesc.Type)
  38. {
  39. if (D3DXPC_MATRIX_COLUMNS != constDesc.Class)
  40. {
  41. return remap.id;
  42. }
  43. if (remap.columns == constDesc.Columns
  44. && remap.rows == constDesc.Rows)
  45. {
  46. return remap.id;
  47. }
  48. }
  49. }
  50. return UniformType::Count;
  51. }
  52. static uint32_t s_optimizationLevelDx9[4] =
  53. {
  54. D3DXSHADER_OPTIMIZATION_LEVEL0,
  55. D3DXSHADER_OPTIMIZATION_LEVEL1,
  56. D3DXSHADER_OPTIMIZATION_LEVEL2,
  57. D3DXSHADER_OPTIMIZATION_LEVEL3,
  58. };
  59. bool compileHLSLShaderDx9(bx::CommandLine& _cmdLine, const std::string& _code, bx::WriterI* _writer)
  60. {
  61. BX_TRACE("DX9");
  62. const char* profile = _cmdLine.findOption('p', "profile");
  63. if (NULL == profile)
  64. {
  65. fprintf(stderr, "Shader profile must be specified.\n");
  66. return false;
  67. }
  68. bool debug = _cmdLine.hasArg('\0', "debug");
  69. uint32_t flags = 0;
  70. flags |= debug ? D3DXSHADER_DEBUG : 0;
  71. flags |= _cmdLine.hasArg('\0', "avoid-flow-control") ? D3DXSHADER_AVOID_FLOW_CONTROL : 0;
  72. flags |= _cmdLine.hasArg('\0', "no-preshader") ? D3DXSHADER_NO_PRESHADER : 0;
  73. flags |= _cmdLine.hasArg('\0', "partial-precision") ? D3DXSHADER_PARTIALPRECISION : 0;
  74. flags |= _cmdLine.hasArg('\0', "prefer-flow-control") ? D3DXSHADER_PREFER_FLOW_CONTROL : 0;
  75. flags |= _cmdLine.hasArg('\0', "backwards-compatibility") ? D3DXSHADER_ENABLE_BACKWARDS_COMPATIBILITY : 0;
  76. bool werror = _cmdLine.hasArg('\0', "Werror");
  77. uint32_t optimization = 3;
  78. if (_cmdLine.hasArg(optimization, 'O') )
  79. {
  80. optimization = bx::uint32_min(optimization, BX_COUNTOF(s_optimizationLevelDx9)-1);
  81. flags |= s_optimizationLevelDx9[optimization];
  82. }
  83. else
  84. {
  85. flags |= D3DXSHADER_SKIPOPTIMIZATION;
  86. }
  87. BX_TRACE("Profile: %s", profile);
  88. BX_TRACE("Flags: 0x%08x", flags);
  89. LPD3DXBUFFER code;
  90. LPD3DXBUFFER errorMsg;
  91. LPD3DXCONSTANTTABLE constantTable;
  92. HRESULT hr;
  93. // Output preprocessed shader so that HLSL can be debugged via GPA
  94. // or PIX. Compiling through memory won't embed preprocessed shader
  95. // file path.
  96. if (debug)
  97. {
  98. std::string hlslfp = _cmdLine.findOption('o');
  99. hlslfp += ".hlsl";
  100. writeFile(hlslfp.c_str(), _code.c_str(), (int32_t)_code.size() );
  101. hr = D3DXCompileShaderFromFileA(hlslfp.c_str()
  102. , NULL
  103. , NULL
  104. , "main"
  105. , profile
  106. , flags
  107. , &code
  108. , &errorMsg
  109. , &constantTable
  110. );
  111. }
  112. else
  113. {
  114. hr = D3DXCompileShader(_code.c_str()
  115. , (uint32_t)_code.size()
  116. , NULL
  117. , NULL
  118. , "main"
  119. , profile
  120. , flags
  121. , &code
  122. , &errorMsg
  123. , &constantTable
  124. );
  125. }
  126. if (FAILED(hr)
  127. || (werror && NULL != errorMsg) )
  128. {
  129. const char* log = (const char*)errorMsg->GetBufferPointer();
  130. char source[1024];
  131. int32_t line = 0;
  132. int32_t column = 0;
  133. int32_t start = 0;
  134. int32_t end = INT32_MAX;
  135. if (3 == sscanf(log, "%[^(](%u,%u):", source, &line, &column)
  136. && 0 != line)
  137. {
  138. start = bx::uint32_imax(1, line-10);
  139. end = start + 20;
  140. }
  141. printCode(_code.c_str(), line, start, end);
  142. fprintf(stderr, "Error: 0x%08x %s\n", (uint32_t)hr, log);
  143. errorMsg->Release();
  144. return false;
  145. }
  146. UniformArray uniforms;
  147. if (NULL != constantTable)
  148. {
  149. D3DXCONSTANTTABLE_DESC desc;
  150. hr = constantTable->GetDesc(&desc);
  151. if (FAILED(hr) )
  152. {
  153. fprintf(stderr, "Error 0x%08x\n", (uint32_t)hr);
  154. return false;
  155. }
  156. BX_TRACE("Creator: %s 0x%08x", desc.Creator, (uint32_t /*mingw warning*/)desc.Version);
  157. BX_TRACE("Num constants: %d", desc.Constants);
  158. BX_TRACE("# cl ty RxC S By Name");
  159. for (uint32_t ii = 0; ii < desc.Constants; ++ii)
  160. {
  161. D3DXHANDLE handle = constantTable->GetConstant(NULL, ii);
  162. D3DXCONSTANT_DESC constDesc;
  163. uint32_t count;
  164. constantTable->GetConstantDesc(handle, &constDesc, &count);
  165. BX_TRACE("%3d %2d %2d [%dx%d] %d %3d %s[%d] c%d (%d)"
  166. , ii
  167. , constDesc.Class
  168. , constDesc.Type
  169. , constDesc.Rows
  170. , constDesc.Columns
  171. , constDesc.StructMembers
  172. , constDesc.Bytes
  173. , constDesc.Name
  174. , constDesc.Elements
  175. , constDesc.RegisterIndex
  176. , constDesc.RegisterCount
  177. );
  178. UniformType::Enum type = findUniformTypeDx9(constDesc);
  179. if (UniformType::Count != type)
  180. {
  181. Uniform un;
  182. un.name = '$' == constDesc.Name[0] ? constDesc.Name+1 : constDesc.Name;
  183. un.type = type;
  184. un.num = constDesc.Elements;
  185. un.regIndex = constDesc.RegisterIndex;
  186. un.regCount = constDesc.RegisterCount;
  187. uniforms.push_back(un);
  188. }
  189. }
  190. }
  191. uint16_t count = (uint16_t)uniforms.size();
  192. bx::write(_writer, count);
  193. uint32_t fragmentBit = profile[0] == 'p' ? BGFX_UNIFORM_FRAGMENTBIT : 0;
  194. for (UniformArray::const_iterator it = uniforms.begin(); it != uniforms.end(); ++it)
  195. {
  196. const Uniform& un = *it;
  197. uint8_t nameSize = (uint8_t)un.name.size();
  198. bx::write(_writer, nameSize);
  199. bx::write(_writer, un.name.c_str(), nameSize);
  200. uint8_t type = un.type|fragmentBit;
  201. bx::write(_writer, type);
  202. bx::write(_writer, un.num);
  203. bx::write(_writer, un.regIndex);
  204. bx::write(_writer, un.regCount);
  205. BX_TRACE("%s, %s, %d, %d, %d"
  206. , un.name.c_str()
  207. , getUniformTypeName(un.type)
  208. , un.num
  209. , un.regIndex
  210. , un.regCount
  211. );
  212. }
  213. uint16_t shaderSize = (uint16_t)code->GetBufferSize();
  214. bx::write(_writer, shaderSize);
  215. bx::write(_writer, code->GetBufferPointer(), shaderSize);
  216. uint8_t nul = 0;
  217. bx::write(_writer, nul);
  218. if (_cmdLine.hasArg('\0', "disasm") )
  219. {
  220. LPD3DXBUFFER disasm;
  221. D3DXDisassembleShader( (const DWORD*)code->GetBufferPointer()
  222. , false
  223. , NULL
  224. , &disasm
  225. );
  226. if (NULL != disasm)
  227. {
  228. std::string disasmfp = _cmdLine.findOption('o');
  229. disasmfp += ".disasm";
  230. writeFile(disasmfp.c_str(), disasm->GetBufferPointer(), disasm->GetBufferSize() );
  231. disasm->Release();
  232. }
  233. }
  234. if (NULL != code)
  235. {
  236. code->Release();
  237. }
  238. if (NULL != errorMsg)
  239. {
  240. errorMsg->Release();
  241. }
  242. if (NULL != constantTable)
  243. {
  244. constantTable->Release();
  245. }
  246. return true;
  247. }
  248. #else
  249. bool compileHLSLShaderDx9(bx::CommandLine& _cmdLine, const std::string& _code, bx::WriterI* _writer)
  250. {
  251. BX_UNUSED(_cmdLine, _code, _writer);
  252. fprintf(stderr, "HLSL compiler is not supported on this platform.\n");
  253. return false;
  254. }
  255. #endif // SHADERC_CONFIG_DIRECT3D9