Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
meepen opened this issue Jun 8, 2015 · 7 comments

Comments

@meepen
Copy link

meepen commented Jun 8, 2015

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
Copy link
Author

meepen commented Jun 8, 2015

@robotboy655
Copy link
Contributor

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

@meepen
Copy link
Author

meepen commented Jun 8, 2015

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

@Jvs34
Copy link

Jvs34 commented Jun 15, 2015

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.

@meepen
Copy link
Author

meepen commented Jun 16, 2015

I figured that out after a while, but yes.

@Tenrys
Copy link

Tenrys commented Dec 29, 2016

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

@raubana
Copy link

raubana commented Oct 11, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants