Painting texture layers - pixel shader

Started by
3 comments, last by CC Ricers 11 years, 1 month ago

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).

- Flick

Advertisement

you perform a 10 lookups and a couple of lerps so that is not the most performance heavy shader...

Without more info about the context you use this shader (texture sizes, sampler types, geometry count, how/how often do you update the textures,...) I don't think we can improve a lot here...

Here is the info:

- All textures are 1024x1024

- The alpha textures are 256x256

- The sampler state is configured like this:


   samplerDesc.Filter = D3D10_FILTER_MIN_MAG_MIP_LINEAR;
    samplerDesc.AddressU = D3D10_TEXTURE_ADDRESS_WRAP;
    samplerDesc.AddressV = D3D10_TEXTURE_ADDRESS_WRAP;
    samplerDesc.AddressW = D3D10_TEXTURE_ADDRESS_WRAP;
    samplerDesc.MipLODBias = 0.0f;
    samplerDesc.MaxAnisotropy = 1;
    samplerDesc.ComparisonFunc = D3D10_COMPARISON_ALWAYS;
    samplerDesc.BorderColor[0] = 0;
    samplerDesc.BorderColor[1] = 0;
    samplerDesc.BorderColor[2] = 0;
    samplerDesc.BorderColor[3] = 0;
    samplerDesc.MinLOD = 0;
    samplerDesc.MaxLOD = D3D10_FLOAT32_MAX;
 

- My terrain has 128x128 vertices

I only update the alpha textures (only when I want to paint)

And one more thing... I only use 1 MipLevel for all textures, maybe this is what is causing this?

- Flick

I only use 1 MipLevel for all textures, maybe this is what is causing this?

Most likely, although it depends on how "zoomed in" you are in to your terrain. If one pixel of your rendered terrain ends up corresponding to more than a couple of texels of your texture, then using mip maps will help performance (they will significantly reduce texture bandwidth, which may be your perf bottleneck). In general, you should be using mip maps.

What format are your textures? Using DXT compression will also significantly reduce texture sampling bandwidth.

From my experience disabling mipmapping lowers framerate in many situations, especially if the texture is being drawn to many pixels like a terrain would. Not so much a problem for closer objects than it is for farther ones. Part of the extra GPU work of not using mipmaps also results from determining which texel takes priority for a given pixel.

New game in progress: Project SeedWorld

My development blog: Electronic Meteor

This topic is closed to new replies.

Advertisement