NVIDIA recently released an open source sample of a GLSL header generator for DescriptorSet-indexed types in Vulkan. The project demonstrates how to simplify usage of VK_EXT_descriptor_indexing and GL_EXT_nonuniform_qualifier within GLSL. A script generates structure and function overloads to hide the code for indexing descriptor sets of samplers and textures.
For example, the tool turns:
// BEFORE
// we use a big resource table for descriptorset indexing
layout(set = 0, binding=0) uniform sampler2D res_tex2Ds[];
// let's make use of GL_EXT_buffer_reference2
layout(buffer_reference, scalar) buffer Material {
// we want to be cache efficient
uint16_t albedoTex;
uint16_t normalTex;
...
};
layout(push_constant, scalar) uniform inputData {
Material materials;
};
// example usage
Material mat = materials[idx];
// if idx varies non-uniformly (e.g. raytracing)
// our texture fetches can start to look pretty ugly
vec4 albedo = texture(res_tex2Ds[nonuniformEXT(uint(mat.albedoTex))], uv);
into:
// AFTER
// where descriptorsets start that are used for indexing
#define DIT_DSET_IDX 0
// this file is generated by the lua script
#include "sampler_indexed_types.glsl"
layout(buffer_reference, scalar) buffer Material {
// new types are available
// for example: struct sampler2D_u16 { uint16_t idx; }
sampler2D_u16 albedoTex;
sampler2D_u16 normalTex;
...
};
layout(push_constant, scalar) uniform inputData {
Material materials;
};
// example usage
Material mat = materials[idx];
// much nicer looking access
vec4 albedo = texture(mat.albedoTex, uv);
Check out the blog post for more information and the Github project here.