# Rendering a Sprite with an Effect… how?

gettings tuck doing some 3d Rendering and I think you guys may have a lot more knowledge in it. I am tasked with basically rendering a texture onto a screen, preparing the way to apply graphical effects there (including some color space conversions later that shall move from the CPU to the graphics card.

My idea to do that is pushing the data into a texture, then render it as a sprite onto a surface or texture that then gets presented (it is going to be transformed by code from other people into some show of videos that are part of a larger scene).

Sadly, for technical reasons this means C#, DirectX9 and I decided to use SharpDx as DiretX wrapper.

I am slowly making my way into the whole thing. For the start, we have an A8R8G8B8 texture to render to, and a source in the same format (which will change later, including some foramats DirectX does not support - thus the idea to use PixelShader to transform for example color spaces).

Where I am stuck is:

• I can not get an Effect to do ANYTHING. HI ahve the idea my setup is somewhere wrong. Sadly finding out how to apply an effect to a sprite is - very hard, given that most stuff on the internet deals with more recent versions of Direct x.

My code:

There is a SIMPLE effect that should just chane the colors of the pixel. This is a test, but the pixels get rendered normal, so something is wrong. The Effect code is:

float4x4 World;
float4x4 View;
float4x4 Projection;

float4 AmbientColor = float4(1, 1, 1, 1);
float AmbientIntensity = 0.1;

struct VertexShaderInput
{
float4 Position : POSITION0;
};

struct VertexShaderOutput
{
float4 Position : POSITION0;
};

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
VertexShaderOutput output;

float4 worldPosition = mul(input.Position, World);
float4 viewPosition = mul(worldPosition, View);
output.Position = mul(viewPosition, Projection);

return output;
}

float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
float4 output;
//output = AmbientColor * AmbientIntensity;
output = float4(0,0,0,0);
return output;
}

technique Render
{
pass Pass1
{
VertexShader = compile vs_2_0 VertexShaderFunction();
PixelShader = compile ps_2_0 PixelShaderFunction();
}
}


The way I try to render it is:

I get the Effect like this:

Dx9.Effect effect = Dx9.Effect.FromFile(m_Device, "SpriteNopEffect.fx", Dx9.ShaderFlags.Debug);


I select the Technique:

effect.Technique = "Render";


That works -. if I change the name then it blows, so the effect file is loaded and the effect found.

The render code is:

        m_Device.BeginScene();
Dx9.Sprite sprite = new Dx9.Sprite(m_Device);

effect.Technique = "Render";
int passcount = effect.Begin();
for (int i = 0; i < passcount; i++)
{
effect.BeginPass(i);
sprite.Begin();
sprite.Draw(
texture,
new SharpDX.Rectangle(0, 0, m_Width, m_Height),
new Dx.Vector3(0, 0, 0),
new Dx.Vector3(0, 0, 0),
new SharpDX.Color4(0xffffffff)
);
sprite.End();
effect.EndPass();
}
effect.End();

m_Device.EndScene();


And here the problem is - whether I have the effect in or not, the output is the same, so EITHER my effect is bullocks OR - the effect is simply not applied.

Any help appreciated.

• You aren't passing any World, View or Projection matrices to the shader in the code you pasted. Are you setting them anywhere else? – r2d2rigo Aug 3 '12 at 13:54
• Well, it WORKS - it does not do anything with the shader, but it works. Both textures are the same size - so the sprite mechanism is doing all the stuff correct ;) – TomTom Aug 3 '12 at 17:32
• Did you try debugging with PIX? – r2d2rigo Aug 6 '12 at 7:22

## 1 Answer

I think Sprite.Begin will set it's own shader for drawing sprites and overwrite yours. So change the order.

sprite.Begin();
effect.BeginPass(i);


note: This is how it works with Xna, and i don't think your shader is ready for what sprite.Draw will do

• Yes, that was it. ITdoes not "WORK" (as - does what it is intended) but at least now the image changes when I comment out the effect or not, which was not the case before when it was a non-operation. And this alone was the question - how to get a sprite to USE the effect. Thanks ;) – TomTom Aug 7 '12 at 13:44