BsVulkanGpuProgram.cpp 21 KB

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