Jump to content
  • Advertisement
Sign in to follow this  
pcmattman

D3D9: Some Confusion about Shaders

This topic is 3799 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone, Been a while since I've been working on my game project (took a break to do some OS development, but I'm back at the helm again on my FPS) and have come back ready to do some good stuff with my game. I'm a bit confused about shaders at the moment, as I've heard a lot about them (and I would love to implement some pixel shaders for things like bloom or blurring, which would be appropriate in some sections of my game script) but I have some newbie questions... Firstly, what's better to use: HLSL, or the assembly language (GPU assembly?)? Note I'm not yet looking for exceptional performance, just something that works and I can easily edit and understand. Secondly, how can I implement these into my existing framework? Each object has a position - how is this given to the vertex shader so that it can position the outputted vertices properly? What about things like rotation and the like? Lastly, what are any tips as to integrating shaders into my scripting system? I use Python scripts to control behaviour in my engine (triggers, level loading, etc...), and I think it might be helpful to provide a shader file (maybe stock files, or custom ones) for each object created in a level script - is this correct? Is it possible to combine several pixel shaders, or should I combine all effects into one pixel shader and load specific shaders for specific cases? I know these are all pretty newbie questions, but I'm still confused after reading up on the internet. Thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement
Hi,

Quote:
HLSL, or the assembly language (GPU assembly?)? Note I'm not yet looking for exceptional performance, just something that works and I can easily edit and understand.

You have the answer already. HLSL is a much betther choice than writing GPU assembly. It is also the recommended way and is normally faster since you can never know how the HLSL compiler will optimise you code. Would you write a FPS Game in x86-Assembly instead using C++ or any high-level language of your choice?

Quote:

Secondly, how can I implement these into my existing framework? Each object has a position - how is this given to the vertex shader so that it can position the outputted vertices properly? What about things like rotation and the like?

You compute an object's transformation matrix and pass it to the shader (normally premultiplied with View and Projection Matrix). The transformation matrix can contain scaling, translation and rotation operations - even shearing objects is no problem.

Best regards,
Porthos

Share this post


Link to post
Share on other sites
Quote:
Original post by pcmattman
Firstly, what's better to use: HLSL, or the assembly language (GPU assembly?)? Note I'm not yet looking for exceptional performance, just something that works and I can easily edit and understand.


HLSL is used by many of the big studios, so I think it's easy to say that if it's good enough for them then its good enough for us hobbyists. HLSL will be more easy to learn, from my experience.

I don't think the use of shaders can be explained in a forum thread, but let me recommend a book. "Introduction to 3d Game Programming with Directx 9.0x A Shader Approach" by Frank Luna is awesome, the best shader book I found after looking at quite a few while at University. It teaches the use of HLSL so that anyone comfortable with C++ can understand them very easily, while I found all the other shader books either didn't go into enough detail, or delved more into the math than the application. Frank's book is both easy to read and runs the gamut from simple shaders to fairly complex ones.

This is all assuming you're working in DX9 of course.

Share this post


Link to post
Share on other sites
Quote:
Original post by Funso
"Introduction to 3d Game Programming with Directx 9.0x A Shader Approach" by Frank Luna is awesome


Although i haven't moved on to the shader part yet, i can vouch for this book. Its very well written and involves using shaders from the earliest possible opportunity. I've learnt more from this book than anything i've read on the internet about the sam subject.

Share this post


Link to post
Share on other sites
Quote:

You compute an object's transformation matrix and pass it to the shader (normally premultiplied with View and Projection Matrix). The transformation matrix can contain scaling, translation and rotation operations - even shearing objects is no problem.


This makes sense now, it makes all the HLSL code I've read make more sense than before. There's a great tute at two-kings.de that I didn't understand up until I read that statement :D.

I'll work at implementing a basic vertex shader for transforming objects now and see what I can do.

Thanks everyone!

Share this post


Link to post
Share on other sites
Something to note is that assembly shaders are gone from DirectX10. Which means that you will need to learn HLSL, sooner or later.

Share this post


Link to post
Share on other sites
Alright, so I got a basic HLSL script working to transform a set of vertices, but now I'm stuck - I need to texture those vertices!

I've tried adding in texture co-ordinates to the vertex declaration and referencing them in the vertex shader (just a Out.Texture = In.Texture) but all I get is gray shading.

Anyone got any ideas?

EDIT: Hang on, do I *need* a pixel shader? I don't have one setup yet, could this be the source of my problem?

EDIT 2: Nope, I now have a pixel shader that just passes through the color and I still have only gray shading... Any ideas?

EDIT 3: Solved the problem, turns out "ps_3_0" is the compiler version I need to use in D3DXCompileShaderFromFile... Now I just have to figure out what fun things I can *DO* with these things :P

[Edited by - pcmattman on March 20, 2008 5:47:23 AM]

Share this post


Link to post
Share on other sites
Instead of passing "ps_3_0" I'd recommend using D3DXGetPixelShaderProfile() (and D3DXGetVertexShaderProfile() for the vertex shaders). That will pick the appropriate profile for the installed video card. That means that it will run on a 2.0 card as long as you don't use 3.0 only features (or hit the 2.0 instruction limits).

Share this post


Link to post
Share on other sites
Quote:
Original post by Adam_42
Instead of passing "ps_3_0" I'd recommend using D3DXGetPixelShaderProfile() (and D3DXGetVertexShaderProfile() for the vertex shaders). That will pick the appropriate profile for the installed video card. That means that it will run on a 2.0 card as long as you don't use 3.0 only features (or hit the 2.0 instruction limits).


While it's okay to do that for now, I wouldn't recommend using that function for compiling every shader in anything where performance is a concern. Nvidia's SM3.0 GPU's (including G80) tend to run ps_3_0 shaders much more slowly than ps_2_0 or ps_2_a. In my experience ATI GPU's don't seem to care, but my tests have been anything but exhaustive.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!