Texture sharing between 2 contexts

hello,
i cannot seem to make texture sharing work in linux (although i got it working on win32 using wglShareLists).

i am creating 2 GLXContext’s and sharing their display lists using glXCreateContext.

then i create a texture in the 1st context and render a triangle in each context using the texture.
the texture shows up in the 1st context however in the 2nd context i get a white triangle.

i get NO errors using glGetError.

can anyone say tell me where is the problem??
i would really appreciate any help or tips!

here is the problem code:
(also available at http://www.dvoyka.com/wwwfivemagics2/share.c))

//build with: gcc share.c -o share -L/usr/X11R6/lib -lX11 -lGL -lGLU
#include <X11/Xlib.h>
#include <X11/X.h>
#include <GL/gl.h>
#include <GL/glx.h>

#define texwidth 16
#define texheight 16

Window SetupWindow(Display* dpy, char* name, int width, int height, GC* Context,
Bool Direct, GLXContext* glContext, GLXContext share)
{

Window ret = 0;

int blackColor = BlackPixel(dpy, DefaultScreen(dpy));

ret = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, width, height, 0, blackColor, blackColor);
XStoreName(dpy, ret, name);
XSelectInput(dpy, ret, StructureNotifyMask);
XMapWindow(dpy, ret);

*Context = XCreateGC(dpy, ret, 0, NULL);

XVisualInfo* vi = NULL;
int attr[] = {GLX_RGBA, GLX_RED_SIZE, 4, GLX_GREEN_SIZE, 4, GLX_BLUE_SIZE,4, None};

vi = glXChooseVisual(dpy, 0, attr);
glXMakeCurrent(dpy, None, NULL);
printf("share=%d
", share);
*glContext = glXCreateContext(dpy, vi, share, Direct);
glXMakeCurrent(dpy, ret, *glContext);

glDisable(GL_CULL_FACE);
glEnable(GL_TEXTURE_2D);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, width, 0.0f, height, 0.0f, 1.0f);

return (ret);
}

//pattern[6] contains the rgb colors for 2 pixels
GLuint CreateTexture(Display* dpy, GLXContext ctx, Window w, GLfloat pattern[6])
{
GLuint ret = 0;

GLfloat texfill[texwidth * texheight * 3];

//fill temp array with pattern
int i = 0;
for (i = 0 ; i < texwidth * texheight * 3; i += 6) {
memcpy(&(texfill[i]), pattern, 6 * sizeof(GLfloat));
}

//generate texture and fill it with pattern
glXMakeCurrent(dpy, w, ctx);
glGenTextures(1, &ret);
glBindTexture(GL_TEXTURE_2D, ret);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16, texwidth, texheight, 0, GL_RGB, GL_FLOAT, texfill);

GLenum error = glGetError();
if (error != GL_NO_ERROR) {
printf("Error while creating texture = %s …,
", gluErrorString(error));
}

return ret;
}

void render(Display* dpy, GLXContext ctx, Window w)
{
//render a triangle using texture
glXMakeCurrent(dpy, w, ctx);
glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_TRIANGLES);
glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex2f(300.0f, 300.0f);
glTexCoord2f(1.0f, 1.0f); glVertex2f(300.0f, 0.0f);
glEnd();

glXSwapBuffers(dpy, w);
}

