garrysmod-issues icon indicating copy to clipboard operation
garrysmod-issues copied to clipboard

render.Clear sets alpha incorrectly for upcoming surface calls in a rendertarget

Open meepen opened this issue 10 years ago • 7 comments

for example,

render.SetRenderTarget(some_rt)
  render.Clear(0,0,0,100)
  surface.SetDrawColor(255,255,255,200)
  surface.DrawRect(0,0,100,100)

this will draw the rectangle with color 255,255,255,(200/255)*100

i am assuming that it should however set the color of the whole rendertarget at black with 100 alpha then let the surface calls that are upcoming do whatever color they want

meepen avatar Jun 08 '15 21:06 meepen

http://facepunch.com/showthread.php?t=1276498

meepen avatar Jun 08 '15 21:06 meepen

AFAIK @UnderscoreKilburn said that there will always be problems with transparency and render targets.

robotboy655 avatar Jun 08 '15 21:06 robotboy655

is there any work arounds? i kind of want to use transparency

meepen avatar Jun 08 '15 21:06 meepen

I believe this is what you want? http://wiki.garrysmod.com/page/render/OverrideAlphaWriteEnable

There's an example in http://wiki.garrysmod.com/page/render/PushRenderTarget

Although if I remember correctly you also need to create the render target with some other flags, but give it a try anyway.

Jvs34 avatar Jun 15 '15 07:06 Jvs34

I figured that out after a while, but yes.

meepen avatar Jun 16 '15 03:06 meepen

Did you eventually figure out which flags to use, @meepdarknessmeep?

Tenrys avatar Dec 29 '16 07:12 Tenrys

Also running into issues with this. All my testing has proved fruitless.

I tried everything I could think of... I tried both GetRenderTarget and GetRenderTargetEx, and neither worked. When using GetRenderTargetEx, I tried all sorts of flags and settings without success. I even followed the example shown at the bottom of the documentation for the render.PushRenderTarget function or method or whatever the heck it's called in Lua.

I'm not sure if it has to do with render context or not, but the few tests I tried weren't successful.

Here's my code for making the texture:

-- https://wiki.facepunch.com/gmod/Global.GetRenderTargetEx
local b_texture = GetRenderTargetEx(
	"cb_dof_b", -- texture name for internal use
	ScrW(), -- texture width. should be power of 2.
	ScrH(), -- texture height. should be power of 2.
	RT_SIZE_DEFAULT, -- size mode. 
	MATERIAL_RT_DEPTH_SHARED, -- depth mode.
	64+256+512+8192, -- texture flags. enums are not defined. currently has SRGB, no mipmap, no lod, and uses eight bit alpha.
	CREATERENDERTARGETFLAGS_UNFILTERABLE_OK, -- render target flags. Used for HDR.
	IMAGE_FORMAT_RGBA8888 -- Image format.
)

I used the PreDrawEffect hook to execute the following every frame:

cam.Start2D()
	render.OverrideAlphaWriteEnable( true, true )
	
	render.PushRenderTarget( b_texture )
	
	render.Clear( 0, 0, 0, 0, true )
	
	render.SetColorMaterial()
	render.DrawScreenQuadEx( 100, 100, ScrW()-200, ScrH()-200 )
	
	local r, g, b, a = render.ReadPixel( 10, 10 )
	
	render.PopRenderTarget()
	
	print( r, g, b, a )
	
	local color = b_texture:GetColor( 10, 10 )
	
	PrintTable( color )
	
	render.ClearRenderTarget( b_texture, Color(0,0,0,0) )
	
	color = b_texture:GetColor( 10, 10 )
	
	PrintTable( color )
	
	render.OverrideAlphaWriteEnable( false )
cam.End2D()

No matter what I do, I still get the same result. It's like there isn't an alpha channel at all or it's not being set right:

0 0 0 nil

a = 255 b = 0 g = 0 r = 0

a = 255 b = 0 g = 0 r = 0

All I want to do is blit one render target to another using the standard alpha compositing operation, but nothing I do seems to work.

raubana avatar Oct 11 '22 19:10 raubana