BsVulkanTexture.cpp 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428
  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(const SPtr<Texture>& target, const TEXTURE_COPY_DESC& desc,
  694. 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. bool copyEntireSurface = desc.srcVolume.getWidth() == 0 ||
  720. desc.srcVolume.getHeight() == 0 ||
  721. desc.srcVolume.getDepth() == 0;
  722. if(copyEntireSurface)
  723. {
  724. PixelUtil::getSizeForMipLevel(
  725. srcProps.getWidth(),
  726. srcProps.getHeight(),
  727. srcProps.getDepth(),
  728. desc.srcMip,
  729. mipWidth,
  730. mipHeight,
  731. mipDepth);
  732. }
  733. else
  734. {
  735. mipWidth = desc.srcVolume.getWidth();
  736. mipHeight = desc.srcVolume.getHeight();
  737. mipDepth = desc.srcVolume.getDepth();
  738. }
  739. VkImageResolve resolveRegion;
  740. resolveRegion.srcOffset = { (INT32)desc.srcVolume.left, (INT32)desc.srcVolume.top, (INT32)desc.srcVolume.front };
  741. resolveRegion.dstOffset = { desc.dstPosition.x, desc.dstPosition.y, desc.dstPosition.z };
  742. resolveRegion.extent = { mipWidth, mipHeight, mipDepth };
  743. resolveRegion.srcSubresource.baseArrayLayer = desc.srcFace;
  744. resolveRegion.srcSubresource.layerCount = 1;
  745. resolveRegion.srcSubresource.mipLevel = desc.srcMip;
  746. resolveRegion.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  747. resolveRegion.dstSubresource.baseArrayLayer = desc.dstFace;
  748. resolveRegion.dstSubresource.layerCount = 1;
  749. resolveRegion.dstSubresource.mipLevel = desc.dstMip;
  750. resolveRegion.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  751. VkImageCopy imageRegion;
  752. imageRegion.srcOffset = { (INT32)desc.srcVolume.left, (INT32)desc.srcVolume.top, (INT32)desc.srcVolume.front };
  753. imageRegion.dstOffset = { desc.dstPosition.x, desc.dstPosition.y, desc.dstPosition.z };
  754. imageRegion.extent = { mipWidth, mipHeight, mipDepth };
  755. imageRegion.srcSubresource.baseArrayLayer = desc.srcFace;
  756. imageRegion.srcSubresource.layerCount = 1;
  757. imageRegion.srcSubresource.mipLevel = desc.srcMip;
  758. imageRegion.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  759. imageRegion.dstSubresource.baseArrayLayer = desc.dstFace;
  760. imageRegion.dstSubresource.layerCount = 1;
  761. imageRegion.dstSubresource.mipLevel = desc.dstMip;
  762. imageRegion.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  763. VkImageSubresourceRange srcRange;
  764. srcRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  765. srcRange.baseArrayLayer = desc.srcFace;
  766. srcRange.layerCount = 1;
  767. srcRange.baseMipLevel = desc.srcMip;
  768. srcRange.levelCount = 1;
  769. VkImageSubresourceRange dstRange;
  770. dstRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  771. dstRange.baseArrayLayer = desc.dstFace;
  772. dstRange.layerCount = 1;
  773. dstRange.baseMipLevel = desc.dstMip;
  774. dstRange.levelCount = 1;
  775. VulkanRenderAPI& rapi = static_cast<VulkanRenderAPI&>(RenderAPI::instance());
  776. VulkanCmdBuffer* vkCB;
  777. if (commandBuffer != nullptr)
  778. vkCB = static_cast<VulkanCommandBuffer*>(commandBuffer.get())->getInternal();
  779. else
  780. vkCB = rapi._getMainCommandBuffer()->getInternal();
  781. UINT32 deviceIdx = vkCB->getDeviceIdx();
  782. VulkanImage* srcImage = mImages[deviceIdx];
  783. VulkanImage* dstImage = other->getResource(deviceIdx);
  784. if (srcImage == nullptr || dstImage == nullptr)
  785. return;
  786. VkImageLayout srcLayout = vkCB->getCurrentLayout(srcImage, srcRange, false);
  787. VkImageLayout dstLayout = vkCB->getCurrentLayout(dstImage, dstRange, false);
  788. VkCommandBuffer vkCmdBuf = vkCB->getHandle();
  789. VkAccessFlags srcAccessMask = srcImage->getAccessFlags(srcLayout);
  790. VkAccessFlags dstAccessMask = dstImage->getAccessFlags(dstLayout);
  791. if (vkCB->isInRenderPass())
  792. vkCB->endRenderPass();
  793. // Transfer textures to a valid layout
  794. vkCB->setLayout(srcImage->getHandle(), srcAccessMask, VK_ACCESS_TRANSFER_READ_BIT, srcLayout,
  795. transferSrcLayout, srcRange);
  796. vkCB->setLayout(dstImage->getHandle(), dstAccessMask, VK_ACCESS_TRANSFER_WRITE_BIT,
  797. dstLayout, transferDstLayout, dstRange);
  798. if (srcHasMultisample && !destHasMultisample) // Resolving from MS to non-MS texture
  799. {
  800. vkCmdResolveImage(vkCmdBuf, srcImage->getHandle(), transferSrcLayout, dstImage->getHandle(), transferDstLayout,
  801. 1, &resolveRegion);
  802. }
  803. else // Just a normal copy
  804. {
  805. vkCmdCopyImage(vkCmdBuf, srcImage->getHandle(), transferSrcLayout, dstImage->getHandle(), transferDstLayout,
  806. 1, &imageRegion);
  807. }
  808. // Transfer back to optimal layouts
  809. srcLayout = srcImage->getOptimalLayout();
  810. // Notify the command buffer that these resources are being used on it
  811. vkCB->registerResource(srcImage, srcRange, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VulkanUseFlag::Read, ResourceUsage::Transfer);
  812. vkCB->registerResource(dstImage, dstRange, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VulkanUseFlag::Write, ResourceUsage::Transfer);
  813. }
  814. PixelData VulkanTexture::lockImpl(GpuLockOptions options, UINT32 mipLevel, UINT32 face, UINT32 deviceIdx,
  815. UINT32 queueIdx)
  816. {
  817. const TextureProperties& props = getProperties();
  818. if (props.getNumSamples() > 1)
  819. {
  820. LOGERR("Multisampled textures cannot be accessed from the CPU directly.");
  821. return PixelData();
  822. }
  823. #if BS_PROFILING_ENABLED
  824. if (options == GBL_READ_ONLY || options == GBL_READ_WRITE)
  825. {
  826. BS_INC_RENDER_STAT_CAT(ResRead, RenderStatObject_Texture);
  827. }
  828. if (options == GBL_READ_WRITE || options == GBL_WRITE_ONLY || options == GBL_WRITE_ONLY_DISCARD || options == GBL_WRITE_ONLY_NO_OVERWRITE)
  829. {
  830. BS_INC_RENDER_STAT_CAT(ResWrite, RenderStatObject_Texture);
  831. }
  832. #endif
  833. UINT32 mipWidth = std::max(1u, props.getWidth() >> mipLevel);
  834. UINT32 mipHeight = std::max(1u, props.getHeight() >> mipLevel);
  835. UINT32 mipDepth = std::max(1u, props.getDepth() >> mipLevel);
  836. PixelData lockedArea(mipWidth, mipHeight, mipDepth, mInternalFormats[deviceIdx]);
  837. VulkanImage* image = mImages[deviceIdx];
  838. if (image == nullptr)
  839. return PixelData();
  840. mIsMapped = true;
  841. mMappedDeviceIdx = deviceIdx;
  842. mMappedGlobalQueueIdx = queueIdx;
  843. mMappedFace = face;
  844. mMappedMip = mipLevel;
  845. mMappedLockOptions = options;
  846. VulkanRenderAPI& rapi = static_cast<VulkanRenderAPI&>(RenderAPI::instance());
  847. VulkanDevice& device = *rapi._getDevice(deviceIdx);
  848. VulkanCommandBufferManager& cbManager = gVulkanCBManager();
  849. GpuQueueType queueType;
  850. UINT32 localQueueIdx = CommandSyncMask::getQueueIdxAndType(queueIdx, queueType);
  851. VulkanImageSubresource* subresource = image->getSubresource(face, mipLevel);
  852. // If memory is host visible try mapping it directly
  853. if (mDirectlyMappable)
  854. {
  855. // Initially the texture will be in preinitialized layout, and it will transition to general layout on first
  856. // use in shader. No further transitions are allowed for directly mappable textures.
  857. assert(subresource->getLayout() == VK_IMAGE_LAYOUT_PREINITIALIZED ||
  858. subresource->getLayout() == VK_IMAGE_LAYOUT_GENERAL);
  859. // GPU should never be allowed to write to a directly mappable texture, since only linear tiling is supported
  860. // for direct mapping, and we don't support using it with either storage textures or render targets.
  861. assert(!mSupportsGPUWrites);
  862. // Check is the GPU currently reading from the image
  863. UINT32 useMask = subresource->getUseInfo(VulkanUseFlag::Read);
  864. bool isUsedOnGPU = useMask != 0;
  865. // We're safe to map directly since GPU isn't using the subresource
  866. if (!isUsedOnGPU)
  867. {
  868. // If some CB has an operation queued that will be using the current contents of the image, create a new
  869. // image so we don't modify the previous use of the image
  870. if (subresource->isBound())
  871. {
  872. VulkanImage* newImage = createImage(device, mInternalFormats[deviceIdx]);
  873. // Copy contents of the current image to the new one, unless caller explicitly specifies he doesn't
  874. // care about the current contents
  875. if (options != GBL_WRITE_ONLY_DISCARD)
  876. {
  877. VkMemoryRequirements memReqs;
  878. vkGetImageMemoryRequirements(device.getLogical(), image->getHandle(), &memReqs);
  879. UINT8* src = image->map(0, (UINT32)memReqs.size);
  880. UINT8* dst = newImage->map(0, (UINT32)memReqs.size);
  881. memcpy(dst, src, memReqs.size);
  882. image->unmap();
  883. newImage->unmap();
  884. }
  885. image->destroy();
  886. image = newImage;
  887. mImages[deviceIdx] = image;
  888. }
  889. image->map(face, mipLevel, lockedArea);
  890. return lockedArea;
  891. }
  892. // Caller guarantees he won't touch the same data as the GPU, so just map even though the GPU is using the
  893. // subresource
  894. if (options == GBL_WRITE_ONLY_NO_OVERWRITE)
  895. {
  896. image->map(face, mipLevel, lockedArea);
  897. return lockedArea;
  898. }
  899. // Caller doesn't care about buffer contents, so just discard the existing buffer and create a new one
  900. if (options == GBL_WRITE_ONLY_DISCARD)
  901. {
  902. // We need to discard the entire image, even though we're only writing to a single sub-resource
  903. image->destroy();
  904. image = createImage(device, mInternalFormats[deviceIdx]);
  905. mImages[deviceIdx] = image;
  906. image->map(face, mipLevel, lockedArea);
  907. return lockedArea;
  908. }
  909. // We need to read the buffer contents
  910. if (options == GBL_READ_ONLY || options == GBL_READ_WRITE)
  911. {
  912. VulkanTransferBuffer* transferCB = cbManager.getTransferBuffer(deviceIdx, queueType, localQueueIdx);
  913. // Ensure flush() will wait for all queues currently using to the texture (if any) to finish
  914. // If only reading, wait for all writes to complete, otherwise wait on both writes and reads
  915. if (options == GBL_READ_ONLY)
  916. useMask = subresource->getUseInfo(VulkanUseFlag::Write);
  917. else
  918. useMask = subresource->getUseInfo(VulkanUseFlag::Read | VulkanUseFlag::Write);
  919. transferCB->appendMask(useMask);
  920. // Submit the command buffer and wait until it finishes
  921. transferCB->flush(true);
  922. // If writing and some CB has an operation queued that will be using the current contents of the image,
  923. // create a new image so we don't modify the previous use of the image
  924. if (options == GBL_READ_WRITE && subresource->isBound())
  925. {
  926. VulkanImage* newImage = createImage(device, mInternalFormats[deviceIdx]);
  927. VkMemoryRequirements memReqs;
  928. vkGetImageMemoryRequirements(device.getLogical(), image->getHandle(), &memReqs);
  929. UINT8* src = image->map(0, (UINT32)memReqs.size);
  930. UINT8* dst = newImage->map(0, (UINT32)memReqs.size);
  931. memcpy(dst, src, memReqs.size);
  932. image->unmap();
  933. newImage->unmap();
  934. image->destroy();
  935. image = newImage;
  936. mImages[deviceIdx] = image;
  937. }
  938. image->map(face, mipLevel, lockedArea);
  939. return lockedArea;
  940. }
  941. // Otherwise, we're doing write only, in which case it's best to use the staging buffer to avoid waiting
  942. // and blocking, so fall through
  943. }
  944. // Can't use direct mapping, so use a staging buffer
  945. // We might need to copy the current contents of the image to the staging buffer. Even if the user doesn't plan on
  946. // reading, it is still required as we will eventually copy all of the contents back to the original image,
  947. // and we can't write potentially uninitialized data. The only exception is when the caller specifies the image
  948. // contents should be discarded in which he guarantees he will overwrite the entire locked area with his own
  949. // contents.
  950. bool needRead = options != GBL_WRITE_ONLY_DISCARD_RANGE && options != GBL_WRITE_ONLY_DISCARD;
  951. // Allocate a staging buffer
  952. mStagingBuffer = createStaging(device, lockedArea, needRead);
  953. if (needRead) // If reading, we need to copy the current contents of the image to the staging buffer
  954. {
  955. VulkanTransferBuffer* transferCB = cbManager.getTransferBuffer(deviceIdx, queueType, localQueueIdx);
  956. // Similar to above, if image supports GPU writes or is currently being written to, we need to wait on any
  957. // potential writes to complete
  958. UINT32 writeUseMask = subresource->getUseInfo(VulkanUseFlag::Write);
  959. if (mSupportsGPUWrites || writeUseMask != 0)
  960. {
  961. // Ensure flush() will wait for all queues currently writing to the image (if any) to finish
  962. transferCB->appendMask(writeUseMask);
  963. }
  964. VkImageSubresourceRange range;
  965. range.aspectMask = image->getAspectFlags();
  966. range.baseArrayLayer = face;
  967. range.layerCount = 1;
  968. range.baseMipLevel = mipLevel;
  969. range.levelCount = 1;
  970. VkImageSubresourceLayers rangeLayers;
  971. if ((props.getUsage() & TU_DEPTHSTENCIL) != 0)
  972. rangeLayers.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
  973. else
  974. rangeLayers.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  975. rangeLayers.baseArrayLayer = range.baseArrayLayer;
  976. rangeLayers.layerCount = range.layerCount;
  977. rangeLayers.mipLevel = range.baseMipLevel;
  978. VkExtent3D extent;
  979. PixelUtil::getSizeForMipLevel(props.getWidth(), props.getHeight(), props.getDepth(), mMappedMip,
  980. extent.width, extent.height, extent.depth);
  981. // Transfer texture to a valid layout
  982. VkAccessFlags currentAccessMask = image->getAccessFlags(subresource->getLayout());
  983. transferCB->setLayout(image->getHandle(), currentAccessMask, VK_ACCESS_TRANSFER_READ_BIT, subresource->getLayout(),
  984. VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, range);
  985. // Queue copy command
  986. image->copy(transferCB, mStagingBuffer, extent, rangeLayers, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
  987. // Transfer back to original layout
  988. VkImageLayout dstLayout = image->getOptimalLayout();
  989. currentAccessMask = image->getAccessFlags(dstLayout);
  990. transferCB->setLayout(image->getHandle(), VK_ACCESS_TRANSFER_READ_BIT, currentAccessMask,
  991. VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, dstLayout, range);
  992. transferCB->getCB()->registerResource(image, range, VulkanUseFlag::Read, ResourceUsage::Transfer);
  993. // Ensure data written to the staging buffer is visible
  994. VkAccessFlags stagingAccessFlags;
  995. if (options == GBL_READ_ONLY)
  996. stagingAccessFlags = VK_ACCESS_HOST_READ_BIT;
  997. else // Must be read/write
  998. stagingAccessFlags = VK_ACCESS_HOST_READ_BIT | VK_ACCESS_HOST_WRITE_BIT;
  999. transferCB->memoryBarrier(mStagingBuffer->getHandle(),
  1000. VK_ACCESS_TRANSFER_WRITE_BIT,
  1001. stagingAccessFlags,
  1002. VK_PIPELINE_STAGE_TRANSFER_BIT,
  1003. VK_PIPELINE_STAGE_HOST_BIT);
  1004. // Submit the command buffer and wait until it finishes
  1005. transferCB->flush(true);
  1006. }
  1007. UINT8* data = mStagingBuffer->map(0, lockedArea.getSize());
  1008. lockedArea.setExternalBuffer(data);
  1009. return lockedArea;
  1010. }
  1011. void VulkanTexture::unlockImpl()
  1012. {
  1013. // Possibly map() failed with some error
  1014. if (!mIsMapped)
  1015. return;
  1016. // Note: If we did any writes they need to be made visible to the GPU. However there is no need to execute
  1017. // a pipeline barrier because (as per spec) host writes are implicitly visible to the device.
  1018. if (mStagingBuffer == nullptr)
  1019. mImages[mMappedDeviceIdx]->unmap();
  1020. else
  1021. {
  1022. mStagingBuffer->unmap();
  1023. bool isWrite = mMappedLockOptions != GBL_READ_ONLY;
  1024. // We the caller wrote anything to the staging buffer, we need to upload it back to the main buffer
  1025. if (isWrite)
  1026. {
  1027. VulkanRenderAPI& rapi = static_cast<VulkanRenderAPI&>(RenderAPI::instance());
  1028. VulkanDevice& device = *rapi._getDevice(mMappedDeviceIdx);
  1029. VulkanCommandBufferManager& cbManager = gVulkanCBManager();
  1030. GpuQueueType queueType;
  1031. UINT32 localQueueIdx = CommandSyncMask::getQueueIdxAndType(mMappedGlobalQueueIdx, queueType);
  1032. VulkanImage* image = mImages[mMappedDeviceIdx];
  1033. VulkanTransferBuffer* transferCB = cbManager.getTransferBuffer(mMappedDeviceIdx, queueType, localQueueIdx);
  1034. VulkanImageSubresource* subresource = image->getSubresource(mMappedFace, mMappedMip);
  1035. VkImageLayout curLayout = subresource->getLayout();
  1036. // If the subresource is used in any way on the GPU, we need to wait for that use to finish before
  1037. // we issue our copy
  1038. UINT32 useMask = subresource->getUseInfo(VulkanUseFlag::Read | VulkanUseFlag::Write);
  1039. bool isNormalWrite = false;
  1040. if (useMask != 0) // Subresource is currently used on the GPU
  1041. {
  1042. // Try to avoid the wait by checking for special write conditions
  1043. // Caller guarantees he won't touch the same data as the GPU, so just copy
  1044. if (mMappedLockOptions == GBL_WRITE_ONLY_NO_OVERWRITE)
  1045. {
  1046. // Fall through to copy()
  1047. }
  1048. // Caller doesn't care about buffer contents, so just discard the existing buffer and create a new one
  1049. else if (mMappedLockOptions == GBL_WRITE_ONLY_DISCARD)
  1050. {
  1051. // We need to discard the entire image, even though we're only writing to a single sub-resource
  1052. image->destroy();
  1053. image = createImage(device, mInternalFormats[mMappedDeviceIdx]);
  1054. mImages[mMappedDeviceIdx] = image;
  1055. subresource = image->getSubresource(mMappedFace, mMappedMip);
  1056. }
  1057. else // Otherwise we have no choice but to issue a dependency between the queues
  1058. {
  1059. transferCB->appendMask(useMask);
  1060. isNormalWrite = true;
  1061. }
  1062. }
  1063. else
  1064. isNormalWrite = true;
  1065. const TextureProperties& props = getProperties();
  1066. // Check if the subresource will still be bound somewhere after the CBs using it finish
  1067. if (isNormalWrite)
  1068. {
  1069. UINT32 useCount = subresource->getUseCount();
  1070. UINT32 boundCount = subresource->getBoundCount();
  1071. bool isBoundWithoutUse = boundCount > useCount;
  1072. // If image is queued for some operation on a CB, then we need to make a copy of the subresource to
  1073. // avoid modifying its use in the previous operation
  1074. if (isBoundWithoutUse)
  1075. {
  1076. VulkanImage* newImage = createImage(device, mInternalFormats[mMappedDeviceIdx]);
  1077. // Avoid copying original contents if the image only has one sub-resource, which we'll overwrite anyway
  1078. if (props.getNumMipmaps() > 0 || props.getNumFaces() > 1)
  1079. {
  1080. VkImageLayout oldImgLayout = image->getOptimalLayout();
  1081. curLayout = newImage->getOptimalLayout();
  1082. copyImage(transferCB, image, newImage, oldImgLayout, curLayout);
  1083. }
  1084. image->destroy();
  1085. image = newImage;
  1086. mImages[mMappedDeviceIdx] = image;
  1087. }
  1088. }
  1089. VkImageSubresourceRange range;
  1090. range.aspectMask = image->getAspectFlags();
  1091. range.baseArrayLayer = mMappedFace;
  1092. range.layerCount = 1;
  1093. range.baseMipLevel = mMappedMip;
  1094. range.levelCount = 1;
  1095. VkImageSubresourceLayers rangeLayers;
  1096. rangeLayers.aspectMask = range.aspectMask;
  1097. rangeLayers.baseArrayLayer = range.baseArrayLayer;
  1098. rangeLayers.layerCount = range.layerCount;
  1099. rangeLayers.mipLevel = range.baseMipLevel;
  1100. VkExtent3D extent;
  1101. PixelUtil::getSizeForMipLevel(props.getWidth(), props.getHeight(), props.getDepth(), mMappedMip,
  1102. extent.width, extent.height, extent.depth);
  1103. VkImageLayout transferLayout;
  1104. if (mDirectlyMappable)
  1105. transferLayout = VK_IMAGE_LAYOUT_GENERAL;
  1106. else
  1107. transferLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
  1108. // Transfer texture to a valid layout
  1109. VkAccessFlags currentAccessMask = image->getAccessFlags(curLayout);
  1110. transferCB->setLayout(image->getHandle(), currentAccessMask, VK_ACCESS_TRANSFER_WRITE_BIT,
  1111. curLayout, transferLayout, range);
  1112. // Queue copy command
  1113. mStagingBuffer->copy(transferCB->getCB(), image, extent, rangeLayers, transferLayout);
  1114. // Transfer back to original (or optimal if initial layout was undefined/preinitialized)
  1115. VkImageLayout dstLayout = image->getOptimalLayout();
  1116. currentAccessMask = image->getAccessFlags(dstLayout);
  1117. transferCB->setLayout(image->getHandle(), VK_ACCESS_TRANSFER_WRITE_BIT, currentAccessMask,
  1118. transferLayout, dstLayout, range);
  1119. // Notify the command buffer that these resources are being used on it
  1120. transferCB->getCB()->registerResource(mStagingBuffer, VK_ACCESS_TRANSFER_READ_BIT, VulkanUseFlag::Read);
  1121. transferCB->getCB()->registerResource(image, range, VulkanUseFlag::Write, ResourceUsage::Transfer);
  1122. // We don't actually flush the transfer buffer here since it's an expensive operation, but it's instead
  1123. // done automatically before next "normal" command buffer submission.
  1124. }
  1125. mStagingBuffer->destroy();
  1126. mStagingBuffer = nullptr;
  1127. }
  1128. mIsMapped = false;
  1129. }
  1130. void VulkanTexture::readDataImpl(PixelData& dest, UINT32 mipLevel, UINT32 face, UINT32 deviceIdx, UINT32 queueIdx)
  1131. {
  1132. if (mProperties.getNumSamples() > 1)
  1133. {
  1134. LOGERR("Multisampled textures cannot be accessed from the CPU directly.");
  1135. return;
  1136. }
  1137. PixelData myData = lock(GBL_READ_ONLY, mipLevel, face, deviceIdx, queueIdx);
  1138. PixelUtil::bulkPixelConversion(myData, dest);
  1139. unlock();
  1140. BS_INC_RENDER_STAT_CAT(ResRead, RenderStatObject_Texture);
  1141. }
  1142. void VulkanTexture::writeDataImpl(const PixelData& src, UINT32 mipLevel, UINT32 face, bool discardWholeBuffer,
  1143. UINT32 queueIdx)
  1144. {
  1145. if (mProperties.getNumSamples() > 1)
  1146. {
  1147. LOGERR("Multisampled textures cannot be accessed from the CPU directly.");
  1148. return;
  1149. }
  1150. mipLevel = Math::clamp(mipLevel, (UINT32)mipLevel, mProperties.getNumMipmaps());
  1151. face = Math::clamp(face, (UINT32)0, mProperties.getNumFaces() - 1);
  1152. if (face > 0 && mProperties.getTextureType() == TEX_TYPE_3D)
  1153. {
  1154. LOGERR("3D texture arrays are not supported.");
  1155. return;
  1156. }
  1157. // Write to every device
  1158. for (UINT32 i = 0; i < BS_MAX_DEVICES; i++)
  1159. {
  1160. if (mImages[i] == nullptr)
  1161. continue;
  1162. PixelData myData = lock(discardWholeBuffer ? GBL_WRITE_ONLY_DISCARD : GBL_WRITE_ONLY_DISCARD_RANGE,
  1163. mipLevel, face, i, queueIdx);
  1164. PixelUtil::bulkPixelConversion(src, myData);
  1165. unlock();
  1166. }
  1167. BS_INC_RENDER_STAT_CAT(ResWrite, RenderStatObject_Texture);
  1168. }
  1169. }}