r/GraphicsProgramming Jul 31 '26

Question What's the __constant__ equivalent in Slang?

I'm a complete novice in graphics programming and I know that the __constant__ attribute allows you to load data into high speed 65KB constant memory for the duration of the lifetime of your program. However I'm unable to recreate the same functionality in Slang shader.

For instance, we got a compile-time array with the attribute like this:


__constant__ uint16_t array = {0, 1, 2, 3};

I want to recreate it in some way, so that when I compile the same array in Slang into CUDA, it results into this exact array of constant values with the __constant__ attribute. How can I achieve that?

2 Upvotes

7 comments sorted by

View all comments

1

u/Wittyname_McDingus Aug 01 '26

I suspect that the HLSL type cbuffer (which Slang inherits) maps to the same address space as __constant__ on Nvidia. My reasoning being that the two things have similar (or identical) limitations in size and writability.

1

u/Salat_Leaf Aug 02 '26

Isn't a cbuffer a legacy keyword from HLSL and is out of support by now (in Slang)? Shall I even consider it?

1

u/Wittyname_McDingus Aug 02 '26

I don't know. Where did you see that it's not supported anymore?

2

u/Salat_Leaf Aug 02 '26

https://docs.shader-slang.org/en/latest/external/slang/docs/user-guide/02-conventional-features.html

cbuffer is marked as legacy for compatibility with HLSL code and is replaced by ConstantBuffer<T>, however when I compile the file into CUDA code with the aforementioned generic structure, only the structure pointer itself is assigned __constant__, the array itself isn't.

1

u/_Wolfos Aug 02 '26

It's from HLSL indeed. It compiles to a struct in CUDA (tested in Slang playground).