Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (727)
Games in Android Showcase (217)
games submitted by our members
Games in WIP (796)
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  
  Switching Shaders - Painter's Algorithm  (Read 420 times)
0 Members and 1 Guest are viewing this topic.
Offline Gjallar

JGO Knight

Medals: 37
Projects: 1

Follower of Nurgle

« Posted 2017-05-10 22:23:36 »

Hello there,

I'm using libGdx but feel free to answer generally.

Let's say you have a 2D game where sprites are sorted by their y-coordinate, so sprites with a lower y-coordinate are "in the foreground" and get rendered over sprites with a higher y-coordinate. (assuming the y-coordinate is pointing upward)

Now let's say i want to render trees in two steps, first i render the stump and then the leaves on top of it with both of these sprites having their separate shaders (let's call it stumpShader and leaveShader). Now in a 3D environment this wouldn't be an issue: First i bind the stumpShader and render all the stumps, and then i bind the leaveShader and render all the leaves. Because of these neat thing called z-buffer i don't need to worry about my trees being rendered in a wrong "order".

But in a 2D environment where i just "paint" trees over other trees i do have to worry about that, because if i render all the stumps first, and then the leaves on the stumps, some leaves will be rendered over stumps that they should actually be behind of. So that way i'm forced to render stump, leaves, stump, leaves, stump, leaves.... and constantly switch between stumpShader and leaveShader.

As far as i know switching shaders is pretty costly and you should do it as sparingly as possible, so this is obviously not an option.

So my question is what do i do in this scenario?
Can i somehow utilize the z-buffer in this kind of 2D setup?
I guess i could write one shader that takes care of both things, but I've also heard that if statements should be avoided in glsl.

Offline Archive
« Reply #1 - Posted 2017-05-11 01:13:07 »

Orthographic projections have a z-coordinate, if you give trees Z values based on their rendering order, you can use the z-buffer. (not 100% sure)

Offline theagentd

« JGO Bitwise Duke »

Medals: 970
Projects: 4
Exp: 8 years

« Reply #2 - Posted 2017-05-11 13:26:55 »

 - Orthographic projection matrices still calculate a depth value, which is configured using the near and far values when setting up your orthographic matrix.

 - Z-buffers do not work well with transparency, as a semi-transparent sprite will update the depth buffer, preventing things from being drawn behind it. If you're OK with binary alpha (either fully opaque or fully transparent), you can use alpha testing / discard; in your shader to prevent depth writes for the fully transparent parts (this is a very common technique used for foliage in 3D games).

 - If-statements are not slow per se. They're only slow if 1. the shader cores in a group take different branches, as that forces all cores to execute all branches, and/or 2. the worst path of your shader uses a lot of registers, as the GPU needs to allocate registers for the worst case branch regardless of if that branch executes or not (this is very rarely a problem). If you have if-statements where all pixels of a triangle take the same path, the overhead is essentially zero. In addition, 2D games are often CPU limited as they often need to micromanage the sprites, so getting rid of CPU work at the cost of GPU work is often a net win as the GPU was sitting idle for most of the time anyway.

 - If you can draw all stumps first, then all leaves, then you only have two shader switches regardless of the number of trees you have, in which case the number of shader switches is constant. In that case, 2 switches instead of 1 is insignificant.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Gjallar

JGO Knight

Medals: 37
Projects: 1

Follower of Nurgle

« Reply #3 - Posted 2017-05-11 21:35:49 »

thank you! very insightful
Pages: [1]
  ignore  |  Print  

Archive (299 views)
2017-04-27 17:45:51

buddyBro (486 views)
2017-04-05 03:38:00

CopyableCougar4 (933 views)
2017-03-24 15:39:42

theagentd (946 views)
2017-03-24 15:32:08

Rule (955 views)
2017-03-19 12:43:22

Rule (924 views)
2017-03-19 12:42:17

Rule (925 views)
2017-03-19 12:36:21

theagentd (988 views)
2017-03-16 05:07:07

theagentd (903 views)
2017-03-15 22:37:06

theagentd (696 views)
2017-03-15 22:32:18
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51 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!