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

/// <reference path="framebuffer-desc.ts"/>
/// <reference path="texture.ts"/>
/// <reference path="rendercontext.ts"/>

namespace sd.render {

	var fboBugs = {
		mustHaveAColourAtt: <boolean>null
	};

	function fboMustHaveAColourAttachment(rc: RenderContext) {
		if (fboBugs.mustHaveAColourAtt === null) {
			var gl = rc.gl;
			var fboBinding = gl.getParameter(gl.FRAMEBUFFER_BINDING);

			var fbo = gl.createFramebuffer();
			gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
		
			// -- create and attach depth buffer
			var depthBuf = gl.createRenderbuffer();
			gl.bindRenderbuffer(gl.RENDERBUFFER, depthBuf);
			gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, 160, 120);
			gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, depthBuf);

			// -- specify empty draw buffer list
			rc.extDrawBuffers.drawBuffersWEBGL([gl.NONE]);

			// This bug occurs on Mac OS X with Safari 9 and Chrome 45, it is due to faulty
			// setup of DRAW and READ buffer bindings in the underlying GL4 context.
			// Bugs have been filed.		
			var fbStatus = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
			fboBugs.mustHaveAColourAtt = (fbStatus != gl.FRAMEBUFFER_COMPLETE);

			gl.bindFramebuffer(gl.FRAMEBUFFER, fboBinding);
			gl.bindTexture(gl.TEXTURE_2D, null);

			gl.deleteFramebuffer(fbo);
			gl.deleteRenderbuffer(depthBuf);
		}

		return fboBugs.mustHaveAColourAtt;
	}


	export function allocateTexturesForFrameBuffer(rc: RenderContext, desc: FrameBufferAllocationDescriptor): FrameBufferDescriptor {
		var fbDesc = makeFrameBufferDescriptor();

		var width = desc.width;
		var height = desc.height;
	
		// -- default to viewport size if not explicitly specified
		if (width == 0 && height == 0) {
			width = rc.gl.drawingBufferWidth;
			height = rc.gl.drawingBufferHeight;
		}
	
		// -- colour
		for (var colourAttIndex = 0; colourAttIndex < desc.colourPixelFormats.length; ++colourAttIndex) {
			if (desc.colourPixelFormats[colourAttIndex] != PixelFormat.None) {
				var texDesc = makeTextureDescriptor();
				texDesc.textureClass = TextureClass.Tex2D;
				texDesc.dim.width = width;
				texDesc.dim.height = height;
				texDesc.sampling.repeatS = texDesc.sampling.repeatT = TextureRepeatMode.ClampToEdge;
				texDesc.sampling.mipFilter = TextureMipFilter.None;
				texDesc.pixelFormat = desc.colourPixelFormats[colourAttIndex];
				texDesc.usageHint = desc.colourUsageHints[colourAttIndex];

				var attachment = fbDesc.colourAttachments[colourAttIndex];
				attachment.texture = new Texture(rc, texDesc);<commit_msg>detect and work around FBO 0-colour bug when creating FBO attachments<commit_after>// framebuffer - FrameBuffer objects
// Part of Stardazed TX
// (c) 2015 by <NAME> - @zenmumbler

/// <reference path="framebuffer-desc.ts"/>
/// <reference path="texture.ts"/>
/// <reference path="rendercontext.ts"/>

namespace sd.render {

	var fboBugs = {
		mustHaveAColourAtt: <boolean>null
	};

	function fboMustHaveAColourAttachment(rc: RenderContext) {
		if (fboBugs.mustHaveAColourAtt === null) {
			var gl = rc.gl;
			var fboBinding = gl.getParameter(gl.FRAMEBUFFER_BINDING);

			var fbo = gl.createFramebuffer();
			gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
		
			// -- create and attach depth buffer
			var depthBuf = gl.createRenderbuffer();
			gl.bindRenderbuffer(gl.RENDERBUFFER, depthBuf);
			gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, 160, 120);
			gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, depthBuf);

			// -- specify empty draw buffer list
			rc.extDrawBuffers.drawBuffersWEBGL([gl.NONE]);

			// This bug occurs on Mac OS X in Safari 9 and Chrome 45, it is due to faulty
			// setup of DRAW and READ buffer bindings in the underlying GL4 context.
			// Bugs have been filed.		
			var fbStatus = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
			fboBugs.mustHaveAColourAtt = (fbStatus != gl.FRAMEBUFFER_COMPLETE);

			gl.bindFramebuffer(gl.FRAMEBUFFER, fboBinding);
			gl.bindTexture(gl.TEXTURE_2D, null);

			gl.deleteFramebuffer(fbo);
			gl.deleteRenderbuffer(depthBuf);
		}

		return fboBugs.mustHaveAColourAtt;
	}


	export function allocateTexturesForFrameBuffer(rc: RenderContext, desc: FrameBufferAllocationDescriptor): FrameBufferDescriptor {
		var fbDesc = makeFrameBufferDescriptor();

		var width = desc.width;
		var height = desc.height;
	
		// -- default to viewport size if not explicitly specified
		if (width == 0 && height == 0) {
			width = rc.gl.drawingBufferWidth;
			height = rc.gl.drawingBufferHeight;
		}
	
		// -- colour
		if ((desc.colourPixelFormats[0] == PixelFormat.None) && fboMustHaveAColourAttachment(rc)) {
			// work around FBO bug, see fboMustHaveAColourAttachment function
			desc.colourPixelFormats[0] = PixelFormat.RGB8;
		}

		for (var colourAttIndex = 0; colourAttIndex < desc.colourPixelFormats.length; ++colourAttIndex) {
			if (desc.colourPixelFormats[colourAttIndex] != PixelFormat.None) {
				var texDesc = makeTextureDescriptor();
				texDesc.textureClass = TextureClass.Tex2D;
				texDesc.dim.width = width;
				texDesc.dim.height = height;
				texDesc.sampling.repeatS = texDesc.sampling.repeatT = TextureRepeatMode.ClampToEdge;
				texDesc.sampling.mipFilter = TextureMipFilter.None;
				texDesc.pixelFormat = desc.colourPixelFormats[colourAttIndex];
				texDesc.usageHint = desc.colourUsageHints[colourAttIndex];

				var attachment = fbDesc.colourAttachments[colourAttIndex];
				attachment.texture = new Texture(rc, texDesc);
{'fraction_non_alphanumeric': 0.11019677996422182, 'fraction_numerical': 0.008228980322003579, 'mean_word_length': 4.196096654275093, 'pattern_counts': {'":': 0, '<': 15, '<?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': '2926695', 'n_tokens_mistral': 2071, 'n_tokens_neox': 1774, 'n_words': 388}