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 #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"
49 #define DRI_CONF_TEXTURE_TILING(def) \
51 PUBLIC const char __driConfigOptions[] =
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
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")
65 DRI_CONF_OPT_BEGIN(texture_tiling
, bool, true)
66 DRI_CONF_DESC(en
, "Enable texture tiling")
69 DRI_CONF_OPT_BEGIN(early_z
, bool, false)
70 DRI_CONF_DESC(en
, "Enable early Z in classic mode (unstable, 945-only).")
73 DRI_CONF_OPT_BEGIN(fragment_shader
, bool, false)
74 DRI_CONF_DESC(en
, "Enable limited ARB_fragment_shader support on 915/945.")
78 DRI_CONF_SECTION_QUALITY
79 DRI_CONF_FORCE_S3TC_ENABLE(false)
80 DRI_CONF_ALLOW_LARGE_TEXTURES(2)
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)
87 DRI_CONF_OPT_BEGIN(stub_occlusion_query
, bool, false)
88 DRI_CONF_DESC(en
, "Enable stub ARB_occlusion_query support on 915/945.")
93 const GLuint __driNConfigOptions
= 11;
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 struct intel_context
*intel
= drawable
->driContextPriv
->driverPrivate
;
111 INTEL_FIREVERTICES(intel
);
113 intel
->need_throttle
= GL_TRUE
;
115 if (intel
->batch
->map
!= intel
->batch
->ptr
)
116 intel_batchbuffer_flush(intel
->batch
);
119 static const struct __DRI2flushExtensionRec intelFlushExtension
= {
120 { __DRI2_FLUSH
, __DRI2_FLUSH_VERSION
},
122 dri2InvalidateDrawable
,
126 intel_create_image_from_name(__DRIcontext
*context
,
127 int width
, int height
, int format
,
128 int name
, int pitch
, void *loaderPrivate
)
131 struct intel_context
*intel
= context
->driverPrivate
;
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(intel
, cpp
, width
, height
,
163 pitch
, name
, "image");
164 if (image
->region
== NULL
) {
173 intel_create_image_from_renderbuffer(__DRIcontext
*context
,
174 int renderbuffer
, void *loaderPrivate
)
177 struct intel_context
*intel
= context
->driverPrivate
;
178 struct gl_renderbuffer
*rb
;
179 struct intel_renderbuffer
*irb
;
181 rb
= _mesa_lookup_renderbuffer(&intel
->ctx
, renderbuffer
);
183 _mesa_error(&intel
->ctx
,
184 GL_INVALID_OPERATION
, "glRenderbufferExternalMESA");
188 irb
= intel_renderbuffer(rb
);
189 image
= CALLOC(sizeof *image
);
193 image
->internal_format
= rb
->InternalFormat
;
194 image
->format
= rb
->Format
;
195 image
->data_type
= rb
->DataType
;
196 image
->data
= loaderPrivate
;
197 intel_region_reference(&image
->region
, irb
->region
);
203 intel_destroy_image(__DRIimage
*image
)
205 intel_region_release(&image
->region
);
209 static struct __DRIimageExtensionRec intelImageExtension
= {
210 { __DRI_IMAGE
, __DRI_IMAGE_VERSION
},
211 intel_create_image_from_name
,
212 intel_create_image_from_renderbuffer
,
216 static const __DRIextension
*intelScreenExtensions
[] = {
217 &driReadDrawableExtension
,
218 &intelTexBufferExtension
.base
,
219 &intelFlushExtension
.base
,
220 &intelImageExtension
.base
,
221 &dri2ConfigQueryExtension
.base
,
226 intel_get_param(__DRIscreen
*psp
, int param
, int *value
)
229 struct drm_i915_getparam gp
;
234 ret
= drmCommandWriteRead(psp
->fd
, DRM_I915_GETPARAM
, &gp
, sizeof(gp
));
236 _mesa_warning(NULL
, "drm_i915_getparam: %d", ret
);
244 nop_callback(GLuint key
, void *data
, void *userData
)
249 intelDestroyScreen(__DRIscreen
* sPriv
)
251 struct intel_screen
*intelScreen
= sPriv
->private;
253 dri_bufmgr_destroy(intelScreen
->bufmgr
);
254 driDestroyOptionInfo(&intelScreen
->optionCache
);
256 /* Some regions may still have references to them at this point, so
257 * flush the hash table to prevent _mesa_DeleteHashTable() from
258 * complaining about the hash not being empty; */
259 _mesa_HashDeleteAll(intelScreen
->named_regions
, nop_callback
, NULL
);
260 _mesa_DeleteHashTable(intelScreen
->named_regions
);
263 sPriv
->private = NULL
;
268 * This is called when we need to set up GL rendering to a new X window.
271 intelCreateBuffer(__DRIscreen
* driScrnPriv
,
272 __DRIdrawable
* driDrawPriv
,
273 const __GLcontextModes
* mesaVis
, GLboolean isPixmap
)
275 struct intel_renderbuffer
*rb
;
278 return GL_FALSE
; /* not implemented */
281 GLboolean swStencil
= (mesaVis
->stencilBits
> 0 &&
282 mesaVis
->depthBits
!= 24);
285 struct gl_framebuffer
*fb
= CALLOC_STRUCT(gl_framebuffer
);
290 _mesa_initialize_window_framebuffer(fb
, mesaVis
);
292 if (mesaVis
->redBits
== 5)
293 rgbFormat
= MESA_FORMAT_RGB565
;
294 else if (mesaVis
->alphaBits
== 0)
295 rgbFormat
= MESA_FORMAT_XRGB8888
;
297 rgbFormat
= MESA_FORMAT_ARGB8888
;
299 /* setup the hardware-based renderbuffers */
300 rb
= intel_create_renderbuffer(rgbFormat
);
301 _mesa_add_renderbuffer(fb
, BUFFER_FRONT_LEFT
, &rb
->Base
);
303 if (mesaVis
->doubleBufferMode
) {
304 rb
= intel_create_renderbuffer(rgbFormat
);
305 _mesa_add_renderbuffer(fb
, BUFFER_BACK_LEFT
, &rb
->Base
);
308 if (mesaVis
->depthBits
== 24) {
309 assert(mesaVis
->stencilBits
== 8);
310 /* combined depth/stencil buffer */
311 struct intel_renderbuffer
*depthStencilRb
312 = intel_create_renderbuffer(MESA_FORMAT_S8_Z24
);
313 /* note: bind RB to two attachment points */
314 _mesa_add_renderbuffer(fb
, BUFFER_DEPTH
, &depthStencilRb
->Base
);
315 _mesa_add_renderbuffer(fb
, BUFFER_STENCIL
, &depthStencilRb
->Base
);
317 else if (mesaVis
->depthBits
== 16) {
318 /* just 16-bit depth buffer, no hw stencil */
319 struct intel_renderbuffer
*depthRb
320 = intel_create_renderbuffer(MESA_FORMAT_Z16
);
321 _mesa_add_renderbuffer(fb
, BUFFER_DEPTH
, &depthRb
->Base
);
324 /* now add any/all software-based renderbuffers we may need */
325 _mesa_add_soft_renderbuffers(fb
,
326 GL_FALSE
, /* never sw color */
327 GL_FALSE
, /* never sw depth */
328 swStencil
, mesaVis
->accumRedBits
> 0,
329 GL_FALSE
, /* never sw alpha */
330 GL_FALSE
/* never sw aux */ );
331 driDrawPriv
->driverPrivate
= fb
;
338 intelDestroyBuffer(__DRIdrawable
* driDrawPriv
)
340 struct gl_framebuffer
*fb
= driDrawPriv
->driverPrivate
;
342 _mesa_reference_framebuffer(&fb
, NULL
);
345 /* There are probably better ways to do this, such as an
346 * init-designated function to register chipids and createcontext
349 extern GLboolean
i830CreateContext(const __GLcontextModes
* mesaVis
,
350 __DRIcontext
* driContextPriv
,
351 void *sharedContextPrivate
);
353 extern GLboolean
i915CreateContext(int api
,
354 const __GLcontextModes
* mesaVis
,
355 __DRIcontext
* driContextPriv
,
356 void *sharedContextPrivate
);
357 extern GLboolean
brwCreateContext(int api
,
358 const __GLcontextModes
* mesaVis
,
359 __DRIcontext
* driContextPriv
,
360 void *sharedContextPrivate
);
363 intelCreateContext(gl_api api
,
364 const __GLcontextModes
* mesaVis
,
365 __DRIcontext
* driContextPriv
,
366 void *sharedContextPrivate
)
368 __DRIscreen
*sPriv
= driContextPriv
->driScreenPriv
;
369 struct intel_screen
*intelScreen
= sPriv
->private;
372 if (IS_9XX(intelScreen
->deviceID
)) {
373 if (!IS_965(intelScreen
->deviceID
)) {
374 return i915CreateContext(api
, mesaVis
, driContextPriv
,
375 sharedContextPrivate
);
378 intelScreen
->no_vbo
= GL_TRUE
;
379 return i830CreateContext(mesaVis
, driContextPriv
, sharedContextPrivate
);
382 if (IS_965(intelScreen
->deviceID
))
383 return brwCreateContext(api
, mesaVis
,
384 driContextPriv
, sharedContextPrivate
);
386 fprintf(stderr
, "Unrecognized deviceID %x\n", intelScreen
->deviceID
);
391 intel_init_bufmgr(struct intel_screen
*intelScreen
)
393 __DRIscreen
*spriv
= intelScreen
->driScrnPriv
;
396 intelScreen
->no_hw
= getenv("INTEL_NO_HW") != NULL
;
398 intelScreen
->bufmgr
= intel_bufmgr_gem_init(spriv
->fd
, BATCH_SZ
);
399 /* Otherwise, use the classic buffer manager. */
400 if (intelScreen
->bufmgr
== NULL
) {
401 fprintf(stderr
, "[%s:%u] Error initializing buffer manager.\n",
406 if (!intel_get_param(spriv
, I915_PARAM_NUM_FENCES_AVAIL
, &num_fences
) ||
408 fprintf(stderr
, "[%s: %u] Kernel 2.6.29 required.\n", __func__
, __LINE__
);
412 drm_intel_bufmgr_gem_enable_fenced_relocs(intelScreen
->bufmgr
);
414 intelScreen
->named_regions
= _mesa_NewHashTable();
420 * This is the driver specific part of the createNewScreen entry point.
421 * Called when using DRI2.
423 * \return the __GLcontextModes supported by this driver
426 __DRIconfig
**intelInitScreen2(__DRIscreen
*psp
)
428 struct intel_screen
*intelScreen
;
431 unsigned int api_mask
;
433 static const GLenum back_buffer_modes
[] = {
434 GLX_NONE
, GLX_SWAP_UNDEFINED_OML
, GLX_SWAP_COPY_OML
436 uint8_t depth_bits
[4], stencil_bits
[4], msaa_samples_array
[1];
438 __DRIconfig
**configs
= NULL
;
440 /* Allocate the private area */
441 intelScreen
= CALLOC(sizeof *intelScreen
);
443 fprintf(stderr
, "\nERROR! Allocating private area failed\n");
446 /* parse information in __driConfigOptions */
447 driParseOptionInfo(&intelScreen
->optionCache
,
448 __driConfigOptions
, __driNConfigOptions
);
450 intelScreen
->driScrnPriv
= psp
;
451 psp
->private = (void *) intelScreen
;
453 /* Determine chipset ID */
454 if (!intel_get_param(psp
, I915_PARAM_CHIPSET_ID
,
455 &intelScreen
->deviceID
))
458 api_mask
= (1 << __DRI_API_OPENGL
);
460 api_mask
|= (1 << __DRI_API_GLES
);
463 api_mask
|= (1 << __DRI_API_GLES2
);
466 if (IS_9XX(intelScreen
->deviceID
) || IS_965(intelScreen
->deviceID
))
467 psp
->api_mask
= api_mask
;
469 if (!intel_init_bufmgr(intelScreen
))
472 psp
->extensions
= intelScreenExtensions
;
474 msaa_samples_array
[0] = 0;
476 fb_format
[0] = GL_RGB
;
477 fb_type
[0] = GL_UNSIGNED_SHORT_5_6_5
;
479 fb_format
[1] = GL_BGR
;
480 fb_type
[1] = GL_UNSIGNED_INT_8_8_8_8_REV
;
482 fb_format
[2] = GL_BGRA
;
483 fb_type
[2] = GL_UNSIGNED_INT_8_8_8_8_REV
;
488 /* Generate a rich set of useful configs that do not include an
489 * accumulation buffer.
491 for (color
= 0; color
< ARRAY_SIZE(fb_format
); color
++) {
492 __DRIconfig
**new_configs
;
495 /* Starting with DRI2 protocol version 1.1 we can request a depth/stencil
496 * buffer that has a diffferent number of bits per pixel than the color
497 * buffer. This isn't yet supported here.
499 if (fb_type
[color
] == GL_UNSIGNED_SHORT_5_6_5
) {
509 new_configs
= driCreateConfigs(fb_format
[color
], fb_type
[color
],
514 ARRAY_SIZE(back_buffer_modes
),
516 ARRAY_SIZE(msaa_samples_array
),
519 configs
= new_configs
;
521 configs
= driConcatConfigs(configs
, new_configs
);
524 /* Generate the minimum possible set of configs that include an
525 * accumulation buffer.
527 for (color
= 0; color
< ARRAY_SIZE(fb_format
); color
++) {
528 __DRIconfig
**new_configs
;
530 if (fb_type
[color
] == GL_UNSIGNED_SHORT_5_6_5
) {
538 new_configs
= driCreateConfigs(fb_format
[color
], fb_type
[color
],
539 depth_bits
, stencil_bits
, 1,
540 back_buffer_modes
+ 1, 1,
541 msaa_samples_array
, 1,
544 configs
= new_configs
;
546 configs
= driConcatConfigs(configs
, new_configs
);
549 if (configs
== NULL
) {
550 fprintf(stderr
, "[%s:%u] Error creating FBConfig!\n", __func__
,
555 return (const __DRIconfig
**)configs
;
558 const struct __DriverAPIRec driDriverAPI
= {
559 .DestroyScreen
= intelDestroyScreen
,
560 .CreateContext
= intelCreateContext
,
561 .DestroyContext
= intelDestroyContext
,
562 .CreateBuffer
= intelCreateBuffer
,
563 .DestroyBuffer
= intelDestroyBuffer
,
564 .MakeCurrent
= intelMakeCurrent
,
565 .UnbindContext
= intelUnbindContext
,
566 .InitScreen2
= intelInitScreen2
,
569 /* This is the table of extensions that the loader will dlsym() for. */
570 PUBLIC
const __DRIextension
*__driDriverExtensions
[] = {
571 &driCoreExtension
.base
,
572 &driDRI2Extension
.base
,