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 **************************************************************************/
31 #include "main/glheader.h"
32 #include "main/context.h"
33 #include "main/framebuffer.h"
34 #include "main/renderbuffer.h"
35 #include "main/texobj.h"
36 #include "main/hash.h"
37 #include "main/fbobject.h"
38 #include "main/version.h"
39 #include "swrast/s_renderbuffer.h"
44 static const __DRIconfigOptionsExtension brw_config_options
= {
45 .base
= { __DRI_CONFIG_OPTIONS
, 1 },
48 DRI_CONF_SECTION_PERFORMANCE
49 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_ALWAYS_SYNC
)
50 /* Options correspond to DRI_CONF_BO_REUSE_DISABLED,
51 * DRI_CONF_BO_REUSE_ALL
53 DRI_CONF_OPT_BEGIN_V(bo_reuse
, enum, 1, "0:1")
54 DRI_CONF_DESC_BEGIN(en
, "Buffer object reuse")
55 DRI_CONF_ENUM(0, "Disable buffer object reuse")
56 DRI_CONF_ENUM(1, "Enable reuse of all sizes of buffer objects")
60 DRI_CONF_OPT_BEGIN_B(hiz
, "true")
61 DRI_CONF_DESC(en
, "Enable Hierarchical Z on gen6+")
64 DRI_CONF_OPT_BEGIN_B(disable_derivative_optimization
, "false")
65 DRI_CONF_DESC(en
, "Derivatives with finer granularity by default")
69 DRI_CONF_SECTION_QUALITY
70 DRI_CONF_FORCE_S3TC_ENABLE("false")
72 DRI_CONF_OPT_BEGIN(clamp_max_samples
, int, -1)
73 DRI_CONF_DESC(en
, "Clamp the value of GL_MAX_SAMPLES to the "
74 "given integer. If negative, then do not clamp.")
78 DRI_CONF_SECTION_DEBUG
79 DRI_CONF_NO_RAST("false")
80 DRI_CONF_ALWAYS_FLUSH_BATCH("false")
81 DRI_CONF_ALWAYS_FLUSH_CACHE("false")
82 DRI_CONF_DISABLE_THROTTLING("false")
83 DRI_CONF_FORCE_GLSL_EXTENSIONS_WARN("false")
84 DRI_CONF_DISABLE_GLSL_LINE_CONTINUATIONS("false")
85 DRI_CONF_DISABLE_BLEND_FUNC_EXTENDED("false")
87 DRI_CONF_OPT_BEGIN_B(shader_precompile
, "true")
88 DRI_CONF_DESC(en
, "Perform code generation at shader link time.")
94 #include "intel_batchbuffer.h"
95 #include "intel_buffers.h"
96 #include "intel_bufmgr.h"
97 #include "intel_chipset.h"
98 #include "intel_fbo.h"
99 #include "intel_mipmap_tree.h"
100 #include "intel_screen.h"
101 #include "intel_tex.h"
102 #include "intel_regions.h"
104 #include "brw_context.h"
106 #include "i915_drm.h"
108 #ifdef USE_NEW_INTERFACE
109 static PFNGLXCREATECONTEXTMODES create_context_modes
= NULL
;
110 #endif /*USE_NEW_INTERFACE */
113 * For debugging purposes, this returns a time in seconds.
120 clock_gettime(CLOCK_MONOTONIC
, &tp
);
122 return tp
.tv_sec
+ tp
.tv_nsec
/ 1000000000.0;
126 aub_dump_bmp(struct gl_context
*ctx
)
128 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
130 for (int i
= 0; i
< fb
->_NumColorDrawBuffers
; i
++) {
131 struct intel_renderbuffer
*irb
=
132 intel_renderbuffer(fb
->_ColorDrawBuffers
[i
]);
134 if (irb
&& irb
->mt
) {
135 enum aub_dump_bmp_format format
;
137 switch (irb
->Base
.Base
.Format
) {
138 case MESA_FORMAT_ARGB8888
:
139 case MESA_FORMAT_XRGB8888
:
140 format
= AUB_DUMP_BMP_FORMAT_ARGB_8888
;
146 assert(irb
->mt
->region
->pitch
% irb
->mt
->region
->cpp
== 0);
147 drm_intel_gem_bo_aub_dump_bmp(irb
->mt
->region
->bo
,
150 irb
->Base
.Base
.Width
,
151 irb
->Base
.Base
.Height
,
153 irb
->mt
->region
->pitch
,
159 static const __DRItexBufferExtension intelTexBufferExtension
= {
160 .base
= { __DRI_TEX_BUFFER
, __DRI_TEX_BUFFER_VERSION
},
162 .setTexBuffer
= intelSetTexBuffer
,
163 .setTexBuffer2
= intelSetTexBuffer2
,
164 .releaseTexBuffer
= NULL
,
168 intel_dri2_flush_with_flags(__DRIcontext
*cPriv
,
169 __DRIdrawable
*dPriv
,
171 enum __DRI2throttleReason reason
)
173 struct brw_context
*brw
= cPriv
->driverPrivate
;
178 struct gl_context
*ctx
= &brw
->ctx
;
180 FLUSH_VERTICES(ctx
, 0);
182 if (flags
& __DRI2_FLUSH_DRAWABLE
)
183 intel_resolve_for_dri2_flush(brw
, dPriv
);
185 if (reason
== __DRI2_THROTTLE_SWAPBUFFER
||
186 reason
== __DRI2_THROTTLE_FLUSHFRONT
) {
187 brw
->need_throttle
= true;
190 intel_batchbuffer_flush(brw
);
192 if (INTEL_DEBUG
& DEBUG_AUB
) {
198 * Provides compatibility with loaders that only support the older (version
199 * 1-3) flush interface.
201 * That includes libGL up to Mesa 9.0, and the X Server at least up to 1.13.
204 intel_dri2_flush(__DRIdrawable
*drawable
)
206 intel_dri2_flush_with_flags(drawable
->driContextPriv
, drawable
,
207 __DRI2_FLUSH_DRAWABLE
,
208 __DRI2_THROTTLE_SWAPBUFFER
);
211 static const struct __DRI2flushExtensionRec intelFlushExtension
= {
212 .base
= { __DRI2_FLUSH
, 4 },
214 .flush
= intel_dri2_flush
,
215 .invalidate
= dri2InvalidateDrawable
,
216 .flush_with_flags
= intel_dri2_flush_with_flags
,
219 static struct intel_image_format intel_image_formats
[] = {
220 { __DRI_IMAGE_FOURCC_ARGB8888
, __DRI_IMAGE_COMPONENTS_RGBA
, 1,
221 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ARGB8888
, 4 } } },
223 { __DRI_IMAGE_FOURCC_XRGB8888
, __DRI_IMAGE_COMPONENTS_RGB
, 1,
224 { { 0, 0, 0, __DRI_IMAGE_FORMAT_XRGB8888
, 4 }, } },
226 { __DRI_IMAGE_FOURCC_RGB565
, __DRI_IMAGE_COMPONENTS_RGB
, 1,
227 { { 0, 0, 0, __DRI_IMAGE_FORMAT_RGB565
, 2 } } },
229 { __DRI_IMAGE_FOURCC_YUV410
, __DRI_IMAGE_COMPONENTS_Y_U_V
, 3,
230 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8
, 1 },
231 { 1, 2, 2, __DRI_IMAGE_FORMAT_R8
, 1 },
232 { 2, 2, 2, __DRI_IMAGE_FORMAT_R8
, 1 } } },
234 { __DRI_IMAGE_FOURCC_YUV411
, __DRI_IMAGE_COMPONENTS_Y_U_V
, 3,
235 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8
, 1 },
236 { 1, 2, 0, __DRI_IMAGE_FORMAT_R8
, 1 },
237 { 2, 2, 0, __DRI_IMAGE_FORMAT_R8
, 1 } } },
239 { __DRI_IMAGE_FOURCC_YUV420
, __DRI_IMAGE_COMPONENTS_Y_U_V
, 3,
240 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8
, 1 },
241 { 1, 1, 1, __DRI_IMAGE_FORMAT_R8
, 1 },
242 { 2, 1, 1, __DRI_IMAGE_FORMAT_R8
, 1 } } },
244 { __DRI_IMAGE_FOURCC_YUV422
, __DRI_IMAGE_COMPONENTS_Y_U_V
, 3,
245 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8
, 1 },
246 { 1, 1, 0, __DRI_IMAGE_FORMAT_R8
, 1 },
247 { 2, 1, 0, __DRI_IMAGE_FORMAT_R8
, 1 } } },
249 { __DRI_IMAGE_FOURCC_YUV444
, __DRI_IMAGE_COMPONENTS_Y_U_V
, 3,
250 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8
, 1 },
251 { 1, 0, 0, __DRI_IMAGE_FORMAT_R8
, 1 },
252 { 2, 0, 0, __DRI_IMAGE_FORMAT_R8
, 1 } } },
254 { __DRI_IMAGE_FOURCC_NV12
, __DRI_IMAGE_COMPONENTS_Y_UV
, 2,
255 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8
, 1 },
256 { 1, 1, 1, __DRI_IMAGE_FORMAT_GR88
, 2 } } },
258 { __DRI_IMAGE_FOURCC_NV16
, __DRI_IMAGE_COMPONENTS_Y_UV
, 2,
259 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8
, 1 },
260 { 1, 1, 0, __DRI_IMAGE_FORMAT_GR88
, 2 } } },
262 /* For YUYV buffers, we set up two overlapping DRI images and treat
263 * them as planar buffers in the compositors. Plane 0 is GR88 and
264 * samples YU or YV pairs and places Y into the R component, while
265 * plane 1 is ARGB and samples YUYV clusters and places pairs and
266 * places U into the G component and V into A. This lets the
267 * texture sampler interpolate the Y components correctly when
268 * sampling from plane 0, and interpolate U and V correctly when
269 * sampling from plane 1. */
270 { __DRI_IMAGE_FOURCC_YUYV
, __DRI_IMAGE_COMPONENTS_Y_XUXV
, 2,
271 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88
, 2 },
272 { 0, 1, 0, __DRI_IMAGE_FORMAT_ARGB8888
, 4 } } }
275 static struct intel_image_format
*
276 intel_image_format_lookup(int fourcc
)
278 struct intel_image_format
*f
= NULL
;
280 for (unsigned i
= 0; i
< ARRAY_SIZE(intel_image_formats
); i
++) {
281 if (intel_image_formats
[i
].fourcc
== fourcc
) {
282 f
= &intel_image_formats
[i
];
291 intel_allocate_image(int dri_format
, void *loaderPrivate
)
295 image
= calloc(1, sizeof *image
);
299 image
->dri_format
= dri_format
;
302 image
->format
= driImageFormatToGLFormat(dri_format
);
303 if (dri_format
!= __DRI_IMAGE_FORMAT_NONE
&&
304 image
->format
== MESA_FORMAT_NONE
) {
309 image
->internal_format
= _mesa_get_format_base_format(image
->format
);
310 image
->data
= loaderPrivate
;
316 * Sets up a DRIImage structure to point to our shared image in a region
319 intel_setup_image_from_mipmap_tree(struct brw_context
*brw
, __DRIimage
*image
,
320 struct intel_mipmap_tree
*mt
, GLuint level
,
323 unsigned int draw_x
, draw_y
;
324 uint32_t mask_x
, mask_y
;
326 intel_miptree_make_shareable(brw
, mt
);
328 intel_miptree_check_level_layer(mt
, level
, zoffset
);
330 intel_region_get_tile_masks(mt
->region
, &mask_x
, &mask_y
, false);
331 intel_miptree_get_image_offset(mt
, level
, zoffset
, &draw_x
, &draw_y
);
333 image
->width
= mt
->level
[level
].width
;
334 image
->height
= mt
->level
[level
].height
;
335 image
->tile_x
= draw_x
& mask_x
;
336 image
->tile_y
= draw_y
& mask_y
;
338 image
->offset
= intel_region_get_aligned_offset(mt
->region
,
343 intel_region_reference(&image
->region
, mt
->region
);
347 intel_setup_image_from_dimensions(__DRIimage
*image
)
349 image
->width
= image
->region
->width
;
350 image
->height
= image
->region
->height
;
353 image
->has_depthstencil
= false;
357 intel_create_image_from_name(__DRIscreen
*screen
,
358 int width
, int height
, int format
,
359 int name
, int pitch
, void *loaderPrivate
)
361 struct intel_screen
*intelScreen
= screen
->driverPrivate
;
365 image
= intel_allocate_image(format
, loaderPrivate
);
369 if (image
->format
== MESA_FORMAT_NONE
)
372 cpp
= _mesa_get_format_bytes(image
->format
);
373 image
->region
= intel_region_alloc_for_handle(intelScreen
,
375 pitch
* cpp
, name
, "image");
376 if (image
->region
== NULL
) {
381 intel_setup_image_from_dimensions(image
);
387 intel_create_image_from_renderbuffer(__DRIcontext
*context
,
388 int renderbuffer
, void *loaderPrivate
)
391 struct brw_context
*brw
= context
->driverPrivate
;
392 struct gl_context
*ctx
= &brw
->ctx
;
393 struct gl_renderbuffer
*rb
;
394 struct intel_renderbuffer
*irb
;
396 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
398 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glRenderbufferExternalMESA");
402 irb
= intel_renderbuffer(rb
);
403 intel_miptree_make_shareable(brw
, irb
->mt
);
404 image
= calloc(1, sizeof *image
);
408 image
->internal_format
= rb
->InternalFormat
;
409 image
->format
= rb
->Format
;
411 image
->data
= loaderPrivate
;
412 intel_region_reference(&image
->region
, irb
->mt
->region
);
413 intel_setup_image_from_dimensions(image
);
414 image
->dri_format
= driGLFormatToImageFormat(image
->format
);
415 image
->has_depthstencil
= irb
->mt
->stencil_mt
? true : false;
417 rb
->NeedsFinishRenderTexture
= true;
422 intel_create_image_from_texture(__DRIcontext
*context
, int target
,
423 unsigned texture
, int zoffset
,
429 struct brw_context
*brw
= context
->driverPrivate
;
430 struct gl_texture_object
*obj
;
431 struct intel_texture_object
*iobj
;
434 obj
= _mesa_lookup_texture(&brw
->ctx
, texture
);
435 if (!obj
|| obj
->Target
!= target
) {
436 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
440 if (target
== GL_TEXTURE_CUBE_MAP
)
443 _mesa_test_texobj_completeness(&brw
->ctx
, obj
);
444 iobj
= intel_texture_object(obj
);
445 if (!obj
->_BaseComplete
|| (level
> 0 && !obj
->_MipmapComplete
)) {
446 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
450 if (level
< obj
->BaseLevel
|| level
> obj
->_MaxLevel
) {
451 *error
= __DRI_IMAGE_ERROR_BAD_MATCH
;
455 if (target
== GL_TEXTURE_3D
&& obj
->Image
[face
][level
]->Depth
< zoffset
) {
456 *error
= __DRI_IMAGE_ERROR_BAD_MATCH
;
459 image
= calloc(1, sizeof *image
);
461 *error
= __DRI_IMAGE_ERROR_BAD_ALLOC
;
465 image
->internal_format
= obj
->Image
[face
][level
]->InternalFormat
;
466 image
->format
= obj
->Image
[face
][level
]->TexFormat
;
467 image
->data
= loaderPrivate
;
468 intel_setup_image_from_mipmap_tree(brw
, image
, iobj
->mt
, level
, zoffset
);
469 image
->dri_format
= driGLFormatToImageFormat(image
->format
);
470 image
->has_depthstencil
= iobj
->mt
->stencil_mt
? true : false;
471 if (image
->dri_format
== MESA_FORMAT_NONE
) {
472 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
477 *error
= __DRI_IMAGE_ERROR_SUCCESS
;
482 intel_destroy_image(__DRIimage
*image
)
484 intel_region_release(&image
->region
);
489 intel_create_image(__DRIscreen
*screen
,
490 int width
, int height
, int format
,
495 struct intel_screen
*intelScreen
= screen
->driverPrivate
;
499 tiling
= I915_TILING_X
;
500 if (use
& __DRI_IMAGE_USE_CURSOR
) {
501 if (width
!= 64 || height
!= 64)
503 tiling
= I915_TILING_NONE
;
506 if (use
& __DRI_IMAGE_USE_LINEAR
)
507 tiling
= I915_TILING_NONE
;
509 image
= intel_allocate_image(format
, loaderPrivate
);
513 cpp
= _mesa_get_format_bytes(image
->format
);
515 intel_region_alloc(intelScreen
, tiling
, cpp
, width
, height
, true);
516 if (image
->region
== NULL
) {
521 intel_setup_image_from_dimensions(image
);
527 intel_query_image(__DRIimage
*image
, int attrib
, int *value
)
530 case __DRI_IMAGE_ATTRIB_STRIDE
:
531 *value
= image
->region
->pitch
;
533 case __DRI_IMAGE_ATTRIB_HANDLE
:
534 *value
= image
->region
->bo
->handle
;
536 case __DRI_IMAGE_ATTRIB_NAME
:
537 return intel_region_flink(image
->region
, (uint32_t *) value
);
538 case __DRI_IMAGE_ATTRIB_FORMAT
:
539 *value
= image
->dri_format
;
541 case __DRI_IMAGE_ATTRIB_WIDTH
:
542 *value
= image
->region
->width
;
544 case __DRI_IMAGE_ATTRIB_HEIGHT
:
545 *value
= image
->region
->height
;
547 case __DRI_IMAGE_ATTRIB_COMPONENTS
:
548 if (image
->planar_format
== NULL
)
550 *value
= image
->planar_format
->components
;
552 case __DRI_IMAGE_ATTRIB_FD
:
553 if (drm_intel_bo_gem_export_to_prime(image
->region
->bo
, value
) == 0)
562 intel_dup_image(__DRIimage
*orig_image
, void *loaderPrivate
)
566 image
= calloc(1, sizeof *image
);
570 intel_region_reference(&image
->region
, orig_image
->region
);
571 if (image
->region
== NULL
) {
576 image
->internal_format
= orig_image
->internal_format
;
577 image
->planar_format
= orig_image
->planar_format
;
578 image
->dri_format
= orig_image
->dri_format
;
579 image
->format
= orig_image
->format
;
580 image
->offset
= orig_image
->offset
;
581 image
->width
= orig_image
->width
;
582 image
->height
= orig_image
->height
;
583 image
->tile_x
= orig_image
->tile_x
;
584 image
->tile_y
= orig_image
->tile_y
;
585 image
->has_depthstencil
= orig_image
->has_depthstencil
;
586 image
->data
= loaderPrivate
;
588 memcpy(image
->strides
, orig_image
->strides
, sizeof(image
->strides
));
589 memcpy(image
->offsets
, orig_image
->offsets
, sizeof(image
->offsets
));
595 intel_validate_usage(__DRIimage
*image
, unsigned int use
)
597 if (use
& __DRI_IMAGE_USE_CURSOR
) {
598 if (image
->region
->width
!= 64 || image
->region
->height
!= 64)
606 intel_create_image_from_names(__DRIscreen
*screen
,
607 int width
, int height
, int fourcc
,
608 int *names
, int num_names
,
609 int *strides
, int *offsets
,
612 struct intel_image_format
*f
= NULL
;
616 if (screen
== NULL
|| names
== NULL
|| num_names
!= 1)
619 f
= intel_image_format_lookup(fourcc
);
623 image
= intel_create_image_from_name(screen
, width
, height
,
624 __DRI_IMAGE_FORMAT_NONE
,
625 names
[0], strides
[0],
631 image
->planar_format
= f
;
632 for (i
= 0; i
< f
->nplanes
; i
++) {
633 index
= f
->planes
[i
].buffer_index
;
634 image
->offsets
[index
] = offsets
[index
];
635 image
->strides
[index
] = strides
[index
];
642 intel_create_image_from_fds(__DRIscreen
*screen
,
643 int width
, int height
, int fourcc
,
644 int *fds
, int num_fds
, int *strides
, int *offsets
,
647 struct intel_screen
*intelScreen
= screen
->driverPrivate
;
648 struct intel_image_format
*f
;
652 if (fds
== NULL
|| num_fds
!= 1)
655 f
= intel_image_format_lookup(fourcc
);
660 image
= intel_allocate_image(f
->planes
[0].dri_format
, loaderPrivate
);
662 image
= intel_allocate_image(__DRI_IMAGE_FORMAT_NONE
, loaderPrivate
);
667 image
->region
= intel_region_alloc_for_fd(intelScreen
,
668 f
->planes
[0].cpp
, width
, height
, strides
[0],
669 height
* strides
[0], fds
[0], "image");
670 if (image
->region
== NULL
) {
675 image
->planar_format
= f
;
676 for (i
= 0; i
< f
->nplanes
; i
++) {
677 index
= f
->planes
[i
].buffer_index
;
678 image
->offsets
[index
] = offsets
[index
];
679 image
->strides
[index
] = strides
[index
];
682 intel_setup_image_from_dimensions(image
);
688 intel_create_image_from_dma_bufs(__DRIscreen
*screen
,
689 int width
, int height
, int fourcc
,
690 int *fds
, int num_fds
,
691 int *strides
, int *offsets
,
692 enum __DRIYUVColorSpace yuv_color_space
,
693 enum __DRISampleRange sample_range
,
694 enum __DRIChromaSiting horizontal_siting
,
695 enum __DRIChromaSiting vertical_siting
,
700 struct intel_image_format
*f
= intel_image_format_lookup(fourcc
);
702 /* For now only packed formats that have native sampling are supported. */
703 if (!f
|| f
->nplanes
!= 1) {
704 *error
= __DRI_IMAGE_ERROR_BAD_MATCH
;
708 image
= intel_create_image_from_fds(screen
, width
, height
, fourcc
, fds
,
709 num_fds
, strides
, offsets
,
713 * Invalid parameters and any inconsistencies between are assumed to be
714 * checked by the caller. Therefore besides unsupported formats one can fail
715 * only in allocation.
718 *error
= __DRI_IMAGE_ERROR_BAD_ALLOC
;
722 image
->dma_buf_imported
= true;
723 image
->yuv_color_space
= yuv_color_space
;
724 image
->sample_range
= sample_range
;
725 image
->horizontal_siting
= horizontal_siting
;
726 image
->vertical_siting
= vertical_siting
;
728 *error
= __DRI_IMAGE_ERROR_SUCCESS
;
733 intel_from_planar(__DRIimage
*parent
, int plane
, void *loaderPrivate
)
735 int width
, height
, offset
, stride
, dri_format
, index
;
736 struct intel_image_format
*f
;
737 uint32_t mask_x
, mask_y
;
740 if (parent
== NULL
|| parent
->planar_format
== NULL
)
743 f
= parent
->planar_format
;
745 if (plane
>= f
->nplanes
)
748 width
= parent
->region
->width
>> f
->planes
[plane
].width_shift
;
749 height
= parent
->region
->height
>> f
->planes
[plane
].height_shift
;
750 dri_format
= f
->planes
[plane
].dri_format
;
751 index
= f
->planes
[plane
].buffer_index
;
752 offset
= parent
->offsets
[index
];
753 stride
= parent
->strides
[index
];
755 image
= intel_allocate_image(dri_format
, loaderPrivate
);
759 if (offset
+ height
* stride
> parent
->region
->bo
->size
) {
760 _mesa_warning(NULL
, "intel_create_sub_image: subimage out of bounds");
765 image
->region
= calloc(sizeof(*image
->region
), 1);
766 if (image
->region
== NULL
) {
771 image
->region
->cpp
= _mesa_get_format_bytes(image
->format
);
772 image
->region
->width
= width
;
773 image
->region
->height
= height
;
774 image
->region
->pitch
= stride
;
775 image
->region
->refcount
= 1;
776 image
->region
->bo
= parent
->region
->bo
;
777 drm_intel_bo_reference(image
->region
->bo
);
778 image
->region
->tiling
= parent
->region
->tiling
;
779 image
->offset
= offset
;
780 intel_setup_image_from_dimensions(image
);
782 intel_region_get_tile_masks(image
->region
, &mask_x
, &mask_y
, false);
785 "intel_create_sub_image: offset not on tile boundary");
790 static struct __DRIimageExtensionRec intelImageExtension
= {
791 .base
= { __DRI_IMAGE
, 8 },
793 .createImageFromName
= intel_create_image_from_name
,
794 .createImageFromRenderbuffer
= intel_create_image_from_renderbuffer
,
795 .destroyImage
= intel_destroy_image
,
796 .createImage
= intel_create_image
,
797 .queryImage
= intel_query_image
,
798 .dupImage
= intel_dup_image
,
799 .validateUsage
= intel_validate_usage
,
800 .createImageFromNames
= intel_create_image_from_names
,
801 .fromPlanar
= intel_from_planar
,
802 .createImageFromTexture
= intel_create_image_from_texture
,
803 .createImageFromFds
= intel_create_image_from_fds
,
804 .createImageFromDmaBufs
= intel_create_image_from_dma_bufs
808 brw_query_renderer_integer(__DRIscreen
*psp
, int param
, int *value
)
810 const struct intel_screen
*const intelScreen
=
811 (struct intel_screen
*) psp
->driverPrivate
;
814 case __DRI2_RENDERER_VENDOR_ID
:
817 case __DRI2_RENDERER_DEVICE_ID
:
818 value
[0] = intelScreen
->deviceID
;
820 case __DRI2_RENDERER_ACCELERATED
:
823 case __DRI2_RENDERER_VIDEO_MEMORY
: {
824 /* Once a batch uses more than 75% of the maximum mappable size, we
825 * assume that there's some fragmentation, and we start doing extra
826 * flushing, etc. That's the big cliff apps will care about.
829 size_t mappable_size
;
831 drm_intel_get_aperture_sizes(psp
->fd
, &mappable_size
, &aper_size
);
833 const unsigned gpu_mappable_megabytes
=
834 (aper_size
/ (1024 * 1024)) * 3 / 4;
836 const long system_memory_pages
= sysconf(_SC_PHYS_PAGES
);
837 const long system_page_size
= sysconf(_SC_PAGE_SIZE
);
839 if (system_memory_pages
<= 0 || system_page_size
<= 0)
842 const uint64_t system_memory_bytes
= (uint64_t) system_memory_pages
843 * (uint64_t) system_page_size
;
845 const unsigned system_memory_megabytes
=
846 (unsigned) (system_memory_bytes
/ 1024);
848 value
[0] = MIN2(system_memory_megabytes
, gpu_mappable_megabytes
);
851 case __DRI2_RENDERER_UNIFIED_MEMORY_ARCHITECTURE
:
854 case __DRI2_RENDERER_PREFERRED_PROFILE
:
855 value
[0] = (psp
->max_gl_core_version
!= 0)
856 ? (1U << __DRI_API_OPENGL_CORE
) : (1U << __DRI_API_OPENGL
);
859 return driQueryRendererIntegerCommon(psp
, param
, value
);
866 brw_query_renderer_string(__DRIscreen
*psp
, int param
, const char **value
)
868 const struct intel_screen
*intelScreen
=
869 (struct intel_screen
*) psp
->driverPrivate
;
872 case __DRI2_RENDERER_VENDOR_ID
:
873 value
[0] = brw_vendor_string
;
875 case __DRI2_RENDERER_DEVICE_ID
:
876 value
[0] = brw_get_renderer_string(intelScreen
->deviceID
);
885 static struct __DRI2rendererQueryExtensionRec intelRendererQueryExtension
= {
886 .base
= { __DRI2_RENDERER_QUERY
, 1 },
888 .queryInteger
= brw_query_renderer_integer
,
889 .queryString
= brw_query_renderer_string
892 static const struct __DRIrobustnessExtensionRec dri2Robustness
= {
893 { __DRI2_ROBUSTNESS
, 1 }
896 static const __DRIextension
*intelScreenExtensions
[] = {
897 &intelTexBufferExtension
.base
,
898 &intelFlushExtension
.base
,
899 &intelImageExtension
.base
,
900 &intelRendererQueryExtension
.base
,
901 &dri2ConfigQueryExtension
.base
,
902 &dri2Robustness
.base
,
907 intel_get_param(__DRIscreen
*psp
, int param
, int *value
)
910 struct drm_i915_getparam gp
;
912 memset(&gp
, 0, sizeof(gp
));
916 ret
= drmCommandWriteRead(psp
->fd
, DRM_I915_GETPARAM
, &gp
, sizeof(gp
));
919 _mesa_warning(NULL
, "drm_i915_getparam: %d", ret
);
927 intel_get_boolean(__DRIscreen
*psp
, int param
)
930 return intel_get_param(psp
, param
, &value
) && value
;
934 intelDestroyScreen(__DRIscreen
* sPriv
)
936 struct intel_screen
*intelScreen
= sPriv
->driverPrivate
;
938 dri_bufmgr_destroy(intelScreen
->bufmgr
);
939 driDestroyOptionInfo(&intelScreen
->optionCache
);
942 sPriv
->driverPrivate
= NULL
;
947 * This is called when we need to set up GL rendering to a new X window.
950 intelCreateBuffer(__DRIscreen
* driScrnPriv
,
951 __DRIdrawable
* driDrawPriv
,
952 const struct gl_config
* mesaVis
, GLboolean isPixmap
)
954 struct intel_renderbuffer
*rb
;
955 struct intel_screen
*screen
= (struct intel_screen
*) driScrnPriv
->driverPrivate
;
957 unsigned num_samples
= intel_quantize_num_samples(screen
, mesaVis
->samples
);
958 struct gl_framebuffer
*fb
;
963 fb
= CALLOC_STRUCT(gl_framebuffer
);
967 _mesa_initialize_window_framebuffer(fb
, mesaVis
);
969 if (mesaVis
->redBits
== 5)
970 rgbFormat
= MESA_FORMAT_RGB565
;
971 else if (mesaVis
->sRGBCapable
)
972 rgbFormat
= MESA_FORMAT_SARGB8
;
973 else if (mesaVis
->alphaBits
== 0)
974 rgbFormat
= MESA_FORMAT_XRGB8888
;
976 rgbFormat
= MESA_FORMAT_SARGB8
;
977 fb
->Visual
.sRGBCapable
= true;
980 /* setup the hardware-based renderbuffers */
981 rb
= intel_create_renderbuffer(rgbFormat
, num_samples
);
982 _mesa_add_renderbuffer(fb
, BUFFER_FRONT_LEFT
, &rb
->Base
.Base
);
984 if (mesaVis
->doubleBufferMode
) {
985 rb
= intel_create_renderbuffer(rgbFormat
, num_samples
);
986 _mesa_add_renderbuffer(fb
, BUFFER_BACK_LEFT
, &rb
->Base
.Base
);
990 * Assert here that the gl_config has an expected depth/stencil bit
991 * combination: one of d24/s8, d16/s0, d0/s0. (See intelInitScreen2(),
992 * which constructs the advertised configs.)
994 if (mesaVis
->depthBits
== 24) {
995 assert(mesaVis
->stencilBits
== 8);
997 if (screen
->devinfo
->has_hiz_and_separate_stencil
) {
998 rb
= intel_create_private_renderbuffer(MESA_FORMAT_X8_Z24
,
1000 _mesa_add_renderbuffer(fb
, BUFFER_DEPTH
, &rb
->Base
.Base
);
1001 rb
= intel_create_private_renderbuffer(MESA_FORMAT_S8
,
1003 _mesa_add_renderbuffer(fb
, BUFFER_STENCIL
, &rb
->Base
.Base
);
1006 * Use combined depth/stencil. Note that the renderbuffer is
1007 * attached to two attachment points.
1009 rb
= intel_create_private_renderbuffer(MESA_FORMAT_S8_Z24
,
1011 _mesa_add_renderbuffer(fb
, BUFFER_DEPTH
, &rb
->Base
.Base
);
1012 _mesa_add_renderbuffer(fb
, BUFFER_STENCIL
, &rb
->Base
.Base
);
1015 else if (mesaVis
->depthBits
== 16) {
1016 assert(mesaVis
->stencilBits
== 0);
1017 rb
= intel_create_private_renderbuffer(MESA_FORMAT_Z16
,
1019 _mesa_add_renderbuffer(fb
, BUFFER_DEPTH
, &rb
->Base
.Base
);
1022 assert(mesaVis
->depthBits
== 0);
1023 assert(mesaVis
->stencilBits
== 0);
1026 /* now add any/all software-based renderbuffers we may need */
1027 _swrast_add_soft_renderbuffers(fb
,
1028 false, /* never sw color */
1029 false, /* never sw depth */
1030 false, /* never sw stencil */
1031 mesaVis
->accumRedBits
> 0,
1032 false, /* never sw alpha */
1033 false /* never sw aux */ );
1034 driDrawPriv
->driverPrivate
= fb
;
1040 intelDestroyBuffer(__DRIdrawable
* driDrawPriv
)
1042 struct gl_framebuffer
*fb
= driDrawPriv
->driverPrivate
;
1044 _mesa_reference_framebuffer(&fb
, NULL
);
1048 intel_init_bufmgr(struct intel_screen
*intelScreen
)
1050 __DRIscreen
*spriv
= intelScreen
->driScrnPriv
;
1052 intelScreen
->no_hw
= getenv("INTEL_NO_HW") != NULL
;
1054 intelScreen
->bufmgr
= intel_bufmgr_gem_init(spriv
->fd
, BATCH_SZ
);
1055 if (intelScreen
->bufmgr
== NULL
) {
1056 fprintf(stderr
, "[%s:%u] Error initializing buffer manager.\n",
1057 __func__
, __LINE__
);
1061 drm_intel_bufmgr_gem_enable_fenced_relocs(intelScreen
->bufmgr
);
1063 if (!intel_get_boolean(spriv
, I915_PARAM_HAS_RELAXED_DELTA
)) {
1064 fprintf(stderr
, "[%s: %u] Kernel 2.6.39 required.\n", __func__
, __LINE__
);
1072 intel_detect_swizzling(struct intel_screen
*screen
)
1074 drm_intel_bo
*buffer
;
1075 unsigned long flags
= 0;
1076 unsigned long aligned_pitch
;
1077 uint32_t tiling
= I915_TILING_X
;
1078 uint32_t swizzle_mode
= 0;
1080 buffer
= drm_intel_bo_alloc_tiled(screen
->bufmgr
, "swizzle test",
1082 &tiling
, &aligned_pitch
, flags
);
1086 drm_intel_bo_get_tiling(buffer
, &tiling
, &swizzle_mode
);
1087 drm_intel_bo_unreference(buffer
);
1089 if (swizzle_mode
== I915_BIT_6_SWIZZLE_NONE
)
1095 static __DRIconfig
**
1096 intel_screen_make_configs(__DRIscreen
*dri_screen
)
1098 static const gl_format formats
[] = {
1100 MESA_FORMAT_ARGB8888
1103 /* GLX_SWAP_COPY_OML is not supported due to page flipping. */
1104 static const GLenum back_buffer_modes
[] = {
1105 GLX_SWAP_UNDEFINED_OML
, GLX_NONE
,
1108 static const uint8_t singlesample_samples
[1] = {0};
1109 static const uint8_t multisample_samples
[2] = {4, 8};
1111 struct intel_screen
*screen
= dri_screen
->driverPrivate
;
1112 const struct brw_device_info
*devinfo
= screen
->devinfo
;
1113 uint8_t depth_bits
[4], stencil_bits
[4];
1114 __DRIconfig
**configs
= NULL
;
1116 /* Generate singlesample configs without accumulation buffer. */
1117 for (int i
= 0; i
< ARRAY_SIZE(formats
); i
++) {
1118 __DRIconfig
**new_configs
;
1119 int num_depth_stencil_bits
= 2;
1121 /* Starting with DRI2 protocol version 1.1 we can request a depth/stencil
1122 * buffer that has a different number of bits per pixel than the color
1123 * buffer, gen >= 6 supports this.
1126 stencil_bits
[0] = 0;
1128 if (formats
[i
] == MESA_FORMAT_RGB565
) {
1130 stencil_bits
[1] = 0;
1131 if (devinfo
->gen
>= 6) {
1133 stencil_bits
[2] = 8;
1134 num_depth_stencil_bits
= 3;
1138 stencil_bits
[1] = 8;
1141 new_configs
= driCreateConfigs(formats
[i
],
1144 num_depth_stencil_bits
,
1145 back_buffer_modes
, 2,
1146 singlesample_samples
, 1,
1148 configs
= driConcatConfigs(configs
, new_configs
);
1151 /* Generate the minimum possible set of configs that include an
1152 * accumulation buffer.
1154 for (int i
= 0; i
< ARRAY_SIZE(formats
); i
++) {
1155 __DRIconfig
**new_configs
;
1157 if (formats
[i
] == MESA_FORMAT_RGB565
) {
1159 stencil_bits
[0] = 0;
1162 stencil_bits
[0] = 8;
1165 new_configs
= driCreateConfigs(formats
[i
],
1166 depth_bits
, stencil_bits
, 1,
1167 back_buffer_modes
, 1,
1168 singlesample_samples
, 1,
1170 configs
= driConcatConfigs(configs
, new_configs
);
1173 /* Generate multisample configs.
1175 * This loop breaks early, and hence is a no-op, on gen < 6.
1177 * Multisample configs must follow the singlesample configs in order to
1178 * work around an X server bug present in 1.12. The X server chooses to
1179 * associate the first listed RGBA888-Z24S8 config, regardless of its
1180 * sample count, with the 32-bit depth visual used for compositing.
1182 * Only doublebuffer configs with GLX_SWAP_UNDEFINED_OML behavior are
1183 * supported. Singlebuffer configs are not supported because no one wants
1186 for (int i
= 0; i
< ARRAY_SIZE(formats
); i
++) {
1187 if (devinfo
->gen
< 6)
1190 __DRIconfig
**new_configs
;
1191 const int num_depth_stencil_bits
= 2;
1192 int num_msaa_modes
= 0;
1195 stencil_bits
[0] = 0;
1197 if (formats
[i
] == MESA_FORMAT_RGB565
) {
1199 stencil_bits
[1] = 0;
1202 stencil_bits
[1] = 8;
1205 if (devinfo
->gen
>= 7)
1207 else if (devinfo
->gen
== 6)
1210 new_configs
= driCreateConfigs(formats
[i
],
1213 num_depth_stencil_bits
,
1214 back_buffer_modes
, 1,
1215 multisample_samples
,
1218 configs
= driConcatConfigs(configs
, new_configs
);
1221 if (configs
== NULL
) {
1222 fprintf(stderr
, "[%s:%u] Error creating FBConfig!\n", __func__
,
1231 set_max_gl_versions(struct intel_screen
*screen
)
1233 __DRIscreen
*psp
= screen
->driScrnPriv
;
1235 switch (screen
->devinfo
->gen
) {
1237 psp
->max_gl_core_version
= 33;
1238 psp
->max_gl_compat_version
= 30;
1239 psp
->max_gl_es1_version
= 11;
1240 psp
->max_gl_es2_version
= 30;
1243 psp
->max_gl_core_version
= 31;
1244 psp
->max_gl_compat_version
= 30;
1245 psp
->max_gl_es1_version
= 11;
1246 psp
->max_gl_es2_version
= 30;
1250 psp
->max_gl_core_version
= 0;
1251 psp
->max_gl_compat_version
= 21;
1252 psp
->max_gl_es1_version
= 11;
1253 psp
->max_gl_es2_version
= 20;
1256 assert(!"unrecognized intel_screen::gen");
1262 * This is the driver specific part of the createNewScreen entry point.
1263 * Called when using DRI2.
1265 * \return the struct gl_config supported by this driver
1268 __DRIconfig
**intelInitScreen2(__DRIscreen
*psp
)
1270 struct intel_screen
*intelScreen
;
1272 if (psp
->image
.loader
) {
1273 } else if (psp
->dri2
.loader
->base
.version
<= 2 ||
1274 psp
->dri2
.loader
->getBuffersWithFormat
== NULL
) {
1276 "\nERROR! DRI2 loader with getBuffersWithFormat() "
1277 "support required\n");
1281 /* Allocate the private area */
1282 intelScreen
= calloc(1, sizeof *intelScreen
);
1284 fprintf(stderr
, "\nERROR! Allocating private area failed\n");
1287 /* parse information in __driConfigOptions */
1288 driParseOptionInfo(&intelScreen
->optionCache
, brw_config_options
.xml
);
1290 intelScreen
->driScrnPriv
= psp
;
1291 psp
->driverPrivate
= (void *) intelScreen
;
1293 if (!intel_init_bufmgr(intelScreen
))
1296 intelScreen
->deviceID
= drm_intel_bufmgr_gem_get_devid(intelScreen
->bufmgr
);
1297 intelScreen
->devinfo
= brw_get_device_info(intelScreen
->deviceID
);
1299 intelScreen
->hw_must_use_separate_stencil
= intelScreen
->devinfo
->gen
>= 7;
1301 intelScreen
->hw_has_swizzling
= intel_detect_swizzling(intelScreen
);
1303 set_max_gl_versions(intelScreen
);
1305 psp
->extensions
= intelScreenExtensions
;
1307 return (const __DRIconfig
**) intel_screen_make_configs(psp
);
1310 struct intel_buffer
{
1312 struct intel_region
*region
;
1315 static __DRIbuffer
*
1316 intelAllocateBuffer(__DRIscreen
*screen
,
1317 unsigned attachment
, unsigned format
,
1318 int width
, int height
)
1320 struct intel_buffer
*intelBuffer
;
1321 struct intel_screen
*intelScreen
= screen
->driverPrivate
;
1323 assert(attachment
== __DRI_BUFFER_FRONT_LEFT
||
1324 attachment
== __DRI_BUFFER_BACK_LEFT
);
1326 intelBuffer
= calloc(1, sizeof *intelBuffer
);
1327 if (intelBuffer
== NULL
)
1330 /* The front and back buffers are color buffers, which are X tiled. */
1331 intelBuffer
->region
= intel_region_alloc(intelScreen
,
1338 if (intelBuffer
->region
== NULL
) {
1343 intel_region_flink(intelBuffer
->region
, &intelBuffer
->base
.name
);
1345 intelBuffer
->base
.attachment
= attachment
;
1346 intelBuffer
->base
.cpp
= intelBuffer
->region
->cpp
;
1347 intelBuffer
->base
.pitch
= intelBuffer
->region
->pitch
;
1349 return &intelBuffer
->base
;
1353 intelReleaseBuffer(__DRIscreen
*screen
, __DRIbuffer
*buffer
)
1355 struct intel_buffer
*intelBuffer
= (struct intel_buffer
*) buffer
;
1357 intel_region_release(&intelBuffer
->region
);
1361 static const struct __DriverAPIRec brw_driver_api
= {
1362 .InitScreen
= intelInitScreen2
,
1363 .DestroyScreen
= intelDestroyScreen
,
1364 .CreateContext
= brwCreateContext
,
1365 .DestroyContext
= intelDestroyContext
,
1366 .CreateBuffer
= intelCreateBuffer
,
1367 .DestroyBuffer
= intelDestroyBuffer
,
1368 .MakeCurrent
= intelMakeCurrent
,
1369 .UnbindContext
= intelUnbindContext
,
1370 .AllocateBuffer
= intelAllocateBuffer
,
1371 .ReleaseBuffer
= intelReleaseBuffer
1374 static const struct __DRIDriverVtableExtensionRec brw_vtable
= {
1375 .base
= { __DRI_DRIVER_VTABLE
, 1 },
1376 .vtable
= &brw_driver_api
,
1379 static const __DRIextension
*brw_driver_extensions
[] = {
1380 &driCoreExtension
.base
,
1381 &driImageDriverExtension
.base
,
1382 &driDRI2Extension
.base
,
1384 &brw_config_options
.base
,
1388 PUBLIC
const __DRIextension
**__driDriverGetExtensions_i965(void)
1390 globalDriverAPI
= &brw_driver_api
;
1392 return brw_driver_extensions
;