@@ -14,6 +14,11 @@ import filterThresholdFrag from '../webgl/shaders/filters/threshold.frag';
1414import filterShaderVert from '../webgl/shaders/filters/default.vert' ;
1515import { filterParamDefaults } from "./const" ;
1616
17+
18+ import filterBaseFrag from "../webgl/shaders/filters/base.frag" ;
19+ import filterBaseVert from "../webgl/shaders/filters/base.vert" ;
20+ import webgl2CompatibilityShader from "../webgl/shaders/webgl2Compatibility.glsl" ;
21+
1722class FilterRenderer2D {
1823 /**
1924 * Creates a new FilterRenderer2D instance.
@@ -27,7 +32,12 @@ class FilterRenderer2D {
2732 this . canvas . height = pInst . height ;
2833
2934 // Initialize the WebGL context
30- this . gl = this . canvas . getContext ( 'webgl' ) ;
35+ let webglVersion = constants . WEBGL2 ;
36+ this . gl = this . canvas . getContext ( 'webgl2' ) ;
37+ if ( ! this . gl ) {
38+ webglVersion = constants . WEBGL ;
39+ this . gl = this . canvas . getContext ( 'webgl' ) ;
40+ }
3141 if ( ! this . gl ) {
3242 console . error ( "WebGL not supported, cannot apply filter." ) ;
3343 return ;
@@ -38,7 +48,7 @@ class FilterRenderer2D {
3848 registerEnabled : new Set ( ) ,
3949 _curShader : null ,
4050 _emptyTexture : null ,
41- webglVersion : 'WEBGL' ,
51+ webglVersion,
4252 states : {
4353 textureWrapX : this . gl . CLAMP_TO_EDGE ,
4454 textureWrapY : this . gl . CLAMP_TO_EDGE ,
@@ -54,6 +64,8 @@ class FilterRenderer2D {
5464 } ,
5565 } ;
5666
67+ this . _baseFilterShader = undefined ;
68+
5769 // Store the fragment shader sources
5870 this . filterShaderSources = {
5971 [ constants . BLUR ] : filterBlurFrag ,
@@ -90,6 +102,45 @@ class FilterRenderer2D {
90102 this . _bindBufferData ( this . texcoordBuffer , this . gl . ARRAY_BUFFER , this . texcoords ) ;
91103 }
92104
105+ _webGL2CompatibilityPrefix ( shaderType , floatPrecision ) {
106+ let code = "" ;
107+ if ( this . _renderer . webglVersion === constants . WEBGL2 ) {
108+ code += "#version 300 es\n#define WEBGL2\n" ;
109+ }
110+ if ( shaderType === "vert" ) {
111+ code += "#define VERTEX_SHADER\n" ;
112+ } else if ( shaderType === "frag" ) {
113+ code += "#define FRAGMENT_SHADER\n" ;
114+ }
115+ if ( floatPrecision ) {
116+ code += `precision ${ floatPrecision } float;\n` ;
117+ }
118+ return code ;
119+ }
120+
121+ baseFilterShader ( ) {
122+ if ( ! this . _baseFilterShader ) {
123+ this . _baseFilterShader = new Shader (
124+ this . _renderer ,
125+ this . _webGL2CompatibilityPrefix ( "vert" , "highp" ) +
126+ webgl2CompatibilityShader +
127+ filterBaseVert ,
128+ this . _webGL2CompatibilityPrefix ( "frag" , "highp" ) +
129+ webgl2CompatibilityShader +
130+ filterBaseFrag ,
131+ {
132+ vertex : { } ,
133+ fragment : {
134+ "vec4 getColor" : `(FilterInputs inputs, in sampler2D canvasContent) {
135+ return getTexture(canvasContent, inputs.texCoord);
136+ }` ,
137+ } ,
138+ }
139+ ) ;
140+ }
141+ return this . _baseFilterShader ;
142+ }
143+
93144 /**
94145 * Set the current filter operation and parameter. If a customShader is provided,
95146 * that overrides the operation-based shader.
0 commit comments