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