Pārlūkot izejas kodu

[DX12] Check the size of the descriptor heap before uploading the instance shader buffers, rather than after drawing, since in some cases, objects may never call the draw function after uploading the shader buffers.

TothBenoit 2 mēneši atpakaļ
vecāks
revīzija
639b72c0f4

+ 1 - 3
h2d/RenderContext.hx

@@ -631,9 +631,7 @@ class RenderContext extends h3d.impl.RenderContext {
 		var buffers = shaderBuffers;
 		var buffers = shaderBuffers;
 		fillParams(buffers, compiledShader, currentShaders);
 		fillParams(buffers, compiledShader, currentShaders);
 		engine.selectMaterial(pass);
 		engine.selectMaterial(pass);
-		engine.uploadShaderBuffers(buffers, Params);
-		engine.uploadShaderBuffers(buffers, Textures);
-		engine.uploadShaderBuffers(buffers, Buffers);
+		engine.uploadInstanceShaderBuffers(buffers);
 	}
 	}
 
 
 	inline function setupColor( obj : h2d.Drawable ) {
 	inline function setupColor( obj : h2d.Drawable ) {

+ 7 - 0
h3d/Engine.hx

@@ -145,6 +145,13 @@ class Engine {
 		driver.selectMaterial(pass);
 		driver.selectMaterial(pass);
 	}
 	}
 
 
+	public function uploadInstanceShaderBuffers(buffers) {
+		driver.flushShaderBuffers();
+		driver.uploadShaderBuffers(buffers, Params);
+		driver.uploadShaderBuffers(buffers, Textures);
+		driver.uploadShaderBuffers(buffers, Buffers);
+	}
+
 	public function uploadShaderBuffers(buffers, which) {
 	public function uploadShaderBuffers(buffers, which) {
 		driver.uploadShaderBuffers(buffers, which);
 		driver.uploadShaderBuffers(buffers, which);
 	}
 	}

+ 12 - 14
h3d/impl/DX12Driver.hx

@@ -2450,7 +2450,6 @@ class DX12Driver extends h3d.impl.Driver {
 		}
 		}
 		flushTransitions();
 		flushTransitions();
 		frame.commandList.drawIndexedInstanced(ntriangles * 3,1,startIndex,0,0);
 		frame.commandList.drawIndexedInstanced(ntriangles * 3,1,startIndex,0,0);
-		flushResources();
 	}
 	}
 
 
 	override function drawInstanced(ibuf:Buffer, commands:InstanceBuffer) {
 	override function drawInstanced(ibuf:Buffer, commands:InstanceBuffer) {
@@ -2470,10 +2469,9 @@ class DX12Driver extends h3d.impl.Driver {
 			flushTransitions();
 			flushTransitions();
 			frame.commandList.drawIndexedInstanced(commands.indexCount, commands.commandCount, commands.startIndex, 0, 0);
 			frame.commandList.drawIndexedInstanced(commands.indexCount, commands.commandCount, commands.startIndex, 0, 0);
 		}
 		}
-		flushResources();
 	}
 	}
 
 
