1 /**************************************************************************
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
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"
38 #include "swrast/swrast.h"
39 #include "swrast_setup/swrast_setup.h"
41 #include "drivers/common/driverfuncs.h"
42 #include "drivers/common/meta.h"
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"
59 #include "../glsl/ralloc.h"
62 int INTEL_DEBUG
= (0);
66 static const GLubyte
*
67 intelGetString(struct gl_context
* ctx
, GLenum name
)
69 const struct intel_context
*const intel
= intel_context(ctx
);
71 static char buffer
[128];
75 return (GLubyte
*) "Intel Open Source Technology Center";
79 switch (intel
->intelScreen
->deviceID
) {
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"
85 chipset
= "Unknown Intel Chipset";
89 (void) driGetRendererString(buffer
, chipset
, 0);
90 return (GLubyte
*) buffer
;
98 intel_resolve_for_dri2_flush(struct intel_context
*intel
,
99 __DRIdrawable
*drawable
)
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.
108 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
109 struct intel_renderbuffer
*rb
;
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.
114 static const gl_buffer_index buffers
[2] = {
119 for (int i
= 0; i
< 2; ++i
) {
120 rb
= intel_get_renderbuffer(fb
, buffers
[i
]);
121 if (rb
== NULL
|| rb
->mt
== NULL
)
123 if (rb
->mt
->num_samples
<= 1)
124 intel_miptree_resolve_color(intel
, rb
->mt
);
126 intel_miptree_downsample(intel
, rb
->mt
);
131 intel_flush_front(struct gl_context
*ctx
)
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
;
138 if (intel
->front_buffer_dirty
&& _mesa_is_winsys_fbo(ctx
->DrawBuffer
)) {
139 if (screen
->dri2
.loader
->flushFrontBuffer
!= NULL
&&
141 driDrawable
->loaderPrivate
) {
143 /* Resolve before flushing FAKE_FRONT_LEFT to FRONT_LEFT.
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
150 intel_resolve_for_dri2_flush(intel
, driDrawable
);
152 screen
->dri2
.loader
->flushFrontBuffer(driDrawable
,
153 driDrawable
->loaderPrivate
);
155 /* We set the dirty bit in intel_prepare_render() if we're
156 * front buffer rendering once we get there.
158 intel
->front_buffer_dirty
= false;
164 intel_bits_per_pixel(const struct intel_renderbuffer
*rb
)
166 return _mesa_get_format_bytes(intel_rb_format(rb
)) * 8;
170 intel_query_dri2_buffers(struct intel_context
*intel
,
171 __DRIdrawable
*drawable
,
172 __DRIbuffer
**buffers
,
176 intel_process_dri2_buffer(struct intel_context
*intel
,
177 __DRIdrawable
*drawable
,
179 struct intel_renderbuffer
*rb
,
180 const char *buffer_name
);
183 intel_update_renderbuffers(__DRIcontext
*context
, __DRIdrawable
*drawable
)
185 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
186 struct intel_renderbuffer
*rb
;
187 struct intel_context
*intel
= context
->driverPrivate
;
188 __DRIbuffer
*buffers
= NULL
;
190 const char *region_name
;
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
;
197 if (unlikely(INTEL_DEBUG
& DEBUG_DRI
))
198 fprintf(stderr
, "enter %s, drawable %p\n", __func__
, drawable
);
200 intel_query_dri2_buffers(intel
, drawable
, &buffers
, &count
);
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";
212 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
213 rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
214 region_name
= "dri2 fake front buffer";
217 case __DRI_BUFFER_BACK_LEFT
:
218 rb
= intel_get_renderbuffer(fb
, BUFFER_BACK_LEFT
);
219 region_name
= "dri2 back buffer";
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
:
229 "unhandled buffer attach event, attachment type %d\n",
230 buffers
[i
].attachment
);
234 intel_process_dri2_buffer(intel
, drawable
, &buffers
[i
], rb
, region_name
);
237 driUpdateFramebufferSize(&intel
->ctx
, drawable
);
241 * intel_prepare_render should be called anywhere that curent read/drawbuffer
245 intel_prepare_render(struct intel_context
*intel
)
247 __DRIcontext
*driContext
= intel
->driContext
;
248 __DRIdrawable
*drawable
;
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
;
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
;
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.
269 if (intel
->is_front_buffer_rendering
)
270 intel
->front_buffer_dirty
= true;
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
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.
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.
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;
297 intel_viewport(struct gl_context
*ctx
, GLint x
, GLint y
, GLsizei w
, GLsizei h
)
299 struct intel_context
*intel
= intel_context(ctx
);
300 __DRIcontext
*driContext
= intel
->driContext
;
302 if (intel
->saved_viewport
)
303 intel
->saved_viewport(ctx
, x
, y
, w
, h
);
305 if (_mesa_is_winsys_fbo(ctx
->DrawBuffer
)) {
306 dri2InvalidateDrawable(driContext
->driDrawablePriv
);
307 dri2InvalidateDrawable(driContext
->driReadablePriv
);
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
},
326 { "sync", DEBUG_SYNC
},
327 { "prim", DEBUG_PRIMS
},
328 { "vert", DEBUG_VERTS
},
329 { "dri", DEBUG_DRI
},
331 { "stats", DEBUG_STATS
},
333 { "urb", DEBUG_URB
},
335 { "clip", DEBUG_CLIP
},
336 { "aub", DEBUG_AUB
},
337 { "shader_time", DEBUG_SHADER_TIME
},
338 { "no16", DEBUG_NO16
},
339 { "blorp", DEBUG_BLORP
},
345 intelInvalidateState(struct gl_context
* ctx
, GLuint new_state
)
347 struct intel_context
*intel
= intel_context(ctx
);
349 if (ctx
->swrast_context
)
350 _swrast_InvalidateState(ctx
, new_state
);
351 _vbo_InvalidateState(ctx
, new_state
);
353 intel
->NewGLState
|= new_state
;
355 if (intel
->vtbl
.invalidate_state
)
356 intel
->vtbl
.invalidate_state( intel
, new_state
);
360 intel_flush_rendering_to_batch(struct gl_context
*ctx
)
362 struct intel_context
*intel
= intel_context(ctx
);
369 _intel_flush(struct gl_context
*ctx
, const char *file
, int line
)
371 struct intel_context
*intel
= intel_context(ctx
);
373 intel_flush_rendering_to_batch(ctx
);
375 if (intel
->batch
.used
)
376 _intel_batchbuffer_flush(intel
, file
, line
);
380 intel_glFlush(struct gl_context
*ctx
)
382 struct intel_context
*intel
= intel_context(ctx
);
385 intel_flush_front(ctx
);
386 if (intel
->is_front_buffer_rendering
)
387 intel
->need_throttle
= true;
391 intelFinish(struct gl_context
* ctx
)
393 struct intel_context
*intel
= intel_context(ctx
);
396 intel_flush_front(ctx
);
398 if (intel
->batch
.last_bo
)
399 drm_intel_bo_wait_rendering(intel
->batch
.last_bo
);
403 intelInitDriverFunctions(struct dd_function_table
*functions
)
405 _mesa_init_driver_functions(functions
);
407 functions
->Flush
= intel_glFlush
;
408 functions
->Finish
= intelFinish
;
409 functions
->GetString
= intelGetString
;
410 functions
->UpdateState
= intelInvalidateState
;
412 intelInitTextureFuncs(functions
);
413 intelInitTextureImageFuncs(functions
);
414 intelInitTextureSubImageFuncs(functions
);
415 intelInitTextureCopyImageFuncs(functions
);
416 intelInitClearFuncs(functions
);
417 intelInitBufferFuncs(functions
);
418 intelInitPixelFuncs(functions
);
419 intelInitBufferObjectFuncs(functions
);
420 intel_init_syncobj_functions(functions
);
424 validate_context_version(struct intel_screen
*screen
,
426 unsigned major_version
,
427 unsigned minor_version
,
428 unsigned *dri_ctx_error
)
430 unsigned req_version
= 10 * major_version
+ minor_version
;
431 unsigned max_version
= 0;
434 case API_OPENGL_COMPAT
:
435 max_version
= screen
->max_gl_compat_version
;
437 case API_OPENGL_CORE
:
438 max_version
= screen
->max_gl_core_version
;
441 max_version
= screen
->max_gl_es1_version
;
444 max_version
= screen
->max_gl_es2_version
;
451 if (max_version
== 0) {
452 *dri_ctx_error
= __DRI_CTX_ERROR_BAD_API
;
454 } else if (req_version
> max_version
) {
455 *dri_ctx_error
= __DRI_CTX_ERROR_BAD_VERSION
;
463 intelInitContext(struct intel_context
*intel
,
465 unsigned major_version
,
466 unsigned minor_version
,
467 const struct gl_config
* mesaVis
,
468 __DRIcontext
* driContextPriv
,
469 void *sharedContextPrivate
,
470 struct dd_function_table
*functions
,
471 unsigned *dri_ctx_error
)
473 struct gl_context
*ctx
= &intel
->ctx
;
474 struct gl_context
*shareCtx
= (struct gl_context
*) sharedContextPrivate
;
475 __DRIscreen
*sPriv
= driContextPriv
->driScreenPriv
;
476 struct intel_screen
*intelScreen
= sPriv
->driverPrivate
;
478 struct gl_config visual
;
480 /* we can't do anything without a connection to the device */
481 if (intelScreen
->bufmgr
== NULL
) {
482 *dri_ctx_error
= __DRI_CTX_ERROR_NO_MEMORY
;
486 if (!validate_context_version(intelScreen
,
487 api
, major_version
, minor_version
,
491 /* Can't rely on invalidate events, fall back to glViewport hack */
492 if (!driContextPriv
->driScreenPriv
->dri2
.useInvalidate
) {
493 intel
->saved_viewport
= functions
->Viewport
;
494 functions
->Viewport
= intel_viewport
;
497 if (mesaVis
== NULL
) {
498 memset(&visual
, 0, sizeof visual
);
502 intel
->intelScreen
= intelScreen
;
504 if (!_mesa_initialize_context(&intel
->ctx
, api
, mesaVis
, shareCtx
,
506 *dri_ctx_error
= __DRI_CTX_ERROR_NO_MEMORY
;
507 printf("%s: failed to init mesa context\n", __FUNCTION__
);
511 driContextPriv
->driverPrivate
= intel
;
512 intel
->driContext
= driContextPriv
;
513 intel
->driFd
= sPriv
->fd
;
515 intel
->gen
= intelScreen
->gen
;
517 const int devID
= intelScreen
->deviceID
;
518 if (IS_SNB_GT1(devID
) || IS_IVB_GT1(devID
) || IS_HSW_GT1(devID
))
520 else if (IS_SNB_GT2(devID
) || IS_IVB_GT2(devID
) || IS_HSW_GT2(devID
))
522 else if (IS_HSW_GT3(devID
))
527 if (IS_HASWELL(devID
)) {
528 intel
->is_haswell
= true;
529 } else if (IS_BAYTRAIL(devID
)) {
530 intel
->is_baytrail
= true;
532 } else if (IS_G4X(devID
)) {
533 intel
->is_g4x
= true;
534 } else if (IS_945(devID
)) {
535 intel
->is_945
= true;
538 if (intel
->gen
>= 5) {
539 intel
->needs_ff_sync
= true;
542 intel
->has_separate_stencil
= intel
->intelScreen
->hw_has_separate_stencil
;
543 intel
->must_use_separate_stencil
= intel
->intelScreen
->hw_must_use_separate_stencil
;
544 intel
->has_hiz
= intel
->gen
>= 6;
545 intel
->has_llc
= intel
->intelScreen
->hw_has_llc
;
546 intel
->has_swizzling
= intel
->intelScreen
->hw_has_swizzling
;
548 memset(&ctx
->TextureFormatSupported
,
549 0, sizeof(ctx
->TextureFormatSupported
));
551 driParseConfigFiles(&intel
->optionCache
, &intelScreen
->optionCache
,
552 sPriv
->myNum
, "i965");
553 intel
->maxBatchSize
= BATCH_SZ
;
555 /* Estimate the size of the mappable aperture into the GTT. There's an
556 * ioctl to get the whole GTT size, but not one to get the mappable subset.
557 * It turns out it's basically always 256MB, though some ancient hardware
560 uint32_t gtt_size
= 256 * 1024 * 1024;
562 /* We don't want to map two objects such that a memcpy between them would
563 * just fault one mapping in and then the other over and over forever. So
564 * we would need to divide the GTT size by 2. Additionally, some GTT is
565 * taken up by things like the framebuffer and the ringbuffer and such, so
566 * be more conservative.
568 intel
->max_gtt_map_object_size
= gtt_size
/ 4;
570 intel
->bufmgr
= intelScreen
->bufmgr
;
572 bo_reuse_mode
= driQueryOptioni(&intel
->optionCache
, "bo_reuse");
573 switch (bo_reuse_mode
) {
574 case DRI_CONF_BO_REUSE_DISABLED
:
576 case DRI_CONF_BO_REUSE_ALL
:
577 intel_bufmgr_gem_enable_reuse(intel
->bufmgr
);
581 ctx
->Const
.MinLineWidth
= 1.0;
582 ctx
->Const
.MinLineWidthAA
= 1.0;
583 ctx
->Const
.MaxLineWidth
= 5.0;
584 ctx
->Const
.MaxLineWidthAA
= 5.0;
585 ctx
->Const
.LineWidthGranularity
= 0.5;
587 ctx
->Const
.MinPointSize
= 1.0;
588 ctx
->Const
.MinPointSizeAA
= 1.0;
589 ctx
->Const
.MaxPointSize
= 255.0;
590 ctx
->Const
.MaxPointSizeAA
= 3.0;
591 ctx
->Const
.PointSizeGranularity
= 1.0;
594 ctx
->Const
.MaxClipPlanes
= 8;
596 ctx
->Const
.StripTextureBorder
= GL_TRUE
;
598 /* reinitialize the context point state.
599 * It depend on constants in __struct gl_contextRec::Const
601 _mesa_init_point(ctx
);
603 ctx
->Const
.MaxRenderbufferSize
= 8192;
605 /* Initialize the software rasterizer and helper modules.
607 * As of GL 3.1 core, the gen4+ driver doesn't need the swrast context for
608 * software fallbacks (which we have to support on legacy GL to do weird
609 * glDrawPixels(), glBitmap(), and other functions).
611 if (api
!= API_OPENGL_CORE
) {
612 _swrast_CreateContext(ctx
);
615 _vbo_CreateContext(ctx
);
616 if (ctx
->swrast_context
) {
617 _tnl_CreateContext(ctx
);
618 _swsetup_CreateContext(ctx
);
620 /* Configure swrast to match hardware characteristics: */
621 _swrast_allow_pixel_fog(ctx
, false);
622 _swrast_allow_vertex_fog(ctx
, true);
625 _mesa_meta_init(ctx
);
627 intel
->hw_stencil
= mesaVis
->stencilBits
&& mesaVis
->depthBits
== 24;
628 intel
->hw_stipple
= 1;
630 intel
->RenderIndex
= ~0;
632 intelInitExtensions(ctx
);
634 INTEL_DEBUG
= driParseDebugString(getenv("INTEL_DEBUG"), debug_control
);
635 if (INTEL_DEBUG
& DEBUG_BUFMGR
)
636 dri_bufmgr_set_debug(intel
->bufmgr
, true);
637 if ((INTEL_DEBUG
& DEBUG_SHADER_TIME
) && intel
->gen
< 7) {
639 "shader_time debugging requires gen7 (Ivybridge) or better.\n");
640 INTEL_DEBUG
&= ~DEBUG_SHADER_TIME
;
642 if (INTEL_DEBUG
& DEBUG_PERF
)
643 intel
->perf_debug
= true;
645 if (INTEL_DEBUG
& DEBUG_AUB
)
646 drm_intel_bufmgr_gem_set_aub_dump(intel
->bufmgr
, true);
648 intel_batchbuffer_init(intel
);
650 intel_fbo_init(intel
);
652 intel
->use_early_z
= driQueryOptionb(&intel
->optionCache
, "early_z");
654 if (!driQueryOptionb(&intel
->optionCache
, "hiz")) {
655 intel
->has_hiz
= false;
656 /* On gen6, you can only do separate stencil with HIZ. */
658 intel
->has_separate_stencil
= false;
661 intel
->prim
.primitive
= ~0;
663 /* Force all software fallbacks */
665 if (driQueryOptionb(&intel
->optionCache
, "no_rast")) {
666 fprintf(stderr
, "disabling 3D rasterization\n");
671 if (driQueryOptionb(&intel
->optionCache
, "always_flush_batch")) {
672 fprintf(stderr
, "flushing batchbuffer before/after each draw call\n");
673 intel
->always_flush_batch
= 1;
676 if (driQueryOptionb(&intel
->optionCache
, "always_flush_cache")) {
677 fprintf(stderr
, "flushing GPU caches before/after each draw call\n");
678 intel
->always_flush_cache
= 1;
681 if (driQueryOptionb(&intel
->optionCache
, "disable_throttling")) {
682 fprintf(stderr
, "disabling flush throttling\n");
683 intel
->disable_throttling
= 1;
690 intelDestroyContext(__DRIcontext
* driContextPriv
)
692 struct intel_context
*intel
=
693 (struct intel_context
*) driContextPriv
->driverPrivate
;
694 struct gl_context
*ctx
= &intel
->ctx
;
696 assert(intel
); /* should never be null */
698 INTEL_FIREVERTICES(intel
);
700 /* Dump a final BMP in case the application doesn't call SwapBuffers */
701 if (INTEL_DEBUG
& DEBUG_AUB
) {
702 intel_batchbuffer_flush(intel
);
703 aub_dump_bmp(&intel
->ctx
);
706 _mesa_meta_free(&intel
->ctx
);
708 intel
->vtbl
.destroy(intel
);
710 if (ctx
->swrast_context
) {
711 _swsetup_DestroyContext(&intel
->ctx
);
712 _tnl_DestroyContext(&intel
->ctx
);
714 _vbo_DestroyContext(&intel
->ctx
);
716 if (ctx
->swrast_context
)
717 _swrast_DestroyContext(&intel
->ctx
);
718 intel
->Fallback
= 0x0; /* don't call _swrast_Flush later */
720 intel_batchbuffer_free(intel
);
722 free(intel
->prim
.vb
);
723 intel
->prim
.vb
= NULL
;
724 drm_intel_bo_unreference(intel
->prim
.vb_bo
);
725 intel
->prim
.vb_bo
= NULL
;
726 drm_intel_bo_unreference(intel
->first_post_swapbuffers_batch
);
727 intel
->first_post_swapbuffers_batch
= NULL
;
729 driDestroyOptionCache(&intel
->optionCache
);
731 /* free the Mesa context */
732 _mesa_free_context_data(&intel
->ctx
);
734 _math_matrix_dtr(&intel
->ViewportMatrix
);
737 driContextPriv
->driverPrivate
= NULL
;
742 intelUnbindContext(__DRIcontext
* driContextPriv
)
744 /* Unset current context and dispath table */
745 _mesa_make_current(NULL
, NULL
, NULL
);
751 * Fixes up the context for GLES23 with our default-to-sRGB-capable behavior
752 * on window system framebuffers.
754 * Desktop GL is fairly reasonable in its handling of sRGB: You can ask if
755 * your renderbuffer can do sRGB encode, and you can flip a switch that does
756 * sRGB encode if the renderbuffer can handle it. You can ask specifically
757 * for a visual where you're guaranteed to be capable, but it turns out that
758 * everyone just makes all their ARGB8888 visuals capable and doesn't offer
759 * incapable ones, becuase there's no difference between the two in resources
760 * used. Applications thus get built that accidentally rely on the default
761 * visual choice being sRGB, so we make ours sRGB capable. Everything sounds
764 * But for GLES2/3, they decided that it was silly to not turn on sRGB encode
765 * for sRGB renderbuffers you made with the GL_EXT_texture_sRGB equivalent.
766 * So they removed the enable knob and made it "if the renderbuffer is sRGB
767 * capable, do sRGB encode". Then, for your window system renderbuffers, you
768 * can ask for sRGB visuals and get sRGB encode, or not ask for sRGB visuals
769 * and get no sRGB encode (assuming that both kinds of visual are available).
770 * Thus our choice to support sRGB by default on our visuals for desktop would
771 * result in broken rendering of GLES apps that aren't expecting sRGB encode.
773 * Unfortunately, renderbuffer setup happens before a context is created. So
774 * in intel_screen.c we always set up sRGB, and here, if you're a GLES2/3
775 * context (without an sRGB visual, though we don't have sRGB visuals exposed
776 * yet), we go turn that back off before anyone finds out.
779 intel_gles3_srgb_workaround(struct intel_context
*intel
,
780 struct gl_framebuffer
*fb
)
782 struct gl_context
*ctx
= &intel
->ctx
;
784 if (_mesa_is_desktop_gl(ctx
) || !fb
->Visual
.sRGBCapable
)
787 /* Some day when we support the sRGB capable bit on visuals available for
788 * GLES, we'll need to respect that and not disable things here.
790 fb
->Visual
.sRGBCapable
= false;
791 for (int i
= 0; i
< BUFFER_COUNT
; i
++) {
792 if (fb
->Attachment
[i
].Renderbuffer
&&
793 fb
->Attachment
[i
].Renderbuffer
->Format
== MESA_FORMAT_SARGB8
) {
794 fb
->Attachment
[i
].Renderbuffer
->Format
= MESA_FORMAT_ARGB8888
;
800 intelMakeCurrent(__DRIcontext
* driContextPriv
,
801 __DRIdrawable
* driDrawPriv
,
802 __DRIdrawable
* driReadPriv
)
804 struct intel_context
*intel
;
805 GET_CURRENT_CONTEXT(curCtx
);
808 intel
= (struct intel_context
*) driContextPriv
->driverPrivate
;
812 /* According to the glXMakeCurrent() man page: "Pending commands to
813 * the previous context, if any, are flushed before it is released."
814 * But only flush if we're actually changing contexts.
816 if (intel_context(curCtx
) && intel_context(curCtx
) != intel
) {
820 if (driContextPriv
) {
821 struct gl_context
*ctx
= &intel
->ctx
;
822 struct gl_framebuffer
*fb
, *readFb
;
824 if (driDrawPriv
== NULL
&& driReadPriv
== NULL
) {
825 fb
= _mesa_get_incomplete_framebuffer();
826 readFb
= _mesa_get_incomplete_framebuffer();
828 fb
= driDrawPriv
->driverPrivate
;
829 readFb
= driReadPriv
->driverPrivate
;
830 driContextPriv
->dri2
.draw_stamp
= driDrawPriv
->dri2
.stamp
- 1;
831 driContextPriv
->dri2
.read_stamp
= driReadPriv
->dri2
.stamp
- 1;
834 intel_prepare_render(intel
);
835 _mesa_make_current(ctx
, fb
, readFb
);
837 intel_gles3_srgb_workaround(intel
, ctx
->WinSysDrawBuffer
);
838 intel_gles3_srgb_workaround(intel
, ctx
->WinSysReadBuffer
);
840 /* We do this in intel_prepare_render() too, but intel->ctx.DrawBuffer
841 * is NULL at that point. We can't call _mesa_makecurrent()
842 * first, since we need the buffer size for the initial
843 * viewport. So just call intel_draw_buffer() again here. */
844 intel_draw_buffer(ctx
);
847 _mesa_make_current(NULL
, NULL
, NULL
);
854 * \brief Query DRI2 to obtain a DRIdrawable's buffers.
856 * To determine which DRI buffers to request, examine the renderbuffers
857 * attached to the drawable's framebuffer. Then request the buffers with
858 * DRI2GetBuffers() or DRI2GetBuffersWithFormat().
860 * This is called from intel_update_renderbuffers().
862 * \param drawable Drawable whose buffers are queried.
863 * \param buffers [out] List of buffers returned by DRI2 query.
864 * \param buffer_count [out] Number of buffers returned.
866 * \see intel_update_renderbuffers()
867 * \see DRI2GetBuffers()
868 * \see DRI2GetBuffersWithFormat()
871 intel_query_dri2_buffers(struct intel_context
*intel
,
872 __DRIdrawable
*drawable
,
873 __DRIbuffer
**buffers
,
876 __DRIscreen
*screen
= intel
->intelScreen
->driScrnPriv
;
877 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
879 unsigned attachments
[8];
881 struct intel_renderbuffer
*front_rb
;
882 struct intel_renderbuffer
*back_rb
;
884 front_rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
885 back_rb
= intel_get_renderbuffer(fb
, BUFFER_BACK_LEFT
);
887 memset(attachments
, 0, sizeof(attachments
));
888 if ((intel
->is_front_buffer_rendering
||
889 intel
->is_front_buffer_reading
||
890 !back_rb
) && front_rb
) {
891 /* If a fake front buffer is in use, then querying for
892 * __DRI_BUFFER_FRONT_LEFT will cause the server to copy the image from
893 * the real front buffer to the fake front buffer. So before doing the
894 * query, we need to make sure all the pending drawing has landed in the
897 intel_flush(&intel
->ctx
);
898 intel_flush_front(&intel
->ctx
);
900 attachments
[i
++] = __DRI_BUFFER_FRONT_LEFT
;
901 attachments
[i
++] = intel_bits_per_pixel(front_rb
);
902 } else if (front_rb
&& intel
->front_buffer_dirty
) {
903 /* We have pending front buffer rendering, but we aren't querying for a
904 * front buffer. If the front buffer we have is a fake front buffer,
905 * the X server is going to throw it away when it processes the query.
906 * So before doing the query, make sure all the pending drawing has
907 * landed in the real front buffer.
909 intel_flush(&intel
->ctx
);
910 intel_flush_front(&intel
->ctx
);
914 attachments
[i
++] = __DRI_BUFFER_BACK_LEFT
;
915 attachments
[i
++] = intel_bits_per_pixel(back_rb
);
918 assert(i
<= ARRAY_SIZE(attachments
));
920 *buffers
= screen
->dri2
.loader
->getBuffersWithFormat(drawable
,
925 drawable
->loaderPrivate
);
929 * \brief Assign a DRI buffer's DRM region to a renderbuffer.
931 * This is called from intel_update_renderbuffers().
934 * DRI buffers whose attachment point is DRI2BufferStencil or
935 * DRI2BufferDepthStencil are handled as special cases.
937 * \param buffer_name is a human readable name, such as "dri2 front buffer",
938 * that is passed to intel_region_alloc_for_handle().
940 * \see intel_update_renderbuffers()
941 * \see intel_region_alloc_for_handle()
944 intel_process_dri2_buffer(struct intel_context
*intel
,
945 __DRIdrawable
*drawable
,
947 struct intel_renderbuffer
*rb
,
948 const char *buffer_name
)
950 struct intel_region
*region
= NULL
;
955 unsigned num_samples
= rb
->Base
.Base
.NumSamples
;
957 /* We try to avoid closing and reopening the same BO name, because the first
958 * use of a mapping of the buffer involves a bunch of page faulting which is
959 * moderately expensive.
961 if (num_samples
== 0) {
964 rb
->mt
->region
->name
== buffer
->name
)
968 rb
->mt
->singlesample_mt
&&
969 rb
->mt
->singlesample_mt
->region
&&
970 rb
->mt
->singlesample_mt
->region
->name
== buffer
->name
)
974 if (unlikely(INTEL_DEBUG
& DEBUG_DRI
)) {
976 "attaching buffer %d, at %d, cpp %d, pitch %d\n",
977 buffer
->name
, buffer
->attachment
,
978 buffer
->cpp
, buffer
->pitch
);
981 intel_miptree_release(&rb
->mt
);
982 region
= intel_region_alloc_for_handle(intel
->intelScreen
,
992 rb
->mt
= intel_miptree_create_for_dri2_buffer(intel
,
997 intel_region_release(®ion
);