<commit_before>import Color from "./../../math/color.js";
import Vector2d from "./../../math/vector2.js";
import Matrix2d from "./../../math/matrix2.js";
import WebGLCompositor from "./webgl_compositor.js";
import Renderer from "./../renderer.js";
import TextureCache from "./../texture_cache.js";
import {Texture, createAtlas } from "./../texture.js";
import { createCanvas, renderer } from "./../video.js";
import * as event from "./../../system/event.js";
import pool from "./../../system/pooling.js";
import { isPowerOfTwo, nextPowerOfTwo, TAU } from "./../../math/math.js";


/**
 * @classdesc
 * a WebGL renderer object
 * @class WebGLRenderer
 * @extends me.Renderer
 * @memberOf me
 * @constructor
 * @param {Object} options The renderer parameters
 * @param {Number} options.width The width of the canvas without scaling
 * @param {Number} options.height The height of the canvas without scaling
 * @param {HTMLCanvasElement} [options.canvas] The html canvas to draw to on screen
 * @param {Boolean} [options.doubleBuffering=false] Whether to enable double buffering
 * @param {Boolean} [options.antiAlias=false] Whether to enable anti-aliasing
 * @param {Boolean} [options.failIfMajorPerformanceCaveat=true] If true, the renderer will switch to CANVAS mode if the performances of a WebGL context would be dramatically lower than that of a native application making equivalent OpenGL calls.
 * @param {Boolean} [options.transparent=false] Whether to enable transparency on the canvas (performance hit when enabled)
 * @param {Boolean} [options.subPixel=false] Whether to enable subpixel renderering (performance hit when enabled)
 * @param {Boolean} [options.preferWebGL1=false] if true the renderer will only use WebGL 1
 * @param {String} [options.powerPreference="default"] a hint to the user agent indicating what configuration of GPU is suitable for the WebGL context ("default", "high-performance", "low-power"). To be noted that Safari and Chrome (since version 80) both default to "low-power" to save battery life and improve the user experience on these dual-GPU machines.
 * @param {Number} [options.zoomX=width] The actual width of the canvas with scaling applied
 * @param {Number} [options.zoomY=height] The actual height of the canvas with scaling applied
 * @param {me.WebGLCompositor} [options.compositor] A class that implements the compositor API
 */
class WebGLRenderer extends Renderer {

