Kaynağa Gözat

Remove DepthBuffer class, it's just a texture now.

clementlandrin 2 yıl önce
ebeveyn
işleme
0238491273

+ 8 - 11
h3d/impl/DX12Driver.hx

@@ -278,9 +278,6 @@ class TextureData extends ResourceData {
 	}
 }
 
-class DepthBufferData extends ResourceData {
-}
-
 class QueryData {
 	public var heap : Int;
 	public var offset : Int;
@@ -330,7 +327,7 @@ class DX12Driver extends h3d.impl.Driver {
 
 	var tmp : TempObjects;
 	var currentRenderTargets : Array<h3d.mat.Texture> = [];
-	var defaultDepth : h3d.mat.DepthBuffer;
+	var defaultDepth : h3d.mat.Texture;
 	var depthEnabled = true;
 	var curStencilRef : Int = -1;
 	var rtWidth : Int;
@@ -384,7 +381,7 @@ class DX12Driver extends h3d.impl.Driver {
 		depthStenciViews = new ManagedHeap(DSV, INITIAL_RT_COUNT);
 		renderTargetViews.onFree = function(prev) frame.toRelease.push(prev);
 		depthStenciViews.onFree = function(prev) frame.toRelease.push(prev);
-		defaultDepth = new h3d.mat.DepthBuffer(0,0, Depth24Stencil8);
+		defaultDepth = new h3d.mat.Texture(0,0, Depth24Stencil8);
 
 		var desc = new CommandSignatureDesc();
 		desc.byteStride = 5 * 4;
@@ -602,14 +599,14 @@ class DX12Driver extends h3d.impl.Driver {
 				throw "Depth size mismatch";
 		}
 		var depthView = depthStenciViews.alloc(1);
-		Driver.createDepthStencilView(tex == null || tex.depthBuffer == defaultDepth ? frame.depthBuffer : @:privateAccess tex.depthBuffer.b.res, null, depthView);
+		Driver.createDepthStencilView(tex == null || tex.depthBuffer == defaultDepth ? frame.depthBuffer : @:privateAccess tex.depthBuffer.t.res, null, depthView);
 		var depths = tmp.depthStencils;
 		depths[0] = depthView;
 		depthEnabled = true;
 		return depths;
 	}
 
-	override function getDefaultDepthBuffer():h3d.mat.DepthBuffer {
+	override function getDefaultDepthBuffer():h3d.mat.Texture {
 		return defaultDepth;
 	}
 
@@ -1222,8 +1219,8 @@ class DX12Driver extends h3d.impl.Driver {
 		return td;
 	}
 
-	override function allocDepthBuffer(b:h3d.mat.DepthBuffer):DepthBuffer {
-		var td = new DepthBufferData();
+	override function allocDepthBuffer(b:h3d.mat.Texture):Texture {
+		var td = new TextureData();
 		var desc = new ResourceDesc();
 		var flags = new haxe.EnumFlags();
 		desc.dimension = TEXTURE2D;
@@ -1249,8 +1246,8 @@ class DX12Driver extends h3d.impl.Driver {
 		t.t = null;
 	}
 
-	override function disposeDepthBuffer(b:h3d.mat.DepthBuffer) {
-		disposeResource(@:privateAccess b.b);
+	override function disposeDepthBuffer(t:h3d.mat.Texture) {
+		disposeResource(@:privateAccess t.t);
 	}
 
 	override function uploadTextureBitmap(t:h3d.mat.Texture, bmp:hxd.BitmapData, mipLevel:Int, side:Int) {

+ 61 - 28
h3d/impl/DirectXDriver.hx

@@ -76,9 +76,9 @@ class DirectXDriver extends h3d.impl.Driver {
 	var hasDeviceError = false;
 
 	var defaultTarget : RenderTargetView;
-	var defaultDepth : DepthBuffer;
-	var defaultDepthInst : h3d.mat.DepthBuffer;
-	var extraDepthInst : h3d.mat.DepthBuffer;
+	var defaultDepth : Texture;
+	var defaultDepthInst : h3d.mat.Texture;
+	var extraDepthInst : h3d.mat.Texture;
 
 	var viewport : hl.BytesAccess<hl.F32> = new hl.Bytes(4 * VIEWPORTS_ELTS);
 	var rects : hl.BytesAccess<Int> = new hl.Bytes(4 * RECTS_ELTS);
@@ -87,7 +87,7 @@ class DirectXDriver extends h3d.impl.Driver {
 	var offsets : Array<Int> = [];
 	var currentShader : CompiledShader;
 	var currentIndex : IndexBuffer;
-	var currentDepth : DepthBuffer;
+	var currentDepth : Texture;
 	var currentLayout : Layout;
 	var currentTargets = new hl.NativeArray<RenderTargetView>(16);
 	var currentTargetResources = new hl.NativeArray<ShaderResourceView>(16);
@@ -184,7 +184,8 @@ class DirectXDriver extends h3d.impl.Driver {
 		shaderVersion = if( version < 10 ) "3_0" else if( version < 11 ) "4_0" else "5_0";
 
 		Driver.iaSetPrimitiveTopology(TriangleList);
-		defaultDepthInst = new h3d.mat.DepthBuffer(-1, -1);
+		defaultDepthInst = new h3d.mat.Texture(-1, -1, Depth24Stencil8);
+		defaultDepthInst.name = "defaultDepth";
 		for( i in 0...VIEWPORTS_ELTS )
 			viewport[i] = 0;
 		for( i in 0...RECTS_ELTS )
@@ -207,6 +208,7 @@ class DirectXDriver extends h3d.impl.Driver {
 
 	override function resize(width:Int, height:Int)  {
 		if( defaultDepth != null ) {
+			defaultDepth.depthView.release();
 			defaultDepth.view.release();
 			defaultDepth.res.release();
 		}
@@ -221,14 +223,29 @@ class DirectXDriver extends h3d.impl.Driver {
 		var depthDesc = new Texture2dDesc();
 		depthDesc.width = width;
 		depthDesc.height = height;
-		depthDesc.format = depthStencilFormat;
-		depthDesc.bind = DepthStencil;
+		depthDesc.format = R24G8_TYPELESS;
+		depthDesc.usage = Default;
+		depthDesc.mipLevels = 1;
+		depthDesc.arraySize = 1;
+		depthDesc.sampleCount = 1;
+		depthDesc.sampleQuality = 0;
+		depthDesc.bind = DepthStencil | ShaderResource;
 		var depth = Driver.createTexture2d(depthDesc);
 		if( depth == null ) throw "Failed to create depthBuffer";
-		var depthView = Driver.createDepthStencilView(depth,depthStencilFormat);
-		defaultDepth = { res : depth, view : depthView };
+		var depthView = Driver.createDepthStencilView(depth,D24_UNORM_S8_UINT, false);
+		var readOnlyDepthView = Driver.createDepthStencilView(depth, D24_UNORM_S8_UINT, true);
+
+		var vdesc = new ShaderResourceViewDesc();
+		vdesc.format = R24_UNORM_X8_TYPELESS;
+		vdesc.dimension = Texture2D;
+		vdesc.arraySize = 1;
+		vdesc.start = 0;
+		vdesc.count = -1;
+		var shaderView = Driver.createShaderResourceView(depth, vdesc);
+
+		defaultDepth = { res : depth, view : shaderView, depthView : depthView, rt : null, mips : 0 };
 		@:privateAccess {
-			defaultDepthInst.b = defaultDepth;
+			defaultDepthInst.t = defaultDepth;
 			defaultDepthInst.width = width;
 			defaultDepthInst.height = height;
 		}
@@ -246,8 +263,8 @@ class DirectXDriver extends h3d.impl.Driver {
 		if( extraDepthInst != null ) @:privateAccess {
 			extraDepthInst.width = width;
 			extraDepthInst.height = height;
-			if( extraDepthInst.b != null ) disposeDepthBuffer(extraDepthInst);
-			extraDepthInst.b = allocDepthBuffer(extraDepthInst);
+			if( extraDepthInst.t != null ) disposeDepthBuffer(extraDepthInst);
+			extraDepthInst.t = allocDepthBuffer(extraDepthInst);
 		}
 	}
 
@@ -273,7 +290,7 @@ class DirectXDriver extends h3d.impl.Driver {
 				Driver.clearColor(currentTargets[i], color.r, color.g, color.b, color.a);
 		}
 		if( currentDepth != null && (depth != null || stencil != null) )
-			Driver.clearDepthStencilView(currentDepth.view, depth, stencil);
+			Driver.clearDepthStencilView(currentDepth.depthView, depth, stencil);
 	}
 
 	override function getDriverName(details:Bool) {
@@ -303,12 +320,13 @@ class DirectXDriver extends h3d.impl.Driver {
 
 	}
 
-	override function getDefaultDepthBuffer():h3d.mat.DepthBuffer {
+	override function getDefaultDepthBuffer():h3d.mat.Texture {
 		if( extraDepthInst == null ) @:privateAccess {
-			extraDepthInst = new h3d.mat.DepthBuffer(0, 0);
+			extraDepthInst = new h3d.mat.Texture(0, 0, Depth24Stencil8);
+			extraDepthInst.name = "extraDepth";
 			extraDepthInst.width = outputWidth;
 			extraDepthInst.height = outputHeight;
-			extraDepthInst.b = allocDepthBuffer(extraDepthInst);
+			extraDepthInst.t = allocDepthBuffer(extraDepthInst);
 		}
 		return extraDepthInst;
 	}
@@ -327,21 +345,36 @@ class DirectXDriver extends h3d.impl.Driver {
 		return { res : res, count : count, bits : bits  };
 	}
 
-	override function allocDepthBuffer( b : h3d.mat.DepthBuffer ) : DepthBuffer {
+	override function allocDepthBuffer( b : h3d.mat.Texture ) : Texture {
 		var depthDesc = new Texture2dDesc();
 		depthDesc.width = b.width;
 		depthDesc.height = b.height;
-		depthDesc.format = D24_UNORM_S8_UINT;
-		depthDesc.bind = DepthStencil;
+		depthDesc.mipLevels = 1;
+		depthDesc.arraySize = 1;
+		depthDesc.format = R24G8_TYPELESS;
+		depthDesc.sampleCount = 1;
+		depthDesc.sampleQuality = 0;
+		depthDesc.usage = Default;
+		depthDesc.bind = DepthStencil | ShaderResource;
 		var depth = Driver.createTexture2d(depthDesc);
 		if( depth == null )
 			return null;
-		return { res : depth, view : Driver.createDepthStencilView(depth,depthDesc.format) };
-	}
-
-	override function disposeDepthBuffer(b:h3d.mat.DepthBuffer) @:privateAccess {
-		var d = b.b;
-		b.b = null;
+		var vdesc = new ShaderResourceViewDesc();
+		vdesc.format = R24_UNORM_X8_TYPELESS;
+		vdesc.dimension = Texture2D;
+		vdesc.arraySize = 1;
+		vdesc.start = 0;
+		vdesc.count = -1;
+		var srv = Driver.createShaderResourceView(depth,vdesc);
+		var depthView = Driver.createDepthStencilView(depth,D24_UNORM_S8_UINT, false);
+		var readOnlyDepthView = Driver.createDepthStencilView(depth, D24_UNORM_S8_UINT, true);
+		return { res : depth, view : srv, depthView : depthView, rt : null, mips : 0 };
+	}
+
+	override function disposeDepthBuffer(b:h3d.mat.Texture) @:privateAccess {
+		var d = b.t;
+		b.t = null;
+		d.depthView.release();
 		d.view.release();
 		d.res.release();
 	}
@@ -924,7 +957,7 @@ class DirectXDriver extends h3d.impl.Driver {
 			currentTargets[0] = defaultTarget;
 			currentTargetResources[0] = null;
 			targetsCount = 1;
-			Driver.omSetRenderTargets(1, currentTargets, currentDepth.view);
+			Driver.omSetRenderTargets(1, currentTargets, currentDepth.depthView);
 			viewport[2] = outputWidth;
 			viewport[3] = outputHeight;
 			viewport[5] = 1.;
@@ -963,7 +996,7 @@ class DirectXDriver extends h3d.impl.Driver {
 		curTexture = textures[0];
 		if( tex.depthBuffer != null && (tex.depthBuffer.width != tex.width || tex.depthBuffer.height != tex.height) )
 			throw "Invalid depth buffer size : does not match render target size";
-		currentDepth = @:privateAccess (tex.depthBuffer == null ? null : tex.depthBuffer.b);
+		currentDepth = @:privateAccess (tex.depthBuffer == null ? null : tex.depthBuffer.t);
 		for( i in 0...textures.length ) {
 			var tex = textures[i];
 			if( tex.t == null ) {
@@ -993,7 +1026,7 @@ class DirectXDriver extends h3d.impl.Driver {
 				Driver.clearColor(rt, 0, 0, 0, 0);
 			}
 		}
-		Driver.omSetRenderTargets(textures.length, currentTargets, currentDepth == null ? null : currentDepth.view);
+		Driver.omSetRenderTargets(textures.length, currentTargets, currentDepth == null ? null : currentDepth.depthView);
 		targetsCount = textures.length;
 
 		var w = tex.width >> mipLevel; if( w == 0 ) w = 1;

+ 4 - 12
h3d/impl/Driver.hx

@@ -4,49 +4,41 @@ package h3d.impl;
 typedef IndexBuffer = {};
 typedef GPUBuffer = {};
 typedef Texture = {};
-typedef DepthBuffer = {};
 typedef Query = {};
 #elseif js
 typedef IndexBuffer = { b : js.html.webgl.Buffer, is32 : Bool };
 typedef GPUBuffer = js.html.webgl.Buffer;
 typedef Texture = { t : js.html.webgl.Texture, width : Int, height : Int, internalFmt : Int, pixelFmt : Int, bits : Int, bias : Float, bind : Int #if multidriver, driver : Driver #end };
-typedef DepthBuffer = { r : js.html.webgl.Renderbuffer #if multidriver, driver : Driver #end };
 typedef Query = {};
 #elseif hlsdl
 typedef IndexBuffer = { b : sdl.GL.Buffer, is32 : Bool };
 typedef GPUBuffer = sdl.GL.Buffer;
 typedef Texture = { t : sdl.GL.Texture, width : Int, height : Int, internalFmt : Int, pixelFmt : Int, bits : Int, bind : Int, bias : Float };
-typedef DepthBuffer = { r : sdl.GL.Renderbuffer };
 typedef Query = { q : sdl.GL.Query, kind : QueryKind };
 #elseif usegl
 typedef IndexBuffer = { b : haxe.GLTypes.Buffer, is32 : Bool };
 typedef GPUBuffer = haxe.GLTypes.Buffer;
 typedef Texture = { t : haxe.GLTypes.Texture, width : Int, height : Int, internalFmt : Int, pixelFmt : Int, bits : Int, bind : Int, bias : Float };
-typedef DepthBuffer = { r : haxe.GLTypes.Renderbuffer };
 typedef Query = { q : haxe.GLTypes.Query, kind : QueryKind };
 #elseif (hldx && dx12)
 typedef IndexBuffer = DX12Driver.IndexBufferData;
 typedef GPUBuffer = DX12Driver.VertexBufferData;
 typedef Texture = h3d.impl.DX12Driver.TextureData;
-typedef DepthBuffer = h3d.impl.DX12Driver.DepthBufferData;
 typedef Query = h3d.impl.DX12Driver.QueryData;
 #elseif hldx
 typedef IndexBuffer = { res : dx.Resource, count : Int, bits : Int };
 typedef GPUBuffer = dx.Resource;
-typedef Texture = { res : dx.Resource, view : dx.Driver.ShaderResourceView, rt : Array<dx.Driver.RenderTargetView>, mips : Int };
-typedef DepthBuffer = { res : dx.Resource, view : dx.Driver.DepthStencilView };
+typedef Texture = { res : dx.Resource, view : dx.Driver.ShaderResourceView, ?depthView : dx.Driver.DepthStencilView, rt : Array<dx.Driver.RenderTargetView>, mips : Int };
 typedef Query = {};
 #elseif usesys
 typedef IndexBuffer = haxe.GraphicsDriver.IndexBuffer;
 typedef GPUBuffer = haxe.GraphicsDriver.GPUBuffer;
 typedef Texture = haxe.GraphicsDriver.Texture;
-typedef DepthBuffer = haxe.GraphicsDriver.DepthBuffer;
 typedef Query = haxe.GraphicsDriver.Query;
 #else
 typedef IndexBuffer = {};
 typedef GPUBuffer = {};
 typedef Texture = {};
-typedef DepthBuffer = {};
 typedef Query = {};
 #end
 
@@ -219,14 +211,14 @@ class Driver {
 	public function setRenderTargets( textures : Array<h3d.mat.Texture> ) {
 	}
 
-	public function allocDepthBuffer( b : h3d.mat.DepthBuffer ) : DepthBuffer {
+	public function allocDepthBuffer( b : h3d.mat.Texture ) : Texture {
 		return null;
 	}
 
-	public function disposeDepthBuffer( b : h3d.mat.DepthBuffer ) {
+	public function disposeDepthBuffer( b : h3d.mat.Texture ) {
 	}
 
-	public function getDefaultDepthBuffer() : h3d.mat.DepthBuffer {
+	public function getDefaultDepthBuffer() : h3d.mat.Texture {
 		return null;
 	}
 

+ 47 - 30
h3d/impl/GlDriver.hx

@@ -572,6 +572,7 @@ class GlDriver extends Driver {
 					var mode = pt.mode;
 					gl.texParameteri(mode, GL.TEXTURE_MAG_FILTER, flags[0]);
 					gl.texParameteri(mode, GL.TEXTURE_MIN_FILTER, flags[1]);
+					gl.texParameteri(mode, GL.TEXTURE_COMPARE_MODE, GL.NONE);
 					var w = TWRAP[wrap];
 					gl.texParameteri(mode, GL.TEXTURE_WRAP_S, w);
 					gl.texParameteri(mode, GL.TEXTURE_WRAP_T, w);
@@ -765,7 +766,7 @@ class GlDriver extends Driver {
 			bits |= GL.DEPTH_BUFFER_BIT;
 		}
 		if( stencil != null ) {
-			// reset stencyl mask when we allow to change it
+			// reset stencil mask when we allow to change it
 			@:privateAccess selectStencilBits(defStencil.opBits, defStencil.maskBits);
 			gl.clearStencil(stencil);
 			bits |= GL.STENCIL_BUFFER_BIT;
@@ -792,7 +793,7 @@ class GlDriver extends Driver {
 			disposeDepthBuffer(defaultDepth);
 			defaultDepth.width = this.bufferWidth;
 			defaultDepth.height = this.bufferHeight;
-			defaultDepth.b = allocDepthBuffer(defaultDepth);
+			defaultDepth.t = allocDepthBuffer(defaultDepth);
 		}
 	}
 
@@ -954,40 +955,54 @@ class GlDriver extends Driver {
 			gl.bindTexture(t.bind, t.t);
 	}
 
-	override function allocDepthBuffer( b : h3d.mat.DepthBuffer ) : DepthBuffer {
-		var r = gl.createRenderbuffer();
-		if( b.format == null )
-			@:privateAccess b.format = #if js (glES >= 3 ? Depth24Stencil8 : Depth16) #else Depth24Stencil8 #end;
-		var format = switch( b.format ) {
-		case Depth16: GL.DEPTH_COMPONENT16;
-		case Depth24 #if js if( glES >= 3 ) #end: GL.DEPTH_COMPONENT24;
-		case Depth24Stencil8: GL.DEPTH_STENCIL;
+	override function allocDepthBuffer( t : h3d.mat.Texture ) : Texture {
+		var tt = gl.createTexture();
+		var tt : Texture = { t : tt, width : t.width, height : t.height, internalFmt : GL.RGBA, pixelFmt : GL.UNSIGNED_BYTE, bits : -1, bind : GL.TEXTURE_2D, bias : 0 #if multidriver, driver : this #end };
+		var fmt = GL.DEPTH_COMPONENT;
+		switch( t.format ) {
+		case Depth16:
+			tt.internalFmt = GL.DEPTH_COMPONENT16;
+		case Depth24 #if js if( glES >= 3 ) #end: tt.internalFmt = GL.DEPTH_COMPONENT;
+		case Depth24Stencil8:
+			tt.internalFmt = GL.DEPTH24_STENCIL8;
+			tt.pixelFmt = GL.UNSIGNED_INT_24_8;
+			fmt = GL.DEPTH_STENCIL;
 		default:
-			throw "Unsupported depth format "+b.format;
+			throw "Unsupported depth format "+	t.format;
 		}
-		gl.bindRenderbuffer(GL.RENDERBUFFER, r);
-		gl.renderbufferStorage(GL.RENDERBUFFER, format, b.width, b.height);
-		gl.bindRenderbuffer(GL.RENDERBUFFER, null);
-		return { r : r #if multidriver, driver : this #end };
+		t.lastFrame = frame;
+		t.flags.unset(WasCleared);
+		gl.bindTexture(tt.bind, tt.t);
+
+		#if (js || (hlsdl >= version("1.12.0")))
+		gl.texParameteri(tt.bind, GL.TEXTURE_MIN_FILTER, GL.NEAREST);
+		gl.texParameteri(tt.bind, GL.TEXTURE_MAG_FILTER, GL.NEAREST);
+		gl.texParameteri(tt.bind, GL.TEXTURE_WRAP_S, GL.CLAMP_TO_EDGE);
+		gl.texParameteri(tt.bind, GL.TEXTURE_WRAP_T, GL.CLAMP_TO_EDGE);
+		#end
+		gl.texImage2D(tt.bind, 0, tt.internalFmt, tt.width, tt.height, 0, fmt, tt.pixelFmt, null);
+
+		restoreBind();
+		return tt;
 	}
 
-	override function disposeDepthBuffer( b : h3d.mat.DepthBuffer ) {
-		@:privateAccess if( b.b != null && b.b.r != null ) {
-			gl.deleteRenderbuffer(b.b.r);
-			b.b = null;
+	override function disposeDepthBuffer( b : h3d.mat.Texture ) {
+		@:privateAccess if( b.t != null && b.t.t != null ) {
+			gl.deleteTexture(b.t.t);
+			b.t = null;
 		}
 	}
 
-	var defaultDepth : h3d.mat.DepthBuffer;
+	var defaultDepth : h3d.mat.Texture;
 
-	override function getDefaultDepthBuffer() : h3d.mat.DepthBuffer {
+	override function getDefaultDepthBuffer() : h3d.mat.Texture {
 		if( defaultDepth != null )
 			return defaultDepth;
-		defaultDepth = new h3d.mat.DepthBuffer(0, 0);
+		defaultDepth = new h3d.mat.Texture(0, 0, Depth24Stencil8);
 		@:privateAccess {
 			defaultDepth.width = this.bufferWidth;
 			defaultDepth.height = this.bufferHeight;
-			defaultDepth.b = allocDepthBuffer(defaultDepth);
+			defaultDepth.t = allocDepthBuffer(defaultDepth);
 		}
 		return defaultDepth;
 	}
@@ -1522,16 +1537,16 @@ class GlDriver extends Driver {
 		if( tex.depthBuffer != null ) {
 			// Depthbuffer and stencilbuffer are combined in one buffer, created with GL.DEPTH_STENCIL
 			if(tex.depthBuffer.hasStencil() && tex.depthBuffer.format == Depth24Stencil8) {
-				gl.framebufferRenderbuffer(GL.FRAMEBUFFER, GL.DEPTH_STENCIL_ATTACHMENT, GL.RENDERBUFFER,@:privateAccess tex.depthBuffer.b.r);
+				gl.framebufferTexture2D(GL.FRAMEBUFFER, GL.DEPTH_STENCIL_ATTACHMENT, GL.TEXTURE_2D,@:privateAccess tex.depthBuffer.t.t, 0);
 			} else {
-				gl.framebufferRenderbuffer(GL.FRAMEBUFFER, GL.DEPTH_STENCIL_ATTACHMENT, GL.RENDERBUFFER,null);
-				gl.framebufferRenderbuffer(GL.FRAMEBUFFER, GL.DEPTH_ATTACHMENT, GL.RENDERBUFFER, @:privateAccess tex.depthBuffer.b.r);
-				gl.framebufferRenderbuffer(GL.FRAMEBUFFER, GL.STENCIL_ATTACHMENT, GL.RENDERBUFFER,tex.depthBuffer.hasStencil() ? @:privateAccess tex.depthBuffer.b.r : null);
+				gl.framebufferTexture2D(GL.FRAMEBUFFER, GL.DEPTH_STENCIL_ATTACHMENT, GL.TEXTURE_2D,null,0);
+				gl.framebufferTexture2D(GL.FRAMEBUFFER, GL.DEPTH_ATTACHMENT, GL.TEXTURE_2D, @:privateAccess tex.depthBuffer.t.t,0);
+				gl.framebufferTexture2D(GL.FRAMEBUFFER, GL.STENCIL_ATTACHMENT, GL.TEXTURE_2D,tex.depthBuffer.hasStencil() ? @:privateAccess tex.depthBuffer.t.t : null,0);
 			}
 		} else {
-			gl.framebufferRenderbuffer(GL.FRAMEBUFFER, GL.DEPTH_STENCIL_ATTACHMENT, GL.RENDERBUFFER,null);
-			gl.framebufferRenderbuffer(GL.FRAMEBUFFER, GL.DEPTH_ATTACHMENT, GL.RENDERBUFFER, null);
-			gl.framebufferRenderbuffer(GL.FRAMEBUFFER, GL.STENCIL_ATTACHMENT, GL.RENDERBUFFER, null);
+			gl.framebufferTexture2D(GL.FRAMEBUFFER, GL.DEPTH_STENCIL_ATTACHMENT, GL.TEXTURE_2D,null,0);
+			gl.framebufferTexture2D(GL.FRAMEBUFFER, GL.DEPTH_ATTACHMENT, GL.TEXTURE_2D, null,0);
+			gl.framebufferTexture2D(GL.FRAMEBUFFER, GL.STENCIL_ATTACHMENT, GL.TEXTURE_2D, null,0);
 		}
 
 		var w = tex.width >> mipLevel; if( w == 0 ) w = 1;
@@ -1613,6 +1628,8 @@ class GlDriver extends Driver {
 			features.set(f,checkFeature(f));
 		if( gl.getExtension("WEBGL_compressed_texture_s3tc") != null )
 			maxCompressedTexturesSupport = 3;
+		if( glES < 3 )
+			gl.getExtension("WEBGL_depth_texture");
 	}
 	function checkFeature( f : Feature ) {
 		return switch( f ) {

+ 7 - 7
h3d/impl/MemoryManager.hx

@@ -12,7 +12,7 @@ class MemoryManager {
 	var buffers : Array<Buffer>;
 	var indexes : Array<Indexes>;
 	var textures : Array<h3d.mat.Texture>;
-	var depths : Array<h3d.mat.DepthBuffer>;
+	var depths : Array<h3d.mat.Texture>;
 
 	var triIndexes16 : Indexes;
 	var quadIndexes16 : Indexes;
@@ -210,12 +210,12 @@ class MemoryManager {
 		texMemory += memSize(t);
 	}
 
-	@:allow(h3d.mat.DepthBuffer.alloc)
-	function allocDepth( b : h3d.mat.DepthBuffer ) {
+	@:allow(h3d.mat.Texture.alloc)
+	function allocDepth( b : h3d.mat.Texture ) {
 		while( true ) {
 			var free = cleanTextures(false);
-			b.b = driver.allocDepthBuffer(b);
-			if( b.b != null ) break;
+			b.t = driver.allocDepthBuffer(b);
+			if( b.t != null ) break;
 
 			if( driver.isDisposed() ) return;
 			while( cleanTextures(false) ) {} // clean all old textures
@@ -226,8 +226,8 @@ class MemoryManager {
 		texMemory += b.width * b.height * 4;
 	}
 
-	@:allow(h3d.mat.DepthBuffer.dispose)
-	function deleteDepth( b : h3d.mat.DepthBuffer ) {
+	@:allow(h3d.mat.Texture.dispose)
+	function deleteDepth( b : h3d.mat.Texture ) {
 		if( !depths.remove(b) ) return;
 		driver.disposeDepthBuffer(b);
 		texMemory -= b.width * b.height * 4;

+ 3 - 3
h3d/impl/Stage3dDriver.hx

@@ -80,7 +80,7 @@ class Stage3dDriver extends Driver {
 	var isStandardMode : Bool;
 	var flashVersion : Float;
 	var tdisposed : Texture;
-	var defaultDepth : h3d.mat.DepthBuffer;
+	var defaultDepth : h3d.mat.Texture;
 	var curColorMask = -1;
 
 	@:allow(h3d.impl.VertexWrapper)
@@ -97,7 +97,7 @@ class Stage3dDriver extends Driver {
 		curSamplerBits = [];
 		curMultiBuffer = [];
 		defStencil = new Stencil();
-		defaultDepth = new h3d.mat.DepthBuffer( -1, -1);
+		defaultDepth = new h3d.mat.Texture( -1, -1, Depth24Stencil8);
 	}
 
 	override function logImpl( str : String ) {
@@ -786,7 +786,7 @@ class Stage3dDriver extends Driver {
 		}
 	}
 
-	override function allocDepthBuffer(b:h3d.mat.DepthBuffer):DepthBuffer {
+	override function allocDepthBuffer(b:h3d.mat.Texture):DepthBuffer {
 		throw "You can't allocate custom depth buffer on this platform.";
 	}
 

+ 2 - 2
h3d/impl/TextureCache.hx

@@ -4,7 +4,7 @@ class TextureCache {
 
 	var cache : Array<h3d.mat.Texture>;
 	var position : Int = 0;
-	var defaultDepthBuffer : h3d.mat.DepthBuffer;
+	var defaultDepthBuffer : h3d.mat.Texture;
 	var ctx : h3d.impl.RenderContext;
 	public var defaultFormat : hxd.PixelFormat;
 
@@ -13,7 +13,7 @@ class TextureCache {
 		cache = [];
 		var engine = h3d.Engine.getCurrent();
 		defaultFormat = h3d.mat.Texture.nativeFormat;
-		defaultDepthBuffer = h3d.mat.DepthBuffer.getDefault();
+		defaultDepthBuffer = h3d.mat.Texture.getDefaultDepth();
 	}
 
 	public inline function get( index = 0 ) {

+ 0 - 59
h3d/mat/DepthBuffer.hx

@@ -1,59 +0,0 @@
-package h3d.mat;
-
-enum DepthFormat {
-	Depth16;
-	Depth24;
-	Depth24Stencil8;
-}
-
-/**
-	Depth buffer are used to store per pixel depth information when rendering a scene (also called Z-buffer)
-**/
-class DepthBuffer {
-
-	@:allow(h3d.impl.MemoryManager)
-	var b : h3d.impl.Driver.DepthBuffer;
-	public var width(default, null) : Int;
-	public var height(default, null) : Int;
-	public var format(default, null) : DepthFormat;
-
-	/**
-		Creates a new depth buffer, it can be attached to one or several render target Texture by setting their `depthBuffer` property.
-	**/
-	public function new( width : Int, height : Int, ?format : DepthFormat ) {
-		this.width = width;
-		this.height = height;
-		this.format = format;
-		if( width > 0 ) alloc();
-	}
-
-	public function hasStencil() {
-		return switch( format ) {
-		case Depth16, Depth24: false;
-		case Depth24Stencil8: true;
-		}
-	}
-
-	function alloc() {
-		h3d.Engine.getCurrent().mem.allocDepth(this);
-	}
-
-	public function dispose() {
-		if( b != null ) {
-			h3d.Engine.getCurrent().mem.deleteDepth(this);
-			b = null;
-		}
-	}
-
-	public function isDisposed() {
-		return b == null;
-	}
-
-	/**
-		This will return the default depth buffer, which is automatically resized to the screen size.
-	**/
-	public static function getDefault() {
-		return h3d.Engine.getCurrent().driver.getDefaultDepthBuffer();
-	}
-
-}

+ 38 - 9
h3d/mat/Texture.hx

@@ -55,7 +55,7 @@ class Texture {
 		When the texture is used as render target, tells which depth buffer will be used.
 		If set to null, depth testing is disabled.
 	**/
-	public var depthBuffer : DepthBuffer;
+	public var depthBuffer : Texture;
 
 	var _lastFrame:Int;
 
@@ -85,10 +85,6 @@ class Texture {
 	}
 
 	public function new(w, h, ?flags : Array<TextureFlags>, ?format : TextureFormat ) {
-		#if !noEngine
-		var engine = h3d.Engine.getCurrent();
-		this.mem = engine.mem;
-		#end
 		if( format == null ) format = nativeFormat;
 		this.id = ++UID;
 		this.format = format;
@@ -97,6 +93,13 @@ class Texture {
 			for( f in flags )
 				this.flags.set(f);
 
+		if ( !isDepth() ) {
+			#if !noEngine
+			var engine = h3d.Engine.getCurrent();
+			this.mem = engine.mem;
+			#end
+		}
+
 		var tw = 1, th = 1;
 		while( tw < w ) tw <<= 1;
 		while( th < h) th <<= 1;
@@ -115,7 +118,7 @@ class Texture {
 		#if track_alloc
 		this.allocPos = new hxd.impl.AllocPos();
 		#end
-		if( !this.flags.has(NoAlloc) ) alloc();
+		if( !this.flags.has(NoAlloc) && (!isDepth() || width > 0) ) alloc();
 	}
 
 	function get_layerCount() {
@@ -123,7 +126,9 @@ class Texture {
 	}
 
 	public function alloc() {
-		if( t == null )
+		if ( isDepth() )
+			h3d.Engine.getCurrent().mem.allocDepth(this);
+		else if( t == null )
 			mem.allocTexture(this);
 	}
 
@@ -331,8 +336,12 @@ class Texture {
 	}
 
 	public function dispose() {
-		if( t != null )
-			mem.deleteTexture(this);
+		if( t != null ) {
+			if ( isDepth() )
+				h3d.Engine.getCurrent().mem.deleteTexture(this);
+			else
+				mem.deleteTexture(this);
+		}
 	}
 
 	/**
@@ -550,4 +559,24 @@ class Texture {
 		b.dispose();
 	}
 
+	public function hasStencil() {
+		return switch( format ) {
+		case Depth24Stencil8: true;
+		default: false;
+		}
+	}
+
+	public function isDepth() {
+		return switch( format ) {
+		case Depth16, Depth24, Depth24Stencil8: true;
+		default: false;
+		}
+	}
+
+	/**
+		This will return the default depth buffer, which is automatically resized to the screen size.
+	**/
+	public static function getDefaultDepth() {
+		return h3d.Engine.getCurrent().driver.getDefaultDepthBuffer();
+	}
 }

+ 1 - 1
h3d/pass/CascadeShadowMap.hx

@@ -176,7 +176,7 @@ class CascadeShadowMap extends DirShadowMap {
 			var texture = ctx.textures.allocTarget("cascadeShadowMap", size, size, false, format);
 			if( customDepth && (depth == null || depth.width != size || depth.height != size || depth.isDisposed()) ) {
 				if( depth != null ) depth.dispose();
-				depth = new h3d.mat.DepthBuffer(size, size);
+				depth = new h3d.mat.Texture(size, size, Depth24Stencil8);
 			}
 			texture.depthBuffer = depth;
 			textures.push(texture);

+ 4 - 3
h3d/pass/DirShadowMap.hx

@@ -3,7 +3,7 @@ package h3d.pass;
 class DirShadowMap extends Shadows {
 
 	var customDepth : Bool;
-	var depth : h3d.mat.DepthBuffer;
+	var depth : h3d.mat.Texture;
 	var dshader : h3d.shader.DirShadow;
 	var border : Border;
 	var mergePass = new h3d.pass.ScreenFx(new h3d.shader.MinMaxShader());
@@ -33,7 +33,7 @@ class DirShadowMap extends Shadows {
 		shader = dshader = new h3d.shader.DirShadow();
 		border = new Border(size, size);
 		customDepth = h3d.Engine.getCurrent().driver.hasFeature(AllocDepthBuffer);
-		if( !customDepth ) depth = h3d.mat.DepthBuffer.getDefault();
+		if( !customDepth ) depth = h3d.mat.Texture.getDefaultDepth();
 	}
 
 	override function set_mode(m:Shadows.RenderMode) {
@@ -335,7 +335,8 @@ class DirShadowMap extends Shadows {
 		var texture = ctx.textures.allocTarget("dirShadowMap", size, size, false, format);
 		if( customDepth && (depth == null || depth.width != size || depth.height != size || depth.isDisposed()) ) {
 			if( depth != null ) depth.dispose();
-			depth = new h3d.mat.DepthBuffer(size, size);
+			depth = new h3d.mat.Texture(size, size, Depth24Stencil8);
+			depth.name = "dirShadowMapDepth";
 		}
 		texture.depthBuffer = depth;
 

+ 2 - 2
h3d/pass/HardwarePick.hx

@@ -36,9 +36,9 @@ class HardwarePick extends Default {
 		material.blend(One, Zero);
 		texOut = new h3d.mat.Texture(3, 3, [Target]);
 		#if !flash
-		texOut.depthBuffer = new h3d.mat.DepthBuffer(3, 3);
+		texOut.depthBuffer = new h3d.mat.Texture(3, 3, Depth24Stencil8);
 		#else
-		texOut.depthBuffer = h3d.mat.DepthBuffer.getDefault();
+		texOut.depthBuffer = h3d.mat.Texture.getDefaultDepth();
 		#end
 	}
 

+ 3 - 3
h3d/pass/PointShadowMap.hx

@@ -12,7 +12,7 @@ enum CubeFaceFlag {
 class PointShadowMap extends Shadows {
 
 	var customDepth : Bool;
-	var depth : h3d.mat.DepthBuffer;
+	var depth : h3d.mat.Texture;
 	var pshader : h3d.shader.PointShadow;
 	var mergePass = new h3d.pass.ScreenFx(new h3d.shader.MinMaxShader.CubeMinMaxShader());
 	public var faceMask(default, null) : haxe.EnumFlags<CubeFaceFlag>;
@@ -31,7 +31,7 @@ class PointShadowMap extends Shadows {
 		lightCamera.fovY = 90;
 		shader = pshader = new h3d.shader.PointShadow();
 		customDepth = h3d.Engine.getCurrent().driver.hasFeature(AllocDepthBuffer);
-		if( !customDepth ) depth = h3d.mat.DepthBuffer.getDefault();
+		if( !customDepth ) depth = h3d.mat.Texture.getDefaultDepth();
 
 		faceMask.set(Front);
 		faceMask.set(Back);
@@ -202,7 +202,7 @@ class PointShadowMap extends Shadows {
 		var texture = ctx.computingStatic ? createStaticTexture() : ctx.textures.allocTarget("pointShadowMap", size, size, false, format, true);
 		if( depth == null || depth.width != texture.width || depth.height != texture.height || depth.isDisposed() ) {
 			if( depth != null ) depth.dispose();
-			depth = new h3d.mat.DepthBuffer(texture.width, texture.height);
+			depth = new h3d.mat.Texture(texture.width, texture.height, Depth24Stencil8);
 		}
 		texture.depthBuffer = depth;
 

+ 3 - 3
h3d/pass/SpotShadowMap.hx

@@ -3,7 +3,7 @@ package h3d.pass;
 class SpotShadowMap extends Shadows {
 
 	var customDepth : Bool;
-	var depth : h3d.mat.DepthBuffer;
+	var depth : h3d.mat.Texture;
 	var sshader : h3d.shader.SpotShadow;
 	var border : Border;
 	var mergePass = new h3d.pass.ScreenFx(new h3d.shader.MinMaxShader());
@@ -17,7 +17,7 @@ class SpotShadowMap extends Shadows {
 		shader = sshader = new h3d.shader.SpotShadow();
 		border = new Border(size, size);
 		customDepth = h3d.Engine.getCurrent().driver.hasFeature(AllocDepthBuffer);
-		if( !customDepth ) depth = h3d.mat.DepthBuffer.getDefault();
+		if( !customDepth ) depth = h3d.mat.Texture.getDefaultDepth();
 	}
 
 	override function set_mode(m:Shadows.RenderMode) {
@@ -135,7 +135,7 @@ class SpotShadowMap extends Shadows {
 		var texture = ctx.computingStatic ? createStaticTexture() : ctx.textures.allocTarget("spotShadowMap", size, size, false, format);
 		if( customDepth && (depth == null || depth.width != texture.width || depth.height != texture.height || depth.isDisposed()) ) {
 			if( depth != null ) depth.dispose();
-			depth = new h3d.mat.DepthBuffer(texture.width, texture.height);
+			depth = new h3d.mat.Texture(texture.width, texture.height, Depth24Stencil8);
 		}
 		texture.depthBuffer = depth;
 

+ 1 - 1
h3d/scene/Scene.hx

@@ -460,7 +460,7 @@ class Scene extends Object implements h3d.IDrawable implements hxd.SceneEvents.I
 		}
 	}
 
-	var prevDB : h3d.mat.DepthBuffer;
+	var prevDB : h3d.mat.Texture;
 	var prevEngine = null;
 	/**
 		Temporarily overrides the output render target. This is useful for picture-in-picture rendering,

+ 3 - 0
hxd/PixelFormat.hx

@@ -23,4 +23,7 @@ enum PixelFormat {
 	RGB16U;
 	RGBA16U;
 	S3TC( v : Int );
+	Depth16;
+	Depth24;
+	Depth24Stencil8;
 }

+ 3 - 1
hxd/Pixels.hx

@@ -577,6 +577,8 @@ class Pixels {
 			if( n == 1 || n == 4 )
 				return blocks << 1;
 			return blocks << 2;
+		case Depth16, Depth24, Depth24Stencil8:
+			throw "Not a pixel format";
 		}
 	}
 
@@ -615,7 +617,7 @@ class Pixels {
 			channel.toInt() * 4;
 		case RGB10A2, RG11B10UF:
 			throw "Bit packed format";
-		case S3TC(_):
+		case S3TC(_), Depth16, Depth24, Depth24Stencil8:
 			throw "Not supported";
 		}
 	}