DirectX 11 RT Shader - Render to Target
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi,
I've been hitting a wall for 2 days trying to render to different render target using FX hlsl shaders in 3DS MAX...
I've come up to this simple 3 passes shader that 1) render the scene in green in a target, 2) render the same scene in red in another target, 3) combine the information of both in a last pass...
This basically gives:
Pass 1 Pass 2 Expected Pass 3
The problem is that I get the following result, which seems to show that results of the different passes are not rendered in the RenderTarget textures :S
I think I have kind of pinned down the problem to be that 3DS Max does not execute the parts defined in the string Script, such as the following block of code. I think this is the case because ClearColor is always white for instance, even if I set it to red...
string Script = "RenderColorTarget1=TargetTextureGreen;" "ClearSetColor=ClearColor;" "ClearSetDepth=ClearDepth;" "Clear=Color;" "Clear=Depth;" "Draw=geometry;";
I've also created a second shader to see what happens if I do not draw a specific object (the floor plane) in the last pass, and I end up seeing the green pixels underneath, which means that the Color buffer was not cleared...
I paste the full code of the shader below in case someone has any idea :S
Cheers,
string ParamID = "0x0003";
float4 ClearColor : DIFFUSE = {1,0,0,1.0};
float ClearDepth = 1.0;
float NearClip = 0.01f;
float FarClip = 250.0f;
//// UN-TWEAKABLES - AUTOMATICALLY-TRACKED TRANSFORMS ////////////////
float4x4 WvpXf : WorldViewProjection < string UIWidget="None"; >;
float4x4 WorldXf : World < string UIWidget="None"; >;
float4x4 ViewXf : View < string UIWidget="None"; >;
float4x4 ProjXf : Projection < string UIWidget="None"; >;
#ifdef _MAX_
int texcoord1 : Texcoord
<
int Texcoord = 1;
int MapChannel = 0;
string UIWidget = "None";
>;
int texcoord2 : Texcoord
<
int Texcoord = 2;
int MapChannel = -2;
string UIWidget = "None";
>;
int texcoord3 : Texcoord
<
int Texcoord = 3;
int MapChannel = -1;
string UIWidget = "None";
>;
#endif
bool g_draw <
string UIName = "Draw for mix technique";
> = true;
//// TWEAKABLE PARAMETERS ////////////////////
Texture2D TargetTextureRed : RENDERCOLORTARGET
<
float2 ViewportRatio = { 1.0, 1.0 };
>;
Texture2D TargetTextureGreen : RENDERCOLORTARGET
<
float2 ViewportRatio = { 1.0, 1.0 };
>;
SamplerState g_TargetTextureRedSampler
{
Filter = MIN_MAG_MIP_LINEAR;
AddressU = Clamp;
AddressV = Clamp;
};
SamplerState g_TargetTextureGreenSampler
{
Filter = MIN_MAG_MIP_LINEAR;
AddressU = Clamp;
AddressV = Clamp;
};
/* data from application vertex buffer */
struct appdata {
float4 Position : POSITION;
float3 Normal : NORMAL;
float3 Tangent : TANGENT;
float3 Binormal : BINORMAL;
float2 UV0 : TEXCOORD0;
};
/* data passed from vertex shader to pixel shader */
struct vertexOutput {
float4 Position : SV_Position;
float2 UV0 : TEXCOORD0;
};
///////// VERTEX SHADING /////////////////////
/*********** Generic Vertex Shader ******/
vertexOutput std_VS(appdata IN) {
vertexOutput OUT = (vertexOutput)0;
float4 Po = float4(IN.Position.xyz,1);
float4 Pw = mul(Po,WorldXf);
float4 Pv = mul(Pw,ViewXf);
float4 Pj = mul(Pv,ProjXf);
float4x4 projCorrected = ProjXf;
projCorrected[2].z = (FarClip + NearClip)/(NearClip- FarClip);
projCorrected[3].z = (2 * FarClip * NearClip)/(NearClip - FarClip);
Pj = mul(Pv,projCorrected);
OUT.Position = Pj;
OUT.UV0 = IN.UV0;
return OUT;
}
vertexOutput mix_VS(appdata IN) {
vertexOutput OUT = (vertexOutput)0;
float4 Po = float4(IN.Position.xyz,1);
float4 Pw = mul(Po,WorldXf);
float4 Pv = mul(Pw,ViewXf);
float4 Pj = mul(Pv,ProjXf);
float4x4 projCorrected = ProjXf;
projCorrected[2].z = (FarClip + NearClip)/(NearClip- FarClip);
projCorrected[3].z = (2 * FarClip * NearClip)/(NearClip - FarClip);
Pj = mul(Pv,projCorrected);
OUT.Position = Pj;
OUT.UV0 = IN.UV0;
if(!g_draw)
{
OUT.Position.z = 10 * OUT.Position.w; // Outside cliping volume
}
return OUT;
}
///////// PIXEL SHADING //////////////////////
float4 red_PS(vertexOutput IN) : SV_Target
{
return float4(1.0,0.0,0.0,1.0);
}
float4 green_PS(vertexOutput IN) : SV_Target
{
return float4(0.0,1.0,0.0,1.0);
}
float4 mix_PS(vertexOutput IN) : SV_Target
{
float r = TargetTextureRed.Sample(g_TargetTextureRedSampler, IN.UV0);
float g = TargetTextureGreen.Sample(g_TargetTextureGreenSampler, IN.UV0);
return float4(r,g,0.0,1.0);
}
///// TECHNIQUES /////////////////////////////
float Script : STANDARDSGLOBAL <
string UIWidget = "none";
string ScriptClass = "scene";
string ScriptOrder = "postprocess";
string ScriptOutput = "color";
string Script = "Technique=Main;";
> = 0.8;
technique11 Main <
string Script =
"ClearSetColor=ClearColor;"
"Clear=Color;"
"Pass=Red;"
"Pass=Green;"
"Pass=Mix;";
>
{
pass Red <
string Script =
"RenderColorTarget0=TargetTextureRed;"
"ClearSetColor=ClearColor;"
"ClearSetColor=ClearColor2;"
"ClearSetDepth=ClearDepth;"
"Clear=Color;"
"Clear=Depth;"
"Draw=geometry;";
>
{
SetVertexShader(CompileShader(vs_5_0,std_VS()));
SetGeometryShader( NULL );
SetPixelShader(CompileShader(ps_5_0,red_PS()));
}
pass Green <
string Script =
"RenderColorTarget1=TargetTextureGreen;"
"ClearSetColor=ClearColor;"
"ClearSetDepth=ClearDepth;"
"Clear=Color;"
"Clear=Depth;"
"Draw=geometry;";
>
{
SetVertexShader(CompileShader(vs_5_0,std_VS()));
SetGeometryShader( NULL );
SetPixelShader(CompileShader(ps_5_0,green_PS()));
}
pass Mix <
string Script =
"RenderColorTarget0=;"
"ClearSetColor=ClearColor;"
"ClearSetDepth=ClearDepth;"
"Clear=Color;"
"Clear=Depth;"
"Draw=Buffer;";
>
{
SetVertexShader(CompileShader(vs_5_0,mix_VS()));
SetGeometryShader( NULL );
SetPixelShader(CompileShader(ps_5_0,mix_PS()));
}
}