Skip to content
Snippets Groups Projects
Select Git revision
  • 320144b34caba287b4a01a42b4de1f1f1c512bac
  • master default protected
  • rednatco-v2
  • rednatco
  • test
  • ntc-tube-uniform-color
  • ntc-tube-missing-atoms
  • restore-vertex-array-per-program
  • watlas2
  • dnatco_new
  • cleanup-old-nodejs
  • webmmb
  • fix_auth_seq_id
  • update_deps
  • ext_dev
  • ntc_balls
  • nci-2
  • plugin
  • bugfix-0.4.5
  • nci
  • servers
  • v0.5.0-dev.1
  • v0.4.5
  • v0.4.4
  • v0.4.3
  • v0.4.2
  • v0.4.1
  • v0.4.0
  • v0.3.12
  • v0.3.11
  • v0.3.10
  • v0.3.9
  • v0.3.8
  • v0.3.7
  • v0.3.6
  • v0.3.5
  • v0.3.4
  • v0.3.3
  • v0.3.2
  • v0.3.1
  • v0.3.0
41 results

state.ts

Blame
  • shader.ts 1.84 KiB
    /**
     * Copyright (c) 2018-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
     *
     * @author Alexander Rose <alexander.rose@weirdbyte.de>
     */
    
    import { idFactory } from '../../mol-util/id-factory';
    import { GLRenderingContext } from './compat';
    import { isDebugMode } from '../../mol-util/debug';
    
    const getNextShaderId = idFactory();
    
    function addLineNumbers(source: string) {
        const lines = source.split('\n');
        for (let i = 0; i < lines.length; ++i) {
            lines[i] = (i + 1) + ': ' + lines[i];
        }
        return lines.join('\n');
    }
    
    export type ShaderType = 'vert' | 'frag'
    export type ShaderProps = { type: ShaderType, source: string }
    export interface Shader {
        readonly id: number
        attach: (program: WebGLProgram) => void
        reset: () => void
        destroy: () => void
    }
    
    export function getShader(gl: GLRenderingContext, props: ShaderProps) {
        const { type, source } = props;
        const shader = gl.createShader(type === 'vert' ? gl.VERTEX_SHADER : gl.FRAGMENT_SHADER);
        if (shader === null) {
            throw new Error(`Error creating ${type} shader`);
        }
    
        gl.shaderSource(shader, source);
        gl.compileShader(shader);
    
        if (isDebugMode && gl.getShaderParameter(shader, gl.COMPILE_STATUS) === false) {
            console.warn(`'${type}' shader info log '${gl.getShaderInfoLog(shader)}'\n${addLineNumbers(source)}`);
            throw new Error(`Error compiling ${type} shader`);
        }
    
        return shader;
    }
    
    export function createShader(gl: GLRenderingContext, props: ShaderProps): Shader {
        let shader = getShader(gl, props);
    
        return {
            id: getNextShaderId(),
            attach: (program: WebGLProgram) => {
                gl.attachShader(program, shader);
            },
    
            reset: () => {
                shader = getShader(gl, props);
            },
            destroy: () => {
                gl.deleteShader(shader);
            }
        };
    }