# Using shader program from shared context

**URL:** <https://forums.imgtec.com/t/using-shader-program-from-shared-context/1641>\
**Category:** PowerVR Insider\
**Tags:** pvrvframe\
**Created:** [January 26, 2013, 1:08pm UTC](https://forums.imgtec.com/t/using-shader-program-from-shared-context/1641 "2013-01-26T13:08:33Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmytro](https://avatars.discourse-cdn.com/v4/letter/d/9fc348/32.png) [@dmytro](https://forums.imgtec.com/u/dmytro)\
**Post date:** [January 26, 2013, 1:08pm UTC](https://forums.imgtec.com/t/using-shader-program-from-shared-context/1641/1 "2013-01-26T13:08:33Z")

</div>

Hi,  
  
  
  
I have faced with an issue when trying to use a shader program in my second GL context that is shared with my first context where shaders and program were created.  
  
The issue is that call of glUseProgram for the second context generates GL\_INVALID\_OPERATION error. On target device it leads to crash, but on Imagination PC emulator it does correct rendering with specified shader program (except mentioned GL error) - this is very strange.  
  
I checked this on another emulators (ARM Mali and Google ANGLE), on them my test application did not get any GL errors at glUseProgram call, so the problem is related only to Imgtec driver.  
  
  
  
Here is pseudo-code from my application (OpenGL ES 2.0):  
  
  
  
`
eglFirstContext = eglCreateContext(eglDisplay, eglConfig, NULL, ai32ContextAttribs);
eglSecondContext = eglCreateContext(eglDisplay, eglConfig, eglFirstContext, ai32ContextAttribs); // eglSecondContext is linked with eglFirstContext

eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglFirstContext);

programId = [creating shader program with simplest vertex/fragment shaders]

eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglSecondContext);

glUseProgram(programId); // generates GL_INVALID_OPERATION
`  
  
  
  
If I comment the second eglMakeCurrent call, then problem disappears, which means it is context sharing problem for sure.  
  
I tested texture sharing on linked contexts - works as expected.  
  
  
  
Could you please clarify the problem and propose some workable solution?  
  
  
  
Thanks in advance!

---

<div class="post-metadata">

**Author:** ![JoeDavis](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.imgtec.com/joedavis/32/71_2.png) [@JoeDavis](https://forums.imgtec.com/u/JoeDavis)\
**Post date:** [January 28, 2013, 8:37am UTC](https://forums.imgtec.com/t/using-shader-program-from-shared-context/1641/2 "2013-01-28T08:37:48Z")

</div>

Hi,  
  
  
  
Unfortunately, our PVRVFrame emulator does not support multi-threaded rendering. We plan to fix this in a future release (as also mentioned [here](http://forum.imgtec.com/discussion/2388/does-pvrvframe-opengl-es-emulation-support-multi-threaded-rendering#latest)).  
  
  
  
Thanks,  
  
Joe

---

<div class="post-metadata">

**Author:** ![dmytro](https://avatars.discourse-cdn.com/v4/letter/d/9fc348/32.png) [@dmytro](https://forums.imgtec.com/u/dmytro)\
**Post date:** [January 28, 2013, 8:48am UTC](https://forums.imgtec.com/t/using-shader-program-from-shared-context/1641/3 "2013-01-28T08:48:23Z")

</div>

Hi Joe,  
  
  
  
My application is single-threaded. On device (with Imgtec driver) I also use single-thread rendering.  
  
  
  
So maybe there is some other reason…

---

<div class="post-metadata">

**Author:** ![JoeDavis](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.imgtec.com/joedavis/32/71_2.png) [@JoeDavis](https://forums.imgtec.com/u/JoeDavis)\
**Post date:** [January 28, 2013, 4:10pm UTC](https://forums.imgtec.com/t/using-shader-program-from-shared-context/1641/4 "2013-01-28T16:10:50Z")

</div>

Hi,  
  
  
  
It sounds like this may be a bug in our emulator. We’d need to investigate to confirm this.  
  
  
  
Can I ask why you would need multiple contexts if your application is single threaded?  
  
  
  
Thanks,  
  
Joe

---

<div class="post-metadata">

**Author:** ![dmytro](https://avatars.discourse-cdn.com/v4/letter/d/9fc348/32.png) [@dmytro](https://forums.imgtec.com/u/dmytro)\
**Post date:** [January 28, 2013, 6:04pm UTC](https://forums.imgtec.com/t/using-shader-program-from-shared-context/1641/5 "2013-01-28T18:04:14Z")

</div>

Hi Joe,  
  
  
  
My application uses two GL contexts in the following way: the first context is created for window surfaces that display different Views on the screen and the second one is needed for offscreen PBuffer rendering (like moving textures from one texture atlas into another).  
  
  
  
As I know, PBuffer requires separate GL context, because it potentially may have such pixel format that differs from pixel format chosen for displayable window surfaces.  
  
  
  
Since it is bug in the driver, could you recommend some workaround for this situation?  
  
  
  
Thank you!

---

<div class="post-metadata">

**Author:** ![ChrisPenrose](https://avatars.discourse-cdn.com/v4/letter/c/e36b37/32.png) [@ChrisPenrose](https://forums.imgtec.com/u/ChrisPenrose)\
**Post date:** [January 29, 2013, 9:12am UTC](https://forums.imgtec.com/t/using-shader-program-from-shared-context/1641/6 "2013-01-29T09:12:04Z")

</div>

Hi Dmytro  
  
  
  
I’m aware of one bug in the emulator where rendering to a pbuffer may not work correctly when the “Host” profile is selected from the GUI. You could try selecting a hardware profile to emulate.  
  
  
  
It does sound like the resources are not being shared correctly across contexts. I’ll see if I can reproduce the problem here.

---

<div class="post-metadata">

**Author:** ![dmytro](https://avatars.discourse-cdn.com/v4/letter/d/9fc348/32.png) [@dmytro](https://forums.imgtec.com/u/dmytro)\
**Post date:** [January 29, 2013, 11:34am UTC](https://forums.imgtec.com/t/using-shader-program-from-shared-context/1641/7 "2013-01-29T11:34:38Z")

</div>

Hi Chris,  
  
  
  
Please note that problem reproduced regardless of using pbuffer.  
  
In my test application I just used one window surface and two shared contexts to reproduce the problem.  
  
  
  
I will be happy if you check this for me.  
  
  
  
Thank you.

---

<div class="post-metadata">

**Author:** ![dmytro](https://avatars.discourse-cdn.com/v4/letter/d/9fc348/32.png) [@dmytro](https://forums.imgtec.com/u/dmytro)\
**Post date:** [January 31, 2013, 6:40am UTC](https://forums.imgtec.com/t/using-shader-program-from-shared-context/1641/8 "2013-01-31T06:40:16Z")

</div>

Hello,  
  
  
  
Any update here?

---

<div class="post-metadata">

**Author:** ![ChrisPenrose](https://avatars.discourse-cdn.com/v4/letter/c/e36b37/32.png) [@ChrisPenrose](https://forums.imgtec.com/u/ChrisPenrose)\
**Post date:** [January 31, 2013, 9:48am UTC](https://forums.imgtec.com/t/using-shader-program-from-shared-context/1641/9 "2013-01-31T09:48:43Z")

</div>

Hi Dmytro  
  
  
  
There seems to be some subtle errors in the way the emulator is currently handling context sharing, in particular the management of the program and shader objects. I’m not sure if there is a solution yet for the issue beyond “stop using shared contexts”. Apologies if that’s not a suitable workaround. I have logged this as a bug and will hopefully get to it before the next major update!

---

<div class="post-metadata">

**Author:** ![dmytro](https://avatars.discourse-cdn.com/v4/letter/d/9fc348/32.png) [@dmytro](https://forums.imgtec.com/u/dmytro)\
**Post date:** [January 31, 2013, 11:27am UTC](https://forums.imgtec.com/t/using-shader-program-from-shared-context/1641/10 "2013-01-31T11:27:52Z")

</div>

Hi Chris,  
  
  
  
It is sad that workaround does not exist, but anyway thank you very much for the clarification!
