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