Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
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  
  Fast sRGB conversion GLSL snippet  (Read 9480 times)
0 Members and 1 Guest are viewing this topic.
Offline theagentd
« Posted 2016-07-11 01:28:06 »

EDIT: I've updated the code below with a new version using ternary operators that's ~25% faster. The ternary operators compile to conditional assignment instructions that are faster than step+mix. The old slower version can be found here for reference: http://www.java-gaming.org/?action=pastebin&id=1467

Hey, this goes kind of hand-in-hand with my gamma correction post. Here's a small snippet to manually calculate sRGB colors, which is useful in some cases if you want to do the sRGB conversion manually.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
vec3 srgbEncode(vec3 color){
   float r = color.r < 0.0031308 ? 12.92 * color.r : 1.055 * pow(color.r, 1.0/2.4) - 0.055;
   float g = color.g < 0.0031308 ? 12.92 * color.g : 1.055 * pow(color.g, 1.0/2.4) - 0.055;
   float b = color.b < 0.0031308 ? 12.92 * color.b : 1.055 * pow(color.b, 1.0/2.4) - 0.055;
   return vec3(r, g, b);
}

vec3 srgbDecode(vec3 color){
   float r = color.r < 0.04045 ? (1.0 / 12.92) * color.r : pow((color.r + 0.055) * (1.0 / 1.055), 2.4);
   float g = color.g < 0.04045 ? (1.0 / 12.92) * color.g : pow((color.g + 0.055) * (1.0 / 1.055), 2.4);
   float b = color.b < 0.04045 ? (1.0 / 12.92) * color.b : pow((color.b + 0.055) * (1.0 / 1.055), 2.4);
   return vec3(r, g, b);
}


There are cases where you would want to do the conversion yourself. For example, if you want to do good dithering, you need to do it in sRGB space, or the noise you add will get distorted by the sRGB encoding. Ideally, dithering shouldn't add any noise at all to a completely black screen, but if you dither in linear space and then convert to sRGB you'll get values as high as 5-6 randomly popping up. So, the correct approach is to convert to sRGB space, THEN dither (with GL_FRAMEBUFFER_SRGB disabled since we did that part ourselves).
1  
2  
   color = srgbEncode(color);
   color = clamp(color + (rand(texCoords) - 0.5) * (1 / 255.0), 0.0, 1.0);


Now, let's say we're rendering to a GL_SRGB8 texture with GL_FRAMEBUFFER_SRGB on so that we get correct blending in linear space, and we realize that we need dithering to reduce banding (even with gamma correction/sRGB you may get subtle banding). In that case, we can't encode to sRGB in the shader and disable GL_FRAMEBUFFER_SRGB since we want the gamma correct blending from GL_FRAMEBUFFER_SRGB, so we have to do something pretty ridiculous to get the correct result. We need to dither in sRGB space, but output linear colors for blending, so...
1  
2  
3  
   color = srgbEncode(color);
   color = clamp(color + (rand(texCoords) - 0.5) * (1 / 255.0), 0.0, 1.0);
   color = srgbDecode(color);


Yup, we convert to sRGB, dither, then convert back to linear space again. And that's an example of why these two functions are useful! The performance of these two functions is very good, as ternary operators are compiled to conditional assignment instructions without any branches required.

Myomyomyo.
Offline Archive
« Reply #1 - Posted 2016-07-11 03:44:48 »

So a few questions, what does the 'step' function do, what is the 'mix' function, and does the vec3 constructor vec3(n) return a vector (n, n, n)?

Offline orange451

JGO Kernel


Medals: 346
Projects: 6
Exp: 6 years


Your face. Your ass. What's the difference?


« Reply #2 - Posted 2016-07-11 04:27:01 »

So a few questions, what does the 'step' function do, what is the 'mix' function, and does the vec3 constructor vec3(n) return a vector (n, n, n)?
1) step() https://www.opengl.org/sdk/docs/man/html/step.xhtml
2) mix() https://www.opengl.org/sdk/docs/man/html/mix.xhtml
3) Yes it does! Smiley

First Recon. A java made online first person shooter!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline theagentd
« Reply #3 - Posted 2016-07-11 14:31:50 »

I found that a simple ternary operator had very noticeably higher performance than mix+step, so I have updated the code above. Note that each vector component had to be calculated individually as GLSL doesn't allow (vec3 < vec3) comparisons, only (float < float).

Myomyomyo.
Offline KaiHH

JGO Kernel


Medals: 472



« Reply #4 - Posted 2016-07-11 14:59:58 »

GLSL doesn't allow (vec3 < vec3) comparisons, only (float < float).
It does with the built-in function lessThan, just not with the infix operator.
Offline Roquen

JGO Kernel


Medals: 516



« Reply #5 - Posted 2016-07-18 14:14:49 »

And for setting-up photoshop properly: https://docs.google.com/presentation/d/1vS5nAAnXbZ8Pt6-dXxy-xEXDkjfjXTmNnthNGccorl0/edit#slide=id.g14b3e28660_0_8
Pages: [1]
  ignore  |  Print  
 
 

 
Ecumene (108 views)
2017-09-30 02:57:34

theagentd (135 views)
2017-09-26 18:23:31

cybrmynd (245 views)
2017-08-02 12:28:51

cybrmynd (240 views)
2017-08-02 12:19:43

cybrmynd (238 views)
2017-08-02 12:18:09

Sralse (252 views)
2017-07-25 17:13:48

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

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

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

theagentd (1373 views)
2017-03-24 15:32:08
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
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!