Forráskód Böngészése

improve webgl error/type handling

Alexander Rose 3 éve
szülő
commit
9b1223ec15
2 módosított fájl, 21 hozzáadás és 19 törlés
  1. 12 14
      src/mol-gl/webgl/buffer.ts
  2. 9 5
      src/mol-gl/webgl/program.ts

+ 12 - 14
src/mol-gl/webgl/buffer.ts

@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2018-2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
+ * Copyright (c) 2018-2022 mol* contributors, licensed under MIT, See LICENSE file for more info.
  *
  * @author Alexander Rose <alexander.rose@weirdbyte.de>
  */
@@ -9,7 +9,7 @@ import { ValueCell } from '../../mol-util';
 import { RenderableSchema } from '../renderable/schema';
 import { idFactory } from '../../mol-util/id-factory';
 import { assertUnreachable, ValueOf } from '../../mol-util/type-helpers';
-import { GLRenderingContext } from './compat';
+import { GLRenderingContext, isWebGL2 } from './compat';
 import { WebGLExtensions } from './extensions';
 import { WebGLState } from './state';
 
@@ -48,6 +48,7 @@ export function getDataType(gl: GLRenderingContext, dataType: DataType) {
         case 'uint32': return gl.UNSIGNED_INT;
         case 'int32': return gl.INT;
         case 'float32': return gl.FLOAT;
+        default: assertUnreachable(dataType);
     }
 }
 
@@ -74,7 +75,12 @@ export function getBufferType(gl: GLRenderingContext, bufferType: BufferType) {
     switch (bufferType) {
         case 'attribute': return gl.ARRAY_BUFFER;
         case 'elements': return gl.ELEMENT_ARRAY_BUFFER;
-        case 'uniform': return (gl as WebGL2RenderingContext).UNIFORM_BUFFER;
+        case 'uniform':
+            if (isWebGL2(gl)) {
+                return gl.UNIFORM_BUFFER;
+            } else {
+                throw new Error('WebGL2 is required for uniform buffers');
+            }
     }
 }
 
@@ -157,18 +163,10 @@ function createBuffer(gl: GLRenderingContext, array: ArrayType, usageHint: Usage
 //
 
 export type AttributeItemSize = 1 | 2 | 3 | 4 | 16
-export type AttributeKind = 'float32' | 'int32'
+export type AttributeKind = 'float32'
 
 export function getAttribType(gl: GLRenderingContext, kind: AttributeKind, itemSize: AttributeItemSize) {
     switch (kind) {
-        case 'int32':
-            switch (itemSize) {
-                case 1: return gl.INT;
-                case 2: return gl.INT_VEC2;
-                case 3: return gl.INT_VEC3;
-                case 4: return gl.INT_VEC4;
-            }
-            break;
         case 'float32':
             switch (itemSize) {
                 case 1: return gl.FLOAT;
@@ -177,9 +175,9 @@ export function getAttribType(gl: GLRenderingContext, kind: AttributeKind, itemS
                 case 4: return gl.FLOAT_VEC4;
                 case 16: return gl.FLOAT_MAT4;
             }
-            break;
+        default:
+            assertUnreachable(kind);
     }
-    throw new Error(`unknown attribute type for kind '${kind}' and itemSize '${itemSize}'`);
 }
 
 export type AttributeDefs = {

+ 9 - 5
src/mol-gl/webgl/program.ts

@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2018-2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
+ * Copyright (c) 2018-2022 mol* contributors, licensed under MIT, See LICENSE file for more info.
  *
  * @author Alexander Rose <alexander.rose@weirdbyte.de>
  */
@@ -13,7 +13,7 @@ import { TextureId, Textures } from './texture';
 import { idFactory } from '../../mol-util/id-factory';
 import { RenderableSchema } from '../renderable/schema';
 import { isDebugMode } from '../../mol-util/debug';
-import { GLRenderingContext } from './compat';
+import { GLRenderingContext, isWebGL2 } from './compat';
 import { ShaderType, Shader } from './shader';
 
 const getNextProgramId = idFactory();
@@ -72,7 +72,7 @@ function checkActiveAttributes(gl: GLRenderingContext, program: WebGLProgram, sc
             }
             const attribType = getAttribType(gl, spec.kind, spec.itemSize);
             if (attribType !== type) {
-                throw new Error(`unexpected attribute type for ${name}`);
+                throw new Error(`unexpected attribute type '${attribType}' for ${name}, expected '${type}'`);
             }
         }
     }
@@ -104,8 +104,12 @@ function checkActiveUniforms(gl: GLRenderingContext, program: WebGLProgram, sche
                         throw new Error(`unexpected sampler type for '${name}'`);
                     }
                 } else if (spec.kind === 'volume-float32' || spec.kind === 'volume-uint8') {
-                    if (type !== (gl as WebGL2RenderingContext).SAMPLER_3D) {
-                        throw new Error(`unexpected sampler type for '${name}'`);
+                    if (isWebGL2(gl)) {
+                        if (type !== gl.SAMPLER_3D) {
+                            throw new Error(`unexpected sampler type for '${name}'`);
+                        }
+                    } else {
+                        throw new Error(`WebGL2 is required to use SAMPLER_3D`);
                     }
                 } else {
                     // TODO