Alpha Testing: I manage to "scorch" my edges

Started by
25 comments, last by phil_t 11 years, 2 months ago

float4 PS_light_SHADOW(...) : COLOR
{
return tex2D(g_texturemap_sampler, a_texcoord0);
}

Change the body to

float4 col = tex2D(g_texturemap_sampler, a_texcoord0);
return float4(col.rgb/col.a, col.a);
Advertisement

I don't know if the alpha texture is pre-made or you made it your self. If you made it your self try saving the texture as grayscale and not RGB, if possible when you convert your image to grayscale one save it with DXT format that uses alpha channel, and than see what happens. Also if that doesn't help, then try to use that alpha texture (grayscale one) with one of the shaders from above to see if that fixes the problem, for you.

Hello I think i may be self shadowing ???

This looks like the classic problem of background colors getting blended into the edges of mipmaps. I spent a significant amount of time on this topic when it arose in my project. In short, what happens is that directx blends the background pixels at the edge of the visible part of the image into the edge pixels of the visible part of the image, resulting in a usually black or dark border around the visible part of the image. It appears to be a weakness or flaw in the mipmap generation algorithm in DirectX. Only pixels with alpha > threshold value should be blended. I'll dig up my notes and post the results. I tested just about every possible combo. point sampling removes the edges, but as i recall, the image disappears at certain viewing angles. this effect can be seen in some plants in Oblivion by Bethesda Softworks. I'll check my notes and post my findings. While my work is done in retained mode, not HLSL, you should be able to easily implement it in HLSL, perhaps with some help from folks here.

Norm Barrows

Rockland Software Productions

"Building PC games since 1988"

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php

As eppo said, you can use PIX to capture a frame of your game while it's running, and then view the it's resources. That will let you see what your texture looks like after it's been loaded into the GPU.

God knows what D3DXCreateTextureFromFileEx does internally... From the looks of it, it is "pre-multiplying" your RGB values with your alpha values... which as Daaark said, is helpful if you want to use alpha blending, but isn't helpful if you just want to use alpha-testing.

You could try loading the TGA file yourself instead of using the D3DX helper library to do it...
http://nothings.org/stb_image.c
http://nehe.gamedev.net/tutorial/loading_compressed_and_uncompressed_tga's/22001/

QFT. Given the texture that we've seen (which does have correctly painted RGB channels extending outside the opaque portions), I strongly suspect D3DX is either explicitly pre-multiplying the alpha channel *or* the texture is ending up as DXT1 (BC1), which implicitly does the same thing.

From "Norm's DirectX Tips":