int main()
{

Display* dpy = XOpenDisplay(0);

GC Context;
Window w1, w2;
GLXContext ctx1, ctx2;
Bool direct = False;

w1 = SetupWindow(dpy, “test1”, 320, 320, &Context, direct, &ctx1, NULL);
//ctx2 shares ctx1
w2 = SetupWindow(dpy, “test2”, 320, 320, &Context, direct, &ctx2, ctx1);

GLuint tex1, tex2;
GLuint list1, list2;
GLfloat pattern[6];

glXMakeCurrent(dpy, w1, ctx1);
glClearColor(1.0, 1.0, 0.0, 0.0);
//red green pattern
pattern[0] = 1.0; pattern[1] = 0.0; pattern[2] = 0.0;
pattern[3] = 0.0; pattern[4] = 1.0; pattern[5] = 0.0;
tex1 = CreateTexture(dpy, ctx1, w1, pattern);
list1= glGenLists(1);

glXMakeCurrent(dpy, w2, ctx2);
glClearColor(1.0, 0.0, 1.0, 0.0);
list2 = glGenLists(1);
tex2 = tex1;

printf("ctx1=%d ctx2=%d tex1=%d tex2=%d list1=%d list2=%d
", ctx1, ctx2, tex1, tex2, list1, list2);
/*
if (tex1 == tex2) {
printf("TEXTURES ARE NOOOOT SHARED!!! tex1=%u, tex2=%u…
", tex1, tex2);
} else {
printf("TEXTURES ARE SHARED OK!!! tex1=%u, tex2=%u…
", tex1, tex2);
}
*/

//infinite loop ok for testing
XEvent evt;
int active = 1;
long counter = 0;
while(active){

counter++;


render(dpy, ctx1, w1);
render(dpy, ctx2, w2);

//print glError if any
GLenum error = glGetError();
if (error != GL_NO_ERROR) {
  printf("Error = %s ..,

", gluErrorString(error));
}
}

XCloseDisplay(dpy);
return(0);
}

[This message has been edited by jadnohra (edited 06-24-2003).]

well… I got the same problem, but I did not tried to resolve it since now…

As far as I know there are some glx funcs that allow you to share some objects between some different contexts. glXCreateContext() only share some general infos. Take a look at the glx doc (somewhere here) for that.

For now, I just create my textures in the rendering context.

hope this helps (a bit).

jide

… oups…

well I look quickly again at your code and I suggest you to look for glx 1.3: so don’t use above versions that are deprecated now !

for ex: glxMakeCurrent() must be replaced by glxMakeContextCurrent() [if I remember well as I’ve got nothing here] and so on for some others.

This will refresh your code.

jdf

first of all, thnx for replying! i ve been posting about this for a week now and no answers except urs.

about glXCreateContext the sepc says (from http://www.opengl.org/developers/documentation/man_pages/hardcopy/GL/html/glx/xcreatecontext.html))

If shareList is not NULL, then all display-list indexes and
definitions are shared by context shareList and by the newly
created context. An arbitrary number of contexts can share
a single display-list space. However, all rendering
contexts that share a single display-list space must
themselves exist in the same address space. Two rendering
contexts share an address space if both are nondirect using
the same server, or if both are direct and owned by a single
process. Note that in the nondirect case, it is not
necessary for the calling threads to share an address space,
only for their related rendering contexts to share an
address space.

If the GL version is 1.1 or greater, then all	texture
objects except object	0, are shared by any contexts that
share	display	lists.

so its not like u said that it shares just some general info, or???

thx for pointing out the deprecated fts, but i have some compaitiblity reasons that force me to try to support as many (older) versions as possible, but do u think this might be causing the problem of sharing not working??

Originally posted by jadnohra:
first of all, thnx for replying! i ve been posting about this for a week now and no answers except urs.

hmmm… maybe i’m too good (hope that won’t kill me…).

[b]

about glXCreateContext the sepc says (from http://www.opengl.org/developers/documentation/man_pages/hardcopy/GL/html/gl x/xcreatecontext.html))

so its not like u said that it shares just some general info, or???

thx for pointing out the deprecated fts, but i have some compaitiblity reasons that force me to try to support as many (older) versions as possible, but do u think this might be causing the problem of sharing not working??
[/b]

well…

there are some about gl, and some about glx here, and in fact some about understanding.

textures are to be considered as objects (as for display lists). But they are at the server location. So you must do some things for that… however, as I told u before, i’ve not done it yet. (i will certainly do it when implementing client-server, or some times before doing that). No real reason for that but my lack of time, my busyfully life.

another thing, the link you gave me for glx docs isn’t the same as me: ftp://ftp.sgi.com/opengl/doc/opengl1.2/glx1.3.pdf
Read it fully and have a look again at gl specs too.

hope this helps

jide

ok thx, any tips about what kinda ‘things’ i need to do for that??

But they are at the server location. So you must do some things for that…

just look at the link !

[a]ftp://ftp.sgi.com/opengl/doc/opengl1.2/glx1.3.pdf[/a].

All is explain.

jide

… well… how to make links in fact ?

my posts look ill.

hmm another thing, you’ve got to know that only glx 1.3 is OpenGL 1.2 compliance. Older versions aren’t.

ok thx for the tips! u were helpful!
btw:
http://www.opengl.org/discussion_boards/ubb/ubbcode.html

oups… I’ve completly forgot ubb…

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.