Sign in to follow this  

asm shader to hlsl

This topic is 3321 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey guys, ive tryed to implement the FOGVolume example from nvidia. Now i got stuck when converting the shaders. They are all in asm and im using hlsl. So here are the shader i want to convert. //////////1 VERTEX SHADER
[Source]

#include "Constants.h"

#define V_POSITION  v0
#define V_NORMAL    v1
#define V_TEXTURE   v2

#define TEMP		r2

vs.1.1
dcl_position	V_POSITION
dcl_normal		V_NORMAL
dcl_texcoord	V_TEXTURE

// Transform position to clip space and output it
dp4 r1.x, V_POSITION, c[CV_WORLDVIEWPROJ_0]
dp4 r1.y, V_POSITION, c[CV_WORLDVIEWPROJ_1]
dp4 r1.z, V_POSITION, c[CV_WORLDVIEWPROJ_2]
dp4 r1.w, V_POSITION, c[CV_WORLDVIEWPROJ_3]

mov oPos, r1

// Set fog to 1.0 for no fog
mov oFog, c[CV_CONSTS_1].z

//--------------------------------------------
// Directional light + ambient
	// negative light vector for vector from vertex to light 
dp3		TEMP, -c[CV_DIRLIGHT1_DIR], V_NORMAL
	// clamp <0 to 0
max		TEMP, TEMP, c[CV_CONSTS_1].xxxx
	// mult by light color
mul		TEMP, TEMP, c[CV_DIRLIGHT1_COLOR]
	// Add ambient
add		oD0, TEMP, c[CV_OBJ_AMBIENT_COL]
//--------------------------------------------
// Compute texture coordinates for encoding pixel depth as an A8R8G8B8 value
// The generated texture coordinates are meant to address color ramp textures with
//  t1 coordinate addressing 2D ramp for red & green
//  t2 coordinate addressing 1D ramp for blue.
// red =	tex coord A .x = high bits of high precision depth
// green =	tex coord A .y = mid bits of high precision depth
// blue =	tex coord B    = low bits of high precision depth
//
// Create a 'normalized' value from the w depth so that it lies
//  in the range [0,1] from the near to the far clip plane.
// Also set r6.w = 1
mad r6, r1.w, c[CV_NORMALIZEWDEPTH].xxxz, c[CV_NORMALIZEWDEPTH].yyyw
mul r3, r6, c[CV_RAMPSCALE].xy
mov TEX_RGCRDO.xy, r3				// output texture coord for red-green color ramps

// Generate texture coordinate for the blue ramp to encode the lowest bits
// Blue should complete one full ramp for every bit increment of green.
mul r6.xyz, r6, c[CV_RAMPSCALE].zzzz

// Calculate y coordinate based on dither control constant to enable or disable dithering.
// The y=0 line of the blue texture has no dithering, so setting y coordinate to zero will disable the dithering

dp3 r6.y, r1, c[CV_DITHER_CONTROL]	// set CV_DITHER_CONTROL to 0,0,0,0 for no dither
mov TEX_BCRDO, r6					// output texture coord for blue color ramp

//-------------------------------------------
//  Pass through the ordinary texture coords
mov oT0, V_TEXTURE
mov oT1, V_TEXTURE

[/Source]
//////////2 PIXEL SHADER
[Source]

#include "Constants.h"

ps.2.0
def c0, 1.0, 1.0, 1.0, 1.0

dcl		v0.xyzw
dcl		t0.xy
dcl		t1.xyzw
dcl_2d		s0
dcl_2d		s1
dcl_2d		s2

TEX_RGDECL
TEX_BDECL

texld	r0, t0, s0		// base texture
mul     r0, r0, v0		// multiply by diffuse color
mov oC0, r0				// output to buffer containing ordinary shading

// Fetch color ramps for RGB-encoding of depth and output to 2nd color buffer
texld	r0, TEX_RGCRDI, TEX_RGSAMP
texld   r1, TEX_BCRDI,  TEX_BSAMP
add		r0, r0, r1			// add red+green+blue ramps

mov oC1, r0


[/Source]
heres my hlsl file
[Source]

struct Mtrl
{
	float4 ambient;
	float4 diffuse;
	float4 spec;
	float  specPower;
};

struct DirLight
{
	float4 ambient;
	float4 diffuse;
	float4 spec;
	float3 dirW;  
};

uniform extern float4x4 gWorldInv;
uniform extern float4x4 gWVP;
uniform extern Mtrl     gMtrl;
uniform extern DirLight gLight;
uniform extern float3   gEyePosW;
uniform extern texture  gTex;
uniform extern texture  gNormalMap;


