Pages: 1 [2] 3 4 5 6 7   Go Down
  Print  
Author Topic: My first deferred setup.  (Read 21936 times)
April 26, 2011, 08:47:45 pm
There is bug in normal calculation in tight angle's. Dose anyone have the same issue?Normal result is not fix. It shift color a bit with camera movement.
G-Buffer code.
Code:
float4x4 wvp : WorldViewProjection ;
float4x4 wv : WorldView;
float4x4 world : World;

texture Albedo_Spec : TEXTURE0
<
string ResourceType = "2D";
>;
 
sampler2D Albedo_Spec_Sampler = sampler_state
{
Texture = <Albedo_Spec>;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};
 


// input from application to vertex shader
struct a2v {
float4 position : POSITION;
float3 Normal       : NORMAL;
float2 texCoord : TEXCOORD0;

};

// output from vertex shader to pixel shader
struct v2f {
    float4 position     : POSITION;
float  Depth        : TEXCOORD0;
float3 Normal       : TEXCOORD1;
float2 texCoord : TEXCOORD2;

};

// output from pixel shader
struct PixelShaderOut {
        float4 rtarget0 : COLOR0;
        float4 rtarget1 : COLOR1;
        float4 rtarget2 : COLOR2;
        float4 rtarget3 : COLOR3;
};

v2f av(a2v In)
{
v2f Out = (v2f)0;
Out.position = mul(In.position, wvp); //transform vert position to homogeneous clip space
Out.Depth = mul(In.position, wv).z;
    //Out.Normal = mul(In.Normal, world);
Out.Normal = mul(float4(In.Normal, 0), wv).xyz;
Out.texCoord = In.texCoord; //pass through texture coordinates from channel 1

return Out;
}

float2 SphereMapEncode2( float3 normal )
{
    float2 enc = normalize(normal.xy) * sqrt(normal.z*0.5f + 0.5f);
    enc = enc*0.5 + 0.5f;
    return enc;
}

PixelShaderOut af(v2f In)
{
// Color 0:

float4 input1 = float4(In.Depth, 0, 0, 0);

// Color 1:
//float4 Depth_Normal = float4(In.Depth,In.Normal.xyz);
float2 EncNormal = SphereMapEncode2(normalize(In.Normal.xyz));
float4 input2 = float4 (EncNormal, 0,0);

// Color 2:
float4 Albedo_Spec = tex2D(Albedo_Spec_Sampler, In.texCoord.xy);
float4 input3 = float4(Albedo_Spec.rgb,1);

// Color 3:
float4 proxy2 = float4(1,0,0,1);
float4 input4 = proxy2;

// Output Pixel Shader:
PixelShaderOut output;
output.rtarget0 = input1;
output.rtarget1 = input2;
output.rtarget2 = input3;
output.rtarget3 = input4;
return output;
}

technique GBuffer  
{  
pass pass0  
    {
VertexShader = compile vs_3_0 av();
ZEnable = TRUE;
ZWriteEnable = TRUE;
ZFunc = LESS;
StencilEnable = TRUE;
stencilfunc = ALWAYS;
StencilPass = REPLACE;
StencilFail = KEEP;
StencilZFail = KEEP;
StencilRef = 1;
PixelShader = compile ps_3_0 af();
}  

}    

btw. I run my deferred setup on ati card and fps drop alot. It was a new ati card. Dont remember its model.


* deffered-08.jpg (228.66 KB, 1024x1845 - viewed 411 times.)

ali-rahimi.net
April 26, 2011, 11:54:54 pm
http://diaryofagraphicsprogrammer.blogspot.com/2008/03/light-pre-pass-renderer.html

Good discussion here.

In my system I ended up putting material ID and anim ID in the UV coords and sending arrays to the shader.
April 27, 2011, 10:53:07 am
Thank's Tom.
there is notification in last post. Today at 09:12:36 am
by Jos van Ouwerkerk
But i dont see any post from jos. same for everyone?  Huh?

ali-rahimi.net
April 27, 2011, 11:10:06 am
I can't delete a message without all of them deleting. And if I post, it all comes back again. Ai, all gone again. If you press reply you'll see my missing posts. Many of the same... I'll report to the moderator.
April 27, 2011, 11:10:30 am
There is bug in normal calculation in tight angle's.

The suggestion Viktor made earlier seems very stable and cheap. There is a nice overview here. Method #4 is the one Viktor mentioned.

I also saw another technique which seems pretty ok:
Code:
float2 encode(float3 n) {
   float2 res = 0.6f + 0.5f * n.xy;
   res.x *= sign(n.z);
   return res;
}

float3 decode(float2 e) {
   float3 res = 2.f * float3(abs(e), 0.f) - 1.2f;
   res.z = sign(e.x) * sqrt(1.f - dot(res.xy, res.xy));
   return res;
}
April 27, 2011, 11:47:26 am
Oh. yes forum issue again. Damn spam's.

ali-rahimi.net
April 29, 2011, 01:34:59 pm
Ok. Finally i add light array, soft particle, soft edge water.

