在Vulkan中使用SDL_image进行纹理时出现意外行为

时间:2017-04-14 15:27:56

标签: textures sdl-2 texture-mapping vulkan sdl-image

我设法加载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分量来修复镜像图像

图片结果: no problem loading this image

这个没有很好地映射 enter image description here

虽然它应该显示这样的东西 enter image description here

1 个答案:

答案 0 :(得分:1)

看起来sdl的行间距与vulkan想要的行间距不匹配。

相反,您可以使用缓冲区从暂存内存中使用vkCmdCopyBufferToImage进行图像复制而不是图像到图像blit,您可以在VkBufferImageCopy结构中明确传递行间距。