Register  |  FAQ  |  Search  |  Memberlist  |  Usergroups  |  Log in 
Reply to topic
 shader text 
liverol


Joined: 29 Jun 2007
Posts: 45
Reply with quote
hi,
i tried the nvidia cgtoolkit and the code it genreated like this:

Code:
// Bump2
//

// Constants
float4 lhtDir < string UIDirectional = "Light Direction"; >;
float4 lhtPos < string UIPosition = "Light Position"; >;
vector vCPS : CameraPosition; 
matrix mTot : WorldViewProjection;
matrix mWldInv : WorldIT;
matrix mWld :  World;

// Model and Texture Names
string XFile = "default.x";
texture tBase < string name = "base.tga"; >;
texture tBump < string name = "bump.dds"; >;

struct VS_INPUT
{
    float4  Pos     : POSITION;
    float3  Normal  : NORMAL;
    float3  Tex0    : TEXCOORD0;
    float3  Tangent : TANGENT;   
};

struct VS_OUTPUT
{
    float4  Pos           : POSITION;
    float3  tLight        : COLOR;
    float3  tNormal       : TEXCOORD0;   
    float3  depRead       : TEXCOORD1;   
    float3  tH            : TEXCOORD2;
    float3  Tex0          : TEXCOORD3;   
};

VS_OUTPUT VShade(VS_INPUT i, uniform int ShadeMethod)
{
    VS_OUTPUT   o;
    float3      wNormal, wTangent, wBinorm, tLight;
   
    //tranform position   
    o.Pos = mul( i.Pos, mTot);
           
    //transform normal
    //and tangent
    wNormal = mul( i.Normal, mWld );
    wTangent = mul( i.Tangent, mWld );

    //lee, normalize to defeat scaled world matrices
    wNormal = normalize(wNormal);
    wTangent = normalize(wTangent);

    //Cross product to create BiNormal
    wBinorm = cross( wTangent, wNormal );

    // The first method is diffuse dot3 bumpmap
    // transform the light vector into Tangent Space   
    float3x3 tangentMatrix = {wTangent, wBinorm, wNormal};
    tangentMatrix = transpose( tangentMatrix);
    tLight = mul(-lhtDir, tangentMatrix);
   
    // all other methods are specular dot3 bump
    // and therefore use the half angle instead of the light       
    // compute the half angle vector   
    float3 wPos, H, tH;
       
    wPos = mul( i.Pos, mWld);
       
    H = vCPS - wPos;
    H = normalize(H);
     
    H = H -lhtDir.xyz;
    H = normalize(H);

    //transform the half angle vector into tangent space
    tH = mul(H, tangentMatrix);
    o.tH = tH;
    o.depRead = tH;   

    if (ShadeMethod==1)
       tLight = tH;

    //mutiply by a half to bias, then add half
    o.tLight = 0.5f + tLight * 0.5f;
   
    // Copy UVs
    o.Tex0 = i.Tex0;                 
    o.tNormal = i.Tex0;                 
   
    return o;
}


it's vary from the shader used in aw i think,and i tried the code nothing happened.
View user's profile Send private message
  
awingsoft
Site Admin

Joined: 25 Dec 2006
Posts: 98
Reply with quote
These are cg source code, you need compile these to DX8.1 vertex/pixel shader codes; see
"DirectX Vertex Shader 1.1 Profile" in the Cg User Manual.
View user's profile Send private message Send e-mail
 thx 
liverol


Joined: 29 Jun 2007
Posts: 45
Reply with quote
thanks,i'm new to shader
View user's profile Send private message
 example 
liverol


Joined: 29 Jun 2007
Posts: 45
Reply with quote
still can't figure this out,
would you please give a simple example to show how
to convert the source code to shader text.i tried the cgc -profile -ps/vs1_1 but what i got is this:

Code:
// Pixel Shader 1.1 generated by NVIDIA Cg compiler
// cgc version 1.5.0019, build date Feb 22 2007 06:26:18
// command line args: -profile ps_1_1
// source file: dl.cg
//vendor NVIDIA Corporation
//version 1.5.0.19
//profile ps_1_1
//program main
//semantic main.colorMap
//semantic main.normalMap
//var sampler2D colorMap :  :  0 : 1 : 1
//var sampler2D normalMap :  :  1 : 2 : 1
//var float4 I.lightVec : $vin.COLOR0 : COLOR0 : 0 : 1
//var float4 I.texCoord0 : $vin.TEXCOORD0 : TEXCOORD0 : 0 : 1
//var float4 I.texCoord1 : $vin.TEXCOORD1 : TEXCOORD1 : 0 : 1
//var float4 main : $vout.COLOR : COLOR : -1 : 1
ps.1.1
tex t0
tex t1
dp3_sat t1.rgb, v0_bx2, t1_bx2
mul r0.rgb, t1, t0
+ mul r0.a, t1.b, t0.a
// 4 instructions
View user's profile Send private message
  
