I have a simple question to ask .. Its one of the fir开发者_StackOverflow社区st things you learn about shaders when using them with XNA that you can pass on variables from C# to the shader code via the Effect.Parameters function. For example:
Effect.Parameters["shaderVariable"].SetValue(someValue);
In the above line, you're sending the value stored in 'someValue' to the shader variable 'shaderVariable' ..
Can we do the reverse as well ? Like for example, you want to debug the inner workings of a shader code and want to send back some numerical data to your C# application, then is there any similar but opposite relation functional available as well ?
Not really. The problem is that a pixel shader is executed once per pixel, and a vertex shader once per vertex. So you've got potentially hundreds of instances of your program to pick from, when you draw, so how do you know which one you want to get the output value from?
The only real way to get output from a shader is via the image that it eventually outputs (GPGPU aside).
But - if all you want to do is shader debugging, use PIX in the DirectX SDK.
The graphics pipeline will only ever go forwards, CPU -> Vertex -> Pixel -> Frame. If you have a value in your pixel shader that you want to examine, just modify your shader slighty so that it returns that value as a color. For example you could return the xy values of a texCoord as the red and green color channels in your image.
Yes you can get data from the shader (at least in XNA 4.0) eg:
var projection = <your effect>.Parameters["projection"].GetValueMatrix();
精彩评论