BsVulkanGpuProgram.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsVulkanGpuProgram.h"
  4. #include "BsVulkanRenderAPI.h"
  5. #include "BsVulkanDevice.h"
  6. #include "BsVulkanUtility.h"
  7. #include "RenderAPI/BsGpuParams.h"
  8. #include "RenderAPI/BsGpuParamDesc.h"
  9. #include "Managers/BsGpuProgramManager.h"
  10. #include "RenderAPI/BsVertexDeclaration.h"
  11. #include "Managers/BsHardwareBufferManager.h"
  12. #include "Profiling/BsRenderStats.h"
  13. #include "FileSystem/BsFileSystem.h"
  14. #include "FileSystem/BsDataStream.h"
  15. #define AMD_EXTENSIONS
  16. #define NV_EXTENSIONS
  17. #include "glslang/Public/ShaderLang.h"
  18. #include "glslang/Include/Types.h"
  19. #include "SPIRV/GlslangToSpv.h"
  20. #include "SPIRV/Logger.h"
  21. namespace bs { namespace ct
  22. {
  23. const TBuiltInResource DefaultTBuiltInResource = {
  24. /* .MaxLights = */ 32,
  25. /* .MaxClipPlanes = */ 6,
  26. /* .MaxTextureUnits = */ 32,
  27. /* .MaxTextureCoords = */ 32,
  28. /* .MaxVertexAttribs = */ 64,
  29. /* .MaxVertexUniformComponents = */ 4096,
  30. /* .MaxVaryingFloats = */ 64,
  31. /* .MaxVertexTextureImageUnits = */ 32,
  32. /* .MaxCombinedTextureImageUnits = */ 80,
  33. /* .MaxTextureImageUnits = */ 32,
  34. /* .MaxFragmentUniformComponents = */ 4096,
  35. /* .MaxDrawBuffers = */ 32,
  36. /* .MaxVertexUniformVectors = */ 128,
  37. /* .MaxVaryingVectors = */ 8,
  38. /* .MaxFragmentUniformVectors = */ 16,
  39. /* .MaxVertexOutputVectors = */ 16,
  40. /* .MaxFragmentInputVectors = */ 15,
  41. /* .MinProgramTexelOffset = */ -8,
  42. /* .MaxProgramTexelOffset = */ 7,
  43. /* .MaxClipDistances = */ 8,
  44. /* .MaxComputeWorkGroupCountX = */ 65535,
  45. /* .MaxComputeWorkGroupCountY = */ 65535,
  46. /* .MaxComputeWorkGroupCountZ = */ 65535,
  47. /* .MaxComputeWorkGroupSizeX = */ 1024,
  48. /* .MaxComputeWorkGroupSizeY = */ 1024,
  49. /* .MaxComputeWorkGroupSizeZ = */ 64,
  50. /* .MaxComputeUniformComponents = */ 1024,
  51. /* .MaxComputeTextureImageUnits = */ 16,
  52. /* .MaxComputeImageUniforms = */ 8,
  53. /* .MaxComputeAtomicCounters = */ 8,
  54. /* .MaxComputeAtomicCounterBuffers = */ 1,
  55. /* .MaxVaryingComponents = */ 60,
  56. /* .MaxVertexOutputComponents = */ 64,
  57. /* .MaxGeometryInputComponents = */ 64,
  58. /* .MaxGeometryOutputComponents = */ 128,
  59. /* .MaxFragmentInputComponents = */ 128,
  60. /* .MaxImageUnits = */ 8,
  61. /* .MaxCombinedImageUnitsAndFragmentOutputs = */ 8,
  62. /* .MaxCombinedShaderOutputResources = */ 8,
  63. /* .MaxImageSamples = */ 0,
  64. /* .MaxVertexImageUniforms = */ 0,
  65. /* .MaxTessControlImageUniforms = */ 0,
  66. /* .MaxTessEvaluationImageUniforms = */ 0,
  67. /* .MaxGeometryImageUniforms = */ 0,
  68. /* .MaxFragmentImageUniforms = */ 8,
  69. /* .MaxCombinedImageUniforms = */ 8,
  70. /* .MaxGeometryTextureImageUnits = */ 16,
  71. /* .MaxGeometryOutputVertices = */ 256,
  72. /* .MaxGeometryTotalOutputComponents = */ 1024,
  73. /* .MaxGeometryUniformComponents = */ 1024,
  74. /* .MaxGeometryVaryingComponents = */ 64,
  75. /* .MaxTessControlInputComponents = */ 128,
  76. /* .MaxTessControlOutputComponents = */ 128,
  77. /* .MaxTessControlTextureImageUnits = */ 16,
  78. /* .MaxTessControlUniformComponents = */ 1024,
  79. /* .MaxTessControlTotalOutputComponents = */ 4096,
  80. /* .MaxTessEvaluationInputComponents = */ 128,
  81. /* .MaxTessEvaluationOutputComponents = */ 128,
  82. /* .MaxTessEvaluationTextureImageUnits = */ 16,
  83. /* .MaxTessEvaluationUniformComponents = */ 1024,
  84. /* .MaxTessPatchComponents = */ 120,
  85. /* .MaxPatchVertices = */ 32,
  86. /* .MaxTessGenLevel = */ 64,
  87. /* .MaxViewports = */ 16,
  88. /* .MaxVertexAtomicCounters = */ 0,
  89. /* .MaxTessControlAtomicCounters = */ 0,
  90. /* .MaxTessEvaluationAtomicCounters = */ 0,
  91. /* .MaxGeometryAtomicCounters = */ 0,
  92. /* .MaxFragmentAtomicCounters = */ 8,
  93. /* .MaxCombinedAtomicCounters = */ 8,
  94. /* .MaxAtomicCounterBindings = */ 1,
  95. /* .MaxVertexAtomicCounterBuffers = */ 0,
  96. /* .MaxTessControlAtomicCounterBuffers = */ 0,
  97. /* .MaxTessEvaluationAtomicCounterBuffers = */ 0,
  98. /* .MaxGeometryAtomicCounterBuffers = */ 0,
  99. /* .MaxFragmentAtomicCounterBuffers = */ 1,
  100. /* .MaxCombinedAtomicCounterBuffers = */ 1,
  101. /* .MaxAtomicCounterBufferSize = */ 16384,
  102. /* .MaxTransformFeedbackBuffers = */ 4,
  103. /* .MaxTransformFeedbackInterleavedComponents = */ 64,
  104. /* .MaxCullDistances = */ 8,
  105. /* .MaxCombinedClipAndCullDistances = */ 8,
  106. /* .MaxSamples = */ 4,
  107. /* .limits = */{
  108. /* .nonInductiveForLoops = */ 1,
  109. /* .whileLoops = */ 1,
  110. /* .doWhileLoops = */ 1,
  111. /* .generalUniformIndexing = */ 1,
  112. /* .generalAttributeMatrixVectorIndexing = */ 1,
  113. /* .generalVaryingIndexing = */ 1,
  114. /* .generalSamplerIndexing = */ 1,
  115. /* .generalVariableIndexing = */ 1,
  116. /* .generalConstantMatrixVectorIndexing = */ 1,
  117. } };
  118. VertexElementType mapGLSLangToVertexElemType(const glslang::TType& type)
  119. {
  120. if (type.isVector())
  121. {
  122. UINT32 vectorSize = type.getVectorSize();
  123. switch (type.getBasicType())
  124. {
  125. case glslang::EbtFloat:
  126. switch(vectorSize)
  127. {
  128. case 2: return VET_FLOAT2;
  129. case 3: return VET_FLOAT3;
  130. case 4: return VET_FLOAT4;
  131. default: return VET_UNKNOWN;
  132. }
  133. case glslang::EbtInt:
  134. switch (vectorSize)
  135. {
  136. case 2: return VET_INT2;
  137. case 3: return VET_INT3;
  138. case 4: return VET_INT4;
  139. default: return VET_UNKNOWN;
  140. }
  141. case glslang::EbtUint:
  142. switch (vectorSize)
  143. {
  144. case 2: return VET_UINT2;
  145. case 3: return VET_UINT3;
  146. case 4: return VET_UINT4;
  147. default: return VET_UNKNOWN;
  148. }
  149. default:
  150. return VET_UNKNOWN;
  151. }
  152. }
  153. if (type.getVectorSize() == 1)
  154. {
  155. switch (type.getBasicType())
  156. {
  157. case glslang::EbtFloat: return VET_FLOAT1;
  158. case glslang::EbtInt: return VET_INT1;
  159. case glslang::EbtUint: return VET_UINT1;
  160. default: return VET_UNKNOWN;
  161. }
  162. }
  163. return VET_UNKNOWN;
  164. }
  165. GpuParamDataType mapGLSLangToGpuParamDataType(const glslang::TType& type)
  166. {
  167. if (type.getBasicType() == glslang::EbtStruct)
  168. return GPDT_STRUCT;
  169. if (type.isVector())
  170. {
  171. UINT32 vectorSize = type.getVectorSize();
  172. switch (type.getBasicType())
  173. {
  174. case glslang::EbtFloat:
  175. switch (vectorSize)
  176. {
  177. case 2: return GPDT_FLOAT2;
  178. case 3: return GPDT_FLOAT3;
  179. case 4: return GPDT_FLOAT4;
  180. default: return GPDT_UNKNOWN;
  181. }
  182. case glslang::EbtInt:
  183. switch (vectorSize)
  184. {
  185. case 2: return GPDT_INT2;
  186. case 3: return GPDT_INT3;
  187. case 4: return GPDT_INT4;
  188. default: return GPDT_UNKNOWN;
  189. }
  190. case glslang::EbtUint:
  191. switch (vectorSize)
  192. {
  193. case 2: return GPDT_INT2;
  194. case 3: return GPDT_INT3;
  195. case 4: return GPDT_INT4;
  196. default: return GPDT_UNKNOWN;
  197. }
  198. default:
  199. return GPDT_UNKNOWN;
  200. }
  201. }
  202. if (type.isMatrix())
  203. {
  204. switch (type.getBasicType())
  205. {
  206. case glslang::EbtFloat:
  207. switch (type.getMatrixCols())
  208. {
  209. case 2:
  210. switch (type.getMatrixRows())
  211. {
  212. case 2: return GPDT_MATRIX_2X2;
  213. case 3: return GPDT_MATRIX_3X2;
  214. case 4: return GPDT_MATRIX_4X2;
  215. default: return GPDT_UNKNOWN;
  216. }
  217. case 3:
  218. switch (type.getMatrixRows())
  219. {
  220. case 2: return GPDT_MATRIX_2X3;
  221. case 3: return GPDT_MATRIX_3X3;
  222. case 4: return GPDT_MATRIX_4X3;
  223. default: return GPDT_UNKNOWN;
  224. }
  225. case 4:
  226. switch (type.getMatrixRows())
  227. {
  228. case 2: return GPDT_MATRIX_2X4;
  229. case 3: return GPDT_MATRIX_3X4;
  230. case 4: return GPDT_MATRIX_4X4;
  231. default: return GPDT_UNKNOWN;
  232. }
  233. }
  234. default:
  235. return GPDT_UNKNOWN;
  236. }
  237. }
  238. if (type.getVectorSize() == 1)
  239. {
  240. switch (type.getBasicType())
  241. {
  242. case glslang::EbtFloat: return GPDT_FLOAT1;
  243. case glslang::EbtInt: return GPDT_INT1;
  244. case glslang::EbtUint: return GPDT_INT1;
  245. case glslang::EbtBool: return GPDT_BOOL;
  246. default: return GPDT_UNKNOWN;
  247. }
  248. }
  249. return GPDT_UNKNOWN;
  250. }
  251. /** Holds a GLSL program input attribute used in vertex programs. */
  252. struct GLSLAttribute
  253. {
  254. /** Constructs a new attribute from a name and a semantic that represents in which way is the attribute used. */
  255. GLSLAttribute(const String& name, VertexElementSemantic semantic)
  256. :mName(name), mSemantic(semantic)
  257. { }
  258. /**
  259. * Return true if attribute name matches the specified name and returns optional semantic index if it exists. Start
  260. * of the two compared strings must match, and the remaining non-matching bit will be assumed to be the semantic
  261. * index. Returns -1 if no match is made.
  262. */
  263. INT32 matchesName(const String& name) const
  264. {
  265. if (!StringUtil::startsWith(name, mName, false))
  266. return -1;
  267. UINT32 length = (UINT32)mName.size();
  268. return parseINT32(name.substr(length));
  269. }
  270. /** Returns the semantic of this attribute. */
  271. VertexElementSemantic getSemantic() const { return mSemantic; }
  272. private:
  273. String mName;
  274. VertexElementSemantic mSemantic;
  275. };
  276. bool attribNameToElementSemantic(const String& name, VertexElementSemantic& semantic, UINT16& index)
  277. {
  278. static GLSLAttribute attributes[] =
  279. {
  280. GLSLAttribute("bs_position", VES_POSITION),
  281. GLSLAttribute("bs_normal", VES_NORMAL),
  282. GLSLAttribute("bs_tangent", VES_TANGENT),
  283. GLSLAttribute("bs_bitangent", VES_BITANGENT),
  284. GLSLAttribute("bs_texcoord", VES_TEXCOORD),
  285. GLSLAttribute("bs_color", VES_COLOR),
  286. GLSLAttribute("bs_blendweights", VES_BLEND_WEIGHTS),
  287. GLSLAttribute("bs_blendindices", VES_BLEND_INDICES),
  288. GLSLAttribute("POSITION", VES_POSITION),
  289. GLSLAttribute("NORMAL", VES_NORMAL),
  290. GLSLAttribute("TANGENT", VES_TANGENT),
  291. GLSLAttribute("BITANGENT", VES_BITANGENT),
  292. GLSLAttribute("TEXCOORD", VES_TEXCOORD),
  293. GLSLAttribute("COLOR", VES_COLOR),
  294. GLSLAttribute("BLENDWEIGHT", VES_BLEND_WEIGHTS),
  295. GLSLAttribute("BLENDINDICES", VES_BLEND_INDICES)
  296. };
  297. static const UINT32 numAttribs = sizeof(attributes) / sizeof(attributes[0]);
  298. for (UINT32 i = 0; i < numAttribs; i++)
  299. {
  300. INT32 attribIndex = attributes[i].matchesName(name);
  301. if (attribIndex != -1)
  302. {
  303. index = attribIndex;
  304. semantic = attributes[i].getSemantic();
  305. return true;
  306. }
  307. }
  308. return false;
  309. }
  310. bool parseVertexAttributes(const glslang::TProgram* program, List<VertexElement>& elementList, String& log)
  311. {
  312. int numAttributes = program->getNumLiveAttributes();
  313. for (int i = 0; i < numAttributes; i++)
  314. {
  315. const glslang::TType* ttype = program->getAttributeTType(i);
  316. UINT32 location = ttype->getQualifier().layoutLocation;
  317. if (location == (UINT32)-1)
  318. {
  319. log = "Vertex attribute parsing error: Found a vertex attribute without a location "
  320. "qualifier. Each attribute must have an explicitly defined location number.";
  321. return false;
  322. }
  323. const char* attribName = program->getAttributeName(i);
  324. VertexElementSemantic semantic = VES_POSITION;
  325. UINT16 index = 0;
  326. if (attribNameToElementSemantic(attribName, semantic, index))
  327. {
  328. VertexElementType type = mapGLSLangToVertexElemType(*ttype);
  329. if (type == VET_UNKNOWN)
  330. LOGERR("Cannot determine vertex input attribute type for attribute: " + String(attribName));
  331. elementList.push_back(VertexElement(0, location, type, semantic, index));
  332. }
  333. else
  334. {
  335. // Ignore built-in attributes
  336. if (memcmp(attribName, "gl_", 3) != 0)
  337. LOGERR("Cannot determine vertex input attribute semantic for attribute: " + String(attribName));
  338. }
  339. }
  340. return true;
  341. }
  342. void parseStruct(const glslang::TTypeList* typeList, UINT32& size)
  343. {
  344. for (auto iter = typeList->begin(); iter != typeList->end(); ++iter)
  345. {
  346. const glslang::TType* ttype = iter->type;
  347. if (ttype->getBasicType() == glslang::EbtStruct)
  348. {
  349. const glslang::TTypeList* childTypeList = ttype->getStruct();
  350. parseStruct(childTypeList, size);
  351. }
  352. else
  353. {
  354. UINT32 arraySize = 1;
  355. if (ttype->isArray())
  356. arraySize = (UINT32)ttype->getCumulativeArraySize();
  357. GpuParamDataType paramType = mapGLSLangToGpuParamDataType(*ttype);
  358. if (paramType == GPDT_UNKNOWN)
  359. {
  360. LOGWRN("Cannot determine type for uniform inside a struct.");
  361. continue;
  362. }
  363. UINT32 elemSize = VulkanUtility::calcInterfaceBlockElementSizeAndOffset(paramType, arraySize, size);
  364. size += elemSize;
  365. }
  366. }
  367. }
  368. bool parseUniforms(const glslang::TProgram* program, GpuParamDesc& desc, String& log)
  369. {
  370. // Parse individual uniforms
  371. struct UniformInfo
  372. {
  373. UINT32 bufferOffset;
  374. UINT32 arraySize;
  375. };
  376. UnorderedMap<String, UniformInfo> uniforms;
  377. int numUniforms = program->getNumLiveUniformVariables();
  378. for (int i = 0; i < numUniforms; i++)
  379. {
  380. const glslang::TType* ttype = program->getUniformTType(i);
  381. const glslang::TQualifier& qualifier = ttype->getQualifier();
  382. const char* name = program->getUniformName(i);
  383. if (ttype->getBasicType() == glslang::EbtSampler) // Object type
  384. {
  385. // Note: Even though the type is named EbtSampler, all object types are categorized under it (including non
  386. // sampled images and buffers)
  387. if (!qualifier.hasBinding())
  388. {
  389. log = "Uniform parsing error: Found an uniform without a binding qualifier. Each uniform must have an "
  390. "explicitly defined binding number.";
  391. return false;
  392. }
  393. const glslang::TSampler& sampler = ttype->getSampler();
  394. GpuParamObjectDesc param;
  395. param.name = name;
  396. param.slot = qualifier.layoutBinding;
  397. param.set = qualifier.layoutSet;
  398. param.type = GPOT_UNKNOWN;
  399. if (param.set == glslang::TQualifier::layoutSetEnd)
  400. param.set = 0;
  401. if (sampler.isImage())
  402. {
  403. switch (sampler.dim)
  404. {
  405. case glslang::Esd1D: param.type = sampler.isArrayed() ? GPOT_RWTEXTURE1DARRAY : GPOT_RWTEXTURE1D; break;
  406. case glslang::Esd2D:
  407. if(sampler.isArrayed())
  408. param.type = sampler.isMultiSample() ? GPOT_RWTEXTURE2DMSARRAY : GPOT_RWTEXTURE2DARRAY;
  409. else
  410. param.type = sampler.isMultiSample() ? GPOT_RWTEXTURE2DMS : GPOT_RWTEXTURE2D;
  411. break;
  412. case glslang::Esd3D: param.type = GPOT_RWTEXTURE3D; break;
  413. case glslang::EsdBuffer: param.type = GPOT_RWBYTE_BUFFER; break;
  414. default:
  415. break;
  416. }
  417. if(sampler.dim != glslang::EsdBuffer)
  418. desc.loadStoreTextures[name] = param;
  419. else
  420. desc.buffers[name] = param;
  421. }
  422. else
  423. {
  424. switch (sampler.dim)
  425. {
  426. case glslang::Esd1D: param.type = GPOT_SAMPLER1D; break;
  427. case glslang::Esd2D: param.type = sampler.isMultiSample() ? GPOT_SAMPLER2DMS : GPOT_SAMPLER2D; break;
  428. case glslang::Esd3D: param.type = GPOT_SAMPLER3D; break;
  429. case glslang::EsdCube: param.type = GPOT_SAMPLERCUBE; break;
  430. default:
  431. break;
  432. }
  433. desc.samplers[name] = param;
  434. if (!sampler.isPureSampler())
  435. {
  436. switch (sampler.dim)
  437. {
  438. case glslang::Esd1D: param.type = sampler.isArrayed() ? GPOT_TEXTURE1DARRAY : GPOT_TEXTURE1D; break;
  439. case glslang::Esd2D:
  440. if(sampler.isArrayed())
  441. param.type = sampler.isMultiSample() ? GPOT_TEXTURE2DMSARRAY : GPOT_TEXTURE2DARRAY;
  442. else
  443. param.type = sampler.isMultiSample() ? GPOT_TEXTURE2DMS : GPOT_TEXTURE2D;
  444. break;
  445. case glslang::Esd3D: param.type = GPOT_TEXTURE3D; break;
  446. case glslang::EsdCube: param.type = sampler.isArrayed() ? GPOT_TEXTURECUBEARRAY : GPOT_TEXTURECUBE; break;
  447. case glslang::EsdBuffer: param.type = GPOT_BYTE_BUFFER; break;
  448. default:
  449. break;
  450. }
  451. if (sampler.dim != glslang::EsdBuffer)
  452. desc.textures[name] = param;
  453. else
  454. desc.buffers[name] = param;
  455. }
  456. }
  457. if(param.type == GPOT_UNKNOWN)
  458. LOGERR("Cannot determine type for uniform: " + String(name));
  459. }
  460. else
  461. {
  462. if(qualifier.storage == glslang::EvqUniform || qualifier.storage == glslang::EvqGlobal)
  463. {
  464. UniformInfo info;
  465. info.arraySize = program->getUniformArraySize(i);
  466. info.bufferOffset = program->getUniformBufferOffset(i);
  467. uniforms[String(name)] = info;
  468. }
  469. }
  470. }
  471. // Parse uniform blocks
  472. int numBlocks = program->getNumLiveUniformBlocks();
  473. for (int i = 0; i < numBlocks; i++)
  474. {
  475. const glslang::TType* ttype = program->getUniformBlockTType(i);
  476. const glslang::TQualifier& qualifier = ttype->getQualifier();
  477. const char* name = program->getUniformBlockName(i);
  478. if (!qualifier.hasBinding())
  479. {
  480. log = "Uniform parsing error: Found a uniform block without a binding qualifier. Each uniform block must "
  481. " have an explicitly defined binding number.";
  482. return false;
  483. }
  484. if(qualifier.storage == glslang::EvqBuffer) // Shared storage buffer
  485. {
  486. GpuParamObjectDesc param;
  487. param.name = name;
  488. param.slot = qualifier.layoutBinding;
  489. param.set = qualifier.layoutSet;
  490. if (param.set == glslang::TQualifier::layoutSetEnd)
  491. param.set = 0;
  492. param.type = GPOT_RWSTRUCTURED_BUFFER;
  493. desc.buffers[name] = param;
  494. }
  495. else // Uniform buffer
  496. {
  497. int size = program->getUniformBlockSize(i);
  498. GpuParamBlockDesc blockDesc;
  499. blockDesc.name = name;
  500. blockDesc.blockSize = size / 4;
  501. blockDesc.isShareable = true;
  502. blockDesc.slot = qualifier.layoutBinding;
  503. blockDesc.set = qualifier.layoutSet;
  504. if (blockDesc.set == glslang::TQualifier::layoutSetEnd)
  505. blockDesc.set = 0;
  506. desc.paramBlocks[name] = blockDesc;
  507. // Parse members of the uniform buffer
  508. const glslang::TTypeList* typeList = ttype->getStruct();
  509. if(typeList == nullptr)
  510. continue;
  511. for (auto iter = typeList->begin(); iter != typeList->end(); ++iter)
  512. {
  513. const glslang::TType* paramTType = iter->type;
  514. String paramName = paramTType->getFieldName().c_str();
  515. auto findIter = uniforms.find(paramName);
  516. if(findIter == uniforms.end()) // Likely unused and was optimized out
  517. continue;
  518. const UniformInfo& uniformInfo = findIter->second;
  519. GpuParamDataType paramType;
  520. UINT32 elementSize = 0;
  521. UINT32 arrayStride = 0;
  522. if (paramTType->getBasicType() == glslang::EbtStruct)
  523. {
  524. paramType = GPDT_STRUCT;
  525. const glslang::TTypeList* paramTypeList = paramTType->getStruct();
  526. parseStruct(paramTypeList, elementSize);
  527. // Struct alignment always a multiple of vec4
  528. arrayStride = Math::divideAndRoundUp(elementSize, 4U) * 4;
  529. }
  530. else
  531. {
  532. paramType = mapGLSLangToGpuParamDataType(*paramTType);
  533. }
  534. if (paramType == GPDT_UNKNOWN)
  535. {
  536. LOGWRN("Cannot determine type for uniform: " + String(name));
  537. continue;
  538. }
  539. if (paramType != GPDT_STRUCT)
  540. {
  541. const GpuParamDataTypeInfo& typeInfo = bs::GpuParams::PARAM_SIZES.lookup[paramType];
  542. elementSize = typeInfo.size / 4;
  543. arrayStride = elementSize;
  544. }
  545. int bufferOffset = uniformInfo.bufferOffset / 4;
  546. GpuParamDataDesc paramDesc;
  547. paramDesc.name = paramName;
  548. paramDesc.type = paramType;
  549. paramDesc.paramBlockSet = blockDesc.set;
  550. paramDesc.paramBlockSlot = blockDesc.slot;
  551. paramDesc.elementSize = elementSize;
  552. paramDesc.arrayElementStride = arrayStride;
  553. paramDesc.arraySize = paramTType->isArray() ? paramTType->getCumulativeArraySize() : 1;
  554. paramDesc.cpuMemOffset = bufferOffset;
  555. paramDesc.gpuMemOffset = bufferOffset;
  556. desc.params[name] = paramDesc;
  557. }
  558. }
  559. }
  560. return true;
  561. }
  562. VulkanShaderModule::VulkanShaderModule(VulkanResourceManager* owner, VkShaderModule module)
  563. :VulkanResource(owner, true), mModule(module)
  564. { }
  565. VulkanShaderModule::~VulkanShaderModule()
  566. {
  567. vkDestroyShaderModule(mOwner->getDevice().getLogical(), mModule, gVulkanAllocator);
  568. }
  569. VulkanGpuProgram::VulkanGpuProgram(const GPU_PROGRAM_DESC& desc, GpuDeviceFlags deviceMask)
  570. : GpuProgram(desc, deviceMask), mDeviceMask(deviceMask), mModules()
  571. {
  572. }
  573. VulkanGpuProgram::~VulkanGpuProgram()
  574. {
  575. for (UINT32 i = 0; i < BS_MAX_DEVICES; i++)
  576. {
  577. if (mModules[i] != nullptr)
  578. mModules[i]->destroy();
  579. }
  580. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_GpuProgram);
  581. }
  582. void VulkanGpuProgram::initialize()
  583. {
  584. if (!isSupported())
  585. {
  586. mIsCompiled = false;
  587. mCompileError = "Specified program is not supported by the current render system.";
  588. GpuProgram::initialize();
  589. return;
  590. }
  591. TBuiltInResource resources = DefaultTBuiltInResource;
  592. glslang::TProgram* program = bs_new<glslang::TProgram>();
  593. EShLanguage glslType;
  594. switch(mProperties.getType())
  595. {
  596. case GPT_FRAGMENT_PROGRAM:
  597. glslType = EShLangFragment;
  598. break;
  599. case GPT_HULL_PROGRAM:
  600. glslType = EShLangTessControl;
  601. break;
  602. case GPT_DOMAIN_PROGRAM:
  603. glslType = EShLangTessEvaluation;
  604. break;
  605. case GPT_GEOMETRY_PROGRAM:
  606. glslType = EShLangGeometry;
  607. break;
  608. case GPT_VERTEX_PROGRAM:
  609. glslType = EShLangVertex;
  610. break;
  611. case GPT_COMPUTE_PROGRAM:
  612. glslType = EShLangCompute;
  613. break;
  614. default:
  615. break;
  616. }
  617. VulkanRenderAPI& rapi = static_cast<VulkanRenderAPI&>(RenderAPI::instance());
  618. VulkanDevice* devices[BS_MAX_DEVICES];
  619. std::vector<UINT32> spirv;
  620. spv::SpvBuildLogger logger;
  621. std::string compileLog;
  622. const String& source = mProperties.getSource();
  623. const char* sourceBytes = source.c_str();
  624. glslang::TShader* shader = bs_new<glslang::TShader>(glslType);
  625. shader->setStrings(&sourceBytes, 1);
  626. shader->setEntryPoint("main");
  627. EShMessages messages = (EShMessages)((int)EShMsgSpvRules | (int)EShMsgVulkanRules);
  628. if (!shader->parse(&resources, 450, false, messages))
  629. {
  630. mIsCompiled = false;
  631. mCompileError = "Compile error: " + String(shader->getInfoLog());
  632. goto cleanup;
  633. }
  634. program->addShader(shader);
  635. if (!program->link(messages))
  636. {
  637. mIsCompiled = false;
  638. mCompileError = "Link error: " + String(program->getInfoLog());
  639. goto cleanup;
  640. }
  641. program->mapIO();
  642. program->buildReflection();
  643. // Compile to SPIR-V
  644. GlslangToSpv(*program->getIntermediate(glslType), spirv, &logger);
  645. // Parse uniforms
  646. if(!parseUniforms(program, *mParametersDesc, mCompileError))
  647. {
  648. mIsCompiled = false;
  649. goto cleanup;
  650. }
  651. // If vertex program, retrieve information about vertex inputs
  652. if (mProperties.getType() == GPT_VERTEX_PROGRAM)
  653. {
  654. List<VertexElement> elementList;
  655. if (parseVertexAttributes(program, elementList, mCompileError))
  656. mInputDeclaration = HardwareBufferManager::instance().createVertexDeclaration(elementList, mDeviceMask);
  657. else
  658. {
  659. mIsCompiled = false;
  660. goto cleanup;
  661. }
  662. }
  663. // Create Vulkan module
  664. VkShaderModuleCreateInfo moduleCI;
  665. moduleCI.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
  666. moduleCI.pNext = nullptr;
  667. moduleCI.flags = 0;
  668. moduleCI.codeSize = spirv.size() * sizeof(UINT32);
  669. moduleCI.pCode = spirv.data();
  670. VulkanUtility::getDevices(rapi, mDeviceMask, devices);
  671. for (UINT32 i = 0; i < BS_MAX_DEVICES; i++)
  672. {
  673. if (devices[i] != nullptr)
  674. {
  675. VkDevice vkDevice = devices[i]->getLogical();
  676. VulkanResourceManager& rescManager = devices[i]->getResourceManager();
  677. VkShaderModule shaderModule;
  678. VkResult result = vkCreateShaderModule(vkDevice, &moduleCI, gVulkanAllocator, &shaderModule);
  679. assert(result == VK_SUCCESS);
  680. mModules[i] = rescManager.create<VulkanShaderModule>(shaderModule);
  681. }
  682. }
  683. mIsCompiled = true;
  684. cleanup:
  685. bs_delete(program);
  686. bs_delete(shader);
  687. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_GpuProgram);
  688. GpuProgram::initialize();
  689. }
  690. }}