Browse Source

Added atomicAdd and groupBarrierMemory to hxsl

TothBenoit 1 year ago
parent
commit
bf3f79d652
4 changed files with 23 additions and 4 deletions
  1. 5 1
      hxsl/Ast.hx
  2. 7 3
      hxsl/Checker.hx
  3. 8 0
      hxsl/GlslOut.hx
  4. 3 0
      hxsl/HlslOut.hx

+ 5 - 1
hxsl/Ast.hx

@@ -305,6 +305,8 @@ enum TGlobal {
 	ComputeVar_LocalInvocationIndex;
 	//ComputeVar_NumWorkGroups - no DirectX support
 	//ComputeVar_WorkGroupSize - no DirectX support
+	AtomicAdd;
+	GroupMemoryBarrier;
 }
 
 enum Component {
@@ -520,7 +522,9 @@ class Tools {
 			return true;
 		case TCall(e, pl):
 			switch( e.e ) {
-			case TGlobal(g) if( g != ImageStore ):
+			case TGlobal( ImageStore | AtomicAdd | GroupMemoryBarrier ):
+				return true;
+			case TGlobal(g):
 			default:
 				return true;
 			}

+ 7 - 3
hxsl/Checker.hx

@@ -172,7 +172,7 @@ class Checker {
 				[{ args : [{ name : "screenPos", type : vec2 }], ret : vec2 }];
 			case UvToScreen:
 				[{ args : [{ name : "uv", type : vec2 }], ret : vec2 }];
-			case Trace:
+			case Trace, GroupMemoryBarrier:
 				[];
 			case FloatBitsToInt, FloatBitsToUint:
 				[for( i => t in genType ) { args : [ { name: "x", type: t } ], ret: genIType[i] }];
@@ -188,6 +188,8 @@ class Checker {
 				[];
 			case VertexID, InstanceID, FragCoord, FrontFacing:
 				null;
+			case AtomicAdd:
+				[{ args : [{ name : "buf", type : TBuffer(TInt, SConst(0), RW) },{ name : "index", type : TInt }, { name : "data", type : TInt }], ret : TInt }];
 			case _ if( g.getName().indexOf("_") > 0 ):
 				var name = g.getName();
 				var idx = name.indexOf("_");
@@ -356,7 +358,7 @@ class Checker {
 	}
 
 	function tryUnify( t1 : Type, t2 : Type ) {
-		if( t1 == t2 )
+		if( t1.equals(t2) )
 			return true;
 		switch( [t1, t2] ) {
 		case [TVec(s1, t1), TVec(s2, t2)] if( s1 == s2 && t1 == t2 ):
@@ -707,7 +709,7 @@ class Checker {
 			var e2 = typeExpr(e2, With(TInt));
 			unify(e2.t, TInt, e2.p);
 			switch( e1.t ) {
-			case TArray(t, size), TBuffer(t,size,_):
+			case TArray(t, size), TBuffer(t,size, Uniform), TBuffer(t,size, Partial):
 				switch( [size, e2.e] ) {
 				case [SConst(v), TConst(CInt(i))] if( i >= v ):
 					error("Indexing outside array bounds", e.pos);
@@ -716,6 +718,8 @@ class Checker {
 				default:
 				}
 				type = t;
+			case TBuffer(t, size, _):
+				type = t;
 			case TMat2:
 				type = vec2;
 			case TMat3:

+ 8 - 0
hxsl/GlslOut.hx

@@ -485,6 +485,14 @@ class GlslOut {
 			add("clamp(");
 			addValue(e, tabs);
 			add(", 0., 1.)");
+		case TCall( { e : TGlobal(AtomicAdd) }, args):			
+			add("atomicAdd(");
+			addValue(args[0], tabs);
+			add("[");
+			addValue(args[1], tabs);
+			add("],");
+			addValue(args[2], tabs);
+			add(")");
 		case TCall({ e : TGlobal(g = Texel) }, args):
 			add(getFunName(g,args,e.t));
 			add("(");

+ 3 - 0
hxsl/HlslOut.hx

@@ -82,6 +82,7 @@ class HlslOut {
 		m.set(IntBitsToFloat, "asfloat");
 		m.set(UintBitsToFloat, "_uintBitsToFloat");
 		m.set(RoundEven, "round");
+		m.set(GroupMemoryBarrier, "GroupMemoryBarrier");
 		for( g in m )
 			KWDS.set(g, true);
 		m;
@@ -348,6 +349,8 @@ class HlslOut {
 			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)); }");
+		case AtomicAdd:
+			decl("int atomicAdd( RWStructuredBuffer<int> buf, int index, int data ) { int val; InterlockedAdd(buf[index], data, val); return val; }");
 		case TextureSize:
 			var tt = args[0].t;
 			var tstr = getTexType(tt);