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

render.CopyTexture() Does not copy the rendertarget's depth buffer

Open meetric1 opened this issue 2 years ago • 2 comments

Details

when using render.CopyTexture on a rendertarget, the depth buffer is not copied over to rendertargets's that have the MATERIAL_RT_DEPTH_SEPARATE flag.

Steps to reproduce

The issue occurs when running the following code ingame (see comments to understand its function): Note that this code might only give correct results when MSAA is disabled, as the texture returned by render.GetFullScreenDepthTexture() might create its own separate (and blank) depth buffer when MSAA is enabled (relating to this code: https://github.com/lua9520/source-engine-2018-hl2_src/blob/3bf9df6b2785fa6d951086978a3e66f49427166a/materialsystem/ctexture.cpp#L1755 ) (although I'm not actually 100% sure about this^)

-- Create rendertarget and reset depth to known good. Make sure this rendertarget has a separate depth buffer
-- Using MATERIAL_RT_DEPTH_SHARED will cause the depth to act properly (make sure to rename the rt to something different if you do!)
local test_rt = GetRenderTargetEx("test001", ScrW(), ScrH(), RT_SIZE_DEFAULT, MATERIAL_RT_DEPTH_SEPARATE, 2 + 256, 0, 0)
render.SetRenderTarget(test_rt)
	render.ClearDepth()
render.SetRenderTarget()

hook.Add("PostDrawOpaqueRenderables", "test", function() 	-- (might be called for skybox as well)
	local depth_rt = render.GetFullScreenDepthTexture()
	render.CopyTexture(depth_rt, test_rt)	-- Copy framebuffer to test_rt (this should be copying its depth buffer too... which it does not)

	-- Draws sphere at 0,0,0. The sphere will appear inside of the rendertarget like it was being rendered normally
	render.SetRenderTarget(test_rt)
		render.SetColorMaterial()
		render.DrawSphere(Vector(), 100, 10, 10, color_white)
	render.SetRenderTarget()

	-- The object (white sphere in this case) on this RT should be obeying the depth buffer defined by the framebuffer (depth_rt)
	render.DrawTextureToScreenRect(test_rt, ScrW() * 0.75, 0, ScrW() / 4, ScrH() / 4)
end)

meetric1 avatar Mar 06 '24 21:03 meetric1

Related/duplicate: https://github.com/Facepunch/garrysmod-requests/issues/2308

robotboy655 avatar Mar 07 '24 01:03 robotboy655

Ah. sorry, didn't see that.

Are there any plans to add that as a feature? I don't see anything easily defined in the 2013 SDK, but I hope it's still possible in some form

meetric1 avatar Mar 07 '24 02:03 meetric1