14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ciconst { shaderFastVs, shaderFastFs } = require('./shaders/shader_fast');
174514f5e3Sopenharmony_ciconst { NapiLog } = require('../../ir/NapiLog.js');
184514f5e3Sopenharmony_ciimport { gl } from '../GLFrame.js';
194514f5e3Sopenharmony_ci
204514f5e3Sopenharmony_ciexport class XShader {
214514f5e3Sopenharmony_ci  static gi() {
224514f5e3Sopenharmony_ci    if (XShader.pinstance_ == null) XShader.pinstance_ = new XShader();
234514f5e3Sopenharmony_ci    return XShader.pinstance_;
244514f5e3Sopenharmony_ci  }
254514f5e3Sopenharmony_ci  constructor() {
264514f5e3Sopenharmony_ci    this.pUseingShader = null;
274514f5e3Sopenharmony_ci    this.shaders = [];
284514f5e3Sopenharmony_ci    this.initFastShader();
294514f5e3Sopenharmony_ci  }
304514f5e3Sopenharmony_ci  initFastShader() {
314514f5e3Sopenharmony_ci    this.shaderFast = { program: this.initShader(shaderFastVs, shaderFastFs) };
324514f5e3Sopenharmony_ci
334514f5e3Sopenharmony_ci    this.shaderFast.position = gl.getAttribLocation(
344514f5e3Sopenharmony_ci      this.shaderFast['program'],
354514f5e3Sopenharmony_ci      'position'
364514f5e3Sopenharmony_ci    );
374514f5e3Sopenharmony_ci    this.shaderFast.aTexCoord = gl.getAttribLocation(
384514f5e3Sopenharmony_ci      this.shaderFast['program'],
394514f5e3Sopenharmony_ci      'aTexCoord'
404514f5e3Sopenharmony_ci    );
414514f5e3Sopenharmony_ci    this.shaderFast.ext1 = gl.getAttribLocation(
424514f5e3Sopenharmony_ci      this.shaderFast['program'],
434514f5e3Sopenharmony_ci      'ext1'
444514f5e3Sopenharmony_ci    );
454514f5e3Sopenharmony_ci    this.shaderFast.ext2 = gl.getAttribLocation(
464514f5e3Sopenharmony_ci      this.shaderFast['program'],
474514f5e3Sopenharmony_ci      'ext2'
484514f5e3Sopenharmony_ci    );
494514f5e3Sopenharmony_ci    this.shaderFast.inColor = gl.getAttribLocation(
504514f5e3Sopenharmony_ci      this.shaderFast['program'],
514514f5e3Sopenharmony_ci      'inColor'
524514f5e3Sopenharmony_ci    );
534514f5e3Sopenharmony_ci
544514f5e3Sopenharmony_ci    this.shaderFast.uMat = gl.getUniformLocation(
554514f5e3Sopenharmony_ci      this.shaderFast['program'],
564514f5e3Sopenharmony_ci      'uMat'
574514f5e3Sopenharmony_ci    );
584514f5e3Sopenharmony_ci
594514f5e3Sopenharmony_ci    this.shaderFast.tex = {};
604514f5e3Sopenharmony_ci    for (let i = 0; i < 16; i++) {
614514f5e3Sopenharmony_ci      this.shaderFast.tex[i] = gl.getUniformLocation(
624514f5e3Sopenharmony_ci        this.shaderFast['program'],
634514f5e3Sopenharmony_ci        'tex' + i
644514f5e3Sopenharmony_ci      );
654514f5e3Sopenharmony_ci    }
664514f5e3Sopenharmony_ci
674514f5e3Sopenharmony_ci    this.shaders[XShader.ID_SHADER_FAST] = this.shaderFast;
684514f5e3Sopenharmony_ci    this.use(XShader.ID_SHADER_FAST);
694514f5e3Sopenharmony_ci  }
704514f5e3Sopenharmony_ci  use(sid) {
714514f5e3Sopenharmony_ci    gl.useProgram(this.shaders[sid]['program']);
724514f5e3Sopenharmony_ci    this.pUseingShader = this.shaders[sid];
734514f5e3Sopenharmony_ci    return this.pUseingShader;
744514f5e3Sopenharmony_ci  }
754514f5e3Sopenharmony_ci  initShader(vss, fss) {
764514f5e3Sopenharmony_ci    var vs = gl.createShader(gl.VERTEX_SHADER);
774514f5e3Sopenharmony_ci    gl.shaderSource(vs, vss);
784514f5e3Sopenharmony_ci    gl.compileShader(vs);
794514f5e3Sopenharmony_ci    if (!gl.getShaderParameter(vs, gl.COMPILE_STATUS)) {
804514f5e3Sopenharmony_ci      NapiLog.logError(
814514f5e3Sopenharmony_ci        'error occured compiling the shaders:' + gl.getShaderInfoLog(vs)
824514f5e3Sopenharmony_ci      );
834514f5e3Sopenharmony_ci      return null;
844514f5e3Sopenharmony_ci    }
854514f5e3Sopenharmony_ci
864514f5e3Sopenharmony_ci    var fs = gl.createShader(gl.FRAGMENT_SHADER);
874514f5e3Sopenharmony_ci    gl.shaderSource(fs, fss);
884514f5e3Sopenharmony_ci    gl.compileShader(fs);
894514f5e3Sopenharmony_ci    if (!gl.getShaderParameter(fs, gl.COMPILE_STATUS)) {
904514f5e3Sopenharmony_ci      NapiLog.logError(
914514f5e3Sopenharmony_ci        'error occured compiling the shaders:' + gl.getShaderInfoLog(fs)
924514f5e3Sopenharmony_ci      );
934514f5e3Sopenharmony_ci      return null;
944514f5e3Sopenharmony_ci    }
954514f5e3Sopenharmony_ci
964514f5e3Sopenharmony_ci    var ret = gl.createProgram();
974514f5e3Sopenharmony_ci
984514f5e3Sopenharmony_ci    gl.attachShader(ret, vs);
994514f5e3Sopenharmony_ci    gl.attachShader(ret, fs);
1004514f5e3Sopenharmony_ci    gl.linkProgram(ret);
1014514f5e3Sopenharmony_ci
1024514f5e3Sopenharmony_ci    if (!gl.getProgramParameter(ret, gl.LINK_STATUS)) {
1034514f5e3Sopenharmony_ci      NapiLog.logError('unable to initialize!');
1044514f5e3Sopenharmony_ci      return;
1054514f5e3Sopenharmony_ci    }
1064514f5e3Sopenharmony_ci
1074514f5e3Sopenharmony_ci    gl.deleteShader(vs);
1084514f5e3Sopenharmony_ci    gl.deleteShader(fs);
1094514f5e3Sopenharmony_ci    return ret;
1104514f5e3Sopenharmony_ci  }
1114514f5e3Sopenharmony_ci}
1124514f5e3Sopenharmony_ci
1134514f5e3Sopenharmony_ciXShader.ID_SHADER_FAST = 2;
1144514f5e3Sopenharmony_ci
1154514f5e3Sopenharmony_ciXShader.pinstance_ = null;
116