i965: fix bugs in projective texture coordinates
[mesa.git] / src / mesa / drivers / dri / intel / intel_screen.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "main/glheader.h"
29 #include "main/context.h"
30 #include "main/framebuffer.h"
31 #include "main/renderbuffer.h"
32
33 #include "utils.h"
34 #include "vblank.h"
35 #include "xmlpool.h"
36
37 #include "intel_batchbuffer.h"
38 #include "intel_buffers.h"
39 #include "intel_bufmgr.h"
40 #include "intel_chipset.h"
41 #include "intel_extensions.h"
42 #include "intel_fbo.h"
43 #include "intel_regions.h"
44 #include "intel_swapbuffers.h"
45 #include "intel_screen.h"
46 #include "intel_span.h"
47 #include "intel_tex.h"
48
49 #include "i915_drm.h"
50 #include "i830_dri.h"
51
52
53 PUBLIC const char __driConfigOptions[] =
54 DRI_CONF_BEGIN
55 DRI_CONF_SECTION_PERFORMANCE
56 DRI_CONF_FTHROTTLE_MODE(DRI_CONF_FTHROTTLE_IRQS)
57 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_ALWAYS_SYNC)
58 /* Options correspond to DRI_CONF_BO_REUSE_DISABLED,
59 * DRI_CONF_BO_REUSE_ALL
60 */
61 DRI_CONF_OPT_BEGIN_V(bo_reuse, enum, 1, "0:1")
62 DRI_CONF_DESC_BEGIN(en, "Buffer object reuse")
63 DRI_CONF_ENUM(0, "Disable buffer object reuse")
64 DRI_CONF_ENUM(1, "Enable reuse of all sizes of buffer objects")
65 DRI_CONF_DESC_END
66 DRI_CONF_OPT_END
67 DRI_CONF_SECTION_END
68 DRI_CONF_SECTION_QUALITY
69 DRI_CONF_FORCE_S3TC_ENABLE(false)
70 DRI_CONF_ALLOW_LARGE_TEXTURES(2)
71 DRI_CONF_SECTION_END
72 DRI_CONF_SECTION_DEBUG
73 DRI_CONF_NO_RAST(false)
74 DRI_CONF_ALWAYS_FLUSH_BATCH(false)
75 DRI_CONF_ALWAYS_FLUSH_CACHE(false)
76 DRI_CONF_SECTION_END
77 DRI_CONF_END;
78
79 const GLuint __driNConfigOptions = 8;
80
81 #ifdef USE_NEW_INTERFACE
82 static PFNGLXCREATECONTEXTMODES create_context_modes = NULL;
83 #endif /*USE_NEW_INTERFACE */
84
85 /**
86 * Map all the memory regions described by the screen.
87 * \return GL_TRUE if success, GL_FALSE if error.
88 */
89 GLboolean
90 intelMapScreenRegions(__DRIscreenPrivate * sPriv)
91 {
92 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
93
94 if (0)
95 _mesa_printf("TEX 0x%08x ", intelScreen->tex.handle);
96 if (intelScreen->tex.size != 0) {
97 if (drmMap(sPriv->fd,
98 intelScreen->tex.handle,
99 intelScreen->tex.size,
100 (drmAddress *) & intelScreen->tex.map) != 0) {
101 intelUnmapScreenRegions(intelScreen);
102 return GL_FALSE;
103 }
104 }
105
106 return GL_TRUE;
107 }
108
109 void
110 intelUnmapScreenRegions(intelScreenPrivate * intelScreen)
111 {
112 if (intelScreen->tex.map) {
113 drmUnmap(intelScreen->tex.map, intelScreen->tex.size);
114 intelScreen->tex.map = NULL;
115 }
116 }
117
118
119 static void
120 intelPrintDRIInfo(intelScreenPrivate * intelScreen,
121 __DRIscreenPrivate * sPriv, I830DRIPtr gDRIPriv)
122 {
123 fprintf(stderr, "*** Front size: 0x%x offset: 0x%x pitch: %d\n",
124 intelScreen->front.size, intelScreen->front.offset,
125 intelScreen->pitch);
126 fprintf(stderr, "*** Back size: 0x%x offset: 0x%x pitch: %d\n",
127 intelScreen->back.size, intelScreen->back.offset,
128 intelScreen->pitch);
129 fprintf(stderr, "*** Depth size: 0x%x offset: 0x%x pitch: %d\n",
130 intelScreen->depth.size, intelScreen->depth.offset,
131 intelScreen->pitch);
132 fprintf(stderr, "*** Texture size: 0x%x offset: 0x%x\n",
133 intelScreen->tex.size, intelScreen->tex.offset);
134 fprintf(stderr, "*** Memory : 0x%x\n", gDRIPriv->mem);
135 }
136
137
138 static void
139 intelPrintSAREA(const drm_i915_sarea_t * sarea)
140 {
141 fprintf(stderr, "SAREA: sarea width %d height %d\n", sarea->width,
142 sarea->height);
143 fprintf(stderr, "SAREA: pitch: %d\n", sarea->pitch);
144 fprintf(stderr,
145 "SAREA: front offset: 0x%08x size: 0x%x handle: 0x%x tiled: %d\n",
146 sarea->front_offset, sarea->front_size,
147 (unsigned) sarea->front_handle, sarea->front_tiled);
148 fprintf(stderr,
149 "SAREA: back offset: 0x%08x size: 0x%x handle: 0x%x tiled: %d\n",
150 sarea->back_offset, sarea->back_size,
151 (unsigned) sarea->back_handle, sarea->back_tiled);
152 fprintf(stderr, "SAREA: depth offset: 0x%08x size: 0x%x handle: 0x%x tiled: %d\n",
153 sarea->depth_offset, sarea->depth_size,
154 (unsigned) sarea->depth_handle, sarea->depth_tiled);
155 fprintf(stderr, "SAREA: tex offset: 0x%08x size: 0x%x handle: 0x%x\n",
156 sarea->tex_offset, sarea->tex_size, (unsigned) sarea->tex_handle);
157 }
158
159
160 /**
161 * A number of the screen parameters are obtained/computed from
162 * information in the SAREA. This function updates those parameters.
163 */
164 static void
165 intelUpdateScreenFromSAREA(intelScreenPrivate * intelScreen,
166 drm_i915_sarea_t * sarea)
167 {
168 intelScreen->width = sarea->width;
169 intelScreen->height = sarea->height;
170 intelScreen->pitch = sarea->pitch;
171
172 intelScreen->front.offset = sarea->front_offset;
173 intelScreen->front.handle = sarea->front_handle;
174 intelScreen->front.size = sarea->front_size;
175 intelScreen->front.tiled = sarea->front_tiled;
176
177 intelScreen->back.offset = sarea->back_offset;
178 intelScreen->back.handle = sarea->back_handle;
179 intelScreen->back.size = sarea->back_size;
180 intelScreen->back.tiled = sarea->back_tiled;
181
182 intelScreen->depth.offset = sarea->depth_offset;
183 intelScreen->depth.handle = sarea->depth_handle;
184 intelScreen->depth.size = sarea->depth_size;
185 intelScreen->depth.tiled = sarea->depth_tiled;
186
187 if (intelScreen->driScrnPriv->ddx_version.minor >= 9) {
188 intelScreen->front.bo_handle = sarea->front_bo_handle;
189 intelScreen->back.bo_handle = sarea->back_bo_handle;
190 intelScreen->depth.bo_handle = sarea->depth_bo_handle;
191 } else {
192 intelScreen->front.bo_handle = -1;
193 intelScreen->back.bo_handle = -1;
194 intelScreen->depth.bo_handle = -1;
195 }
196
197 intelScreen->tex.offset = sarea->tex_offset;
198 intelScreen->logTextureGranularity = sarea->log_tex_granularity;
199 intelScreen->tex.handle = sarea->tex_handle;
200 intelScreen->tex.size = sarea->tex_size;
201
202 if (0)
203 intelPrintSAREA(sarea);
204 }
205
206 static const __DRItexOffsetExtension intelTexOffsetExtension = {
207 { __DRI_TEX_OFFSET },
208 intelSetTexOffset,
209 };
210
211 static const __DRItexBufferExtension intelTexBufferExtension = {
212 { __DRI_TEX_BUFFER, __DRI_TEX_BUFFER_VERSION },
213 intelSetTexBuffer,
214 intelSetTexBuffer2,
215 };
216
217 static const __DRIextension *intelScreenExtensions[] = {
218 &driReadDrawableExtension,
219 &driCopySubBufferExtension.base,
220 &driSwapControlExtension.base,
221 &driFrameTrackingExtension.base,
222 &driMediaStreamCounterExtension.base,
223 &intelTexOffsetExtension.base,
224 &intelTexBufferExtension.base,
225 NULL
226 };
227
228 static GLboolean
229 intel_get_param(__DRIscreenPrivate *psp, int param, int *value)
230 {
231 int ret;
232 struct drm_i915_getparam gp;
233
234 gp.param = param;
235 gp.value = value;
236
237 ret = drmCommandWriteRead(psp->fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
238 if (ret) {
239 fprintf(stderr, "drm_i915_getparam: %d\n", ret);
240 return GL_FALSE;
241 }
242
243 return GL_TRUE;
244 }
245
246 static GLboolean intelInitDriver(__DRIscreenPrivate *sPriv)
247 {
248 intelScreenPrivate *intelScreen;
249 I830DRIPtr gDRIPriv = (I830DRIPtr) sPriv->pDevPriv;
250 drm_i915_sarea_t *sarea;
251
252 if (sPriv->devPrivSize != sizeof(I830DRIRec)) {
253 fprintf(stderr,
254 "\nERROR! sizeof(I830DRIRec) does not match passed size from device driver\n");
255 return GL_FALSE;
256 }
257
258 /* Allocate the private area */
259 intelScreen = (intelScreenPrivate *) CALLOC(sizeof(intelScreenPrivate));
260 if (!intelScreen) {
261 fprintf(stderr, "\nERROR! Allocating private area failed\n");
262 return GL_FALSE;
263 }
264 /* parse information in __driConfigOptions */
265 driParseOptionInfo(&intelScreen->optionCache,
266 __driConfigOptions, __driNConfigOptions);
267
268 intelScreen->driScrnPriv = sPriv;
269 sPriv->private = (void *) intelScreen;
270 sarea = (drm_i915_sarea_t *)
271 (((GLubyte *) sPriv->pSAREA) + gDRIPriv->sarea_priv_offset);
272 intelScreen->sarea = sarea;
273
274 intelScreen->deviceID = gDRIPriv->deviceID;
275
276 intelUpdateScreenFromSAREA(intelScreen, sarea);
277
278 if (!intelMapScreenRegions(sPriv)) {
279 fprintf(stderr, "\nERROR! mapping regions\n");
280 _mesa_free(intelScreen);
281 sPriv->private = NULL;
282 return GL_FALSE;
283 }
284
285 if (0)
286 intelPrintDRIInfo(intelScreen, sPriv, gDRIPriv);
287
288 intelScreen->drmMinor = sPriv->drm_version.minor;
289
290 /* Determine if IRQs are active? */
291 if (!intel_get_param(sPriv, I915_PARAM_IRQ_ACTIVE,
292 &intelScreen->irq_active))
293 return GL_FALSE;
294
295 sPriv->extensions = intelScreenExtensions;
296
297 return GL_TRUE;
298 }
299
300
301 static void
302 intelDestroyScreen(__DRIscreenPrivate * sPriv)
303 {
304 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
305
306 dri_bufmgr_destroy(intelScreen->bufmgr);
307 intelUnmapScreenRegions(intelScreen);
308 driDestroyOptionCache(&intelScreen->optionCache);
309
310 FREE(intelScreen);
311 sPriv->private = NULL;
312 }
313
314
315 /**
316 * This is called when we need to set up GL rendering to a new X window.
317 */
318 static GLboolean
319 intelCreateBuffer(__DRIscreenPrivate * driScrnPriv,
320 __DRIdrawablePrivate * driDrawPriv,
321 const __GLcontextModes * mesaVis, GLboolean isPixmap)
322 {
323 if (isPixmap) {
324 return GL_FALSE; /* not implemented */
325 }
326 else {
327 GLboolean swStencil = (mesaVis->stencilBits > 0 &&
328 mesaVis->depthBits != 24);
329 GLenum rgbFormat;
330
331 struct intel_framebuffer *intel_fb = CALLOC_STRUCT(intel_framebuffer);
332
333 if (!intel_fb)
334 return GL_FALSE;
335
336 _mesa_initialize_framebuffer(&intel_fb->Base, mesaVis);
337
338 if (mesaVis->redBits == 5)
339 rgbFormat = GL_RGB5;
340 else if (mesaVis->alphaBits == 0)
341 rgbFormat = GL_RGB8;
342 else
343 rgbFormat = GL_RGBA8;
344
345 /* setup the hardware-based renderbuffers */
346 intel_fb->color_rb[0] = intel_create_renderbuffer(rgbFormat);
347 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_FRONT_LEFT,
348 &intel_fb->color_rb[0]->Base);
349
350 if (mesaVis->doubleBufferMode) {
351 intel_fb->color_rb[1] = intel_create_renderbuffer(rgbFormat);
352
353 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_BACK_LEFT,
354 &intel_fb->color_rb[1]->Base);
355
356 }
357
358 if (mesaVis->depthBits == 24) {
359 if (mesaVis->stencilBits == 8) {
360 /* combined depth/stencil buffer */
361 struct intel_renderbuffer *depthStencilRb
362 = intel_create_renderbuffer(GL_DEPTH24_STENCIL8_EXT);
363 /* note: bind RB to two attachment points */
364 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_DEPTH,
365 &depthStencilRb->Base);
366 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_STENCIL,
367 &depthStencilRb->Base);
368 } else {
369 struct intel_renderbuffer *depthRb
370 = intel_create_renderbuffer(GL_DEPTH_COMPONENT24);
371 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_DEPTH,
372 &depthRb->Base);
373 }
374 }
375 else if (mesaVis->depthBits == 16) {
376 /* just 16-bit depth buffer, no hw stencil */
377 struct intel_renderbuffer *depthRb
378 = intel_create_renderbuffer(GL_DEPTH_COMPONENT16);
379 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_DEPTH, &depthRb->Base);
380 }
381
382 /* now add any/all software-based renderbuffers we may need */
383 _mesa_add_soft_renderbuffers(&intel_fb->Base,
384 GL_FALSE, /* never sw color */
385 GL_FALSE, /* never sw depth */
386 swStencil, mesaVis->accumRedBits > 0,
387 GL_FALSE, /* never sw alpha */
388 GL_FALSE /* never sw aux */ );
389 driDrawPriv->driverPrivate = (void *) intel_fb;
390
391 return GL_TRUE;
392 }
393 }
394
395 static void
396 intelDestroyBuffer(__DRIdrawablePrivate * driDrawPriv)
397 {
398 struct intel_framebuffer *intel_fb = driDrawPriv->driverPrivate;
399 struct intel_renderbuffer *depth_rb;
400 struct intel_renderbuffer *stencil_rb;
401
402 if (intel_fb) {
403 if (intel_fb->color_rb[0]) {
404 intel_renderbuffer_set_region(intel_fb->color_rb[0], NULL);
405 }
406
407 if (intel_fb->color_rb[1]) {
408 intel_renderbuffer_set_region(intel_fb->color_rb[1], NULL);
409 }
410
411 depth_rb = intel_get_renderbuffer(&intel_fb->Base, BUFFER_DEPTH);
412 if (depth_rb) {
413 intel_renderbuffer_set_region(depth_rb, NULL);
414 }
415
416 stencil_rb = intel_get_renderbuffer(&intel_fb->Base, BUFFER_STENCIL);
417 if (stencil_rb) {
418 intel_renderbuffer_set_region(stencil_rb, NULL);
419 }
420 }
421
422 _mesa_reference_framebuffer((GLframebuffer **)(&(driDrawPriv->driverPrivate)), NULL);
423 }
424
425
426 /**
427 * Get information about previous buffer swaps.
428 */
429 static int
430 intelGetSwapInfo(__DRIdrawablePrivate * dPriv, __DRIswapInfo * sInfo)
431 {
432 struct intel_framebuffer *intel_fb;
433
434 if ((dPriv == NULL) || (dPriv->driverPrivate == NULL)
435 || (sInfo == NULL)) {
436 return -1;
437 }
438
439 intel_fb = dPriv->driverPrivate;
440 sInfo->swap_count = intel_fb->swap_count;
441 sInfo->swap_ust = intel_fb->swap_ust;
442 sInfo->swap_missed_count = intel_fb->swap_missed_count;
443
444 sInfo->swap_missed_usage = (sInfo->swap_missed_count != 0)
445 ? driCalculateSwapUsage(dPriv, 0, intel_fb->swap_missed_ust)
446 : 0.0;
447
448 return 0;
449 }
450
451
452 /* There are probably better ways to do this, such as an
453 * init-designated function to register chipids and createcontext
454 * functions.
455 */
456 extern GLboolean i830CreateContext(const __GLcontextModes * mesaVis,
457 __DRIcontextPrivate * driContextPriv,
458 void *sharedContextPrivate);
459
460 extern GLboolean i915CreateContext(const __GLcontextModes * mesaVis,
461 __DRIcontextPrivate * driContextPriv,
462 void *sharedContextPrivate);
463 extern GLboolean brwCreateContext(const __GLcontextModes * mesaVis,
464 __DRIcontextPrivate * driContextPriv,
465 void *sharedContextPrivate);
466
467 static GLboolean
468 intelCreateContext(const __GLcontextModes * mesaVis,
469 __DRIcontextPrivate * driContextPriv,
470 void *sharedContextPrivate)
471 {
472 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
473 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
474
475 #ifdef I915
476 if (IS_9XX(intelScreen->deviceID)) {
477 if (!IS_965(intelScreen->deviceID)) {
478 return i915CreateContext(mesaVis, driContextPriv,
479 sharedContextPrivate);
480 }
481 } else {
482 intelScreen->no_vbo = GL_TRUE;
483 return i830CreateContext(mesaVis, driContextPriv, sharedContextPrivate);
484 }
485 #else
486 if (IS_965(intelScreen->deviceID))
487 return brwCreateContext(mesaVis, driContextPriv, sharedContextPrivate);
488 #endif
489 fprintf(stderr, "Unrecognized deviceID %x\n", intelScreen->deviceID);
490 return GL_FALSE;
491 }
492
493
494 static __DRIconfig **
495 intelFillInModes(__DRIscreenPrivate *psp,
496 unsigned pixel_bits, unsigned depth_bits,
497 unsigned stencil_bits, GLboolean have_back_buffer)
498 {
499 __DRIconfig **configs;
500 __GLcontextModes *m;
501 unsigned depth_buffer_factor;
502 unsigned back_buffer_factor;
503 int i;
504
505 /* GLX_SWAP_COPY_OML is only supported because the Intel driver doesn't
506 * support pageflipping at all.
507 */
508 static const GLenum back_buffer_modes[] = {
509 GLX_NONE, GLX_SWAP_UNDEFINED_OML, GLX_SWAP_COPY_OML
510 };
511
512 uint8_t depth_bits_array[3];
513 uint8_t stencil_bits_array[3];
514 uint8_t msaa_samples_array[1];
515
516 depth_bits_array[0] = 0;
517 depth_bits_array[1] = depth_bits;
518 depth_bits_array[2] = depth_bits;
519
520 /* Just like with the accumulation buffer, always provide some modes
521 * with a stencil buffer. It will be a sw fallback, but some apps won't
522 * care about that.
523 */
524 stencil_bits_array[0] = 0;
525 stencil_bits_array[1] = 0;
526 if (depth_bits == 24)
527 stencil_bits_array[1] = (stencil_bits == 0) ? 8 : stencil_bits;
528
529 stencil_bits_array[2] = (stencil_bits == 0) ? 8 : stencil_bits;
530
531 msaa_samples_array[0] = 0;
532
533 depth_buffer_factor = ((depth_bits != 0) || (stencil_bits != 0)) ? 3 : 1;
534 back_buffer_factor = (have_back_buffer) ? 3 : 1;
535
536 if (pixel_bits == 16) {
537 configs = driCreateConfigs(GL_RGB, GL_UNSIGNED_SHORT_5_6_5,
538 depth_bits_array, stencil_bits_array,
539 depth_buffer_factor, back_buffer_modes,
540 back_buffer_factor,
541 msaa_samples_array, 1);
542 }
543 else {
544 __DRIconfig **configs_a8r8g8b8;
545 __DRIconfig **configs_x8r8g8b8;
546
547 configs_a8r8g8b8 = driCreateConfigs(GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV,
548 depth_bits_array,
549 stencil_bits_array,
550 depth_buffer_factor,
551 back_buffer_modes,
552 back_buffer_factor,
553 msaa_samples_array, 1);
554 configs_x8r8g8b8 = driCreateConfigs(GL_BGR, GL_UNSIGNED_INT_8_8_8_8_REV,
555 depth_bits_array,
556 stencil_bits_array,
557 depth_buffer_factor,
558 back_buffer_modes,
559 back_buffer_factor,
560 msaa_samples_array, 1);
561 configs = driConcatConfigs(configs_a8r8g8b8, configs_x8r8g8b8);
562 }
563
564 if (configs == NULL) {
565 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
566 __LINE__);
567 return NULL;
568 }
569
570 /* Mark the visual as slow if there are "fake" stencil bits.
571 */
572 for (i = 0; configs[i]; i++) {
573 m = &configs[i]->modes;
574 if ((m->stencilBits != 0) && (m->stencilBits != stencil_bits)) {
575 m->visualRating = GLX_SLOW_CONFIG;
576 }
577 }
578
579 return configs;
580 }
581
582 static GLboolean
583 intel_init_bufmgr(intelScreenPrivate *intelScreen)
584 {
585 GLboolean gem_disable = getenv("INTEL_NO_GEM") != NULL;
586 int gem_kernel = 0;
587 GLboolean gem_supported;
588 struct drm_i915_getparam gp;
589 __DRIscreenPrivate *spriv = intelScreen->driScrnPriv;
590 int num_fences;
591
592 intelScreen->no_hw = getenv("INTEL_NO_HW") != NULL;
593
594 gp.param = I915_PARAM_HAS_GEM;
595 gp.value = &gem_kernel;
596
597 (void) drmCommandWriteRead(spriv->fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
598
599 /* If we've got a new enough DDX that's initializing GEM and giving us
600 * object handles for the shared buffers, use that.
601 */
602 intelScreen->ttm = GL_FALSE;
603 if (intelScreen->driScrnPriv->dri2.enabled)
604 gem_supported = GL_TRUE;
605 else if (intelScreen->driScrnPriv->ddx_version.minor >= 9 &&
606 gem_kernel &&
607 intelScreen->front.bo_handle != -1)
608 gem_supported = GL_TRUE;
609 else
610 gem_supported = GL_FALSE;
611
612 if (!gem_disable && gem_supported) {
613 intelScreen->bufmgr = intel_bufmgr_gem_init(spriv->fd, BATCH_SZ);
614 if (intelScreen->bufmgr != NULL)
615 intelScreen->ttm = GL_TRUE;
616 }
617 /* Otherwise, use the classic buffer manager. */
618 if (intelScreen->bufmgr == NULL) {
619 if (gem_disable) {
620 fprintf(stderr, "GEM disabled. Using classic.\n");
621 } else {
622 fprintf(stderr, "Failed to initialize GEM. "
623 "Falling back to classic.\n");
624 }
625
626 if (intelScreen->tex.size == 0) {
627 fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
628 __func__, __LINE__);
629 return GL_FALSE;
630 }
631
632 intelScreen->bufmgr =
633 intel_bufmgr_fake_init(spriv->fd,
634 intelScreen->tex.offset,
635 intelScreen->tex.map,
636 intelScreen->tex.size,
637 (unsigned int * volatile)
638 &intelScreen->sarea->last_dispatch);
639 }
640
641 if (intel_get_param(spriv, I915_PARAM_NUM_FENCES_AVAIL, &num_fences))
642 intelScreen->kernel_exec_fencing = !!num_fences;
643 else
644 intelScreen->kernel_exec_fencing = GL_FALSE;
645
646 return GL_TRUE;
647 }
648
649 /**
650 * This is the driver specific part of the createNewScreen entry point.
651 * Called when using legacy DRI.
652 *
653 * \todo maybe fold this into intelInitDriver
654 *
655 * \return the __GLcontextModes supported by this driver
656 */
657 static const __DRIconfig **intelInitScreen(__DRIscreenPrivate *psp)
658 {
659 intelScreenPrivate *intelScreen;
660 #ifdef I915
661 static const __DRIversion ddx_expected = { 1, 5, 0 };
662 #else
663 static const __DRIversion ddx_expected = { 1, 6, 0 };
664 #endif
665 static const __DRIversion dri_expected = { 4, 0, 0 };
666 static const __DRIversion drm_expected = { 1, 5, 0 };
667 I830DRIPtr dri_priv = (I830DRIPtr) psp->pDevPriv;
668
669 if (!driCheckDriDdxDrmVersions2("i915",
670 &psp->dri_version, &dri_expected,
671 &psp->ddx_version, &ddx_expected,
672 &psp->drm_version, &drm_expected)) {
673 return NULL;
674 }
675
676 /* Calling driInitExtensions here, with a NULL context pointer,
677 * does not actually enable the extensions. It just makes sure
678 * that all the dispatch offsets for all the extensions that
679 * *might* be enables are known. This is needed because the
680 * dispatch offsets need to be known when _mesa_context_create is
681 * called, but we can't enable the extensions until we have a
682 * context pointer.
683 *
684 * Hello chicken. Hello egg. How are you two today?
685 */
686 intelInitExtensions(NULL, GL_TRUE);
687
688 if (!intelInitDriver(psp))
689 return NULL;
690
691 psp->extensions = intelScreenExtensions;
692
693 intelScreen = psp->private;
694 if (!intel_init_bufmgr(intelScreen))
695 return GL_FALSE;
696
697 return (const __DRIconfig **)
698 intelFillInModes(psp, dri_priv->cpp * 8,
699 (dri_priv->cpp == 2) ? 16 : 24,
700 (dri_priv->cpp == 2) ? 0 : 8, 1);
701 }
702
703 struct intel_context *intelScreenContext(intelScreenPrivate *intelScreen)
704 {
705 /*
706 * This should probably change to have the screen allocate a dummy
707 * context at screen creation. For now just use the current context.
708 */
709
710 GET_CURRENT_CONTEXT(ctx);
711 if (ctx == NULL) {
712 _mesa_problem(NULL, "No current context in intelScreenContext\n");
713 return NULL;
714 }
715 return intel_context(ctx);
716 }
717
718 /**
719 * This is the driver specific part of the createNewScreen entry point.
720 * Called when using DRI2.
721 *
722 * \return the __GLcontextModes supported by this driver
723 */
724 static const
725 __DRIconfig **intelInitScreen2(__DRIscreenPrivate *psp)
726 {
727 intelScreenPrivate *intelScreen;
728 GLenum fb_format[3];
729 GLenum fb_type[3];
730 /* GLX_SWAP_COPY_OML is only supported because the Intel driver doesn't
731 * support pageflipping at all.
732 */
733 static const GLenum back_buffer_modes[] = {
734 GLX_NONE, GLX_SWAP_UNDEFINED_OML, GLX_SWAP_COPY_OML
735 };
736 uint8_t depth_bits[4], stencil_bits[4], msaa_samples_array[1];
737 int color;
738 __DRIconfig **configs = NULL;
739
740 /* Calling driInitExtensions here, with a NULL context pointer,
741 * does not actually enable the extensions. It just makes sure
742 * that all the dispatch offsets for all the extensions that
743 * *might* be enables are known. This is needed because the
744 * dispatch offsets need to be known when _mesa_context_create is
745 * called, but we can't enable the extensions until we have a
746 * context pointer.
747 *
748 * Hello chicken. Hello egg. How are you two today?
749 */
750 intelInitExtensions(NULL, GL_TRUE);
751
752 /* Allocate the private area */
753 intelScreen = (intelScreenPrivate *) CALLOC(sizeof(intelScreenPrivate));
754 if (!intelScreen) {
755 fprintf(stderr, "\nERROR! Allocating private area failed\n");
756 return GL_FALSE;
757 }
758 /* parse information in __driConfigOptions */
759 driParseOptionInfo(&intelScreen->optionCache,
760 __driConfigOptions, __driNConfigOptions);
761
762 intelScreen->driScrnPriv = psp;
763 psp->private = (void *) intelScreen;
764
765 intelScreen->drmMinor = psp->drm_version.minor;
766
767 /* Determine chipset ID */
768 if (!intel_get_param(psp, I915_PARAM_CHIPSET_ID,
769 &intelScreen->deviceID))
770 return GL_FALSE;
771
772 if (!intel_init_bufmgr(intelScreen))
773 return GL_FALSE;
774
775 intelScreen->irq_active = 1;
776 psp->extensions = intelScreenExtensions;
777
778 depth_bits[0] = 0;
779 stencil_bits[0] = 0;
780 depth_bits[1] = 16;
781 stencil_bits[1] = 0;
782 depth_bits[2] = 24;
783 stencil_bits[2] = 0;
784 depth_bits[3] = 24;
785 stencil_bits[3] = 8;
786
787 msaa_samples_array[0] = 0;
788
789 fb_format[0] = GL_RGB;
790 fb_type[0] = GL_UNSIGNED_SHORT_5_6_5;
791
792 fb_format[1] = GL_BGR;
793 fb_type[1] = GL_UNSIGNED_INT_8_8_8_8_REV;
794
795 fb_format[2] = GL_BGRA;
796 fb_type[2] = GL_UNSIGNED_INT_8_8_8_8_REV;
797
798 depth_bits[0] = 0;
799 stencil_bits[0] = 0;
800
801 for (color = 0; color < ARRAY_SIZE(fb_format); color++) {
802 __DRIconfig **new_configs;
803 int depth_factor;
804
805 /* With DRI2 right now, GetBuffers always returns a depth/stencil buffer
806 * with the same cpp as the drawable. So we can't support depth cpp !=
807 * color cpp currently.
808 */
809 if (fb_type[color] == GL_UNSIGNED_SHORT_5_6_5) {
810 depth_bits[1] = 16;
811 stencil_bits[1] = 0;
812
813 depth_factor = 2;
814 } else {
815 depth_bits[1] = 24;
816 stencil_bits[1] = 0;
817 depth_bits[2] = 24;
818 stencil_bits[2] = 8;
819
820 depth_factor = 3;
821 }
822 new_configs = driCreateConfigs(fb_format[color], fb_type[color],
823 depth_bits,
824 stencil_bits,
825 depth_factor,
826 back_buffer_modes,
827 ARRAY_SIZE(back_buffer_modes),
828 msaa_samples_array,
829 ARRAY_SIZE(msaa_samples_array));
830 if (configs == NULL)
831 configs = new_configs;
832 else
833 configs = driConcatConfigs(configs, new_configs);
834 }
835
836 if (configs == NULL) {
837 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
838 __LINE__);
839 return NULL;
840 }
841
842 return (const __DRIconfig **)configs;
843 }
844
845 const struct __DriverAPIRec driDriverAPI = {
846 .InitScreen = intelInitScreen,
847 .DestroyScreen = intelDestroyScreen,
848 .CreateContext = intelCreateContext,
849 .DestroyContext = intelDestroyContext,
850 .CreateBuffer = intelCreateBuffer,
851 .DestroyBuffer = intelDestroyBuffer,
852 .SwapBuffers = intelSwapBuffers,
853 .MakeCurrent = intelMakeCurrent,
854 .UnbindContext = intelUnbindContext,
855 .GetSwapInfo = intelGetSwapInfo,
856 .GetDrawableMSC = driDrawableGetMSC32,
857 .WaitForMSC = driWaitForMSC32,
858 .CopySubBuffer = intelCopySubBuffer,
859
860 .InitScreen2 = intelInitScreen2,
861 };