浏览代码

Factorize Packing

sebavan 6 年之前
父节点
当前提交
a9defdd356

+ 5 - 5
src/Rendering/depthRenderer.ts

@@ -26,7 +26,7 @@ export class DepthRenderer {
     private _depthMap: RenderTargetTexture;
     private _effect: Effect;
     private readonly _storeNoneLinearDepth: boolean;
-    private readonly _supportsFloat: boolean;
+    public readonly isPacked: boolean;
 
     private _cachedDefines: string;
     private _camera: Nullable<Camera>;
@@ -53,7 +53,7 @@ export class DepthRenderer {
     constructor(scene: Scene, type: number = Constants.TEXTURETYPE_FLOAT, camera: Nullable<Camera> = null, storeNoneLinearDepth = false) {
         this._scene = scene;
         this._storeNoneLinearDepth = storeNoneLinearDepth;
-        this._supportsFloat = type !== Constants.TEXTURETYPE_UNSIGNED_BYTE;
+        this.isPacked = type === Constants.TEXTURETYPE_UNSIGNED_BYTE;
 
         DepthRenderer._SceneComponentInitialization(this._scene);
 
@@ -61,7 +61,7 @@ export class DepthRenderer {
         var engine = scene.getEngine();
 
         // Render target
-        var format = this._supportsFloat ? Constants.TEXTUREFORMAT_R : Constants.TEXTUREFORMAT_RGBA;
+        var format = this.isPacked ? Constants.TEXTUREFORMAT_RGBA : Constants.TEXTUREFORMAT_R;
         this._depthMap = new RenderTargetTexture("depthMap", { width: engine.getRenderWidth(), height: engine.getRenderHeight() }, this._scene, false, true, type,
             false, undefined, undefined, undefined, undefined,
             format);
@@ -229,8 +229,8 @@ export class DepthRenderer {
         }
 
         // Float Mode
-        if (this._supportsFloat) {
-            defines.push("#define FLOAT");
+        if (this.isPacked) {
+            defines.push("#define PACKED");
         }
 
         // Get correct effect

+ 16 - 0
src/Shaders/ShadersInclude/packingFunctions.fx

@@ -0,0 +1,16 @@
+vec4 pack(float depth)
+{
+    const vec4 bit_shift = vec4(255.0 * 255.0 * 255.0, 255.0 * 255.0, 255.0, 1.0);
+    const vec4 bit_mask = vec4(0.0, 1.0 / 255.0, 1.0 / 255.0, 1.0 / 255.0);
+
+    vec4 res = fract(depth * bit_shift);
+    res -= res.xxyz * bit_mask;
+
+    return res;
+}
+
+float unpack(vec4 color)
+{
+    const vec4 bit_shift = vec4(1.0 / (255.0 * 255.0 * 255.0), 1.0 / (255.0 * 255.0), 1.0 / 255.0, 1.0);
+    return dot(color, bit_shift);
+}

+ 1 - 5
src/Shaders/ShadersInclude/shadowsFragmentFunctions.fx

@@ -1,10 +1,6 @@
 #ifdef SHADOWS
     #ifndef SHADOWFLOAT
-        float unpack(vec4 color)
-        {
-            const vec4 bit_shift = vec4(1.0 / (255.0 * 255.0 * 255.0), 1.0 / (255.0 * 255.0), 1.0 / 255.0, 1.0);
-            return dot(color, bit_shift);
-        }
+        #include<packingFunctions>
     #endif
 
     float computeFallOff(float value, vec2 clipSpace, float frustumEdgeFalloff)

+ 8 - 17
src/Shaders/depth.fragment.fx

@@ -5,17 +5,8 @@ uniform sampler2D diffuseSampler;
 
 varying float vDepthMetric;
 
-#ifndef FLOAT
-	vec4 pack(float depth)
-	{
-		const vec4 bit_shift = vec4(255.0 * 255.0 * 255.0, 255.0 * 255.0, 255.0, 1.0);
-		const vec4 bit_mask = vec4(0.0, 1.0 / 255.0, 1.0 / 255.0, 1.0 / 255.0);
-
-		vec4 res = fract(depth * bit_shift);
-		res -= res.xxyz * bit_mask;
-
-		return res;
-	}
+#ifdef PACKED
+	#include<packingFunctions>
 #endif
 
 void main(void)
@@ -26,16 +17,16 @@ void main(void)
 #endif
 
 #ifdef NONELINEARDEPTH
-	#ifdef FLOAT
-		gl_FragColor = vec4(gl_FragCoord.z, 0.0, 0.0, 0.0);
-	#else
+	#ifdef PACKED
 		gl_FragColor = pack(gl_FragCoord.z);
+	#else
+		gl_FragColor = vec4(gl_FragCoord.z, 0.0, 0.0, 0.0);
 	#endif
 #else
-	#ifdef FLOAT
-		gl_FragColor = vec4(vDepthMetric, 0.0, 0.0, 1.0);
-	#else
+	#ifdef PACKED
 		gl_FragColor = pack(vDepthMetric);
+	#else
+		gl_FragColor = vec4(vDepthMetric, 0.0, 0.0, 1.0);
 	#endif
 #endif
 }

+ 1 - 16
src/Shaders/kernelBlur.fragment.fx

@@ -23,22 +23,7 @@ varying vec2 sampleCenter;
 #include<kernelBlurVaryingDeclaration>[0..varyingCount]
 
 #ifdef PACKEDFLOAT
-    vec4 pack(float depth)
-    {
-        const vec4 bit_shift = vec4(255.0 * 255.0 * 255.0, 255.0 * 255.0, 255.0, 1.0);
-        const vec4 bit_mask = vec4(0.0, 1.0 / 255.0, 1.0 / 255.0, 1.0 / 255.0);
-
-        vec4 res = fract(depth * bit_shift);
-        res -= res.xxyz * bit_mask;
-
-        return res;
-    }
-
-    float unpack(vec4 color)
-    {
-        const vec4 bit_shift = vec4(1.0 / (255.0 * 255.0 * 255.0), 1.0 / (255.0 * 255.0), 1.0 / 255.0, 1.0);
-        return dot(color, bit_shift);
-    }
+	#include<packingFunctions>
 #endif
 
 void main(void)

+ 1 - 10
src/Shaders/shadowMap.fragment.fx

@@ -1,14 +1,5 @@
 #ifndef FLOAT
-vec4 pack(float depth)
-{
-    const vec4 bit_shift = vec4(255.0 * 255.0 * 255.0, 255.0 * 255.0, 255.0, 1.0);
-    const vec4 bit_mask = vec4(0.0, 1.0 / 255.0, 1.0 / 255.0, 1.0 / 255.0);
-
-    vec4 res = fract(depth * bit_shift);
-    res -= res.xxyz * bit_mask;
-
-    return res;
-}
+	#include<packingFunctions>
 #endif
 
 varying float vDepthMetric;

+ 1 - 9
src/Shaders/standard.fragment.fx

@@ -212,15 +212,7 @@ uniform vec2 dsOffsets[9];
 uniform float halfDestPixelSize;
 
 #ifdef FINAL_DOWN_SAMPLER
-vec4 pack(float value) {
-	const vec4 bit_shift = vec4(255.0 * 255.0 * 255.0, 255.0 * 255.0, 255.0, 1.0);
-	const vec4 bit_mask = vec4(0.0, 1.0 / 255.0, 1.0 / 255.0, 1.0 / 255.0);
-
-	vec4 res = fract(value * bit_shift);
-	res -= res.xxyz * bit_mask;
-
-	return res;
-}
+	#include<packingFunctions>
 #endif
 
 void main()