BsVulkanTexture.cpp 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsVulkanTexture.h"
  4. #include "BsVulkanRenderAPI.h"
  5. #include "BsVulkanDevice.h"
  6. #include "BsVulkanUtility.h"
  7. #include "Managers/BsVulkanCommandBufferManager.h"
  8. #include "BsVulkanHardwareBuffer.h"
  9. #include "CoreThread/BsCoreThread.h"
  10. #include "Profiling/BsRenderStats.h"
  11. #include "Math/BsMath.h"
  12. namespace bs { namespace ct
  13. {
  14. VULKAN_IMAGE_DESC createDesc(VkImage image, VkDeviceMemory memory, VkImageLayout layout, const TextureProperties& props)
  15. {
  16. VULKAN_IMAGE_DESC desc;
  17. desc.image = image;
  18. desc.memory = memory;
  19. desc.type = props.getTextureType();
  20. desc.format = VulkanUtility::getPixelFormat(props.getFormat(), props.isHardwareGammaEnabled());
  21. desc.numFaces = props.getNumFaces();
  22. desc.numMipLevels = props.getNumMipmaps() + 1;
  23. desc.layout = layout;
  24. desc.usage = (UINT32)props.getUsage();
  25. return desc;
  26. }
  27. VulkanImage::VulkanImage(VulkanResourceManager* owner, VkImage image, VkDeviceMemory memory, VkImageLayout layout,
  28. const TextureProperties& props, bool ownsImage)
  29. : VulkanImage(owner, createDesc(image, memory, layout, props), ownsImage)
  30. { }
  31. VulkanImage::VulkanImage(VulkanResourceManager* owner, const VULKAN_IMAGE_DESC& desc, bool ownsImage)
  32. : VulkanResource(owner, false), mImage(desc.image), mMemory(desc.memory), mFramebufferMainView(VK_NULL_HANDLE)
  33. , mUsage(desc.usage), mOwnsImage(ownsImage), mNumFaces(desc.numFaces), mNumMipLevels(desc.numMipLevels)
  34. {
  35. mImageViewCI.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
  36. mImageViewCI.pNext = nullptr;
  37. mImageViewCI.flags = 0;
  38. mImageViewCI.image = desc.image;
  39. mImageViewCI.format = desc.format;
  40. mImageViewCI.components = {
  41. VK_COMPONENT_SWIZZLE_R,
  42. VK_COMPONENT_SWIZZLE_G,
  43. VK_COMPONENT_SWIZZLE_B,
  44. VK_COMPONENT_SWIZZLE_A
  45. };
  46. switch (desc.type)
  47. {
  48. case TEX_TYPE_1D:
  49. mImageViewCI.viewType = VK_IMAGE_VIEW_TYPE_1D;
  50. break;
  51. default:
  52. case TEX_TYPE_2D:
  53. mImageViewCI.viewType = VK_IMAGE_VIEW_TYPE_2D;
  54. break;
  55. case TEX_TYPE_3D:
  56. mImageViewCI.viewType = VK_IMAGE_VIEW_TYPE_3D;
  57. break;
  58. case TEX_TYPE_CUBE_MAP:
  59. mImageViewCI.viewType = VK_IMAGE_VIEW_TYPE_CUBE;
  60. break;
  61. }
  62. TextureSurface completeSurface(0, desc.numMipLevels, 0, desc.numFaces);
  63. if ((mUsage & TU_DEPTHSTENCIL) != 0)
  64. {
  65. mFramebufferMainView = createView(completeSurface, getAspectFlags());
  66. mMainView = createView(completeSurface, VK_IMAGE_ASPECT_DEPTH_BIT);
  67. }
  68. else
  69. mMainView = createView(completeSurface, VK_IMAGE_ASPECT_COLOR_BIT);
  70. ImageViewInfo mainViewInfo;
  71. mainViewInfo.surface = completeSurface;
  72. mainViewInfo.framebuffer = false;
  73. mainViewInfo.view = mMainView;
  74. mImageInfos.push_back(mainViewInfo);
  75. if (mFramebufferMainView != VK_NULL_HANDLE)
  76. {
  77. ImageViewInfo fbMainViewInfo;
  78. fbMainViewInfo.surface = completeSurface;
  79. fbMainViewInfo.framebuffer = true;
  80. fbMainViewInfo.view = mFramebufferMainView;
  81. mImageInfos.push_back(fbMainViewInfo);
  82. }
  83. UINT32 numSubresources = mNumFaces * mNumMipLevels;
  84. mSubresources = (VulkanImageSubresource**)bs_alloc(sizeof(VulkanImageSubresource*) * numSubresources);
  85. for (UINT32 i = 0; i < numSubresources; i++)
  86. mSubresources[i] = owner->create<VulkanImageSubresource>(desc.layout);
  87. }
  88. VulkanImage::~VulkanImage()
  89. {
  90. VulkanDevice& device = mOwner->getDevice();
  91. VkDevice vkDevice = device.getLogical();
  92. UINT32 numSubresources = mNumFaces * mNumMipLevels;
  93. for (UINT32 i = 0; i < numSubresources; i++)
  94. {
  95. assert(!mSubresources[i]->isBound()); // Image beeing freed but its subresources are still bound somewhere
  96. mSubresources[i]->destroy();
  97. }
  98. for(auto& entry : mImageInfos)
  99. vkDestroyImageView(vkDevice, entry.view, gVulkanAllocator);
  100. if (mOwnsImage)
  101. {
  102. vkDestroyImage(vkDevice, mImage, gVulkanAllocator);
  103. device.freeMemory(mMemory);
  104. }
  105. }
  106. VkImageView VulkanImage::getView(bool framebuffer) const
  107. {
  108. if(framebuffer && (mUsage & TU_DEPTHSTENCIL) != 0)
  109. return mFramebufferMainView;
  110. return mMainView;
  111. }
  112. VkImageView VulkanImage::getView(const TextureSurface& surface, bool framebuffer) const
  113. {
  114. for(auto& entry : mImageInfos)
  115. {
  116. if (surface.mipLevel == entry.surface.mipLevel &&
  117. surface.numMipLevels == entry.surface.numMipLevels &&
  118. surface.face == entry.surface.face &&
  119. surface.numFaces == entry.surface.numFaces)
  120. {
  121. if((mUsage & TU_DEPTHSTENCIL) == 0)
  122. return entry.view;
  123. else
  124. {
  125. if (framebuffer == entry.framebuffer)
  126. return entry.view;
  127. }
  128. }
  129. }
  130. ImageViewInfo info;
  131. info.surface = surface;
  132. info.framebuffer = framebuffer;
  133. if ((mUsage & TU_DEPTHSTENCIL) != 0)
  134. {
  135. if(framebuffer)
  136. info.view = createView(surface, getAspectFlags());
  137. else
  138. info.view = createView(surface, VK_IMAGE_ASPECT_DEPTH_BIT);
  139. }
  140. else
  141. info.view = createView(surface, VK_IMAGE_ASPECT_COLOR_BIT);
  142. mImageInfos.push_back(info);
  143. return info.view;
  144. }
  145. VkImageView VulkanImage::createView(const TextureSurface& surface, VkImageAspectFlags aspectMask) const
  146. {
  147. VkImageViewType oldViewType = mImageViewCI.viewType;
  148. switch (oldViewType)
  149. {
  150. case VK_IMAGE_VIEW_TYPE_CUBE:
  151. if(surface.numFaces == 1)
  152. mImageViewCI.viewType = VK_IMAGE_VIEW_TYPE_2D;
  153. else if(surface.numFaces % 6 == 0)
  154. {
  155. if(mNumFaces > 6)
  156. mImageViewCI.viewType = VK_IMAGE_VIEW_TYPE_CUBE_ARRAY;
  157. }
  158. else
  159. mImageViewCI.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY;
  160. break;
  161. case VK_IMAGE_VIEW_TYPE_1D:
  162. if(surface.numFaces > 1)
  163. mImageViewCI.viewType = VK_IMAGE_VIEW_TYPE_1D_ARRAY;
  164. break;
  165. case VK_IMAGE_VIEW_TYPE_2D:
  166. case VK_IMAGE_VIEW_TYPE_3D:
  167. if (surface.numFaces > 1)
  168. mImageViewCI.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY;
  169. break;
  170. default:
  171. break;
  172. }
  173. mImageViewCI.subresourceRange.aspectMask = aspectMask;
  174. mImageViewCI.subresourceRange.baseMipLevel = surface.mipLevel;
  175. mImageViewCI.subresourceRange.levelCount = surface.numMipLevels == 0 ? VK_REMAINING_MIP_LEVELS : surface.numMipLevels;
  176. mImageViewCI.subresourceRange.baseArrayLayer = surface.face;
  177. mImageViewCI.subresourceRange.layerCount = surface.numFaces == 0 ? VK_REMAINING_ARRAY_LAYERS : surface.numFaces;
  178. VkImageView view;
  179. VkResult result = vkCreateImageView(mOwner->getDevice().getLogical(), &mImageViewCI, gVulkanAllocator, &view);
  180. assert(result == VK_SUCCESS);
  181. mImageViewCI.viewType = oldViewType;
  182. return view;
  183. }
  184. VkImageLayout VulkanImage::getOptimalLayout() const
  185. {
  186. // If it's load-store, no other flags matter, it must be in general layout
  187. if ((mUsage & TU_LOADSTORE) != 0)
  188. return VK_IMAGE_LAYOUT_GENERAL;
  189. if ((mUsage & TU_RENDERTARGET) != 0)
  190. return VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
  191. else if ((mUsage & TU_DEPTHSTENCIL) != 0)
  192. return VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
  193. else
  194. {
  195. if ((mUsage & TU_DYNAMIC) != 0)
  196. return VK_IMAGE_LAYOUT_GENERAL;
  197. return VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
  198. }
  199. }
  200. VkImageAspectFlags VulkanImage::getAspectFlags() const
  201. {
  202. if ((mUsage & TU_DEPTHSTENCIL) != 0)
  203. {
  204. bool hasStencil = mImageViewCI.format == VK_FORMAT_D16_UNORM_S8_UINT ||
  205. mImageViewCI.format == VK_FORMAT_D24_UNORM_S8_UINT ||
  206. mImageViewCI.format == VK_FORMAT_D32_SFLOAT_S8_UINT;
  207. if (hasStencil)
  208. return VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
  209. return VK_IMAGE_ASPECT_DEPTH_BIT;
  210. }
  211. return VK_IMAGE_ASPECT_COLOR_BIT;
  212. }
  213. VkImageSubresourceRange VulkanImage::getRange() const
  214. {
  215. VkImageSubresourceRange range;
  216. range.baseArrayLayer = 0;
  217. range.layerCount = mNumFaces;
  218. range.baseMipLevel = 0;
  219. range.levelCount = mNumMipLevels;
  220. range.aspectMask = getAspectFlags();
  221. return range;
  222. }
  223. VkImageSubresourceRange VulkanImage::getRange(const TextureSurface& surface) const
  224. {
  225. VkImageSubresourceRange range;
  226. range.baseArrayLayer = surface.face;
  227. range.layerCount = surface.numFaces == 0 ? mNumFaces : surface.numFaces;
  228. range.baseMipLevel = surface.mipLevel;
  229. range.levelCount = surface.numMipLevels == 0 ? mNumMipLevels : surface.numMipLevels;
  230. range.aspectMask = getAspectFlags();
  231. return range;
  232. }
  233. VulkanImageSubresource* VulkanImage::getSubresource(UINT32 face, UINT32 mipLevel)
  234. {
  235. return mSubresources[mipLevel * mNumFaces + face];
  236. }
  237. void VulkanImage::map(UINT32 face, UINT32 mipLevel, PixelData& output) const
  238. {
  239. VulkanDevice& device = mOwner->getDevice();
  240. VkImageSubresource range;
  241. range.mipLevel = mipLevel;
  242. range.arrayLayer = face;
  243. if (mImageViewCI.subresourceRange.aspectMask == VK_IMAGE_ASPECT_COLOR_BIT)
  244. range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  245. else // Depth stencil, but we only map depth
  246. range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
  247. VkSubresourceLayout layout;
  248. vkGetImageSubresourceLayout(device.getLogical(), mImage, &range, &layout);
  249. assert(layout.size == output.getSize());
  250. output.setRowPitch((UINT32)layout.rowPitch);
  251. output.setSlicePitch((UINT32)layout.depthPitch);
  252. UINT8* data;
  253. VkResult result = vkMapMemory(device.getLogical(), mMemory, layout.offset, layout.size, 0, (void**)&data);
  254. assert(result == VK_SUCCESS);
  255. output.setExternalBuffer(data);
  256. }
  257. UINT8* VulkanImage::map(UINT32 offset, UINT32 size) const
  258. {
  259. VulkanDevice& device = mOwner->getDevice();
  260. UINT8* data;
  261. VkResult result = vkMapMemory(device.getLogical(), mMemory, offset, size, 0, (void**)&data);
  262. assert(result == VK_SUCCESS);
  263. return data;
  264. }
  265. void VulkanImage::unmap()
  266. {
  267. VulkanDevice& device = mOwner->getDevice();
  268. vkUnmapMemory(device.getLogical(), mMemory);
  269. }
  270. void VulkanImage::copy(VulkanTransferBuffer* cb, VulkanBuffer* destination, const VkExtent3D& extent,
  271. const VkImageSubresourceLayers& range, VkImageLayout layout)
  272. {
  273. VkBufferImageCopy region;
  274. region.bufferRowLength = destination->getRowPitch();
  275. region.bufferImageHeight = destination->getSliceHeight();
  276. region.bufferOffset = 0;
  277. region.imageOffset.x = 0;
  278. region.imageOffset.y = 0;
  279. region.imageOffset.z = 0;
  280. region.imageExtent = extent;
  281. region.imageSubresource = range;
  282. vkCmdCopyImageToBuffer(cb->getCB()->getHandle(), mImage, layout, destination->getHandle(), 1, &region);
  283. }
  284. VkAccessFlags VulkanImage::getAccessFlags(VkImageLayout layout, bool readOnly)
  285. {
  286. VkAccessFlags accessFlags;
  287. switch (layout)
  288. {
  289. case VK_IMAGE_LAYOUT_GENERAL:
  290. {
  291. accessFlags = VK_ACCESS_SHADER_READ_BIT;
  292. if ((mUsage & TU_LOADSTORE) != 0)
  293. {
  294. if (!readOnly)
  295. accessFlags |= VK_ACCESS_SHADER_WRITE_BIT;
  296. }
  297. if ((mUsage & TU_RENDERTARGET) != 0)
  298. {
  299. accessFlags |= VK_ACCESS_COLOR_ATTACHMENT_READ_BIT;
  300. if(!readOnly)
  301. accessFlags |= VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
  302. }
  303. else if ((mUsage & TU_DEPTHSTENCIL) != 0)
  304. {
  305. accessFlags |= VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT;
  306. if (!readOnly)
  307. accessFlags |= VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
  308. }
  309. }
  310. break;
  311. case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
  312. accessFlags = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_COLOR_ATTACHMENT_READ_BIT;
  313. break;
  314. case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
  315. accessFlags = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT;
  316. break;
  317. case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
  318. accessFlags = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_SHADER_READ_BIT;
  319. break;
  320. case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
  321. accessFlags = VK_ACCESS_SHADER_READ_BIT;
  322. break;
  323. case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
  324. accessFlags = VK_ACCESS_TRANSFER_READ_BIT;
  325. break;
  326. case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
  327. accessFlags = VK_ACCESS_TRANSFER_WRITE_BIT;
  328. break;
  329. case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR:
  330. accessFlags = VK_ACCESS_MEMORY_READ_BIT;
  331. break;
  332. case VK_IMAGE_LAYOUT_UNDEFINED:
  333. case VK_IMAGE_LAYOUT_PREINITIALIZED:
  334. accessFlags = 0;
  335. break;
  336. default:
  337. accessFlags = 0;
  338. LOGWRN("Unsupported source layout for Vulkan image.");
  339. break;
  340. }
  341. return accessFlags;
  342. }
  343. void VulkanImage::getBarriers(const VkImageSubresourceRange& range, Vector<VkImageMemoryBarrier>& barriers)
  344. {
  345. UINT32 numSubresources = range.levelCount * range.layerCount;
  346. // Nothing to do
  347. if (numSubresources == 0)
  348. return;
  349. UINT32 mip = range.baseMipLevel;
  350. UINT32 face = range.baseArrayLayer;
  351. UINT32 lastMip = range.baseMipLevel + range.levelCount - 1;
  352. UINT32 lastFace = range.baseArrayLayer + range.layerCount - 1;
  353. VkImageMemoryBarrier defaultBarrier;
  354. defaultBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
  355. defaultBarrier.pNext = nullptr;
  356. defaultBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  357. defaultBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  358. defaultBarrier.image = getHandle();
  359. defaultBarrier.subresourceRange.aspectMask = range.aspectMask;
  360. defaultBarrier.subresourceRange.layerCount = 1;
  361. defaultBarrier.subresourceRange.levelCount = 1;
  362. defaultBarrier.subresourceRange.baseArrayLayer = 0;
  363. defaultBarrier.subresourceRange.baseMipLevel = 0;
  364. auto addNewBarrier = [&](VulkanImageSubresource* subresource, UINT32 face, UINT32 mip)
  365. {
  366. barriers.push_back(defaultBarrier);
  367. VkImageMemoryBarrier* barrier = &barriers.back();
  368. barrier->subresourceRange.baseArrayLayer = face;
  369. barrier->subresourceRange.baseMipLevel = mip;
  370. barrier->srcAccessMask = getAccessFlags(subresource->getLayout());
  371. barrier->oldLayout = subresource->getLayout();
  372. return barrier;
  373. };
  374. bs_frame_mark();
  375. {
  376. FrameVector<bool> processed(numSubresources, false);
  377. // Add first subresource
  378. VulkanImageSubresource* subresource = getSubresource(face, mip);
  379. addNewBarrier(subresource, face, mip);
  380. numSubresources--;
  381. processed[0] = true;
  382. while (numSubresources > 0)
  383. {
  384. // Try to expand the barrier as much as possible
  385. VkImageMemoryBarrier* barrier = &barriers.back();
  386. while (true)
  387. {
  388. // Expand by one in the X direction
  389. bool expandedFace = true;
  390. if (face < lastFace)
  391. {
  392. for (UINT32 i = 0; i < barrier->subresourceRange.levelCount; i++)
  393. {
  394. UINT32 curMip = barrier->subresourceRange.baseMipLevel + i;
  395. VulkanImageSubresource* subresource = getSubresource(face + 1, curMip);
  396. if (barrier->oldLayout != subresource->getLayout())
  397. {
  398. expandedFace = false;
  399. break;
  400. }
  401. }
  402. if (expandedFace)
  403. {
  404. barrier->subresourceRange.layerCount++;
  405. numSubresources -= barrier->subresourceRange.levelCount;
  406. face++;
  407. for (UINT32 i = 0; i < barrier->subresourceRange.levelCount; i++)
  408. {
  409. UINT32 curMip = (barrier->subresourceRange.baseMipLevel + i) - range.baseMipLevel;
  410. UINT32 idx = curMip * range.layerCount + (face - range.baseArrayLayer);
  411. processed[idx] = true;
  412. }
  413. }
  414. }
  415. else
  416. expandedFace = false;
  417. // Expand by one in the Y direction
  418. bool expandedMip = true;
  419. if (mip < lastMip)
  420. {
  421. for (UINT32 i = 0; i < barrier->subresourceRange.layerCount; i++)
  422. {
  423. UINT32 curFace = barrier->subresourceRange.baseArrayLayer + i;
  424. VulkanImageSubresource* subresource = getSubresource(curFace, mip + 1);
  425. if (barrier->oldLayout != subresource->getLayout())
  426. {
  427. expandedMip = false;
  428. break;
  429. }
  430. }
  431. if (expandedMip)
  432. {
  433. barrier->subresourceRange.levelCount++;
  434. numSubresources -= barrier->subresourceRange.layerCount;
  435. mip++;
  436. for (UINT32 i = 0; i < barrier->subresourceRange.layerCount; i++)
  437. {
  438. UINT32 curFace = (barrier->subresourceRange.baseArrayLayer + i) - range.baseArrayLayer;
  439. UINT32 idx = (mip - range.baseMipLevel) * range.layerCount + curFace;
  440. processed[idx] = true;
  441. }
  442. }
  443. }
  444. else
  445. expandedMip = false;
  446. // If we can't grow no more, we're done with this square
  447. if (!expandedMip && !expandedFace)
  448. break;
  449. }
  450. // Look for a new starting point (sub-resource we haven't processed yet)
  451. for (UINT32 i = 0; i < range.levelCount; i++)
  452. {
  453. bool found = false;
  454. for (UINT32 j = 0; j < range.layerCount; j++)
  455. {
  456. UINT32 idx = i * range.layerCount + j;
  457. if (!processed[idx])
  458. {
  459. mip = range.baseMipLevel + i;
  460. face = range.baseArrayLayer + j;
  461. found = true;
  462. processed[idx] = true;
  463. break;
  464. }
  465. }
  466. if (found)
  467. {
  468. VulkanImageSubresource* subresource = getSubresource(face, mip);
  469. addNewBarrier(subresource, face, mip);
  470. numSubresources--;
  471. break;
  472. }
  473. }
  474. }
  475. }
  476. bs_frame_clear();
  477. }
  478. VulkanImageSubresource::VulkanImageSubresource(VulkanResourceManager* owner, VkImageLayout layout)
  479. :VulkanResource(owner, false), mLayout(layout)
  480. { }
  481. VulkanTexture::VulkanTexture(const TEXTURE_DESC& desc, const SPtr<PixelData>& initialData,
  482. GpuDeviceFlags deviceMask)
  483. : Texture(desc, initialData, deviceMask), mImages(), mInternalFormats(), mDeviceMask(deviceMask)
  484. , mStagingBuffer(nullptr), mMappedDeviceIdx((UINT32)-1), mMappedGlobalQueueIdx((UINT32)-1)
  485. , mMappedMip(0), mMappedFace(0), mMappedRowPitch(0), mMappedSlicePitch(0)
  486. , mMappedLockOptions(GBL_WRITE_ONLY), mDirectlyMappable(false), mSupportsGPUWrites(false), mIsMapped(false)
  487. {
  488. }
  489. VulkanTexture::~VulkanTexture()
  490. {
  491. for (UINT32 i = 0; i < BS_MAX_DEVICES; i++)
  492. {
  493. if (mImages[i] == nullptr)
  494. return;
  495. mImages[i]->destroy();
  496. }
  497. assert(mStagingBuffer == nullptr);
  498. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_Texture);
  499. }
  500. void VulkanTexture::initialize()
  501. {
  502. THROW_IF_NOT_CORE_THREAD;
  503. const TextureProperties& props = mProperties;
  504. mImageCI.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
  505. mImageCI.pNext = nullptr;
  506. mImageCI.flags = 0;
  507. TextureType texType = props.getTextureType();
  508. switch(texType)
  509. {
  510. case TEX_TYPE_1D:
  511. mImageCI.imageType = VK_IMAGE_TYPE_1D;
  512. break;
  513. case TEX_TYPE_2D:
  514. mImageCI.imageType = VK_IMAGE_TYPE_2D;
  515. break;
  516. case TEX_TYPE_3D:
  517. mImageCI.imageType = VK_IMAGE_TYPE_3D;
  518. break;
  519. case TEX_TYPE_CUBE_MAP:
  520. mImageCI.imageType = VK_IMAGE_TYPE_2D;
  521. mImageCI.flags = VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
  522. break;
  523. }
  524. // Note: I force rendertarget and depthstencil types to be readable in shader. Depending on performance impact
  525. // it might be beneficial to allow the user to enable this explicitly only when needed.
  526. mImageCI.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
  527. int usage = props.getUsage();
  528. if ((usage & TU_RENDERTARGET) != 0)
  529. {
  530. mImageCI.usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
  531. mSupportsGPUWrites = true;
  532. }
  533. else if ((usage & TU_DEPTHSTENCIL) != 0)
  534. {
  535. mImageCI.usage |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
  536. mSupportsGPUWrites = true;
  537. }
  538. if ((usage & TU_LOADSTORE) != 0)
  539. {
  540. mImageCI.usage |= VK_IMAGE_USAGE_STORAGE_BIT;
  541. mSupportsGPUWrites = true;
  542. }
  543. VkImageTiling tiling = VK_IMAGE_TILING_OPTIMAL;
  544. VkImageLayout layout = VK_IMAGE_LAYOUT_UNDEFINED;
  545. if ((usage & TU_DYNAMIC) != 0) // Attempt to use linear tiling for dynamic textures, so we can directly map and modify them
  546. {
  547. // Only support 2D textures, with one sample and one mip level, only used for shader reads
  548. // (Optionally check vkGetPhysicalDeviceFormatProperties & vkGetPhysicalDeviceImageFormatProperties for
  549. // additional supported configs, but right now there doesn't seem to be any additional support)
  550. if(texType == TEX_TYPE_2D && props.getNumSamples() <= 1 && props.getNumMipmaps() == 0 &&
  551. props.getNumFaces() == 1 && (mImageCI.usage & VK_IMAGE_USAGE_SAMPLED_BIT) != 0)
  552. {
  553. // Also, only support normal textures, not render targets or storage textures
  554. if (!mSupportsGPUWrites)
  555. {
  556. mDirectlyMappable = true;
  557. tiling = VK_IMAGE_TILING_LINEAR;
  558. layout = VK_IMAGE_LAYOUT_PREINITIALIZED;
  559. }
  560. }
  561. }
  562. mImageCI.extent = { props.getWidth(), props.getHeight(), props.getDepth() };
  563. mImageCI.mipLevels = props.getNumMipmaps() + 1;
  564. mImageCI.arrayLayers = props.getNumFaces();
  565. mImageCI.samples = VulkanUtility::getSampleFlags(props.getNumSamples());
  566. mImageCI.tiling = tiling;
  567. mImageCI.initialLayout = layout;
  568. mImageCI.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
  569. mImageCI.queueFamilyIndexCount = 0;
  570. mImageCI.pQueueFamilyIndices = nullptr;
  571. VulkanRenderAPI& rapi = static_cast<VulkanRenderAPI&>(RenderAPI::instance());
  572. VulkanDevice* devices[BS_MAX_DEVICES];
  573. VulkanUtility::getDevices(rapi, mDeviceMask, devices);
  574. // Allocate buffers per-device
  575. for (UINT32 i = 0; i < BS_MAX_DEVICES; i++)
  576. {
  577. if (devices[i] == nullptr)
  578. continue;
  579. bool optimalTiling = tiling == VK_IMAGE_TILING_OPTIMAL;
  580. mInternalFormats[i] = VulkanUtility::getClosestSupportedPixelFormat(
  581. *devices[i], props.getFormat(), props.getTextureType(), props.getUsage(), optimalTiling,
  582. props.isHardwareGammaEnabled());
  583. mImages[i] = createImage(*devices[i], mInternalFormats[i]);
  584. }
  585. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_Texture);
  586. Texture::initialize();
  587. }
  588. VulkanImage* VulkanTexture::createImage(VulkanDevice& device, PixelFormat format)
  589. {
  590. bool directlyMappable = mImageCI.tiling == VK_IMAGE_TILING_LINEAR;
  591. VkMemoryPropertyFlags flags = directlyMappable ?
  592. (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) : // Note: Try using cached memory
  593. VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
  594. VkDevice vkDevice = device.getLogical();
  595. mImageCI.format = VulkanUtility::getPixelFormat(format, mProperties.isHardwareGammaEnabled());;
  596. VkImage image;
  597. VkResult result = vkCreateImage(vkDevice, &mImageCI, gVulkanAllocator, &image);
  598. assert(result == VK_SUCCESS);
  599. VkMemoryRequirements memReqs;
  600. vkGetImageMemoryRequirements(vkDevice, image, &memReqs);
  601. VkDeviceMemory memory = device.allocateMemory(memReqs, flags);
  602. result = vkBindImageMemory(vkDevice, image, memory, 0);
  603. assert(result == VK_SUCCESS);
  604. return device.getResourceManager().create<VulkanImage>(image, memory, mImageCI.initialLayout, getProperties());
  605. }
  606. VulkanBuffer* VulkanTexture::createStaging(VulkanDevice& device, const PixelData& pixelData, bool readable)
  607. {
  608. VkBufferCreateInfo bufferCI;
  609. bufferCI.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
  610. bufferCI.pNext = nullptr;
  611. bufferCI.flags = 0;
  612. bufferCI.size = pixelData.getSize();
  613. bufferCI.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
  614. bufferCI.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
  615. bufferCI.queueFamilyIndexCount = 0;
  616. bufferCI.pQueueFamilyIndices = nullptr;
  617. if (readable)
  618. bufferCI.usage |= VK_BUFFER_USAGE_TRANSFER_DST_BIT;
  619. VkDevice vkDevice = device.getLogical();
  620. VkBuffer buffer;
  621. VkResult result = vkCreateBuffer(vkDevice, &bufferCI, gVulkanAllocator, &buffer);
  622. assert(result == VK_SUCCESS);
  623. VkMemoryRequirements memReqs;
  624. vkGetBufferMemoryRequirements(vkDevice, buffer, &memReqs);
  625. VkMemoryPropertyFlags flags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
  626. VkDeviceMemory memory = device.allocateMemory(memReqs, flags);
  627. result = vkBindBufferMemory(vkDevice, buffer, memory, 0);
  628. assert(result == VK_SUCCESS);
  629. VkBufferView view = VK_NULL_HANDLE;
  630. return device.getResourceManager().create<VulkanBuffer>(buffer, view, memory,
  631. pixelData.getRowPitch(), pixelData.getSlicePitch());
  632. }
  633. void VulkanTexture::copyImage(VulkanTransferBuffer* cb, VulkanImage* srcImage, VulkanImage* dstImage,
  634. VkImageLayout srcFinalLayout, VkImageLayout dstFinalLayout)
  635. {
  636. UINT32 numFaces = mProperties.getNumFaces();
  637. UINT32 numMipmaps = mProperties.getNumMipmaps() + 1;
  638. UINT32 mipWidth = mProperties.getWidth();
  639. UINT32 mipHeight = mProperties.getHeight();
  640. UINT32 mipDepth = mProperties.getDepth();
  641. VkImageCopy* imageRegions = bs_stack_alloc<VkImageCopy>(numMipmaps);
  642. for(UINT32 i = 0; i < numMipmaps; i++)
  643. {
  644. VkImageCopy& imageRegion = imageRegions[i];
  645. imageRegion.srcOffset = { 0, 0, 0 };
  646. imageRegion.dstOffset = { 0, 0, 0 };
  647. imageRegion.extent = { mipWidth, mipHeight, mipDepth };
  648. imageRegion.srcSubresource.baseArrayLayer = 0;
  649. imageRegion.srcSubresource.layerCount = numFaces;
  650. imageRegion.srcSubresource.mipLevel = i;
  651. imageRegion.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  652. imageRegion.dstSubresource.baseArrayLayer = 0;
  653. imageRegion.dstSubresource.layerCount = numFaces;
  654. imageRegion.dstSubresource.mipLevel = i;
  655. imageRegion.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  656. if (mipWidth != 1) mipWidth /= 2;
  657. if (mipHeight != 1) mipHeight /= 2;
  658. if (mipDepth != 1) mipDepth /= 2;
  659. }
  660. VkImageSubresourceRange range;
  661. range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  662. range.baseArrayLayer = 0;
  663. range.layerCount = numFaces;
  664. range.baseMipLevel = 0;
  665. range.levelCount = numMipmaps;
  666. VkImageLayout transferSrcLayout, transferDstLayout;
  667. if (mDirectlyMappable)
  668. {
  669. transferSrcLayout = VK_IMAGE_LAYOUT_GENERAL;
  670. transferDstLayout = VK_IMAGE_LAYOUT_GENERAL;
  671. }
  672. else
  673. {
  674. transferSrcLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
  675. transferDstLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
  676. }
  677. // Transfer textures to a valid layout
  678. cb->setLayout(srcImage, range, VK_ACCESS_TRANSFER_READ_BIT, transferSrcLayout);
  679. cb->setLayout(dstImage, range, VK_ACCESS_TRANSFER_WRITE_BIT, transferDstLayout);
  680. vkCmdCopyImage(cb->getCB()->getHandle(), srcImage->getHandle(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
  681. dstImage->getHandle(), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, numMipmaps, imageRegions);
  682. // Transfer back to final layouts
  683. VkAccessFlags srcAccessMask = srcImage->getAccessFlags(srcFinalLayout);
  684. cb->setLayout(srcImage->getHandle(), VK_ACCESS_TRANSFER_READ_BIT, srcAccessMask,
  685. transferSrcLayout, srcFinalLayout, range);
  686. VkAccessFlags dstAccessMask = dstImage->getAccessFlags(dstFinalLayout);
  687. cb->setLayout(dstImage->getHandle(), VK_ACCESS_TRANSFER_WRITE_BIT, dstAccessMask,
  688. transferDstLayout, dstFinalLayout, range);
  689. cb->getCB()->registerResource(srcImage, range, VulkanUseFlag::Read, ResourceUsage::Transfer);
  690. cb->getCB()->registerResource(dstImage, range, VulkanUseFlag::Write, ResourceUsage::Transfer);
  691. bs_stack_free(imageRegions);
  692. }
  693. void VulkanTexture::copyImpl(UINT32 srcFace, UINT32 srcMipLevel, UINT32 destFace, UINT32 destMipLevel,
  694. const SPtr<Texture>& target, const SPtr<CommandBuffer>& commandBuffer)
  695. {
  696. VulkanTexture* other = static_cast<VulkanTexture*>(target.get());
  697. const TextureProperties& srcProps = mProperties;
  698. const TextureProperties& dstProps = other->getProperties();
  699. bool srcHasMultisample = srcProps.getNumSamples() > 1;
  700. bool destHasMultisample = dstProps.getNumSamples() > 1;
  701. if ((srcProps.getUsage() & TU_DEPTHSTENCIL) != 0 || (dstProps.getUsage() & TU_DEPTHSTENCIL) != 0)
  702. {
  703. LOGERR("Texture copy/resolve isn't supported for depth-stencil textures.");
  704. return;
  705. }
  706. bool needsResolve = srcHasMultisample && !destHasMultisample;
  707. bool isMSCopy = srcHasMultisample || destHasMultisample;
  708. if (!needsResolve && isMSCopy)
  709. {
  710. if (srcProps.getNumSamples() != dstProps.getNumSamples())
  711. {
  712. LOGERR("When copying textures their multisample counts must match. Ignoring copy.");
  713. return;
  714. }
  715. }
  716. VkImageLayout transferSrcLayout = mDirectlyMappable ? VK_IMAGE_LAYOUT_GENERAL : VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
  717. VkImageLayout transferDstLayout = other->mDirectlyMappable ? VK_IMAGE_LAYOUT_GENERAL : VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
  718. UINT32 mipWidth, mipHeight, mipDepth;
  719. PixelUtil::getSizeForMipLevel(srcProps.getWidth(), srcProps.getHeight(), srcProps.getDepth(), srcMipLevel,
  720. mipWidth, mipHeight, mipDepth);
  721. VkImageResolve resolveRegion;
  722. resolveRegion.srcOffset = { 0, 0, 0 };
  723. resolveRegion.dstOffset = { 0, 0, 0 };
  724. resolveRegion.extent = { mipWidth, mipHeight, mipDepth };
  725. resolveRegion.srcSubresource.baseArrayLayer = srcFace;
  726. resolveRegion.srcSubresource.layerCount = 1;
  727. resolveRegion.srcSubresource.mipLevel = srcMipLevel;
  728. resolveRegion.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  729. resolveRegion.dstSubresource.baseArrayLayer = destFace;
  730. resolveRegion.dstSubresource.layerCount = 1;
  731. resolveRegion.dstSubresource.mipLevel = destMipLevel;
  732. resolveRegion.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  733. VkImageCopy imageRegion;
  734. imageRegion.srcOffset = { 0, 0, 0 };
  735. imageRegion.dstOffset = { 0, 0, 0 };
  736. imageRegion.extent = { mipWidth, mipHeight, mipDepth };
  737. imageRegion.srcSubresource.baseArrayLayer = srcFace;
  738. imageRegion.srcSubresource.layerCount = 1;
  739. imageRegion.srcSubresource.mipLevel = srcMipLevel;
  740. imageRegion.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  741. imageRegion.dstSubresource.baseArrayLayer = destFace;
  742. imageRegion.dstSubresource.layerCount = 1;
  743. imageRegion.dstSubresource.mipLevel = destMipLevel;
  744. imageRegion.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  745. VkImageSubresourceRange srcRange;
  746. srcRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  747. srcRange.baseArrayLayer = srcFace;
  748. srcRange.layerCount = 1;
  749. srcRange.baseMipLevel = srcMipLevel;
  750. srcRange.levelCount = 1;
  751. VkImageSubresourceRange dstRange;
  752. dstRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  753. dstRange.baseArrayLayer = destFace;
  754. dstRange.layerCount = 1;
  755. dstRange.baseMipLevel = destMipLevel;
  756. dstRange.levelCount = 1;
  757. VulkanRenderAPI& rapi = static_cast<VulkanRenderAPI&>(RenderAPI::instance());
  758. VulkanCmdBuffer* vkCB;
  759. if (commandBuffer != nullptr)
  760. vkCB = static_cast<VulkanCommandBuffer*>(commandBuffer.get())->getInternal();
  761. else
  762. vkCB = rapi._getMainCommandBuffer()->getInternal();
  763. UINT32 deviceIdx = vkCB->getDeviceIdx();
  764. VulkanImage* srcImage = mImages[deviceIdx];
  765. VulkanImage* dstImage = other->getResource(deviceIdx);
  766. if (srcImage == nullptr || dstImage == nullptr)
  767. return;
  768. VkImageLayout srcLayout = vkCB->getCurrentLayout(srcImage, srcRange, false);
  769. VkImageLayout dstLayout = vkCB->getCurrentLayout(dstImage, dstRange, false);
  770. VkCommandBuffer vkCmdBuf = vkCB->getHandle();
  771. VkAccessFlags srcAccessMask = srcImage->getAccessFlags(srcLayout);
  772. VkAccessFlags dstAccessMask = dstImage->getAccessFlags(dstLayout);
  773. if (vkCB->isInRenderPass())
  774. vkCB->endRenderPass();
  775. // Transfer textures to a valid layout
  776. vkCB->setLayout(srcImage->getHandle(), srcAccessMask, VK_ACCESS_TRANSFER_READ_BIT, srcLayout,
  777. transferSrcLayout, srcRange);
  778. vkCB->setLayout(dstImage->getHandle(), dstAccessMask, VK_ACCESS_TRANSFER_WRITE_BIT,
  779. dstLayout, transferDstLayout, dstRange);
  780. if (srcHasMultisample && !destHasMultisample) // Resolving from MS to non-MS texture
  781. {
  782. vkCmdResolveImage(vkCmdBuf, srcImage->getHandle(), transferSrcLayout, dstImage->getHandle(), transferDstLayout,
  783. 1, &resolveRegion);
  784. }
  785. else // Just a normal copy
  786. {
  787. vkCmdCopyImage(vkCmdBuf, srcImage->getHandle(), transferSrcLayout, dstImage->getHandle(), transferDstLayout,
  788. 1, &imageRegion);
  789. }
  790. // Transfer back to optimal layouts
  791. srcLayout = srcImage->getOptimalLayout();
  792. srcAccessMask = srcImage->getAccessFlags(srcLayout);
  793. vkCB->setLayout(srcImage->getHandle(), VK_ACCESS_TRANSFER_READ_BIT, srcAccessMask, transferSrcLayout,
  794. srcLayout, srcRange);
  795. dstLayout = dstImage->getOptimalLayout();
  796. dstAccessMask = dstImage->getAccessFlags(dstLayout);
  797. vkCB->setLayout(dstImage->getHandle(), VK_ACCESS_TRANSFER_WRITE_BIT, dstAccessMask, transferDstLayout,
  798. dstLayout, dstRange);
  799. // Notify the command buffer that these resources are being used on it
  800. vkCB->registerResource(srcImage, srcRange, VulkanUseFlag::Read, ResourceUsage::Transfer);
  801. vkCB->registerResource(dstImage, dstRange, VulkanUseFlag::Write, ResourceUsage::Transfer);
  802. }
  803. PixelData VulkanTexture::lockImpl(GpuLockOptions options, UINT32 mipLevel, UINT32 face, UINT32 deviceIdx,
  804. UINT32 queueIdx)
  805. {
  806. const TextureProperties& props = getProperties();
  807. if (props.getNumSamples() > 1)
  808. {
  809. LOGERR("Multisampled textures cannot be accessed from the CPU directly.");
  810. return PixelData();
  811. }
  812. #if BS_PROFILING_ENABLED
  813. if (options == GBL_READ_ONLY || options == GBL_READ_WRITE)
  814. {
  815. BS_INC_RENDER_STAT_CAT(ResRead, RenderStatObject_Texture);
  816. }
  817. if (options == GBL_READ_WRITE || options == GBL_WRITE_ONLY || options == GBL_WRITE_ONLY_DISCARD || options == GBL_WRITE_ONLY_NO_OVERWRITE)
  818. {
  819. BS_INC_RENDER_STAT_CAT(ResWrite, RenderStatObject_Texture);
  820. }
  821. #endif
  822. UINT32 mipWidth = std::max(1u, props.getWidth() >> mipLevel);
  823. UINT32 mipHeight = std::max(1u, props.getHeight() >> mipLevel);
  824. UINT32 mipDepth = std::max(1u, props.getDepth() >> mipLevel);
  825. PixelData lockedArea(mipWidth, mipHeight, mipDepth, mInternalFormats[deviceIdx]);
  826. VulkanImage* image = mImages[deviceIdx];
  827. if (image == nullptr)
  828. return PixelData();
  829. mIsMapped = true;
  830. mMappedDeviceIdx = deviceIdx;
  831. mMappedGlobalQueueIdx = queueIdx;
  832. mMappedFace = face;
  833. mMappedMip = mipLevel;
  834. mMappedLockOptions = options;
  835. VulkanRenderAPI& rapi = static_cast<VulkanRenderAPI&>(RenderAPI::instance());
  836. VulkanDevice& device = *rapi._getDevice(deviceIdx);
  837. VulkanCommandBufferManager& cbManager = gVulkanCBManager();
  838. GpuQueueType queueType;
  839. UINT32 localQueueIdx = CommandSyncMask::getQueueIdxAndType(queueIdx, queueType);
  840. VulkanImageSubresource* subresource = image->getSubresource(face, mipLevel);
  841. // If memory is host visible try mapping it directly
  842. if (mDirectlyMappable)
  843. {
  844. // Initially the texture will be in preinitialized layout, and it will transition to general layout on first
  845. // use in shader. No further transitions are allowed for directly mappable textures.
  846. assert(subresource->getLayout() == VK_IMAGE_LAYOUT_PREINITIALIZED ||
  847. subresource->getLayout() == VK_IMAGE_LAYOUT_GENERAL);
  848. // GPU should never be allowed to write to a directly mappable texture, since only linear tiling is supported
  849. // for direct mapping, and we don't support using it with either storage textures or render targets.
  850. assert(!mSupportsGPUWrites);
  851. // Check is the GPU currently reading from the image
  852. UINT32 useMask = subresource->getUseInfo(VulkanUseFlag::Read);
  853. bool isUsedOnGPU = useMask != 0;
  854. // We're safe to map directly since GPU isn't using the subresource
  855. if (!isUsedOnGPU)
  856. {
  857. // If some CB has an operation queued that will be using the current contents of the image, create a new
  858. // image so we don't modify the previous use of the image
  859. if (subresource->isBound())
  860. {
  861. VulkanImage* newImage = createImage(device, mInternalFormats[deviceIdx]);
  862. // Copy contents of the current image to the new one, unless caller explicitly specifies he doesn't
  863. // care about the current contents
  864. if (options != GBL_WRITE_ONLY_DISCARD)
  865. {
  866. VkMemoryRequirements memReqs;
  867. vkGetImageMemoryRequirements(device.getLogical(), image->getHandle(), &memReqs);
  868. UINT8* src = image->map(0, (UINT32)memReqs.size);
  869. UINT8* dst = newImage->map(0, (UINT32)memReqs.size);
  870. memcpy(dst, src, memReqs.size);
  871. image->unmap();
  872. newImage->unmap();
  873. }
  874. image->destroy();
  875. image = newImage;
  876. mImages[deviceIdx] = image;
  877. }
  878. image->map(face, mipLevel, lockedArea);
  879. return lockedArea;
  880. }
  881. // Caller guarantees he won't touch the same data as the GPU, so just map even though the GPU is using the
  882. // subresource
  883. if (options == GBL_WRITE_ONLY_NO_OVERWRITE)
  884. {
  885. image->map(face, mipLevel, lockedArea);
  886. return lockedArea;
  887. }
  888. // Caller doesn't care about buffer contents, so just discard the existing buffer and create a new one
  889. if (options == GBL_WRITE_ONLY_DISCARD)
  890. {
  891. // We need to discard the entire image, even though we're only writing to a single sub-resource
  892. image->destroy();
  893. image = createImage(device, mInternalFormats[deviceIdx]);
  894. mImages[deviceIdx] = image;
  895. image->map(face, mipLevel, lockedArea);
  896. return lockedArea;
  897. }
  898. // We need to read the buffer contents
  899. if (options == GBL_READ_ONLY || options == GBL_READ_WRITE)
  900. {
  901. VulkanTransferBuffer* transferCB = cbManager.getTransferBuffer(deviceIdx, queueType, localQueueIdx);
  902. // Ensure flush() will wait for all queues currently using to the texture (if any) to finish
  903. // If only reading, wait for all writes to complete, otherwise wait on both writes and reads
  904. if (options == GBL_READ_ONLY)
  905. useMask = subresource->getUseInfo(VulkanUseFlag::Write);
  906. else
  907. useMask = subresource->getUseInfo(VulkanUseFlag::Read | VulkanUseFlag::Write);
  908. transferCB->appendMask(useMask);
  909. // Submit the command buffer and wait until it finishes
  910. transferCB->flush(true);
  911. // If writing and some CB has an operation queued that will be using the current contents of the image,
  912. // create a new image so we don't modify the previous use of the image
  913. if (options == GBL_READ_WRITE && subresource->isBound())
  914. {
  915. VulkanImage* newImage = createImage(device, mInternalFormats[deviceIdx]);
  916. VkMemoryRequirements memReqs;
  917. vkGetImageMemoryRequirements(device.getLogical(), image->getHandle(), &memReqs);
  918. UINT8* src = image->map(0, (UINT32)memReqs.size);
  919. UINT8* dst = newImage->map(0, (UINT32)memReqs.size);
  920. memcpy(dst, src, memReqs.size);
  921. image->unmap();
  922. newImage->unmap();
  923. image->destroy();
  924. image = newImage;
  925. mImages[deviceIdx] = image;
  926. }
  927. image->map(face, mipLevel, lockedArea);
  928. return lockedArea;
  929. }
  930. // Otherwise, we're doing write only, in which case it's best to use the staging buffer to avoid waiting
  931. // and blocking, so fall through
  932. }
  933. // Can't use direct mapping, so use a staging buffer
  934. // We might need to copy the current contents of the image to the staging buffer. Even if the user doesn't plan on
  935. // reading, it is still required as we will eventually copy all of the contents back to the original image,
  936. // and we can't write potentially uninitialized data. The only exception is when the caller specifies the image
  937. // contents should be discarded in which he guarantees he will overwrite the entire locked area with his own
  938. // contents.
  939. bool needRead = options != GBL_WRITE_ONLY_DISCARD_RANGE && options != GBL_WRITE_ONLY_DISCARD;
  940. // Allocate a staging buffer
  941. mStagingBuffer = createStaging(device, lockedArea, needRead);
  942. if (needRead) // If reading, we need to copy the current contents of the image to the staging buffer
  943. {
  944. VulkanTransferBuffer* transferCB = cbManager.getTransferBuffer(deviceIdx, queueType, localQueueIdx);
  945. // Similar to above, if image supports GPU writes or is currently being written to, we need to wait on any
  946. // potential writes to complete
  947. UINT32 writeUseMask = subresource->getUseInfo(VulkanUseFlag::Write);
  948. if (mSupportsGPUWrites || writeUseMask != 0)
  949. {
  950. // Ensure flush() will wait for all queues currently writing to the image (if any) to finish
  951. transferCB->appendMask(writeUseMask);
  952. }
  953. VkImageSubresourceRange range;
  954. range.aspectMask = image->getAspectFlags();
  955. range.baseArrayLayer = face;
  956. range.layerCount = 1;
  957. range.baseMipLevel = mipLevel;
  958. range.levelCount = 1;
  959. VkImageSubresourceLayers rangeLayers;
  960. if ((props.getUsage() & TU_DEPTHSTENCIL) != 0)
  961. rangeLayers.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
  962. else
  963. rangeLayers.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  964. rangeLayers.baseArrayLayer = range.baseArrayLayer;
  965. rangeLayers.layerCount = range.layerCount;
  966. rangeLayers.mipLevel = range.baseMipLevel;
  967. VkExtent3D extent;
  968. PixelUtil::getSizeForMipLevel(props.getWidth(), props.getHeight(), props.getDepth(), mMappedMip,
  969. extent.width, extent.height, extent.depth);
  970. // Transfer texture to a valid layout
  971. VkAccessFlags currentAccessMask = image->getAccessFlags(subresource->getLayout());
  972. transferCB->setLayout(image->getHandle(), currentAccessMask, VK_ACCESS_TRANSFER_READ_BIT, subresource->getLayout(),
  973. VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, range);
  974. // Queue copy command
  975. image->copy(transferCB, mStagingBuffer, extent, rangeLayers, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
  976. // Transfer back to original layout
  977. VkImageLayout dstLayout = image->getOptimalLayout();
  978. currentAccessMask = image->getAccessFlags(dstLayout);
  979. transferCB->setLayout(image->getHandle(), VK_ACCESS_TRANSFER_READ_BIT, currentAccessMask,
  980. VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, dstLayout, range);
  981. transferCB->getCB()->registerResource(image, range, VulkanUseFlag::Read, ResourceUsage::Transfer);
  982. // Ensure data written to the staging buffer is visible
  983. VkAccessFlags stagingAccessFlags;
  984. if (options == GBL_READ_ONLY)
  985. stagingAccessFlags = VK_ACCESS_HOST_READ_BIT;
  986. else // Must be read/write
  987. stagingAccessFlags = VK_ACCESS_HOST_READ_BIT | VK_ACCESS_HOST_WRITE_BIT;
  988. transferCB->memoryBarrier(mStagingBuffer->getHandle(),
  989. VK_ACCESS_TRANSFER_WRITE_BIT,
  990. stagingAccessFlags,
  991. VK_PIPELINE_STAGE_TRANSFER_BIT,
  992. VK_PIPELINE_STAGE_HOST_BIT);
  993. // Submit the command buffer and wait until it finishes
  994. transferCB->flush(true);
  995. }
  996. UINT8* data = mStagingBuffer->map(0, lockedArea.getSize());
  997. lockedArea.setExternalBuffer(data);
  998. return lockedArea;
  999. }
  1000. void VulkanTexture::unlockImpl()
  1001. {
  1002. // Possibly map() failed with some error
  1003. if (!mIsMapped)
  1004. return;
  1005. // Note: If we did any writes they need to be made visible to the GPU. However there is no need to execute
  1006. // a pipeline barrier because (as per spec) host writes are implicitly visible to the device.
  1007. if (mStagingBuffer == nullptr)
  1008. mImages[mMappedDeviceIdx]->unmap();
  1009. else
  1010. {
  1011. mStagingBuffer->unmap();
  1012. bool isWrite = mMappedLockOptions != GBL_READ_ONLY;
  1013. // We the caller wrote anything to the staging buffer, we need to upload it back to the main buffer
  1014. if (isWrite)
  1015. {
  1016. VulkanRenderAPI& rapi = static_cast<VulkanRenderAPI&>(RenderAPI::instance());
  1017. VulkanDevice& device = *rapi._getDevice(mMappedDeviceIdx);
  1018. VulkanCommandBufferManager& cbManager = gVulkanCBManager();
  1019. GpuQueueType queueType;
  1020. UINT32 localQueueIdx = CommandSyncMask::getQueueIdxAndType(mMappedGlobalQueueIdx, queueType);
  1021. VulkanImage* image = mImages[mMappedDeviceIdx];
  1022. VulkanTransferBuffer* transferCB = cbManager.getTransferBuffer(mMappedDeviceIdx, queueType, localQueueIdx);
  1023. VulkanImageSubresource* subresource = image->getSubresource(mMappedFace, mMappedMip);
  1024. VkImageLayout curLayout = subresource->getLayout();
  1025. // If the subresource is used in any way on the GPU, we need to wait for that use to finish before
  1026. // we issue our copy
  1027. UINT32 useMask = subresource->getUseInfo(VulkanUseFlag::Read | VulkanUseFlag::Write);
  1028. bool isNormalWrite = false;
  1029. if (useMask != 0) // Subresource is currently used on the GPU
  1030. {
  1031. // Try to avoid the wait by checking for special write conditions
  1032. // Caller guarantees he won't touch the same data as the GPU, so just copy
  1033. if (mMappedLockOptions == GBL_WRITE_ONLY_NO_OVERWRITE)
  1034. {
  1035. // Fall through to copy()
  1036. }
  1037. // Caller doesn't care about buffer contents, so just discard the existing buffer and create a new one
  1038. else if (mMappedLockOptions == GBL_WRITE_ONLY_DISCARD)
  1039. {
  1040. // We need to discard the entire image, even though we're only writing to a single sub-resource
  1041. image->destroy();
  1042. image = createImage(device, mInternalFormats[mMappedDeviceIdx]);
  1043. mImages[mMappedDeviceIdx] = image;
  1044. subresource = image->getSubresource(mMappedFace, mMappedMip);
  1045. }
  1046. else // Otherwise we have no choice but to issue a dependency between the queues
  1047. {
  1048. transferCB->appendMask(useMask);
  1049. isNormalWrite = true;
  1050. }
  1051. }
  1052. else
  1053. isNormalWrite = true;
  1054. const TextureProperties& props = getProperties();
  1055. // Check if the subresource will still be bound somewhere after the CBs using it finish
  1056. if (isNormalWrite)
  1057. {
  1058. UINT32 useCount = subresource->getUseCount();
  1059. UINT32 boundCount = subresource->getBoundCount();
  1060. bool isBoundWithoutUse = boundCount > useCount;
  1061. // If image is queued for some operation on a CB, then we need to make a copy of the subresource to
  1062. // avoid modifying its use in the previous operation
  1063. if (isBoundWithoutUse)
  1064. {
  1065. VulkanImage* newImage = createImage(device, mInternalFormats[mMappedDeviceIdx]);
  1066. // Avoid copying original contents if the image only has one sub-resource, which we'll overwrite anyway
  1067. if (props.getNumMipmaps() > 0 || props.getNumFaces() > 1)
  1068. {
  1069. VkImageLayout oldImgLayout = image->getOptimalLayout();
  1070. curLayout = newImage->getOptimalLayout();
  1071. copyImage(transferCB, image, newImage, oldImgLayout, curLayout);
  1072. }
  1073. image->destroy();
  1074. image = newImage;
  1075. mImages[mMappedDeviceIdx] = image;
  1076. }
  1077. }
  1078. VkImageSubresourceRange range;
  1079. range.aspectMask = image->getAspectFlags();
  1080. range.baseArrayLayer = mMappedFace;
  1081. range.layerCount = 1;
  1082. range.baseMipLevel = mMappedMip;
  1083. range.levelCount = 1;
  1084. VkImageSubresourceLayers rangeLayers;
  1085. rangeLayers.aspectMask = range.aspectMask;
  1086. rangeLayers.baseArrayLayer = range.baseArrayLayer;
  1087. rangeLayers.layerCount = range.layerCount;
  1088. rangeLayers.mipLevel = range.baseMipLevel;
  1089. VkExtent3D extent;
  1090. PixelUtil::getSizeForMipLevel(props.getWidth(), props.getHeight(), props.getDepth(), mMappedMip,
  1091. extent.width, extent.height, extent.depth);
  1092. VkImageLayout transferLayout;
  1093. if (mDirectlyMappable)
  1094. transferLayout = VK_IMAGE_LAYOUT_GENERAL;
  1095. else
  1096. transferLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
  1097. // Transfer texture to a valid layout
  1098. VkAccessFlags currentAccessMask = image->getAccessFlags(curLayout);
  1099. transferCB->setLayout(image->getHandle(), currentAccessMask, VK_ACCESS_TRANSFER_WRITE_BIT,
  1100. curLayout, transferLayout, range);
  1101. // Queue copy command
  1102. mStagingBuffer->copy(transferCB->getCB(), image, extent, rangeLayers, transferLayout);
  1103. // Transfer back to original (or optimal if initial layout was undefined/preinitialized)
  1104. VkImageLayout dstLayout = image->getOptimalLayout();
  1105. currentAccessMask = image->getAccessFlags(dstLayout);
  1106. transferCB->setLayout(image->getHandle(), VK_ACCESS_TRANSFER_WRITE_BIT, currentAccessMask,
  1107. transferLayout, dstLayout, range);
  1108. // Notify the command buffer that these resources are being used on it
  1109. transferCB->getCB()->registerResource(mStagingBuffer, VK_ACCESS_TRANSFER_READ_BIT, VulkanUseFlag::Read);
  1110. transferCB->getCB()->registerResource(image, range, VulkanUseFlag::Write, ResourceUsage::Transfer);
  1111. // We don't actually flush the transfer buffer here since it's an expensive operation, but it's instead
  1112. // done automatically before next "normal" command buffer submission.
  1113. }
  1114. mStagingBuffer->destroy();
  1115. mStagingBuffer = nullptr;
  1116. }
  1117. mIsMapped = false;
  1118. }
  1119. void VulkanTexture::readDataImpl(PixelData& dest, UINT32 mipLevel, UINT32 face, UINT32 deviceIdx, UINT32 queueIdx)
  1120. {
  1121. if (mProperties.getNumSamples() > 1)
  1122. {
  1123. LOGERR("Multisampled textures cannot be accessed from the CPU directly.");
  1124. return;
  1125. }
  1126. PixelData myData = lock(GBL_READ_ONLY, mipLevel, face, deviceIdx, queueIdx);
  1127. PixelUtil::bulkPixelConversion(myData, dest);
  1128. unlock();
  1129. BS_INC_RENDER_STAT_CAT(ResRead, RenderStatObject_Texture);
  1130. }
  1131. void VulkanTexture::writeDataImpl(const PixelData& src, UINT32 mipLevel, UINT32 face, bool discardWholeBuffer,
  1132. UINT32 queueIdx)
  1133. {
  1134. if (mProperties.getNumSamples() > 1)
  1135. {
  1136. LOGERR("Multisampled textures cannot be accessed from the CPU directly.");
  1137. return;
  1138. }
  1139. mipLevel = Math::clamp(mipLevel, (UINT32)mipLevel, mProperties.getNumMipmaps());
  1140. face = Math::clamp(face, (UINT32)0, mProperties.getNumFaces() - 1);
  1141. if (face > 0 && mProperties.getTextureType() == TEX_TYPE_3D)
  1142. {
  1143. LOGERR("3D texture arrays are not supported.");
  1144. return;
  1145. }
  1146. // Write to every device
  1147. for (UINT32 i = 0; i < BS_MAX_DEVICES; i++)
  1148. {
  1149. if (mImages[i] == nullptr)
  1150. continue;
  1151. PixelData myData = lock(discardWholeBuffer ? GBL_WRITE_ONLY_DISCARD : GBL_WRITE_ONLY_DISCARD_RANGE,
  1152. mipLevel, face, i, queueIdx);
  1153. PixelUtil::bulkPixelConversion(src, myData);
  1154. unlock();
  1155. }
  1156. BS_INC_RENDER_STAT_CAT(ResWrite, RenderStatObject_Texture);
  1157. }
  1158. }}