Merge branch 'mesa_7_7_branch'
[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/extensions.h"
32 #include "main/framebuffer.h"
33 #include "main/imports.h"
34 #include "main/points.h"
35
36 #include "swrast/swrast.h"
37 #include "swrast_setup/swrast_setup.h"
38 #include "tnl/tnl.h"
39 #include "drivers/common/driverfuncs.h"
40 #include "drivers/common/meta.h"
41
42 #include "i830_dri.h"
43
44 #include "intel_chipset.h"
45 #include "intel_buffers.h"
46 #include "intel_tex.h"
47 #include "intel_batchbuffer.h"
48 #include "intel_clear.h"
49 #include "intel_extensions.h"
50 #include "intel_pixel.h"
51 #include "intel_regions.h"
52 #include "intel_buffer_objects.h"
53 #include "intel_fbo.h"
54 #include "intel_decode.h"
55 #include "intel_bufmgr.h"
56 #include "intel_screen.h"
57
58 #include "drirenderbuffer.h"
59 #include "utils.h"
60 #include "xmlpool.h" /* for symbolic values of enum-type options */
61
62
63 #ifndef INTEL_DEBUG
64 int INTEL_DEBUG = (0);
65 #endif
66
67
68 #define DRIVER_DATE "20091221 DEVELOPMENT"
69 #define DRIVER_DATE_GEM "GEM " DRIVER_DATE
70
71
72 static const GLubyte *
73 intelGetString(GLcontext * ctx, GLenum name)
74 {
75 const struct intel_context *const intel = intel_context(ctx);
76 const char *chipset;
77 static char buffer[128];
78
79 switch (name) {
80 case GL_VENDOR:
81 return (GLubyte *) "Tungsten Graphics, Inc";
82 break;
83
84 case GL_RENDERER:
85 switch (intel->intelScreen->deviceID) {
86 case PCI_CHIP_845_G:
87 chipset = "Intel(R) 845G";
88 break;
89 case PCI_CHIP_I830_M:
90 chipset = "Intel(R) 830M";
91 break;
92 case PCI_CHIP_I855_GM:
93 chipset = "Intel(R) 852GM/855GM";
94 break;
95 case PCI_CHIP_I865_G:
96 chipset = "Intel(R) 865G";
97 break;
98 case PCI_CHIP_I915_G:
99 chipset = "Intel(R) 915G";
100 break;
101 case PCI_CHIP_E7221_G:
102 chipset = "Intel (R) E7221G (i915)";
103 break;
104 case PCI_CHIP_I915_GM:
105 chipset = "Intel(R) 915GM";
106 break;
107 case PCI_CHIP_I945_G:
108 chipset = "Intel(R) 945G";
109 break;
110 case PCI_CHIP_I945_GM:
111 chipset = "Intel(R) 945GM";
112 break;
113 case PCI_CHIP_I945_GME:
114 chipset = "Intel(R) 945GME";
115 break;
116 case PCI_CHIP_G33_G:
117 chipset = "Intel(R) G33";
118 break;
119 case PCI_CHIP_Q35_G:
120 chipset = "Intel(R) Q35";
121 break;
122 case PCI_CHIP_Q33_G:
123 chipset = "Intel(R) Q33";
124 break;
125 case PCI_CHIP_IGD_GM:
126 case PCI_CHIP_IGD_G:
127 chipset = "Intel(R) IGD";
128 break;
129 case PCI_CHIP_I965_Q:
130 chipset = "Intel(R) 965Q";
131 break;
132 case PCI_CHIP_I965_G:
133 case PCI_CHIP_I965_G_1:
134 chipset = "Intel(R) 965G";
135 break;
136 case PCI_CHIP_I946_GZ:
137 chipset = "Intel(R) 946GZ";
138 break;
139 case PCI_CHIP_I965_GM:
140 chipset = "Intel(R) 965GM";
141 break;
142 case PCI_CHIP_I965_GME:
143 chipset = "Intel(R) 965GME/GLE";
144 break;
145 case PCI_CHIP_GM45_GM:
146 chipset = "Mobile IntelĀ® GM45 Express Chipset";
147 break;
148 case PCI_CHIP_IGD_E_G:
149 chipset = "Intel(R) Integrated Graphics Device";
150 break;
151 case PCI_CHIP_G45_G:
152 chipset = "Intel(R) G45/G43";
153 break;
154 case PCI_CHIP_Q45_G:
155 chipset = "Intel(R) Q45/Q43";
156 break;
157 case PCI_CHIP_G41_G:
158 chipset = "Intel(R) G41";
159 break;
160 case PCI_CHIP_B43_G:
161 chipset = "Intel(R) B43";
162 break;
163 case PCI_CHIP_ILD_G:
164 chipset = "Intel(R) IGDNG_D";
165 break;
166 case PCI_CHIP_ILM_G:
167 chipset = "Intel(R) IGDNG_M";
168 break;
169 default:
170 chipset = "Unknown Intel Chipset";
171 break;
172 }
173
174 (void) driGetRendererString(buffer, chipset, DRIVER_DATE_GEM, 0);
175 return (GLubyte *) buffer;
176
177 default:
178 return NULL;
179 }
180 }
181
182 static unsigned
183 intel_bits_per_pixel(const struct intel_renderbuffer *rb)
184 {
185 return _mesa_get_format_bytes(rb->Base.Format) * 8;
186 }
187
188 void
189 intel_update_renderbuffers(__DRIcontext *context, __DRIdrawable *drawable)
190 {
191 struct gl_framebuffer *fb = drawable->driverPrivate;
192 struct intel_renderbuffer *rb;
193 struct intel_region *region, *depth_region;
194 struct intel_context *intel = context->driverPrivate;
195 struct intel_renderbuffer *front_rb, *back_rb, *depth_rb, *stencil_rb;
196 __DRIbuffer *buffers = NULL;
197 __DRIscreen *screen;
198 int i, count;
199 unsigned int attachments[10];
200 uint32_t name;
201 const char *region_name;
202
203 if (INTEL_DEBUG & DEBUG_DRI)
204 fprintf(stderr, "enter %s, drawable %p\n", __func__, drawable);
205
206 screen = intel->intelScreen->driScrnPriv;
207
208 if (screen->dri2.loader
209 && (screen->dri2.loader->base.version > 2)
210 && (screen->dri2.loader->getBuffersWithFormat != NULL)) {
211
212 front_rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
213 back_rb = intel_get_renderbuffer(fb, BUFFER_BACK_LEFT);
214 depth_rb = intel_get_renderbuffer(fb, BUFFER_DEPTH);
215 stencil_rb = intel_get_renderbuffer(fb, BUFFER_STENCIL);
216
217 i = 0;
218 if ((intel->is_front_buffer_rendering ||
219 intel->is_front_buffer_reading ||
220 !back_rb) && front_rb) {
221 attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
222 attachments[i++] = intel_bits_per_pixel(front_rb);
223 }
224
225 if (back_rb) {
226 attachments[i++] = __DRI_BUFFER_BACK_LEFT;
227 attachments[i++] = intel_bits_per_pixel(back_rb);
228 }
229
230 if ((depth_rb != NULL) && (stencil_rb != NULL)) {
231 attachments[i++] = __DRI_BUFFER_DEPTH_STENCIL;
232 attachments[i++] = intel_bits_per_pixel(depth_rb);
233 } else if (depth_rb != NULL) {
234 attachments[i++] = __DRI_BUFFER_DEPTH;
235 attachments[i++] = intel_bits_per_pixel(depth_rb);
236 } else if (stencil_rb != NULL) {
237 attachments[i++] = __DRI_BUFFER_STENCIL;
238 attachments[i++] = intel_bits_per_pixel(stencil_rb);
239 }
240
241 buffers =
242 (*screen->dri2.loader->getBuffersWithFormat)(drawable,
243 &drawable->w,
244 &drawable->h,
245 attachments, i / 2,
246 &count,
247 drawable->loaderPrivate);
248 } else if (screen->dri2.loader) {
249 i = 0;
250 if (intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT))
251 attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
252 if (intel_get_renderbuffer(fb, BUFFER_BACK_LEFT))
253 attachments[i++] = __DRI_BUFFER_BACK_LEFT;
254 if (intel_get_renderbuffer(fb, BUFFER_DEPTH))
255 attachments[i++] = __DRI_BUFFER_DEPTH;
256 if (intel_get_renderbuffer(fb, BUFFER_STENCIL))
257 attachments[i++] = __DRI_BUFFER_STENCIL;
258
259 buffers = (*screen->dri2.loader->getBuffers)(drawable,
260 &drawable->w,
261 &drawable->h,
262 attachments, i,
263 &count,
264 drawable->loaderPrivate);
265 }
266
267 if (buffers == NULL)
268 return;
269
270 drawable->x = 0;
271 drawable->y = 0;
272 drawable->backX = 0;
273 drawable->backY = 0;
274 drawable->numClipRects = 1;
275 drawable->pClipRects[0].x1 = 0;
276 drawable->pClipRects[0].y1 = 0;
277 drawable->pClipRects[0].x2 = drawable->w;
278 drawable->pClipRects[0].y2 = drawable->h;
279 drawable->numBackClipRects = 1;
280 drawable->pBackClipRects[0].x1 = 0;
281 drawable->pBackClipRects[0].y1 = 0;
282 drawable->pBackClipRects[0].x2 = drawable->w;
283 drawable->pBackClipRects[0].y2 = drawable->h;
284
285 depth_region = NULL;
286 for (i = 0; i < count; i++) {
287 switch (buffers[i].attachment) {
288 case __DRI_BUFFER_FRONT_LEFT:
289 rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
290 region_name = "dri2 front buffer";
291 break;
292
293 case __DRI_BUFFER_FAKE_FRONT_LEFT:
294 rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
295 region_name = "dri2 fake front buffer";
296 break;
297
298 case __DRI_BUFFER_BACK_LEFT:
299 rb = intel_get_renderbuffer(fb, BUFFER_BACK_LEFT);
300 region_name = "dri2 back buffer";
301 break;
302
303 case __DRI_BUFFER_DEPTH:
304 rb = intel_get_renderbuffer(fb, BUFFER_DEPTH);
305 region_name = "dri2 depth buffer";
306 break;
307
308 case __DRI_BUFFER_DEPTH_STENCIL:
309 rb = intel_get_renderbuffer(fb, BUFFER_DEPTH);
310 region_name = "dri2 depth / stencil buffer";
311 break;
312
313 case __DRI_BUFFER_STENCIL:
314 rb = intel_get_renderbuffer(fb, BUFFER_STENCIL);
315 region_name = "dri2 stencil buffer";
316 break;
317
318 case __DRI_BUFFER_ACCUM:
319 default:
320 fprintf(stderr,
321 "unhandled buffer attach event, attacment type %d\n",
322 buffers[i].attachment);
323 return;
324 }
325
326 if (rb == NULL)
327 continue;
328
329 if (rb->region) {
330 dri_bo_flink(rb->region->buffer, &name);
331 if (name == buffers[i].name)
332 continue;
333 }
334
335 if (INTEL_DEBUG & DEBUG_DRI)
336 fprintf(stderr,
337 "attaching buffer %d, at %d, cpp %d, pitch %d\n",
338 buffers[i].name, buffers[i].attachment,
339 buffers[i].cpp, buffers[i].pitch);
340
341 if (buffers[i].attachment == __DRI_BUFFER_STENCIL && depth_region) {
342 if (INTEL_DEBUG & DEBUG_DRI)
343 fprintf(stderr, "(reusing depth buffer as stencil)\n");
344 intel_region_reference(&region, depth_region);
345 }
346 else
347 region = intel_region_alloc_for_handle(intel, buffers[i].cpp,
348 drawable->w,
349 drawable->h,
350 buffers[i].pitch / buffers[i].cpp,
351 buffers[i].name,
352 region_name);
353
354 if (buffers[i].attachment == __DRI_BUFFER_DEPTH)
355 depth_region = region;
356
357 intel_renderbuffer_set_region(rb, region);
358 intel_region_release(&region);
359
360 if (buffers[i].attachment == __DRI_BUFFER_DEPTH_STENCIL) {
361 rb = intel_get_renderbuffer(fb, BUFFER_STENCIL);
362 if (rb != NULL) {
363 struct intel_region *stencil_region = NULL;
364
365 if (rb->region) {
366 dri_bo_flink(rb->region->buffer, &name);
367 if (name == buffers[i].name)
368 continue;
369 }
370
371 intel_region_reference(&stencil_region, region);
372 intel_renderbuffer_set_region(rb, stencil_region);
373 intel_region_release(&stencil_region);
374 }
375 }
376 }
377
378 drawable->validBuffers = GL_TRUE;
379 driUpdateFramebufferSize(&intel->ctx, drawable);
380 }
381
382 void
383 intel_viewport(GLcontext *ctx, GLint x, GLint y, GLsizei w, GLsizei h)
384 {
385 struct intel_context *intel = intel_context(ctx);
386 __DRIcontext *driContext = intel->driContext;
387 void (*old_viewport)(GLcontext *ctx, GLint x, GLint y,
388 GLsizei w, GLsizei h);
389
390 if (!intel->meta.internal_viewport_call && ctx->DrawBuffer->Name == 0) {
391 /* If we're rendering to the fake front buffer, make sure all the pending
392 * drawing has landed on the real front buffer. Otherwise when we
393 * eventually get to DRI2GetBuffersWithFormat the stale real front
394 * buffer contents will get copied to the new fake front buffer.
395 */
396 if (intel->is_front_buffer_rendering) {
397 intel_flush(ctx, GL_FALSE);
398 }
399
400 intel_update_renderbuffers(driContext, driContext->driDrawablePriv);
401 if (driContext->driDrawablePriv != driContext->driReadablePriv)
402 intel_update_renderbuffers(driContext, driContext->driReadablePriv);
403 }
404
405 old_viewport = ctx->Driver.Viewport;
406 ctx->Driver.Viewport = NULL;
407 intel->driDrawable = driContext->driDrawablePriv;
408 intel_draw_buffer(ctx, intel->ctx.DrawBuffer);
409 ctx->Driver.Viewport = old_viewport;
410 }
411
412
413 static const struct dri_debug_control debug_control[] = {
414 { "tex", DEBUG_TEXTURE},
415 { "state", DEBUG_STATE},
416 { "ioctl", DEBUG_IOCTL},
417 { "blit", DEBUG_BLIT},
418 { "mip", DEBUG_MIPTREE},
419 { "fall", DEBUG_FALLBACKS},
420 { "verb", DEBUG_VERBOSE},
421 { "bat", DEBUG_BATCH},
422 { "pix", DEBUG_PIXEL},
423 { "buf", DEBUG_BUFMGR},
424 { "reg", DEBUG_REGION},
425 { "fbo", DEBUG_FBO},
426 { "lock", DEBUG_LOCK},
427 { "sync", DEBUG_SYNC},
428 { "prim", DEBUG_PRIMS },
429 { "vert", DEBUG_VERTS },
430 { "dri", DEBUG_DRI },
431 { "dma", DEBUG_DMA },
432 { "san", DEBUG_SANITY },
433 { "sleep", DEBUG_SLEEP },
434 { "stats", DEBUG_STATS },
435 { "tile", DEBUG_TILE },
436 { "sing", DEBUG_SINGLE_THREAD },
437 { "thre", DEBUG_SINGLE_THREAD },
438 { "wm", DEBUG_WM },
439 { "urb", DEBUG_URB },
440 { "vs", DEBUG_VS },
441 { NULL, 0 }
442 };
443
444
445 static void
446 intelInvalidateState(GLcontext * ctx, GLuint new_state)
447 {
448 struct intel_context *intel = intel_context(ctx);
449
450 _swrast_InvalidateState(ctx, new_state);
451 _swsetup_InvalidateState(ctx, new_state);
452 _vbo_InvalidateState(ctx, new_state);
453 _tnl_InvalidateState(ctx, new_state);
454 _tnl_invalidate_vertex_state(ctx, new_state);
455
456 intel->NewGLState |= new_state;
457
458 if (intel->vtbl.invalidate_state)
459 intel->vtbl.invalidate_state( intel, new_state );
460 }
461
462 void
463 intel_flush(GLcontext *ctx, GLboolean needs_mi_flush)
464 {
465 struct intel_context *intel = intel_context(ctx);
466
467 if (intel->Fallback)
468 _swrast_flush(ctx);
469
470 if (intel->gen < 4)
471 INTEL_FIREVERTICES(intel);
472
473 if (intel->batch->map != intel->batch->ptr)
474 intel_batchbuffer_flush(intel->batch);
475
476 if ((ctx->DrawBuffer->Name == 0) && intel->front_buffer_dirty) {
477 __DRIscreen *const screen = intel->intelScreen->driScrnPriv;
478
479 if (screen->dri2.loader &&
480 (screen->dri2.loader->base.version >= 2)
481 && (screen->dri2.loader->flushFrontBuffer != NULL) &&
482 intel->driDrawable && intel->driDrawable->loaderPrivate) {
483 (*screen->dri2.loader->flushFrontBuffer)(intel->driDrawable,
484 intel->driDrawable->loaderPrivate);
485
486 /* Only clear the dirty bit if front-buffer rendering is no longer
487 * enabled. This is done so that the dirty bit can only be set in
488 * glDrawBuffer. Otherwise the dirty bit would have to be set at
489 * each of N places that do rendering. This has worse performances,
490 * but it is much easier to get correct.
491 */
492 if (!intel->is_front_buffer_rendering) {
493 intel->front_buffer_dirty = GL_FALSE;
494 }
495 }
496 }
497 }
498
499 void
500 intelFlush(GLcontext * ctx)
501 {
502 intel_flush(ctx, GL_FALSE);
503 }
504
505 static void
506 intel_glFlush(GLcontext *ctx)
507 {
508 struct intel_context *intel = intel_context(ctx);
509
510 intel_flush(ctx, GL_TRUE);
511
512 /* We're using glFlush as an indicator that a frame is done, which is
513 * what DRI2 does before calling SwapBuffers (and means we should catch
514 * people doing front-buffer rendering, as well)..
515 *
516 * Wait for the swapbuffers before the one we just emitted, so we don't
517 * get too many swaps outstanding for apps that are GPU-heavy but not
518 * CPU-heavy.
519 *
520 * Unfortunately, we don't have a handle to the batch containing the swap,
521 * and getting our hands on that doesn't seem worth it, so we just us the
522 * first batch we emitted after the last swap.
523 */
524 if (!intel->using_dri2_swapbuffers &&
525 intel->first_post_swapbuffers_batch != NULL) {
526 drm_intel_bo_wait_rendering(intel->first_post_swapbuffers_batch);
527 drm_intel_bo_unreference(intel->first_post_swapbuffers_batch);
528 intel->first_post_swapbuffers_batch = NULL;
529 }
530 }
531
532 void
533 intelFinish(GLcontext * ctx)
534 {
535 struct gl_framebuffer *fb = ctx->DrawBuffer;
536 int i;
537
538 intelFlush(ctx);
539
540 for (i = 0; i < fb->_NumColorDrawBuffers; i++) {
541 struct intel_renderbuffer *irb;
542
543 irb = intel_renderbuffer(fb->_ColorDrawBuffers[i]);
544
545 if (irb && irb->region)
546 dri_bo_wait_rendering(irb->region->buffer);
547 }
548 if (fb->_DepthBuffer) {
549 /* XXX: Wait on buffer idle */
550 }
551 }
552
553 void
554 intelInitDriverFunctions(struct dd_function_table *functions)
555 {
556 _mesa_init_driver_functions(functions);
557
558 functions->Flush = intel_glFlush;
559 functions->Finish = intelFinish;
560 functions->GetString = intelGetString;
561 functions->UpdateState = intelInvalidateState;
562
563 intelInitTextureFuncs(functions);
564 intelInitTextureImageFuncs(functions);
565 intelInitTextureSubImageFuncs(functions);
566 intelInitTextureCopyImageFuncs(functions);
567 intelInitStateFuncs(functions);
568 intelInitClearFuncs(functions);
569 intelInitBufferFuncs(functions);
570 intelInitPixelFuncs(functions);
571 intelInitBufferObjectFuncs(functions);
572 intel_init_syncobj_functions(functions);
573 }
574
575
576 GLboolean
577 intelInitContext(struct intel_context *intel,
578 const __GLcontextModes * mesaVis,
579 __DRIcontext * driContextPriv,
580 void *sharedContextPrivate,
581 struct dd_function_table *functions)
582 {
583 GLcontext *ctx = &intel->ctx;
584 GLcontext *shareCtx = (GLcontext *) sharedContextPrivate;
585 __DRIscreen *sPriv = driContextPriv->driScreenPriv;
586 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
587 int bo_reuse_mode;
588
589 if (!_mesa_initialize_context(&intel->ctx, mesaVis, shareCtx,
590 functions, (void *) intel)) {
591 _mesa_printf("%s: failed to init mesa context\n", __FUNCTION__);
592 return GL_FALSE;
593 }
594
595 driContextPriv->driverPrivate = intel;
596 intel->intelScreen = intelScreen;
597 intel->driScreen = sPriv;
598 intel->driContext = driContextPriv;
599 intel->driFd = sPriv->fd;
600
601 if (IS_965(intel->intelScreen->deviceID)) {
602 intel->gen = 4;
603 } else if (IS_9XX(intel->intelScreen->deviceID)) {
604 intel->gen = 3;
605 if (IS_945(intel->intelScreen->deviceID)) {
606 intel->is_945 = GL_TRUE;
607 }
608 } else {
609 intel->gen = 2;
610 }
611
612 if (IS_IGDNG(intel->intelScreen->deviceID)) {
613 intel->is_ironlake = GL_TRUE;
614 intel->needs_ff_sync = GL_TRUE;
615 intel->has_luminance_srgb = GL_TRUE;
616 } else if (IS_G4X(intel->intelScreen->deviceID)) {
617 intel->has_luminance_srgb = GL_TRUE;
618 intel->is_g4x = GL_TRUE;
619 }
620
621 driParseConfigFiles(&intel->optionCache, &intelScreen->optionCache,
622 intel->driScreen->myNum,
623 (intel->gen >= 4) ? "i965" : "i915");
624 if (intelScreen->deviceID == PCI_CHIP_I865_G)
625 intel->maxBatchSize = 4096;
626 else
627 intel->maxBatchSize = BATCH_SZ;
628
629 intel->bufmgr = intelScreen->bufmgr;
630
631 bo_reuse_mode = driQueryOptioni(&intel->optionCache, "bo_reuse");
632 switch (bo_reuse_mode) {
633 case DRI_CONF_BO_REUSE_DISABLED:
634 break;
635 case DRI_CONF_BO_REUSE_ALL:
636 intel_bufmgr_gem_enable_reuse(intel->bufmgr);
637 break;
638 }
639
640 /* This doesn't yet catch all non-conformant rendering, but it's a
641 * start.
642 */
643 if (getenv("INTEL_STRICT_CONFORMANCE")) {
644 unsigned int value = atoi(getenv("INTEL_STRICT_CONFORMANCE"));
645 if (value > 0) {
646 intel->conformance_mode = value;
647 }
648 else {
649 intel->conformance_mode = 1;
650 }
651 }
652
653 if (intel->conformance_mode > 0) {
654 ctx->Const.MinLineWidth = 1.0;
655 ctx->Const.MinLineWidthAA = 1.0;
656 ctx->Const.MaxLineWidth = 1.0;
657 ctx->Const.MaxLineWidthAA = 1.0;
658 ctx->Const.LineWidthGranularity = 1.0;
659 }
660 else {
661 ctx->Const.MinLineWidth = 1.0;
662 ctx->Const.MinLineWidthAA = 1.0;
663 ctx->Const.MaxLineWidth = 5.0;
664 ctx->Const.MaxLineWidthAA = 5.0;
665 ctx->Const.LineWidthGranularity = 0.5;
666 }
667
668 ctx->Const.MinPointSize = 1.0;
669 ctx->Const.MinPointSizeAA = 1.0;
670 ctx->Const.MaxPointSize = 255.0;
671 ctx->Const.MaxPointSizeAA = 3.0;
672 ctx->Const.PointSizeGranularity = 1.0;
673
674 /* reinitialize the context point state.
675 * It depend on constants in __GLcontextRec::Const
676 */
677 _mesa_init_point(ctx);
678
679 meta_init_metaops(ctx, &intel->meta);
680 ctx->Const.MaxColorAttachments = 4; /* XXX FBO: review this */
681 if (intel->gen >= 4) {
682 if (MAX_WIDTH > 8192)
683 ctx->Const.MaxRenderbufferSize = 8192;
684 } else {
685 if (MAX_WIDTH > 2048)
686 ctx->Const.MaxRenderbufferSize = 2048;
687 }
688
689 /* Initialize the software rasterizer and helper modules. */
690 _swrast_CreateContext(ctx);
691 _vbo_CreateContext(ctx);
692 _tnl_CreateContext(ctx);
693 _swsetup_CreateContext(ctx);
694
695 /* Configure swrast to match hardware characteristics: */
696 _swrast_allow_pixel_fog(ctx, GL_FALSE);
697 _swrast_allow_vertex_fog(ctx, GL_TRUE);
698
699 _mesa_meta_init(ctx);
700
701 intel->hw_stencil = mesaVis->stencilBits && mesaVis->depthBits == 24;
702 intel->hw_stipple = 1;
703
704 /* XXX FBO: this doesn't seem to be used anywhere */
705 switch (mesaVis->depthBits) {
706 case 0: /* what to do in this case? */
707 case 16:
708 intel->polygon_offset_scale = 1.0;
709 break;
710 case 24:
711 intel->polygon_offset_scale = 2.0; /* req'd to pass glean */
712 break;
713 default:
714 assert(0);
715 break;
716 }
717
718 if (intel->gen >= 4)
719 intel->polygon_offset_scale /= 0xffff;
720
721 intel->RenderIndex = ~0;
722
723 if (intel->gen >= 4 && !intel->intelScreen->irq_active) {
724 _mesa_printf("IRQs not active. Exiting\n");
725 exit(1);
726 }
727
728 intelInitExtensions(ctx);
729
730 INTEL_DEBUG = driParseDebugString(getenv("INTEL_DEBUG"), debug_control);
731 if (INTEL_DEBUG & DEBUG_BUFMGR)
732 dri_bufmgr_set_debug(intel->bufmgr, GL_TRUE);
733
734 intel->batch = intel_batchbuffer_alloc(intel);
735
736 intel_fbo_init(intel);
737
738 if (intel->ctx.Mesa_DXTn) {
739 _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
740 _mesa_enable_extension(ctx, "GL_S3_s3tc");
741 }
742 else if (driQueryOptionb(&intel->optionCache, "force_s3tc_enable")) {
743 _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
744 }
745 intel->use_texture_tiling = driQueryOptionb(&intel->optionCache,
746 "texture_tiling");
747 if (intel->use_texture_tiling &&
748 !intel->intelScreen->kernel_exec_fencing) {
749 fprintf(stderr, "No kernel support for execution fencing, "
750 "disabling texture tiling\n");
751 intel->use_texture_tiling = GL_FALSE;
752 }
753 intel->use_early_z = driQueryOptionb(&intel->optionCache, "early_z");
754
755 intel->prim.primitive = ~0;
756
757 /* Force all software fallbacks */
758 if (driQueryOptionb(&intel->optionCache, "no_rast")) {
759 fprintf(stderr, "disabling 3D rasterization\n");
760 intel->no_rast = 1;
761 }
762
763 if (driQueryOptionb(&intel->optionCache, "always_flush_batch")) {
764 fprintf(stderr, "flushing batchbuffer before/after each draw call\n");
765 intel->always_flush_batch = 1;
766 }
767
768 if (driQueryOptionb(&intel->optionCache, "always_flush_cache")) {
769 fprintf(stderr, "flushing GPU caches before/after each draw call\n");
770 intel->always_flush_cache = 1;
771 }
772
773 /* Disable all hardware rendering (skip emitting batches and fences/waits
774 * to the kernel)
775 */
776 intel->no_hw = getenv("INTEL_NO_HW") != NULL;
777
778 return GL_TRUE;
779 }
780
781 void
782 intelDestroyContext(__DRIcontext * driContextPriv)
783 {
784 struct intel_context *intel =
785 (struct intel_context *) driContextPriv->driverPrivate;
786
787 assert(intel); /* should never be null */
788 if (intel) {
789 GLboolean release_texture_heaps;
790
791 INTEL_FIREVERTICES(intel);
792
793 _mesa_meta_free(&intel->ctx);
794
795 meta_destroy_metaops(&intel->meta);
796
797 intel->vtbl.destroy(intel);
798
799 release_texture_heaps = (intel->ctx.Shared->RefCount == 1);
800 _swsetup_DestroyContext(&intel->ctx);
801 _tnl_DestroyContext(&intel->ctx);
802 _vbo_DestroyContext(&intel->ctx);
803
804 _swrast_DestroyContext(&intel->ctx);
805 intel->Fallback = 0x0; /* don't call _swrast_Flush later */
806
807 intel_batchbuffer_free(intel->batch);
808 intel->batch = NULL;
809
810 free(intel->prim.vb);
811 intel->prim.vb = NULL;
812 dri_bo_unreference(intel->prim.vb_bo);
813 intel->prim.vb_bo = NULL;
814 dri_bo_unreference(intel->first_post_swapbuffers_batch);
815 intel->first_post_swapbuffers_batch = NULL;
816
817 if (release_texture_heaps) {
818 /* Nothing is currently done here to free texture heaps;
819 * but we're not using the texture heap utilities, so I
820 * rather think we shouldn't. I've taken a look, and can't
821 * find any private texture data hanging around anywhere, but
822 * I'm not yet certain there isn't any at all...
823 */
824 /* if (INTEL_DEBUG & DEBUG_TEXTURE)
825 fprintf(stderr, "do something to free texture heaps\n");
826 */
827 }
828
829 driDestroyOptionCache(&intel->optionCache);
830
831 /* free the Mesa context */
832 _mesa_free_context_data(&intel->ctx);
833
834 FREE(intel);
835 driContextPriv->driverPrivate = NULL;
836 }
837 }
838
839 GLboolean
840 intelUnbindContext(__DRIcontext * driContextPriv)
841 {
842 struct intel_context *intel =
843 (struct intel_context *) driContextPriv->driverPrivate;
844
845 /* Deassociate the context with the drawables.
846 */
847 intel->driDrawable = NULL;
848 intel->driReadDrawable = NULL;
849
850 return GL_TRUE;
851 }
852
853 GLboolean
854 intelMakeCurrent(__DRIcontext * driContextPriv,
855 __DRIdrawable * driDrawPriv,
856 __DRIdrawable * driReadPriv)
857 {
858 struct intel_context *intel;
859 GET_CURRENT_CONTEXT(curCtx);
860
861 if (driContextPriv)
862 intel = (struct intel_context *) driContextPriv->driverPrivate;
863 else
864 intel = NULL;
865
866 /* According to the glXMakeCurrent() man page: "Pending commands to
867 * the previous context, if any, are flushed before it is released."
868 * But only flush if we're actually changing contexts.
869 */
870 if (intel_context(curCtx) && intel_context(curCtx) != intel) {
871 _mesa_flush(curCtx);
872 }
873
874 if (driContextPriv) {
875 struct gl_framebuffer *fb = driDrawPriv->driverPrivate;
876 struct gl_framebuffer *readFb = driReadPriv->driverPrivate;
877
878 intel_update_renderbuffers(driContextPriv, driDrawPriv);
879 if (driDrawPriv != driReadPriv)
880 intel_update_renderbuffers(driContextPriv, driReadPriv);
881
882 /* set GLframebuffer size to match window, if needed */
883 driUpdateFramebufferSize(&intel->ctx, driDrawPriv);
884
885 if (driReadPriv != driDrawPriv) {
886 driUpdateFramebufferSize(&intel->ctx, driReadPriv);
887 }
888
889 _mesa_make_current(&intel->ctx, fb, readFb);
890 intel->driReadDrawable = driReadPriv;
891 intel->driDrawable = driDrawPriv;
892 intel_draw_buffer(&intel->ctx, fb);
893 }
894 else {
895 _mesa_make_current(NULL, NULL, NULL);
896 }
897
898 return GL_TRUE;
899 }