Jump to content
  • Advertisement
Sign in to follow this  

OpenGL [Vulkan] Descriptor binding point confusion / Uniform buffer memory barriers / Compressed images

This topic is 993 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

#Q1 - Descriptor Binding Points

I feel like I'm starting to get the hang of the API, but there's still a couple of things that are unclear to me.

For instance, how do I actually change the binding point for a descriptor?

I have a small test fragment shader with 2 uniform descriptor sets at binding point 0:

#version 400

#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable

out vec4 fs_color;

layout(std140, set = 0, binding = 0) uniform testa {
	vec4 color;
} u_testa;

layout(std140, set = 1, binding = 0) uniform testb {
	vec4 color;
} u_testb;

void main()
	fs_color = (u_testa.color +u_testb.color) /2.0;

In my test program they're initialized like this: (This is essentially a copy of the "multiple_sets" demo from the Vulkan SDK)

static const unsigned descriptor_set_count = 2;
const int binding_point = 0; // The shader binding point

VkDescriptorSetLayoutBinding uniform_binding[1] = {};
uniform_binding[0].binding = binding_point;
uniform_binding[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
uniform_binding[0].descriptorCount = 1;
uniform_binding[0].stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
uniform_binding[0].pImmutableSamplers = NULL;
VkDescriptorSetLayoutCreateInfo uniform_layout_info[1] = {};
uniform_layout_info[0].sType =
uniform_layout_info[0].pNext = NULL;
uniform_layout_info[0].bindingCount = 1;
uniform_layout_info[0].pBindings = uniform_binding;

VkDescriptorSetLayoutBinding uniform_binding2[1] = {};
uniform_binding2[0].binding = binding_point;
uniform_binding2[0].descriptorType =
uniform_binding2[0].descriptorCount = 1;
uniform_binding2[0].stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
uniform_binding2[0].pImmutableSamplers = NULL;
VkDescriptorSetLayoutCreateInfo uniform_layout_info2[1] = {};
uniform_layout_info2[0].sType =
uniform_layout_info2[0].pNext = NULL;
uniform_layout_info2[0].bindingCount = 1;
uniform_layout_info2[0].pBindings = uniform_binding2;

// Create multiple sets, using each createInfo
static const unsigned uniform_set_index = 0;
static const unsigned uniform_set_index2 = 1;
VkDescriptorSetLayout descriptor_layouts[descriptor_set_count] = {};
auto res = vkCreateDescriptorSetLayout(device, uniform_layout_info, NULL,
assert(res == VK_SUCCESS);
res = vkCreateDescriptorSetLayout(device, uniform_layout_info2, NULL,
assert(res == VK_SUCCESS);

// Create pipeline layout with multiple descriptor sets
VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo[1] = {};
pipelineLayoutCreateInfo[0].sType =
pipelineLayoutCreateInfo[0].pNext = NULL;
pipelineLayoutCreateInfo[0].pushConstantRangeCount = 0;
pipelineLayoutCreateInfo[0].pPushConstantRanges = NULL;
pipelineLayoutCreateInfo[0].setLayoutCount = descriptor_set_count;
pipelineLayoutCreateInfo[0].pSetLayouts = descriptor_layouts;
VkPipelineLayout pipeline_layout;
res = vkCreatePipelineLayout(device, pipelineLayoutCreateInfo, NULL,
assert(res == VK_SUCCESS);

// Create a single pool to contain data for our two descriptor sets
VkDescriptorPoolSize type_count[1] = {};
type_count[0].descriptorCount = 2;

VkDescriptorPoolCreateInfo pool_info[1] = {};
pool_info[0].pNext = NULL;
pool_info[0].maxSets = descriptor_set_count;
pool_info[0].poolSizeCount =
	sizeof(type_count) / sizeof(VkDescriptorPoolSize);
pool_info[0].pPoolSizes = type_count;

VkDescriptorPool descriptor_pool[1] = {};
res = vkCreateDescriptorPool(device, pool_info, NULL, descriptor_pool);
assert(res == VK_SUCCESS);

VkDescriptorSetAllocateInfo alloc_info[1];
alloc_info[0].pNext = NULL;
alloc_info[0].descriptorPool = descriptor_pool[0];
alloc_info[0].descriptorSetCount = descriptor_set_count;
alloc_info[0].pSetLayouts = descriptor_layouts;

// Populate descriptor sets
VkDescriptorSet descriptor_sets[descriptor_set_count];
descriptor_sets[descriptor_set_count] = {};
res = vkAllocateDescriptorSets(device, alloc_info, descriptor_sets);
assert(res == VK_SUCCESS);

// Using empty brace initializer on the next line triggers a bug in older
// versions of gcc, so memset instead
VkWriteDescriptorSet descriptor_writes[2];
memset(descriptor_writes, 0, sizeof(descriptor_writes));

VkDescriptorBufferInfo buffer_info;
buffer_info.buffer = colorBuffer;
buffer_info.offset = 0;
buffer_info.range = sizeof(glm::vec4);
// Populate with info about our uniform buffer
descriptor_writes[0] = {};
descriptor_writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptor_writes[0].pNext = NULL;
descriptor_writes[0].dstSet = descriptor_sets[uniform_set_index];
descriptor_writes[0].descriptorCount = 1;
descriptor_writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
descriptor_writes[0].pBufferInfo = &buffer_info;
descriptor_writes[0].dstArrayElement = 0;
descriptor_writes[0].dstBinding = binding_point;

VkDescriptorBufferInfo buffer_info2;
buffer_info2.buffer = colorBuffer2;
buffer_info2.offset = 0;
buffer_info2.range = sizeof(glm::vec4);
// Populate with info about our sampled image
descriptor_writes[1] = {};
descriptor_writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptor_writes[1].pNext = NULL;
descriptor_writes[1].dstSet = descriptor_sets[uniform_set_index2];
descriptor_writes[1].descriptorCount = 1;
descriptor_writes[1].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
descriptor_writes[1].pBufferInfo = &buffer_info2;
descriptor_writes[1].dstArrayElement = 0;
descriptor_writes[1].dstBinding = binding_point;

vkUpdateDescriptorSets(device, descriptor_set_count, descriptor_writes,
					   0, NULL);

'binding_point' is used in the 'VkDescriptorSetLayoutBinding' and 'VkDescriptorBufferInfo' structures.


During rendering, the descriptor sets are bound using:

vkCmdBindDescriptorSets(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
                        pipeline_layout, 0, descriptor_set_count,
                        descriptor_sets, 0, NULL);

This works fine, as long as the binding point is 0. If I change 'binding_point' to 1, and the binding points in the shader respectively, the program crashes at the 'vkUpdateDescriptorSets'-call with a write access violation.


What am I missing?


#Q2 - Uniform Buffer Memory Barriers

Another question, regarding uniform buffers:

Say I have a uniform for my MVP matrix. This uniform changes for every object that has to be rendered, so my approach was this:

1) Bind the shader pipeline

2) Map the memory for the MVP buffer, write the object matrix, unmap the memory

3) Bind the descriptor set for the uniform

4) Bind the vertex buffer

