• 10
• 10
• 12
• 12
• 14

Buffer arguments to functions in HLSL (Compute Shaders)

This topic is 428 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I want to write a function in a compute shader that takes a StructuredBuffer or an RWStructuredBuffer as an argument.  Something like this:

void SomeFunction(StructuredBuffer<CustomStruct> buffer) {
// ...
}

This seems to work, but I'm concerned that the buffer will actually be copied into the parameter each time the function is called.  It seems counter-intuitive to me that it would work that way,  I would think that the buffer would somehow be passed by reference.  I searched for a bit and could not find a definite answer as to whether the entire buffer is copied or not.  Does anyone here know how this works?

Share on other sites

No copy, you are fine, hlsl is not C++ :)

Share on other sites

You can think of a StructuredBuffer in HLSL as being similar to a pointer in C++: it lets you access the data that it points to, but it doesn't "contain" the data like std::vector does. It's really just a "view" to the underlying data.

On a related topic, there's actually modifiers that you can use for function parameters in HLSL. These effectively specify whether you're "passing by value" vs. "passing by reference", since they control whether calling functions see changes made to that parameter. The default modifer is "in", which means that calling functions don't see changes made inside of the function (essentially pass by value).