how to get back a variable value which is changed in a Shader?

Started by
7 comments, last by neneboricua19 18 years, 7 months ago
Normally, we set the value of a variable using DirectX SetConstantX()... How can I get back a variable's value which is changed in a Shader, for example , a float variable which is interpolated in the Shader, API need get back this changed value, how to get back it? Help me.

Qian Jun
Advertisement
Write the value to a render target and read back the value with GetRenderTargetData.
I'm curious, what sort of performance hit are we talking about with this method you describe donnie?
Reading back from GPU is slow and there is going to be a pipeline stall but in the case of a 1x1 float texture there's no performance problem.
Reading data back from the GPU is always a performance hit, no matter how much data is read back. That's becouse it creates a pipeline stall, like DonnieDarko said.
Effect effect = ...;

for ( int j = 0; j < effect.Description.Parameters; j++)
{
EffectHandle handleParam = effect.GetParameter(null, j);
ParameterDescription desc = effect.GetParameterDescription(handleParam);

if(desc.Class == ParameterClass.Vector){
Vector4 v4 = effect.GetValueVector4(handleParam);
}

}

I just put Vector4 as an example...

Don't use this for every frame.

Rendering to a render target and reading that is barely a performance hit. Almost all multipass effects use the render-target technique.
Quote:Original post by vidalsasoon
Effect effect = ...;

for ( int j = 0; j < effect.Description.Parameters; j++)
{
EffectHandle handleParam = effect.GetParameter(null, j);
ParameterDescription desc = effect.GetParameterDescription(handleParam);

if(desc.Class == ParameterClass.Vector){
Vector4 v4 = effect.GetValueVector4(handleParam);
}

}

I just put Vector4 as an example...

Don't use this for every frame.


This code only sets and reads constant registers. Those do not change during the shader run.

The only way to get a "calculated" value back from a shader is via a render target. At least until Dx10 comes out.
Quote:Original post by rjackets
Rendering to a render target and reading that is barely a performance hit. Almost all multipass effects use the render-target technique.

Carefull with blanket statements like this.

If you mean that rendering to a render target and then setting that render target as a texture in a subsequent pass, then yes, while there is a performance hit, the hit isn't as bad as it was a few years ago.

However, if you mean rendering to a render target and then locking this texture so you can read the information into the CPU, then there will be a sizeable performance hit because this will stall the graphics pipeline and break the graphics chip's parallelism.

neneboricua

This topic is closed to new replies.

Advertisement