asm => hlsl..... projection shader

Posted By: zSteam

asm => hlsl..... projection shader - 10/19/07 01:38

hello

I'm trying to build a texture projection demo. but the shader is in asm and i need it in hsl to increase the result. can me anyone explain/convert the shader?
I think the projection shader is very important for many 3dgs users.

Code:

texture mtlSkin1; //projector
texture entSkin1; //colormap
matrix matWorldViewProj;
matrix matWorld;
matrix matMtl;

technique projector
{

pass p0 //projector
{

//load matrices
//VertexShaderConstant[0] = <matWorld>; //World Matrix
VertexShaderConstant[8] = <matWorldViewProj>;
VertexShaderConstant[95] = <matMtl>;

//range constant
VertexShaderConstant[33] = {0.01f, 0.5f, 0.05f, 0.0f};

Texture[0] = <mtlSkin1>; //projection tex
Texture[1] = <entSkin1>; //projection tex

AddressU[0] = Clamp; // don't wrap around edges
AddressV[0] = Clamp;
AddressU[1] = Clamp; // don't wrap around edges
AddressV[1] = Clamp;

zWriteEnable=true; // enables writing to the z-buffer

VertexShader=

asm
{
vs_1_1

dcl_position v0
dcl_normal v3
dcl_texcoord0 v7
dcl_texcoord1 v8

; position in clip space
m4x4 oPos, v0, c8

; position in texture projection space
m4x4 r10,v0,c95

; Divide each component by the range value
mul r10, r10, c33.x

; multiply with 0.5 and add 0.5
mad r10, r10, c33.yyyy, c33.yyyy

; map the x and y components into the first texture
mov oT0.xy, r10.xy

mov oT1.xy, v7.xy; color

};

PixelShader=
asm
{

ps_1_1

tex t0
tex t1

mov r0,t0
mul r0,r0,t1
};
}

}


Posted By: zSteam

Re: asm => hlsl..... projection shader - 10/19/07 13:32

I tried to convert the shader to HLSL but it doesn't work.... the engine starts and close without a message. => crash?

what's wrong?

here is my prototype shader:

Code:

//--------------------------------------------------------------
// Texture Projection Shader
//
// by zSteam
// -------------------------------------------------------------

float4x4 matWorldViewProj;
float4x4 matMtl;

float4 range = {0.01f, 0.5f, 0.05f, 0.0f};
float tps;

struct VS_OUTPUT
{
float4 oPos : POSITION;
float2 Tex0 : TEXCOORD0;
float2 Tex1 : TEXCOORD1;
};

VS_OUTPUT mainVS( float4 oPos : POSITION, float2 Tex0 : TEXCOORD0, float2 Tex1 : TEXCOORD1 )
{
VS_OUTPUT Out = (VS_OUTPUT) 0;

Out.oPos = mul(oPos, matWorldViewProj);

tps = mul(oPos, matMtl);
tps = tps * range.x;
tps = (tps * range.yyyy) + range.yyyy;

Out.Tex0.xy = tps.xy;
Out.Tex1.xy = Tex0.xy;

return Out;
}

texture entSkin1;
texture mtlSkin1;

sampler ColorMapSampler = sampler_state
{
Texture = <entSkin1>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = Clamp; // don't wrap around edges
AddressV = Clamp; // don't wrap around edges
};

sampler ProjTex = sampler_state
{
Texture = <mtlSkin1>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = Clamp; // don't wrap around edges
AddressV = Clamp; // don't wrap around edges
};

float4 mainPS(float2 Tex : TEXCOORD0) : COLOR
{
Color = ColorMapSampler * ProjTex;
return Color;
}

technique projector
{
pass p0
{
zWriteEnable=true;
VertexShader = compile vs_1_1 mainVS();
PixelShader = compile ps_1_1 mainPS();
}
}


Posted By: bstudio

Re: asm => hlsl..... projection shader - 10/19/07 14:31

You named the inputs the same as the outputs, change that.
Posted By: zSteam

Re: asm => hlsl..... projection shader - 10/19/07 14:44

ok ty i've changed the in-/outputs but i've the same problem

my currently code

Code:

...
struct VS_OUTPUT
{
float4 oPos : POSITION;
float2 oTex0 : TEXCOORD0;
float2 oTex1 : TEXCOORD1;
};

VS_OUTPUT mainVS( float4 Pos : POSITION, float2 Tex : TEXCOORD0 )
{
VS_OUTPUT Out = (VS_OUTPUT) 0;

Out.oPos = mul(Pos, matWorldViewProj);

tps = mul(Pos, matMtl);
tps = tps * range.x;
tps = (tps * range.yyyy) + range.yyyy;

Out.oTex0.xy = tps.xy;
Out.oTex1.xy = Tex.xy;

return Out;
}

...

float4 mainPS(float4 Color: COLOR) : COLOR
{
Color = ColorMapSampler * ProjTex;
return Color;
}
...


Posted By: xXxGuitar511

Re: asm => hlsl..... projection shader - 10/19/07 14:51

"Color = ColorMapSampler * ProjTex;"

..YOu can't retrieve a texture like this. You need to use a texture lookup [tex2D(sampler, float2 coords);]



Also many undefined variables, such as "tps, Colorrange"
Posted By: zSteam

Re: asm => hlsl..... projection shader - 10/19/07 15:07

hey it works! thx man => this is my first own programmed hlsl shader^^
Posted By: zSteam

Re: asm => hlsl..... projection shader - 10/19/07 16:13

OK It works, but I have a problem ... The projected image is distorted on the edges.

How can I solve this problem?

screeny:


my code:

Code:

