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

Rendertargets getting cut off when drawn to screen and screen height is below 1024 #1629

Open
ZH-Hristov opened this issue Feb 17, 2024 · 19 comments

Comments

@ZH-Hristov
Copy link
Contributor

Tested with this code:

--@client

local HUDRT = render.createRenderTarget("HUDRT")

local w, h = render.getResolution()
local clr = Color(255, 0, 0)
local clearclr = Color(0, 0, 0, 0)

enableHud(player(), true)

hook.add("drawhud", "HUD", function()
    render.selectRenderTarget("HUDRT")
    render.clear(clearclr)
    render.setColor(clr)
    render.drawRect(1024 * 0.5 - 25, 0, 50, 1024) -- this should reach the bottom of the screen
    render.selectRenderTarget()
    
    render.setRenderTargetTexture("HUDRT")
    render.drawTexturedRect(w * 0.5 - (h * 0.5), 0, h, h)
end)

Creates a centered RT and tries to draw a red rectangle from the top to the bottom of the screen.

For example: The rectangle will be properly fully drawn at 1080p, but will be cut off about 3/4 of the way down at 720p.
1080p
720p

This could be a GMod issue for all I know, so its place might not be here.

@thegrb93
Copy link
Owner

I think the drawHud hook clips rendering within the screen resolution. Maybe just need to put a DisableClipping call in it.

@adamnejm
Copy link
Collaborator

If disabling the clipping won't work, maybe using RT_SIZE_LITERAL could.

@ZH-Hristov
Copy link
Contributor Author

SF render.enableClipping doesn't change it.

@thegrb93
Copy link
Owner

that function is for 3d clip planes. it won't do it

@thegrb93 thegrb93 added the bug label Feb 23, 2024
@thegrb93
Copy link
Owner

thegrb93 commented Feb 24, 2024

I tried this

-- TEXTUREFLAGS_PROCEDURAL + TEXTUREFLAGS_EIGHTBITALPHA + TEXTUREFLAGS_RENDERTARGET + TEXTUREFLAGS_DEPTHRENDERTARGET
return GetRenderTargetEx("Starfall_CustomRT_" .. i, 1024, 1024, RT_SIZE_LITERAL, MATERIAL_RT_DEPTH_SEPARATE, bit.bor(2048, 8192, 32768, 65536), 0, 0 )

I'm pretty sure I tried it before too and it didn't work. Not sure it can be fixed

@ZH-Hristov
Copy link
Contributor Author

@thegrb93 Got a reply from rubat. Apparently drawing on it in 2D mode when it's bigger than the screen requires it to be wrapped in cam.start2D

Will give it a try when I can later.

@thegrb93
Copy link
Owner

thegrb93 commented Jul 6, 2024

The selectRenderTarget function does use cam.start2d

@ZH-Hristov
Copy link
Contributor Author

ZH-Hristov commented Jul 7, 2024

The selectRenderTarget function does use cam.start2d

This has to be a issue in Starfall somewhere. I recreated the exact same thing in normal GLua and it works fine there.

local exampleRT = GetRenderTarget( "example_rt", 1024, 1024 )

render.PushRenderTarget( exampleRT )
	cam.Start2D()
        render.Clear(0, 0, 0, 0)
		surface.SetDrawColor( 255, 0, 0, 255 )
		surface.DrawRect( 1024 * 0.5 - 25, 0, 50, 1024 )
	cam.End2D()
render.PopRenderTarget()

local customMaterial = CreateMaterial( "example_rt_mat", "UnlitGeneric", {
	["$basetexture"] = exampleRT:GetName(), -- You can use "example_rt" as well
	["$translucent"] = 1,
	["$vertexcolor"] = 1
} )

hook.Add( "HUDPaint", "ExampleDraw", function()
	surface.SetDrawColor( 255, 255, 255, 255 )
	surface.SetMaterial( customMaterial )
	surface.DrawTexturedRect( ScrW() * 0.5 - (ScrH() * 0.5), 0, ScrH(), ScrH() )
end )

Tested at 720p. SF one is still cut off.

@ZH-Hristov
Copy link
Contributor Author

@thegrb93 Got any clues? I tried DisableClipping in the render library but that didn't change anything.

The GLua snippet I sent works fine at low height resolutions, despite seemingly not doing anything different functionally?

@thegrb93
Copy link
Owner

I'll try in a bit

@thegrb93
Copy link
Owner

@ZH-Hristov It's because of the HUDPaint hook. If you do this code, then you see the same clipping starfall has.

local exampleRT = GetRenderTarget( "example_rt", 1024, 1024 )

local customMaterial = CreateMaterial( "example_rt_mat", "UnlitGeneric", {
	["$basetexture"] = exampleRT:GetName(), -- You can use "example_rt" as well
	["$translucent"] = 1,
	["$vertexcolor"] = 1
} )

