| 1 |
/** |
| 2 |
* Copyright (c) 2018 The xterm.js authors. All rights reserved. |
| 3 |
* @license MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
import { throwIfFalsy } from 'browser/renderer/shared/RendererUtils'; |
| 7 |
|
| 8 |
/** |
| 9 |
* A matrix that when multiplies will translate 0-1 coordinates (left to right, |
| 10 |
* top to bottom) to clip space. |
| 11 |
*/ |
| 12 |
export const PROJECTION_MATRIX = new Float32Array([ |
| 13 |
2, 0, 0, 0, |
| 14 |
0, -2, 0, 0, |
| 15 |
0, 0, 1, 0, |
| 16 |
-1, 1, 0, 1 |
| 17 |
]); |
| 18 |
|
| 19 |
export function createProgram(gl: WebGLRenderingContext, vertexSource: string, fragmentSource: string): WebGLProgram | undefined { |
| 20 |
const program = throwIfFalsy(gl.createProgram()); |
| 21 |
gl.attachShader(program, throwIfFalsy(createShader(gl, gl.VERTEX_SHADER, vertexSource))); |
| 22 |
gl.attachShader(program, throwIfFalsy(createShader(gl, gl.FRAGMENT_SHADER, fragmentSource))); |
| 23 |
gl.linkProgram(program); |
| 24 |
const success = gl.getProgramParameter(program, gl.LINK_STATUS); |
| 25 |
if (success) { |
| 26 |
return program; |
| 27 |
} |
| 28 |
|
| 29 |
console.error(gl.getProgramInfoLog(program)); |
| 30 |
gl.deleteProgram(program); |
| 31 |
} |
| 32 |
|
| 33 |
export function createShader(gl: WebGLRenderingContext, type: number, source: string): WebGLShader | undefined { |
| 34 |
const shader = throwIfFalsy(gl.createShader(type)); |
| 35 |
gl.shaderSource(shader, source); |
| 36 |
gl.compileShader(shader); |
| 37 |
const success = gl.getShaderParameter(shader, gl.COMPILE_STATUS); |
| 38 |
if (success) { |
| 39 |
return shader; |
| 40 |
} |
| 41 |
|
| 42 |
console.error(gl.getShaderInfoLog(shader)); |
| 43 |
gl.deleteShader(shader); |
| 44 |
} |
| 45 |
|
| 46 |
export function expandFloat32Array(source: Float32Array, max: number): Float32Array { |
| 47 |
const newLength = Math.min(source.length * 2, max); |
| 48 |
const newArray = new Float32Array(newLength); |
| 49 |
for (let i = 0; i < source.length; i++) { |
| 50 |
newArray[i] = source[i]; |
| 51 |
} |
| 52 |
return newArray; |
| 53 |
} |
| 54 |
|
| 55 |
export class GLTexture { |
| 56 |
public texture: WebGLTexture; |
| 57 |
public version: number; |
| 58 |
|
| 59 |
constructor(texture: WebGLTexture) { |
| 60 |
this.texture = texture; |
| 61 |
this.version = -1; |
| 62 |
} |
| 63 |
} |