Shader.cpp 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  1. /**
  2. * Copyright (c) 2006-2023 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "graphics/vertex.h"
  21. #include "Shader.h"
  22. #include "Graphics.h"
  23. #include "libraries/glslang/glslang/Public/ShaderLang.h"
  24. #include "libraries/glslang/SPIRV/GlslangToSpv.h"
  25. namespace love
  26. {
  27. namespace graphics
  28. {
  29. namespace vulkan
  30. {
  31. static const TBuiltInResource defaultTBuiltInResource = {
  32. /* .MaxLights = */ 32,
  33. /* .MaxClipPlanes = */ 6,
  34. /* .MaxTextureUnits = */ 32,
  35. /* .MaxTextureCoords = */ 32,
  36. /* .MaxVertexAttribs = */ 64,
  37. /* .MaxVertexUniformComponents = */ 16384,
  38. /* .MaxVaryingFloats = */ 128,
  39. /* .MaxVertexTextureImageUnits = */ 32,
  40. /* .MaxCombinedTextureImageUnits = */ 80,
  41. /* .MaxTextureImageUnits = */ 32,
  42. /* .MaxFragmentUniformComponents = */ 16384,
  43. /* .MaxDrawBuffers = */ 8,
  44. /* .MaxVertexUniformVectors = */ 4096,
  45. /* .MaxVaryingVectors = */ 32,
  46. /* .MaxFragmentUniformVectors = */ 4096,
  47. /* .MaxVertexOutputVectors = */ 32,
  48. /* .MaxFragmentInputVectors = */ 31,
  49. /* .MinProgramTexelOffset = */ -8,
  50. /* .MaxProgramTexelOffset = */ 7,
  51. /* .MaxClipDistances = */ 8,
  52. /* .MaxComputeWorkGroupCountX = */ 65535,
  53. /* .MaxComputeWorkGroupCountY = */ 65535,
  54. /* .MaxComputeWorkGroupCountZ = */ 65535,
  55. /* .MaxComputeWorkGroupSizeX = */ 1024,
  56. /* .MaxComputeWorkGroupSizeY = */ 1024,
  57. /* .MaxComputeWorkGroupSizeZ = */ 64,
  58. /* .MaxComputeUniformComponents = */ 1024,
  59. /* .MaxComputeTextureImageUnits = */ 32,
  60. /* .MaxComputeImageUniforms = */ 16,
  61. /* .MaxComputeAtomicCounters = */ 4096,
  62. /* .MaxComputeAtomicCounterBuffers = */ 8,
  63. /* .MaxVaryingComponents = */ 128,
  64. /* .MaxVertexOutputComponents = */ 128,
  65. /* .MaxGeometryInputComponents = */ 128,
  66. /* .MaxGeometryOutputComponents = */ 128,
  67. /* .MaxFragmentInputComponents = */ 128,
  68. /* .MaxImageUnits = */ 192,
  69. /* .MaxCombinedImageUnitsAndFragmentOutputs = */ 144,
  70. /* .MaxCombinedShaderOutputResources = */ 144,
  71. /* .MaxImageSamples = */ 32,
  72. /* .MaxVertexImageUniforms = */ 16,
  73. /* .MaxTessControlImageUniforms = */ 16,
  74. /* .MaxTessEvaluationImageUniforms = */ 16,
  75. /* .MaxGeometryImageUniforms = */ 16,
  76. /* .MaxFragmentImageUniforms = */ 16,
  77. /* .MaxCombinedImageUniforms = */ 80,
  78. /* .MaxGeometryTextureImageUnits = */ 16,
  79. /* .MaxGeometryOutputVertices = */ 256,
  80. /* .MaxGeometryTotalOutputComponents = */ 1024,
  81. /* .MaxGeometryUniformComponents = */ 1024,
  82. /* .MaxGeometryVaryingComponents = */ 64,
  83. /* .MaxTessControlInputComponents = */ 128,
  84. /* .MaxTessControlOutputComponents = */ 128,
  85. /* .MaxTessControlTextureImageUnits = */ 16,
  86. /* .MaxTessControlUniformComponents = */ 1024,
  87. /* .MaxTessControlTotalOutputComponents = */ 4096,
  88. /* .MaxTessEvaluationInputComponents = */ 128,
  89. /* .MaxTessEvaluationOutputComponents = */ 128,
  90. /* .MaxTessEvaluationTextureImageUnits = */ 16,
  91. /* .MaxTessEvaluationUniformComponents = */ 1024,
  92. /* .MaxTessPatchComponents = */ 120,
  93. /* .MaxPatchVertices = */ 32,
  94. /* .MaxTessGenLevel = */ 64,
  95. /* .MaxViewports = */ 16,
  96. /* .MaxVertexAtomicCounters = */ 4096,
  97. /* .MaxTessControlAtomicCounters = */ 4096,
  98. /* .MaxTessEvaluationAtomicCounters = */ 4096,
  99. /* .MaxGeometryAtomicCounters = */ 4096,
  100. /* .MaxFragmentAtomicCounters = */ 4096,
  101. /* .MaxCombinedAtomicCounters = */ 4096,
  102. /* .MaxAtomicCounterBindings = */ 8,
  103. /* .MaxVertexAtomicCounterBuffers = */ 8,
  104. /* .MaxTessControlAtomicCounterBuffers = */ 8,
  105. /* .MaxTessEvaluationAtomicCounterBuffers = */ 8,
  106. /* .MaxGeometryAtomicCounterBuffers = */ 8,
  107. /* .MaxFragmentAtomicCounterBuffers = */ 8,
  108. /* .MaxCombinedAtomicCounterBuffers = */ 8,
  109. /* .MaxAtomicCounterBufferSize = */ 16384,
  110. /* .MaxTransformFeedbackBuffers = */ 4,
  111. /* .MaxTransformFeedbackInterleavedComponents = */ 64,
  112. /* .MaxCullDistances = */ 8,
  113. /* .MaxCombinedClipAndCullDistances = */ 8,
  114. /* .MaxSamples = */ 32,
  115. /* .maxMeshOutputVerticesNV = */ 256,
  116. /* .maxMeshOutputPrimitivesNV = */ 512,
  117. /* .maxMeshWorkGroupSizeX_NV = */ 32,
  118. /* .maxMeshWorkGroupSizeY_NV = */ 1,
  119. /* .maxMeshWorkGroupSizeZ_NV = */ 1,
  120. /* .maxTaskWorkGroupSizeX_NV = */ 32,
  121. /* .maxTaskWorkGroupSizeY_NV = */ 1,
  122. /* .maxTaskWorkGroupSizeZ_NV = */ 1,
  123. /* .maxMeshViewCountNV = */ 4,
  124. /* .maxDualSourceDrawBuffersEXT = */ 1,
  125. /* .limits = */ {
  126. /* .nonInductiveForLoops = */ 1,
  127. /* .whileLoops = */ 1,
  128. /* .doWhileLoops = */ 1,
  129. /* .generalUniformIndexing = */ 1,
  130. /* .generalAttributeMatrixVectorIndexing = */ 1,
  131. /* .generalVaryingIndexing = */ 1,
  132. /* .generalSamplerIndexing = */ 1,
  133. /* .generalVariableIndexing = */ 1,
  134. /* .generalConstantMatrixVectorIndexing = */ 1,
  135. }
  136. };
  137. static const uint32_t STREAMBUFFER_DEFAULT_SIZE = 16;
  138. static const uint32_t DESCRIPTOR_POOL_SIZE = 1;
  139. class BindingMapper
  140. {
  141. public:
  142. uint32_t operator()(spirv_cross::CompilerGLSL &comp, std::vector<uint32_t> &spirv, const std::string &name, const spirv_cross::ID &id)
  143. {
  144. auto it = bindingMappings.find(name);
  145. if (it == bindingMappings.end())
  146. {
  147. auto binding = comp.get_decoration(id, spv::DecorationBinding);
  148. if (isFreeBinding(binding))
  149. {
  150. bindingMappings[name] = binding;
  151. return binding;
  152. }
  153. else
  154. {
  155. uint32_t freeBinding = getFreeBinding();
  156. uint32_t binaryBindingOffset;
  157. if (!comp.get_binary_offset_for_decoration(id, spv::DecorationBinding, binaryBindingOffset))
  158. throw love::Exception("could not get binary offset for binding");
  159. spirv[binaryBindingOffset] = freeBinding;
  160. bindingMappings[name] = freeBinding;
  161. return freeBinding;
  162. }
  163. }
  164. else
  165. return it->second;
  166. };
  167. private:
  168. uint32_t getFreeBinding()
  169. {
  170. for (uint32_t i = 0;; i++)
  171. {
  172. if (isFreeBinding(i))
  173. return i;
  174. }
  175. }
  176. bool isFreeBinding(uint32_t binding)
  177. {
  178. for (const auto &entry : bindingMappings)
  179. {
  180. if (entry.second == binding)
  181. return false;
  182. }
  183. return true;
  184. }
  185. std::map<std::string, uint32_t> bindingMappings;
  186. };
  187. static VkShaderStageFlagBits getStageBit(ShaderStageType type)
  188. {
  189. switch (type)
  190. {
  191. case SHADERSTAGE_VERTEX:
  192. return VK_SHADER_STAGE_VERTEX_BIT;
  193. case SHADERSTAGE_PIXEL:
  194. return VK_SHADER_STAGE_FRAGMENT_BIT;
  195. case SHADERSTAGE_COMPUTE:
  196. return VK_SHADER_STAGE_COMPUTE_BIT;
  197. default:
  198. throw love::Exception("invalid type");
  199. }
  200. }
  201. static EShLanguage getGlslShaderType(ShaderStageType stage)
  202. {
  203. switch (stage)
  204. {
  205. case SHADERSTAGE_VERTEX:
  206. return EShLangVertex;
  207. case SHADERSTAGE_PIXEL:
  208. return EShLangFragment;
  209. case SHADERSTAGE_COMPUTE:
  210. return EShLangCompute;
  211. default:
  212. throw love::Exception("unkonwn shader stage type");
  213. }
  214. }
  215. static bool usesLocalUniformData(const graphics::Shader::UniformInfo* info)
  216. {
  217. return info->baseType == graphics::Shader::UNIFORM_BOOL ||
  218. info->baseType == graphics::Shader::UNIFORM_FLOAT ||
  219. info->baseType == graphics::Shader::UNIFORM_INT ||
  220. info->baseType == graphics::Shader::UNIFORM_MATRIX ||
  221. info->baseType == graphics::Shader::UNIFORM_UINT;
  222. }
  223. Shader::Shader(StrongRef<love::graphics::ShaderStage> stages[])
  224. : graphics::Shader(stages)
  225. {
  226. auto gfx = Module::getInstance<Graphics>(Module::ModuleType::M_GRAPHICS);
  227. vgfx = dynamic_cast<Graphics*>(gfx);
  228. loadVolatile();
  229. }
  230. bool Shader::loadVolatile()
  231. {
  232. device = vgfx->getDevice();
  233. computePipeline = VK_NULL_HANDLE;
  234. for (int i = 0; i < BUILTIN_MAX_ENUM; i++)
  235. builtinUniformInfo[i] = nullptr;
  236. compileShaders();
  237. calculateUniformBufferSizeAligned();
  238. createDescriptorSetLayout();
  239. createPipelineLayout();
  240. createDescriptorPoolSizes();
  241. createStreamBuffers();
  242. descriptorSetsVector.resize(MAX_FRAMES_IN_FLIGHT);
  243. currentFrame = 0;
  244. currentUsedUniformStreamBuffersCount = 0;
  245. currentUsedDescriptorSetsCount = 0;
  246. newFrame();
  247. return true;
  248. }
  249. void Shader::unloadVolatile()
  250. {
  251. if (shaderModules.empty())
  252. return;
  253. for (const auto &uniform : uniformInfos)
  254. {
  255. switch (uniform.second.baseType)
  256. {
  257. case UNIFORM_SAMPLER:
  258. case UNIFORM_STORAGETEXTURE:
  259. for (int i = 0; i < uniform.second.count; i++)
  260. {
  261. if (uniform.second.textures[i] != nullptr)
  262. uniform.second.textures[i]->release();
  263. }
  264. delete[] uniform.second.textures;
  265. break;
  266. case UNIFORM_TEXELBUFFER:
  267. case UNIFORM_STORAGEBUFFER:
  268. for (int i = 0; i < uniform.second.count; i++)
  269. {
  270. if (uniform.second.buffers[i] != nullptr)
  271. uniform.second.buffers[i]->release();
  272. }
  273. delete[] uniform.second.buffers;
  274. break;
  275. }
  276. }
  277. vgfx->queueCleanUp([shaderModules = std::move(shaderModules), device = device, descriptorSetLayout = descriptorSetLayout, pipelineLayout = pipelineLayout, descriptorPools = descriptorPools, computePipeline = computePipeline](){
  278. for (const auto pool : descriptorPools)
  279. vkDestroyDescriptorPool(device, pool, nullptr);
  280. for (const auto shaderModule : shaderModules)
  281. vkDestroyShaderModule(device, shaderModule, nullptr);
  282. vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
  283. vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
  284. if (computePipeline != VK_NULL_HANDLE)
  285. vkDestroyPipeline(device, computePipeline, nullptr);
  286. });
  287. while (!freeDescriptorSets.empty())
  288. freeDescriptorSets.pop();
  289. for (const auto streamBuffer : streamBuffers)
  290. streamBuffer->release();
  291. shaderModules.clear();
  292. shaderStages.clear();
  293. streamBuffers.clear();
  294. descriptorPools.clear();
  295. descriptorSetsVector.clear();
  296. }
  297. const std::vector<VkPipelineShaderStageCreateInfo> &Shader::getShaderStages() const
  298. {
  299. return shaderStages;
  300. }
  301. const VkPipelineLayout Shader::getGraphicsPipelineLayout() const
  302. {
  303. return pipelineLayout;
  304. }
  305. VkPipeline Shader::getComputePipeline() const
  306. {
  307. return computePipeline;
  308. }
  309. void Shader::newFrame()
  310. {
  311. currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
  312. currentUsedUniformStreamBuffersCount = 0;
  313. currentUsedDescriptorSetsCount = 0;
  314. if (streamBuffers.size() > 1)
  315. {
  316. size_t newSize = 0;
  317. for (auto streamBuffer : streamBuffers)
  318. {
  319. newSize += streamBuffer->getSize();
  320. streamBuffer->release();
  321. }
  322. streamBuffers.clear();
  323. streamBuffers.push_back(new StreamBuffer(vgfx, BUFFERUSAGE_UNIFORM, newSize));
  324. }
  325. else
  326. streamBuffers.at(0)->nextFrame();
  327. if (descriptorSetsVector.at(currentFrame).size() == 0)
  328. descriptorSetsVector.at(currentFrame).push_back(allocateDescriptorSet());
  329. currentDescriptorSet = descriptorSetsVector.at(currentFrame).at(0);
  330. }
  331. void Shader::cmdPushDescriptorSets(VkCommandBuffer commandBuffer, VkPipelineBindPoint bindPoint)
  332. {
  333. if (!localUniformData.empty())
  334. {
  335. auto usedStreamBufferMemory = currentUsedUniformStreamBuffersCount * uniformBufferSizeAligned;
  336. if (usedStreamBufferMemory >= streamBuffers.back()->getSize())
  337. {
  338. streamBuffers.push_back(new StreamBuffer(vgfx, BUFFERUSAGE_UNIFORM, STREAMBUFFER_DEFAULT_SIZE * uniformBufferSizeAligned));
  339. currentUsedUniformStreamBuffersCount = 0;
  340. }
  341. if (builtinUniformDataOffset.hasValue)
  342. {
  343. auto builtinData = vgfx->getCurrentBuiltinUniformData();
  344. auto dst = localUniformData.data() + builtinUniformDataOffset.value;
  345. memcpy(dst, &builtinData, sizeof(builtinData));
  346. }
  347. auto currentStreamBuffer = streamBuffers.back();
  348. auto mapInfo = currentStreamBuffer->map(uniformBufferSizeAligned);
  349. memcpy(mapInfo.data, localUniformData.data(), localUniformData.size());
  350. auto offset = currentStreamBuffer->unmap(uniformBufferSizeAligned);
  351. currentStreamBuffer->markUsed(uniformBufferSizeAligned);
  352. VkDescriptorBufferInfo bufferInfo{};
  353. bufferInfo.buffer = (VkBuffer)currentStreamBuffer->getHandle();
  354. bufferInfo.offset = offset;
  355. bufferInfo.range = localUniformData.size();
  356. VkWriteDescriptorSet uniformWrite{};
  357. uniformWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
  358. uniformWrite.dstSet = currentDescriptorSet;
  359. uniformWrite.dstBinding = localUniformLocation;
  360. uniformWrite.dstArrayElement = 0;
  361. uniformWrite.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
  362. uniformWrite.descriptorCount = 1;
  363. uniformWrite.pBufferInfo = &bufferInfo;
  364. vkUpdateDescriptorSets(device, 1, &uniformWrite, 0, nullptr);
  365. currentUsedUniformStreamBuffersCount++;
  366. }
  367. for (const auto &u : uniformInfos)
  368. {
  369. auto &info = u.second;
  370. if (usesLocalUniformData(&info))
  371. continue;
  372. if (info.baseType == UNIFORM_SAMPLER || info.baseType == UNIFORM_STORAGETEXTURE)
  373. {
  374. bool isSampler = info.baseType == UNIFORM_SAMPLER;
  375. std::vector<VkDescriptorImageInfo> imageInfos;
  376. for (int i = 0; i < info.count; i++)
  377. {
  378. auto vkTexture = dynamic_cast<Texture*>(info.textures[i]);
  379. if (vkTexture == nullptr)
  380. throw love::Exception("uniform variable %s is not set.", info.name.c_str());
  381. VkDescriptorImageInfo imageInfo{};
  382. imageInfo.imageLayout = vkTexture->getImageLayout();
  383. imageInfo.imageView = (VkImageView)vkTexture->getRenderTargetHandle();
  384. if (isSampler)
  385. imageInfo.sampler = (VkSampler)vkTexture->getSamplerHandle();
  386. imageInfos.push_back(imageInfo);
  387. }
  388. VkWriteDescriptorSet write{};
  389. write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
  390. write.dstSet = currentDescriptorSet;
  391. write.dstBinding = info.location;
  392. write.dstArrayElement = 0;
  393. if (isSampler)
  394. write.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
  395. else
  396. write.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
  397. write.descriptorCount = static_cast<uint32_t>(info.count);
  398. write.pImageInfo = imageInfos.data();
  399. vkUpdateDescriptorSets(device, 1, &write, 0, nullptr);
  400. }
  401. if (info.baseType == UNIFORM_STORAGEBUFFER)
  402. {
  403. VkWriteDescriptorSet write{};
  404. write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
  405. write.dstSet = currentDescriptorSet;
  406. write.dstBinding = info.location;
  407. write.dstArrayElement = 0;
  408. write.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
  409. write.descriptorCount = info.count;
  410. std::vector<VkDescriptorBufferInfo> bufferInfos;
  411. for (int i = 0; i < info.count; i++)
  412. {
  413. if (info.buffers[i] == nullptr)
  414. throw love::Exception("uniform variable %s is not set.", info.name.c_str());
  415. VkDescriptorBufferInfo bufferInfo{};
  416. bufferInfo.buffer = (VkBuffer)info.buffers[i]->getHandle();;
  417. bufferInfo.offset = 0;
  418. bufferInfo.range = info.buffers[i]->getSize();
  419. bufferInfos.push_back(bufferInfo);
  420. }
  421. write.pBufferInfo = bufferInfos.data();
  422. vkUpdateDescriptorSets(device, 1, &write, 0, nullptr);
  423. }
  424. if (info.baseType == UNIFORM_TEXELBUFFER)
  425. {
  426. VkWriteDescriptorSet write{};
  427. write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
  428. write.dstSet = currentDescriptorSet;
  429. write.dstBinding = info.location;
  430. write.dstArrayElement = 0;
  431. write.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER;
  432. write.descriptorCount = info.count;
  433. std::vector<VkBufferView> bufferViews;
  434. for (int i = 0; i < info.count; i++)
  435. {
  436. if (info.buffers[i] == nullptr)
  437. throw love::Exception("uniform variable %s is not set.", info.name.c_str());
  438. bufferViews.push_back((VkBufferView)info.buffers[i]->getTexelBufferHandle());
  439. }
  440. write.pTexelBufferView = bufferViews.data();
  441. vkUpdateDescriptorSets(device, 1, &write, 0, nullptr);
  442. }
  443. }
  444. vkCmdBindDescriptorSets(commandBuffer, bindPoint, pipelineLayout, 0, 1, &currentDescriptorSet, 0, nullptr);
  445. currentUsedDescriptorSetsCount++;
  446. if (currentUsedDescriptorSetsCount >= static_cast<uint32_t>(descriptorSetsVector.at(currentFrame).size()))
  447. descriptorSetsVector.at(currentFrame).push_back(allocateDescriptorSet());
  448. currentDescriptorSet = descriptorSetsVector.at(currentFrame).at(currentUsedDescriptorSetsCount);
  449. }
  450. Shader::~Shader()
  451. {
  452. unloadVolatile();
  453. }
  454. void Shader::attach()
  455. {
  456. if (!isCompute)
  457. {
  458. if (Shader::current != this)
  459. {
  460. Graphics::flushBatchedDrawsGlobal();
  461. Shader::current = this;
  462. Vulkan::shaderSwitch();
  463. }
  464. }
  465. else
  466. vgfx->setComputeShader(this);
  467. }
  468. int Shader::getVertexAttributeIndex(const std::string &name)
  469. {
  470. auto it = attributes.find(name);
  471. return it == attributes.end() ? -1 : it->second;
  472. }
  473. const Shader::UniformInfo *Shader::getUniformInfo(const std::string &name) const
  474. {
  475. return &uniformInfos.at(name);
  476. }
  477. const Shader::UniformInfo *Shader::getUniformInfo(BuiltinUniform builtin) const
  478. {
  479. return builtinUniformInfo[builtin];
  480. }
  481. void Shader::updateUniform(const UniformInfo *info, int count)
  482. {
  483. if (current == this)
  484. Graphics::flushBatchedDrawsGlobal();
  485. if (usesLocalUniformData(info))
  486. memcpy(localUniformData.data(), localUniformStagingData.data(), localUniformStagingData.size());
  487. }
  488. void Shader::sendTextures(const UniformInfo *info, graphics::Texture **textures, int count)
  489. {
  490. for (int i = 0; i < count; i++)
  491. {
  492. auto oldTexture = info->textures[i];
  493. info->textures[i] = textures[i];
  494. info->textures[i]->retain();
  495. if (oldTexture)
  496. oldTexture->release();
  497. }
  498. }
  499. void Shader::sendBuffers(const UniformInfo *info, love::graphics::Buffer **buffers, int count)
  500. {
  501. for (int i = 0; i < count; i++)
  502. {
  503. auto oldBuffer = info->buffers[i];
  504. info->buffers[i] = buffers[i];
  505. info->buffers[i]->retain();
  506. if (oldBuffer)
  507. oldBuffer->release();
  508. }
  509. }
  510. void Shader::calculateUniformBufferSizeAligned()
  511. {
  512. auto minAlignment = vgfx->getMinUniformBufferOffsetAlignment();
  513. size_t size = localUniformStagingData.size();
  514. auto factor = static_cast<VkDeviceSize>(std::ceil(
  515. static_cast<float>(size) / static_cast<float>(minAlignment)
  516. ));
  517. uniformBufferSizeAligned = factor * minAlignment;
  518. }
  519. void Shader::buildLocalUniforms(spirv_cross::Compiler &comp, const spirv_cross::SPIRType &type, size_t baseoff, const std::string &basename)
  520. {
  521. using namespace spirv_cross;
  522. const auto &membertypes = type.member_types;
  523. for (size_t uindex = 0; uindex < membertypes.size(); uindex++)
  524. {
  525. const auto &memberType = comp.get_type(membertypes[uindex]);
  526. size_t memberSize = comp.get_declared_struct_member_size(type, uindex);
  527. size_t offset = baseoff + comp.type_struct_member_offset(type, uindex);
  528. std::string name = basename + comp.get_member_name(type.self, uindex);
  529. switch (memberType.basetype)
  530. {
  531. case SPIRType::Struct:
  532. name += ".";
  533. buildLocalUniforms(comp, memberType, offset, name);
  534. continue;
  535. case SPIRType::Int:
  536. case SPIRType::UInt:
  537. case SPIRType::Float:
  538. break;
  539. default:
  540. continue;
  541. }
  542. UniformInfo u{};
  543. u.name = name;
  544. u.dataSize = memberSize;
  545. u.count = memberType.array.empty() ? 1 : memberType.array[0];
  546. u.components = 1;
  547. u.data = localUniformStagingData.data() + offset;
  548. if (memberType.columns == 1)
  549. {
  550. if (memberType.basetype == SPIRType::Int)
  551. u.baseType = UNIFORM_INT;
  552. else if (memberType.basetype == SPIRType::UInt)
  553. u.baseType = UNIFORM_UINT;
  554. else
  555. u.baseType = UNIFORM_FLOAT;
  556. u.components = memberType.vecsize;
  557. }
  558. else
  559. {
  560. u.baseType = UNIFORM_MATRIX;
  561. u.matrix.rows = memberType.vecsize;
  562. u.matrix.columns = memberType.columns;
  563. }
  564. const auto &reflectionIt = validationReflection.localUniforms.find(u.name);
  565. if (reflectionIt != validationReflection.localUniforms.end())
  566. {
  567. const auto &localUniform = reflectionIt->second;
  568. const auto &values = localUniform.initializerValues;
  569. if (!values.empty())
  570. memcpy(
  571. u.data,
  572. values.data(),
  573. std::min(u.dataSize, values.size() * sizeof(LocalUniformValue)));
  574. }
  575. uniformInfos[u.name] = u;
  576. BuiltinUniform builtin = BUILTIN_MAX_ENUM;
  577. if (getConstant(u.name.c_str(), builtin))
  578. {
  579. if (builtin == BUILTIN_UNIFORMS_PER_DRAW)
  580. builtinUniformDataOffset = offset;
  581. builtinUniformInfo[builtin] = &uniformInfos[u.name];
  582. }
  583. }
  584. }
  585. void Shader::compileShaders()
  586. {
  587. using namespace glslang;
  588. using namespace spirv_cross;
  589. std::vector<std::unique_ptr<TShader>> glslangShaders;
  590. auto program = std::make_unique<TProgram>();
  591. const auto &enabledExtensions = vgfx->getEnabledOptionalDeviceExtensions();
  592. for (int i = 0; i < SHADERSTAGE_MAX_ENUM; i++)
  593. {
  594. if (!stages[i])
  595. continue;
  596. auto stage = (ShaderStageType)i;
  597. if (stage == SHADERSTAGE_COMPUTE)
  598. isCompute = true;
  599. auto glslangShaderStage = getGlslShaderType(stage);
  600. auto tshader = std::make_unique<TShader>(glslangShaderStage);
  601. tshader->setEnvInput(EShSourceGlsl, glslangShaderStage, EShClientVulkan, 450);
  602. tshader->setEnvClient(EShClientVulkan, EShTargetVulkan_1_2);
  603. if (enabledExtensions.spirv14)
  604. tshader->setEnvTarget(EshTargetSpv, EShTargetSpv_1_4);
  605. else
  606. tshader->setEnvTarget(EshTargetSpv, EShTargetSpv_1_0);
  607. tshader->setAutoMapLocations(true);
  608. tshader->setAutoMapBindings(true);
  609. tshader->setEnvInputVulkanRulesRelaxed();
  610. tshader->setGlobalUniformBinding(0);
  611. tshader->setGlobalUniformSet(0);
  612. auto &glsl = stages[i]->getSource();
  613. const char *csrc = glsl.c_str();
  614. const int sourceLength = static_cast<int>(glsl.length());
  615. tshader->setStringsWithLengths(&csrc, &sourceLength, 1);
  616. int defaultVersion = 450;
  617. EProfile defaultProfile = ECoreProfile;
  618. bool forceDefault = false;
  619. bool forwardCompat = true;
  620. if (!tshader->parse(&defaultTBuiltInResource, defaultVersion, defaultProfile, forceDefault, forwardCompat, EShMsgSuppressWarnings))
  621. {
  622. const char *stageName = "unknown";
  623. ShaderStage::getConstant(stage, stageName);
  624. std::string err = "Error parsing " + std::string(stageName) + " shader:\n\n"
  625. + std::string(tshader->getInfoLog()) + "\n"
  626. + std::string(tshader->getInfoDebugLog());
  627. throw love::Exception("%s", err.c_str());
  628. }
  629. program->addShader(tshader.get());
  630. glslangShaders.push_back(std::move(tshader));
  631. }
  632. if (!program->link(EShMsgDefault))
  633. throw love::Exception("link failed! %s\n", program->getInfoLog());
  634. if (!program->mapIO())
  635. throw love::Exception("mapIO failed");
  636. uniformInfos.clear();
  637. BindingMapper bindingMapper;
  638. for (int i = 0; i < SHADERSTAGE_MAX_ENUM; i++)
  639. {
  640. auto shaderStage = (ShaderStageType)i;
  641. auto glslangStage = getGlslShaderType(shaderStage);
  642. auto intermediate = program->getIntermediate(glslangStage);
  643. if (intermediate == nullptr)
  644. continue;
  645. spv::SpvBuildLogger logger;
  646. glslang::SpvOptions opt;
  647. opt.validate = true;
  648. std::vector<uint32_t> spirv;
  649. GlslangToSpv(*intermediate, spirv, &logger, &opt);
  650. spirv_cross::CompilerGLSL comp(spirv);
  651. // we only care about variables that are actually getting used.
  652. auto active = comp.get_active_interface_variables();
  653. auto shaderResources = comp.get_shader_resources(active);
  654. comp.set_enabled_interface_variables(std::move(active));
  655. for (const auto &resource : shaderResources.uniform_buffers)
  656. {
  657. if (resource.name == "gl_DefaultUniformBlock")
  658. {
  659. const auto& type = comp.get_type(resource.base_type_id);
  660. size_t defaultUniformBlockSize = comp.get_declared_struct_size(type);
  661. localUniformStagingData.resize(defaultUniformBlockSize);
  662. localUniformData.resize(defaultUniformBlockSize);
  663. localUniformLocation = bindingMapper(comp, spirv, resource.name, resource.id);
  664. memset(localUniformStagingData.data(), 0, defaultUniformBlockSize);
  665. memset(localUniformData.data(), 0, defaultUniformBlockSize);
  666. std::string basename("");
  667. buildLocalUniforms(comp, type, 0, basename);
  668. memcpy(localUniformData.data(), localUniformStagingData.data(), localUniformStagingData.size());
  669. }
  670. else
  671. throw love::Exception("unimplemented: non default uniform blocks.");
  672. }
  673. for (const auto &r : shaderResources.sampled_images)
  674. {
  675. const SPIRType &basetype = comp.get_type(r.base_type_id);
  676. const SPIRType &type = comp.get_type(r.type_id);
  677. const SPIRType &imagetype = comp.get_type(basetype.image.type);
  678. graphics::Shader::UniformInfo info;
  679. info.location = bindingMapper(comp, spirv, r.name, r.id);
  680. info.baseType = UNIFORM_SAMPLER;
  681. info.name = r.name;
  682. info.count = type.array.empty() ? 1 : type.array[0];
  683. info.isDepthSampler = type.image.depth;
  684. info.components = 1;
  685. switch (imagetype.basetype)
  686. {
  687. case SPIRType::Float:
  688. info.dataBaseType = DATA_BASETYPE_FLOAT;
  689. break;
  690. case SPIRType::Int:
  691. info.dataBaseType = DATA_BASETYPE_INT;
  692. break;
  693. case SPIRType::UInt:
  694. info.dataBaseType = DATA_BASETYPE_UINT;
  695. break;
  696. default:
  697. break;
  698. }
  699. switch (basetype.image.dim)
  700. {
  701. case spv::Dim2D:
  702. info.textureType = basetype.image.arrayed ? TEXTURE_2D_ARRAY : TEXTURE_2D;
  703. info.textures = new love::graphics::Texture *[info.count];
  704. break;
  705. case spv::Dim3D:
  706. info.textureType = TEXTURE_VOLUME;
  707. info.textures = new love::graphics::Texture *[info.count];
  708. break;
  709. case spv::DimCube:
  710. if (basetype.image.arrayed) {
  711. throw love::Exception("cubemap arrays are not currently supported");
  712. }
  713. info.textureType = TEXTURE_CUBE;
  714. info.textures = new love::graphics::Texture *[info.count];
  715. break;
  716. case spv::DimBuffer:
  717. info.baseType = UNIFORM_TEXELBUFFER;
  718. info.buffers = new love::graphics::Buffer *[info.count];
  719. break;
  720. default:
  721. throw love::Exception("unknown dim");
  722. }
  723. if (info.baseType == UNIFORM_TEXELBUFFER)
  724. {
  725. for (int i = 0; i < info.count; i++)
  726. info.buffers[i] = nullptr;
  727. }
  728. else
  729. {
  730. for (int i = 0; i < info.count; i++)
  731. {
  732. info.textures[i] = nullptr;
  733. }
  734. }
  735. uniformInfos[r.name] = info;
  736. BuiltinUniform builtin;
  737. if (getConstant(r.name.c_str(), builtin))
  738. builtinUniformInfo[builtin] = &uniformInfos[info.name];
  739. }
  740. for (const auto &r : shaderResources.storage_buffers)
  741. {
  742. const auto &type = comp.get_type(r.type_id);
  743. UniformInfo u{};
  744. u.baseType = UNIFORM_STORAGEBUFFER;
  745. u.components = 1;
  746. u.name = r.name;
  747. u.count = type.array.empty() ? 1 : type.array[0];
  748. if (!fillUniformReflectionData(u))
  749. continue;
  750. u.location = bindingMapper(comp, spirv, r.name, r.id);
  751. u.buffers = new love::graphics::Buffer *[u.count];
  752. for (int i = 0; i < u.count; i++)
  753. u.buffers[i] = nullptr;
  754. uniformInfos[u.name] = u;
  755. }
  756. for (const auto &r : shaderResources.storage_images)
  757. {
  758. const auto &type = comp.get_type(r.type_id);
  759. UniformInfo u{};
  760. u.baseType = UNIFORM_STORAGETEXTURE;
  761. u.components = 1;
  762. u.name = r.name;
  763. u.count = type.array.empty() ? 1 : type.array[0];
  764. if (!fillUniformReflectionData(u))
  765. continue;
  766. u.textures = new love::graphics::Texture *[u.count];
  767. u.location = bindingMapper(comp, spirv, r.name, r.id);
  768. for (int i = 0; i < u.count; i++)
  769. u.textures[i] = nullptr;
  770. uniformInfos[u.name] = u;
  771. }
  772. if (shaderStage == SHADERSTAGE_VERTEX)
  773. {
  774. int nextAttributeIndex = ATTRIB_MAX_ENUM;
  775. for (const auto &r : shaderResources.stage_inputs)
  776. {
  777. int index;
  778. BuiltinVertexAttribute builtinAttribute;
  779. if (graphics::getConstant(r.name.c_str(), builtinAttribute))
  780. index = (int)builtinAttribute;
  781. else
  782. index = nextAttributeIndex++;
  783. uint32_t locationOffset;
  784. if (!comp.get_binary_offset_for_decoration(r.id, spv::DecorationLocation, locationOffset))
  785. throw love::Exception("could not get binary offset for location");
  786. spirv[locationOffset] = (uint32_t)index;
  787. attributes[r.name] = index;
  788. }
  789. }
  790. VkShaderModuleCreateInfo createInfo{};
  791. createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
  792. createInfo.codeSize = spirv.size() * sizeof(uint32_t);
  793. createInfo.pCode = spirv.data();
  794. VkShaderModule shaderModule;
  795. if (vkCreateShaderModule(device, &createInfo, nullptr, &shaderModule) != VK_SUCCESS)
  796. throw love::Exception("failed to create shader module");
  797. shaderModules.push_back(shaderModule);
  798. VkPipelineShaderStageCreateInfo shaderStageInfo{};
  799. shaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
  800. shaderStageInfo.stage = getStageBit((ShaderStageType)i);
  801. shaderStageInfo.module = shaderModule;
  802. shaderStageInfo.pName = "main";
  803. shaderStages.push_back(shaderStageInfo);
  804. }
  805. }
  806. void Shader::createDescriptorSetLayout()
  807. {
  808. std::vector<VkDescriptorSetLayoutBinding> bindings;
  809. VkShaderStageFlags stageFlags;
  810. if (isCompute)
  811. stageFlags = VK_SHADER_STAGE_COMPUTE_BIT;
  812. else
  813. stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT;
  814. for (auto const &entry : uniformInfos)
  815. {
  816. auto type = Vulkan::getDescriptorType(entry.second.baseType);
  817. if (type != VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER)
  818. {
  819. VkDescriptorSetLayoutBinding layoutBinding{};
  820. layoutBinding.binding = entry.second.location;
  821. layoutBinding.descriptorType = type;
  822. layoutBinding.descriptorCount = entry.second.count;
  823. layoutBinding.stageFlags = stageFlags;
  824. bindings.push_back(layoutBinding);
  825. }
  826. }
  827. if (!localUniformStagingData.empty())
  828. {
  829. VkDescriptorSetLayoutBinding uniformBinding{};
  830. uniformBinding.binding = localUniformLocation;
  831. uniformBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
  832. uniformBinding.descriptorCount = 1;
  833. uniformBinding.stageFlags = stageFlags;
  834. bindings.push_back(uniformBinding);
  835. }
  836. VkDescriptorSetLayoutCreateInfo layoutInfo{};
  837. layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
  838. layoutInfo.bindingCount = static_cast<uint32_t>(bindings.size());
  839. layoutInfo.pBindings = bindings.data();
  840. if (vkCreateDescriptorSetLayout(device, &layoutInfo, nullptr, &descriptorSetLayout) != VK_SUCCESS)
  841. throw love::Exception("failed to create descriptor set layout");
  842. }
  843. void Shader::createPipelineLayout()
  844. {
  845. VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
  846. pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
  847. pipelineLayoutInfo.setLayoutCount = 1;
  848. pipelineLayoutInfo.pSetLayouts = &descriptorSetLayout;
  849. pipelineLayoutInfo.pushConstantRangeCount = 0;
  850. if (vkCreatePipelineLayout(device, &pipelineLayoutInfo, nullptr, &pipelineLayout) != VK_SUCCESS)
  851. throw love::Exception("failed to create pipeline layout");
  852. if (isCompute)
  853. {
  854. assert(shaderStages.size() == 1);
  855. VkComputePipelineCreateInfo computeInfo{};
  856. computeInfo.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO;
  857. computeInfo.stage = shaderStages.at(0);
  858. computeInfo.layout = pipelineLayout;
  859. if (vkCreateComputePipelines(device, VK_NULL_HANDLE, 1, &computeInfo, nullptr, &computePipeline) != VK_SUCCESS)
  860. throw love::Exception("failed to create compute pipeline");
  861. }
  862. }
  863. void Shader::createDescriptorPoolSizes()
  864. {
  865. if (!localUniformData.empty())
  866. {
  867. VkDescriptorPoolSize size{};
  868. size.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
  869. size.descriptorCount = 1;
  870. descriptorPoolSizes.push_back(size);
  871. }
  872. for (const auto &entry : uniformInfos)
  873. {
  874. VkDescriptorPoolSize size{};
  875. auto type = Vulkan::getDescriptorType(entry.second.baseType);
  876. if (type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) {
  877. continue;
  878. }
  879. size.type = type;
  880. size.descriptorCount = 1;
  881. descriptorPoolSizes.push_back(size);
  882. }
  883. }
  884. void Shader::createStreamBuffers()
  885. {
  886. size_t size = STREAMBUFFER_DEFAULT_SIZE * uniformBufferSizeAligned;
  887. if (size > 0)
  888. streamBuffers.push_back(new StreamBuffer(vgfx, BUFFERUSAGE_UNIFORM, size));
  889. }
  890. void Shader::setVideoTextures(graphics::Texture *ytexture, graphics::Texture *cbtexture, graphics::Texture *crtexture)
  891. {
  892. std::array<graphics::Texture*, 3> textures = {
  893. ytexture, cbtexture, crtexture
  894. };
  895. std::array<BuiltinUniform, 3> builtIns = {
  896. BUILTIN_TEXTURE_VIDEO_Y,
  897. BUILTIN_TEXTURE_VIDEO_CB,
  898. BUILTIN_TEXTURE_VIDEO_CR,
  899. };
  900. static_assert(textures.size() == builtIns.size(), "expected number of textures to be the same");
  901. for (size_t i = 0; i < textures.size(); i++)
  902. {
  903. if (builtinUniformInfo[builtIns[i]] != nullptr)
  904. {
  905. textures[i]->retain();
  906. if (builtinUniformInfo[builtIns[i]]->textures[0])
  907. builtinUniformInfo[builtIns[i]]->textures[0]->release();
  908. builtinUniformInfo[builtIns[i]]->textures[0] = textures[i];
  909. }
  910. }
  911. }
  912. bool Shader::hasUniform(const std::string &name) const
  913. {
  914. return uniformInfos.find(name) != uniformInfos.end();
  915. }
  916. void Shader::setMainTex(graphics::Texture *texture)
  917. {
  918. if (builtinUniformInfo[BUILTIN_TEXTURE_MAIN] != nullptr)
  919. {
  920. texture->retain();
  921. if (builtinUniformInfo[BUILTIN_TEXTURE_MAIN]->textures[0])
  922. builtinUniformInfo[BUILTIN_TEXTURE_MAIN]->textures[0]->release();
  923. builtinUniformInfo[BUILTIN_TEXTURE_MAIN]->textures[0] = texture;
  924. }
  925. }
  926. VkDescriptorSet Shader::allocateDescriptorSet()
  927. {
  928. if (freeDescriptorSets.empty())
  929. {
  930. VkDescriptorPoolCreateInfo createInfo{};
  931. createInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
  932. createInfo.maxSets = DESCRIPTOR_POOL_SIZE;
  933. createInfo.poolSizeCount = static_cast<uint32_t>(descriptorPoolSizes.size());
  934. createInfo.pPoolSizes = descriptorPoolSizes.data();
  935. VkDescriptorPool pool;
  936. if (vkCreateDescriptorPool(device, &createInfo, nullptr, &pool) != VK_SUCCESS)
  937. throw love::Exception("failed to create descriptor pool");
  938. descriptorPools.push_back(pool);
  939. std::vector<VkDescriptorSetLayout> layouts(DESCRIPTOR_POOL_SIZE, descriptorSetLayout);
  940. VkDescriptorSetAllocateInfo allocInfo{};
  941. allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
  942. allocInfo.descriptorPool = pool;
  943. allocInfo.descriptorSetCount = DESCRIPTOR_POOL_SIZE;
  944. allocInfo.pSetLayouts = layouts.data();
  945. std::vector<VkDescriptorSet> descriptorSet;
  946. descriptorSet.resize(DESCRIPTOR_POOL_SIZE);
  947. VkResult result = vkAllocateDescriptorSets(device, &allocInfo, descriptorSet.data());
  948. if (result != VK_SUCCESS)
  949. throw love::Exception("failed to allocate descriptor set");
  950. for (const auto ds : descriptorSet)
  951. freeDescriptorSets.push(ds);
  952. }
  953. auto ds = freeDescriptorSets.front();
  954. freeDescriptorSets.pop();
  955. return ds;
  956. }
  957. } // vulkan
  958. } // graphics
  959. } // love