mesa/drivers: use new swrast renderbuffer functions
[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 <errno.h>
29 #include "main/glheader.h"
30 #include "main/context.h"
31 #include "main/framebuffer.h"
32 #include "main/renderbuffer.h"
33 #include "main/hash.h"
34 #include "main/fbobject.h"
35 #include "main/mfeatures.h"
36 #include "swrast/s_renderbuffer.h"
37
38 #include "utils.h"
39 #include "xmlpool.h"
40
41 PUBLIC const char __driConfigOptions[] =
42 DRI_CONF_BEGIN
43 DRI_CONF_SECTION_PERFORMANCE
44 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_ALWAYS_SYNC)
45 /* Options correspond to DRI_CONF_BO_REUSE_DISABLED,
46 * DRI_CONF_BO_REUSE_ALL
47 */
48 DRI_CONF_OPT_BEGIN_V(bo_reuse, enum, 1, "0:1")
49 DRI_CONF_DESC_BEGIN(en, "Buffer object reuse")
50 DRI_CONF_ENUM(0, "Disable buffer object reuse")
51 DRI_CONF_ENUM(1, "Enable reuse of all sizes of buffer objects")
52 DRI_CONF_DESC_END
53 DRI_CONF_OPT_END
54
55 DRI_CONF_OPT_BEGIN(texture_tiling, bool, true)
56 DRI_CONF_DESC(en, "Enable texture tiling")
57 DRI_CONF_OPT_END
58
59 DRI_CONF_OPT_BEGIN(early_z, bool, false)
60 DRI_CONF_DESC(en, "Enable early Z in classic mode (unstable, 945-only).")
61 DRI_CONF_OPT_END
62
63 DRI_CONF_OPT_BEGIN(fragment_shader, bool, true)
64 DRI_CONF_DESC(en, "Enable limited ARB_fragment_shader support on 915/945.")
65 DRI_CONF_OPT_END
66
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
77 DRI_CONF_OPT_BEGIN(stub_occlusion_query, bool, false)
78 DRI_CONF_DESC(en, "Enable stub ARB_occlusion_query support on 915/945.")
79 DRI_CONF_OPT_END
80
81 DRI_CONF_OPT_BEGIN(shader_precompile, bool, false)
82 DRI_CONF_DESC(en, "Perform code generation at shader link time.")
83 DRI_CONF_OPT_END
84 DRI_CONF_SECTION_END
85 DRI_CONF_END;
86
87 const GLuint __driNConfigOptions = 12;
88
89 #include "intel_batchbuffer.h"
90 #include "intel_buffers.h"
91 #include "intel_bufmgr.h"
92 #include "intel_chipset.h"
93 #include "intel_fbo.h"
94 #include "intel_mipmap_tree.h"
95 #include "intel_screen.h"
96 #include "intel_tex.h"
97 #include "intel_regions.h"
98
99 #include "i915_drm.h"
100
101 #ifdef USE_NEW_INTERFACE
102 static PFNGLXCREATECONTEXTMODES create_context_modes = NULL;
103 #endif /*USE_NEW_INTERFACE */
104
105 static const __DRItexBufferExtension intelTexBufferExtension = {
106 { __DRI_TEX_BUFFER, __DRI_TEX_BUFFER_VERSION },
107 intelSetTexBuffer,
108 intelSetTexBuffer2,
109 };
110
111 static void
112 intelDRI2Flush(__DRIdrawable *drawable)
113 {
114 GET_CURRENT_CONTEXT(ctx);
115 struct intel_context *intel = intel_context(ctx);
116
117 if (intel->gen < 4)
118 INTEL_FIREVERTICES(intel);
119
120 intel->need_throttle = true;
121
122 if (intel->batch.used)
123 intel_batchbuffer_flush(intel);
124 }
125
126 static const struct __DRI2flushExtensionRec intelFlushExtension = {
127 { __DRI2_FLUSH, __DRI2_FLUSH_VERSION },
128 intelDRI2Flush,
129 dri2InvalidateDrawable,
130 };
131
132 static __DRIimage *
133 intel_create_image_from_name(__DRIscreen *screen,
134 int width, int height, int format,
135 int name, int pitch, void *loaderPrivate)
136 {
137 struct intel_screen *intelScreen = screen->driverPrivate;
138 __DRIimage *image;
139 int cpp;
140
141 image = CALLOC(sizeof *image);
142 if (image == NULL)
143 return NULL;
144
145 switch (format) {
146 case __DRI_IMAGE_FORMAT_RGB565:
147 image->format = MESA_FORMAT_RGB565;
148 image->internal_format = GL_RGB;
149 image->data_type = GL_UNSIGNED_BYTE;
150 break;
151 case __DRI_IMAGE_FORMAT_XRGB8888:
152 image->format = MESA_FORMAT_XRGB8888;
153 image->internal_format = GL_RGB;
154 image->data_type = GL_UNSIGNED_BYTE;
155 break;
156 case __DRI_IMAGE_FORMAT_ARGB8888:
157 image->format = MESA_FORMAT_ARGB8888;
158 image->internal_format = GL_RGBA;
159 image->data_type = GL_UNSIGNED_BYTE;
160 break;
161 case __DRI_IMAGE_FORMAT_ABGR8888:
162 image->format = MESA_FORMAT_RGBA8888_REV;
163 image->internal_format = GL_RGBA;
164 image->data_type = GL_UNSIGNED_BYTE;
165 break;
166 default:
167 free(image);
168 return NULL;
169 }
170
171 image->data = loaderPrivate;
172 cpp = _mesa_get_format_bytes(image->format);
173
174 image->region = intel_region_alloc_for_handle(intelScreen,
175 cpp, width, height,
176 pitch, name, "image");
177 if (image->region == NULL) {
178 FREE(image);
179 return NULL;
180 }
181
182 return image;
183 }
184
185 static __DRIimage *
186 intel_create_image_from_renderbuffer(__DRIcontext *context,
187 int renderbuffer, void *loaderPrivate)
188 {
189 __DRIimage *image;
190 struct intel_context *intel = context->driverPrivate;
191 struct gl_renderbuffer *rb;
192 struct intel_renderbuffer *irb;
193
194 rb = _mesa_lookup_renderbuffer(&intel->ctx, renderbuffer);
195 if (!rb) {
196 _mesa_error(&intel->ctx,
197 GL_INVALID_OPERATION, "glRenderbufferExternalMESA");
198 return NULL;
199 }
200
201 irb = intel_renderbuffer(rb);
202 image = CALLOC(sizeof *image);
203 if (image == NULL)
204 return NULL;
205
206 image->internal_format = rb->InternalFormat;
207 image->format = rb->Format;
208 image->data_type = rb->DataType;
209 image->data = loaderPrivate;
210 intel_region_reference(&image->region, irb->mt->region);
211
212 return image;
213 }
214
215 static void
216 intel_destroy_image(__DRIimage *image)
217 {
218 intel_region_release(&image->region);
219 FREE(image);
220 }
221
222 static __DRIimage *
223 intel_create_image(__DRIscreen *screen,
224 int width, int height, int format,
225 unsigned int use,
226 void *loaderPrivate)
227 {
228 __DRIimage *image;
229 struct intel_screen *intelScreen = screen->driverPrivate;
230 uint32_t tiling;
231 int cpp;
232
233 tiling = I915_TILING_X;
234 if (use & __DRI_IMAGE_USE_CURSOR) {
235 if (width != 64 || height != 64)
236 return NULL;
237 tiling = I915_TILING_NONE;
238 }
239
240 image = CALLOC(sizeof *image);
241 if (image == NULL)
242 return NULL;
243
244 switch (format) {
245 case __DRI_IMAGE_FORMAT_RGB565:
246 image->format = MESA_FORMAT_RGB565;
247 image->internal_format = GL_RGB;
248 image->data_type = GL_UNSIGNED_BYTE;
249 break;
250 case __DRI_IMAGE_FORMAT_XRGB8888:
251 image->format = MESA_FORMAT_XRGB8888;
252 image->internal_format = GL_RGB;
253 image->data_type = GL_UNSIGNED_BYTE;
254 break;
255 case __DRI_IMAGE_FORMAT_ARGB8888:
256 image->format = MESA_FORMAT_ARGB8888;
257 image->internal_format = GL_RGBA;
258 image->data_type = GL_UNSIGNED_BYTE;
259 break;
260 case __DRI_IMAGE_FORMAT_ABGR8888:
261 image->format = MESA_FORMAT_RGBA8888_REV;
262 image->internal_format = GL_RGBA;
263 image->data_type = GL_UNSIGNED_BYTE;
264 break;
265 default:
266 free(image);
267 return NULL;
268 }
269
270 image->data = loaderPrivate;
271 cpp = _mesa_get_format_bytes(image->format);
272
273 image->region =
274 intel_region_alloc(intelScreen, tiling,
275 cpp, width, height, true);
276 if (image->region == NULL) {
277 FREE(image);
278 return NULL;
279 }
280
281 return image;
282 }
283
284 static GLboolean
285 intel_query_image(__DRIimage *image, int attrib, int *value)
286 {
287 switch (attrib) {
288 case __DRI_IMAGE_ATTRIB_STRIDE:
289 *value = image->region->pitch * image->region->cpp;
290 return true;
291 case __DRI_IMAGE_ATTRIB_HANDLE:
292 *value = image->region->bo->handle;
293 return true;
294 case __DRI_IMAGE_ATTRIB_NAME:
295 return intel_region_flink(image->region, (uint32_t *) value);
296 default:
297 return false;
298 }
299 }
300
301 static __DRIimage *
302 intel_dup_image(__DRIimage *orig_image, void *loaderPrivate)
303 {
304 __DRIimage *image;
305
306 image = CALLOC(sizeof *image);
307 if (image == NULL)
308 return NULL;
309
310 intel_region_reference(&image->region, orig_image->region);
311 if (image->region == NULL) {
312 FREE(image);
313 return NULL;
314 }
315
316 image->internal_format = orig_image->internal_format;
317 image->format = orig_image->format;
318 image->data_type = orig_image->data_type;
319 image->data = loaderPrivate;
320
321 return image;
322 }
323
324 static struct __DRIimageExtensionRec intelImageExtension = {
325 { __DRI_IMAGE, __DRI_IMAGE_VERSION },
326 intel_create_image_from_name,
327 intel_create_image_from_renderbuffer,
328 intel_destroy_image,
329 intel_create_image,
330 intel_query_image,
331 intel_dup_image
332 };
333
334 static const __DRIextension *intelScreenExtensions[] = {
335 &intelTexBufferExtension.base,
336 &intelFlushExtension.base,
337 &intelImageExtension.base,
338 &dri2ConfigQueryExtension.base,
339 NULL
340 };
341
342 static bool
343 intel_get_param(__DRIscreen *psp, int param, int *value)
344 {
345 int ret;
346 struct drm_i915_getparam gp;
347
348 gp.param = param;
349 gp.value = value;
350
351 ret = drmCommandWriteRead(psp->fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
352 if (ret) {
353 if (ret != -EINVAL)
354 _mesa_warning(NULL, "drm_i915_getparam: %d", ret);
355 return false;
356 }
357
358 return true;
359 }
360
361 static bool
362 intel_get_boolean(__DRIscreen *psp, int param)
363 {
364 int value = 0;
365 return intel_get_param(psp, param, &value) && value;
366 }
367
368 static void
369 nop_callback(GLuint key, void *data, void *userData)
370 {
371 }
372
373 static void
374 intelDestroyScreen(__DRIscreen * sPriv)
375 {
376 struct intel_screen *intelScreen = sPriv->driverPrivate;
377
378 dri_bufmgr_destroy(intelScreen->bufmgr);
379 driDestroyOptionInfo(&intelScreen->optionCache);
380
381 /* Some regions may still have references to them at this point, so
382 * flush the hash table to prevent _mesa_DeleteHashTable() from
383 * complaining about the hash not being empty; */
384 _mesa_HashDeleteAll(intelScreen->named_regions, nop_callback, NULL);
385 _mesa_DeleteHashTable(intelScreen->named_regions);
386
387 FREE(intelScreen);
388 sPriv->driverPrivate = NULL;
389 }
390
391
392 /**
393 * This is called when we need to set up GL rendering to a new X window.
394 */
395 static GLboolean
396 intelCreateBuffer(__DRIscreen * driScrnPriv,
397 __DRIdrawable * driDrawPriv,
398 const struct gl_config * mesaVis, GLboolean isPixmap)
399 {
400 struct intel_renderbuffer *rb;
401 struct intel_screen *screen = (struct intel_screen*) driScrnPriv->driverPrivate;
402
403 if (isPixmap) {
404 return false; /* not implemented */
405 }
406 else {
407 gl_format rgbFormat;
408
409 struct gl_framebuffer *fb = CALLOC_STRUCT(gl_framebuffer);
410
411 if (!fb)
412 return false;
413
414 _mesa_initialize_window_framebuffer(fb, mesaVis);
415
416 if (mesaVis->redBits == 5)
417 rgbFormat = MESA_FORMAT_RGB565;
418 else if (mesaVis->alphaBits == 0)
419 rgbFormat = MESA_FORMAT_XRGB8888;
420 else
421 rgbFormat = MESA_FORMAT_ARGB8888;
422
423 /* setup the hardware-based renderbuffers */
424 rb = intel_create_renderbuffer(rgbFormat);
425 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &rb->Base);
426
427 if (mesaVis->doubleBufferMode) {
428 rb = intel_create_renderbuffer(rgbFormat);
429 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &rb->Base);
430 }
431
432 /*
433 * Assert here that the gl_config has an expected depth/stencil bit
434 * combination: one of d24/s8, d16/s0, d0/s0. (See intelInitScreen2(),
435 * which constructs the advertised configs.)
436 */
437 if (mesaVis->depthBits == 24) {
438 assert(mesaVis->stencilBits == 8);
439
440 if (screen->hw_has_separate_stencil
441 && screen->dri2_has_hiz != INTEL_DRI2_HAS_HIZ_FALSE) {
442 /*
443 * Request a separate stencil buffer even if we do not yet know if
444 * the screen supports it. (See comments for
445 * enum intel_dri2_has_hiz).
446 */
447 rb = intel_create_renderbuffer(MESA_FORMAT_X8_Z24);
448 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &rb->Base);
449 rb = intel_create_renderbuffer(MESA_FORMAT_S8);
450 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &rb->Base);
451 } else {
452 /*
453 * Use combined depth/stencil. Note that the renderbuffer is
454 * attached to two attachment points.
455 */
456 rb = intel_create_renderbuffer(MESA_FORMAT_S8_Z24);
457 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &rb->Base);
458 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &rb->Base);
459 }
460 }
461 else if (mesaVis->depthBits == 16) {
462 assert(mesaVis->stencilBits == 0);
463 /* just 16-bit depth buffer, no hw stencil */
464 struct intel_renderbuffer *depthRb
465 = intel_create_renderbuffer(MESA_FORMAT_Z16);
466 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
467 }
468 else {
469 assert(mesaVis->depthBits == 0);
470 assert(mesaVis->stencilBits == 0);
471 }
472
473 /* now add any/all software-based renderbuffers we may need */
474 _swrast_add_soft_renderbuffers(fb,
475 false, /* never sw color */
476 false, /* never sw depth */
477 false, /* never sw stencil */
478 mesaVis->accumRedBits > 0,
479 false, /* never sw alpha */
480 false /* never sw aux */ );
481 driDrawPriv->driverPrivate = fb;
482
483 return true;
484 }
485 }
486
487 static void
488 intelDestroyBuffer(__DRIdrawable * driDrawPriv)
489 {
490 struct gl_framebuffer *fb = driDrawPriv->driverPrivate;
491
492 _mesa_reference_framebuffer(&fb, NULL);
493 }
494
495 /* There are probably better ways to do this, such as an
496 * init-designated function to register chipids and createcontext
497 * functions.
498 */
499 extern bool
500 i830CreateContext(const struct gl_config *mesaVis,
501 __DRIcontext *driContextPriv,
502 void *sharedContextPrivate);
503
504 extern bool
505 i915CreateContext(int api,
506 const struct gl_config *mesaVis,
507 __DRIcontext *driContextPriv,
508 void *sharedContextPrivate);
509 extern bool
510 brwCreateContext(int api,
511 const struct gl_config *mesaVis,
512 __DRIcontext *driContextPriv,
513 void *sharedContextPrivate);
514
515 static GLboolean
516 intelCreateContext(gl_api api,
517 const struct gl_config * mesaVis,
518 __DRIcontext * driContextPriv,
519 void *sharedContextPrivate)
520 {
521 __DRIscreen *sPriv = driContextPriv->driScreenPriv;
522 struct intel_screen *intelScreen = sPriv->driverPrivate;
523
524 #ifdef I915
525 if (IS_9XX(intelScreen->deviceID)) {
526 if (!IS_965(intelScreen->deviceID)) {
527 return i915CreateContext(api, mesaVis, driContextPriv,
528 sharedContextPrivate);
529 }
530 } else {
531 intelScreen->no_vbo = true;
532 return i830CreateContext(mesaVis, driContextPriv, sharedContextPrivate);
533 }
534 #else
535 if (IS_965(intelScreen->deviceID))
536 return brwCreateContext(api, mesaVis,
537 driContextPriv, sharedContextPrivate);
538 #endif
539 fprintf(stderr, "Unrecognized deviceID 0x%x\n", intelScreen->deviceID);
540 return false;
541 }
542
543 static bool
544 intel_init_bufmgr(struct intel_screen *intelScreen)
545 {
546 __DRIscreen *spriv = intelScreen->driScrnPriv;
547 int num_fences = 0;
548
549 intelScreen->no_hw = (getenv("INTEL_NO_HW") != NULL ||
550 getenv("INTEL_DEVID_OVERRIDE") != NULL);
551
552 intelScreen->bufmgr = intel_bufmgr_gem_init(spriv->fd, BATCH_SZ);
553 if (intelScreen->bufmgr == NULL) {
554 fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
555 __func__, __LINE__);
556 return false;
557 }
558
559 if (!intel_get_param(spriv, I915_PARAM_NUM_FENCES_AVAIL, &num_fences) ||
560 num_fences == 0) {
561 fprintf(stderr, "[%s: %u] Kernel 2.6.29 required.\n", __func__, __LINE__);
562 return false;
563 }
564
565 drm_intel_bufmgr_gem_enable_fenced_relocs(intelScreen->bufmgr);
566
567 intelScreen->named_regions = _mesa_NewHashTable();
568
569 intelScreen->relaxed_relocations = 0;
570 intelScreen->relaxed_relocations |=
571 intel_get_boolean(spriv, I915_PARAM_HAS_RELAXED_DELTA) << 0;
572
573 return true;
574 }
575
576 /**
577 * Override intel_screen.hw_has_hiz with environment variable INTEL_HIZ.
578 *
579 * Valid values for INTEL_HIZ are "0" and "1". If an invalid valid value is
580 * encountered, a warning is emitted and INTEL_HIZ is ignored.
581 */
582 static void
583 intel_override_hiz(struct intel_screen *intel)
584 {
585 const char *s = getenv("INTEL_HIZ");
586 if (!s) {
587 return;
588 } else if (!strncmp("0", s, 2)) {
589 intel->hw_has_hiz = false;
590 } else if (!strncmp("1", s, 2)) {
591 intel->hw_has_hiz = true;
592 } else {
593 fprintf(stderr,
594 "warning: env variable INTEL_HIZ=\"%s\" has invalid value "
595 "and is ignored", s);
596 }
597 }
598
599 /**
600 * Override intel_screen.hw_has_separate_stencil with environment variable
601 * INTEL_SEPARATE_STENCIL.
602 *
603 * Valid values for INTEL_SEPARATE_STENCIL are "0" and "1". If an invalid
604 * valid value is encountered, a warning is emitted and INTEL_SEPARATE_STENCIL
605 * is ignored.
606 */
607 static void
608 intel_override_separate_stencil(struct intel_screen *screen)
609 {
610 const char *s = getenv("INTEL_SEPARATE_STENCIL");
611 if (!s) {
612 return;
613 } else if (!strncmp("0", s, 2)) {
614 screen->hw_has_separate_stencil = false;
615 } else if (!strncmp("1", s, 2)) {
616 screen->hw_has_separate_stencil = true;
617 } else {
618 fprintf(stderr,
619 "warning: env variable INTEL_SEPARATE_STENCIL=\"%s\" has "
620 "invalid value and is ignored", s);
621 }
622 }
623
624 /**
625 * This is the driver specific part of the createNewScreen entry point.
626 * Called when using DRI2.
627 *
628 * \return the struct gl_config supported by this driver
629 */
630 static const
631 __DRIconfig **intelInitScreen2(__DRIscreen *psp)
632 {
633 struct intel_screen *intelScreen;
634 GLenum fb_format[3];
635 GLenum fb_type[3];
636 unsigned int api_mask;
637 char *devid_override;
638
639 static const GLenum back_buffer_modes[] = {
640 GLX_NONE, GLX_SWAP_UNDEFINED_OML, GLX_SWAP_COPY_OML
641 };
642 uint8_t depth_bits[4], stencil_bits[4], msaa_samples_array[1];
643 int color;
644 __DRIconfig **configs = NULL;
645
646 /* Allocate the private area */
647 intelScreen = CALLOC(sizeof *intelScreen);
648 if (!intelScreen) {
649 fprintf(stderr, "\nERROR! Allocating private area failed\n");
650 return false;
651 }
652 /* parse information in __driConfigOptions */
653 driParseOptionInfo(&intelScreen->optionCache,
654 __driConfigOptions, __driNConfigOptions);
655
656 intelScreen->driScrnPriv = psp;
657 psp->driverPrivate = (void *) intelScreen;
658
659 /* Determine chipset ID */
660 if (!intel_get_param(psp, I915_PARAM_CHIPSET_ID,
661 &intelScreen->deviceID))
662 return false;
663
664 /* Allow an override of the device ID for the purpose of making the
665 * driver produce dumps for debugging of new chipset enablement.
666 * This implies INTEL_NO_HW, to avoid programming your actual GPU
667 * incorrectly.
668 */
669 devid_override = getenv("INTEL_DEVID_OVERRIDE");
670 if (devid_override) {
671 intelScreen->deviceID = strtod(devid_override, NULL);
672 }
673
674 if (IS_GEN7(intelScreen->deviceID)) {
675 intelScreen->gen = 7;
676 } else if (IS_GEN6(intelScreen->deviceID)) {
677 intelScreen->gen = 6;
678 } else if (IS_GEN5(intelScreen->deviceID)) {
679 intelScreen->gen = 5;
680 } else if (IS_965(intelScreen->deviceID)) {
681 intelScreen->gen = 4;
682 } else if (IS_9XX(intelScreen->deviceID)) {
683 intelScreen->gen = 3;
684 } else {
685 intelScreen->gen = 2;
686 }
687
688 intelScreen->hw_has_separate_stencil = intelScreen->gen >= 6;
689 intelScreen->hw_must_use_separate_stencil = intelScreen->gen >= 7;
690 intelScreen->hw_has_hiz = intelScreen->gen == 6; /* Not yet for gen7. */
691 intelScreen->dri2_has_hiz = INTEL_DRI2_HAS_HIZ_UNKNOWN;
692
693 intel_override_hiz(intelScreen);
694 intel_override_separate_stencil(intelScreen);
695
696 api_mask = (1 << __DRI_API_OPENGL);
697 #if FEATURE_ES1
698 api_mask |= (1 << __DRI_API_GLES);
699 #endif
700 #if FEATURE_ES2
701 api_mask |= (1 << __DRI_API_GLES2);
702 #endif
703
704 if (IS_9XX(intelScreen->deviceID) || IS_965(intelScreen->deviceID))
705 psp->api_mask = api_mask;
706
707 if (!intel_init_bufmgr(intelScreen))
708 return false;
709
710 psp->extensions = intelScreenExtensions;
711
712 msaa_samples_array[0] = 0;
713
714 fb_format[0] = GL_RGB;
715 fb_type[0] = GL_UNSIGNED_SHORT_5_6_5;
716
717 fb_format[1] = GL_BGR;
718 fb_type[1] = GL_UNSIGNED_INT_8_8_8_8_REV;
719
720 fb_format[2] = GL_BGRA;
721 fb_type[2] = GL_UNSIGNED_INT_8_8_8_8_REV;
722
723 depth_bits[0] = 0;
724 stencil_bits[0] = 0;
725
726 /* Generate a rich set of useful configs that do not include an
727 * accumulation buffer.
728 */
729 for (color = 0; color < ARRAY_SIZE(fb_format); color++) {
730 __DRIconfig **new_configs;
731 int depth_factor;
732
733 /* Starting with DRI2 protocol version 1.1 we can request a depth/stencil
734 * buffer that has a diffferent number of bits per pixel than the color
735 * buffer. This isn't yet supported here.
736 */
737 if (fb_type[color] == GL_UNSIGNED_SHORT_5_6_5) {
738 depth_bits[1] = 16;
739 stencil_bits[1] = 0;
740 } else {
741 depth_bits[1] = 24;
742 stencil_bits[1] = 8;
743 }
744
745 depth_factor = 2;
746
747 new_configs = driCreateConfigs(fb_format[color], fb_type[color],
748 depth_bits,
749 stencil_bits,
750 depth_factor,
751 back_buffer_modes,
752 ARRAY_SIZE(back_buffer_modes),
753 msaa_samples_array,
754 ARRAY_SIZE(msaa_samples_array),
755 false);
756 if (configs == NULL)
757 configs = new_configs;
758 else
759 configs = driConcatConfigs(configs, new_configs);
760 }
761
762 /* Generate the minimum possible set of configs that include an
763 * accumulation buffer.
764 */
765 for (color = 0; color < ARRAY_SIZE(fb_format); color++) {
766 __DRIconfig **new_configs;
767
768 if (fb_type[color] == GL_UNSIGNED_SHORT_5_6_5) {
769 depth_bits[0] = 16;
770 stencil_bits[0] = 0;
771 } else {
772 depth_bits[0] = 24;
773 stencil_bits[0] = 8;
774 }
775
776 new_configs = driCreateConfigs(fb_format[color], fb_type[color],
777 depth_bits, stencil_bits, 1,
778 back_buffer_modes + 1, 1,
779 msaa_samples_array, 1,
780 true);
781 if (configs == NULL)
782 configs = new_configs;
783 else
784 configs = driConcatConfigs(configs, new_configs);
785 }
786
787 if (configs == NULL) {
788 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
789 __LINE__);
790 return NULL;
791 }
792
793 return (const __DRIconfig **)configs;
794 }
795
796 struct intel_buffer {
797 __DRIbuffer base;
798 struct intel_region *region;
799 };
800
801 /**
802 * \brief Get tiling format for a DRI buffer.
803 *
804 * \param attachment is the buffer's attachmet point, such as
805 * __DRI_BUFFER_DEPTH.
806 * \param out_tiling is the returned tiling format for buffer.
807 * \return false if attachment is unrecognized or is incompatible with screen.
808 */
809 static bool
810 intel_get_dri_buffer_tiling(struct intel_screen *screen,
811 uint32_t attachment,
812 uint32_t *out_tiling)
813 {
814 if (screen->gen < 4) {
815 *out_tiling = I915_TILING_X;
816 return true;
817 }
818
819 switch (attachment) {
820 case __DRI_BUFFER_DEPTH:
821 case __DRI_BUFFER_DEPTH_STENCIL:
822 case __DRI_BUFFER_HIZ:
823 *out_tiling = I915_TILING_Y;
824 return true;
825 case __DRI_BUFFER_ACCUM:
826 case __DRI_BUFFER_FRONT_LEFT:
827 case __DRI_BUFFER_FRONT_RIGHT:
828 case __DRI_BUFFER_BACK_LEFT:
829 case __DRI_BUFFER_BACK_RIGHT:
830 case __DRI_BUFFER_FAKE_FRONT_LEFT:
831 case __DRI_BUFFER_FAKE_FRONT_RIGHT:
832 *out_tiling = I915_TILING_X;
833 return true;
834 case __DRI_BUFFER_STENCIL:
835 /* The stencil buffer is W tiled. However, we request from the kernel
836 * a non-tiled buffer because the GTT is incapable of W fencing.
837 */
838 *out_tiling = I915_TILING_NONE;
839 return true;
840 default:
841 if(unlikely(INTEL_DEBUG & DEBUG_DRI)) {
842 fprintf(stderr, "error: %s: unrecognized DRI buffer attachment 0x%x\n",
843 __FUNCTION__, attachment);
844 }
845 return false;
846 }
847 }
848
849 static __DRIbuffer *
850 intelAllocateBuffer(__DRIscreen *screen,
851 unsigned attachment, unsigned format,
852 int width, int height)
853 {
854 struct intel_buffer *intelBuffer;
855 struct intel_screen *intelScreen = screen->driverPrivate;
856
857 uint32_t tiling;
858 uint32_t region_width;
859 uint32_t region_height;
860 uint32_t region_cpp;
861
862 bool ok = true;
863
864 ok = intel_get_dri_buffer_tiling(intelScreen, attachment, &tiling);
865 if (!ok)
866 return NULL;
867
868 intelBuffer = CALLOC(sizeof *intelBuffer);
869 if (intelBuffer == NULL)
870 return NULL;
871
872 if (attachment == __DRI_BUFFER_STENCIL) {
873 /* The stencil buffer has quirky pitch requirements. From Vol 2a,
874 * 11.5.6.2.1 3DSTATE_STENCIL_BUFFER, field "Surface Pitch":
875 * The pitch must be set to 2x the value computed based on width, as
876 * the stencil buffer is stored with two rows interleaved.
877 * To accomplish this, we resort to the nasty hack of doubling the
878 * region's cpp and halving its height.
879 */
880 region_width = ALIGN(width, 64);
881 region_height = ALIGN(ALIGN(height, 2) / 2, 64);
882 region_cpp = format / 4;
883 } else {
884 region_width = width;
885 region_height = height;
886 region_cpp = format / 8;
887 }
888
889 intelBuffer->region = intel_region_alloc(intelScreen,
890 tiling,
891 region_cpp,
892 region_width,
893 region_height,
894 true);
895
896 if (intelBuffer->region == NULL) {
897 FREE(intelBuffer);
898 return NULL;
899 }
900
901 intel_region_flink(intelBuffer->region, &intelBuffer->base.name);
902
903 intelBuffer->base.attachment = attachment;
904 intelBuffer->base.cpp = intelBuffer->region->cpp;
905 intelBuffer->base.pitch =
906 intelBuffer->region->pitch * intelBuffer->region->cpp;
907
908 return &intelBuffer->base;
909 }
910
911 static void
912 intelReleaseBuffer(__DRIscreen *screen, __DRIbuffer *buffer)
913 {
914 struct intel_buffer *intelBuffer = (struct intel_buffer *) buffer;
915
916 intel_region_release(&intelBuffer->region);
917 free(intelBuffer);
918 }
919
920
921 const struct __DriverAPIRec driDriverAPI = {
922 .InitScreen = intelInitScreen2,
923 .DestroyScreen = intelDestroyScreen,
924 .CreateContext = intelCreateContext,
925 .DestroyContext = intelDestroyContext,
926 .CreateBuffer = intelCreateBuffer,
927 .DestroyBuffer = intelDestroyBuffer,
928 .MakeCurrent = intelMakeCurrent,
929 .UnbindContext = intelUnbindContext,
930 .AllocateBuffer = intelAllocateBuffer,
931 .ReleaseBuffer = intelReleaseBuffer
932 };
933
934 /* This is the table of extensions that the loader will dlsym() for. */
935 PUBLIC const __DRIextension *__driDriverExtensions[] = {
936 &driCoreExtension.base,
937 &driDRI2Extension.base,
938 NULL
939 };