What vertex shader code should be used for a pixel shader used for simple 2D SpriteBatch drawing in XNA?

Posted by Michael on Stack Overflow See other posts from Stack Overflow or by Michael
Published on 2011-11-19T01:26:40Z Indexed on 2011/11/19 1:50 UTC
Read the original article Hit count: 376

Filed under:
|
|

Preface

First of all, why is a vertex shader required for a SilverlightEffect (.slfx file) in Silverlight 5? I'm trying to port a simple 2D XNA game to Silverlight 5 RC, and I would like to use a basic pixel shader. This shader works great in XNA for Windows and Xbox, but I can't get it to compile with Silverlight as a SilverlightEffect.

The MS blog for the Silverlight Toolkit says that "there is no difference between .slfx and .fx", but apparently this isn't quite true -- or at least SpriteBatch is working some magic for us in "regular XNA", and it isn't in "Silverlight XNA".

If I try to directly copy my pixel shader file into a Silverlight project (and change it to the supported "Effect - Silverlight" importer/processor), when I try to compile I see the following error message:

Invalid effect file. Unable to find vertex shader in pass "P0"

Indeed, there isn't a vertex shader in my pixel shader file. I haven't needed one with my other 2D XNA apps since I'm just doing basic SpriteBatch drawing.

I tried adding a vertex shader to my shader file, using Remi Gillig's comment on this Shawn Hargreaves blog post for guidance, but it doesn't quite work. The shader file successfully compiles, and I see some semblance of my game on screen, but it's tiny, twisted, repeated, and all jumbled up. So clearly something's not quite right.


The Real Question

So that brings me to my real question: Since a vertex shader is required, is there a basic vertex shader function that works for simple 2D SpriteBatch drawing?

And if the vertex shader requires world/view/project matricies as parameters, what values am I supposed to use for a 2D game?

Can any shader pros help? Thanks!

© Stack Overflow or respective owner

Related posts about Silverlight

Related posts about XNA