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

This topic is 4228 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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)

##### Share on other sites
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.

##### Share on other sites
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;	}

##### Share on other sites
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 GhavamiThe error message is quite clear in my opinion, you haven't initialized the variable output.*** Source Snippet Removed ***

##### Share on other sites
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).

##### Share on other sites
Thank you!
actually I *am using "disable depth writes", by
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 TheAdmiralPerhaps 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

##### Share on other sites
Why not just initialize your output structure to 0?

PS_OUTPUT output = (PS_OUTPUT)0;

It's good practice anyway.

##### Share on other sites
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.

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
13
5. 5
frob
12

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633664
• Total Posts
3013238
×