    constructor(options) {

        // parent contructor
        super(options);

        /**
         * The WebGL context
         * @name gl
         * @memberOf me.WebGLRenderer
         * type {WebGLRenderingContext}
         */
        this.context = this.gl = this.getContextGL(this.getScreenCanvas(), options.transparent);

        /**
         * The WebGL version used by this renderer (1 or 2)
         * @name WebGLVersion
         * @memberOf me.WebGLRenderer
         * @type {Number}
         * @default 1
         * @readonly
         */
        this.webGLVersion = 1;

        /**
         * The vendor string of the underlying graphics driver.
         * @name GPUVendor
         * @memberOf me.WebGLRenderer
         * @type {String}
         * @default null
         * @readonly
         */
        this.GPUVendor = null;

        /**
         * The renderer string of the underlying graphics driver.
         * @name GPURenderer
         * @memberOf me.WebGLRenderer
         * @type {String}
         * @default null
         * @readonly
         */
        this.GPURenderer = null;

        /**
         * Maximum number of texture unit supported under the current context
         * @name maxTextures
         * @memberOf me.WebGLRenderer
         * @type {Number}
         * @readonly
         */
        this.maxTextures = this.gl.getParameter(this.gl.MAX_TEXTURE_IMAGE_UNITS);

        /**
         * @ignore
         */
        this._colorStack = [];

        /**
         * @ignore
         */
        this._matrixStack = [];

        /**
         * @ignore
         */
        this._scissorStack = [];

        /**
         * @ignore
         */
        this._glPoints = [
            new Vector2d(),
            new Vector2d(),
            new Vector2d(),
            new Vector2d()
        ];

        /**
         * The current transformation matrix used for transformations on the overall scene
         * @name currentTransform
         * @type me.Matrix2d
         * @memberOf me.WebGLRenderer#
         */
        this.currentTransform = new Matrix2d();

        /**
         * The current compositor used by the renderer
         * @name currentCompositor
         * @type me.WebGLCompositor
         * @memberOf me.WebGLRenderer#
         */
        this.currentCompositor = null;

        // Create a compositor
        var Compositor = this.settings.compositor || WebGLCompositor;
        this.setCompositor(new Compositor(this));


        // default WebGL state(s)
        this.gl.disable(this.gl.DEPTH_TEST);
        this.gl.disable(this.gl.SCISSOR_TEST);
        this.gl.enable(this.gl.BLEND);

        // set default mode
        this.setBlendMode(this.settings.blendMode);

        // get GPU vendor and renderer
        var debugInfo = this.gl.getExtension("WEBGL_debug_renderer_info");
        if (debugInfo !== null) {
            this.GPUVendor = this.gl.getParameter(debugInfo.UNMASKED_VENDOR_WEBGL);
            this.GPURenderer = this.gl.getParameter(debugInfo.UNMASKED_RENDERER_WEBGL);
        }

        // Create a texture cache
        this.cache = new TextureCache(this.maxTextures);

        // to simulate context lost and restore :
        // var ctx = me.video.renderer.context.getExtension('WEBGL_lose_context');
        // ctx.loseContext()
        // reference to this renderer
        var renderer = this;
        this.getScreenCanvas().addEventListener("webglcontextlost", function (e) {
            e.preventDefault();
            renderer.isContextValid = false;
            event.emit(event.WEBGL_ONCONTEXT_LOST, renderer);
        }, false );
        // ctx.restoreContext()
        this.getScreenCanvas().addEventListener("webglcontextrestored", function () {
            renderer.reset();
            renderer.isContextValid = true;
            event.emit(event.WEBGL_ONCONTEXT_RESTORED, renderer);
        }, false );

        return this;
    }<commit_msg>remove one more unnecessary local reference to this<commit_after>import Color from "./../../math/color.js";
import Vector2d from "./../../math/vector2.js";
import Matrix2d from "./../../math/matrix2.js";
import WebGLCompositor from "./webgl_compositor.js";
import Renderer from "./../renderer.js";
import TextureCache from "./../texture_cache.js";
import {Texture, createAtlas } from "./../texture.js";
import { createCanvas, renderer } from "./../video.js";
import * as event from "./../../system/event.js";
import pool from "./../../system/pooling.js";
import { isPowerOfTwo, nextPowerOfTwo, TAU } from "./../../math/math.js";


/**
 * @classdesc
 * a WebGL renderer object
 * @class WebGLRenderer
 * @extends me.Renderer
 * @memberOf me
 * @constructor
 * @param {Object} options The renderer parameters
 * @param {Number} options.width The width of the canvas without scaling
 * @param {Number} options.height The height of the canvas without scaling
 * @param {HTMLCanvasElement} [options.canvas] The html canvas to draw to on screen
 * @param {Boolean} [options.doubleBuffering=false] Whether to enable double buffering
 * @param {Boolean} [options.antiAlias=false] Whether to enable anti-aliasing
 * @param {Boolean} [options.failIfMajorPerformanceCaveat=true] If true, the renderer will switch to CANVAS mode if the performances of a WebGL context would be dramatically lower than that of a native application making equivalent OpenGL calls.
 * @param {Boolean} [options.transparent=false] Whether to enable transparency on the canvas (performance hit when enabled)
 * @param {Boolean} [options.subPixel=false] Whether to enable subpixel renderering (performance hit when enabled)
 * @param {Boolean} [options.preferWebGL1=false] if true the renderer will only use WebGL 1
 * @param {String} [options.powerPreference="default"] a hint to the user agent indicating what configuration of GPU is suitable for the WebGL context ("default", "high-performance", "low-power"). To be noted that Safari and Chrome (since version 80) both default to "low-power" to save battery life and improve the user experience on these dual-GPU machines.
 * @param {Number} [options.zoomX=width] The actual width of the canvas with scaling applied
 * @param {Number} [options.zoomY=height] The actual height of the canvas with scaling applied
 * @param {me.WebGLCompositor} [options.compositor] A class that implements the compositor API
 */
class WebGLRenderer extends Renderer {

