66
77var Class = require ( '../../utils/Class' ) ;
88var Utils = require ( './Utils' ) ;
9+ var WEBGL_CONST = require ( './const' ) ;
910
1011/**
1112 * @classdesc
@@ -20,7 +21,7 @@ var Utils = require('./Utils');
2021 * @param {string } name - The name of this Shader.
2122 * @param {string } vertexShader - The vertex shader source code as a single string.
2223 * @param {string } fragmentShader - The fragment shader source code as a single string.
23- * @param {string [] } attributes -
24+ * @param {Phaser.Types.Renderer.WebGL.WebGLPipelineAttributesConfig [] } attributes - An array of attributes.
2425 * @param {string[] } [uniforms] - An array of shader uniform names that will be looked-up to get the locations for.
2526 */
2627var WebGLShader = new Class ( {
@@ -80,10 +81,43 @@ var WebGLShader = new Class({
8081 * Array of objects that describe the vertex attributes.
8182 *
8283 * @name Phaser.Renderer.WebGL.WebGLPipeline#attributes
83- * @type {Phaser.Types.Renderer.WebGL.WebGLPipelineAttributesConfig }
84+ * @type {Phaser.Types.Renderer.WebGL.WebGLPipelineAttribute[] }
8485 * @since 3.50.0
8586 */
86- this . attributes = attributes ;
87+ this . attributes ;
88+
89+ /**
90+ * The amount of vertex attribute components of 32 bit length.
91+ *
92+ * @name Phaser.Renderer.WebGL.WebGLShader#vertexComponentCount
93+ * @type {integer }
94+ * @since 3.50.0
95+ */
96+ this . vertexComponentCount = 0 ;
97+
98+ /**
99+ * The size, in bytes, of a single vertex.
100+ *
101+ * This is derived by adding together all of the vertex attributes.
102+ *
103+ * For example, the Multi Pipeline has the following attributes:
104+ *
105+ * inPosition - (size 2 x gl.FLOAT) = 8
106+ * inTexCoord - (size 2 x gl.FLOAT) = 8
107+ * inTexId - (size 1 x gl.FLOAT) = 4
108+ * inTintEffect - (size 1 x gl.FLOAT) = 4
109+ * inTint - (size 4 x gl.UNSIGNED_BYTE) = 4
110+ *
111+ * The total is 8 + 8 + 4 + 4 + 4 = 28, which is the default for this property.
112+ *
113+ * This is calculated automatically during the `createAttributes` method.
114+ *
115+ * @name Phaser.Renderer.WebGL.WebGLShader#vertexSize
116+ * @type {integer }
117+ * @readonly
118+ * @since 3.50.0
119+ */
120+ this . vertexSize = 0 ;
87121
88122 /**
89123 * The uniforms that this shader requires, as set via the configuration object.
@@ -98,12 +132,69 @@ var WebGLShader = new Class({
98132 */
99133 this . uniforms = { } ;
100134
135+ this . createAttributes ( attributes ) ;
136+
101137 if ( uniforms )
102138 {
103139 this . setUniformLocations ( uniforms ) ;
104140 }
105141 } ,
106142
143+ /**
144+ * Takes the vertex attributes array and parses it, creating the resulting array that is stored
145+ * in this shaders `attributes` property, calculating the offset, normalization and location
146+ * in the process.
147+ *
148+ * @method Phaser.Renderer.WebGL.WebGLShader#createAttributes
149+ * @since 3.50.0
150+ *
151+ * @param {Phaser.Types.Renderer.WebGL.WebGLPipelineAttributesConfig[] } attributes - An array of attributes configs.
152+ */
153+ createAttributes : function ( attributes )
154+ {
155+ var count = 0 ;
156+ var offset = 0 ;
157+ var result = [ ] ;
158+
159+ this . vertexComponentCount = 0 ;
160+
161+ for ( var i = 0 ; i < attributes . length ; i ++ )
162+ {
163+ var element = attributes [ i ] ;
164+
165+ var name = element . name ;
166+ var size = element . size ; // i.e. 1 for a float, 2 for a vec2, 4 for a vec4, etc
167+ var type = element . type . enum ; // The GLenum
168+ var typeSize = element . type . size ; // The size in bytes of the type
169+ var normalized = ( element . normalized ) ? true : false ;
170+
171+ result . push ( {
172+ name : name ,
173+ size : size ,
174+ type : type ,
175+ normalized : normalized ,
176+ offset : offset ,
177+ enabled : false ,
178+ location : - 1
179+ } ) ;
180+
181+ if ( typeSize === 4 )
182+ {
183+ count += size ;
184+ }
185+ else
186+ {
187+ count ++ ;
188+ }
189+
190+ offset += size * typeSize ;
191+ }
192+
193+ this . vertexSize = offset ;
194+ this . vertexComponentCount = count ;
195+ this . attributes = result ;
196+ } ,
197+
107198 /**
108199 * Sets the program this shader uses as being the active shader in the WebGL Renderer.
109200 *
@@ -117,7 +208,7 @@ var WebGLShader = new Class({
117208 *
118209 * @return {this } This WebGLShader instance.
119210 */
120- bind : function ( )
211+ bind : function ( reset )
121212 {
122213 this . renderer . setProgram ( this . program ) ;
123214
@@ -132,6 +223,73 @@ var WebGLShader = new Class({
132223 pipeline . mvpDirty = false ;
133224 }
134225
226+ // this.setAttribPointers(reset);
227+
228+ return this ;
229+ } ,
230+
231+ /**
232+ * Sets the vertex attribute pointers.
233+ *
234+ * This should only be called after the vertex buffer has been bound.
235+ *
236+ * @method Phaser.Renderer.WebGL.WebGLPipeline#setAttribPointers
237+ * @since 3.50.0
238+ *
239+ * @param {boolean } [reset=false] - Reset the vertex attribute locations?
240+ *
241+ * @return {this } This WebGLShader instance.
242+ */
243+ setAttribPointers : function ( reset )
244+ {
245+ if ( reset === undefined ) { reset = false ; }
246+
247+ var gl = this . gl ;
248+ var vertexSize = this . vertexSize ;
249+ var attributes = this . attributes ;
250+ var program = this . program ;
251+
252+ for ( var i = 0 ; i < attributes . length ; i ++ )
253+ {
254+ var element = attributes [ i ] ;
255+
256+ var size = element . size ;
257+ var type = element . type ;
258+ var offset = element . offset ;
259+ var enabled = element . enabled ;
260+ var location = element . location ;
261+ var normalized = ( element . normalized ) ? true : false ;
262+
263+ if ( reset )
264+ {
265+ var attribLocation = gl . getAttribLocation ( program , element . name ) ;
266+
267+ if ( attribLocation >= 0 )
268+ {
269+ gl . enableVertexAttribArray ( attribLocation ) ;
270+
271+ gl . vertexAttribPointer ( attribLocation , size , type , normalized , vertexSize , offset ) ;
272+
273+ element . enabled = true ;
274+ element . location = attribLocation ;
275+ }
276+ else if ( attribLocation !== - 1 )
277+ {
278+ gl . disableVertexAttribArray ( attribLocation ) ;
279+ }
280+ }
281+ else if ( enabled )
282+ {
283+ gl . vertexAttribPointer ( location , size , type , normalized , vertexSize , offset ) ;
284+ }
285+ else if ( ! enabled && location > - 1 )
286+ {
287+ gl . disableVertexAttribArray ( location ) ;
288+
289+ element . location = - 1 ;
290+ }
291+ }
292+
135293 return this ;
136294 } ,
137295
0 commit comments