glslspir-v

How to convert uint64_t to sampler2D?


I want to use bindless textures, but spirv does not support it. I found here: https://www.khronos.org/opengl/wiki/Bindless_Texture that uint64_t can be converted to a sampler2D.

uint64_t values can be converted to any sampler or image type using constructors: sampler2DArray(some_uint64). They can also be converted back to 64-bit integers.

and I thought that I could upload a bindless handle as the uint64_t and then convert it to the sampler2D.

spir-v compiler gives me them errors:

error: 'sampler/image' : cannot construct this type
error: 'sampler2D' : sampler-constructor requires two arguments
error: 'constructor' : too many arguments
error: 'assign' :  cannot convert from ' const float' to 'layout( location=0) out highp 4-component vector of float'

shader code

#version 460 core
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require
#extension GL_EXT_scalar_block_layout : require

layout(location = 0) out vec4 f_color;

layout(location = 0) in vec2 v_uv;

layout(std430, binding = 5) uniform Textures
{
    uint64_t albedo;
};

void main()
{
    f_color = texture(sampler2D(albedo), v_uv);
}

is it possible to convert uint64_t to sampler2D? How to do it?


Solution

  • As with all GLSL extensions, you must explicitly enable them. However, you have a more interesting problem: bindless texturing cannot be used with SPIR-V. What you want to do is only possible if you feed the GLSL directly to OpenGL, without the SPIR-V intermediary.

    Vulkan does not support bindless textures; you need to use arrays of samplers to get an equivalent effect. And OpenGL doesn't support GL_EXT_scalar_block_layout. So the code you're writing cannot be used by any graphics system.