5) Run the draw-call

6) Repeat for all objects from step 2)


This works if there's just 1 object. If there's more than one, they all use the MVP from the very last object. Most likely because the draw-calls are delayed, so the MVP memory is overwritten before the previous draw-call has finished.

What can I do to prevent this? My first thought was to create a memory pipeline barrier with VK_ACCESS_UNIFORM_READ_BIT as source mask and VK_ACCESS_HOST_WRITE_BIT as destination access mask (=All uniform reads should finish before the host tries to write to the memory again), that didn't seem to have any effect however.


#Q3 - Compressed Images

One more question, regarding compressed images.

How can I achieve the equivalent of glGetCompressedTexImage with DXT1, DXT3 and DXT5 compressions in Vulkan? The equivalent format should be:




Is that correct? Since compressed images are supported directly by the hardware, I should be able to use the same image data I've used for glGetCompressedTexImage, and map it to the memory of a VkImage, true? So far the image has always ended up corrupted, is there anything else I need to take into account?

Edited by Silverlan

Share this post

Link to post
Share on other sites

I'm still struggling with compressed images.

Here's what the specification says about that:


Compressed texture images stored using the S3TC compressed image formats are represented as a collection of 4×4 texel blocks, where each block contains 64 or 128 bits of texel data. The image is encoded as a normal 2D raster image in which each 4×4 block is treated as a single pixel.

