ShaderProgramCompiler.cpp 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. // Copyright (C) 2009-2023, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/ShaderCompiler/ShaderProgramCompiler.h>
  6. #include <AnKi/ShaderCompiler/ShaderProgramParser.h>
  7. #include <AnKi/ShaderCompiler/Dxc.h>
  8. #include <AnKi/Util/Serializer.h>
  9. #include <AnKi/Util/HashMap.h>
  10. #include <SpirvCross/spirv_cross.hpp>
  11. #define ANKI_DIXL_REFLECTION (ANKI_OS_WINDOWS)
  12. #if ANKI_DIXL_REFLECTION
  13. # include <windows.h>
  14. # include <ThirdParty/Dxc/dxcapi.h>
  15. # include <ThirdParty/Dxc/d3d12shader.h>
  16. # include <wrl.h>
  17. # include <AnKi/Util/CleanupWindows.h>
  18. static HMODULE g_dxcLib = 0;
  19. static DxcCreateInstanceProc g_DxcCreateInstance = nullptr;
  20. #endif
  21. namespace anki {
  22. void freeShaderProgramBinary(ShaderProgramBinary*& binary)
  23. {
  24. if(binary == nullptr)
  25. {
  26. return;
  27. }
  28. BaseMemoryPool& mempool = ShaderCompilerMemoryPool::getSingleton();
  29. for(ShaderProgramBinaryCodeBlock& code : binary->m_codeBlocks)
  30. {
  31. mempool.free(code.m_binary.getBegin());
  32. }
  33. mempool.free(binary->m_codeBlocks.getBegin());
  34. for(ShaderProgramBinaryMutator& mutator : binary->m_mutators)
  35. {
  36. mempool.free(mutator.m_values.getBegin());
  37. }
  38. mempool.free(binary->m_mutators.getBegin());
  39. for(ShaderProgramBinaryMutation& m : binary->m_mutations)
  40. {
  41. mempool.free(m.m_values.getBegin());
  42. }
  43. mempool.free(binary->m_mutations.getBegin());
  44. for(ShaderProgramBinaryVariant& variant : binary->m_variants)
  45. {
  46. mempool.free(variant.m_techniqueCodeBlocks.getBegin());
  47. }
  48. mempool.free(binary->m_variants.getBegin());
  49. mempool.free(binary->m_techniques.getBegin());
  50. for(ShaderProgramBinaryStruct& s : binary->m_structs)
  51. {
  52. mempool.free(s.m_members.getBegin());
  53. }
  54. mempool.free(binary->m_structs.getBegin());
  55. mempool.free(binary);
  56. binary = nullptr;
  57. }
  58. /// Spin the dials. Used to compute all mutator combinations.
  59. static Bool spinDials(ShaderCompilerDynamicArray<U32>& dials, ConstWeakArray<ShaderProgramParserMutator> mutators)
  60. {
  61. ANKI_ASSERT(dials.getSize() == mutators.getSize() && dials.getSize() > 0);
  62. Bool done = true;
  63. U32 crntDial = dials.getSize() - 1;
  64. while(true)
  65. {
  66. // Turn dial
  67. ++dials[crntDial];
  68. if(dials[crntDial] >= mutators[crntDial].m_values.getSize())
  69. {
  70. if(crntDial == 0)
  71. {
  72. // Reached the 1st dial, stop spinning
  73. done = true;
  74. break;
  75. }
  76. else
  77. {
  78. dials[crntDial] = 0;
  79. --crntDial;
  80. }
  81. }
  82. else
  83. {
  84. done = false;
  85. break;
  86. }
  87. }
  88. return done;
  89. }
  90. template<typename TFunc>
  91. static void visitSpirv(ConstWeakArray<U32> spv, TFunc func)
  92. {
  93. ANKI_ASSERT(spv.getSize() > 5);
  94. const U32* it = &spv[5];
  95. do
  96. {
  97. const U32 instructionCount = *it >> 16u;
  98. const U32 opcode = *it & 0xFFFFu;
  99. func(opcode);
  100. it += instructionCount;
  101. } while(it < spv.getEnd());
  102. ANKI_ASSERT(it == spv.getEnd());
  103. }
  104. static Error doReflectionSpirv(ConstWeakArray<U8> spirv, ShaderType type, ShaderReflection& refl, ShaderCompilerString& errorStr)
  105. {
  106. spirv_cross::Compiler spvc(reinterpret_cast<const U32*>(&spirv[0]), spirv.getSize() / sizeof(U32));
  107. spirv_cross::ShaderResources rsrc = spvc.get_shader_resources();
  108. spirv_cross::ShaderResources rsrcActive = spvc.get_shader_resources(spvc.get_active_interface_variables());
  109. auto func = [&](const spirv_cross::SmallVector<spirv_cross::Resource>& resources, const DescriptorType origType,
  110. const DescriptorFlag origFlags) -> Error {
  111. for(const spirv_cross::Resource& r : resources)
  112. {
  113. const U32 id = r.id;
  114. const U32 set = spvc.get_decoration(id, spv::Decoration::DecorationDescriptorSet);
  115. const U32 binding = spvc.get_decoration(id, spv::Decoration::DecorationBinding);
  116. if(set >= kMaxDescriptorSets || binding >= kMaxBindingsPerDescriptorSet)
  117. {
  118. errorStr.sprintf("Exceeded set or binding for: %s", r.name.c_str());
  119. return Error::kUserData;
  120. }
  121. const spirv_cross::SPIRType& typeInfo = spvc.get_type(r.type_id);
  122. U32 arraySize = 1;
  123. if(typeInfo.array.size() != 0)
  124. {
  125. if(typeInfo.array.size() != 1 || (arraySize = typeInfo.array[0]) == 0)
  126. {
  127. errorStr.sprintf("Only 1D arrays are supported: %s", r.name.c_str());
  128. return Error::kUserData;
  129. }
  130. }
  131. refl.m_descriptorSetMask.set(set);
  132. // Images are special, they might be texel buffers
  133. DescriptorType type = origType;
  134. DescriptorFlag flags = origFlags;
  135. if(type == DescriptorType::kTexture)
  136. {
  137. if(typeInfo.image.dim == spv::DimBuffer)
  138. {
  139. type = DescriptorType::kTexelBuffer;
  140. if(typeInfo.image.sampled == 1)
  141. {
  142. flags = DescriptorFlag::kRead;
  143. }
  144. else
  145. {
  146. ANKI_ASSERT(typeInfo.image.sampled == 2);
  147. flags = DescriptorFlag::kReadWrite;
  148. }
  149. }
  150. }
  151. // Check that there are no other descriptors with the same binding
  152. if(refl.m_descriptorTypes[set][binding] == DescriptorType::kCount)
  153. {
  154. // New binding, init it
  155. refl.m_descriptorTypes[set][binding] = type;
  156. refl.m_descriptorArraySizes[set][binding] = U16(arraySize);
  157. refl.m_descriptorFlags[set][binding] = flags;
  158. }
  159. else
  160. {
  161. // Same binding, make sure the type is compatible
  162. if(refl.m_descriptorTypes[set][binding] != type || refl.m_descriptorArraySizes[set][binding] != arraySize
  163. || refl.m_descriptorFlags[set][binding] != flags)
  164. {
  165. errorStr.sprintf("Descriptor with same binding but different type or array size: %s", r.name.c_str());
  166. return Error::kUserData;
  167. }
  168. }
  169. }
  170. return Error::kNone;
  171. };
  172. Error err = Error::kNone;
  173. err = func(rsrc.uniform_buffers, DescriptorType::kUniformBuffer, DescriptorFlag::kRead);
  174. if(!err)
  175. {
  176. err = func(rsrc.separate_images, DescriptorType::kTexture, DescriptorFlag::kRead); // This also handles texel buffers
  177. }
  178. if(!err)
  179. {
  180. err = func(rsrc.separate_samplers, DescriptorType::kSampler, DescriptorFlag::kRead);
  181. }
  182. if(!err)
  183. {
  184. err = func(rsrc.storage_buffers, DescriptorType::kStorageBuffer, DescriptorFlag::kReadWrite);
  185. }
  186. if(!err)
  187. {
  188. err = func(rsrc.storage_images, DescriptorType::kTexture, DescriptorFlag::kReadWrite);
  189. }
  190. if(!err)
  191. {
  192. err = func(rsrc.acceleration_structures, DescriptorType::kAccelerationStructure, DescriptorFlag::kRead);
  193. }
  194. // Color attachments
  195. if(type == ShaderType::kFragment)
  196. {
  197. for(const spirv_cross::Resource& r : rsrc.stage_outputs)
  198. {
  199. const U32 id = r.id;
  200. const U32 location = spvc.get_decoration(id, spv::Decoration::DecorationLocation);
  201. refl.m_colorAttachmentWritemask.set(location);
  202. }
  203. }
  204. // Push consts
  205. if(rsrc.push_constant_buffers.size() == 1)
  206. {
  207. const U32 blockSize = U32(spvc.get_declared_struct_size(spvc.get_type(rsrc.push_constant_buffers[0].base_type_id)));
  208. if(blockSize == 0 || (blockSize % 16) != 0 || blockSize > kMaxU8)
  209. {
  210. errorStr.sprintf("Incorrect push constants size");
  211. return Error::kUserData;
  212. }
  213. refl.m_pushConstantsSize = U8(blockSize);
  214. }
  215. // Attribs
  216. if(type == ShaderType::kVertex)
  217. {
  218. for(const spirv_cross::Resource& r : rsrcActive.stage_inputs)
  219. {
  220. VertexAttributeSemantic a = VertexAttributeSemantic::kCount;
  221. #define ANKI_ATTRIB_NAME(x) "in.var." #x
  222. if(r.name == ANKI_ATTRIB_NAME(POSITION))
  223. {
  224. a = VertexAttributeSemantic::kPosition;
  225. }
  226. else if(r.name == ANKI_ATTRIB_NAME(NORMAL))
  227. {
  228. a = VertexAttributeSemantic::kNormal;
  229. }
  230. else if(r.name == ANKI_ATTRIB_NAME(TEXCOORD0) || r.name == ANKI_ATTRIB_NAME(TEXCOORD))
  231. {
  232. a = VertexAttributeSemantic::kTexCoord;
  233. }
  234. else if(r.name == ANKI_ATTRIB_NAME(COLOR))
  235. {
  236. a = VertexAttributeSemantic::kColor;
  237. }
  238. else if(r.name == ANKI_ATTRIB_NAME(MISC0) || r.name == ANKI_ATTRIB_NAME(MISC))
  239. {
  240. a = VertexAttributeSemantic::kMisc0;
  241. }
  242. else if(r.name == ANKI_ATTRIB_NAME(MISC1))
  243. {
  244. a = VertexAttributeSemantic::kMisc1;
  245. }
  246. else if(r.name == ANKI_ATTRIB_NAME(MISC2))
  247. {
  248. a = VertexAttributeSemantic::kMisc2;
  249. }
  250. else if(r.name == ANKI_ATTRIB_NAME(MISC3))
  251. {
  252. a = VertexAttributeSemantic::kMisc3;
  253. }
  254. else
  255. {
  256. errorStr.sprintf("Unexpected attribute name: %s", r.name.c_str());
  257. return Error::kUserData;
  258. }
  259. #undef ANKI_ATTRIB_NAME
  260. refl.m_vertexAttributeMask.set(a);
  261. const U32 id = r.id;
  262. const U32 location = spvc.get_decoration(id, spv::Decoration::DecorationLocation);
  263. if(location > kMaxU8)
  264. {
  265. errorStr.sprintf("Too high location value for attribute: %s", r.name.c_str());
  266. return Error::kUserData;
  267. }
  268. refl.m_vertexAttributeLocations[a] = U8(location);
  269. }
  270. }
  271. // Discards?
  272. if(type == ShaderType::kFragment)
  273. {
  274. visitSpirv(ConstWeakArray<U32>(reinterpret_cast<const U32*>(&spirv[0]), spirv.getSize() / sizeof(U32)), [&](U32 cmd) {
  275. if(cmd == spv::OpKill)
  276. {
  277. refl.m_discards = true;
  278. }
  279. });
  280. }
  281. return Error::kNone;
  282. }
  283. #if ANKI_DIXL_REFLECTION
  284. # define ANKI_REFL_CHECK(x) \
  285. do \
  286. { \
  287. HRESULT rez; \
  288. if((rez = (x)) < 0) [[unlikely]] \
  289. { \
  290. errorStr.sprintf("DXC function failed (HRESULT: %d): %s", rez, #x); \
  291. return Error::kFunctionFailed; \
  292. } \
  293. } while(0)
  294. static Error doReflectionDxil(ConstWeakArray<U8> dxil, ShaderType type, ShaderReflection& refl, ShaderCompilerString& errorStr)
  295. {
  296. using Microsoft::WRL::ComPtr;
  297. const Bool isRayTracing = type >= ShaderType::kFirstRayTracing && type <= ShaderType::kLastRayTracing;
  298. if(isRayTracing)
  299. {
  300. // TODO: Skip for now. RT shaders require explicity register()
  301. return Error::kNone;
  302. }
  303. ComPtr<IDxcUtils> utils;
  304. ANKI_REFL_CHECK(g_DxcCreateInstance(CLSID_DxcUtils, IID_PPV_ARGS(&utils)));
  305. ComPtr<ID3D12ShaderReflection> dxRefl;
  306. ComPtr<ID3D12LibraryReflection> libRefl;
  307. ID3D12FunctionReflection* funcRefl = nullptr;
  308. D3D12_SHADER_DESC shaderDesc = {};
  309. U32 bindingCount = 0;
  310. if(!isRayTracing)
  311. {
  312. const DxcBuffer buff = {dxil.getBegin(), dxil.getSizeInBytes(), 0};
  313. ANKI_REFL_CHECK(utils->CreateReflection(&buff, IID_PPV_ARGS(&dxRefl)));
  314. ANKI_REFL_CHECK(dxRefl->GetDesc(&shaderDesc));
  315. bindingCount = shaderDesc.BoundResources;
  316. }
  317. else
  318. {
  319. const DxcBuffer buff = {dxil.getBegin(), dxil.getSizeInBytes(), 0};
  320. ANKI_REFL_CHECK(utils->CreateReflection(&buff, IID_PPV_ARGS(&libRefl)));
  321. D3D12_LIBRARY_DESC libDesc = {};
  322. libRefl->GetDesc(&libDesc);
  323. if(libDesc.FunctionCount != 1)
  324. {
  325. errorStr.sprintf("Expecting 1 in D3D12_LIBRARY_DESC::FunctionCount");
  326. return Error::kUserData;
  327. }
  328. funcRefl = libRefl->GetFunctionByIndex(0);
  329. D3D12_FUNCTION_DESC funcDesc;
  330. ANKI_REFL_CHECK(funcRefl->GetDesc(&funcDesc));
  331. bindingCount = funcDesc.BoundResources;
  332. }
  333. for(U32 i = 0; i < bindingCount; ++i)
  334. {
  335. D3D12_SHADER_INPUT_BIND_DESC bind;
  336. if(dxRefl.Get() != nullptr)
  337. {
  338. ANKI_REFL_CHECK(dxRefl->GetResourceBindingDesc(i, &bind));
  339. }
  340. else
  341. {
  342. ANKI_REFL_CHECK(funcRefl->GetResourceBindingDesc(i, &bind));
  343. }
  344. ShaderReflectionBinding akBinding;
  345. akBinding.m_type = DescriptorType::kCount;
  346. akBinding.m_flags = DescriptorFlag::kNone;
  347. akBinding.m_arraySize = U16(bind.BindCount);
  348. if(bind.Type == D3D_SIT_CBUFFER)
  349. {
  350. // ConstantBuffer
  351. if(bind.BindPoint == ANKI_PUSH_CONSTANTS_D3D_BINDING && bind.Space == ANKI_PUSH_CONSTANTS_D3D_SPACE)
  352. {
  353. // It's push/root constants
  354. ANKI_ASSERT(!"TODO");
  355. continue;
  356. }
  357. akBinding.m_type = DescriptorType::kUniformBuffer;
  358. akBinding.m_flags = DescriptorFlag::kRead;
  359. akBinding.m_semantic = BindingSemantic('b', bind.BindPoint);
  360. }
  361. else if(bind.Type == D3D_SIT_TEXTURE && bind.Dimension != D3D_SRV_DIMENSION_BUFFER)
  362. {
  363. // Texture2D etc
  364. akBinding.m_type = DescriptorType::kTexture;
  365. akBinding.m_flags = DescriptorFlag::kRead;
  366. akBinding.m_semantic = BindingSemantic('t', bind.BindPoint);
  367. }
  368. else if(bind.Type == D3D_SIT_TEXTURE && bind.Dimension == D3D_SRV_DIMENSION_BUFFER)
  369. {
  370. // Buffer
  371. akBinding.m_type = DescriptorType::kTexelBuffer;
  372. akBinding.m_flags = DescriptorFlag::kRead;
  373. akBinding.m_semantic = BindingSemantic('t', bind.BindPoint);
  374. }
  375. else if(bind.Type == D3D_SIT_SAMPLER)
  376. {
  377. // SamplerState
  378. akBinding.m_type = DescriptorType::kSampler;
  379. akBinding.m_flags = DescriptorFlag::kRead;
  380. akBinding.m_semantic = BindingSemantic('s', bind.BindPoint);
  381. }
  382. else if(bind.Type == D3D_SIT_UAV_RWTYPED && bind.Dimension == D3D_SRV_DIMENSION_BUFFER)
  383. {
  384. // RWBuffer
  385. akBinding.m_type = DescriptorType::kTexelBuffer;
  386. akBinding.m_flags = DescriptorFlag::kReadWrite;
  387. akBinding.m_semantic = BindingSemantic('u', bind.BindPoint);
  388. }
  389. else if(bind.Type == D3D_SIT_UAV_RWTYPED && bind.Dimension != D3D_SRV_DIMENSION_BUFFER)
  390. {
  391. // RWTexture2D etc
  392. akBinding.m_type = DescriptorType::kTexture;
  393. akBinding.m_flags = DescriptorFlag::kReadWrite;
  394. akBinding.m_semantic = BindingSemantic('u', bind.BindPoint);
  395. }
  396. else if(bind.Type == D3D_SIT_BYTEADDRESS)
  397. {
  398. // ByteAddressBuffer
  399. akBinding.m_type = DescriptorType::kStorageBuffer;
  400. akBinding.m_flags = DescriptorFlag::kRead | DescriptorFlag::kByteAddressBuffer;
  401. akBinding.m_semantic = BindingSemantic('t', bind.BindPoint);
  402. }
  403. else if(bind.Type == D3D_SIT_UAV_RWBYTEADDRESS)
  404. {
  405. // RWByteAddressBuffer
  406. akBinding.m_type = DescriptorType::kStorageBuffer;
  407. akBinding.m_flags = DescriptorFlag::kReadWrite | DescriptorFlag::kByteAddressBuffer;
  408. akBinding.m_semantic = BindingSemantic('u', bind.BindPoint);
  409. }
  410. else if(bind.Type == D3D_SIT_RTACCELERATIONSTRUCTURE)
  411. {
  412. // RaytracingAccelerationStructure
  413. akBinding.m_type = DescriptorType::kAccelerationStructure;
  414. akBinding.m_flags = DescriptorFlag::kRead;
  415. akBinding.m_semantic = BindingSemantic('t', bind.BindPoint);
  416. }
  417. else if(bind.Type == D3D_SIT_STRUCTURED)
  418. {
  419. // StructuredBuffer
  420. akBinding.m_type = DescriptorType::kStorageBuffer;
  421. akBinding.m_flags = DescriptorFlag::kRead;
  422. akBinding.m_semantic = BindingSemantic('t', bind.BindPoint);
  423. }
  424. else if(bind.Type == D3D_SIT_UAV_RWSTRUCTURED)
  425. {
  426. // RWStructuredBuffer
  427. akBinding.m_type = DescriptorType::kStorageBuffer;
  428. akBinding.m_flags = DescriptorFlag::kReadWrite;
  429. akBinding.m_semantic = BindingSemantic('u', bind.BindPoint);
  430. }
  431. else
  432. {
  433. errorStr.sprintf("Unrecognized type for binding: %s", bind.Name);
  434. return Error::kUserData;
  435. }
  436. refl.m_bindings[bind.Space][refl.m_bindingCounts[bind.Space++]] = akBinding;
  437. }
  438. for(U32 i = 0; i < kMaxDescriptorSets; ++i)
  439. {
  440. std::sort(refl.m_bindings[i].getBegin(), refl.m_bindings[i].getBegin() + refl.m_bindingCounts[i]);
  441. }
  442. if(type == ShaderType::kVertex)
  443. {
  444. for(U32 i = 0; i < shaderDesc.InputParameters; ++i)
  445. {
  446. D3D12_SIGNATURE_PARAMETER_DESC in;
  447. ANKI_REFL_CHECK(dxRefl->GetInputParameterDesc(i, &in));
  448. VertexAttributeSemantic a = VertexAttributeSemantic::kCount;
  449. # define ANKI_ATTRIB_NAME(x, idx) CString(in.SemanticName) == # x&& in.SemanticIndex == idx
  450. if(ANKI_ATTRIB_NAME(POSITION, 0))
  451. {
  452. a = VertexAttributeSemantic::kPosition;
  453. }
  454. else if(ANKI_ATTRIB_NAME(NORMAL, 0))
  455. {
  456. a = VertexAttributeSemantic::kNormal;
  457. }
  458. else if(ANKI_ATTRIB_NAME(TEXCOORD, 0))
  459. {
  460. a = VertexAttributeSemantic::kTexCoord;
  461. }
  462. else if(ANKI_ATTRIB_NAME(COLOR, 0))
  463. {
  464. a = VertexAttributeSemantic::kColor;
  465. }
  466. else if(ANKI_ATTRIB_NAME(MISC, 0))
  467. {
  468. a = VertexAttributeSemantic::kMisc0;
  469. }
  470. else if(ANKI_ATTRIB_NAME(MISC, 1))
  471. {
  472. a = VertexAttributeSemantic::kMisc1;
  473. }
  474. else if(ANKI_ATTRIB_NAME(MISC, 2))
  475. {
  476. a = VertexAttributeSemantic::kMisc2;
  477. }
  478. else if(ANKI_ATTRIB_NAME(MISC, 3))
  479. {
  480. a = VertexAttributeSemantic::kMisc3;
  481. }
  482. else if(ANKI_ATTRIB_NAME(SV_VERTEXID, 0) || ANKI_ATTRIB_NAME(SV_INSTANCEID, 0))
  483. {
  484. // Ignore
  485. continue;
  486. }
  487. else
  488. {
  489. errorStr.sprintf("Unexpected attribute name: %s", in.SemanticName);
  490. return Error::kUserData;
  491. }
  492. # undef ANKI_ATTRIB_NAME
  493. refl.m_vertexAttributeMask.set(a);
  494. refl.m_vertexAttributeLocations[a] = U8(i);
  495. }
  496. }
  497. if(type == ShaderType::kFragment)
  498. {
  499. for(U32 i = 0; i < shaderDesc.OutputParameters; ++i)
  500. {
  501. D3D12_SIGNATURE_PARAMETER_DESC desc;
  502. ANKI_REFL_CHECK(dxRefl->GetOutputParameterDesc(i, &desc));
  503. if(CString(desc.SemanticName) == "SV_TARGET")
  504. {
  505. refl.m_colorAttachmentWritemask.set(desc.SemanticIndex);
  506. }
  507. }
  508. }
  509. return Error::kNone;
  510. }
  511. #endif // #if ANKI_DIXL_REFLECTION
  512. static void compileVariantAsync(const ShaderProgramParser& parser, Bool spirv, ShaderProgramBinaryMutation& mutation,
  513. ShaderCompilerDynamicArray<ShaderProgramBinaryVariant>& variants,
  514. ShaderCompilerDynamicArray<ShaderProgramBinaryCodeBlock>& codeBlocks,
  515. ShaderCompilerDynamicArray<U64>& sourceCodeHashes, ShaderProgramAsyncTaskInterface& taskManager, Mutex& mtx,
  516. Atomic<I32>& error)
  517. {
  518. class Ctx
  519. {
  520. public:
  521. const ShaderProgramParser* m_parser;
  522. ShaderProgramBinaryMutation* m_mutation;
  523. ShaderCompilerDynamicArray<ShaderProgramBinaryVariant>* m_variants;
  524. ShaderCompilerDynamicArray<ShaderProgramBinaryCodeBlock>* m_codeBlocks;
  525. ShaderCompilerDynamicArray<U64>* m_sourceCodeHashes;
  526. Mutex* m_mtx;
  527. Atomic<I32>* m_err;
  528. Bool m_spirv;
  529. };
  530. Ctx* ctx = newInstance<Ctx>(ShaderCompilerMemoryPool::getSingleton());
  531. ctx->m_parser = &parser;
  532. ctx->m_mutation = &mutation;
  533. ctx->m_variants = &variants;
  534. ctx->m_codeBlocks = &codeBlocks;
  535. ctx->m_sourceCodeHashes = &sourceCodeHashes;
  536. ctx->m_mtx = &mtx;
  537. ctx->m_err = &error;
  538. ctx->m_spirv = spirv;
  539. auto callback = [](void* userData) {
  540. Ctx& ctx = *static_cast<Ctx*>(userData);
  541. class Cleanup
  542. {
  543. public:
  544. Ctx* m_ctx;
  545. ~Cleanup()
  546. {
  547. deleteInstance(ShaderCompilerMemoryPool::getSingleton(), m_ctx);
  548. }
  549. } cleanup{&ctx};
  550. if(ctx.m_err->load() != 0)
  551. {
  552. // Don't bother
  553. return;
  554. }
  555. const U32 techniqueCount = ctx.m_parser->getTechniques().getSize();
  556. // Compile the sources
  557. ShaderCompilerDynamicArray<ShaderProgramBinaryTechniqueCodeBlocks> codeBlockIndices;
  558. codeBlockIndices.resize(techniqueCount);
  559. for(auto& it : codeBlockIndices)
  560. {
  561. it.m_codeBlockIndices.fill(kMaxU32);
  562. }
  563. ShaderCompilerString compilerErrorLog;
  564. Error err = Error::kNone;
  565. U newCodeBlockCount = 0;
  566. for(U32 t = 0; t < techniqueCount && !err; ++t)
  567. {
  568. const ShaderProgramParserTechnique& technique = ctx.m_parser->getTechniques()[t];
  569. for(ShaderType shaderType : EnumBitsIterable<ShaderType, ShaderTypeBit>(technique.m_shaderTypes))
  570. {
  571. ShaderCompilerString source;
  572. ctx.m_parser->generateVariant(ctx.m_mutation->m_values, technique, shaderType, source);
  573. // Check if the source code was found before
  574. const U64 sourceCodeHash = source.computeHash();
  575. if(technique.m_activeMutators[shaderType] != kMaxU64)
  576. {
  577. LockGuard lock(*ctx.m_mtx);
  578. ANKI_ASSERT(ctx.m_sourceCodeHashes->getSize() == ctx.m_codeBlocks->getSize());
  579. Bool found = false;
  580. for(U32 i = 0; i < ctx.m_sourceCodeHashes->getSize(); ++i)
  581. {
  582. if((*ctx.m_sourceCodeHashes)[i] == sourceCodeHash)
  583. {
  584. codeBlockIndices[t].m_codeBlockIndices[shaderType] = i;
  585. found = true;
  586. break;
  587. }
  588. }
  589. if(found)
  590. {
  591. continue;
  592. }
  593. }
  594. ShaderCompilerDynamicArray<U8> il;
  595. if(ctx.m_spirv)
  596. {
  597. err = compileHlslToSpirv(source, shaderType, ctx.m_parser->compileWith16bitTypes(), il, compilerErrorLog);
  598. }
  599. else
  600. {
  601. err = compileHlslToDxil(source, shaderType, ctx.m_parser->compileWith16bitTypes(), il, compilerErrorLog);
  602. }
  603. if(err)
  604. {
  605. break;
  606. }
  607. const U64 newHash = computeHash(il.getBegin(), il.getSizeInBytes());
  608. ShaderReflection refl;
  609. if(ctx.m_spirv)
  610. {
  611. err = doReflectionSpirv(il, shaderType, refl, compilerErrorLog);
  612. }
  613. else
  614. {
  615. #if ANKI_DIXL_REFLECTION
  616. err = doReflectionDxil(il, shaderType, refl, compilerErrorLog);
  617. #else
  618. ANKI_SHADER_COMPILER_LOGE("Can't generate shader compilation on non-windows platforms");
  619. err = Error::kFunctionFailed;
  620. #endif
  621. }
  622. if(err)
  623. {
  624. break;
  625. }
  626. // Add the binary if not already there
  627. {
  628. LockGuard lock(*ctx.m_mtx);
  629. Bool found = false;
  630. for(U32 j = 0; j < ctx.m_codeBlocks->getSize(); ++j)
  631. {
  632. if((*ctx.m_codeBlocks)[j].m_hash == newHash)
  633. {
  634. codeBlockIndices[t].m_codeBlockIndices[shaderType] = j;
  635. found = true;
  636. break;
  637. }
  638. }
  639. if(!found)
  640. {
  641. codeBlockIndices[t].m_codeBlockIndices[shaderType] = ctx.m_codeBlocks->getSize();
  642. auto& codeBlock = *ctx.m_codeBlocks->emplaceBack();
  643. il.moveAndReset(codeBlock.m_binary);
  644. codeBlock.m_hash = newHash;
  645. codeBlock.m_reflection = refl;
  646. ctx.m_sourceCodeHashes->emplaceBack(sourceCodeHash);
  647. ANKI_ASSERT(ctx.m_sourceCodeHashes->getSize() == ctx.m_codeBlocks->getSize());
  648. ++newCodeBlockCount;
  649. }
  650. }
  651. }
  652. }
  653. if(err)
  654. {
  655. I32 expectedErr = 0;
  656. const Bool isFirstError = ctx.m_err->compareExchange(expectedErr, err._getCode());
  657. if(isFirstError)
  658. {
  659. ANKI_SHADER_COMPILER_LOGE("Shader compilation failed:\n%s", compilerErrorLog.cstr());
  660. return;
  661. }
  662. return;
  663. }
  664. // Do variant stuff
  665. {
  666. LockGuard lock(*ctx.m_mtx);
  667. Bool createVariant = true;
  668. if(newCodeBlockCount == 0)
  669. {
  670. // No new code blocks generated, search all variants to see if there is a duplicate
  671. for(U32 i = 0; i < ctx.m_variants->getSize(); ++i)
  672. {
  673. Bool same = true;
  674. for(U32 t = 0; t < techniqueCount; ++t)
  675. {
  676. const ShaderProgramBinaryTechniqueCodeBlocks& a = (*ctx.m_variants)[i].m_techniqueCodeBlocks[t];
  677. const ShaderProgramBinaryTechniqueCodeBlocks& b = codeBlockIndices[t];
  678. if(memcmp(&a, &b, sizeof(a)) != 0)
  679. {
  680. // Not the same
  681. same = false;
  682. break;
  683. }
  684. }
  685. if(same)
  686. {
  687. createVariant = false;
  688. ctx.m_mutation->m_variantIndex = i;
  689. break;
  690. }
  691. }
  692. }
  693. // Create a new variant
  694. if(createVariant)
  695. {
  696. ctx.m_mutation->m_variantIndex = ctx.m_variants->getSize();
  697. ShaderProgramBinaryVariant* variant = ctx.m_variants->emplaceBack();
  698. codeBlockIndices.moveAndReset(variant->m_techniqueCodeBlocks);
  699. }
  700. }
  701. };
  702. taskManager.enqueueTask(callback, ctx);
  703. }
  704. static Error compileShaderProgramInternal(CString fname, Bool spirv, ShaderProgramFilesystemInterface& fsystem,
  705. ShaderProgramPostParseInterface* postParseCallback, ShaderProgramAsyncTaskInterface* taskManager_,
  706. ConstWeakArray<ShaderCompilerDefine> defines_, ShaderProgramBinary*& binary)
  707. {
  708. #if ANKI_DIXL_REFLECTION
  709. if(!spirv)
  710. {
  711. // Init DXC
  712. g_dxcLib = LoadLibraryA(ANKI_SOURCE_DIRECTORY "/ThirdParty/Bin/Windows64/dxcompiler.dll");
  713. if(g_dxcLib == 0)
  714. {
  715. ANKI_SHADER_COMPILER_LOGE("dxcompiler.dll missing or wrong architecture");
  716. return Error::kFunctionFailed;
  717. }
  718. g_DxcCreateInstance = reinterpret_cast<DxcCreateInstanceProc>(GetProcAddress(g_dxcLib, "DxcCreateInstance"));
  719. if(g_DxcCreateInstance == nullptr)
  720. {
  721. ANKI_SHADER_COMPILER_LOGE("DxcCreateInstance was not found in the dxcompiler.dll");
  722. return Error::kFunctionFailed;
  723. }
  724. }
  725. #endif
  726. ShaderCompilerMemoryPool& memPool = ShaderCompilerMemoryPool::getSingleton();
  727. ShaderCompilerDynamicArray<ShaderCompilerDefine> defines;
  728. for(const ShaderCompilerDefine& d : defines_)
  729. {
  730. defines.emplaceBack(d);
  731. }
  732. defines.emplaceBack(ShaderCompilerDefine{"ANKI_GR_BACKEND_VULKAN", spirv});
  733. defines.emplaceBack(ShaderCompilerDefine{"ANKI_GR_BACKEND_DIRECT3D", !spirv});
  734. // Initialize the binary
  735. binary = newInstance<ShaderProgramBinary>(memPool);
  736. memcpy(&binary->m_magic[0], kShaderBinaryMagic, 8);
  737. // Parse source
  738. ShaderProgramParser parser(fname, &fsystem, defines);
  739. ANKI_CHECK(parser.parse());
  740. if(postParseCallback && postParseCallback->skipCompilation(parser.getHash()))
  741. {
  742. return Error::kNone;
  743. }
  744. // Get mutators
  745. U32 mutationCount = 0;
  746. if(parser.getMutators().getSize() > 0)
  747. {
  748. newArray(memPool, parser.getMutators().getSize(), binary->m_mutators);
  749. for(U32 i = 0; i < binary->m_mutators.getSize(); ++i)
  750. {
  751. ShaderProgramBinaryMutator& out = binary->m_mutators[i];
  752. const ShaderProgramParserMutator& in = parser.getMutators()[i];
  753. zeroMemory(out);
  754. newArray(memPool, in.m_values.getSize(), out.m_values);
  755. memcpy(out.m_values.getBegin(), in.m_values.getBegin(), in.m_values.getSizeInBytes());
  756. memcpy(out.m_name.getBegin(), in.m_name.cstr(), in.m_name.getLength() + 1);
  757. // Update the count
  758. mutationCount = (i == 0) ? out.m_values.getSize() : mutationCount * out.m_values.getSize();
  759. }
  760. }
  761. else
  762. {
  763. ANKI_ASSERT(binary->m_mutators.getSize() == 0);
  764. }
  765. // Create all variants
  766. Mutex mtx;
  767. Atomic<I32> errorAtomic(0);
  768. class SyncronousShaderProgramAsyncTaskInterface : public ShaderProgramAsyncTaskInterface
  769. {
  770. public:
  771. void enqueueTask(void (*callback)(void* userData), void* userData) final
  772. {
  773. callback(userData);
  774. }
  775. Error joinTasks() final
  776. {
  777. // Nothing
  778. return Error::kNone;
  779. }
  780. } syncTaskManager;
  781. ShaderProgramAsyncTaskInterface& taskManager = (taskManager_) ? *taskManager_ : syncTaskManager;
  782. if(parser.getMutators().getSize() > 0)
  783. {
  784. // Initialize
  785. ShaderCompilerDynamicArray<MutatorValue> mutationValues;
  786. mutationValues.resize(parser.getMutators().getSize());
  787. ShaderCompilerDynamicArray<U32> dials;
  788. dials.resize(parser.getMutators().getSize(), 0);
  789. ShaderCompilerDynamicArray<ShaderProgramBinaryVariant> variants;
  790. ShaderCompilerDynamicArray<ShaderProgramBinaryCodeBlock> codeBlocks;
  791. ShaderCompilerDynamicArray<U64> sourceCodeHashes;
  792. ShaderCompilerDynamicArray<ShaderProgramBinaryMutation> mutations;
  793. mutations.resize(mutationCount);
  794. ShaderCompilerHashMap<U64, U32> mutationHashToIdx;
  795. // Grow the storage of the variants array. Can't have it resize, threads will work on stale data
  796. variants.resizeStorage(mutationCount);
  797. mutationCount = 0;
  798. // Spin for all possible combinations of mutators and
  799. // - Create the spirv
  800. // - Populate the binary variant
  801. do
  802. {
  803. // Create the mutation
  804. for(U32 i = 0; i < parser.getMutators().getSize(); ++i)
  805. {
  806. mutationValues[i] = parser.getMutators()[i].m_values[dials[i]];
  807. }
  808. ShaderProgramBinaryMutation& mutation = mutations[mutationCount++];
  809. newArray(memPool, mutationValues.getSize(), mutation.m_values);
  810. memcpy(mutation.m_values.getBegin(), mutationValues.getBegin(), mutationValues.getSizeInBytes());
  811. mutation.m_hash = computeHash(mutationValues.getBegin(), mutationValues.getSizeInBytes());
  812. ANKI_ASSERT(mutation.m_hash > 0);
  813. if(parser.skipMutation(mutationValues))
  814. {
  815. mutation.m_variantIndex = kMaxU32;
  816. }
  817. else
  818. {
  819. // New and unique mutation and thus variant, add it
  820. compileVariantAsync(parser, spirv, mutation, variants, codeBlocks, sourceCodeHashes, taskManager, mtx, errorAtomic);
  821. ANKI_ASSERT(mutationHashToIdx.find(mutation.m_hash) == mutationHashToIdx.getEnd());
  822. mutationHashToIdx.emplace(mutation.m_hash, mutationCount - 1);
  823. }
  824. } while(!spinDials(dials, parser.getMutators()));
  825. ANKI_ASSERT(mutationCount == mutations.getSize());
  826. // Done, wait the threads
  827. ANKI_CHECK(taskManager.joinTasks());
  828. // Now error out
  829. ANKI_CHECK(Error(errorAtomic.getNonAtomically()));
  830. // Store temp containers to binary
  831. codeBlocks.moveAndReset(binary->m_codeBlocks);
  832. mutations.moveAndReset(binary->m_mutations);
  833. variants.moveAndReset(binary->m_variants);
  834. }
  835. else
  836. {
  837. newArray(memPool, 1, binary->m_mutations);
  838. ShaderCompilerDynamicArray<ShaderProgramBinaryVariant> variants;
  839. ShaderCompilerDynamicArray<ShaderProgramBinaryCodeBlock> codeBlocks;
  840. ShaderCompilerDynamicArray<U64> sourceCodeHashes;
  841. compileVariantAsync(parser, spirv, binary->m_mutations[0], variants, codeBlocks, sourceCodeHashes, taskManager, mtx, errorAtomic);
  842. ANKI_CHECK(taskManager.joinTasks());
  843. ANKI_CHECK(Error(errorAtomic.getNonAtomically()));
  844. ANKI_ASSERT(codeBlocks.getSize() >= parser.getTechniques().getSize());
  845. ANKI_ASSERT(binary->m_mutations[0].m_variantIndex == 0);
  846. ANKI_ASSERT(variants.getSize() == 1);
  847. binary->m_mutations[0].m_hash = 1;
  848. codeBlocks.moveAndReset(binary->m_codeBlocks);
  849. variants.moveAndReset(binary->m_variants);
  850. }
  851. // Sort the mutations
  852. std::sort(binary->m_mutations.getBegin(), binary->m_mutations.getEnd(),
  853. [](const ShaderProgramBinaryMutation& a, const ShaderProgramBinaryMutation& b) {
  854. return a.m_hash < b.m_hash;
  855. });
  856. // Techniques
  857. newArray(memPool, parser.getTechniques().getSize(), binary->m_techniques);
  858. for(U32 i = 0; i < parser.getTechniques().getSize(); ++i)
  859. {
  860. zeroMemory(binary->m_techniques[i].m_name);
  861. memcpy(binary->m_techniques[i].m_name.getBegin(), parser.getTechniques()[i].m_name.cstr(), parser.getTechniques()[i].m_name.getLength() + 1);
  862. binary->m_techniques[i].m_shaderTypes = parser.getTechniques()[i].m_shaderTypes;
  863. binary->m_shaderTypes |= parser.getTechniques()[i].m_shaderTypes;
  864. }
  865. // Structs
  866. if(parser.getGhostStructs().getSize())
  867. {
  868. newArray(memPool, parser.getGhostStructs().getSize(), binary->m_structs);
  869. }
  870. for(U32 i = 0; i < parser.getGhostStructs().getSize(); ++i)
  871. {
  872. const ShaderProgramParserGhostStruct& in = parser.getGhostStructs()[i];
  873. ShaderProgramBinaryStruct& out = binary->m_structs[i];
  874. zeroMemory(out);
  875. memcpy(out.m_name.getBegin(), in.m_name.cstr(), in.m_name.getLength() + 1);
  876. ANKI_ASSERT(in.m_members.getSize());
  877. newArray(memPool, in.m_members.getSize(), out.m_members);
  878. for(U32 j = 0; j < in.m_members.getSize(); ++j)
  879. {
  880. const ShaderProgramParserMember& inm = in.m_members[j];
  881. ShaderProgramBinaryStructMember& outm = out.m_members[j];
  882. zeroMemory(outm.m_name);
  883. memcpy(outm.m_name.getBegin(), inm.m_name.cstr(), inm.m_name.getLength() + 1);
  884. outm.m_offset = inm.m_offset;
  885. outm.m_type = inm.m_type;
  886. }
  887. out.m_size = in.m_members.getBack().m_offset + getShaderVariableDataTypeInfo(in.m_members.getBack().m_type).m_size;
  888. }
  889. return Error::kNone;
  890. }
  891. Error compileShaderProgram(CString fname, Bool spirv, ShaderProgramFilesystemInterface& fsystem, ShaderProgramPostParseInterface* postParseCallback,
  892. ShaderProgramAsyncTaskInterface* taskManager, ConstWeakArray<ShaderCompilerDefine> defines, ShaderProgramBinary*& binary)
  893. {
  894. const Error err = compileShaderProgramInternal(fname, spirv, fsystem, postParseCallback, taskManager, defines, binary);
  895. if(err)
  896. {
  897. ANKI_SHADER_COMPILER_LOGE("Failed to compile: %s", fname.cstr());
  898. freeShaderProgramBinary(binary);
  899. }
  900. return err;
  901. }
  902. } // end namespace anki