Browse Source

added Texture.startingMip

Nicolas Cannasse 1 year ago
parent
commit
b30ab26551
5 changed files with 36 additions and 10 deletions
  1. 4 0
      h3d/impl/DX12Driver.hx
  2. 22 5
      h3d/impl/DirectXDriver.hx
  3. 2 2
      h3d/impl/Driver.hx
  4. 7 3
      h3d/impl/GlDriver.hx
  5. 1 0
      h3d/mat/Texture.hx

+ 4 - 0
h3d/impl/DX12Driver.hx

@@ -1577,19 +1577,23 @@ class DX12Driver extends h3d.impl.Driver {
 					if( t.flags.has(Cube) ) {
 						var desc = tmp.texCubeSRV;
 						desc.format = t.t.format;
+						desc.mostDetailedMip = t.startingMip;
 						tdesc = desc;
 					} else if( t.flags.has(IsArray) ) {
 						var desc = tmp.tex2DArraySRV;
 						desc.format = t.t.format;
 						desc.arraySize = t.layerCount;
+						desc.mostDetailedMip = t.startingMip;
 						tdesc = desc;
 					} else if ( t.isDepth() ) {
 						var desc = tmp.tex2DSRV;
 						desc.format = R24_UNORM_X8_TYPELESS;
+						desc.mostDetailedMip = t.startingMip;
 						tdesc = desc;
 					} else {
 						var desc = tmp.tex2DSRV;
 						desc.format = t.t.format;
+						desc.mostDetailedMip = t.startingMip;
 						tdesc = desc;
 					}
 					t.lastFrame = frameCount;

+ 22 - 5
h3d/impl/DirectXDriver.hx

@@ -468,14 +468,19 @@ class DirectXDriver extends h3d.impl.Driver {
 		t.lastFrame = frame;
 		t.flags.unset(WasCleared);
 
+		return { res : tex, view : makeTexView(t, tex, 0), rt : rt ? [] : null, mips : mips };
+	}
+
+	function makeTexView( t : h3d.mat.Texture, tex, startMip ) {
+		var isCube = t.flags.has(Cube);
+		var isArray = t.flags.has(IsArray);
 		var vdesc = new ShaderResourceViewDesc();
-		vdesc.format = desc.format;
+		vdesc.format = getTextureFormat(t);
 		vdesc.dimension = isCube ? TextureCube : isArray ? Texture2DArray : Texture2D;
-		vdesc.arraySize = desc.arraySize;
-		vdesc.start = 0; // top mip level
+		vdesc.arraySize = isArray ? t.layerCount : isCube ? 6 : 0;
+		vdesc.start = startMip; // top mip level
 		vdesc.count = -1; // all mip levels
-		var view = Driver.createShaderResourceView(tex, vdesc);
-		return { res : tex, view : view, rt : rt ? [] : null, mips : mips };
+		return Driver.createShaderResourceView(tex, vdesc);
 	}
 
 	override function disposeTexture( t : h3d.mat.Texture ) {
@@ -484,6 +489,10 @@ class DirectXDriver extends h3d.impl.Driver {
 		t.t = null;
 		if( tt.view != null ) tt.view.release();
 		if( tt.res != null ) tt.res.release();
+		if( tt.views != null )
+			for( v in tt.views )
+				if( v != null )
+					v.release();
 		if( tt.rt != null )
 			for( rt in tt.rt )
 				if( rt != null )
@@ -1335,6 +1344,14 @@ class DirectXDriver extends h3d.impl.Driver {
 				t.lastFrame = frame;
 
 				var view = t.t.view;
+				if( t.startingMip > 0 ) {
+					if( t.t.views == null ) t.t.views = [];
+					view = t.t.views[t.startingMip];
+					if( view == null ) {
+						view = makeTexView(t, t.t.res, t.startingMip);
+						t.t.views[t.startingMip] = view;
+					}
+				}
 				if( view != state.resources[i] || t.t.depthView != null ) {
 					state.resources[i] = view;
 					max = i;

+ 2 - 2
h3d/impl/Driver.hx

@@ -13,7 +13,7 @@ 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 Texture = { t : sdl.GL.Texture, width : Int, height : Int, internalFmt : Int, pixelFmt : Int, bits : Int, bind : Int, bias : Float, startMip : Int };
 typedef Query = { q : sdl.GL.Query, kind : QueryKind };
 #elseif usegl
 typedef IndexBuffer = { b : haxe.GLTypes.Buffer, is32 : Bool };
@@ -28,7 +28,7 @@ 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, ?depthView : dx.Driver.DepthStencilView, ?readOnlyDepthView : dx.Driver.DepthStencilView, rt : Array<dx.Driver.RenderTargetView>, mips : Int };
+typedef Texture = { res : dx.Resource, view : dx.Driver.ShaderResourceView, ?depthView : dx.Driver.DepthStencilView, ?readOnlyDepthView : dx.Driver.DepthStencilView, rt : Array<dx.Driver.RenderTargetView>, mips : Int, ?views : Array<dx.Driver.ShaderResourceView> };
 typedef Query = {};
 #elseif usesys
 typedef IndexBuffer = haxe.GraphicsDriver.IndexBuffer;

+ 7 - 3
h3d/impl/GlDriver.hx

@@ -577,6 +577,10 @@ class GlDriver extends Driver {
 					gl.texParameteri(mode, GL.TEXTURE_WRAP_S, w);
 					gl.texParameteri(mode, GL.TEXTURE_WRAP_T, w);
 				}
+				if( t.t.startMip != t.startingMip ) {
+					gl.texParameteri(pt.mode, GL.TEXTURE_BASE_LEVEL, t.startingMip);
+					t.t.startMip = t.startingMip;
+				}
 				#if !js
 				if( t.lodBias != t.t.bias ) {
 					t.t.bias = t.lodBias;
@@ -853,7 +857,7 @@ class GlDriver extends Driver {
 		discardError();
 		var tt = gl.createTexture();
 		var bind = getBindType(t);
-		var tt : Texture = { t : tt, width : t.width, height : t.height, internalFmt : GL.RGBA, pixelFmt : GL.UNSIGNED_BYTE, bits : -1, bind : bind, bias : 0 #if multidriver, driver : this #end };
+		var tt : Texture = { t : tt, width : t.width, height : t.height, internalFmt : GL.RGBA, pixelFmt : GL.UNSIGNED_BYTE, bits : -1, bind : bind, bias : 0, startMip : t.startingMip #if multidriver, driver : this #end };
 		switch( t.format ) {
 		case RGBA:
 			// default
@@ -950,7 +954,7 @@ class GlDriver extends Driver {
 		}
 
 		#if (js || (hlsdl >= version("1.12.0")))
-		gl.texParameteri(bind, GL.TEXTURE_BASE_LEVEL, 0);
+		gl.texParameteri(bind, GL.TEXTURE_BASE_LEVEL, t.startingMip);
 		gl.texParameteri(bind, GL.TEXTURE_MAX_LEVEL, t.mipLevels-1);
 		#end
 
@@ -1006,7 +1010,7 @@ class GlDriver extends Driver {
 
 	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 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, startMip: 0 #if multidriver, driver : this #end };
 		var fmt = GL.DEPTH_COMPONENT;
 		switch( t.format ) {
 		case Depth16:

+ 1 - 0
h3d/mat/Texture.hx

@@ -41,6 +41,7 @@ class Texture {
 	public var filter(default,set) : Filter;
 	public var wrap(default, set) : Wrap;
 	public var layerCount(get, never) : Int;
+	public var startingMip : Int = 0;
 	public var lodBias : Float = 0.;
 	public var mipLevels(get, never) : Int;
 	var customMipLevels : Int;