Sign in to follow this  
lomateron

HLSL arrays are buggy

Recommended Posts

lomateron    491

In this shader  I have this variable "b" there are two ways I can create it:

1) float b[2];

2) float2 b;

 

If I compile using "1)", it will show me this warning: output variable VSCheckAddJoint contains a system interpreted value <SV_POSITION> which should be written in every execution path of the shader. And there exists mysterious paths that output values that I don't want.

 

If I compile using "2)", it will show me no warnings,and the shader will work as I intended.

struct VS_P
{
    float4 Pos : POSITION;
	uint Id : SV_InstanceID;
};
struct PS_Ff
{
	float4 Frc : FFOO;
	float4 Pos : SV_POSITION;
};
PS_Ff VSCheckAddJoint(VS_P input)
{
	PS_Ff output; float b[2]; uint cC, nuMj, iii; uint3 u; float comp; float4 r; bool ghgdl;

	b[0] = 0.0f; b[1] = 0.0f; cC = 0; nuMj = 0; iii = 0; u = 0; comp = 0.0f; r = 0.0f;
	output.Pos = 0.0f; output.Frc = 0.0f;

	b[0] = Tex1Dl.Load(uint2(T, 0));
	b[1] = Tex1Dl.Load(uint2(T + 1, 0));

	iii = input.Id / 6;
	input.Id = input.Id % 6;
	comp = b[!iii];

	cC = uint(C.y);
	u = uint3((uint(b[iii]) % cC) * 6 + input.Id, uint(b[iii]) / cC, 0);
	r = Tex2Db.Load(u);
	u.x /= 6;
	nuMj = uint(Tex2Dg.Load(u));

	ghgdl = false;
	for (uint i = input.Id*4; i < nuMj; i++)
	{
		uint gn = i & uint(3);//i % 4
		if (r[gn] == comp){ ghgdl = true; }
	}

	output.Pos.w = ghgdl;
	output.Frc = ghgdl;
	return output;
}
Edited by lomateron

Share this post


Link to post
Share on other sites
mhagain    13430

Just so nobody else has to:

PS_Ff VSCheckAddJoint (VS_P input)
{
	PS_Ff output;
	float b[2];
	uint cC, nuMj, iii;
	uint3 u;
	float comp;
	float4 r;
	bool ghgdl;
	b[0] = 0.0f;
	b[1] = 0.0f;
	cC = 0;
	nuMj = 0;
	iii = 0;
	u = 0;
	comp = 0.0f;
	r = 0.0f;

	output.Pos = 0.0f;
	output.Frc = 0.0f;
	b[0] = Tex1Dl.Load (uint2 (T, 0));
	b[1] = Tex1Dl.Load (uint2 (T + 1, 0));
	iii = input.Id / 6;
	input.Id = input.Id % 6;
	comp = b[!iii]; cC = uint (C.y);
	u = uint3 ((uint (b[iii]) % cC) * 6 + input.Id, uint (b[iii]) / cC, 0);
	r = Tex2Db.Load (u);
	u.x /= 6;
	nuMj = uint (Tex2Dg.Load (u));
	ghgdl = false;
	for (uint i = input.Id * 4; i < nuMj; i++)
	{
		uint gn = i & uint (3);//i % 4
		if (r[gn] == comp)
		{
			ghgdl = true;
		}
	}

	output.Pos.w = ghgdl;
	output.Frc = ghgdl;
	return output;
}

Share this post


Link to post
Share on other sites
Migi0027    4628

I've had a few cases where I needed to pre-initialize the array, perhaps this is your case as well.

...
PS_Ff output; float b[2]; uint cC, nuMj, iii; uint3 u; float comp; float4 r; bool ghgdl;
...

INTO THIS:
...
PS_Ff output; float b[2] = {0, 0}; uint cC, nuMj, iii; uint3 u; float comp; float4 r; bool ghgdl;

Exactly why Im not sure.

Share this post


Link to post
Share on other sites
lomateron    491

In another shader where I use uint3 u[2];....... doing the  uint3 u[2]={(uint3)0,(uint3)0}; took out the warning

but I still had a problem in which the shader outputed a value I didn't want

and I just fixed it by not using arrays, now I am using uint3 ux,uy; and by just changing that I fixed the problem

So I am not going to use arrays again in HLSL

Share this post


Link to post
Share on other sites
JohnnyCode    1046

Also make sure your cpu shader uniforms setting instructions are apropriate when you change from a vector uniform declaration (float3 u;) to an array of vectors or floats (float u[3]).

DX sets declared uniforms with rich offer of instructions. You are obviously in factoring stage of a shader so I will not critisize on performance point, but I think you are overwhelming the shader with "initialized uniform variable at compile time".

Share this post


Link to post
Share on other sites

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