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