BsVulkanGpuProgram.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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 "BsGpuParams.h"
  8. #include "BsGpuParamDesc.h"
  9. #include "BsGpuProgramManager.h"
  10. #include "BsVertexDeclaration.h"
  11. #include "BsHardwareBufferManager.h"
  12. #include "BsRenderStats.h"
  13. #include "BsFileSystem.h"
  14. #include "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. };
  288. static const UINT32 numAttribs = sizeof(attributes) / sizeof(attributes[0]);
  289. for (UINT32 i = 0; i < numAttribs; i++)
  290. {
  291. INT32 attribIndex = attributes[i].matchesName(name);
  292. if (attribIndex != -1)
  293. {
  294. index = attribIndex;
  295. semantic = attributes[i].getSemantic();
  296. return true;
  297. }
  298. }
  299. return false;
  300. }
  301. bool parseVertexAttributes(const glslang::TProgram* program, List<VertexElement>& elementList, String& log)
  302. {
  303. int numAttributes = program->getNumLiveAttributes();
  304. for (int i = 0; i < numAttributes; i++)
  305. {
  306. const glslang::TType* ttype = program->getAttributeTType(i);
  307. UINT32 location = ttype->getQualifier().layoutLocation;
  308. if (location == -1)
  309. {
  310. log = "Vertex attribute parsing error: Found a vertex attribute without a location "
  311. "qualifier. Each attribute must have an explicitly defined location number.";
  312. return false;
  313. }
  314. const char* attribName = program->getAttributeName(i);
  315. VertexElementSemantic semantic = VES_POSITION;
  316. UINT16 index = 0;
  317. if (attribNameToElementSemantic(attribName, semantic, index))
  318. {
  319. VertexElementType type = mapGLSLangToVertexElemType(*ttype);
  320. if (type == VET_UNKNOWN)
  321. LOGERR("Cannot determine vertex input attribute type for attribute: " + String(attribName));
  322. elementList.push_back(VertexElement(0, location, type, semantic, index));
  323. }
  324. else
  325. {
  326. // Ignore built-in attributes
  327. if (memcmp(attribName, "gl_", 3) != 0)
  328. LOGERR("Cannot determine vertex input attribute semantic for attribute: " + String(attribName));
  329. }
  330. }
  331. return true;
  332. }
  333. bool parseUniforms(const glslang::TProgram* program, GpuParamDesc& desc, String& log)
  334. {
  335. // Parse uniform blocks
  336. UnorderedMap<UINT32, String> uniformBlockMap;
  337. int numBlocks = program->getNumLiveUniformBlocks();
  338. for (int i = 0; i < numBlocks; i++)
  339. {
  340. const glslang::TType* ttype = program->getUniformBlockTType(i);
  341. const glslang::TQualifier& qualifier = ttype->getQualifier();
  342. const char* name = program->getUniformBlockName(i);
  343. if (!qualifier.hasBinding())
  344. {
  345. log = "Uniform parsing error: Found a uniform block without a binding qualifier. Each uniform block must "
  346. " have an explicitly defined binding number.";
  347. return false;
  348. }
  349. if(qualifier.storage == glslang::EvqBuffer) // Shared storage buffer
  350. {
  351. GpuParamObjectDesc param;
  352. param.name = name;
  353. param.slot = qualifier.layoutBinding;
  354. param.set = qualifier.layoutSet;
  355. if (param.set == glslang::TQualifier::layoutSetEnd)
  356. param.set = 0;
  357. param.type = GPOT_RWSTRUCTURED_BUFFER;
  358. desc.buffers[name] = param;
  359. }
  360. else // Uniform buffer
  361. {
  362. int size = program->getUniformBlockSize(i);
  363. GpuParamBlockDesc param;
  364. param.name = name;
  365. param.blockSize = size / 4;
  366. param.isShareable = true;
  367. param.slot = qualifier.layoutBinding;
  368. param.set = qualifier.layoutSet;
  369. if (param.set == glslang::TQualifier::layoutSetEnd)
  370. param.set = 0;
  371. desc.paramBlocks[name] = param;
  372. uniformBlockMap[i] = name;
  373. }
  374. }
  375. // Parse individual uniforms
  376. int numUniforms = program->getNumLiveUniformVariables();
  377. for (int i = 0; i < numUniforms; i++)
  378. {
  379. const glslang::TType* ttype = program->getUniformTType(i);
  380. const glslang::TQualifier& qualifier = ttype->getQualifier();
  381. const char* name = program->getUniformName(i);
  382. if (ttype->getBasicType() == glslang::EbtSampler) // Object type
  383. {
  384. // Note: Even though the type is named EbtSampler, all object types are categorized under it (including non
  385. // sampled images and buffers)
  386. if (!qualifier.hasBinding())
  387. {
  388. log = "Uniform parsing error: Found an uniform without a binding qualifier. Each uniform must have an "
  389. "explicitly defined binding number.";
  390. return false;
  391. }
  392. const glslang::TSampler& sampler = ttype->getSampler();
  393. GpuParamObjectDesc param;
  394. param.name = name;
  395. param.slot = qualifier.layoutBinding;
  396. param.set = qualifier.layoutSet;
  397. if (param.set == glslang::TQualifier::layoutSetEnd)
  398. param.set = 0;
  399. if (sampler.isImage())
  400. {
  401. switch (sampler.dim)
  402. {
  403. case glslang::Esd1D: param.type = GPOT_RWTEXTURE1D; break;
  404. case glslang::Esd2D: param.type = sampler.isMultiSample() ? GPOT_RWTEXTURE2DMS : GPOT_RWTEXTURE2D; break;
  405. case glslang::Esd3D: param.type = GPOT_RWTEXTURE3D; break;
  406. case glslang::EsdBuffer: param.type = GPOT_RWBYTE_BUFFER; break;
  407. }
  408. if(sampler.dim != glslang::EsdBuffer)
  409. desc.loadStoreTextures[name] = param;
  410. else
  411. desc.buffers[name] = param;
  412. }
  413. else
  414. {
  415. switch (sampler.dim)
  416. {
  417. case glslang::Esd1D: param.type = GPOT_SAMPLER1D; break;
  418. case glslang::Esd2D: param.type = sampler.isMultiSample() ? GPOT_SAMPLER2DMS : GPOT_SAMPLER2D; break;
  419. case glslang::Esd3D: param.type = GPOT_SAMPLER3D; break;
  420. case glslang::EsdCube: param.type = GPOT_SAMPLERCUBE; break;
  421. }
  422. desc.samplers[name] = param;
  423. if (!sampler.isPureSampler())
  424. {
  425. switch (sampler.dim)
  426. {
  427. case glslang::Esd1D: param.type = GPOT_TEXTURE1D; break;
  428. case glslang::Esd2D: param.type = sampler.isMultiSample() ? GPOT_TEXTURE2DMS : GPOT_TEXTURE2D; break;
  429. case glslang::Esd3D: param.type = GPOT_TEXTURE3D; break;
  430. case glslang::EsdCube: param.type = GPOT_TEXTURECUBE; break;
  431. case glslang::EsdBuffer: param.type = GPOT_BYTE_BUFFER; break;
  432. }
  433. if (sampler.dim != glslang::EsdBuffer)
  434. desc.textures[name] = param;
  435. else
  436. desc.buffers[name] = param;
  437. }
  438. }
  439. if(param.type == GPOT_UNKNOWN)
  440. LOGERR("Cannot determine type for uniform: " + String(name));
  441. }
  442. else
  443. {
  444. // We don't parse individual members of shared storage buffers
  445. if (qualifier.storage == glslang::EvqBuffer)
  446. continue;
  447. if(ttype->getBasicType() == glslang::EbtStruct)
  448. {
  449. // Not handling structs at the moment
  450. }
  451. else
  452. {
  453. GpuParamDataType paramType = mapGLSLangToGpuParamDataType(*ttype);
  454. if (paramType == GPDT_UNKNOWN)
  455. {
  456. LOGWRN("Cannot determine type for uniform: " + String(name));
  457. continue;
  458. }
  459. int blockIdx = program->getUniformBlockIndex(i);
  460. auto iterFind = uniformBlockMap.find(blockIdx);
  461. if (iterFind == uniformBlockMap.end())
  462. LOGERR("Uniform is referencing a uniform block that doesn't exist: " + String(name));
  463. const GpuParamBlockDesc& paramBlockDesc = desc.paramBlocks[iterFind->second];
  464. const GpuParamDataTypeInfo& typeInfo = bs::GpuParams::PARAM_SIZES.lookup[paramType];
  465. int bufferOffset = program->getUniformBufferOffset(i) / 4;
  466. GpuParamDataDesc param;
  467. param.name = name;
  468. param.type = paramType;
  469. param.paramBlockSet = paramBlockDesc.set;
  470. param.paramBlockSlot = paramBlockDesc.slot;
  471. param.elementSize = typeInfo.size / 4;
  472. param.arrayElementStride = param.elementSize;
  473. param.arraySize = program->getUniformArraySize(i);
  474. param.cpuMemOffset = bufferOffset;
  475. param.gpuMemOffset = bufferOffset;
  476. desc.params[name] = param;
  477. }
  478. }
  479. }
  480. return true;
  481. }
  482. VulkanShaderModule::VulkanShaderModule(VulkanResourceManager* owner, VkShaderModule module)
  483. :VulkanResource(owner, true), mModule(module)
  484. { }
  485. VulkanShaderModule::~VulkanShaderModule()
  486. {
  487. vkDestroyShaderModule(mOwner->getDevice().getLogical(), mModule, gVulkanAllocator);
  488. }
  489. VulkanGpuProgram::VulkanGpuProgram(const GPU_PROGRAM_DESC& desc, GpuDeviceFlags deviceMask)
  490. : GpuProgram(desc, deviceMask), mDeviceMask(deviceMask), mModules()
  491. {
  492. }
  493. VulkanGpuProgram::~VulkanGpuProgram()
  494. {
  495. for (UINT32 i = 0; i < BS_MAX_DEVICES; i++)
  496. {
  497. if (mModules[i] != nullptr)
  498. mModules[i]->destroy();
  499. }
  500. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_GpuProgram);
  501. }
  502. void VulkanGpuProgram::initialize()
  503. {
  504. if (!isSupported())
  505. {
  506. mIsCompiled = false;
  507. mCompileError = "Specified program is not supported by the current render system.";
  508. GpuProgram::initialize();
  509. return;
  510. }
  511. TBuiltInResource resources = DefaultTBuiltInResource;
  512. glslang::TProgram* program = bs_new<glslang::TProgram>();
  513. EShLanguage glslType;
  514. switch(mProperties.getType())
  515. {
  516. case GPT_FRAGMENT_PROGRAM:
  517. glslType = EShLangFragment;
  518. break;
  519. case GPT_HULL_PROGRAM:
  520. glslType = EShLangTessControl;
  521. break;
  522. case GPT_DOMAIN_PROGRAM:
  523. glslType = EShLangTessEvaluation;
  524. break;
  525. case GPT_GEOMETRY_PROGRAM:
  526. glslType = EShLangGeometry;
  527. break;
  528. case GPT_VERTEX_PROGRAM:
  529. glslType = EShLangVertex;
  530. break;
  531. case GPT_COMPUTE_PROGRAM:
  532. glslType = EShLangCompute;
  533. break;
  534. }
  535. std::vector<UINT32> spirv;
  536. spv::SpvBuildLogger logger;
  537. std::string compileLog;
  538. const String& source = mProperties.getSource();
  539. const char* sourceBytes = source.c_str();
  540. glslang::TShader* shader = bs_new<glslang::TShader>(glslType);
  541. shader->setStrings(&sourceBytes, 1);
  542. shader->setEntryPoint("main");
  543. EShMessages messages = (EShMessages)((int)EShMsgSpvRules | (int)EShMsgVulkanRules);
  544. if (!shader->parse(&resources, 450, false, messages))
  545. {
  546. mIsCompiled = false;
  547. mCompileError = "Compile error: " + String(shader->getInfoLog());
  548. goto cleanup;
  549. }
  550. program->addShader(shader);
  551. if (!program->link(messages))
  552. {
  553. mIsCompiled = false;
  554. mCompileError = "Link error: " + String(program->getInfoLog());
  555. goto cleanup;
  556. }
  557. program->mapIO();
  558. program->buildReflection();
  559. // Compile to SPIR-V
  560. GlslangToSpv(*program->getIntermediate(glslType), spirv, &logger);
  561. // Parse uniforms
  562. if(!parseUniforms(program, *mParametersDesc, mCompileError))
  563. {
  564. mIsCompiled = false;
  565. goto cleanup;
  566. }
  567. // If vertex program, retrieve information about vertex inputs
  568. if (mProperties.getType() == GPT_VERTEX_PROGRAM)
  569. {
  570. List<VertexElement> elementList;
  571. if (parseVertexAttributes(program, elementList, mCompileError))
  572. mInputDeclaration = HardwareBufferManager::instance().createVertexDeclaration(elementList, mDeviceMask);
  573. else
  574. {
  575. mIsCompiled = false;
  576. goto cleanup;
  577. }
  578. }
  579. // Create Vulkan module
  580. VkShaderModuleCreateInfo moduleCI;
  581. moduleCI.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
  582. moduleCI.pNext = nullptr;
  583. moduleCI.flags = 0;
  584. moduleCI.codeSize = spirv.size() * sizeof(UINT32);
  585. moduleCI.pCode = spirv.data();
  586. VulkanRenderAPI& rapi = static_cast<VulkanRenderAPI&>(RenderAPI::instance());
  587. VulkanDevice* devices[BS_MAX_DEVICES];
  588. VulkanUtility::getDevices(rapi, mDeviceMask, devices);
  589. for (UINT32 i = 0; i < BS_MAX_DEVICES; i++)
  590. {
  591. if (devices[i] != nullptr)
  592. {
  593. VkDevice vkDevice = devices[i]->getLogical();
  594. VulkanResourceManager& rescManager = devices[i]->getResourceManager();
  595. VkShaderModule shaderModule;
  596. VkResult result = vkCreateShaderModule(vkDevice, &moduleCI, gVulkanAllocator, &shaderModule);
  597. assert(result == VK_SUCCESS);
  598. mModules[i] = rescManager.create<VulkanShaderModule>(shaderModule);
  599. }
  600. }
  601. mIsCompiled = true;
  602. cleanup:
  603. bs_delete(program);
  604. bs_delete(shader);
  605. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_GpuProgram);
  606. GpuProgram::initialize();
  607. }
  608. }}