Sign in to follow this  

HLSL functions question

This topic is 1445 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

void function(float x,float y)

{

            x=x*y;

            y=3.0f;

}

 

float4 PStriangle():SV_Target

{

          float a,b;

          a=4.0f;

          b=5.0f;

          function(a,b);

}

 

In HLSL 4.0 I want "function" to modify "a" and let "b" like it was, do I do this in the same way it's done in C++?

So "function" will look like this:

 

void function(float& x,float y)

{

            x=x*y;

            y=3.0f;

}

Edited by lomateron

Share this post


Link to post
Share on other sites
There are special keywords to accomplish this: in, out, inout (no modifier means in, as one would expect).

So, for your example:

void function(inout float x,inout float y)
//...

This works with shader signatures, too, e.g. a vertex shader:

void VS(float4 position: POSITION, out float4 positionCS: SV_Position)

Share this post


Link to post
Share on other sites

wait but I want "b" not to be modified, so I think

void function(inout float x,inout float y)

is not correct

 

when you say "no modifier means in, as one would expect"

then why "inout" exist? 

whats the difference between "inout" and "out"

Edited by lomateron

Share this post


Link to post
Share on other sites

I know now..this is from a book

 

out: Specifies that the parameter should be copied to the argument
when the function returns. This is useful for returning values through
parameters. The out keyword is necessary because the HLSL doesn’t
allow us to pass by reference or to pass a pointer. We note that if a
parameter is marked as out the argument is not copied to the
parameter before the function begins. In other words, an out parameter
can only be used to output data — it can’t be used for input.

Share this post


Link to post
Share on other sites
inout is both ways, out just out.

Your example code isn't quite consistent with your explanation, hence my confusion (y looks like a in Parameter, but you still modify it in that function). Sorry about that.

In that case:

function(inout float x, float y)

Share this post


Link to post
Share on other sites

This topic is 1445 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this