i965: Rename __DRIScreen pointers to "dri_screen".
[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_RGB565, __DRI_IMAGE_COMPONENTS_RGB, 1,
234 { { 0, 0, 0, __DRI_IMAGE_FORMAT_RGB565, 2 } } },
235
236 { __DRI_IMAGE_FOURCC_R8, __DRI_IMAGE_COMPONENTS_R, 1,
237 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 }, } },
238
239 { __DRI_IMAGE_FOURCC_GR88, __DRI_IMAGE_COMPONENTS_RG, 1,
240 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88, 2 }, } },
241
242 { __DRI_IMAGE_FOURCC_YUV410, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
243 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
244 { 1, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 },
245 { 2, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 } } },
246
247 { __DRI_IMAGE_FOURCC_YUV411, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
248 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
249 { 1, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 },
250 { 2, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
251
252 { __DRI_IMAGE_FOURCC_YUV420, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
253 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
254 { 1, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 },
255 { 2, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 } } },
256
257 { __DRI_IMAGE_FOURCC_YUV422, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
258 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
259 { 1, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 },
260 { 2, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
261
262 { __DRI_IMAGE_FOURCC_YUV444, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
263 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
264 { 1, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
265 { 2, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
266
267 { __DRI_IMAGE_FOURCC_YVU410, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
268 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
269 { 2, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 },
270 { 1, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 } } },
271
272 { __DRI_IMAGE_FOURCC_YVU411, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
273 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
274 { 2, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 },
275 { 1, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
276
277 { __DRI_IMAGE_FOURCC_YVU420, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
278 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
279 { 2, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 },
280 { 1, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 } } },
281
282 { __DRI_IMAGE_FOURCC_YVU422, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
283 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
284 { 2, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 },
285 { 1, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
286
287 { __DRI_IMAGE_FOURCC_YVU444, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
288 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
289 { 2, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
290 { 1, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
291
292 { __DRI_IMAGE_FOURCC_NV12, __DRI_IMAGE_COMPONENTS_Y_UV, 2,
293 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
294 { 1, 1, 1, __DRI_IMAGE_FORMAT_GR88, 2 } } },
295
296 { __DRI_IMAGE_FOURCC_NV16, __DRI_IMAGE_COMPONENTS_Y_UV, 2,
297 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
298 { 1, 1, 0, __DRI_IMAGE_FORMAT_GR88, 2 } } },
299
300 /* For YUYV buffers, we set up two overlapping DRI images and treat
301 * them as planar buffers in the compositors. Plane 0 is GR88 and
302 * samples YU or YV pairs and places Y into the R component, while
303 * plane 1 is ARGB and samples YUYV clusters and places pairs and
304 * places U into the G component and V into A. This lets the
305 * texture sampler interpolate the Y components correctly when
306 * sampling from plane 0, and interpolate U and V correctly when
307 * sampling from plane 1. */
308 { __DRI_IMAGE_FOURCC_YUYV, __DRI_IMAGE_COMPONENTS_Y_XUXV, 2,
309 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88, 2 },
310 { 0, 1, 0, __DRI_IMAGE_FORMAT_ARGB8888, 4 } } }
311 };
312
313 static void
314 intel_image_warn_if_unaligned(__DRIimage *image, const char *func)
315 {
316 uint32_t tiling, swizzle;
317 drm_intel_bo_get_tiling(image->bo, &tiling, &swizzle);
318
319 if (tiling != I915_TILING_NONE && (image->offset & 0xfff)) {
320 _mesa_warning(NULL, "%s: offset 0x%08x not on tile boundary",
321 func, image->offset);
322 }
323 }
324
325 static struct intel_image_format *
326 intel_image_format_lookup(int fourcc)
327 {
328 struct intel_image_format *f = NULL;
329
330 for (unsigned i = 0; i < ARRAY_SIZE(intel_image_formats); i++) {
331 if (intel_image_formats[i].fourcc == fourcc) {
332 f = &intel_image_formats[i];
333 break;
334 }
335 }
336
337 return f;
338 }
339
340 static boolean intel_lookup_fourcc(int dri_format, int *fourcc)
341 {
342 for (unsigned i = 0; i < ARRAY_SIZE(intel_image_formats); i++) {
343 if (intel_image_formats[i].planes[0].dri_format == dri_format) {
344 *fourcc = intel_image_formats[i].fourcc;
345 return true;
346 }
347 }
348 return false;
349 }
350
351 static __DRIimage *
352 intel_allocate_image(int dri_format, void *loaderPrivate)
353 {
354 __DRIimage *image;
355
356 image = calloc(1, sizeof *image);
357 if (image == NULL)
358 return NULL;
359
360 image->dri_format = dri_format;
361 image->offset = 0;
362
363 image->format = driImageFormatToGLFormat(dri_format);
364 if (dri_format != __DRI_IMAGE_FORMAT_NONE &&
365 image->format == MESA_FORMAT_NONE) {
366 free(image);
367 return NULL;
368 }
369
370 image->internal_format = _mesa_get_format_base_format(image->format);
371 image->data = loaderPrivate;
372
373 return image;
374 }
375
376 /**
377 * Sets up a DRIImage structure to point to a slice out of a miptree.
378 */
379 static void
380 intel_setup_image_from_mipmap_tree(struct brw_context *brw, __DRIimage *image,
381 struct intel_mipmap_tree *mt, GLuint level,
382 GLuint zoffset)
383 {
384 intel_miptree_make_shareable(brw, mt);
385
386 intel_miptree_check_level_layer(mt, level, zoffset);
387
388 image->width = minify(mt->physical_width0, level - mt->first_level);
389 image->height = minify(mt->physical_height0, level - mt->first_level);
390 image->pitch = mt->pitch;
391
392 image->offset = intel_miptree_get_tile_offsets(mt, level, zoffset,
393 &image->tile_x,
394 &image->tile_y);
395
396 drm_intel_bo_unreference(image->bo);
397 image->bo = mt->bo;
398 drm_intel_bo_reference(mt->bo);
399 }
400
401 static __DRIimage *
402 intel_create_image_from_name(__DRIscreen *dri_screen,
403 int width, int height, int format,
404 int name, int pitch, void *loaderPrivate)
405 {
406 struct intel_screen *intelScreen = dri_screen->driverPrivate;
407 __DRIimage *image;
408 int cpp;
409
410 image = intel_allocate_image(format, loaderPrivate);
411 if (image == NULL)
412 return NULL;
413
414 if (image->format == MESA_FORMAT_NONE)
415 cpp = 1;
416 else
417 cpp = _mesa_get_format_bytes(image->format);
418
419 image->width = width;
420 image->height = height;
421 image->pitch = pitch * cpp;
422 image->bo = drm_intel_bo_gem_create_from_name(intelScreen->bufmgr, "image",
423 name);
424 if (!image->bo) {
425 free(image);
426 return NULL;
427 }
428
429 return image;
430 }
431
432 static __DRIimage *
433 intel_create_image_from_renderbuffer(__DRIcontext *context,
434 int renderbuffer, void *loaderPrivate)
435 {
436 __DRIimage *image;
437 struct brw_context *brw = context->driverPrivate;
438 struct gl_context *ctx = &brw->ctx;
439 struct gl_renderbuffer *rb;
440 struct intel_renderbuffer *irb;
441
442 rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
443 if (!rb) {
444 _mesa_error(ctx, GL_INVALID_OPERATION, "glRenderbufferExternalMESA");
445 return NULL;
446 }
447
448 irb = intel_renderbuffer(rb);
449 intel_miptree_make_shareable(brw, irb->mt);
450 image = calloc(1, sizeof *image);
451 if (image == NULL)
452 return NULL;
453
454 image->internal_format = rb->InternalFormat;
455 image->format = rb->Format;
456 image->offset = 0;
457 image->data = loaderPrivate;
458 drm_intel_bo_unreference(image->bo);
459 image->bo = irb->mt->bo;
460 drm_intel_bo_reference(irb->mt->bo);
461 image->width = rb->Width;
462 image->height = rb->Height;
463 image->pitch = irb->mt->pitch;
464 image->dri_format = driGLFormatToImageFormat(image->format);
465 image->has_depthstencil = irb->mt->stencil_mt? true : false;
466
467 rb->NeedsFinishRenderTexture = true;
468 return image;
469 }
470
471 static __DRIimage *
472 intel_create_image_from_texture(__DRIcontext *context, int target,
473 unsigned texture, int zoffset,
474 int level,
475 unsigned *error,
476 void *loaderPrivate)
477 {
478 __DRIimage *image;
479 struct brw_context *brw = context->driverPrivate;
480 struct gl_texture_object *obj;
481 struct intel_texture_object *iobj;
482 GLuint face = 0;
483
484 obj = _mesa_lookup_texture(&brw->ctx, texture);
485 if (!obj || obj->Target != target) {
486 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
487 return NULL;
488 }
489
490 if (target == GL_TEXTURE_CUBE_MAP)
491 face = zoffset;
492
493 _mesa_test_texobj_completeness(&brw->ctx, obj);
494 iobj = intel_texture_object(obj);
495 if (!obj->_BaseComplete || (level > 0 && !obj->_MipmapComplete)) {
496 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
497 return NULL;
498 }
499
500 if (level < obj->BaseLevel || level > obj->_MaxLevel) {
501 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
502 return NULL;
503 }
504
505 if (target == GL_TEXTURE_3D && obj->Image[face][level]->Depth < zoffset) {
506 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
507 return NULL;
508 }
509 image = calloc(1, sizeof *image);
510 if (image == NULL) {
511 *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
512 return NULL;
513 }
514
515 image->internal_format = obj->Image[face][level]->InternalFormat;
516 image->format = obj->Image[face][level]->TexFormat;
517 image->data = loaderPrivate;
518 intel_setup_image_from_mipmap_tree(brw, image, iobj->mt, level, zoffset);
519 image->dri_format = driGLFormatToImageFormat(image->format);
520 image->has_depthstencil = iobj->mt->stencil_mt? true : false;
521 if (image->dri_format == MESA_FORMAT_NONE) {
522 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
523 free(image);
524 return NULL;
525 }
526
527 *error = __DRI_IMAGE_ERROR_SUCCESS;
528 return image;
529 }
530
531 static void
532 intel_destroy_image(__DRIimage *image)
533 {
534 drm_intel_bo_unreference(image->bo);
535 free(image);
536 }
537
538 static __DRIimage *
539 intel_create_image(__DRIscreen *dri_screen,
540 int width, int height, int format,
541 unsigned int use,
542 void *loaderPrivate)
543 {
544 __DRIimage *image;
545 struct intel_screen *intelScreen = dri_screen->driverPrivate;
546 uint32_t tiling;
547 int cpp;
548 unsigned long pitch;
549
550 tiling = I915_TILING_X;
551 if (use & __DRI_IMAGE_USE_CURSOR) {
552 if (width != 64 || height != 64)
553 return NULL;
554 tiling = I915_TILING_NONE;
555 }
556
557 if (use & __DRI_IMAGE_USE_LINEAR)
558 tiling = I915_TILING_NONE;
559
560 image = intel_allocate_image(format, loaderPrivate);
561 if (image == NULL)
562 return NULL;
563
564 cpp = _mesa_get_format_bytes(image->format);
565 image->bo = drm_intel_bo_alloc_tiled(intelScreen->bufmgr, "image",
566 width, height, cpp, &tiling,
567 &pitch, 0);
568 if (image->bo == NULL) {
569 free(image);
570 return NULL;
571 }
572 image->width = width;
573 image->height = height;
574 image->pitch = pitch;
575
576 return image;
577 }
578
579 static GLboolean
580 intel_query_image(__DRIimage *image, int attrib, int *value)
581 {
582 switch (attrib) {
583 case __DRI_IMAGE_ATTRIB_STRIDE:
584 *value = image->pitch;
585 return true;
586 case __DRI_IMAGE_ATTRIB_HANDLE:
587 *value = image->bo->handle;
588 return true;
589 case __DRI_IMAGE_ATTRIB_NAME:
590 return !drm_intel_bo_flink(image->bo, (uint32_t *) value);
591 case __DRI_IMAGE_ATTRIB_FORMAT:
592 *value = image->dri_format;
593 return true;
594 case __DRI_IMAGE_ATTRIB_WIDTH:
595 *value = image->width;
596 return true;
597 case __DRI_IMAGE_ATTRIB_HEIGHT:
598 *value = image->height;
599 return true;
600 case __DRI_IMAGE_ATTRIB_COMPONENTS:
601 if (image->planar_format == NULL)
602 return false;
603 *value = image->planar_format->components;
604 return true;
605 case __DRI_IMAGE_ATTRIB_FD:
606 return !drm_intel_bo_gem_export_to_prime(image->bo, value);
607 case __DRI_IMAGE_ATTRIB_FOURCC:
608 return intel_lookup_fourcc(image->dri_format, value);
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 *dri_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 (dri_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(dri_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 *dri_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 = dri_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 *dri_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(dri_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 *dri_screen,
868 int param, unsigned int *value)
869 {
870 const struct intel_screen *const intelScreen =
871 (struct intel_screen *) dri_screen->driverPrivate;
872
873 switch (param) {
874 case __DRI2_RENDERER_VENDOR_ID:
875 value[0] = 0x8086;
876 return 0;
877 case __DRI2_RENDERER_DEVICE_ID:
878 value[0] = intelScreen->deviceID;
879 return 0;
880 case __DRI2_RENDERER_ACCELERATED:
881 value[0] = 1;
882 return 0;
883 case __DRI2_RENDERER_VIDEO_MEMORY: {
884 /* Once a batch uses more than 75% of the maximum mappable size, we
885 * assume that there's some fragmentation, and we start doing extra
886 * flushing, etc. That's the big cliff apps will care about.
887 */
888 size_t aper_size;
889 size_t mappable_size;
890
891 drm_intel_get_aperture_sizes(dri_screen->fd, &mappable_size, &aper_size);
892
893 const unsigned gpu_mappable_megabytes =
894 (aper_size / (1024 * 1024)) * 3 / 4;
895
896 const long system_memory_pages = sysconf(_SC_PHYS_PAGES);
897 const long system_page_size = sysconf(_SC_PAGE_SIZE);
898
899 if (system_memory_pages <= 0 || system_page_size <= 0)
900 return -1;
901
902 const uint64_t system_memory_bytes = (uint64_t) system_memory_pages
903 * (uint64_t) system_page_size;
904
905 const unsigned system_memory_megabytes =
906 (unsigned) (system_memory_bytes / (1024 * 1024));
907
908 value[0] = MIN2(system_memory_megabytes, gpu_mappable_megabytes);
909 return 0;
910 }
911 case __DRI2_RENDERER_UNIFIED_MEMORY_ARCHITECTURE:
912 value[0] = 1;
913 return 0;
914 default:
915 return driQueryRendererIntegerCommon(dri_screen, param, value);
916 }
917
918 return -1;
919 }
920
921 static int
922 brw_query_renderer_string(__DRIscreen *dri_screen,
923 int param, const char **value)
924 {
925 const struct intel_screen *intelScreen =
926 (struct intel_screen *) dri_screen->driverPrivate;
927
928 switch (param) {
929 case __DRI2_RENDERER_VENDOR_ID:
930 value[0] = brw_vendor_string;
931 return 0;
932 case __DRI2_RENDERER_DEVICE_ID:
933 value[0] = brw_get_renderer_string(intelScreen);
934 return 0;
935 default:
936 break;
937 }
938
939 return -1;
940 }
941
942 static const __DRI2rendererQueryExtension intelRendererQueryExtension = {
943 .base = { __DRI2_RENDERER_QUERY, 1 },
944
945 .queryInteger = brw_query_renderer_integer,
946 .queryString = brw_query_renderer_string
947 };
948
949 static const __DRIrobustnessExtension dri2Robustness = {
950 .base = { __DRI2_ROBUSTNESS, 1 }
951 };
952
953 static const __DRIextension *intelScreenExtensions[] = {
954 &intelTexBufferExtension.base,
955 &intelFenceExtension.base,
956 &intelFlushExtension.base,
957 &intelImageExtension.base,
958 &intelRendererQueryExtension.base,
959 &dri2ConfigQueryExtension.base,
960 NULL
961 };
962
963 static const __DRIextension *intelRobustScreenExtensions[] = {
964 &intelTexBufferExtension.base,
965 &intelFenceExtension.base,
966 &intelFlushExtension.base,
967 &intelImageExtension.base,
968 &intelRendererQueryExtension.base,
969 &dri2ConfigQueryExtension.base,
970 &dri2Robustness.base,
971 NULL
972 };
973
974 static int
975 intel_get_param(struct intel_screen *screen, int param, int *value)
976 {
977 int ret = 0;
978 struct drm_i915_getparam gp;
979
980 memset(&gp, 0, sizeof(gp));
981 gp.param = param;
982 gp.value = value;
983
984 if (drmIoctl(screen->driScrnPriv->fd, DRM_IOCTL_I915_GETPARAM, &gp) == -1) {
985 ret = -errno;
986 if (ret != -EINVAL)
987 _mesa_warning(NULL, "drm_i915_getparam: %d", ret);
988 }
989
990 return ret;
991 }
992
993 static bool
994 intel_get_boolean(struct intel_screen *screen, int param)
995 {
996 int value = 0;
997 return (intel_get_param(screen, param, &value) == 0) && value;
998 }
999
1000 static int
1001 intel_get_integer(struct intel_screen *screen, int param)
1002 {
1003 int value = -1;
1004
1005 if (intel_get_param(screen, param, &value) == 0)
1006 return value;
1007
1008 return -1;
1009 }
1010
1011 static void
1012 intelDestroyScreen(__DRIscreen * sPriv)
1013 {
1014 struct intel_screen *intelScreen = sPriv->driverPrivate;
1015
1016 dri_bufmgr_destroy(intelScreen->bufmgr);
1017 driDestroyOptionInfo(&intelScreen->optionCache);
1018
1019 ralloc_free(intelScreen);
1020 sPriv->driverPrivate = NULL;
1021 }
1022
1023
1024 /**
1025 * This is called when we need to set up GL rendering to a new X window.
1026 */
1027 static GLboolean
1028 intelCreateBuffer(__DRIscreen *dri_screen,
1029 __DRIdrawable * driDrawPriv,
1030 const struct gl_config * mesaVis, GLboolean isPixmap)
1031 {
1032 struct intel_renderbuffer *rb;
1033 struct intel_screen *screen = (struct intel_screen *)
1034 dri_screen->driverPrivate;
1035 mesa_format rgbFormat;
1036 unsigned num_samples =
1037 intel_quantize_num_samples(screen, mesaVis->samples);
1038 struct gl_framebuffer *fb;
1039
1040 if (isPixmap)
1041 return false;
1042
1043 fb = CALLOC_STRUCT(gl_framebuffer);
1044 if (!fb)
1045 return false;
1046
1047 _mesa_initialize_window_framebuffer(fb, mesaVis);
1048
1049 if (screen->winsys_msaa_samples_override != -1) {
1050 num_samples = screen->winsys_msaa_samples_override;
1051 fb->Visual.samples = num_samples;
1052 }
1053
1054 if (mesaVis->redBits == 5) {
1055 rgbFormat = mesaVis->redMask == 0x1f ? MESA_FORMAT_R5G6B5_UNORM
1056 : MESA_FORMAT_B5G6R5_UNORM;
1057 } else if (mesaVis->sRGBCapable) {
1058 rgbFormat = mesaVis->redMask == 0xff ? MESA_FORMAT_R8G8B8A8_SRGB
1059 : MESA_FORMAT_B8G8R8A8_SRGB;
1060 } else if (mesaVis->alphaBits == 0) {
1061 rgbFormat = mesaVis->redMask == 0xff ? MESA_FORMAT_R8G8B8X8_UNORM
1062 : MESA_FORMAT_B8G8R8X8_UNORM;
1063 } else {
1064 rgbFormat = mesaVis->redMask == 0xff ? MESA_FORMAT_R8G8B8A8_SRGB
1065 : MESA_FORMAT_B8G8R8A8_SRGB;
1066 fb->Visual.sRGBCapable = true;
1067 }
1068
1069 /* setup the hardware-based renderbuffers */
1070 rb = intel_create_renderbuffer(rgbFormat, num_samples);
1071 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &rb->Base.Base);
1072
1073 if (mesaVis->doubleBufferMode) {
1074 rb = intel_create_renderbuffer(rgbFormat, num_samples);
1075 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &rb->Base.Base);
1076 }
1077
1078 /*
1079 * Assert here that the gl_config has an expected depth/stencil bit
1080 * combination: one of d24/s8, d16/s0, d0/s0. (See intelInitScreen2(),
1081 * which constructs the advertised configs.)
1082 */
1083 if (mesaVis->depthBits == 24) {
1084 assert(mesaVis->stencilBits == 8);
1085
1086 if (screen->devinfo->has_hiz_and_separate_stencil) {
1087 rb = intel_create_private_renderbuffer(MESA_FORMAT_Z24_UNORM_X8_UINT,
1088 num_samples);
1089 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &rb->Base.Base);
1090 rb = intel_create_private_renderbuffer(MESA_FORMAT_S_UINT8,
1091 num_samples);
1092 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &rb->Base.Base);
1093 } else {
1094 /*
1095 * Use combined depth/stencil. Note that the renderbuffer is
1096 * attached to two attachment points.
1097 */
1098 rb = intel_create_private_renderbuffer(MESA_FORMAT_Z24_UNORM_S8_UINT,
1099 num_samples);
1100 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &rb->Base.Base);
1101 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &rb->Base.Base);
1102 }
1103 }
1104 else if (mesaVis->depthBits == 16) {
1105 assert(mesaVis->stencilBits == 0);
1106 rb = intel_create_private_renderbuffer(MESA_FORMAT_Z_UNORM16,
1107 num_samples);
1108 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &rb->Base.Base);
1109 }
1110 else {
1111 assert(mesaVis->depthBits == 0);
1112 assert(mesaVis->stencilBits == 0);
1113 }
1114
1115 /* now add any/all software-based renderbuffers we may need */
1116 _swrast_add_soft_renderbuffers(fb,
1117 false, /* never sw color */
1118 false, /* never sw depth */
1119 false, /* never sw stencil */
1120 mesaVis->accumRedBits > 0,
1121 false, /* never sw alpha */
1122 false /* never sw aux */ );
1123 driDrawPriv->driverPrivate = fb;
1124
1125 return true;
1126 }
1127
1128 static void
1129 intelDestroyBuffer(__DRIdrawable * driDrawPriv)
1130 {
1131 struct gl_framebuffer *fb = driDrawPriv->driverPrivate;
1132
1133 _mesa_reference_framebuffer(&fb, NULL);
1134 }
1135
1136 static void
1137 intel_detect_sseu(struct intel_screen *intelScreen)
1138 {
1139 assert(intelScreen->devinfo->gen >= 8);
1140 int ret;
1141
1142 intelScreen->subslice_total = -1;
1143 intelScreen->eu_total = -1;
1144
1145 ret = intel_get_param(intelScreen, I915_PARAM_SUBSLICE_TOTAL,
1146 &intelScreen->subslice_total);
1147 if (ret < 0 && ret != -EINVAL)
1148 goto err_out;
1149
1150 ret = intel_get_param(intelScreen,
1151 I915_PARAM_EU_TOTAL, &intelScreen->eu_total);
1152 if (ret < 0 && ret != -EINVAL)
1153 goto err_out;
1154
1155 /* Without this information, we cannot get the right Braswell brandstrings,
1156 * and we have to use conservative numbers for GPGPU on many platforms, but
1157 * otherwise, things will just work.
1158 */
1159 if (intelScreen->subslice_total < 1 || intelScreen->eu_total < 1)
1160 _mesa_warning(NULL,
1161 "Kernel 4.1 required to properly query GPU properties.\n");
1162
1163 return;
1164
1165 err_out:
1166 intelScreen->subslice_total = -1;
1167 intelScreen->eu_total = -1;
1168 _mesa_warning(NULL, "Failed to query GPU properties (%s).\n", strerror(-ret));
1169 }
1170
1171 static bool
1172 intel_init_bufmgr(struct intel_screen *intelScreen)
1173 {
1174 __DRIscreen *dri_screen = intelScreen->driScrnPriv;
1175
1176 intelScreen->no_hw = getenv("INTEL_NO_HW") != NULL;
1177
1178 intelScreen->bufmgr = intel_bufmgr_gem_init(dri_screen->fd, BATCH_SZ);
1179 if (intelScreen->bufmgr == NULL) {
1180 fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
1181 __func__, __LINE__);
1182 return false;
1183 }
1184
1185 drm_intel_bufmgr_gem_enable_fenced_relocs(intelScreen->bufmgr);
1186
1187 if (!intel_get_boolean(intelScreen, I915_PARAM_HAS_RELAXED_DELTA)) {
1188 fprintf(stderr, "[%s: %u] Kernel 2.6.39 required.\n", __func__, __LINE__);
1189 return false;
1190 }
1191
1192 return true;
1193 }
1194
1195 static bool
1196 intel_detect_swizzling(struct intel_screen *screen)
1197 {
1198 drm_intel_bo *buffer;
1199 unsigned long flags = 0;
1200 unsigned long aligned_pitch;
1201 uint32_t tiling = I915_TILING_X;
1202 uint32_t swizzle_mode = 0;
1203
1204 buffer = drm_intel_bo_alloc_tiled(screen->bufmgr, "swizzle test",
1205 64, 64, 4,
1206 &tiling, &aligned_pitch, flags);
1207 if (buffer == NULL)
1208 return false;
1209
1210 drm_intel_bo_get_tiling(buffer, &tiling, &swizzle_mode);
1211 drm_intel_bo_unreference(buffer);
1212
1213 if (swizzle_mode == I915_BIT_6_SWIZZLE_NONE)
1214 return false;
1215 else
1216 return true;
1217 }
1218
1219 static int
1220 intel_detect_timestamp(struct intel_screen *screen)
1221 {
1222 uint64_t dummy = 0, last = 0;
1223 int upper, lower, loops;
1224
1225 /* On 64bit systems, some old kernels trigger a hw bug resulting in the
1226 * TIMESTAMP register being shifted and the low 32bits always zero.
1227 *
1228 * More recent kernels offer an interface to read the full 36bits
1229 * everywhere.
1230 */
1231 if (drm_intel_reg_read(screen->bufmgr, TIMESTAMP | 1, &dummy) == 0)
1232 return 3;
1233
1234 /* Determine if we have a 32bit or 64bit kernel by inspecting the
1235 * upper 32bits for a rapidly changing timestamp.
1236 */
1237 if (drm_intel_reg_read(screen->bufmgr, TIMESTAMP, &last))
1238 return 0;
1239
1240 upper = lower = 0;
1241 for (loops = 0; loops < 10; loops++) {
1242 /* The TIMESTAMP should change every 80ns, so several round trips
1243 * through the kernel should be enough to advance it.
1244 */
1245 if (drm_intel_reg_read(screen->bufmgr, TIMESTAMP, &dummy))
1246 return 0;
1247
1248 upper += (dummy >> 32) != (last >> 32);
1249 if (upper > 1) /* beware 32bit counter overflow */
1250 return 2; /* upper dword holds the low 32bits of the timestamp */
1251
1252 lower += (dummy & 0xffffffff) != (last & 0xffffffff);
1253 if (lower > 1)
1254 return 1; /* timestamp is unshifted */
1255
1256 last = dummy;
1257 }
1258
1259 /* No advancement? No timestamp! */
1260 return 0;
1261 }
1262
1263 /**
1264 * Return array of MSAA modes supported by the hardware. The array is
1265 * zero-terminated and sorted in decreasing order.
1266 */
1267 const int*
1268 intel_supported_msaa_modes(const struct intel_screen *screen)
1269 {
1270 static const int gen9_modes[] = {16, 8, 4, 2, 0, -1};
1271 static const int gen8_modes[] = {8, 4, 2, 0, -1};
1272 static const int gen7_modes[] = {8, 4, 0, -1};
1273 static const int gen6_modes[] = {4, 0, -1};
1274 static const int gen4_modes[] = {0, -1};
1275
1276 if (screen->devinfo->gen >= 9) {
1277 return gen9_modes;
1278 } else if (screen->devinfo->gen >= 8) {
1279 return gen8_modes;
1280 } else if (screen->devinfo->gen >= 7) {
1281 return gen7_modes;
1282 } else if (screen->devinfo->gen == 6) {
1283 return gen6_modes;
1284 } else {
1285 return gen4_modes;
1286 }
1287 }
1288
1289 static __DRIconfig**
1290 intel_screen_make_configs(__DRIscreen *dri_screen)
1291 {
1292 static const mesa_format formats[] = {
1293 MESA_FORMAT_B5G6R5_UNORM,
1294 MESA_FORMAT_B8G8R8A8_UNORM,
1295 MESA_FORMAT_B8G8R8X8_UNORM
1296 };
1297
1298 /* GLX_SWAP_COPY_OML is not supported due to page flipping. */
1299 static const GLenum back_buffer_modes[] = {
1300 GLX_SWAP_UNDEFINED_OML, GLX_NONE,
1301 };
1302
1303 static const uint8_t singlesample_samples[1] = {0};
1304 static const uint8_t multisample_samples[2] = {4, 8};
1305
1306 struct intel_screen *screen = dri_screen->driverPrivate;
1307 const struct gen_device_info *devinfo = screen->devinfo;
1308 uint8_t depth_bits[4], stencil_bits[4];
1309 __DRIconfig **configs = NULL;
1310
1311 /* Generate singlesample configs without accumulation buffer. */
1312 for (unsigned i = 0; i < ARRAY_SIZE(formats); i++) {
1313 __DRIconfig **new_configs;
1314 int num_depth_stencil_bits = 2;
1315
1316 /* Starting with DRI2 protocol version 1.1 we can request a depth/stencil
1317 * buffer that has a different number of bits per pixel than the color
1318 * buffer, gen >= 6 supports this.
1319 */
1320 depth_bits[0] = 0;
1321 stencil_bits[0] = 0;
1322
1323 if (formats[i] == MESA_FORMAT_B5G6R5_UNORM) {
1324 depth_bits[1] = 16;
1325 stencil_bits[1] = 0;
1326 if (devinfo->gen >= 6) {
1327 depth_bits[2] = 24;
1328 stencil_bits[2] = 8;
1329 num_depth_stencil_bits = 3;
1330 }
1331 } else {
1332 depth_bits[1] = 24;
1333 stencil_bits[1] = 8;
1334 }
1335
1336 new_configs = driCreateConfigs(formats[i],
1337 depth_bits,
1338 stencil_bits,
1339 num_depth_stencil_bits,
1340 back_buffer_modes, 2,
1341 singlesample_samples, 1,
1342 false, false);
1343 configs = driConcatConfigs(configs, new_configs);
1344 }
1345
1346 /* Generate the minimum possible set of configs that include an
1347 * accumulation buffer.
1348 */
1349 for (unsigned i = 0; i < ARRAY_SIZE(formats); i++) {
1350 __DRIconfig **new_configs;
1351
1352 if (formats[i] == MESA_FORMAT_B5G6R5_UNORM) {
1353 depth_bits[0] = 16;
1354 stencil_bits[0] = 0;
1355 } else {
1356 depth_bits[0] = 24;
1357 stencil_bits[0] = 8;
1358 }
1359
1360 new_configs = driCreateConfigs(formats[i],
1361 depth_bits, stencil_bits, 1,
1362 back_buffer_modes, 1,
1363 singlesample_samples, 1,
1364 true, false);
1365 configs = driConcatConfigs(configs, new_configs);
1366 }
1367
1368 /* Generate multisample configs.
1369 *
1370 * This loop breaks early, and hence is a no-op, on gen < 6.
1371 *
1372 * Multisample configs must follow the singlesample configs in order to
1373 * work around an X server bug present in 1.12. The X server chooses to
1374 * associate the first listed RGBA888-Z24S8 config, regardless of its
1375 * sample count, with the 32-bit depth visual used for compositing.
1376 *
1377 * Only doublebuffer configs with GLX_SWAP_UNDEFINED_OML behavior are
1378 * supported. Singlebuffer configs are not supported because no one wants
1379 * them.
1380 */
1381 for (unsigned i = 0; i < ARRAY_SIZE(formats); i++) {
1382 if (devinfo->gen < 6)
1383 break;
1384
1385 __DRIconfig **new_configs;
1386 const int num_depth_stencil_bits = 2;
1387 int num_msaa_modes = 0;
1388
1389 depth_bits[0] = 0;
1390 stencil_bits[0] = 0;
1391
1392 if (formats[i] == MESA_FORMAT_B5G6R5_UNORM) {
1393 depth_bits[1] = 16;
1394 stencil_bits[1] = 0;
1395 } else {
1396 depth_bits[1] = 24;
1397 stencil_bits[1] = 8;
1398 }
1399
1400 if (devinfo->gen >= 7)
1401 num_msaa_modes = 2;
1402 else if (devinfo->gen == 6)
1403 num_msaa_modes = 1;
1404
1405 new_configs = driCreateConfigs(formats[i],
1406 depth_bits,
1407 stencil_bits,
1408 num_depth_stencil_bits,
1409 back_buffer_modes, 1,
1410 multisample_samples,
1411 num_msaa_modes,
1412 false, false);
1413 configs = driConcatConfigs(configs, new_configs);
1414 }
1415
1416 if (configs == NULL) {
1417 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
1418 __LINE__);
1419 return NULL;
1420 }
1421
1422 return configs;
1423 }
1424
1425 static void
1426 set_max_gl_versions(struct intel_screen *screen)
1427 {
1428 __DRIscreen *dri_screen = screen->driScrnPriv;
1429
1430 switch (screen->devinfo->gen) {
1431 case 9:
1432 case 8:
1433 dri_screen->max_gl_core_version = 44;
1434 dri_screen->max_gl_compat_version = 30;
1435 dri_screen->max_gl_es1_version = 11;
1436 dri_screen->max_gl_es2_version = 31;
1437 break;
1438 case 7:
1439 dri_screen->max_gl_core_version = 33;
1440 dri_screen->max_gl_compat_version = 30;
1441 dri_screen->max_gl_es1_version = 11;
1442 dri_screen->max_gl_es2_version = screen->devinfo->is_haswell ? 31 : 30;
1443 break;
1444 case 6:
1445 dri_screen->max_gl_core_version = 33;
1446 dri_screen->max_gl_compat_version = 30;
1447 dri_screen->max_gl_es1_version = 11;
1448 dri_screen->max_gl_es2_version = 30;
1449 break;
1450 case 5:
1451 case 4:
1452 dri_screen->max_gl_core_version = 0;
1453 dri_screen->max_gl_compat_version = 21;
1454 dri_screen->max_gl_es1_version = 11;
1455 dri_screen->max_gl_es2_version = 20;
1456 break;
1457 default:
1458 unreachable("unrecognized intel_screen::gen");
1459 }
1460 }
1461
1462 /**
1463 * Return the revision (generally the revid field of the PCI header) of the
1464 * graphics device.
1465 *
1466 * XXX: This function is useful to keep around even if it is not currently in
1467 * use. It is necessary for new platforms and revision specific workarounds or
1468 * features. Please don't remove it so that we know it at least continues to
1469 * build.
1470 */
1471 static __attribute__((__unused__)) int
1472 brw_get_revision(int fd)
1473 {
1474 struct drm_i915_getparam gp;
1475 int revision;
1476 int ret;
1477
1478 memset(&gp, 0, sizeof(gp));
1479 gp.param = I915_PARAM_REVISION;
1480 gp.value = &revision;
1481
1482 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
1483 if (ret)
1484 revision = -1;
1485
1486 return revision;
1487 }
1488
1489 /* Drop when RS headers get pulled to libdrm */
1490 #ifndef I915_PARAM_HAS_RESOURCE_STREAMER
1491 #define I915_PARAM_HAS_RESOURCE_STREAMER 36
1492 #endif
1493
1494 static void
1495 shader_debug_log_mesa(void *data, const char *fmt, ...)
1496 {
1497 struct brw_context *brw = (struct brw_context *)data;
1498 va_list args;
1499
1500 va_start(args, fmt);
1501 GLuint msg_id = 0;
1502 _mesa_gl_vdebug(&brw->ctx, &msg_id,
1503 MESA_DEBUG_SOURCE_SHADER_COMPILER,
1504 MESA_DEBUG_TYPE_OTHER,
1505 MESA_DEBUG_SEVERITY_NOTIFICATION, fmt, args);
1506 va_end(args);
1507 }
1508
1509 static void
1510 shader_perf_log_mesa(void *data, const char *fmt, ...)
1511 {
1512 struct brw_context *brw = (struct brw_context *)data;
1513
1514 va_list args;
1515 va_start(args, fmt);
1516
1517 if (unlikely(INTEL_DEBUG & DEBUG_PERF)) {
1518 va_list args_copy;
1519 va_copy(args_copy, args);
1520 vfprintf(stderr, fmt, args_copy);
1521 va_end(args_copy);
1522 }
1523
1524 if (brw->perf_debug) {
1525 GLuint msg_id = 0;
1526 _mesa_gl_vdebug(&brw->ctx, &msg_id,
1527 MESA_DEBUG_SOURCE_SHADER_COMPILER,
1528 MESA_DEBUG_TYPE_PERFORMANCE,
1529 MESA_DEBUG_SEVERITY_MEDIUM, fmt, args);
1530 }
1531 va_end(args);
1532 }
1533
1534 /**
1535 * This is the driver specific part of the createNewScreen entry point.
1536 * Called when using DRI2.
1537 *
1538 * \return the struct gl_config supported by this driver
1539 */
1540 static const
1541 __DRIconfig **intelInitScreen2(__DRIscreen *dri_screen)
1542 {
1543 struct intel_screen *intelScreen;
1544
1545 if (dri_screen->image.loader) {
1546 } else if (dri_screen->dri2.loader->base.version <= 2 ||
1547 dri_screen->dri2.loader->getBuffersWithFormat == NULL) {
1548 fprintf(stderr,
1549 "\nERROR! DRI2 loader with getBuffersWithFormat() "
1550 "support required\n");
1551 return false;
1552 }
1553
1554 /* Allocate the private area */
1555 intelScreen = rzalloc(NULL, struct intel_screen);
1556 if (!intelScreen) {
1557 fprintf(stderr, "\nERROR! Allocating private area failed\n");
1558 return false;
1559 }
1560 /* parse information in __driConfigOptions */
1561 driParseOptionInfo(&intelScreen->optionCache, brw_config_options.xml);
1562
1563 intelScreen->driScrnPriv = dri_screen;
1564 dri_screen->driverPrivate = (void *) intelScreen;
1565
1566 if (!intel_init_bufmgr(intelScreen))
1567 return false;
1568
1569 intelScreen->deviceID = drm_intel_bufmgr_gem_get_devid(intelScreen->bufmgr);
1570 intelScreen->devinfo = gen_get_device_info(intelScreen->deviceID);
1571 if (!intelScreen->devinfo)
1572 return false;
1573
1574 brw_process_intel_debug_variable();
1575
1576 if (INTEL_DEBUG & DEBUG_BUFMGR)
1577 dri_bufmgr_set_debug(intelScreen->bufmgr, true);
1578
1579 if ((INTEL_DEBUG & DEBUG_SHADER_TIME) && intelScreen->devinfo->gen < 7) {
1580 fprintf(stderr,
1581 "shader_time debugging requires gen7 (Ivybridge) or better.\n");
1582 INTEL_DEBUG &= ~DEBUG_SHADER_TIME;
1583 }
1584
1585 if (INTEL_DEBUG & DEBUG_AUB)
1586 drm_intel_bufmgr_gem_set_aub_dump(intelScreen->bufmgr, true);
1587
1588 #ifndef I915_PARAM_MMAP_GTT_VERSION
1589 #define I915_PARAM_MMAP_GTT_VERSION 40 /* XXX delete me with new libdrm */
1590 #endif
1591 if (intel_get_integer(intelScreen, I915_PARAM_MMAP_GTT_VERSION) >= 1) {
1592 /* Theorectically unlimited! At least for individual objects...
1593 *
1594 * Currently the entire (global) address space for all GTT maps is
1595 * limited to 64bits. That is all objects on the system that are
1596 * setup for GTT mmapping must fit within 64bits. An attempt to use
1597 * one that exceeds the limit with fail in drm_intel_bo_map_gtt().
1598 *
1599 * Long before we hit that limit, we will be practically limited by
1600 * that any single object must fit in physical memory (RAM). The upper
1601 * limit on the CPU's address space is currently 48bits (Skylake), of
1602 * which only 39bits can be physical memory. (The GPU itself also has
1603 * a 48bit addressable virtual space.) We can fit over 32 million
1604 * objects of the current maximum allocable size before running out
1605 * of mmap space.
1606 */
1607 intelScreen->max_gtt_map_object_size = UINT64_MAX;
1608 } else {
1609 /* Estimate the size of the mappable aperture into the GTT. There's an
1610 * ioctl to get the whole GTT size, but not one to get the mappable subset.
1611 * It turns out it's basically always 256MB, though some ancient hardware
1612 * was smaller.
1613 */
1614 uint32_t gtt_size = 256 * 1024 * 1024;
1615
1616 /* We don't want to map two objects such that a memcpy between them would
1617 * just fault one mapping in and then the other over and over forever. So
1618 * we would need to divide the GTT size by 2. Additionally, some GTT is
1619 * taken up by things like the framebuffer and the ringbuffer and such, so
1620 * be more conservative.
1621 */
1622 intelScreen->max_gtt_map_object_size = gtt_size / 4;
1623 }
1624
1625 intelScreen->hw_has_swizzling = intel_detect_swizzling(intelScreen);
1626 intelScreen->hw_has_timestamp = intel_detect_timestamp(intelScreen);
1627
1628 /* GENs prior to 8 do not support EU/Subslice info */
1629 if (intelScreen->devinfo->gen >= 8) {
1630 intel_detect_sseu(intelScreen);
1631 } else if (intelScreen->devinfo->gen == 7) {
1632 intelScreen->subslice_total = 1 << (intelScreen->devinfo->gt - 1);
1633 }
1634
1635 const char *force_msaa = getenv("INTEL_FORCE_MSAA");
1636 if (force_msaa) {
1637 intelScreen->winsys_msaa_samples_override =
1638 intel_quantize_num_samples(intelScreen, atoi(force_msaa));
1639 printf("Forcing winsys sample count to %d\n",
1640 intelScreen->winsys_msaa_samples_override);
1641 } else {
1642 intelScreen->winsys_msaa_samples_override = -1;
1643 }
1644
1645 set_max_gl_versions(intelScreen);
1646
1647 /* Notification of GPU resets requires hardware contexts and a kernel new
1648 * enough to support DRM_IOCTL_I915_GET_RESET_STATS. If the ioctl is
1649 * supported, calling it with a context of 0 will either generate EPERM or
1650 * no error. If the ioctl is not supported, it always generate EINVAL.
1651 * Use this to determine whether to advertise the __DRI2_ROBUSTNESS
1652 * extension to the loader.
1653 *
1654 * Don't even try on pre-Gen6, since we don't attempt to use contexts there.
1655 */
1656 if (intelScreen->devinfo->gen >= 6) {
1657 struct drm_i915_reset_stats stats;
1658 memset(&stats, 0, sizeof(stats));
1659
1660 const int ret = drmIoctl(dri_screen->fd, DRM_IOCTL_I915_GET_RESET_STATS, &stats);
1661
1662 intelScreen->has_context_reset_notification =
1663 (ret != -1 || errno != EINVAL);
1664 }
1665
1666 if (intel_get_param(intelScreen, I915_PARAM_CMD_PARSER_VERSION,
1667 &intelScreen->cmd_parser_version) < 0) {
1668 intelScreen->cmd_parser_version = 0;
1669 }
1670
1671 /* Haswell requires command parser version 6 in order to write to the
1672 * MI_MATH GPR registers, and version 7 in order to use
1673 * MI_LOAD_REGISTER_REG (which all users of MI_MATH use).
1674 */
1675 intelScreen->has_mi_math_and_lrr = intelScreen->devinfo->gen >= 8 ||
1676 (intelScreen->devinfo->is_haswell &&
1677 intelScreen->cmd_parser_version >= 7);
1678
1679 dri_screen->extensions = !intelScreen->has_context_reset_notification
1680 ? intelScreenExtensions : intelRobustScreenExtensions;
1681
1682 intelScreen->compiler = brw_compiler_create(intelScreen,
1683 intelScreen->devinfo);
1684 intelScreen->compiler->shader_debug_log = shader_debug_log_mesa;
1685 intelScreen->compiler->shader_perf_log = shader_perf_log_mesa;
1686 intelScreen->program_id = 1;
1687
1688 if (intelScreen->devinfo->has_resource_streamer) {
1689 intelScreen->has_resource_streamer =
1690 intel_get_boolean(intelScreen, I915_PARAM_HAS_RESOURCE_STREAMER);
1691 }
1692
1693 return (const __DRIconfig**) intel_screen_make_configs(dri_screen);
1694 }
1695
1696 struct intel_buffer {
1697 __DRIbuffer base;
1698 drm_intel_bo *bo;
1699 };
1700
1701 static __DRIbuffer *
1702 intelAllocateBuffer(__DRIscreen *dri_screen,
1703 unsigned attachment, unsigned format,
1704 int width, int height)
1705 {
1706 struct intel_buffer *intelBuffer;
1707 struct intel_screen *intelScreen = dri_screen->driverPrivate;
1708
1709 assert(attachment == __DRI_BUFFER_FRONT_LEFT ||
1710 attachment == __DRI_BUFFER_BACK_LEFT);
1711
1712 intelBuffer = calloc(1, sizeof *intelBuffer);
1713 if (intelBuffer == NULL)
1714 return NULL;
1715
1716 /* The front and back buffers are color buffers, which are X tiled. */
1717 uint32_t tiling = I915_TILING_X;
1718 unsigned long pitch;
1719 int cpp = format / 8;
1720 intelBuffer->bo = drm_intel_bo_alloc_tiled(intelScreen->bufmgr,
1721 "intelAllocateBuffer",
1722 width,
1723 height,
1724 cpp,
1725 &tiling, &pitch,
1726 BO_ALLOC_FOR_RENDER);
1727
1728 if (intelBuffer->bo == NULL) {
1729 free(intelBuffer);
1730 return NULL;
1731 }
1732
1733 drm_intel_bo_flink(intelBuffer->bo, &intelBuffer->base.name);
1734
1735 intelBuffer->base.attachment = attachment;
1736 intelBuffer->base.cpp = cpp;
1737 intelBuffer->base.pitch = pitch;
1738
1739 return &intelBuffer->base;
1740 }
1741
1742 static void
1743 intelReleaseBuffer(__DRIscreen *dri_screen, __DRIbuffer *buffer)
1744 {
1745 struct intel_buffer *intelBuffer = (struct intel_buffer *) buffer;
1746
1747 drm_intel_bo_unreference(intelBuffer->bo);
1748 free(intelBuffer);
1749 }
1750
1751 static const struct __DriverAPIRec brw_driver_api = {
1752 .InitScreen = intelInitScreen2,
1753 .DestroyScreen = intelDestroyScreen,
1754 .CreateContext = brwCreateContext,
1755 .DestroyContext = intelDestroyContext,
1756 .CreateBuffer = intelCreateBuffer,
1757 .DestroyBuffer = intelDestroyBuffer,
1758 .MakeCurrent = intelMakeCurrent,
1759 .UnbindContext = intelUnbindContext,
1760 .AllocateBuffer = intelAllocateBuffer,
1761 .ReleaseBuffer = intelReleaseBuffer
1762 };
1763
1764 static const struct __DRIDriverVtableExtensionRec brw_vtable = {
1765 .base = { __DRI_DRIVER_VTABLE, 1 },
1766 .vtable = &brw_driver_api,
1767 };
1768
1769 static const __DRIextension *brw_driver_extensions[] = {
1770 &driCoreExtension.base,
1771 &driImageDriverExtension.base,
1772 &driDRI2Extension.base,
1773 &brw_vtable.base,
1774 &brw_config_options.base,
1775 NULL
1776 };
1777
1778 PUBLIC const __DRIextension **__driDriverGetExtensions_i965(void)
1779 {
1780 globalDriverAPI = &brw_driver_api;
1781
1782 return brw_driver_extensions;
1783 }