26cef1149b89bacac9616c631763cdec5bda6695
[mesa.git] / src / mesa / drivers / dri / i915 / intel_context.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29 #include "main/glheader.h"
30 #include "main/context.h"
31 #include "main/extensions.h"
32 #include "main/fbobject.h"
33 #include "main/framebuffer.h"
34 #include "main/imports.h"
35 #include "main/points.h"
36 #include "main/renderbuffer.h"
37
38 #include "swrast/swrast.h"
39 #include "swrast_setup/swrast_setup.h"
40 #include "tnl/tnl.h"
41 #include "drivers/common/driverfuncs.h"
42 #include "drivers/common/meta.h"
43
44 #include "intel_chipset.h"
45 #include "intel_buffers.h"
46 #include "intel_tex.h"
47 #include "intel_batchbuffer.h"
48 #include "intel_clear.h"
49 #include "intel_extensions.h"
50 #include "intel_pixel.h"
51 #include "intel_regions.h"
52 #include "intel_buffer_objects.h"
53 #include "intel_fbo.h"
54 #include "intel_bufmgr.h"
55 #include "intel_screen.h"
56 #include "intel_mipmap_tree.h"
57
58 #include "utils.h"
59 #include "../glsl/ralloc.h"
60
61 #ifndef INTEL_DEBUG
62 int INTEL_DEBUG = (0);
63 #endif
64
65
66 static const GLubyte *
67 intelGetString(struct gl_context * ctx, GLenum name)
68 {
69 const struct intel_context *const intel = intel_context(ctx);
70 const char *chipset;
71 static char buffer[128];
72
73 switch (name) {
74 case GL_VENDOR:
75 return (GLubyte *) "Intel Open Source Technology Center";
76 break;
77
78 case GL_RENDERER:
79 switch (intel->intelScreen->deviceID) {
80 #undef CHIPSET
81 #define CHIPSET(id, symbol, str) case id: chipset = str; break;
82 #include "pci_ids/i915_pci_ids.h"
83 #include "pci_ids/i965_pci_ids.h"
84 default:
85 chipset = "Unknown Intel Chipset";
86 break;
87 }
88
89 (void) driGetRendererString(buffer, chipset, 0);
90 return (GLubyte *) buffer;
91
92 default:
93 return NULL;
94 }
95 }
96
97 void
98 intel_resolve_for_dri2_flush(struct intel_context *intel,
99 __DRIdrawable *drawable)
100 {
101 if (intel->gen < 6) {
102 /* MSAA and fast color clear are not supported, so don't waste time
103 * checking whether a resolve is needed.
104 */
105 return;
106 }
107
108 struct gl_framebuffer *fb = drawable->driverPrivate;
109 struct intel_renderbuffer *rb;
110
111 /* Usually, only the back buffer will need to be downsampled. However,
112 * the front buffer will also need it if the user has rendered into it.
113 */
114 static const gl_buffer_index buffers[2] = {
115 BUFFER_BACK_LEFT,
116 BUFFER_FRONT_LEFT,
117 };
118
119 for (int i = 0; i < 2; ++i) {
120 rb = intel_get_renderbuffer(fb, buffers[i]);
121 if (rb == NULL || rb->mt == NULL)
122 continue;
123 if (rb->mt->num_samples <= 1)
124 intel_miptree_resolve_color(intel, rb->mt);
125 else
126 intel_miptree_downsample(intel, rb->mt);
127 }
128 }
129
130 static void
131 intel_flush_front(struct gl_context *ctx)
132 {
133 struct intel_context *intel = intel_context(ctx);
134 __DRIcontext *driContext = intel->driContext;
135 __DRIdrawable *driDrawable = driContext->driDrawablePriv;
136 __DRIscreen *const screen = intel->intelScreen->driScrnPriv;
137
138 if (intel->front_buffer_dirty && _mesa_is_winsys_fbo(ctx->DrawBuffer)) {
139 if (screen->dri2.loader->flushFrontBuffer != NULL &&
140 driDrawable &&
141 driDrawable->loaderPrivate) {
142
143 /* Resolve before flushing FAKE_FRONT_LEFT to FRONT_LEFT.
144 *
145 * This potentially resolves both front and back buffer. It
146 * is unnecessary to resolve the back, but harms nothing except
147 * performance. And no one cares about front-buffer render
148 * performance.
149 */
150 intel_resolve_for_dri2_flush(intel, driDrawable);
151
152 screen->dri2.loader->flushFrontBuffer(driDrawable,
153 driDrawable->loaderPrivate);
154
155 /* We set the dirty bit in intel_prepare_render() if we're
156 * front buffer rendering once we get there.
157 */
158 intel->front_buffer_dirty = false;
159 }
160 }
161 }
162
163 static unsigned
164 intel_bits_per_pixel(const struct intel_renderbuffer *rb)
165 {
166 return _mesa_get_format_bytes(intel_rb_format(rb)) * 8;
167 }
168
169 static void
170 intel_query_dri2_buffers(struct intel_context *intel,
171 __DRIdrawable *drawable,
172 __DRIbuffer **buffers,
173 int *count);
174
175 static void
176 intel_process_dri2_buffer(struct intel_context *intel,
177 __DRIdrawable *drawable,
178 __DRIbuffer *buffer,
179 struct intel_renderbuffer *rb,
180 const char *buffer_name);
181
182 void
183 intel_update_renderbuffers(__DRIcontext *context, __DRIdrawable *drawable)
184 {
185 struct gl_framebuffer *fb = drawable->driverPrivate;
186 struct intel_renderbuffer *rb;
187 struct intel_context *intel = context->driverPrivate;
188 __DRIbuffer *buffers = NULL;
189 int i, count;
190 const char *region_name;
191
192 /* Set this up front, so that in case our buffers get invalidated
193 * while we're getting new buffers, we don't clobber the stamp and
194 * thus ignore the invalidate. */
195 drawable->lastStamp = drawable->dri2.stamp;
196
197 if (unlikely(INTEL_DEBUG & DEBUG_DRI))
198 fprintf(stderr, "enter %s, drawable %p\n", __func__, drawable);
199
200 intel_query_dri2_buffers(intel, drawable, &buffers, &count);
201
202 if (buffers == NULL)
203 return;
204
205 for (i = 0; i < count; i++) {
206 switch (buffers[i].attachment) {
207 case __DRI_BUFFER_FRONT_LEFT:
208 rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
209 region_name = "dri2 front buffer";
210 break;
211
212 case __DRI_BUFFER_FAKE_FRONT_LEFT:
213 rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
214 region_name = "dri2 fake front buffer";
215 break;
216
217 case __DRI_BUFFER_BACK_LEFT:
218 rb = intel_get_renderbuffer(fb, BUFFER_BACK_LEFT);
219 region_name = "dri2 back buffer";
220 break;
221
222 case __DRI_BUFFER_DEPTH:
223 case __DRI_BUFFER_HIZ:
224 case __DRI_BUFFER_DEPTH_STENCIL:
225 case __DRI_BUFFER_STENCIL:
226 case __DRI_BUFFER_ACCUM:
227 default:
228 fprintf(stderr,
229 "unhandled buffer attach event, attachment type %d\n",
230 buffers[i].attachment);
231 return;
232 }
233
234 intel_process_dri2_buffer(intel, drawable, &buffers[i], rb, region_name);
235 }
236
237 driUpdateFramebufferSize(&intel->ctx, drawable);
238 }
239
240 /**
241 * intel_prepare_render should be called anywhere that curent read/drawbuffer
242 * state is required.
243 */
244 void
245 intel_prepare_render(struct intel_context *intel)
246 {
247 __DRIcontext *driContext = intel->driContext;
248 __DRIdrawable *drawable;
249
250 drawable = driContext->driDrawablePriv;
251 if (drawable && drawable->dri2.stamp != driContext->dri2.draw_stamp) {
252 if (drawable->lastStamp != drawable->dri2.stamp)
253 intel_update_renderbuffers(driContext, drawable);
254 intel_draw_buffer(&intel->ctx);
255 driContext->dri2.draw_stamp = drawable->dri2.stamp;
256 }
257
258 drawable = driContext->driReadablePriv;
259 if (drawable && drawable->dri2.stamp != driContext->dri2.read_stamp) {
260 if (drawable->lastStamp != drawable->dri2.stamp)
261 intel_update_renderbuffers(driContext, drawable);
262 driContext->dri2.read_stamp = drawable->dri2.stamp;
263 }
264
265 /* If we're currently rendering to the front buffer, the rendering
266 * that will happen next will probably dirty the front buffer. So
267 * mark it as dirty here.
268 */
269 if (intel->is_front_buffer_rendering)
270 intel->front_buffer_dirty = true;
271
272 /* Wait for the swapbuffers before the one we just emitted, so we
273 * don't get too many swaps outstanding for apps that are GPU-heavy
274 * but not CPU-heavy.
275 *
276 * We're using intelDRI2Flush (called from the loader before
277 * swapbuffer) and glFlush (for front buffer rendering) as the
278 * indicator that a frame is done and then throttle when we get
279 * here as we prepare to render the next frame. At this point for
280 * round trips for swap/copy and getting new buffers are done and
281 * we'll spend less time waiting on the GPU.
282 *
283 * Unfortunately, we don't have a handle to the batch containing
284 * the swap, and getting our hands on that doesn't seem worth it,
285 * so we just us the first batch we emitted after the last swap.
286 */
287 if (intel->need_throttle && intel->first_post_swapbuffers_batch) {
288 if (!intel->disable_throttling)
289 drm_intel_bo_wait_rendering(intel->first_post_swapbuffers_batch);
290 drm_intel_bo_unreference(intel->first_post_swapbuffers_batch);
291 intel->first_post_swapbuffers_batch = NULL;
292 intel->need_throttle = false;
293 }
294 }
295
296 static void
297 intel_viewport(struct gl_context *ctx, GLint x, GLint y, GLsizei w, GLsizei h)
298 {
299 struct intel_context *intel = intel_context(ctx);
300 __DRIcontext *driContext = intel->driContext;
301
302 if (intel->saved_viewport)
303 intel->saved_viewport(ctx, x, y, w, h);
304
305 if (_mesa_is_winsys_fbo(ctx->DrawBuffer)) {
306 dri2InvalidateDrawable(driContext->driDrawablePriv);
307 dri2InvalidateDrawable(driContext->driReadablePriv);
308 }
309 }
310
311 static const struct dri_debug_control debug_control[] = {
312 { "tex", DEBUG_TEXTURE},
313 { "state", DEBUG_STATE},
314 { "ioctl", DEBUG_IOCTL},
315 { "blit", DEBUG_BLIT},
316 { "mip", DEBUG_MIPTREE},
317 { "fall", DEBUG_PERF},
318 { "perf", DEBUG_PERF},
319 { "bat", DEBUG_BATCH},
320 { "pix", DEBUG_PIXEL},
321 { "buf", DEBUG_BUFMGR},
322 { "reg", DEBUG_REGION},
323 { "fbo", DEBUG_FBO},
324 { "fs", DEBUG_WM },
325 { "gs", DEBUG_GS},
326 { "sync", DEBUG_SYNC},
327 { "prim", DEBUG_PRIMS },
328 { "vert", DEBUG_VERTS },
329 { "dri", DEBUG_DRI },
330 { "sf", DEBUG_SF },
331 { "stats", DEBUG_STATS },
332 { "wm", DEBUG_WM },
333 { "urb", DEBUG_URB },
334 { "vs", DEBUG_VS },
335 { "clip", DEBUG_CLIP },
336 { "aub", DEBUG_AUB },
337 { "shader_time", DEBUG_SHADER_TIME },
338 { "no16", DEBUG_NO16 },
339 { "blorp", DEBUG_BLORP },
340 { NULL, 0 }
341 };
342
343
344 static void
345 intelInvalidateState(struct gl_context * ctx, GLuint new_state)
346 {
347 struct intel_context *intel = intel_context(ctx);
348
349 if (ctx->swrast_context)
350 _swrast_InvalidateState(ctx, new_state);
351 _vbo_InvalidateState(ctx, new_state);
352
353 intel->NewGLState |= new_state;
354
355 if (intel->vtbl.invalidate_state)
356 intel->vtbl.invalidate_state( intel, new_state );
357 }
358
359 void
360 intel_flush_rendering_to_batch(struct gl_context *ctx)
361 {
362 struct intel_context *intel = intel_context(ctx);
363
364 if (intel->Fallback)
365 _swrast_flush(ctx);
366
367 if (intel->gen < 4)
368 INTEL_FIREVERTICES(intel);
369 }
370
371 void
372 _intel_flush(struct gl_context *ctx, const char *file, int line)
373 {
374 struct intel_context *intel = intel_context(ctx);
375
376 intel_flush_rendering_to_batch(ctx);
377
378 if (intel->batch.used)
379 _intel_batchbuffer_flush(intel, file, line);
380 }
381
382 static void
383 intel_glFlush(struct gl_context *ctx)
384 {
385 struct intel_context *intel = intel_context(ctx);
386
387 intel_flush(ctx);
388 intel_flush_front(ctx);
389 if (intel->is_front_buffer_rendering)
390 intel->need_throttle = true;
391 }
392
393 void
394 intelFinish(struct gl_context * ctx)
395 {
396 struct intel_context *intel = intel_context(ctx);
397
398 intel_flush(ctx);
399 intel_flush_front(ctx);
400
401 if (intel->batch.last_bo)
402 drm_intel_bo_wait_rendering(intel->batch.last_bo);
403 }
404
405 void
406 intelInitDriverFunctions(struct dd_function_table *functions)
407 {
408 _mesa_init_driver_functions(functions);
409
410 functions->Flush = intel_glFlush;
411 functions->Finish = intelFinish;
412 functions->GetString = intelGetString;
413 functions->UpdateState = intelInvalidateState;
414
415 intelInitTextureFuncs(functions);
416 intelInitTextureImageFuncs(functions);
417 intelInitTextureSubImageFuncs(functions);
418 intelInitTextureCopyImageFuncs(functions);
419 intelInitClearFuncs(functions);
420 intelInitBufferFuncs(functions);
421 intelInitPixelFuncs(functions);
422 intelInitBufferObjectFuncs(functions);
423 intel_init_syncobj_functions(functions);
424 }
425
426 static bool
427 validate_context_version(struct intel_screen *screen,
428 int mesa_api,
429 unsigned major_version,
430 unsigned minor_version,
431 unsigned *dri_ctx_error)
432 {
433 unsigned req_version = 10 * major_version + minor_version;
434 unsigned max_version = 0;
435
436 switch (mesa_api) {
437 case API_OPENGL_COMPAT:
438 max_version = screen->max_gl_compat_version;
439 break;
440 case API_OPENGL_CORE:
441 max_version = screen->max_gl_core_version;
442 break;
443 case API_OPENGLES:
444 max_version = screen->max_gl_es1_version;
445 break;
446 case API_OPENGLES2:
447 max_version = screen->max_gl_es2_version;
448 break;
449 default:
450 max_version = 0;
451 break;
452 }
453
454 if (max_version == 0) {
455 *dri_ctx_error = __DRI_CTX_ERROR_BAD_API;
456 return false;
457 } else if (req_version > max_version) {
458 *dri_ctx_error = __DRI_CTX_ERROR_BAD_VERSION;
459 return false;
460 }
461
462 return true;
463 }
464
465 bool
466 intelInitContext(struct intel_context *intel,
467 int api,
468 unsigned major_version,
469 unsigned minor_version,
470 const struct gl_config * mesaVis,
471 __DRIcontext * driContextPriv,
472 void *sharedContextPrivate,
473 struct dd_function_table *functions,
474 unsigned *dri_ctx_error)
475 {
476 struct gl_context *ctx = &intel->ctx;
477 struct gl_context *shareCtx = (struct gl_context *) sharedContextPrivate;
478 __DRIscreen *sPriv = driContextPriv->driScreenPriv;
479 struct intel_screen *intelScreen = sPriv->driverPrivate;
480 int bo_reuse_mode;
481 struct gl_config visual;
482
483 /* we can't do anything without a connection to the device */
484 if (intelScreen->bufmgr == NULL) {
485 *dri_ctx_error = __DRI_CTX_ERROR_NO_MEMORY;
486 return false;
487 }
488
489 if (!validate_context_version(intelScreen,
490 api, major_version, minor_version,
491 dri_ctx_error))
492 return false;
493
494 /* Can't rely on invalidate events, fall back to glViewport hack */
495 if (!driContextPriv->driScreenPriv->dri2.useInvalidate) {
496 intel->saved_viewport = functions->Viewport;
497 functions->Viewport = intel_viewport;
498 }
499
500 if (mesaVis == NULL) {
501 memset(&visual, 0, sizeof visual);
502 mesaVis = &visual;
503 }
504
505 intel->intelScreen = intelScreen;
506
507 if (!_mesa_initialize_context(&intel->ctx, api, mesaVis, shareCtx,
508 functions)) {
509 *dri_ctx_error = __DRI_CTX_ERROR_NO_MEMORY;
510 printf("%s: failed to init mesa context\n", __FUNCTION__);
511 return false;
512 }
513
514 driContextPriv->driverPrivate = intel;
515 intel->driContext = driContextPriv;
516 intel->driFd = sPriv->fd;
517
518 intel->gen = intelScreen->gen;
519
520 const int devID = intelScreen->deviceID;
521 if (IS_SNB_GT1(devID) || IS_IVB_GT1(devID) || IS_HSW_GT1(devID))
522 intel->gt = 1;
523 else if (IS_SNB_GT2(devID) || IS_IVB_GT2(devID) || IS_HSW_GT2(devID))
524 intel->gt = 2;
525 else if (IS_HSW_GT3(devID))
526 intel->gt = 3;
527 else
528 intel->gt = 0;
529
530 if (IS_HASWELL(devID)) {
531 intel->is_haswell = true;
532 } else if (IS_BAYTRAIL(devID)) {
533 intel->is_baytrail = true;
534 intel->gt = 1;
535 } else if (IS_G4X(devID)) {
536 intel->is_g4x = true;
537 } else if (IS_945(devID)) {
538 intel->is_945 = true;
539 }
540
541 if (intel->gen >= 5) {
542 intel->needs_ff_sync = true;
543 }
544
545 intel->has_separate_stencil = intel->intelScreen->hw_has_separate_stencil;
546 intel->must_use_separate_stencil = intel->intelScreen->hw_must_use_separate_stencil;
547 intel->has_llc = intel->intelScreen->hw_has_llc;
548 intel->has_swizzling = intel->intelScreen->hw_has_swizzling;
549
550 memset(&ctx->TextureFormatSupported,
551 0, sizeof(ctx->TextureFormatSupported));
552
553 driParseConfigFiles(&intel->optionCache, &intelScreen->optionCache,
554 sPriv->myNum, (intel->gen >= 4) ? "i965" : "i915");
555 if (intel->gen < 4)
556 intel->maxBatchSize = 4096;
557 else
558 intel->maxBatchSize = BATCH_SZ;
559
560 /* Estimate the size of the mappable aperture into the GTT. There's an
561 * ioctl to get the whole GTT size, but not one to get the mappable subset.
562 * It turns out it's basically always 256MB, though some ancient hardware
563 * was smaller.
564 */
565 uint32_t gtt_size = 256 * 1024 * 1024;
566 if (intel->gen == 2)
567 gtt_size = 128 * 1024 * 1024;
568
569 /* We don't want to map two objects such that a memcpy between them would
570 * just fault one mapping in and then the other over and over forever. So
571 * we would need to divide the GTT size by 2. Additionally, some GTT is
572 * taken up by things like the framebuffer and the ringbuffer and such, so
573 * be more conservative.
574 */
575 intel->max_gtt_map_object_size = gtt_size / 4;
576
577 intel->bufmgr = intelScreen->bufmgr;
578
579 bo_reuse_mode = driQueryOptioni(&intel->optionCache, "bo_reuse");
580 switch (bo_reuse_mode) {
581 case DRI_CONF_BO_REUSE_DISABLED:
582 break;
583 case DRI_CONF_BO_REUSE_ALL:
584 intel_bufmgr_gem_enable_reuse(intel->bufmgr);
585 break;
586 }
587
588 ctx->Const.MinLineWidth = 1.0;
589 ctx->Const.MinLineWidthAA = 1.0;
590 ctx->Const.MaxLineWidth = 5.0;
591 ctx->Const.MaxLineWidthAA = 5.0;
592 ctx->Const.LineWidthGranularity = 0.5;
593
594 ctx->Const.MinPointSize = 1.0;
595 ctx->Const.MinPointSizeAA = 1.0;
596 ctx->Const.MaxPointSize = 255.0;
597 ctx->Const.MaxPointSizeAA = 3.0;
598 ctx->Const.PointSizeGranularity = 1.0;
599
600 if (intel->gen >= 6)
601 ctx->Const.MaxClipPlanes = 8;
602
603 ctx->Const.StripTextureBorder = GL_TRUE;
604
605 /* reinitialize the context point state.
606 * It depend on constants in __struct gl_contextRec::Const
607 */
608 _mesa_init_point(ctx);
609
610 if (intel->gen >= 4) {
611 ctx->Const.MaxRenderbufferSize = 8192;
612 } else {
613 ctx->Const.MaxRenderbufferSize = 2048;
614 }
615
616 /* Initialize the software rasterizer and helper modules.
617 *
618 * As of GL 3.1 core, the gen4+ driver doesn't need the swrast context for
619 * software fallbacks (which we have to support on legacy GL to do weird
620 * glDrawPixels(), glBitmap(), and other functions).
621 */
622 if (intel->gen <= 3 || api != API_OPENGL_CORE) {
623 _swrast_CreateContext(ctx);
624 }
625
626 _vbo_CreateContext(ctx);
627 if (ctx->swrast_context) {
628 _tnl_CreateContext(ctx);
629 _swsetup_CreateContext(ctx);
630
631 /* Configure swrast to match hardware characteristics: */
632 _swrast_allow_pixel_fog(ctx, false);
633 _swrast_allow_vertex_fog(ctx, true);
634 }
635
636 _mesa_meta_init(ctx);
637
638 intel->hw_stencil = mesaVis->stencilBits && mesaVis->depthBits == 24;
639 intel->hw_stipple = 1;
640
641 intel->RenderIndex = ~0;
642
643 intelInitExtensions(ctx);
644
645 INTEL_DEBUG = driParseDebugString(getenv("INTEL_DEBUG"), debug_control);
646 if (INTEL_DEBUG & DEBUG_BUFMGR)
647 dri_bufmgr_set_debug(intel->bufmgr, true);
648 if ((INTEL_DEBUG & DEBUG_SHADER_TIME) && intel->gen < 7) {
649 fprintf(stderr,
650 "shader_time debugging requires gen7 (Ivybridge) or better.\n");
651 INTEL_DEBUG &= ~DEBUG_SHADER_TIME;
652 }
653 if (INTEL_DEBUG & DEBUG_PERF)
654 intel->perf_debug = true;
655
656 if (INTEL_DEBUG & DEBUG_AUB)
657 drm_intel_bufmgr_gem_set_aub_dump(intel->bufmgr, true);
658
659 intel_batchbuffer_init(intel);
660
661 intel_fbo_init(intel);
662
663 intel->use_early_z = driQueryOptionb(&intel->optionCache, "early_z");
664
665 intel->prim.primitive = ~0;
666
667 /* Force all software fallbacks */
668 #ifdef I915
669 if (driQueryOptionb(&intel->optionCache, "no_rast")) {
670 fprintf(stderr, "disabling 3D rasterization\n");
671 intel->no_rast = 1;
672 }
673 #endif
674
675 if (driQueryOptionb(&intel->optionCache, "always_flush_batch")) {
676 fprintf(stderr, "flushing batchbuffer before/after each draw call\n");
677 intel->always_flush_batch = 1;
678 }
679
680 if (driQueryOptionb(&intel->optionCache, "always_flush_cache")) {
681 fprintf(stderr, "flushing GPU caches before/after each draw call\n");
682 intel->always_flush_cache = 1;
683 }
684
685 if (driQueryOptionb(&intel->optionCache, "disable_throttling")) {
686 fprintf(stderr, "disabling flush throttling\n");
687 intel->disable_throttling = 1;
688 }
689
690 return true;
691 }
692
693 void
694 intelDestroyContext(__DRIcontext * driContextPriv)
695 {
696 struct intel_context *intel =
697 (struct intel_context *) driContextPriv->driverPrivate;
698 struct gl_context *ctx = &intel->ctx;
699
700 assert(intel); /* should never be null */
701 if (intel) {
702 INTEL_FIREVERTICES(intel);
703
704 /* Dump a final BMP in case the application doesn't call SwapBuffers */
705 if (INTEL_DEBUG & DEBUG_AUB) {
706 intel_batchbuffer_flush(intel);
707 aub_dump_bmp(&intel->ctx);
708 }
709
710 _mesa_meta_free(&intel->ctx);
711
712 intel->vtbl.destroy(intel);
713
714 if (ctx->swrast_context) {
715 _swsetup_DestroyContext(&intel->ctx);
716 _tnl_DestroyContext(&intel->ctx);
717 }
718 _vbo_DestroyContext(&intel->ctx);
719
720 if (ctx->swrast_context)
721 _swrast_DestroyContext(&intel->ctx);
722 intel->Fallback = 0x0; /* don't call _swrast_Flush later */
723
724 intel_batchbuffer_free(intel);
725
726 free(intel->prim.vb);
727 intel->prim.vb = NULL;
728 drm_intel_bo_unreference(intel->prim.vb_bo);
729 intel->prim.vb_bo = NULL;
730 drm_intel_bo_unreference(intel->first_post_swapbuffers_batch);
731 intel->first_post_swapbuffers_batch = NULL;
732
733 driDestroyOptionCache(&intel->optionCache);
734
735 /* free the Mesa context */
736 _mesa_free_context_data(&intel->ctx);
737
738 _math_matrix_dtr(&intel->ViewportMatrix);
739
740 ralloc_free(intel);
741 driContextPriv->driverPrivate = NULL;
742 }
743 }
744
745 GLboolean
746 intelUnbindContext(__DRIcontext * driContextPriv)
747 {
748 /* Unset current context and dispath table */
749 _mesa_make_current(NULL, NULL, NULL);
750
751 return true;
752 }
753
754 /**
755 * Fixes up the context for GLES23 with our default-to-sRGB-capable behavior
756 * on window system framebuffers.
757 *
758 * Desktop GL is fairly reasonable in its handling of sRGB: You can ask if
759 * your renderbuffer can do sRGB encode, and you can flip a switch that does
760 * sRGB encode if the renderbuffer can handle it. You can ask specifically
761 * for a visual where you're guaranteed to be capable, but it turns out that
762 * everyone just makes all their ARGB8888 visuals capable and doesn't offer
763 * incapable ones, becuase there's no difference between the two in resources
764 * used. Applications thus get built that accidentally rely on the default
765 * visual choice being sRGB, so we make ours sRGB capable. Everything sounds
766 * great...
767 *
768 * But for GLES2/3, they decided that it was silly to not turn on sRGB encode
769 * for sRGB renderbuffers you made with the GL_EXT_texture_sRGB equivalent.
770 * So they removed the enable knob and made it "if the renderbuffer is sRGB
771 * capable, do sRGB encode". Then, for your window system renderbuffers, you
772 * can ask for sRGB visuals and get sRGB encode, or not ask for sRGB visuals
773 * and get no sRGB encode (assuming that both kinds of visual are available).
774 * Thus our choice to support sRGB by default on our visuals for desktop would
775 * result in broken rendering of GLES apps that aren't expecting sRGB encode.
776 *
777 * Unfortunately, renderbuffer setup happens before a context is created. So
778 * in intel_screen.c we always set up sRGB, and here, if you're a GLES2/3
779 * context (without an sRGB visual, though we don't have sRGB visuals exposed
780 * yet), we go turn that back off before anyone finds out.
781 */
782 static void
783 intel_gles3_srgb_workaround(struct intel_context *intel,
784 struct gl_framebuffer *fb)
785 {
786 struct gl_context *ctx = &intel->ctx;
787
788 if (_mesa_is_desktop_gl(ctx) || !fb->Visual.sRGBCapable)
789 return;
790
791 /* Some day when we support the sRGB capable bit on visuals available for
792 * GLES, we'll need to respect that and not disable things here.
793 */
794 fb->Visual.sRGBCapable = false;
795 for (int i = 0; i < BUFFER_COUNT; i++) {
796 if (fb->Attachment[i].Renderbuffer &&
797 fb->Attachment[i].Renderbuffer->Format == MESA_FORMAT_SARGB8) {
798 fb->Attachment[i].Renderbuffer->Format = MESA_FORMAT_ARGB8888;
799 }
800 }
801 }
802
803 GLboolean
804 intelMakeCurrent(__DRIcontext * driContextPriv,
805 __DRIdrawable * driDrawPriv,
806 __DRIdrawable * driReadPriv)
807 {
808 struct intel_context *intel;
809 GET_CURRENT_CONTEXT(curCtx);
810
811 if (driContextPriv)
812 intel = (struct intel_context *) driContextPriv->driverPrivate;
813 else
814 intel = NULL;
815
816 /* According to the glXMakeCurrent() man page: "Pending commands to
817 * the previous context, if any, are flushed before it is released."
818 * But only flush if we're actually changing contexts.
819 */
820 if (intel_context(curCtx) && intel_context(curCtx) != intel) {
821 _mesa_flush(curCtx);
822 }
823
824 if (driContextPriv) {
825 struct gl_context *ctx = &intel->ctx;
826 struct gl_framebuffer *fb, *readFb;
827
828 if (driDrawPriv == NULL && driReadPriv == NULL) {
829 fb = _mesa_get_incomplete_framebuffer();
830 readFb = _mesa_get_incomplete_framebuffer();
831 } else {
832 fb = driDrawPriv->driverPrivate;
833 readFb = driReadPriv->driverPrivate;
834 driContextPriv->dri2.draw_stamp = driDrawPriv->dri2.stamp - 1;
835 driContextPriv->dri2.read_stamp = driReadPriv->dri2.stamp - 1;
836 }
837
838 intel_prepare_render(intel);
839 _mesa_make_current(ctx, fb, readFb);
840
841 intel_gles3_srgb_workaround(intel, ctx->WinSysDrawBuffer);
842 intel_gles3_srgb_workaround(intel, ctx->WinSysReadBuffer);
843
844 /* We do this in intel_prepare_render() too, but intel->ctx.DrawBuffer
845 * is NULL at that point. We can't call _mesa_makecurrent()
846 * first, since we need the buffer size for the initial
847 * viewport. So just call intel_draw_buffer() again here. */
848 intel_draw_buffer(ctx);
849 }
850 else {
851 _mesa_make_current(NULL, NULL, NULL);
852 }
853
854 return true;
855 }
856
857 /**
858 * \brief Query DRI2 to obtain a DRIdrawable's buffers.
859 *
860 * To determine which DRI buffers to request, examine the renderbuffers
861 * attached to the drawable's framebuffer. Then request the buffers with
862 * DRI2GetBuffers() or DRI2GetBuffersWithFormat().
863 *
864 * This is called from intel_update_renderbuffers().
865 *
866 * \param drawable Drawable whose buffers are queried.
867 * \param buffers [out] List of buffers returned by DRI2 query.
868 * \param buffer_count [out] Number of buffers returned.
869 *
870 * \see intel_update_renderbuffers()
871 * \see DRI2GetBuffers()
872 * \see DRI2GetBuffersWithFormat()
873 */
874 static void
875 intel_query_dri2_buffers(struct intel_context *intel,
876 __DRIdrawable *drawable,
877 __DRIbuffer **buffers,
878 int *buffer_count)
879 {
880 __DRIscreen *screen = intel->intelScreen->driScrnPriv;
881 struct gl_framebuffer *fb = drawable->driverPrivate;
882 int i = 0;
883 unsigned attachments[8];
884
885 struct intel_renderbuffer *front_rb;
886 struct intel_renderbuffer *back_rb;
887
888 front_rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
889 back_rb = intel_get_renderbuffer(fb, BUFFER_BACK_LEFT);
890
891 memset(attachments, 0, sizeof(attachments));
892 if ((intel->is_front_buffer_rendering ||
893 intel->is_front_buffer_reading ||
894 !back_rb) && front_rb) {
895 /* If a fake front buffer is in use, then querying for
896 * __DRI_BUFFER_FRONT_LEFT will cause the server to copy the image from
897 * the real front buffer to the fake front buffer. So before doing the
898 * query, we need to make sure all the pending drawing has landed in the
899 * real front buffer.
900 */
901 intel_flush(&intel->ctx);
902 intel_flush_front(&intel->ctx);
903
904 attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
905 attachments[i++] = intel_bits_per_pixel(front_rb);
906 } else if (front_rb && intel->front_buffer_dirty) {
907 /* We have pending front buffer rendering, but we aren't querying for a
908 * front buffer. If the front buffer we have is a fake front buffer,
909 * the X server is going to throw it away when it processes the query.
910 * So before doing the query, make sure all the pending drawing has
911 * landed in the real front buffer.
912 */
913 intel_flush(&intel->ctx);
914 intel_flush_front(&intel->ctx);
915 }
916
917 if (back_rb) {
918 attachments[i++] = __DRI_BUFFER_BACK_LEFT;
919 attachments[i++] = intel_bits_per_pixel(back_rb);
920 }
921
922 assert(i <= ARRAY_SIZE(attachments));
923
924 *buffers = screen->dri2.loader->getBuffersWithFormat(drawable,
925 &drawable->w,
926 &drawable->h,
927 attachments, i / 2,
928 buffer_count,
929 drawable->loaderPrivate);
930 }
931
932 /**
933 * \brief Assign a DRI buffer's DRM region to a renderbuffer.
934 *
935 * This is called from intel_update_renderbuffers().
936 *
937 * \par Note:
938 * DRI buffers whose attachment point is DRI2BufferStencil or
939 * DRI2BufferDepthStencil are handled as special cases.
940 *
941 * \param buffer_name is a human readable name, such as "dri2 front buffer",
942 * that is passed to intel_region_alloc_for_handle().
943 *
944 * \see intel_update_renderbuffers()
945 * \see intel_region_alloc_for_handle()
946 */
947 static void
948 intel_process_dri2_buffer(struct intel_context *intel,
949 __DRIdrawable *drawable,
950 __DRIbuffer *buffer,
951 struct intel_renderbuffer *rb,
952 const char *buffer_name)
953 {
954 struct intel_region *region = NULL;
955
956 if (!rb)
957 return;
958
959 unsigned num_samples = rb->Base.Base.NumSamples;
960
961 /* We try to avoid closing and reopening the same BO name, because the first
962 * use of a mapping of the buffer involves a bunch of page faulting which is
963 * moderately expensive.
964 */
965 if (num_samples == 0) {
966 if (rb->mt &&
967 rb->mt->region &&
968 rb->mt->region->name == buffer->name)
969 return;
970 } else {
971 if (rb->mt &&
972 rb->mt->singlesample_mt &&
973 rb->mt->singlesample_mt->region &&
974 rb->mt->singlesample_mt->region->name == buffer->name)
975 return;
976 }
977
978 if (unlikely(INTEL_DEBUG & DEBUG_DRI)) {
979 fprintf(stderr,
980 "attaching buffer %d, at %d, cpp %d, pitch %d\n",
981 buffer->name, buffer->attachment,
982 buffer->cpp, buffer->pitch);
983 }
984
985 intel_miptree_release(&rb->mt);
986 region = intel_region_alloc_for_handle(intel->intelScreen,
987 buffer->cpp,
988 drawable->w,
989 drawable->h,
990 buffer->pitch,
991 buffer->name,
992 buffer_name);
993 if (!region)
994 return;
995
996 rb->mt = intel_miptree_create_for_dri2_buffer(intel,
997 buffer->attachment,
998 intel_rb_format(rb),
999 num_samples,
1000 region);
1001 intel_region_release(&region);
1002 }