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