Re: Unable to share contexts when importing cocos.particle_systems

279 views
Skip to first unread message

claudio canepa

unread,
Apr 1, 2013, 12:02:36 PM4/1/13
to cocos2d discuss
When pyglet creates a window it tries to bind a shared GL context, and in your case fails.
Seems that sharing contexts are not working in your system.

Thats usually a harware / driver limitation, by example the old intel 945G integrated graphic chipset  does that with certain driver versions. 

Your best bet is to try updating drivers; sadly some old laptops don't upgrade the branded drivers, and don't allow to use the generic drivers.

As a dirty workaround you can add at the very beginning of your program
     import pyglet
     pyglet.options['shadow_window'] = False

For further advice, I suggest to run some of the pyglet samples and report the traceback to pyglet mailing list, adding info about your video chipset



On Mon, Apr 1, 2013 at 9:23 AM, <hell...@gmail.com> wrote:
When I try to import cocos.particle_systems I get the following exception pyglet.gl.ContextException: Unable to share contexts.

I do not even use any code from the import or anything else involving particle systems, I simply import the module and then I get this exception.
I even had a look at the code in particle_systems.py and I couldn't find anything that would cause this. I further tried to block the import of particle.py from there but for some reason that  had zero effect.

The full error is:

Traceback (most recent call last):
  File "run_game.py", line 10, in <module>
    main.main()
  File "main.py", line 211, in main
    director.init(width=1280, height=720, caption="Planet Eater")
  File "C:\Python27\lib\site-packages\cocos2d-0.5.5-py2.7.egg\cocos\director.py", line 295, in init
    self.window = window.Window( *args, **kwargs )
  File "C:\Python27\lib\site-packages\pyglet\window\win32\__init__.py", line 131, in __init__
    super(Win32Window, self).__init__(*args, **kwargs)
  File "C:\Python27\lib\site-packages\pyglet\window\__init__.py", line 559, in __init__
    self._create()
  File "C:\Python27\lib\site-packages\pyglet\window\win32\__init__.py", line 261, in _create
    self.context.attach(self.canvas)
  File "C:\Python27\lib\site-packages\pyglet\gl\win32.py", line 263, in attach
    super(Win32ARBContext, self).attach(canvas)
  File "C:\Python27\lib\site-packages\pyglet\gl\win32.py", line 208, in attach
    raise gl.ContextException('Unable to share contexts')
pyglet.gl.ContextException: Unable to share contexts

--
You received this message because you are subscribed to the Google Groups "cocos2d discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cocos-discus...@googlegroups.com.
To post to this group, send email to cocos-...@googlegroups.com.
Visit this group at http://groups.google.com/group/cocos-discuss?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

hell...@gmail.com

unread,
Apr 1, 2013, 1:21:31 PM4/1/13
to cocos-...@googlegroups.com
I don't understand why the particle systems module would cause such a failure. Everything works fine if I don't import this module.

I am running on Intel I7 and have very recent drivers for my graphics card (although I don't think the dedicated graphics card is being used by the program).

When I try your suggestion I have the error:

Traceback (most recent call last):
  File "run_game.py", line 7, in <module>
    import main
  File "main.py", line 15, in <module>
    import cocos.particle_systems
  File "C:\Python27\lib\site-packages\cocos2d-0.5.5-py2.7.egg\cocos\particle_systems.py", line 38, in <module>
    from particle import ParticleSystem, Color
  File "C:\Python27\lib\site-packages\cocos2d-0.5.5-py2.7.egg\cocos\particle.py", line 92, in <module>
    class ParticleSystem( CocosNode ):
  File "C:\Python27\lib\site-packages\cocos2d-0.5.5-py2.7.egg\cocos\particle.py", line 167, in ParticleSystem
    texture = pic.get_texture()
  File "C:\Python27\lib\site-packages\pyglet\image\__init__.py", line 801, in get_texture
    force_rectangle)
  File "C:\Python27\lib\site-packages\pyglet\image\__init__.py", line 786, in create_texture
    rectangle, force_rectangle)
  File "C:\Python27\lib\site-packages\pyglet\image\__init__.py", line 1486, in create
    glGenTextures(1, byref(id))
  File "C:\Python27\lib\site-packages\pyglet\gl\lib.py", line 100, in errcheck
    raise GLException('No GL context; create a Window first')
pyglet.gl.lib.GLException: No GL context; create a Window first

claudio canepa

unread,
Apr 1, 2013, 5:56:37 PM4/1/13
to cocos2d discuss
On Mon, Apr 1, 2013 at 2:21 PM, <hell...@gmail.com> wrote:
I don't understand why the particle systems module would cause such a failure. Everything works fine if I don't import this module.

Really ?
Here is the traceback you posted before:

