Hi !
Featured games (85)
games approved by the League of Dukes
Games in Showcase (615)
Games in Android Showcase (173)
games submitted by our members
Games in WIP (659)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Reusing depth values from FBO  (Read 2499 times)
0 Members and 1 Guest are viewing this topic.
Offline Kerai

Junior Devvie

Medals: 4

« Posted 2014-02-24 04:14:44 »

Suppose I have a 2D scene to render (orthographic projection), which I render with depth testing enabled. This scene is composed of lots and lots of small tiles, so I render it in bigger chunks to FBOs, to avoid re-rendering lots of triangles every frame.

Now, when I render contents of those FBOs, I also would like to preserve the depth value of every of its pixels/texels.
How should I go about that?

GL ES 2.0 / WebGL compatible solution would be appreciated.
Offline davedes
« Reply #1 - Posted 2014-02-24 04:36:20 »

Firstly, why are you using depth testing for 2D? If your sprites are semi transparent it probably isn't worth it.

Secondly, have you actually benchmarked and found that you are vertex bound? If there is no bottleneck, or the bottleneck lies elsewhere, you will probably just further degrade performance.

A sprite batcher and texture atlases should be fast enough to render hundreds of thousands of triangles. Introducing FBO binds means less batching and more state switches, ie. generally worse performance. So it's best to just avoid them if you don't need them.

Offline Kerai

Junior Devvie

Medals: 4

« Reply #2 - Posted 2014-02-24 05:48:41 »

If your sprites are semi transparent it probably isn't worth it.
They are not semi transparent.

Introducing FBO binds means less batching and more state switches
When zoomed out, it's better to have a few prerendered chunks, than half milion of tiles. (yet they need their depth values preserved)
I even packed tile mesh to VBO. New machines don't have a problem with million tris, but it really is a bottleneck on older ones.

I appreciate your advices, but I already know that stuff Wink (sprite batching and texture atlases)

but you can't always fit all your sprites into one atlas. Terrain has it's separate atlas and I don't want to switch between objects and terrain atlases, depth buffer solved that so far. (and terrain is not flat, some objects can be behind some parts of terrain)

It's more of a testing ground, and I have a few ideas on my mind.
I'm going off to sleep now and I hoped that meanwhile someone would share what they know about FBO usage.
How to render FBO's content preserving it's depth values is all I'm asking for.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline pitbuller
« Reply #3 - Posted 2014-02-24 09:56:17 »

Depth texture is what you want. But not all gles2.0 devices does support those.(ios does)
Offline davedes
« Reply #4 - Posted 2014-02-24 13:54:00 »

Well, interesting problem. Smiley

For older devices that don't support depth textures, another idea is to use a "multi texture" batch like so:

I wrote a version in JavaScript that dramatically reduced my draw calls per frame in a pretty large game (with hundreds of assets that would not fit in a small number of sprite sheets).

Offline Kerai

Junior Devvie

Medals: 4

« Reply #5 - Posted 2014-02-25 01:35:38 »

I managed to do what I wanted, more or less. I post it here, so maybe someone will find it useful.

Since GLES/WebGL do not support depth textures out of box (extensions exist, but I don't use them), I did it in fragment shader by storing depth value in alpha channel.

while rendering to FBO
color.a = gl_FragCoord.z;

and while rendering that FBO to the screen

gl_FragDepth = color.a;
color.a = 1.0;

Also, to avoid doing that with completely transparent pixels, in both shaders add
if(color.a == 0) discard;

before doing anything.

That wouldn't work well with semi transparent tiles, as devades mentioned.

For older devices that don't support depth textures, another idea is to use a "multi texture" batch like so:
I also though of that, but as I also mentioned earlier, I want to be able to zoom out ver far, so I have to prerender some chunks of terrain into texture anyway. (one could argue that with such zoom out I shouldn't also be displaying any objects, so depth stops being relevant, but whatever Tongue )
Offline davedes
« Reply #6 - Posted 2014-02-25 01:56:21 »

gl_FragDepth is really on supported in GLES3, or in GLES2 through an extension:

Most desktops running WebGL should support this extension, though.

Offline Kerai

Junior Devvie

Medals: 4

« Reply #7 - Posted 2014-02-25 02:57:02 »

Yeah, you're right. It's not part of webgl...

Wonder if there is another way of doing this
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

Coldstream24 (15 views)
2015-09-03 00:41:28

Andrew_3ds (24 views)
2015-09-01 19:08:10

afikri (16 views)
2015-08-31 09:30:22

afikri (24 views)
2015-08-31 09:30:07

afikri (12 views)
2015-08-31 09:27:24

afikri (16 views)
2015-08-31 09:26:40

Roquen (24 views)
2015-08-29 11:30:54

GamerC4 (34 views)
2015-08-22 20:38:50

GamerC4 (32 views)
2015-08-22 20:37:18

GamerC4 (37 views)
2015-08-22 20:37:01
HotSpot Options
by Roquen
2015-08-29 11:33:11

Rendering resources
by Roquen
2015-08-17 12:42:29

Rendering resources
by Roquen
2015-08-17 09:36:56

Rendering resources
by Roquen
2015-08-13 07:40:51

Networking Resources
by Roquen
2015-08-13 07:40:43

List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!