[Xcb] Figuring out how to use XCB-GLX

Joe M joe9mail at gmail.com
Mon Sep 26 13:38:39 UTC 2016


Hello,

Sorry for bothering you about this.

I am trying to use OpenGL from xcb without using Xlib and found these
patches
https://lists.x.org/archives/xorg-devel/2013-August/037593.html . I am
trying to figure out how to use the functionality provided by those
patches.

Any suggestions / advice is greatly appreciated as I am stuck at this point.

----------------------------------
Output of the below code:
----------------------------------
gcc -std=gnu99 -Wall third.c -lxcb -lxcb-glx -lpthread -lGL -lX11 -lGLU && ./a.out

Informations of screen number 0 : 1271u:
  width.........: 4880
  height........: 1200
  white pixel...: 16777215
  black pixel...: 0

major:1, minor:4
INFO: OpenGL Version: (null)
INFO: OpenGL Version: (null)
INFO: OpenGL Version: (null)
INFO: OpenGL Version: (null)


----------------------------------
Below is my code trying to use it:
----------------------------------

/* gcc -std=gnu99 -Wall third.c -lxcb -lxcb-glx -lpthread -lGL -lX11 -lGLU && ./a.out */

#include <stdio.h>
#include <stdlib.h>
#include <xcb/xcb.h>
#include <xcb/glx.h>
#include <GL/glx.h>
#include <inttypes.h>
#include <pthread.h>
#include <unistd.h>