    constructor(options) {

        // parent contructor
        super(options);

        /**
         * The WebGL context
         * @name gl
         * @memberOf me.WebGLRenderer
         * type {WebGLRenderingContext}
         */
        this.context = this.gl = this.getContextGL(this.getScreenCanvas(), options.transparent);

        /**
         * The WebGL version used by this renderer (1 or 2)
         * @name WebGLVersion
         * @memberOf me.WebGLRenderer
         * @type {Number}
         * @default 1
         * @readonly
         */
        this.webGLVersion = 1;

        /**
         * The vendor string of the underlying graphics driver.
         * @name GPUVendor
         * @memberOf me.WebGLRenderer
         * @type {String}
         * @default null
         * @readonly
         */
        this.GPUVendor = null;

        /**
         * The renderer string of the underlying graphics driver.
         * @name GPURenderer
         * @memberOf me.WebGLRenderer
         * @type {String}
         * @default null
         * @readonly
         */
        this.GPURenderer = null;

        /**
         * Maximum number of texture unit supported under the current context
         * @name maxTextures
         * @memberOf me.WebGLRenderer
         * @type {Number}
         * @readonly
         */
        this.maxTextures = this.gl.getParameter(this.gl.MAX_TEXTURE_IMAGE_UNITS);

        /**
         * @ignore
         */
        this._colorStack = [];

        /**
         * @ignore
         */
        this._matrixStack = [];

        /**
         * @ignore
         */
        this._scissorStack = [];

        /**
         * @ignore
         */
        this._glPoints = [
            new Vector2d(),
            new Vector2d(),
            new Vector2d(),
            new Vector2d()
        ];

        /**
         * The current transformation matrix used for transformations on the overall scene
         * @name currentTransform
         * @type me.Matrix2d
         * @memberOf me.WebGLRenderer#
         */
        this.currentTransform = new Matrix2d();

        /**
         * The current compositor used by the renderer
         * @name currentCompositor
         * @type me.WebGLCompositor
         * @memberOf me.WebGLRenderer#
         */
        this.currentCompositor = null;

        // Create a compositor
        var Compositor = this.settings.compositor || WebGLCompositor;
        this.setCompositor(new Compositor(this));


        // default WebGL state(s)
        this.gl.disable(this.gl.DEPTH_TEST);
        this.gl.disable(this.gl.SCISSOR_TEST);
        this.gl.enable(this.gl.BLEND);

        // set default mode
        this.setBlendMode(this.settings.blendMode);

        // get GPU vendor and renderer
        var debugInfo = this.gl.getExtension("WEBGL_debug_renderer_info");
        if (debugInfo !== null) {
            this.GPUVendor = this.gl.getParameter(debugInfo.UNMASKED_VENDOR_WEBGL);
            this.GPURenderer = this.gl.getParameter(debugInfo.UNMASKED_RENDERER_WEBGL);
        }

        // Create a texture cache
        this.cache = new TextureCache(this.maxTextures);

        // to simulate context lost and restore :
        // var ctx = me.video.renderer.context.getExtension('WEBGL_lose_context');
        // ctx.loseContext()
        this.getScreenCanvas().addEventListener("webglcontextlost", (e) => {
            e.preventDefault();
            this.isContextValid = false;
            event.emit(event.WEBGL_ONCONTEXT_LOST, this);
        }, false );
        // ctx.restoreContext()
        this.getScreenCanvas().addEventListener("webglcontextrestored", () => {
            this.reset();
            this.isContextValid = true;
            event.emit(event.WEBGL_ONCONTEXT_RESTORED, this);
        }, false );

        return this;
    }
{'fraction_non_alphanumeric': 0.10842896715472893, 'fraction_numerical': 0.002849228333992877, 'mean_word_length': 2.3000783494384955, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '3726059', 'n_tokens_mistral': 3487, 'n_tokens_neox': 3409, 'n_words': 990}