Jump to content

  • Log In with Google      Sign In   
  • Create Account

Proper Shader Algorith For Standard Lighting Effect


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 adder_noir   Members   -  Reputation: 271

Like
0Likes
Like

Posted 16 May 2011 - 01:28 AM

Hi,

I've got a load of source compiled and running with the from source engine I'm learning. Quite a big step for me all those static libraries, dynamincally linked libraries, a tonne of SIMD stuff that had to be commented out because it was in the wrong assembly format for my compiler (Intel whereas CB uses AT&T), pixel shader, vertex shaders etc....

Sure I've come a long way. Shoot I even know how most of it works. I printed off a load of paper copies of the source and I often nip out to local pubs for a coffee whilst having a read!

But I have hit a couple of snags in concept stuff. Mainly about what's really going on between the CPU, the RAM and the GPU and the RAM on the graphics card (not sure of proper terminology for this). That's for another thread though, this one is about shaders and lighting.

I've got the shaders to run and work. Have a look at their code, pretty simple as far as I know in shader talk:

Vertex Shader(vsh):
vs.1.1
dcl_position0 v0
dcl_normal0   v3
dcl_texcoord0 v6
m4x4 oPos, v0, c0
mov oD0, c4
mov oT0, v6

The vsh does some stuff with the geometry and then moves the value stored in the constant register c4 into the output register oD0 which is then inputted into the psh. Whenever a function that affects the light is used it gets the value relating to light colour and intensity and loads it into the register c4. SetAmbientLight does this, for example.

Pixel Shader:(psh)
ps.1.1
tex t0
mul r0, v0, t0

Here the value in v0 (which I assume is the received light data from the vsh) is multiplied with the texel and the final result stored in the psh output register r0. Ok fine.

BUT (you knew it was coming).....

The scene is blacker than the underside of a witch's boob on a moonless night. If I change the code for the psh to this:

Pixel Shader:(psh)
ps.1.1
tex t0
add r0, v0, t0

... then all affected geometry becomes so bright you could hunt rabbits with them on a dark winter's evening.

So, obviously something odd is happening here. The original author's shaders produces scenes that are way too dark. Yet my own alterations produce scenes that are horribly bright, and cause very bad colour mixing between the ambient light and the texel colour. Like if for example I set ambient to 0.5f,0.5f,0.5f middle of teh road grey, then instead of the predominatly green appearance of the demo model (a green tank) showing through more strongly, it just has this awful painted grey appearance to it, rather like someone has just thrown a load of grey paint over it.

So:

What is the proper method here? I just want to be able to nicely mimic with a shader what i've already achieved fine with the fixed function pipeline in the past. But I don't know the proper algorithm for lighting. The one I've made is bright but awful, the one the author made is horribly dark, even with ambient light set to max (1.0f,1.0f,1.0f).

I'd also rather not discuss point and directional lights just yet, although I have got these working well, but again I had to depart from the author's code as it was too dark. Furthermore the extra passes per light make things difficult so if possible I'd rather just talk about getting good ambient light working for now, thanks.

I'd appreciate any replies anyone can take the time to write. Seems to me good lighting it what makes a huge difference to the pleasure the eye feels when playing a game or looking at any rendered scene for that matter.

Anway, thanks :)

Sponsor:

#2 thedodgeruk   Members   -  Reputation: 124

Like
1Likes
Like

Posted 16 May 2011 - 05:49 PM

you need to move to proper HLSL now , not the old methid

Hi,

I've got a load of source compiled and running with the from source engine I'm learning. Quite a big step for me all those static libraries, dynamincally linked libraries, a tonne of SIMD stuff that had to be commented out because it was in the wrong assembly format for my compiler (Intel whereas CB uses AT&T), pixel shader, vertex shaders etc....

Sure I've come a long way. Shoot I even know how most of it works. I printed off a load of paper copies of the source and I often nip out to local pubs for a coffee whilst having a read!

But I have hit a couple of snags in concept stuff. Mainly about what's really going on between the CPU, the RAM and the GPU and the RAM on the graphics card (not sure of proper terminology for this). That's for another thread though, this one is about shaders and lighting.

I've got the shaders to run and work. Have a look at their code, pretty simple as far as I know in shader talk:

Vertex Shader(vsh):
vs.1.1
dcl_position0 v0
dcl_normal0   v3
dcl_texcoord0 v6
m4x4 oPos, v0, c0
mov oD0, c4
mov oT0, v6

The vsh does some stuff with the geometry and then moves the value stored in the constant register c4 into the output register oD0 which is then inputted into the psh. Whenever a function that affects the light is used it gets the value relating to light colour and intensity and loads it into the register c4. SetAmbientLight does this, for example.

Pixel Shader:(psh)
ps.1.1
tex t0
mul r0, v0, t0

Here the value in v0 (which I assume is the received light data from the vsh) is multiplied with the texel and the final result stored in the psh output register r0. Ok fine.

BUT (you knew it was coming).....

