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