*** Updated 7/1/2005 ***
See Revision 4 Notes for changes at end of FAQ
We are putting together a shader FAQ. I will update the FAQ as new replies are added. Please post any questions or Questions and answers for common items you have found.
------------------------------------------------------------------------------
What Types of Shaders are Supported?FX files which would include HLSL, and ASM code including a Technique encapsulating the PS and VS functions are supported.
------------------------------------------------------------------------------
Where can i learn more about HLSL Language?Microsoft has an introductory HLSL reference that I found quite good to learn from:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnhlsl/html/shaderx2_introductionto.asphttp://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/directx9_c/directx/graphics/reference/Shaders/HighLevelShaderLanguage.asphttp://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnhlsl/html/shaderx2_introductionto.asphttp://www.fusionindustries.com/content/lore/code/articles/fi-faq-cg.php3http://www.imm.dtu.dk/visiondag/VD03/grafisk/WolfgangEngel.pdfhttp://www.shaderx.com/direct3d.net/tutorials/RenderMonkey%20Movies/1Steps/RenderMonkey.htm------------------------------------------------------------------------------
What are some good books about shaders?Real-Time Shader Programming Covering Directx 9.0 By Ron Fosner
Published by Morgan Kaufmann
ISBN 1-55860-853-2
The book describes low level shader programming, which isn't as much of as a headache as the math involved. I'm finding its giving me a very good base understanding of shaders and a good spring board from which to later pickup HLSL.
A really nice touch is that every page is in colour. unusual for this type of book.
------------------------------------------------------------------------------
Where can i find example Shaders?Microsoft includes a number of examples with the Directx SDK. A nice tutorial site can be found here with several examples:
http://www.neatware.com/lbstudio/web/hlsl.html------------------------------------------------------------------------------
What are some programs that can be used to edit shaders?Rendermonkey from ATINVidia FX Composer RTZen - RTShaderSoul Shader (see bottom of FAQ for more info)
------------------------------------------------------------------------------
What is an example of a HLSL Shader?// View Matrix Projection
float4x4 matViewProjection : ViewProjection;
//Format for Output for Vertex Shader
struct VS_OUTPUT
{
float4 Pos: POSITION;
};
// Vertex Shader
VS_OUTPUT vs_main( float4 inPos: POSITION )
{
VS_OUTPUT Out;
Out.Pos = mul(inPos, matViewProjection);
return Out;
}
// Pixel Shader
float4 ps_main( float4 inDiffuse: COLOR0 ) : COLOR0
{
// Output constant color:
float4 color;
color[0] = color[3] = 1.0;
color[1] = color[2] = 0.0;
return color;
}
technique Effect1
{
pass Pass_0
{
VertexShader = compile vs_1_1 vs_main();
PixelShader = compile ps_1_1 ps_main();
}
}
------------------------------------------------------------------------------
Is there an example of a full screen shader?Thanks to Sylvain, yes there is:
http://www.shelter7.com/FullscreenShader.fx------------------------------------------------------------------------------
How do I Apply a Shader FX file to a TVMesh?Declare your Variables and Create them
Delphi:
Mesh: TVMesh;
Shader: TVShader;
Mesh := scene.CreateMeshBuilder();
Shader := scene.CreateShader();
VB:
Dim Mesh as TVMesh;
Dim Shader as TVShader;
Set Mesh = scene.CreateMeshBuilder()
Set Shader = scene.CreateShader()
Setup your Mesh:
Mesh.LoadTVM(FileName, true, true)
Setup Your Shader:
if not Shader.CreateFromEffectFile(FXFile) then
ErrorMessage := Shader.GetLastError;
Finally apply the shader to the mesh:
Mesh.SetShader(Shader);
------------------------------------------------------------------------------
How do I assign a shader to a specific group in a TVMesh?The Shader can be assigned with SetShaderEx to a specific group. If one group has a shader though, the rest of the models groups must also have a shader or they will not be visible.
------------------------------------------------------------------------------
My shader has multiple vertex/pixel shaders in the same file (aka techniques). How can I choose which one i want to apply on my mesh ? Use TVShader.SetTechniqueByID or TVShader.SetTechnique.
------------------------------------------------------------------------------
I want to have multiple versions of a shader ( 2.0, 1.1) How do i setup my shader to automatically select the latest version the card supports? When setting up techniques such as the following:
technique Effect1
{
pass Pass_0
{
VertexShader = compile vs_2_0 vs_main();
PixelShader = compile ps_2_0 ps_main();
}
}
technique Effect1b
{
pass Pass_0
{
VertexShader = compile vs_1_1 vs_main();
PixelShader = compile ps_1_1 ps_main();
}
}
The first technique Effect1 will be used. If the card does not support 2.0 PS or 2.0 VS then the next technique in the file will be used. In this case Effect1b would be used if only 1.1 PS and VS are available on the card.
if you have several techniques and you set the technique it will start from that technique and work to lower in the file from the selected point.
------------------------------------------------------------------------------
How can I pass parameters to my shader?Try to locate the parameters in your shader.
Determine if their value is assigned by TV (See TV Semantics reference).
If not u should set them up yourself in your code by using functions like:
TVShader.SetEffectParamMatrix
TVShader.SetEffectParamVector
TVShader.SetEffectParamBoolean
TVShader.SetEffectParamFloat
TVShader.SetEffectParamTexture
------------------------------------------------------------------------------
I tried to Load a .FX file that contains a texture but it will not load (Mesh does not show up)A Texture can be loaded as follows:
texture Noise_Tex
<
string filename = "C:\Textures\Texture.dds";
>;
The problem is the "\" is a escape character in C++ and confuses the loader. You will want to pass in "\\" and this should take care of the problem. So the above becomes:
texture Noise_Tex
<
string filename = "C:\\Textures\\Texture.dds";
>;
------------------------------------------------------------------------------
My meshes are completely white when i apply a shader on them. Verify if your shader use one of those semantics: AMBIENT, DIFFUSE, EMISSIVE, SPECULARPOWER TV will fill those parameters with your mesh material values.
You should apply a correct material on the mesh or remove the semantic from the shader file.
------------------------------------------------------------------------------
Rendermonkey is producing FX files that do not workThere are a number of problems with RenderMonkey's output.
Non-escaped characters for textures "\" should be "\\" for texture paths.
The vectors are output with parentesis insted of curley braces "(" should be "{"
Out.Pos = mul(matViewProjection, isPos); is reversed and should be Out.Pos = mul(inPos, matViewProjection);
As more incompatabilities are found they will be added to this list.
------------------------------------------------------------------------------
TV Semantics Reference TV will automaticaly set a few parameters value in your shaders, here is a list:
Description TV equivalent DataType Semantics
World matrix Mesh.GetMatrix float4x4 "WORLD", "WORLDMATRIX", "MWORLD", "MATWORLD"
View matrix Inverse Camera.GetMatrix float4x4 "VIEW", "VIEWMATRIX"
Projection matrix Camera.GetProjectionMatrix float4x4 "PROJ", "PROJECTION", "PROJECTIONMATRIX", "PROJMATRIX"
World-view matrix float4x4 "WORLDVIEW", "WORLDVIEWMATRIX", "WV", "WVIEWMATRIX"
World-view-projection matrix float4x4 "WORLDVIEWPROJ", "WVP", "WORLDVIEWPROJECTION", "WORLDVIEWPROJECTIONMATRIX"
View-projection matrix float4x4 "VIEWPROJ", "VIEWPROJECTION", "VIEWPROJMATRIX", "VIEWPROJECTIONMATRIX"
Inverse view matrix Camera.GetMatrix float4x4 "VIEWI", "VIEWINVERSE", "VIEWINVERSEMATRIX", "VI"
Transposed, inverse world matrix float4x4 "WORLDIT", "WORLDINVERSETRANSPOSE", "WORLDTRANSPOSEINVERSE", "WIT"
Transposed, inverse view matrix float4x4 "VIEWIT", "VIEWINVERSETRANSPOSE", "VIEWTRANSPOSEINVERSE", "VIT"
Camera (Eye) position Camera.GetPosition float3 "CAMERAPOS", "CAMERAPOSITION", "VIEWPOS", "VIEWPOSITION"
Simulation time TV.TickCount float "TIME", "TIMECOUNT", "TICKCOUNT", "SECONDS"
Directional Light Direction float3 "LIGHTDIR0_DIRECTION"
Point Light Positionfloat3 "LIGHTPOINT0_POSITION"
Directional Light Color float "LIGHTDIR0_COLOR"
Point Light Color float "LIGHTPOINT0_COLOR"
you can have 5 simulaneous point/directional lights. (replacing 0 with a number between 0 and 4 will do)
Lights 0-4 are available.
------------------------------------------------------------------------------
How do you apply HLSL Shaders to 3ds Max?Read this nice tutorial for a good step by step.
http://www.monitorstudios.com/bcloward/tutorials_shaders1.html------------------------------------------------------------------------------
Is there an editor that lets you use TV to edit shadersSoul Shader is available. Please let me know of any problems.
Soul ShaderYou must download the ATL/COM dll and register it for this to work. If it no longer works
its likely the dll interface has changed. Let me know and I will recompile it. I will always try to keep everything up to date as possible.
What version is this document?Revision 4
*Changes and Additions
- Soul Shader Link Update
- Spelling Fix
- Added HLSL 3ds Max Tutorial