Vincent


Joined: 25 Dec 2006
Posts: 296
Reply with quote
good, you've aready got the asm shader codes. Then you should use the asm codes in d3d effect like below:

Code:
technique tec0
{
    pass p0
    {
        // Load matrices
        VertexShaderConstant[0] = ;    // World Matrix
        VertexShaderConstant[4] = ;   // World*View*Proj Matrix
 
        // Material properties of object
        VertexShaderConstant[9]  = (0.8f,0.8f,0.8f,0.8f); // Diffuse
        VertexShaderConstant[10] = (0.0f,0.0f,0.0f,0.0f); // Specular
        VertexShaderConstant[11] = (0.0f,0.0f,0.0f,0.0f); // Ambient
   
        // Properties of light   
        VertexShaderConstant[13] = (0.7f,0.7f,0.7f,0.7f); // Diffuse
        VertexShaderConstant[14] = (0.0f,0.0f,0.0f,0.0f); // Specular
        VertexShaderConstant[15] = (0.0f,0.0f,0.0f,0.0f); // Ambient
        VertexShaderConstant[16] = ;   //Light Direction

        vertexShaderConstant[20] = (.5f,.5f,.5f,.5f);
   
        // Camera information
        VertexShaderConstant[24] = ;   

        Texture[0]   = ;
        Texture[1]   = ;
        wrap0        = U | V;
        wrap1        = U | V;
   
        AddressU[0] = wrap;
        AddressV[0] = wrap;
        AddressU[1] = wrap;
        AddressV[1] = wrap;

        MinFilter[0] = Linear;
        MagFilter[0] = Linear;
        MinFilter[1] = Linear;
        MagFilter[1] = Linear;

        VertexShader =
        decl
        {
            stream 0;
            float v0[3]; // Position
            float v3[3]; // Normal
            float v7[3]; // Texture Coord1
            float v8[3]; // Tangent
        }   
        asm
        {
            vs.1.1
            // Transform position
            m4x4 oPos,v0,c4
           
            // Transform normal and tangent
            m3x3 r7,v8,c0
            m3x3 r8,v3,c0
           
            // Cross product
            mul r0,-r7.zxyw,r8.yzxw;
            mad r5,-r7.yzxw,r8.zxyw,-r0;
           
            // Transform the light vector
            dp3 r6.x,r7,-c16
            dp3 r6.y,r5,-c16
            dp3 r6.z,r8,-c16
           
            // Multiply by a half to bias, then add half
            mad r6.xyz,r6.xyz,c20,c20
           
            mov oT0.xy,v7.xy
            mov oT1.xy,v7.xy
            mov oD0.xyz,r6.xyz
        };   
   
        PixelShader =
        asm
        {
            ps.1.1
            tex t0    // Sample texture
            tex t1    // Sample normal
            mov r0,t1
            dp3   r0,t1_bx2,v0_bx2; // Dot(light,normal)
            mul r0,t0,r0          // Modulate against base color
        };    
    }
}


these codes are taken from DX8.1 doc, you can find more examples by indexing "Effects" in the doc.

maybe I'll write a tutorial for this when I have time.
View user's profile Send private message
 ok 
liverol


Joined: 29 Jun 2007
Posts: 45
Reply with quote
understand the asm part now,but just the first sight,
i'll wait.
View user's profile Send private message
 several methods to get assembly shader code 
Vincent


Joined: 25 Dec 2006
Posts: 296
Reply with quote
except Cg, there are also other shader tools: ATI RenderMonkey, Nvidia FXComposer, DX Effect Edit, etc.
They all can produce dx9 effect file (.fx, contain HLSL code), then you can use dx9 shader compiler (fxc.exe) to convert HLSL codes to assembly codes. But Awakening use dx8.1, so it only supports vs1.0, vs1.1, ps1.0 ~ ps1.4.

In IDE of some shader tools, you also can view the assembly codes, just need copy & paste to use.
View user's profile Send private message
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT  
Page 1 of 1  

  
  
 Reply to topic