Traceback (most recent call last):
File "run_game.py", line 10, in <module>
main.main()
File "main.py", line 211, in main
director.init(width=1280, height=720, caption="Planet Eater")
File "C:\Python27\lib\site-packages\cocos2d-0.5.5-py2.7.egg\cocos\director.py", line 295, in init
self.window = window.Window( *args, **kwargs )
File "C:\Python27\lib\site-packages\pyglet\window\win32\__init__.py", line 131, in __init__
super(Win32Window, self).__init__(*args, **kwargs)
File "C:\Python27\lib\site-packages\pyglet\window\__init__.py", line 559, in __init__
self._create()
File "C:\Python27\lib\site-packages\pyglet\window\win32\__init__.py", line 261, in _create
self.context.attach(self.canvas)
File "C:\Python27\lib\site-packages\pyglet\gl\win32.py", line 263, in attach
super(Win32ARBContext, self).attach(canvas)
File "C:\Python27\lib\site-packages\pyglet\gl\win32.py", line 208, in attach
raise gl.ContextException('Unable to share contexts')
pyglet.gl.ContextException: Unable to share contexts


It tells that your prog started in run_game.py , which imports a module main, then calls main.main() , which calls (cocos) director.init which tries to instantiate a window trough pyglet   window.Window which explodes when it can't create an Open GL shared context.
No particles involved here. 
--
 

This sounds as importing cocos particles before the window creation. Are you sure the call to director.init is before your import of cocos.particle_system ?

Can you post a short script that demostrates the problem ?


 

hell...@gmail.com

unread,
Apr 2, 2013, 11:23:28 AM4/2/13
to cocos-...@googlegroups.com
This sounds as importing cocos particles before the window creation. Are you sure the call to director.init is before your import of cocos.particle_system ?

Can you post a short script that demostrates the problem ?

Yep, everything works fine without the line "import cocos.particle_systems".
Importing this module along with everything else, ie. at the top of the file, causes the error.

However, when I import the module AFTER director.init it works! So if I import and use the module during the creation of one of my objects, say, the program runs.

This is very odd. This is certainly not how it is supposed to work.
How did you get to asking the this question? (ie. how do you know to call director.init before importing the module?) It certainly isn't in the tests or the documentation.

hell...@gmail.com

unread,
Apr 2, 2013, 11:31:17 AM4/2/13
to cocos-...@googlegroups.com
Here is a script which demonstrates the problem.
If you try importing at position ONE it gives the 'Unable to share contexts' error.
If you try importing at either of the position TWOs, everything works fine.

from cocos.director import director
import cocos.layer
#import cocos.particle_systems #ONE
 
class Layer(cocos.layer.Layer):
    def __init__(self):
        super(Layer, self).__init__()
        #import cocos.particle_systems #TWO
 
if __name__ == '__main__':
    director.init()
    #import cocos.particle_systems #TWO
    director.run(cocos.scene.Scene(Layer()))

claudio canepa

unread,
Apr 2, 2013, 4:42:08 PM4/2/13
to cocos2d discuss
  -- 

Results with ati gpu:
(Runs ok meant no tracebacks and an empty windows appears.)
1st run : all imports commented out. Runs ok.
2nd run : uncomment only ONE. Runs ok.
3rd run : uncomment only the first TWO. Runs Ok
4th run : uncomment only the second TWO. Runs ok.
 
Same results with an intel 945G gpu  (with driver that can handle sharing GL contexts)

Tested both with pyglet 1.1.4 and pyglet 1.2dev updated from repo today.

I would expect all the four cases to crash if sharing Open GL contexts are not supported by your gpu drivers, (because by default pyglet builds a hidden window that will share context with all the windows created by pyglet window.Window)

Can you run the same four cases, from a fresh copy-pasted scrip and report success / failure for each run and include all the traceback for each failure ?

And,  just to be sure, you are not setting any pyglet options as environment variables ? 

To narrow the issue, could you try the following script

import pyglet

#from pyglet.gl import * # ONE
window = pyglet.window.Window()
#from pyglet.gl import * # TWO

@window.event
def on_draw():
      window.clear()

