我设法加载jpg图像并将它们用作vulkan中的纹理,但是不同的图像会产生不同的结果,有些图像效果很好,有些图像效果不好。
这是与图像加载和格式转换相关的代码块: -
void Vulkan::createTextureImage()
{
SDL_Surface *tempImg;
SDL_RWops *rwop;
rwop = SDL_RWFromFile("Textures/img1.jpg", "rb");
tempImg = IMG_LoadJPG_RW(rwop);
SDL_Surface *image = SDL_ConvertSurfaceFormat(tempImg, SDL_PIXELFORMAT_ABGR8888, 0);
VkDeviceSize imageSize = image->format->BytesPerPixel * image->h * image->w;
if (!image)
{
throw std::runtime_error("failed to load texture image!");
}
VkImage stagingImage;
VkDeviceMemory staingImageMemory;
createImage(
image->w, image->h,
VK_FORMAT_R8G8B8A8_UNORM,
VK_IMAGE_TILING_LINEAR,
VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
stagingImage,
staingImageMemory);
std::cout << "the image size is " << imageSize << std::endl;
void * data;
vkMapMemory(device, staingImageMemory, 0, imageSize, 0, &data);
memcpy(data, image->pixels, (size_t)imageSize);
vkUnmapMemory(device, staingImageMemory);
createImage(
image->w, image->h,
VK_FORMAT_R8G8B8A8_UNORM,
VK_IMAGE_TILING_OPTIMAL,
VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
textureImage,
textureImageMemory);
transitionImageLayout(stagingImage, VK_FORMAT_R8G8B8A8_UNORM,
VK_IMAGE_LAYOUT_PREINITIALIZED,
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
transitionImageLayout(textureImage, VK_FORMAT_R8G8B8A8_UNORM,
VK_IMAGE_LAYOUT_PREINITIALIZED,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
copyImage(stagingImage, textureImage, image->w, image->h);
transitionImageLayout(textureImage, VK_FORMAT_R8G8B8A8_UNORM,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
}
我所做的唯一修改是翻转纹理的V分量来修复镜像图像
答案 0 :(得分:1)
看起来sdl的行间距与vulkan想要的行间距不匹配。
相反,您可以使用缓冲区从暂存内存中使用vkCmdCopyBufferToImage
进行图像复制而不是图像到图像blit,您可以在VkBufferImageCopy
结构中明确传递行间距。