Code:
float4x4 MATRIX0 : CHANNELMATRIX0;
float4x4 MATRIX1 : CHANNELMATRIX1;
float4x4 MATRIX2 : CHANNELMATRIX2;
float4x4 MATRIX3 : CHANNELMATRIX3;
float4x4 MATRIX4 : CHANNELMATRIX4;
float4x4 MATRIX5 : CHANNELMATRIX5;
float4x4 MATRIX6 : CHANNELMATRIX6;
float4x4 MATRIX7 : CHANNELMATRIX7;
float4x4 MATRIX8 : CHANNELMATRIX8;
float4x4 MATRIX9 : CHANNELMATRIX9;
float4x4 MATRIX10 : CHANNELMATRIX10;
float4x4 MATRIX11 : CHANNELMATRIX11;
float4x4 MATRIX12 : CHANNELMATRIX12;
float4x4 MATRIX13 : CHANNELMATRIX13;
float4x4 MATRIX14 : CHANNELMATRIX14;
float4x4 MATRIX15 : CHANNELMATRIX15;
float4x4 MATRIX16 : CHANNELMATRIX16;
float4x4 MATRIX17 : CHANNELMATRIX17;
float4x4 MATRIX18 : CHANNELMATRIX18;
float4x4 MATRIX19 : CHANNELMATRIX19;
float4x4 MATRIX20 : CHANNELMATRIX20;

static float4x4 L_Light_Data[21] =
{
MATRIX0,
MATRIX1,
        MATRIX2,
MATRIX3,
MATRIX4,
MATRIX5,
MATRIX6,
MATRIX7,
MATRIX8,
MATRIX9,
        MATRIX10,
MATRIX11,
MATRIX12,
MATRIX13,
MATRIX14,
MATRIX15,
MATRIX16,
MATRIX17,
MATRIX18,
    MATRIX19,
MATRIX20,
};

//-------------------------------------------------------------------------------
// L_Light_Array function
//-------------------------------------------------------------------------------
float3 L_Light_Array(float2 coord, float3 WorldPos,float3 EyeVec, float3 Normal)
{
float3 L_Light_Result = 0;
for(int i=0; i<21; i++)
{
    //Noralize light vector -------------------------------------------------------------------
float3 L_Vector = normalize(L_Light_Data[i] [0].xyz-WorldPos.xyz);
    //Att -------------------------------------------------------------------
float L_Length =  length(L_Light_Data[i] [0].xyz-WorldPos);
float L_Att = (1 / ((L_Length * L_Length) * L_Light_Data[i] [0].w));
//Specular -------------------------------------------------------------------
float3 Normalized_HalfAngle = normalize((L_Vector + EyeVec.xyz));
float NdotH_specular = saturate(dot(Normalized_HalfAngle,Normal));
float NdotH_pow_Glossiness = pow(NdotH_specular,L_Light_Data[i] [1].w);
float3 Specular_Ligthing = (L_Light_Data[i] [1].xyz * NdotH_pow_Glossiness);
//Direct -------------------------------------------------------------------
float  L_Lambert = max(0, dot(L_Vector, Normal));
    float3 Diffuse_Ligthing = (L_Light_Data[i] [1].xyz*L_Lambert);

float3 Output = L_Att*(Specular_Ligthing+Diffuse_Ligthing);


L_Light_Result += Output;
}
return  L_Light_Result;
}


Any tip?


* deffered-09.jpg (171.34 KB, 1815x1011 - viewed 426 times.)

ali-rahimi.net
April 29, 2011, 02:00:19 pm
Great progress Ali!!!
April 29, 2011, 03:30:03 pm
Thanks Tom. Now the hardest part for me started. How the hell i should import ton's of static data for low frequency Baked Volumetric occ or GI?  I read several post, but again have no clue how do do it. If someone guide me or send me a simple example, Then i can do it. Btw i don't implement  any Quad or volumetric culling for lights.

ali-rahimi.net
May 02, 2011, 09:26:38 am
Oki. Another question. I want to setup a scissor with Stencil (To optimize lighting) But don't find any example. IS this even possible or i must use clip instead of Stencil? Any example?
Some related topic's:
http://forum.quest3d.com/index.php?topic=62528.0
http://forum.quest3d.com/index.php?topic=69347.0
http://forum.quest3d.com/index.php?topic=64140.0

ali-rahimi.net
May 02, 2011, 11:55:11 am
If you use the stencil buffer for more advanced tasks like this I recommend to skip all stencil command in Quest3D and just set the stencil render states in the shader technique.

For lighting a deferred scene this does not seem necessary however. For an omni for example you can just render a sphere with z-test enabled and z-write disabled. Where the sphere is drawn, light might fall and you do the lighting calculations. The rest will be skipped. Note that this skips pixels in front and to the side of the light volume, but not pixels behind the light volume.

The only exception is when the camera is inside the light volume. Then you can still optimize, but it is more difficult. Since the chance of the camera being in the volume is fairly small, I'd just do a full screen lighting pass if the camera is inside.
May 03, 2011, 07:54:23 am
Thanks again Jos. However i don't grasp the concept . I need a separate rtt for the masking?  Huh?

ali-rahimi.net
May 03, 2011, 08:35:27 am
No Neo, I'm saying that when you're ready, you don't need to mask.

Instead of first masking where to let the light influence the scene and then rendering the light, you just render the light only where it influences the scene. You let the geometry of the light range and the z-buffer do the work for you.
May 03, 2011, 08:47:47 am
If i get it right. It mean i must use several sphere (light volume) in my final deferred pp instead of a pixel perfect square? For example consider i have 10 point light with decay, Then i must build 10 sphere and assign each light ps calculation to it's related volume mesh ? Again if i'm right. Then how should i assign each light ps calculation to it's related volume? Or am i totally wrong?
Thanks.

ali-rahimi.net
May 03, 2011, 10:08:29 am
You are right. That is the idea. You can just use the same mesh for each light. Lets say you use a global shader for it. For each light you set the color and other settings, set the world matrix and commit and then call the mesh.
Pages: 1 [2] 3 4 5 6 7   Go Down
  Print  
 
Jump to: