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"
45 static const __DRIconfigOptionsExtension i915_config_options
= {
46 .base
= { __DRI_CONFIG_OPTIONS
, 1 },
50 DRI_CONF_SECTION_PERFORMANCE
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(fragment_shader
, "true")
62 DRI_CONF_DESC(en
, "Enable limited ARB_fragment_shader support on 915/945.")
66 DRI_CONF_SECTION_QUALITY
68 DRI_CONF_SECTION_DEBUG
69 DRI_CONF_ALWAYS_FLUSH_BATCH("false")
70 DRI_CONF_ALWAYS_FLUSH_CACHE("false")
71 DRI_CONF_DISABLE_THROTTLING("false")
72 DRI_CONF_FORCE_GLSL_EXTENSIONS_WARN("false")
73 DRI_CONF_DISABLE_GLSL_LINE_CONTINUATIONS("false")
74 DRI_CONF_DISABLE_BLEND_FUNC_EXTENDED("false")
76 DRI_CONF_OPT_BEGIN_B(stub_occlusion_query
, "false")
77 DRI_CONF_DESC(en
, "Enable stub ARB_occlusion_query support on 915/945.")
80 DRI_CONF_OPT_BEGIN_B(shader_precompile
, "true")
81 DRI_CONF_DESC(en
, "Perform code generation at shader link time.")
87 #include "intel_batchbuffer.h"
88 #include "intel_buffers.h"
89 #include "intel_bufmgr.h"
90 #include "intel_chipset.h"
91 #include "intel_fbo.h"
92 #include "intel_mipmap_tree.h"
93 #include "intel_screen.h"
94 #include "intel_tex.h"
95 #include "intel_regions.h"
97 #include "drm-uapi/i915_drm.h"
100 * For debugging purposes, this returns a time in seconds.
107 clock_gettime(CLOCK_MONOTONIC
, &tp
);
109 return tp
.tv_sec
+ tp
.tv_nsec
/ 1000000000.0;
113 aub_dump_bmp(struct gl_context
*ctx
)
115 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
117 for (int i
= 0; i
< fb
->_NumColorDrawBuffers
; i
++) {
118 struct intel_renderbuffer
*irb
=
119 intel_renderbuffer(fb
->_ColorDrawBuffers
[i
]);
121 if (irb
&& irb
->mt
) {
122 enum aub_dump_bmp_format format
;
124 switch (irb
->Base
.Base
.Format
) {
125 case MESA_FORMAT_B8G8R8A8_UNORM
:
126 case MESA_FORMAT_B8G8R8X8_UNORM
:
127 format
= AUB_DUMP_BMP_FORMAT_ARGB_8888
;
133 assert(irb
->mt
->region
->pitch
% irb
->mt
->region
->cpp
== 0);
134 drm_intel_gem_bo_aub_dump_bmp(irb
->mt
->region
->bo
,
137 irb
->Base
.Base
.Width
,
138 irb
->Base
.Base
.Height
,
140 irb
->mt
->region
->pitch
,
146 static const __DRItexBufferExtension intelTexBufferExtension
= {
147 .base
= { __DRI_TEX_BUFFER
, 3 },
149 .setTexBuffer
= intelSetTexBuffer
,
150 .setTexBuffer2
= intelSetTexBuffer2
,
151 .releaseTexBuffer
= NULL
,
155 intelDRI2Flush(__DRIdrawable
*drawable
)
157 GET_CURRENT_CONTEXT(ctx
);
158 struct intel_context
*intel
= intel_context(ctx
);
162 INTEL_FIREVERTICES(intel
);
164 intel
->need_throttle
= true;
166 if (intel
->batch
.used
)
167 intel_batchbuffer_flush(intel
);
169 if (INTEL_DEBUG
& DEBUG_AUB
) {
174 static const struct __DRI2flushExtensionRec intelFlushExtension
= {
175 .base
= { __DRI2_FLUSH
, 3 },
177 .flush
= intelDRI2Flush
,
178 .invalidate
= dri2InvalidateDrawable
,
181 static struct intel_image_format intel_image_formats
[] = {
182 { DRM_FORMAT_ARGB8888
, __DRI_IMAGE_COMPONENTS_RGBA
, 1,
183 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ARGB8888
, 4 } } },
185 { __DRI_IMAGE_FOURCC_SARGB8888
, __DRI_IMAGE_COMPONENTS_RGBA
, 1,
186 { { 0, 0, 0, __DRI_IMAGE_FORMAT_SARGB8
, 4 } } },
188 { DRM_FORMAT_XRGB8888
, __DRI_IMAGE_COMPONENTS_RGB
, 1,
189 { { 0, 0, 0, __DRI_IMAGE_FORMAT_XRGB8888
, 4 }, } },
191 { DRM_FORMAT_YUV410
, __DRI_IMAGE_COMPONENTS_Y_U_V
, 3,
192 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8
, 1 },
193 { 1, 2, 2, __DRI_IMAGE_FORMAT_R8
, 1 },
194 { 2, 2, 2, __DRI_IMAGE_FORMAT_R8
, 1 } } },
196 { DRM_FORMAT_YUV411
, __DRI_IMAGE_COMPONENTS_Y_U_V
, 3,
197 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8
, 1 },
198 { 1, 2, 0, __DRI_IMAGE_FORMAT_R8
, 1 },
199 { 2, 2, 0, __DRI_IMAGE_FORMAT_R8
, 1 } } },
201 { DRM_FORMAT_YUV420
, __DRI_IMAGE_COMPONENTS_Y_U_V
, 3,
202 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8
, 1 },
203 { 1, 1, 1, __DRI_IMAGE_FORMAT_R8
, 1 },
204 { 2, 1, 1, __DRI_IMAGE_FORMAT_R8
, 1 } } },
206 { DRM_FORMAT_YUV422
, __DRI_IMAGE_COMPONENTS_Y_U_V
, 3,
207 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8
, 1 },
208 { 1, 1, 0, __DRI_IMAGE_FORMAT_R8
, 1 },
209 { 2, 1, 0, __DRI_IMAGE_FORMAT_R8
, 1 } } },
211 { DRM_FORMAT_YUV444
, __DRI_IMAGE_COMPONENTS_Y_U_V
, 3,
212 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8
, 1 },
213 { 1, 0, 0, __DRI_IMAGE_FORMAT_R8
, 1 },
214 { 2, 0, 0, __DRI_IMAGE_FORMAT_R8
, 1 } } },
216 { DRM_FORMAT_NV12
, __DRI_IMAGE_COMPONENTS_Y_UV
, 2,
217 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8
, 1 },
218 { 1, 1, 1, __DRI_IMAGE_FORMAT_GR88
, 2 } } },
220 { DRM_FORMAT_NV16
, __DRI_IMAGE_COMPONENTS_Y_UV
, 2,
221 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8
, 1 },
222 { 1, 1, 0, __DRI_IMAGE_FORMAT_GR88
, 2 } } },
224 /* For YUYV and UYVY buffers, we set up two overlapping DRI images
225 * and treat them as planar buffers in the compositors.
226 * Plane 0 is GR88 and samples YU or YV pairs and places Y into
227 * the R component, while plane 1 is ARGB/ABGR and samples YUYV/UYVY
228 * clusters and places pairs and places U into the G component and
229 * V into A. This lets the texture sampler interpolate the Y
230 * components correctly when sampling from plane 0, and interpolate
231 * U and V correctly when sampling from plane 1. */
232 { DRM_FORMAT_YUYV
, __DRI_IMAGE_COMPONENTS_Y_XUXV
, 2,
233 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88
, 2 },
234 { 0, 1, 0, __DRI_IMAGE_FORMAT_ARGB8888
, 4 } } },
235 { DRM_FORMAT_UYVY
, __DRI_IMAGE_COMPONENTS_Y_UXVX
, 2,
236 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88
, 2 },
237 { 0, 1, 0, __DRI_IMAGE_FORMAT_ABGR8888
, 4 } } }
241 intel_allocate_image(int dri_format
, void *loaderPrivate
)
245 image
= calloc(1, sizeof *image
);
249 image
->dri_format
= dri_format
;
252 image
->format
= driImageFormatToGLFormat(dri_format
);
253 if (dri_format
!= __DRI_IMAGE_FORMAT_NONE
&&
254 image
->format
== MESA_FORMAT_NONE
) {
259 image
->internal_format
= _mesa_get_format_base_format(image
->format
);
260 image
->data
= loaderPrivate
;
266 * Sets up a DRIImage structure to point to our shared image in a region
269 intel_setup_image_from_mipmap_tree(struct intel_context
*intel
, __DRIimage
*image
,
270 struct intel_mipmap_tree
*mt
, GLuint level
,
273 unsigned int draw_x
, draw_y
;
274 uint32_t mask_x
, mask_y
;
276 intel_miptree_check_level_layer(mt
, level
, zoffset
);
278 intel_region_get_tile_masks(mt
->region
, &mask_x
, &mask_y
);
279 intel_miptree_get_image_offset(mt
, level
, zoffset
, &draw_x
, &draw_y
);
281 image
->width
= mt
->level
[level
].width
;
282 image
->height
= mt
->level
[level
].height
;
283 image
->tile_x
= draw_x
& mask_x
;
284 image
->tile_y
= draw_y
& mask_y
;
286 image
->offset
= intel_region_get_aligned_offset(mt
->region
,
290 intel_region_reference(&image
->region
, mt
->region
);
294 intel_setup_image_from_dimensions(__DRIimage
*image
)
296 image
->width
= image
->region
->width
;
297 image
->height
= image
->region
->height
;
303 intel_create_image_from_name(__DRIscreen
*screen
,
304 int width
, int height
, int format
,
305 int name
, int pitch
, void *loaderPrivate
)
307 struct intel_screen
*intelScreen
= screen
->driverPrivate
;
311 image
= intel_allocate_image(format
, loaderPrivate
);
315 if (image
->format
== MESA_FORMAT_NONE
)
318 cpp
= _mesa_get_format_bytes(image
->format
);
319 image
->region
= intel_region_alloc_for_handle(intelScreen
,
321 pitch
* cpp
, name
, "image");
322 if (image
->region
== NULL
) {
327 intel_setup_image_from_dimensions(image
);
333 intel_create_image_from_renderbuffer(__DRIcontext
*context
,
334 int renderbuffer
, void *loaderPrivate
)
337 struct intel_context
*intel
= context
->driverPrivate
;
338 struct gl_renderbuffer
*rb
;
339 struct intel_renderbuffer
*irb
;
341 rb
= _mesa_lookup_renderbuffer(&intel
->ctx
, renderbuffer
);
343 _mesa_error(&intel
->ctx
,
344 GL_INVALID_OPERATION
, "glRenderbufferExternalMESA");
348 irb
= intel_renderbuffer(rb
);
349 image
= calloc(1, sizeof *image
);
353 image
->internal_format
= rb
->InternalFormat
;
354 image
->format
= rb
->Format
;
356 image
->data
= loaderPrivate
;
357 intel_region_reference(&image
->region
, irb
->mt
->region
);
358 intel_setup_image_from_dimensions(image
);
359 image
->dri_format
= driGLFormatToImageFormat(image
->format
);
361 rb
->NeedsFinishRenderTexture
= true;
366 intel_create_image_from_texture(__DRIcontext
*context
, int target
,
367 unsigned texture
, int zoffset
,
373 struct intel_context
*intel
= context
->driverPrivate
;
374 struct gl_texture_object
*obj
;
375 struct intel_texture_object
*iobj
;
378 obj
= _mesa_lookup_texture(&intel
->ctx
, texture
);
379 if (!obj
|| obj
->Target
!= target
) {
380 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
384 if (target
== GL_TEXTURE_CUBE_MAP
)
387 _mesa_test_texobj_completeness(&intel
->ctx
, obj
);
388 iobj
= intel_texture_object(obj
);
389 if (!obj
->_BaseComplete
|| (level
> 0 && !obj
->_MipmapComplete
)) {
390 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
394 if (level
< obj
->BaseLevel
|| level
> obj
->_MaxLevel
) {
395 *error
= __DRI_IMAGE_ERROR_BAD_MATCH
;
399 if (target
== GL_TEXTURE_3D
&& obj
->Image
[face
][level
]->Depth
< zoffset
) {
400 *error
= __DRI_IMAGE_ERROR_BAD_MATCH
;
403 image
= calloc(1, sizeof *image
);
405 *error
= __DRI_IMAGE_ERROR_BAD_ALLOC
;
409 image
->internal_format
= obj
->Image
[face
][level
]->InternalFormat
;
410 image
->format
= obj
->Image
[face
][level
]->TexFormat
;
411 image
->data
= loaderPrivate
;
412 intel_setup_image_from_mipmap_tree(intel
, image
, iobj
->mt
, level
, zoffset
);
413 image
->dri_format
= driGLFormatToImageFormat(image
->format
);
414 if (image
->dri_format
== __DRI_IMAGE_FORMAT_NONE
) {
415 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
420 *error
= __DRI_IMAGE_ERROR_SUCCESS
;
425 intel_destroy_image(__DRIimage
*image
)
427 intel_region_release(&image
->region
);
432 intel_create_image(__DRIscreen
*screen
,
433 int width
, int height
, int format
,
438 struct intel_screen
*intelScreen
= screen
->driverPrivate
;
442 tiling
= I915_TILING_X
;
443 if (use
& __DRI_IMAGE_USE_CURSOR
) {
444 if (width
!= 64 || height
!= 64)
446 tiling
= I915_TILING_NONE
;
449 if (use
& __DRI_IMAGE_USE_LINEAR
)
450 tiling
= I915_TILING_NONE
;
452 image
= intel_allocate_image(format
, loaderPrivate
);
456 cpp
= _mesa_get_format_bytes(image
->format
);
458 intel_region_alloc(intelScreen
, tiling
, cpp
, width
, height
, true);
459 if (image
->region
== NULL
) {
464 intel_setup_image_from_dimensions(image
);
470 intel_query_image(__DRIimage
*image
, int attrib
, int *value
)
473 case __DRI_IMAGE_ATTRIB_STRIDE
:
474 *value
= image
->region
->pitch
;
476 case __DRI_IMAGE_ATTRIB_HANDLE
:
477 *value
= image
->region
->bo
->handle
;
479 case __DRI_IMAGE_ATTRIB_NAME
:
480 return intel_region_flink(image
->region
, (uint32_t *) value
);
481 case __DRI_IMAGE_ATTRIB_FORMAT
:
482 *value
= image
->dri_format
;
484 case __DRI_IMAGE_ATTRIB_WIDTH
:
485 *value
= image
->region
->width
;
487 case __DRI_IMAGE_ATTRIB_HEIGHT
:
488 *value
= image
->region
->height
;
490 case __DRI_IMAGE_ATTRIB_COMPONENTS
:
491 if (image
->planar_format
== NULL
)
493 *value
= image
->planar_format
->components
;
495 case __DRI_IMAGE_ATTRIB_FD
:
496 return !drm_intel_bo_gem_export_to_prime(image
->region
->bo
, value
);
503 intel_dup_image(__DRIimage
*orig_image
, void *loaderPrivate
)
507 image
= calloc(1, sizeof *image
);
511 intel_region_reference(&image
->region
, orig_image
->region
);
512 if (image
->region
== NULL
) {
517 image
->internal_format
= orig_image
->internal_format
;
518 image
->planar_format
= orig_image
->planar_format
;
519 image
->dri_format
= orig_image
->dri_format
;
520 image
->format
= orig_image
->format
;
521 image
->offset
= orig_image
->offset
;
522 image
->width
= orig_image
->width
;
523 image
->height
= orig_image
->height
;
524 image
->tile_x
= orig_image
->tile_x
;
525 image
->tile_y
= orig_image
->tile_y
;
526 image
->data
= loaderPrivate
;
528 memcpy(image
->strides
, orig_image
->strides
, sizeof(image
->strides
));
529 memcpy(image
->offsets
, orig_image
->offsets
, sizeof(image
->offsets
));
535 intel_validate_usage(__DRIimage
*image
, unsigned int use
)
537 if (use
& __DRI_IMAGE_USE_CURSOR
) {
538 if (image
->region
->width
!= 64 || image
->region
->height
!= 64)
546 intel_create_image_from_names(__DRIscreen
*screen
,
547 int width
, int height
, int fourcc
,
548 int *names
, int num_names
,
549 int *strides
, int *offsets
,
552 struct intel_image_format
*f
= NULL
;
556 if (screen
== NULL
|| names
== NULL
|| num_names
!= 1)
559 for (i
= 0; i
< ARRAY_SIZE(intel_image_formats
); i
++) {
560 if (intel_image_formats
[i
].fourcc
== fourcc
) {
561 f
= &intel_image_formats
[i
];
568 image
= intel_create_image_from_name(screen
, width
, height
,
569 __DRI_IMAGE_FORMAT_NONE
,
570 names
[0], strides
[0],
576 image
->planar_format
= f
;
577 for (i
= 0; i
< f
->nplanes
; i
++) {
578 index
= f
->planes
[i
].buffer_index
;
579 image
->offsets
[index
] = offsets
[index
];
580 image
->strides
[index
] = strides
[index
];
587 intel_create_image_from_fds(__DRIscreen
*screen
,
588 int width
, int height
, int fourcc
,
589 int *fds
, int num_fds
, int *strides
, int *offsets
,
592 struct intel_screen
*intelScreen
= screen
->driverPrivate
;
593 struct intel_image_format
*f
= NULL
;
597 if (fds
== NULL
|| num_fds
!= 1)
600 for (i
= 0; i
< ARRAY_SIZE(intel_image_formats
); i
++) {
601 if (intel_image_formats
[i
].fourcc
== fourcc
) {
602 f
= &intel_image_formats
[i
];
609 image
= intel_allocate_image(__DRI_IMAGE_FORMAT_NONE
, loaderPrivate
);
613 image
->region
= intel_region_alloc_for_fd(intelScreen
,
614 f
->planes
[0].cpp
, width
, height
, strides
[0],
615 height
* strides
[0], fds
[0], "image");
616 if (image
->region
== NULL
) {
621 intel_setup_image_from_dimensions(image
);
623 image
->planar_format
= f
;
624 for (i
= 0; i
< f
->nplanes
; i
++) {
625 index
= f
->planes
[i
].buffer_index
;
626 image
->offsets
[index
] = offsets
[index
];
627 image
->strides
[index
] = strides
[index
];
635 intel_from_planar(__DRIimage
*parent
, int plane
, void *loaderPrivate
)
637 int width
, height
, offset
, stride
, dri_format
, index
;
638 struct intel_image_format
*f
;
639 uint32_t mask_x
, mask_y
;
642 if (parent
== NULL
|| parent
->planar_format
== NULL
)
645 f
= parent
->planar_format
;
647 if (plane
>= f
->nplanes
)
650 width
= parent
->region
->width
>> f
->planes
[plane
].width_shift
;
651 height
= parent
->region
->height
>> f
->planes
[plane
].height_shift
;
652 dri_format
= f
->planes
[plane
].dri_format
;
653 index
= f
->planes
[plane
].buffer_index
;
654 offset
= parent
->offsets
[index
];
655 stride
= parent
->strides
[index
];
657 image
= intel_allocate_image(dri_format
, loaderPrivate
);
661 if (offset
+ height
* stride
> parent
->region
->bo
->size
) {
662 _mesa_warning(NULL
, "intel_create_sub_image: subimage out of bounds");
667 image
->region
= calloc(sizeof(*image
->region
), 1);
668 if (image
->region
== NULL
) {
673 image
->region
->cpp
= _mesa_get_format_bytes(image
->format
);
674 image
->region
->width
= width
;
675 image
->region
->height
= height
;
676 image
->region
->pitch
= stride
;
677 image
->region
->refcount
= 1;
678 image
->region
->bo
= parent
->region
->bo
;
679 drm_intel_bo_reference(image
->region
->bo
);
680 image
->region
->tiling
= parent
->region
->tiling
;
681 image
->offset
= offset
;
682 intel_setup_image_from_dimensions(image
);
684 intel_region_get_tile_masks(image
->region
, &mask_x
, &mask_y
);
687 "intel_create_sub_image: offset not on tile boundary");
692 static const __DRIimageExtension intelImageExtension
= {
693 .base
= { __DRI_IMAGE
, 7 },
695 .createImageFromName
= intel_create_image_from_name
,
696 .createImageFromRenderbuffer
= intel_create_image_from_renderbuffer
,
697 .destroyImage
= intel_destroy_image
,
698 .createImage
= intel_create_image
,
699 .queryImage
= intel_query_image
,
700 .dupImage
= intel_dup_image
,
701 .validateUsage
= intel_validate_usage
,
702 .createImageFromNames
= intel_create_image_from_names
,
703 .fromPlanar
= intel_from_planar
,
704 .createImageFromTexture
= intel_create_image_from_texture
,
705 .createImageFromFds
= intel_create_image_from_fds
709 i915_query_renderer_integer(__DRIscreen
*psp
, int param
, unsigned int *value
)
711 const struct intel_screen
*const intelScreen
=
712 (struct intel_screen
*) psp
->driverPrivate
;
715 case __DRI2_RENDERER_VENDOR_ID
:
718 case __DRI2_RENDERER_DEVICE_ID
:
719 value
[0] = intelScreen
->deviceID
;
721 case __DRI2_RENDERER_ACCELERATED
:
724 case __DRI2_RENDERER_VIDEO_MEMORY
: {
725 /* Once a batch uses more than 75% of the maximum mappable size, we
726 * assume that there's some fragmentation, and we start doing extra
727 * flushing, etc. That's the big cliff apps will care about.
730 size_t mappable_size
;
732 drm_intel_get_aperture_sizes(psp
->fd
, &mappable_size
, &aper_size
);
734 const unsigned gpu_mappable_megabytes
=
735 (aper_size
/ (1024 * 1024)) * 3 / 4;
737 const long system_memory_pages
= sysconf(_SC_PHYS_PAGES
);
738 const long system_page_size
= sysconf(_SC_PAGE_SIZE
);
740 if (system_memory_pages
<= 0 || system_page_size
<= 0)
743 const uint64_t system_memory_bytes
= (uint64_t) system_memory_pages
744 * (uint64_t) system_page_size
;
746 const unsigned system_memory_megabytes
=
747 (unsigned) (system_memory_bytes
/ (1024 * 1024));
749 value
[0] = MIN2(system_memory_megabytes
, gpu_mappable_megabytes
);
752 case __DRI2_RENDERER_UNIFIED_MEMORY_ARCHITECTURE
:
755 case __DRI2_RENDERER_HAS_TEXTURE_3D
:
759 return driQueryRendererIntegerCommon(psp
, param
, value
);
766 i915_query_renderer_string(__DRIscreen
*psp
, int param
, const char **value
)
768 const struct intel_screen
*intelScreen
=
769 (struct intel_screen
*) psp
->driverPrivate
;
772 case __DRI2_RENDERER_VENDOR_ID
:
773 value
[0] = i915_vendor_string
;
775 case __DRI2_RENDERER_DEVICE_ID
:
776 value
[0] = i915_get_renderer_string(intelScreen
->deviceID
);
785 static const __DRI2rendererQueryExtension intelRendererQueryExtension
= {
786 .base
= { __DRI2_RENDERER_QUERY
, 1 },
788 .queryInteger
= i915_query_renderer_integer
,
789 .queryString
= i915_query_renderer_string
792 static const __DRIextension
*intelScreenExtensions
[] = {
793 &intelTexBufferExtension
.base
,
794 &intelFenceExtension
.base
,
795 &intelFlushExtension
.base
,
796 &intelImageExtension
.base
,
797 &intelRendererQueryExtension
.base
,
798 &dri2ConfigQueryExtension
.base
,
799 &dri2NoErrorExtension
.base
,
804 intel_get_param(__DRIscreen
*psp
, int param
, int *value
)
807 struct drm_i915_getparam gp
;
809 memset(&gp
, 0, sizeof(gp
));
813 ret
= drmCommandWriteRead(psp
->fd
, DRM_I915_GETPARAM
, &gp
, sizeof(gp
));
816 _mesa_warning(NULL
, "drm_i915_getparam: %d", ret
);
824 intel_get_boolean(__DRIscreen
*psp
, int param
)
827 return intel_get_param(psp
, param
, &value
) && value
;
831 intelDestroyScreen(__DRIscreen
* sPriv
)
833 struct intel_screen
*intelScreen
= sPriv
->driverPrivate
;
835 dri_bufmgr_destroy(intelScreen
->bufmgr
);
836 driDestroyOptionInfo(&intelScreen
->optionCache
);
839 sPriv
->driverPrivate
= NULL
;
844 * This is called when we need to set up GL rendering to a new X window.
847 intelCreateBuffer(__DRIscreen
* driScrnPriv
,
848 __DRIdrawable
* driDrawPriv
,
849 const struct gl_config
* mesaVis
, GLboolean isPixmap
)
851 struct intel_renderbuffer
*rb
;
852 mesa_format rgbFormat
;
853 struct gl_framebuffer
*fb
;
858 fb
= CALLOC_STRUCT(gl_framebuffer
);
862 _mesa_initialize_window_framebuffer(fb
, mesaVis
);
864 if (mesaVis
->redBits
== 5)
865 rgbFormat
= MESA_FORMAT_B5G6R5_UNORM
;
866 else if (mesaVis
->sRGBCapable
)
867 rgbFormat
= MESA_FORMAT_B8G8R8A8_SRGB
;
868 else if (mesaVis
->alphaBits
== 0)
869 rgbFormat
= MESA_FORMAT_B8G8R8X8_UNORM
;
871 rgbFormat
= MESA_FORMAT_B8G8R8A8_UNORM
;
873 /* setup the hardware-based renderbuffers */
874 rb
= intel_create_renderbuffer(rgbFormat
);
875 _mesa_attach_and_own_rb(fb
, BUFFER_FRONT_LEFT
, &rb
->Base
.Base
);
877 if (mesaVis
->doubleBufferMode
) {
878 rb
= intel_create_renderbuffer(rgbFormat
);
879 _mesa_attach_and_own_rb(fb
, BUFFER_BACK_LEFT
, &rb
->Base
.Base
);
883 * Assert here that the gl_config has an expected depth/stencil bit
884 * combination: one of d24/s8, d16/s0, d0/s0. (See intelInitScreen2(),
885 * which constructs the advertised configs.)
887 if (mesaVis
->depthBits
== 24) {
888 assert(mesaVis
->stencilBits
== 8);
891 * Use combined depth/stencil. Note that the renderbuffer is
892 * attached to two attachment points.
894 rb
= intel_create_private_renderbuffer(MESA_FORMAT_Z24_UNORM_S8_UINT
);
895 _mesa_attach_and_own_rb(fb
, BUFFER_DEPTH
, &rb
->Base
.Base
);
896 _mesa_attach_and_reference_rb(fb
, BUFFER_STENCIL
, &rb
->Base
.Base
);
898 else if (mesaVis
->depthBits
== 16) {
899 assert(mesaVis
->stencilBits
== 0);
900 rb
= intel_create_private_renderbuffer(MESA_FORMAT_Z_UNORM16
);
901 _mesa_attach_and_own_rb(fb
, BUFFER_DEPTH
, &rb
->Base
.Base
);
904 assert(mesaVis
->depthBits
== 0);
905 assert(mesaVis
->stencilBits
== 0);
908 /* now add any/all software-based renderbuffers we may need */
909 _swrast_add_soft_renderbuffers(fb
,
910 false, /* never sw color */
911 false, /* never sw depth */
912 false, /* never sw stencil */
913 mesaVis
->accumRedBits
> 0,
914 false, /* never sw alpha */
915 false /* never sw aux */ );
916 driDrawPriv
->driverPrivate
= fb
;
922 intelDestroyBuffer(__DRIdrawable
* driDrawPriv
)
924 struct gl_framebuffer
*fb
= driDrawPriv
->driverPrivate
;
926 _mesa_reference_framebuffer(&fb
, NULL
);
929 /* There are probably better ways to do this, such as an
930 * init-designated function to register chipids and createcontext
934 i830CreateContext(int api
,
935 const struct gl_config
*mesaVis
,
936 __DRIcontext
*driContextPriv
,
937 unsigned major_version
,
938 unsigned minor_version
,
941 void *sharedContextPrivate
);
944 i915CreateContext(int api
,
945 const struct gl_config
*mesaVis
,
946 __DRIcontext
*driContextPriv
,
947 unsigned major_version
,
948 unsigned minor_version
,
951 void *sharedContextPrivate
);
954 intelCreateContext(gl_api api
,
955 const struct gl_config
* mesaVis
,
956 __DRIcontext
* driContextPriv
,
957 const struct __DriverContextConfig
*ctx_config
,
959 void *sharedContextPrivate
)
961 bool success
= false;
963 __DRIscreen
*sPriv
= driContextPriv
->driScreenPriv
;
964 struct intel_screen
*intelScreen
= sPriv
->driverPrivate
;
966 if (ctx_config
->flags
& ~(__DRI_CTX_FLAG_DEBUG
| __DRI_CTX_FLAG_NO_ERROR
)) {
967 *error
= __DRI_CTX_ERROR_UNKNOWN_FLAG
;
971 if (ctx_config
->attribute_mask
) {
972 *error
= __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
;
976 if (IS_GEN3(intelScreen
->deviceID
)) {
977 success
= i915CreateContext(api
, mesaVis
, driContextPriv
,
978 ctx_config
->major_version
,
979 ctx_config
->minor_version
,
981 error
, sharedContextPrivate
);
983 intelScreen
->no_vbo
= true;
984 success
= i830CreateContext(api
, mesaVis
, driContextPriv
,
985 ctx_config
->major_version
,
986 ctx_config
->minor_version
,
988 error
, sharedContextPrivate
);
994 if (driContextPriv
->driverPrivate
!= NULL
)
995 intelDestroyContext(driContextPriv
);
1001 intel_init_bufmgr(struct intel_screen
*intelScreen
)
1003 __DRIscreen
*spriv
= intelScreen
->driScrnPriv
;
1005 intelScreen
->no_hw
= getenv("INTEL_NO_HW") != NULL
;
1007 intelScreen
->bufmgr
= intel_bufmgr_gem_init(spriv
->fd
, BATCH_SZ
);
1008 if (intelScreen
->bufmgr
== NULL
) {
1009 fprintf(stderr
, "[%s:%u] Error initializing buffer manager.\n",
1010 __func__
, __LINE__
);
1014 drm_intel_bufmgr_gem_enable_fenced_relocs(intelScreen
->bufmgr
);
1016 if (!intel_get_boolean(spriv
, I915_PARAM_HAS_RELAXED_DELTA
)) {
1017 fprintf(stderr
, "[%s: %u] Kernel 2.6.39 required.\n", __func__
, __LINE__
);
1024 static __DRIconfig
**
1025 intel_screen_make_configs(__DRIscreen
*dri_screen
)
1027 static const mesa_format formats
[] = {
1028 MESA_FORMAT_B5G6R5_UNORM
,
1029 MESA_FORMAT_B8G8R8A8_UNORM
,
1030 MESA_FORMAT_B8G8R8X8_UNORM
1033 /* __DRI_ATTRIB_SWAP_COPY is not supported due to page flipping. */
1034 static const GLenum back_buffer_modes
[] = {
1035 __DRI_ATTRIB_SWAP_UNDEFINED
, __DRI_ATTRIB_SWAP_NONE
1038 static const uint8_t singlesample_samples
[1] = {0};
1040 uint8_t depth_bits
[4], stencil_bits
[4];
1041 __DRIconfig
**configs
= NULL
;
1043 /* Generate singlesample configs without accumulation buffer. */
1044 for (int i
= 0; i
< ARRAY_SIZE(formats
); i
++) {
1045 __DRIconfig
**new_configs
;
1046 int num_depth_stencil_bits
= 2;
1048 /* Starting with DRI2 protocol version 1.1 we can request a depth/stencil
1049 * buffer that has a different number of bits per pixel than the color
1053 stencil_bits
[0] = 0;
1055 if (formats
[i
] == MESA_FORMAT_B5G6R5_UNORM
) {
1057 stencil_bits
[1] = 0;
1060 stencil_bits
[1] = 8;
1063 new_configs
= driCreateConfigs(formats
[i
],
1066 num_depth_stencil_bits
,
1067 back_buffer_modes
, 2,
1068 singlesample_samples
, 1,
1069 false, false, false);
1070 configs
= driConcatConfigs(configs
, new_configs
);
1073 /* Generate the minimum possible set of configs that include an
1074 * accumulation buffer.
1076 for (int i
= 0; i
< ARRAY_SIZE(formats
); i
++) {
1077 __DRIconfig
**new_configs
;
1079 if (formats
[i
] == MESA_FORMAT_B5G6R5_UNORM
) {
1081 stencil_bits
[0] = 0;
1084 stencil_bits
[0] = 8;
1087 new_configs
= driCreateConfigs(formats
[i
],
1088 depth_bits
, stencil_bits
, 1,
1089 back_buffer_modes
, 1,
1090 singlesample_samples
, 1,
1091 true, false, false);
1092 configs
= driConcatConfigs(configs
, new_configs
);
1095 if (configs
== NULL
) {
1096 fprintf(stderr
, "[%s:%u] Error creating FBConfig!\n", __func__
,
1105 set_max_gl_versions(struct intel_screen
*screen
)
1107 __DRIscreen
*psp
= screen
->driScrnPriv
;
1109 switch (screen
->gen
) {
1111 bool has_fragment_shader
= driQueryOptionb(&screen
->optionCache
, "fragment_shader");
1112 bool has_occlusion_query
= driQueryOptionb(&screen
->optionCache
, "stub_occlusion_query");
1114 psp
->max_gl_core_version
= 0;
1115 psp
->max_gl_es1_version
= 11;
1116 psp
->max_gl_es2_version
= 20;
1118 if (has_fragment_shader
&& has_occlusion_query
) {
1119 psp
->max_gl_compat_version
= 21;
1121 psp
->max_gl_compat_version
= 14;
1126 psp
->max_gl_core_version
= 0;
1127 psp
->max_gl_compat_version
= 13;
1128 psp
->max_gl_es1_version
= 11;
1129 psp
->max_gl_es2_version
= 0;
1132 assert(!"unrecognized intel_screen::gen");
1138 * This is the driver specific part of the createNewScreen entry point.
1139 * Called when using DRI2.
1141 * \return the struct gl_config supported by this driver
1144 __DRIconfig
**intelInitScreen2(__DRIscreen
*psp
)
1146 struct intel_screen
*intelScreen
;
1148 if (psp
->image
.loader
) {
1149 } else if (psp
->dri2
.loader
->base
.version
<= 2 ||
1150 psp
->dri2
.loader
->getBuffersWithFormat
== NULL
) {
1152 "\nERROR! DRI2 loader with getBuffersWithFormat() "
1153 "support required\n");
1157 /* Allocate the private area */
1158 intelScreen
= calloc(1, sizeof *intelScreen
);
1160 fprintf(stderr
, "\nERROR! Allocating private area failed\n");
1163 /* parse information in __driConfigOptions */
1164 driParseOptionInfo(&intelScreen
->optionCache
, i915_config_options
.xml
);
1166 intelScreen
->driScrnPriv
= psp
;
1167 psp
->driverPrivate
= (void *) intelScreen
;
1169 if (!intel_init_bufmgr(intelScreen
))
1172 intelScreen
->deviceID
= drm_intel_bufmgr_gem_get_devid(intelScreen
->bufmgr
);
1174 if (IS_GEN3(intelScreen
->deviceID
)) {
1175 intelScreen
->gen
= 3;
1177 intelScreen
->gen
= 2;
1180 set_max_gl_versions(intelScreen
);
1182 psp
->extensions
= intelScreenExtensions
;
1184 return (const __DRIconfig
**) intel_screen_make_configs(psp
);
1187 struct intel_buffer
{
1189 struct intel_region
*region
;
1192 static __DRIbuffer
*
1193 intelAllocateBuffer(__DRIscreen
*screen
,
1194 unsigned attachment
, unsigned format
,
1195 int width
, int height
)
1197 struct intel_buffer
*intelBuffer
;
1198 struct intel_screen
*intelScreen
= screen
->driverPrivate
;
1200 assert(attachment
== __DRI_BUFFER_FRONT_LEFT
||
1201 attachment
== __DRI_BUFFER_BACK_LEFT
);
1203 intelBuffer
= calloc(1, sizeof *intelBuffer
);
1204 if (intelBuffer
== NULL
)
1207 /* The front and back buffers are color buffers, which are X tiled. */
1208 intelBuffer
->region
= intel_region_alloc(intelScreen
,
1215 if (intelBuffer
->region
== NULL
) {
1220 intel_region_flink(intelBuffer
->region
, &intelBuffer
->base
.name
);
1222 intelBuffer
->base
.attachment
= attachment
;
1223 intelBuffer
->base
.cpp
= intelBuffer
->region
->cpp
;
1224 intelBuffer
->base
.pitch
= intelBuffer
->region
->pitch
;
1226 return &intelBuffer
->base
;
1230 intelReleaseBuffer(__DRIscreen
*screen
, __DRIbuffer
*buffer
)
1232 struct intel_buffer
*intelBuffer
= (struct intel_buffer
*) buffer
;
1234 intel_region_release(&intelBuffer
->region
);
1239 static const struct __DriverAPIRec i915_driver_api
= {
1240 .InitScreen
= intelInitScreen2
,
1241 .DestroyScreen
= intelDestroyScreen
,
1242 .CreateContext
= intelCreateContext
,
1243 .DestroyContext
= intelDestroyContext
,
1244 .CreateBuffer
= intelCreateBuffer
,
1245 .DestroyBuffer
= intelDestroyBuffer
,
1246 .MakeCurrent
= intelMakeCurrent
,
1247 .UnbindContext
= intelUnbindContext
,
1248 .AllocateBuffer
= intelAllocateBuffer
,
1249 .ReleaseBuffer
= intelReleaseBuffer
1252 static const struct __DRIDriverVtableExtensionRec i915_vtable
= {
1253 .base
= { __DRI_DRIVER_VTABLE
, 1 },
1254 .vtable
= &i915_driver_api
,
1257 /* This is the table of extensions that the loader will dlsym() for. */
1258 static const __DRIextension
*i915_driver_extensions
[] = {
1259 &driCoreExtension
.base
,
1260 &driImageDriverExtension
.base
,
1261 &driDRI2Extension
.base
,
1263 &i915_config_options
.base
,
1267 PUBLIC
const __DRIextension
**__driDriverGetExtensions_i915(void)
1269 globalDriverAPI
= &i915_driver_api
;
1271 return i915_driver_extensions
;