Explorar o código

textures working

Nicolas Cannasse %!s(int64=3) %!d(string=hai) anos
pai
achega
e7f2f3783f
Modificáronse 3 ficheiros con 103 adicións e 4 borrados
  1. 78 2
      h3d/impl/VulkanDriver.hx
  2. 3 0
      hxd/App.hx
  3. 22 2
      hxsl/GlslOut.hx

+ 78 - 2
h3d/impl/VulkanDriver.hx

@@ -20,6 +20,7 @@ class CompiledShader {
 	public var input : VkPipelineVertexInput;
 	public var input : VkPipelineVertexInput;
 	public var inputID : InputNames;
 	public var inputID : InputNames;
 	public var layout : VkPipelineLayout;
 	public var layout : VkPipelineLayout;
+	public var samplerSets : hl.NativeArray<VkDescriptorSet>;
 	public function new(shader) {
 	public function new(shader) {
 		this.shader = shader;
 		this.shader = shader;
 	}
 	}
@@ -42,6 +43,7 @@ class VulkanDriver extends Driver {
 	var programs : Map<Int,CompiledShader> = new Map();
 	var programs : Map<Int,CompiledShader> = new Map();
 	var command : VkCommandBuffer;
 	var command : VkCommandBuffer;
 	var commandPool : VkCommandPool;
 	var commandPool : VkCommandPool;
+	var samplerPool : VkDescriptorPool;
 	var savedPointers : Array<Dynamic> = [];
 	var savedPointers : Array<Dynamic> = [];
 
 
 	var memReq = new VkMemoryRequirements();
 	var memReq = new VkMemoryRequirements();
@@ -63,6 +65,9 @@ class VulkanDriver extends Driver {
 	var currentFrameIndex : Int;
 	var currentFrameIndex : Int;
 	var limits : VkPhysicalDeviceLimits;
 	var limits : VkPhysicalDeviceLimits;
 
 
+	var defaultSampler : VkSampler; // TOREMOVE
+	var frameCount = 2;
+
 	public function new() {
 	public function new() {
 		var win = hxd.Window.getInstance();
 		var win = hxd.Window.getInstance();
 		initContext(@:privateAccess win.window.vkctx);
 		initContext(@:privateAccess win.window.vkctx);
@@ -73,6 +78,7 @@ class VulkanDriver extends Driver {
 
 
 	function initContext(surface) {
 	function initContext(surface) {
 		var queueFamily = 0;
 		var queueFamily = 0;
+
 		ctx = Vulkan.initContext(surface, queueFamily);
 		ctx = Vulkan.initContext(surface, queueFamily);
 		if( ctx == null ) throw "Failed to init context";
 		if( ctx == null ) throw "Failed to init context";
 		this.queueFamily = queueFamily;
 		this.queueFamily = queueFamily;
@@ -83,7 +89,10 @@ class VulkanDriver extends Driver {
 		poolInf.queueFamilyIndex = queueFamily;
 		poolInf.queueFamilyIndex = queueFamily;
 		commandPool = ctx.createCommandPool(poolInf);
 		commandPool = ctx.createCommandPool(poolInf);
 
 
-		var frameCount = 2;
+
+		var inf = new VkSamplerCreateInfo();
+		defaultSampler = ctx.createSampler(inf);
+
 		frames = [];
 		frames = [];
 		for( i in 0...frameCount ) {
 		for( i in 0...frameCount ) {
 			var frame = new VulkanFrame();
 			var frame = new VulkanFrame();
@@ -438,14 +447,67 @@ class VulkanDriver extends Driver {
 		c.vertex.globalsOffset = shader.vertex.globalsSize * 16;
 		c.vertex.globalsOffset = shader.vertex.globalsSize * 16;
 		c.fragment.globalsOffset = shader.fragment.globalsSize * 16;
 		c.fragment.globalsOffset = shader.fragment.globalsSize * 16;
 
 
+		var bindings = [], sets = [];
+		for( i in 0...shader.vertex.texturesCount ) {
+			var s = new VkDescriptorSetLayoutBinding();
+			s.binding = i;
+			s.descriptorCount = 1;
+			s.descriptorType = COMBINED_IMAGE_SAMPLER;
+			s.stageFlags.set(VERTEX);
+			bindings.push(s);
+		}
+		for( i in 0...shader.fragment.texturesCount ) {
+			var s = new VkDescriptorSetLayoutBinding();
+			s.binding = i;
+			s.descriptorCount = 1;
+			s.descriptorType = COMBINED_IMAGE_SAMPLER;
+			s.stageFlags.set(FRAGMENT);
+			bindings.push(s);
+		}
+		if( bindings.length > 0 ) {
+			var desc = new VkDescriptorSetLayoutCreateInfo();
+			desc.bindingCount = bindings.length;
+			desc.bindings = makeArray(bindings);
+			var set = ctx.createDescriptorSetLayout(desc);
+			c.samplerSets = allocateDescriptorSets(set);
+			sets.push(set);
+		}
+
 		var inf = new VkPipelineLayoutCreateInfo();
 		var inf = new VkPipelineLayoutCreateInfo();
 		inf.pushConstantRangeCount = 2;
 		inf.pushConstantRangeCount = 2;
 		inf.pushConstantRanges = makeArray([vconsts,fconsts]);
 		inf.pushConstantRanges = makeArray([vconsts,fconsts]);
+		inf.setLayoutCount = sets.length;
+		inf.setLayouts = sets.length == 0 ? null : makeArray(sets);
 		c.layout = ctx.createPipelineLayout(inf);
 		c.layout = ctx.createPipelineLayout(inf);
 
 
+
 		return c;
 		return c;
 	}
 	}
 
 
+	function allocateDescriptorSets( set : VkDescriptorSetLayout ) {
+		if( samplerPool == null ) {
+			var poolSize = new VkDescriptorPoolSize();
+			poolSize.descriptorCount = frameCount;
+			poolSize.type = COMBINED_IMAGE_SAMPLER;
+			var poolInf = new VkDescriptorPoolCreateInfo();
+			poolInf.poolSizeCount = 1;
+			poolInf.pPoolSizes = makeArray([poolSize]);
+			poolInf.maxSets = 4096;
+			poolInf.flags.set(UPDATE_AFTER_BIND);
+			samplerPool = ctx.createDescriptorPool(poolInf);
+			if( samplerPool == null )
+				throw "assert";
+		}
+		var sets = new hl.NativeArray(frameCount);
+		var inf = new VkDescriptorSetAllocateInfo();
+		inf.descriptorPool = samplerPool;
+		inf.descriptorSetCount = frameCount;
+		inf.pSetLayouts = makeArray([for( i in 0...frameCount ) set]);
+		if( !ctx.allocateDescriptorSets(inf, sets) )
+			throw "assert";
+		return sets;
+	}
+
 	override function getShaderInputNames() : InputNames {
 	override function getShaderInputNames() : InputNames {
 		return currentShader.inputID;
 		return currentShader.inputID;
 	}
 	}
@@ -800,8 +862,22 @@ class VulkanDriver extends Driver {
 				flags.set(s.vertex ? VERTEX : FRAGMENT);
 				flags.set(s.vertex ? VERTEX : FRAGMENT);
 				command.pushConstants(currentShader.layout, flags, s.pushConstantsOffset + s.globalsOffset, buf.params.length*4, hl.Bytes.getArray(buf.params.toArray()));
 				command.pushConstants(currentShader.layout, flags, s.pushConstantsOffset + s.globalsOffset, buf.params.length*4, hl.Bytes.getArray(buf.params.toArray()));
 			}
 			}
-		case Buffers:
 		case Textures:
 		case Textures:
+			if( buf.tex.length > 0 ) {
+				var s = currentShader.samplerSets[currentFrameIndex];
+				var imageInfo = new VkDescriptorImageInfo();
+				imageInfo.imageView = buf.tex[0].t.view;
+				imageInfo.sampler = defaultSampler;
+				imageInfo.imageLayout = SHADER_READ_ONLY_OPTIMAL;
+				var write = new VkWriteDescriptorSet();
+				write.descriptorCount = 1;
+				write.descriptorType = COMBINED_IMAGE_SAMPLER;
+				write.dstSet = s;
+				write.pImageInfo = makeArray([imageInfo]);
+				ctx.updateDescriptorSets(1, makeArray([write]), 0, null);
+				command.bindDescriptorSets(GRAPHICS, currentShader.layout, 0, 1, makeArray([s]), 0, null);
+			}
+		case Buffers:
 		}
 		}
 	}
 	}
 
 

+ 3 - 0
hxd/App.hx

@@ -142,6 +142,9 @@ class App implements h3d.IDrawable {
 			init();
 			init();
 			hxd.Timer.skip();
 			hxd.Timer.skip();
 			mainLoop();
 			mainLoop();
+			#if heaps_vulkan
+			engine.driver.present();
+			#end
 			hxd.System.setLoop(mainLoop);
 			hxd.System.setLoop(mainLoop);
 			hxd.Key.initialize();
 			hxd.Key.initialize();
 		});
 		});

+ 22 - 2
hxsl/GlslOut.hx

@@ -1,5 +1,5 @@
 package hxsl;
 package hxsl;
-import hxsl.Ast;
+using hxsl.Ast;
 
 
 class GlslOut {
 class GlslOut {
 
 
@@ -56,6 +56,7 @@ class GlslOut {
 	var outIndex : Int = 0;
 	var outIndex : Int = 0;
 	var inputIndex : Int = 0;
 	var inputIndex : Int = 0;
 	var varyingIndex : Int = 0;
 	var varyingIndex : Int = 0;
+	var textureIndex : Int = 0;
 	var vulkanParametersPadding : Int = 0;
 	var vulkanParametersPadding : Int = 0;
 	public var varNames : Map<Int,String>;
 	public var varNames : Map<Int,String>;
 	public var glES : Null<Float>;
 	public var glES : Null<Float>;
@@ -606,6 +607,9 @@ class GlslOut {
 				if( v.type.match(TBuffer(_)) )
 				if( v.type.match(TBuffer(_)) )
 					add("layout(std140) ");
 					add("layout(std140) ");
 				add("uniform ");
 				add("uniform ");
+			} else {
+				if( isSampler(v.type) )
+					add('layout(binding=${textureIndex++}) uniform ');
 			}
 			}
 		case Input:
 		case Input:
 			if( isVulkan )
 			if( isVulkan )
@@ -643,6 +647,15 @@ class GlslOut {
 		add(";\n");
 		add(";\n");
 	}
 	}
 
 
+	function isSampler( t : Type ) {
+		if( t.isSampler() )
+			return true;
+		return switch( t ) {
+		case TArray(t,_): t.isSampler();
+		default: false;
+		}
+	}
+
 	function initVars( s : ShaderData ){
 	function initVars( s : ShaderData ){
 		outIndex = 0;
 		outIndex = 0;
 		inputIndex = 0;
 		inputIndex = 0;
@@ -659,16 +672,23 @@ class GlslOut {
 			if( params.length > 0 || globals.length > 0 ) {
 			if( params.length > 0 || globals.length > 0 ) {
 				add("layout( push_constant ) uniform _Constants_ {\n");
 				add("layout( push_constant ) uniform _Constants_ {\n");
 				if( vulkanParametersPadding > 0 )
 				if( vulkanParametersPadding > 0 )
-					add("\tvec4 __dummy["+vulkanParametersPadding+"];\n");
+					add("\tvec4 _dummy_padding_["+vulkanParametersPadding+"];\n");
 				for( v in globals ) {
 				for( v in globals ) {
+					if( isSampler(v.type) ) continue;
 					add("\t");
 					add("\t");
 					initVar(v);
 					initVar(v);
 				}
 				}
 				for( v in params ) {
 				for( v in params ) {
+					if( isSampler(v.type) ) continue;
 					add("\t");
 					add("\t");
 					initVar(v);
 					initVar(v);
 				}
 				}
 				add("};\n");
 				add("};\n");
+				for( v in globals.concat(params) ) {
+					if( isSampler(v.type) ) {
+						initVar(v);
+					}
+				}
 			}
 			}
 		} else {
 		} else {
 			for( v in s.vars )
 			for( v in s.vars )