void *thread_function () {

   /* Open the connection to the X server. Use the DISPLAY environment variable */
   int        screen_number;
   xcb_connection_t    *connection = xcb_connect(NULL,&screen_number);
   if (xcb_connection_has_error(connection)) {
      printf("Cannot open display\n");
      exit(1);
   }

   /* Get the screen whose number is screen_number */
   const xcb_setup_t *setup = xcb_get_setup (connection);
   xcb_screen_iterator_t iter = xcb_setup_roots_iterator (setup);

   // we want the screen at index screenNum of the iterator
   int        i;
   for (i = 0; i < screen_number; ++i) {
      xcb_screen_next (&iter);
   }
   xcb_screen_t        *screen = iter.data;

   /* report */
   printf ("\n");
   printf ("Informations of screen number %i : %u"PRIu32":\n", screen_number,screen->root);
   printf ("  width.........: %"PRIu16"\n", screen->width_in_pixels);
   printf ("  height........: %"PRIu16"\n", screen->height_in_pixels);
   printf ("  white pixel...: %"PRIu32"\n", screen->white_pixel);
   printf ("  black pixel...: %"PRIu32"\n", screen->black_pixel);
   printf ("\n");

   /* Create a black graphics context for drawing in the
    * foreground */
   const xcb_drawable_t root_window = screen->root;
   xcb_gcontext_t gcontext = xcb_generate_id(connection);
   uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES;
   uint32_t values[2];
   values[0] = screen->black_pixel;
   values[1] = 0;
   xcb_create_gc(connection, gcontext, root_window, mask, values);

   /* Ask for our window's Id */
   const xcb_window_t new_window = xcb_generate_id(connection);

   /* Create the window with white background */
   mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
   values[0] = screen->white_pixel;
   values[1] = XCB_EVENT_MASK_EXPOSURE;
   xcb_create_window (connection,                             /* Connection          */
                      XCB_COPY_FROM_PARENT,          /* depth (same as root)*/
                      new_window,                           /* window Id           */
                      screen->root,                  /* parent window       */
                      0, 0,                          /* x, y                */
                      150, 150,                      /* width, height       */
                      10,                            /* border_width        */
                      XCB_WINDOW_CLASS_INPUT_OUTPUT, /* class               */
                      screen->root_visual,           /* visual              */
                      mask, values);                      /* masks, not used yet */

   /* Map the window on the screen */
   xcb_map_window (connection, new_window);

   /* Make sure commands are sent before we pause, so window is shown */
   xcb_flush (connection);

   /*    Create the OpenGL context with these attributes. */
   /*    uint32_t attribs[] = */
   /*    { */
   /*       GLX_CONTEXT_MAJOR_VERSION_ARB,	OPENGL_VERSION_MAJOR, */
   /*       GLX_CONTEXT_MINOR_VERSION_ARB,	OPENGL_VERSION_MINOR, */
   /*       GLX_CONTEXT_PROFILE_MASK_ARB,	GLX_CONTEXT_CORE_PROFILE_BIT_ARB, */
   /*       GLX_CONTEXT_FLAGS_ARB,			GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB, */
   /*       0 */
   /*    }; */
   xcb_glx_query_version_cookie_t cookie;
   xcb_glx_query_version_reply_t *reply;

   /* Send the glXQueryVersion request */
   cookie = xcb_glx_query_version(connection, 2, 1);
   reply = xcb_glx_query_version_reply(connection, cookie, NULL);
   free(reply);

   printf("major:%d, minor:%d\n", reply->major_version,reply->minor_version);

   xcb_glx_context_t glx_context = xcb_generate_id(connection);
   xcb_void_cookie_t cookie1 = xcb_glx_create_context_checked(connection,
                                                              glx_context,
                                                              screen->root_visual,
                                                              screen_number,
                                                              0,1);
   xcb_generic_error_t *error;
   if ((error = xcb_request_check(connection, cookie1))) {
      fprintf(stderr, "Could not create context\n");
   }
   /*    sleep (2); */
   /*    fprintf(stdout, "xcb_generic_error_t: %d\n", error->error_code); */
   free(error);
   /*    fprintf(stdout, "xcb_generic_error_t: %"PRIu16", %i, %i, %i\n", e->minor_code,e->major_code,e->error_code, e->response_type); */

   fprintf(stdout, "INFO: OpenGL Version: %s\n", glGetString(GL_RENDERER));
   fprintf(stdout, "INFO: OpenGL Version: %s\n", glGetString(GL_VENDOR));
   fprintf(stdout, "INFO: OpenGL Version: %s\n", glGetString(GL_VERSION));
   fprintf(stdout, "INFO: OpenGL Version: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
   fprintf(stdout, "\n\n\n\n");
   sleep (10);

   /*    /\* event loop *\/ */
   /*    while (!done && (event = xcb_wait_for_event(connection))) { */
   /*       switch (event->response_type & ~0x80) { */
   /*    case XCB_EXPOSE:    /\* draw or redraw the window *\/ */
   /*       xcb_poly_fill_rectangle(connection, window, gcontext,  1, &r); */
   /*       xcb_flush(connection); */
   /*       break; */
   /*    case XCB_KEY_PRESS:  /\* exit on key press *\/ */
   /*       done = 1; */
   /*       break; */
   /*       } */
   /*       free(event); */
   /*    } */

   /*    destroy the OpenGL context */
   xcb_glx_destroy_context_checked (connection, glx_context);

   /*       close connection to server */
   xcb_disconnect(connection);

   return 0;
}

int main(int argc, char *argv[]) {
   thread_function();
   /*    pthread_t tid1; */
   /*    pthread_t tid2; */
   /*    pthread_t tid3; */
   /*    pthread_t tid4; */
   /*    printf("Before Threads\n"); */
   /*    sleep (1); */
   /*    pthread_create(&tid1, NULL, thread_function, NULL); */
   /*    sleep (1); */
   /*    pthread_create(&tid2, NULL, thread_function, NULL); */
   /*    sleep (1); */
   /*    pthread_create(&tid3, NULL, thread_function, NULL); */
   /*    sleep (1); */
   /*    pthread_create(&tid4, NULL, thread_function, NULL); */
   /*    pthread_join(tid1, NULL); */
   /*    pthread_join(tid2, NULL); */
   /*    pthread_join(tid3, NULL); */
   /*    pthread_join(tid4, NULL); */
   /*    printf("After Threads\n"); */
   exit(0);
}

Thanks again,
Joe
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <https://lists.freedesktop.org/archives/xcb/attachments/20160926/50f0d54d/attachment.sig>


More information about the Xcb mailing list