Skip to content

Commit 0f6a5b9

Browse files
committed
Fix webgl extensions that are now core
1 parent 9b5c7c0 commit 0f6a5b9

File tree

4 files changed

+447
-51
lines changed

4 files changed

+447
-51
lines changed

Source/Renderer/Context.js

+133-22
Original file line numberDiff line numberDiff line change
@@ -214,30 +214,40 @@ define([
214214
}
215215

216216
// TODO : should make useWebGL2 a context flag instead?
217-
var useWebGL2 = true;
217+
var defaultToWebGL2 = true;
218218
var webGL2Supported = (typeof WebGL2RenderingContext !== 'undefined');
219-
if (useWebGL2 && webGL2Supported) {
220-
this._originalGLContext = canvas.getContext('webgl2', webglOptions) || canvas.getContext('experimental-webgl2', webglOptions) || undefined;
219+
var webGL2 = false;
220+
var glContext;
221+
222+
if (defaultToWebGL2 && webGL2Supported) {
223+
glContext = canvas.getContext('webgl2', webglOptions) || canvas.getContext('experimental-webgl2', webglOptions) || undefined;
224+
if (defined(glContext)) {
225+
webGL2 = true;
226+
}
221227
}
222-
if (!defined(this._originalGLContext)) {
223-
this._originalGLContext = canvas.getContext('webgl', webglOptions) || canvas.getContext('experimental-webgl', webglOptions) || undefined;
228+
if (!defined(glContext)) {
229+
glContext = canvas.getContext('webgl', webglOptions) || canvas.getContext('experimental-webgl', webglOptions) || undefined;
224230
}
225-
if (!defined(this._originalGLContext)) {
231+
if (!defined(glContext)) {
226232
throw new RuntimeError('The browser supports WebGL, but initialization failed.');
227233
}
228234

235+
this._originalGLContext = glContext;
236+
this._webgl2 = webGL2;
229237
this._id = createGuid();
230238

231239
// Validation and logging disabled by default for speed.
232-
this.validateFramebuffer = false;
240+
this.validateFramebuffer = true;
233241
this.validateShaderProgram = false;
234242
this.logShaderCompilation = false;
235243

236244
this._throwOnWebGLError = false;
245+
// TODO : fix this later
246+
var gl = this._gl = wrapGL(this._originalGLContext, throwOnError);
237247

238248
this._shaderCache = new ShaderCache(this);
239249

240-
var gl = this._gl = this._originalGLContext;
250+
//var gl = this._gl = this._originalGLContext;
241251

242252
this._redBits = gl.getParameter(gl.RED_BITS);
243253
this._greenBits = gl.getParameter(gl.GREEN_BITS);
@@ -291,8 +301,18 @@ define([
291301
this._instancedArrays = getExtension(gl, ['ANGLE_instanced_arrays']);
292302

293303
this._drawBuffers = getExtension(gl, ['WEBGL_draw_buffers']);
294-
ContextLimits._maximumDrawBuffers = defined(this._drawBuffers) ? gl.getParameter(this._drawBuffers.MAX_DRAW_BUFFERS_WEBGL) : 1;
295-
ContextLimits._maximumColorAttachments = defined(this._drawBuffers) ? gl.getParameter(this._drawBuffers.MAX_COLOR_ATTACHMENTS_WEBGL) : 1; // min when supported: 4
304+
if (this.drawBuffers) {
305+
if (this._webgl2) {
306+
ContextLimits._maximumDrawBuffers = gl.getParameter(WebGLConstants.MAX_DRAW_BUFFERS);
307+
ContextLimits._maximumColorAttachments = gl.getParameter(WebGLConstants.MAX_COLOR_ATTACHMENTS);
308+
} else {
309+
ContextLimits._maximumDrawBuffers = gl.getParameter(this._drawBuffers.MAX_DRAW_BUFFERS_WEBGL);
310+
ContextLimits._maximumColorAttachments = gl.getParameter(this._drawBuffers.MAX_COLOR_ATTACHMENTS_WEBGL); // min when supported: 4
311+
}
312+
} else {
313+
ContextLimits._maximumDrawBuffers = 1;
314+
ContextLimits._maximumColorAttachments = 1;
315+
}
296316

297317
this._debugShaders = getExtension(gl, ['WEBGL_debug_shaders']);
298318

@@ -481,7 +501,7 @@ define([
481501
*/
482502
standardDerivatives : {
483503
get : function() {
484-
return !!this._standardDerivatives;
504+
return !!this._standardDerivatives || this._webgl2;
485505
}
486506
},
487507

@@ -495,7 +515,7 @@ define([
495515
*/
496516
elementIndexUint : {
497517
get : function() {
498-
return !!this._elementIndexUint;
518+
return !!this._elementIndexUint || this._webgl2;
499519
}
500520
},
501521

@@ -508,7 +528,7 @@ define([
508528
*/
509529
depthTexture : {
510530
get : function() {
511-
return !!this._depthTexture;
531+
return !!this._depthTexture || this._webgl2;
512532
}
513533
},
514534

@@ -521,7 +541,7 @@ define([
521541
*/
522542
floatingPointTexture : {
523543
get : function() {
524-
return !!this._textureFloat;
544+
return !!this._textureFloat || this._webgl2;
525545
}
526546
},
527547

@@ -541,7 +561,46 @@ define([
541561
*/
542562
vertexArrayObject : {
543563
get : function() {
544-
return !!this._vertexArrayObject;
564+
return !!this._vertexArrayObject || this._webgl2;
565+
}
566+
},
567+
568+
createVertexArray : {
569+
get : function() {
570+
var that = this;
571+
return function() {
572+
if (that._webgl2) {
573+
that._gl.createVertexArray();
574+
} else {
575+
that._vertexArrayObject.createVertexArrayOES();
576+
}
577+
};
578+
}
579+
},
580+
581+
bindVertexArray : {
582+
get : function() {
583+
var that = this;
584+
return function(vertexArray) {
585+
if (that._webgl2) {
586+
that._gl.bindVertexArray(vertexArray);
587+
} else {
588+
that._vertexArrayObject.bindVertexArrayOES(vertexArray);
589+
}
590+
};
591+
}
592+
},
593+
594+
deleteVertexArray : {
595+
get : function() {
596+
var that = this;
597+
return function(vertexArray) {
598+
if (that._webgl2) {
599+
that._gl.deleteVertexArray(vertexArray);
600+
} else {
601+
that._vertexArrayObject.deleteVertexArrayOES(vertexArray);
602+
}
603+
};
545604
}
546605
},
547606

@@ -556,7 +615,7 @@ define([
556615
*/
557616
fragmentDepth : {
558617
get : function() {
559-
return !!this._fragDepth;
618+
return !!this._fragDepth || this._webgl2;
560619
}
561620
},
562621

@@ -569,7 +628,46 @@ define([
569628
*/
570629
instancedArrays : {
571630
get : function() {
572-
return !!this._instancedArrays;
631+
return !!this._instancedArrays || this._webgl2;
632+
}
633+
},
634+
635+
drawElementsInstanced : {
636+
get : function() {
637+
var that = this;
638+
return function(mode, count, type, offset, instanceCount) {
639+
if (that._webgl2) {
640+
that._gl.drawElementsInstanced(mode, count, type, offset, instanceCount);
641+
} else {
642+
that._instancedArrays.drawElementsInstancedANGLE(mode, count, type, offset, instanceCount);
643+
}
644+
};
645+
}
646+
},
647+
648+
drawArraysInstanced : {
649+
get : function() {
650+
var that = this;
651+
return function(mode, first, count, instanceCount) {
652+
if (that._webgl2) {
653+
that._gl.drawArraysInstanced(mode, first, count, instanceCount);
654+
} else {
655+
that._instancedArrays.drawArraysInstancedANGLE(mode, first, count, instanceCount);
656+
}
657+
};
658+
}
659+
},
660+
661+
vertexAttribDivisor : {
662+
get : function() {
663+
var that = this;
664+
return function(index, divisor) {
665+
if (that._webgl2) {
666+
that._gl.vertexAttribDivisor(index, divisor);
667+
} else {
668+
that._instancedArrays.vertexAttribDivisorANGLE(index, divisor);
669+
}
670+
};
573671
}
574672
},
575673

@@ -585,7 +683,20 @@ define([
585683
*/
586684
drawBuffers : {
587685
get : function() {
588-
return !!this._drawBuffers;
686+
return !!this._drawBuffers || this._webgl2;
687+
}
688+
},
689+
690+
drawBuffersGL : {
691+
get : function() {
692+
var that = this;
693+
return function(buffers) {
694+
if (that._webgl2) {
695+
that._gl.drawBuffers(buffers);
696+
} else {
697+
that._drawBuffers.drawBuffersWEBGL(buffers);
698+
}
699+
};
589700
}
590701
},
591702

@@ -759,7 +870,7 @@ define([
759870
}
760871

761872
if (context.drawBuffers) {
762-
context._drawBuffers.drawBuffersWEBGL(buffers);
873+
context.drawBuffersGL(buffers);
763874
}
764875
}
765876
}
@@ -877,14 +988,14 @@ define([
877988
if (instanceCount === 0) {
878989
context._gl.drawElements(primitiveType, count, indexBuffer.indexDatatype, offset);
879990
} else {
880-
context._instancedArrays.drawElementsInstancedANGLE(primitiveType, count, indexBuffer.indexDatatype, offset, instanceCount);
991+
context.drawElementsInstanced(primitiveType, count, indexBuffer.indexDatatype, offset, instanceCount);
881992
}
882993
} else {
883994
count = defaultValue(count, va.numberOfVertices);
884995
if (instanceCount === 0) {
885996
context._gl.drawArrays(primitiveType, offset, count);
886997
} else {
887-
context._instancedArrays.drawArraysInstancedANGLE(primitiveType, offset, count, instanceCount);
998+
context.drawArraysInstanced(primitiveType, offset, count, instanceCount);
888999
}
8891000
}
8901001

@@ -919,7 +1030,7 @@ define([
9191030

9201031
var buffers = scratchBackBufferArray;
9211032
if (this.drawBuffers) {
922-
this._drawBuffers.drawBuffersWEBGL(scratchBackBufferArray);
1033+
this.drawBuffersGL(buffers);
9231034
}
9241035

9251036
var length = this._maxFrameTextureUnitIndex;

Source/Renderer/Texture.js

+21-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ define([
1414
'./Sampler',
1515
'./TextureMagnificationFilter',
1616
'./TextureMinificationFilter',
17-
'./TextureWrap'
17+
'./TextureWrap',
18+
'./WebGLConstants'
1819
], function(
1920
Cartesian2,
2021
defaultValue,
@@ -30,7 +31,8 @@ define([
3031
Sampler,
3132
TextureMagnificationFilter,
3233
TextureMinificationFilter,
33-
TextureWrap) {
34+
TextureWrap,
35+
WebGLConstants) {
3436
"use strict";
3537

3638
var Texture = function(options) {
@@ -48,6 +50,19 @@ define([
4850
var height = defined(source) ? source.height : options.height;
4951
var pixelFormat = defaultValue(options.pixelFormat, PixelFormat.RGBA);
5052
var pixelDatatype = defaultValue(options.pixelDatatype, PixelDatatype.UNSIGNED_BYTE);
53+
var internalFormat = pixelFormat;
54+
55+
if (context._webgl2) {
56+
if (pixelFormat === PixelFormat.DEPTH_STENCIL) {
57+
internalFormat = WebGLConstants.DEPTH24_STENCIL8;
58+
} else if (pixelFormat === PixelFormat.DEPTH_COMPONENT) {
59+
if (pixelDatatype === PixelDatatype.UNSIGNED_SHORT) {
60+
internalFormat = WebGLConstants.DEPTH_COMPONENT16;
61+
} else if (pixelDatatype === PixelDatatype.UNSIGNED_INT) {
62+
internalFormat = WebGLConstants.DEPTH_COMPONENT24;
63+
}
64+
}
65+
}
5166

5267
//>>includeStart('debug', pragmas.debug);
5368
if (!defined(width) || !defined(height)) {
@@ -123,24 +138,24 @@ define([
123138

124139
if (defined(source.arrayBufferView)) {
125140
// Source: typed array
126-
gl.texImage2D(textureTarget, 0, pixelFormat, width, height, 0, pixelFormat, pixelDatatype, source.arrayBufferView);
141+
gl.texImage2D(textureTarget, 0, internalFormat, width, height, 0, pixelFormat, pixelDatatype, source.arrayBufferView);
127142
} else if (defined(source.framebuffer)) {
128143
// Source: framebuffer
129144
if (source.framebuffer !== context.defaultFramebuffer) {
130145
source.framebuffer._bind();
131146
}
132147

133-
gl.copyTexImage2D(textureTarget, 0, pixelFormat, source.xOffset, source.yOffset, width, height, 0);
148+
gl.copyTexImage2D(textureTarget, 0, internalFormat, source.xOffset, source.yOffset, width, height, 0);
134149

135150
if (source.framebuffer !== context.defaultFramebuffer) {
136151
source.framebuffer._unBind();
137152
}
138153
} else {
139154
// Source: ImageData, HTMLImageElement, HTMLCanvasElement, or HTMLVideoElement
140-
gl.texImage2D(textureTarget, 0, pixelFormat, pixelFormat, pixelDatatype, source);
155+
gl.texImage2D(textureTarget, 0, internalFormat, pixelFormat, pixelDatatype, source);
141156
}
142157
} else {
143-
gl.texImage2D(textureTarget, 0, pixelFormat, width, height, 0, pixelFormat, pixelDatatype, null);
158+
gl.texImage2D(textureTarget, 0, internalFormat, width, height, 0, pixelFormat, pixelDatatype, null);
144159
}
145160
gl.bindTexture(textureTarget, null);
146161

0 commit comments

Comments
 (0)