Render targets with alpha
#1 Members - Reputation: 395
Posted 27 February 2013 - 01:58 PM
My first layer, the game scene, is rendered to the back buffer, my next scene, the HUD (which currently is just a bunch of text quads), is rendered to a render target. I then blend the HUD render target onto the back buffer with a very simple full screen quad shader.
The problem is that my HUD render target doesn't appear to be fully transparent in the areas where I haven't drawn text. When I Clear the render target I clear it with 0,0,0,0 which I thought would give me a fully transparent surface. I then render my text quads to that target.
When I blend, the HUD layer just appears to cover the back buffer surface completely. If I adjust the alpha component in the output of the simple blend pixel shader to something like 0.5 then I get half brightness on the background and half brightness on the HUD layer.
Is there some special blend mode parameters I need to set for rendering alpha to rendertargets?
My blending is:
BlendOp=ADD;
SrcBlend=SRCALPHA;
DestBlend=INVSRCALPHA;
AlphaBlendEnable=TRUE;
This shader is very simple, renders to the back buffer and just takes a sample from the HUD layer.
#3 Members - Reputation: 395
Posted 27 February 2013 - 04:06 PM
If I manually set the alpha in the blend shader pixel shader return value to 0.5 it works, but obviously the areas of the HUD that haven't been drawn on should be alpha 0.
#7 Members - Reputation: 951
Posted 27 February 2013 - 10:00 PM
Betting it's clear color too.
#8 Members - Reputation: 395
Posted 28 February 2013 - 01:05 AM
Should I not be clearing colour?Betting it's clear color too.
Yes, definitely clearing with 4 zeros. I can't quite remember whether it's XRGB or ARGB (I'll check) but it's definitely 4 zeros.Are you clearing with D3DCOLOR_ARGB(0,0,0,0)?
Even D3DCOLOR_XRGB(0,0,0) should do (no warranty), just make sure its three zeros and not like 255.
#9 Members - Reputation: 951
Posted 28 February 2013 - 09:34 AM
Should I not be clearing colour?Betting it's clear color too.
Yes, definitely clearing with 4 zeros. I can't quite remember whether it's XRGB or ARGB (I'll check) but it's definitely 4 zeros.>Are you clearing with D3DCOLOR_ARGB(0,0,0,0)?
Even D3DCOLOR_XRGB(0,0,0) should do (no warranty), just make sure its three zeros and not like 255.
Not quite, I just suspect you're clearing it to an 'opaque' alpha value when you really want it to be cleared to a transparent one. You can try flipping the alpha to 1.0f/255u and seeing if that changes anything. Of course, all this is irrelevant if you're using an XRGB render target!
#10 GDNet+ - Reputation: 920
Posted 28 February 2013 - 09:42 AM
Thats what my clear call is looking like:
m_lpDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_ARGB(0,0,0,0), 1.0f, 0);
What does yours look like? Did you make sure the texture is still bound to the device when clearing? Does this happen in release and debug mode? I remember having a very similar issue with alpha transparency, only in release mode, I try to find the thread (it's quite old), can't recall the solution anymore...
#11 Members - Reputation: 395
Posted 28 February 2013 - 09:52 AM
Not quite, I just suspect you're clearing it to an 'opaque' alpha value when you really want it to be cleared to a transparent one. You can try flipping the alpha to 1.0f/255u and seeing if that changes anything. Of course, all this is irrelevant if you're using an XRGB render target!
Definitely using an XRGB render target - if I return an alpha of 0.5 in the shader ithe transparency works but everything is half bright as expected.
When I clear, I clear z and colour and use the equivalent of D3DCOLOR(0,0,0,0). Wouldn't that just resolve down to a DWORD of zero anyway? So even if I used FromXRGB(0,0,0), surely that would still resolve down to a DWORD of zero?
#12 Members - Reputation: 951
Posted 28 February 2013 - 10:34 AM
Is the *HUD* render target being cleared and/or XRGB, or is the backbuffer?
#13 Members - Reputation: 395
Posted 28 February 2013 - 10:57 AM
Is the *HUD* render target being cleared and/or XRGB, or is the backbuffer?
Yes, the hud render target gets cleared in the 'normal' way, i.e. clear colour, clear z, with a colour of 0,0,0,0. Not sure what you mean by 'or is the backbuffer?'
#14 GDNet+ - Reputation: 920
Posted 28 February 2013 - 11:35 AM
Yes, the hud render target gets cleared in the 'normal' way, i.e. clear
colour, clear z, with a colour of 0,0,0,0. Not sure what you mean by 'or
is the backbuffer?'
If you call Device->clear(X,...); the currently set rendertarget gets cleared, where the first parameter ist the stage you bound it to using SetRenderTarget(X, surface). So if you just call "clear" at the end of the render loop after you set the rendertarget back to backbuffer (SetRenderTarget(0,NULL)) only the backbuffer will get cleared. You need to call Clear expicitely when the texture is bound as a rendertarget. You need to clear the backbuffer like you probably do know in addition to that.
Plus, no need to "Clear z" for a backbuffer-sized rendertarget.
Edited by The King2, 28 February 2013 - 11:36 AM.






