| 391 | } |
| 392 | |
| 393 | void VKRenderSystem::WriteTexture(Texture& texture, const TextureRegion& textureRegion, const SrcImageDescriptor& imageDesc) |
| 394 | { |
| 395 | auto& textureVK = LLGL_CAST(VKTexture&, texture); |
| 396 | |
| 397 | const auto& cfg = GetConfiguration(); |
| 398 | |
| 399 | /* Determine size of image for staging buffer */ |
| 400 | const auto& offset = textureRegion.offset; |
| 401 | const auto& extent = textureRegion.extent; |
| 402 | const auto& subresource = textureRegion.subresource; |
| 403 | const auto format = VKTypes::Unmap(textureVK.GetVkFormat()); |
| 404 | |
| 405 | auto image = textureVK.GetVkImage(); |
| 406 | const auto imageSize = extent.width * extent.height * extent.depth; |
| 407 | const void* imageData = nullptr; |
| 408 | const auto imageDataSize = static_cast<VkDeviceSize>(GetMemoryFootprint(format, imageSize)); |
| 409 | |
| 410 | /* Check if image data must be converted */ |
| 411 | ByteBuffer intermediateData; |
| 412 | |
| 413 | const auto& formatAttribs = GetFormatAttribs(format); |
| 414 | if (formatAttribs.bitSize > 0 && (formatAttribs.flags & FormatFlags::IsCompressed) == 0) |
| 415 | { |
| 416 | /* Convert image format (will be null if no conversion is necessary) */ |
| 417 | intermediateData = ConvertImageBuffer(imageDesc, formatAttribs.format, formatAttribs.dataType, cfg.threadCount); |
| 418 | } |
| 419 | |
| 420 | if (intermediateData) |
| 421 | { |
| 422 | /* |
| 423 | Validate that source image data was large enough so conversion is valid, |
| 424 | then use temporary image buffer as source for initial data |
| 425 | */ |
| 426 | const auto srcImageDataSize = imageSize * ImageFormatSize(imageDesc.format) * DataTypeSize(imageDesc.dataType); |
| 427 | AssertImageDataSize(imageDesc.dataSize, static_cast<std::size_t>(srcImageDataSize)); |
| 428 | imageData = intermediateData.get(); |
| 429 | } |
| 430 | else |
| 431 | { |
| 432 | /* |
| 433 | Validate that image data is large enough, |
| 434 | then use input data as source for initial data |
| 435 | */ |
| 436 | AssertImageDataSize(imageDesc.dataSize, static_cast<std::size_t>(imageDataSize)); |
| 437 | imageData = imageDesc.data; |
| 438 | } |
| 439 | |
| 440 | /* Create staging buffer */ |
| 441 | VkBufferCreateInfo stagingCreateInfo; |
| 442 | BuildVkBufferCreateInfo( |
| 443 | stagingCreateInfo, |
| 444 | imageDataSize, |
| 445 | VK_BUFFER_USAGE_TRANSFER_SRC_BIT // <-- TODO: support read/write mapping //GetStagingVkBufferUsageFlags(desc.cpuAccessFlags) |
| 446 | ); |
| 447 | |
| 448 | auto stagingBuffer = CreateStagingBuffer(stagingCreateInfo, imageData, imageDataSize); |
| 449 | |
| 450 | /* Copy staging buffer into hardware texture, then transfer image into sampling-ready state */ |
no test coverage detected