Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Difficulties running example.py on Mac #1

Open
m-bob opened this issue Feb 4, 2022 · 8 comments
Open

Difficulties running example.py on Mac #1

m-bob opened this issue Feb 4, 2022 · 8 comments

Comments

@m-bob
Copy link

m-bob commented Feb 4, 2022

Hi,

First, thanks for making this! I'm looking forward to using it.
Second, the code looks very well-structured and I may try to adapt it to PyQt, which is what I've been using. Or perhaps not. It looks like PySide is the future, so I might go with the flow. OTOH, only a few changes are needed to migrate your code to PyQt and that might be a faster/easier way for me to use it.

Third, I'm having difficulty trying your example. I followed the installation instructions (in a new Conda environment), and everything went (apparently) flawlessly. Then I tried to run example.py in the samples folder. That didn't work. First, I had to remove the text 'src' from a number of src.'module' import statements. Once the import statements were fixed, I got other, easily fixable errors, and then the code ran and showed a window. But I'm getting a vast number of "IndexErrors" and the window remains blank.

I believe the errors are a result of an early error: the shaders don't compile. They fail to compile because "version 330 is not supported."

On the Mac, something called the OpenGL CoreFormat is needed to enable GLSL 330. I've gotten it to work using PyQt, but I'm not familiar with PySide, and my solution for PyQt (or work-around, or whatever) doesn't work for PySide6.

There are examples of how to do this for glut, glew, and pyqt, but I haven't been able to find any example of working code that uses PySide6 OpenGL on the Mac. Moderngl-window works on the Mac but it supports only PySide2, not PySide6. VisPy appears to support PySide6, but it doesn't look like it supports the Mac.

Does anyone know how to get PySide6 OpenGL to work on the Mac? (I'm still running Catalina, but I couldn't find any working examples for newer OSes.)

Thanks!

@m-bob
Copy link
Author

m-bob commented Feb 4, 2022