The scene is blacker than the underside of a witch's boob on a moonless night. If I change the code for the psh to this:

Pixel Shader:(psh)
ps.1.1
tex t0
add r0, v0, t0

... then all affected geometry becomes so bright you could hunt rabbits with them on a dark winter's evening.

So, obviously something odd is happening here. The original author's shaders produces scenes that are way too dark. Yet my own alterations produce scenes that are horribly bright, and cause very bad colour mixing between the ambient light and the texel colour. Like if for example I set ambient to 0.5f,0.5f,0.5f middle of teh road grey, then instead of the predominatly green appearance of the demo model (a green tank) showing through more strongly, it just has this awful painted grey appearance to it, rather like someone has just thrown a load of grey paint over it.

So:

What is the proper method here? I just want to be able to nicely mimic with a shader what i've already achieved fine with the fixed function pipeline in the past. But I don't know the proper algorithm for lighting. The one I've made is bright but awful, the one the author made is horribly dark, even with ambient light set to max (1.0f,1.0f,1.0f).

I'd also rather not discuss point and directional lights just yet, although I have got these working well, but again I had to depart from the author's code as it was too dark. Furthermore the extra passes per light make things difficult so if possible I'd rather just talk about getting good ambient light working for now, thanks.

I'd appreciate any replies anyone can take the time to write. Seems to me good lighting it what makes a huge difference to the pleasure the eye feels when playing a game or looking at any rendered scene for that matter.

Anway, thanks :)



#3 adder_noir   Members   -  Reputation: 271

Like
0Likes
Like

Posted 16 May 2011 - 06:46 PM

Ok thanks. Out of curiousity what benefits does HLSL have over the assembly style syntax?

#4 arbitus   Members   -  Reputation: 436

Like
1Likes
Like

Posted 16 May 2011 - 07:15 PM

Ok thanks. Out of curiousity what benefits does HLSL have over the assembly style syntax?


Well the first benefit is that it makes your code more readable and your intent more clear. I can puzzle out what the assembly is doing in a few minutes, but I would know at a glance what your HLSL is doing. Finding bugs and mistakes would be easier because I would see what your intent is inside your code. Also, modern compilers are generally more intelligent than humans at making assembly level optimizations as well as being capable of compiling to multiple types of assembly from the same HLSL code.

#5 TheUnbeliever   Members   -  Reputation: 961

Like
1Likes
Like

Posted 16 May 2011 - 08:31 PM

Side point: I assume CodeBlocks uses GCC. In this case, if you can find a setting that lets you manually configure command line arguments, then changing this to pass "-masm=intel" to GCC should work.
[TheUnbeliever]

#6 adder_noir   Members   -  Reputation: 271

Like
0Likes
Like

Posted 17 May 2011 - 04:54 AM

This is good information. Especially the idea about the GCC thing that may well have to be tried out sometime! I've got a simpe demo of a vertex shader here:

float4x4 World;
                            float4x4 View;
                            float4x4 Projection;
                            
                            struct VertexOut
                            {
                                float4 Pos : POSITION;
                                float4 Color : COLOR;
                            };
                            
                            VertexOut VShader(float4 Pos : POSITION)
                            {
                                VertexOut Vert;
                                float4x4 Transform;
                            
                                Transform = mul(World, View);
                                Transform = mul(Transform, Projection);
                                Vert.Pos = mul(Pos, Transform);
                            
                                Vert.Color = float4(1, 1, 1, 1);
                            
                                return Vert;
                            }
                            
                            technique FirstTechnique
                            {
                                pass FirstPass
                                {
                                    Lighting = FALSE;
                                    ZEnable = TRUE;
                            
                                    VertexShader = compile vs_2_0 VShader();
                                }
                            }

But I'm not sure if it's suitable for ambient lighting. Anyways I'll have a read through the DirectX tutorial again and also the part of my book which covers shaders. If I find anything interesting I'll let you know. Thanks.

#7 adder_noir   Members   -  Reputation: 271

Like
0Likes
Like

Posted 18 May 2011 - 04:28 AM

Hi just a quick question, can you use HLSL for both vertex and pixel shaders? Is this a stupid question? I voted everyone up who replied to this thread so thanks ;)

#8 Radikalizm   Crossbones+   -  Reputation: 2883

Like
1Likes
Like

Posted 18 May 2011 - 05:54 AM

Hi just a quick question, can you use HLSL for both vertex and pixel shaders? Is this a stupid question? I voted everyone up who replied to this thread so thanks ;)


Yes, HLSL can be used for both vertex and pixel shaders, and starting from D3D10 also geometry shaders

I gets all your texture budgets!


#9 adder_noir   Members   -  Reputation: 271

Like
0Likes
Like

Posted 18 May 2011 - 04:17 PM


Hi just a quick question, can you use HLSL for both vertex and pixel shaders? Is this a stupid question? I voted everyone up who replied to this thread so thanks ;)


Yes, HLSL can be used for both vertex and pixel shaders, and starting from D3D10 also geometry shaders



Cheers, I voted you up :D




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS