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 nop_callback(GLuint key
, void *data
, void *userData
)
318 intelDestroyScreen(__DRIscreen
* sPriv
)
320 struct intel_screen
*intelScreen
= sPriv
->private;
322 dri_bufmgr_destroy(intelScreen
->bufmgr
);
323 driDestroyOptionInfo(&intelScreen
->optionCache
);
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
);
332 sPriv
->private = NULL
;
337 * This is called when we need to set up GL rendering to a new X window.
340 intelCreateBuffer(__DRIscreen
* driScrnPriv
,
341 __DRIdrawable
* driDrawPriv
,
342 const struct gl_config
* mesaVis
, GLboolean isPixmap
)
344 struct intel_renderbuffer
*rb
;
347 return GL_FALSE
; /* not implemented */
350 GLboolean swStencil
= (mesaVis
->stencilBits
> 0 &&
351 mesaVis
->depthBits
!= 24);
354 struct gl_framebuffer
*fb
= CALLOC_STRUCT(gl_framebuffer
);
359 _mesa_initialize_window_framebuffer(fb
, mesaVis
);
361 if (mesaVis
->redBits
== 5)
362 rgbFormat
= MESA_FORMAT_RGB565
;
363 else if (mesaVis
->alphaBits
== 0)
364 rgbFormat
= MESA_FORMAT_XRGB8888
;
366 rgbFormat
= MESA_FORMAT_ARGB8888
;
368 /* setup the hardware-based renderbuffers */
369 rb
= intel_create_renderbuffer(rgbFormat
);
370 _mesa_add_renderbuffer(fb
, BUFFER_FRONT_LEFT
, &rb
->Base
);
372 if (mesaVis
->doubleBufferMode
) {
373 rb
= intel_create_renderbuffer(rgbFormat
);
374 _mesa_add_renderbuffer(fb
, BUFFER_BACK_LEFT
, &rb
->Base
);
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
);
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
);
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
;
407 intelDestroyBuffer(__DRIdrawable
* driDrawPriv
)
409 struct gl_framebuffer
*fb
= driDrawPriv
->driverPrivate
;
411 _mesa_reference_framebuffer(&fb
, NULL
);
414 /* There are probably better ways to do this, such as an
415 * init-designated function to register chipids and createcontext
418 extern GLboolean
i830CreateContext(const struct gl_config
* mesaVis
,
419 __DRIcontext
* driContextPriv
,
420 void *sharedContextPrivate
);
422 extern GLboolean
i915CreateContext(int api
,
423 const struct gl_config
* mesaVis
,
424 __DRIcontext
* driContextPriv
,
425 void *sharedContextPrivate
);
426 extern GLboolean
brwCreateContext(int api
,
427 const struct gl_config
* mesaVis
,
428 __DRIcontext
* driContextPriv
,
429 void *sharedContextPrivate
);
432 intelCreateContext(gl_api api
,
433 const struct gl_config
* mesaVis
,
434 __DRIcontext
* driContextPriv
,
435 void *sharedContextPrivate
)
437 __DRIscreen
*sPriv
= driContextPriv
->driScreenPriv
;
438 struct intel_screen
*intelScreen
= sPriv
->private;
441 if (IS_9XX(intelScreen
->deviceID
)) {
442 if (!IS_965(intelScreen
->deviceID
)) {
443 return i915CreateContext(api
, mesaVis
, driContextPriv
,
444 sharedContextPrivate
);
447 intelScreen
->no_vbo
= GL_TRUE
;
448 return i830CreateContext(mesaVis
, driContextPriv
, sharedContextPrivate
);
451 if (IS_965(intelScreen
->deviceID
))
452 return brwCreateContext(api
, mesaVis
,
453 driContextPriv
, sharedContextPrivate
);
455 fprintf(stderr
, "Unrecognized deviceID 0x%x\n", intelScreen
->deviceID
);
460 intel_init_bufmgr(struct intel_screen
*intelScreen
)
462 __DRIscreen
*spriv
= intelScreen
->driScrnPriv
;
465 intelScreen
->no_hw
= (getenv("INTEL_NO_HW") != NULL
||
466 getenv("INTEL_DEVID_OVERRIDE") != NULL
);
468 intelScreen
->bufmgr
= intel_bufmgr_gem_init(spriv
->fd
, BATCH_SZ
);
469 if (intelScreen
->bufmgr
== NULL
) {
470 fprintf(stderr
, "[%s:%u] Error initializing buffer manager.\n",
475 if (!intel_get_param(spriv
, I915_PARAM_NUM_FENCES_AVAIL
, &num_fences
) ||
477 fprintf(stderr
, "[%s: %u] Kernel 2.6.29 required.\n", __func__
, __LINE__
);
481 drm_intel_bufmgr_gem_enable_fenced_relocs(intelScreen
->bufmgr
);
483 intelScreen
->named_regions
= _mesa_NewHashTable();
489 * This is the driver specific part of the createNewScreen entry point.
490 * Called when using DRI2.
492 * \return the struct gl_config supported by this driver
495 __DRIconfig
**intelInitScreen2(__DRIscreen
*psp
)
497 struct intel_screen
*intelScreen
;
500 unsigned int api_mask
;
501 char *devid_override
;
503 static const GLenum back_buffer_modes
[] = {
504 GLX_NONE
, GLX_SWAP_UNDEFINED_OML
, GLX_SWAP_COPY_OML
506 uint8_t depth_bits
[4], stencil_bits
[4], msaa_samples_array
[1];
508 __DRIconfig
**configs
= NULL
;
510 /* Allocate the private area */
511 intelScreen
= CALLOC(sizeof *intelScreen
);
513 fprintf(stderr
, "\nERROR! Allocating private area failed\n");
516 /* parse information in __driConfigOptions */
517 driParseOptionInfo(&intelScreen
->optionCache
,
518 __driConfigOptions
, __driNConfigOptions
);
520 intelScreen
->driScrnPriv
= psp
;
521 psp
->private = (void *) intelScreen
;
523 /* Determine chipset ID */
524 if (!intel_get_param(psp
, I915_PARAM_CHIPSET_ID
,
525 &intelScreen
->deviceID
))
528 /* Allow an override of the device ID for the purpose of making the
529 * driver produce dumps for debugging of new chipset enablement.
530 * This implies INTEL_NO_HW, to avoid programming your actual GPU
533 devid_override
= getenv("INTEL_DEVID_OVERRIDE");
534 if (devid_override
) {
535 intelScreen
->deviceID
= strtod(devid_override
, NULL
);
538 api_mask
= (1 << __DRI_API_OPENGL
);
540 api_mask
|= (1 << __DRI_API_GLES
);
543 api_mask
|= (1 << __DRI_API_GLES2
);
546 if (IS_9XX(intelScreen
->deviceID
) || IS_965(intelScreen
->deviceID
))
547 psp
->api_mask
= api_mask
;
549 if (!intel_init_bufmgr(intelScreen
))
552 psp
->extensions
= intelScreenExtensions
;
554 msaa_samples_array
[0] = 0;
556 fb_format
[0] = GL_RGB
;
557 fb_type
[0] = GL_UNSIGNED_SHORT_5_6_5
;
559 fb_format
[1] = GL_BGR
;
560 fb_type
[1] = GL_UNSIGNED_INT_8_8_8_8_REV
;
562 fb_format
[2] = GL_BGRA
;
563 fb_type
[2] = GL_UNSIGNED_INT_8_8_8_8_REV
;
568 /* Generate a rich set of useful configs that do not include an
569 * accumulation buffer.
571 for (color
= 0; color
< ARRAY_SIZE(fb_format
); color
++) {
572 __DRIconfig
**new_configs
;
575 /* Starting with DRI2 protocol version 1.1 we can request a depth/stencil
576 * buffer that has a diffferent number of bits per pixel than the color
577 * buffer. This isn't yet supported here.
579 if (fb_type
[color
] == GL_UNSIGNED_SHORT_5_6_5
) {
589 new_configs
= driCreateConfigs(fb_format
[color
], fb_type
[color
],
594 ARRAY_SIZE(back_buffer_modes
),
596 ARRAY_SIZE(msaa_samples_array
),
599 configs
= new_configs
;
601 configs
= driConcatConfigs(configs
, new_configs
);
604 /* Generate the minimum possible set of configs that include an
605 * accumulation buffer.
607 for (color
= 0; color
< ARRAY_SIZE(fb_format
); color
++) {
608 __DRIconfig
**new_configs
;
610 if (fb_type
[color
] == GL_UNSIGNED_SHORT_5_6_5
) {
618 new_configs
= driCreateConfigs(fb_format
[color
], fb_type
[color
],
619 depth_bits
, stencil_bits
, 1,
620 back_buffer_modes
+ 1, 1,
621 msaa_samples_array
, 1,
624 configs
= new_configs
;
626 configs
= driConcatConfigs(configs
, new_configs
);
629 if (configs
== NULL
) {
630 fprintf(stderr
, "[%s:%u] Error creating FBConfig!\n", __func__
,
635 return (const __DRIconfig
**)configs
;
638 struct intel_buffer
{
640 struct intel_region
*region
;
644 intelAllocateBuffer(__DRIscreen
*screen
,
645 unsigned attachment
, unsigned format
,
646 int width
, int height
)
648 struct intel_buffer
*intelBuffer
;
649 struct intel_screen
*intelScreen
= screen
->private;
651 intelBuffer
= CALLOC(sizeof *intelBuffer
);
652 if (intelBuffer
== NULL
)
655 intelBuffer
->region
= intel_region_alloc(intelScreen
, I915_TILING_NONE
,
656 format
/ 8, width
, height
, GL_TRUE
);
658 if (intelBuffer
->region
== NULL
) {
663 intel_region_flink(intelBuffer
->region
, &intelBuffer
->base
.name
);
665 intelBuffer
->base
.attachment
= attachment
;
666 intelBuffer
->base
.cpp
= intelBuffer
->region
->cpp
;
667 intelBuffer
->base
.pitch
=
668 intelBuffer
->region
->pitch
* intelBuffer
->region
->cpp
;
670 return &intelBuffer
->base
;
674 intelReleaseBuffer(__DRIscreen
*screen
, __DRIbuffer
*buffer
)
676 struct intel_buffer
*intelBuffer
= (struct intel_buffer
*) buffer
;
678 intel_region_release(&intelBuffer
->region
);
683 const struct __DriverAPIRec driDriverAPI
= {
684 .DestroyScreen
= intelDestroyScreen
,
685 .CreateContext
= intelCreateContext
,
686 .DestroyContext
= intelDestroyContext
,
687 .CreateBuffer
= intelCreateBuffer
,
688 .DestroyBuffer
= intelDestroyBuffer
,
689 .MakeCurrent
= intelMakeCurrent
,
690 .UnbindContext
= intelUnbindContext
,
691 .InitScreen2
= intelInitScreen2
,
692 .AllocateBuffer
= intelAllocateBuffer
,
693 .ReleaseBuffer
= intelReleaseBuffer
696 /* This is the table of extensions that the loader will dlsym() for. */
697 PUBLIC
const __DRIextension
*__driDriverExtensions
[] = {
698 &driCoreExtension
.base
,
699 &driDRI2Extension
.base
,