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"
38 PUBLIC
const char __driConfigOptions
[] =
40 DRI_CONF_SECTION_PERFORMANCE
41 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_ALWAYS_SYNC
)
42 /* Options correspond to DRI_CONF_BO_REUSE_DISABLED,
43 * DRI_CONF_BO_REUSE_ALL
45 DRI_CONF_OPT_BEGIN_V(bo_reuse
, enum, 1, "0:1")
46 DRI_CONF_DESC_BEGIN(en
, "Buffer object reuse")
47 DRI_CONF_ENUM(0, "Disable buffer object reuse")
48 DRI_CONF_ENUM(1, "Enable reuse of all sizes of buffer objects")
52 DRI_CONF_OPT_BEGIN(texture_tiling
, bool, true)
53 DRI_CONF_DESC(en
, "Enable texture tiling")
56 DRI_CONF_OPT_BEGIN(early_z
, bool, false)
57 DRI_CONF_DESC(en
, "Enable early Z in classic mode (unstable, 945-only).")
60 DRI_CONF_OPT_BEGIN(fragment_shader
, bool, true)
61 DRI_CONF_DESC(en
, "Enable limited ARB_fragment_shader support on 915/945.")
65 DRI_CONF_SECTION_QUALITY
66 DRI_CONF_FORCE_S3TC_ENABLE(false)
67 DRI_CONF_ALLOW_LARGE_TEXTURES(2)
69 DRI_CONF_SECTION_DEBUG
70 DRI_CONF_NO_RAST(false)
71 DRI_CONF_ALWAYS_FLUSH_BATCH(false)
72 DRI_CONF_ALWAYS_FLUSH_CACHE(false)
74 DRI_CONF_OPT_BEGIN(stub_occlusion_query
, bool, false)
75 DRI_CONF_DESC(en
, "Enable stub ARB_occlusion_query support on 915/945.")
80 const GLuint __driNConfigOptions
= 11;
82 #include "intel_batchbuffer.h"
83 #include "intel_buffers.h"
84 #include "intel_bufmgr.h"
85 #include "intel_chipset.h"
86 #include "intel_fbo.h"
87 #include "intel_screen.h"
88 #include "intel_tex.h"
89 #include "intel_regions.h"
93 #ifdef USE_NEW_INTERFACE
94 static PFNGLXCREATECONTEXTMODES create_context_modes
= NULL
;
95 #endif /*USE_NEW_INTERFACE */
97 static const __DRItexBufferExtension intelTexBufferExtension
= {
98 { __DRI_TEX_BUFFER
, __DRI_TEX_BUFFER_VERSION
},
104 intelDRI2Flush(__DRIdrawable
*drawable
)
106 struct intel_context
*intel
= drawable
->driContextPriv
->driverPrivate
;
109 INTEL_FIREVERTICES(intel
);
111 intel
->need_throttle
= GL_TRUE
;
113 if (intel
->batch
->map
!= intel
->batch
->ptr
)
114 intel_batchbuffer_flush(intel
->batch
);
117 static const struct __DRI2flushExtensionRec intelFlushExtension
= {
118 { __DRI2_FLUSH
, __DRI2_FLUSH_VERSION
},
120 dri2InvalidateDrawable
,
124 intel_create_image_from_name(__DRIscreen
*screen
,
125 int width
, int height
, int format
,
126 int name
, int pitch
, void *loaderPrivate
)
128 struct intel_screen
*intelScreen
= screen
->private;
132 image
= CALLOC(sizeof *image
);
137 case __DRI_IMAGE_FORMAT_RGB565
:
138 image
->format
= MESA_FORMAT_RGB565
;
139 image
->internal_format
= GL_RGB
;
140 image
->data_type
= GL_UNSIGNED_BYTE
;
142 case __DRI_IMAGE_FORMAT_XRGB8888
:
143 image
->format
= MESA_FORMAT_XRGB8888
;
144 image
->internal_format
= GL_RGB
;
145 image
->data_type
= GL_UNSIGNED_BYTE
;
147 case __DRI_IMAGE_FORMAT_ARGB8888
:
148 image
->format
= MESA_FORMAT_ARGB8888
;
149 image
->internal_format
= GL_RGBA
;
150 image
->data_type
= GL_UNSIGNED_BYTE
;
157 image
->data
= loaderPrivate
;
158 cpp
= _mesa_get_format_bytes(image
->format
);
160 image
->region
= intel_region_alloc_for_handle(intelScreen
,
162 pitch
, name
, "image");
163 if (image
->region
== NULL
) {
172 intel_create_image_from_renderbuffer(__DRIcontext
*context
,
173 int renderbuffer
, void *loaderPrivate
)
176 struct intel_context
*intel
= context
->driverPrivate
;
177 struct gl_renderbuffer
*rb
;
178 struct intel_renderbuffer
*irb
;
180 rb
= _mesa_lookup_renderbuffer(&intel
->ctx
, renderbuffer
);
182 _mesa_error(&intel
->ctx
,
183 GL_INVALID_OPERATION
, "glRenderbufferExternalMESA");
187 irb
= intel_renderbuffer(rb
);
188 image
= CALLOC(sizeof *image
);
192 image
->internal_format
= rb
->InternalFormat
;
193 image
->format
= rb
->Format
;
194 image
->data_type
= rb
->DataType
;
195 image
->data
= loaderPrivate
;
196 intel_region_reference(&image
->region
, irb
->region
);
202 intel_destroy_image(__DRIimage
*image
)
204 intel_region_release(&image
->region
);
209 intel_create_image(__DRIscreen
*screen
,
210 int width
, int height
, int format
,
215 struct intel_screen
*intelScreen
= screen
->private;
218 image
= CALLOC(sizeof *image
);
223 case __DRI_IMAGE_FORMAT_RGB565
:
224 image
->format
= MESA_FORMAT_RGB565
;
225 image
->internal_format
= GL_RGB
;
226 image
->data_type
= GL_UNSIGNED_BYTE
;
228 case __DRI_IMAGE_FORMAT_XRGB8888
:
229 image
->format
= MESA_FORMAT_XRGB8888
;
230 image
->internal_format
= GL_RGB
;
231 image
->data_type
= GL_UNSIGNED_BYTE
;
233 case __DRI_IMAGE_FORMAT_ARGB8888
:
234 image
->format
= MESA_FORMAT_ARGB8888
;
235 image
->internal_format
= GL_RGBA
;
236 image
->data_type
= GL_UNSIGNED_BYTE
;
243 image
->data
= loaderPrivate
;
244 cpp
= _mesa_get_format_bytes(image
->format
);
247 intel_region_alloc(intelScreen
, I915_TILING_NONE
,
248 cpp
, width
, height
, GL_TRUE
);
249 if (image
->region
== NULL
) {
258 intel_query_image(__DRIimage
*image
, int attrib
, int *value
)
261 case __DRI_IMAGE_ATTRIB_STRIDE
:
262 *value
= image
->region
->pitch
* image
->region
->cpp
;
264 case __DRI_IMAGE_ATTRIB_HANDLE
:
265 *value
= image
->region
->buffer
->handle
;
267 case __DRI_IMAGE_ATTRIB_NAME
:
268 return intel_region_flink(image
->region
, (uint32_t *) value
);
274 static struct __DRIimageExtensionRec intelImageExtension
= {
275 { __DRI_IMAGE
, __DRI_IMAGE_VERSION
},
276 intel_create_image_from_name
,
277 intel_create_image_from_renderbuffer
,
283 static const __DRIextension
*intelScreenExtensions
[] = {
284 &driReadDrawableExtension
,
285 &intelTexBufferExtension
.base
,
286 &intelFlushExtension
.base
,
287 &intelImageExtension
.base
,
288 &dri2ConfigQueryExtension
.base
,
293 intel_get_param(__DRIscreen
*psp
, int param
, int *value
)
296 struct drm_i915_getparam gp
;
301 ret
= drmCommandWriteRead(psp
->fd
, DRM_I915_GETPARAM
, &gp
, sizeof(gp
));
303 _mesa_warning(NULL
, "drm_i915_getparam: %d", ret
);
311 nop_callback(GLuint key
, void *data
, void *userData
)
316 intelDestroyScreen(__DRIscreen
* sPriv
)
318 struct intel_screen
*intelScreen
= sPriv
->private;
320 dri_bufmgr_destroy(intelScreen
->bufmgr
);
321 driDestroyOptionInfo(&intelScreen
->optionCache
);
323 /* Some regions may still have references to them at this point, so
324 * flush the hash table to prevent _mesa_DeleteHashTable() from
325 * complaining about the hash not being empty; */
326 _mesa_HashDeleteAll(intelScreen
->named_regions
, nop_callback
, NULL
);
327 _mesa_DeleteHashTable(intelScreen
->named_regions
);
330 sPriv
->private = NULL
;
335 * This is called when we need to set up GL rendering to a new X window.
338 intelCreateBuffer(__DRIscreen
* driScrnPriv
,
339 __DRIdrawable
* driDrawPriv
,
340 const struct gl_config
* mesaVis
, GLboolean isPixmap
)
342 struct intel_renderbuffer
*rb
;
345 return GL_FALSE
; /* not implemented */
348 GLboolean swStencil
= (mesaVis
->stencilBits
> 0 &&
349 mesaVis
->depthBits
!= 24);
352 struct gl_framebuffer
*fb
= CALLOC_STRUCT(gl_framebuffer
);
357 _mesa_initialize_window_framebuffer(fb
, mesaVis
);
359 if (mesaVis
->redBits
== 5)
360 rgbFormat
= MESA_FORMAT_RGB565
;
361 else if (mesaVis
->alphaBits
== 0)
362 rgbFormat
= MESA_FORMAT_XRGB8888
;
364 rgbFormat
= MESA_FORMAT_ARGB8888
;
366 /* setup the hardware-based renderbuffers */
367 rb
= intel_create_renderbuffer(rgbFormat
);
368 _mesa_add_renderbuffer(fb
, BUFFER_FRONT_LEFT
, &rb
->Base
);
370 if (mesaVis
->doubleBufferMode
) {
371 rb
= intel_create_renderbuffer(rgbFormat
);
372 _mesa_add_renderbuffer(fb
, BUFFER_BACK_LEFT
, &rb
->Base
);
375 if (mesaVis
->depthBits
== 24) {
376 assert(mesaVis
->stencilBits
== 8);
377 /* combined depth/stencil buffer */
378 struct intel_renderbuffer
*depthStencilRb
379 = intel_create_renderbuffer(MESA_FORMAT_S8_Z24
);
380 /* note: bind RB to two attachment points */
381 _mesa_add_renderbuffer(fb
, BUFFER_DEPTH
, &depthStencilRb
->Base
);
382 _mesa_add_renderbuffer(fb
, BUFFER_STENCIL
, &depthStencilRb
->Base
);
384 else if (mesaVis
->depthBits
== 16) {
385 /* just 16-bit depth buffer, no hw stencil */
386 struct intel_renderbuffer
*depthRb
387 = intel_create_renderbuffer(MESA_FORMAT_Z16
);
388 _mesa_add_renderbuffer(fb
, BUFFER_DEPTH
, &depthRb
->Base
);
391 /* now add any/all software-based renderbuffers we may need */
392 _mesa_add_soft_renderbuffers(fb
,
393 GL_FALSE
, /* never sw color */
394 GL_FALSE
, /* never sw depth */
395 swStencil
, mesaVis
->accumRedBits
> 0,
396 GL_FALSE
, /* never sw alpha */
397 GL_FALSE
/* never sw aux */ );
398 driDrawPriv
->driverPrivate
= fb
;
405 intelDestroyBuffer(__DRIdrawable
* driDrawPriv
)
407 struct gl_framebuffer
*fb
= driDrawPriv
->driverPrivate
;
409 _mesa_reference_framebuffer(&fb
, NULL
);
412 /* There are probably better ways to do this, such as an
413 * init-designated function to register chipids and createcontext
416 extern GLboolean
i830CreateContext(const struct gl_config
* mesaVis
,
417 __DRIcontext
* driContextPriv
,
418 void *sharedContextPrivate
);
420 extern GLboolean
i915CreateContext(int api
,
421 const struct gl_config
* mesaVis
,
422 __DRIcontext
* driContextPriv
,
423 void *sharedContextPrivate
);
424 extern GLboolean
brwCreateContext(int api
,
425 const struct gl_config
* mesaVis
,
426 __DRIcontext
* driContextPriv
,
427 void *sharedContextPrivate
);
430 intelCreateContext(gl_api api
,
431 const struct gl_config
* mesaVis
,
432 __DRIcontext
* driContextPriv
,
433 void *sharedContextPrivate
)
435 __DRIscreen
*sPriv
= driContextPriv
->driScreenPriv
;
436 struct intel_screen
*intelScreen
= sPriv
->private;
439 if (IS_9XX(intelScreen
->deviceID
)) {
440 if (!IS_965(intelScreen
->deviceID
)) {
441 return i915CreateContext(api
, mesaVis
, driContextPriv
,
442 sharedContextPrivate
);
445 intelScreen
->no_vbo
= GL_TRUE
;
446 return i830CreateContext(mesaVis
, driContextPriv
, sharedContextPrivate
);
449 if (IS_965(intelScreen
->deviceID
))
450 return brwCreateContext(api
, mesaVis
,
451 driContextPriv
, sharedContextPrivate
);
453 fprintf(stderr
, "Unrecognized deviceID 0x%x\n", intelScreen
->deviceID
);
458 intel_init_bufmgr(struct intel_screen
*intelScreen
)
460 __DRIscreen
*spriv
= intelScreen
->driScrnPriv
;
463 intelScreen
->no_hw
= (getenv("INTEL_NO_HW") != NULL
||
464 getenv("INTEL_DEVID_OVERRIDE") != NULL
);
466 intelScreen
->bufmgr
= intel_bufmgr_gem_init(spriv
->fd
, BATCH_SZ
);
467 if (intelScreen
->bufmgr
== NULL
) {
468 fprintf(stderr
, "[%s:%u] Error initializing buffer manager.\n",
473 if (!intel_get_param(spriv
, I915_PARAM_NUM_FENCES_AVAIL
, &num_fences
) ||
475 fprintf(stderr
, "[%s: %u] Kernel 2.6.29 required.\n", __func__
, __LINE__
);
479 drm_intel_bufmgr_gem_enable_fenced_relocs(intelScreen
->bufmgr
);
481 intelScreen
->named_regions
= _mesa_NewHashTable();
487 * This is the driver specific part of the createNewScreen entry point.
488 * Called when using DRI2.
490 * \return the struct gl_config supported by this driver
493 __DRIconfig
**intelInitScreen2(__DRIscreen
*psp
)
495 struct intel_screen
*intelScreen
;
498 unsigned int api_mask
;
499 char *devid_override
;
501 static const GLenum back_buffer_modes
[] = {
502 GLX_NONE
, GLX_SWAP_UNDEFINED_OML
, GLX_SWAP_COPY_OML
504 uint8_t depth_bits
[4], stencil_bits
[4], msaa_samples_array
[1];
506 __DRIconfig
**configs
= NULL
;
508 /* Allocate the private area */
509 intelScreen
= CALLOC(sizeof *intelScreen
);
511 fprintf(stderr
, "\nERROR! Allocating private area failed\n");
514 /* parse information in __driConfigOptions */
515 driParseOptionInfo(&intelScreen
->optionCache
,
516 __driConfigOptions
, __driNConfigOptions
);
518 intelScreen
->driScrnPriv
= psp
;
519 psp
->private = (void *) intelScreen
;
521 /* Determine chipset ID */
522 if (!intel_get_param(psp
, I915_PARAM_CHIPSET_ID
,
523 &intelScreen
->deviceID
))
526 /* Allow an override of the device ID for the purpose of making the
527 * driver produce dumps for debugging of new chipset enablement.
528 * This implies INTEL_NO_HW, to avoid programming your actual GPU
531 devid_override
= getenv("INTEL_DEVID_OVERRIDE");
532 if (devid_override
) {
533 intelScreen
->deviceID
= strtod(devid_override
, NULL
);
536 api_mask
= (1 << __DRI_API_OPENGL
);
538 api_mask
|= (1 << __DRI_API_GLES
);
541 api_mask
|= (1 << __DRI_API_GLES2
);
544 if (IS_9XX(intelScreen
->deviceID
) || IS_965(intelScreen
->deviceID
))
545 psp
->api_mask
= api_mask
;
547 if (!intel_init_bufmgr(intelScreen
))
550 psp
->extensions
= intelScreenExtensions
;
552 msaa_samples_array
[0] = 0;
554 fb_format
[0] = GL_RGB
;
555 fb_type
[0] = GL_UNSIGNED_SHORT_5_6_5
;
557 fb_format
[1] = GL_BGR
;
558 fb_type
[1] = GL_UNSIGNED_INT_8_8_8_8_REV
;
560 fb_format
[2] = GL_BGRA
;
561 fb_type
[2] = GL_UNSIGNED_INT_8_8_8_8_REV
;
566 /* Generate a rich set of useful configs that do not include an
567 * accumulation buffer.
569 for (color
= 0; color
< ARRAY_SIZE(fb_format
); color
++) {
570 __DRIconfig
**new_configs
;
573 /* Starting with DRI2 protocol version 1.1 we can request a depth/stencil
574 * buffer that has a diffferent number of bits per pixel than the color
575 * buffer. This isn't yet supported here.
577 if (fb_type
[color
] == GL_UNSIGNED_SHORT_5_6_5
) {
587 new_configs
= driCreateConfigs(fb_format
[color
], fb_type
[color
],
592 ARRAY_SIZE(back_buffer_modes
),
594 ARRAY_SIZE(msaa_samples_array
),
597 configs
= new_configs
;
599 configs
= driConcatConfigs(configs
, new_configs
);
602 /* Generate the minimum possible set of configs that include an
603 * accumulation buffer.
605 for (color
= 0; color
< ARRAY_SIZE(fb_format
); color
++) {
606 __DRIconfig
**new_configs
;
608 if (fb_type
[color
] == GL_UNSIGNED_SHORT_5_6_5
) {
616 new_configs
= driCreateConfigs(fb_format
[color
], fb_type
[color
],
617 depth_bits
, stencil_bits
, 1,
618 back_buffer_modes
+ 1, 1,
619 msaa_samples_array
, 1,
622 configs
= new_configs
;
624 configs
= driConcatConfigs(configs
, new_configs
);
627 if (configs
== NULL
) {
628 fprintf(stderr
, "[%s:%u] Error creating FBConfig!\n", __func__
,
633 return (const __DRIconfig
**)configs
;
636 const struct __DriverAPIRec driDriverAPI
= {
637 .DestroyScreen
= intelDestroyScreen
,
638 .CreateContext
= intelCreateContext
,
639 .DestroyContext
= intelDestroyContext
,
640 .CreateBuffer
= intelCreateBuffer
,
641 .DestroyBuffer
= intelDestroyBuffer
,
642 .MakeCurrent
= intelMakeCurrent
,
643 .UnbindContext
= intelUnbindContext
,
644 .InitScreen2
= intelInitScreen2
,
647 /* This is the table of extensions that the loader will dlsym() for. */
648 PUBLIC
const __DRIextension
*__driDriverExtensions
[] = {
649 &driCoreExtension
.base
,
650 &driDRI2Extension
.base
,