BsVulkanGpuParams.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsVulkanGpuParams.h"
  4. #include "BsVulkanUtility.h"
  5. #include "BsVulkanRenderAPI.h"
  6. #include "BsVulkanDevice.h"
  7. #include "BsVulkanGpuParamBlockBuffer.h"
  8. #include "BsVulkanGpuBuffer.h"
  9. #include "BsVulkanTexture.h"
  10. #include "BsVulkanDescriptorSet.h"
  11. #include "BsVulkanSamplerState.h"
  12. #include "BsGpuParamDesc.h"
  13. namespace BansheeEngine
  14. {
  15. VulkanGpuParams::VulkanGpuParams(const GPU_PARAMS_DESC& desc, GpuDeviceFlags deviceMask)
  16. : GpuParamsCore(desc, deviceMask), mPerDeviceData{}, mNumDevices(0), mDeviceMask(deviceMask), mData(nullptr)
  17. , mSetsDirty(nullptr)
  18. {
  19. // Generate all required bindings
  20. UINT32 numBindings = 0;
  21. UINT32 numSets = 0;
  22. UINT32 numElementTypes = (UINT32)ElementType::Count;
  23. for (UINT32 i = 0; i < numElementTypes; i++)
  24. {
  25. numBindings += mNumElements[i];
  26. numSets = std::max(numSets, mNumSets[i]);
  27. }
  28. UINT32 bindingsPerSetBytes = sizeof(UINT32) * numSets;
  29. UINT32* bindingsPerSet = (UINT32*)bs_stack_alloc(bindingsPerSetBytes);
  30. UINT32 bindingsSize = sizeof(VkDescriptorSetLayoutBinding) * numBindings;
  31. VkDescriptorSetLayoutBinding* bindings = (VkDescriptorSetLayoutBinding*)bs_stack_alloc(bindingsSize);
  32. memset(bindings, 0, bindingsSize);
  33. UINT32 globalBindingIdx = 0;
  34. for (UINT32 i = 0; i < numSets; i++)
  35. {
  36. bindingsPerSet[i] = 0;
  37. for (UINT32 j = 0; j < numElementTypes; j++)
  38. {
  39. if (i >= mNumSets[j])
  40. continue;
  41. UINT32 start = mOffsets[j][i];
  42. UINT32 end;
  43. if (i < (mNumSets[j] - 1))
  44. end = mOffsets[j][i + 1];
  45. else
  46. end = mNumElements[j];
  47. UINT32 elementsInSet = end - start;
  48. for (UINT32 k = 0; k < elementsInSet; k++)
  49. {
  50. VkDescriptorSetLayoutBinding& binding = bindings[globalBindingIdx + k];
  51. binding.binding = bindingsPerSet[i] + k;
  52. }
  53. globalBindingIdx += elementsInSet;
  54. bindingsPerSet[i] += elementsInSet;
  55. }
  56. }
  57. UINT32* bindingOffsets = (UINT32*)bs_stack_alloc(sizeof(UINT32) * numSets);
  58. if (numSets > 0)
  59. {
  60. bindingOffsets[0] = 0;
  61. for (UINT32 i = 1; i < numSets; i++)
  62. bindingOffsets[i] = bindingsPerSet[i - 1];
  63. }
  64. VkShaderStageFlags stageFlagsLookup[6];
  65. stageFlagsLookup[GPT_VERTEX_PROGRAM] = VK_SHADER_STAGE_VERTEX_BIT;
  66. stageFlagsLookup[GPT_HULL_PROGRAM] = VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT;
  67. stageFlagsLookup[GPT_DOMAIN_PROGRAM] = VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT;
  68. stageFlagsLookup[GPT_GEOMETRY_PROGRAM] = VK_SHADER_STAGE_GEOMETRY_BIT;
  69. stageFlagsLookup[GPT_FRAGMENT_PROGRAM] = VK_SHADER_STAGE_FRAGMENT_BIT;
  70. stageFlagsLookup[GPT_COMPUTE_PROGRAM] = VK_SHADER_STAGE_COMPUTE_BIT;
  71. UINT32 numParamDescs = sizeof(mParamDescs) / sizeof(mParamDescs[0]);
  72. for (UINT32 i = 0; i < numParamDescs; i++)
  73. {
  74. const SPtr<GpuParamDesc>& paramDesc = mParamDescs[i];
  75. if (paramDesc == nullptr)
  76. continue;
  77. auto setUpBindings = [&](auto& params, VkDescriptorType descType)
  78. {
  79. for (auto& entry : params)
  80. {
  81. UINT32 bindingIdx = bindingOffsets[entry.second.set] + entry.second.slot;
  82. VkDescriptorSetLayoutBinding& binding = bindings[bindingIdx];
  83. binding.descriptorCount = 1;
  84. binding.stageFlags |= stageFlagsLookup[i];
  85. binding.descriptorType = descType;
  86. }
  87. };
  88. // Note: Assuming all textures and samplers use the same set/slot combination, and that they're combined
  89. setUpBindings(paramDesc->paramBlocks, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
  90. setUpBindings(paramDesc->textures, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
  91. setUpBindings(paramDesc->loadStoreTextures, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE);
  92. setUpBindings(paramDesc->buffers, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
  93. setUpBindings(paramDesc->samplers, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
  94. }
  95. VulkanRenderAPI& rapi = static_cast<VulkanRenderAPI&>(RenderAPICore::instance());
  96. VulkanDevice* devices[BS_MAX_LINKED_DEVICES];
  97. VulkanUtility::getDevices(rapi, deviceMask, devices);
  98. // Allocate layouts per-device
  99. for (UINT32 i = 0; i < BS_MAX_LINKED_DEVICES; i++)
  100. {
  101. if (devices[i] == nullptr)
  102. break;
  103. mNumDevices++;
  104. }
  105. // Note: I'm assuming a single WriteInfo per binding, but if arrays sizes larger than 1 are eventually supported
  106. // I'll need to adjust the code.
  107. UINT32 setsDirtyBytes = sizeof(bool) * numSets;
  108. UINT32 perSetBytes = sizeof(PerSetData) * numSets;
  109. UINT32 writeSetInfosBytes = sizeof(VkWriteDescriptorSet) * numBindings;
  110. UINT32 writeInfosBytes = sizeof(WriteInfo) * numBindings;
  111. mData = (UINT8*)bs_alloc(setsDirtyBytes + (perSetBytes + writeSetInfosBytes + writeInfosBytes) * mNumDevices);
  112. UINT8* dataIter = mData;
  113. mSetsDirty = (bool*)dataIter;
  114. memset(mSetsDirty, 1, setsDirtyBytes);
  115. dataIter += setsDirtyBytes;
  116. for(UINT32 i = 0; i < mNumDevices; i++)
  117. {
  118. mPerDeviceData[i].numSets = numSets;
  119. mPerDeviceData[i].perSetData = (PerSetData*)dataIter;
  120. dataIter += sizeof(perSetBytes);
  121. VulkanDescriptorManager& descManager = devices[i]->getDescriptorManager();
  122. UINT32 bindingOffset = 0;
  123. for (UINT32 j = 0; j < numSets; j++)
  124. {
  125. UINT32 numBindingsPerSet = bindingsPerSet[j];
  126. PerSetData& perSetData = mPerDeviceData[i].perSetData[j];
  127. perSetData.writeSetInfos = (VkWriteDescriptorSet*)dataIter;
  128. dataIter += sizeof(VkWriteDescriptorSet) * numBindingsPerSet;
  129. perSetData.writeInfos = (WriteInfo*)dataIter;
  130. dataIter += sizeof(WriteInfo) * numBindingsPerSet;
  131. VkDescriptorSetLayoutBinding* perSetBindings = &bindings[bindingOffset];
  132. perSetData.layout = descManager.getLayout(perSetBindings, numBindingsPerSet);
  133. perSetData.set = descManager.createSet(perSetData.layout);
  134. perSetData.numElements = numBindingsPerSet;
  135. for(UINT32 k = 0; k < numBindingsPerSet; k++)
  136. {
  137. // Note: Instead of using one structure per binding, it's possible to update multiple at once
  138. // by specifying larger descriptorCount, if they all share type and shader stages.
  139. VkWriteDescriptorSet& writeSetInfo = perSetData.writeSetInfos[k];
  140. writeSetInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
  141. writeSetInfo.pNext = nullptr;
  142. writeSetInfo.dstSet = VK_NULL_HANDLE; // TODO
  143. writeSetInfo.dstBinding = perSetBindings[k].binding;
  144. writeSetInfo.dstArrayElement = 0;
  145. writeSetInfo.descriptorCount = perSetBindings[k].descriptorCount;
  146. writeSetInfo.descriptorType = perSetBindings[k].descriptorType;
  147. writeSetInfo.pTexelBufferView = nullptr;
  148. bool isImage = writeSetInfo.descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER ||
  149. writeSetInfo.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE ||
  150. writeSetInfo.descriptorType == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE;
  151. if(isImage)
  152. {
  153. bool isLoadStore = writeSetInfo.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
  154. VkDescriptorImageInfo& imageInfo = perSetData.writeInfos[k].image;
  155. imageInfo.sampler = VK_NULL_HANDLE;
  156. imageInfo.imageView = VK_NULL_HANDLE;
  157. imageInfo.imageLayout = isLoadStore ? VK_IMAGE_LAYOUT_GENERAL : VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
  158. writeSetInfo.pImageInfo = &imageInfo;
  159. writeSetInfo.pBufferInfo = nullptr;
  160. }
  161. else
  162. {
  163. VkDescriptorBufferInfo& bufferInfo = perSetData.writeInfos[k].buffer;
  164. bufferInfo.buffer = VK_NULL_HANDLE;
  165. bufferInfo.offset = 0;
  166. bufferInfo.range = VK_WHOLE_SIZE;
  167. writeSetInfo.pBufferInfo = &bufferInfo;
  168. writeSetInfo.pImageInfo = nullptr;
  169. }
  170. }
  171. bindingOffset += numBindingsPerSet;
  172. }
  173. }
  174. bs_stack_free(bindingOffsets);
  175. bs_stack_free(bindings);
  176. bs_stack_free(bindingsPerSet);
  177. }
  178. VulkanGpuParams::~VulkanGpuParams()
  179. {
  180. for (UINT32 i = 0; i < mNumDevices; i++)
  181. {
  182. for (UINT32 j = 0; j < mPerDeviceData[i].numSets; j++)
  183. mPerDeviceData[i].perSetData[j].set->destroy();
  184. }
  185. bs_free(mData); // Everything allocated under a single buffer to a single free is enough
  186. }
  187. void VulkanGpuParams::setParamBlockBuffer(UINT32 set, UINT32 slot, const SPtr<GpuParamBlockBufferCore>& paramBlockBuffer)
  188. {
  189. GpuParamsCore::setParamBlockBuffer(set, slot, paramBlockBuffer);
  190. VulkanGpuParamBlockBufferCore* vulkanParamBlockBuffer =
  191. static_cast<VulkanGpuParamBlockBufferCore*>(paramBlockBuffer.get());
  192. VkBuffer buffers[BS_MAX_LINKED_DEVICES];
  193. vulkanParamBlockBuffer->getHandles(mDeviceMask, buffers);
  194. for (UINT32 i = 0; i < mNumDevices; i++)
  195. mPerDeviceData[i].perSetData[set].writeInfos[slot].buffer.buffer = buffers[i];
  196. mSetsDirty[set] = true;
  197. }
  198. void VulkanGpuParams::setTexture(UINT32 set, UINT32 slot, const SPtr<TextureCore>& texture)
  199. {
  200. GpuParamsCore::setTexture(set, slot, texture);
  201. VulkanTextureCore* vulkanTexture = static_cast<VulkanTextureCore*>(texture.get());
  202. VkImageView imageViews[BS_MAX_LINKED_DEVICES];
  203. vulkanTexture->getViews(mDeviceMask, imageViews);
  204. for (UINT32 i = 0; i < mNumDevices; i++)
  205. mPerDeviceData[i].perSetData[set].writeInfos[slot].image.imageView = imageViews[i];
  206. mSetsDirty[set] = true;
  207. }
  208. void VulkanGpuParams::setLoadStoreTexture(UINT32 set, UINT32 slot, const SPtr<TextureCore>& texture,
  209. const TextureSurface& surface)
  210. {
  211. GpuParamsCore::setLoadStoreTexture(set, slot, texture, surface);
  212. VulkanTextureCore* vulkanTexture = static_cast<VulkanTextureCore*>(texture.get());
  213. VkImageView imageViews[BS_MAX_LINKED_DEVICES];
  214. vulkanTexture->getViews(mDeviceMask, imageViews, surface);
  215. for (UINT32 i = 0; i < mNumDevices; i++)
  216. mPerDeviceData[i].perSetData[set].writeInfos[slot].image.imageView = imageViews[i];
  217. mSetsDirty[set] = true;
  218. }
  219. void VulkanGpuParams::setBuffer(UINT32 set, UINT32 slot, const SPtr<GpuBufferCore>& buffer)
  220. {
  221. GpuParamsCore::setBuffer(set, slot, buffer);
  222. VulkanGpuBufferCore* vulkanBuffer = static_cast<VulkanGpuBufferCore*>(buffer.get());
  223. VkBuffer buffers[BS_MAX_LINKED_DEVICES];
  224. vulkanBuffer->getHandles(mDeviceMask, buffers);
  225. for (UINT32 i = 0; i < mNumDevices; i++)
  226. mPerDeviceData[i].perSetData[set].writeInfos[slot].buffer.buffer = buffers[i];
  227. mSetsDirty[set] = true;
  228. }
  229. void VulkanGpuParams::setSamplerState(UINT32 set, UINT32 slot, const SPtr<SamplerStateCore>& sampler)
  230. {
  231. GpuParamsCore::setSamplerState(set, slot, sampler);
  232. VulkanSamplerState* vulkanSampler = static_cast<VulkanSamplerState*>(sampler.get());
  233. VkSampler samplers[BS_MAX_LINKED_DEVICES];
  234. vulkanSampler->getHandles(mDeviceMask, samplers);
  235. for (UINT32 i = 0; i < mNumDevices; i++)
  236. mPerDeviceData[i].perSetData[set].writeInfos[slot].image.sampler = samplers[i];
  237. mSetsDirty[set] = true;
  238. }
  239. void VulkanGpuParams::setLoadStoreSurface(UINT32 set, UINT32 slot, const TextureSurface& surface)
  240. {
  241. GpuParamsCore::setLoadStoreSurface(set, slot, surface);
  242. SPtr<TextureCore> texture = getLoadStoreTexture(set, slot);
  243. if (texture == nullptr)
  244. return;
  245. VulkanTextureCore* vulkanTexture = static_cast<VulkanTextureCore*>(texture.get());
  246. VkImageView imageViews[BS_MAX_LINKED_DEVICES];
  247. vulkanTexture->getViews(mDeviceMask, imageViews, surface);
  248. for (UINT32 i = 0; i < mNumDevices; i++)
  249. mPerDeviceData[i].perSetData[set].writeInfos[slot].image.imageView = imageViews[i];
  250. mSetsDirty[set] = true;
  251. }
  252. }