uniform extern texture  RedGreen;
uniform extern texture  BlueRamp;

uniform extern float4	CV_DITHER_CONTROL		= {0,0,0,0};
uniform extern float4	c0						= {1,1,1,1};

uniform extern float4	CV_RGB_TEXADDR_WEIGHTS;
uniform extern float4	CV_HALF_TEXEL_SIZE;


uniform extern float4	CV_CONSTS_1;
uniform extern float4	CV_BITREDUCE;
uniform extern float4	CV_NORMALIZEWDEPTH;
uniform extern float4	CV_RAMPSCALE;


sampler RedGreenSampler = sampler_state
{
	Texture = <RedGreen>;
	MinFilter = POINT;
	MagFilter = POINT;
	MipFilter = NONE;
	AddressU  = WRAP;
    AddressV  = WRAP;
};

sampler BlueRampSampler = sampler_state
{
	Texture = <BlueRamp>;
	MinFilter = POINT;
	MagFilter = POINT;
	MipFilter = NONE;
	AddressU  = WRAP;
    AddressV  = WRAP;
};


sampler TexS = sampler_state
{
	Texture = <gTex>;
	MinFilter = ANISOTROPIC;
	MaxAnisotropy = 8;
	MagFilter = LINEAR;
	MipFilter = LINEAR;
	AddressU  = WRAP;
    AddressV  = WRAP;
};

sampler NormalMapS = sampler_state
{
	Texture = <gNormalMap>;
	MinFilter = ANISOTROPIC;
	MaxAnisotropy = 8;
	MagFilter = LINEAR;
	MipFilter = LINEAR;
	AddressU  = WRAP;
    AddressV  = WRAP;
};
 
struct OutputVS
{
    float4 posH      : POSITION0;
    float3 toEyeT    : TEXCOORD0;
    float3 lightDirT : TEXCOORD1;
    float2 tex0      : TEXCOORD2;
    float2 tex1		: TEXCOORD3;
      float2 tex2		: TEXCOORD4;
    
};

OutputVS NormalMapVS(float3 posL      : POSITION0, 
                     float3 tangentL  : TANGENT0,
                     float3 binormalL : BINORMAL0,
                     float3 normalL   : NORMAL0, 
                     float2 tex0      : TEXCOORD0)
{
    // Zero out  output.
	OutputVS outVS = (OutputVS)0;
	
	// Build TBN-basis.
	float3x3 TBN;
	TBN[0] = tangentL;
	TBN[1] = binormalL;
	TBN[2] = normalL;
	
	// Matrix transforms from object space to tangent space.
	float3x3 toTangentSpace = transpose(TBN);
	
	// Transform eye position to local space.
	float3 eyePosL = mul(float4(gEyePosW, 1.0f), gWorldInv);
	
	// Transform to-eye vector to tangent space.
	float3 toEyeL = eyePosL - posL;
	outVS.toEyeT = mul(toEyeL, toTangentSpace);
	
	// Transform light direction to tangent space.
	float3 lightDirL = mul(float4(gLight.dirW, 0.0f), gWorldInv).xyz;
	outVS.lightDirT  = mul(lightDirL, toTangentSpace);
	
	// Transform to homogeneous clip space.
	outVS.posH = mul(float4(posL, 1.0f), gWVP);
	
	// Pass on texture coordinates to be interpolated in rasterization.
	outVS.tex0 = tex0;
	
	


///---------------------------------------------------------------------------------
	//mad r6, r1.w, c[CV_NORMALIZEWDEPTH].xxxz, c[CV_NORMALIZEWDEPTH].yyyw
	//dest.x = src0.x * src1.x + src2.x;
	//dest.y = src0.y * src1.y + src2.y;
	//dest.z = src0.z * src1.z + src2.z;
	//dest.w = src0.w * src1.w + src2.w;

	float4 r6;
	r6 = outVS.posH.wwww * CV_NORMALIZEWDEPTH.xxxz + CV_NORMALIZEWDEPTH.yyyw;	
	
	//mul r3, r6, c[CV_RAMPSCALE].xy
	float2 r3;
	r3 = r6 * CV_RAMPSCALE.xy;
	
	//mov TEX_RGCRDO.xy, r3				// output texture coord for red-green color ramps
	outVS.tex1 = r3.xy;
///---------------------------------------------------------------------------------
	
	
	
	//mul r6.xyz, r6, c[CV_RAMPSCALE].zzzz
	r6.xyz = r6.xyz * CV_RAMPSCALE.zzz;
	
	
	//dp3 r6.y, r1, c[CV_DITHER_CONTROL]	// set CV_DITHER_CONTROL to 0,0,0,0 for no dither
	//dp3 dst, src0, src1 
	//dest.x = dest.y = dest.z = dest.w =  (src0.x * src1.x) + (src0.y * src1.y) + (src0.z * src1.z);
	
	r6.y = outVS.posH.x * CV_DITHER_CONTROL.x +  outVS.posH.y * CV_DITHER_CONTROL.y  + outVS.posH.z * CV_DITHER_CONTROL.z ;


	//mov TEX_BCRDO, r6	
	outVS.tex2 = r6.xy;
	
	
	
	
	// Done--return the output.
    return outVS;
}

