Check texture for alpha

Started by
1 comment, last by ET3D 15 years, 3 months ago
Can anyone suggest a simple way to check if a texture contains any alpha data in DirectX10? I see that the texture description could tell me if I wanted to handle every format constant, but am wondering if there is something cleaner than : if( texDesc.Format == DXGI_FORMAT_R32G32B32A32_TYPELESS || texDesc.Format == DXGI_FORMAT_R32G32B32A32_FLOAT || texDesc.Format == DXGI_FORMAT_R32G32B32A32_UINT || texDesc.Format == DXGI_FORMAT_R32G32B32A32_SINT || texDesc.Format == DXGI_FORMAT_R16G16B16A16_TYPELESS || texDesc.Format == DXGI_FORMAT_R16G16B16A16_FLOAT || texDesc.Format == DXGI_FORMAT_R16G16B16A16_UNORM || texDesc.Format == DXGI_FORMAT_R16G16B16A16_UINT || texDesc.Format == DXGI_FORMAT_R16G16B16A16_SNORM || texDesc.Format == DXGI_FORMAT_R16G16B16A16_SINT || // etc. a bunch of other constants with alpha... ) { // Has alpha } [Edited by - brekehan on December 29, 2008 2:31:06 PM]
Advertisement
Another way I thought of was to

request it be loaded in R32G32B32A32 format,
check that it got loaded in R32G32B32A32,
iterate through the data,
check each pixel for alpha != 1

That is also pretty computationally expensive, even if it is at init time.

Anyone have better ideas?
I understand from your second post that you want to know that there's an actual non-opaque pixel, regardless of whether the texture has alpha in its format.

In that case, you have no choice but to check the pixels. There are several ways to save from having to check every pixel with the CPU on loading.

- Check offline. Either run a program over your textures, if there's a fixed set of textures, or check them first time they're loaded, then store the information somewhere.

- Check with the GPU. There are several ways to check:

- Scale the texture by half repeatedly, with a shader that keeps a non-opaque alpha if any of the 4 pixels has it. After several passes check the remaining pixel on the CPU.

- Run a shader that changes the Z value depending on alpha, making the pixel fail Z if it doesn't have alpha. Use an occlusion query to count the number of pixels which pass Z testing (and therefore have alpha, in this case).

This topic is closed to new replies.

Advertisement