Jump to content
  • Advertisement
Sign in to follow this  
LExTER

Help with HLSL

This topic is 3757 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, I'm new around here... I've started learning HLSL for DirectX9, and I've tried many tricks with my pixel shaders and my vertex shaders and I have got problems, here are the shaders (I'm using NVIDIA FX Composer to watch them): texture tex; float3 lightpos = { 0,10,-10}; matrix worldviewproj:worldviewprojection; static float t = 55; sampler sam = sampler_state{texture = tex; FILTER = MIN_MAG_MIP_LINEAR; }; struct vs_ret { float4 pos:position; float2 tex1:texcoord1; float3 test:texcoord0; }; struct vs_in{ float4 pos:position; float2 tex1:texcoord1; float3 test:texcoord0; }; vs_ret VS(vs_in ins) { vs_ret tmp = (vs_ret)0; tmp.tex1=float2(ins.tex1.x/2,ins.tex1.y); tmp.pos=mul(ins.pos,worldviewproj); tmp.test = float3(5,4,3); tmp.test.x=5; return tmp; } float4 PS(float2 tex:texcoord1,float3 test:texcoord0):COLOR { float4 col = 0; col = tex2D(sam,tex); if (test.x == 5) col = 0; return col; } technique Simplest { pass Pass0 { VertexShader = compile vs_3_0 VS(); PixelShader = compile ps_3_0 PS(); CullMode = none; } } The problem is that sometimes texcoord0 is changed between vertex and pixel shader, and change its values.. (I know there is a problem because I made a check in the Pixel Shader which checks if texcoord0 has changed, and if it has, it turns the current pixel into black, and I've got weird black lines on the mesh, indicating that texcoord0 has changed..). Can someone explain me why? or what am I doing wrong? Thank you very much.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
... which checks if texcoord0 has changed, and if it has, it turns the current pixel into black ...


Skimming over the shader, I think your code actually works the other way around. If I read correctly, in the vertex shader you're setting test.x (ie texcoord0.x) to 5 and check if this equals 5 in the pixel shader. So if anything, it'll output a black color if it is exactly equal to 5. That it doesn't make everything all black might be explained by floating point inaccuracy, perhaps made worse by the interpolation done before texcoord0 reaches the pixel shader. A more appropiate test would be


if (abs(test.x - 5) > 0.0001) // or some other small number, the error delta


I'm not entirely sure you need to worry about floating point inaccuracy here, but I'm sure someone will correct me if I'm wrong [smile] Aside from my dodgy analysis though, exactly how is this causing you problems? What are you trying to do where you run into this issue?

Share this post


Link to post
Share on other sites
Thanks, It was it.
I'm trying to "play" with HLSL to get a better understanding about it, later I'll build my own shaders for my projects.
But can you explain me why does it happen? It doesnt really make any sense..

Thank you very much.

Share this post


Link to post
Share on other sites
Its a floating point error, the short version is that the way that floating point numbers are stored in memory is sort of a shorthand notation(EDIT: it is actually exponential notation, if you are being specific), and as such "floating point" numbers dont exactly have all the possible precision you think they do and the precision you can get changes depending on what number range you are dealing with(there are actually less numbers you can store between 10000 and 10001 than between say 0 and 1 EDIT: if you look at the IEEE wikipedia page, you can see why this is true). Because of this you arent 100% guaranteed that the floating point number you want(especially if you add, subtract, multiply or divide floating point numbers before you check them) is EXACTLY right. Because of this it is best practice to try and check for being inside a very small range rather than being spot on to a specific number.

Wikipedia article on IEEE 754 format (floating point format)

http://en.wikipedia.org/wiki/IEEE_754

Microsoft's explanation on floating point

http://msdn.microsoft.com/en-us/library/c151dt3s(VS.80).aspx

I just found these real quick by googling "floating point precision" so theres more links there, but these are the best I saw.

Share this post


Link to post
Share on other sites
Thank you both, I've got a better understanding how it works..
I'm looking for a good tutorial for HLSL, and I cant find one.
Can you give me a link?

Thanks.

Share this post


Link to post
Share on other sites
I've seen this tutorial before, didn't like it too much, do you have another tutorial?

Thanks

Share this post


Link to post
Share on other sites
depends on what you are looking for

this article

http://www.gamasutra.com/features/20030418/engel_03.shtml

has a pretty simple explanation (its actually pretty much from the guy's book) on doing phong shading on objects using shaders. It was one of the first HLSL shaders I made, and the end result I found to be more than satisfactory. In the article he only goes over the .fx files, and not the C hooks to send data to the shader, however he does also have his source code up so you can look at that and learn from it.

Wolfgang Engel wrote the article, he is the editor for all the ShaderX series books, and also wrote Programming Vertex and Pixel Shaders. All great books in my opinion. To really get a grasp on shaders it would be best if you could find an actual book to start you off. Look at your local library and see if they possibly have

http://www.amazon.com/Introduction-Game-Programming-Direct-9-0c/dp/1598220160/ref=pd_bbs_sr_4?ie=UTF8&s=books&qid=1213304502&sr=8-4

or the before mentioned

http://www.amazon.com/Programming-Vertex-Pixel-Shaders/dp/1584503491/ref=pd_bbs_6?ie=UTF8&s=books&qid=1213304502&sr=8-6

these books aren't cheap(well the first one isn't so bad) so if you could find them at a library its worth a shot to save some cash. If not buying the intro to game programming direct 9.0c isn't a bad idea, I have the earlier revision of the same book, and I have seen that version as well. The newer one goes more in depth into HLSL than the previous one which is good.

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!