i965: Set subslice_total on Gen7/7.5 platforms.
[mesa.git] / src / mesa / drivers / dri / i965 / intel_screen.c
1 /*
2 * Copyright 2003 VMware, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #include <errno.h>
27 #include <time.h>
28 #include <unistd.h>
29 #include "main/context.h"
30 #include "main/framebuffer.h"
31 #include "main/renderbuffer.h"
32 #include "main/texobj.h"
33 #include "main/hash.h"
34 #include "main/fbobject.h"
35 #include "main/version.h"
36 #include "swrast/s_renderbuffer.h"
37 #include "util/ralloc.h"
38 #include "brw_shader.h"
39 #include "compiler/nir/nir.h"
40
41 #include "utils.h"
42 #include "xmlpool.h"
43
44 static const __DRIconfigOptionsExtension brw_config_options = {
45 .base = { __DRI_CONFIG_OPTIONS, 1 },
46 .xml =
47 DRI_CONF_BEGIN
48 DRI_CONF_SECTION_PERFORMANCE
49 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_ALWAYS_SYNC)
50 /* Options correspond to DRI_CONF_BO_REUSE_DISABLED,
51 * DRI_CONF_BO_REUSE_ALL
52 */
53 DRI_CONF_OPT_BEGIN_V(bo_reuse, enum, 1, "0:1")
54 DRI_CONF_DESC_BEGIN(en, "Buffer object reuse")
55 DRI_CONF_ENUM(0, "Disable buffer object reuse")
56 DRI_CONF_ENUM(1, "Enable reuse of all sizes of buffer objects")
57 DRI_CONF_DESC_END
58 DRI_CONF_OPT_END
59
60 DRI_CONF_OPT_BEGIN_B(hiz, "true")
61 DRI_CONF_DESC(en, "Enable Hierarchical Z on gen6+")
62 DRI_CONF_OPT_END
63 DRI_CONF_SECTION_END
64
65 DRI_CONF_SECTION_QUALITY
66 DRI_CONF_FORCE_S3TC_ENABLE("false")
67
68 DRI_CONF_PRECISE_TRIG("false")
69
70 DRI_CONF_OPT_BEGIN(clamp_max_samples, int, -1)
71 DRI_CONF_DESC(en, "Clamp the value of GL_MAX_SAMPLES to the "
72 "given integer. If negative, then do not clamp.")
73 DRI_CONF_OPT_END
74 DRI_CONF_SECTION_END
75
76 DRI_CONF_SECTION_DEBUG
77 DRI_CONF_NO_RAST("false")
78 DRI_CONF_ALWAYS_FLUSH_BATCH("false")
79 DRI_CONF_ALWAYS_FLUSH_CACHE("false")
80 DRI_CONF_DISABLE_THROTTLING("false")
81 DRI_CONF_FORCE_GLSL_EXTENSIONS_WARN("false")
82 DRI_CONF_DISABLE_GLSL_LINE_CONTINUATIONS("false")
83 DRI_CONF_DISABLE_BLEND_FUNC_EXTENDED("false")
84 DRI_CONF_DUAL_COLOR_BLEND_BY_LOCATION("false")
85 DRI_CONF_ALLOW_GLSL_EXTENSION_DIRECTIVE_MIDSHADER("false")
86
87 DRI_CONF_OPT_BEGIN_B(shader_precompile, "true")
88 DRI_CONF_DESC(en, "Perform code generation at shader link time.")
89 DRI_CONF_OPT_END
90 DRI_CONF_SECTION_END
91 DRI_CONF_END
92 };
93
94 #include "intel_batchbuffer.h"
95 #include "intel_buffers.h"
96 #include "intel_bufmgr.h"
97 #include "intel_fbo.h"
98 #include "intel_mipmap_tree.h"
99 #include "intel_screen.h"
100 #include "intel_tex.h"
101 #include "intel_image.h"
102
103 #include "brw_context.h"
104
105 #include "i915_drm.h"
106
107 /**
108 * For debugging purposes, this returns a time in seconds.
109 */
110 double
111 get_time(void)
112 {
113 struct timespec tp;
114
115 clock_gettime(CLOCK_MONOTONIC, &tp);
116
117 return tp.tv_sec + tp.tv_nsec / 1000000000.0;
118 }
119
120 void
121 aub_dump_bmp(struct gl_context *ctx)
122 {
123 struct gl_framebuffer *fb = ctx->DrawBuffer;
124
125 for (unsigned i = 0; i < fb->_NumColorDrawBuffers; i++) {
126 struct intel_renderbuffer *irb =
127 intel_renderbuffer(fb->_ColorDrawBuffers[i]);
128
129 if (irb && irb->mt) {
130 enum aub_dump_bmp_format format;
131
132 switch (irb->Base.Base.Format) {
133 case MESA_FORMAT_B8G8R8A8_UNORM:
134 case MESA_FORMAT_B8G8R8X8_UNORM:
135 format = AUB_DUMP_BMP_FORMAT_ARGB_8888;
136 break;
137 default:
138 continue;
139 }
140
141 drm_intel_gem_bo_aub_dump_bmp(irb->mt->bo,
142 irb->draw_x,
143 irb->draw_y,
144 irb->Base.Base.Width,
145 irb->Base.Base.Height,
146 format,
147 irb->mt->pitch,
148 0);
149 }
150 }
151 }
152
153 static const __DRItexBufferExtension intelTexBufferExtension = {
154 .base = { __DRI_TEX_BUFFER, 3 },
155
156 .setTexBuffer = intelSetTexBuffer,
157 .setTexBuffer2 = intelSetTexBuffer2,
158 .releaseTexBuffer = NULL,
159 };
160
161 static void
162 intel_dri2_flush_with_flags(__DRIcontext *cPriv,
163 __DRIdrawable *dPriv,
164 unsigned flags,
165 enum __DRI2throttleReason reason)
166 {
167 struct brw_context *brw = cPriv->driverPrivate;
168
169 if (!brw)
170 return;
171
172 struct gl_context *ctx = &brw->ctx;
173
174 FLUSH_VERTICES(ctx, 0);
175
176 if (flags & __DRI2_FLUSH_DRAWABLE)
177 intel_resolve_for_dri2_flush(brw, dPriv);
178
179 if (reason == __DRI2_THROTTLE_SWAPBUFFER)
180 brw->need_swap_throttle = true;
181 if (reason == __DRI2_THROTTLE_FLUSHFRONT)
182 brw->need_flush_throttle = true;
183
184 intel_batchbuffer_flush(brw);
185
186 if (INTEL_DEBUG & DEBUG_AUB) {
187 aub_dump_bmp(ctx);
188 }
189 }
190
191 /**
192 * Provides compatibility with loaders that only support the older (version
193 * 1-3) flush interface.
194 *
195 * That includes libGL up to Mesa 9.0, and the X Server at least up to 1.13.
196 */
197 static void
198 intel_dri2_flush(__DRIdrawable *drawable)
199 {
200 intel_dri2_flush_with_flags(drawable->driContextPriv, drawable,
201 __DRI2_FLUSH_DRAWABLE,
202 __DRI2_THROTTLE_SWAPBUFFER);
203 }
204
205 static const struct __DRI2flushExtensionRec intelFlushExtension = {
206 .base = { __DRI2_FLUSH, 4 },
207
208 .flush = intel_dri2_flush,
209 .invalidate = dri2InvalidateDrawable,
210 .flush_with_flags = intel_dri2_flush_with_flags,
211 };
212
213 static struct intel_image_format intel_image_formats[] = {
214 { __DRI_IMAGE_FOURCC_ARGB8888, __DRI_IMAGE_COMPONENTS_RGBA, 1,
215 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ARGB8888, 4 } } },
216
217 { __DRI_IMAGE_FOURCC_ABGR8888, __DRI_IMAGE_COMPONENTS_RGBA, 1,
218 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR8888, 4 } } },
219
220 { __DRI_IMAGE_FOURCC_SARGB8888, __DRI_IMAGE_COMPONENTS_RGBA, 1,
221 { { 0, 0, 0, __DRI_IMAGE_FORMAT_SARGB8, 4 } } },
222
223 { __DRI_IMAGE_FOURCC_XRGB8888, __DRI_IMAGE_COMPONENTS_RGB, 1,
224 { { 0, 0, 0, __DRI_IMAGE_FORMAT_XRGB8888, 4 }, } },
225
226 { __DRI_IMAGE_FOURCC_XBGR8888, __DRI_IMAGE_COMPONENTS_RGB, 1,
227 { { 0, 0, 0, __DRI_IMAGE_FORMAT_XBGR8888, 4 }, } },
228
229 { __DRI_IMAGE_FOURCC_RGB565, __DRI_IMAGE_COMPONENTS_RGB, 1,
230 { { 0, 0, 0, __DRI_IMAGE_FORMAT_RGB565, 2 } } },
231
232 { __DRI_IMAGE_FOURCC_R8, __DRI_IMAGE_COMPONENTS_R, 1,
233 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 }, } },
234
235 { __DRI_IMAGE_FOURCC_GR88, __DRI_IMAGE_COMPONENTS_RG, 1,
236 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88, 2 }, } },
237
238 { __DRI_IMAGE_FOURCC_YUV410, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
239 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
240 { 1, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 },
241 { 2, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 } } },
242
243 { __DRI_IMAGE_FOURCC_YUV411, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
244 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
245 { 1, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 },
246 { 2, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
247
248 { __DRI_IMAGE_FOURCC_YUV420, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
249 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
250 { 1, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 },
251 { 2, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 } } },
252
253 { __DRI_IMAGE_FOURCC_YUV422, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
254 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
255 { 1, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 },
256 { 2, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
257
258 { __DRI_IMAGE_FOURCC_YUV444, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
259 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
260 { 1, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
261 { 2, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
262
263 { __DRI_IMAGE_FOURCC_YVU410, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
264 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
265 { 2, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 },
266 { 1, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 } } },
267
268 { __DRI_IMAGE_FOURCC_YVU411, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
269 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
270 { 2, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 },
271 { 1, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
272
273 { __DRI_IMAGE_FOURCC_YVU420, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
274 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
275 { 2, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 },
276 { 1, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 } } },
277
278 { __DRI_IMAGE_FOURCC_YVU422, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
279 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
280 { 2, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 },
281 { 1, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
282
283 { __DRI_IMAGE_FOURCC_YVU444, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
284 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
285 { 2, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
286 { 1, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
287
288 { __DRI_IMAGE_FOURCC_NV12, __DRI_IMAGE_COMPONENTS_Y_UV, 2,
289 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
290 { 1, 1, 1, __DRI_IMAGE_FORMAT_GR88, 2 } } },
291
292 { __DRI_IMAGE_FOURCC_NV16, __DRI_IMAGE_COMPONENTS_Y_UV, 2,
293 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
294 { 1, 1, 0, __DRI_IMAGE_FORMAT_GR88, 2 } } },
295
296 /* For YUYV buffers, we set up two overlapping DRI images and treat
297 * them as planar buffers in the compositors. Plane 0 is GR88 and
298 * samples YU or YV pairs and places Y into the R component, while
299 * plane 1 is ARGB and samples YUYV clusters and places pairs and
300 * places U into the G component and V into A. This lets the
301 * texture sampler interpolate the Y components correctly when
302 * sampling from plane 0, and interpolate U and V correctly when
303 * sampling from plane 1. */
304 { __DRI_IMAGE_FOURCC_YUYV, __DRI_IMAGE_COMPONENTS_Y_XUXV, 2,
305 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88, 2 },
306 { 0, 1, 0, __DRI_IMAGE_FORMAT_ARGB8888, 4 } } }
307 };
308
309 static void
310 intel_image_warn_if_unaligned(__DRIimage *image, const char *func)
311 {
312 uint32_t tiling, swizzle;
313 drm_intel_bo_get_tiling(image->bo, &tiling, &swizzle);
314
315 if (tiling != I915_TILING_NONE && (image->offset & 0xfff)) {
316 _mesa_warning(NULL, "%s: offset 0x%08x not on tile boundary",
317 func, image->offset);
318 }
319 }
320
321 static struct intel_image_format *
322 intel_image_format_lookup(int fourcc)
323 {
324 struct intel_image_format *f = NULL;
325
326 for (unsigned i = 0; i < ARRAY_SIZE(intel_image_formats); i++) {
327 if (intel_image_formats[i].fourcc == fourcc) {
328 f = &intel_image_formats[i];
329 break;
330 }
331 }
332
333 return f;
334 }
335
336 static boolean intel_lookup_fourcc(int dri_format, int *fourcc)
337 {
338 for (unsigned i = 0; i < ARRAY_SIZE(intel_image_formats); i++) {
339 if (intel_image_formats[i].planes[0].dri_format == dri_format) {
340 *fourcc = intel_image_formats[i].fourcc;
341 return true;
342 }
343 }
344 return false;
345 }
346
347 static __DRIimage *
348 intel_allocate_image(int dri_format, void *loaderPrivate)
349 {
350 __DRIimage *image;
351
352 image = calloc(1, sizeof *image);
353 if (image == NULL)
354 return NULL;
355
356 image->dri_format = dri_format;
357 image->offset = 0;
358
359 image->format = driImageFormatToGLFormat(dri_format);
360 if (dri_format != __DRI_IMAGE_FORMAT_NONE &&
361 image->format == MESA_FORMAT_NONE) {
362 free(image);
363 return NULL;
364 }
365
366 image->internal_format = _mesa_get_format_base_format(image->format);
367 image->data = loaderPrivate;
368
369 return image;
370 }
371
372 /**
373 * Sets up a DRIImage structure to point to a slice out of a miptree.
374 */
375 static void
376 intel_setup_image_from_mipmap_tree(struct brw_context *brw, __DRIimage *image,
377 struct intel_mipmap_tree *mt, GLuint level,
378 GLuint zoffset)
379 {
380 intel_miptree_make_shareable(brw, mt);
381
382 intel_miptree_check_level_layer(mt, level, zoffset);
383
384 image->width = minify(mt->physical_width0, level - mt->first_level);
385 image->height = minify(mt->physical_height0, level - mt->first_level);
386 image->pitch = mt->pitch;
387
388 image->offset = intel_miptree_get_tile_offsets(mt, level, zoffset,
389 &image->tile_x,
390 &image->tile_y);
391
392 drm_intel_bo_unreference(image->bo);
393 image->bo = mt->bo;
394 drm_intel_bo_reference(mt->bo);
395 }
396
397 static __DRIimage *
398 intel_create_image_from_name(__DRIscreen *screen,
399 int width, int height, int format,
400 int name, int pitch, void *loaderPrivate)
401 {
402 struct intel_screen *intelScreen = screen->driverPrivate;
403 __DRIimage *image;
404 int cpp;
405
406 image = intel_allocate_image(format, loaderPrivate);
407 if (image == NULL)
408 return NULL;
409
410 if (image->format == MESA_FORMAT_NONE)
411 cpp = 1;
412 else
413 cpp = _mesa_get_format_bytes(image->format);
414
415 image->width = width;
416 image->height = height;
417 image->pitch = pitch * cpp;
418 image->bo = drm_intel_bo_gem_create_from_name(intelScreen->bufmgr, "image",
419 name);
420 if (!image->bo) {
421 free(image);
422 return NULL;
423 }
424
425 return image;
426 }
427
428 static __DRIimage *
429 intel_create_image_from_renderbuffer(__DRIcontext *context,
430 int renderbuffer, void *loaderPrivate)
431 {
432 __DRIimage *image;
433 struct brw_context *brw = context->driverPrivate;
434 struct gl_context *ctx = &brw->ctx;
435 struct gl_renderbuffer *rb;
436 struct intel_renderbuffer *irb;
437
438 rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
439 if (!rb) {
440 _mesa_error(ctx, GL_INVALID_OPERATION, "glRenderbufferExternalMESA");
441 return NULL;
442 }
443
444 irb = intel_renderbuffer(rb);
445 intel_miptree_make_shareable(brw, irb->mt);
446 image = calloc(1, sizeof *image);
447 if (image == NULL)
448 return NULL;
449
450 image->internal_format = rb->InternalFormat;
451 image->format = rb->Format;
452 image->offset = 0;
453 image->data = loaderPrivate;
454 drm_intel_bo_unreference(image->bo);
455 image->bo = irb->mt->bo;
456 drm_intel_bo_reference(irb->mt->bo);
457 image->width = rb->Width;
458 image->height = rb->Height;
459 image->pitch = irb->mt->pitch;
460 image->dri_format = driGLFormatToImageFormat(image->format);
461 image->has_depthstencil = irb->mt->stencil_mt? true : false;
462
463 rb->NeedsFinishRenderTexture = true;
464 return image;
465 }
466
467 static __DRIimage *
468 intel_create_image_from_texture(__DRIcontext *context, int target,
469 unsigned texture, int zoffset,
470 int level,
471 unsigned *error,
472 void *loaderPrivate)
473 {
474 __DRIimage *image;
475 struct brw_context *brw = context->driverPrivate;
476 struct gl_texture_object *obj;
477 struct intel_texture_object *iobj;
478 GLuint face = 0;
479
480 obj = _mesa_lookup_texture(&brw->ctx, texture);
481 if (!obj || obj->Target != target) {
482 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
483 return NULL;
484 }
485
486 if (target == GL_TEXTURE_CUBE_MAP)
487 face = zoffset;
488
489 _mesa_test_texobj_completeness(&brw->ctx, obj);
490 iobj = intel_texture_object(obj);
491 if (!obj->_BaseComplete || (level > 0 && !obj->_MipmapComplete)) {
492 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
493 return NULL;
494 }
495
496 if (level < obj->BaseLevel || level > obj->_MaxLevel) {
497 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
498 return NULL;
499 }
500
501 if (target == GL_TEXTURE_3D && obj->Image[face][level]->Depth < zoffset) {
502 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
503 return NULL;
504 }
505 image = calloc(1, sizeof *image);
506 if (image == NULL) {
507 *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
508 return NULL;
509 }
510
511 image->internal_format = obj->Image[face][level]->InternalFormat;
512 image->format = obj->Image[face][level]->TexFormat;
513 image->data = loaderPrivate;
514 intel_setup_image_from_mipmap_tree(brw, image, iobj->mt, level, zoffset);
515 image->dri_format = driGLFormatToImageFormat(image->format);
516 image->has_depthstencil = iobj->mt->stencil_mt? true : false;
517 if (image->dri_format == MESA_FORMAT_NONE) {
518 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
519 free(image);
520 return NULL;
521 }
522
523 *error = __DRI_IMAGE_ERROR_SUCCESS;
524 return image;
525 }
526
527 static void
528 intel_destroy_image(__DRIimage *image)
529 {
530 drm_intel_bo_unreference(image->bo);
531 free(image);
532 }
533
534 static __DRIimage *
535 intel_create_image(__DRIscreen *screen,
536 int width, int height, int format,
537 unsigned int use,
538 void *loaderPrivate)
539 {
540 __DRIimage *image;
541 struct intel_screen *intelScreen = screen->driverPrivate;
542 uint32_t tiling;
543 int cpp;
544 unsigned long pitch;
545
546 tiling = I915_TILING_X;
547 if (use & __DRI_IMAGE_USE_CURSOR) {
548 if (width != 64 || height != 64)
549 return NULL;
550 tiling = I915_TILING_NONE;
551 }
552
553 if (use & __DRI_IMAGE_USE_LINEAR)
554 tiling = I915_TILING_NONE;
555
556 image = intel_allocate_image(format, loaderPrivate);
557 if (image == NULL)
558 return NULL;
559
560 cpp = _mesa_get_format_bytes(image->format);
561 image->bo = drm_intel_bo_alloc_tiled(intelScreen->bufmgr, "image",
562 width, height, cpp, &tiling,
563 &pitch, 0);
564 if (image->bo == NULL) {
565 free(image);
566 return NULL;
567 }
568 image->width = width;
569 image->height = height;
570 image->pitch = pitch;
571
572 return image;
573 }
574
575 static GLboolean
576 intel_query_image(__DRIimage *image, int attrib, int *value)
577 {
578 switch (attrib) {
579 case __DRI_IMAGE_ATTRIB_STRIDE:
580 *value = image->pitch;
581 return true;
582 case __DRI_IMAGE_ATTRIB_HANDLE:
583 *value = image->bo->handle;
584 return true;
585 case __DRI_IMAGE_ATTRIB_NAME:
586 return !drm_intel_bo_flink(image->bo, (uint32_t *) value);
587 case __DRI_IMAGE_ATTRIB_FORMAT:
588 *value = image->dri_format;
589 return true;
590 case __DRI_IMAGE_ATTRIB_WIDTH:
591 *value = image->width;
592 return true;
593 case __DRI_IMAGE_ATTRIB_HEIGHT:
594 *value = image->height;
595 return true;
596 case __DRI_IMAGE_ATTRIB_COMPONENTS:
597 if (image->planar_format == NULL)
598 return false;
599 *value = image->planar_format->components;
600 return true;
601 case __DRI_IMAGE_ATTRIB_FD:
602 if (drm_intel_bo_gem_export_to_prime(image->bo, value) == 0)
603 return true;
604 return false;
605 case __DRI_IMAGE_ATTRIB_FOURCC:
606 if (intel_lookup_fourcc(image->dri_format, value))
607 return true;
608 return false;
609 case __DRI_IMAGE_ATTRIB_NUM_PLANES:
610 *value = 1;
611 return true;
612
613 default:
614 return false;
615 }
616 }
617
618 static __DRIimage *
619 intel_dup_image(__DRIimage *orig_image, void *loaderPrivate)
620 {
621 __DRIimage *image;
622
623 image = calloc(1, sizeof *image);
624 if (image == NULL)
625 return NULL;
626
627 drm_intel_bo_reference(orig_image->bo);
628 image->bo = orig_image->bo;
629 image->internal_format = orig_image->internal_format;
630 image->planar_format = orig_image->planar_format;
631 image->dri_format = orig_image->dri_format;
632 image->format = orig_image->format;
633 image->offset = orig_image->offset;
634 image->width = orig_image->width;
635 image->height = orig_image->height;
636 image->pitch = orig_image->pitch;
637 image->tile_x = orig_image->tile_x;
638 image->tile_y = orig_image->tile_y;
639 image->has_depthstencil = orig_image->has_depthstencil;
640 image->data = loaderPrivate;
641
642 memcpy(image->strides, orig_image->strides, sizeof(image->strides));
643 memcpy(image->offsets, orig_image->offsets, sizeof(image->offsets));
644
645 return image;
646 }
647
648 static GLboolean
649 intel_validate_usage(__DRIimage *image, unsigned int use)
650 {
651 if (use & __DRI_IMAGE_USE_CURSOR) {
652 if (image->width != 64 || image->height != 64)
653 return GL_FALSE;
654 }
655
656 return GL_TRUE;
657 }
658
659 static __DRIimage *
660 intel_create_image_from_names(__DRIscreen *screen,
661 int width, int height, int fourcc,
662 int *names, int num_names,
663 int *strides, int *offsets,
664 void *loaderPrivate)
665 {
666 struct intel_image_format *f = NULL;
667 __DRIimage *image;
668 int i, index;
669
670 if (screen == NULL || names == NULL || num_names != 1)
671 return NULL;
672
673 f = intel_image_format_lookup(fourcc);
674 if (f == NULL)
675 return NULL;
676
677 image = intel_create_image_from_name(screen, width, height,
678 __DRI_IMAGE_FORMAT_NONE,
679 names[0], strides[0],
680 loaderPrivate);
681
682 if (image == NULL)
683 return NULL;
684
685 image->planar_format = f;
686 for (i = 0; i < f->nplanes; i++) {
687 index = f->planes[i].buffer_index;
688 image->offsets[index] = offsets[index];
689 image->strides[index] = strides[index];
690 }
691
692 return image;
693 }
694
695 static __DRIimage *
696 intel_create_image_from_fds(__DRIscreen *screen,
697 int width, int height, int fourcc,
698 int *fds, int num_fds, int *strides, int *offsets,
699 void *loaderPrivate)
700 {
701 struct intel_screen *intelScreen = screen->driverPrivate;
702 struct intel_image_format *f;
703 __DRIimage *image;
704 int i, index;
705
706 if (fds == NULL || num_fds < 1)
707 return NULL;
708
709 /* We only support all planes from the same bo */
710 for (i = 0; i < num_fds; i++)
711 if (fds[0] != fds[i])
712 return NULL;
713
714 f = intel_image_format_lookup(fourcc);
715 if (f == NULL)
716 return NULL;
717
718 if (f->nplanes == 1)
719 image = intel_allocate_image(f->planes[0].dri_format, loaderPrivate);
720 else
721 image = intel_allocate_image(__DRI_IMAGE_FORMAT_NONE, loaderPrivate);
722
723 if (image == NULL)
724 return NULL;
725
726 image->width = width;
727 image->height = height;
728 image->pitch = strides[0];
729
730 image->planar_format = f;
731 int size = 0;
732 for (i = 0; i < f->nplanes; i++) {
733 index = f->planes[i].buffer_index;
734 image->offsets[index] = offsets[index];
735 image->strides[index] = strides[index];
736
737 const int plane_height = height >> f->planes[i].height_shift;
738 const int end = offsets[index] + plane_height * strides[index];
739 if (size < end)
740 size = end;
741 }
742
743 image->bo = drm_intel_bo_gem_create_from_prime(intelScreen->bufmgr,
744 fds[0], size);
745 if (image->bo == NULL) {
746 free(image);
747 return NULL;
748 }
749
750 if (f->nplanes == 1) {
751 image->offset = image->offsets[0];
752 intel_image_warn_if_unaligned(image, __func__);
753 }
754
755 return image;
756 }
757
758 static __DRIimage *
759 intel_create_image_from_dma_bufs(__DRIscreen *screen,
760 int width, int height, int fourcc,
761 int *fds, int num_fds,
762 int *strides, int *offsets,
763 enum __DRIYUVColorSpace yuv_color_space,
764 enum __DRISampleRange sample_range,
765 enum __DRIChromaSiting horizontal_siting,
766 enum __DRIChromaSiting vertical_siting,
767 unsigned *error,
768 void *loaderPrivate)
769 {
770 __DRIimage *image;
771 struct intel_image_format *f = intel_image_format_lookup(fourcc);
772
773 if (!f) {
774 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
775 return NULL;
776 }
777
778 image = intel_create_image_from_fds(screen, width, height, fourcc, fds,
779 num_fds, strides, offsets,
780 loaderPrivate);
781
782 /*
783 * Invalid parameters and any inconsistencies between are assumed to be
784 * checked by the caller. Therefore besides unsupported formats one can fail
785 * only in allocation.
786 */
787 if (!image) {
788 *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
789 return NULL;
790 }
791
792 image->dma_buf_imported = true;
793 image->yuv_color_space = yuv_color_space;
794 image->sample_range = sample_range;
795 image->horizontal_siting = horizontal_siting;
796 image->vertical_siting = vertical_siting;
797
798 *error = __DRI_IMAGE_ERROR_SUCCESS;
799 return image;
800 }
801
802 static __DRIimage *
803 intel_from_planar(__DRIimage *parent, int plane, void *loaderPrivate)
804 {
805 int width, height, offset, stride, dri_format, index;
806 struct intel_image_format *f;
807 __DRIimage *image;
808
809 if (parent == NULL || parent->planar_format == NULL)
810 return NULL;
811
812 f = parent->planar_format;
813
814 if (plane >= f->nplanes)
815 return NULL;
816
817 width = parent->width >> f->planes[plane].width_shift;
818 height = parent->height >> f->planes[plane].height_shift;
819 dri_format = f->planes[plane].dri_format;
820 index = f->planes[plane].buffer_index;
821 offset = parent->offsets[index];
822 stride = parent->strides[index];
823
824 image = intel_allocate_image(dri_format, loaderPrivate);
825 if (image == NULL)
826 return NULL;
827
828 if (offset + height * stride > parent->bo->size) {
829 _mesa_warning(NULL, "intel_create_sub_image: subimage out of bounds");
830 free(image);
831 return NULL;
832 }
833
834 image->bo = parent->bo;
835 drm_intel_bo_reference(parent->bo);
836
837 image->width = width;
838 image->height = height;
839 image->pitch = stride;
840 image->offset = offset;
841
842 intel_image_warn_if_unaligned(image, __func__);
843
844 return image;
845 }
846
847 static const __DRIimageExtension intelImageExtension = {
848 .base = { __DRI_IMAGE, 11 },
849
850 .createImageFromName = intel_create_image_from_name,
851 .createImageFromRenderbuffer = intel_create_image_from_renderbuffer,
852 .destroyImage = intel_destroy_image,
853 .createImage = intel_create_image,
854 .queryImage = intel_query_image,
855 .dupImage = intel_dup_image,
856 .validateUsage = intel_validate_usage,
857 .createImageFromNames = intel_create_image_from_names,
858 .fromPlanar = intel_from_planar,
859 .createImageFromTexture = intel_create_image_from_texture,
860 .createImageFromFds = intel_create_image_from_fds,
861 .createImageFromDmaBufs = intel_create_image_from_dma_bufs,
862 .blitImage = NULL,
863 .getCapabilities = NULL
864 };
865
866 static int
867 brw_query_renderer_integer(__DRIscreen *psp, int param, unsigned int *value)
868 {
869 const struct intel_screen *const intelScreen =
870 (struct intel_screen *) psp->driverPrivate;
871
872 switch (param) {
873 case __DRI2_RENDERER_VENDOR_ID:
874 value[0] = 0x8086;
875 return 0;
876 case __DRI2_RENDERER_DEVICE_ID:
877 value[0] = intelScreen->deviceID;
878 return 0;
879 case __DRI2_RENDERER_ACCELERATED:
880 value[0] = 1;
881 return 0;
882 case __DRI2_RENDERER_VIDEO_MEMORY: {
883 /* Once a batch uses more than 75% of the maximum mappable size, we
884 * assume that there's some fragmentation, and we start doing extra
885 * flushing, etc. That's the big cliff apps will care about.
886 */
887 size_t aper_size;
888 size_t mappable_size;
889
890 drm_intel_get_aperture_sizes(psp->fd, &mappable_size, &aper_size);
891
892 const unsigned gpu_mappable_megabytes =
893 (aper_size / (1024 * 1024)) * 3 / 4;
894
895 const long system_memory_pages = sysconf(_SC_PHYS_PAGES);
896 const long system_page_size = sysconf(_SC_PAGE_SIZE);
897
898 if (system_memory_pages <= 0 || system_page_size <= 0)
899 return -1;
900
901 const uint64_t system_memory_bytes = (uint64_t) system_memory_pages
902 * (uint64_t) system_page_size;
903
904 const unsigned system_memory_megabytes =
905 (unsigned) (system_memory_bytes / (1024 * 1024));
906
907 value[0] = MIN2(system_memory_megabytes, gpu_mappable_megabytes);
908 return 0;
909 }
910 case __DRI2_RENDERER_UNIFIED_MEMORY_ARCHITECTURE:
911 value[0] = 1;
912 return 0;
913 default:
914 return driQueryRendererIntegerCommon(psp, param, value);
915 }
916
917 return -1;
918 }
919
920 static int
921 brw_query_renderer_string(__DRIscreen *psp, int param, const char **value)
922 {
923 const struct intel_screen *intelScreen =
924 (struct intel_screen *) psp->driverPrivate;
925
926 switch (param) {
927 case __DRI2_RENDERER_VENDOR_ID:
928 value[0] = brw_vendor_string;
929 return 0;
930 case __DRI2_RENDERER_DEVICE_ID:
931 value[0] = brw_get_renderer_string(intelScreen);
932 return 0;
933 default:
934 break;
935 }
936
937 return -1;
938 }
939
940 static const __DRI2rendererQueryExtension intelRendererQueryExtension = {
941 .base = { __DRI2_RENDERER_QUERY, 1 },
942
943 .queryInteger = brw_query_renderer_integer,
944 .queryString = brw_query_renderer_string
945 };
946
947 static const __DRIrobustnessExtension dri2Robustness = {
948 .base = { __DRI2_ROBUSTNESS, 1 }
949 };
950
951 static const __DRIextension *intelScreenExtensions[] = {
952 &intelTexBufferExtension.base,
953 &intelFenceExtension.base,
954 &intelFlushExtension.base,
955 &intelImageExtension.base,
956 &intelRendererQueryExtension.base,
957 &dri2ConfigQueryExtension.base,
958 NULL
959 };
960
961 static const __DRIextension *intelRobustScreenExtensions[] = {
962 &intelTexBufferExtension.base,
963 &intelFenceExtension.base,
964 &intelFlushExtension.base,
965 &intelImageExtension.base,
966 &intelRendererQueryExtension.base,
967 &dri2ConfigQueryExtension.base,
968 &dri2Robustness.base,
969 NULL
970 };
971
972 static int
973 intel_get_param(__DRIscreen *psp, int param, int *value)
974 {
975 int ret;
976 struct drm_i915_getparam gp;
977
978 memset(&gp, 0, sizeof(gp));
979 gp.param = param;
980 gp.value = value;
981
982 ret = drmCommandWriteRead(psp->fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
983 if (ret < 0 && ret != -EINVAL)
984 _mesa_warning(NULL, "drm_i915_getparam: %d", ret);
985
986 return ret;
987 }
988
989 static bool
990 intel_get_boolean(__DRIscreen *psp, int param)
991 {
992 int value = 0;
993 return (intel_get_param(psp, param, &value) == 0) && value;
994 }
995
996 static void
997 intelDestroyScreen(__DRIscreen * sPriv)
998 {
999 struct intel_screen *intelScreen = sPriv->driverPrivate;
1000
1001 dri_bufmgr_destroy(intelScreen->bufmgr);
1002 driDestroyOptionInfo(&intelScreen->optionCache);
1003
1004 ralloc_free(intelScreen);
1005 sPriv->driverPrivate = NULL;
1006 }
1007
1008
1009 /**
1010 * This is called when we need to set up GL rendering to a new X window.
1011 */
1012 static GLboolean
1013 intelCreateBuffer(__DRIscreen * driScrnPriv,
1014 __DRIdrawable * driDrawPriv,
1015 const struct gl_config * mesaVis, GLboolean isPixmap)
1016 {
1017 struct intel_renderbuffer *rb;
1018 struct intel_screen *screen = (struct intel_screen*) driScrnPriv->driverPrivate;
1019 mesa_format rgbFormat;
1020 unsigned num_samples = intel_quantize_num_samples(screen, mesaVis->samples);
1021 struct gl_framebuffer *fb;
1022
1023 if (isPixmap)
1024 return false;
1025
1026 fb = CALLOC_STRUCT(gl_framebuffer);
1027 if (!fb)
1028 return false;
1029
1030 _mesa_initialize_window_framebuffer(fb, mesaVis);
1031
1032 if (screen->winsys_msaa_samples_override != -1) {
1033 num_samples = screen->winsys_msaa_samples_override;
1034 fb->Visual.samples = num_samples;
1035 }
1036
1037 if (mesaVis->redBits == 5) {
1038 rgbFormat = mesaVis->redMask == 0x1f ? MESA_FORMAT_R5G6B5_UNORM
1039 : MESA_FORMAT_B5G6R5_UNORM;
1040 } else if (mesaVis->sRGBCapable) {
1041 rgbFormat = mesaVis->redMask == 0xff ? MESA_FORMAT_R8G8B8A8_SRGB
1042 : MESA_FORMAT_B8G8R8A8_SRGB;
1043 } else if (mesaVis->alphaBits == 0) {
1044 rgbFormat = mesaVis->redMask == 0xff ? MESA_FORMAT_R8G8B8X8_UNORM
1045 : MESA_FORMAT_B8G8R8X8_UNORM;
1046 } else {
1047 rgbFormat = mesaVis->redMask == 0xff ? MESA_FORMAT_R8G8B8A8_SRGB
1048 : MESA_FORMAT_B8G8R8A8_SRGB;
1049 fb->Visual.sRGBCapable = true;
1050 }
1051
1052 /* setup the hardware-based renderbuffers */
1053 rb = intel_create_renderbuffer(rgbFormat, num_samples);
1054 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &rb->Base.Base);
1055
1056 if (mesaVis->doubleBufferMode) {
1057 rb = intel_create_renderbuffer(rgbFormat, num_samples);
1058 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &rb->Base.Base);
1059 }
1060
1061 /*
1062 * Assert here that the gl_config has an expected depth/stencil bit
1063 * combination: one of d24/s8, d16/s0, d0/s0. (See intelInitScreen2(),
1064 * which constructs the advertised configs.)
1065 */
1066 if (mesaVis->depthBits == 24) {
1067 assert(mesaVis->stencilBits == 8);
1068
1069 if (screen->devinfo->has_hiz_and_separate_stencil) {
1070 rb = intel_create_private_renderbuffer(MESA_FORMAT_Z24_UNORM_X8_UINT,
1071 num_samples);
1072 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &rb->Base.Base);
1073 rb = intel_create_private_renderbuffer(MESA_FORMAT_S_UINT8,
1074 num_samples);
1075 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &rb->Base.Base);
1076 } else {
1077 /*
1078 * Use combined depth/stencil. Note that the renderbuffer is
1079 * attached to two attachment points.
1080 */
1081 rb = intel_create_private_renderbuffer(MESA_FORMAT_Z24_UNORM_S8_UINT,
1082 num_samples);
1083 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &rb->Base.Base);
1084 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &rb->Base.Base);
1085 }
1086 }
1087 else if (mesaVis->depthBits == 16) {
1088 assert(mesaVis->stencilBits == 0);
1089 rb = intel_create_private_renderbuffer(MESA_FORMAT_Z_UNORM16,
1090 num_samples);
1091 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &rb->Base.Base);
1092 }
1093 else {
1094 assert(mesaVis->depthBits == 0);
1095 assert(mesaVis->stencilBits == 0);
1096 }
1097
1098 /* now add any/all software-based renderbuffers we may need */
1099 _swrast_add_soft_renderbuffers(fb,
1100 false, /* never sw color */
1101 false, /* never sw depth */
1102 false, /* never sw stencil */
1103 mesaVis->accumRedBits > 0,
1104 false, /* never sw alpha */
1105 false /* never sw aux */ );
1106 driDrawPriv->driverPrivate = fb;
1107
1108 return true;
1109 }
1110
1111 static void
1112 intelDestroyBuffer(__DRIdrawable * driDrawPriv)
1113 {
1114 struct gl_framebuffer *fb = driDrawPriv->driverPrivate;
1115
1116 _mesa_reference_framebuffer(&fb, NULL);
1117 }
1118
1119 static void
1120 intel_detect_sseu(struct intel_screen *intelScreen)
1121 {
1122 assert(intelScreen->devinfo->gen >= 8);
1123 int ret;
1124
1125 intelScreen->subslice_total = -1;
1126 intelScreen->eu_total = -1;
1127
1128 ret = intel_get_param(intelScreen->driScrnPriv, I915_PARAM_SUBSLICE_TOTAL,
1129 &intelScreen->subslice_total);
1130 if (ret < 0 && ret != -EINVAL)
1131 goto err_out;
1132
1133 ret = intel_get_param(intelScreen->driScrnPriv,
1134 I915_PARAM_EU_TOTAL, &intelScreen->eu_total);
1135 if (ret < 0 && ret != -EINVAL)
1136 goto err_out;
1137
1138 /* Without this information, we cannot get the right Braswell brandstrings,
1139 * and we have to use conservative numbers for GPGPU on many platforms, but
1140 * otherwise, things will just work.
1141 */
1142 if (intelScreen->subslice_total < 1 || intelScreen->eu_total < 1)
1143 _mesa_warning(NULL,
1144 "Kernel 4.1 required to properly query GPU properties.\n");
1145
1146 return;
1147
1148 err_out:
1149 intelScreen->subslice_total = -1;
1150 intelScreen->eu_total = -1;
1151 _mesa_warning(NULL, "Failed to query GPU properties (%s).\n", strerror(-ret));
1152 }
1153
1154 static bool
1155 intel_init_bufmgr(struct intel_screen *intelScreen)
1156 {
1157 __DRIscreen *spriv = intelScreen->driScrnPriv;
1158
1159 intelScreen->no_hw = getenv("INTEL_NO_HW") != NULL;
1160
1161 intelScreen->bufmgr = intel_bufmgr_gem_init(spriv->fd, BATCH_SZ);
1162 if (intelScreen->bufmgr == NULL) {
1163 fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
1164 __func__, __LINE__);
1165 return false;
1166 }
1167
1168 drm_intel_bufmgr_gem_enable_fenced_relocs(intelScreen->bufmgr);
1169
1170 if (!intel_get_boolean(spriv, I915_PARAM_HAS_RELAXED_DELTA)) {
1171 fprintf(stderr, "[%s: %u] Kernel 2.6.39 required.\n", __func__, __LINE__);
1172 return false;
1173 }
1174
1175 return true;
1176 }
1177
1178 static bool
1179 intel_detect_swizzling(struct intel_screen *screen)
1180 {
1181 drm_intel_bo *buffer;
1182 unsigned long flags = 0;
1183 unsigned long aligned_pitch;
1184 uint32_t tiling = I915_TILING_X;
1185 uint32_t swizzle_mode = 0;
1186
1187 buffer = drm_intel_bo_alloc_tiled(screen->bufmgr, "swizzle test",
1188 64, 64, 4,
1189 &tiling, &aligned_pitch, flags);
1190 if (buffer == NULL)
1191 return false;
1192
1193 drm_intel_bo_get_tiling(buffer, &tiling, &swizzle_mode);
1194 drm_intel_bo_unreference(buffer);
1195
1196 if (swizzle_mode == I915_BIT_6_SWIZZLE_NONE)
1197 return false;
1198 else
1199 return true;
1200 }
1201
1202 static int
1203 intel_detect_timestamp(struct intel_screen *screen)
1204 {
1205 uint64_t dummy = 0, last = 0;
1206 int upper, lower, loops;
1207
1208 /* On 64bit systems, some old kernels trigger a hw bug resulting in the
1209 * TIMESTAMP register being shifted and the low 32bits always zero.
1210 *
1211 * More recent kernels offer an interface to read the full 36bits
1212 * everywhere.
1213 */
1214 if (drm_intel_reg_read(screen->bufmgr, TIMESTAMP | 1, &dummy) == 0)
1215 return 3;
1216
1217 /* Determine if we have a 32bit or 64bit kernel by inspecting the
1218 * upper 32bits for a rapidly changing timestamp.
1219 */
1220 if (drm_intel_reg_read(screen->bufmgr, TIMESTAMP, &last))
1221 return 0;
1222
1223 upper = lower = 0;
1224 for (loops = 0; loops < 10; loops++) {
1225 /* The TIMESTAMP should change every 80ns, so several round trips
1226 * through the kernel should be enough to advance it.
1227 */
1228 if (drm_intel_reg_read(screen->bufmgr, TIMESTAMP, &dummy))
1229 return 0;
1230
1231 upper += (dummy >> 32) != (last >> 32);
1232 if (upper > 1) /* beware 32bit counter overflow */
1233 return 2; /* upper dword holds the low 32bits of the timestamp */
1234
1235 lower += (dummy & 0xffffffff) != (last & 0xffffffff);
1236 if (lower > 1)
1237 return 1; /* timestamp is unshifted */
1238
1239 last = dummy;
1240 }
1241
1242 /* No advancement? No timestamp! */
1243 return 0;
1244 }
1245
1246 /**
1247 * Return array of MSAA modes supported by the hardware. The array is
1248 * zero-terminated and sorted in decreasing order.
1249 */
1250 const int*
1251 intel_supported_msaa_modes(const struct intel_screen *screen)
1252 {
1253 static const int gen9_modes[] = {16, 8, 4, 2, 0, -1};
1254 static const int gen8_modes[] = {8, 4, 2, 0, -1};
1255 static const int gen7_modes[] = {8, 4, 0, -1};
1256 static const int gen6_modes[] = {4, 0, -1};
1257 static const int gen4_modes[] = {0, -1};
1258
1259 if (screen->devinfo->gen >= 9) {
1260 return gen9_modes;
1261 } else if (screen->devinfo->gen >= 8) {
1262 return gen8_modes;
1263 } else if (screen->devinfo->gen >= 7) {
1264 return gen7_modes;
1265 } else if (screen->devinfo->gen == 6) {
1266 return gen6_modes;
1267 } else {
1268 return gen4_modes;
1269 }
1270 }
1271
1272 static __DRIconfig**
1273 intel_screen_make_configs(__DRIscreen *dri_screen)
1274 {
1275 static const mesa_format formats[] = {
1276 MESA_FORMAT_B5G6R5_UNORM,
1277 MESA_FORMAT_B8G8R8A8_UNORM,
1278 MESA_FORMAT_B8G8R8X8_UNORM
1279 };
1280
1281 /* GLX_SWAP_COPY_OML is not supported due to page flipping. */
1282 static const GLenum back_buffer_modes[] = {
1283 GLX_SWAP_UNDEFINED_OML, GLX_NONE,
1284 };
1285
1286 static const uint8_t singlesample_samples[1] = {0};
1287 static const uint8_t multisample_samples[2] = {4, 8};
1288
1289 struct intel_screen *screen = dri_screen->driverPrivate;
1290 const struct brw_device_info *devinfo = screen->devinfo;
1291 uint8_t depth_bits[4], stencil_bits[4];
1292 __DRIconfig **configs = NULL;
1293
1294 /* Generate singlesample configs without accumulation buffer. */
1295 for (unsigned i = 0; i < ARRAY_SIZE(formats); i++) {
1296 __DRIconfig **new_configs;
1297 int num_depth_stencil_bits = 2;
1298
1299 /* Starting with DRI2 protocol version 1.1 we can request a depth/stencil
1300 * buffer that has a different number of bits per pixel than the color
1301 * buffer, gen >= 6 supports this.
1302 */
1303 depth_bits[0] = 0;
1304 stencil_bits[0] = 0;
1305
1306 if (formats[i] == MESA_FORMAT_B5G6R5_UNORM) {
1307 depth_bits[1] = 16;
1308 stencil_bits[1] = 0;
1309 if (devinfo->gen >= 6) {
1310 depth_bits[2] = 24;
1311 stencil_bits[2] = 8;
1312 num_depth_stencil_bits = 3;
1313 }
1314 } else {
1315 depth_bits[1] = 24;
1316 stencil_bits[1] = 8;
1317 }
1318
1319 new_configs = driCreateConfigs(formats[i],
1320 depth_bits,
1321 stencil_bits,
1322 num_depth_stencil_bits,
1323 back_buffer_modes, 2,
1324 singlesample_samples, 1,
1325 false);
1326 configs = driConcatConfigs(configs, new_configs);
1327 }
1328
1329 /* Generate the minimum possible set of configs that include an
1330 * accumulation buffer.
1331 */
1332 for (unsigned i = 0; i < ARRAY_SIZE(formats); i++) {
1333 __DRIconfig **new_configs;
1334
1335 if (formats[i] == MESA_FORMAT_B5G6R5_UNORM) {
1336 depth_bits[0] = 16;
1337 stencil_bits[0] = 0;
1338 } else {
1339 depth_bits[0] = 24;
1340 stencil_bits[0] = 8;
1341 }
1342
1343 new_configs = driCreateConfigs(formats[i],
1344 depth_bits, stencil_bits, 1,
1345 back_buffer_modes, 1,
1346 singlesample_samples, 1,
1347 true);
1348 configs = driConcatConfigs(configs, new_configs);
1349 }
1350
1351 /* Generate multisample configs.
1352 *
1353 * This loop breaks early, and hence is a no-op, on gen < 6.
1354 *
1355 * Multisample configs must follow the singlesample configs in order to
1356 * work around an X server bug present in 1.12. The X server chooses to
1357 * associate the first listed RGBA888-Z24S8 config, regardless of its
1358 * sample count, with the 32-bit depth visual used for compositing.
1359 *
1360 * Only doublebuffer configs with GLX_SWAP_UNDEFINED_OML behavior are
1361 * supported. Singlebuffer configs are not supported because no one wants
1362 * them.
1363 */
1364 for (unsigned i = 0; i < ARRAY_SIZE(formats); i++) {
1365 if (devinfo->gen < 6)
1366 break;
1367
1368 __DRIconfig **new_configs;
1369 const int num_depth_stencil_bits = 2;
1370 int num_msaa_modes = 0;
1371
1372 depth_bits[0] = 0;
1373 stencil_bits[0] = 0;
1374
1375 if (formats[i] == MESA_FORMAT_B5G6R5_UNORM) {
1376 depth_bits[1] = 16;
1377 stencil_bits[1] = 0;
1378 } else {
1379 depth_bits[1] = 24;
1380 stencil_bits[1] = 8;
1381 }
1382
1383 if (devinfo->gen >= 7)
1384 num_msaa_modes = 2;
1385 else if (devinfo->gen == 6)
1386 num_msaa_modes = 1;
1387
1388 new_configs = driCreateConfigs(formats[i],
1389 depth_bits,
1390 stencil_bits,
1391 num_depth_stencil_bits,
1392 back_buffer_modes, 1,
1393 multisample_samples,
1394 num_msaa_modes,
1395 false);
1396 configs = driConcatConfigs(configs, new_configs);
1397 }
1398
1399 if (configs == NULL) {
1400 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
1401 __LINE__);
1402 return NULL;
1403 }
1404
1405 return configs;
1406 }
1407
1408 static void
1409 set_max_gl_versions(struct intel_screen *screen)
1410 {
1411 __DRIscreen *psp = screen->driScrnPriv;
1412
1413 switch (screen->devinfo->gen) {
1414 case 9:
1415 case 8:
1416 psp->max_gl_core_version = 43;
1417 psp->max_gl_compat_version = 30;
1418 psp->max_gl_es1_version = 11;
1419 psp->max_gl_es2_version = 31;
1420 break;
1421 case 7:
1422 case 6:
1423 psp->max_gl_core_version = 33;
1424 psp->max_gl_compat_version = 30;
1425 psp->max_gl_es1_version = 11;
1426 psp->max_gl_es2_version = 30;
1427 break;
1428 case 5:
1429 case 4:
1430 psp->max_gl_core_version = 0;
1431 psp->max_gl_compat_version = 21;
1432 psp->max_gl_es1_version = 11;
1433 psp->max_gl_es2_version = 20;
1434 break;
1435 default:
1436 unreachable("unrecognized intel_screen::gen");
1437 }
1438 }
1439
1440 /**
1441 * Return the revision (generally the revid field of the PCI header) of the
1442 * graphics device.
1443 *
1444 * XXX: This function is useful to keep around even if it is not currently in
1445 * use. It is necessary for new platforms and revision specific workarounds or
1446 * features. Please don't remove it so that we know it at least continues to
1447 * build.
1448 */
1449 static __attribute__((__unused__)) int
1450 brw_get_revision(int fd)
1451 {
1452 struct drm_i915_getparam gp;
1453 int revision;
1454 int ret;
1455
1456 memset(&gp, 0, sizeof(gp));
1457 gp.param = I915_PARAM_REVISION;
1458 gp.value = &revision;
1459
1460 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
1461 if (ret)
1462 revision = -1;
1463
1464 return revision;
1465 }
1466
1467 /* Drop when RS headers get pulled to libdrm */
1468 #ifndef I915_PARAM_HAS_RESOURCE_STREAMER
1469 #define I915_PARAM_HAS_RESOURCE_STREAMER 36
1470 #endif
1471
1472 static void
1473 shader_debug_log_mesa(void *data, const char *fmt, ...)
1474 {
1475 struct brw_context *brw = (struct brw_context *)data;
1476 va_list args;
1477
1478 va_start(args, fmt);
1479 GLuint msg_id = 0;
1480 _mesa_gl_vdebug(&brw->ctx, &msg_id,
1481 MESA_DEBUG_SOURCE_SHADER_COMPILER,
1482 MESA_DEBUG_TYPE_OTHER,
1483 MESA_DEBUG_SEVERITY_NOTIFICATION, fmt, args);
1484 va_end(args);
1485 }
1486
1487 static void
1488 shader_perf_log_mesa(void *data, const char *fmt, ...)
1489 {
1490 struct brw_context *brw = (struct brw_context *)data;
1491
1492 va_list args;
1493 va_start(args, fmt);
1494
1495 if (unlikely(INTEL_DEBUG & DEBUG_PERF)) {
1496 va_list args_copy;
1497 va_copy(args_copy, args);
1498 vfprintf(stderr, fmt, args_copy);
1499 va_end(args_copy);
1500 }
1501
1502 if (brw->perf_debug) {
1503 GLuint msg_id = 0;
1504 _mesa_gl_vdebug(&brw->ctx, &msg_id,
1505 MESA_DEBUG_SOURCE_SHADER_COMPILER,
1506 MESA_DEBUG_TYPE_PERFORMANCE,
1507 MESA_DEBUG_SEVERITY_MEDIUM, fmt, args);
1508 }
1509 va_end(args);
1510 }
1511
1512 /**
1513 * This is the driver specific part of the createNewScreen entry point.
1514 * Called when using DRI2.
1515 *
1516 * \return the struct gl_config supported by this driver
1517 */
1518 static const
1519 __DRIconfig **intelInitScreen2(__DRIscreen *psp)
1520 {
1521 struct intel_screen *intelScreen;
1522
1523 if (psp->image.loader) {
1524 } else if (psp->dri2.loader->base.version <= 2 ||
1525 psp->dri2.loader->getBuffersWithFormat == NULL) {
1526 fprintf(stderr,
1527 "\nERROR! DRI2 loader with getBuffersWithFormat() "
1528 "support required\n");
1529 return false;
1530 }
1531
1532 /* Allocate the private area */
1533 intelScreen = rzalloc(NULL, struct intel_screen);
1534 if (!intelScreen) {
1535 fprintf(stderr, "\nERROR! Allocating private area failed\n");
1536 return false;
1537 }
1538 /* parse information in __driConfigOptions */
1539 driParseOptionInfo(&intelScreen->optionCache, brw_config_options.xml);
1540
1541 intelScreen->driScrnPriv = psp;
1542 psp->driverPrivate = (void *) intelScreen;
1543
1544 if (!intel_init_bufmgr(intelScreen))
1545 return false;
1546
1547 intelScreen->deviceID = drm_intel_bufmgr_gem_get_devid(intelScreen->bufmgr);
1548 intelScreen->devinfo = brw_get_device_info(intelScreen->deviceID);
1549 if (!intelScreen->devinfo)
1550 return false;
1551
1552 brw_process_intel_debug_variable();
1553
1554 if (INTEL_DEBUG & DEBUG_BUFMGR)
1555 dri_bufmgr_set_debug(intelScreen->bufmgr, true);
1556
1557 if ((INTEL_DEBUG & DEBUG_SHADER_TIME) && intelScreen->devinfo->gen < 7) {
1558 fprintf(stderr,
1559 "shader_time debugging requires gen7 (Ivybridge) or better.\n");
1560 INTEL_DEBUG &= ~DEBUG_SHADER_TIME;
1561 }
1562
1563 if (INTEL_DEBUG & DEBUG_AUB)
1564 drm_intel_bufmgr_gem_set_aub_dump(intelScreen->bufmgr, true);
1565
1566 intelScreen->hw_has_swizzling = intel_detect_swizzling(intelScreen);
1567 intelScreen->hw_has_timestamp = intel_detect_timestamp(intelScreen);
1568
1569 /* GENs prior to 8 do not support EU/Subslice info */
1570 if (intelScreen->devinfo->gen >= 8) {
1571 intel_detect_sseu(intelScreen);
1572 } else if (intelScreen->devinfo->gen == 7) {
1573 intelScreen->subslice_total = 1 << (intelScreen->devinfo->gt - 1);
1574 }
1575
1576 const char *force_msaa = getenv("INTEL_FORCE_MSAA");
1577 if (force_msaa) {
1578 intelScreen->winsys_msaa_samples_override =
1579 intel_quantize_num_samples(intelScreen, atoi(force_msaa));
1580 printf("Forcing winsys sample count to %d\n",
1581 intelScreen->winsys_msaa_samples_override);
1582 } else {
1583 intelScreen->winsys_msaa_samples_override = -1;
1584 }
1585
1586 set_max_gl_versions(intelScreen);
1587
1588 /* Notification of GPU resets requires hardware contexts and a kernel new
1589 * enough to support DRM_IOCTL_I915_GET_RESET_STATS. If the ioctl is
1590 * supported, calling it with a context of 0 will either generate EPERM or
1591 * no error. If the ioctl is not supported, it always generate EINVAL.
1592 * Use this to determine whether to advertise the __DRI2_ROBUSTNESS
1593 * extension to the loader.
1594 *
1595 * Don't even try on pre-Gen6, since we don't attempt to use contexts there.
1596 */
1597 if (intelScreen->devinfo->gen >= 6) {
1598 struct drm_i915_reset_stats stats;
1599 memset(&stats, 0, sizeof(stats));
1600
1601 const int ret = drmIoctl(psp->fd, DRM_IOCTL_I915_GET_RESET_STATS, &stats);
1602
1603 intelScreen->has_context_reset_notification =
1604 (ret != -1 || errno != EINVAL);
1605 }
1606
1607 struct drm_i915_getparam getparam;
1608 getparam.param = I915_PARAM_CMD_PARSER_VERSION;
1609 getparam.value = &intelScreen->cmd_parser_version;
1610 const int ret = drmIoctl(psp->fd, DRM_IOCTL_I915_GETPARAM, &getparam);
1611 if (ret == -1)
1612 intelScreen->cmd_parser_version = 0;
1613
1614 /* Haswell requires command parser version 6 in order to write to the
1615 * MI_MATH GPR registers, and version 7 in order to use
1616 * MI_LOAD_REGISTER_REG (which all users of MI_MATH use).
1617 */
1618 intelScreen->has_mi_math_and_lrr = intelScreen->devinfo->gen >= 8 ||
1619 (intelScreen->devinfo->is_haswell &&
1620 intelScreen->cmd_parser_version >= 7);
1621
1622 psp->extensions = !intelScreen->has_context_reset_notification
1623 ? intelScreenExtensions : intelRobustScreenExtensions;
1624
1625 intelScreen->compiler = brw_compiler_create(intelScreen,
1626 intelScreen->devinfo);
1627 intelScreen->compiler->shader_debug_log = shader_debug_log_mesa;
1628 intelScreen->compiler->shader_perf_log = shader_perf_log_mesa;
1629 intelScreen->program_id = 1;
1630
1631 if (intelScreen->devinfo->has_resource_streamer) {
1632 int val = -1;
1633 getparam.param = I915_PARAM_HAS_RESOURCE_STREAMER;
1634 getparam.value = &val;
1635
1636 drmIoctl(psp->fd, DRM_IOCTL_I915_GETPARAM, &getparam);
1637 intelScreen->has_resource_streamer = val > 0;
1638 }
1639
1640 return (const __DRIconfig**) intel_screen_make_configs(psp);
1641 }
1642
1643 struct intel_buffer {
1644 __DRIbuffer base;
1645 drm_intel_bo *bo;
1646 };
1647
1648 static __DRIbuffer *
1649 intelAllocateBuffer(__DRIscreen *screen,
1650 unsigned attachment, unsigned format,
1651 int width, int height)
1652 {
1653 struct intel_buffer *intelBuffer;
1654 struct intel_screen *intelScreen = screen->driverPrivate;
1655
1656 assert(attachment == __DRI_BUFFER_FRONT_LEFT ||
1657 attachment == __DRI_BUFFER_BACK_LEFT);
1658
1659 intelBuffer = calloc(1, sizeof *intelBuffer);
1660 if (intelBuffer == NULL)
1661 return NULL;
1662
1663 /* The front and back buffers are color buffers, which are X tiled. */
1664 uint32_t tiling = I915_TILING_X;
1665 unsigned long pitch;
1666 int cpp = format / 8;
1667 intelBuffer->bo = drm_intel_bo_alloc_tiled(intelScreen->bufmgr,
1668 "intelAllocateBuffer",
1669 width,
1670 height,
1671 cpp,
1672 &tiling, &pitch,
1673 BO_ALLOC_FOR_RENDER);
1674
1675 if (intelBuffer->bo == NULL) {
1676 free(intelBuffer);
1677 return NULL;
1678 }
1679
1680 drm_intel_bo_flink(intelBuffer->bo, &intelBuffer->base.name);
1681
1682 intelBuffer->base.attachment = attachment;
1683 intelBuffer->base.cpp = cpp;
1684 intelBuffer->base.pitch = pitch;
1685
1686 return &intelBuffer->base;
1687 }
1688
1689 static void
1690 intelReleaseBuffer(__DRIscreen *screen, __DRIbuffer *buffer)
1691 {
1692 struct intel_buffer *intelBuffer = (struct intel_buffer *) buffer;
1693
1694 drm_intel_bo_unreference(intelBuffer->bo);
1695 free(intelBuffer);
1696 }
1697
1698 static const struct __DriverAPIRec brw_driver_api = {
1699 .InitScreen = intelInitScreen2,
1700 .DestroyScreen = intelDestroyScreen,
1701 .CreateContext = brwCreateContext,
1702 .DestroyContext = intelDestroyContext,
1703 .CreateBuffer = intelCreateBuffer,
1704 .DestroyBuffer = intelDestroyBuffer,
1705 .MakeCurrent = intelMakeCurrent,
1706 .UnbindContext = intelUnbindContext,
1707 .AllocateBuffer = intelAllocateBuffer,
1708 .ReleaseBuffer = intelReleaseBuffer
1709 };
1710
1711 static const struct __DRIDriverVtableExtensionRec brw_vtable = {
1712 .base = { __DRI_DRIVER_VTABLE, 1 },
1713 .vtable = &brw_driver_api,
1714 };
1715
1716 static const __DRIextension *brw_driver_extensions[] = {
1717 &driCoreExtension.base,
1718 &driImageDriverExtension.base,
1719 &driDRI2Extension.base,
1720 &brw_vtable.base,
1721 &brw_config_options.base,
1722 NULL
1723 };
1724
1725 PUBLIC const __DRIextension **__driDriverGetExtensions_i965(void)
1726 {
1727 globalDriverAPI = &brw_driver_api;
1728
1729 return brw_driver_extensions;
1730 }