Prechádzať zdrojové kódy

Depth bias and depth clamp refacto :
- Removed depth bias and depth clamp from textures.
- Added method to set depth bias and depth clamp globally from Engine and Driver.
- Added depth clamp in Pass.
- Fixed culled in Pass being stored in bits instead of in flags.

TothBenoit 2 dní pred
rodič
commit
e1bd978e44

+ 3 - 3
h2d/ObjectFollower.hx

@@ -195,16 +195,16 @@ class ObjectFollower extends Object {
 			var camera = scene.getRenderCamera();
 			var camera = scene.getRenderCamera();
 			var wantedMode : h3d.mat.Data.Compare = camera.reverseDepth ? GreaterEqual : LessEqual;
 			var wantedMode : h3d.mat.Data.Compare = camera.reverseDepth ? GreaterEqual : LessEqual;
 			var prev = ctx.baseShader.zValue;
 			var prev = ctx.baseShader.zValue;
-			var prevMode = ctx.pass.depthTest, prevWrite = ctx.pass.depthWrite;
+			var prevMode = ctx.pass.depthTest, prevWrite = ctx.pass.depthWrite, prevClamp = ctx.pass.depthClamp;
 			if( prevMode != wantedMode ) {
 			if( prevMode != wantedMode ) {
-				ctx.pass.depth(true, wantedMode);
+				ctx.pass.depth(true, wantedMode, prevClamp);
 				ctx.engine.selectMaterial(ctx.pass);
 				ctx.engine.selectMaterial(ctx.pass);
 			}
 			}
 			ctx.baseShader.zValue = zValue;
 			ctx.baseShader.zValue = zValue;
 			super.drawRec(ctx);
 			super.drawRec(ctx);
 			ctx.baseShader.zValue = prev;
 			ctx.baseShader.zValue = prev;
 			if( prevMode != wantedMode ) {
 			if( prevMode != wantedMode ) {
-				ctx.pass.depth(prevWrite, prevMode);
+				ctx.pass.depth(prevWrite, prevMode, prevClamp);
 				ctx.engine.selectMaterial(ctx.pass);
 				ctx.engine.selectMaterial(ctx.pass);
 			}
 			}
 		}
 		}

+ 6 - 4
h3d/Engine.hx

@@ -436,10 +436,12 @@ class Engine {
 		return true;
 		return true;
 	}
 	}
 
 
