浏览代码

added pack/unpack to hxsl (abstract float<->color encoding)

Nicolas Cannasse 11 年之前
父节点
当前提交
57d5812b08
共有 5 个文件被更改,包括 13 次插入4 次删除
  1. 1 2
      h3d/shader/BaseMesh.hx
  2. 1 1
      h3d/shader/Shadow.hx
  3. 2 0
      hxsl/Ast.hx
  4. 5 1
      hxsl/Checker.hx
  5. 4 0
      hxsl/GlslOut.hx

+ 1 - 2
h3d/shader/BaseMesh.hx

@@ -57,8 +57,7 @@ class BaseMesh extends hxsl.Shader {
 		
 		function fragment() {
 			output.color = pixelColor;
-			var dcolor = (depth * vec4(1,255,255.*255.,255.*255.*255.)).fract();
-			output.distance = dcolor - dcolor.yzww * vec4(1. / 255., 1. / 255., 1. / 255., 0.);
+			output.distance = pack(depth);
 		}
 
 	};

+ 1 - 1
h3d/shader/Shadow.hx

@@ -19,7 +19,7 @@ class Shadow extends hxsl.Shader {
 		}
 		
 		function fragment() {
-			var depth = shadow.map.get(shadowPos.xy).dot(vec4(1., 1. / 255., 1. / (255. * 255.), 1. / (255. * 255. * 255.)));
+			var depth = unpack(shadow.map.get(shadowPos.xy));
 			var shade = exp( shadow.power * (depth - shadowPos.z + shadow.bias) ).clamp(0.,1.);
 			pixelColor.rgb *= (1. - shade) * shadow.color.rgb + shade;
 		}

+ 2 - 0
hxsl/Ast.hx

@@ -205,6 +205,8 @@ enum TGlobal {
 	// extra (not in GLSL ES)
 	Mat3x4;
 	Saturate;
+	Pack;
+	Unpack;
 	// extensions
 	DFdx;
 	DFdy;

+ 5 - 1
hxsl/Checker.hx

@@ -99,6 +99,10 @@ class Checker {
 				r;
 			case DFdx, DFdy, Fwidth:
 				genFloat;
+			case Pack:
+				[ { args : [ { name : "value", type : TFloat } ], ret : TVec(4, VFloat) } ];
+			case Unpack:
+				[ { args : [ { name : "value", type : TVec(4, VFloat) } ], ret : TFloat } ];
 			}
 			if( def != null )
 				globals.set(g.toString(), { t : TFun(def), g : g } );
@@ -672,7 +676,7 @@ class Checker {
 		case TBool: stype = VBool; 1;
 		case TVec(size, t): stype = t; size;
 		case TBytes(size): stype = VInt; size;
-		default: 0;
+		default: stype = null; 0;
 		}
 		if( ncomps > 0 && f.length <= 4 ) {
 			var str = "xrsygtzbpwaq";

+ 4 - 0
hxsl/GlslOut.hx

@@ -168,6 +168,10 @@ class GlslOut {
 				decl("vec3 m3x4mult( vec3 v, mat3x4 m) { vec4 ve = vec4(v,1.0); return vec3(dot(m.a,ve),dot(m.b,ve),dot(m.c,ve)); }");
 			case DFdx, DFdy, Fwidth:
 				decl("#extension GL_OES_standard_derivatives:enable");
+			case Pack:
+				decl("vec4 pack( float v ) { vec4 color = fract(v * vec4(1, 255, 255.*255., 255.*255.*255.)); return color - color.yzww * vec4(1. / 255., 1. / 255., 1. / 255., 0.); }");
+			case Unpack:
+				decl("float unpack( vec4 color ) { return dot(color,vec4(1., 1. / 255., 1. / (255. * 255.), 1. / (255. * 255. * 255.))); }");
 			default:
 			}
 			add(globalNames.get(g));