HLSL functions question

Started by
3 comments, last by unbird 10 years, 3 months ago

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;

}

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

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"

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

This topic is closed to new replies.

Advertisement