gre / gl-react

gl-react – React library to write and compose WebGL shaders

Home Page:https://gl-react-cookbook.surge.sh

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error compiling shader

st0ffern opened this issue · comments

Android

Error:
gl-shader: Error compiling shader: 0:8: S0012: Global variable initializer must be a constant expression

Code is:

import { Surface } from "gl-react-native";
import GLImage from "gl-react-image"

<Surface style={{width: 100, height: 200}}>
  <GLImage
    source={{uri: "https://i.imgur.com/2VP5osy.jpg"}}
    resizeMode="cover"
  />
</Surface>

looks like it's a bug in gl-react-image ?

Yes, this is quite wierd. Have been looking at it for "24 hours"

Works in Android simulator but not on device.
Running a normal shader with static values works fine.

I highly suspect this is because of this guy, https://github.com/gre/gl-react-image/blob/master/src/index.js#L45

maybe worth trying to inline the calc each time r was used

I can try to remove the "free" shader and see if that works first.

shader are only compiled if they are used so I doubt this will do it. also this pattern is used in contain and cover too, just try to move the float r = res.x / res.y; into the main()

I think the bug is because the device must have a different version of OpenGL or something like that –_–

ah, okey. Well moving the float r line into main() did not work

mmh weird. because this error really makes me thing it's a global variable glsl thing, and float r is the only case in gl-react-image shader mmh (like in this fix yorung/AdamasFramework@f9b7569 )

If it is a global variable thing and it works on older version of OpenGL a workaround could be to test a
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
in the manifest?

  • OpenGL|ES 2.0 on simulator
  • OpenGL|ES 3.2 on device

update
restricting the opengl version will make the app not run on devices with newer versions.

Additions to the OpenGL ES Shading Language 3.20 Specification

"In declarations of global variables with no storage qualifier or
with a const qualifier, any initializer must be a constant expression.
Declarations of global variables with other storage qualifiers may not
contain initializers. Global variables without storage qualifiers that
are not initialized in their declaration or by the application will not
be initialized by OpenGL ES, but rather will enter main() with undefined
values."

Ref: https://www.khronos.org/registry/OpenGL/extensions/EXT/EXT_shader_non_constant_global_initializers.txt

@gre my newbie mistake i changed the src/ and not the lib/ in node_modules

Moving the float r into main works. I think that declaration outside and done inside main() is best :) i will make a PR on it for gl-react-image

Tested and works on OpenGL 2 & 3.2 😉