• Advertisement
Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement

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).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement