Sign in to follow this  
adder_noir

Proper Shader Algorith For Standard Lighting Effect

Recommended Posts

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:

[code]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[/code]

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.

[code]Pixel Shader:(psh)
ps.1.1
tex t0
mul r0, v0, t0
[/code]

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 [i]knew[/i] 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:

[code]Pixel Shader:(psh)
ps.1.1
tex t0
add r0, v0, t0
[/code]

... 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 :)

Share this post


Link to post
Share on other sites
you need to move to proper HLSL now , not the old methid

[quote name='adder_noir' timestamp='1305530935' post='4811336']
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:

[code]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[/code]

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.

[code]Pixel Shader:(psh)
ps.1.1
tex t0
mul r0, v0, t0
[/code]

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 [i]knew[/i] 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:

[code]Pixel Shader:(psh)
ps.1.1
tex t0
add r0, v0, t0
[/code]

... 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 :)
[/quote]

Share this post


Link to post
Share on other sites
[quote name='adder_noir' timestamp='1305593177' post='4811716']
Ok thanks. Out of curiousity what benefits does HLSL have over the assembly style syntax?
[/quote]

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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:

[code]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();
}
}[/code]

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.

Share this post


Link to post
Share on other sites
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 ;)

Share this post


Link to post
Share on other sites
[quote name='adder_noir' timestamp='1305714481' post='4812447']
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 ;)
[/quote]

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

Share this post


Link to post
Share on other sites
[quote name='Radikalizm' timestamp='1305719686' post='4812476']
[quote name='adder_noir' timestamp='1305714481' post='4812447']
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 ;)
[/quote]

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


Cheers, I voted you up :D

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this