textures with transparent backgrounds and mipmapping:
use point sampling for the mipfilter when loading the texture. D3DX_FILTER_NONE is probaly undefined as a filter for loading textures and generating mipmaps, as its the flag used to turn mipmaps OFF! using D3DX_FILTER_NONE results in directx copying a portion of each mipmap (the UL 1/4 ?) to the next level down, with no scaling or filtering or anything. this somehow results in textures that appear and disappear as their triangles move in relation to the camera. you can see this effect on some plants in oblivion. the upside to this is that with no filtering or sampling, no background color gets blended in, and you dont get any dark edges around the sprite images.
so here's the deal with dark edges, sprites, and mipmapping:
sprite textures drawn without mipmaps tend to "sparkle" as the camera moves. the non-top level mipmaps in a sprite texture tend to have the background color blended into the edges of the image, either when directx makes the mipmaps, or when it samples them to texture a triangle.
when directx creates and / or uses mipmaps, it has a tendancy to blend the keycolor or background color into the pixels at the edge of the sprite image. linear, box, and triangle filters do this quite noticeably, point filtering only somewhat. playing with alpaha testing doesnt help much, as it only controls how diretcx draws these already messed up textures. weither the texture is colorkeyed, or has a black and white alpha channel or a grayscale alpha channel makes little difference. making your own mipmaps with black and white alpha helps very little, as directx still blends between miplevels.
about the best you can do, is set the alpha test high (i use 128), and use textures that aren't too dark. the high alpha test filters out the black edges pretty well.
but ideally the apha test should be "if greater than 0, draw". for this, you'd have to go to a LOD scheme, where you had textures at 256,128,64, etc sizes, and draw sprite textured meshes with no mipmaps, using a different size texture based on range (IE do the mipmap manually). but there, its possible you'd still get background color blended into the edges as directx scaled the texture. but maybe not, beacuse you get no black edges on the topmost sprite tex. you only get it on lower mipmap levels.
another technique that showed promise was a texture that filled a 256x256 (stone in my case), and an alpha channel that had a white silouhette of the object (a distant rock). all mipmaps of all levels of this texcture were solid stone, with no background color, guaranteeing that the background color couldn't get sampled and blended in.
setting min and mag filters to point filtering as opposed to anisotropic filtering may also help make sprite textures look better.
i was using sprite textures to draw billboards of distant rocks and plants. i kept moving the clip distance for meshes further out. i started at meshes up to 100 range, then billboards til 350 range. i got as far as meshes to 250 or 300 range and the rest billboards, and decided to just get rid of the billboards and draw all meshes out to 350 range.
=================================================================================
here's what i do in my current project:
// load sprite texture
void Zloadspritetex3(char *s) // as loadtex, w/ color key, and point mipfilter
{
HRESULT h;
char s2[100];
h=D3DXCreateTextureFromFileExA(Zd3d_device_ptr,s,
D3DX_DEFAULT,D3DX_DEFAULT, // width,height
0, // mip lvls. 0 or default = full chain, 1= no mip maps.
0, // usage: not dynamic, not render tgt.
D3DFMT_A8R8G8B8,D3DPOOL_DEFAULT,
D3DX_FILTER_NONE, // image filter
D3DX_FILTER_POINT, // mip filter. none=vanishing mipmaps problem. point=wrap problems.
// linear,tri,and box blend the keycolor into the edges of the image (dark edges problem).
// no mips causes sparkle problem.
0xFF000000, // 0XFF000000, color key
NULL,NULL,&(Ztex[numtextures].tex));
if (h != D3D_OK) { strcpy_s(s2,100,"Error loading "); strcat_s(s2,100,s); Zmsg2(s2); exit(1); }
strcpy_s(Ztex[numtextures].name,100,s);
numtextures++;
}
int Zalphatestlvl=128;
// turn alphatesting on/off
void Zalphatest(int onoff)
{
if (onoff==1)
{
Zd3d_device_ptr->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);
Zd3d_device_ptr->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL); // D3DCMP_NOTEQUAL
Zd3d_device_ptr->SetRenderState(D3DRS_ALPHAREF,(DWORD)Zalphatestlvl); // (DWORD)0x00000000
}
else
{
Zd3d_device_ptr->SetRenderState(D3DRS_ALPHATESTENABLE,FALSE);
}
}
unless you want to generate your own mipmaps that don't blend the background into the visible image edges, this is about the best you can do.
I also read about one game that first did alphatest pass, then an alphablend pass to draw the fine edges of the visible image that get truncated in a simple alphatest.
Hope this helps!
Norm Barrows
Rockland Software Productions - Building PC games since 1988

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php

Do you render the alpha tested geometry before or after you render opaque geometry?

If you render it first, it'll still write the depth values for pixels with alpha < 1.0. The terrain would then fail the depth comparison for those pixels and your clearcolor would become visible.

Do you render the alpha tested geometry before or after you render opaque geometry?

If you render it first, it'll still write the depth values for pixels with alpha < 1.0. The terrain would then fail the depth comparison for those pixels and your clearcolor would become visible.

This is only true for alpha blending. For alpha testing each pixel is either opaque or clipped. Of course its possible to enable both in which case your point is valid.

I am hard at work testing different approaches. I got drowned in suggestions here smile.png

My first attempt will be to write my own loader, so I can be sure D3DX is not messing with me.

I will get back with a full report when I come out of the pixel mines again smile.png

EDIT:

Actually Im gonna install PIX first, it looks like it can save me the trouble, if D3DX is actually doing its job properly.

My first attempt will be to write my own loader, so I can be sure D3DX is not messing with me.

Much easier than writing your own loader would be to just take a few minutes and take frame capture in PIX to see the exact pixel values of the source texture and make sure they're right.

This topic is closed to new replies.

Advertisement