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