Sign in to follow this  
age_of_lion

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

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Rendering to a render target and reading that is barely a performance hit. Almost all multipass effects use the render-target technique.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

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