Default texture for texture sampler

Hi there,
Can you help me implementing an optional texture in Optix?

I need to load an optional texture to display it using Optix engine. Currently I get error on context validation when the texture sampler is not bound to a buffer.

It would be perfect to set an default output for the tex2D function when the texture is not bound.

Thanks for help!

Hi @szamil,

Which version of OptiX are you using? Can you elaborate on what you mean by the sampler is not bound to a buffer?

I don’t think there is a way to specify a default output value for when you want a texture sampler but don’t want to use a texture buffer, so I’d guess the answer is to avoid creating the texture and avoid calling tex2d() if you don’t want to sample a texture.

Alternatively, you could create a 1x1 pixel texture buffer in memory manually with a color of your choosing, and use that as your texture buffer instead of data that you’ve read from a file.

Is having a separate non-texture code path viable in your case, or does it cause problems for you?


Hi David,
Thanks for reply.

I’m using Optix 5.0.0, but for sure we’re need to move to the latest version soon.

When the texture file is not selected, the Buffer cannot be created (now width/height), hence TextureSampler has nothing bound, and cannot be set as variable (or do I missing something?):

OptiX Error: 'Variable not found (Details: Function "_rtContextValidate" caught exception: Variable "Unresolved reference to variable c_texture from _Z17phong_closest_hitv_cp9" not found in scope)'

I followed the suggested 1x1 pixel solution, although it is not ideal.

If you want to have device programs which use textures conditionally, do not use texture sampler variables and tex1D/2D/3D commands directly.

Instead you should always use bindless texture ID variables directly or inside of structs (it’s actually an int) or buffers of bindless texture IDs if you need to pick from many texture objects and the OptiX rtTex*<type>() intrinsics, which are the only way to support cubemaps and the more specific texture fetch instructions with LOD or gradient arguments.

Read this chapter in the OptiX 6.5.0 Programming Guide:

Since the bindless texture IDs are integers, there is a specific texture ID called RT_TEXTURE_ID_NULL which can be used to check at runtime if a bindless texture is present inside the device code.
That is not possible when using TextureSamplers directly. There must be a texture sampler created which means you must have a non-empty buffer bound to that.

OptiX 5 based example code using bindless texture IDs here:

Ports of some of these examples to the OptiX 7 API can be found here:
Note that OptiX 7 doesn’t know anything about textures and uses native CUDA texture object and the matching CUDA texture fetch functions on the device.

Yes, you should definitely port to OptiX 7.

Thank you very much! That is exactly what I need!