Texture.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. /**
  2. * Copyright (c) 2006-2024 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "Texture.h"
  21. #include "Graphics.h"
  22. #include "Vulkan.h"
  23. #include <limits>
  24. namespace love
  25. {
  26. namespace graphics
  27. {
  28. namespace vulkan
  29. {
  30. Texture::Texture(love::graphics::Graphics *gfx, const Settings &settings, const Slices *data)
  31. : love::graphics::Texture(gfx, settings, data)
  32. , vgfx(dynamic_cast<Graphics*>(gfx))
  33. , slices(settings.type)
  34. , imageAspect(0)
  35. {
  36. if (data)
  37. slices = *data;
  38. loadVolatile();
  39. // ImageData is referenced by the first loadVolatile call, but we don't
  40. // hang on to it after that so we can save memory.
  41. slices.clear();
  42. }
  43. Texture::Texture(love::graphics::Graphics *gfx, love::graphics::Texture *base, const Texture::ViewSettings &viewsettings)
  44. : love::graphics::Texture(gfx, base, viewsettings)
  45. , vgfx(dynamic_cast<Graphics*>(gfx))
  46. , slices(viewsettings.type.get(base->getTextureType()))
  47. , imageAspect(0)
  48. {
  49. loadVolatile();
  50. }
  51. bool Texture::loadVolatile()
  52. {
  53. allocator = vgfx->getVmaAllocator();
  54. device = vgfx->getDevice();
  55. bool root = rootView.texture == this;
  56. if (isPixelFormatDepth(format))
  57. imageAspect |= VK_IMAGE_ASPECT_DEPTH_BIT;
  58. if (isPixelFormatStencil(format))
  59. imageAspect |= VK_IMAGE_ASPECT_STENCIL_BIT;
  60. if (isPixelFormatColor(format))
  61. imageAspect |= VK_IMAGE_ASPECT_COLOR_BIT;
  62. auto vulkanFormat = Vulkan::getTextureFormat(format);
  63. VkImageUsageFlags usageFlags =
  64. VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
  65. VK_IMAGE_USAGE_TRANSFER_DST_BIT;
  66. if (readable)
  67. usageFlags |= VK_IMAGE_USAGE_SAMPLED_BIT;
  68. if (computeWrite)
  69. usageFlags |= VK_IMAGE_USAGE_STORAGE_BIT;
  70. if (renderTarget)
  71. {
  72. if (isPixelFormatDepthStencil(format))
  73. usageFlags |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
  74. else
  75. usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
  76. }
  77. layerCount = 1;
  78. if (texType == TEXTURE_2D_ARRAY)
  79. layerCount = getLayerCount();
  80. else if (texType == TEXTURE_CUBE)
  81. layerCount = 6;
  82. if (root)
  83. {
  84. VkImageCreateFlags createFlags = 0;
  85. std::vector<VkFormat> vkviewformats;
  86. for (PixelFormat viewformat : viewFormats)
  87. {
  88. if (viewformat != format)
  89. {
  90. createFlags |= VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT;
  91. TextureFormat f = Vulkan::getTextureFormat(viewformat);
  92. vkviewformats.push_back(f.internalFormat);
  93. }
  94. }
  95. if (texType == TEXTURE_CUBE || texType == TEXTURE_2D_ARRAY)
  96. createFlags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
  97. msaaSamples = vgfx->getMsaaCount(requestedMSAA);
  98. VkImageCreateInfo imageInfo{};
  99. imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
  100. imageInfo.flags = createFlags;
  101. imageInfo.imageType = Vulkan::getImageType(getTextureType());
  102. imageInfo.extent.width = static_cast<uint32_t>(pixelWidth);
  103. imageInfo.extent.height = static_cast<uint32_t>(pixelHeight);
  104. imageInfo.extent.depth = static_cast<uint32_t>(depth);
  105. imageInfo.arrayLayers = static_cast<uint32_t>(layerCount);
  106. imageInfo.mipLevels = static_cast<uint32_t>(mipmapCount);
  107. imageInfo.format = vulkanFormat.internalFormat;
  108. imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
  109. imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
  110. imageInfo.usage = usageFlags;
  111. imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
  112. imageInfo.samples = msaaSamples;
  113. VkImageFormatListCreateInfo viewFormatsInfo{};
  114. viewFormatsInfo.sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO;
  115. if (!vkviewformats.empty() && vgfx->getDeviceApiVersion() >= VK_API_VERSION_1_2)
  116. {
  117. viewFormatsInfo.viewFormatCount = (uint32)vkviewformats.size();
  118. viewFormatsInfo.pViewFormats = vkviewformats.data();
  119. imageInfo.pNext = &viewFormatsInfo;
  120. }
  121. VmaAllocationCreateInfo imageAllocationCreateInfo{};
  122. if (vmaCreateImage(allocator, &imageInfo, &imageAllocationCreateInfo, &textureImage, &textureImageAllocation, nullptr) != VK_SUCCESS)
  123. throw love::Exception("failed to create image");
  124. auto commandBuffer = vgfx->getCommandBufferForDataTransfer();
  125. if (isPixelFormatDepthStencil(format))
  126. imageLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
  127. else if (computeWrite)
  128. imageLayout = VK_IMAGE_LAYOUT_GENERAL;
  129. else
  130. imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
  131. Vulkan::cmdTransitionImageLayout(commandBuffer, textureImage,
  132. VK_IMAGE_LAYOUT_UNDEFINED, imageLayout,
  133. 0, VK_REMAINING_MIP_LEVELS,
  134. 0, VK_REMAINING_ARRAY_LAYERS);
  135. bool hasdata = slices.get(0, 0) != nullptr;
  136. if (hasdata)
  137. {
  138. for (int mip = 0; mip < getMipmapCount(); mip++)
  139. {
  140. int sliceCount;
  141. if (texType == TEXTURE_CUBE)
  142. sliceCount = 6;
  143. else
  144. sliceCount = slices.getSliceCount();
  145. for (int slice = 0; slice < sliceCount; slice++)
  146. {
  147. auto id = slices.get(slice, mip);
  148. if (id != nullptr)
  149. uploadImageData(id, mip, slice, 0, 0);
  150. }
  151. }
  152. }
  153. else
  154. clear();
  155. }
  156. else
  157. {
  158. Texture *roottex = (Texture *) rootView.texture;
  159. textureImage = roottex->textureImage;
  160. textureImageAllocation = VK_NULL_HANDLE;
  161. imageLayout = roottex->imageLayout;
  162. msaaSamples = roottex->msaaSamples;
  163. }
  164. createTextureImageView();
  165. setSamplerState(samplerState);
  166. if (root && !isPixelFormatDepthStencil(format) && slices.getMipmapCount() <= 1 && getMipmapsMode() != MIPMAPS_NONE)
  167. generateMipmaps();
  168. if (renderTarget)
  169. {
  170. renderTargetImageViews.resize(getMipmapCount());
  171. for (int mip = 0; mip < getMipmapCount(); mip++)
  172. {
  173. renderTargetImageViews.at(mip).resize(layerCount);
  174. for (int slice = 0; slice < layerCount; slice++)
  175. {
  176. VkImageViewCreateInfo viewInfo{};
  177. viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
  178. viewInfo.image = textureImage;
  179. viewInfo.viewType = Vulkan::getImageViewType(getTextureType());
  180. viewInfo.format = vulkanFormat.internalFormat;
  181. viewInfo.subresourceRange.aspectMask = imageAspect;
  182. viewInfo.subresourceRange.baseMipLevel = mip + rootView.startMipmap;
  183. viewInfo.subresourceRange.levelCount = 1;
  184. viewInfo.subresourceRange.baseArrayLayer = slice + rootView.startLayer;
  185. viewInfo.subresourceRange.layerCount = 1;
  186. viewInfo.components.r = vulkanFormat.swizzleR;
  187. viewInfo.components.g = vulkanFormat.swizzleG;
  188. viewInfo.components.b = vulkanFormat.swizzleB;
  189. viewInfo.components.a = vulkanFormat.swizzleA;
  190. if (vkCreateImageView(device, &viewInfo, nullptr, &renderTargetImageViews.at(mip).at(slice)) != VK_SUCCESS)
  191. throw love::Exception("could not create render target image view");
  192. }
  193. }
  194. }
  195. int64 memsize = 0;
  196. if (root)
  197. {
  198. for (int mip = 0; mip < getMipmapCount(); mip++)
  199. {
  200. int w = getPixelWidth(mip);
  201. int h = getPixelHeight(mip);
  202. int slices = getDepth(mip) * layerCount;
  203. memsize += getPixelFormatSliceSize(format, w, h) * slices;
  204. }
  205. memsize *= static_cast<int>(msaaSamples);
  206. }
  207. setGraphicsMemorySize(memsize);
  208. if (!debugName.empty())
  209. {
  210. if (vgfx->getEnabledOptionalInstanceExtensions().debugInfo)
  211. {
  212. VkDebugUtilsObjectNameInfoEXT nameInfo{};
  213. nameInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT;
  214. if (root)
  215. {
  216. nameInfo.objectType = VK_OBJECT_TYPE_IMAGE;
  217. nameInfo.objectHandle = (uint64_t)textureImage;
  218. }
  219. else
  220. {
  221. nameInfo.objectType = VK_OBJECT_TYPE_IMAGE_VIEW;
  222. nameInfo.objectHandle = (uint64_t)textureImageView;
  223. }
  224. nameInfo.pObjectName = debugName.c_str();
  225. vkSetDebugUtilsObjectNameEXT(device, &nameInfo);
  226. }
  227. }
  228. return true;
  229. }
  230. void Texture::unloadVolatile()
  231. {
  232. if (textureImage == VK_NULL_HANDLE)
  233. return;
  234. vgfx->queueCleanUp([
  235. device = device,
  236. textureImageView = textureImageView,
  237. allocator = allocator,
  238. textureImage = textureImage,
  239. textureImageAllocation = textureImageAllocation,
  240. textureImageViews = std::move(renderTargetImageViews)] () {
  241. vkDestroyImageView(device, textureImageView, nullptr);
  242. if (textureImageAllocation)
  243. vmaDestroyImage(allocator, textureImage, textureImageAllocation);
  244. for (const auto &views : textureImageViews)
  245. for (const auto &view : views)
  246. vkDestroyImageView(device, view, nullptr);
  247. });
  248. textureImage = VK_NULL_HANDLE;
  249. textureImageAllocation = VK_NULL_HANDLE;
  250. setGraphicsMemorySize(0);
  251. }
  252. Texture::~Texture()
  253. {
  254. unloadVolatile();
  255. }
  256. ptrdiff_t Texture::getRenderTargetHandle() const
  257. {
  258. return (ptrdiff_t)textureImageView;
  259. }
  260. ptrdiff_t Texture::getSamplerHandle() const
  261. {
  262. return (ptrdiff_t)textureSampler;
  263. }
  264. VkImageView Texture::getRenderTargetView(int mip, int layer)
  265. {
  266. return renderTargetImageViews.at(mip).at(layer);
  267. }
  268. VkSampleCountFlagBits Texture::getMsaaSamples() const
  269. {
  270. return msaaSamples;
  271. }
  272. int Texture::getMSAA() const
  273. {
  274. return static_cast<int>(msaaSamples);
  275. }
  276. ptrdiff_t Texture::getHandle() const
  277. {
  278. return (ptrdiff_t)textureImage;
  279. }
  280. void Texture::setSamplerState(const SamplerState &s)
  281. {
  282. samplerState = validateSamplerState(s);
  283. textureSampler = vgfx->getCachedSampler(samplerState);
  284. }
  285. VkImageLayout Texture::getImageLayout() const
  286. {
  287. return imageLayout;
  288. }
  289. void Texture::createTextureImageView()
  290. {
  291. auto vulkanFormat = Vulkan::getTextureFormat(format);
  292. VkImageViewCreateInfo viewInfo{};
  293. viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
  294. viewInfo.image = textureImage;
  295. viewInfo.viewType = Vulkan::getImageViewType(getTextureType());
  296. viewInfo.format = vulkanFormat.internalFormat;
  297. viewInfo.subresourceRange.aspectMask = imageAspect;
  298. viewInfo.subresourceRange.baseMipLevel = rootView.startMipmap;
  299. viewInfo.subresourceRange.levelCount = getMipmapCount();
  300. viewInfo.subresourceRange.baseArrayLayer = rootView.startLayer;
  301. viewInfo.subresourceRange.layerCount = layerCount;
  302. viewInfo.components.r = vulkanFormat.swizzleR;
  303. viewInfo.components.g = vulkanFormat.swizzleG;
  304. viewInfo.components.b = vulkanFormat.swizzleB;
  305. viewInfo.components.a = vulkanFormat.swizzleA;
  306. if (vkCreateImageView(device, &viewInfo, nullptr, &textureImageView) != VK_SUCCESS)
  307. throw love::Exception("could not create texture image view");
  308. }
  309. void Texture::clear()
  310. {
  311. auto commandBuffer = vgfx->getCommandBufferForDataTransfer();
  312. VkImageSubresourceRange range{};
  313. range.aspectMask = imageAspect;
  314. range.baseMipLevel = 0;
  315. range.levelCount = VK_REMAINING_MIP_LEVELS;
  316. range.baseArrayLayer = 0;
  317. range.layerCount = VK_REMAINING_ARRAY_LAYERS;
  318. if (imageLayout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL)
  319. {
  320. Vulkan::cmdTransitionImageLayout(commandBuffer, textureImage,
  321. VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
  322. 0, VK_REMAINING_MIP_LEVELS, 0, VK_REMAINING_ARRAY_LAYERS);
  323. auto clearColor = getClearValue();
  324. vkCmdClearColorImage(commandBuffer, textureImage, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &clearColor, 1, &range);
  325. Vulkan::cmdTransitionImageLayout(commandBuffer, textureImage,
  326. VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
  327. 0, VK_REMAINING_MIP_LEVELS, 0, VK_REMAINING_ARRAY_LAYERS);
  328. }
  329. else if (imageLayout == VK_IMAGE_LAYOUT_GENERAL)
  330. {
  331. auto clearColor = getClearValue();
  332. vkCmdClearColorImage(commandBuffer, textureImage, VK_IMAGE_LAYOUT_GENERAL, &clearColor, 1, &range);
  333. }
  334. else
  335. {
  336. Vulkan::cmdTransitionImageLayout(commandBuffer, textureImage,
  337. imageLayout, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
  338. 0, VK_REMAINING_MIP_LEVELS, 0, VK_REMAINING_ARRAY_LAYERS);
  339. VkClearDepthStencilValue depthStencilColor{};
  340. depthStencilColor.depth = 0.0f;
  341. depthStencilColor.stencil = 0;
  342. vkCmdClearDepthStencilImage(commandBuffer, textureImage, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &depthStencilColor, 1, &range);
  343. Vulkan::cmdTransitionImageLayout(commandBuffer, textureImage,
  344. VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, imageLayout,
  345. 0, VK_REMAINING_MIP_LEVELS, 0, VK_REMAINING_ARRAY_LAYERS);
  346. }
  347. }
  348. VkClearColorValue Texture::getClearValue()
  349. {
  350. auto vulkanFormat = Vulkan::getTextureFormat(format);
  351. VkClearColorValue clearColor{};
  352. switch (vulkanFormat.internalFormatRepresentation)
  353. {
  354. case FORMATREPRESENTATION_FLOAT:
  355. clearColor.float32[0] = 0.0f;
  356. clearColor.float32[1] = 0.0f;
  357. clearColor.float32[2] = 0.0f;
  358. clearColor.float32[3] = 0.0f;
  359. break;
  360. case FORMATREPRESENTATION_SINT:
  361. clearColor.int32[0] = 0;
  362. clearColor.int32[1] = 0;
  363. clearColor.int32[2] = 0;
  364. clearColor.int32[3] = 0;
  365. break;
  366. case FORMATREPRESENTATION_UINT:
  367. clearColor.uint32[0] = 0;
  368. clearColor.uint32[1] = 0;
  369. clearColor.uint32[2] = 0;
  370. clearColor.uint32[3] = 0;
  371. break;
  372. }
  373. return clearColor;
  374. }
  375. void Texture::generateMipmapsInternal()
  376. {
  377. auto commandBuffer = vgfx->getCommandBufferForDataTransfer();
  378. if (imageLayout != VK_IMAGE_LAYOUT_GENERAL)
  379. Vulkan::cmdTransitionImageLayout(commandBuffer, textureImage,
  380. VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
  381. 0, static_cast<uint32_t>(getMipmapCount()), 0, static_cast<uint32_t>(layerCount));
  382. VkImageMemoryBarrier barrier{};
  383. barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
  384. barrier.image = textureImage;
  385. barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  386. barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  387. barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  388. barrier.subresourceRange.baseArrayLayer = rootView.startLayer;
  389. barrier.subresourceRange.layerCount = static_cast<uint32_t>(layerCount);
  390. barrier.subresourceRange.baseMipLevel = rootView.startMipmap;
  391. barrier.subresourceRange.levelCount = 1u;
  392. uint32_t mipLevels = static_cast<uint32_t>(getMipmapCount());
  393. for (uint32_t i = 1; i < mipLevels; i++)
  394. {
  395. barrier.subresourceRange.baseMipLevel = rootView.startMipmap + i - 1;
  396. barrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
  397. barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
  398. barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
  399. barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
  400. if (imageLayout != VK_IMAGE_LAYOUT_GENERAL)
  401. vkCmdPipelineBarrier(commandBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0,
  402. 0, nullptr,
  403. 0, nullptr,
  404. 1, &barrier);
  405. VkImageBlit blit{};
  406. blit.srcOffsets[0] = { 0, 0, 0 };
  407. blit.srcOffsets[1] = { getPixelWidth(i - 1), getPixelHeight(i - 1), 1 };
  408. blit.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  409. blit.srcSubresource.mipLevel = rootView.startMipmap + i - 1;
  410. blit.srcSubresource.baseArrayLayer = rootView.startLayer;
  411. blit.srcSubresource.layerCount = static_cast<uint32_t>(layerCount);
  412. blit.dstOffsets[0] = { 0, 0, 0 };
  413. blit.dstOffsets[1] = { getPixelWidth(i), getPixelHeight(i), 1 };
  414. blit.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  415. blit.dstSubresource.mipLevel = rootView.startMipmap + i;
  416. blit.dstSubresource.baseArrayLayer = rootView.startLayer;
  417. blit.dstSubresource.layerCount = static_cast<uint32_t>(layerCount);
  418. vkCmdBlitImage(commandBuffer,
  419. textureImage, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
  420. textureImage, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
  421. 1, &blit,
  422. VK_FILTER_LINEAR);
  423. barrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
  424. barrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
  425. barrier.srcAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
  426. barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
  427. if (imageLayout != VK_IMAGE_LAYOUT_GENERAL)
  428. vkCmdPipelineBarrier(commandBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, 0,
  429. 0, nullptr,
  430. 0, nullptr,
  431. 1, &barrier);
  432. }
  433. barrier.subresourceRange.baseMipLevel = rootView.startMipmap + mipLevels - 1;
  434. barrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
  435. barrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
  436. barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
  437. barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
  438. if (imageLayout != VK_IMAGE_LAYOUT_GENERAL)
  439. vkCmdPipelineBarrier(commandBuffer,
  440. VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, 0,
  441. 0, nullptr,
  442. 0, nullptr,
  443. 1, &barrier);
  444. }
  445. void Texture::uploadByteData(const void *data, size_t size, int level, int slice, const Rect &r)
  446. {
  447. VkBuffer stagingBuffer;
  448. VmaAllocation vmaAllocation;
  449. VkBufferCreateInfo bufferCreateInfo{};
  450. bufferCreateInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
  451. bufferCreateInfo.size = size;
  452. bufferCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
  453. VmaAllocationCreateInfo allocCreateInfo = {};
  454. allocCreateInfo.usage = VMA_MEMORY_USAGE_AUTO;
  455. allocCreateInfo.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT;
  456. VmaAllocationInfo allocInfo;
  457. vmaCreateBuffer(allocator, &bufferCreateInfo, &allocCreateInfo, &stagingBuffer, &vmaAllocation, &allocInfo);
  458. memcpy(allocInfo.pMappedData, data, size);
  459. VkBufferImageCopy region{};
  460. region.bufferOffset = 0;
  461. region.bufferRowLength = 0;
  462. region.bufferImageHeight = 0;
  463. uint32_t baseLayer = rootView.startLayer;
  464. if (getTextureType() != TEXTURE_VOLUME)
  465. baseLayer += slice;
  466. level += rootView.startMipmap;
  467. region.imageSubresource.aspectMask = imageAspect;
  468. region.imageSubresource.mipLevel = level;
  469. region.imageSubresource.baseArrayLayer = baseLayer;
  470. region.imageSubresource.layerCount = 1;
  471. region.imageOffset = { r.x, r.y, 0 };
  472. region.imageExtent = {
  473. static_cast<uint32_t>(r.w),
  474. static_cast<uint32_t>(r.h), 1
  475. };
  476. if (getTextureType() == TEXTURE_VOLUME)
  477. region.imageOffset.z = slice;
  478. auto commandBuffer = vgfx->getCommandBufferForDataTransfer();
  479. if (imageLayout != VK_IMAGE_LAYOUT_GENERAL)
  480. {
  481. Vulkan::cmdTransitionImageLayout(commandBuffer, textureImage,
  482. imageLayout, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
  483. level, 1, baseLayer, 1);
  484. vkCmdCopyBufferToImage(
  485. commandBuffer,
  486. stagingBuffer,
  487. textureImage,
  488. VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
  489. 1,
  490. &region
  491. );
  492. Vulkan::cmdTransitionImageLayout(commandBuffer, textureImage,
  493. VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, imageLayout,
  494. level, 1, baseLayer, 1);
  495. }
  496. else
  497. vkCmdCopyBufferToImage(
  498. commandBuffer,
  499. stagingBuffer,
  500. textureImage,
  501. imageLayout,
  502. 1,
  503. &region
  504. );
  505. vgfx->queueCleanUp([allocator = allocator, stagingBuffer, vmaAllocation]() {
  506. vmaDestroyBuffer(allocator, stagingBuffer, vmaAllocation);
  507. });
  508. }
  509. void Texture::copyFromBuffer(graphics::Buffer *source, size_t sourceoffset, int sourcewidth, size_t size, int slice, int mipmap, const Rect &rect)
  510. {
  511. auto commandBuffer = vgfx->getCommandBufferForDataTransfer();
  512. VkImageSubresourceLayers layers{};
  513. layers.aspectMask = imageAspect;
  514. layers.mipLevel = mipmap + rootView.startMipmap;
  515. layers.baseArrayLayer = slice + rootView.startLayer;
  516. layers.layerCount = 1;
  517. VkBufferImageCopy region{};
  518. region.bufferOffset = sourceoffset;
  519. region.bufferRowLength = sourcewidth;
  520. region.bufferImageHeight = 1;
  521. region.imageSubresource = layers;
  522. region.imageExtent.width = static_cast<uint32_t>(rect.w);
  523. region.imageExtent.height = static_cast<uint32_t>(rect.h);
  524. if (imageLayout != VK_IMAGE_LAYOUT_GENERAL)
  525. {
  526. Vulkan::cmdTransitionImageLayout(commandBuffer, textureImage, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
  527. vkCmdCopyBufferToImage(commandBuffer, (VkBuffer)source->getHandle(), textureImage, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region);
  528. Vulkan::cmdTransitionImageLayout(commandBuffer, textureImage, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
  529. }
  530. else
  531. vkCmdCopyBufferToImage(commandBuffer, (VkBuffer)source->getHandle(), textureImage, VK_IMAGE_LAYOUT_GENERAL, 1, &region);
  532. }
  533. void Texture::copyToBuffer(graphics::Buffer *dest, int slice, int mipmap, const Rect &rect, size_t destoffset, int destwidth, size_t size)
  534. {
  535. auto commandBuffer = vgfx->getCommandBufferForDataTransfer();
  536. VkImageSubresourceLayers layers{};
  537. layers.aspectMask = imageAspect;
  538. layers.mipLevel = mipmap + rootView.startMipmap;
  539. layers.baseArrayLayer = slice + rootView.startLayer;
  540. layers.layerCount = 1;
  541. VkBufferImageCopy region{};
  542. region.bufferOffset = destoffset;
  543. region.bufferRowLength = destwidth;
  544. region.bufferImageHeight = 0;
  545. region.imageSubresource = layers;
  546. region.imageExtent.width = static_cast<uint32_t>(rect.w);
  547. region.imageExtent.height = static_cast<uint32_t>(rect.h);
  548. region.imageExtent.depth = 1;
  549. if (imageLayout != VK_IMAGE_LAYOUT_GENERAL)
  550. {
  551. Vulkan::cmdTransitionImageLayout(commandBuffer, textureImage, imageLayout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
  552. vkCmdCopyImageToBuffer(commandBuffer, textureImage, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, (VkBuffer) dest->getHandle(), 1, &region);
  553. Vulkan::cmdTransitionImageLayout(commandBuffer, textureImage, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, imageLayout);
  554. }
  555. else
  556. vkCmdCopyImageToBuffer(commandBuffer, textureImage, VK_IMAGE_LAYOUT_GENERAL, (VkBuffer)dest->getHandle(), 1, &region);
  557. }
  558. } // vulkan
  559. } // graphics
  560. } // love