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 "drm-uapi/drm_fourcc.h"
32 #include "main/glheader.h"
33 #include "main/context.h"
34 #include "main/framebuffer.h"
35 #include "main/renderbuffer.h"
36 #include "main/texobj.h"
37 #include "main/hash.h"
38 #include "main/fbobject.h"
39 #include "main/version.h"
40 #include "swrast/s_renderbuffer.h"
43 #include "util/xmlpool.h"
44 #include "util/u_memory.h"
46 static const __DRIconfigOptionsExtension i915_config_options
= {
47 .base
= { __DRI_CONFIG_OPTIONS
, 1 },
51 DRI_CONF_SECTION_PERFORMANCE
52 /* Options correspond to DRI_CONF_BO_REUSE_DISABLED,
53 * DRI_CONF_BO_REUSE_ALL
55 DRI_CONF_OPT_BEGIN_V(bo_reuse
, enum, 1, "0:1")
56 DRI_CONF_DESC_BEGIN("Buffer object reuse")
57 DRI_CONF_ENUM(0, "Disable buffer object reuse")
58 DRI_CONF_ENUM(1, "Enable reuse of all sizes of buffer objects")
62 DRI_CONF_OPT_BEGIN_B(fragment_shader
, "true")
63 DRI_CONF_DESC("Enable limited ARB_fragment_shader support on 915/945.")
67 DRI_CONF_SECTION_QUALITY
69 DRI_CONF_SECTION_DEBUG
70 DRI_CONF_ALWAYS_FLUSH_BATCH("false")
71 DRI_CONF_ALWAYS_FLUSH_CACHE("false")
72 DRI_CONF_DISABLE_THROTTLING("false")
73 DRI_CONF_FORCE_GLSL_EXTENSIONS_WARN("false")
74 DRI_CONF_DISABLE_GLSL_LINE_CONTINUATIONS("false")
75 DRI_CONF_DISABLE_BLEND_FUNC_EXTENDED("false")
77 DRI_CONF_OPT_BEGIN_B(stub_occlusion_query
, "false")
78 DRI_CONF_DESC("Enable stub ARB_occlusion_query support on 915/945.")
81 DRI_CONF_OPT_BEGIN_B(shader_precompile
, "true")
82 DRI_CONF_DESC("Perform code generation at shader link time.")
88 #include "intel_batchbuffer.h"
89 #include "intel_buffers.h"
90 #include "intel_bufmgr.h"
91 #include "intel_chipset.h"
92 #include "intel_fbo.h"
93 #include "intel_mipmap_tree.h"
94 #include "intel_screen.h"
95 #include "intel_tex.h"
96 #include "intel_regions.h"
98 #include "drm-uapi/i915_drm.h"
101 * For debugging purposes, this returns a time in seconds.
108 clock_gettime(CLOCK_MONOTONIC
, &tp
);
110 return tp
.tv_sec
+ tp
.tv_nsec
/ 1000000000.0;
114 aub_dump_bmp(struct gl_context
*ctx
)
116 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
118 for (int i
= 0; i
< fb
->_NumColorDrawBuffers
; i
++) {
119 struct intel_renderbuffer
*irb
=
120 intel_renderbuffer(fb
->_ColorDrawBuffers
[i
]);
122 if (irb
&& irb
->mt
) {
123 enum aub_dump_bmp_format format
;
125 switch (irb
->Base
.Base
.Format
) {
126 case MESA_FORMAT_B8G8R8A8_UNORM
:
127 case MESA_FORMAT_B8G8R8X8_UNORM
:
128 format
= AUB_DUMP_BMP_FORMAT_ARGB_8888
;
134 assert(irb
->mt
->region
->pitch
% irb
->mt
->region
->cpp
== 0);
135 drm_intel_gem_bo_aub_dump_bmp(irb
->mt
->region
->bo
,
138 irb
->Base
.Base
.Width
,
139 irb
->Base
.Base
.Height
,
141 irb
->mt
->region
->pitch
,
147 static const __DRItexBufferExtension intelTexBufferExtension
= {
148 .base
= { __DRI_TEX_BUFFER
, 3 },
150 .setTexBuffer
= intelSetTexBuffer
,
151 .setTexBuffer2
= intelSetTexBuffer2
,
152 .releaseTexBuffer
= NULL
,
156 intelDRI2Flush(__DRIdrawable
*drawable
)
158 GET_CURRENT_CONTEXT(ctx
);
159 struct intel_context
*intel
= intel_context(ctx
);
163 INTEL_FIREVERTICES(intel
);
165 intel
->need_throttle
= true;
167 if (intel
->batch
.used
)
168 intel_batchbuffer_flush(intel
);
170 if (INTEL_DEBUG
& DEBUG_AUB
) {
175 static const struct __DRI2flushExtensionRec intelFlushExtension
= {
176 .base
= { __DRI2_FLUSH
, 3 },
178 .flush
= intelDRI2Flush
,
179 .invalidate
= dri2InvalidateDrawable
,
182 static struct intel_image_format intel_image_formats
[] = {
183 { DRM_FORMAT_ARGB8888
, __DRI_IMAGE_COMPONENTS_RGBA
, 1,
184 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ARGB8888
, 4 } } },
186 { __DRI_IMAGE_FOURCC_SARGB8888
, __DRI_IMAGE_COMPONENTS_RGBA
, 1,
187 { { 0, 0, 0, __DRI_IMAGE_FORMAT_SARGB8
, 4 } } },
189 { DRM_FORMAT_XRGB8888
, __DRI_IMAGE_COMPONENTS_RGB
, 1,
190 { { 0, 0, 0, __DRI_IMAGE_FORMAT_XRGB8888
, 4 }, } },
192 { DRM_FORMAT_YUV410
, __DRI_IMAGE_COMPONENTS_Y_U_V
, 3,
193 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8
, 1 },
194 { 1, 2, 2, __DRI_IMAGE_FORMAT_R8
, 1 },
195 { 2, 2, 2, __DRI_IMAGE_FORMAT_R8
, 1 } } },
197 { DRM_FORMAT_YUV411
, __DRI_IMAGE_COMPONENTS_Y_U_V
, 3,
198 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8
, 1 },
199 { 1, 2, 0, __DRI_IMAGE_FORMAT_R8
, 1 },
200 { 2, 2, 0, __DRI_IMAGE_FORMAT_R8
, 1 } } },
202 { DRM_FORMAT_YUV420
, __DRI_IMAGE_COMPONENTS_Y_U_V
, 3,
203 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8
, 1 },
204 { 1, 1, 1, __DRI_IMAGE_FORMAT_R8
, 1 },
205 { 2, 1, 1, __DRI_IMAGE_FORMAT_R8
, 1 } } },
207 { DRM_FORMAT_YUV422
, __DRI_IMAGE_COMPONENTS_Y_U_V
, 3,
208 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8
, 1 },
209 { 1, 1, 0, __DRI_IMAGE_FORMAT_R8
, 1 },
210 { 2, 1, 0, __DRI_IMAGE_FORMAT_R8
, 1 } } },
212 { DRM_FORMAT_YUV444
, __DRI_IMAGE_COMPONENTS_Y_U_V
, 3,
213 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8
, 1 },
214 { 1, 0, 0, __DRI_IMAGE_FORMAT_R8
, 1 },
215 { 2, 0, 0, __DRI_IMAGE_FORMAT_R8
, 1 } } },
217 { DRM_FORMAT_NV12
, __DRI_IMAGE_COMPONENTS_Y_UV
, 2,
218 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8
, 1 },
219 { 1, 1, 1, __DRI_IMAGE_FORMAT_GR88
, 2 } } },
221 { DRM_FORMAT_NV16
, __DRI_IMAGE_COMPONENTS_Y_UV
, 2,
222 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8
, 1 },
223 { 1, 1, 0, __DRI_IMAGE_FORMAT_GR88
, 2 } } },
225 /* For YUYV and UYVY buffers, we set up two overlapping DRI images
226 * and treat them as planar buffers in the compositors.
227 * Plane 0 is GR88 and samples YU or YV pairs and places Y into
228 * the R component, while plane 1 is ARGB/ABGR and samples YUYV/UYVY
229 * clusters and places pairs and places U into the G component and
230 * V into A. This lets the texture sampler interpolate the Y
231 * components correctly when sampling from plane 0, and interpolate
232 * U and V correctly when sampling from plane 1. */
233 { DRM_FORMAT_YUYV
, __DRI_IMAGE_COMPONENTS_Y_XUXV
, 2,
234 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88
, 2 },
235 { 0, 1, 0, __DRI_IMAGE_FORMAT_ARGB8888
, 4 } } },
236 { DRM_FORMAT_UYVY
, __DRI_IMAGE_COMPONENTS_Y_UXVX
, 2,
237 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88
, 2 },
238 { 0, 1, 0, __DRI_IMAGE_FORMAT_ABGR8888
, 4 } } }
242 intel_allocate_image(int dri_format
, void *loaderPrivate
)
246 image
= calloc(1, sizeof *image
);
250 image
->dri_format
= dri_format
;
253 image
->format
= driImageFormatToGLFormat(dri_format
);
254 if (dri_format
!= __DRI_IMAGE_FORMAT_NONE
&&
255 image
->format
== MESA_FORMAT_NONE
) {
260 image
->internal_format
= _mesa_get_format_base_format(image
->format
);
261 image
->data
= loaderPrivate
;
267 * Sets up a DRIImage structure to point to our shared image in a region
270 intel_setup_image_from_mipmap_tree(struct intel_context
*intel
, __DRIimage
*image
,
271 struct intel_mipmap_tree
*mt
, GLuint level
,
274 unsigned int draw_x
, draw_y
;
275 uint32_t mask_x
, mask_y
;
277 intel_miptree_check_level_layer(mt
, level
, zoffset
);
279 intel_region_get_tile_masks(mt
->region
, &mask_x
, &mask_y
);
280 intel_miptree_get_image_offset(mt
, level
, zoffset
, &draw_x
, &draw_y
);
282 image
->width
= mt
->level
[level
].width
;
283 image
->height
= mt
->level
[level
].height
;
284 image
->tile_x
= draw_x
& mask_x
;
285 image
->tile_y
= draw_y
& mask_y
;
287 image
->offset
= intel_region_get_aligned_offset(mt
->region
,
291 intel_region_reference(&image
->region
, mt
->region
);
295 intel_setup_image_from_dimensions(__DRIimage
*image
)
297 image
->width
= image
->region
->width
;
298 image
->height
= image
->region
->height
;
304 intel_create_image_from_name(__DRIscreen
*screen
,
305 int width
, int height
, int format
,
306 int name
, int pitch
, void *loaderPrivate
)
308 struct intel_screen
*intelScreen
= screen
->driverPrivate
;
312 image
= intel_allocate_image(format
, loaderPrivate
);
316 if (image
->format
== MESA_FORMAT_NONE
)
319 cpp
= _mesa_get_format_bytes(image
->format
);
320 image
->region
= intel_region_alloc_for_handle(intelScreen
,
322 pitch
* cpp
, name
, "image");
323 if (image
->region
== NULL
) {
328 intel_setup_image_from_dimensions(image
);
334 intel_create_image_from_renderbuffer(__DRIcontext
*context
,
335 int renderbuffer
, void *loaderPrivate
)
338 struct intel_context
*intel
= context
->driverPrivate
;
339 struct gl_renderbuffer
*rb
;
340 struct intel_renderbuffer
*irb
;
342 rb
= _mesa_lookup_renderbuffer(&intel
->ctx
, renderbuffer
);
344 _mesa_error(&intel
->ctx
,
345 GL_INVALID_OPERATION
, "glRenderbufferExternalMESA");
349 irb
= intel_renderbuffer(rb
);
350 image
= calloc(1, sizeof *image
);
354 image
->internal_format
= rb
->InternalFormat
;
355 image
->format
= rb
->Format
;
357 image
->data
= loaderPrivate
;
358 intel_region_reference(&image
->region
, irb
->mt
->region
);
359 intel_setup_image_from_dimensions(image
);
360 image
->dri_format
= driGLFormatToImageFormat(image
->format
);
362 rb
->NeedsFinishRenderTexture
= true;
367 intel_create_image_from_texture(__DRIcontext
*context
, int target
,
368 unsigned texture
, int zoffset
,
374 struct intel_context
*intel
= context
->driverPrivate
;
375 struct gl_texture_object
*obj
;
376 struct intel_texture_object
*iobj
;
379 obj
= _mesa_lookup_texture(&intel
->ctx
, texture
);
380 if (!obj
|| obj
->Target
!= target
) {
381 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
385 if (target
== GL_TEXTURE_CUBE_MAP
)
388 _mesa_test_texobj_completeness(&intel
->ctx
, obj
);
389 iobj
= intel_texture_object(obj
);
390 if (!obj
->_BaseComplete
|| (level
> 0 && !obj
->_MipmapComplete
)) {
391 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
395 if (level
< obj
->BaseLevel
|| level
> obj
->_MaxLevel
) {
396 *error
= __DRI_IMAGE_ERROR_BAD_MATCH
;
400 if (target
== GL_TEXTURE_3D
&& obj
->Image
[face
][level
]->Depth
< zoffset
) {
401 *error
= __DRI_IMAGE_ERROR_BAD_MATCH
;
404 image
= calloc(1, sizeof *image
);
406 *error
= __DRI_IMAGE_ERROR_BAD_ALLOC
;
410 image
->internal_format
= obj
->Image
[face
][level
]->InternalFormat
;
411 image
->format
= obj
->Image
[face
][level
]->TexFormat
;
412 image
->data
= loaderPrivate
;
413 intel_setup_image_from_mipmap_tree(intel
, image
, iobj
->mt
, level
, zoffset
);
414 image
->dri_format
= driGLFormatToImageFormat(image
->format
);
415 if (image
->dri_format
== __DRI_IMAGE_FORMAT_NONE
) {
416 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
421 *error
= __DRI_IMAGE_ERROR_SUCCESS
;
426 intel_destroy_image(__DRIimage
*image
)
428 intel_region_release(&image
->region
);
433 intel_create_image(__DRIscreen
*screen
,
434 int width
, int height
, int format
,
439 struct intel_screen
*intelScreen
= screen
->driverPrivate
;
443 tiling
= I915_TILING_X
;
444 if (use
& __DRI_IMAGE_USE_CURSOR
) {
445 if (width
!= 64 || height
!= 64)
447 tiling
= I915_TILING_NONE
;
450 if (use
& __DRI_IMAGE_USE_LINEAR
)
451 tiling
= I915_TILING_NONE
;
453 image
= intel_allocate_image(format
, loaderPrivate
);
457 cpp
= _mesa_get_format_bytes(image
->format
);
459 intel_region_alloc(intelScreen
, tiling
, cpp
, width
, height
, true);
460 if (image
->region
== NULL
) {
465 intel_setup_image_from_dimensions(image
);
471 intel_query_image(__DRIimage
*image
, int attrib
, int *value
)
474 case __DRI_IMAGE_ATTRIB_STRIDE
:
475 *value
= image
->region
->pitch
;
477 case __DRI_IMAGE_ATTRIB_HANDLE
:
478 *value
= image
->region
->bo
->handle
;
480 case __DRI_IMAGE_ATTRIB_NAME
:
481 return intel_region_flink(image
->region
, (uint32_t *) value
);
482 case __DRI_IMAGE_ATTRIB_FORMAT
:
483 *value
= image
->dri_format
;
485 case __DRI_IMAGE_ATTRIB_WIDTH
:
486 *value
= image
->region
->width
;
488 case __DRI_IMAGE_ATTRIB_HEIGHT
:
489 *value
= image
->region
->height
;
491 case __DRI_IMAGE_ATTRIB_COMPONENTS
:
492 if (image
->planar_format
== NULL
)
494 *value
= image
->planar_format
->components
;
496 case __DRI_IMAGE_ATTRIB_FD
:
497 return !drm_intel_bo_gem_export_to_prime(image
->region
->bo
, value
);
504 intel_dup_image(__DRIimage
*orig_image
, void *loaderPrivate
)
508 image
= calloc(1, sizeof *image
);
512 intel_region_reference(&image
->region
, orig_image
->region
);
513 if (image
->region
== NULL
) {
518 image
->internal_format
= orig_image
->internal_format
;
519 image
->planar_format
= orig_image
->planar_format
;
520 image
->dri_format
= orig_image
->dri_format
;
521 image
->format
= orig_image
->format
;
522 image
->offset
= orig_image
->offset
;
523 image
->width
= orig_image
->width
;
524 image
->height
= orig_image
->height
;
525 image
->tile_x
= orig_image
->tile_x
;
526 image
->tile_y
= orig_image
->tile_y
;
527 image
->data
= loaderPrivate
;
529 memcpy(image
->strides
, orig_image
->strides
, sizeof(image
->strides
));
530 memcpy(image
->offsets
, orig_image
->offsets
, sizeof(image
->offsets
));
536 intel_validate_usage(__DRIimage
*image
, unsigned int use
)
538 if (use
& __DRI_IMAGE_USE_CURSOR
) {
539 if (image
->region
->width
!= 64 || image
->region
->height
!= 64)
547 intel_create_image_from_names(__DRIscreen
*screen
,
548 int width
, int height
, int fourcc
,
549 int *names
, int num_names
,
550 int *strides
, int *offsets
,
553 struct intel_image_format
*f
= NULL
;
557 if (screen
== NULL
|| names
== NULL
|| num_names
!= 1)
560 for (i
= 0; i
< ARRAY_SIZE(intel_image_formats
); i
++) {
561 if (intel_image_formats
[i
].fourcc
== fourcc
) {
562 f
= &intel_image_formats
[i
];
569 image
= intel_create_image_from_name(screen
, width
, height
,
570 __DRI_IMAGE_FORMAT_NONE
,
571 names
[0], strides
[0],
577 image
->planar_format
= f
;
578 for (i
= 0; i
< f
->nplanes
; i
++) {
579 index
= f
->planes
[i
].buffer_index
;
580 image
->offsets
[index
] = offsets
[index
];
581 image
->strides
[index
] = strides
[index
];
588 intel_create_image_from_fds(__DRIscreen
*screen
,
589 int width
, int height
, int fourcc
,
590 int *fds
, int num_fds
, int *strides
, int *offsets
,
593 struct intel_screen
*intelScreen
= screen
->driverPrivate
;
594 struct intel_image_format
*f
= NULL
;
598 if (fds
== NULL
|| num_fds
!= 1)
601 for (i
= 0; i
< ARRAY_SIZE(intel_image_formats
); i
++) {
602 if (intel_image_formats
[i
].fourcc
== fourcc
) {
603 f
= &intel_image_formats
[i
];
610 image
= intel_allocate_image(__DRI_IMAGE_FORMAT_NONE
, loaderPrivate
);
614 image
->region
= intel_region_alloc_for_fd(intelScreen
,
615 f
->planes
[0].cpp
, width
, height
, strides
[0],
616 height
* strides
[0], fds
[0], "image");
617 if (image
->region
== NULL
) {
622 intel_setup_image_from_dimensions(image
);
624 image
->planar_format
= f
;
625 for (i
= 0; i
< f
->nplanes
; i
++) {
626 index
= f
->planes
[i
].buffer_index
;
627 image
->offsets
[index
] = offsets
[index
];
628 image
->strides
[index
] = strides
[index
];
636 intel_from_planar(__DRIimage
*parent
, int plane
, void *loaderPrivate
)
638 int width
, height
, offset
, stride
, dri_format
, index
;
639 struct intel_image_format
*f
;
640 uint32_t mask_x
, mask_y
;
643 if (parent
== NULL
|| parent
->planar_format
== NULL
)
646 f
= parent
->planar_format
;
648 if (plane
>= f
->nplanes
)
651 width
= parent
->region
->width
>> f
->planes
[plane
].width_shift
;
652 height
= parent
->region
->height
>> f
->planes
[plane
].height_shift
;
653 dri_format
= f
->planes
[plane
].dri_format
;
654 index
= f
->planes
[plane
].buffer_index
;
655 offset
= parent
->offsets
[index
];
656 stride
= parent
->strides
[index
];
658 image
= intel_allocate_image(dri_format
, loaderPrivate
);
662 if (offset
+ height
* stride
> parent
->region
->bo
->size
) {
663 _mesa_warning(NULL
, "intel_create_sub_image: subimage out of bounds");
668 image
->region
= calloc(sizeof(*image
->region
), 1);
669 if (image
->region
== NULL
) {
674 image
->region
->cpp
= _mesa_get_format_bytes(image
->format
);
675 image
->region
->width
= width
;
676 image
->region
->height
= height
;
677 image
->region
->pitch
= stride
;
678 image
->region
->refcount
= 1;
679 image
->region
->bo
= parent
->region
->bo
;
680 drm_intel_bo_reference(image
->region
->bo
);
681 image
->region
->tiling
= parent
->region
->tiling
;
682 image
->offset
= offset
;
683 intel_setup_image_from_dimensions(image
);
685 intel_region_get_tile_masks(image
->region
, &mask_x
, &mask_y
);
688 "intel_create_sub_image: offset not on tile boundary");
693 static const __DRIimageExtension intelImageExtension
= {
694 .base
= { __DRI_IMAGE
, 7 },
696 .createImageFromName
= intel_create_image_from_name
,
697 .createImageFromRenderbuffer
= intel_create_image_from_renderbuffer
,
698 .destroyImage
= intel_destroy_image
,
699 .createImage
= intel_create_image
,
700 .queryImage
= intel_query_image
,
701 .dupImage
= intel_dup_image
,
702 .validateUsage
= intel_validate_usage
,
703 .createImageFromNames
= intel_create_image_from_names
,
704 .fromPlanar
= intel_from_planar
,
705 .createImageFromTexture
= intel_create_image_from_texture
,
706 .createImageFromFds
= intel_create_image_from_fds
710 i915_query_renderer_integer(__DRIscreen
*psp
, int param
, unsigned int *value
)
712 const struct intel_screen
*const intelScreen
=
713 (struct intel_screen
*) psp
->driverPrivate
;
716 case __DRI2_RENDERER_VENDOR_ID
:
719 case __DRI2_RENDERER_DEVICE_ID
:
720 value
[0] = intelScreen
->deviceID
;
722 case __DRI2_RENDERER_ACCELERATED
:
725 case __DRI2_RENDERER_VIDEO_MEMORY
: {
726 /* Once a batch uses more than 75% of the maximum mappable size, we
727 * assume that there's some fragmentation, and we start doing extra
728 * flushing, etc. That's the big cliff apps will care about.
731 size_t mappable_size
;
733 drm_intel_get_aperture_sizes(psp
->fd
, &mappable_size
, &aper_size
);
735 const unsigned gpu_mappable_megabytes
=
736 (aper_size
/ (1024 * 1024)) * 3 / 4;
738 const long system_memory_pages
= sysconf(_SC_PHYS_PAGES
);
739 const long system_page_size
= sysconf(_SC_PAGE_SIZE
);
741 if (system_memory_pages
<= 0 || system_page_size
<= 0)
744 const uint64_t system_memory_bytes
= (uint64_t) system_memory_pages
745 * (uint64_t) system_page_size
;
747 const unsigned system_memory_megabytes
=
748 (unsigned) (system_memory_bytes
/ (1024 * 1024));
750 value
[0] = MIN2(system_memory_megabytes
, gpu_mappable_megabytes
);
753 case __DRI2_RENDERER_UNIFIED_MEMORY_ARCHITECTURE
:
756 case __DRI2_RENDERER_HAS_TEXTURE_3D
:
760 return driQueryRendererIntegerCommon(psp
, param
, value
);
767 i915_query_renderer_string(__DRIscreen
*psp
, int param
, const char **value
)
769 const struct intel_screen
*intelScreen
=
770 (struct intel_screen
*) psp
->driverPrivate
;
773 case __DRI2_RENDERER_VENDOR_ID
:
774 value
[0] = i915_vendor_string
;
776 case __DRI2_RENDERER_DEVICE_ID
:
777 value
[0] = i915_get_renderer_string(intelScreen
->deviceID
);
786 static const __DRI2rendererQueryExtension intelRendererQueryExtension
= {
787 .base
= { __DRI2_RENDERER_QUERY
, 1 },
789 .queryInteger
= i915_query_renderer_integer
,
790 .queryString
= i915_query_renderer_string
793 static const __DRIextension
*intelScreenExtensions
[] = {
794 &intelTexBufferExtension
.base
,
795 &intelFenceExtension
.base
,
796 &intelFlushExtension
.base
,
797 &intelImageExtension
.base
,
798 &intelRendererQueryExtension
.base
,
799 &dri2ConfigQueryExtension
.base
,
800 &dri2NoErrorExtension
.base
,
805 intel_get_param(__DRIscreen
*psp
, int param
, int *value
)
808 struct drm_i915_getparam gp
;
810 memset(&gp
, 0, sizeof(gp
));
814 ret
= drmCommandWriteRead(psp
->fd
, DRM_I915_GETPARAM
, &gp
, sizeof(gp
));
817 _mesa_warning(NULL
, "drm_i915_getparam: %d", ret
);
825 intel_get_boolean(__DRIscreen
*psp
, int param
)
828 return intel_get_param(psp
, param
, &value
) && value
;
832 intelDestroyScreen(__DRIscreen
* sPriv
)
834 struct intel_screen
*intelScreen
= sPriv
->driverPrivate
;
836 dri_bufmgr_destroy(intelScreen
->bufmgr
);
837 driDestroyOptionInfo(&intelScreen
->optionCache
);
840 sPriv
->driverPrivate
= NULL
;
845 * This is called when we need to set up GL rendering to a new X window.
848 intelCreateBuffer(__DRIscreen
* driScrnPriv
,
849 __DRIdrawable
* driDrawPriv
,
850 const struct gl_config
* mesaVis
, GLboolean isPixmap
)
852 struct intel_renderbuffer
*rb
;
853 mesa_format rgbFormat
;
854 struct gl_framebuffer
*fb
;
859 fb
= CALLOC_STRUCT(gl_framebuffer
);
863 _mesa_initialize_window_framebuffer(fb
, mesaVis
);
865 if (mesaVis
->redBits
== 5)
866 rgbFormat
= MESA_FORMAT_B5G6R5_UNORM
;
867 else if (mesaVis
->sRGBCapable
)
868 rgbFormat
= MESA_FORMAT_B8G8R8A8_SRGB
;
869 else if (mesaVis
->alphaBits
== 0)
870 rgbFormat
= MESA_FORMAT_B8G8R8X8_UNORM
;
872 rgbFormat
= MESA_FORMAT_B8G8R8A8_UNORM
;
874 /* setup the hardware-based renderbuffers */
875 rb
= intel_create_renderbuffer(rgbFormat
);
876 _mesa_attach_and_own_rb(fb
, BUFFER_FRONT_LEFT
, &rb
->Base
.Base
);
878 if (mesaVis
->doubleBufferMode
) {
879 rb
= intel_create_renderbuffer(rgbFormat
);
880 _mesa_attach_and_own_rb(fb
, BUFFER_BACK_LEFT
, &rb
->Base
.Base
);
884 * Assert here that the gl_config has an expected depth/stencil bit
885 * combination: one of d24/s8, d16/s0, d0/s0. (See intelInitScreen2(),
886 * which constructs the advertised configs.)
888 if (mesaVis
->depthBits
== 24) {
889 assert(mesaVis
->stencilBits
== 8);
892 * Use combined depth/stencil. Note that the renderbuffer is
893 * attached to two attachment points.
895 rb
= intel_create_private_renderbuffer(MESA_FORMAT_Z24_UNORM_S8_UINT
);
896 _mesa_attach_and_own_rb(fb
, BUFFER_DEPTH
, &rb
->Base
.Base
);
897 _mesa_attach_and_reference_rb(fb
, BUFFER_STENCIL
, &rb
->Base
.Base
);
899 else if (mesaVis
->depthBits
== 16) {
900 assert(mesaVis
->stencilBits
== 0);
901 rb
= intel_create_private_renderbuffer(MESA_FORMAT_Z_UNORM16
);
902 _mesa_attach_and_own_rb(fb
, BUFFER_DEPTH
, &rb
->Base
.Base
);
905 assert(mesaVis
->depthBits
== 0);
906 assert(mesaVis
->stencilBits
== 0);
909 /* now add any/all software-based renderbuffers we may need */
910 _swrast_add_soft_renderbuffers(fb
,
911 false, /* never sw color */
912 false, /* never sw depth */
913 false, /* never sw stencil */
914 mesaVis
->accumRedBits
> 0,
915 false, /* never sw alpha */
916 false /* never sw aux */ );
917 driDrawPriv
->driverPrivate
= fb
;
923 intelDestroyBuffer(__DRIdrawable
* driDrawPriv
)
925 struct gl_framebuffer
*fb
= driDrawPriv
->driverPrivate
;
927 _mesa_reference_framebuffer(&fb
, NULL
);
930 /* There are probably better ways to do this, such as an
931 * init-designated function to register chipids and createcontext
935 i830CreateContext(int api
,
936 const struct gl_config
*mesaVis
,
937 __DRIcontext
*driContextPriv
,
938 unsigned major_version
,
939 unsigned minor_version
,
942 void *sharedContextPrivate
);
945 i915CreateContext(int api
,
946 const struct gl_config
*mesaVis
,
947 __DRIcontext
*driContextPriv
,
948 unsigned major_version
,
949 unsigned minor_version
,
952 void *sharedContextPrivate
);
955 intelCreateContext(gl_api api
,
956 const struct gl_config
* mesaVis
,
957 __DRIcontext
* driContextPriv
,
958 const struct __DriverContextConfig
*ctx_config
,
960 void *sharedContextPrivate
)
962 bool success
= false;
964 __DRIscreen
*sPriv
= driContextPriv
->driScreenPriv
;
965 struct intel_screen
*intelScreen
= sPriv
->driverPrivate
;
967 if (ctx_config
->flags
& ~(__DRI_CTX_FLAG_DEBUG
| __DRI_CTX_FLAG_NO_ERROR
)) {
968 *error
= __DRI_CTX_ERROR_UNKNOWN_FLAG
;
972 if (ctx_config
->attribute_mask
) {
973 *error
= __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
;
977 if (IS_GEN3(intelScreen
->deviceID
)) {
978 success
= i915CreateContext(api
, mesaVis
, driContextPriv
,
979 ctx_config
->major_version
,
980 ctx_config
->minor_version
,
982 error
, sharedContextPrivate
);
984 intelScreen
->no_vbo
= true;
985 success
= i830CreateContext(api
, mesaVis
, driContextPriv
,
986 ctx_config
->major_version
,
987 ctx_config
->minor_version
,
989 error
, sharedContextPrivate
);
995 if (driContextPriv
->driverPrivate
!= NULL
)
996 intelDestroyContext(driContextPriv
);
1002 intel_init_bufmgr(struct intel_screen
*intelScreen
)
1004 __DRIscreen
*spriv
= intelScreen
->driScrnPriv
;
1006 intelScreen
->no_hw
= getenv("INTEL_NO_HW") != NULL
;
1008 intelScreen
->bufmgr
= intel_bufmgr_gem_init(spriv
->fd
, BATCH_SZ
);
1009 if (intelScreen
->bufmgr
== NULL
) {
1010 fprintf(stderr
, "[%s:%u] Error initializing buffer manager.\n",
1011 __func__
, __LINE__
);
1015 drm_intel_bufmgr_gem_enable_fenced_relocs(intelScreen
->bufmgr
);
1017 if (!intel_get_boolean(spriv
, I915_PARAM_HAS_RELAXED_DELTA
)) {
1018 fprintf(stderr
, "[%s: %u] Kernel 2.6.39 required.\n", __func__
, __LINE__
);
1025 static __DRIconfig
**
1026 intel_screen_make_configs(__DRIscreen
*dri_screen
)
1028 static const mesa_format formats
[] = {
1029 MESA_FORMAT_B5G6R5_UNORM
,
1030 MESA_FORMAT_B8G8R8A8_UNORM
,
1031 MESA_FORMAT_B8G8R8X8_UNORM
1034 /* __DRI_ATTRIB_SWAP_COPY is not supported due to page flipping. */
1035 static const GLenum back_buffer_modes
[] = {
1036 __DRI_ATTRIB_SWAP_UNDEFINED
, __DRI_ATTRIB_SWAP_NONE
1039 static const uint8_t singlesample_samples
[1] = {0};
1041 uint8_t depth_bits
[4], stencil_bits
[4];
1042 __DRIconfig
**configs
= NULL
;
1044 /* Generate singlesample configs without accumulation buffer. */
1045 for (int i
= 0; i
< ARRAY_SIZE(formats
); i
++) {
1046 __DRIconfig
**new_configs
;
1047 int num_depth_stencil_bits
= 2;
1049 /* Starting with DRI2 protocol version 1.1 we can request a depth/stencil
1050 * buffer that has a different number of bits per pixel than the color
1054 stencil_bits
[0] = 0;
1056 if (formats
[i
] == MESA_FORMAT_B5G6R5_UNORM
) {
1058 stencil_bits
[1] = 0;
1061 stencil_bits
[1] = 8;
1064 new_configs
= driCreateConfigs(formats
[i
],
1067 num_depth_stencil_bits
,
1068 back_buffer_modes
, 2,
1069 singlesample_samples
, 1,
1070 false, false, false);
1071 configs
= driConcatConfigs(configs
, new_configs
);
1074 /* Generate the minimum possible set of configs that include an
1075 * accumulation buffer.
1077 for (int i
= 0; i
< ARRAY_SIZE(formats
); i
++) {
1078 __DRIconfig
**new_configs
;
1080 if (formats
[i
] == MESA_FORMAT_B5G6R5_UNORM
) {
1082 stencil_bits
[0] = 0;
1085 stencil_bits
[0] = 8;
1088 new_configs
= driCreateConfigs(formats
[i
],
1089 depth_bits
, stencil_bits
, 1,
1090 back_buffer_modes
, 1,
1091 singlesample_samples
, 1,
1092 true, false, false);
1093 configs
= driConcatConfigs(configs
, new_configs
);
1096 if (configs
== NULL
) {
1097 fprintf(stderr
, "[%s:%u] Error creating FBConfig!\n", __func__
,
1106 set_max_gl_versions(struct intel_screen
*screen
)
1108 __DRIscreen
*psp
= screen
->driScrnPriv
;
1110 switch (screen
->gen
) {
1112 bool has_fragment_shader
= driQueryOptionb(&screen
->optionCache
, "fragment_shader");
1113 bool has_occlusion_query
= driQueryOptionb(&screen
->optionCache
, "stub_occlusion_query");
1115 psp
->max_gl_core_version
= 0;
1116 psp
->max_gl_es1_version
= 11;
1117 psp
->max_gl_es2_version
= 20;
1119 if (has_fragment_shader
&& has_occlusion_query
) {
1120 psp
->max_gl_compat_version
= 21;
1122 psp
->max_gl_compat_version
= 14;
1127 psp
->max_gl_core_version
= 0;
1128 psp
->max_gl_compat_version
= 13;
1129 psp
->max_gl_es1_version
= 11;
1130 psp
->max_gl_es2_version
= 0;
1133 assert(!"unrecognized intel_screen::gen");
1139 * This is the driver specific part of the createNewScreen entry point.
1140 * Called when using DRI2.
1142 * \return the struct gl_config supported by this driver
1145 __DRIconfig
**intelInitScreen2(__DRIscreen
*psp
)
1147 struct intel_screen
*intelScreen
;
1149 if (psp
->image
.loader
) {
1150 } else if (psp
->dri2
.loader
->base
.version
<= 2 ||
1151 psp
->dri2
.loader
->getBuffersWithFormat
== NULL
) {
1153 "\nERROR! DRI2 loader with getBuffersWithFormat() "
1154 "support required\n");
1158 /* Allocate the private area */
1159 intelScreen
= calloc(1, sizeof *intelScreen
);
1161 fprintf(stderr
, "\nERROR! Allocating private area failed\n");
1164 /* parse information in __driConfigOptions */
1165 driParseOptionInfo(&intelScreen
->optionCache
, i915_config_options
.xml
);
1167 intelScreen
->driScrnPriv
= psp
;
1168 psp
->driverPrivate
= (void *) intelScreen
;
1170 if (!intel_init_bufmgr(intelScreen
))
1173 intelScreen
->deviceID
= drm_intel_bufmgr_gem_get_devid(intelScreen
->bufmgr
);
1175 if (IS_GEN3(intelScreen
->deviceID
)) {
1176 intelScreen
->gen
= 3;
1178 intelScreen
->gen
= 2;
1181 set_max_gl_versions(intelScreen
);
1183 psp
->extensions
= intelScreenExtensions
;
1185 return (const __DRIconfig
**) intel_screen_make_configs(psp
);
1188 struct intel_buffer
{
1190 struct intel_region
*region
;
1193 static __DRIbuffer
*
1194 intelAllocateBuffer(__DRIscreen
*screen
,
1195 unsigned attachment
, unsigned format
,
1196 int width
, int height
)
1198 struct intel_buffer
*intelBuffer
;
1199 struct intel_screen
*intelScreen
= screen
->driverPrivate
;
1201 assert(attachment
== __DRI_BUFFER_FRONT_LEFT
||
1202 attachment
== __DRI_BUFFER_BACK_LEFT
);
1204 intelBuffer
= calloc(1, sizeof *intelBuffer
);
1205 if (intelBuffer
== NULL
)
1208 /* The front and back buffers are color buffers, which are X tiled. */
1209 intelBuffer
->region
= intel_region_alloc(intelScreen
,
1216 if (intelBuffer
->region
== NULL
) {
1221 intel_region_flink(intelBuffer
->region
, &intelBuffer
->base
.name
);
1223 intelBuffer
->base
.attachment
= attachment
;
1224 intelBuffer
->base
.cpp
= intelBuffer
->region
->cpp
;
1225 intelBuffer
->base
.pitch
= intelBuffer
->region
->pitch
;
1227 return &intelBuffer
->base
;
1231 intelReleaseBuffer(__DRIscreen
*screen
, __DRIbuffer
*buffer
)
1233 struct intel_buffer
*intelBuffer
= (struct intel_buffer
*) buffer
;
1235 intel_region_release(&intelBuffer
->region
);
1240 static const struct __DriverAPIRec i915_driver_api
= {
1241 .InitScreen
= intelInitScreen2
,
1242 .DestroyScreen
= intelDestroyScreen
,
1243 .CreateContext
= intelCreateContext
,
1244 .DestroyContext
= intelDestroyContext
,
1245 .CreateBuffer
= intelCreateBuffer
,
1246 .DestroyBuffer
= intelDestroyBuffer
,
1247 .MakeCurrent
= intelMakeCurrent
,
1248 .UnbindContext
= intelUnbindContext
,
1249 .AllocateBuffer
= intelAllocateBuffer
,
1250 .ReleaseBuffer
= intelReleaseBuffer
1253 static const struct __DRIDriverVtableExtensionRec i915_vtable
= {
1254 .base
= { __DRI_DRIVER_VTABLE
, 1 },
1255 .vtable
= &i915_driver_api
,
1258 /* This is the table of extensions that the loader will dlsym() for. */
1259 static const __DRIextension
*i915_driver_extensions
[] = {
1260 &driCoreExtension
.base
,
1261 &driImageDriverExtension
.base
,
1262 &driDRI2Extension
.base
,
1264 &i915_config_options
.base
,
1268 PUBLIC
const __DRIextension
**__driDriverGetExtensions_i915(void)
1270 globalDriverAPI
= &i915_driver_api
;
1272 return i915_driver_extensions
;