Browse Source

Merge pull request #9490 from Popov72/fix_glinstanceid_ios

Fix crash of some node materials using instances on iOS
sebavan 4 years ago
parent
commit
d07b6bc416

+ 1 - 0
dist/preview release/what's new.md

@@ -45,6 +45,7 @@
 - Fix an issue with keyboard control (re)attachment. ([#9411](https://github.com/BabylonJS/Babylon.js/issues/9411)) ([RaananW](https://github.com/RaananW))
 - Fix issue where PBRSpecularGlossiness materials were excluded from export [#9423](https://github.com/BabylonJS/Babylon.js/issues/9423)([Drigax](https://github.com/drigax))
 - Fix direct loading of a glTF string that has base64-encoded URI. ([bghgary](https://github.com/bghgary))
+- Fix crash of some node materials using instances on iOS ([Popov72](https://github.com/Popov72))
 - Fix the code generated for the NME gradient block ([Popov72](https://github.com/Popov72))
 - Fix ssao2RenderingPipeline for orthographic cameras ([Kesshi](https://github.com/Kesshi))
 

+ 2 - 0
src/Engines/engineCapabilities.ts

@@ -88,4 +88,6 @@ export interface EngineCapabilities {
     maxMSAASamples: number;
     /** Defines if the blend min max extension is supported */
     blendMinMax: boolean;
+    /** In some iOS + WebGL1, gl_InstanceID (and gl_InstanceIDEXT) is undefined even if instancedArrays is true. So don't use gl_InstanceID in those cases */
+    canUseGLInstanceID: boolean;
 }

+ 2 - 1
src/Engines/nativeEngine.ts

@@ -800,7 +800,8 @@ export class NativeEngine extends Engine {
             instancedArrays: false,
             canUseTimestampForTimerQuery: false,
             blendMinMax: false,
-            maxMSAASamples: 1
+            maxMSAASamples: 1,
+            canUseGLInstanceID: true,
         };
 
         this._features = {

+ 2 - 1
src/Engines/nullEngine.ts

@@ -140,7 +140,8 @@ export class NullEngine extends Engine {
             instancedArrays: false,
             canUseTimestampForTimerQuery: false,
             maxMSAASamples: 1,
-            blendMinMax: false
+            blendMinMax: false,
+            canUseGLInstanceID: false,
         };
 
         this._features = {

+ 2 - 1
src/Engines/thinEngine.ts

@@ -912,7 +912,8 @@ export class ThinEngine {
             blendMinMax: false,
             multiview: this._gl.getExtension('OVR_multiview2'),
             oculusMultiview: this._gl.getExtension('OCULUS_multiview'),
-            depthTextureExtension: false
+            depthTextureExtension: false,
+            canUseGLInstanceID: !(this._badOS && this._webGLVersion <= 1),
         };
 
         // Infos

+ 2 - 1
src/Engines/webgpuEngine.ts

@@ -473,7 +473,8 @@ export class WebGPUEngine extends Engine {
             oculusMultiview: false,
             parallelShaderCompile: undefined,
             blendMinMax: true,
-            maxMSAASamples: 4 // TODO WEBGPU what is the right value?
+            maxMSAASamples: 4,
+            canUseGLInstanceID: true,
         };
 
         this._caps.parallelShaderCompile = null as any;

+ 7 - 1
src/Materials/Node/Blocks/Vertex/instancesBlock.ts

@@ -159,6 +159,8 @@ export class InstancesBlock extends NodeMaterialBlock {
     protected _buildBlock(state: NodeMaterialBuildState) {
         super._buildBlock(state);
 
+        const engine = state.sharedData.scene.getEngine();
+
         // Register for defines
         state.sharedData.blocksWithDefines.push(this);
 
@@ -175,7 +177,11 @@ export class InstancesBlock extends NodeMaterialBlock {
         state.compilationString += `#ifdef THIN_INSTANCES\r\n`;
         state.compilationString += `${output.associatedVariableName} = ${this.world.associatedVariableName} * ${output.associatedVariableName};\r\n`;
         state.compilationString += `#endif\r\n`;
-        state.compilationString += this._declareOutput(instanceID, state) + ` = float(gl_InstanceID);\r\n`;
+        if (engine._caps.canUseGLInstanceID) {
+            state.compilationString += this._declareOutput(instanceID, state) + ` = 0.0;\r\n`;
+        } else {
+            state.compilationString += this._declareOutput(instanceID, state) + ` = float(gl_InstanceID);\r\n`;
+        }
         state.compilationString += `#else\r\n`;
         state.compilationString += this._declareOutput(output, state) + ` = ${this.world.associatedVariableName};\r\n`;
         state.compilationString += this._declareOutput(instanceID, state) + ` = 0.0;\r\n`;