فهرست منبع

add code to experiment with linear RGB workflow

Alexander Rose 4 سال پیش
والد
کامیت
0662506d35
4فایلهای تغییر یافته به همراه73 افزوده شده و 7 حذف شده
  1. 11 4
      src/mol-gl/shader/chunks/common.glsl.ts
  2. 27 0
      src/mol-gl/webgl/compat.ts
  3. 8 2
      src/mol-gl/webgl/extensions.ts
  4. 27 1
      src/mol-util/color/color.ts

+ 11 - 4
src/mol-gl/shader/chunks/common.glsl.ts

@@ -52,12 +52,19 @@ const vec4 UnpackFactors = UnpackDownscale / vec4(PackFactors, 1.0);
 const float ShiftRight8 = 1.0 / 256.0;
 
 vec4 packDepthToRGBA(const in float v) {
-	vec4 r = vec4(fract(v * PackFactors), v);
-	r.yzw -= r.xyz * ShiftRight8; // tidy overflow
-	return r * PackUpscale;
+    vec4 r = vec4(fract(v * PackFactors), v);
+    r.yzw -= r.xyz * ShiftRight8; // tidy overflow
+    return r * PackUpscale;
 }
 float unpackRGBAToDepth(const in vec4 v) {
-	return dot(v, UnpackFactors);
+    return dot(v, UnpackFactors);
+}
+
+vec4 sRGBToLinear(const in vec4 c) {
+    return vec4(mix(pow(c.rgb * 0.9478672986 + vec3(0.0521327014), vec3(2.4)), c.rgb * 0.0773993808, vec3(lessThanEqual(c.rgb, vec3(0.04045)))), c.a);
+}
+vec4 linearTosRGB(const in vec4 c) {
+    return vec4(mix(pow(c.rgb, vec3(0.41666)) * 1.055 - vec3(0.055), c.rgb * 12.92, vec3(lessThanEqual(c.rgb, vec3(0.0031308)))), c.a);
 }
 
 #if __VERSION__ != 300

+ 27 - 0
src/mol-gl/webgl/compat.ts

@@ -240,4 +240,31 @@ export function getDepthTexture(gl: GLRenderingContext): COMPAT_depth_texture |
             UNSIGNED_INT_24_8: ext.UNSIGNED_INT_24_8_WEBGL
         };
     }
+}
+
+export interface COMPAT_sRGB {
+    readonly FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING: number;
+    readonly SRGB8_ALPHA8: number;
+    readonly SRGB8: number;
+    readonly SRGB: number;
+}
+
+export function getSRGB(gl: GLRenderingContext): COMPAT_sRGB | null {
+    if (isWebGL2(gl)) {
+        return {
+            FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING: gl.FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING,
+            SRGB8_ALPHA8: gl.SRGB8_ALPHA8,
+            SRGB8: gl.SRGB8,
+            SRGB: gl.SRGB
+        };
+    } else {
+        const ext = gl.getExtension('EXT_sRGB');
+        if (ext === null) return null;
+        return {
+            FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING: ext.FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING_EXT,
+            SRGB8_ALPHA8: ext.SRGB8_ALPHA8_EXT,
+            SRGB8: ext.SRGB_ALPHA_EXT,
+            SRGB: ext.SRGB_EXT
+        };
+    }
 }

+ 8 - 2
src/mol-gl/webgl/extensions.ts

@@ -1,10 +1,10 @@
 /**
- * Copyright (c) 2018-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
+ * Copyright (c) 2018-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  *
  * @author Alexander Rose <alexander.rose@weirdbyte.de>
  */
 
-import { GLRenderingContext, COMPAT_instanced_arrays, COMPAT_standard_derivatives, COMPAT_vertex_array_object, getInstancedArrays, getStandardDerivatives, getVertexArrayObject, COMPAT_element_index_uint, getElementIndexUint, COMPAT_texture_float, getTextureFloat, COMPAT_texture_float_linear, getTextureFloatLinear, COMPAT_blend_minmax, getBlendMinMax, getFragDepth, COMPAT_frag_depth, COMPAT_color_buffer_float, getColorBufferFloat, COMPAT_draw_buffers, getDrawBuffers, getShaderTextureLod, COMPAT_shader_texture_lod, getDepthTexture, COMPAT_depth_texture } from './compat';
+import { GLRenderingContext, COMPAT_instanced_arrays, COMPAT_standard_derivatives, COMPAT_vertex_array_object, getInstancedArrays, getStandardDerivatives, getVertexArrayObject, COMPAT_element_index_uint, getElementIndexUint, COMPAT_texture_float, getTextureFloat, COMPAT_texture_float_linear, getTextureFloatLinear, COMPAT_blend_minmax, getBlendMinMax, getFragDepth, COMPAT_frag_depth, COMPAT_color_buffer_float, getColorBufferFloat, COMPAT_draw_buffers, getDrawBuffers, getShaderTextureLod, COMPAT_shader_texture_lod, getDepthTexture, COMPAT_depth_texture, COMPAT_sRGB, getSRGB } from './compat';
 import { isDebugMode } from '../../mol-util/debug';
 
 export type WebGLExtensions = {
@@ -21,6 +21,7 @@ export type WebGLExtensions = {
     colorBufferFloat: COMPAT_color_buffer_float | null
     drawBuffers: COMPAT_draw_buffers | null
     shaderTextureLod: COMPAT_shader_texture_lod | null
+    sRGB: COMPAT_sRGB | null
 }
 
 export function createExtensions(gl: GLRenderingContext): WebGLExtensions {
@@ -79,6 +80,10 @@ export function createExtensions(gl: GLRenderingContext): WebGLExtensions {
     if (isDebugMode && shaderTextureLod === null) {
         console.log('Could not find support for "shader_texture_lod"');
     }
+    const sRGB = getSRGB(gl);
+    if (isDebugMode && sRGB === null) {
+        console.log('Could not find support for "sRGB"');
+    }
 
     return {
         instancedArrays,
@@ -94,5 +99,6 @@ export function createExtensions(gl: GLRenderingContext): WebGLExtensions {
         colorBufferFloat,
         drawBuffers,
         shaderTextureLod,
+        sRGB,
     };
 }

+ 27 - 1
src/mol-util/color/color.ts

@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2018-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
+ * Copyright (c) 2018-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  *
  * @author Alexander Rose <alexander.rose@weirdbyte.de>
  */
@@ -118,6 +118,32 @@ export namespace Color {
     export function lighten(c: Color, amount: number): Color {
         return darken(c, -amount);
     }
+
+    //
+
+    function _sRGBToLinear(c: number) {
+        return (c < 0.04045) ? c * 0.0773993808 : Math.pow(c * 0.9478672986 + 0.0521327014, 2.4);
+    }
+
+    export function sRGBToLinear(c: Color) {
+        return fromNormalizedRgb(
+            _sRGBToLinear((c >> 16 & 255) / 255),
+            _sRGBToLinear((c >> 8 & 255) / 255),
+            _sRGBToLinear((c & 255) / 255)
+        );
+    }
+
+    function _linearToSRGB(c: number) {
+        return (c < 0.0031308) ? c * 12.92 : 1.055 * (Math.pow(c, 0.41666)) - 0.055;
+    }
+
+    export function linearToSRGB(c: Color) {
+        return fromNormalizedRgb(
+            _linearToSRGB((c >> 16 & 255) / 255),
+            _linearToSRGB((c >> 8 & 255) / 255),
+            _linearToSRGB((c & 255) / 255)
+        );
+    }
 }
 
 export interface ColorList {