Hi! I’ve problems when copying a buffer to an image with an offset.
I want to make an equivalent of glTexSubImage2D…
But when I’m trying this :
void Texture::update(const sf::Uint8* pixels, unsigned int texWidth, unsigned int texHeight, unsigned int x, unsigned int y) {
VkDeviceSize imageSize = texWidth * texHeight * 4;
if (!pixels) {
throw std::runtime_error("échec du chargement d'une image!");
}
VkBuffer stagingBuffer;
VkDeviceMemory stagingBufferMemory;
createBuffer(imageSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, stagingBuffer, stagingBufferMemory);
void* data;
vkMapMemory(vkDevice.getDevice(), stagingBufferMemory, 0, imageSize, 0, &data);
memcpy(data, pixels, static_cast<size_t>(imageSize));
vkUnmapMemory(vkDevice.getDevice(), stagingBufferMemory);
createImage(texWidth, texHeight, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, textureImage, textureImageMemory);
transitionImageLayout(textureImage, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
copyBufferToImage(stagingBuffer, textureImage, static_cast<uint32_t>(texWidth), static_cast<uint32_t>(texHeight),static_cast<uint32_t>(x), static_cast<uint32_t>(y));
transitionImageLayout(textureImage, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
vkDestroyBuffer(vkDevice.getDevice(), stagingBuffer, nullptr);
vkFreeMemory(vkDevice.getDevice(), stagingBufferMemory, nullptr);
createTextureImageView(VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_ASPECT_COLOR_BIT);
createTextureSampler();
}
void Texture::copyBufferToImage(VkBuffer buffer, VkImage image, uint32_t width, uint32_t height, uint32_t x, uint32_t y) {
VkCommandBuffer commandBuffer = beginSingleTimeCommands();
VkBufferImageCopy region{};
region.bufferOffset = 0;
region.bufferRowLength = 0;
region.bufferImageHeight = 0;
region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
region.imageSubresource.mipLevel = 0;
region.imageSubresource.baseArrayLayer = 0;
region.imageSubresource.layerCount = 1;
region.imageOffset = {x, y, 0};
region.imageExtent = {
width,
height,
1
};
std::cout<<"offsets : "<<x<<','<<y<<std::endl<<"size : "<<width<<","<<height<<std::endl;
vkCmdCopyBufferToImage(commandBuffer, buffer, image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ®ion);
endSingleTimeCommands(commandBuffer);
}
I got errors :
validation layer: vkCmdCopyBufferToImage(): pRegions[0].imageOffset.y (3) and (imageExtent.height + imageOffset.y) (19) must be >= zero or <= image subresource height (16).
The Vulkan spec states: For each element of pRegions, imageOffset.y and (imageExtent.height + imageOffset.y) must both be greater than or equal to 0 and less than or equal to the height of the specified imageSubresource of dstImage (https://vulkan.lunarg.com/doc/view/1.4.309.0/windows/antora/spec/latest/chapters/copies.html#VUID-vkCmdCopyBufferToImage-imageSubresource-07972)
validation layer: vkCmdCopyBufferToImage(): pRegions[0] exceeds image bounds
region extent (width = 17, height = 16, depth = 1)
region offset (x = 0, y = 3, z = 0)
image extent (width = 17, height = 16, depth = 1)
The Vulkan spec states: The image region specified by each element of pRegions must be contained within the specified imageSubresource of dstImage (https://vulkan.lunarg.com/doc/view/1.4.309.0/windows/antora/spec/latest/chapters/copies.html#VUID-vkCmdCopyBufferToImage-imageSubresource-07970)
Or with opengl I haven’t any problem, what values have I to set ? What’s the difference with glTexSubImage2D ?
Thanks.