Source: https://www.khronos.org/registry/dataformat/specs/1.1/dataformat.1.1.html#S3TC



For images created with linear tiling, rowPitch, arrayPitch and depthPitch describe the layout of the subresource in linear memory. For uncompressed formats, rowPitch is the number of bytes between texels with the same x coordinate in adjacent rows (y coordinates differ by one). arrayPitch is the number of bytes between texels with the same x and y coordinate in adjacent array layers of the image (array layer values differ by one). depthPitch is the number of bytes between texels with the same x and y coordinate in adjacent slices of a 3D image (z coordinates differ by one). Expressed as an addressing formula, the starting byte of a texel in the subresource has address:

// (x,y,z,layer) are in texel coordinates

address(x,y,z,layer) = layer*arrayPitch + z*depthPitch + y*rowPitch + x*texelSize + offset

For compressed formats, the rowPitch is the number of bytes between compressed blocks in adjacent rows. arrayPitch is the number of bytes between blocks in adjacent array layers. depthPitch is the number of bytes between blocks in adjacent slices of a 3D image.

// (x,y,z,layer) are in block coordinates

address(x,y,z,layer) = layer*arrayPitch + z*depthPitch + y*rowPitch + x*blockSize + offset;

arrayPitch is undefined for images that were not created as arrays. depthPitch is defined only for 3D images.

For color formats, the aspectMask member of VkImageSubresource must be VK_IMAGE_ASPECT_COLOR_BIT. For depth/stencil formats, aspect must be either VK_IMAGE_ASPECT_DEPTH_BIT or VK_IMAGE_ASPECT_STENCIL_BIT. On implementations that store depth and stencil aspects separately, querying each of these subresource layouts will return a different offset and size representing the region of memory used for that aspect. On implementations that store depth and stencil aspects interleaved, the same offset and size are returned and represent the interleaved memory allocation.


Source: https://www.khronos.org/registry/vulkan/specs/1.0/xhtml/vkspec.html#resources-images


I'm using GLI to load the dds-data (Which is supposed to work with Vulkan, but I've also tried other libraries).

Here's my code for loading and mapping the data:

struct dds load_dds(const char *fileName)
    auto tex = gli::load_dds(fileName);
    auto format = tex.format();
    VkFormat vkFormat = static_cast<VkFormat>(format);
    auto extents = tex.extent();
    auto r = dds {};
    r.texture = new gli::texture(tex);
    r.width = extents.x;
    r.height = extents.y;
    r.format = vkFormat;
    return r;
void map_data_dds(struct dds *r,void *imgData,VkSubresourceLayout layout)
    auto &tex = *static_cast<gli::texture*>(r->texture);
    gli::storage storage {tex.format(),tex.extent(),tex.layers(),tex.faces(),tex.levels()};

    auto *srcData = static_cast<uint8_t*>(tex.data(0,0,0));
    auto *destData = static_cast<uint8_t*>(imgData); // Pointer to mapped memory of VkImage
    destData += layout.offset; // layout = VkImageLayout of the image
    auto extents = tex.extent();
    auto w = extents.x;
    auto h = extents.y;
    auto blockSize = storage.block_size();
    auto blockCount = storage.block_count(0);
    //auto blockExtent = storage.block_extent();

    auto method = 0; // All methods have the same result
    if(method == 0)
        for(auto y=decltype(blockCount.y){0};y<blockCount.y;++y)
            auto *rowDest = destData +y *layout.rowPitch;
            auto *rowSrc = srcData +y *(blockCount.x *blockSize);
            for(auto x=decltype(blockCount.x){0};x<blockCount.x;++x)
                auto *pxDest = rowDest +x *blockSize;
                auto *pxSrc = rowSrc +x *blockSize; // 4x4 image block
                memcpy(pxDest,pxSrc,blockSize); // 64Bit per block
                //memset(pxDest,128,blockSize); // 64Bit per block
    else if(method == 1)
        memcpy(destData,tex.data(0,0,0),tex.size(0)); // Just one layer for now
        //destData += tex.size(0);

