Browse Source

move work on hxsl3

Nicolas Cannasse 11 years ago
parent
commit
c08c60657e
11 changed files with 103 additions and 63 deletions
  1. 1 0
      engine.hxml
  2. 1 1
      engine.hxproj
  3. 1 43
      h2d/Tile.hx
  4. 2 4
      h3d/impl/GlDriver.hx
  5. 5 0
      h3d/mat/Texture.hx
  6. 16 6
      h3d/pass/Base.hx
  7. 19 0
      h3d/pass/Pass.hx
  8. 4 7
      h3d/shader/BaseMesh.hx
  9. 7 1
      h3d/shader/Texture.hx
  10. 41 0
      h3d/shader/Texture2.hx
  11. 6 1
      samples/basic/Test.hx

+ 1 - 0
engine.hxml

@@ -6,3 +6,4 @@
 -D h3d
 -D resourcesPath=samples/res
 -D source-map-content
+-dce full

+ 1 - 1
engine.hxproj

@@ -23,7 +23,7 @@
     <option flashStrict="True" />
     <option mainClass="Test" />
     <option enabledebug="False" />
-    <option additional="-lib format&#xA;-lib hxsl3&#xA;-D h3d&#xA;-D resourcesPath=samples/res&#xA;-D source-map-content" />
+    <option additional="-lib format&#xA;-lib hxsl3&#xA;-D h3d&#xA;-D resourcesPath=samples/res&#xA;-D source-map-content&#xA;-dce full" />
   </build>
   <!-- haxelib libraries -->
   <haxelib>

+ 1 - 43
h2d/Tile.hx

@@ -148,14 +148,8 @@ class Tile {
 	}
 	
 
-	static var COLOR_CACHE = new Map<Int,h3d.mat.Texture>();
 	public static function fromColor( color : Int, ?width = 1, ?height = 1, ?allocPos : h3d.impl.AllocPos ) {
-		var t = COLOR_CACHE.get(color);
-		if( t == null || t.isDisposed() ) {
-			t = h3d.mat.Texture.fromColor(color, allocPos);
-			COLOR_CACHE.set(color, t);
-		}
-		var t = new Tile(t, 0, 0, 1, 1);
+		var t = new Tile(h3d.mat.Texture.fromColor(color), 0, 0, 1, 1);
 		// scale to size
 		t.width = width;
 		t.height = height;
@@ -210,42 +204,6 @@ class Tile {
 		return new Tile(t, 0, 0, pixels.width, pixels.height);
 	}
 	
-	#if flash
-	public static function fromSprites( sprites : Array<flash.display.Sprite>, ?allocPos : h3d.impl.AllocPos ) {
-		var tmp = [];
-		var width = 0;
-		var height = 0;
-		for( s in sprites ) {
-			var g = s.getBounds(s);
-			var dx = Math.floor(g.left);
-			var dy = Math.floor(g.top);
-			var w = Math.ceil(g.right) - dx;
-			var h = Math.ceil(g.bottom) - dy;
-			tmp.push( { s : s, x : width, dx : dx, dy : dy, w : w, h : h } );
-			width += w;
-			if( height < h ) height = h;
-		}
-		var rw = 1, rh = 1;
-		while( rw < width )
-			rw <<= 1;
-		while( rh < height )
-			rh <<= 1;
-		var bmp = new flash.display.BitmapData(rw, rh, true, 0);
-		var m = new flash.geom.Matrix();
-		for( t in tmp ) {
-			m.tx = t.x-t.dx;
-			m.ty = -t.dy;
-			bmp.draw(t.s, m);
-		}
-		var main = fromBitmap(hxd.BitmapData.fromNative(bmp), allocPos);
-		bmp.dispose();
-		var tiles = [];
-		for( t in tmp )
-			tiles.push(main.sub(t.x, 0, t.w, t.h, t.dx, t.dy));
-		return tiles;
-	}
-	#end
-	
 	static function isEmpty( b : hxd.BitmapData, px, py, width, height, bg : Int ) {
 		var empty = true;
 		var xmin = width, ymin = height, xmax = 0, ymax = 0;

+ 2 - 4
h3d/impl/GlDriver.hx

@@ -169,6 +169,8 @@ class GlDriver extends Driver {
 		case Textures:
 			for( i in 0...s.textures.length ) {
 				var t = buf.tex[i];
+				gl.activeTexture(GL.TEXTURE0 + i);
+				gl.uniform1i(s.textures[i], i);
 				
 				gl.bindTexture(GL.TEXTURE_2D, t.t);
 				var flags = TFILTERS[Type.enumIndex(t.mipMap)][Type.enumIndex(t.filter)];
@@ -177,9 +179,6 @@ class GlDriver extends Driver {
 				var w = TWRAP[Type.enumIndex(t.wrap)];
 				gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_WRAP_S, w);
 				gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_WRAP_T, w);
-				
-				gl.activeTexture(GL.TEXTURE0 + i);
-				gl.uniform1i(s.textures[i], i);
 			}
 		}
 	}
