Merge branch 'instanced-arrays'
[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 const GLubyte *
74 intelGetString(GLcontext * ctx, GLenum name)
75 {
76 const struct intel_context *const intel = intel_context(ctx);
77 const char *chipset;
78 static char buffer[128];
79
80 switch (name) {
81 case GL_VENDOR:
82 return (GLubyte *) "Tungsten Graphics, Inc";
83 break;
84
85 case GL_RENDERER:
86 switch (intel->intelScreen->deviceID) {
87 case PCI_CHIP_845_G:
88 chipset = "Intel(R) 845G";
89 break;
90 case PCI_CHIP_I830_M:
91 chipset = "Intel(R) 830M";
92 break;
93 case PCI_CHIP_I855_GM:
94 chipset = "Intel(R) 852GM/855GM";
95 break;
96 case PCI_CHIP_I865_G:
97 chipset = "Intel(R) 865G";
98 break;
99 case PCI_CHIP_I915_G:
100 chipset = "Intel(R) 915G";
101 break;
102 case PCI_CHIP_E7221_G:
103 chipset = "Intel (R) E7221G (i915)";
104 break;
105 case PCI_CHIP_I915_GM:
106 chipset = "Intel(R) 915GM";
107 break;
108 case PCI_CHIP_I945_G:
109 chipset = "Intel(R) 945G";
110 break;
111 case PCI_CHIP_I945_GM:
112 chipset = "Intel(R) 945GM";
113 break;
114 case PCI_CHIP_I945_GME:
115 chipset = "Intel(R) 945GME";
116 break;
117 case PCI_CHIP_G33_G:
118 chipset = "Intel(R) G33";
119 break;
120 case PCI_CHIP_Q35_G:
121 chipset = "Intel(R) Q35";
122 break;
123 case PCI_CHIP_Q33_G:
124 chipset = "Intel(R) Q33";
125 break;
126 case PCI_CHIP_IGD_GM:
127 case PCI_CHIP_IGD_G:
128 chipset = "Intel(R) IGD";
129 break;
130 case PCI_CHIP_I965_Q:
131 chipset = "Intel(R) 965Q";
132 break;
133 case PCI_CHIP_I965_G:
134 case PCI_CHIP_I965_G_1:
135 chipset = "Intel(R) 965G";
136 break;
137 case PCI_CHIP_I946_GZ:
138 chipset = "Intel(R) 946GZ";
139 break;
140 case PCI_CHIP_I965_GM:
141 chipset = "Intel(R) 965GM";
142 break;
143 case PCI_CHIP_I965_GME:
144 chipset = "Intel(R) 965GME/GLE";
145 break;
146 case PCI_CHIP_GM45_GM:
147 chipset = "Mobile IntelĀ® GM45 Express Chipset";
148 break;
149 case PCI_CHIP_IGD_E_G:
150 chipset = "Intel(R) Integrated Graphics Device";
151 break;
152 case PCI_CHIP_G45_G:
153 chipset = "Intel(R) G45/G43";
154 break;
155 case PCI_CHIP_Q45_G:
156 chipset = "Intel(R) Q45/Q43";
157 break;
158 case PCI_CHIP_G41_G:
159 chipset = "Intel(R) G41";
160 break;
161 case PCI_CHIP_B43_G:
162 chipset = "Intel(R) B43";
163 break;
164 case PCI_CHIP_ILD_G:
165 chipset = "Intel(R) IGDNG_D";
166 break;
167 case PCI_CHIP_ILM_G:
168 chipset = "Intel(R) IGDNG_M";
169 break;
170 default:
171 chipset = "Unknown Intel Chipset";
172 break;
173 }
174
175 (void) driGetRendererString(buffer, chipset, DRIVER_DATE_GEM, 0);
176 return (GLubyte *) buffer;
177
178 default:
179 return NULL;
180 }
181 }
182
183 static unsigned
184 intel_bits_per_pixel(const struct intel_renderbuffer *rb)
185 {
186 return _mesa_get_format_bytes(rb->Base.Format) * 8;
187 }
188
189 void
190 intel_update_renderbuffers(__DRIcontext *context, __DRIdrawable *drawable)
191 {
192 struct gl_framebuffer *fb = drawable->driverPrivate;
193 struct intel_renderbuffer *rb;
194 struct intel_region *region, *depth_region;
195 struct intel_context *intel = context->driverPrivate;
196 struct intel_renderbuffer *front_rb, *back_rb, *depth_rb, *stencil_rb;
197 __DRIbuffer *buffers = NULL;
198 __DRIscreen *screen;
199 int i, count;
200 unsigned int attachments[10];
201 uint32_t name;
202 const char *region_name;
203
204 if (INTEL_DEBUG & DEBUG_DRI)
205 fprintf(stderr, "enter %s, drawable %p\n", __func__, drawable);
206
207 screen = intel->intelScreen->driScrnPriv;
208
209 if (screen->dri2.loader
210 && (screen->dri2.loader->base.version > 2)
211 && (screen->dri2.loader->getBuffersWithFormat != NULL)) {
212
213 front_rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
214 back_rb = intel_get_renderbuffer(fb, BUFFER_BACK_LEFT);
215 depth_rb = intel_get_renderbuffer(fb, BUFFER_DEPTH);
216 stencil_rb = intel_get_renderbuffer(fb, BUFFER_STENCIL);
217
218 i = 0;
219 if ((intel->is_front_buffer_rendering ||
220 intel->is_front_buffer_reading ||
221 !back_rb) && front_rb) {
222 attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
223 attachments[i++] = intel_bits_per_pixel(front_rb);
224 }
225
226 if (back_rb) {
227 attachments[i++] = __DRI_BUFFER_BACK_LEFT;
228 attachments[i++] = intel_bits_per_pixel(back_rb);
229 }
230
231 if ((depth_rb != NULL) && (stencil_rb != NULL)) {
232 attachments[i++] = __DRI_BUFFER_DEPTH_STENCIL;
233 attachments[i++] = intel_bits_per_pixel(depth_rb);
234 } else if (depth_rb != NULL) {
235 attachments[i++] = __DRI_BUFFER_DEPTH;
236 attachments[i++] = intel_bits_per_pixel(depth_rb);
237 } else if (stencil_rb != NULL) {
238 attachments[i++] = __DRI_BUFFER_STENCIL;
239 attachments[i++] = intel_bits_per_pixel(stencil_rb);
240 }
241
242 buffers =
243 (*screen->dri2.loader->getBuffersWithFormat)(drawable,
244 &drawable->w,
245 &drawable->h,
246 attachments, i / 2,
247 &count,
248 drawable->loaderPrivate);
249 } else if (screen->dri2.loader) {
250 i = 0;
251 if (intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT))
252 attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
253 if (intel_get_renderbuffer(fb, BUFFER_BACK_LEFT))
254 attachments[i++] = __DRI_BUFFER_BACK_LEFT;
255 if (intel_get_renderbuffer(fb, BUFFER_DEPTH))
256 attachments[i++] = __DRI_BUFFER_DEPTH;
257 if (intel_get_renderbuffer(fb, BUFFER_STENCIL))
258 attachments[i++] = __DRI_BUFFER_STENCIL;
259
260 buffers = (*screen->dri2.loader->getBuffers)(drawable,
261 &drawable->w,
262 &drawable->h,
263 attachments, i,
264 &count,
265 drawable->loaderPrivate);
266 }
267
268 if (buffers == NULL)
269 return;
270
271 drawable->x = 0;
272 drawable->y = 0;
273 drawable->backX = 0;
274 drawable->backY = 0;
275 drawable->numClipRects = 1;
276 drawable->pClipRects[0].x1 = 0;
277 drawable->pClipRects[0].y1 = 0;
278 drawable->pClipRects[0].x2 = drawable->w;
279 drawable->pClipRects[0].y2 = drawable->h;
280 drawable->numBackClipRects = 1;
281 drawable->pBackClipRects[0].x1 = 0;
282 drawable->pBackClipRects[0].y1 = 0;
283 drawable->pBackClipRects[0].x2 = drawable->w;
284 drawable->pBackClipRects[0].y2 = drawable->h;
285
286 depth_region = NULL;
287 for (i = 0; i < count; i++) {
288 switch (buffers[i].attachment) {
289 case __DRI_BUFFER_FRONT_LEFT:
290 rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
291 region_name = "dri2 front buffer";
292 break;
293
294 case __DRI_BUFFER_FAKE_FRONT_LEFT:
295 rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
296 region_name = "dri2 fake front buffer";
297 break;
298
299 case __DRI_BUFFER_BACK_LEFT:
300 rb = intel_get_renderbuffer(fb, BUFFER_BACK_LEFT);
301 region_name = "dri2 back buffer";
302 break;
303
304 case __DRI_BUFFER_DEPTH:
305 rb = intel_get_renderbuffer(fb, BUFFER_DEPTH);
306 region_name = "dri2 depth buffer";
307 break;
308
309 case __DRI_BUFFER_DEPTH_STENCIL:
310 rb = intel_get_renderbuffer(fb, BUFFER_DEPTH);
311 region_name = "dri2 depth / stencil buffer";
312 break;
313
314 case __DRI_BUFFER_STENCIL:
315 rb = intel_get_renderbuffer(fb, BUFFER_STENCIL);
316 region_name = "dri2 stencil buffer";
317 break;
318
319 case __DRI_BUFFER_ACCUM:
320 default:
321 fprintf(stderr,
322 "unhandled buffer attach event, attacment type %d\n",
323 buffers[i].attachment);
324 return;
325 }
326
327 if (rb == NULL)
328 continue;
329
330 if (rb->region) {
331 dri_bo_flink(rb->region->buffer, &name);
332 if (name == buffers[i].name)
333 continue;
334 }
335
336 if (INTEL_DEBUG & DEBUG_DRI)
337 fprintf(stderr,
338 "attaching buffer %d, at %d, cpp %d, pitch %d\n",
339 buffers[i].name, buffers[i].attachment,
340 buffers[i].cpp, buffers[i].pitch);
341
342 if (buffers[i].attachment == __DRI_BUFFER_STENCIL && depth_region) {
343 if (INTEL_DEBUG & DEBUG_DRI)
344 fprintf(stderr, "(reusing depth buffer as stencil)\n");
345 intel_region_reference(&region, depth_region);
346 }
347 else
348 region = intel_region_alloc_for_handle(intel, buffers[i].cpp,
349 drawable->w,
350 drawable->h,
351 buffers[i].pitch / buffers[i].cpp,
352 buffers[i].name,
353 region_name);
354
355 if (buffers[i].attachment == __DRI_BUFFER_DEPTH)
356 depth_region = region;
357
358 intel_renderbuffer_set_region(rb, region);
359 intel_region_release(&region);
360
361 if (buffers[i].attachment == __DRI_BUFFER_DEPTH_STENCIL) {
362 rb = intel_get_renderbuffer(fb, BUFFER_STENCIL);
363 if (rb != NULL) {
364 struct intel_region *stencil_region = NULL;
365
366 if (rb->region) {
367 dri_bo_flink(rb->region->buffer, &name);
368 if (name == buffers[i].name)
369 continue;
370 }
371
372 intel_region_reference(&stencil_region, region);
373 intel_renderbuffer_set_region(rb, stencil_region);
374 intel_region_release(&stencil_region);
375 }
376 }
377 }
378
379 drawable->validBuffers = GL_TRUE;
380 driUpdateFramebufferSize(&intel->ctx, drawable);
381 }
382
383 void
384 intel_viewport(GLcontext *ctx, GLint x, GLint y, GLsizei w, GLsizei h)
385 {
386 struct intel_context *intel = intel_context(ctx);
387 __DRIcontext *driContext = intel->driContext;
388 void (*old_viewport)(GLcontext *ctx, GLint x, GLint y,
389 GLsizei w, GLsizei h);
390
391 if (!intel->meta.internal_viewport_call && ctx->DrawBuffer->Name == 0) {
392 /* If we're rendering to the fake front buffer, make sure all the pending
393 * drawing has landed on the real front buffer. Otherwise when we
394 * eventually get to DRI2GetBuffersWithFormat the stale real front
395 * buffer contents will get copied to the new fake front buffer.
396 */
397 if (intel->is_front_buffer_rendering) {
398 intel_flush(ctx, GL_FALSE);
399 }
400
401 intel_update_renderbuffers(driContext, driContext->driDrawablePriv);
402 if (driContext->driDrawablePriv != driContext->driReadablePriv)
403 intel_update_renderbuffers(driContext, driContext->driReadablePriv);
404 }
405
406 old_viewport = ctx->Driver.Viewport;
407 ctx->Driver.Viewport = NULL;
408 intel->driDrawable = driContext->driDrawablePriv;
409 intel_draw_buffer(ctx, intel->ctx.DrawBuffer);
410 ctx->Driver.Viewport = old_viewport;
411 }
412
413
414 static const struct dri_debug_control debug_control[] = {
415 { "tex", DEBUG_TEXTURE},
416 { "state", DEBUG_STATE},
417 { "ioctl", DEBUG_IOCTL},
418 { "blit", DEBUG_BLIT},
419 { "mip", DEBUG_MIPTREE},
420 { "fall", DEBUG_FALLBACKS},
421 { "verb", DEBUG_VERBOSE},
422 { "bat", DEBUG_BATCH},
423 { "pix", DEBUG_PIXEL},
424 { "buf", DEBUG_BUFMGR},
425 { "reg", DEBUG_REGION},
426 { "fbo", DEBUG_FBO},
427 { "lock", DEBUG_LOCK},
428 { "sync", DEBUG_SYNC},
429 { "prim", DEBUG_PRIMS },
430 { "vert", DEBUG_VERTS },
431 { "dri", DEBUG_DRI },
432 { "dma", DEBUG_DMA },
433 { "san", DEBUG_SANITY },
434 { "sleep", DEBUG_SLEEP },
435 { "stats", DEBUG_STATS },
436 { "tile", DEBUG_TILE },
437 { "sing", DEBUG_SINGLE_THREAD },
438 { "thre", DEBUG_SINGLE_THREAD },
439 { "wm", DEBUG_WM },
440 { "urb", DEBUG_URB },
441 { "vs", DEBUG_VS },
442 { NULL, 0 }
443 };
444
445
446 static void
447 intelInvalidateState(GLcontext * ctx, GLuint new_state)
448 {
449 struct intel_context *intel = intel_context(ctx);
450
451 _swrast_InvalidateState(ctx, new_state);
452 _swsetup_InvalidateState(ctx, new_state);
453 _vbo_InvalidateState(ctx, new_state);
454 _tnl_InvalidateState(ctx, new_state);
455 _tnl_invalidate_vertex_state(ctx, new_state);
456
457 intel->NewGLState |= new_state;
458
459 if (intel->vtbl.invalidate_state)
460 intel->vtbl.invalidate_state( intel, new_state );
461 }
462
463 void
464 intel_flush(GLcontext *ctx, GLboolean needs_mi_flush)
465 {
466 struct intel_context *intel = intel_context(ctx);
467
468 if (intel->Fallback)
469 _swrast_flush(ctx);
470
471 if (intel->gen < 4)
472 INTEL_FIREVERTICES(intel);
473
474 if (intel->batch->map != intel->batch->ptr)
475 intel_batchbuffer_flush(intel->batch);
476
477 if ((ctx->DrawBuffer->Name == 0) && intel->front_buffer_dirty) {
478 __DRIscreen *const screen = intel->intelScreen->driScrnPriv;
479
480 if (screen->dri2.loader &&
481 (screen->dri2.loader->base.version >= 2)
482 && (screen->dri2.loader->flushFrontBuffer != NULL) &&
483 intel->driDrawable && intel->driDrawable->loaderPrivate) {
484 (*screen->dri2.loader->flushFrontBuffer)(intel->driDrawable,
485 intel->driDrawable->loaderPrivate);
486
487 /* Only clear the dirty bit if front-buffer rendering is no longer
488 * enabled. This is done so that the dirty bit can only be set in
489 * glDrawBuffer. Otherwise the dirty bit would have to be set at
490 * each of N places that do rendering. This has worse performances,
491 * but it is much easier to get correct.
492 */
493 if (!intel->is_front_buffer_rendering) {
494 intel->front_buffer_dirty = GL_FALSE;
495 }
496 }
497 }
498 }
499
500 void
501 intelFlush(GLcontext * ctx)
502 {
503 intel_flush(ctx, GL_FALSE);
504 }
505
506 static void
507 intel_glFlush(GLcontext *ctx)
508 {
509 struct intel_context *intel = intel_context(ctx);
510
511 intel_flush(ctx, GL_TRUE);
512
513 /* We're using glFlush as an indicator that a frame is done, which is
514 * what DRI2 does before calling SwapBuffers (and means we should catch
515 * people doing front-buffer rendering, as well)..
516 *
517 * Wait for the swapbuffers before the one we just emitted, so we don't
518 * get too many swaps outstanding for apps that are GPU-heavy but not
519 * CPU-heavy.
520 *
521 * Unfortunately, we don't have a handle to the batch containing the swap,
522 * and getting our hands on that doesn't seem worth it, so we just us the
523 * first batch we emitted after the last swap.
524 */
525 if (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 }