intel: flush old context before binding new context
[mesa.git] / src / mesa / drivers / dri / intel / 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/arrayobj.h"
32 #include "main/extensions.h"
33 #include "main/framebuffer.h"
34 #include "main/imports.h"
35 #include "main/points.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 "i830_dri.h"
44
45 #include "intel_chipset.h"
46 #include "intel_buffers.h"
47 #include "intel_tex.h"
48 #include "intel_batchbuffer.h"
49 #include "intel_clear.h"
50 #include "intel_extensions.h"
51 #include "intel_pixel.h"
52 #include "intel_regions.h"
53 #include "intel_buffer_objects.h"
54 #include "intel_fbo.h"
55 #include "intel_decode.h"
56 #include "intel_bufmgr.h"
57 #include "intel_screen.h"
58 #include "intel_swapbuffers.h"
59
60 #include "drirenderbuffer.h"
61 #include "vblank.h"
62 #include "utils.h"
63 #include "xmlpool.h" /* for symbolic values of enum-type options */
64
65
66 #ifndef INTEL_DEBUG
67 int INTEL_DEBUG = (0);
68 #endif
69
70
71 #define DRIVER_DATE "20090712 2009Q2 RC3"
72 #define DRIVER_DATE_GEM "GEM " DRIVER_DATE
73
74
75 static void intel_flush(GLcontext *ctx, GLboolean needs_mi_flush);
76
77 static const GLubyte *
78 intelGetString(GLcontext * ctx, GLenum name)
79 {
80 const struct intel_context *const intel = intel_context(ctx);
81 const char *chipset;
82 static char buffer[128];
83
84 switch (name) {
85 case GL_VENDOR:
86 return (GLubyte *) "Tungsten Graphics, Inc";
87 break;
88
89 case GL_RENDERER:
90 switch (intel->intelScreen->deviceID) {
91 case PCI_CHIP_845_G:
92 chipset = "Intel(R) 845G";
93 break;
94 case PCI_CHIP_I830_M:
95 chipset = "Intel(R) 830M";
96 break;
97 case PCI_CHIP_I855_GM:
98 chipset = "Intel(R) 852GM/855GM";
99 break;
100 case PCI_CHIP_I865_G:
101 chipset = "Intel(R) 865G";
102 break;
103 case PCI_CHIP_I915_G:
104 chipset = "Intel(R) 915G";
105 break;
106 case PCI_CHIP_E7221_G:
107 chipset = "Intel (R) E7221G (i915)";
108 break;
109 case PCI_CHIP_I915_GM:
110 chipset = "Intel(R) 915GM";
111 break;
112 case PCI_CHIP_I945_G:
113 chipset = "Intel(R) 945G";
114 break;
115 case PCI_CHIP_I945_GM:
116 chipset = "Intel(R) 945GM";
117 break;
118 case PCI_CHIP_I945_GME:
119 chipset = "Intel(R) 945GME";
120 break;
121 case PCI_CHIP_G33_G:
122 chipset = "Intel(R) G33";
123 break;
124 case PCI_CHIP_Q35_G:
125 chipset = "Intel(R) Q35";
126 break;
127 case PCI_CHIP_Q33_G:
128 chipset = "Intel(R) Q33";
129 break;
130 case PCI_CHIP_IGD_GM:
131 case PCI_CHIP_IGD_G:
132 chipset = "Intel(R) IGD";
133 break;
134 case PCI_CHIP_I965_Q:
135 chipset = "Intel(R) 965Q";
136 break;
137 case PCI_CHIP_I965_G:
138 case PCI_CHIP_I965_G_1:
139 chipset = "Intel(R) 965G";
140 break;
141 case PCI_CHIP_I946_GZ:
142 chipset = "Intel(R) 946GZ";
143 break;
144 case PCI_CHIP_I965_GM:
145 chipset = "Intel(R) 965GM";
146 break;
147 case PCI_CHIP_I965_GME:
148 chipset = "Intel(R) 965GME/GLE";
149 break;
150 case PCI_CHIP_GM45_GM:
151 chipset = "Mobile IntelĀ® GM45 Express Chipset";
152 break;
153 case PCI_CHIP_IGD_E_G:
154 chipset = "Intel(R) Integrated Graphics Device";
155 break;
156 case PCI_CHIP_G45_G:
157 chipset = "Intel(R) G45/G43";
158 break;
159 case PCI_CHIP_Q45_G:
160 chipset = "Intel(R) Q45/Q43";
161 break;
162 case PCI_CHIP_G41_G:
163 chipset = "Intel(R) G41";
164 break;
165 case PCI_CHIP_B43_G:
166 chipset = "Intel(R) B43";
167 break;
168 case PCI_CHIP_ILD_G:
169 chipset = "Intel(R) IGDNG_D";
170 break;
171 case PCI_CHIP_ILM_G:
172 chipset = "Intel(R) IGDNG_M";
173 break;
174 default:
175 chipset = "Unknown Intel Chipset";
176 break;
177 }
178
179 (void) driGetRendererString(buffer, chipset,
180 (intel->ttm) ? DRIVER_DATE_GEM : DRIVER_DATE,
181 0);
182 return (GLubyte *) buffer;
183
184 default:
185 return NULL;
186 }
187 }
188
189 static unsigned
190 intel_bits_per_pixel(const struct intel_renderbuffer *rb)
191 {
192 switch (rb->Base._ActualFormat) {
193 case GL_RGB5:
194 case GL_DEPTH_COMPONENT16:
195 return 16;
196 case GL_RGB8:
197 case GL_RGBA8:
198 case GL_DEPTH_COMPONENT24:
199 case GL_DEPTH24_STENCIL8_EXT:
200 case GL_STENCIL_INDEX8_EXT:
201 return 32;
202 default:
203 return 0;
204 }
205 }
206
207 void
208 intel_update_renderbuffers(__DRIcontext *context, __DRIdrawable *drawable)
209 {
210 struct intel_framebuffer *intel_fb = drawable->driverPrivate;
211 struct intel_renderbuffer *rb;
212 struct intel_region *region, *depth_region;
213 struct intel_context *intel = context->driverPrivate;
214 __DRIbuffer *buffers = NULL;
215 __DRIscreen *screen;
216 int i, count;
217 unsigned int attachments[10];
218 uint32_t name;
219 const char *region_name;
220
221 if (INTEL_DEBUG & DEBUG_DRI)
222 fprintf(stderr, "enter %s, drawable %p\n", __func__, drawable);
223
224 screen = intel->intelScreen->driScrnPriv;
225
226 if (screen->dri2.loader
227 && (screen->dri2.loader->base.version > 2)
228 && (screen->dri2.loader->getBuffersWithFormat != NULL)) {
229 struct intel_renderbuffer *depth_rb;
230 struct intel_renderbuffer *stencil_rb;
231
232 i = 0;
233 if ((intel->is_front_buffer_rendering ||
234 intel->is_front_buffer_reading ||
235 !intel_fb->color_rb[1])
236 && intel_fb->color_rb[0]) {
237 attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
238 attachments[i++] = intel_bits_per_pixel(intel_fb->color_rb[0]);
239 }
240
241 if (intel_fb->color_rb[1]) {
242 attachments[i++] = __DRI_BUFFER_BACK_LEFT;
243 attachments[i++] = intel_bits_per_pixel(intel_fb->color_rb[1]);
244 }
245
246 depth_rb = intel_get_renderbuffer(&intel_fb->Base, BUFFER_DEPTH);
247 stencil_rb = intel_get_renderbuffer(&intel_fb->Base, BUFFER_STENCIL);
248
249 if ((depth_rb != NULL) && (stencil_rb != NULL)) {
250 attachments[i++] = __DRI_BUFFER_DEPTH_STENCIL;
251 attachments[i++] = intel_bits_per_pixel(depth_rb);
252 } else if (depth_rb != NULL) {
253 attachments[i++] = __DRI_BUFFER_DEPTH;
254 attachments[i++] = intel_bits_per_pixel(depth_rb);
255 } else if (stencil_rb != NULL) {
256 attachments[i++] = __DRI_BUFFER_STENCIL;
257 attachments[i++] = intel_bits_per_pixel(stencil_rb);
258 }
259
260 buffers =
261 (*screen->dri2.loader->getBuffersWithFormat)(drawable,
262 &drawable->w,
263 &drawable->h,
264 attachments, i / 2,
265 &count,
266 drawable->loaderPrivate);
267 } else if (screen->dri2.loader) {
268 i = 0;
269 if (intel_fb->color_rb[0])
270 attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
271 if (intel_fb->color_rb[1])
272 attachments[i++] = __DRI_BUFFER_BACK_LEFT;
273 if (intel_get_renderbuffer(&intel_fb->Base, BUFFER_DEPTH))
274 attachments[i++] = __DRI_BUFFER_DEPTH;
275 if (intel_get_renderbuffer(&intel_fb->Base, BUFFER_STENCIL))
276 attachments[i++] = __DRI_BUFFER_STENCIL;
277
278 buffers = (*screen->dri2.loader->getBuffers)(drawable,
279 &drawable->w,
280 &drawable->h,
281 attachments, i,
282 &count,
283 drawable->loaderPrivate);
284 }
285
286 if (buffers == NULL)
287 return;
288
289 drawable->x = 0;
290 drawable->y = 0;
291 drawable->backX = 0;
292 drawable->backY = 0;
293 drawable->numClipRects = 1;
294 drawable->pClipRects[0].x1 = 0;
295 drawable->pClipRects[0].y1 = 0;
296 drawable->pClipRects[0].x2 = drawable->w;
297 drawable->pClipRects[0].y2 = drawable->h;
298 drawable->numBackClipRects = 1;
299 drawable->pBackClipRects[0].x1 = 0;
300 drawable->pBackClipRects[0].y1 = 0;
301 drawable->pBackClipRects[0].x2 = drawable->w;
302 drawable->pBackClipRects[0].y2 = drawable->h;
303
304 depth_region = NULL;
305 for (i = 0; i < count; i++) {
306 switch (buffers[i].attachment) {
307 case __DRI_BUFFER_FRONT_LEFT:
308 rb = intel_fb->color_rb[0];
309 region_name = "dri2 front buffer";
310 break;
311
312 case __DRI_BUFFER_FAKE_FRONT_LEFT:
313 rb = intel_fb->color_rb[0];
314 region_name = "dri2 fake front buffer";
315 break;
316
317 case __DRI_BUFFER_BACK_LEFT:
318 rb = intel_fb->color_rb[1];
319 region_name = "dri2 back buffer";
320 break;
321
322 case __DRI_BUFFER_DEPTH:
323 rb = intel_get_renderbuffer(&intel_fb->Base, BUFFER_DEPTH);
324 region_name = "dri2 depth buffer";
325 break;
326
327 case __DRI_BUFFER_DEPTH_STENCIL:
328 rb = intel_get_renderbuffer(&intel_fb->Base, BUFFER_DEPTH);
329 region_name = "dri2 depth / stencil buffer";
330 break;
331
332 case __DRI_BUFFER_STENCIL:
333 rb = intel_get_renderbuffer(&intel_fb->Base, BUFFER_STENCIL);
334 region_name = "dri2 stencil buffer";
335 break;
336
337 case __DRI_BUFFER_ACCUM:
338 default:
339 fprintf(stderr,
340 "unhandled buffer attach event, attacment type %d\n",
341 buffers[i].attachment);
342 return;
343 }
344
345 if (rb == NULL)
346 continue;
347
348 if (rb->region) {
349 dri_bo_flink(rb->region->buffer, &name);
350 if (name == buffers[i].name)
351 continue;
352 }
353
354 if (INTEL_DEBUG & DEBUG_DRI)
355 fprintf(stderr,
356 "attaching buffer %d, at %d, cpp %d, pitch %d\n",
357 buffers[i].name, buffers[i].attachment,
358 buffers[i].cpp, buffers[i].pitch);
359
360 if (buffers[i].attachment == __DRI_BUFFER_STENCIL && depth_region) {
361 if (INTEL_DEBUG & DEBUG_DRI)
362 fprintf(stderr, "(reusing depth buffer as stencil)\n");
363 intel_region_reference(&region, depth_region);
364 }
365 else
366 region = intel_region_alloc_for_handle(intel, buffers[i].cpp,
367 drawable->w,
368 drawable->h,
369 buffers[i].pitch / buffers[i].cpp,
370 buffers[i].name,
371 region_name);
372
373 if (buffers[i].attachment == __DRI_BUFFER_DEPTH)
374 depth_region = region;
375
376 intel_renderbuffer_set_region(rb, region);
377 intel_region_release(&region);
378
379 if (buffers[i].attachment == __DRI_BUFFER_DEPTH_STENCIL) {
380 rb = intel_get_renderbuffer(&intel_fb->Base, BUFFER_STENCIL);
381 if (rb != NULL) {
382 struct intel_region *stencil_region = NULL;
383
384 if (rb->region) {
385 dri_bo_flink(rb->region->buffer, &name);
386 if (name == buffers[i].name)
387 continue;
388 }
389
390 intel_region_reference(&stencil_region, region);
391 intel_renderbuffer_set_region(rb, stencil_region);
392 intel_region_release(&stencil_region);
393 }
394 }
395 }
396
397 driUpdateFramebufferSize(&intel->ctx, drawable);
398 }
399
400 void
401 intel_viewport(GLcontext *ctx, GLint x, GLint y, GLsizei w, GLsizei h)
402 {
403 struct intel_context *intel = intel_context(ctx);
404 __DRIcontext *driContext = intel->driContext;
405 void (*old_viewport)(GLcontext *ctx, GLint x, GLint y,
406 GLsizei w, GLsizei h);
407
408 if (!driContext->driScreenPriv->dri2.enabled)
409 return;
410
411 if (!intel->meta.internal_viewport_call && ctx->DrawBuffer->Name == 0) {
412 /* If we're rendering to the fake front buffer, make sure all the pending
413 * drawing has landed on the real front buffer. Otherwise when we
414 * eventually get to DRI2GetBuffersWithFormat the stale real front
415 * buffer contents will get copied to the new fake front buffer.
416 */
417 if (intel->is_front_buffer_rendering) {
418 intel_flush(ctx, GL_FALSE);
419 }
420
421 intel_update_renderbuffers(driContext, driContext->driDrawablePriv);
422 if (driContext->driDrawablePriv != driContext->driReadablePriv)
423 intel_update_renderbuffers(driContext, driContext->driReadablePriv);
424 }
425
426 old_viewport = ctx->Driver.Viewport;
427 ctx->Driver.Viewport = NULL;
428 intel->driDrawable = driContext->driDrawablePriv;
429 intelWindowMoved(intel);
430 intel_draw_buffer(ctx, intel->ctx.DrawBuffer);
431 ctx->Driver.Viewport = old_viewport;
432 }
433
434
435 static const struct dri_debug_control debug_control[] = {
436 { "tex", DEBUG_TEXTURE},
437 { "state", DEBUG_STATE},
438 { "ioctl", DEBUG_IOCTL},
439 { "blit", DEBUG_BLIT},
440 { "mip", DEBUG_MIPTREE},
441 { "fall", DEBUG_FALLBACKS},
442 { "verb", DEBUG_VERBOSE},
443 { "bat", DEBUG_BATCH},
444 { "pix", DEBUG_PIXEL},
445 { "buf", DEBUG_BUFMGR},
446 { "reg", DEBUG_REGION},
447 { "fbo", DEBUG_FBO},
448 { "lock", DEBUG_LOCK},
449 { "sync", DEBUG_SYNC},
450 { "prim", DEBUG_PRIMS },
451 { "vert", DEBUG_VERTS },
452 { "dri", DEBUG_DRI },
453 { "dma", DEBUG_DMA },
454 { "san", DEBUG_SANITY },
455 { "sleep", DEBUG_SLEEP },
456 { "stats", DEBUG_STATS },
457 { "tile", DEBUG_TILE },
458 { "sing", DEBUG_SINGLE_THREAD },
459 { "thre", DEBUG_SINGLE_THREAD },
460 { "wm", DEBUG_WM },
461 { "urb", DEBUG_URB },
462 { "vs", DEBUG_VS },
463 { NULL, 0 }
464 };
465
466
467 static void
468 intelInvalidateState(GLcontext * ctx, GLuint new_state)
469 {
470 struct intel_context *intel = intel_context(ctx);
471
472 _swrast_InvalidateState(ctx, new_state);
473 _swsetup_InvalidateState(ctx, new_state);
474 _vbo_InvalidateState(ctx, new_state);
475 _tnl_InvalidateState(ctx, new_state);
476 _tnl_invalidate_vertex_state(ctx, new_state);
477
478 intel->NewGLState |= new_state;
479
480 if (intel->vtbl.invalidate_state)
481 intel->vtbl.invalidate_state( intel, new_state );
482 }
483
484 static void
485 intel_flush(GLcontext *ctx, GLboolean needs_mi_flush)
486 {
487 struct intel_context *intel = intel_context(ctx);
488
489 if (intel->Fallback)
490 _swrast_flush(ctx);
491
492 if (!IS_965(intel->intelScreen->deviceID))
493 INTEL_FIREVERTICES(intel);
494
495 /* Emit a flush so that any frontbuffer rendering that might have occurred
496 * lands onscreen in a timely manner, even if the X Server doesn't trigger
497 * a flush for us.
498 */
499 if (needs_mi_flush)
500 intel_batchbuffer_emit_mi_flush(intel->batch);
501
502 if (intel->batch->map != intel->batch->ptr)
503 intel_batchbuffer_flush(intel->batch);
504
505 if ((ctx->DrawBuffer->Name == 0) && intel->front_buffer_dirty) {
506 __DRIscreen *const screen = intel->intelScreen->driScrnPriv;
507
508 if (screen->dri2.loader &&
509 (screen->dri2.loader->base.version >= 2)
510 && (screen->dri2.loader->flushFrontBuffer != NULL) &&
511 intel->driDrawable && intel->driDrawable->loaderPrivate) {
512 (*screen->dri2.loader->flushFrontBuffer)(intel->driDrawable,
513 intel->driDrawable->loaderPrivate);
514
515 /* Only clear the dirty bit if front-buffer rendering is no longer
516 * enabled. This is done so that the dirty bit can only be set in
517 * glDrawBuffer. Otherwise the dirty bit would have to be set at
518 * each of N places that do rendering. This has worse performances,
519 * but it is much easier to get correct.
520 */
521 if (!intel->is_front_buffer_rendering) {
522 intel->front_buffer_dirty = GL_FALSE;
523 }
524 }
525 }
526 }
527
528 void
529 intelFlush(GLcontext * ctx)
530 {
531 intel_flush(ctx, GL_FALSE);
532 }
533
534 static void
535 intel_glFlush(GLcontext *ctx)
536 {
537 struct intel_context *intel = intel_context(ctx);
538
539 intel_flush(ctx, GL_TRUE);
540
541 /* We're using glFlush as an indicator that a frame is done, which is
542 * what DRI2 does before calling SwapBuffers (and means we should catch
543 * people doing front-buffer rendering, as well)..
544 *
545 * Wait for the swapbuffers before the one we just emitted, so we don't
546 * get too many swaps outstanding for apps that are GPU-heavy but not
547 * CPU-heavy.
548 *
549 * Unfortunately, we don't have a handle to the batch containing the swap,
550 * and getting our hands on that doesn't seem worth it, so we just us the
551 * first batch we emitted after the last swap.
552 */
553 if (intel->first_post_swapbuffers_batch != NULL) {
554 drm_intel_bo_wait_rendering(intel->first_post_swapbuffers_batch);
555 drm_intel_bo_unreference(intel->first_post_swapbuffers_batch);
556 intel->first_post_swapbuffers_batch = NULL;
557 }
558 }
559
560 void
561 intelFinish(GLcontext * ctx)
562 {
563 struct gl_framebuffer *fb = ctx->DrawBuffer;
564 int i;
565
566 intelFlush(ctx);
567
568 for (i = 0; i < fb->_NumColorDrawBuffers; i++) {
569 struct intel_renderbuffer *irb;
570
571 irb = intel_renderbuffer(fb->_ColorDrawBuffers[i]);
572
573 if (irb && irb->region)
574 dri_bo_wait_rendering(irb->region->buffer);
575 }
576 if (fb->_DepthBuffer) {
577 /* XXX: Wait on buffer idle */
578 }
579 }
580
581 void
582 intelInitDriverFunctions(struct dd_function_table *functions)
583 {
584 _mesa_init_driver_functions(functions);
585
586 functions->Flush = intel_glFlush;
587 functions->Finish = intelFinish;
588 functions->GetString = intelGetString;
589 functions->UpdateState = intelInvalidateState;
590
591 functions->CopyColorTable = _swrast_CopyColorTable;
592 functions->CopyColorSubTable = _swrast_CopyColorSubTable;
593 functions->CopyConvolutionFilter1D = _swrast_CopyConvolutionFilter1D;
594 functions->CopyConvolutionFilter2D = _swrast_CopyConvolutionFilter2D;
595
596 intelInitTextureFuncs(functions);
597 intelInitTextureImageFuncs(functions);
598 intelInitTextureSubImageFuncs(functions);
599 intelInitTextureCopyImageFuncs(functions);
600 intelInitStateFuncs(functions);
601 intelInitClearFuncs(functions);
602 intelInitBufferFuncs(functions);
603 intelInitPixelFuncs(functions);
604 intelInitBufferObjectFuncs(functions);
605 intel_init_syncobj_functions(functions);
606 }
607
608
609 GLboolean
610 intelInitContext(struct intel_context *intel,
611 const __GLcontextModes * mesaVis,
612 __DRIcontextPrivate * driContextPriv,
613 void *sharedContextPrivate,
614 struct dd_function_table *functions)
615 {
616 GLcontext *ctx = &intel->ctx;
617 GLcontext *shareCtx = (GLcontext *) sharedContextPrivate;
618 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
619 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
620 int fthrottle_mode;
621
622 if (!_mesa_initialize_context(&intel->ctx, mesaVis, shareCtx,
623 functions, (void *) intel)) {
624 _mesa_printf("%s: failed to init mesa context\n", __FUNCTION__);
625 return GL_FALSE;
626 }
627
628 driContextPriv->driverPrivate = intel;
629 intel->intelScreen = intelScreen;
630 intel->driScreen = sPriv;
631 intel->sarea = intelScreen->sarea;
632 intel->driContext = driContextPriv;
633
634 /* Dri stuff */
635 intel->hHWContext = driContextPriv->hHWContext;
636 intel->driFd = sPriv->fd;
637 intel->driHwLock = sPriv->lock;
638
639 driParseConfigFiles(&intel->optionCache, &intelScreen->optionCache,
640 intel->driScreen->myNum,
641 IS_965(intelScreen->deviceID) ? "i965" : "i915");
642 if (intelScreen->deviceID == PCI_CHIP_I865_G)
643 intel->maxBatchSize = 4096;
644 else
645 intel->maxBatchSize = BATCH_SZ;
646
647 intel->bufmgr = intelScreen->bufmgr;
648
649 if (0) /* for debug */
650 drm_intel_bufmgr_set_debug(intel->bufmgr, 1);
651
652 intel->ttm = intelScreen->ttm;
653 if (intel->ttm) {
654 int bo_reuse_mode;
655
656 bo_reuse_mode = driQueryOptioni(&intel->optionCache, "bo_reuse");
657 switch (bo_reuse_mode) {
658 case DRI_CONF_BO_REUSE_DISABLED:
659 break;
660 case DRI_CONF_BO_REUSE_ALL:
661 intel_bufmgr_gem_enable_reuse(intel->bufmgr);
662 break;
663 }
664 }
665
666 /* This doesn't yet catch all non-conformant rendering, but it's a
667 * start.
668 */
669 if (getenv("INTEL_STRICT_CONFORMANCE")) {
670 unsigned int value = atoi(getenv("INTEL_STRICT_CONFORMANCE"));
671 if (value > 0) {
672 intel->conformance_mode = value;
673 }
674 else {
675 intel->conformance_mode = 1;
676 }
677 }
678
679 if (intel->conformance_mode > 0) {
680 ctx->Const.MinLineWidth = 1.0;
681 ctx->Const.MinLineWidthAA = 1.0;
682 ctx->Const.MaxLineWidth = 1.0;
683 ctx->Const.MaxLineWidthAA = 1.0;
684 ctx->Const.LineWidthGranularity = 1.0;
685 }
686 else {
687 ctx->Const.MinLineWidth = 1.0;
688 ctx->Const.MinLineWidthAA = 1.0;
689 ctx->Const.MaxLineWidth = 5.0;
690 ctx->Const.MaxLineWidthAA = 5.0;
691 ctx->Const.LineWidthGranularity = 0.5;
692 }
693
694 ctx->Const.MinPointSize = 1.0;
695 ctx->Const.MinPointSizeAA = 1.0;
696 ctx->Const.MaxPointSize = 255.0;
697 ctx->Const.MaxPointSizeAA = 3.0;
698 ctx->Const.PointSizeGranularity = 1.0;
699
700 /* reinitialize the context point state.
701 * It depend on constants in __GLcontextRec::Const
702 */
703 _mesa_init_point(ctx);
704
705 meta_init_metaops(ctx, &intel->meta);
706 ctx->Const.MaxColorAttachments = 4; /* XXX FBO: review this */
707 if (IS_965(intelScreen->deviceID)) {
708 if (MAX_WIDTH > 8192)
709 ctx->Const.MaxRenderbufferSize = 8192;
710 } else {
711 if (MAX_WIDTH > 2048)
712 ctx->Const.MaxRenderbufferSize = 2048;
713 }
714
715 /* Initialize the software rasterizer and helper modules. */
716 _swrast_CreateContext(ctx);
717 _vbo_CreateContext(ctx);
718 _tnl_CreateContext(ctx);
719 _swsetup_CreateContext(ctx);
720
721 /* Configure swrast to match hardware characteristics: */
722 _swrast_allow_pixel_fog(ctx, GL_FALSE);
723 _swrast_allow_vertex_fog(ctx, GL_TRUE);
724
725 _mesa_meta_init(ctx);
726
727 intel->hw_stencil = mesaVis->stencilBits && mesaVis->depthBits == 24;
728 intel->hw_stipple = 1;
729
730 /* XXX FBO: this doesn't seem to be used anywhere */
731 switch (mesaVis->depthBits) {
732 case 0: /* what to do in this case? */
733 case 16:
734 intel->polygon_offset_scale = 1.0;
735 break;
736 case 24:
737 intel->polygon_offset_scale = 2.0; /* req'd to pass glean */
738 break;
739 default:
740 assert(0);
741 break;
742 }
743
744 if (IS_965(intelScreen->deviceID))
745 intel->polygon_offset_scale /= 0xffff;
746
747 intel->RenderIndex = ~0;
748
749 fthrottle_mode = driQueryOptioni(&intel->optionCache, "fthrottle_mode");
750 intel->irqsEmitted = 0;
751
752 intel->do_irqs = (intel->intelScreen->irq_active &&
753 fthrottle_mode == DRI_CONF_FTHROTTLE_IRQS);
754
755 intel->do_usleeps = (fthrottle_mode == DRI_CONF_FTHROTTLE_USLEEPS);
756
757 if (IS_965(intelScreen->deviceID) && !intel->intelScreen->irq_active) {
758 _mesa_printf("IRQs not active. Exiting\n");
759 exit(1);
760 }
761
762 intelInitExtensions(ctx, GL_FALSE);
763
764 INTEL_DEBUG = driParseDebugString(getenv("INTEL_DEBUG"), debug_control);
765 if (INTEL_DEBUG & DEBUG_BUFMGR)
766 dri_bufmgr_set_debug(intel->bufmgr, GL_TRUE);
767
768 if (!sPriv->dri2.enabled)
769 intel_recreate_static_regions(intel);
770
771 intel->batch = intel_batchbuffer_alloc(intel);
772
773 intel_fbo_init(intel);
774
775 if (intel->ctx.Mesa_DXTn) {
776 _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
777 _mesa_enable_extension(ctx, "GL_S3_s3tc");
778 }
779 else if (driQueryOptionb(&intel->optionCache, "force_s3tc_enable")) {
780 _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
781 }
782 intel->use_texture_tiling = driQueryOptionb(&intel->optionCache,
783 "texture_tiling");
784 if (intel->use_texture_tiling &&
785 !intel->intelScreen->kernel_exec_fencing) {
786 fprintf(stderr, "No kernel support for execution fencing, "
787 "disabling texture tiling\n");
788 intel->use_texture_tiling = GL_FALSE;
789 }
790 intel->use_early_z = driQueryOptionb(&intel->optionCache, "early_z");
791
792 intel->prim.primitive = ~0;
793
794 /* Force all software fallbacks */
795 if (driQueryOptionb(&intel->optionCache, "no_rast")) {
796 fprintf(stderr, "disabling 3D rasterization\n");
797 intel->no_rast = 1;
798 }
799
800 if (driQueryOptionb(&intel->optionCache, "always_flush_batch")) {
801 fprintf(stderr, "flushing batchbuffer before/after each draw call\n");
802 intel->always_flush_batch = 1;
803 }
804
805 if (driQueryOptionb(&intel->optionCache, "always_flush_cache")) {
806 fprintf(stderr, "flushing GPU caches before/after each draw call\n");
807 intel->always_flush_cache = 1;
808 }
809
810 /* Disable all hardware rendering (skip emitting batches and fences/waits
811 * to the kernel)
812 */
813 intel->no_hw = getenv("INTEL_NO_HW") != NULL;
814
815 return GL_TRUE;
816 }
817
818 void
819 intelDestroyContext(__DRIcontextPrivate * driContextPriv)
820 {
821 struct intel_context *intel =
822 (struct intel_context *) driContextPriv->driverPrivate;
823
824 assert(intel); /* should never be null */
825 if (intel) {
826 GLboolean release_texture_heaps;
827
828 INTEL_FIREVERTICES(intel);
829
830 _mesa_meta_free(&intel->ctx);
831
832 meta_destroy_metaops(&intel->meta);
833
834 intel->vtbl.destroy(intel);
835
836 release_texture_heaps = (intel->ctx.Shared->RefCount == 1);
837 _swsetup_DestroyContext(&intel->ctx);
838 _tnl_DestroyContext(&intel->ctx);
839 _vbo_DestroyContext(&intel->ctx);
840
841 _swrast_DestroyContext(&intel->ctx);
842 intel->Fallback = 0x0; /* don't call _swrast_Flush later */
843
844 intel_batchbuffer_free(intel->batch);
845 intel->batch = NULL;
846
847 free(intel->prim.vb);
848 intel->prim.vb = NULL;
849 dri_bo_unreference(intel->prim.vb_bo);
850 intel->prim.vb_bo = NULL;
851 dri_bo_unreference(intel->first_post_swapbuffers_batch);
852 intel->first_post_swapbuffers_batch = NULL;
853
854 if (release_texture_heaps) {
855 /* Nothing is currently done here to free texture heaps;
856 * but we're not using the texture heap utilities, so I
857 * rather think we shouldn't. I've taken a look, and can't
858 * find any private texture data hanging around anywhere, but
859 * I'm not yet certain there isn't any at all...
860 */
861 /* if (INTEL_DEBUG & DEBUG_TEXTURE)
862 fprintf(stderr, "do something to free texture heaps\n");
863 */
864 }
865
866 /* XXX In intelMakeCurrent() below, the context's static regions are
867 * referenced inside the frame buffer; it's listed as a hack,
868 * with a comment of "XXX FBO temporary fix-ups!", but
869 * as long as it's there, we should release the regions here.
870 * The do/while loop around the block is used to allow the
871 * "continue" statements inside the block to exit the block,
872 * to avoid many layers of "if" constructs.
873 */
874 do {
875 __DRIdrawablePrivate * driDrawPriv = intel->driDrawable;
876 struct intel_framebuffer *intel_fb;
877 struct intel_renderbuffer *irbDepth, *irbStencil;
878 if (!driDrawPriv) {
879 /* We're already detached from the drawable; exit this block. */
880 continue;
881 }
882 intel_fb = (struct intel_framebuffer *) driDrawPriv->driverPrivate;
883 if (!intel_fb) {
884 /* The frame buffer is already gone; exit this block. */
885 continue;
886 }
887 irbDepth = intel_get_renderbuffer(&intel_fb->Base, BUFFER_DEPTH);
888 irbStencil = intel_get_renderbuffer(&intel_fb->Base, BUFFER_STENCIL);
889
890 /* If the regions of the frame buffer still match the regions
891 * of the context, release them. If they've changed somehow,
892 * leave them alone.
893 */
894 if (intel_fb->color_rb[0] && intel_fb->color_rb[0]->region == intel->front_region) {
895 intel_renderbuffer_set_region(intel_fb->color_rb[0], NULL);
896 }
897 if (intel_fb->color_rb[1] && intel_fb->color_rb[1]->region == intel->back_region) {
898 intel_renderbuffer_set_region(intel_fb->color_rb[1], NULL);
899 }
900
901 if (irbDepth && irbDepth->region == intel->depth_region) {
902 intel_renderbuffer_set_region(irbDepth, NULL);
903 }
904 /* Usually, the stencil buffer is the same as the depth buffer;
905 * but they're handled separately in MakeCurrent, so we'll
906 * handle them separately here.
907 */
908 if (irbStencil && irbStencil->region == intel->depth_region) {
909 intel_renderbuffer_set_region(irbStencil, NULL);
910 }
911 } while (0);
912
913 intel_region_release(&intel->front_region);
914 intel_region_release(&intel->back_region);
915 intel_region_release(&intel->depth_region);
916
917 driDestroyOptionCache(&intel->optionCache);
918
919 /* free the Mesa context */
920 _mesa_free_context_data(&intel->ctx);
921
922 FREE(intel);
923 driContextPriv->driverPrivate = NULL;
924 }
925 }
926
927 GLboolean
928 intelUnbindContext(__DRIcontextPrivate * driContextPriv)
929 {
930 struct intel_context *intel =
931 (struct intel_context *) driContextPriv->driverPrivate;
932
933 /* Deassociate the context with the drawables.
934 */
935 intel->driDrawable = NULL;
936 intel->driReadDrawable = NULL;
937
938 return GL_TRUE;
939 }
940
941 GLboolean
942 intelMakeCurrent(__DRIcontextPrivate * driContextPriv,
943 __DRIdrawablePrivate * driDrawPriv,
944 __DRIdrawablePrivate * driReadPriv)
945 {
946 __DRIscreenPrivate *psp = driDrawPriv->driScreenPriv;
947 struct intel_context *intel;
948 GET_CURRENT_CONTEXT(curCtx);
949
950 if (driContextPriv)
951 intel = (struct intel_context *) driContextPriv->driverPrivate;
952 else
953 intel = NULL;
954
955 /* According to the glXMakeCurrent() man page: "Pending commands to
956 * the previous context, if any, are flushed before it is released."
957 * But only flush if we're actually changing contexts.
958 */
959 if (intel_context(curCtx) && intel_context(curCtx) != intel) {
960 _mesa_flush(curCtx);
961 }
962
963 if (driContextPriv) {
964 struct intel_framebuffer *intel_fb =
965 (struct intel_framebuffer *) driDrawPriv->driverPrivate;
966 GLframebuffer *readFb = (GLframebuffer *) driReadPriv->driverPrivate;
967
968 if (driContextPriv->driScreenPriv->dri2.enabled) {
969 intel_update_renderbuffers(driContextPriv, driDrawPriv);
970 if (driDrawPriv != driReadPriv)
971 intel_update_renderbuffers(driContextPriv, driReadPriv);
972 } else {
973 /* XXX FBO temporary fix-ups! These are released in
974 * intelDextroyContext(), above. Changes here should be
975 * reflected there.
976 */
977 /* if the renderbuffers don't have regions, init them from the context */
978 struct intel_renderbuffer *irbDepth
979 = intel_get_renderbuffer(&intel_fb->Base, BUFFER_DEPTH);
980 struct intel_renderbuffer *irbStencil
981 = intel_get_renderbuffer(&intel_fb->Base, BUFFER_STENCIL);
982
983 if (intel_fb->color_rb[0]) {
984 intel_renderbuffer_set_region(intel_fb->color_rb[0],
985 intel->front_region);
986 }
987 if (intel_fb->color_rb[1]) {
988 intel_renderbuffer_set_region(intel_fb->color_rb[1],
989 intel->back_region);
990 }
991
992 if (irbDepth) {
993 intel_renderbuffer_set_region(irbDepth, intel->depth_region);
994 }
995 if (irbStencil) {
996 intel_renderbuffer_set_region(irbStencil, intel->depth_region);
997 }
998 }
999
1000 /* set GLframebuffer size to match window, if needed */
1001 driUpdateFramebufferSize(&intel->ctx, driDrawPriv);
1002
1003 if (driReadPriv != driDrawPriv) {
1004 driUpdateFramebufferSize(&intel->ctx, driReadPriv);
1005 }
1006
1007 _mesa_make_current(&intel->ctx, &intel_fb->Base, readFb);
1008
1009 intel->driReadDrawable = driReadPriv;
1010
1011 if (intel->driDrawable != driDrawPriv) {
1012 if (driDrawPriv->swap_interval == (unsigned)-1) {
1013 int i;
1014
1015 driDrawPriv->vblFlags = (intel->intelScreen->irq_active != 0)
1016 ? driGetDefaultVBlankFlags(&intel->optionCache)
1017 : VBLANK_FLAG_NO_IRQ;
1018
1019 /* Prevent error printf if one crtc is disabled, this will
1020 * be properly calculated in intelWindowMoved() next.
1021 */
1022 driDrawPriv->vblFlags = intelFixupVblank(intel, driDrawPriv);
1023
1024 (*psp->systemTime->getUST) (&intel_fb->swap_ust);
1025 driDrawableInitVBlank(driDrawPriv);
1026 intel_fb->vbl_waited = driDrawPriv->vblSeq;
1027
1028 for (i = 0; i < 2; i++) {
1029 if (intel_fb->color_rb[i])
1030 intel_fb->color_rb[i]->vbl_pending = driDrawPriv->vblSeq;
1031 }
1032 }
1033 intel->driDrawable = driDrawPriv;
1034 intelWindowMoved(intel);
1035 }
1036
1037 intel_draw_buffer(&intel->ctx, &intel_fb->Base);
1038 }
1039 else {
1040 _mesa_make_current(NULL, NULL, NULL);
1041 }
1042
1043 return GL_TRUE;
1044 }
1045
1046 static void
1047 intelContendedLock(struct intel_context *intel, GLuint flags)
1048 {
1049 __DRIdrawablePrivate *dPriv = intel->driDrawable;
1050 __DRIscreenPrivate *sPriv = intel->driScreen;
1051 volatile drm_i915_sarea_t *sarea = intel->sarea;
1052 int me = intel->hHWContext;
1053
1054 drmGetLock(intel->driFd, intel->hHWContext, flags);
1055
1056 if (INTEL_DEBUG & DEBUG_LOCK)
1057 _mesa_printf("%s - got contended lock\n", __progname);
1058
1059 /* If the window moved, may need to set a new cliprect now.
1060 *
1061 * NOTE: This releases and regains the hw lock, so all state
1062 * checking must be done *after* this call:
1063 */
1064 if (dPriv)
1065 DRI_VALIDATE_DRAWABLE_INFO(sPriv, dPriv);
1066
1067 if (sarea && sarea->ctxOwner != me) {
1068 if (INTEL_DEBUG & DEBUG_BUFMGR) {
1069 fprintf(stderr, "Lost Context: sarea->ctxOwner %x me %x\n",
1070 sarea->ctxOwner, me);
1071 }
1072 sarea->ctxOwner = me;
1073 }
1074
1075 /* If the last consumer of the texture memory wasn't us, notify the fake
1076 * bufmgr and record the new owner. We should have the memory shared
1077 * between contexts of a single fake bufmgr, but this will at least make
1078 * things correct for now.
1079 */
1080 if (!intel->ttm && sarea->texAge != intel->hHWContext) {
1081 sarea->texAge = intel->hHWContext;
1082 intel_bufmgr_fake_contended_lock_take(intel->bufmgr);
1083 if (INTEL_DEBUG & DEBUG_BATCH)
1084 intel_decode_context_reset();
1085 if (INTEL_DEBUG & DEBUG_BUFMGR)
1086 fprintf(stderr, "Lost Textures: sarea->texAge %x hw context %x\n",
1087 sarea->ctxOwner, intel->hHWContext);
1088 }
1089
1090 /* Drawable changed?
1091 */
1092 if (dPriv && intel->lastStamp != dPriv->lastStamp) {
1093 intelWindowMoved(intel);
1094 intel->lastStamp = dPriv->lastStamp;
1095 }
1096 }
1097
1098
1099 _glthread_DECLARE_STATIC_MUTEX(lockMutex);
1100
1101 /* Lock the hardware and validate our state.
1102 */
1103 void LOCK_HARDWARE( struct intel_context *intel )
1104 {
1105 __DRIdrawable *dPriv = intel->driDrawable;
1106 __DRIscreen *sPriv = intel->driScreen;
1107 char __ret = 0;
1108 struct intel_framebuffer *intel_fb = NULL;
1109 struct intel_renderbuffer *intel_rb = NULL;
1110
1111 intel->locked++;
1112 if (intel->locked >= 2)
1113 return;
1114
1115 if (!sPriv->dri2.enabled)
1116 _glthread_LOCK_MUTEX(lockMutex);
1117
1118 if (intel->driDrawable) {
1119 intel_fb = intel->driDrawable->driverPrivate;
1120
1121 if (intel_fb)
1122 intel_rb =
1123 intel_get_renderbuffer(&intel_fb->Base,
1124 intel_fb->Base._ColorDrawBufferIndexes[0]);
1125 }
1126
1127 if (intel_rb && dPriv->vblFlags &&
1128 !(dPriv->vblFlags & VBLANK_FLAG_NO_IRQ) &&
1129 (intel_fb->vbl_waited - intel_rb->vbl_pending) > (1<<23)) {
1130 drmVBlank vbl;
1131
1132 vbl.request.type = DRM_VBLANK_ABSOLUTE;
1133
1134 if ( dPriv->vblFlags & VBLANK_FLAG_SECONDARY ) {
1135 vbl.request.type |= DRM_VBLANK_SECONDARY;
1136 }
1137
1138 vbl.request.sequence = intel_rb->vbl_pending;
1139 drmWaitVBlank(intel->driFd, &vbl);
1140 intel_fb->vbl_waited = vbl.reply.sequence;
1141 }
1142
1143 if (!sPriv->dri2.enabled) {
1144 DRM_CAS(intel->driHwLock, intel->hHWContext,
1145 (DRM_LOCK_HELD|intel->hHWContext), __ret);
1146
1147 if (__ret)
1148 intelContendedLock( intel, 0 );
1149 }
1150
1151
1152 if (INTEL_DEBUG & DEBUG_LOCK)
1153 _mesa_printf("%s - locked\n", __progname);
1154 }
1155
1156
1157 /* Unlock the hardware using the global current context
1158 */
1159 void UNLOCK_HARDWARE( struct intel_context *intel )
1160 {
1161 __DRIscreen *sPriv = intel->driScreen;
1162
1163 intel->locked--;
1164 if (intel->locked > 0)
1165 return;
1166
1167 assert(intel->locked == 0);
1168
1169 if (!sPriv->dri2.enabled) {
1170 DRM_UNLOCK(intel->driFd, intel->driHwLock, intel->hHWContext);
1171 _glthread_UNLOCK_MUTEX(lockMutex);
1172 }
1173
1174 if (INTEL_DEBUG & DEBUG_LOCK)
1175 _mesa_printf("%s - unlocked\n", __progname);
1176
1177 /**
1178 * Nothing should be left in batch outside of LOCK/UNLOCK which references
1179 * cliprects.
1180 */
1181 if (intel->batch->cliprect_mode == REFERENCES_CLIPRECTS)
1182 intel_batchbuffer_flush(intel->batch);
1183 }
1184