shaderc_hlsl.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. /*
  2. * Copyright 2011-2016 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include "shaderc.h"
  6. #if SHADERC_CONFIG_HLSL
  7. #define INITGUID
  8. #include <d3dcompiler.h>
  9. #include <d3d11shader.h>
  10. #ifndef D3D_SVF_USED
  11. # define D3D_SVF_USED 2
  12. #endif // D3D_SVF_USED
  13. struct CTHeader
  14. {
  15. uint32_t Size;
  16. uint32_t Creator;
  17. uint32_t Version;
  18. uint32_t Constants;
  19. uint32_t ConstantInfo;
  20. uint32_t Flags;
  21. uint32_t Target;
  22. };
  23. struct CTInfo
  24. {
  25. uint32_t Name;
  26. uint16_t RegisterSet;
  27. uint16_t RegisterIndex;
  28. uint16_t RegisterCount;
  29. uint16_t Reserved;
  30. uint32_t TypeInfo;
  31. uint32_t DefaultValue;
  32. };
  33. struct CTType
  34. {
  35. uint16_t Class;
  36. uint16_t Type;
  37. uint16_t Rows;
  38. uint16_t Columns;
  39. uint16_t Elements;
  40. uint16_t StructMembers;
  41. uint32_t StructMemberInfo;
  42. };
  43. struct RemapInputSemantic
  44. {
  45. bgfx::Attrib::Enum m_attr;
  46. const char* m_name;
  47. uint8_t m_index;
  48. };
  49. static const RemapInputSemantic s_remapInputSemantic[bgfx::Attrib::Count+1] =
  50. {
  51. { bgfx::Attrib::Position, "POSITION", 0 },
  52. { bgfx::Attrib::Normal, "NORMAL", 0 },
  53. { bgfx::Attrib::Tangent, "TANGENT", 0 },
  54. { bgfx::Attrib::Bitangent, "BITANGENT", 0 },
  55. { bgfx::Attrib::Color0, "COLOR", 0 },
  56. { bgfx::Attrib::Color1, "COLOR", 1 },
  57. { bgfx::Attrib::Indices, "BLENDINDICES", 0 },
  58. { bgfx::Attrib::Weight, "BLENDWEIGHT", 0 },
  59. { bgfx::Attrib::TexCoord0, "TEXCOORD", 0 },
  60. { bgfx::Attrib::TexCoord1, "TEXCOORD", 1 },
  61. { bgfx::Attrib::TexCoord2, "TEXCOORD", 2 },
  62. { bgfx::Attrib::TexCoord3, "TEXCOORD", 3 },
  63. { bgfx::Attrib::TexCoord4, "TEXCOORD", 4 },
  64. { bgfx::Attrib::TexCoord5, "TEXCOORD", 5 },
  65. { bgfx::Attrib::TexCoord6, "TEXCOORD", 6 },
  66. { bgfx::Attrib::TexCoord7, "TEXCOORD", 7 },
  67. { bgfx::Attrib::Count, "", 0 },
  68. };
  69. const RemapInputSemantic& findInputSemantic(const char* _name, uint8_t _index)
  70. {
  71. for (uint32_t ii = 0; ii < bgfx::Attrib::Count; ++ii)
  72. {
  73. const RemapInputSemantic& ris = s_remapInputSemantic[ii];
  74. if (0 == strcmp(ris.m_name, _name)
  75. && ris.m_index == _index)
  76. {
  77. return ris;
  78. }
  79. }
  80. return s_remapInputSemantic[bgfx::Attrib::Count];
  81. }
  82. struct UniformRemap
  83. {
  84. UniformType::Enum id;
  85. D3D_SHADER_VARIABLE_CLASS paramClass;
  86. D3D_SHADER_VARIABLE_TYPE paramType;
  87. uint8_t columns;
  88. uint8_t rows;
  89. };
  90. static const UniformRemap s_uniformRemap[] =
  91. {
  92. { UniformType::Int1, D3D_SVC_SCALAR, D3D_SVT_INT, 0, 0 },
  93. { UniformType::Vec4, D3D_SVC_VECTOR, D3D_SVT_FLOAT, 0, 0 },
  94. { UniformType::Mat3, D3D_SVC_MATRIX_COLUMNS, D3D_SVT_FLOAT, 3, 3 },
  95. { UniformType::Mat4, D3D_SVC_MATRIX_COLUMNS, D3D_SVT_FLOAT, 4, 4 },
  96. { UniformType::Int1, D3D_SVC_OBJECT, D3D_SVT_SAMPLER, 0, 0 },
  97. { UniformType::Int1, D3D_SVC_OBJECT, D3D_SVT_SAMPLER2D, 0, 0 },
  98. { UniformType::Int1, D3D_SVC_OBJECT, D3D_SVT_SAMPLER3D, 0, 0 },
  99. { UniformType::Int1, D3D_SVC_OBJECT, D3D_SVT_SAMPLERCUBE, 0, 0 },
  100. };
  101. UniformType::Enum findUniformType(const D3D11_SHADER_TYPE_DESC& constDesc)
  102. {
  103. for (uint32_t ii = 0; ii < BX_COUNTOF(s_uniformRemap); ++ii)
  104. {
  105. const UniformRemap& remap = s_uniformRemap[ii];
  106. if (remap.paramClass == constDesc.Class
  107. && remap.paramType == constDesc.Type)
  108. {
  109. if (D3D_SVC_MATRIX_COLUMNS != constDesc.Class)
  110. {
  111. return remap.id;
  112. }
  113. if (remap.columns == constDesc.Columns
  114. && remap.rows == constDesc.Rows)
  115. {
  116. return remap.id;
  117. }
  118. }
  119. }
  120. return UniformType::Count;
  121. }
  122. static uint32_t s_optimizationLevelDx11[4] =
  123. {
  124. D3DCOMPILE_OPTIMIZATION_LEVEL0,
  125. D3DCOMPILE_OPTIMIZATION_LEVEL1,
  126. D3DCOMPILE_OPTIMIZATION_LEVEL2,
  127. D3DCOMPILE_OPTIMIZATION_LEVEL3,
  128. };
  129. typedef std::vector<std::string> UniformNameList;
  130. static bool isSampler(D3D_SHADER_VARIABLE_TYPE _svt)
  131. {
  132. switch (_svt)
  133. {
  134. case D3D_SVT_SAMPLER:
  135. case D3D_SVT_SAMPLER1D:
  136. case D3D_SVT_SAMPLER2D:
  137. case D3D_SVT_SAMPLER3D:
  138. case D3D_SVT_SAMPLERCUBE:
  139. return true;
  140. default:
  141. break;
  142. }
  143. return false;
  144. }
  145. bool getReflectionDataDx9(ID3DBlob* _code, UniformArray& _uniforms)
  146. {
  147. // see reference for magic values: https://msdn.microsoft.com/en-us/library/ff552891(VS.85).aspx
  148. const uint32_t D3DSIO_COMMENT = 0x0000FFFE;
  149. const uint32_t D3DSIO_END = 0x0000FFFF;
  150. const uint32_t D3DSI_OPCODE_MASK = 0x0000FFFF;
  151. const uint32_t D3DSI_COMMENTSIZE_MASK = 0x7FFF0000;
  152. const uint32_t CTAB_CONSTANT = MAKEFOURCC('C','T','A','B');
  153. // parse the shader blob for the constant table
  154. const size_t codeSize = _code->GetBufferSize();
  155. const uint32_t* ptr = (const uint32_t*)_code->GetBufferPointer();
  156. const uint32_t* end = (const uint32_t*)( (const uint8_t*)ptr + codeSize);
  157. const CTHeader* header = NULL;
  158. ptr++; // first byte is shader type / version; skip it since we already know
  159. while (ptr < end && *ptr != D3DSIO_END)
  160. {
  161. uint32_t cur = *ptr++;
  162. if ( (cur & D3DSI_OPCODE_MASK) != D3DSIO_COMMENT)
  163. {
  164. continue;
  165. }
  166. // try to find CTAB comment block
  167. uint32_t commentSize = (cur & D3DSI_COMMENTSIZE_MASK) >> 16;
  168. uint32_t fourcc = *ptr;
  169. if (fourcc == CTAB_CONSTANT)
  170. {
  171. // found the constant table data
  172. header = (const CTHeader*)(ptr + 1);
  173. uint32_t tableSize = (commentSize - 1) * 4;
  174. if (tableSize < sizeof(CTHeader) || header->Size != sizeof(CTHeader) )
  175. {
  176. fprintf(stderr, "Error: Invalid constant table data\n");
  177. return false;
  178. }
  179. break;
  180. }
  181. // this is a different kind of comment section, so skip over it
  182. ptr += commentSize - 1;
  183. }
  184. if (!header)
  185. {
  186. fprintf(stderr, "Error: Could not find constant table data\n");
  187. return false;
  188. }
  189. const uint8_t* headerBytePtr = (const uint8_t*)header;
  190. const char* creator = (const char*)(headerBytePtr + header->Creator);
  191. BX_TRACE("Creator: %s 0x%08x", creator, header->Version);
  192. BX_TRACE("Num constants: %d", header->Constants);
  193. BX_TRACE("# cl ty RxC S By Name");
  194. const CTInfo* ctInfoArray = (const CTInfo*)(headerBytePtr + header->ConstantInfo);
  195. for (uint32_t ii = 0; ii < header->Constants; ++ii)
  196. {
  197. const CTInfo& ctInfo = ctInfoArray[ii];
  198. const CTType& ctType = *(const CTType*)(headerBytePtr + ctInfo.TypeInfo);
  199. const char* name = (const char*)(headerBytePtr + ctInfo.Name);
  200. BX_TRACE("%3d %2d %2d [%dx%d] %d %s[%d] c%d (%d)"
  201. , ii
  202. , ctType.Class
  203. , ctType.Type
  204. , ctType.Rows
  205. , ctType.Columns
  206. , ctType.StructMembers
  207. , name
  208. , ctType.Elements
  209. , ctInfo.RegisterIndex
  210. , ctInfo.RegisterCount
  211. );
  212. D3D11_SHADER_TYPE_DESC desc;
  213. desc.Class = (D3D_SHADER_VARIABLE_CLASS)ctType.Class;
  214. desc.Type = (D3D_SHADER_VARIABLE_TYPE)ctType.Type;
  215. desc.Rows = ctType.Rows;
  216. desc.Columns = ctType.Columns;
  217. UniformType::Enum type = findUniformType(desc);
  218. if (UniformType::Count != type)
  219. {
  220. Uniform un;
  221. un.name = '$' == name[0] ? name + 1 : name;
  222. un.type = isSampler(desc.Type)
  223. ? UniformType::Enum(BGFX_UNIFORM_SAMPLERBIT | type)
  224. : type
  225. ;
  226. un.num = (uint8_t)ctType.Elements;
  227. un.regIndex = ctInfo.RegisterIndex;
  228. un.regCount = ctInfo.RegisterCount;
  229. _uniforms.push_back(un);
  230. }
  231. }
  232. return true;
  233. }
  234. bool getReflectionDataDx11(ID3DBlob* _code, bool _vshader, UniformArray& _uniforms, uint8_t& _numAttrs, uint16_t* _attrs, uint16_t& _size, UniformNameList& unusedUniforms)
  235. {
  236. ID3D11ShaderReflection* reflect = NULL;
  237. HRESULT hr = D3DReflect(_code->GetBufferPointer()
  238. , _code->GetBufferSize()
  239. , IID_ID3D11ShaderReflection
  240. , (void**)&reflect
  241. );
  242. if (FAILED(hr) )
  243. {
  244. fprintf(stderr, "Error: 0x%08x\n", (uint32_t)hr);
  245. return false;
  246. }
  247. D3D11_SHADER_DESC desc;
  248. hr = reflect->GetDesc(&desc);
  249. if (FAILED(hr) )
  250. {
  251. fprintf(stderr, BX_FILE_LINE_LITERAL "Error: 0x%08x\n", (uint32_t)hr);
  252. return false;
  253. }
  254. BX_TRACE("Creator: %s 0x%08x", desc.Creator, desc.Version);
  255. BX_TRACE("Num constant buffers: %d", desc.ConstantBuffers);
  256. BX_TRACE("Input:");
  257. if (_vshader) // Only care about input semantic on vertex shaders
  258. {
  259. for (uint32_t ii = 0; ii < desc.InputParameters; ++ii)
  260. {
  261. D3D11_SIGNATURE_PARAMETER_DESC spd;
  262. reflect->GetInputParameterDesc(ii, &spd);
  263. BX_TRACE("\t%2d: %s%d, vt %d, ct %d, mask %x, reg %d"
  264. , ii
  265. , spd.SemanticName
  266. , spd.SemanticIndex
  267. , spd.SystemValueType
  268. , spd.ComponentType
  269. , spd.Mask
  270. , spd.Register
  271. );
  272. const RemapInputSemantic& ris = findInputSemantic(spd.SemanticName, spd.SemanticIndex);
  273. if (ris.m_attr != bgfx::Attrib::Count)
  274. {
  275. _attrs[_numAttrs] = bgfx::attribToId(ris.m_attr);
  276. ++_numAttrs;
  277. }
  278. }
  279. }
  280. BX_TRACE("Output:");
  281. for (uint32_t ii = 0; ii < desc.OutputParameters; ++ii)
  282. {
  283. D3D11_SIGNATURE_PARAMETER_DESC spd;
  284. reflect->GetOutputParameterDesc(ii, &spd);
  285. BX_TRACE("\t%2d: %s%d, %d, %d", ii, spd.SemanticName, spd.SemanticIndex, spd.SystemValueType, spd.ComponentType);
  286. }
  287. for (uint32_t ii = 0, num = bx::uint32_min(1, desc.ConstantBuffers); ii < num; ++ii)
  288. {
  289. ID3D11ShaderReflectionConstantBuffer* cbuffer = reflect->GetConstantBufferByIndex(ii);
  290. D3D11_SHADER_BUFFER_DESC bufferDesc;
  291. hr = cbuffer->GetDesc(&bufferDesc);
  292. _size = (uint16_t)bufferDesc.Size;
  293. if (SUCCEEDED(hr) )
  294. {
  295. BX_TRACE("%s, %d, vars %d, size %d"
  296. , bufferDesc.Name
  297. , bufferDesc.Type
  298. , bufferDesc.Variables
  299. , bufferDesc.Size
  300. );
  301. for (uint32_t jj = 0; jj < bufferDesc.Variables; ++jj)
  302. {
  303. ID3D11ShaderReflectionVariable* var = cbuffer->GetVariableByIndex(jj);
  304. ID3D11ShaderReflectionType* type = var->GetType();
  305. D3D11_SHADER_VARIABLE_DESC varDesc;
  306. hr = var->GetDesc(&varDesc);
  307. if (SUCCEEDED(hr) )
  308. {
  309. D3D11_SHADER_TYPE_DESC constDesc;
  310. hr = type->GetDesc(&constDesc);
  311. if (SUCCEEDED(hr) )
  312. {
  313. UniformType::Enum uniformType = findUniformType(constDesc);
  314. if (UniformType::Count != uniformType
  315. && 0 != (varDesc.uFlags & D3D_SVF_USED) )
  316. {
  317. Uniform un;
  318. un.name = varDesc.Name;
  319. un.type = uniformType;
  320. un.num = constDesc.Elements;
  321. un.regIndex = varDesc.StartOffset;
  322. un.regCount = BX_ALIGN_16(varDesc.Size) / 16;
  323. _uniforms.push_back(un);
  324. BX_TRACE("\t%s, %d, size %d, flags 0x%08x, %d (used)"
  325. , varDesc.Name
  326. , varDesc.StartOffset
  327. , varDesc.Size
  328. , varDesc.uFlags
  329. , uniformType
  330. );
  331. }
  332. else
  333. {
  334. if (0 == (varDesc.uFlags & D3D_SVF_USED) )
  335. {
  336. unusedUniforms.push_back(varDesc.Name);
  337. }
  338. BX_TRACE("\t%s, unknown type", varDesc.Name);
  339. }
  340. }
  341. }
  342. }
  343. }
  344. }
  345. BX_TRACE("Bound:");
  346. for (uint32_t ii = 0; ii < desc.BoundResources; ++ii)
  347. {
  348. D3D11_SHADER_INPUT_BIND_DESC bindDesc;
  349. hr = reflect->GetResourceBindingDesc(ii, &bindDesc);
  350. if (SUCCEEDED(hr) )
  351. {
  352. if (D3D_SIT_SAMPLER == bindDesc.Type)
  353. {
  354. BX_TRACE("\t%s, %d, %d, %d"
  355. , bindDesc.Name
  356. , bindDesc.Type
  357. , bindDesc.BindPoint
  358. , bindDesc.BindCount
  359. );
  360. const char * end = strstr(bindDesc.Name, "Sampler");
  361. if (NULL != end)
  362. {
  363. Uniform un;
  364. un.name.assign(bindDesc.Name, (end - bindDesc.Name) );
  365. un.type = UniformType::Enum(BGFX_UNIFORM_SAMPLERBIT | UniformType::Int1);
  366. un.num = 1;
  367. un.regIndex = bindDesc.BindPoint;
  368. un.regCount = bindDesc.BindCount;
  369. _uniforms.push_back(un);
  370. }
  371. }
  372. }
  373. }
  374. if (NULL != reflect)
  375. {
  376. reflect->Release();
  377. }
  378. return true;
  379. }
  380. bool compileHLSLShader(bx::CommandLine& _cmdLine, uint32_t _d3d, const std::string& _code, bx::WriterI* _writer, bool _firstPass)
  381. {
  382. BX_TRACE("DX11");
  383. const char* profile = _cmdLine.findOption('p', "profile");
  384. if (NULL == profile)
  385. {
  386. fprintf(stderr, "Shader profile must be specified.\n");
  387. return false;
  388. }
  389. bool debug = _cmdLine.hasArg('\0', "debug");
  390. uint32_t flags = D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY;
  391. flags |= debug ? D3DCOMPILE_DEBUG : 0;
  392. flags |= _cmdLine.hasArg('\0', "avoid-flow-control") ? D3DCOMPILE_AVOID_FLOW_CONTROL : 0;
  393. flags |= _cmdLine.hasArg('\0', "no-preshader") ? D3DCOMPILE_NO_PRESHADER : 0;
  394. flags |= _cmdLine.hasArg('\0', "partial-precision") ? D3DCOMPILE_PARTIAL_PRECISION : 0;
  395. flags |= _cmdLine.hasArg('\0', "prefer-flow-control") ? D3DCOMPILE_PREFER_FLOW_CONTROL : 0;
  396. flags |= _cmdLine.hasArg('\0', "backwards-compatibility") ? D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY : 0;
  397. bool werror = _cmdLine.hasArg('\0', "Werror");
  398. if (werror)
  399. {
  400. flags |= D3DCOMPILE_WARNINGS_ARE_ERRORS;
  401. }
  402. uint32_t optimization = 3;
  403. if (_cmdLine.hasArg(optimization, 'O') )
  404. {
  405. optimization = bx::uint32_min(optimization, BX_COUNTOF(s_optimizationLevelDx11)-1);
  406. flags |= s_optimizationLevelDx11[optimization];
  407. }
  408. else
  409. {
  410. flags |= D3DCOMPILE_SKIP_OPTIMIZATION;
  411. }
  412. BX_TRACE("Profile: %s", profile);
  413. BX_TRACE("Flags: 0x%08x", flags);
  414. ID3DBlob* code;
  415. ID3DBlob* errorMsg;
  416. // Output preprocessed shader so that HLSL can be debugged via GPA
  417. // or PIX. Compiling through memory won't embed preprocessed shader
  418. // file path.
  419. std::string hlslfp;
  420. if (debug)
  421. {
  422. hlslfp = _cmdLine.findOption('o');
  423. hlslfp += ".hlsl";
  424. writeFile(hlslfp.c_str(), _code.c_str(), (int32_t)_code.size() );
  425. }
  426. HRESULT hr = D3DCompile(_code.c_str()
  427. , _code.size()
  428. , hlslfp.c_str()
  429. , NULL
  430. , NULL
  431. , "main"
  432. , profile
  433. , flags
  434. , 0
  435. , &code
  436. , &errorMsg
  437. );
  438. if (FAILED(hr)
  439. || (werror && NULL != errorMsg) )
  440. {
  441. const char* log = (char*)errorMsg->GetBufferPointer();
  442. int32_t line = 0;
  443. int32_t column = 0;
  444. int32_t start = 0;
  445. int32_t end = INT32_MAX;
  446. if (2 == sscanf(log, "(%u,%u):", &line, &column)
  447. && 0 != line)
  448. {
  449. start = bx::uint32_imax(1, line-10);
  450. end = start + 20;
  451. }
  452. printCode(_code.c_str(), line, start, end);
  453. fprintf(stderr, "Error: 0x%08x %s\n", (uint32_t)hr, log);
  454. errorMsg->Release();
  455. return false;
  456. }
  457. UniformArray uniforms;
  458. uint8_t numAttrs = 0;
  459. uint16_t attrs[bgfx::Attrib::Count];
  460. uint16_t size = 0;
  461. if (_d3d == 9)
  462. {
  463. if (!getReflectionDataDx9(code, uniforms) )
  464. {
  465. return false;
  466. }
  467. }
  468. else
  469. {
  470. UniformNameList unusedUniforms;
  471. if (!getReflectionDataDx11(code, profile[0] == 'v', uniforms, numAttrs, attrs, size, unusedUniforms) )
  472. {
  473. return false;
  474. }
  475. if (_firstPass
  476. && unusedUniforms.size() > 0)
  477. {
  478. const size_t strLength = strlen("uniform");
  479. // first time through, we just find unused uniforms and get rid of them
  480. std::string output;
  481. LineReader reader(_code.c_str() );
  482. while (!reader.isEof() )
  483. {
  484. std::string line = reader.getLine();
  485. for (UniformNameList::iterator it = unusedUniforms.begin(), itEnd = unusedUniforms.end(); it != itEnd; ++it)
  486. {
  487. size_t index = line.find("uniform ");
  488. if (index == std::string::npos)
  489. {
  490. continue;
  491. }
  492. // matching lines like: uniform u_name;
  493. // we want to replace "uniform" with "static" so that it's no longer
  494. // included in the uniform blob that the application must upload
  495. // we can't just remove them, because unused functions might still reference
  496. // them and cause a compile error when they're gone
  497. if (!!bx::findIdentifierMatch(line.c_str(), it->c_str() ) )
  498. {
  499. line = line.replace(index, strLength, "static");
  500. unusedUniforms.erase(it);
  501. break;
  502. }
  503. }
  504. output += line;
  505. }
  506. // recompile with the unused uniforms converted to statics
  507. return compileHLSLShader(_cmdLine, _d3d, output.c_str(), _writer, false);
  508. }
  509. }
  510. uint16_t count = (uint16_t)uniforms.size();
  511. bx::write(_writer, count);
  512. uint32_t fragmentBit = profile[0] == 'p' ? BGFX_UNIFORM_FRAGMENTBIT : 0;
  513. for (UniformArray::const_iterator it = uniforms.begin(); it != uniforms.end(); ++it)
  514. {
  515. const Uniform& un = *it;
  516. uint8_t nameSize = (uint8_t)un.name.size();
  517. bx::write(_writer, nameSize);
  518. bx::write(_writer, un.name.c_str(), nameSize);
  519. uint8_t type = un.type|fragmentBit;
  520. bx::write(_writer, type);
  521. bx::write(_writer, un.num);
  522. bx::write(_writer, un.regIndex);
  523. bx::write(_writer, un.regCount);
  524. BX_TRACE("%s, %s, %d, %d, %d"
  525. , un.name.c_str()
  526. , getUniformTypeName(un.type)
  527. , un.num
  528. , un.regIndex
  529. , un.regCount
  530. );
  531. }
  532. {
  533. ID3DBlob* stripped;
  534. hr = D3DStripShader(code->GetBufferPointer()
  535. , code->GetBufferSize()
  536. , D3DCOMPILER_STRIP_REFLECTION_DATA
  537. | D3DCOMPILER_STRIP_TEST_BLOBS
  538. , &stripped
  539. );
  540. if (SUCCEEDED(hr) )
  541. {
  542. code->Release();
  543. code = stripped;
  544. }
  545. }
  546. uint16_t shaderSize = (uint16_t)code->GetBufferSize();
  547. bx::write(_writer, shaderSize);
  548. bx::write(_writer, code->GetBufferPointer(), shaderSize);
  549. uint8_t nul = 0;
  550. bx::write(_writer, nul);
  551. if (_d3d > 9)
  552. {
  553. bx::write(_writer, numAttrs);
  554. bx::write(_writer, attrs, numAttrs*sizeof(uint16_t) );
  555. bx::write(_writer, size);
  556. }
  557. if (_cmdLine.hasArg('\0', "disasm") )
  558. {
  559. ID3DBlob* disasm;
  560. D3DDisassemble(code->GetBufferPointer()
  561. , code->GetBufferSize()
  562. , 0
  563. , NULL
  564. , &disasm
  565. );
  566. if (NULL != disasm)
  567. {
  568. std::string disasmfp = _cmdLine.findOption('o');
  569. disasmfp += ".disasm";
  570. writeFile(disasmfp.c_str(), disasm->GetBufferPointer(), (uint32_t)disasm->GetBufferSize() );
  571. disasm->Release();
  572. }
  573. }
  574. if (NULL != errorMsg)
  575. {
  576. errorMsg->Release();
  577. }
  578. code->Release();
  579. return true;
  580. }
  581. #else
  582. bool compileHLSLShader(bx::CommandLine& _cmdLine, uint32_t _d3d, const std::string& _code, bx::WriterI* _writer, bool _firstPass)
  583. {
  584. BX_UNUSED(_cmdLine, _d3d, _code, _writer, _firstPass);
  585. fprintf(stderr, "HLSL compiler is not supported on this platform.\n");
  586. return false;
  587. }
  588. #endif // SHADERC_CONFIG_HLSL