Here's my code for initializing the texture (Which is 1:1 the same as the cube demo from the SDK, except for the dds-code):

static void demo_prepare_texture_image(struct demo *demo, const char *filename,
                                       struct texture_object *tex_obj,
                                       VkImageTiling tiling,
                                       VkImageUsageFlags usage,
                                       VkFlags required_props) {
    VkResult U_ASSERT_ONLY err;
    bool U_ASSERT_ONLY pass;
   /* const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
    int32_t tex_width;
    int32_t tex_height;
    if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
        printf("Failed to load textures\n");
    struct dds ddsData = load_dds("C:\\VulkanSDK\\\\Demos\\x64\\Debug\\iron01.dds");

    VkFormat tex_format = ddsData.format;
    int32_t tex_width = ddsData.width;
    int32_t tex_height = ddsData.height;

    tex_obj->tex_width = tex_width;
    tex_obj->tex_height = tex_height;

    const VkImageCreateInfo image_create_info = {
        .pNext = NULL,
        .imageType = VK_IMAGE_TYPE_2D,
        .format = tex_format,
        .extent = {tex_width, tex_height, 1},
        .mipLevels = 1,
        .arrayLayers = 1,
        .samples = VK_SAMPLE_COUNT_1_BIT,
        .tiling = tiling,
        .usage = usage,
        .flags = 0,
        .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,

    VkMemoryRequirements mem_reqs;

    err =
        vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);

    vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);

    tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
    tex_obj->mem_alloc.pNext = NULL;
    tex_obj->mem_alloc.allocationSize = mem_reqs.size;
    tex_obj->mem_alloc.memoryTypeIndex = 0;

    pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,

    /* allocate memory */
    err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,

    /* bind memory */
    err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);

    if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
        const VkImageSubresource subres = {
            .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
            .mipLevel = 0,
            .arrayLayer = 0,
        VkSubresourceLayout layout;
        void *data;

        vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres,

        err = vkMapMemory(demo->device, tex_obj->mem, 0,
                          tex_obj->mem_alloc.allocationSize, 0, &data);

        // DDS

       // if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
       //     fprintf(stderr, "Error loading texture: %s\n", filename);

        vkUnmapMemory(demo->device, tex_obj->mem);

    demo_set_image_layout(demo, tex_obj->image, VK_IMAGE_ASPECT_COLOR_BIT,
                          VK_IMAGE_LAYOUT_PREINITIALIZED, tex_obj->imageLayout,
    /* setting the image layout does not reference the actual memory so no need
     * to add a mem ref */

I've uploaded the entire demo here. The only things I've changed from the cube demo from the Vulkan SDK are the functions above.

I've tried various different images, with different compressions (BC1/2/3), none of them work.





turns into:


(Not the cube demo, but same principle)




turns into:




Any hints would be much appreciated.

Share this post

Link to post
Share on other sites

Based on my understanding of the spec, it's actually because you're mapping into an image with tiling VK_IMAGE_TILING_OPTIMAL layout, which does not necessarily have to have things exist in linear scanlines. The fix is to create a staging resource with identical format and VK_IMAGE_TILING_LINEAR layout instead, and then copy from the LINEAR image to the OPTIMAL one by way of a vkCmdCopyImage.

Share this post

Link to post
Share on other sites

Based on my understanding of the spec, it's actually because you're mapping into an image with tiling VK_IMAGE_TILING_OPTIMAL layout, which does not necessarily have to have things exist in linear scanlines. The fix is to create a staging resource with identical format and VK_IMAGE_TILING_LINEAR layout instead, and then copy from the LINEAR image to the OPTIMAL one by way of a vkCmdCopyImage.

I've tried setting the tiling to linear without copying, and I've tried copying from a linear staging image to optimal, but the result is always the same.


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!