Converting shaders to Torque format
by Bil Simser · in Torque Game Engine Advanced · 07/05/2004 (9:21 pm) · 17 replies
I read somewhere awhile ago that you could drop in shaders from say RenderMonkey (or maybe DX9) and run them as-is in Torque. I'm having some problems understanding and getting these to work so maybe someone can shed some light on this.
I have this shader exported from a RenderMonkey sample (Plastic) in fx format:
So for things like view_proj_matrix and view_position variables, I passed them into the vertex shader like this:
(RenderMonkey has them registered as C0 and C4 but I didn't think VC_TEX_TRANS1 was right?)
There's also the color varible for the pixel shader that I passed in as register(C0) since that's what the ATI shader had it set as.
Anyways, it compiles with no errors but doesn't show up when I swap a material out for them (I replaced the OrcSkin mapping with my material).
What brain dead thing am I doing wrong here? I notice all the TSE shaders use their own conventions for names like the VS_OUTPUT but that's just names right? Or does it make a difference?
I have this shader exported from a RenderMonkey sample (Plastic) in fx format:
//**************************************************************//
// Effect File exported by RenderMonkey
//
// - For this version, exporting to FX file is not perfect.
// There are many informations doesn't get exported.
// Currently, exporter does not try to resolve naming conflicts
// ( functions, variables etc )
// Please fix these issues before using this Effect file
//**************************************************************//
float4 view_position : CameraPosition = { -185.552780, -43.722942, -60.485661, 1.000000 };
float4x4 view_proj_matrix : ViewProjection;
//--------------------------------------------------------------//
// Plastic
//--------------------------------------------------------------//
float4 color = { 1.000000, 0.300752, 0.000000, 1.000000 };
//--------------------------------------------------------------//
// Single Pass
//--------------------------------------------------------------//
struct VS_OUTPUT {
float4 Pos: POSITION;
float3 normal: TEXCOORD0;
float3 viewVec: TEXCOORD1;
};
//
// Vertex shader
//
VS_OUTPUT main(float4 Pos: POSITION, float3 normal: NORMAL){
VS_OUTPUT Out;
Out.Pos = mul(view_proj_matrix, Pos);
// World-space lighting
Out.normal = normal;
Out.viewVec = view_position - Pos;
return Out;
}
//
// Pixel shader
//
float4 main(float3 normal: TEXCOORD0, float3 viewVec: TEXCOORD1) : COLOR {
// Simple lighting lighting model for a dull plastic appearance.
float v = 0.5 * (1 + dot(normalize(viewVec), normal));
return v * color;
}So for things like view_proj_matrix and view_position variables, I passed them into the vertex shader like this:
VS_OUTPUT main(float4 Pos: POSITION, float3 normal: NORMAL,
uniform float4x4 view_proj_matrix : register(VC_WORLD_PROJ),
uniform float4x4 view_position : register(VC_EYE_POS)
)(RenderMonkey has them registered as C0 and C4 but I didn't think VC_TEX_TRANS1 was right?)
There's also the color varible for the pixel shader that I passed in as register(C0) since that's what the ATI shader had it set as.
Anyways, it compiles with no errors but doesn't show up when I swap a material out for them (I replaced the OrcSkin mapping with my material).
What brain dead thing am I doing wrong here? I notice all the TSE shaders use their own conventions for names like the VS_OUTPUT but that's just names right? Or does it make a difference?
#2
07/08/2004 (4:03 am)
@Anthony: Putting these variables in the VS_OUTPUT structure doesn't jive with what's in the hlsl files in TSE. Just look at one of the vertex shaders and it passes in the matrices as parameters to main();
#3
do this
note the VC_EYE_POS is a vector not a matrix
07/08/2004 (7:10 am)
Opps ,my bad, I thought you wanted to pass them to the pixel shader in that casedo this
VS_OUTPUT main( Appdata In, uniform float4x4 modelview : register(VC_WORLD_PROJ), uniform float3 eyePos : register(VC_EYE_POS))
note the VC_EYE_POS is a vector not a matrix
#4
07/08/2004 (7:17 am)
@Anthony: The problem is that I *am* passing them into the pixel and vertex shader (where appropriate). However it's not working which doesn't make sense.
#5
in the Material::setShaderConstants function
for instance
those here is how VC_EYE_POS is defined
07/08/2004 (7:18 am)
One other thing these constants are defined in material.cppin the Material::setShaderConstants function
for instance
those here is how VC_EYE_POS is defined
//------------------------- Point3F eyePos = sgData.camPos; MatrixF objTrans = sgData.objTrans; //other code removed objTrans.inverse(); objTrans.mulP( eyePos ); GFX->setVertexShaderConstF( VC_EYE_POS, (float*)&eyePos, 1 ); //other code removed
#6
07/08/2004 (7:27 am)
I guess then your eye vector is wrong try this float3 eyeVec = normalize( eyePos - Pos);then pass that
#7
Given the shader code first posted, could someone show me what the Torque version of this should look like?
Thanks.
07/08/2004 (7:53 am)
@Anthony: I'm not following you with your normalize call to calculate eyeVec (which I don't use anyways) so I'm a little lost here and maybe I'm not asking the right question.Given the shader code first posted, could someone show me what the Torque version of this should look like?
Thanks.
#8
I think this should work of course the color could be a constant from the engine instead of setting it in the shader.
edited
07/08/2004 (9:07 am)
#define IN_HLSL
#include "shdrConsts.h"
//// Vertex shader
struct VS_INPUT {
float4 Pos: POSITION;
float3 normal: NORMAL;
};
struct VS_OUTPUT {
float4 Pos: POSITION;
float3 normal: TEXCOORD0;
float3 viewVec: TEXCOORD1;
};
VS_OUTPUT main(VS_INPUT IN,
uniform float4x4 modelview : register(VC_WORLD_PROJ),
uniform float3 eyePos : register(VC_EYE_POS)
){
VS_OUTPUT Out;
Out.Pos = mul(modelview, IN.Pos);
// World-space lighting
Out.normal = IN.normal;
Out.viewVec = normalize( eyePos - IN.Pos);
return Out;
}and//// Pixel shader
struct PS_INPUT {
float4 Pos: POSITION;
float3 normal: TEXCOORD0;
float3 viewVec: TEXCOORD1;
};
struct PS_OUTPUT {
float4 col: COLOR;
};
PS_OUTPUT main(PS_INPUT IN){
// Simple lighting lighting model for a dull plastic appearance.
PS_OUTPUT Out;
float v = 0.5 * (1 + dot(normalize(IN.viewVec), IN.normal));
float4 color = { 1.000000, 0.300752, 0.000000, 1.000000 };
Out.col = v * color;
return Out;
}I think this should work of course the color could be a constant from the engine instead of setting it in the shader.
edited
#9
plasticShaderP.hlsl(13): warning X4707: texcoord inputs used directly (that is, other than sampling from textures) in shader body in ps_1_1 are always clamped from 0 to 1
plasticShaderP.hlsl(13): error X4532: cannot map expression to pixel shader instruction set
07/08/2004 (9:29 am)
That compiles for me but will do nothing because I have 1.1 shader my errors and warnings are plasticShaderP.hlsl(13): warning X4707: texcoord inputs used directly (that is, other than sampling from textures) in shader body in ps_1_1 are always clamped from 0 to 1
plasticShaderP.hlsl(13): error X4532: cannot map expression to pixel shader instruction set
#10
Edit: Finally got it working. Silly me. I was returning a COLOR type when it should have been COLOR0. Have some more testing to but will write up a detailed resource on converting shaders from nVidia (FxComposer) and ATI (RenderMonkey) and the fx format to Torque now that I have that figured out. Will probably write a small fx->hlsl converter as it's all academic now. Thanks for your help Anthony.
07/08/2004 (11:35 am)
I'll give it a try when I get home. Yeah, the pixel shader is 2.0 because of the clamping. Thanks.Edit: Finally got it working. Silly me. I was returning a COLOR type when it should have been COLOR0. Have some more testing to but will write up a detailed resource on converting shaders from nVidia (FxComposer) and ATI (RenderMonkey) and the fx format to Torque now that I have that figured out. Will probably write a small fx->hlsl converter as it's all academic now. Thanks for your help Anthony.
#11
07/08/2004 (8:36 pm)
Sweet.
#12
Was wondering, anybody look into some tutorial to get FX shaders right into TSE? I was flipping around some examples of RenderMonkey, and they are simply amazing. I've never worked with shaders or anything before though, so i'm sorta lost on getting it into TSE, all i know is i want to.. ;) Anyone happen to come up with a good guide to fit em in?
10/01/2004 (6:19 pm)
I hate bumping threads, but this WAS in plain site anyhow.. ;)Was wondering, anybody look into some tutorial to get FX shaders right into TSE? I was flipping around some examples of RenderMonkey, and they are simply amazing. I've never worked with shaders or anything before though, so i'm sorta lost on getting it into TSE, all i know is i want to.. ;) Anyone happen to come up with a good guide to fit em in?
#13
10/02/2004 (6:32 am)
Yeah, Im also curious about this.
#14
10/02/2004 (6:41 am)
It really isn't hard at all. RenderMonkey uses HLSL so all you need to do is set up all the constants in Torque. If you come to IGC I can show you.
#15
10/02/2004 (8:39 am)
Hm, what do you mean IGC?
#16
Woah is it free?
www.ati.com/developer/rendermonkey/downloads.html
Seems too good to be true for a nifty looking tool.
10/30/2004 (4:24 pm)
Uh, question. I can't find a price for RenderMonkey.Woah is it free?
www.ati.com/developer/rendermonkey/downloads.html
Seems too good to be true for a nifty looking tool.
#17
10/30/2004 (5:45 pm)
Heh, ya its free alright.. Least, from what i've seen.. And i beleive its allowed for commercial products as well.. Awesome deal i think. ;)
Associate Anthony Rosenbaum
VS_OUTPUT { float4x4 view_proj_matrix : TEXCOORD1, float4x4 view_position : TEXCOORD2 }However when I wrote my lighting shader because it was designed for 1.1 shaders. I did all the calulation in the vertex, which gave great results.