glx-shader-sharing.c 4.89 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
/*
 * Copyright (c) 2010 VMware, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

/**
 * @file glx-shader-sharing.c
 * @author Brian Paul
 *
 * Create two GLX contexts with shared shaders.  Destroy first context,
 * draw with second context.
 */

#include "piglit-util.h"
#include "piglit-glx-util.h"

int piglit_width = 50, piglit_height = 50;

static const char *TestName = "glx-shader-sharing";

static Display *dpy;
static Window win;
static XVisualInfo *visinfo;


static const char *vert_shader_text =
   "void main() \n"
   "{ \n"
   "   gl_Position = ftransform(); \n"
   "   gl_FrontColor = gl_Color; \n"
   "} \n";

static const char *frag_shader_text =
   "void main() \n"
   "{ \n"
   "   gl_FragColor = vec4(1.0) - gl_Color; \n"
   "} \n";


static GLuint vert_shader, frag_shader;

static GLuint program;


static void
check_error(int line)
{
   GLenum e = glGetError();
   if (e)
      printf("GL Error 0x%x at line %d\n", e, line);
}


enum piglit_result
draw(Display *dpy)
{
   const GLfloat red[3] = {1.0F, 0.0F, 0.0F};
   const GLfloat green[3] = {0.0F, 1.0F, 0.0F};
77 78
   GLXContext ctx1 = piglit_get_glx_context(dpy, visinfo);
   GLXContext ctx2 = piglit_get_glx_context_share(dpy, visinfo, ctx1);
79 80
   int ok;

81 82
   if (!ctx1 || !ctx2) {
      fprintf(stderr, "%s: create contexts failed\n", TestName);
83
      piglit_report_result(PIGLIT_FAIL);
84 85
   }

86 87 88 89 90 91 92
   /*
    * Bind first context, make some shaders, draw something.
    */
   glXMakeCurrent(dpy, win, ctx1);

   glewInit();

Paul Berry's avatar
Paul Berry committed
93
   if (piglit_get_gl_version() < 20) {
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
      printf("%s: Requires OpenGL 2.0\n", TestName);
      return PIGLIT_SKIP;
   }

   glClearColor(1.0, 0.0, 0.0, 1.0);
   glClear(GL_COLOR_BUFFER_BIT);

   vert_shader = piglit_compile_shader_text(GL_VERTEX_SHADER, vert_shader_text);
   frag_shader = piglit_compile_shader_text(GL_FRAGMENT_SHADER, frag_shader_text);
   program = piglit_link_simple_program(vert_shader, frag_shader);
   check_error(__LINE__);
   assert(program);

   glUseProgram(program);
   check_error(__LINE__);

   piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
   glClearColor(0.1, 0.1, 0.1, 0.0);
   glClear(GL_COLOR_BUFFER_BIT);

   glColor3f(0, 1, 1);
   piglit_draw_rect(10, 10, piglit_width - 20, piglit_height - 20);
   check_error(__LINE__);

   ok = piglit_probe_pixel_rgb(piglit_width / 2, piglit_height / 2, red);

   glXSwapBuffers(dpy, win);

   if (!ok) {
      printf("%s: drawing with context 1 failed\n", TestName);
124
      return PIGLIT_FAIL;
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
   }

   /*
    * Destroy first context
    */
   glXDestroyContext(dpy, ctx1);


   /*
    * Draw something with second context (and shaders above)
    */
   glXMakeCurrent(dpy, win, ctx2);

   /*program = piglit_link_simple_program(vert_shader, frag_shader);*/
   check_error(__LINE__);
   assert(program);

   glUseProgram(program);
   check_error(__LINE__);

   piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
   glClearColor(0.2, 0.2, 0.2, 0.0);
   glClear(GL_COLOR_BUFFER_BIT);

   glColor3f(1, 0, 1);
   piglit_draw_rect(10, 10, piglit_width - 20, piglit_height - 20);
   check_error(__LINE__);

   ok = piglit_probe_pixel_rgb(piglit_width / 2, piglit_height / 2, green);

   glXSwapBuffers(dpy, win);

   if (!ok) {
      printf("%s: drawing with context 2 failed\n", TestName);
159
      return PIGLIT_FAIL;
160 161 162 163
   }

   glXDestroyContext(dpy, ctx2);

164

165
   return PIGLIT_PASS;
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
}


int
main(int argc, char **argv)
{
   int i;

   for(i = 1; i < argc; ++i) {
      if (strcmp(argv[i], "-auto") == 0)
         piglit_automatic = 1;
      else
         fprintf(stderr, "%s bad option: %s\n", TestName, argv[i]);
   }

   dpy = XOpenDisplay(NULL);
   if (dpy == NULL) {
      fprintf(stderr, "%s: open display failed\n", TestName);
184
      piglit_report_result(PIGLIT_FAIL);
185 186 187 188 189 190 191 192 193 194 195
   }

   visinfo = piglit_get_glx_visual(dpy);
   win = piglit_get_glx_window(dpy, visinfo);

   XMapWindow(dpy, win);

   piglit_glx_event_loop(dpy, draw);

   return 0;
}