float4 NormalMapPS(float3 toEyeT    : TEXCOORD0,
                   float3 lightDirT : TEXCOORD1,
                   float2 tex0      : TEXCOORD2,
                   float2 tex1		: TEXCOORD3,
					float2 tex2		: TEXCOORD4,
					out float4 SecondColor: COLOR1) : COLOR
{
	// Interpolated normals can become unnormal--so normalize.
	toEyeT    = normalize(toEyeT);
	lightDirT = normalize(lightDirT);
	
	// Light vector is opposite the direction of the light.
	float3 lightVecT = -lightDirT;
	
	// Sample normal map.
	float3 normalT = tex2D(NormalMapS, tex0);
	
	// Expand from [0, 1] compressed interval to true [-1, 1] interval.
    normalT = 2.0f*normalT - 1.0f;
    
    // Make it a unit vector.
	normalT = normalize(normalT);
	
	// Compute the reflection vector.
	float3 r = reflect(-lightVecT, normalT);
	
	// Determine how much (if any) specular light makes it into the eye.
	float t  = pow(max(dot(r, toEyeT), 0.0f), gMtrl.specPower);
	
	// Determine the diffuse light intensity that strikes the vertex.
	float s = max(dot(lightVecT, normalT), 0.0f);
	
	// If the diffuse light intensity is low, kill the specular lighting term.
	// It doesn't look right to add specular light when the surface receives 
	// little diffuse light.
	if(s <= 0.0f)
	     t = 0.0f;
	
	// Compute the ambient, diffuse and specular terms separatly. 
	float3 spec = t*(gMtrl.spec*gLight.spec).rgb;
	float3 diffuse = s*(gMtrl.diffuse*gLight.diffuse).rgb;
	float3 ambient = gMtrl.ambient*gLight.ambient;
	
	// Get the texture color.
	float4 texColor = tex2D(TexS, tex0);
	
	// Combine the color from lighting with the texture color.
	float3 color = (ambient + diffuse)*texColor.rgb + spec;
	
	
	
	//VS
	//mov oT0, V_TEXTURE
	//mov oT1, V_TEXTURE
	//mov TEX_BCRDO, r6					// output texture coord for blue color ramp						tex2
	//mov TEX_RGCRDO.xy, r3				// output texture coord for red-green color ramps				tex1
	
	
	// Fetch color ramps for RGB-encoding of depth and output to 2nd color buffer
	//texld		r0, TEX_RGCRDI, TEX_RGSAMP
	//texld		r1, TEX_BCRDI,  TEX_BSAMP
	//add		r0, r0, r1			// add red+green+blue ramps
	//mov oC1, r0
	
	float4 r0 = tex2D(RedGreenSampler, tex1);	
	float4 r1 = tex2D(BlueRampSampler, tex2);
	
	float4 finalColor = r0 + r1;

	SecondColor	= float4(finalColor);

	
	
	
	
	
	// Output the color and the alpha.
    return float4(color, gMtrl.diffuse.a*texColor.a);
}

technique NormalMapTech
{
    pass P0
    {
        // Specify the vertex and pixel shader associated with this pass.
        vertexShader = compile vs_2_0 NormalMapVS();
        pixelShader  = compile ps_2_0 NormalMapPS();
    }
}



since im not experied with asm commands i looked up every of them and implemented them in hlsl. THis can for sure be optimized but atm im just looking for the mistake. Ive set all constants into my FX File so this should not be the problem. here are the output screens: http://www.speedshare.org/download.php?id=41B77F202 one is the correct with the assembler shader, the other is the hlsl. It has do something with the encode stuff i just dont use the asm shader because i want to use another lighting technique. Thank you for any help

Share this post


Link to post
Share on other sites

This topic is 3321 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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