1 /**************************************************************************
3 * Copyright 2003 VMware, Inc.
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 VMWARE 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"
40 #include "glsl/ralloc.h"
45 static const __DRIconfigOptionsExtension brw_config_options
= {
46 .base
= { __DRI_CONFIG_OPTIONS
, 1 },
49 DRI_CONF_SECTION_PERFORMANCE
50 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_ALWAYS_SYNC
)
51 /* Options correspond to DRI_CONF_BO_REUSE_DISABLED,
52 * DRI_CONF_BO_REUSE_ALL
54 DRI_CONF_OPT_BEGIN_V(bo_reuse
, enum, 1, "0:1")
55 DRI_CONF_DESC_BEGIN(en
, "Buffer object reuse")
56 DRI_CONF_ENUM(0, "Disable buffer object reuse")
57 DRI_CONF_ENUM(1, "Enable reuse of all sizes of buffer objects")
61 DRI_CONF_OPT_BEGIN_B(hiz
, "true")
62 DRI_CONF_DESC(en
, "Enable Hierarchical Z on gen6+")
65 DRI_CONF_OPT_BEGIN_B(disable_derivative_optimization
, "false")
66 DRI_CONF_DESC(en
, "Derivatives with finer granularity by default")
70 DRI_CONF_SECTION_QUALITY
71 DRI_CONF_FORCE_S3TC_ENABLE("false")
73 DRI_CONF_OPT_BEGIN(clamp_max_samples
, int, -1)
74 DRI_CONF_DESC(en
, "Clamp the value of GL_MAX_SAMPLES to the "
75 "given integer. If negative, then do not clamp.")
79 DRI_CONF_SECTION_DEBUG
80 DRI_CONF_NO_RAST("false")
81 DRI_CONF_ALWAYS_FLUSH_BATCH("false")
82 DRI_CONF_ALWAYS_FLUSH_CACHE("false")
83 DRI_CONF_DISABLE_THROTTLING("false")
84 DRI_CONF_FORCE_GLSL_EXTENSIONS_WARN("false")
85 DRI_CONF_DISABLE_GLSL_LINE_CONTINUATIONS("false")
86 DRI_CONF_DISABLE_BLEND_FUNC_EXTENDED("false")
88 DRI_CONF_OPT_BEGIN_B(shader_precompile
, "true")
89 DRI_CONF_DESC(en
, "Perform code generation at shader link time.")
95 #include "intel_batchbuffer.h"
96 #include "intel_buffers.h"
97 #include "intel_bufmgr.h"
98 #include "intel_chipset.h"
99 #include "intel_fbo.h"
100 #include "intel_mipmap_tree.h"
101 #include "intel_screen.h"
102 #include "intel_tex.h"
103 #include "intel_regions.h"
105 #include "brw_context.h"
107 #include "i915_drm.h"
110 * For debugging purposes, this returns a time in seconds.
117 clock_gettime(CLOCK_MONOTONIC
, &tp
);
119 return tp
.tv_sec
+ tp
.tv_nsec
/ 1000000000.0;
123 aub_dump_bmp(struct gl_context
*ctx
)
125 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
127 for (int i
= 0; i
< fb
->_NumColorDrawBuffers
; i
++) {
128 struct intel_renderbuffer
*irb
=
129 intel_renderbuffer(fb
->_ColorDrawBuffers
[i
]);
131 if (irb
&& irb
->mt
) {
132 enum aub_dump_bmp_format format
;
134 switch (irb
->Base
.Base
.Format
) {
135 case MESA_FORMAT_B8G8R8A8_UNORM
:
136 case MESA_FORMAT_B8G8R8X8_UNORM
:
137 format
= AUB_DUMP_BMP_FORMAT_ARGB_8888
;
143 assert(irb
->mt
->region
->pitch
% irb
->mt
->region
->cpp
== 0);
144 drm_intel_gem_bo_aub_dump_bmp(irb
->mt
->region
->bo
,
147 irb
->Base
.Base
.Width
,
148 irb
->Base
.Base
.Height
,
150 irb
->mt
->region
->pitch
,
156 static const __DRItexBufferExtension intelTexBufferExtension
= {
157 .base
= { __DRI_TEX_BUFFER
, 3 },
159 .setTexBuffer
= intelSetTexBuffer
,
160 .setTexBuffer2
= intelSetTexBuffer2
,
161 .releaseTexBuffer
= NULL
,
165 intel_dri2_flush_with_flags(__DRIcontext
*cPriv
,
166 __DRIdrawable
*dPriv
,
168 enum __DRI2throttleReason reason
)
170 struct brw_context
*brw
= cPriv
->driverPrivate
;
175 struct gl_context
*ctx
= &brw
->ctx
;
177 FLUSH_VERTICES(ctx
, 0);
179 if (flags
& __DRI2_FLUSH_DRAWABLE
)
180 intel_resolve_for_dri2_flush(brw
, dPriv
);
182 if (reason
== __DRI2_THROTTLE_SWAPBUFFER
||
183 reason
== __DRI2_THROTTLE_FLUSHFRONT
) {
184 brw
->need_throttle
= true;
187 intel_batchbuffer_flush(brw
);
189 if (INTEL_DEBUG
& DEBUG_AUB
) {
195 * Provides compatibility with loaders that only support the older (version
196 * 1-3) flush interface.
198 * That includes libGL up to Mesa 9.0, and the X Server at least up to 1.13.
201 intel_dri2_flush(__DRIdrawable
*drawable
)
203 intel_dri2_flush_with_flags(drawable
->driContextPriv
, drawable
,
204 __DRI2_FLUSH_DRAWABLE
,
205 __DRI2_THROTTLE_SWAPBUFFER
);
208 static const struct __DRI2flushExtensionRec intelFlushExtension
= {
209 .base
= { __DRI2_FLUSH
, 4 },
211 .flush
= intel_dri2_flush
,
212 .invalidate
= dri2InvalidateDrawable
,
213 .flush_with_flags
= intel_dri2_flush_with_flags
,
216 static struct intel_image_format intel_image_formats
[] = {
217 { __DRI_IMAGE_FOURCC_ARGB8888
, __DRI_IMAGE_COMPONENTS_RGBA
, 1,
218 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ARGB8888
, 4 } } },
220 { __DRI_IMAGE_FOURCC_SARGB8888
, __DRI_IMAGE_COMPONENTS_RGBA
, 1,
221 { { 0, 0, 0, __DRI_IMAGE_FORMAT_SARGB8
, 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 intel_miptree_make_shareable(brw
, mt
);
325 intel_miptree_check_level_layer(mt
, level
, zoffset
);
327 image
->width
= minify(mt
->physical_width0
, level
- mt
->first_level
);
328 image
->height
= minify(mt
->physical_height0
, level
- mt
->first_level
);
330 image
->offset
= intel_miptree_get_tile_offsets(mt
, level
, zoffset
,
334 intel_region_reference(&image
->region
, mt
->region
);
338 intel_setup_image_from_dimensions(__DRIimage
*image
)
340 image
->width
= image
->region
->width
;
341 image
->height
= image
->region
->height
;
344 image
->has_depthstencil
= false;
348 intel_create_image_from_name(__DRIscreen
*screen
,
349 int width
, int height
, int format
,
350 int name
, int pitch
, void *loaderPrivate
)
352 struct intel_screen
*intelScreen
= screen
->driverPrivate
;
356 image
= intel_allocate_image(format
, loaderPrivate
);
360 if (image
->format
== MESA_FORMAT_NONE
)
363 cpp
= _mesa_get_format_bytes(image
->format
);
364 image
->region
= intel_region_alloc_for_handle(intelScreen
,
366 pitch
* cpp
, name
, "image");
367 if (image
->region
== NULL
) {
372 intel_setup_image_from_dimensions(image
);
378 intel_create_image_from_renderbuffer(__DRIcontext
*context
,
379 int renderbuffer
, void *loaderPrivate
)
382 struct brw_context
*brw
= context
->driverPrivate
;
383 struct gl_context
*ctx
= &brw
->ctx
;
384 struct gl_renderbuffer
*rb
;
385 struct intel_renderbuffer
*irb
;
387 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
389 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glRenderbufferExternalMESA");
393 irb
= intel_renderbuffer(rb
);
394 intel_miptree_make_shareable(brw
, irb
->mt
);
395 image
= calloc(1, sizeof *image
);
399 image
->internal_format
= rb
->InternalFormat
;
400 image
->format
= rb
->Format
;
402 image
->data
= loaderPrivate
;
403 intel_region_reference(&image
->region
, irb
->mt
->region
);
404 intel_setup_image_from_dimensions(image
);
405 image
->dri_format
= driGLFormatToImageFormat(image
->format
);
406 image
->has_depthstencil
= irb
->mt
->stencil_mt
? true : false;
408 rb
->NeedsFinishRenderTexture
= true;
413 intel_create_image_from_texture(__DRIcontext
*context
, int target
,
414 unsigned texture
, int zoffset
,
420 struct brw_context
*brw
= context
->driverPrivate
;
421 struct gl_texture_object
*obj
;
422 struct intel_texture_object
*iobj
;
425 obj
= _mesa_lookup_texture(&brw
->ctx
, texture
);
426 if (!obj
|| obj
->Target
!= target
) {
427 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
431 if (target
== GL_TEXTURE_CUBE_MAP
)
434 _mesa_test_texobj_completeness(&brw
->ctx
, obj
);
435 iobj
= intel_texture_object(obj
);
436 if (!obj
->_BaseComplete
|| (level
> 0 && !obj
->_MipmapComplete
)) {
437 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
441 if (level
< obj
->BaseLevel
|| level
> obj
->_MaxLevel
) {
442 *error
= __DRI_IMAGE_ERROR_BAD_MATCH
;
446 if (target
== GL_TEXTURE_3D
&& obj
->Image
[face
][level
]->Depth
< zoffset
) {
447 *error
= __DRI_IMAGE_ERROR_BAD_MATCH
;
450 image
= calloc(1, sizeof *image
);
452 *error
= __DRI_IMAGE_ERROR_BAD_ALLOC
;
456 image
->internal_format
= obj
->Image
[face
][level
]->InternalFormat
;
457 image
->format
= obj
->Image
[face
][level
]->TexFormat
;
458 image
->data
= loaderPrivate
;
459 intel_setup_image_from_mipmap_tree(brw
, image
, iobj
->mt
, level
, zoffset
);
460 image
->dri_format
= driGLFormatToImageFormat(image
->format
);
461 image
->has_depthstencil
= iobj
->mt
->stencil_mt
? true : false;
462 if (image
->dri_format
== MESA_FORMAT_NONE
) {
463 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
468 *error
= __DRI_IMAGE_ERROR_SUCCESS
;
473 intel_destroy_image(__DRIimage
*image
)
475 intel_region_release(&image
->region
);
480 intel_create_image(__DRIscreen
*screen
,
481 int width
, int height
, int format
,
486 struct intel_screen
*intelScreen
= screen
->driverPrivate
;
490 tiling
= I915_TILING_X
;
491 if (use
& __DRI_IMAGE_USE_CURSOR
) {
492 if (width
!= 64 || height
!= 64)
494 tiling
= I915_TILING_NONE
;
497 if (use
& __DRI_IMAGE_USE_LINEAR
)
498 tiling
= I915_TILING_NONE
;
500 image
= intel_allocate_image(format
, loaderPrivate
);
504 cpp
= _mesa_get_format_bytes(image
->format
);
506 intel_region_alloc(intelScreen
, tiling
, cpp
, width
, height
, true);
507 if (image
->region
== NULL
) {
512 intel_setup_image_from_dimensions(image
);
518 intel_query_image(__DRIimage
*image
, int attrib
, int *value
)
521 case __DRI_IMAGE_ATTRIB_STRIDE
:
522 *value
= image
->region
->pitch
;
524 case __DRI_IMAGE_ATTRIB_HANDLE
:
525 *value
= image
->region
->bo
->handle
;
527 case __DRI_IMAGE_ATTRIB_NAME
:
528 return intel_region_flink(image
->region
, (uint32_t *) value
);
529 case __DRI_IMAGE_ATTRIB_FORMAT
:
530 *value
= image
->dri_format
;
532 case __DRI_IMAGE_ATTRIB_WIDTH
:
533 *value
= image
->region
->width
;
535 case __DRI_IMAGE_ATTRIB_HEIGHT
:
536 *value
= image
->region
->height
;
538 case __DRI_IMAGE_ATTRIB_COMPONENTS
:
539 if (image
->planar_format
== NULL
)
541 *value
= image
->planar_format
->components
;
543 case __DRI_IMAGE_ATTRIB_FD
:
544 if (drm_intel_bo_gem_export_to_prime(image
->region
->bo
, value
) == 0)
553 intel_dup_image(__DRIimage
*orig_image
, void *loaderPrivate
)
557 image
= calloc(1, sizeof *image
);
561 intel_region_reference(&image
->region
, orig_image
->region
);
562 if (image
->region
== NULL
) {
567 image
->internal_format
= orig_image
->internal_format
;
568 image
->planar_format
= orig_image
->planar_format
;
569 image
->dri_format
= orig_image
->dri_format
;
570 image
->format
= orig_image
->format
;
571 image
->offset
= orig_image
->offset
;
572 image
->width
= orig_image
->width
;
573 image
->height
= orig_image
->height
;
574 image
->tile_x
= orig_image
->tile_x
;
575 image
->tile_y
= orig_image
->tile_y
;
576 image
->has_depthstencil
= orig_image
->has_depthstencil
;
577 image
->data
= loaderPrivate
;
579 memcpy(image
->strides
, orig_image
->strides
, sizeof(image
->strides
));
580 memcpy(image
->offsets
, orig_image
->offsets
, sizeof(image
->offsets
));
586 intel_validate_usage(__DRIimage
*image
, unsigned int use
)
588 if (use
& __DRI_IMAGE_USE_CURSOR
) {
589 if (image
->region
->width
!= 64 || image
->region
->height
!= 64)
597 intel_create_image_from_names(__DRIscreen
*screen
,
598 int width
, int height
, int fourcc
,
599 int *names
, int num_names
,
600 int *strides
, int *offsets
,
603 struct intel_image_format
*f
= NULL
;
607 if (screen
== NULL
|| names
== NULL
|| num_names
!= 1)
610 f
= intel_image_format_lookup(fourcc
);
614 image
= intel_create_image_from_name(screen
, width
, height
,
615 __DRI_IMAGE_FORMAT_NONE
,
616 names
[0], strides
[0],
622 image
->planar_format
= f
;
623 for (i
= 0; i
< f
->nplanes
; i
++) {
624 index
= f
->planes
[i
].buffer_index
;
625 image
->offsets
[index
] = offsets
[index
];
626 image
->strides
[index
] = strides
[index
];
633 intel_create_image_from_fds(__DRIscreen
*screen
,
634 int width
, int height
, int fourcc
,
635 int *fds
, int num_fds
, int *strides
, int *offsets
,
638 struct intel_screen
*intelScreen
= screen
->driverPrivate
;
639 struct intel_image_format
*f
;
643 if (fds
== NULL
|| num_fds
!= 1)
646 f
= intel_image_format_lookup(fourcc
);
651 image
= intel_allocate_image(f
->planes
[0].dri_format
, loaderPrivate
);
653 image
= intel_allocate_image(__DRI_IMAGE_FORMAT_NONE
, loaderPrivate
);
658 image
->region
= intel_region_alloc_for_fd(intelScreen
,
659 f
->planes
[0].cpp
, width
, height
, strides
[0],
660 height
* strides
[0], fds
[0], "image");
661 if (image
->region
== NULL
) {
666 image
->planar_format
= f
;
667 for (i
= 0; i
< f
->nplanes
; i
++) {
668 index
= f
->planes
[i
].buffer_index
;
669 image
->offsets
[index
] = offsets
[index
];
670 image
->strides
[index
] = strides
[index
];
673 if (f
->nplanes
== 1) {
674 image
->offset
= image
->offsets
[0];
675 if (image
->region
->tiling
!= I915_TILING_NONE
&& (image
->offset
& 0xfff))
677 "intel_create_image_from_fds: offset not on tile boundary");
680 intel_setup_image_from_dimensions(image
);
686 intel_create_image_from_dma_bufs(__DRIscreen
*screen
,
687 int width
, int height
, int fourcc
,
688 int *fds
, int num_fds
,
689 int *strides
, int *offsets
,
690 enum __DRIYUVColorSpace yuv_color_space
,
691 enum __DRISampleRange sample_range
,
692 enum __DRIChromaSiting horizontal_siting
,
693 enum __DRIChromaSiting vertical_siting
,
698 struct intel_image_format
*f
= intel_image_format_lookup(fourcc
);
700 /* For now only packed formats that have native sampling are supported. */
701 if (!f
|| f
->nplanes
!= 1) {
702 *error
= __DRI_IMAGE_ERROR_BAD_MATCH
;
706 image
= intel_create_image_from_fds(screen
, width
, height
, fourcc
, fds
,
707 num_fds
, strides
, offsets
,
711 * Invalid parameters and any inconsistencies between are assumed to be
712 * checked by the caller. Therefore besides unsupported formats one can fail
713 * only in allocation.
716 *error
= __DRI_IMAGE_ERROR_BAD_ALLOC
;
720 image
->dma_buf_imported
= true;
721 image
->yuv_color_space
= yuv_color_space
;
722 image
->sample_range
= sample_range
;
723 image
->horizontal_siting
= horizontal_siting
;
724 image
->vertical_siting
= vertical_siting
;
726 *error
= __DRI_IMAGE_ERROR_SUCCESS
;
731 intel_from_planar(__DRIimage
*parent
, int plane
, void *loaderPrivate
)
733 int width
, height
, offset
, stride
, dri_format
, index
;
734 struct intel_image_format
*f
;
737 if (parent
== NULL
|| parent
->planar_format
== NULL
)
740 f
= parent
->planar_format
;
742 if (plane
>= f
->nplanes
)
745 width
= parent
->region
->width
>> f
->planes
[plane
].width_shift
;
746 height
= parent
->region
->height
>> f
->planes
[plane
].height_shift
;
747 dri_format
= f
->planes
[plane
].dri_format
;
748 index
= f
->planes
[plane
].buffer_index
;
749 offset
= parent
->offsets
[index
];
750 stride
= parent
->strides
[index
];
752 image
= intel_allocate_image(dri_format
, loaderPrivate
);
756 if (offset
+ height
* stride
> parent
->region
->bo
->size
) {
757 _mesa_warning(NULL
, "intel_create_sub_image: subimage out of bounds");
762 image
->region
= calloc(sizeof(*image
->region
), 1);
763 if (image
->region
== NULL
) {
768 image
->region
->cpp
= _mesa_get_format_bytes(image
->format
);
769 image
->region
->width
= width
;
770 image
->region
->height
= height
;
771 image
->region
->pitch
= stride
;
772 image
->region
->refcount
= 1;
773 image
->region
->bo
= parent
->region
->bo
;
774 drm_intel_bo_reference(image
->region
->bo
);
775 image
->region
->tiling
= parent
->region
->tiling
;
776 image
->offset
= offset
;
777 intel_setup_image_from_dimensions(image
);
781 "intel_create_sub_image: offset not on tile boundary");
786 static const __DRIimageExtension intelImageExtension
= {
787 .base
= { __DRI_IMAGE
, 8 },
789 .createImageFromName
= intel_create_image_from_name
,
790 .createImageFromRenderbuffer
= intel_create_image_from_renderbuffer
,
791 .destroyImage
= intel_destroy_image
,
792 .createImage
= intel_create_image
,
793 .queryImage
= intel_query_image
,
794 .dupImage
= intel_dup_image
,
795 .validateUsage
= intel_validate_usage
,
796 .createImageFromNames
= intel_create_image_from_names
,
797 .fromPlanar
= intel_from_planar
,
798 .createImageFromTexture
= intel_create_image_from_texture
,
799 .createImageFromFds
= intel_create_image_from_fds
,
800 .createImageFromDmaBufs
= intel_create_image_from_dma_bufs
804 brw_query_renderer_integer(__DRIscreen
*psp
, int param
, unsigned int *value
)
806 const struct intel_screen
*const intelScreen
=
807 (struct intel_screen
*) psp
->driverPrivate
;
810 case __DRI2_RENDERER_VENDOR_ID
:
813 case __DRI2_RENDERER_DEVICE_ID
:
814 value
[0] = intelScreen
->deviceID
;
816 case __DRI2_RENDERER_ACCELERATED
:
819 case __DRI2_RENDERER_VIDEO_MEMORY
: {
820 /* Once a batch uses more than 75% of the maximum mappable size, we
821 * assume that there's some fragmentation, and we start doing extra
822 * flushing, etc. That's the big cliff apps will care about.
825 size_t mappable_size
;
827 drm_intel_get_aperture_sizes(psp
->fd
, &mappable_size
, &aper_size
);
829 const unsigned gpu_mappable_megabytes
=
830 (aper_size
/ (1024 * 1024)) * 3 / 4;
832 const long system_memory_pages
= sysconf(_SC_PHYS_PAGES
);
833 const long system_page_size
= sysconf(_SC_PAGE_SIZE
);
835 if (system_memory_pages
<= 0 || system_page_size
<= 0)
838 const uint64_t system_memory_bytes
= (uint64_t) system_memory_pages
839 * (uint64_t) system_page_size
;
841 const unsigned system_memory_megabytes
=
842 (unsigned) (system_memory_bytes
/ (1024 * 1024));
844 value
[0] = MIN2(system_memory_megabytes
, gpu_mappable_megabytes
);
847 case __DRI2_RENDERER_UNIFIED_MEMORY_ARCHITECTURE
:
850 case __DRI2_RENDERER_PREFERRED_PROFILE
:
851 value
[0] = (psp
->max_gl_core_version
!= 0)
852 ? (1U << __DRI_API_OPENGL_CORE
) : (1U << __DRI_API_OPENGL
);
855 return driQueryRendererIntegerCommon(psp
, param
, value
);
862 brw_query_renderer_string(__DRIscreen
*psp
, int param
, const char **value
)
864 const struct intel_screen
*intelScreen
=
865 (struct intel_screen
*) psp
->driverPrivate
;
868 case __DRI2_RENDERER_VENDOR_ID
:
869 value
[0] = brw_vendor_string
;
871 case __DRI2_RENDERER_DEVICE_ID
:
872 value
[0] = brw_get_renderer_string(intelScreen
->deviceID
);
881 static const __DRI2rendererQueryExtension intelRendererQueryExtension
= {
882 .base
= { __DRI2_RENDERER_QUERY
, 1 },
884 .queryInteger
= brw_query_renderer_integer
,
885 .queryString
= brw_query_renderer_string
888 static const __DRIrobustnessExtension dri2Robustness
= {
889 .base
= { __DRI2_ROBUSTNESS
, 1 }
892 static const __DRIextension
*intelScreenExtensions
[] = {
893 &intelTexBufferExtension
.base
,
894 &intelFlushExtension
.base
,
895 &intelImageExtension
.base
,
896 &intelRendererQueryExtension
.base
,
897 &dri2ConfigQueryExtension
.base
,
901 static const __DRIextension
*intelRobustScreenExtensions
[] = {
902 &intelTexBufferExtension
.base
,
903 &intelFlushExtension
.base
,
904 &intelImageExtension
.base
,
905 &intelRendererQueryExtension
.base
,
906 &dri2ConfigQueryExtension
.base
,
907 &dri2Robustness
.base
,
912 intel_get_param(__DRIscreen
*psp
, int param
, int *value
)
915 struct drm_i915_getparam gp
;
917 memset(&gp
, 0, sizeof(gp
));
921 ret
= drmCommandWriteRead(psp
->fd
, DRM_I915_GETPARAM
, &gp
, sizeof(gp
));
924 _mesa_warning(NULL
, "drm_i915_getparam: %d", ret
);
932 intel_get_boolean(__DRIscreen
*psp
, int param
)
935 return intel_get_param(psp
, param
, &value
) && value
;
939 intelDestroyScreen(__DRIscreen
* sPriv
)
941 struct intel_screen
*intelScreen
= sPriv
->driverPrivate
;
943 dri_bufmgr_destroy(intelScreen
->bufmgr
);
944 driDestroyOptionInfo(&intelScreen
->optionCache
);
946 ralloc_free(intelScreen
);
947 sPriv
->driverPrivate
= NULL
;
952 * This is called when we need to set up GL rendering to a new X window.
955 intelCreateBuffer(__DRIscreen
* driScrnPriv
,
956 __DRIdrawable
* driDrawPriv
,
957 const struct gl_config
* mesaVis
, GLboolean isPixmap
)
959 struct intel_renderbuffer
*rb
;
960 struct intel_screen
*screen
= (struct intel_screen
*) driScrnPriv
->driverPrivate
;
961 mesa_format rgbFormat
;
962 unsigned num_samples
= intel_quantize_num_samples(screen
, mesaVis
->samples
);
963 struct gl_framebuffer
*fb
;
968 fb
= CALLOC_STRUCT(gl_framebuffer
);
972 _mesa_initialize_window_framebuffer(fb
, mesaVis
);
974 if (screen
->winsys_msaa_samples_override
!= -1) {
975 num_samples
= screen
->winsys_msaa_samples_override
;
976 fb
->Visual
.samples
= num_samples
;
979 if (mesaVis
->redBits
== 5)
980 rgbFormat
= MESA_FORMAT_B5G6R5_UNORM
;
981 else if (mesaVis
->sRGBCapable
)
982 rgbFormat
= MESA_FORMAT_B8G8R8A8_SRGB
;
983 else if (mesaVis
->alphaBits
== 0)
984 rgbFormat
= MESA_FORMAT_B8G8R8X8_UNORM
;
986 rgbFormat
= MESA_FORMAT_B8G8R8A8_SRGB
;
987 fb
->Visual
.sRGBCapable
= true;
990 /* setup the hardware-based renderbuffers */
991 rb
= intel_create_renderbuffer(rgbFormat
, num_samples
);
992 _mesa_add_renderbuffer(fb
, BUFFER_FRONT_LEFT
, &rb
->Base
.Base
);
994 if (mesaVis
->doubleBufferMode
) {
995 rb
= intel_create_renderbuffer(rgbFormat
, num_samples
);
996 _mesa_add_renderbuffer(fb
, BUFFER_BACK_LEFT
, &rb
->Base
.Base
);
1000 * Assert here that the gl_config has an expected depth/stencil bit
1001 * combination: one of d24/s8, d16/s0, d0/s0. (See intelInitScreen2(),
1002 * which constructs the advertised configs.)
1004 if (mesaVis
->depthBits
== 24) {
1005 assert(mesaVis
->stencilBits
== 8);
1007 if (screen
->devinfo
->has_hiz_and_separate_stencil
) {
1008 rb
= intel_create_private_renderbuffer(MESA_FORMAT_Z24_UNORM_X8_UINT
,
1010 _mesa_add_renderbuffer(fb
, BUFFER_DEPTH
, &rb
->Base
.Base
);
1011 rb
= intel_create_private_renderbuffer(MESA_FORMAT_S_UINT8
,
1013 _mesa_add_renderbuffer(fb
, BUFFER_STENCIL
, &rb
->Base
.Base
);
1016 * Use combined depth/stencil. Note that the renderbuffer is
1017 * attached to two attachment points.
1019 rb
= intel_create_private_renderbuffer(MESA_FORMAT_Z24_UNORM_S8_UINT
,
1021 _mesa_add_renderbuffer(fb
, BUFFER_DEPTH
, &rb
->Base
.Base
);
1022 _mesa_add_renderbuffer(fb
, BUFFER_STENCIL
, &rb
->Base
.Base
);
1025 else if (mesaVis
->depthBits
== 16) {
1026 assert(mesaVis
->stencilBits
== 0);
1027 rb
= intel_create_private_renderbuffer(MESA_FORMAT_Z_UNORM16
,
1029 _mesa_add_renderbuffer(fb
, BUFFER_DEPTH
, &rb
->Base
.Base
);
1032 assert(mesaVis
->depthBits
== 0);
1033 assert(mesaVis
->stencilBits
== 0);
1036 /* now add any/all software-based renderbuffers we may need */
1037 _swrast_add_soft_renderbuffers(fb
,
1038 false, /* never sw color */
1039 false, /* never sw depth */
1040 false, /* never sw stencil */
1041 mesaVis
->accumRedBits
> 0,
1042 false, /* never sw alpha */
1043 false /* never sw aux */ );
1044 driDrawPriv
->driverPrivate
= fb
;
1050 intelDestroyBuffer(__DRIdrawable
* driDrawPriv
)
1052 struct gl_framebuffer
*fb
= driDrawPriv
->driverPrivate
;
1054 _mesa_reference_framebuffer(&fb
, NULL
);
1058 intel_init_bufmgr(struct intel_screen
*intelScreen
)
1060 __DRIscreen
*spriv
= intelScreen
->driScrnPriv
;
1062 intelScreen
->no_hw
= getenv("INTEL_NO_HW") != NULL
;
1064 intelScreen
->bufmgr
= intel_bufmgr_gem_init(spriv
->fd
, BATCH_SZ
);
1065 if (intelScreen
->bufmgr
== NULL
) {
1066 fprintf(stderr
, "[%s:%u] Error initializing buffer manager.\n",
1067 __func__
, __LINE__
);
1071 drm_intel_bufmgr_gem_enable_fenced_relocs(intelScreen
->bufmgr
);
1073 if (!intel_get_boolean(spriv
, I915_PARAM_HAS_RELAXED_DELTA
)) {
1074 fprintf(stderr
, "[%s: %u] Kernel 2.6.39 required.\n", __func__
, __LINE__
);
1082 intel_detect_swizzling(struct intel_screen
*screen
)
1084 drm_intel_bo
*buffer
;
1085 unsigned long flags
= 0;
1086 unsigned long aligned_pitch
;
1087 uint32_t tiling
= I915_TILING_X
;
1088 uint32_t swizzle_mode
= 0;
1090 buffer
= drm_intel_bo_alloc_tiled(screen
->bufmgr
, "swizzle test",
1092 &tiling
, &aligned_pitch
, flags
);
1096 drm_intel_bo_get_tiling(buffer
, &tiling
, &swizzle_mode
);
1097 drm_intel_bo_unreference(buffer
);
1099 if (swizzle_mode
== I915_BIT_6_SWIZZLE_NONE
)
1106 * Return array of MSAA modes supported by the hardware. The array is
1107 * zero-terminated and sorted in decreasing order.
1110 intel_supported_msaa_modes(const struct intel_screen
*screen
)
1112 static const int gen8_modes
[] = {8, 4, 2, 0, -1};
1113 static const int gen7_modes
[] = {8, 4, 0, -1};
1114 static const int gen6_modes
[] = {4, 0, -1};
1115 static const int gen4_modes
[] = {0, -1};
1117 if (screen
->devinfo
->gen
>= 8) {
1119 } else if (screen
->devinfo
->gen
>= 7) {
1121 } else if (screen
->devinfo
->gen
== 6) {
1128 static __DRIconfig
**
1129 intel_screen_make_configs(__DRIscreen
*dri_screen
)
1131 static const mesa_format formats
[] = {
1132 MESA_FORMAT_B5G6R5_UNORM
,
1133 MESA_FORMAT_B8G8R8A8_UNORM
1136 /* GLX_SWAP_COPY_OML is not supported due to page flipping. */
1137 static const GLenum back_buffer_modes
[] = {
1138 GLX_SWAP_UNDEFINED_OML
, GLX_NONE
,
1141 static const uint8_t singlesample_samples
[1] = {0};
1142 static const uint8_t multisample_samples
[2] = {4, 8};
1144 struct intel_screen
*screen
= dri_screen
->driverPrivate
;
1145 const struct brw_device_info
*devinfo
= screen
->devinfo
;
1146 uint8_t depth_bits
[4], stencil_bits
[4];
1147 __DRIconfig
**configs
= NULL
;
1149 /* Generate singlesample configs without accumulation buffer. */
1150 for (int i
= 0; i
< ARRAY_SIZE(formats
); i
++) {
1151 __DRIconfig
**new_configs
;
1152 int num_depth_stencil_bits
= 2;
1154 /* Starting with DRI2 protocol version 1.1 we can request a depth/stencil
1155 * buffer that has a different number of bits per pixel than the color
1156 * buffer, gen >= 6 supports this.
1159 stencil_bits
[0] = 0;
1161 if (formats
[i
] == MESA_FORMAT_B5G6R5_UNORM
) {
1163 stencil_bits
[1] = 0;
1164 if (devinfo
->gen
>= 6) {
1166 stencil_bits
[2] = 8;
1167 num_depth_stencil_bits
= 3;
1171 stencil_bits
[1] = 8;
1174 new_configs
= driCreateConfigs(formats
[i
],
1177 num_depth_stencil_bits
,
1178 back_buffer_modes
, 2,
1179 singlesample_samples
, 1,
1181 configs
= driConcatConfigs(configs
, new_configs
);
1184 /* Generate the minimum possible set of configs that include an
1185 * accumulation buffer.
1187 for (int i
= 0; i
< ARRAY_SIZE(formats
); i
++) {
1188 __DRIconfig
**new_configs
;
1190 if (formats
[i
] == MESA_FORMAT_B5G6R5_UNORM
) {
1192 stencil_bits
[0] = 0;
1195 stencil_bits
[0] = 8;
1198 new_configs
= driCreateConfigs(formats
[i
],
1199 depth_bits
, stencil_bits
, 1,
1200 back_buffer_modes
, 1,
1201 singlesample_samples
, 1,
1203 configs
= driConcatConfigs(configs
, new_configs
);
1206 /* Generate multisample configs.
1208 * This loop breaks early, and hence is a no-op, on gen < 6.
1210 * Multisample configs must follow the singlesample configs in order to
1211 * work around an X server bug present in 1.12. The X server chooses to
1212 * associate the first listed RGBA888-Z24S8 config, regardless of its
1213 * sample count, with the 32-bit depth visual used for compositing.
1215 * Only doublebuffer configs with GLX_SWAP_UNDEFINED_OML behavior are
1216 * supported. Singlebuffer configs are not supported because no one wants
1219 for (int i
= 0; i
< ARRAY_SIZE(formats
); i
++) {
1220 if (devinfo
->gen
< 6)
1223 __DRIconfig
**new_configs
;
1224 const int num_depth_stencil_bits
= 2;
1225 int num_msaa_modes
= 0;
1228 stencil_bits
[0] = 0;
1230 if (formats
[i
] == MESA_FORMAT_B5G6R5_UNORM
) {
1232 stencil_bits
[1] = 0;
1235 stencil_bits
[1] = 8;
1238 if (devinfo
->gen
>= 7)
1240 else if (devinfo
->gen
== 6)
1243 new_configs
= driCreateConfigs(formats
[i
],
1246 num_depth_stencil_bits
,
1247 back_buffer_modes
, 1,
1248 multisample_samples
,
1251 configs
= driConcatConfigs(configs
, new_configs
);
1254 if (configs
== NULL
) {
1255 fprintf(stderr
, "[%s:%u] Error creating FBConfig!\n", __func__
,
1264 set_max_gl_versions(struct intel_screen
*screen
)
1266 __DRIscreen
*psp
= screen
->driScrnPriv
;
1268 switch (screen
->devinfo
->gen
) {
1271 psp
->max_gl_core_version
= 33;
1272 psp
->max_gl_compat_version
= 30;
1273 psp
->max_gl_es1_version
= 11;
1274 psp
->max_gl_es2_version
= 30;
1277 psp
->max_gl_core_version
= 31;
1278 psp
->max_gl_compat_version
= 30;
1279 psp
->max_gl_es1_version
= 11;
1280 psp
->max_gl_es2_version
= 30;
1284 psp
->max_gl_core_version
= 0;
1285 psp
->max_gl_compat_version
= 21;
1286 psp
->max_gl_es1_version
= 11;
1287 psp
->max_gl_es2_version
= 20;
1290 assert(!"unrecognized intel_screen::gen");
1296 * This is the driver specific part of the createNewScreen entry point.
1297 * Called when using DRI2.
1299 * \return the struct gl_config supported by this driver
1302 __DRIconfig
**intelInitScreen2(__DRIscreen
*psp
)
1304 struct intel_screen
*intelScreen
;
1306 if (psp
->image
.loader
) {
1307 } else if (psp
->dri2
.loader
->base
.version
<= 2 ||
1308 psp
->dri2
.loader
->getBuffersWithFormat
== NULL
) {
1310 "\nERROR! DRI2 loader with getBuffersWithFormat() "
1311 "support required\n");
1315 /* Allocate the private area */
1316 intelScreen
= rzalloc(NULL
, struct intel_screen
);
1318 fprintf(stderr
, "\nERROR! Allocating private area failed\n");
1321 /* parse information in __driConfigOptions */
1322 driParseOptionInfo(&intelScreen
->optionCache
, brw_config_options
.xml
);
1324 intelScreen
->driScrnPriv
= psp
;
1325 psp
->driverPrivate
= (void *) intelScreen
;
1327 if (!intel_init_bufmgr(intelScreen
))
1330 intelScreen
->deviceID
= drm_intel_bufmgr_gem_get_devid(intelScreen
->bufmgr
);
1331 intelScreen
->devinfo
= brw_get_device_info(intelScreen
->deviceID
);
1332 if (!intelScreen
->devinfo
)
1335 intelScreen
->hw_must_use_separate_stencil
= intelScreen
->devinfo
->gen
>= 7;
1337 intelScreen
->hw_has_swizzling
= intel_detect_swizzling(intelScreen
);
1339 const char *force_msaa
= getenv("INTEL_FORCE_MSAA");
1341 intelScreen
->winsys_msaa_samples_override
=
1342 intel_quantize_num_samples(intelScreen
, atoi(force_msaa
));
1343 printf("Forcing winsys sample count to %d\n",
1344 intelScreen
->winsys_msaa_samples_override
);
1346 intelScreen
->winsys_msaa_samples_override
= -1;
1349 set_max_gl_versions(intelScreen
);
1351 /* Notification of GPU resets requires hardware contexts and a kernel new
1352 * enough to support DRM_IOCTL_I915_GET_RESET_STATS. If the ioctl is
1353 * supported, calling it with a context of 0 will either generate EPERM or
1354 * no error. If the ioctl is not supported, it always generate EINVAL.
1355 * Use this to determine whether to advertise the __DRI2_ROBUSTNESS
1356 * extension to the loader.
1358 * Don't even try on pre-Gen6, since we don't attempt to use contexts there.
1360 if (intelScreen
->devinfo
->gen
>= 6) {
1361 struct drm_i915_reset_stats stats
;
1362 memset(&stats
, 0, sizeof(stats
));
1364 const int ret
= drmIoctl(psp
->fd
, DRM_IOCTL_I915_GET_RESET_STATS
, &stats
);
1366 intelScreen
->has_context_reset_notification
=
1367 (ret
!= -1 || errno
!= EINVAL
);
1370 psp
->extensions
= !intelScreen
->has_context_reset_notification
1371 ? intelScreenExtensions
: intelRobustScreenExtensions
;
1373 brw_fs_alloc_reg_sets(intelScreen
);
1374 brw_vec4_alloc_reg_set(intelScreen
);
1376 return (const __DRIconfig
**) intel_screen_make_configs(psp
);
1379 struct intel_buffer
{
1381 struct intel_region
*region
;
1384 static __DRIbuffer
*
1385 intelAllocateBuffer(__DRIscreen
*screen
,
1386 unsigned attachment
, unsigned format
,
1387 int width
, int height
)
1389 struct intel_buffer
*intelBuffer
;
1390 struct intel_screen
*intelScreen
= screen
->driverPrivate
;
1392 assert(attachment
== __DRI_BUFFER_FRONT_LEFT
||
1393 attachment
== __DRI_BUFFER_BACK_LEFT
);
1395 intelBuffer
= calloc(1, sizeof *intelBuffer
);
1396 if (intelBuffer
== NULL
)
1399 /* The front and back buffers are color buffers, which are X tiled. */
1400 intelBuffer
->region
= intel_region_alloc(intelScreen
,
1407 if (intelBuffer
->region
== NULL
) {
1412 intel_region_flink(intelBuffer
->region
, &intelBuffer
->base
.name
);
1414 intelBuffer
->base
.attachment
= attachment
;
1415 intelBuffer
->base
.cpp
= intelBuffer
->region
->cpp
;
1416 intelBuffer
->base
.pitch
= intelBuffer
->region
->pitch
;
1418 return &intelBuffer
->base
;
1422 intelReleaseBuffer(__DRIscreen
*screen
, __DRIbuffer
*buffer
)
1424 struct intel_buffer
*intelBuffer
= (struct intel_buffer
*) buffer
;
1426 intel_region_release(&intelBuffer
->region
);
1430 static const struct __DriverAPIRec brw_driver_api
= {
1431 .InitScreen
= intelInitScreen2
,
1432 .DestroyScreen
= intelDestroyScreen
,
1433 .CreateContext
= brwCreateContext
,
1434 .DestroyContext
= intelDestroyContext
,
1435 .CreateBuffer
= intelCreateBuffer
,
1436 .DestroyBuffer
= intelDestroyBuffer
,
1437 .MakeCurrent
= intelMakeCurrent
,
1438 .UnbindContext
= intelUnbindContext
,
1439 .AllocateBuffer
= intelAllocateBuffer
,
1440 .ReleaseBuffer
= intelReleaseBuffer
1443 static const struct __DRIDriverVtableExtensionRec brw_vtable
= {
1444 .base
= { __DRI_DRIVER_VTABLE
, 1 },
1445 .vtable
= &brw_driver_api
,
1448 static const __DRIextension
*brw_driver_extensions
[] = {
1449 &driCoreExtension
.base
,
1450 &driImageDriverExtension
.base
,
1451 &driDRI2Extension
.base
,
1453 &brw_config_options
.base
,
1457 PUBLIC
const __DRIextension
**__driDriverGetExtensions_i965(void)
1459 globalDriverAPI
= &brw_driver_api
;
1461 return brw_driver_extensions
;