hook.Add( "HUDPaint", "ExampleDraw", function()
	render.PushRenderTarget( exampleRT )
		cam.Start2D()
			render.Clear(0, 0, 0, 0)
			surface.SetDrawColor( 255, 0, 0, 255 )
			surface.DrawRect( 1024 * 0.5 - 25, 0, 50, 1024 )
		cam.End2D()
	render.PopRenderTarget()

	surface.SetDrawColor( 255, 255, 255, 255 )
	surface.SetMaterial( customMaterial )
	surface.DrawTexturedRect( ScrW() * 0.5 - (ScrH() * 0.5), 0, ScrH(), ScrH() )
end )

@thegrb93
Copy link
Owner

thegrb93 commented Jul 19, 2024

Seems most gmod render hooks suffer from this. I tried a few of them in starfall

--@name Untitled
--@author Sparky
--@client

enableHud(nil, true)

render.createRenderTarget("hi")
hook.add("predrawtranslucentrenderables","",function()
    render.selectRenderTarget("hi")
    render.clear()
    render.setColor(Color(255,0,0))
    render.drawRect(0,0,1024,1024)
    render.selectRenderTarget()

    render.pushViewMatrix({type="2D"})

    render.setColor(Color(255,255,255))
    render.setRenderTargetTexture("hi")
    render.drawTexturedRect(100,200,200,200)

    render.popViewMatrix()
end)

image

@ZH-Hristov
Copy link
Contributor Author

@ZH-Hristov It's because of the HUDPaint hook. If you do this code, then you see the same clipping starfall has.

local exampleRT = GetRenderTarget( "example_rt", 1024, 1024 )

local customMaterial = CreateMaterial( "example_rt_mat", "UnlitGeneric", {
	["$basetexture"] = exampleRT:GetName(), -- You can use "example_rt" as well
	["$translucent"] = 1,
	["$vertexcolor"] = 1
} )

hook.Add( "HUDPaint", "ExampleDraw", function()
	render.PushRenderTarget( exampleRT )
		cam.Start2D()
			render.Clear(0, 0, 0, 0)
			surface.SetDrawColor( 255, 0, 0, 255 )
			surface.DrawRect( 1024 * 0.5 - 25, 0, 50, 1024 )
		cam.End2D()
	render.PopRenderTarget()

	surface.SetDrawColor( 255, 255, 255, 255 )
	surface.SetMaterial( customMaterial )
	surface.DrawTexturedRect( ScrW() * 0.5 - (ScrH() * 0.5), 0, ScrH(), ScrH() )
end )

Adding DisableClipping in the RT context here works:

local exampleRT = GetRenderTarget( "example_rt", 1024, 1024 )

local customMaterial = CreateMaterial( "example_rt_mat", "UnlitGeneric", {
	["$basetexture"] = exampleRT:GetName(), -- You can use "example_rt" as well
	["$translucent"] = 1,
	["$vertexcolor"] = 1
} )

hook.Add( "HUDPaint", "ExampleDraw", function()
	render.PushRenderTarget( exampleRT )
	DisableClipping(true)
		cam.Start2D()
			render.Clear(0, 0, 0, 0)
			surface.SetDrawColor( 255, 0, 0, 255 )
			surface.DrawRect( 1024 * 0.5 - 25, 0, 50, 1024 )
		cam.End2D()
		DisableClipping(false)
	render.PopRenderTarget()

	surface.SetDrawColor( 255, 255, 255, 255 )
	surface.SetMaterial( customMaterial )
	surface.DrawTexturedRect( ScrW() * 0.5 - (ScrH() * 0.5), 0, ScrH(), ScrH() )
end )

So I suppose we need that in the render lib

@ZH-Hristov
Copy link
Contributor Author

Weird though, since the prepare render hook in SF has a surface.DisableClipping(true) call. It's apparently deprecated but it's an alias so it shouldn't matter. Maybe pushing an RT or a cam enables clipping? Idk.

@thegrb93
Copy link
Owner

Idk if its an alias. You should try adding it

@ZH-Hristov
Copy link
Contributor Author

I have no damn clue why this refuses to work in SF. I tried adding it in PrepareRender, I tried adding it as a render library function and I tried adding it right before cam.Start2D in the selectRenderTarget function.

It still gets cut off.

kill me

@thegrb93
Copy link
Owner

I'll check it out again

@thegrb93
Copy link
Owner

thegrb93 commented Jul 20, 2024

@ZH-Hristov Ok I got it working with render.drawRectFast

--@name Untitled
--@author Sparky
--@client

enableHud(nil, true)

render.createRenderTarget("hi")
hook.add("drawhud","",function()
    render.selectRenderTarget("hi")
    render.clear()
    render.setColor(Color(255,0,0))
    render.drawRectFast(0,0,1024,1024)
    render.selectRenderTarget()

    render.pushViewMatrix({type="2D"})

    render.setColor(Color(255,255,255))
    render.setRenderTargetTexture("hi")
    render.drawTexturedRect(100,200,200,200)

    render.popViewMatrix()
end)

It looks like the 3D rendering functions in the 2D context, such as render.DrawQuad suffers from clipping still, but surface render functions don't.

@ZH-Hristov
Copy link
Contributor Author

I didn't know the SF rect functions used DrawQuad instead. I guess I'll just use the "fast" ones.

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

No branches or pull requests

3 participants