-	function flushResources() {
+	override function flushShaderBuffers() {
 		if( frame.shaderResourceViews.available < 128 || frame.samplerViews.available < 64 ) {
 		if( frame.shaderResourceViews.available < 128 || frame.samplerViews.available < 64 ) {
 			frame.shaderResourceViews = frame.shaderResourceCache.next();
 			frame.shaderResourceViews = frame.shaderResourceCache.next();
 			frame.samplerViews = frame.samplerCache.next();
 			frame.samplerViews = frame.samplerCache.next();
@@ -2483,17 +2481,18 @@ class DX12Driver extends h3d.impl.Driver {
 			arr[1] = @:privateAccess frame.samplerViews.heap;
 			arr[1] = @:privateAccess frame.samplerViews.heap;
 			frame.commandList.setDescriptorHeaps(arr);
 			frame.commandList.setDescriptorHeaps(arr);
 			inline function rebindGlobal(bindSlot, desc) {
 			inline function rebindGlobal(bindSlot, desc) {
-				var srv = frame.shaderResourceViews.alloc(1);
-				Driver.createConstantBufferView(desc, srv);
-				if( currentShader.isCompute )
-					frame.commandList.setComputeRootDescriptorTable(bindSlot, frame.shaderResourceViews.toGPU(srv));
-				else
-					frame.commandList.setGraphicsRootDescriptorTable(bindSlot, frame.shaderResourceViews.toGPU(srv));
+				if ( bindSlot >= 0 ) {
+					var srv = frame.shaderResourceViews.alloc(1);
+					Driver.createConstantBufferView(desc, srv);
+					if( currentShader.isCompute )
+						frame.commandList.setComputeRootDescriptorTable(bindSlot, frame.shaderResourceViews.toGPU(srv));
+					else
+						frame.commandList.setGraphicsRootDescriptorTable(bindSlot, frame.shaderResourceViews.toGPU(srv));
+				}
 			}
 			}
-			if ( lastVertexGlobalBind >= 0 )
-				rebindGlobal(lastVertexGlobalBind, tmp.vertexGlobalDesc);
-			if ( lastFragmentGlobalBind >= 0 )
-				rebindGlobal(lastFragmentGlobalBind, tmp.fragmentGlobalDesc);
+
+			rebindGlobal(lastVertexGlobalBind, tmp.vertexGlobalDesc);
+			rebindGlobal(lastFragmentGlobalBind, tmp.fragmentGlobalDesc);
 		}
 		}
 	}
 	}
 
 
@@ -2545,7 +2544,6 @@ class DX12Driver extends h3d.impl.Driver {
 		flushTransitions();
 		flushTransitions();
 		frame.commandList.dispatch(x,y,z);
 		frame.commandList.dispatch(x,y,z);
 		uavBarrier();
 		uavBarrier();
-		flushResources();
 	}
 	}
 
 
 	function uavBarrier() {
 	function uavBarrier() {

+ 3 - 0
h3d/impl/Driver.hx

@@ -186,6 +186,9 @@ class Driver {
 	public function uploadShaderBuffers( buffers : h3d.shader.Buffers, which : h3d.shader.Buffers.BufferKind ) {
 	public function uploadShaderBuffers( buffers : h3d.shader.Buffers, which : h3d.shader.Buffers.BufferKind ) {
 	}
 	}
 
 
+	public function flushShaderBuffers() {
+	}
+
 	public function selectBuffer( buffer : Buffer ) {
 	public function selectBuffer( buffer : Buffer ) {
 	}
 	}
 
 

+ 1 - 3
h3d/pass/Output.hx

@@ -114,9 +114,7 @@ class Output {
 			}
 			}
 			if( !p.pass.dynamicParameters ) {
 			if( !p.pass.dynamicParameters ) {
 				ctx.fillParams(buf, p.shader, p.shaders);
 				ctx.fillParams(buf, p.shader, p.shaders);
-				ctx.engine.uploadShaderBuffers(buf, Params);
-				ctx.engine.uploadShaderBuffers(buf, Textures);
-				ctx.engine.uploadShaderBuffers(buf, Buffers);
+				ctx.engine.uploadInstanceShaderBuffers(buf);
 			}
 			}
 			drawObject(p);
 			drawObject(p);
 		}
 		}

+ 1 - 3
h3d/pass/ScreenFx.hx

@@ -58,9 +58,7 @@ class ScreenFx<T:h3d.shader.ScreenShader> {
 		ctx.fillGlobals(buffers, rts);
 		ctx.fillGlobals(buffers, rts);
 		ctx.fillParams(buffers, rts, shaders);
 		ctx.fillParams(buffers, rts, shaders);
 		engine.uploadShaderBuffers(buffers, Globals);
 		engine.uploadShaderBuffers(buffers, Globals);
-		engine.uploadShaderBuffers(buffers, Params);
-		engine.uploadShaderBuffers(buffers, Textures);
-		engine.uploadShaderBuffers(buffers, Buffers);
+		engine.uploadInstanceShaderBuffers(buffers);
 		primitive.render(engine);
 		primitive.render(engine);
 		if( isNewCtx )
 		if( isNewCtx )
 			ctx.clearCurrent();
 			ctx.clearCurrent();

+ 2 - 6
h3d/scene/RenderContext.hx

@@ -198,9 +198,7 @@ class RenderContext extends h3d.impl.RenderContext {
 		fillGlobals(buf, rt);
 		fillGlobals(buf, rt);
 		engine.uploadShaderBuffers(buf, Globals);
 		engine.uploadShaderBuffers(buf, Globals);
 		fillParams(buf, rt, computeLink, true);
 		fillParams(buf, rt, computeLink, true);
-		engine.uploadShaderBuffers(buf, Params);
-		engine.uploadShaderBuffers(buf, Textures);
-		engine.uploadShaderBuffers(buf, Buffers);
+		engine.uploadInstanceShaderBuffers(buf);
 		engine.driver.computeDispatch(x,y,z);
 		engine.driver.computeDispatch(x,y,z);
 		@:privateAccess engine.dispatches++;
 		@:privateAccess engine.dispatches++;
 		if ( computeLink == tmpComputeLink )
 		if ( computeLink == tmpComputeLink )
@@ -252,9 +250,7 @@ class RenderContext extends h3d.impl.RenderContext {
 
 
 	public function uploadParams() {
 	public function uploadParams() {
 		fillParams(shaderBuffers, drawPass.shader, drawPass.shaders);
 		fillParams(shaderBuffers, drawPass.shader, drawPass.shaders);
-		engine.uploadShaderBuffers(shaderBuffers, Params);
-		engine.uploadShaderBuffers(shaderBuffers, Textures);
-		engine.uploadShaderBuffers(shaderBuffers, Buffers);
+		engine.uploadInstanceShaderBuffers(shaderBuffers);
 	}
 	}
 
 
 	public function done() {
 	public function done() {