tags:

views:

1382

answers:

9

I want to get started doing some game development using Microsoft's XNA. Part of that is Shader development, but I have no idea how to get started. I know that nVidia's FX Composer is a great tool to develop shaders, but I did not find much useful and updated content on how to actually get started.

What tutorials would you recommend?

+1  A: 

SAMS's XNA Unleashed by Chad Carter is a great starting point for XNA and assumes little knowledge of game development practices or hard maths before you start. It has two chapters on basic and advanced shaders.

As a sidenote, keep an eye out on Google for WPF Shader tutorials, it now uses the same technology to allow customer shaders in WPF applications and tutorials for that I believe are largely compatible with XNA.

Nidonocu
+20  A: 

Development of shaders in XNA (which obviously uses DirectX) requires knowledge of HLSL or shader assembly. I'd recommend getting familiar with the former before diving into the latter.

Before writing any shaders, it's a good idea to get solid understanding of the shader pipeline, and attempt to get your mind around what is possible when using programmable shaders. When you're familiar with the life of a pixel (from source data all the way through to the screen) then understanding examples of shaders becomes a lot easier.

Next make an attempt to write your own HLSL which does what the Fixed T&L pipeline used to do, just to get you hands dirty. This is the equivalent of a "hello world" program in vertex/pixel shader world. When you're able to do that and you understand what you've written you're ready to go onto the more fun stuff.

As a next step you might want to simulate basic sepcular lighting in one of your shaders from a single light source. You can then adapt this down the track to use multiple lights. Play with colours, and movement of lights. This will help get familiar with the use of shader constants as well.

When you have a couple of basic shaders together, you should attempt to make it so that your game/engine uses multiple/different shaders on different objects. Start adding some other bits like basic bump or normal maps.

When you get to this stage, the world is your oyster. You can start diving into some funky effectcs, and even consider using the GPU for more than it was originally intended.

For those who are a little more advanced, there are a couple of good books that are available for free online which have some great information from Nvidia here and here.

Don't forget that there's an excellent series of books called ShaderX which covers some awesome shader stuff. There's 1, 2, 3, 4, 5 and 6 already in print, and 7 is coming soon.

Good luck. If you get some shaders going, I'd love to see them :)

OJ
Thanks for the detailed reply. Be sure, as soon as I get something up and running i'll add a link :) Can take a few more months though.
Michael Stum
+4  A: 

I'd just like to reiterate how great the GPU Gems books are - a truly fantastic resource for any serious graphics development.

OJ has basically summed up a really good process of learning, I'd just add that having a good foundation in geometric math (vectors/matrices as a minimum) cannot be underestimated - but it's not as hard as people sometimes make out. Learning what a dot product, cross product, normal vector and matrix multiply are and do is a good first step :). Try to understand exactly what is happening between World/View/Projection-Clip/Screen space, what the perspective divide is, etc.

When I was beginning learning, a good exercise is to actually implement the entire T&L pipeline in software, complete with clipping/culling etc. This is a long process and may not seem worthwhile as I'm sure you want to just dive into the fun stuff, but having a proper understanding of what's going on is really useful and will prove worthwhile when you invariably run into more insidious and difficult to diagnose bugs.

Try not to be sidelined with tools like the FX Composer initially, they're useful for prototyping, but having a solid foundation in the basics is much more worthwhile in the long run.

Ali Parr
+1  A: 

I haven't used XNA or DirectX. But, for getting to know the basics of shader programming with Cg, the Cg Tutorial is the best book I've found.

Ashwin
+1  A: 

You should also look at RenderMonkey. It's a good tool. As far as books go, check out XNA 2.0 Game Programming Recipes from Riemer Grootjans ... great book.

Joel Martinez
+1  A: 

I'd join the praises given to OJ - truely a good answer. Still, once you do have the basic understanding you can learn a lot and fast by downloading one of the following two great tools:

Once you do that, go to their project libraries and start browsing through examples, starting with the basic shading and moving to shadows, normal mapping, materials, effects and everything you find of interest. take a project, start altering the algorithms according to some goals you set and see how to get it. You'll find that many of the examples are really advanced and they will open up your horizons.

Have fun

Adi
+2  A: 

Whilst a lot of good advice has already been given, if you're having a hard job getting your mind around the steps involved, Mental Mill provides a visual way of constructing Shaders (the Artist Edition is bundled free with Fx Composer -- itself also free!).

Whilst you'd be better off long term learning the HLSL code directly, Mental Mill can generate these Fx files for you (so you can peek at the changes). Note, like all code-generators, it's a little more verbose than you're likely to do once you understand HLSL!

The visual progression of the effects, from one method to another, is very impressive!

Ray Hayes
+1  A: 

One quick thought on visual shader editors. Those sort of editors are a very fun thing to play with, but I would really, really recommend to stick with working with text-based HLSL (there both RM and FXC do the job). I work as a game dev and have built a few shader systems (the one in Battlefield 2 and Farcry 2, for instance) and, to be honest, have found that HLSL is more valuable than any graphic shader network to date.

There are things like following the flow of execution, debugging the shader or being able to quickly iterate test cases that give you an insight that is pretty much impossible to get through a visual tool.

I have used visual shader builders myself, and I like them, and one day, when shading is not as performance-critical as it is today, they might be the right tool for the job.

..though my guess is that we'll see deferred lighting and probably full software rendering before that day comes ;)

Rodrigo Lopez
+1  A: 

One of the best ways to get started with shaders is to read Introduction to 3D Game Programming with DirectX 9.0c a Shader Approach by Frank Luna. The author first introduces the DirectX and the HLSL and then gradualy reveals the power of shader. He starts with very simple shaders but by the end of the book you know how to create lighting, shadows, particle systems, etc. Great Book.

OJ suggested that you read the ShaderX series, but the stuff there is not for beginners so it will not help you a lot when you are making your first steps.

I've also posted an article here about the best books that will get you started with game development.

Milan Nankov
That looks like a really interesting book. Yeah, I looked at both ShaderX and the also recommended GPU Gems, but they seem to be really hardcore. Kinda like reading Bruce Schneier when not knowing anything about Cryptography. I think I'll order Luna's Book.
Michael Stum
I actually edited the book you mentioned! :)
Rodrigo Lopez