blob: e2b79a7c0e19443ba05b190a30438e254e068b48 [file] [log] [blame]
Dave Airliec68233c2005-02-26 04:51:47 +00001/*
2 * Test the GL_NV_texture_rectangle and GL_MESA_ycrcb_texture extensions and GLX_MESA_allocate-memory
3 *
4 * Dave Airlie - Feb 2005
5 */
6
7#include <assert.h>
8#include <math.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <X11/Xlib.h>
13#include <X11/keysym.h>
14#define GL_GLEXT_PROTOTYPES
15#include <GL/glx.h>
16
17#include "../util/readtex.c" /* I know, this is a hack. */
18
Dave Airlie8ca51502005-02-26 04:56:25 +000019#define TEXTURE_FILE "../images/girl2.rgb"
Dave Airliec68233c2005-02-26 04:51:47 +000020
21static GLfloat Xrot = 0, Yrot = 0, Zrot = 0;
22static GLint ImgWidth, ImgHeight;
23static GLushort *ImageYUV = NULL;
24static void *glx_memory;
25
26static void DrawObject(void)
27{
28 glBegin(GL_QUADS);
29
30 glTexCoord2f(0, 0);
31 glVertex2f(-1.0, -1.0);
32
33 glTexCoord2f(ImgWidth, 0);
34 glVertex2f(1.0, -1.0);
35
36 glTexCoord2f(ImgWidth, ImgHeight);
37 glVertex2f(1.0, 1.0);
38
39 glTexCoord2f(0, ImgHeight);
40 glVertex2f(-1.0, 1.0);
41
42 glEnd();
43}
44
45
46static void scr_Display( void )
47{
48 glClear( GL_COLOR_BUFFER_BIT );
49
50 glPushMatrix();
51 glRotatef(Xrot, 1.0, 0.0, 0.0);
52 glRotatef(Yrot, 0.0, 1.0, 0.0);
53 glRotatef(Zrot, 0.0, 0.0, 1.0);
54 DrawObject();
55 glPopMatrix();
56
57}
58
59
60static void Reshape( int width, int height )
61{
62 glViewport( 0, 0, width, height );
63 glMatrixMode( GL_PROJECTION );
64 glLoadIdentity();
65 glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
66 glMatrixMode( GL_MODELVIEW );
67 glLoadIdentity();
68 glTranslatef( 0.0, 0.0, -15.0 );
69}
70
71static int queryClient(Display *dpy, int screen)
72{
73#ifdef GLX_MESA_allocate_memory
74 char *extensions;
75
76 extensions = (char *)glXQueryExtensionsString(dpy, screen);
77 if (!extensions || !strstr(extensions,"GLX_MESA_allocate_memory")) {
78 return 0;
79 }
80
81 return 1;
82#else
83 return 0;
84#endif
85}
86
87static int
88query_extension(char* extName) {
89 char *p = (char *) glGetString(GL_EXTENSIONS);
90 char *end = p + strlen(p);
91 while (p < end) {
92 int n = strcspn(p, " ");
93 if ((strlen(extName) == n) && (strncmp(extName, p, n) == 0))
94 return GL_TRUE;
95 p += (n + 1);
96 }
97 return GL_FALSE;
98}
99
100static void Init( int argc, char *argv[] , Display *dpy, int screen, Window win)
101{
102 GLuint texObj = 100;
103 const char *file;
104 void *glx_memory;
105
106 if (!query_extension("GL_NV_texture_rectangle")) {
107 printf("Sorry, GL_NV_texture_rectangle is required\n");
108 exit(0);
109 }
110
111 if (!query_extension("GL_MESA_ycbcr_texture")) {
112 printf("Sorry, GL_MESA_ycbcr_texture is required\n");
113 exit(0);
114 }
115
116 if (!queryClient(dpy, screen)) {
117 printf("Sorry, GLX_MESA_allocate_memory is required\n");
118 exit(0);
119 }
120
121 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, 1);
122 glBindTexture(GL_TEXTURE_RECTANGLE_NV, texObj);
123#ifdef LINEAR_FILTER
124 /* linear filtering looks much nicer but is much slower for Mesa */
125 glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
126 glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
127#else
128 glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
129 glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
130#endif
131
132 if (argc > 1)
133 file = argv[1];
134 else
135 file = TEXTURE_FILE;
136
137 ImageYUV = LoadYUVImage(file, &ImgWidth, &ImgHeight);
138 if (!ImageYUV) {
139 printf("Couldn't read %s\n", TEXTURE_FILE);
140 exit(0);
141 }
142
143 glx_memory = glXAllocateMemoryMESA(dpy, screen, ImgWidth * ImgHeight * 2, 0, 0 ,0);
144 if (!glx_memory)
145 {
146 fprintf(stderr,"Failed to allocate MESA memory\n");
147 exit(-1);
148 }
149
150 memcpy(glx_memory, ImageYUV, ImgWidth * ImgHeight * 2);
151
152 printf("Image: %dx%d\n", ImgWidth, ImgHeight);
153
154 glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0,
155 GL_YCBCR_MESA, ImgWidth, ImgHeight, 0,
156 GL_YCBCR_MESA, GL_UNSIGNED_SHORT_8_8_APPLE, glx_memory);
157
158 assert(glGetError() == GL_NO_ERROR);
159
160 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
161
162 glEnable(GL_TEXTURE_RECTANGLE_NV);
163
164 glShadeModel(GL_FLAT);
165 glClearColor(0.3, 0.3, 0.4, 1.0);
166
167}
168
169/*
170 * Create an RGB, double-buffered window.
171 * Return the window and context handles.
172 */
173static void
174make_window( Display *dpy, const char *name,
175 int x, int y, int width, int height,
176 Window *winRet, GLXContext *ctxRet)
177{
178 int attribs[] = { GLX_RGBA,
179 GLX_RED_SIZE, 1,
180 GLX_GREEN_SIZE, 1,
181 GLX_BLUE_SIZE, 1,
182 GLX_DOUBLEBUFFER,
183 GLX_DEPTH_SIZE, 1,
184 None };
185 int scrnum;
186 XSetWindowAttributes attr;
187 unsigned long mask;
188 Window root;
189 Window win;
190 GLXContext ctx;
191 XVisualInfo *visinfo;
192
193 scrnum = DefaultScreen( dpy );
194 root = RootWindow( dpy, scrnum );
195
196 visinfo = glXChooseVisual( dpy, scrnum, attribs );
197 if (!visinfo) {
198 printf("Error: couldn't get an RGB, Double-buffered visual\n");
199 exit(1);
200 }
201
202 /* window attributes */
203 attr.background_pixel = 0;
204 attr.border_pixel = 0;
205 attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone);
206 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
207 attr.override_redirect = 0;
208 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask | CWOverrideRedirect;
209
210 win = XCreateWindow( dpy, root, 0, 0, width, height,
211 0, visinfo->depth, InputOutput,
212 visinfo->visual, mask, &attr );
213
214 /* set hints and properties */
215 {
216 XSizeHints sizehints;
217 sizehints.x = x;
218 sizehints.y = y;
219 sizehints.width = width;
220 sizehints.height = height;
221 sizehints.flags = USSize | USPosition;
222 XSetNormalHints(dpy, win, &sizehints);
223 XSetStandardProperties(dpy, win, name, name,
224 None, (char **)NULL, 0, &sizehints);
225 }
226
227 ctx = glXCreateContext( dpy, visinfo, NULL, True );
228 if (!ctx) {
229 printf("Error: glXCreateContext failed\n");
230 exit(1);
231 }
232
233 XFree(visinfo);
234
235 *winRet = win;
236 *ctxRet = ctx;
237}
238
239
240static void
241event_loop(Display *dpy, Window win)
242{
243 while (1) {
244 while (XPending(dpy) > 0) {
245 XEvent event;
246 XNextEvent(dpy, &event);
247 switch (event.type) {
248 case Expose:
249 /* we'll redraw below */
250 break;
251 case ConfigureNotify:
252 Reshape(event.xconfigure.width, event.xconfigure.height);
253 break;
254 case KeyPress:
255 {
256 char buffer[10];
257 int r, code;
258 code = XLookupKeysym(&event.xkey, 0);
259 r = XLookupString(&event.xkey, buffer, sizeof(buffer),
260 NULL, NULL);
261 if (buffer[0] == 27) {
262 /* escape */
263 return;
264
265 }
266 }
267 }
268 }
269
270 }
271}
272
273
274int
275main(int argc, char *argv[])
276{
277 Display *dpy;
278 Window win;
279 GLXContext ctx;
280 char *dpyName = NULL;
281 GLboolean printInfo = GL_FALSE;
282 int i;
283
284 for (i = 1; i < argc; i++) {
285 if (strcmp(argv[i], "-display") == 0) {
286 dpyName = argv[i+1];
287 i++;
288 }
289 else if (strcmp(argv[i], "-info") == 0) {
290 printInfo = GL_TRUE;
291 }
292 else
293 printf("Warrning: unknown parameter: %s\n", argv[i]);
294 }
295
296 dpy = XOpenDisplay(dpyName);
297 if (!dpy) {
298 printf("Error: couldn't open display %s\n",
Brian73eee242006-12-13 08:30:26 -0700299 XDisplayName(dpyName));
Dave Airliec68233c2005-02-26 04:51:47 +0000300 return -1;
301 }
302
303 make_window(dpy, "yuvrect_client", 0, 0, 300, 300, &win, &ctx);
304 XMapWindow(dpy, win);
305 glXMakeCurrent(dpy, win, ctx);
306
307 if (printInfo) {
308 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
309 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
310 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
311 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
312 }
313
314 Init(argc, argv, dpy, DefaultScreen(dpy), win);
315
316 scr_Display();
317 glXSwapBuffers(dpy, win);
318 event_loop(dpy, win);
319
320 glXFreeMemoryMESA(dpy, DefaultScreen(dpy), glx_memory);
321 glXDestroyContext(dpy, ctx);
322 XDestroyWindow(dpy, win);
323 XCloseDisplay(dpy);
324
325 return 0;
326}