@@ -193,7 +192,6 @@ class GlDriver extends Driver {
 		var diff = bits ^ curMatBits;
 		if( diff == 0 )
 			return;
-		trace(diff);
 		if( diff & Pass.culling_mask != 0 ) {
 			var cull = Pass.getCulling(bits);
 			if( cull == 0 )

+ 5 - 0
h3d/mat/Texture.hx

@@ -139,10 +139,14 @@ class Texture {
 	}
 	
 	static var tmpPixels : hxd.Pixels = null;
+	static var COLOR_CACHE = new Map<Int,h3d.mat.Texture>();
 	/**
 		Creates a 1x1 texture using the ARGB color passed as parameter.
 	**/
 	public static function fromColor( color : Int, ?allocPos : h3d.impl.AllocPos ) {
+		var t = COLOR_CACHE.get(color);
+		if( t != null && !t.isDisposed() )
+			return t;
 		var mem = h3d.Engine.getCurrent().mem;
 		var t = mem.allocTexture(1, 1, false, allocPos);
 		if( tmpPixels == null ) tmpPixels = new hxd.Pixels(1, 1, haxe.io.Bytes.alloc(4), BGRA);
@@ -152,6 +156,7 @@ class Texture {
 		tmpPixels.bytes.set(2, (color>>16) & 0xFF);
 		tmpPixels.bytes.set(3, color>>>24);
 		t.uploadPixels(tmpPixels);
+		COLOR_CACHE.set(color, t);
 		return t;
 	}
 

+ 16 - 6
h3d/pass/Base.hx

@@ -97,12 +97,22 @@ class Base {
 			fillRec(v, p.type, buf.params, p.pos);
 		}
 		var tid = 0;
-		for( p in s.textures )
-			buf.tex[tid++] = getParamValue(p, shaders);
+		for( p in s.textures ) {
+			var t = getParamValue(p, shaders);
+			if( t == null ) t = h3d.mat.Texture.fromColor(0xFFFF00FF);
+			buf.tex[tid++] = t;
+		}
 		return buf;
 	}
 	
 	@:access(hxsl.Shader)
+	public function compileShader( p : Pass ) {
+		var shaders = p.getShadersRec();
+		var instances = [for( s in shaders ) { s.updateConstants(globals); s.instance; }];
+		var shader = shaderCache.link(instances, output);
+		return shader;
+	}
+	
 	@:access(h3d.scene.Object)
 	@:access(h3d.Engine.driver)
 	public function draw( ctx : h3d.scene.RenderContext, passes : Object ) {
@@ -110,11 +120,11 @@ class Base {
 		setGlobals();
 		var p = passes;
 		while( p != null ) {
-			var pp = p.pass.parentPass;
-			var shaders = pp == null ? p.pass.shaders : pp.shaders.concat(p.pass.shaders);
-			var instances = [for( s in shaders ) { s.updateConstants(globals); s.instance; }];
-			var shader = shaderCache.link(instances, output);
+			var shaders = p.pass.getShadersRec();
+			var shader = compileShader(p.pass);
+			// TODO : sort passes by shader/textures
 			globalModelView.set(globals, p.obj.absPos);
+			// TODO : reuse buffers between calls
 			var vbuf = allocBuffer(shader.vertex, shaders);
 			var fbuf = allocBuffer(shader.fragment, shaders);
 			ctx.engine.driver.selectShader(shader.vertex, shader.fragment);

+ 19 - 0
h3d/pass/Pass.hx

@@ -54,6 +54,25 @@ class Pass {
 	public function setColorMask(r, g, b, a) {
 		this.colorMask = (r?1:0) | (g?2:0) | (b?4:0) | (a?8:0);
 	}
+	
+	public function addShader(s) {
+		shaders.push(s);
+	}
+	
+	public function removeShader(s) {
+		return shaders.remove(s);
+	}
+	
+	function getShadersRec() {
+		if( parentPass == null )
+			return shaders;
+		return parentPass.getShadersRec().concat(shaders);
+	}
 
+	public function getDebugShaderCode( scene : h3d.scene.Scene, toHxsl = true ) {
+		var shader = scene.getRenderPass(name).compileShader(this);
+		var toString = toHxsl ? function(d) return hxsl.Printer.shaderToString(d,true) : hxsl.GlslOut.toGlsl;
+		return "VERTEX=\n" + toString(shader.vertex.data) + "\n\nFRAGMENT=\n" + toString(shader.fragment.data);
+	}
 
 }

+ 4 - 7
h3d/shader/BaseMesh.hx

@@ -8,17 +8,16 @@ class BaseMesh extends hxsl.Shader {
 			var view : Mat4;
 			var proj : Mat4;
 			var position : Vec3;
-			var projDiag : Vec3; // [_11,_22,_33]
+			var projDiag : Vec3;
 			var viewProj : Mat4;
 			var inverseViewProj : Mat4;
-			@var var dir : Vec3; // allow mix of variable types in structure (each variable is independent anyway)
+			@var var dir : Vec3;
 		};
 
 		@global var global : {
 			var time : Float;
 			@perObject var modelView : Mat4;
 			@perObject var modelViewInverse : Mat4;
-			// ... other available globals in BasePass
 		};
 		
 		@input var input : {
@@ -27,12 +26,10 @@ class BaseMesh extends hxsl.Shader {
 		};
 		
 		var output : {
-			var position : Vec4; // written in vertex
-			var color : Vec4; // written in fragment
+			var position : Vec4;
+			var color : Vec4;
 		};
 		
-		// vars are always exported
-		
 		var transformedPosition : Vec3;
 		var transformedNormal : Vec3;
 		var projectedPosition : Vec4;

+ 7 - 1
h3d/shader/Texture.hx

@@ -21,7 +21,7 @@ class Texture extends hxsl.Shader {
 		
 		function fragment() {
 			var c = texture.get(calculatedUV);
-			if( killAlpha && c.a - killAlphaThreshold < 0 ) discard; // in multipass, we will have to specify if we want to keep the kill's or not
+			if( killAlpha && c.a - killAlphaThreshold < 0 ) discard;
 			if( additive )
 				pixelColor += c;
 			else
@@ -29,4 +29,10 @@ class Texture extends hxsl.Shader {
 		}
 	}
 	
+	
+	public function new(?tex) {
+		super();
+		this.texture = tex;
+	}
+	
 }

+ 41 - 0
h3d/shader/Texture2.hx

@@ -0,0 +1,41 @@
+package h3d.shader;
+
+/**
+	This is similar to [Texture] shader but uses a second UV set.
+**/
+class Texture2 extends hxsl.Shader {
+
+	static var SRC = {
+		@input var input : {
+			var uv2 : Vec2;
+		};
+		
+		@const var additive : Bool;
+		@const var killAlpha : Bool;
+		@param var killAlphaThreshold : Float;
+		
+		@param var texture : Sampler2D;
+		var calculatedUV2 : Vec2;
+		var pixelColor : Vec4;
+		
+		function vertex() {
+			calculatedUV2 = input.uv2;
+		}
+		
+		function fragment() {
+			var c = texture.get(calculatedUV2);
+			if( killAlpha && c.a - killAlphaThreshold < 0 ) discard;
+			if( additive )
+				pixelColor += c;
+			else
+				pixelColor *= c;
+		}
+	}
+	
+	
+	public function new(?tex) {
+		super();
+		this.texture = tex;
+	}
+	
+}

+ 6 - 1
samples/basic/Test.hx

@@ -27,11 +27,16 @@ class Test {
 		var tex = hxd.Res.hxlogo.toTexture();
 		var mat = new h3d.mat.MeshMaterial(tex);
 		
+		var t2 = new h3d.shader.Texture(h3d.mat.Texture.fromColor(0x80808080));
+		mat.mainPass.addShader(t2);
+		
 		scene = new Scene();
 		obj1 = new Mesh(prim, mat, scene);
 		obj2 = new Mesh(prim, new h3d.mat.MeshMaterial(), scene);
 		obj2.material.color.set(1, 0.7, 0.5);
-		
+
+			trace(mat.mainPass.getDebugShaderCode(scene));
+
 		/*mat.lightSystem = {
 			ambient : new h3d.Vector(0, 0, 0),
 			dirs : [{ dir : new h3d.Vector(-0.3,-0.5,-1), color : new h3d.Vector(1,1,1) }],