• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
QNAN

Alpha Testing: I manage to "scorch" my edges

26 posts in this topic

As I understand it, Alpha Testing will simply ignore those pixels, that do not pass the test.

I have made an alpha-test with  renderstates [D3DRS_ALPHAFUNC, D3DCMP_GREATER] and [D3DRS_ALPHAREF, 128].

 

Since the problem arose, I have simplified my texture, making the rgb-layer completely white, while preserving the alpha-layer.

 

I have further simplified my shader, so that it now simply outputs the texture raw, and that is all. No lighting or any other kinds of calculations.

 

The result is still scorched edges, as can be seen below:

 

alphatest_scorched.jpg

 

I would have expected a completely white plant, with no grey-scales (partial alpha) and no black edges.

How come, that the alpha is burned into the texture like that?

 

As can be seen, the pixel shader is extremely simple (I removed the argument list because it is irrelevant (except a_texcoord0)).

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

My renderstates are as follows:

SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE)
SetRenderState(D3DRS_ZWRITEENABLE, TRUE)
SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE)
SetRenderState(D3DRS_ALPHATESTENABLE, TRUE)
SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATER)
SetRenderState(D3DRS_ALPHAREF, 128)

 

Thank you in advance.

0

Share this post


Link to post
Share on other sites
I use Paint Shop Pro 7 (ya, I know, ancient, but simple and wonderfully fast) and Photoshop CS4.
I do all alpha work in Photoshop.
The texture is in this case .tga. I use many kinds of formats. .tga usually when testing something with alpha, although I would use .dds "live".

I will upload the original texture. Be aware, that it will only use the upper-left quarter of the texturemap.
The texture should be professional level.

I had to pack the texture in a .zip, as the server refused to have it downloaded.
http://www.samtalebasen.dk/test.zip Edited by QNAN
0

Share this post


Link to post
Share on other sites
Btw, I whited out the entire rgb layer - completely, meaning across borders - of the texture in the opening post. That should ensure, that the colors around the borders were the same. Edited by QNAN
0

Share this post


Link to post
Share on other sites
Are you not reading Hodgman's post?

Textures get filtered. When you render them, their values come from more than a single pixel. It doesn't matter what color they are, the surrounding pixels will have an affect unless you are using NEAREST or POINT filtering. So 1 pixel in the image will have alpha 1, and the next will have alpha 0. But when interpolated during rendering, you will get some that average out to 0.5.

Using a similar color on the surrounding transparent pixels will help a bit.

Using the pre-multiplied alpha technique will help a lot more.

http://blogs.msdn.com/b/shawnhar/archive/2009/11/06/premultiplied-alpha.aspx
2

Share this post


Link to post
Share on other sites

Your texture looks good --  the transparent areas do have colours similar to the opaque areas...

For the benefit of others, it looks like this:

z8pX2PY.png

 

How are you loading the texture? Perhaps your texture loader is changing the transparent areas to black?

 

Using the pre-multiplied alpha technique will help a lot more

This is good advice -- this black border artefact simply doesn't occur with pre-multiplied alpha blending.

However, that requires the use of alpha-blending, not just alpha testing that the OP is using, so it might not be applicable (e.g. if the correct sort order can't be guaranteed).

1

Share this post


Link to post
Share on other sites

Btw, I whited out the entire rgb layer - completely, meaning across borders - of the texture in the opening post. That should ensure, that the colors around the borders were the same.


When do you do this? In Photoshop or during texture load time? Many photo editors have the nasty habit of zeroing out RGB values for 0-alpha pixels when they export an image.

Have you checked the texture in PIX? Edited by eppo
1

Share this post


Link to post
Share on other sites
A good trick to fix black borders especially when storing in 1bit DXT1 is to do the following in the shader.

color.rgb /=color.a;

So as you are start filtering towards a edge pixel you renormalize the colours.
2

Share this post


Link to post
Share on other sites

I tried to alter the texture filtering in the shader to Point, with this result:
alphatest_scorched_filteringpoint.jpg
 
As can be seen, this does not solve my problem. The result is just pixelated.
 
This is how the sampler looks in the shader:

sampler2D g_texturemap_sampler =
sampler_state
{
	Texture = <g_texturemap>;
	MinFilter = Point;
	MagFilter = Point;
	MipFilter = Point;
};

 

I also tried to plug my texture into an ATI-demo called "Alpha to Coverage", replacing it with one of the other textures. The demo also draws AlphaTesting, which I set it to in the screenshot.

This is how it looks in the ATI demo:

alphatest_scorched_ATI_demo.jpg

 

There is no flaws, so I believe that this is not a texture problem - at least not at the file level.

 

Maybe in my creation?

D3DXCreateTextureFromFileExW(   m_device,
                                "../database/texture/foliage/gniff.tga", // gniff.tga is just a testname - it is the right one
                                D3DX_FROM_FILE,			// width
                                D3DX_FROM_FILE,			// height
                                D3DX_DEFAULT,			// miplevels
                                0,				// usage
                                D3DFMT_A8R8G8B8,		// format
                                D3DPOOL_MANAGED,		// pool
                                D3DX_DEFAULT,			// filter
                                D3DX_DEFAULT,			// mipfilter
                                0,				// colorkey
                                NULL,				// srcinfo
                                NULL,				// palette
                                a_textureDX))			// texture

 

