<commit_before>package h3d.pass;

@:build(hxsl.Macros.buildGlobals())
@:access(h3d.mat.Pass)
class Default extends Base {

	var manager : h3d.shader.Manager;
	var globals(get, never) : hxsl.Globals;
	var cachedBuffer : h3d.shader.Buffers;

	var hasTargetDepth : Bool;
	var textureCache : Array<h3d.mat.Texture>;
	var textureCachePosition : Int = 0;
	var textureCacheFrame : Int;

	public var lightSystem : LightSystem;

	inline function get_globals() return manager.globals;

	@global("camera.view") var cameraView : h3d.Matrix = ctx.camera.mcam;
	@global("camera.proj") var cameraProj : h3d.Matrix = ctx.camera.mproj;
	@global("camera.position") var cameraPos : h3d.Vector = ctx.camera.pos;
	@global("camera.projDiag") var cameraProjDiag : h3d.Vector = new h3d.Vector(ctx.camera.mproj._11,ctx.camera.mproj._22,ctx.camera.mproj._33,ctx.camera.mproj._44);
	@global("camera.viewProj") var cameraViewProj : h3d.Matrix = ctx.camera.m;
	@global("camera.inverseViewProj") var cameraInverseViewProj : h3d.Matrix = ctx.camera.getInverseViewProj();
	@global("global.time") var globalTime : Float = ctx.time;
	@global("global.modelView") var globalModelView : h3d.Matrix;
	@global("global.modelViewInverse") var globalModelViewInverse : h3d.Matrix;
	@global("global.flipY") var globalFlipY : Float = {
		var t = ctx.engine.getTarget();
		t != null && !t.flags.has(TargetNoFlipY) ? -1 : 1;
	}

	public function new() {
		super();
		manager = new h3d.shader.Manager(getOutputs());
		initGlobals();
		lightSystem = new LightSystem(globals);

		hasTargetDepth = h3d.Engine.getCurrent().driver.hasFeature(TargetDepthBuffer);
		textureCache = [];
	}

	function getOutputs() {
		return ["output.position", "output.color"];
	}

	override function compileShader( p : h3d.mat.Pass ) {
		var out = [for( s in p.getShadersRec() ) s];
		out.reverse();
		return manager.compileShaders(out);
	}

	override function getLightSystem() {
		return lightSystem;
	}

	function getTargetTexture( name : String, width : Int, height : Int, hasDepth=true ) {
		if( textureCacheFrame != ctx.frame ) {
			// dispose extra textures we didn't use
			while( textureCache.length > textureCachePosition ) {
				var t = textureCache.pop();
				if( t != null ) t.dispose();
			}
			textureCacheFrame = ctx.frame;
			textureCachePosition = 0;
		}
		var t = textureCache[textureCachePosition];
		if( t == null || t.isDisposed() || t.width != width || t.height != height ) {
			if( t != null ) t.dispose();
			var flags : Array<h3d.mat.Data.TextureFlags> = [Target, TargetNoFlipY];
			if( hasDepth ) flags.push(hasTargetDepth ? TargetDepth : TargetUseDefaultDepth);
			t = new h3d.mat.Texture(width, height, flags);
			textureCache[textureCachePosition] = t;
		}
		t.setName(name);
		textureCachePosition++;
		return t;
	}

	@:access(h3d.scene)
	function setupShaders( passes : Object ) {
		var p = passes;
		var lightInit = false;
		var instances = [];
		while( p != null ) {
			var shaders = p.pass.getShadersRec();
			if( p.pass.enableLights && lightSystem != null ) {
				if( !lightInit ) {
					lightSystem.initLights(ctx.lights);
					lightInit = true;
				}
				shaders = lightSystem.computeLight(p.obj, shaders);
			}
			var count = 0;
			for( s in shaders )
				p.shaders[count++] = s;
			// TODO : allow reversed shader compilation !
			// reverse
			for( n in 0...count >> 1 ) {
				var n2 = count - 1 - n;
				var tmp = p.shaders[n];
				p.shaders[n] = p.shaders[n2];
				p.shaders[n2] = tmp;
			}
			for( i in 0...count ) {
				var s = p.shaders[i];
 				s.updateConstants(globals);
				instances[i] = @:privateAccess s.instance;
			}
			instances[count] = null; // mark end
			p.shader = manager.compileInstances(instances);
			p = p.next;
		}
	}

	static inline function sortByShader( o1 : Object, o2 : Object ) {
		var d = o1.shader.id - o2.shader.id;
		if( d != 0 ) return d;
		// TODO : sort by textures
		return 0;
	}

	function uploadParams() {<commit_msg>allow to customize shaders in sub pass<commit_after>package h3d.pass;

@:build(hxsl.Macros.buildGlobals())
@:access(h3d.mat.Pass)
class Default extends Base {

	var manager : h3d.shader.Manager;
	var globals(get, never) : hxsl.Globals;
	var cachedBuffer : h3d.shader.Buffers;

	var hasTargetDepth : Bool;
	var textureCache : Array<h3d.mat.Texture>;
	var textureCachePosition : Int = 0;
	var textureCacheFrame : Int;

	public var lightSystem : LightSystem;

	inline function get_globals() return manager.globals;

	@global("camera.view") var cameraView : h3d.Matrix = ctx.camera.mcam;
	@global("camera.proj") var cameraProj : h3d.Matrix = ctx.camera.mproj;
	@global("camera.position") var cameraPos : h3d.Vector = ctx.camera.pos;
	@global("camera.projDiag") var cameraProjDiag : h3d.Vector = new h3d.Vector(ctx.camera.mproj._11,ctx.camera.mproj._22,ctx.camera.mproj._33,ctx.camera.mproj._44);
	@global("camera.viewProj") var cameraViewProj : h3d.Matrix = ctx.camera.m;
	@global("camera.inverseViewProj") var cameraInverseViewProj : h3d.Matrix = ctx.camera.getInverseViewProj();
	@global("global.time") var globalTime : Float = ctx.time;
	@global("global.modelView") var globalModelView : h3d.Matrix;
	@global("global.modelViewInverse") var globalModelViewInverse : h3d.Matrix;
	@global("global.flipY") var globalFlipY : Float = {
		var t = ctx.engine.getTarget();
		t != null && !t.flags.has(TargetNoFlipY) ? -1 : 1;
	}

	public function new() {
		super();
		manager = new h3d.shader.Manager(getOutputs());
		initGlobals();
		lightSystem = new LightSystem(globals);

		hasTargetDepth = h3d.Engine.getCurrent().driver.hasFeature(TargetDepthBuffer);
		textureCache = [];
	}

	function getOutputs() {
		return ["output.position", "output.color"];
	}

	override function compileShader( p : h3d.mat.Pass ) {
		var out = [for( s in p.getShadersRec() ) s];
		out.reverse();
		return manager.compileShaders(out);
	}

	override function getLightSystem() {
		return lightSystem;
	}

	function getTargetTexture( name : String, width : Int, height : Int, hasDepth=true ) {
		if( textureCacheFrame != ctx.frame ) {
			// dispose extra textures we didn't use
			while( textureCache.length > textureCachePosition ) {
				var t = textureCache.pop();
				if( t != null ) t.dispose();
			}
			textureCacheFrame = ctx.frame;
			textureCachePosition = 0;
		}
		var t = textureCache[textureCachePosition];
		if( t == null || t.isDisposed() || t.width != width || t.height != height ) {
			if( t != null ) t.dispose();
			var flags : Array<h3d.mat.Data.TextureFlags> = [Target, TargetNoFlipY];
			if( hasDepth ) flags.push(hasTargetDepth ? TargetDepth : TargetUseDefaultDepth);
			t = new h3d.mat.Texture(width, height, flags);
			textureCache[textureCachePosition] = t;
		}
		t.setName(name);
		textureCachePosition++;
		return t;
	}

	function processShaders( p : Object, shaders : hxsl.ShaderList ) {
		return shaders;
	}

	@:access(h3d.scene)
	function setupShaders( passes : Object ) {
		var p = passes;
		var lightInit = false;
		var instances = [];
		while( p != null ) {
			var shaders = p.pass.getShadersRec();
			if( p.pass.enableLights && lightSystem != null ) {
				if( !lightInit ) {
					lightSystem.initLights(ctx.lights);
					lightInit = true;
				}
				shaders = lightSystem.computeLight(p.obj, shaders);
			}
			shaders = processShaders(p, shaders);
			var count = 0;
			for( s in shaders )
				p.shaders[count++] = s;
			// TODO : allow reversed shader compilation !
			// reverse
			for( n in 0...count >> 1 ) {
				var n2 = count - 1 - n;
				var tmp = p.shaders[n];
				p.shaders[n] = p.shaders[n2];
				p.shaders[n2] = tmp;
			}
			for( i in 0...count ) {
				var s = p.shaders[i];
 				s.updateConstants(globals);
				instances[i] = @:privateAccess s.instance;
			}
			instances[count] = null; // mark end
			p.shader = manager.compileInstances(instances);
			p = p.next;
		}
	}

	static inline function sortByShader( o1 : Object, o2 : Object ) {
		var d = o1.shader.id - o2.shader.id;
		if( d != 0 ) return d;
		// TODO : sort by textures
		return 0;
	}

	function uploadParams() {
{'fraction_non_alphanumeric': 0.1432850723914129, 'fraction_numerical': 0.011482775836245632, 'mean_word_length': 3.68322618351841, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6468626', 'n_tokens_mistral': 3144, 'n_tokens_neox': 2809, 'n_words': 637}