I believe pyqtgraph runs on the Mac. (Haven't verified that.) It has a PySide6 backend and it uses shaders. So, it might have provided an example to solve the problem I'm having -- but I no longer think so.

I downloaded the Github pyqtgraph source and grepped for CoreProfile, but I didn't find it. Equally relevant, pyqtgraph appears to use GLSL version 1.2 shaders -- so perhaps it's really not relevant.

Here's the key question, slightly updated: is there any example which usesPySide6 and GLSL version 330 shaders on the Mac? (Note: GLSL 330 is the most recent version supported on the Mac. But it does work -- I can use version 330 shaders with PyQt and other OpenGL frameworks, but not (yet) with PySide6.

@m-bob
Copy link
Author

m-bob commented Feb 4, 2022

Sorry to keep "spamming." I just found an example which should help. It's at https://doc.qt.io/qtforpython/examples/example_opengl__hellogl2.html and it uses PySide6 on the Mac. The shaders it uses are only Version 1.5, but the example includes code that sets the CoreProfile and version. The example uses version 3, 2 but I changed it to 3,3 and it still works.

The key bit of code is:

fmt = QSurfaceFormat()
fmt.setDepthBufferSize(24)
if options.multisample:
fmt.setSamples(4)
if options.coreprofile:
fmt.setVersion(3, 3)
fmt.setProfile(QSurfaceFormat.CoreProfile)
QSurfaceFormat.setDefaultFormat(fmt)

which is inserted between the calls to Application and instantiating the main window.

I'll try it in a bit.

@vigneshpai2003
Copy link
Owner

Hey
I’m glad to hear someone is finding this code useful, this project was initially just to learn about OpenGL. Further, the code was written at a time when I was still in the process of learning python.

This issue has brought back my interest in this project and I will try to clean up the code a bit and try to incorporate the changes you suggested.

Till then, if you want something working, I would recommend that you use the PySide or PyQt 3d libraries as they would have the cross platform compatibility and hopefully work on Mac.

@m-bob
Copy link
Author

m-bob commented Feb 5, 2022

Thanks Vignesh! I have a use for your code once I can get it to work. Unfortunately, what I thought would work, didn't. I'll keep trying but I'll also keep an eye out for whatever you can do. OpenGL on the Mac is very challenging! But I'm sure there's an answer.

At any rate, I'll be super-excited to see your sample image displayed on my Mac.

@vigneshpai2003
Copy link
Owner

The code was initially tested only on Windows where it was running due to magic. I faced a similar set of issues on Linux (Ubuntu 20.04), this was due to:

  1. 'src.' imports: due to automatic refactoring.
  2. relative paths: python samples/example.py gives the index error, while cd samples followed by python example.py works (this is because the example file references asset files via relative paths).
  3. glsl files in installation: .glsl files were not being included in the installation which I did not notice since I was testing using an editable installation (pip install -e .).
  4. PySide6 version: There seems to have been a OpenGL related bug in the version of PySide6 I was using that is fixed in the latest version. After upgrading to the newer version, the example code runs perfectly on Linux.
  5. pip caches: After updating stuff, remember to install using pip install --no-cache-dir ., or else the previous cached code is installed.

I have fixed all the above issues. Following the following steps works for me.

  1. pip install -r requirements.txt
  2. pip install --no-cache-dir .
  3. cd samples
  4. python example.py

Perhaps this fixes your issue on Mac as it fixed my issue on Linux.

@m-bob
Copy link
Author

m-bob commented Feb 5, 2022

I'll try your suggestions later this weekend. Thank you.

Previously, I encountered (and was able to fix) the issues 1 and 3. I just tried your suggestion regarding 2., but it made no difference. I haven't re-installed viggy-3d as you suggest, but I will.

With luck, your changes will fix things. But the Mac is a fairly hostile environment for OpenGL, and, despite Qt's platform-independence, I think additional measures will be required fo get the code working on the Mac.

I'll let you know. Thanks again!

@m-bob
Copy link
Author

m-bob commented Feb 5, 2022

Found some extra time, and just tried it. Same behavior as before.

The first -- and perhaps only important -- error I get is:


File "/Users/murray/viggy-3d/samples/example.py", line 18, in initializeGL
super().initializeGL()
File "/Users/murray/opt/anaconda3/envs/warp/lib/python3.9/site-packages/viggy_3d/Graph.py", line 161, in initializeGL
self.modelShader = Shader(os.path.join(os.path.dirname(file), "shaders/model"))
File "/Users/murray/opt/anaconda3/envs/warp/lib/python3.9/site-packages/viggy_3d/Shader.py", line 18, in init
compiledShaders.append(GL.shaders.compileShader(f.read(), GL.GL_VERTEX_SHADER))
File "/Users/murray/opt/anaconda3/envs/warp/lib/python3.9/site-packages/OpenGL/GL/shaders.py", line 235, in compileShader
raise ShaderCompilationError(
OpenGL.GL.shaders.ShaderCompilationError: ('Shader compile failure (0): b"ERROR: 0:1: '' : version '330' is not supported\nERROR: 0:1: '' : syntax error: #version\nERROR: 0:5: 'layout' : syntax error: syntax error\n"', [b'# version 330 core\n\n\n// vertex attributes\nlayout (location = 0) in vec3 position;\nlayout (location = 1) in vec3 normal; // need not be normalized\nlayout (location = 2) in vec2 UV;\n\n\n// output to fragment\nout VS_OUT {\n vec3 position;\n vec3 normal;\n vec2 UV;\n} vs_out;\n\n\nuniform mat4 model;\nuniform mat4 view;\nuniform mat4 projection;\n\n\nvoid main()\n{\n // the position in world coordinates\n vs_out.position = (model * vec4(position, 1.0)).xyz;\n // the normal with model rotations but no translations\n vs_out.normal = normalize(mat3(model) * normal);\n vs_out.UV = UV;\n\n // the position in screen coordinates\n gl_Position = projection * view * vec4(vs_out.position, 1.0);\n}\n'], GL_VERTEX_SHADER)
Traceback (most recent call last):


It's the same error as before. Then I get a huge number of list index out of range errors -- also as before.

I think the key problem is that the version 330 shaders are not being compiled. From what I've seen, getting that to work on the Mac involves invoking the PySide6 instructions

fmt = QSurfaceFormat ()
fmt.setVersion(3, 3)
fmt.setProfile (QSurfaceFormat.CoreProfile) QSurfaceFormat.setDefaultFormat (fmt)

The example program I linked to above does that -- and it works perfectly on my Mac (with both PySide6 and version 330 shaders). So it seems it should be easy to modify your example.py to include those statements.

However, doing that isn't enough. I still get the shader compilation error that version 330 is not supported.

I also get a new message:

qt.qpa.openglcontext: Could not create NSOpenGLContext with shared context, falling back to unshared context.

I don't understand it. After that error message, the compilation and index errors show up.

Looking at the working code shows a number of other differences involving how the shaders are set up, compiled, and used. I didn't think those differences matter, but apparently at least some of them do.

I decided to try merging your example.py code with the working PySide6 example code. I'm still getting errors, including shader errors, but I'm not getting the version 330 not supported error any longer. That might count as progress.

Still trying. The vertex shader doesn't seem to have any problems. The fragment shader compiles, but does not validate.

I'm getting an error

OpenGL.GL.shaders.ShaderValidationError: Validation failure (0): b'Validation Failed: Current draw framebuffer is invalid.\n'

No clue what that means or how to fix it. Going to let it be for awhile.

@m-bob
Copy link
Author

m-bob commented Feb 5, 2022

I googled "PyOpenGL validation failure."

Found this: https://stackoverflow.com/questions/53279015/pyopengl-validation-failure

The article is informative and it has Mac-specific information. It states: "In core profile you must have a named VAO, it is not optional." So perhaps there needs to be a named VAO for this code to run on the Mac. The PySide6 example I'm using does have a VAO, so that's consistent. But I have working PyQt5 code that uses a GLSL 330 shader on the Mac in CoreProfile which uses VBOs, but not a VAO. So it's not true that a VAO is required. But perhaps it is required when using PySide6. Who knows?

I still don't know what the difference was that allowed Version 330 GLSL shaders to compile.

Using OpenGL, esp. on the Mac, seems to be a real PITA. Insufficient and poor documentation, trial and error process, frequent Google searching, and too much grief over boilerplate.

But I'll keep going.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants