游戏开发论坛

 找回密码
 立即注册
搜索
查看: 1731|回复: 4

DX 8&9 SHADER 问题

[复制链接]

16

主题

59

帖子

59

积分

注册会员

Rank: 2

积分
59
发表于 2010-5-8 19:40:00 | 显示全部楼层 |阅读模式
现在手里有一个DX8的shader 不知道DX9的话可以识别吗? 类似mul r0, c14, v7.x这样的语句 在9里是不是要写成
r0 = mul(c14,v7.x); 这里的r0是临时寄存器 不需要事先声明 DX9里有类似这样的东西吗? 顺便把这个文件也共享一下吧
// v0    - Vertex Position
// v3    - Vertex Normal
// v7    - Vertex Texture Data u,v
// v8    - Vertex Tangent (v direction)
// v5    - Wave Height Scale
//
// c0    - { 0.0,  0.5, 1.0, 2.0}
// c1    - { 4.0, .5pi, pi, 2pi}  
// c2    - {1, -1/3!, 1/5!, -1/7!  }  //for sin
// c3    - {1/2!, -1/4!, 1/6!, -1/8!  }  //for cos
// c4-7 - Composite World-View-Projection Matrix
// c8     - ModelSpace Camera Position
// c9     - ModelSpace Light Position
// c10   - {fixup factor for taylor series imprecision, }(1.02, 0.1, 0, 0)
// c11   - {waveHeight0, waveHeight1, waveHeight2, waveHeight3}  (80.0, 100.0, 5.0, 5.0)
// c12   - {waveOffset0, waveOffset1, waveOffset2, waveOffset3}  (0.0, 0.2, 0.0, 0.0)
// c13   - {waveSpeed0, waveSpeed1, waveSpeed2, waveSpeed3}   (0.2, 0.15, 0.4, 0.4)
// c14   - {waveDirX0, waveDirX1, waveDirX2, waveDirX3}   (0.25, 0.0, -0.7, -0.8)
// c15   - {waveDirY0, waveDirY1, waveDirY2, waveDirY3}   (0.0, 0.15, -0.7, 0.1)
// c16   - { time, sin(time)}
// c17   - {basetexcoord distortion x0, y0, x1, y1} (0.031, 0.04, -0.03, 0.02)
// c18   - World Martix
vs.1.1

mul r0, c14, v7.x       // use tex coords as inputs to sinusoidal warp
mad r0, c15, v7.y, r0   // use tex coords as inputs to sinusoidal warp

mov r1, c16.x           //time...
mad r0, r1, c13, r0     // add scaled time to move bumps according to frequency
add r0, r0, c12         // starting time offset
frc r0.xy, r0           // take frac of all 4 components
frc r1.xy, r0.zwzw      //
mov r0.zw, r1.xyxy      //

mul r0, r0, c10.x       // multiply by fixup factor (due to inaccuracy)
sub r0, r0, c0.y        // subtract .5
mul r0, r0, c1.w        // mult tex coords by 2pi  coords range from(-pi to pi)

mul r5, r0, r0          // (wave vec)^2
mul r1, r5, r0          // (wave vec)^3
mul r6, r1, r0          // (wave vec)^4
mul r2, r6, r0          // (wave vec)^5
mul r7, r2, r0          // (wave vec)^6
mul r3, r7, r0          // (wave vec)^7
mul r8, r3, r0          // (wave vec)^8

mad r4, r1, c2.y, r0    //(wave vec) - ((wave vec)^3)/3!
mad r4, r2, c2.z, r4    //  + ((wave vec)^5)/5!
mad r4, r3, c2.w, r4    //  - ((wave vec)^7)/7!

mov r0, c0.z            //1
mad r5, r5, c3.x ,r0    //-(wave vec)^2/2!
mad r5, r6, c3.y, r5    //+(wave vec)^4/4!
mad r5, r7, c3.z, r5    //-(wave vec)^6/6!
mad r5, r8, c3.w, r5    //+(wave vec)^8/8!

sub r0, c0.z, v5.x      //... 1-wave scale
mul r4, r4, r0          // scale sin
mul r5, r5, r0          // scale cos
dp4 r0, r4, c11         //multiply wave heights by waves

mul r0.xyz, v3, r0      //multiply wave magnitude at this vertex by normal
add r0.xyz, r0, v0      //add to position
mov r0.w, c0.z          //homogenous component

m4x4    oPos, r0, c4    // OutPos = ObjSpacePos * World-View-Projection Matrix

mul     r1, r5, c11               //cos* waveheight
dp4     r9.x, -r1, c14            //normal x offset
dp4     r9.yzw, -r1, c15          //normal y offset and tangent offset

mov     r5, v3                    //starting normal
mad     r5.xy, r9, c10.y, r5      //warped normal move nx, ny according to
                                  //cos*wavedir*waveeheight

mov     r4, v8                    //tangent
mad     r4.z, -r9.x, c10.y, r4.z  //warped tangent vector  

mov     r10, r5
m3x3    r5, r10, c18       // transform normal
dp3     r10.x, r5, r5
rsq     r10.y, r10.x
mul     r5, r5, r10.y             //normalize normal

