[hlsl] error of "Use of potentially uninitialized variable"

Started by
6 comments, last by Drilian 16 years, 11 months ago
Hi, below hlsl 10 gets below error and exit, thanks for suggestions!

struct PS_OUTPUT
{
	uint4 col :	SV_Target;
	float depth :			SV_Depth;
};

PS_OUTPUT psHashJoin(..)
{
PS_OUTPUT output;
	if(..)
	{
		output.col = ..
		output.depth = 1.f;
		return output;
	}
warning X4000: Use of potentially uninitialized variable (output)
Advertisement
If that is your whole script then its because if that "if" statement evaluates to false you wont be setting output.col or output.depth, or returning any output.

"There will come a time when you believe everything is finished. That will be the beginning." -Louis L'Amour
The error message is quite clear in my opinion, you haven't initialized the variable output.
PS_OUTPUT output;// What value does output have here?	if(..)	{		output.col = ..		output.depth = 1.f;		return output;	}
Best regards, Omid
Thank you!
in the ps code, actually sometimes I just don't want to update the depthbuffer.
Then I choose to "uninitialize" ouput.depth, and it proves that it's ok when I wrote API like this:
	D3D10_DEPTH_STENCIL_DESC dsDesc;	dsDesc.DepthEnable = true; //note: if false,then won't discard automatically! since I'll use occlusion query, i want it discard depth==1.0	dsDesc.DepthWriteMask = D3D10_DEPTH_WRITE_MASK_ZERO; //note: this only means that dx won't write z AUTOMATICALLY, but I can still write SV_Depth is ps!!!	dsDesc.DepthFunc = D3D10_COMPARISON_LESS;	dsDesc.StencilEnable = false;	ID3D10DepthStencilState * pDSState;	g_pd3dDevice->CreateDepthStencilState(&dsDesc, &pDSState);	g_pd3dDevice->OMSetDepthStencilState(pDSState, 1);

Quote:Original post by Omid Ghavami
The error message is quite clear in my opinion, you haven't initialized the variable output.
*** Source Snippet Removed ***


Perhaps you could use HLSL's clip intrinsic to abort rendering when the depth-test fails. If that's no good, you could achieve the same effect by writing an invisible pixel, provided you can find an appropriate depth value to write with (or simply disable depth-writes, if that's viable).

Admiral
Ring3 Circus - Diary of a programmer, journal of a hacker.
Thank you!
actually I *am using "disable depth writes", by
dsDesc.DepthWriteMask = D3D10_DEPTH_WRITE_MASK_ZERO;
and I dismissed the depth component from PS_OUTPUT.
This way, I use "discard" to dynamic flow control (DFC), but I can't benefit the potential big profit from early-z, since I can't write z, so can't update z to be 1.0 dynamically, so can't do early z in the next pass.


Quote:Original post by TheAdmiral
Perhaps you could use HLSL's clip intrinsic to abort rendering when the depth-test fails. If that's no good, you could achieve the same effect by writing an invisible pixel, provided you can find an appropriate depth value to write with (or simply disable depth-writes, if that's viable).

Admiral


Why not just initialize your output structure to 0?

PS_OUTPUT output = (PS_OUTPUT)0;


It's good practice anyway.
I don't find that to be good practice. It seems that if I ever fail to write meaningful data to the output (i.e. I screwed up), I would want the compiler to let me know.

This topic is closed to new replies.

Advertisement