Vulkan and textures

Hello !

I am trying to load textures with Vulkan. I want to load png, or jpg or bmp or whatever. To have a row major order textures, I am using linear tiling, and after I will copy it into one with optimal tiling.
But maybe I am wrong, and please tell me if it is the case.

Before loading real texture, I am just “emulating” a simple gradient texture (dark on top and white on bottom).

So, I am creating an image like that :


VkImageCreateInfo info = {};
    info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
    info.pNext = nullptr;
    info.flags = 0;
    info.imageType = VK_IMAGE_TYPE_2D;
    info.format = VK_FORMAT_R8G8B8A8_SRGB;
    info.extent.width = 1024;
    info.extent.height = 1024;
    info.mipLevels = 1;
    info.arrayLayers = 1;
    info.samples = VK_SAMPLE_COUNT_1_BIT;
    info.tiling = VK_IMAGE_TILING_LINEAR;
    info.usage = VK_IMAGE_USAGE_SAMPLED_BIT;
    info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
    info.initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED;

    ... Get memory Requirement, alloc and binding

Since I am using linear tiling, I thought that piece of code will make it dark on top and white on bottom, but it is not the case (unfortunately).

(4096 should be the size in bytes of the line, but maybe it is not… (1024 * 4)).

for(int i = 0; i < 1024; ++i)
        memset(std::get<3>(mAllocation) + i * 4096, i * 255. / 1024, 4096);

After, I am using 4D position and 2D textures (4float, 2float) is a vertex

float vertices[] = {-1, -1, 1, 1, 0, 0,
                        1, -1, 1, 1, 1, 0,
                        -1, 1, 1, 1, 0, 1,
                        1, 1, 1, 1, 1};

And instead of getting a beautiful gradient, I got that …

Thanks for help !

Finally, I solved my issue. I forgot a value for the fourth vertex ^^. I feel like stupid x).

So, right now I am trying to load a texture, but I still have another issue…

I got an image with some black blocks … The original is a power of 2 png image

I tested with a jpg (non power of 2) and it works. So, I guess the problem could be from transparency, but it seems weird…

I recovered image with SDL :

 SDL_Surface *surface = IMG_Load(path.c_str());

    if(surface == nullptr)
        throw std::runtime_error("Image: " + path + " not found");

    SDL_PixelFormat format = *surface->format;
    format.BitsPerPixel = 32;
    format.BytesPerPixel = 4;
    SDL_Surface *surfaceConverted = SDL_ConvertSurface(surface, &format, SDL_SWSURFACE);

Then, I set the createImageInfo structure:

VkImageCreateInfo info = {};
    info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
    info.pNext = nullptr;
    info.flags = 0;
    info.imageType = VK_IMAGE_TYPE_2D;
    info.format = VK_FORMAT_R8G8B8A8_SRGB;
    info.extent.width = surfaceConverted->w;
    info.extent.height = surfaceConverted->h;
    info.mipLevels = 1;
    info.arrayLayers = 1;
    info.samples = VK_SAMPLE_COUNT_1_BIT;
    info.tiling = VK_IMAGE_TILING_LINEAR;
    info.usage = VK_IMAGE_USAGE_SAMPLED_BIT;
    info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
    info.initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED;

After I allocate and bind the memory, get the subResource layout, and memcpy to it :

char *mappedBytes = std::get<3>(mAllocation) + subResourceLayout.offset;
    char *data = (char*)surfaceConverted->pixels;

    for(int i = 0; i < surfaceConverted->h; ++i) {
        memcpy(mappedBytes, data, surfaceConverted->pitch);
        mappedBytes += subResourceLayout.rowPitch;
        data += surfaceConverted->pitch;
    }

Thanks !!