mov     r10, r4
m3x3    r4, r10, c18       // transform tangent
dp3     r10.x, r4, r4
rsq     r10.y, r10.x
mul     r4, r4, r10.y             //normalize tangent

mul     r3, r4.yzxw, r5.zxyw
mad     r3, r4.zxyw, -r5.yzxw, r3 //xprod to find binormal

mov     r10, r0
m4x4    r0, r10, c18       // transform vertex position

sub     r2, c8,  r0                //view vector
dp3     r10.x, r2, r2
rsq     r10.y, r10.x
mul     r2, r2, r10.y             //normalized view vector         

mov     r0, c16.x
mul     r0, r0, c17.xyxy
frc     r0.xy, r0             //frc of incoming time
add     r0, v7, r0            //add time to tex coords
mov     oT0, r0               //distorted tex coord 0

mov     r0, c16.x
mul     r0, r0, c17.zwzw
frc     r0.xy, r0             //frc of incoming time         
add     r0, v7, r0            //add time to tex coords
mov     oT1, r0.yxzw          //distorted tex coord 1

mov     oT2, r2               //pass in view vector (worldspace)
mov     oT3, r3               //binormal
mov     oT4, r4               //tangent
mov     oT5, r5               //normal


[em1]

1

主题

266

帖子

280

积分

中级会员

Rank: 3Rank: 3

积分
280
发表于 2010-5-8 22:54:00 | 显示全部楼层

Re:DX 8&9 SHADER 问题

这里DX8的shader用的是汇编。
现在大都用HLSL。

16

主题

59

帖子

59

积分

注册会员

Rank: 2

积分
59
 楼主| 发表于 2010-5-9 05:11:00 | 显示全部楼层

Re:DX 8&9 SHADER 问题

是啊 所以我想给它改成HLSL 发现直接编辑TXT有点不方便 一般大家都是用什么写SHADER ?

16

主题

59

帖子

59

积分

注册会员

Rank: 2

积分
59
 楼主| 发表于 2010-5-9 05:12:00 | 显示全部楼层

Re:DX 8&9 SHADER 问题

试着改了一下 肯定很多错误 帮忙指出 谢谢
//Ocean Surface VS
float4 c0  = {0.0,  0.5, 1.0, 2.0};        //use for calculation
float4 c1  = {4.0, .5pi, pi, 2pi};        //use for calculation
float4 c2  = { 1.0f,-1.0f/6.0f,1.0f/120.0f,-1.0f/5040.0f};      //for sin
float4 c3  = {1.0f/2.0f,-1.0f/24.0f,1.0f/720.0f,-1.0f/40320.0f};//for cos
matrix WorldViewProjectionMatrix;       //Composite World-View-Projection
float4 c8;                                //ModelSpace Camera Position
float4 c9;                                //ModelSpace Light Position
float4 c10 = {1.02, 0.1, 0, 0};                //fixup factor for taylor                                                         //series imprecision
float4 c11 = {80.0, 100.0, 5.0, 5.0};        //waveHeight
float4 c12 = {0.0, 0.2, 0.0, 0.0};        //waveOffset
float4 c13 = {0.2, 0.15, 0.4, 0.4};        //waveSpeed
float4 c14 = {0.25, 0.0, -0.7, -0.8};         //frequency of the wave relative to S
float4 c15 = {0.0, 0.15, -0.7, 0.1};          //frequency of the wave relative to T
float4 c16 = {0.0,0.0,0.0,0.0};                //time, sin(time)
float4 c17 = {0.031, 0.04, -0.03, 0.02};//basetexcoord distortion x0,y0,x1,y1
matrix c18;                                //World Martix

// v0    - Vertex Position
// v3    - Vertex Normal
// v7    - Vertex Texture Data u,v
// v8    - Vertex Tangent (v direction)
// v5    - Wave Height Scale

struct VS_INPUT
{
        float4 Pos                 : POSITION;
        float4 Normal                : NORMAL;
        float3 Tex                    : TEXCOORD0;
        float3 Tan                : TANGENT;
        float3 Heightscale;       
};

struct VS_OUTPUT
{
        float4 position         : POSITION0;
        float3 uvCoords0         : TEXCOORD0;
        float3 uvCoords1         : TEXCOORD1;
        float4 viewVector;
        float3 binormal                : BINORMAL;
        float3 tangent                : TANGENT;
        float4 normal                : NORMAL;               
};

