shaderc_d3d9.cpp 7.5 KB

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