[.net] Shaders in Managed DirectX

Started by
2 comments, last by GameDev.net 18 years, 4 months ago
This might sound like a dumb question but here it goes anyway... I know what a shader is (i think), but how does it work and how can I use them in Managed DirectX? Best regards / Patrik Svensson
Advertisement
There is a fantastic article on the site located here that will give you a great overview of both vertex and pixel shaders. Although its directed towards directx 8 and not managed dx, you should still get a real good understanding of shaders after you read it.

The DirectX SDK has some examples that should get you started. Check out the examples using the HLSL (High Level Shader Language) to get started.
Using shaders in managed directx is really quite simple. So, here's how you do it:

1. (Optional) In your .X files, you can attach an EffectInstance block in each material block (just like textures). Syntax is like the following:
EffectInstance {
"foo.fx";
}
Unfortunately, I've never found a way in 3d max to input in this information anywhere for exporting, so I've always had to manually put it into the exported files, not a big deal, but something you have to remember to do.

The Mesh.FromX function that loads in .x files, will fill an array with EffectInstances for you to retrieve the information about what shader goes with what object.
2. Load your shader effect file into your game using the Effect.FromFile function.
3. Set up your effect file by setting its various variables. To do this, you use EffectHandles. Create an EffectHandle with EffectHandle.FromString(variableName), and use Effect.setValue(effectHandle, value) to set the variables themselves.
4. Just before you would normally render your polygons, call the beginEffect() function on your effect object, this will also return the number of passes the shader is going to make. Then, call beginPass(passNumber) on your effect object, render your geometry, call endPass() on the effect, and repeat for all of the required passes. Finish it off by calling endEffect() on the effect object.

You might want to check out this link as well for information on writing shaders.

http://www.ati.com/developer/ShaderX2_IntroductionToHLSL.pdf

The BasicHLSL sample will hopefully point you in the right direction in terms of implementing all of this, and gives you a basic shader that you can fiddle with. I'd recommend playing around with that hlsl file with that sample, before you code in shader support into your engine, that should give you a better idea of what shaders can do. Plus that's a good starting point to base future shaders off of, as it does standard transform and lighting already (though you'll want to pass in light variables instead of hardcoding them into the effect naturally).
Andor.. you just saved me three tons of work. I had no idea you could include that in the X file!! thank you sir!

/broNzon

This topic is closed to new replies.

Advertisement