1 /**************************************************************************
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
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"
39 PUBLIC const char __driConfigOptions[] =
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
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")
53 DRI_CONF_OPT_BEGIN(texture_tiling, bool, true)
54 DRI_CONF_DESC(en, "Enable texture tiling")
57 DRI_CONF_OPT_BEGIN(early_z, bool, false)
58 DRI_CONF_DESC(en, "Enable early Z in classic mode (unstable, 945-only).")
61 DRI_CONF_OPT_BEGIN(fragment_shader, bool, true)
62 DRI_CONF_DESC(en, "Enable limited ARB_fragment_shader support on 915/945.")
66 DRI_CONF_SECTION_QUALITY
67 DRI_CONF_FORCE_S3TC_ENABLE(false)
68 DRI_CONF_ALLOW_LARGE_TEXTURES(2)
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)
75 DRI_CONF_OPT_BEGIN(stub_occlusion_query, bool, false)
76 DRI_CONF_DESC(en, "Enable stub ARB_occlusion_query support on 915/945.")
81 const GLuint __driNConfigOptions = 11;
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"
94 #ifdef USE_NEW_INTERFACE
95 static PFNGLXCREATECONTEXTMODES create_context_modes = NULL;
96 #endif /*USE_NEW_INTERFACE */
98 static const __DRItexBufferExtension intelTexBufferExtension = {
99 { __DRI_TEX_BUFFER, __DRI_TEX_BUFFER_VERSION },
105 intelDRI2Flush(__DRIdrawable *drawable)
107 GET_CURRENT_CONTEXT(ctx);
108 struct intel_context *intel = intel_context(ctx);
111 INTEL_FIREVERTICES(intel);
113 intel->need_throttle = GL_TRUE;
115 if (intel->batch.used)
116 intel_batchbuffer_flush(intel);
119 static const struct __DRI2flushExtensionRec intelFlushExtension = {
120 { __DRI2_FLUSH, __DRI2_FLUSH_VERSION },
122 dri2InvalidateDrawable,
126 intel_create_image_from_name(__DRIscreen *screen,
127 int width, int height, int format,
128 int name, int pitch, void *loaderPrivate)
130 struct intel_screen *intelScreen = screen->private;
134 image = CALLOC(sizeof *image);
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;
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;
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;
159 image->data = loaderPrivate;
160 cpp = _mesa_get_format_bytes(image->format);
162 image->region = intel_region_alloc_for_handle(intelScreen,
164 pitch, name, "image");
165 if (image->region == NULL) {
174 intel_create_image_from_renderbuffer(__DRIcontext *context,
175 int renderbuffer, void *loaderPrivate)
178 struct intel_context *intel = context->driverPrivate;
179 struct gl_renderbuffer *rb;
180 struct intel_renderbuffer *irb;
182 rb = _mesa_lookup_renderbuffer(&intel->ctx, renderbuffer);
184 _mesa_error(&intel->ctx,
185 GL_INVALID_OPERATION, "glRenderbufferExternalMESA");
189 irb = intel_renderbuffer(rb);
190 image = CALLOC(sizeof *image);
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);
204 intel_destroy_image(__DRIimage *image)
206 intel_region_release(&image->region);
211 intel_create_image(__DRIscreen *screen,
212 int width, int height, int format,
217 struct intel_screen *intelScreen = screen->private;
220 image = CALLOC(sizeof *image);
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;
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;
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;
245 image->data = loaderPrivate;
246 cpp = _mesa_get_format_bytes(image->format);
249 intel_region_alloc(intelScreen, I915_TILING_NONE,
250 cpp, width, height, GL_TRUE);
251 if (image->region == NULL) {
260 intel_query_image(__DRIimage *image, int attrib, int *value)
263 case __DRI_IMAGE_ATTRIB_STRIDE:
264 *value = image->region->pitch * image->region->cpp;
266 case __DRI_IMAGE_ATTRIB_HANDLE:
267 *value = image->region->buffer->handle;
269 case __DRI_IMAGE_ATTRIB_NAME:
270 return intel_region_flink(image->region, (uint32_t *) value);
276 static struct __DRIimageExtensionRec intelImageExtension = {
277 { __DRI_IMAGE, __DRI_IMAGE_VERSION },
278 intel_create_image_from_name,
279 intel_create_image_from_renderbuffer,
285 static const __DRIextension *intelScreenExtensions[] = {
286 &driReadDrawableExtension,
287 &intelTexBufferExtension.base,
288 &intelFlushExtension.base,
289 &intelImageExtension.base,
290 &dri2ConfigQueryExtension.base,
295 intel_get_param(__DRIscreen *psp, int param, int *value)
298 struct drm_i915_getparam gp;
303 ret = drmCommandWriteRead(psp->fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
305 _mesa_warning(NULL, "drm_i915_getparam: %d", ret);
313 intel_get_boolean(__DRIscreen *psp, int param)
316 return intel_get_param(psp, param, &value) && value;
320 nop_callback(GLuint key, void *data, void *userData)
325 intelDestroyScreen(__DRIscreen * sPriv)
327 struct intel_screen *intelScreen = sPriv->private;
329 dri_bufmgr_destroy(intelScreen->bufmgr);
330 driDestroyOptionInfo(&intelScreen->optionCache);
332 /* Some regions may still have references to them at this point, so
333 * flush the hash table to prevent _mesa_DeleteHashTable() from
334 * complaining about the hash not being empty; */
335 _mesa_HashDeleteAll(intelScreen->named_regions, nop_callback, NULL);
336 _mesa_DeleteHashTable(intelScreen->named_regions);
339 sPriv->private = NULL;
344 * This is called when we need to set up GL rendering to a new X window.
347 intelCreateBuffer(__DRIscreen * driScrnPriv,
348 __DRIdrawable * driDrawPriv,
349 const struct gl_config * mesaVis, GLboolean isPixmap)
351 struct intel_renderbuffer *rb;
354 return GL_FALSE; /* not implemented */
357 GLboolean swStencil = (mesaVis->stencilBits > 0 &&
358 mesaVis->depthBits != 24);
361 struct gl_framebuffer *fb = CALLOC_STRUCT(gl_framebuffer);
366 _mesa_initialize_window_framebuffer(fb, mesaVis);
368 if (mesaVis->redBits == 5)
369 rgbFormat = MESA_FORMAT_RGB565;
370 else if (mesaVis->alphaBits == 0)
371 rgbFormat = MESA_FORMAT_XRGB8888;
373 rgbFormat = MESA_FORMAT_ARGB8888;
375 /* setup the hardware-based renderbuffers */
376 rb = intel_create_renderbuffer(rgbFormat);
377 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &rb->Base);
379 if (mesaVis->doubleBufferMode) {
380 rb = intel_create_renderbuffer(rgbFormat);
381 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &rb->Base);
384 if (mesaVis->depthBits == 24) {
385 assert(mesaVis->stencilBits == 8);
386 /* combined depth/stencil buffer */
387 struct intel_renderbuffer *depthStencilRb
388 = intel_create_renderbuffer(MESA_FORMAT_S8_Z24);
389 /* note: bind RB to two attachment points */
390 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthStencilRb->Base);
391 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &depthStencilRb->Base);
393 else if (mesaVis->depthBits == 16) {
394 /* just 16-bit depth buffer, no hw stencil */
395 struct intel_renderbuffer *depthRb
396 = intel_create_renderbuffer(MESA_FORMAT_Z16);
397 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
400 /* now add any/all software-based renderbuffers we may need */
401 _mesa_add_soft_renderbuffers(fb,
402 GL_FALSE, /* never sw color */
403 GL_FALSE, /* never sw depth */
404 swStencil, mesaVis->accumRedBits > 0,
405 GL_FALSE, /* never sw alpha */
406 GL_FALSE /* never sw aux */ );
407 driDrawPriv->driverPrivate = fb;
414 intelDestroyBuffer(__DRIdrawable * driDrawPriv)
416 struct gl_framebuffer *fb = driDrawPriv->driverPrivate;
418 _mesa_reference_framebuffer(&fb, NULL);
421 /* There are probably better ways to do this, such as an
422 * init-designated function to register chipids and createcontext
425 extern GLboolean i830CreateContext(const struct gl_config * mesaVis,
426 __DRIcontext * driContextPriv,
427 void *sharedContextPrivate);
429 extern GLboolean i915CreateContext(int api,
430 const struct gl_config * mesaVis,
431 __DRIcontext * driContextPriv,
432 void *sharedContextPrivate);
433 extern GLboolean brwCreateContext(int api,
434 const struct gl_config * mesaVis,
435 __DRIcontext * driContextPriv,
436 void *sharedContextPrivate);
439 intelCreateContext(gl_api api,
440 const struct gl_config * mesaVis,
441 __DRIcontext * driContextPriv,
442 void *sharedContextPrivate)
444 __DRIscreen *sPriv = driContextPriv->driScreenPriv;
445 struct intel_screen *intelScreen = sPriv->private;
448 if (IS_9XX(intelScreen->deviceID)) {
449 if (!IS_965(intelScreen->deviceID)) {
450 return i915CreateContext(api, mesaVis, driContextPriv,
451 sharedContextPrivate);
454 intelScreen->no_vbo = GL_TRUE;
455 return i830CreateContext(mesaVis, driContextPriv, sharedContextPrivate);
458 if (IS_965(intelScreen->deviceID))
459 return brwCreateContext(api, mesaVis,
460 driContextPriv, sharedContextPrivate);
462 fprintf(stderr, "Unrecognized deviceID 0x%x\n", intelScreen->deviceID);
467 intel_init_bufmgr(struct intel_screen *intelScreen)
469 __DRIscreen *spriv = intelScreen->driScrnPriv;
472 intelScreen->no_hw = (getenv("INTEL_NO_HW") != NULL ||
473 getenv("INTEL_DEVID_OVERRIDE") != NULL);
475 intelScreen->bufmgr = intel_bufmgr_gem_init(spriv->fd, BATCH_SZ);
476 if (intelScreen->bufmgr == NULL) {
477 fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
482 if (!intel_get_param(spriv, I915_PARAM_NUM_FENCES_AVAIL, &num_fences) ||
484 fprintf(stderr, "[%s: %u] Kernel 2.6.29 required.\n", __func__, __LINE__);
488 drm_intel_bufmgr_gem_enable_fenced_relocs(intelScreen->bufmgr);
490 intelScreen->named_regions = _mesa_NewHashTable();
492 intelScreen->relaxed_relocations = 0;
493 intelScreen->relaxed_relocations |=
494 intel_get_boolean(spriv, I915_PARAM_HAS_RELAXED_DELTA) << 0;
500 * This is the driver specific part of the createNewScreen entry point.
501 * Called when using DRI2.
503 * \return the struct gl_config supported by this driver
506 __DRIconfig **intelInitScreen2(__DRIscreen *psp)
508 struct intel_screen *intelScreen;
511 unsigned int api_mask;
512 char *devid_override;
514 static const GLenum back_buffer_modes[] = {
515 GLX_NONE, GLX_SWAP_UNDEFINED_OML, GLX_SWAP_COPY_OML
517 uint8_t depth_bits[4], stencil_bits[4], msaa_samples_array[1];
519 __DRIconfig **configs = NULL;
521 /* Allocate the private area */
522 intelScreen = CALLOC(sizeof *intelScreen);
524 fprintf(stderr, "\nERROR! Allocating private area failed\n");
527 /* parse information in __driConfigOptions */
528 driParseOptionInfo(&intelScreen->optionCache,
529 __driConfigOptions, __driNConfigOptions);
531 intelScreen->driScrnPriv = psp;
532 psp->private = (void *) intelScreen;
534 /* Determine chipset ID */
535 if (!intel_get_param(psp, I915_PARAM_CHIPSET_ID,
536 &intelScreen->deviceID))
539 /* Allow an override of the device ID for the purpose of making the
540 * driver produce dumps for debugging of new chipset enablement.
541 * This implies INTEL_NO_HW, to avoid programming your actual GPU
544 devid_override = getenv("INTEL_DEVID_OVERRIDE");
545 if (devid_override) {
546 intelScreen->deviceID = strtod(devid_override, NULL);
549 api_mask = (1 << __DRI_API_OPENGL);
551 api_mask |= (1 << __DRI_API_GLES);
554 api_mask |= (1 << __DRI_API_GLES2);
557 if (IS_9XX(intelScreen->deviceID) || IS_965(intelScreen->deviceID))
558 psp->api_mask = api_mask;
560 if (!intel_init_bufmgr(intelScreen))
563 psp->extensions = intelScreenExtensions;
565 msaa_samples_array[0] = 0;
567 fb_format[0] = GL_RGB;
568 fb_type[0] = GL_UNSIGNED_SHORT_5_6_5;
570 fb_format[1] = GL_BGR;
571 fb_type[1] = GL_UNSIGNED_INT_8_8_8_8_REV;
573 fb_format[2] = GL_BGRA;
574 fb_type[2] = GL_UNSIGNED_INT_8_8_8_8_REV;
579 /* Generate a rich set of useful configs that do not include an
580 * accumulation buffer.
582 for (color = 0; color < ARRAY_SIZE(fb_format); color++) {
583 __DRIconfig **new_configs;
586 /* Starting with DRI2 protocol version 1.1 we can request a depth/stencil
587 * buffer that has a diffferent number of bits per pixel than the color
588 * buffer. This isn't yet supported here.
590 if (fb_type[color] == GL_UNSIGNED_SHORT_5_6_5) {
600 new_configs = driCreateConfigs(fb_format[color], fb_type[color],
605 ARRAY_SIZE(back_buffer_modes),
607 ARRAY_SIZE(msaa_samples_array),
610 configs = new_configs;
612 configs = driConcatConfigs(configs, new_configs);
615 /* Generate the minimum possible set of configs that include an
616 * accumulation buffer.
618 for (color = 0; color < ARRAY_SIZE(fb_format); color++) {
619 __DRIconfig **new_configs;
621 if (fb_type[color] == GL_UNSIGNED_SHORT_5_6_5) {
629 new_configs = driCreateConfigs(fb_format[color], fb_type[color],
630 depth_bits, stencil_bits, 1,
631 back_buffer_modes + 1, 1,
632 msaa_samples_array, 1,
635 configs = new_configs;
637 configs = driConcatConfigs(configs, new_configs);
640 if (configs == NULL) {
641 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
646 return (const __DRIconfig **)configs;
649 struct intel_buffer {
651 struct intel_region *region;
655 intelAllocateBuffer(__DRIscreen *screen,
656 unsigned attachment, unsigned format,
657 int width, int height)
659 struct intel_buffer *intelBuffer;
660 struct intel_screen *intelScreen = screen->private;
662 intelBuffer = CALLOC(sizeof *intelBuffer);
663 if (intelBuffer == NULL)
666 intelBuffer->region = intel_region_alloc(intelScreen, I915_TILING_NONE,
667 format / 8, width, height, GL_TRUE);
669 if (intelBuffer->region == NULL) {
674 intel_region_flink(intelBuffer->region, &intelBuffer->base.name);
676 intelBuffer->base.attachment = attachment;
677 intelBuffer->base.cpp = intelBuffer->region->cpp;
678 intelBuffer->base.pitch =
679 intelBuffer->region->pitch * intelBuffer->region->cpp;
681 return &intelBuffer->base;
685 intelReleaseBuffer(__DRIscreen *screen, __DRIbuffer *buffer)
687 struct intel_buffer *intelBuffer = (struct intel_buffer *) buffer;
689 intel_region_release(&intelBuffer->region);
694 const struct __DriverAPIRec driDriverAPI = {
695 .DestroyScreen = intelDestroyScreen,
696 .CreateContext = intelCreateContext,
697 .DestroyContext = intelDestroyContext,
698 .CreateBuffer = intelCreateBuffer,
699 .DestroyBuffer = intelDestroyBuffer,
700 .MakeCurrent = intelMakeCurrent,
701 .UnbindContext = intelUnbindContext,
702 .InitScreen2 = intelInitScreen2,
703 .AllocateBuffer = intelAllocateBuffer,
704 .ReleaseBuffer = intelReleaseBuffer
707 /* This is the table of extensions that the loader will dlsym() for. */
708 PUBLIC const __DRIextension *__driDriverExtensions[] = {
709 &driCoreExtension.base,
710 &driDRI2Extension.base,