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 **************************************************************************/
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"
40 PUBLIC
const char __driConfigOptions
[] =
42 DRI_CONF_SECTION_PERFORMANCE
43 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_ALWAYS_SYNC
)
44 /* Options correspond to DRI_CONF_BO_REUSE_DISABLED,
45 * DRI_CONF_BO_REUSE_ALL
47 DRI_CONF_OPT_BEGIN_V(bo_reuse
, enum, 1, "0:1")
48 DRI_CONF_DESC_BEGIN(en
, "Buffer object reuse")
49 DRI_CONF_ENUM(0, "Disable buffer object reuse")
50 DRI_CONF_ENUM(1, "Enable reuse of all sizes of buffer objects")
54 DRI_CONF_OPT_BEGIN(texture_tiling
, bool, true)
55 DRI_CONF_DESC(en
, "Enable texture tiling")
58 DRI_CONF_OPT_BEGIN(early_z
, bool, false)
59 DRI_CONF_DESC(en
, "Enable early Z in classic mode (unstable, 945-only).")
62 DRI_CONF_OPT_BEGIN(fragment_shader
, bool, true)
63 DRI_CONF_DESC(en
, "Enable limited ARB_fragment_shader support on 915/945.")
67 DRI_CONF_SECTION_QUALITY
68 DRI_CONF_FORCE_S3TC_ENABLE(false)
69 DRI_CONF_ALLOW_LARGE_TEXTURES(2)
71 DRI_CONF_SECTION_DEBUG
72 DRI_CONF_NO_RAST(false)
73 DRI_CONF_ALWAYS_FLUSH_BATCH(false)
74 DRI_CONF_ALWAYS_FLUSH_CACHE(false)
76 DRI_CONF_OPT_BEGIN(stub_occlusion_query
, bool, false)
77 DRI_CONF_DESC(en
, "Enable stub ARB_occlusion_query support on 915/945.")
82 const GLuint __driNConfigOptions
= 11;
84 #include "intel_batchbuffer.h"
85 #include "intel_buffers.h"
86 #include "intel_bufmgr.h"
87 #include "intel_chipset.h"
88 #include "intel_fbo.h"
89 #include "intel_screen.h"
90 #include "intel_tex.h"
91 #include "intel_regions.h"
95 #ifdef USE_NEW_INTERFACE
96 static PFNGLXCREATECONTEXTMODES create_context_modes
= NULL
;
97 #endif /*USE_NEW_INTERFACE */
99 static const __DRItexBufferExtension intelTexBufferExtension
= {
100 { __DRI_TEX_BUFFER
, __DRI_TEX_BUFFER_VERSION
},
106 intelDRI2Flush(__DRIdrawable
*drawable
)
108 GET_CURRENT_CONTEXT(ctx
);
109 struct intel_context
*intel
= intel_context(ctx
);
112 INTEL_FIREVERTICES(intel
);
114 intel
->need_throttle
= GL_TRUE
;
116 if (intel
->batch
.used
)
117 intel_batchbuffer_flush(intel
);
120 static const struct __DRI2flushExtensionRec intelFlushExtension
= {
121 { __DRI2_FLUSH
, __DRI2_FLUSH_VERSION
},
123 dri2InvalidateDrawable
,
127 intel_create_image_from_name(__DRIscreen
*screen
,
128 int width
, int height
, int format
,
129 int name
, int pitch
, void *loaderPrivate
)
131 struct intel_screen
*intelScreen
= screen
->private;
135 image
= CALLOC(sizeof *image
);
140 case __DRI_IMAGE_FORMAT_RGB565
:
141 image
->format
= MESA_FORMAT_RGB565
;
142 image
->internal_format
= GL_RGB
;
143 image
->data_type
= GL_UNSIGNED_BYTE
;
145 case __DRI_IMAGE_FORMAT_XRGB8888
:
146 image
->format
= MESA_FORMAT_XRGB8888
;
147 image
->internal_format
= GL_RGB
;
148 image
->data_type
= GL_UNSIGNED_BYTE
;
150 case __DRI_IMAGE_FORMAT_ARGB8888
:
151 image
->format
= MESA_FORMAT_ARGB8888
;
152 image
->internal_format
= GL_RGBA
;
153 image
->data_type
= GL_UNSIGNED_BYTE
;
160 image
->data
= loaderPrivate
;
161 cpp
= _mesa_get_format_bytes(image
->format
);
163 image
->region
= intel_region_alloc_for_handle(intelScreen
,
165 pitch
, name
, "image");
166 if (image
->region
== NULL
) {
175 intel_create_image_from_renderbuffer(__DRIcontext
*context
,
176 int renderbuffer
, void *loaderPrivate
)
179 struct intel_context
*intel
= context
->driverPrivate
;
180 struct gl_renderbuffer
*rb
;
181 struct intel_renderbuffer
*irb
;
183 rb
= _mesa_lookup_renderbuffer(&intel
->ctx
, renderbuffer
);
185 _mesa_error(&intel
->ctx
,
186 GL_INVALID_OPERATION
, "glRenderbufferExternalMESA");
190 irb
= intel_renderbuffer(rb
);
191 image
= CALLOC(sizeof *image
);
195 image
->internal_format
= rb
->InternalFormat
;
196 image
->format
= rb
->Format
;
197 image
->data_type
= rb
->DataType
;
198 image
->data
= loaderPrivate
;
199 intel_region_reference(&image
->region
, irb
->region
);
205 intel_destroy_image(__DRIimage
*image
)
207 intel_region_release(&image
->region
);
212 intel_create_image(__DRIscreen
*screen
,
213 int width
, int height
, int format
,
218 struct intel_screen
*intelScreen
= screen
->private;
221 image
= CALLOC(sizeof *image
);
226 case __DRI_IMAGE_FORMAT_RGB565
:
227 image
->format
= MESA_FORMAT_RGB565
;
228 image
->internal_format
= GL_RGB
;
229 image
->data_type
= GL_UNSIGNED_BYTE
;
231 case __DRI_IMAGE_FORMAT_XRGB8888
:
232 image
->format
= MESA_FORMAT_XRGB8888
;
233 image
->internal_format
= GL_RGB
;
234 image
->data_type
= GL_UNSIGNED_BYTE
;
236 case __DRI_IMAGE_FORMAT_ARGB8888
:
237 image
->format
= MESA_FORMAT_ARGB8888
;
238 image
->internal_format
= GL_RGBA
;
239 image
->data_type
= GL_UNSIGNED_BYTE
;
246 image
->data
= loaderPrivate
;
247 cpp
= _mesa_get_format_bytes(image
->format
);
250 intel_region_alloc(intelScreen
, I915_TILING_X
,
251 cpp
, width
, height
, GL_TRUE
);
252 if (image
->region
== NULL
) {
261 intel_query_image(__DRIimage
*image
, int attrib
, int *value
)
264 case __DRI_IMAGE_ATTRIB_STRIDE
:
265 *value
= image
->region
->pitch
* image
->region
->cpp
;
267 case __DRI_IMAGE_ATTRIB_HANDLE
:
268 *value
= image
->region
->buffer
->handle
;
270 case __DRI_IMAGE_ATTRIB_NAME
:
271 return intel_region_flink(image
->region
, (uint32_t *) value
);
277 static struct __DRIimageExtensionRec intelImageExtension
= {
278 { __DRI_IMAGE
, __DRI_IMAGE_VERSION
},
279 intel_create_image_from_name
,
280 intel_create_image_from_renderbuffer
,
286 static const __DRIextension
*intelScreenExtensions
[] = {
287 &driReadDrawableExtension
,
288 &intelTexBufferExtension
.base
,
289 &intelFlushExtension
.base
,
290 &intelImageExtension
.base
,
291 &dri2ConfigQueryExtension
.base
,
296 intel_get_param(__DRIscreen
*psp
, int param
, int *value
)
299 struct drm_i915_getparam gp
;
304 ret
= drmCommandWriteRead(psp
->fd
, DRM_I915_GETPARAM
, &gp
, sizeof(gp
));
307 _mesa_warning(NULL
, "drm_i915_getparam: %d", ret
);
315 intel_get_boolean(__DRIscreen
*psp
, int param
)
318 return intel_get_param(psp
, param
, &value
) && value
;
322 nop_callback(GLuint key
, void *data
, void *userData
)
327 intelDestroyScreen(__DRIscreen
* sPriv
)
329 struct intel_screen
*intelScreen
= sPriv
->private;
331 dri_bufmgr_destroy(intelScreen
->bufmgr
);
332 driDestroyOptionInfo(&intelScreen
->optionCache
);
334 /* Some regions may still have references to them at this point, so
335 * flush the hash table to prevent _mesa_DeleteHashTable() from
336 * complaining about the hash not being empty; */
337 _mesa_HashDeleteAll(intelScreen
->named_regions
, nop_callback
, NULL
);
338 _mesa_DeleteHashTable(intelScreen
->named_regions
);
341 sPriv
->private = NULL
;
346 * This is called when we need to set up GL rendering to a new X window.
349 intelCreateBuffer(__DRIscreen
* driScrnPriv
,
350 __DRIdrawable
* driDrawPriv
,
351 const struct gl_config
* mesaVis
, GLboolean isPixmap
)
353 struct intel_renderbuffer
*rb
;
356 return GL_FALSE
; /* not implemented */
359 GLboolean swStencil
= (mesaVis
->stencilBits
> 0 &&
360 mesaVis
->depthBits
!= 24);
363 struct gl_framebuffer
*fb
= CALLOC_STRUCT(gl_framebuffer
);
368 _mesa_initialize_window_framebuffer(fb
, mesaVis
);
370 if (mesaVis
->redBits
== 5)
371 rgbFormat
= MESA_FORMAT_RGB565
;
372 else if (mesaVis
->alphaBits
== 0)
373 rgbFormat
= MESA_FORMAT_XRGB8888
;
375 rgbFormat
= MESA_FORMAT_ARGB8888
;
377 /* setup the hardware-based renderbuffers */
378 rb
= intel_create_renderbuffer(rgbFormat
);
379 _mesa_add_renderbuffer(fb
, BUFFER_FRONT_LEFT
, &rb
->Base
);
381 if (mesaVis
->doubleBufferMode
) {
382 rb
= intel_create_renderbuffer(rgbFormat
);
383 _mesa_add_renderbuffer(fb
, BUFFER_BACK_LEFT
, &rb
->Base
);
386 if (mesaVis
->depthBits
== 24) {
387 assert(mesaVis
->stencilBits
== 8);
388 /* combined depth/stencil buffer */
389 struct intel_renderbuffer
*depthStencilRb
390 = intel_create_renderbuffer(MESA_FORMAT_S8_Z24
);
391 /* note: bind RB to two attachment points */
392 _mesa_add_renderbuffer(fb
, BUFFER_DEPTH
, &depthStencilRb
->Base
);
393 _mesa_add_renderbuffer(fb
, BUFFER_STENCIL
, &depthStencilRb
->Base
);
395 else if (mesaVis
->depthBits
== 16) {
396 /* just 16-bit depth buffer, no hw stencil */
397 struct intel_renderbuffer
*depthRb
398 = intel_create_renderbuffer(MESA_FORMAT_Z16
);
399 _mesa_add_renderbuffer(fb
, BUFFER_DEPTH
, &depthRb
->Base
);
402 /* now add any/all software-based renderbuffers we may need */
403 _mesa_add_soft_renderbuffers(fb
,
404 GL_FALSE
, /* never sw color */
405 GL_FALSE
, /* never sw depth */
406 swStencil
, mesaVis
->accumRedBits
> 0,
407 GL_FALSE
, /* never sw alpha */
408 GL_FALSE
/* never sw aux */ );
409 driDrawPriv
->driverPrivate
= fb
;
416 intelDestroyBuffer(__DRIdrawable
* driDrawPriv
)
418 struct gl_framebuffer
*fb
= driDrawPriv
->driverPrivate
;
420 _mesa_reference_framebuffer(&fb
, NULL
);
423 /* There are probably better ways to do this, such as an
424 * init-designated function to register chipids and createcontext
427 extern GLboolean
i830CreateContext(const struct gl_config
* mesaVis
,
428 __DRIcontext
* driContextPriv
,
429 void *sharedContextPrivate
);
431 extern GLboolean
i915CreateContext(int api
,
432 const struct gl_config
* mesaVis
,
433 __DRIcontext
* driContextPriv
,
434 void *sharedContextPrivate
);
435 extern GLboolean
brwCreateContext(int api
,
436 const struct gl_config
* mesaVis
,
437 __DRIcontext
* driContextPriv
,
438 void *sharedContextPrivate
);
441 intelCreateContext(gl_api api
,
442 const struct gl_config
* mesaVis
,
443 __DRIcontext
* driContextPriv
,
444 void *sharedContextPrivate
)
446 __DRIscreen
*sPriv
= driContextPriv
->driScreenPriv
;
447 struct intel_screen
*intelScreen
= sPriv
->private;
450 if (IS_9XX(intelScreen
->deviceID
)) {
451 if (!IS_965(intelScreen
->deviceID
)) {
452 return i915CreateContext(api
, mesaVis
, driContextPriv
,
453 sharedContextPrivate
);
456 intelScreen
->no_vbo
= GL_TRUE
;
457 return i830CreateContext(mesaVis
, driContextPriv
, sharedContextPrivate
);
460 if (IS_965(intelScreen
->deviceID
))
461 return brwCreateContext(api
, mesaVis
,
462 driContextPriv
, sharedContextPrivate
);
464 fprintf(stderr
, "Unrecognized deviceID 0x%x\n", intelScreen
->deviceID
);
469 intel_init_bufmgr(struct intel_screen
*intelScreen
)
471 __DRIscreen
*spriv
= intelScreen
->driScrnPriv
;
474 intelScreen
->no_hw
= (getenv("INTEL_NO_HW") != NULL
||
475 getenv("INTEL_DEVID_OVERRIDE") != NULL
);
477 intelScreen
->bufmgr
= intel_bufmgr_gem_init(spriv
->fd
, BATCH_SZ
);
478 if (intelScreen
->bufmgr
== NULL
) {
479 fprintf(stderr
, "[%s:%u] Error initializing buffer manager.\n",
484 if (!intel_get_param(spriv
, I915_PARAM_NUM_FENCES_AVAIL
, &num_fences
) ||
486 fprintf(stderr
, "[%s: %u] Kernel 2.6.29 required.\n", __func__
, __LINE__
);
490 drm_intel_bufmgr_gem_enable_fenced_relocs(intelScreen
->bufmgr
);
492 intelScreen
->named_regions
= _mesa_NewHashTable();
494 intelScreen
->relaxed_relocations
= 0;
495 intelScreen
->relaxed_relocations
|=
496 intel_get_boolean(spriv
, I915_PARAM_HAS_RELAXED_DELTA
) << 0;
502 * This is the driver specific part of the createNewScreen entry point.
503 * Called when using DRI2.
505 * \return the struct gl_config supported by this driver
508 __DRIconfig
**intelInitScreen2(__DRIscreen
*psp
)
510 struct intel_screen
*intelScreen
;
513 unsigned int api_mask
;
514 char *devid_override
;
516 static const GLenum back_buffer_modes
[] = {
517 GLX_NONE
, GLX_SWAP_UNDEFINED_OML
, GLX_SWAP_COPY_OML
519 uint8_t depth_bits
[4], stencil_bits
[4], msaa_samples_array
[1];
521 __DRIconfig
**configs
= NULL
;
523 /* Allocate the private area */
524 intelScreen
= CALLOC(sizeof *intelScreen
);
526 fprintf(stderr
, "\nERROR! Allocating private area failed\n");
529 /* parse information in __driConfigOptions */
530 driParseOptionInfo(&intelScreen
->optionCache
,
531 __driConfigOptions
, __driNConfigOptions
);
533 intelScreen
->driScrnPriv
= psp
;
534 psp
->private = (void *) intelScreen
;
536 /* Determine chipset ID */
537 if (!intel_get_param(psp
, I915_PARAM_CHIPSET_ID
,
538 &intelScreen
->deviceID
))
541 /* Allow an override of the device ID for the purpose of making the
542 * driver produce dumps for debugging of new chipset enablement.
543 * This implies INTEL_NO_HW, to avoid programming your actual GPU
546 devid_override
= getenv("INTEL_DEVID_OVERRIDE");
547 if (devid_override
) {
548 intelScreen
->deviceID
= strtod(devid_override
, NULL
);
551 if (IS_GEN6(intelScreen
->deviceID
)) {
552 intelScreen
->gen
= 6;
553 } else if (IS_GEN5(intelScreen
->deviceID
)) {
554 intelScreen
->gen
= 5;
555 } else if (IS_965(intelScreen
->deviceID
)) {
556 intelScreen
->gen
= 4;
557 } else if (IS_9XX(intelScreen
->deviceID
)) {
558 intelScreen
->gen
= 3;
560 intelScreen
->gen
= 2;
563 api_mask
= (1 << __DRI_API_OPENGL
);
565 api_mask
|= (1 << __DRI_API_GLES
);
568 api_mask
|= (1 << __DRI_API_GLES2
);
571 if (IS_9XX(intelScreen
->deviceID
) || IS_965(intelScreen
->deviceID
))
572 psp
->api_mask
= api_mask
;
574 if (!intel_init_bufmgr(intelScreen
))
577 psp
->extensions
= intelScreenExtensions
;
579 msaa_samples_array
[0] = 0;
581 fb_format
[0] = GL_RGB
;
582 fb_type
[0] = GL_UNSIGNED_SHORT_5_6_5
;
584 fb_format
[1] = GL_BGR
;
585 fb_type
[1] = GL_UNSIGNED_INT_8_8_8_8_REV
;
587 fb_format
[2] = GL_BGRA
;
588 fb_type
[2] = GL_UNSIGNED_INT_8_8_8_8_REV
;
593 /* Generate a rich set of useful configs that do not include an
594 * accumulation buffer.
596 for (color
= 0; color
< ARRAY_SIZE(fb_format
); color
++) {
597 __DRIconfig
**new_configs
;
600 /* Starting with DRI2 protocol version 1.1 we can request a depth/stencil
601 * buffer that has a diffferent number of bits per pixel than the color
602 * buffer. This isn't yet supported here.
604 if (fb_type
[color
] == GL_UNSIGNED_SHORT_5_6_5
) {
614 new_configs
= driCreateConfigs(fb_format
[color
], fb_type
[color
],
619 ARRAY_SIZE(back_buffer_modes
),
621 ARRAY_SIZE(msaa_samples_array
),
624 configs
= new_configs
;
626 configs
= driConcatConfigs(configs
, new_configs
);
629 /* Generate the minimum possible set of configs that include an
630 * accumulation buffer.
632 for (color
= 0; color
< ARRAY_SIZE(fb_format
); color
++) {
633 __DRIconfig
**new_configs
;
635 if (fb_type
[color
] == GL_UNSIGNED_SHORT_5_6_5
) {
643 new_configs
= driCreateConfigs(fb_format
[color
], fb_type
[color
],
644 depth_bits
, stencil_bits
, 1,
645 back_buffer_modes
+ 1, 1,
646 msaa_samples_array
, 1,
649 configs
= new_configs
;
651 configs
= driConcatConfigs(configs
, new_configs
);
654 if (configs
== NULL
) {
655 fprintf(stderr
, "[%s:%u] Error creating FBConfig!\n", __func__
,
660 return (const __DRIconfig
**)configs
;
663 struct intel_buffer
{
665 struct intel_region
*region
;
669 intelAllocateBuffer(__DRIscreen
*screen
,
670 unsigned attachment
, unsigned format
,
671 int width
, int height
)
673 struct intel_buffer
*intelBuffer
;
674 struct intel_screen
*intelScreen
= screen
->private;
676 intelBuffer
= CALLOC(sizeof *intelBuffer
);
677 if (intelBuffer
== NULL
)
680 intelBuffer
->region
= intel_region_alloc(intelScreen
, I915_TILING_NONE
,
681 format
/ 8, width
, height
, GL_TRUE
);
683 if (intelBuffer
->region
== NULL
) {
688 intel_region_flink(intelBuffer
->region
, &intelBuffer
->base
.name
);
690 intelBuffer
->base
.attachment
= attachment
;
691 intelBuffer
->base
.cpp
= intelBuffer
->region
->cpp
;
692 intelBuffer
->base
.pitch
=
693 intelBuffer
->region
->pitch
* intelBuffer
->region
->cpp
;
695 return &intelBuffer
->base
;
699 intelReleaseBuffer(__DRIscreen
*screen
, __DRIbuffer
*buffer
)
701 struct intel_buffer
*intelBuffer
= (struct intel_buffer
*) buffer
;
703 intel_region_release(&intelBuffer
->region
);
708 const struct __DriverAPIRec driDriverAPI
= {
709 .DestroyScreen
= intelDestroyScreen
,
710 .CreateContext
= intelCreateContext
,
711 .DestroyContext
= intelDestroyContext
,
712 .CreateBuffer
= intelCreateBuffer
,
713 .DestroyBuffer
= intelDestroyBuffer
,
714 .MakeCurrent
= intelMakeCurrent
,
715 .UnbindContext
= intelUnbindContext
,
716 .InitScreen2
= intelInitScreen2
,
717 .AllocateBuffer
= intelAllocateBuffer
,
718 .ReleaseBuffer
= intelReleaseBuffer
721 /* This is the table of extensions that the loader will dlsym() for. */
722 PUBLIC
const __DRIextension
*__driDriverExtensions
[] = {
723 &driCoreExtension
.base
,
724 &driDRI2Extension
.base
,