If it is not any of these, could it then be a renderstate, that Im unaware of? Is there any renderstate, that can cause this?

 

Thank you for your help guys.

Edited by QNAN
0

Share this post


Link to post
Share on other sites

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);
2

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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"

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Edited by QNAN
0

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

I stole code for loading .tga from the ATI demo I used ealier. But it still took quite a while to implement, so I really regret that I didn't try PIX earlier.

I didn't know PIX and I guess I shied away because of that. Too bad.

 

Once I got it up and running, I must say that Im impressed. I can easily see myself getting addicted to it smile.png

 

It does not look like there is anything wrong with the D3DX-loading of the .tga. My ATI-imported code didn't solve the problem either, so I expected that. But what can it be then?

I made a collage of the PIX session I ran. If you are interested in something not shown, let me know. I am also uploading my .PixRun file, if anyone is interested. It is created by the latest DX-SDK (june 2010 - had to download it especially for this, as the DX I run on had an ancient version (DX SDK June 2005).

 

 

Here is the PIXRun-file (zipped):

http://www.samtalebasen.dk/Run7.PIXRun.zip

 

Here is the PIX-collage (BIG!):

PIX_collage.jpg

Edited by QNAN
1

Share this post


Link to post
Share on other sites

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.

Yes you're right, i should've mentioned that.

It looks like blending is disabled in that Pix screenshot, so that doesn't seem to be the problem.

0

Share this post


Link to post
Share on other sites

It looks like blending is disabled in that Pix screenshot, so that doesn't seem to be the problem.

His PIX screenshot is not the actual scenario though, just a test app (QNAN, am I correct? When I open the PIX run all I see is one piece of vegetation draw - there is no background or anything).

My best guess is that you do have alpha-blending turned on when you're rendering your alpha-tested vegetation, and that you're drawing it prior to drawing your colorful background. Thus, the black value to which you cleared the render target is showing through where the alpha < 255.

Can you double check that alpha-blending is OFF (in your actual usage scenario)? Edited by phil_t
0

Share this post


Link to post
Share on other sites

I figured it out, and it was not an easy one:

My rendertarget was in the format of ARGB and cleared to black. The alpha values of the plants then changed the alpha value of the rendertarget. This rendertarget was then rendered onto another rendertarget (in order to support multiple viewports), also cleared black, but with XRGB format. And then the black hits through the alpha values.

 

I changed the rendertarget to XRGB and the problem was solved. I was experimenting once with antialias and at some point set the rendertarget to ARGB and never changed it back, and then it hit me now like a landmine smile.png

 

I thank you all for your patience and incredible will to help. Even when we did not get close to the actual problem here, I learned alot:

 - Not taking anything for granted, not even a D3DX-file-loader

 - Both editors and loaders can play funny tricks to save space, like storing RGB-layer of pixels with alpha 0 as black (0).

 - I learned that good alpha textures needs similar RGB color across the border, or filtering will create an unwanted result (since I have only used other ppl's textures, this was new  to me)

 - When I go into details on mipmaps, I have to be careful with the same borders on these as well

 - I learned to use PIX!

 

Thank you all for your help. I wouldn't know what to do without the incredible amount of expertise on this forum.

 

 

EDIT:

phil_t:

It was actually the same scenario as before, I had just disabled rendering skybox, terrain and non-alphatested objects, so I wouldn't get an ungodly amount of textures and surfaces in the test.

Edited by QNAN
1

Share this post


Link to post
Share on other sites

It looks a lot like you're not setting the blend mode correctly.  

 

The black borders is a sign that your texture is stored with pre-multiplied alpha.  (Since you're not handling alpha blending correctly, it'll look black because the color is being pre-multiplied by an alpha close to 0)  This is most likely fine.

 

With pre=multiplied alphas, you probably want to set your blend mode to something like: S + D(1-Sa)

 

Hope this helps.

Edited by Polarist
-1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0