-	public function onTextureBiasChanged(t : h3d.mat.Texture) {
-		if ( !t.isDepth() )
-			throw "Can change texture bias on depth buffer only";
-		driver.onTextureBiasChanged(t);
+	public function setDepthClamp( enabled : Bool ) {
+		driver.setDepthClamp(enabled);
+	}
+
+	public function setDepthBias( depthBias : Float, slopeScaledBias : Float ) {
+		driver.setDepthBias( depthBias, slopeScaledBias );
 	}
 	}
 
 
 	public function dispose() {
 	public function dispose() {

+ 13 - 1
h3d/impl/DX12Driver.hx

@@ -413,6 +413,7 @@ class DX12Driver extends h3d.impl.Driver {
 	var currentPipelineState : PipelineState;
 	var currentPipelineState : PipelineState;
 	var lastVertexGlobalBind : Int = -1;
 	var lastVertexGlobalBind : Int = -1;
 	var lastFragmentGlobalBind : Int = -1;
 	var lastFragmentGlobalBind : Int = -1;
+	var useDepthClamp : Bool = false;
 
 
 	public static var INITIAL_RT_COUNT = 1024;
 	public static var INITIAL_RT_COUNT = 1024;
 	public static var INITIAL_SRV_COUNT = 1024;
 	public static var INITIAL_SRV_COUNT = 1024;
@@ -928,6 +929,14 @@ class DX12Driver extends h3d.impl.Driver {
 		pipelineBuilder.setDepth(depthBuffer);
 		pipelineBuilder.setDepth(depthBuffer);
 	}
 	}
 
 
+	override function setDepthClamp( enabled : Bool ) {
+		useDepthClamp = enabled;
+	}
+
+	override function setDepthBias( depthBias : Float, slopeScaledBias : Float ) {
+		pipelineBuilder.setDepthBias(depthBias, slopeScaledBias);
+	}
+
 	override function setRenderZone(x:Int, y:Int, width:Int, height:Int) {
 	override function setRenderZone(x:Int, y:Int, width:Int, height:Int) {
 		if( width < 0 && height < 0 && x == 0 && y == 0 ) {
 		if( width < 0 && height < 0 && x == 0 && y == 0 ) {
 			tmp.rect.left = 0;
 			tmp.rect.left = 0;
@@ -2208,7 +2217,10 @@ class DX12Driver extends h3d.impl.Driver {
 	}
 	}
 
 
 	override function selectMaterial( pass : h3d.mat.Pass ) @:privateAccess {
 	override function selectMaterial( pass : h3d.mat.Pass ) @:privateAccess {
+		var depthClamp = pass.depthClamp;
+		pass.depthClamp = depthClamp || useDepthClamp;
 		pipelineBuilder.selectMaterial(pass);
 		pipelineBuilder.selectMaterial(pass);
+		pass.depthClamp = depthClamp;
 		var st = pass.stencil;
 		var st = pass.stencil;
 		if( st != null && curStencilRef != st.reference ) {
 		if( st != null && curStencilRef != st.reference ) {
 			curStencilRef = st.reference;
 			curStencilRef = st.reference;
@@ -2279,9 +2291,9 @@ class DX12Driver extends h3d.impl.Driver {
 		p.depthStencilDesc.depthEnable = pass.depthTest != Always;
 		p.depthStencilDesc.depthEnable = pass.depthTest != Always;
 		p.depthStencilDesc.depthWriteMask = !pass.depthWrite || !depthEnabled ? ZERO : ALL;
 		p.depthStencilDesc.depthWriteMask = !pass.depthWrite || !depthEnabled ? ZERO : ALL;
 		p.depthStencilDesc.depthFunc = COMP[pass.depthTest.getIndex()];
 		p.depthStencilDesc.depthFunc = COMP[pass.depthTest.getIndex()];
+		p.rasterizerState.depthClipEnable = !pass.depthClamp;
 		p.rasterizerState.depthBias = Std.int(depth.bias);
 		p.rasterizerState.depthBias = Std.int(depth.bias);
 		p.rasterizerState.slopeScaledDepthBias = depth.slopeScaledBias;
 		p.rasterizerState.slopeScaledDepthBias = depth.slopeScaledBias;
-		p.rasterizerState.depthClipEnable = !depth.clamp;
 
 
 		var bl = p.blendState;
 		var bl = p.blendState;
 		for( i in 0...rtCount ) {
 		for( i in 0...rtCount ) {

+ 6 - 3
h3d/impl/Driver.hx

@@ -213,6 +213,12 @@ class Driver {
 	public function setDepth( tex : Null<h3d.mat.Texture> ) {
 	public function setDepth( tex : Null<h3d.mat.Texture> ) {
 	}
 	}
 
 
+	public function setDepthClamp( enabled : Bool ) {
+	}
+
+	public function setDepthBias( depthBias : Float,  slopeScaledBias : Float ) {
+	}
+
 	public function allocDepthBuffer( b : h3d.mat.Texture ) : Texture {
 	public function allocDepthBuffer( b : h3d.mat.Texture ) : Texture {
 		return null;
 		return null;
 	}
 	}
@@ -274,9 +280,6 @@ class Driver {
 	public function readBufferBytes( b : Buffer, startVertex : Int, vertexCount : Int, buf : haxe.io.Bytes, bufPos : Int ) {
 	public function readBufferBytes( b : Buffer, startVertex : Int, vertexCount : Int, buf : haxe.io.Bytes, bufPos : Int ) {
 	}
 	}
 
 
-	public function onTextureBiasChanged( t : h3d.mat.Texture ) {
-	}
-
 	/**
 	/**
 		Returns true if we could copy the texture, false otherwise (not supported by driver or mismatch in size/format)
 		Returns true if we could copy the texture, false otherwise (not supported by driver or mismatch in size/format)
 	**/
 	**/

+ 22 - 21
h3d/impl/GlDriver.hx

@@ -109,6 +109,7 @@ class GlDriver extends Driver {
 	var lastActiveIndex : Int = 0;
 	var lastActiveIndex : Int = 0;
 	var curColorMask = -1;
 	var curColorMask = -1;
 	var currentDivisor : Array<Int> = [for( i in 0...32 ) 0];
 	var currentDivisor : Array<Int> = [for( i in 0...32 ) 0];
+	var useDepthClamp = false;
 
 
 	var bufferWidth : Int;
 	var bufferWidth : Int;
 	var bufferHeight : Int;
 	var bufferHeight : Int;
@@ -893,6 +894,16 @@ class GlDriver extends Driver {
 				gl.depthFunc(COMPARE[cmp]);
 				gl.depthFunc(COMPARE[cmp]);
 			}
 			}
 		}
 		}
+
+		#if !js
+		if ( (!useDepthClamp && diff & Pass.depthClamp_mask != 0) ) {
+			if ( Pass.getDepthClamp(bits) != 0 )
+				gl.enable(GL.DEPTH_CLAMP);
+			else
+				gl.disable(GL.DEPTH_CLAMP);
+		}
+		#end
+
 		curMatBits = bits;
 		curMatBits = bits;
 	}
 	}
 
 
@@ -1794,9 +1805,6 @@ class GlDriver extends Driver {
 		else
 		else
 			gl.framebufferTexture2D(GL.FRAMEBUFFER, GL.COLOR_ATTACHMENT0, tex.flags.has(Cube) ? CUBE_FACES[layer] : GL.TEXTURE_2D, tex.t.t, mipLevel);
 			gl.framebufferTexture2D(GL.FRAMEBUFFER, GL.COLOR_ATTACHMENT0, tex.flags.has(Cube) ? CUBE_FACES[layer] : GL.TEXTURE_2D, tex.t.t, mipLevel);
 
 
-		setPolygonOffset( tex.depthBuffer );
-		setDepthClamp( tex.depthBuffer );
-
 		if( tex.depthBuffer != null && depthBinding != NotBound ) {
 		if( tex.depthBuffer != null && depthBinding != NotBound ) {
 			// Depthbuffer and stencilbuffer are combined in one buffer, created with GL.DEPTH_STENCIL
 			// Depthbuffer and stencilbuffer are combined in one buffer, created with GL.DEPTH_STENCIL
 			if(tex.depthBuffer.hasStencil() && tex.depthBuffer.format == Depth24Stencil8) {
 			if(tex.depthBuffer.hasStencil() && tex.depthBuffer.format == Depth24Stencil8) {
@@ -1874,9 +1882,6 @@ class GlDriver extends Driver {
 
 
 		gl.framebufferTexture2D(GL.FRAMEBUFFER, GL.COLOR_ATTACHMENT0, GL.TEXTURE_2D, null, 0);
 		gl.framebufferTexture2D(GL.FRAMEBUFFER, GL.COLOR_ATTACHMENT0, GL.TEXTURE_2D, null, 0);
 
 
-		setPolygonOffset( depthBuffer );
-		setDepthClamp( depthBuffer );
-
 		if(depthBuffer.hasStencil() && depthBuffer.format == Depth24Stencil8) {
 		if(depthBuffer.hasStencil() && depthBuffer.format == Depth24Stencil8) {
 			gl.framebufferTexture2D(GL.FRAMEBUFFER, GL.DEPTH_STENCIL_ATTACHMENT, GL.TEXTURE_2D,@:privateAccess depthBuffer.t.t, 0);
 			gl.framebufferTexture2D(GL.FRAMEBUFFER, GL.DEPTH_STENCIL_ATTACHMENT, GL.TEXTURE_2D,@:privateAccess depthBuffer.t.t, 0);
 		} else {
 		} else {
@@ -1900,28 +1905,24 @@ class GlDriver extends Driver {
 		#end
 		#end
 	}
 	}
 
 
-	override function onTextureBiasChanged( t : h3d.mat.Texture ) {
-		setPolygonOffset(t);
-	}
-
-	function setPolygonOffset( depthBuffer : h3d.mat.Texture ) {
-		if ( depthBuffer != null && ( depthBuffer.depthBias != 0 || depthBuffer.slopeScaledBias != 0 ) ) {
-			gl.enable(GL.POLYGON_OFFSET_FILL);
-			gl.polygonOffset(depthBuffer.slopeScaledBias, depthBuffer.depthBias);
-		}
-		else
-			gl.disable(GL.POLYGON_OFFSET_FILL);
-	}
-
-	function setDepthClamp( dephTexture : h3d.mat.Texture ) {
+	override function setDepthClamp( enabled : Bool ) {
 		#if !js
 		#if !js
-		if ( dephTexture != null && dephTexture.depthClamp )
+		useDepthClamp = enabled;
+		if ( useDepthClamp )
 			gl.enable(GL.DEPTH_CLAMP);
 			gl.enable(GL.DEPTH_CLAMP);
 		else
 		else
 			gl.disable(GL.DEPTH_CLAMP);
 			gl.disable(GL.DEPTH_CLAMP);
 		#end
 		#end
 	}
 	}
 
 
+	override function setDepthBias( depthBias : Float, slopeScaledBias : Float ) {
+		if ( depthBias != 0 || slopeScaledBias != 0 ) {
+			gl.enable(GL.POLYGON_OFFSET_FILL);
+			gl.polygonOffset(slopeScaledBias, depthBias);
+		} else
+			gl.disable(GL.POLYGON_OFFSET_FILL);
+	}
+
 	override function init( onCreate : Bool -> Void, forceSoftware = false ) {
 	override function init( onCreate : Bool -> Void, forceSoftware = false ) {
 		#if js
 		#if js
 		// wait until all assets have properly load
 		// wait until all assets have properly load

+ 10 - 21
h3d/impl/PipelineCache.hx

@@ -66,7 +66,6 @@ class DepthProps {
 	public var format : hxd.PixelFormat;
 	public var format : hxd.PixelFormat;
 	public var bias : Single;
 	public var bias : Single;
 	public var slopeScaledBias : Single;
 	public var slopeScaledBias : Single;
-	public var clamp : Bool;
 	public function new() {}
 	public function new() {}
 }
 }
 
 
@@ -76,8 +75,7 @@ class PipelineBuilder {
 	static inline var PSIGN_COLOR_MASK = PSIGN_MATID + 4;
 	static inline var PSIGN_COLOR_MASK = PSIGN_MATID + 4;
 	static inline var PSIGN_DEPTH_BIAS = PSIGN_COLOR_MASK + 1;
 	static inline var PSIGN_DEPTH_BIAS = PSIGN_COLOR_MASK + 1;
 	static inline var PSIGN_SLOPE_SCALED_DEPTH_BIAS = PSIGN_DEPTH_BIAS + 4;
 	static inline var PSIGN_SLOPE_SCALED_DEPTH_BIAS = PSIGN_DEPTH_BIAS + 4;
-	static inline var PSIGN_DEPTH_CLAMP = PSIGN_SLOPE_SCALED_DEPTH_BIAS + 4;
-	static inline var PSIGN_STENCIL_MASK = PSIGN_DEPTH_CLAMP + 1;
+	static inline var PSIGN_STENCIL_MASK = PSIGN_SLOPE_SCALED_DEPTH_BIAS + 4;
 	static inline var PSIGN_STENCIL_OPS = PSIGN_STENCIL_MASK + 2;
 	static inline var PSIGN_STENCIL_OPS = PSIGN_STENCIL_MASK + 2;
 	static inline var PSIGN_RENDER_TARGETS = PSIGN_STENCIL_OPS + 4;
 	static inline var PSIGN_RENDER_TARGETS = PSIGN_STENCIL_OPS + 4;
 	static inline var PSIGN_DEPTH_TARGET_FORMAT = PSIGN_RENDER_TARGETS + 8;
 	static inline var PSIGN_DEPTH_TARGET_FORMAT = PSIGN_RENDER_TARGETS + 8;
@@ -131,18 +129,10 @@ class PipelineBuilder {
 		needFlush = sh.mode != Compute;
 		needFlush = sh.mode != Compute;
 	}
 	}
 
 
-	function setDepthProps( depth : h3d.mat.Texture ) {
-		if( depth == null ) {
-			signature.setI32(PSIGN_DEPTH_TARGET_FORMAT,0);
-			signature.setF32(PSIGN_DEPTH_BIAS,0);
-			signature.setF32(PSIGN_SLOPE_SCALED_DEPTH_BIAS,0);
-			signature.setUI8(PSIGN_DEPTH_CLAMP,0);
-		} else {
-			signature.setI32(PSIGN_DEPTH_TARGET_FORMAT, depth.format.getIndex());
-			signature.setF32(PSIGN_DEPTH_BIAS, depth.depthBias);
-			signature.setF32(PSIGN_SLOPE_SCALED_DEPTH_BIAS, depth.slopeScaledBias);
-			signature.setUI8(PSIGN_DEPTH_CLAMP, depth.depthClamp ? 1 : 0);
-		}
+	public function setDepthBias( depthBias : Float, slopeScaledBias : Float  ) {
+		signature.setF32(PSIGN_DEPTH_BIAS, depthBias);
+		signature.setF32(PSIGN_SLOPE_SCALED_DEPTH_BIAS, slopeScaledBias);
+		needFlush = true;
 	}
 	}
 
 
 	static function initFormats() {
 	static function initFormats() {
@@ -157,7 +147,6 @@ class PipelineBuilder {
 		var d = tmpDepth;
 		var d = tmpDepth;
 		d.format = FORMATS[signature.getI32(PSIGN_DEPTH_TARGET_FORMAT)];
 		d.format = FORMATS[signature.getI32(PSIGN_DEPTH_TARGET_FORMAT)];
 		d.bias = signature.getF32(PSIGN_DEPTH_BIAS);
 		d.bias = signature.getF32(PSIGN_DEPTH_BIAS);
-		d.clamp = signature.getUI8(PSIGN_DEPTH_CLAMP) != 0;
 		d.slopeScaledBias = signature.getF32(PSIGN_SLOPE_SCALED_DEPTH_BIAS);
 		d.slopeScaledBias = signature.getF32(PSIGN_SLOPE_SCALED_DEPTH_BIAS);
 		return d;
 		return d;
 	}
 	}
@@ -165,8 +154,8 @@ class PipelineBuilder {
 	public function setRenderTarget( tex : h3d.mat.Texture, depthEnabled : Bool ) {
 	public function setRenderTarget( tex : h3d.mat.Texture, depthEnabled : Bool ) {
 		signature.setI32(PSIGN_RENDER_TARGETS, tex == null ? 0 : getRTBits(tex));
 		signature.setI32(PSIGN_RENDER_TARGETS, tex == null ? 0 : getRTBits(tex));
 		signature.setI32(PSIGN_RENDER_TARGETS + 4, 0);
 		signature.setI32(PSIGN_RENDER_TARGETS + 4, 0);
-		var depth = tex == null || !depthEnabled ? null : tex.depthBuffer;
-		setDepthProps(depth);
+		var format = tex == null || !depthEnabled ? 0 : tex.depthBuffer.format.getIndex();
+		signature.setI32(PSIGN_DEPTH_TARGET_FORMAT, format);
 		needFlush = true;
 		needFlush = true;
 	}
 	}
 
 
@@ -177,7 +166,7 @@ class PipelineBuilder {
 	public function setDepth( depth : h3d.mat.Texture ) {
 	public function setDepth( depth : h3d.mat.Texture ) {
 		signature.setI32(PSIGN_RENDER_TARGETS, 0);
 		signature.setI32(PSIGN_RENDER_TARGETS, 0);
 		signature.setI32(PSIGN_RENDER_TARGETS + 4, 0);
 		signature.setI32(PSIGN_RENDER_TARGETS + 4, 0);
-		setDepthProps(depth);
+		signature.setI32(PSIGN_DEPTH_TARGET_FORMAT, depth.format.getIndex());
 		needFlush = true;
 		needFlush = true;
 	}
 	}
 
 
@@ -188,8 +177,8 @@ class PipelineBuilder {
 		for ( i in textures.length...8)
 		for ( i in textures.length...8)
 			signature.setUI8(PSIGN_RENDER_TARGETS + i, 0);
 			signature.setUI8(PSIGN_RENDER_TARGETS + i, 0);
 		var tex = textures[0];
 		var tex = textures[0];
-		var depth = tex == null || !depthEnabled ? null : tex.depthBuffer;
-		setDepthProps(depth);
+		var format = tex == null || !depthEnabled ? 0 : tex.depthBuffer.format.getIndex();
+		signature.setI32(PSIGN_DEPTH_TARGET_FORMAT, format);
 		needFlush = true;
 		needFlush = true;
 	}
 	}
 
 

+ 6 - 2
h3d/mat/Pass.hx

@@ -33,10 +33,13 @@ class Pass {
 	**/
 	**/
 	@:bits(flags) public var isStatic : Bool;
 	@:bits(flags) public var isStatic : Bool;
 
 
+	@:bits(flags) public var culled : Bool;
+
 	@:bits(flags) var batchMode : Bool; // for MeshBatch
 	@:bits(flags) var batchMode : Bool; // for MeshBatch
 
 
 	@:bits(bits) public var culling : Face;
 	@:bits(bits) public var culling : Face;
 	@:bits(bits) public var depthWrite : Bool;
 	@:bits(bits) public var depthWrite : Bool;
+	@:bits(bits) public var depthClamp : Bool;
 	@:bits(bits) public var depthTest : Compare;
 	@:bits(bits) public var depthTest : Compare;
 	@:bits(bits) public var blendSrc : Blend;
 	@:bits(bits) public var blendSrc : Blend;
 	@:bits(bits) public var blendDst : Blend;
 	@:bits(bits) public var blendDst : Blend;
@@ -45,7 +48,6 @@ class Pass {
 	@:bits(bits) public var blendOp : Operation;
 	@:bits(bits) public var blendOp : Operation;
 	@:bits(bits) public var blendAlphaOp : Operation;
 	@:bits(bits) public var blendAlphaOp : Operation;
 	@:bits(bits) public var wireframe : Bool;
 	@:bits(bits) public var wireframe : Bool;
-	@:bits(bits) public var culled : Bool;
 	public var colorMask : Int;
 	public var colorMask : Int;
 	public var layer : Int = 0;
 	public var layer : Int = 0;
 
 
@@ -73,6 +75,7 @@ class Pass {
 		dynamicParameters = p.dynamicParameters;
 		dynamicParameters = p.dynamicParameters;
 		culling = p.culling;
 		culling = p.culling;
 		depthWrite = p.depthWrite;
 		depthWrite = p.depthWrite;
+		depthClamp = p.depthClamp;
 		depthTest = p.depthTest;
 		depthTest = p.depthTest;
 		blendSrc = p.blendSrc;
 		blendSrc = p.blendSrc;
 		blendDst = p.blendDst;
 		blendDst = p.blendDst;
@@ -141,9 +144,10 @@ class Pass {
 		}
 		}
 	}
 	}
 
 
-	public function depth( write, test ) {
+	public function depth( write, test, clamp = false) {
 		this.depthWrite = write;
 		this.depthWrite = write;
 		this.depthTest = test;
 		this.depthTest = test;
+		this.depthClamp = clamp;
 	}
 	}
 
 
 	public function setColorMask(r, g, b, a) {
 	public function setColorMask(r, g, b, a) {

+ 0 - 19
h3d/mat/Texture.hx

@@ -40,9 +40,6 @@ class Texture {
 	public var startingMip : Int = 0;
 	public var startingMip : Int = 0;
 	public var lodBias : Float = 0.;
 	public var lodBias : Float = 0.;
 	public var mipLevels(get, never) : Int;
 	public var mipLevels(get, never) : Int;
-	public var depthBias(default, set) : Float = 0.;
-	public var slopeScaledBias(default, set) : Float = 0.;
-	public var depthClamp : Bool = false;
 	var customMipLevels : Int;
 	var customMipLevels : Int;
 
 
 	/**
 	/**
@@ -201,22 +198,6 @@ class Texture {
 		name = n;
 		name = n;
 	}
 	}
 
 
-	public function set_depthBias(v : Float) {
-		if ( v != depthBias ) {
-			depthBias = v;
-			h3d.Engine.getCurrent().onTextureBiasChanged(this);
-		}
-		return depthBias;
-	}
-
-	public function set_slopeScaledBias(v : Float) {
-		if ( v != slopeScaledBias ) {
-			slopeScaledBias = v;
-			h3d.Engine.getCurrent().onTextureBiasChanged(this);
-		}
-		return slopeScaledBias;
-	}
-
 	function set_mipMap(m:MipMap) {
 	function set_mipMap(m:MipMap) {
 		bits = (bits & ~(3 << 0)) | (Type.enumIndex(m) << 0);
 		bits = (bits & ~(3 << 0)) | (Type.enumIndex(m) << 0);
 		return mipMap = m;
 		return mipMap = m;

+ 4 - 2
h3d/pass/CascadeShadowMap.hx

@@ -300,6 +300,7 @@ class CascadeShadowMap extends DirShadowMap {
 		var prevCheckNearFar = lightCamera.frustum.checkNearFar;
 		var prevCheckNearFar = lightCamera.frustum.checkNearFar;
 		lightCamera.frustum.checkNearFar = false;
 		lightCamera.frustum.checkNearFar = false;
 		var textures = [];
 		var textures = [];
+		ctx.engine.setDepthClamp(true);
 		for (i in 0...cascade) {
 		for (i in 0...cascade) {
 			currentCascadeIndex = i;
 			currentCascadeIndex = i;
 
 
@@ -309,8 +310,8 @@ class CascadeShadowMap extends DirShadowMap {
 			texture.filter = Nearest;
 			texture.filter = Nearest;
 
 
 			var param = params[i];
 			var param = params[i];
-			texture.slopeScaledBias = (param != null) ? param.slopeBias : 0;
-			texture.depthClamp = true;
+			var slopeScaledBias = (param != null) ? param.slopeBias : 0;
+			ctx.engine.setDepthBias(0, slopeScaledBias);
 
 
 			var lc = lightCameras[i];
 			var lc = lightCameras[i];
 			var dimension = Math.max(lc.orthoBounds.xMax - lc.orthoBounds.xMin,	lc.orthoBounds.yMax - lc.orthoBounds.yMin);
 			var dimension = Math.max(lc.orthoBounds.xMax - lc.orthoBounds.xMin,	lc.orthoBounds.yMax - lc.orthoBounds.yMin);
@@ -321,6 +322,7 @@ class CascadeShadowMap extends DirShadowMap {
 			textures[i] = processShadowMap( passes, texture, sort);
 			textures[i] = processShadowMap( passes, texture, sort);
 			passes.load(p);
 			passes.load(p);
 		}
 		}
+		ctx.engine.setDepthClamp(false);
 		syncCascadeShader(textures);
 		syncCascadeShader(textures);
 		lightCamera.frustum.checkNearFar = prevCheckNearFar;
 		lightCamera.frustum.checkNearFar = prevCheckNearFar;