//--------------------------------------------------------------
// Texture Projection Shader HLSL
//
// © by zSteam
//
// date: 19.10.2007
// -------------------------------------------------------------

float4x4 matWorldViewProj;
float4x4 matMtl;

struct VS_OUTPUT
{
float4 oPos : POSITION;
float2 oTex0 : TEXCOORD0;
float2 oTex1 : TEXCOORD1;
};

VS_OUTPUT mainVS( float4 Pos : POSITION, float2 Tex : TEXCOORD0 )
{
float2 tps;

VS_OUTPUT Out = (VS_OUTPUT) 0;

Out.oPos = mul(Pos, matWorldViewProj);

tps = mul(Pos, matMtl) * 0.01f;
tps = (tps * 0.5f) + 0.5f;

Out.oTex0.xy = tps.xy;
Out.oTex1.xy = Tex.xy;

return Out;
}

texture entSkin1;
texture mtlSkin1;

sampler ColorMapSampler = sampler_state
{
Texture = <entSkin1>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};

sampler ProjTex = sampler_state
{
Texture = <mtlSkin1>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = Clamp; // don't wrap around edges
AddressV = Clamp; // don't wrap around edges
};

float4 mainPS(float2 Tex0 : TEXCOORD0, float2 Tex1 : TEXCOORD1 ) : COLOR
{
float4 base = tex2D(ColorMapSampler, Tex1);
float4 proj = tex2D(ProjTex, Tex0)*4;

return (base*proj);
}

technique projector
{
pass p1
{
VertexShader = compile vs_1_1 mainVS();
PixelShader = compile ps_1_1 mainPS();
}
}

technique fallback { pass one { } }


Posted By: ello

Re: asm => hlsl..... projection shader - 10/19/07 17:57

Quote:

You named the inputs the same as the outputs, change that.




as far as i know this causes no problem. i never found problems with this
Posted By: Poison

Re: asm => hlsl..... projection shader - 10/19/07 18:02

I think it is a problem with the graphics card!
Wich Card do you have??

Cheers

Poison Byte

PS: Sometimes I have the same Problem
Posted By: zSteam

Re: asm => hlsl..... projection shader - 10/19/07 18:26

hi .. it's not my graficcard (ati radeon 1800xt => shader 3.0)

if i change AddressU/AddressV to wrap, the prjected picture shows tiling.

but i only need one tile (no other tiling)



Posted By: Steempipe

Re: asm => hlsl..... projection shader - 10/19/07 18:27

I forget the different states, but look at these:

AddressU = Clamp; // don't wrap around edges
AddressV = Clamp; // don't wrap around edges


Edit: which you just did! Cool.


As for the tiling, you will have to revisit your coordinates in the lines:
tps = mul(Pos, matMtl) * 0.01f;
tps = (tps * 0.5f) + 0.5f;

I have not done anything similar in a year or so, so I am not sure what formulas you should try.

Posted By: zSteam

Re: asm => hlsl..... projection shader - 10/19/07 18:56

ok thanks .. it works....

but now my next problem:

the projected picture must be smaller in the near than in the far distance like a flashlight.
how can i solve this problem?

screenshot:


the code:
Code:
 

//--------------------------------------------------------------
// Texture Projection Shader HLSL
//
// © by zSteam
//
// date: 19.10.2007
// -------------------------------------------------------------

float4x4 matWorldViewProj;
float4x4 matMtl;

struct VS_OUTPUT
{
float4 Pos : POSITION;
float2 texCoord : TEXCOORD0;
float3 lightVec : TEXCOORD1;
};

VS_OUTPUT mainVS( float4 Pos : POSITION, float2 texCoord : TEXCOORD0 )
{
VS_OUTPUT Out = (VS_OUTPUT) 0;

Out.Pos = mul(Pos, matWorldViewProj);

float2 tps = (mul(Pos, matMtl) * 0.005f) + 0.5f; // ? und korrektur der position des projizierten bildes

Out.texCoord = tps;
Out.lightVec.xy = texCoord.xy;

return Out;
}

texture entSkin1;
texture mtlSkin1;

sampler BaseTex = sampler_state
{
Texture = <entSkin1>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = wrap;
AddressV = wrap;
};

sampler ProjTex = sampler_state
{
Texture = <mtlSkin1>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = clamp;
AddressV = clamp;
};

float4 mainPS(float2 texCoord : TEXCOORD0, float2 lightVec : TEXCOORD1 ) : COLOR
{
float4 base = tex2D( BaseTex, lightVec );
float4 proj = tex2D( ProjTex, texCoord ) * 4;

return (base + 0.1 * proj);
}

technique projector
{
pass p1
{
VertexShader = compile vs_1_1 mainVS();
PixelShader = compile ps_1_1 mainPS();
}
}

technique fallback { pass one { } }


Posted By: Joey

Re: asm => hlsl..... projection shader - 10/21/07 14:19

your projection matrix is orthographic, not perspective.
Posted By: zSteam

Re: asm => hlsl..... projection shader - 10/21/07 16:08

hi

i'm a shader noob...
do you mean float4x4 matWorldViewProj;?
what must i change to improve the shader?

here is a compiled demo:
Texture Projection Shader.exe
Posted By: Emre

Re: asm => hlsl..... projection shader - 10/23/07 15:26


Posted By: Poison

Re: asm => hlsl..... projection shader - 10/23/07 15:28

The file has no virus in it, I have checked it with my anti-virus software.
think that your software has got a failure.
Posted By: Captain_Kiyaku

Re: asm => hlsl..... projection shader - 10/24/07 06:37

Small Sidenote: If it's packed with Nacasi, some Virus Scanner will detect it as Trojan but it's not.
© 2024 lite-C Forums