Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
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 2029 times)
0 Members and 1 Guest are viewing this topic.
Offline Kerai

Junior Member


Medals: 4



« Posted 2014-02-24 05: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 05: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 Member


Medals: 4



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

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

Quote
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 10: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 14: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:
http://webglsamples.googlecode.com/hg/sprites/readme.html

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 Member


Medals: 4



« Reply #5 - Posted 2014-02-25 02: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
1  
color.a = gl_FragCoord.z;


and while rendering that FBO to the screen

1  
2  
gl_FragDepth = color.a;
color.a = 1.0;



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

before doing anything.

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


Also
Quote
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 02:56:21 »

gl_FragDepth is really on supported in GLES3, or in GLES2 through an extension:
http://www.khronos.org/registry/gles/extensions/EXT/EXT_frag_depth.txt

Most desktops running WebGL should support this extension, though.

Offline Kerai

Junior Member


Medals: 4



« Reply #7 - Posted 2014-02-25 03: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  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

E.R. Fleming (35 views)
2014-07-29 03:07:13

E.R. Fleming (13 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36

Riven (44 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54
java-gaming.org 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‑gaming.org
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!