HLSL: Unable to set shader global in Vert Shader? (Getting maxZ)
by Greg "axon" Ruthenbeck · in Technical Issues · 02/14/2006 (9:57 pm) · 2 replies
Hi all,
I've got an application using an effect file to define a few render passes.
In one of the passes I want to get the max & min transformed vertex depth (see code):
In the directx application:
1) set the min & max (Effect.SetValue)
2) Render the above pass using a model or 2
3) Use Effect.GetValueFloat() to read back the minZ and maxZ values from the GPU.
Trouble is that the value I get after the render pass is the same value I have set it to (just before the pass). The Vertex Shader has not altered the value.(?!?)
Is there a HLSL keyword I'm missing or something to make it update?
Hope someone can help. Thx in advance.
Greg
PS: Please ignore the fact that I could use the depth buffer
I've got an application using an effect file to define a few render passes.
In one of the passes I want to get the max & min transformed vertex depth (see code):
/* data from application vertex buffer */
struct appdata {
float3 Position : POSITION;
};
/* data passed from vertex shader to pixel shader */
struct vsOut {
float4 HPosition : POSITION;
float4 diffCol : COLOR0;
};
/* Variables used externally */
float minZ = 100000; // Something big
float maxZ = -100000; // Something small
/*********** vertex shader ******/
vsOut vertShader(appdata IN)
{
vsOut OUT;
float4 Po = float4(IN.Position.x,IN.Position.y,IN.Position.z,1.0);
float4 hpos = mul(Po, WorldViewProj);
minZ = min(minZ, hpos.z); // ***** TROUBLE HERE?
maxZ = max(maxZ, hpos.z); // ***** & HERE?
OUT.diffCol = whatever;
OUT.HPosition = hpos;
return OUT;
}In the directx application:
1) set the min & max (Effect.SetValue)
2) Render the above pass using a model or 2
3) Use Effect.GetValueFloat() to read back the minZ and maxZ values from the GPU.
Trouble is that the value I get after the render pass is the same value I have set it to (just before the pass). The Vertex Shader has not altered the value.(?!?)
Is there a HLSL keyword I'm missing or something to make it update?
Hope someone can help. Thx in advance.
Greg
PS: Please ignore the fact that I could use the depth buffer
#2
Vertex shaders cannot modify any variables other than the ones it outputs (vsOUT in your case).
minZ and maxZ will be discarded by the compiler as the results do not affect vsOUT.
Constants like minZ and maxZ are INPUTS only.
If you really needed the results, you could pass minZ and maxZ to the pixel shader and get them to write out the values in to the frame buffer. This would require a floating point render target, and locking, and would basically be more pain than it is worth.
You are better off doing this operation on the CPU.
If you have a bounding sphere or a bounding box for the mesh you are rendering, you can transform this in to Camera WorldViewProj space and calculate the min and max Z value that way.
03/13/2006 (6:10 am)
I'm afraid that it is not possible to do what you are trying here.Vertex shaders cannot modify any variables other than the ones it outputs (vsOUT in your case).
minZ and maxZ will be discarded by the compiler as the results do not affect vsOUT.
Constants like minZ and maxZ are INPUTS only.
If you really needed the results, you could pass minZ and maxZ to the pixel shader and get them to write out the values in to the frame buffer. This would require a floating point render target, and locking, and would basically be more pain than it is worth.
You are better off doing this operation on the CPU.
If you have a bounding sphere or a bounding box for the mesh you are rendering, you can transform this in to Camera WorldViewProj space and calculate the min and max Z value that way.
Torque Owner Kirchgessner
Prosimian Productions
Then try recompiling it