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