I'm creating a little editor for my terrain so I can "paint" the terrain, but I'm getting something around 20 FPS using 8 textures and 2 alphamaps from now.
My HLSL code for pixel shader:
////////////////////////////////////////////////////////////////////////////////
// Filename: terrain.ps
////////////////////////////////////////////////////////////////////////////////
/////////////
// GLOBALS //
/////////////
Texture2D textures[8];
Texture2D alphaTextures[2];
//////////////
// SAMPLERS //
//////////////
SamplerState SampleType;
//////////////
// TYPEDEFS //
//////////////
struct PixelInputType
{
float4 position : SV_POSITION;
float2 tex : TEXCOORD0;
float2 alpha : TEXCOORD1;
float3 normal : NORMAL;
};
////////////////////////////////////////////////////////////////////////////////
// Pixel Shader
////////////////////////////////////////////////////////////////////////////////
float4 TerrainPixelShader(PixelInputType input) : SV_TARGET
{
int i;
float4 color;
float4 baseColor;
float4 textureColor[8];
float4 alphaMap[2];
// Sample all textures
for(i=0; i<8; i++)
{
textureColor[i] = textures[i].Sample(SampleType, input.tex, 0);
}
// Sample the alphamaps
for(i=0; i<2; i++)
{
alphaMap[i] = alphaTextures[i].Sample(SampleType, input.alpha, 0);
}
// Set the base color
color.r = 0;
color.g = 0;
color.b = 0;
color.a = 255;
// Add the second layer using the red channel of the alpha map.
color = lerp(color, textureColor[0], alphaMap[0].r);
// Add the third layer using the green channel of the alpha map.
color = lerp(color, textureColor[1], alphaMap[0].g);
// Add the forth layer using the blue channel of the alpha map.
color = lerp(color, textureColor[2], alphaMap[0].b);
// Add the fifth layer using the alpha channel of the alpha map.
color = lerp(color, textureColor[3], alphaMap[0].a);
// Add the sixth layer using the red channel of the alpha map.
color = lerp(color, textureColor[4], alphaMap[1].r);
// Add the seventh layer using the green channel of the alpha map.
color = lerp(color, textureColor[5], alphaMap[1].g);
// Add the eigth layer using the blue channel of the alpha map.
color = lerp(color, textureColor[6], alphaMap[1].b);
// Add the eigth layer using the alpha channel of the alpha map.
color = lerp(color, textureColor[7], alphaMap[1].a);
// Return the final color
return color;
}
Yes I know that I could check if I really need to use the 8 textures (and skip some part of the code) but I want to imagine a situation where I NEED to use all textures.
The FPS start to drop when I paint more layers, everything start black, when I paint the first layer (Just need to be a little pixel) my FPS go from 55 to 45, the second layer makes my fps go from 45 to 37 and so on...
There is a better way to achieve this or this Is the only way? I'm a little new on directX so I belive there is something wrong with my code (or my theory) Thanks!
Note that my first texture coordinates repeat the terrain about 4X and the alpha fills only one time, this is slowing things too because if I use the alpha coordinates for the textures too my FPS grows up +-20. (the coordinates go from 0.0 to 1.0 on both).