浏览代码

Merge branch 'master' into dynamic-bones

lviguier 7 月之前
父节点
当前提交
1cd5cfcb83
共有 5 个文件被更改,包括 43 次插入9 次删除
  1. 32 4
      h3d/impl/DX12Driver.hx
  2. 1 0
      h3d/impl/GlDriver.hx
  3. 1 1
      h3d/shader/SAO.hx
  4. 1 1
      h3d/shader/pbr/DefaultForward.hx
  5. 8 3
      hxsl/HlslOut.hx

+ 32 - 4
h3d/impl/DX12Driver.hx

@@ -226,6 +226,8 @@ class CompiledShader {
 	@:packed public var rect(default,null) : Rect;
 	@:packed public var rect(default,null) : Rect;
 	@:packed public var bufferSRV(default,null) : BufferSRV;
 	@:packed public var bufferSRV(default,null) : BufferSRV;
 	@:packed public var samplerDesc(default,null) : SamplerDesc;
 	@:packed public var samplerDesc(default,null) : SamplerDesc;
+	@:packed public var vertexGlobalDesc(default,null) : ConstantBufferViewDesc;
+	@:packed public var fragmentGlobalDesc(default,null) : ConstantBufferViewDesc;
 	@:packed public var cbvDesc(default,null) : ConstantBufferViewDesc;
 	@:packed public var cbvDesc(default,null) : ConstantBufferViewDesc;
 	@:packed public var rtvDesc(default,null) : RenderTargetViewDesc;
 	@:packed public var rtvDesc(default,null) : RenderTargetViewDesc;
 	@:packed public var uavDesc(default,null) : UAVBufferViewDesc;
 	@:packed public var uavDesc(default,null) : UAVBufferViewDesc;
@@ -404,6 +406,8 @@ class DX12Driver extends h3d.impl.Driver {
 	var tsFreq : haxe.Int64;
 	var tsFreq : haxe.Int64;
 	var heapCount : Int;
 	var heapCount : Int;
 	var currentPipelineState : PipelineState;
 	var currentPipelineState : PipelineState;
+	var lastVertexGlobalBind : Int = -1;
+	var lastFragmentGlobalBind : Int = -1;
 
 
 	public static var INITIAL_RT_COUNT = 1024;
 	public static var INITIAL_RT_COUNT = 1024;
 	public static var INITIAL_BUMP_ALLOCATOR_SIZE = 2 * 1024 * 1024;
 	public static var INITIAL_BUMP_ALLOCATOR_SIZE = 2 * 1024 * 1024;
@@ -1397,6 +1401,10 @@ class DX12Driver extends h3d.impl.Driver {
 
 
 		var inputLayout = hl.CArray.alloc(InputElementDesc, inputs.length);
 		var inputLayout = hl.CArray.alloc(InputElementDesc, inputs.length);
 		var format : Array<hxd.BufferFormat.BufferInput> = [];
 		var format : Array<hxd.BufferFormat.BufferInput> = [];
+		var allNames = new Map();
+		var varNames = new Map();
+		for ( i => v in inputs)
+			hxsl.HlslOut.varName(v, varNames, allNames);
 		for( i => v in inputs ) {
 		for( i => v in inputs ) {
 			var d = inputLayout[i];
 			var d = inputLayout[i];
 			var perInst = 0;
 			var perInst = 0;
@@ -1406,7 +1414,7 @@ class DX12Driver extends h3d.impl.Driver {
 					case PerInstance(k): perInst = k;
 					case PerInstance(k): perInst = k;
 					default:
 					default:
 					}
 					}
-			d.semanticName = @:privateAccess hxsl.HlslOut.semanticName(v.name).toUtf8();
+			d.semanticName = @:privateAccess hxsl.HlslOut.semanticName(varNames.get(v.id)).toUtf8();
 			d.inputSlot = i;
 			d.inputSlot = i;
 			format.push({ name : v.name, type : hxd.BufferFormat.InputFormat.fromHXSL(v.type) });
 			format.push({ name : v.name, type : hxd.BufferFormat.InputFormat.fromHXSL(v.type) });
 			if( perInst > 0 ) {
 			if( perInst > 0 ) {
@@ -1904,6 +1912,8 @@ class DX12Driver extends h3d.impl.Driver {
 					frame.commandList.setGraphicsRoot32BitConstants(regs.params, dataSize >> 2, data, 0);
 					frame.commandList.setGraphicsRoot32BitConstants(regs.params, dataSize >> 2, data, 0);
 			}
 			}
 		case Globals:
 		case Globals:
+			var isFragment = shader.kind == Fragment;
+			var bind = -1;
 			if( shader.globalsSize > 0 ) {
 			if( shader.globalsSize > 0 ) {
 				var data = hl.Bytes.getArray(buf.globals.toData());
 				var data = hl.Bytes.getArray(buf.globals.toData());
 				var dataSize = shader.globalsSize << 4;
 				var dataSize = shader.globalsSize << 4;
@@ -1911,19 +1921,25 @@ class DX12Driver extends h3d.impl.Driver {
 					// update CBV
 					// update CBV
 					var srv = frame.shaderResourceViews.alloc(1);
 					var srv = frame.shaderResourceViews.alloc(1);
 					var alloc = allocDynamicBuffer(data,dataSize);
 					var alloc = allocDynamicBuffer(data,dataSize);
-					var desc = tmp.cbvDesc;
+					var desc = isFragment ? tmp.fragmentGlobalDesc : tmp.vertexGlobalDesc;
 					desc.bufferLocation = alloc.resource.getGpuVirtualAddress() + alloc.offset;
 					desc.bufferLocation = alloc.resource.getGpuVirtualAddress() + alloc.offset;
 					desc.sizeInBytes = alloc.byteSize;
 					desc.sizeInBytes = alloc.byteSize;
 					Driver.createConstantBufferView(desc, srv);
 					Driver.createConstantBufferView(desc, srv);
+					bind = regs.globals & 0xFF;
 					if( currentShader.isCompute )
 					if( currentShader.isCompute )
-						frame.commandList.setComputeRootDescriptorTable(regs.globals & 0xFF, frame.shaderResourceViews.toGPU(srv));
+						frame.commandList.setComputeRootDescriptorTable(bind, frame.shaderResourceViews.toGPU(srv));
 					else
 					else
-						frame.commandList.setGraphicsRootDescriptorTable(regs.globals & 0xFF, frame.shaderResourceViews.toGPU(srv));
+						frame.commandList.setGraphicsRootDescriptorTable(bind, frame.shaderResourceViews.toGPU(srv));
 				} else if( currentShader.isCompute )
 				} else if( currentShader.isCompute )
 					frame.commandList.setComputeRoot32BitConstants(regs.globals, dataSize >> 2, data, 0);
 					frame.commandList.setComputeRoot32BitConstants(regs.globals, dataSize >> 2, data, 0);
 				else
 				else
 					frame.commandList.setGraphicsRoot32BitConstants(regs.globals, dataSize >> 2, data, 0);
 					frame.commandList.setGraphicsRoot32BitConstants(regs.globals, dataSize >> 2, data, 0);
 			}
 			}
+			if ( isFragment )
+				lastFragmentGlobalBind = bind;
+			else
+				lastVertexGlobalBind = bind;
+
 		case Textures:
 		case Textures:
 			if( shader.texturesCount > 0 ) {
 			if( shader.texturesCount > 0 ) {
 				if ( hasBuffersTexturesChanged(buf, regs) ) {
 				if ( hasBuffersTexturesChanged(buf, regs) ) {
@@ -2385,6 +2401,18 @@ class DX12Driver extends h3d.impl.Driver {
 			arr[0] = @:privateAccess frame.shaderResourceViews.heap;
 			arr[0] = @:privateAccess frame.shaderResourceViews.heap;
 			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) {
+				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);
 		}
 		}
 	}
 	}
 
 

+ 1 - 0
h3d/impl/GlDriver.hx

@@ -1987,6 +1987,7 @@ class GlDriver extends Driver {
 
 
 	override function computeDispatch(x:Int = 1, y:Int = 1, z:Int = 1) {
 	override function computeDispatch(x:Int = 1, y:Int = 1, z:Int = 1) {
 		GL.dispatchCompute(x,y,z);
 		GL.dispatchCompute(x,y,z);
+		GL.memoryBarrier(GL.BUFFER_UPDATE_BARRIER_BIT);
 	}
 	}
 
 
 	override function allocQuery(kind:QueryKind) {
 	override function allocQuery(kind:QueryKind) {

+ 1 - 1
h3d/shader/SAO.hx

@@ -47,7 +47,7 @@ class SAO extends ScreenShader {
 			var radius = sampleRadius;
 			var radius = sampleRadius;
 			if (USE_SCALABLE_BIAS) {
 			if (USE_SCALABLE_BIAS) {
 				var vQ = Q * cameraView;
 				var vQ = Q * cameraView;
-				radius *= log(1.0 + vQ.z) + 1;
+				radius *= max(0.0, log(1.0 + vQ.z)) + 1;
 			}
 			}
 			var v = Q - position;
 			var v = Q - position;
 
 

+ 1 - 1
h3d/shader/pbr/DefaultForward.hx

@@ -224,7 +224,7 @@ class DefaultForward extends hxsl.Shader {
 			@unroll for ( c in 0...CASCADE_COUNT ) {
 			@unroll for ( c in 0...CASCADE_COUNT ) {
 				var cascadeScale = lightInfos[i + 5 + 2 * c];
 				var cascadeScale = lightInfos[i + 5 + 2 * c];
 				var shadowPos0 = transformedPosition * shadowProj;
 				var shadowPos0 = transformedPosition * shadowProj;
-				var shadowPos = i == 0 ? shadowPos0 : shadowPos0 * cascadeScale.xyz + lightInfos[i + 6 + 2 * c].xyz;
+				var shadowPos = c == 0 ? shadowPos0 : shadowPos0 * cascadeScale.xyz + lightInfos[i + 6 + 2 * c].xyz;
 				if ( inside(shadowPos) ) {
 				if ( inside(shadowPos) ) {
 					var zMax = saturate(shadowPos.z);
 					var zMax = saturate(shadowPos.z);
 					var shadowUv = shadowPos.xy;
 					var shadowUv = shadowPos.xy;

+ 8 - 3
hxsl/HlslOut.hx

@@ -134,7 +134,7 @@ class HlslOut {
 	}
 	}
 
 
 	inline function ident( v : TVar ) {
 	inline function ident( v : TVar ) {
-		add(varName(v));
+		add(varName(v, varNames, allNames));
 	}
 	}
 
 
 	function decl( s : String ) {
 	function decl( s : String ) {
@@ -732,7 +732,7 @@ class HlslOut {
 		}
 		}
 	}
 	}
 
 
-	function varName( v : TVar ) {
+	public static function varName(v : TVar, varNames : Map<Int, String>, allNames : Map<String, Int>) : String {
 		var n = varNames.get(v.id);
 		var n = varNames.get(v.id);
 		if( n != null )
 		if( n != null )
 			return n;
 			return n;
@@ -792,7 +792,7 @@ class HlslOut {
 			if( v.kind == Output )
 			if( v.kind == Output )
 				add(" : " + (isVertex ? SV_POSITION : SV_TARGET + (index++)));
 				add(" : " + (isVertex ? SV_POSITION : SV_TARGET + (index++)));
 			else
 			else
-				add(" : " + semanticName(v.name));
+				add(" : " + semanticName(varNames.get(v.id)));
 			add(";\n");
 			add(";\n");
 			varAccess.set(v.id, prefix);
 			varAccess.set(v.id, prefix);
 		}
 		}
@@ -801,6 +801,8 @@ class HlslOut {
 		for( f in s.funs )
 		for( f in s.funs )
 			collectGlobals(foundGlobals, f.expr);
 			collectGlobals(foundGlobals, f.expr);
 
 
+		var oldAllNames = allNames;
+		allNames = new Map();
 		add("struct s_input {\n");
 		add("struct s_input {\n");
 		if( kind == Fragment )
 		if( kind == Fragment )
 			add("\tfloat4 __pos__ : "+SV_POSITION+";\n");
 			add("\tfloat4 __pos__ : "+SV_POSITION+";\n");
@@ -828,6 +830,7 @@ class HlslOut {
 		add("};\n\n");
 		add("};\n\n");
 
 
 		if( !isCompute ) {
 		if( !isCompute ) {
+			allNames = new Map();
 			add("struct s_output {\n");
 			add("struct s_output {\n");
 			for( v in s.vars )
 			for( v in s.vars )
 				if( v.kind == Output )
 				if( v.kind == Output )
@@ -837,6 +840,8 @@ class HlslOut {
 					declVar("_out.", v);
 					declVar("_out.", v);
 			add("};\n\n");
 			add("};\n\n");
 		}
 		}
+
+		allNames = oldAllNames;
 	}
 	}
 
 
 	function initGlobals( s : ShaderData ) {
 	function initGlobals( s : ShaderData ) {