1/* 2 * Copyright (c) 2022-2023 Shenzhen Kaihong Digital Industry Development Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16const { shaderFastVs, shaderFastFs } = require('./shaders/shader_fast'); 17const { NapiLog } = require('../../ir/NapiLog.js'); 18import { gl } from '../GLFrame.js'; 19 20export class XShader { 21 static gi() { 22 if (XShader.pinstance_ == null) XShader.pinstance_ = new XShader(); 23 return XShader.pinstance_; 24 } 25 constructor() { 26 this.pUseingShader = null; 27 this.shaders = []; 28 this.initFastShader(); 29 } 30 initFastShader() { 31 this.shaderFast = { program: this.initShader(shaderFastVs, shaderFastFs) }; 32 33 this.shaderFast.position = gl.getAttribLocation( 34 this.shaderFast['program'], 35 'position' 36 ); 37 this.shaderFast.aTexCoord = gl.getAttribLocation( 38 this.shaderFast['program'], 39 'aTexCoord' 40 ); 41 this.shaderFast.ext1 = gl.getAttribLocation( 42 this.shaderFast['program'], 43 'ext1' 44 ); 45 this.shaderFast.ext2 = gl.getAttribLocation( 46 this.shaderFast['program'], 47 'ext2' 48 ); 49 this.shaderFast.inColor = gl.getAttribLocation( 50 this.shaderFast['program'], 51 'inColor' 52 ); 53 54 this.shaderFast.uMat = gl.getUniformLocation( 55 this.shaderFast['program'], 56 'uMat' 57 ); 58 59 this.shaderFast.tex = {}; 60 for (let i = 0; i < 16; i++) { 61 this.shaderFast.tex[i] = gl.getUniformLocation( 62 this.shaderFast['program'], 63 'tex' + i 64 ); 65 } 66 67 this.shaders[XShader.ID_SHADER_FAST] = this.shaderFast; 68 this.use(XShader.ID_SHADER_FAST); 69 } 70 use(sid) { 71 gl.useProgram(this.shaders[sid]['program']); 72 this.pUseingShader = this.shaders[sid]; 73 return this.pUseingShader; 74 } 75 initShader(vss, fss) { 76 var vs = gl.createShader(gl.VERTEX_SHADER); 77 gl.shaderSource(vs, vss); 78 gl.compileShader(vs); 79 if (!gl.getShaderParameter(vs, gl.COMPILE_STATUS)) { 80 NapiLog.logError( 81 'error occured compiling the shaders:' + gl.getShaderInfoLog(vs) 82 ); 83 return null; 84 } 85 86 var fs = gl.createShader(gl.FRAGMENT_SHADER); 87 gl.shaderSource(fs, fss); 88 gl.compileShader(fs); 89 if (!gl.getShaderParameter(fs, gl.COMPILE_STATUS)) { 90 NapiLog.logError( 91 'error occured compiling the shaders:' + gl.getShaderInfoLog(fs) 92 ); 93 return null; 94 } 95 96 var ret = gl.createProgram(); 97 98 gl.attachShader(ret, vs); 99 gl.attachShader(ret, fs); 100 gl.linkProgram(ret); 101 102 if (!gl.getProgramParameter(ret, gl.LINK_STATUS)) { 103 NapiLog.logError('unable to initialize!'); 104 return; 105 } 106 107 gl.deleteShader(vs); 108 gl.deleteShader(fs); 109 return ret; 110 } 111} 112 113XShader.ID_SHADER_FAST = 2; 114 115XShader.pinstance_ = null; 116