GL Depth testing

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
OuTopos
Prole
Posts: 8
Joined: Thu Aug 02, 2012 9:44 am

GL Depth testing

Post by OuTopos »

Is there a way to enable GL depth testing?

I can move vertices in 3d with the help of a vertex shader but there is no depth testing.

I guess that is what love3d (https://github.com/excessive/love3d) is doing.
OuTopos
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: GL Depth testing

Post by T-Bone »

Why would you want that in a 2D game?
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: GL Depth testing

Post by s-ol »

T-Bone wrote:Why would you want that in a 2D game?
I'd guess so he can ignore draw order and instead use z values.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
slime
Solid Snayke
Posts: 3166
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: GL Depth testing

Post by slime »

Alpha blending requires objects to be drawn in back-to-front order for proper blending, so changing the z coordinate using a shader isn't much help in that case.
Also, the hardware depth buffer is binary per-pixel (each pixel of what you draw is either fully occluded or not occluded at all) and uses the geometry of what you draw rather than its opacity (it will treat a fully transparent image the exact same as a fully opaque rectangle), so it's not very useful either, in 2D games.
User avatar
OuTopos
Prole
Posts: 8
Joined: Thu Aug 02, 2012 9:44 am

Re: GL Depth testing

Post by OuTopos »

I'm okay with having 1-bit alpha though which can be achieved with the shader.

I want do do it to try to avoid having to sort thousands of sprites in a Lua-table and to save drawcalls.
Instead I would merge all (static) sprites to one mesh per texture. And let the hardware perform depth test.
In my mind that would yield better performance for a 2D game as well.

Anyway the question was if it can be done.
And as I understand that is part of what the Love3D extension is doing. But maybe I'm wrong?
OuTopos
User avatar
slime
Solid Snayke
Posts: 3166
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: GL Depth testing

Post by slime »

To reduce the number of draw calls for sprites you should use a [wiki]SpriteBatch[/wiki], it's the most efficient way to do that in LÖVE currently.

The love3d library will let you do depth testing and depth writes. However, if you output the depth value in a pixel shader via checking the alpha value in a texture, you'll probably get significantly worse performance than if you had no depth at all.

Having a conditional operation per-pixel based on the texture's alpha will slow down the pixel shader a lot, and by writing the depth value in the pixel shader you'll be preventing the GPU from doing an important optimization (normally it skips processing of any pixel that it knows is occluded based on depth, but if you write the depth in the pixel shader it can't know it's occluded beforehand.)

I doubt it will run any faster using depth the depth buffer compared to sorting CPU-side, if you're using a sprite batch. You could probably find plenty of ways to optimize the CPU-side sorting too, if that becomes a bottleneck.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Amazon [Bot], Bing [Bot] and 9 guests