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