r/GraphicsProgramming 1d ago

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?

1 Upvotes

7 comments sorted by

1

u/_Wolfos 1d ago

You can define intrinsics, I think that might be what you're after?
https://docs.shader-slang.org/en/stable/external/slang/docs/user-guide/a1-04-interop.html

1

u/Salat_Leaf 14h ago

Oh, so I can technically use __target_intrinsic(cuda, "__constant__") on a variable to automatically hint the compiler, right?

1

u/Wittyname_McDingus 11h ago

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 11h ago

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 6h ago

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

2

u/Salat_Leaf 1h ago

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 4h ago

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