VS_OUTPUT Main(VS_INPUT input)
{
        float4 r0 = 0;
        float4 r1 = 0;
        float4 r2 = 0;
        float4 r3 = 0;
        float4 r4 = 0;
        float4 r5 = 0;
        float4 r6 = 0;
        float4 r7 = 0;
        float4 r8 = 0;
        float4 r9 = 0;
        float4 r10 = 0;
        float temp = 0;
       
        //Zero out our output
        VS_OUTPUT outVS = (VS_OUTPUT)0;

        // use tex coords as inputs to sinusoidal warp
        r0 = c14 * Tex.x;       
        // use tex coords as inputs to sinusoidal warp
        r0 = c15 * Tex.y + r0;       
        //time
        r1 = float4(1.0,1.0,1.0,1.0);
        r1 = r1 * c16.x;
        // add scaled time to move bumps according to frequency       
        r0 = r1 * c13 + r0;       
        // starting time offset
        r0 = r0 + c12;           
        // take frac of all 4 components
        r0 = r0 % 1;               
        // multiply by fixup factor (due to inaccuracy)
        r0 = r0 * c10.x;
        //subtract .5                 
        r0 = r0 - c0.y;
        //mult texcoords by 2pi  coords range from(-pi to pi)
        r0 = r0 * c1.w;               
                               
        //calculate the taylor series expansion of sine(r4) and cosine(r5)
        r5 = r0 * r0;                // (wave vec)^2
        r1 = r5 * r0;                // (wave vec)^3       
        r6 = r1 * r0;                // (wave vec)^4
        r2 = r6 * r0;                // (wave vec)^5
        r7 = r2 * r0;                // (wave vec)^6
        r3 = r7 * r0;                // (wave vec)^7
        r8 = r3 * r0;                // (wave vec)^8

        r4 = r1 * c2.y + r0;        //(wave vec) - ((wave vec)^3)/3!
        r4 = r2 * c2.z + r4;        //  + ((wave vec)^5)/5!
        r4 = r3 * c2.w + r4;        //  - ((wave vec)^7)/7!
       
        r0 = float4(1.0,1.0,1.0,1.0);
        r5 = r5 * c3.x + r0;        //-(wave vec)^2/2!
        r5 = r6 * c3.y + r5;        //-(wave vec)^4/4!
        r5 = r7 * c3.z + r5;        //-(wave vec)^6/6!
        r5 = r8 * c3.w + r5;        //-(wave vec)^8/8!
       
        r0 = c0.z - v5.x;        //... 1-wave scale
        r4 = r4 * r0;                // scale sin
        r5 = r5 * r0;                // scale cos
        r0 = dot(r4,c11);        //multiply wave heights by waves
       
       
        //multiply wave magnitude at this vertex by normal
        r0.xyz = Normal * r0;         
        //add to position
        r0.xyz = r0 + Pos;
        //homogenous component
        r0.w = c0.z;         
        // OutPos = ObjSpacePos * World-View-Projection Matrix
        outVS.position = mul(r0,WorldViewProjectionMatrix);

        //cos* waveheight
        r1 = r5 * c11;
        //normal x offset
        r9.x = dot(-r1,c14);
        //normal y offset and tangent offset
        r9.yzw = dot(-r1,c15);

        //starting normal
        r5 = Normal;
        //warped normal move nx, ny according to cos*wavedir*waveeheight
        r5.xy = r9 * c10.y + r5;

        //tangent
        r4 = float4(Tan,1.0);
        //warped tangent vector
        r4.z = -r9.x * c10.y + r4.z;

        r10 = r5;
        // transform normal
        r5 = mul(r10,c18);
        r10.x = dot(r5.xyz,r5.xyz);
        r10.y = rsqrt(r10.x);
        //normalize normal
        r5 = r5 * r10.y;         

        r10 = r4;
        // transform tangent
        r4 = mul(r10,c18);
        r10.x = dot(r4.xyz,r4.xyz);
        r10.y = rsqrt(r10.x);
        //normalize tangent
        r4 = r4 * r10.y;            

        //xprod to find binormal
        r3 = r4.yzxw * r5.zxyw;
        r3 = r4.zxyw * -r5.yzxw + r3;

        r10 = r0;
        // transform vertex position
        r0 = mul(r10,c18);

        //view vector
        r2 = c8 - r0;
        r10.x = dot(r2.xyz,r2.xyz);
        r10.y = rsqrt(r10.x);
        //normalized view vector
        r2 = r2 * r10.y;

        r0 = c16.x;
        r0 = r0 * c17.xyxy;
        //frc of incoming time
        r0.xy = r0.xy % 1;
        //add time to tex coords
        r0.x = Tex.x + r0.x;
        r0.y = Tex.y + r0.y;                  
        //distorted tex coord 0
        outVS.uvCoords0.x = r0.x;
        outVS.uvCoords0.y = r0.y;

        r0 = c16.x;
        r0 = r0 * c17.zwzw;
        //frc of incoming time
        r0.xy = r0.xy % 1;
        //add time to tex coords
        r0.x = Tex.x + r0.x;
        r0.y = Tex.y + r0.y;         
        //distorted tex coord 1
        outVS.uvCoords1.x = r0.x;
        outVS.uvCoords1.y = r0.y;

        //pass in view vector (worldspace)
        outVS.viewVector = r2;
        //binormal
        outVS.binormal = r3;
        //tangent
        outVS.tangent = r4;
        //normal
        outVS.normal = r5;
}

3

主题

11

帖子

11

积分

新手上路

Rank: 1

积分
11
发表于 2010-5-9 13:26:00 | 显示全部楼层

Re:DX 8&9 SHADER 问题

RenderMonkey
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-6-9 18:27

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表