Browse Source

Added floatBitsToInt and associated to HXSL

Clement Espeute 1 year ago
parent
commit
da419d43f5
4 changed files with 40 additions and 1 deletions
  1. 5 0
      hxsl/Ast.hx
  2. 5 0
      hxsl/Checker.hx
  3. 20 0
      hxsl/GlslOut.hx
  4. 10 1
      hxsl/HlslOut.hx

+ 5 - 0
hxsl/Ast.hx

@@ -274,6 +274,11 @@ enum TGlobal {
 	// gl globals
 	// gl globals
 	FragCoord;
 	FragCoord;
 	FrontFacing;
 	FrontFacing;
+	// bit casting
+	FloatBitsToInt;
+	FloatBitsToUint;
+	IntBitsToFloat;
+	UintBitsToFloat;
 }
 }
 
 
 enum Component {
 enum Component {

+ 5 - 0
hxsl/Checker.hx

@@ -43,6 +43,7 @@ class Checker {
 		if( GLOBALS != null ) return GLOBALS;
 		if( GLOBALS != null ) return GLOBALS;
 		var globals = new Map();
 		var globals = new Map();
 		var genType = [TFloat, vec2, vec3, vec4];
 		var genType = [TFloat, vec2, vec3, vec4];
+		var genIType = [TInt, ivec2, ivec3, ivec4];
 		var baseType = [TFloat, TBool, TInt];
 		var baseType = [TFloat, TBool, TInt];
 		var genFloat = [for( t in genType ) { args : [ { name : "value", type : t } ], ret : t } ];
 		var genFloat = [for( t in genType ) { args : [ { name : "value", type : t } ], ret : t } ];
 		var genFloat2 = [for( t in genType ) { args : [ { name : "a", type : t }, { name : "b", type : t } ], ret : t } ];
 		var genFloat2 = [for( t in genType ) { args : [ { name : "a", type : t }, { name : "b", type : t } ], ret : t } ];
@@ -183,6 +184,10 @@ class Checker {
 				[{ args : [{ name : "uv", type : vec2 }], ret : vec2 }];
 				[{ args : [{ name : "uv", type : vec2 }], ret : vec2 }];
 			case Trace:
 			case Trace:
 				[];
 				[];
+			case FloatBitsToInt, FloatBitsToUint:
+				[for( i => t in genType ) { args : [ { name: "x", type: t } ], ret: genIType[i] }];
+			case IntBitsToFloat, UintBitsToFloat:
+				[for( i => t in genType ) { args : [ { name: "x", type: genIType[i] } ], ret: t }];
 			case VertexID, InstanceID, FragCoord, FrontFacing:
 			case VertexID, InstanceID, FragCoord, FrontFacing:
 				null;
 				null;
 			}
 			}

+ 20 - 0
hxsl/GlslOut.hx

@@ -38,6 +38,9 @@ class GlslOut {
 		set(BVec4, "bvec4");
 		set(BVec4, "bvec4");
 		set(FragCoord, "gl_FragCoord");
 		set(FragCoord, "gl_FragCoord");
 		set(FrontFacing, "gl_FrontFacing");
 		set(FrontFacing, "gl_FrontFacing");
+		set(FrontFacing, "gl_FrontFacing");
+		set(FloatBitsToUint, "_floatBitsToUint");
+		set(UintBitsToFloat, "_uintBitsToFloat");
 		for( g in gl )
 		for( g in gl )
 			KWDS.set(g, true);
 			KWDS.set(g, true);
 		gl;
 		gl;
@@ -299,6 +302,23 @@ class GlslOut {
 			decl("vec2 screenToUv( vec2 v ) { return v * vec2(0.5,-0.5) + vec2(0.5,0.5); }");
 			decl("vec2 screenToUv( vec2 v ) { return v * vec2(0.5,-0.5) + vec2(0.5,0.5); }");
 		case UvToScreen:
 		case UvToScreen:
 			decl("vec2 uvToScreen( vec2 v ) { return v * vec2(2.,-2.) + vec2(-1., 1.); }");
 			decl("vec2 uvToScreen( vec2 v ) { return v * vec2(2.,-2.) + vec2(-1., 1.); }");
+		case FloatBitsToInt, IntBitsToFloat:
+			if( version < 330 )
+				decl("#extension GL_ARB_shader_bit_encoding :enable");
+		case FloatBitsToUint:
+			if( version < 330 )
+				decl("#extension GL_ARB_shader_bit_encoding :enable");
+			decl("int _floatBitsToUint( float v) { return int(floatBitsToUint(v)); }");
+			decl("ivec2 _floatBitsToUint( vec2 v ) { return ivec2(floatBitsToUint(v)); }");
+			decl("ivec3 _floatBitsToUint( vec3 v ) { return ivec3(floatBitsToUint(v)); }");
+			decl("ivec4 _floatBitsToUint( vec4 v ) { return ivec4(floatBitsToUint(v)); }");
+		case UintBitsToFloat:
+			if( version < 330 )
+				decl("#extension GL_ARB_shader_bit_encoding :enable");
+			decl("float _uintBitsToFloat( int v ) { return uintBitsToFloat(uint(v)); }");
+			decl("vec2 _uintBitsToFloat( ivec2 v ) { return uintBitsToFloat(uvec2(v)); }");
+			decl("vec3 _uintBitsToFloat( ivec3 v ) { return uintBitsToFloat(uvec3(v)); }");
+			decl("vec4 _uintBitsToFloat( ivec4 v ) { return uintBitsToFloat(uvec4(v)); }");
 		default:
 		default:
 		}
 		}
 		return GLOBALS[g.getIndex()];
 		return GLOBALS[g.getIndex()];

+ 10 - 1
hxsl/HlslOut.hx

@@ -74,12 +74,16 @@ class HlslOut {
 		m.set(InstanceID,"_in.instanceID");
 		m.set(InstanceID,"_in.instanceID");
 		m.set(IVec2, "int2");
 		m.set(IVec2, "int2");
 		m.set(IVec3, "int3");
 		m.set(IVec3, "int3");
-		m.set(IVec4, "int3");
+		m.set(IVec4, "int4");
 		m.set(BVec2, "bool2");
 		m.set(BVec2, "bool2");
 		m.set(BVec3, "bool3");
 		m.set(BVec3, "bool3");
 		m.set(BVec4, "bool4");
 		m.set(BVec4, "bool4");
 		m.set(FragCoord,"_in.__pos__");
 		m.set(FragCoord,"_in.__pos__");
 		m.set(FrontFacing, "_in.isFrontFace");
 		m.set(FrontFacing, "_in.isFrontFace");
+		m.set(FloatBitsToInt, "asint");
+		m.set(FloatBitsToUint, "asuint");
+		m.set(IntBitsToFloat, "asfloat");
+		m.set(UintBitsToFloat, "_uintBitsToFloat");
 		for( g in m )
 		for( g in m )
 			KWDS.set(g, true);
 			KWDS.set(g, true);
 		m;
 		m;
@@ -424,6 +428,11 @@ class HlslOut {
 				decl("float dFdy( float v ) { return ddy(v); }");
 				decl("float dFdy( float v ) { return ddy(v); }");
 				decl("float2 dFdy( float2 v ) { return ddy(v); }");
 				decl("float2 dFdy( float2 v ) { return ddy(v); }");
 				decl("float3 dFdy( float3 v ) { return ddy(v); }");
 				decl("float3 dFdy( float3 v ) { return ddy(v); }");
+			case UintBitsToFloat:
+				decl("float _uintBitsToFloat( int v ) { return asfloat(asuint(v)); }");
+				decl("float2 _uintBitsToFloat( int2 v ) { return asfloat(asuint(v)); }");
+				decl("float3 _uintBitsToFloat( int3 v ) { return asfloat(asuint(v)); }");
+				decl("float4 _uintBitsToFloat( int4 v ) { return asfloat(asuint(v)); }");
 			default:
 			default:
 			}
 			}
 			add(GLOBALS.get(g));
 			add(GLOBALS.get(g));