Sunstone dev log 30: HDRI skybox
I’m working on image-based lighting. The first step was adding lighting controls to the UI. Then I added a HDR “image” light type and updated my skybox shader to sample from it.

The model is black because I disabled all other lights and the HDRI is not yet casting light on the model.
Descriptor indexing
I pass an array of Lights to my shader:
struct Light {
/*
Values:
- 0 - ambient
- 1 - directional
*/
uint type;
/*
Units:
- Lumens for ambient
- Lux for directional
*/
float magnitude;
// Components in range [0, 1]
vec3 color;
/* Direction the light is pointing.
Only defined for directional lights.
*/
vec3 direction;
};
layout(std140, ...) buffer readonly Lights {
uint size;
Light data[];
} lights;It’s essentially a sum type,
data Light
= Ambient Color Luminance
| Directional Color Illuminance Direction
type Lights = List Lightand for image-based lighting, I want to add a third constructor:
| Image Sampler2DHowever, I can’t just add a sampler2D field to the Light struct, because
it’s an opaque type.
To work around this, I introduced
descriptor indexing.
Aside
After adding even more fields to the Light type, I’m starting to think that
it would be better to split it across multiple arrays.
struct LightAmbient {
vec3 color;
float luminance;
}
struct LightDirectional {
vec3 color;
float illuminance;
vec3 direction;
}
struct LightImage {
uint textureID;
}
layout(std140, ...) buffer readonly LightsAmbient {
uint size;
LightAmbient data[];
} lightsAmbient;
layout(std140, ...) buffer readonly LightsDirectional {
uint size;
LightDirectional data[];
} lightsDirectional;
layout(std140, ...) buffer readonly LightsImage {
uint size;
LightImage data[];
} lightsImage;The shader binds an array of sampler2Ds
layout(..) uniform sampler2D textures[]; and the “image” light type carries
an index into this array. The texture is then retrieved from the
array by non-uniform indexing (textures[nonuniformEXT(index)]).
One problem I haven’t yet solved is how to choose an appropriate
DescriptorSetLayoutBinding.descriptorCount for the texture array. The
relevant Vulkan sample
uses
VkPhysicalDeviceDescriptorIndexingProperties.maxDescriptorSetUpdateAfterBindSampledImages
for this value. This only works if there are no other descriptors of that type
in use. My shaders do use other sampler2D bindings, so I got validation
warnings that maxDescriptorSetUpdateAfterBindSampledImages was being exceeded.
The best fix I’ve thought of so far is to replace all the sampler2D bindings
with IDs into the textures array and use
maxDescriptorSetUpdateAfterBindSampledImages as in the sample. Right now
that’s too much of a tangent, so I just set the descriptorCount to small-ish
value and added an assert to guard against overflow.
Fragment ray directions
The HDRIs I’m using (such as this one) represent a spherical image via an equirectangular projection. For the skybox, I think of the image as a sphere with an infinite radius surrounding the scene, with the camera at the center. To sample the image, we pick a direction (a unit vector), convert that to spherical coordinates, and use those to sample the texture.
If you consider the framebuffer to be located at the near plane, then you can trace a ray from the camera through a pixel in the framebuffer. This is the direction that’s used to sample the HDRI for skybox display. I already covered most of the math when I was working on the physically-based camera. Here’s the remaining derivation:

What I built
- Lighting UI
- Descriptor-indexed texture management (essentially bump allocation, currently no deallocation)
- Sample HDRI for skybox
What I learned
- Tagged unions don’t work well in CPP when they include types with non-trival
destructors, e.g.
std::string. You have to usestd::variantinstead.
Next steps
Image-based lighting for diffuse materials
When a HDRI is loaded in the UI, run a compute shader that builds an irradiance map for the image. Sample the irradiance map when shading using an image light.