Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions hxsl/Ast.hx
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ enum TGlobal {
InstanceID;
// gl globals
FragCoord;
FragDepth;
FrontFacing;
// bit casting
FloatBitsToInt;
Expand Down Expand Up @@ -522,6 +523,8 @@ class Tools {
return hasSideEffect(it) || hasSideEffect(loop);
case TArray(e, index):
return hasSideEffect(e) || hasSideEffect(index);
case TGlobal(FragDepth):
return true;
case TConst(_), TVar(_), TGlobal(_):
return false;
case TCall({ e : TGlobal(SetLayout) },_):
Expand Down
5 changes: 4 additions & 1 deletion hxsl/Checker.hx
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ class Checker {
];
case ImageStore:
[];
case VertexID, InstanceID, FragCoord, FrontFacing:
case VertexID, InstanceID, FragCoord, FrontFacing,FragDepth:
null;
case AtomicAdd:
[{ args : [{ name : "buf", type : TBuffer(TInt, SConst(0), RW) },{ name : "index", type : TInt }, { name : "data", type : TInt }], ret : TInt }];
Expand Down Expand Up @@ -225,6 +225,7 @@ class Checker {
globals.set("vertexID", { t : TInt, g : VertexID });
globals.set("instanceID", { t : TInt, g : InstanceID });
globals.set("fragCoord", { t : vec4, g : FragCoord });
globals.set("fragDepth", { t : TFloat, g : FragDepth });
globals.set("frontFacing", { t : TBool, g : FrontFacing });
for( gname => vl in gvars )
globals.set(gname, { t : TStruct([
Expand Down Expand Up @@ -418,6 +419,8 @@ class Checker {
case TArray(e, _):
checkWrite(e);
return;
case TGlobal(FragDepth):
return;
default:
}
error("This expression cannot be assigned", e.p);
Expand Down
24 changes: 24 additions & 0 deletions hxsl/Dce.hx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class Dce {
var used : Map<Int,VarDeps>;
var channelVars : Array<TVar>;
var markAsKeep : Bool;
var fragDepthId = Tools.allocVarId();

public function new() {
}
Expand Down Expand Up @@ -202,6 +203,27 @@ class Dce {
link(v, writeTo);
case TSwiz({ e : TVar(v) }, swiz):
link(v, writeTo, swizBits(swiz));
case TBinop(op, { e : TGlobal(FragDepth) }, e2 ):
var v:TVar = {
id: fragDepthId,
name: "FragDepth",
type: TFloat,
kind: Global,
};
var v = get(v);
switch(op) {
// Last assign will always clear all other dependencies
case OpAssign:
v.adeps = [];
v.deps.clear();
case OpAssignOp(_):
default:
return;
}
v.keep = 15;
writeTo.push(v, 15);
check(e2, writeTo, isAffected);
writeTo.pop();
case TBinop(OpAssign | OpAssignOp(_), { e : TVar(v) }, e):
var v = get(v);
writeTo.push(v,15);
Expand Down Expand Up @@ -296,6 +318,8 @@ class Dce {
count++;
}
return { e : TBlock(out), p : e.p, t : e.t };
case TBinop(OpAssign | OpAssignOp(_), {e: TGlobal(FragDepth) },{e: TVar(v) }) if(get(v).used == 0):
return { e : TConst(CNull), t : e.t, p : e.p };
case TVarDecl(v,_) | TBinop(OpAssign | OpAssignOp(_), { e : (TVar(v) | TSwiz( { e : TVar(v) }, _) | TArray( { e : TVar(v) }, _)) }, _) if( get(v).used == 0 ):
return { e : TConst(CNull), t : e.t, p : e.p };
case TBinop(OpAssign | OpAssignOp(_), { e : TSwiz( { e : TVar(v) }, swiz) }, _) if( get(v).used & swizBits(swiz) == 0 ):
Expand Down
1 change: 1 addition & 0 deletions hxsl/GlslOut.hx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class GlslOut {
set(BVec3, "bvec3");
set(BVec4, "bvec4");
set(FragCoord, "gl_FragCoord");
set(FragDepth, "gl_FragDepth");
set(FrontFacing, "gl_FrontFacing");
set(FloatBitsToUint, "_floatBitsToUint");
set(UintBitsToFloat, "_uintBitsToFloat");
Expand Down
20 changes: 19 additions & 1 deletion hxsl/Linker.hx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ private class ShaderInfos {
public var vertex : Null<Bool>;
public var onStack : Bool;
public var hasDiscard : Bool;
public var hasFragDepth : Bool;
public var isCompute : Bool;
public var marked : Null<Bool>;
public function new(n, v) {
Expand Down Expand Up @@ -201,6 +202,23 @@ class Linker {
return { e : TVar(v.v), t : v.v.type, p : e.p };
case TBinop(op, e1, e2):
switch( [op, e1.e] ) {
case [OpAssign | OpAssignOp(_), TGlobal(FragDepth)]:
if( curShader != null ) {
curShader.hasFragDepth = true;
}

var e2 = mapExprVar(e2);
switch(e2.e) {
case TVar(v2):
var v2 = allocVar(v2,e2.p);
if( !curShader.readMap.exists(v2.id) ) {
curShader.readMap.set(v2.id, v2);
curShader.readVars.push(v2);
}
default:
}

return { e : TBinop(op, { e : TGlobal(FragDepth),t : TFloat, p : e.p }, e2), t : e.t, p : e.p };
case [OpAssign, TVar(v)] if( !locals.exists(v.id) ):
var e2 = mapExprVar(e2);
var v = allocVar(v, e1.p);
Expand Down Expand Up @@ -414,7 +432,7 @@ class Linker {

// force shaders containing discard to be included
for( s in shaders )
if( s.hasDiscard || s.isCompute ) {
if( s.hasDiscard || s.isCompute || s.hasFragDepth) {
initDependencies(s);
entry.deps.set(s, true);
}
Expand Down