pyglet.app.run()
( try with the code as-is, then uncommenting only the # ONE line, then uncommenting only the # TWO line)


hell...@gmail.com

unread,
Apr 7, 2013, 10:09:51 AM4/7/13
to cocos-...@googlegroups.com
I've checked the environment variables and indeed I am not setting any pyglet options. I am using pyglet 1.2alpha1 and python 2.7.3.

With a fresh copy of my script:
1st run: all imports commented out - Runs OK.
2nd run: uncomment only ONE - Error
Traceback (most recent call last):
  File "cocostest.py", line 17, in <module>
    director.init()

  File "C:\Python27\lib\site-packages\cocos2d-0.5.5-py2.7.egg\cocos\director.py", line 295, in init
    self.window = window.Window( *args, **kwargs )
  File "C:\Python27\lib\site-packages\pyglet\window\win32\__init__.py", line 131, in __init__
    super(Win32Window, self).__init__(*args, **kwargs)
  File "C:\Python27\lib\site-packages\pyglet\window\__init__.py", line 559, in __init__
    self._create()
  File "C:\Python27\lib\site-packages\pyglet\window\win32\__init__.py", line 261, in _create
    self.context.attach(self.canvas)
  File "C:\Python27\lib\site-packages\pyglet\gl\win32.py", line 263, in attach
    super(Win32ARBContext, self).attach(canvas)
  File "C:\Python27\lib\site-packages\pyglet\gl\win32.py", line 208, in attach
    raise gl.ContextException('Unable to share contexts')
pyglet.gl.ContextException: Unable to share contexts

3rd run: uncomment only the first TWO - Runs OK.
4th run: uncomment only the second TWO - Runs OK.

With your script:
1st run: both commented out - Runs OK.
2nd run: uncommented only #ONE - Runs OK.
3rd run: uncommented only #TWO - Runs OK.

claudio canepa

unread,
Apr 7, 2013, 1:57:18 PM4/7/13
to cocos2d discuss
On Sun, Apr 7, 2013 at 11:09 AM, <hell...@gmail.com> wrote:
I've checked the environment variables and indeed I am not setting any pyglet options. I am using pyglet 1.2alpha1 and python 2.7.3.


Just in case I retested with pyglet 1.2alpha1, all runs ok here.
Thats... surprising.

Comparing the second and third runs of your script:
   the third run explodes in the director.init call
   at this point the only difference in app state are the ones created by the import cocos_particle_systems in the exploding script
   and what does that import ? It is equivalent to 

       from particle import ParticleSystem, Color
       from euclid import Point2

because all other code is class definition, so no running at import time.
From the point of view of side effects at import, thats equivalent (replacing the first import) to:

import random
import pyglet
from pyglet.gl import *
import math
import copy
import numpy
import ctypes

from cocosnode import CocosNode
from euclid import Point2

forced_point_sprites = None
rand = lambda: random.random() * 2 - 1

All other lines are in class and func definitions, so no running at import 

cocosnode.py has no side effects on imports, euclid does nothing openGL, the last two lines don't change the openGL state.

In fact, the only lines looking as potential culprits to change GL state are

   import pyglet
   from pyglet.gl import *

I would expect then that my script, the one only involving pyglet, will crash in the second run.

Yet your results tell otherwise.

And now what ?

  I suggest you double check that the second run with my script uncomments only ONE and that it does not traceback ( yes, yes,  just in case)

If the results are the same (no crash) , the only way I can see to investigate the issue is testing your script gradually commenting out code in cocos.particle.py and see when it ceases to crash.

As I can not reproduce the issue I cant test by myself, sorry. 

 

Qas K

unread,
Oct 26, 2013, 11:31:05 AM10/26/13
to cocos-...@googlegroups.com
Update several months later (since I had given up). On a different PC I don't have this problem either.

Daniel Gillet

unread,
Feb 8, 2015, 5:29:46 AM2/8/15
to cocos-...@googlegroups.com
Hello,

Sorry to dig out this old issue. But I ran in the very same problem on my laptop, though it works fine on my desktop.

After some investigation, I found the culprit. It's in particle.py, at line 174:
pic = pyglet.image.load('fire.png', file=pyglet.resource.file('fire.png'))

This is a class attribute, and gets executed at import, before director.init() had a chance to run. Still it's annoying that I need to initialize the director before loading images like this as class attributes.

Claudio wrote : "I would expect all the four cases to crash if sharing Open GL contexts are not supported by your gpu drivers, (because by default pyglet builds a hidden window that will share context with all the windows created by pyglet window.Window)"

So I will now try to figure out what pygelt does when calling pyglet.image.load. If someone with more experience with pyglet can point me in the right direction, I'll appreciate. I'll get back to you if I find anything...

Daniel.

Le lundi 1 avril 2013 14:23:41 UTC+2, Qasim Khalil a écrit :
When I try to import cocos.particle_systems I get the following exception pyglet.gl.ContextException: Unable to share contexts.

I do not even use any code from the import or anything else involving particle systems, I simply import the module and then I get this exception.
I even had a look at the code in particle_systems.py and I couldn't find anything that would cause this. I further tried to block the import of particle.py from there but for some reason that  had zero effect.

The full error is:

Traceback (most recent call last):
  File "run_game.py", line 10, in <module>
    main.main()
  File "main.py", line 211, in main
    director.init(width=1280, height=720, caption="Planet Eater")

Daniel Gillet

unread,
Feb 8, 2015, 8:19:18 AM2/8/15
to cocos-...@googlegroups.com
It's actually not pyglet.image.load but texture = pic.get_texture() which is causing problem.

I posted a minimal code on the pyglet discussion group: https://groups.google.com/forum/#!topic/pyglet-users/44pS-xkoObI

Daniel.
Reply all
Reply to author
Forward
0 new messages