<commit_before>// renderpass - RenderPass objects
// Part of Stardazed TX
// (c) 2015 by <NAME> - @zenmumbler

/// <reference path="renderpass-desc.ts"/>
/// <reference path="pipeline.ts"/>
/// <reference path="framebuffer.ts"/>
/// <reference path="mesh.ts"/>
/// <reference path="rendercontext.ts"/>

namespace sd.render {

	export function runRenderPass(rc: RenderContext, rpDesc: RenderPassDescriptor, frameBuffer: FrameBuffer, passFunc: (rp: RenderPass) => void) {
		var rp = new RenderPass(rc, rpDesc, frameBuffer);
		rp.setup();
		passFunc(rp);
		rp.teardown();
	}


	function glDepthFuncForDepthTest(rc: RenderContext, depthTest: DepthTest) {
		switch (depthTest) {
			case DepthTest.AllowAll:
				return rc.gl.ALWAYS;
			case DepthTest.DenyAll:
				return rc.gl.NEVER;
			case DepthTest.Less:
				return rc.gl.LESS;
			case DepthTest.LessOrEqual:
				return rc.gl.LEQUAL;
			case DepthTest.Equal:
				return rc.gl.EQUAL;
			case DepthTest.NotEqual:
				return rc.gl.NOTEQUAL;
			case DepthTest.GreaterOrEqual:
				return rc.gl.GEQUAL;
			case DepthTest.Greater:
				return rc.gl.GREATER;

			default:
				return rc.gl.NONE;
		}
	}


	export class RenderPass {
		private pipeline_: Pipeline = null;
		private mesh_: Mesh = null;

		constructor(private rc: RenderContext, private desc_: RenderPassDescriptor, private frameBuffer_: FrameBuffer = null) {
			assert(desc_.clearColour.length >= 4);
		}


		setup() {
			var gl = this.rc.gl;

			if (this.frameBuffer_) {
				this.frameBuffer_.bind();

				// auto-set viewport to FB dimensions (good idea?)
				var port = render.makeViewport();
				port.width = this.frameBuffer_.width;
				port.height = this.frameBuffer_.height;
				this.setViewPort(port);
			}
			else {
				// set viewport to full canvas
				this.setViewPort(render.makeViewport());	
			}

			// -- clear indicated buffers
			var glClearMask = 0;
			if (this.desc_.clearMask & ClearMask.Colour) {
				gl.clearColor(this.desc_.clearColour[0], this.desc_.clearColour[1], this.desc_.clearColour[2], this.desc_.clearColour[3]);
				glClearMask |= gl.COLOR_BUFFER_BIT;
			}
			if (this.desc_.clearMask & ClearMask.Depth) {
				gl.clearDepth(this.desc_.clearDepth);
				glClearMask |= gl.DEPTH_BUFFER_BIT;
			}
			if (this.desc_.clearMask & ClearMask.Stencil) {
				gl.clearStencil(this.desc_.clearStencil);
				glClearMask |= gl.STENCIL_BUFFER_BIT;
			}
			if (glClearMask) {
				gl.clear(glClearMask);
			}
		}


		teardown() {
			this.setPipeline(null); // will implicitly clear mesh as well

			if (this.frameBuffer_) {
				this.frameBuffer_.unbind();
			}
		}


		// -- observers
		get frameBuffer() { return this.frameBuffer_; }


		// -- render state
		setPipeline(pipeline: Pipeline) {
			if (pipeline === this.pipeline_)
				return;

			if (! pipeline) {
				// only need to explicitly unbind if there is no replacement pipeline
				if (this.mesh_) {
					this.mesh_.unbind(this.pipeline_);
					this.mesh_ = null;
				}
				this.pipeline_.unbind();
			}

			this.pipeline_ = pipeline;
			if (this.pipeline_) {
				// TODO: validate Pipeline against FrameBuffer
				this.pipeline_.bind();
			}
		}


		setMesh(mesh: Mesh) {
			assert(this.pipeline_, "You must set the Pipeline before setting the Mesh");
			if (this.mesh_ === mesh)
				return;

			if (this.mesh_ && !mesh) {
				// only need to explicitly unbind if there is no replacement mesh
				this.mesh_.unbind(this.pipeline_);
			}
			this.mesh_ = mesh;
			if (this.mesh_) {
				this.mesh_.bind(this.pipeline_);
			}<commit_msg>Guess what, you DO need to do that or weird, weird shit will happen<commit_after>// renderpass - RenderPass objects
// Part of Stardazed TX
// (c) 2015 by <NAME> - @zenmumbler

/// <reference path="renderpass-desc.ts"/>
/// <reference path="pipeline.ts"/>
/// <reference path="framebuffer.ts"/>
/// <reference path="mesh.ts"/>
/// <reference path="rendercontext.ts"/>

namespace sd.render {

	export function runRenderPass(rc: RenderContext, rpDesc: RenderPassDescriptor, frameBuffer: FrameBuffer, passFunc: (rp: RenderPass) => void) {
		var rp = new RenderPass(rc, rpDesc, frameBuffer);
		rp.setup();
		passFunc(rp);
		rp.teardown();
	}


	function glDepthFuncForDepthTest(rc: RenderContext, depthTest: DepthTest) {
		switch (depthTest) {
			case DepthTest.AllowAll:
				return rc.gl.ALWAYS;
			case DepthTest.DenyAll:
				return rc.gl.NEVER;
			case DepthTest.Less:
				return rc.gl.LESS;
			case DepthTest.LessOrEqual:
				return rc.gl.LEQUAL;
			case DepthTest.Equal:
				return rc.gl.EQUAL;
			case DepthTest.NotEqual:
				return rc.gl.NOTEQUAL;
			case DepthTest.GreaterOrEqual:
				return rc.gl.GEQUAL;
			case DepthTest.Greater:
				return rc.gl.GREATER;

			default:
				return rc.gl.NONE;
		}
	}


	export class RenderPass {
		private pipeline_: Pipeline = null;
		private mesh_: Mesh = null;

		constructor(private rc: RenderContext, private desc_: RenderPassDescriptor, private frameBuffer_: FrameBuffer = null) {
			assert(desc_.clearColour.length >= 4);
		}


		setup() {
			var gl = this.rc.gl;

			if (this.frameBuffer_) {
				this.frameBuffer_.bind();

				// auto-set viewport to FB dimensions (good idea?)
				var port = render.makeViewport();
				port.width = this.frameBuffer_.width;
				port.height = this.frameBuffer_.height;
				this.setViewPort(port);
			}
			else {
				// set viewport to full canvas
				this.setViewPort(render.makeViewport());	
			}

			// -- clear indicated buffers
			var glClearMask = 0;
			if (this.desc_.clearMask & ClearMask.Colour) {
				gl.clearColor(this.desc_.clearColour[0], this.desc_.clearColour[1], this.desc_.clearColour[2], this.desc_.clearColour[3]);
				glClearMask |= gl.COLOR_BUFFER_BIT;
			}
			if (this.desc_.clearMask & ClearMask.Depth) {
				gl.clearDepth(this.desc_.clearDepth);
				glClearMask |= gl.DEPTH_BUFFER_BIT;
			}
			if (this.desc_.clearMask & ClearMask.Stencil) {
				gl.clearStencil(this.desc_.clearStencil);
				glClearMask |= gl.STENCIL_BUFFER_BIT;
			}
			if (glClearMask) {
				gl.clear(glClearMask);
			}
		}


		teardown() {
			this.setPipeline(null); // will implicitly clear mesh as well

			if (this.frameBuffer_) {
				this.frameBuffer_.unbind();
			}
		}


		// -- observers
		get frameBuffer() { return this.frameBuffer_; }


		// -- render state
		setPipeline(pipeline: Pipeline) {
			if (pipeline === this.pipeline_)
				return;

			if (this.mesh_) {
				this.mesh_.unbind(this.pipeline_);
				this.mesh_ = null;
			}
			if (this.pipeline_) {
				this.pipeline_.unbind();
			}

			this.pipeline_ = pipeline;
			if (this.pipeline_) {
				// TODO: validate Pipeline against FrameBuffer
				this.pipeline_.bind();
			}
		}


		setMesh(mesh: Mesh) {
			assert(this.pipeline_, "You must set the Pipeline before setting the Mesh");
			if (this.mesh_ === mesh)
				return;

			if (this.mesh_ && !mesh) {
				// only need to explicitly unbind if there is no replacement mesh
				this.mesh_.unbind(this.pipeline_);
			}
			this.mesh_ = mesh;
			if (this.mesh_) {
				this.mesh_.bind(this.pipeline_);
			}
{'fraction_non_alphanumeric': 0.13024850042844902, 'fraction_numerical': 0.0028563267637817767, 'mean_word_length': 3.1097417840375585, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, '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': 1, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1631221', 'n_tokens_mistral': 2833, 'n_tokens_neox': 2369, 'n_words': 520}