Merge branch 'draw-instanced'
[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 #include "main/hash.h"
33 #include "main/fbobject.h"
34 #include "main/mfeatures.h"
35
36 #include "utils.h"
37 #include "xmlpool.h"
38
39 PUBLIC const char __driConfigOptions[] =
40 DRI_CONF_BEGIN
41 DRI_CONF_SECTION_PERFORMANCE
42 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_ALWAYS_SYNC)
43 /* Options correspond to DRI_CONF_BO_REUSE_DISABLED,
44 * DRI_CONF_BO_REUSE_ALL
45 */
46 DRI_CONF_OPT_BEGIN_V(bo_reuse, enum, 1, "0:1")
47 DRI_CONF_DESC_BEGIN(en, "Buffer object reuse")
48 DRI_CONF_ENUM(0, "Disable buffer object reuse")
49 DRI_CONF_ENUM(1, "Enable reuse of all sizes of buffer objects")
50 DRI_CONF_DESC_END
51 DRI_CONF_OPT_END
52
53 DRI_CONF_OPT_BEGIN(texture_tiling, bool, true)
54 DRI_CONF_DESC(en, "Enable texture tiling")
55 DRI_CONF_OPT_END
56
57 DRI_CONF_OPT_BEGIN(early_z, bool, false)
58 DRI_CONF_DESC(en, "Enable early Z in classic mode (unstable, 945-only).")
59 DRI_CONF_OPT_END
60
61 DRI_CONF_OPT_BEGIN(fragment_shader, bool, true)
62 DRI_CONF_DESC(en, "Enable limited ARB_fragment_shader support on 915/945.")
63 DRI_CONF_OPT_END
64
65 DRI_CONF_SECTION_END
66 DRI_CONF_SECTION_QUALITY
67 DRI_CONF_FORCE_S3TC_ENABLE(false)
68 DRI_CONF_ALLOW_LARGE_TEXTURES(2)
69 DRI_CONF_SECTION_END
70 DRI_CONF_SECTION_DEBUG
71 DRI_CONF_NO_RAST(false)
72 DRI_CONF_ALWAYS_FLUSH_BATCH(false)
73 DRI_CONF_ALWAYS_FLUSH_CACHE(false)
74
75 DRI_CONF_OPT_BEGIN(stub_occlusion_query, bool, false)
76 DRI_CONF_DESC(en, "Enable stub ARB_occlusion_query support on 915/945.")
77 DRI_CONF_OPT_END
78 DRI_CONF_SECTION_END
79 DRI_CONF_END;
80
81 const GLuint __driNConfigOptions = 11;
82
83 #include "intel_batchbuffer.h"
84 #include "intel_buffers.h"
85 #include "intel_bufmgr.h"
86 #include "intel_chipset.h"
87 #include "intel_fbo.h"
88 #include "intel_screen.h"
89 #include "intel_tex.h"
90 #include "intel_regions.h"
91
92 #include "i915_drm.h"
93
94 #ifdef USE_NEW_INTERFACE
95 static PFNGLXCREATECONTEXTMODES create_context_modes = NULL;
96 #endif /*USE_NEW_INTERFACE */
97
98 static const __DRItexBufferExtension intelTexBufferExtension = {
99 { __DRI_TEX_BUFFER, __DRI_TEX_BUFFER_VERSION },
100 intelSetTexBuffer,
101 intelSetTexBuffer2,
102 };
103
104 static void
105 intelDRI2Flush(__DRIdrawable *drawable)
106 {
107 struct intel_context *intel = drawable->driContextPriv->driverPrivate;
108
109 if (intel->gen < 4)
110 INTEL_FIREVERTICES(intel);
111
112 intel->need_throttle = GL_TRUE;
113
114 if (intel->batch->map != intel->batch->ptr)
115 intel_batchbuffer_flush(intel->batch);
116 }
117
118 static const struct __DRI2flushExtensionRec intelFlushExtension = {
119 { __DRI2_FLUSH, __DRI2_FLUSH_VERSION },
120 intelDRI2Flush,
121 dri2InvalidateDrawable,
122 };
123
124 static __DRIimage *
125 intel_create_image_from_name(__DRIscreen *screen,
126 int width, int height, int format,
127 int name, int pitch, void *loaderPrivate)
128 {
129 struct intel_screen *intelScreen = screen->private;
130 __DRIimage *image;
131 int cpp;
132
133 image = CALLOC(sizeof *image);
134 if (image == NULL)
135 return NULL;
136
137 switch (format) {
138 case __DRI_IMAGE_FORMAT_RGB565:
139 image->format = MESA_FORMAT_RGB565;
140 image->internal_format = GL_RGB;
141 image->data_type = GL_UNSIGNED_BYTE;
142 break;
143 case __DRI_IMAGE_FORMAT_XRGB8888:
144 image->format = MESA_FORMAT_XRGB8888;
145 image->internal_format = GL_RGB;
146 image->data_type = GL_UNSIGNED_BYTE;
147 break;
148 case __DRI_IMAGE_FORMAT_ARGB8888:
149 image->format = MESA_FORMAT_ARGB8888;
150 image->internal_format = GL_RGBA;
151 image->data_type = GL_UNSIGNED_BYTE;
152 break;
153 default:
154 free(image);
155 return NULL;
156 }
157
158 image->data = loaderPrivate;
159 cpp = _mesa_get_format_bytes(image->format);
160
161 image->region = intel_region_alloc_for_handle(intelScreen,
162 cpp, width, height,
163 pitch, name, "image");
164 if (image->region == NULL) {
165 FREE(image);
166 return NULL;
167 }
168
169 return image;
170 }
171
172 static __DRIimage *
173 intel_create_image_from_renderbuffer(__DRIcontext *context,
174 int renderbuffer, void *loaderPrivate)
175 {
176 __DRIimage *image;
177 struct intel_context *intel = context->driverPrivate;
178 struct gl_renderbuffer *rb;
179 struct intel_renderbuffer *irb;
180
181 rb = _mesa_lookup_renderbuffer(&intel->ctx, renderbuffer);
182 if (!rb) {
183 _mesa_error(&intel->ctx,
184 GL_INVALID_OPERATION, "glRenderbufferExternalMESA");
185 return NULL;
186 }
187
188 irb = intel_renderbuffer(rb);
189 image = CALLOC(sizeof *image);
190 if (image == NULL)
191 return NULL;
192
193 image->internal_format = rb->InternalFormat;
194 image->format = rb->Format;
195 image->data_type = rb->DataType;
196 image->data = loaderPrivate;
197 intel_region_reference(&image->region, irb->region);
198
199 return image;
200 }
201
202 static void
203 intel_destroy_image(__DRIimage *image)
204 {
205 intel_region_release(&image->region);
206 FREE(image);
207 }
208
209 static __DRIimage *
210 intel_create_image(__DRIscreen *screen,
211 int width, int height, int format,
212 unsigned int use,
213 void *loaderPrivate)
214 {
215 __DRIimage *image;
216 struct intel_screen *intelScreen = screen->private;
217 int cpp;
218
219 image = CALLOC(sizeof *image);
220 if (image == NULL)
221 return NULL;
222
223 switch (format) {
224 case __DRI_IMAGE_FORMAT_RGB565:
225 image->format = MESA_FORMAT_RGB565;
226 image->internal_format = GL_RGB;
227 image->data_type = GL_UNSIGNED_BYTE;
228 break;
229 case __DRI_IMAGE_FORMAT_XRGB8888:
230 image->format = MESA_FORMAT_XRGB8888;
231 image->internal_format = GL_RGB;
232 image->data_type = GL_UNSIGNED_BYTE;
233 break;
234 case __DRI_IMAGE_FORMAT_ARGB8888:
235 image->format = MESA_FORMAT_ARGB8888;
236 image->internal_format = GL_RGBA;
237 image->data_type = GL_UNSIGNED_BYTE;
238 break;
239 default:
240 free(image);
241 return NULL;
242 }
243
244 image->data = loaderPrivate;
245 cpp = _mesa_get_format_bytes(image->format);
246
247 image->region =
248 intel_region_alloc(intelScreen, I915_TILING_NONE,
249 cpp, width, height, GL_TRUE);
250 if (image->region == NULL) {
251 FREE(image);
252 return NULL;
253 }
254
255 return image;
256 }
257
258 static GLboolean
259 intel_query_image(__DRIimage *image, int attrib, int *value)
260 {
261 switch (attrib) {
262 case __DRI_IMAGE_ATTRIB_STRIDE:
263 *value = image->region->pitch * image->region->cpp;
264 return GL_TRUE;
265 case __DRI_IMAGE_ATTRIB_HANDLE:
266 *value = image->region->buffer->handle;
267 return GL_TRUE;
268 case __DRI_IMAGE_ATTRIB_NAME:
269 return intel_region_flink(image->region, (uint32_t *) value);
270 default:
271 return GL_FALSE;
272 }
273 }
274
275 static struct __DRIimageExtensionRec intelImageExtension = {
276 { __DRI_IMAGE, __DRI_IMAGE_VERSION },
277 intel_create_image_from_name,
278 intel_create_image_from_renderbuffer,
279 intel_destroy_image,
280 intel_create_image,
281 intel_query_image
282 };
283
284 static const __DRIextension *intelScreenExtensions[] = {
285 &driReadDrawableExtension,
286 &intelTexBufferExtension.base,
287 &intelFlushExtension.base,
288 &intelImageExtension.base,
289 &dri2ConfigQueryExtension.base,
290 NULL
291 };
292
293 static GLboolean
294 intel_get_param(__DRIscreen *psp, int param, int *value)
295 {
296 int ret;
297 struct drm_i915_getparam gp;
298
299 gp.param = param;
300 gp.value = value;
301
302 ret = drmCommandWriteRead(psp->fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
303 if (ret) {
304 _mesa_warning(NULL, "drm_i915_getparam: %d", ret);
305 return GL_FALSE;
306 }
307
308 return GL_TRUE;
309 }
310
311 static void
312 nop_callback(GLuint key, void *data, void *userData)
313 {
314 }
315
316 static void
317 intelDestroyScreen(__DRIscreen * sPriv)
318 {
319 struct intel_screen *intelScreen = sPriv->private;
320
321 dri_bufmgr_destroy(intelScreen->bufmgr);
322 driDestroyOptionInfo(&intelScreen->optionCache);
323
324 /* Some regions may still have references to them at this point, so
325 * flush the hash table to prevent _mesa_DeleteHashTable() from
326 * complaining about the hash not being empty; */
327 _mesa_HashDeleteAll(intelScreen->named_regions, nop_callback, NULL);
328 _mesa_DeleteHashTable(intelScreen->named_regions);
329
330 FREE(intelScreen);
331 sPriv->private = NULL;
332 }
333
334
335 /**
336 * This is called when we need to set up GL rendering to a new X window.
337 */
338 static GLboolean
339 intelCreateBuffer(__DRIscreen * driScrnPriv,
340 __DRIdrawable * driDrawPriv,
341 const struct gl_config * mesaVis, GLboolean isPixmap)
342 {
343 struct intel_renderbuffer *rb;
344
345 if (isPixmap) {
346 return GL_FALSE; /* not implemented */
347 }
348 else {
349 GLboolean swStencil = (mesaVis->stencilBits > 0 &&
350 mesaVis->depthBits != 24);
351 gl_format rgbFormat;
352
353 struct gl_framebuffer *fb = CALLOC_STRUCT(gl_framebuffer);
354
355 if (!fb)
356 return GL_FALSE;
357
358 _mesa_initialize_window_framebuffer(fb, mesaVis);
359
360 if (mesaVis->redBits == 5)
361 rgbFormat = MESA_FORMAT_RGB565;
362 else if (mesaVis->alphaBits == 0)
363 rgbFormat = MESA_FORMAT_XRGB8888;
364 else
365 rgbFormat = MESA_FORMAT_ARGB8888;
366
367 /* setup the hardware-based renderbuffers */
368 rb = intel_create_renderbuffer(rgbFormat);
369 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &rb->Base);
370
371 if (mesaVis->doubleBufferMode) {
372 rb = intel_create_renderbuffer(rgbFormat);
373 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &rb->Base);
374 }
375
376 if (mesaVis->depthBits == 24) {
377 assert(mesaVis->stencilBits == 8);
378 /* combined depth/stencil buffer */
379 struct intel_renderbuffer *depthStencilRb
380 = intel_create_renderbuffer(MESA_FORMAT_S8_Z24);
381 /* note: bind RB to two attachment points */
382 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthStencilRb->Base);
383 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &depthStencilRb->Base);
384 }
385 else if (mesaVis->depthBits == 16) {
386 /* just 16-bit depth buffer, no hw stencil */
387 struct intel_renderbuffer *depthRb
388 = intel_create_renderbuffer(MESA_FORMAT_Z16);
389 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
390 }
391
392 /* now add any/all software-based renderbuffers we may need */
393 _mesa_add_soft_renderbuffers(fb,
394 GL_FALSE, /* never sw color */
395 GL_FALSE, /* never sw depth */
396 swStencil, mesaVis->accumRedBits > 0,
397 GL_FALSE, /* never sw alpha */
398 GL_FALSE /* never sw aux */ );
399 driDrawPriv->driverPrivate = fb;
400
401 return GL_TRUE;
402 }
403 }
404
405 static void
406 intelDestroyBuffer(__DRIdrawable * driDrawPriv)
407 {
408 struct gl_framebuffer *fb = driDrawPriv->driverPrivate;
409
410 _mesa_reference_framebuffer(&fb, NULL);
411 }
412
413 /* There are probably better ways to do this, such as an
414 * init-designated function to register chipids and createcontext
415 * functions.
416 */
417 extern GLboolean i830CreateContext(const struct gl_config * mesaVis,
418 __DRIcontext * driContextPriv,
419 void *sharedContextPrivate);
420
421 extern GLboolean i915CreateContext(int api,
422 const struct gl_config * mesaVis,
423 __DRIcontext * driContextPriv,
424 void *sharedContextPrivate);
425 extern GLboolean brwCreateContext(int api,
426 const struct gl_config * mesaVis,
427 __DRIcontext * driContextPriv,
428 void *sharedContextPrivate);
429
430 static GLboolean
431 intelCreateContext(gl_api api,
432 const struct gl_config * mesaVis,
433 __DRIcontext * driContextPriv,
434 void *sharedContextPrivate)
435 {
436 __DRIscreen *sPriv = driContextPriv->driScreenPriv;
437 struct intel_screen *intelScreen = sPriv->private;
438
439 #ifdef I915
440 if (IS_9XX(intelScreen->deviceID)) {
441 if (!IS_965(intelScreen->deviceID)) {
442 return i915CreateContext(api, mesaVis, driContextPriv,
443 sharedContextPrivate);
444 }
445 } else {
446 intelScreen->no_vbo = GL_TRUE;
447 return i830CreateContext(mesaVis, driContextPriv, sharedContextPrivate);
448 }
449 #else
450 if (IS_965(intelScreen->deviceID))
451 return brwCreateContext(api, mesaVis,
452 driContextPriv, sharedContextPrivate);
453 #endif
454 fprintf(stderr, "Unrecognized deviceID 0x%x\n", intelScreen->deviceID);
455 return GL_FALSE;
456 }
457
458 static GLboolean
459 intel_init_bufmgr(struct intel_screen *intelScreen)
460 {
461 __DRIscreen *spriv = intelScreen->driScrnPriv;
462 int num_fences = 0;
463
464 intelScreen->no_hw = (getenv("INTEL_NO_HW") != NULL ||
465 getenv("INTEL_DEVID_OVERRIDE") != NULL);
466
467 intelScreen->bufmgr = intel_bufmgr_gem_init(spriv->fd, BATCH_SZ);
468 if (intelScreen->bufmgr == NULL) {
469 fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
470 __func__, __LINE__);
471 return GL_FALSE;
472 }
473
474 if (!intel_get_param(spriv, I915_PARAM_NUM_FENCES_AVAIL, &num_fences) ||
475 num_fences == 0) {
476 fprintf(stderr, "[%s: %u] Kernel 2.6.29 required.\n", __func__, __LINE__);
477 return GL_FALSE;
478 }
479
480 drm_intel_bufmgr_gem_enable_fenced_relocs(intelScreen->bufmgr);
481
482 intelScreen->named_regions = _mesa_NewHashTable();
483
484 return GL_TRUE;
485 }
486
487 /**
488 * This is the driver specific part of the createNewScreen entry point.
489 * Called when using DRI2.
490 *
491 * \return the struct gl_config supported by this driver
492 */
493 static const
494 __DRIconfig **intelInitScreen2(__DRIscreen *psp)
495 {
496 struct intel_screen *intelScreen;
497 GLenum fb_format[3];
498 GLenum fb_type[3];
499 unsigned int api_mask;
500 char *devid_override;
501
502 static const GLenum back_buffer_modes[] = {
503 GLX_NONE, GLX_SWAP_UNDEFINED_OML, GLX_SWAP_COPY_OML
504 };
505 uint8_t depth_bits[4], stencil_bits[4], msaa_samples_array[1];
506 int color;
507 __DRIconfig **configs = NULL;
508
509 /* Allocate the private area */
510 intelScreen = CALLOC(sizeof *intelScreen);
511 if (!intelScreen) {
512 fprintf(stderr, "\nERROR! Allocating private area failed\n");
513 return GL_FALSE;
514 }
515 /* parse information in __driConfigOptions */
516 driParseOptionInfo(&intelScreen->optionCache,
517 __driConfigOptions, __driNConfigOptions);
518
519 intelScreen->driScrnPriv = psp;
520 psp->private = (void *) intelScreen;
521
522 /* Determine chipset ID */
523 if (!intel_get_param(psp, I915_PARAM_CHIPSET_ID,
524 &intelScreen->deviceID))
525 return GL_FALSE;
526
527 /* Allow an override of the device ID for the purpose of making the
528 * driver produce dumps for debugging of new chipset enablement.
529 * This implies INTEL_NO_HW, to avoid programming your actual GPU
530 * incorrectly.
531 */
532 devid_override = getenv("INTEL_DEVID_OVERRIDE");
533 if (devid_override) {
534 intelScreen->deviceID = strtod(devid_override, NULL);
535 }
536
537 api_mask = (1 << __DRI_API_OPENGL);
538 #if FEATURE_ES1
539 api_mask |= (1 << __DRI_API_GLES);
540 #endif
541 #if FEATURE_ES2
542 api_mask |= (1 << __DRI_API_GLES2);
543 #endif
544
545 if (IS_9XX(intelScreen->deviceID) || IS_965(intelScreen->deviceID))
546 psp->api_mask = api_mask;
547
548 if (!intel_init_bufmgr(intelScreen))
549 return GL_FALSE;
550
551 psp->extensions = intelScreenExtensions;
552
553 msaa_samples_array[0] = 0;
554
555 fb_format[0] = GL_RGB;
556 fb_type[0] = GL_UNSIGNED_SHORT_5_6_5;
557
558 fb_format[1] = GL_BGR;
559 fb_type[1] = GL_UNSIGNED_INT_8_8_8_8_REV;
560
561 fb_format[2] = GL_BGRA;
562 fb_type[2] = GL_UNSIGNED_INT_8_8_8_8_REV;
563
564 depth_bits[0] = 0;
565 stencil_bits[0] = 0;
566
567 /* Generate a rich set of useful configs that do not include an
568 * accumulation buffer.
569 */
570 for (color = 0; color < ARRAY_SIZE(fb_format); color++) {
571 __DRIconfig **new_configs;
572 int depth_factor;
573
574 /* Starting with DRI2 protocol version 1.1 we can request a depth/stencil
575 * buffer that has a diffferent number of bits per pixel than the color
576 * buffer. This isn't yet supported here.
577 */
578 if (fb_type[color] == GL_UNSIGNED_SHORT_5_6_5) {
579 depth_bits[1] = 16;
580 stencil_bits[1] = 0;
581 } else {
582 depth_bits[1] = 24;
583 stencil_bits[1] = 8;
584 }
585
586 depth_factor = 2;
587
588 new_configs = driCreateConfigs(fb_format[color], fb_type[color],
589 depth_bits,
590 stencil_bits,
591 depth_factor,
592 back_buffer_modes,
593 ARRAY_SIZE(back_buffer_modes),
594 msaa_samples_array,
595 ARRAY_SIZE(msaa_samples_array),
596 GL_FALSE);
597 if (configs == NULL)
598 configs = new_configs;
599 else
600 configs = driConcatConfigs(configs, new_configs);
601 }
602
603 /* Generate the minimum possible set of configs that include an
604 * accumulation buffer.
605 */
606 for (color = 0; color < ARRAY_SIZE(fb_format); color++) {
607 __DRIconfig **new_configs;
608
609 if (fb_type[color] == GL_UNSIGNED_SHORT_5_6_5) {
610 depth_bits[0] = 16;
611 stencil_bits[0] = 0;
612 } else {
613 depth_bits[0] = 24;
614 stencil_bits[0] = 8;
615 }
616
617 new_configs = driCreateConfigs(fb_format[color], fb_type[color],
618 depth_bits, stencil_bits, 1,
619 back_buffer_modes + 1, 1,
620 msaa_samples_array, 1,
621 GL_TRUE);
622 if (configs == NULL)
623 configs = new_configs;
624 else
625 configs = driConcatConfigs(configs, new_configs);
626 }
627
628 if (configs == NULL) {
629 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
630 __LINE__);
631 return NULL;
632 }
633
634 return (const __DRIconfig **)configs;
635 }
636
637 const struct __DriverAPIRec driDriverAPI = {
638 .DestroyScreen = intelDestroyScreen,
639 .CreateContext = intelCreateContext,
640 .DestroyContext = intelDestroyContext,
641 .CreateBuffer = intelCreateBuffer,
642 .DestroyBuffer = intelDestroyBuffer,
643 .MakeCurrent = intelMakeCurrent,
644 .UnbindContext = intelUnbindContext,
645 .InitScreen2 = intelInitScreen2,
646 };
647
648 /* This is the table of extensions that the loader will dlsym() for. */
649 PUBLIC const __DRIextension *__driDriverExtensions[] = {
650 &driCoreExtension.base,
651 &driDRI2Extension.base,
652 NULL
653 };