OSDN Git Service

intel: Add flags to intel_context for hiz and separate stencil
[android-x86/external-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/fbobject.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 "intel_chipset.h"
44 #include "intel_buffers.h"
45 #include "intel_tex.h"
46 #include "intel_batchbuffer.h"
47 #include "intel_clear.h"
48 #include "intel_extensions.h"
49 #include "intel_pixel.h"
50 #include "intel_regions.h"
51 #include "intel_buffer_objects.h"
52 #include "intel_fbo.h"
53 #include "intel_bufmgr.h"
54 #include "intel_screen.h"
55
56 #include "drirenderbuffer.h"
57 #include "utils.h"
58
59
60 #ifndef INTEL_DEBUG
61 int INTEL_DEBUG = (0);
62 #endif
63
64
65 static const GLubyte *
66 intelGetString(struct gl_context * ctx, GLenum name)
67 {
68    const struct intel_context *const intel = intel_context(ctx);
69    const char *chipset;
70    static char buffer[128];
71
72    switch (name) {
73    case GL_VENDOR:
74       return (GLubyte *) "Tungsten Graphics, Inc";
75       break;
76
77    case GL_RENDERER:
78       switch (intel->intelScreen->deviceID) {
79       case PCI_CHIP_845_G:
80          chipset = "Intel(R) 845G";
81          break;
82       case PCI_CHIP_I830_M:
83          chipset = "Intel(R) 830M";
84          break;
85       case PCI_CHIP_I855_GM:
86          chipset = "Intel(R) 852GM/855GM";
87          break;
88       case PCI_CHIP_I865_G:
89          chipset = "Intel(R) 865G";
90          break;
91       case PCI_CHIP_I915_G:
92          chipset = "Intel(R) 915G";
93          break;
94       case PCI_CHIP_E7221_G:
95          chipset = "Intel (R) E7221G (i915)";
96          break;
97       case PCI_CHIP_I915_GM:
98          chipset = "Intel(R) 915GM";
99          break;
100       case PCI_CHIP_I945_G:
101          chipset = "Intel(R) 945G";
102          break;
103       case PCI_CHIP_I945_GM:
104          chipset = "Intel(R) 945GM";
105          break;
106       case PCI_CHIP_I945_GME:
107          chipset = "Intel(R) 945GME";
108          break;
109       case PCI_CHIP_G33_G:
110          chipset = "Intel(R) G33";
111          break;
112       case PCI_CHIP_Q35_G:
113          chipset = "Intel(R) Q35";
114          break;
115       case PCI_CHIP_Q33_G:
116          chipset = "Intel(R) Q33";
117          break;
118       case PCI_CHIP_IGD_GM:
119       case PCI_CHIP_IGD_G:
120          chipset = "Intel(R) IGD";
121          break;
122       case PCI_CHIP_I965_Q:
123          chipset = "Intel(R) 965Q";
124          break;
125       case PCI_CHIP_I965_G:
126       case PCI_CHIP_I965_G_1:
127          chipset = "Intel(R) 965G";
128          break;
129       case PCI_CHIP_I946_GZ:
130          chipset = "Intel(R) 946GZ";
131          break;
132       case PCI_CHIP_I965_GM:
133          chipset = "Intel(R) 965GM";
134          break;
135       case PCI_CHIP_I965_GME:
136          chipset = "Intel(R) 965GME/GLE";
137          break;
138       case PCI_CHIP_GM45_GM:
139          chipset = "Mobile IntelĀ® GM45 Express Chipset";
140          break; 
141       case PCI_CHIP_IGD_E_G:
142          chipset = "Intel(R) Integrated Graphics Device";
143          break;
144       case PCI_CHIP_G45_G:
145          chipset = "Intel(R) G45/G43";
146          break;
147       case PCI_CHIP_Q45_G:
148          chipset = "Intel(R) Q45/Q43";
149          break;
150       case PCI_CHIP_G41_G:
151          chipset = "Intel(R) G41";
152          break;
153       case PCI_CHIP_B43_G:
154       case PCI_CHIP_B43_G1:
155          chipset = "Intel(R) B43";
156          break;
157       case PCI_CHIP_ILD_G:
158          chipset = "Intel(R) Ironlake Desktop";
159          break;
160       case PCI_CHIP_ILM_G:
161          chipset = "Intel(R) Ironlake Mobile";
162          break;
163       case PCI_CHIP_SANDYBRIDGE_GT1:
164       case PCI_CHIP_SANDYBRIDGE_GT2:
165       case PCI_CHIP_SANDYBRIDGE_GT2_PLUS:
166          chipset = "Intel(R) Sandybridge Desktop";
167          break;
168       case PCI_CHIP_SANDYBRIDGE_M_GT1:
169       case PCI_CHIP_SANDYBRIDGE_M_GT2:
170       case PCI_CHIP_SANDYBRIDGE_M_GT2_PLUS:
171          chipset = "Intel(R) Sandybridge Mobile";
172          break;
173       case PCI_CHIP_SANDYBRIDGE_S:
174          chipset = "Intel(R) Sandybridge Server";
175          break;
176       case PCI_CHIP_IVYBRIDGE_GT1:
177       case PCI_CHIP_IVYBRIDGE_GT2:
178          chipset = "Intel(R) Ivybridge Desktop";
179          break;
180       case PCI_CHIP_IVYBRIDGE_M_GT1:
181       case PCI_CHIP_IVYBRIDGE_M_GT2:
182          chipset = "Intel(R) Ivybridge Mobile";
183          break;
184       case PCI_CHIP_IVYBRIDGE_S_GT1:
185          chipset = "Intel(R) Ivybridge Server";
186          break;
187       default:
188          chipset = "Unknown Intel Chipset";
189          break;
190       }
191
192       (void) driGetRendererString(buffer, chipset, 0);
193       return (GLubyte *) buffer;
194
195    default:
196       return NULL;
197    }
198 }
199
200 static void
201 intel_flush_front(struct gl_context *ctx)
202 {
203    struct intel_context *intel = intel_context(ctx);
204     __DRIcontext *driContext = intel->driContext;
205     __DRIscreen *const screen = intel->intelScreen->driScrnPriv;
206
207    if ((ctx->DrawBuffer->Name == 0) && intel->front_buffer_dirty) {
208       if (screen->dri2.loader &&
209           (screen->dri2.loader->base.version >= 2)
210           && (screen->dri2.loader->flushFrontBuffer != NULL) &&
211           driContext->driDrawablePriv &&
212           driContext->driDrawablePriv->loaderPrivate) {
213          (*screen->dri2.loader->flushFrontBuffer)(driContext->driDrawablePriv,
214                                                   driContext->driDrawablePriv->loaderPrivate);
215
216          /* We set the dirty bit in intel_prepare_render() if we're
217           * front buffer rendering once we get there.
218           */
219          intel->front_buffer_dirty = GL_FALSE;
220       }
221    }
222 }
223
224 static unsigned
225 intel_bits_per_pixel(const struct intel_renderbuffer *rb)
226 {
227    return _mesa_get_format_bytes(rb->Base.Format) * 8;
228 }
229
230 void
231 intel_update_renderbuffers(__DRIcontext *context, __DRIdrawable *drawable)
232 {
233    struct gl_framebuffer *fb = drawable->driverPrivate;
234    struct intel_renderbuffer *rb;
235    struct intel_region *region, *depth_region;
236    struct intel_context *intel = context->driverPrivate;
237    struct intel_renderbuffer *front_rb, *back_rb, *depth_rb, *stencil_rb;
238    __DRIbuffer *buffers = NULL;
239    __DRIscreen *screen;
240    int i, count;
241    unsigned int attachments[10];
242    const char *region_name;
243
244    /* If we're rendering to the fake front buffer, make sure all the
245     * pending drawing has landed on the real front buffer.  Otherwise
246     * when we eventually get to DRI2GetBuffersWithFormat the stale
247     * real front buffer contents will get copied to the new fake front
248     * buffer.
249     */
250    if (intel->is_front_buffer_rendering) {
251       intel_flush(&intel->ctx);
252       intel_flush_front(&intel->ctx);
253    }
254
255    /* Set this up front, so that in case our buffers get invalidated
256     * while we're getting new buffers, we don't clobber the stamp and
257     * thus ignore the invalidate. */
258    drawable->lastStamp = drawable->dri2.stamp;
259
260    if (unlikely(INTEL_DEBUG & DEBUG_DRI))
261       fprintf(stderr, "enter %s, drawable %p\n", __func__, drawable);
262
263    screen = intel->intelScreen->driScrnPriv;
264
265    if (screen->dri2.loader
266        && (screen->dri2.loader->base.version > 2)
267        && (screen->dri2.loader->getBuffersWithFormat != NULL)) {
268
269       front_rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
270       back_rb = intel_get_renderbuffer(fb, BUFFER_BACK_LEFT);
271       depth_rb = intel_get_renderbuffer(fb, BUFFER_DEPTH);
272       stencil_rb = intel_get_renderbuffer(fb, BUFFER_STENCIL);
273
274       i = 0;
275       if ((intel->is_front_buffer_rendering ||
276            intel->is_front_buffer_reading ||
277            !back_rb) && front_rb) {
278          attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
279          attachments[i++] = intel_bits_per_pixel(front_rb);
280       }
281
282       if (back_rb) {
283          attachments[i++] = __DRI_BUFFER_BACK_LEFT;
284          attachments[i++] = intel_bits_per_pixel(back_rb);
285       }
286
287       if ((depth_rb != NULL) && (stencil_rb != NULL)) {
288          attachments[i++] = __DRI_BUFFER_DEPTH_STENCIL;
289          attachments[i++] = intel_bits_per_pixel(depth_rb);
290       } else if (depth_rb != NULL) {
291          attachments[i++] = __DRI_BUFFER_DEPTH;
292          attachments[i++] = intel_bits_per_pixel(depth_rb);
293       } else if (stencil_rb != NULL) {
294          attachments[i++] = __DRI_BUFFER_STENCIL;
295          attachments[i++] = intel_bits_per_pixel(stencil_rb);
296       }
297
298       buffers =
299          (*screen->dri2.loader->getBuffersWithFormat)(drawable,
300                                                       &drawable->w,
301                                                       &drawable->h,
302                                                       attachments, i / 2,
303                                                       &count,
304                                                       drawable->loaderPrivate);
305    } else if (screen->dri2.loader) {
306       i = 0;
307       if (intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT))
308          attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
309       if (intel_get_renderbuffer(fb, BUFFER_BACK_LEFT))
310          attachments[i++] = __DRI_BUFFER_BACK_LEFT;
311       if (intel_get_renderbuffer(fb, BUFFER_DEPTH))
312          attachments[i++] = __DRI_BUFFER_DEPTH;
313       if (intel_get_renderbuffer(fb, BUFFER_STENCIL))
314          attachments[i++] = __DRI_BUFFER_STENCIL;
315
316       buffers = (*screen->dri2.loader->getBuffers)(drawable,
317                                                    &drawable->w,
318                                                    &drawable->h,
319                                                    attachments, i,
320                                                    &count,
321                                                    drawable->loaderPrivate);
322    }
323
324    if (buffers == NULL)
325       return;
326
327    drawable->x = 0;
328    drawable->y = 0;
329    drawable->backX = 0;
330    drawable->backY = 0;
331    drawable->numClipRects = 1;
332    drawable->pClipRects[0].x1 = 0;
333    drawable->pClipRects[0].y1 = 0;
334    drawable->pClipRects[0].x2 = drawable->w;
335    drawable->pClipRects[0].y2 = drawable->h;
336    drawable->numBackClipRects = 1;
337    drawable->pBackClipRects[0].x1 = 0;
338    drawable->pBackClipRects[0].y1 = 0;
339    drawable->pBackClipRects[0].x2 = drawable->w;
340    drawable->pBackClipRects[0].y2 = drawable->h;
341
342    depth_region = NULL;
343    for (i = 0; i < count; i++) {
344        switch (buffers[i].attachment) {
345        case __DRI_BUFFER_FRONT_LEFT:
346            rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
347            region_name = "dri2 front buffer";
348            break;
349
350        case __DRI_BUFFER_FAKE_FRONT_LEFT:
351            rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
352            region_name = "dri2 fake front buffer";
353            break;
354
355        case __DRI_BUFFER_BACK_LEFT:
356            rb = intel_get_renderbuffer(fb, BUFFER_BACK_LEFT);
357            region_name = "dri2 back buffer";
358            break;
359
360        case __DRI_BUFFER_DEPTH:
361            rb = intel_get_renderbuffer(fb, BUFFER_DEPTH);
362            region_name = "dri2 depth buffer";
363            break;
364
365        case __DRI_BUFFER_DEPTH_STENCIL:
366            rb = intel_get_renderbuffer(fb, BUFFER_DEPTH);
367            region_name = "dri2 depth / stencil buffer";
368            break;
369
370        case __DRI_BUFFER_STENCIL:
371            rb = intel_get_renderbuffer(fb, BUFFER_STENCIL);
372            region_name = "dri2 stencil buffer";
373            break;
374
375        case __DRI_BUFFER_ACCUM:
376        default:
377            fprintf(stderr,
378                    "unhandled buffer attach event, attachment type %d\n",
379                    buffers[i].attachment);
380            return;
381        }
382
383        if (rb == NULL)
384           continue;
385
386        if (rb->region && rb->region->name == buffers[i].name)
387              continue;
388
389        if (unlikely(INTEL_DEBUG & DEBUG_DRI))
390           fprintf(stderr,
391                   "attaching buffer %d, at %d, cpp %d, pitch %d\n",
392                   buffers[i].name, buffers[i].attachment,
393                   buffers[i].cpp, buffers[i].pitch);
394        
395        if (buffers[i].attachment == __DRI_BUFFER_STENCIL && depth_region) {
396           if (unlikely(INTEL_DEBUG & DEBUG_DRI))
397              fprintf(stderr, "(reusing depth buffer as stencil)\n");
398           intel_region_reference(&region, depth_region);
399        }
400        else
401           region = intel_region_alloc_for_handle(intel->intelScreen,
402                                                  buffers[i].cpp,
403                                                  drawable->w,
404                                                  drawable->h,
405                                                  buffers[i].pitch / buffers[i].cpp,
406                                                  buffers[i].name,
407                                                  region_name);
408
409        if (buffers[i].attachment == __DRI_BUFFER_DEPTH)
410           depth_region = region;
411
412        intel_renderbuffer_set_region(intel, rb, region);
413        intel_region_release(&region);
414
415        if (buffers[i].attachment == __DRI_BUFFER_DEPTH_STENCIL) {
416           rb = intel_get_renderbuffer(fb, BUFFER_STENCIL);
417           if (rb != NULL) {
418              struct intel_region *stencil_region = NULL;
419
420              if (rb->region && rb->region->name == buffers[i].name)
421                    continue;
422
423              intel_region_reference(&stencil_region, region);
424              intel_renderbuffer_set_region(intel, rb, stencil_region);
425              intel_region_release(&stencil_region);
426           }
427        }
428    }
429
430    driUpdateFramebufferSize(&intel->ctx, drawable);
431 }
432
433 /**
434  * intel_prepare_render should be called anywhere that curent read/drawbuffer
435  * state is required.
436  */
437 void
438 intel_prepare_render(struct intel_context *intel)
439 {
440    __DRIcontext *driContext = intel->driContext;
441    __DRIdrawable *drawable;
442
443    drawable = driContext->driDrawablePriv;
444    if (drawable && drawable->dri2.stamp != driContext->dri2.draw_stamp) {
445       if (drawable->lastStamp != drawable->dri2.stamp)
446          intel_update_renderbuffers(driContext, drawable);
447       intel_draw_buffer(&intel->ctx, intel->ctx.DrawBuffer);
448       driContext->dri2.draw_stamp = drawable->dri2.stamp;
449    }
450
451    drawable = driContext->driReadablePriv;
452    if (drawable && drawable->dri2.stamp != driContext->dri2.read_stamp) {
453       if (drawable->lastStamp != drawable->dri2.stamp)
454          intel_update_renderbuffers(driContext, drawable);
455       driContext->dri2.read_stamp = drawable->dri2.stamp;
456    }
457
458    /* If we're currently rendering to the front buffer, the rendering
459     * that will happen next will probably dirty the front buffer.  So
460     * mark it as dirty here.
461     */
462    if (intel->is_front_buffer_rendering)
463       intel->front_buffer_dirty = GL_TRUE;
464
465    /* Wait for the swapbuffers before the one we just emitted, so we
466     * don't get too many swaps outstanding for apps that are GPU-heavy
467     * but not CPU-heavy.
468     *
469     * We're using intelDRI2Flush (called from the loader before
470     * swapbuffer) and glFlush (for front buffer rendering) as the
471     * indicator that a frame is done and then throttle when we get
472     * here as we prepare to render the next frame.  At this point for
473     * round trips for swap/copy and getting new buffers are done and
474     * we'll spend less time waiting on the GPU.
475     *
476     * Unfortunately, we don't have a handle to the batch containing
477     * the swap, and getting our hands on that doesn't seem worth it,
478     * so we just us the first batch we emitted after the last swap.
479     */
480    if (intel->need_throttle && intel->first_post_swapbuffers_batch) {
481       drm_intel_bo_wait_rendering(intel->first_post_swapbuffers_batch);
482       drm_intel_bo_unreference(intel->first_post_swapbuffers_batch);
483       intel->first_post_swapbuffers_batch = NULL;
484       intel->need_throttle = GL_FALSE;
485    }
486 }
487
488 static void
489 intel_viewport(struct gl_context *ctx, GLint x, GLint y, GLsizei w, GLsizei h)
490 {
491     struct intel_context *intel = intel_context(ctx);
492     __DRIcontext *driContext = intel->driContext;
493
494     if (intel->saved_viewport)
495         intel->saved_viewport(ctx, x, y, w, h);
496
497     if (ctx->DrawBuffer->Name == 0) {
498        dri2InvalidateDrawable(driContext->driDrawablePriv);
499        dri2InvalidateDrawable(driContext->driReadablePriv);
500     }
501 }
502
503 static const struct dri_debug_control debug_control[] = {
504    { "tex",   DEBUG_TEXTURE},
505    { "state", DEBUG_STATE},
506    { "ioctl", DEBUG_IOCTL},
507    { "blit",  DEBUG_BLIT},
508    { "mip",   DEBUG_MIPTREE},
509    { "fall",  DEBUG_FALLBACKS},
510    { "verb",  DEBUG_VERBOSE},
511    { "bat",   DEBUG_BATCH},
512    { "pix",   DEBUG_PIXEL},
513    { "buf",   DEBUG_BUFMGR},
514    { "reg",   DEBUG_REGION},
515    { "fbo",   DEBUG_FBO},
516    { "gs",    DEBUG_GS},
517    { "sync",  DEBUG_SYNC},
518    { "prim",  DEBUG_PRIMS },
519    { "vert",  DEBUG_VERTS },
520    { "dri",   DEBUG_DRI },
521    { "sf",    DEBUG_SF },
522    { "san",   DEBUG_SANITY },
523    { "sleep", DEBUG_SLEEP },
524    { "stats", DEBUG_STATS },
525    { "tile",  DEBUG_TILE },
526    { "sing",  DEBUG_SINGLE_THREAD },
527    { "thre",  DEBUG_SINGLE_THREAD },
528    { "wm",    DEBUG_WM },
529    { "urb",   DEBUG_URB },
530    { "vs",    DEBUG_VS },
531    { "clip",  DEBUG_CLIP },
532    { NULL,    0 }
533 };
534
535
536 static void
537 intelInvalidateState(struct gl_context * ctx, GLuint new_state)
538 {
539     struct intel_context *intel = intel_context(ctx);
540
541    _swrast_InvalidateState(ctx, new_state);
542    _swsetup_InvalidateState(ctx, new_state);
543    _vbo_InvalidateState(ctx, new_state);
544    _tnl_InvalidateState(ctx, new_state);
545    _tnl_invalidate_vertex_state(ctx, new_state);
546
547    intel->NewGLState |= new_state;
548
549    if (intel->vtbl.invalidate_state)
550       intel->vtbl.invalidate_state( intel, new_state );
551 }
552
553 void
554 intel_flush(struct gl_context *ctx)
555 {
556    struct intel_context *intel = intel_context(ctx);
557
558    if (intel->Fallback)
559       _swrast_flush(ctx);
560
561    if (intel->gen < 4)
562       INTEL_FIREVERTICES(intel);
563
564    if (intel->batch.used)
565       intel_batchbuffer_flush(intel);
566 }
567
568 static void
569 intel_glFlush(struct gl_context *ctx)
570 {
571    struct intel_context *intel = intel_context(ctx);
572
573    intel_flush(ctx);
574    intel_flush_front(ctx);
575    if (intel->is_front_buffer_rendering)
576       intel->need_throttle = GL_TRUE;
577 }
578
579 void
580 intelFinish(struct gl_context * ctx)
581 {
582    struct gl_framebuffer *fb = ctx->DrawBuffer;
583    int i;
584
585    intel_flush(ctx);
586    intel_flush_front(ctx);
587
588    for (i = 0; i < fb->_NumColorDrawBuffers; i++) {
589        struct intel_renderbuffer *irb;
590
591        irb = intel_renderbuffer(fb->_ColorDrawBuffers[i]);
592
593        if (irb && irb->region && irb->region->buffer)
594           drm_intel_bo_wait_rendering(irb->region->buffer);
595    }
596    if (fb->_DepthBuffer) {
597       /* XXX: Wait on buffer idle */
598    }
599 }
600
601 void
602 intelInitDriverFunctions(struct dd_function_table *functions)
603 {
604    _mesa_init_driver_functions(functions);
605
606    functions->Flush = intel_glFlush;
607    functions->Finish = intelFinish;
608    functions->GetString = intelGetString;
609    functions->UpdateState = intelInvalidateState;
610
611    intelInitTextureFuncs(functions);
612    intelInitTextureImageFuncs(functions);
613    intelInitTextureSubImageFuncs(functions);
614    intelInitTextureCopyImageFuncs(functions);
615    intelInitStateFuncs(functions);
616    intelInitClearFuncs(functions);
617    intelInitBufferFuncs(functions);
618    intelInitPixelFuncs(functions);
619    intelInitBufferObjectFuncs(functions);
620    intel_init_syncobj_functions(functions);
621 }
622
623 /**
624  * Override intel->has_hiz with environment variable INTEL_HIZ.
625  *
626  * Valid values for INTEL_HIZ are "0" and "1". If an invalid valid value is
627  * encountered, a warning is emitted and INTEL_HIZ is ignored.
628  */
629 static void
630 intel_override_hiz(struct intel_context *intel)
631 {
632    const char *s = getenv("INTEL_HIZ");
633    if (!s) {
634       return;
635    } else if (!strncmp("0", s, 2)) {
636       intel->has_hiz = false;
637    } else if (!strncmp("1", s, 2)) {
638       intel->has_hiz = true;
639    } else {
640       _mesa_warning(&intel->ctx,
641                     "env variable INTEL_HIZ=\"%s\" has invalid value and "
642                     "is ignored", s);
643    }
644 }
645
646 /**
647  * Override intel->has_separate_stencil with environment variable
648  * INTEL_SEPARATE_STENCIL.
649  *
650  * Valid values for INTEL_SEPARATE_STENCIL are "0" and "1". If an invalid
651  * value is encountered, a warning is emitted and INTEL_SEPARATE_STENCIL is
652  * ignored.
653  */
654 static void
655 intel_override_separate_stencil(struct intel_context *intel)
656 {
657    const char *s = getenv("INTEL_SEPARATE_STENCIL");
658    if (!s) {
659       return;
660    } else if (!strncmp("0", s, 2)) {
661       intel->has_separate_stencil = false;
662    } else if (!strncmp("1", s, 2)) {
663       intel->has_separate_stencil = true;
664    } else {
665       _mesa_warning(&intel->ctx,
666                     "env variable INTEL_SEPARATE_STENCIL=\"%s\" has invalid "
667                     "value and is ignored", s);
668    }
669 }
670
671 GLboolean
672 intelInitContext(struct intel_context *intel,
673                  int api,
674                  const struct gl_config * mesaVis,
675                  __DRIcontext * driContextPriv,
676                  void *sharedContextPrivate,
677                  struct dd_function_table *functions)
678 {
679    struct gl_context *ctx = &intel->ctx;
680    struct gl_context *shareCtx = (struct gl_context *) sharedContextPrivate;
681    __DRIscreen *sPriv = driContextPriv->driScreenPriv;
682    struct intel_screen *intelScreen = sPriv->private;
683    int bo_reuse_mode;
684    struct gl_config visual;
685
686    /* we can't do anything without a connection to the device */
687    if (intelScreen->bufmgr == NULL)
688       return GL_FALSE;
689
690    /* Can't rely on invalidate events, fall back to glViewport hack */
691    if (!driContextPriv->driScreenPriv->dri2.useInvalidate) {
692       intel->saved_viewport = functions->Viewport;
693       functions->Viewport = intel_viewport;
694    }
695
696    if (mesaVis == NULL) {
697       memset(&visual, 0, sizeof visual);
698       mesaVis = &visual;
699    }
700
701    if (!_mesa_initialize_context(&intel->ctx, api, mesaVis, shareCtx,
702                                  functions, (void *) intel)) {
703       printf("%s: failed to init mesa context\n", __FUNCTION__);
704       return GL_FALSE;
705    }
706
707    driContextPriv->driverPrivate = intel;
708    intel->intelScreen = intelScreen;
709    intel->driContext = driContextPriv;
710    intel->driFd = sPriv->fd;
711
712    intel->has_xrgb_textures = GL_TRUE;
713    intel->gen = intelScreen->gen;
714    if (IS_GEN7(intel->intelScreen->deviceID)) {
715       intel->needs_ff_sync = GL_TRUE;
716       intel->has_luminance_srgb = GL_TRUE;
717       /* FINISHME: Enable intel->has_separate_stencil on Gen7. */
718       /* FINISHME: Enable intel->must_use_separate_stencil on Gen7. */
719       /* FINISHME: Enable intel->has_hiz on Gen7. */
720    } else if (IS_GEN6(intel->intelScreen->deviceID)) {
721       intel->needs_ff_sync = GL_TRUE;
722       intel->has_luminance_srgb = GL_TRUE;
723       /* FINISHME: Enable intel->has_separate_stencil on Gen6. */
724       /* FINISHME: Enable intel->has_hiz on Gen6. */
725    } else if (IS_GEN5(intel->intelScreen->deviceID)) {
726       intel->needs_ff_sync = GL_TRUE;
727       intel->has_luminance_srgb = GL_TRUE;
728    } else if (IS_965(intel->intelScreen->deviceID)) {
729       if (IS_G4X(intel->intelScreen->deviceID)) {
730           intel->has_luminance_srgb = GL_TRUE;
731           intel->is_g4x = GL_TRUE;
732       }
733    } else if (IS_9XX(intel->intelScreen->deviceID)) {
734       if (IS_945(intel->intelScreen->deviceID)) {
735          intel->is_945 = GL_TRUE;
736       }
737    } else {
738       if (intel->intelScreen->deviceID == PCI_CHIP_I830_M ||
739           intel->intelScreen->deviceID == PCI_CHIP_845_G) {
740          intel->has_xrgb_textures = GL_FALSE;
741       }
742    }
743
744    intel_override_hiz(intel);
745    intel_override_separate_stencil(intel);
746
747    memset(&ctx->TextureFormatSupported, 0,
748           sizeof(ctx->TextureFormatSupported));
749    ctx->TextureFormatSupported[MESA_FORMAT_ARGB8888] = GL_TRUE;
750    if (intel->has_xrgb_textures)
751       ctx->TextureFormatSupported[MESA_FORMAT_XRGB8888] = GL_TRUE;
752    ctx->TextureFormatSupported[MESA_FORMAT_ARGB4444] = GL_TRUE;
753    ctx->TextureFormatSupported[MESA_FORMAT_ARGB1555] = GL_TRUE;
754    ctx->TextureFormatSupported[MESA_FORMAT_RGB565] = GL_TRUE;
755    ctx->TextureFormatSupported[MESA_FORMAT_L8] = GL_TRUE;
756    ctx->TextureFormatSupported[MESA_FORMAT_A8] = GL_TRUE;
757    ctx->TextureFormatSupported[MESA_FORMAT_I8] = GL_TRUE;
758    ctx->TextureFormatSupported[MESA_FORMAT_AL88] = GL_TRUE;
759    if (intel->gen >= 4)
760       ctx->TextureFormatSupported[MESA_FORMAT_AL1616] = GL_TRUE;
761    ctx->TextureFormatSupported[MESA_FORMAT_S8_Z24] = GL_TRUE;
762    /*
763     * This was disabled in initial FBO enabling to avoid combinations
764     * of depth+stencil that wouldn't work together.  We since decided
765     * that it was OK, since it's up to the app to come up with the
766     * combo that actually works, so this can probably be re-enabled.
767     */
768    /*
769    ctx->TextureFormatSupported[MESA_FORMAT_Z16] = GL_TRUE;
770    ctx->TextureFormatSupported[MESA_FORMAT_Z24] = GL_TRUE;
771    */
772
773    /* ctx->Extensions.MESA_ycbcr_texture */
774    ctx->TextureFormatSupported[MESA_FORMAT_YCBCR] = GL_TRUE;
775    ctx->TextureFormatSupported[MESA_FORMAT_YCBCR_REV] = GL_TRUE;
776
777    /* GL_3DFX_texture_compression_FXT1 */
778    ctx->TextureFormatSupported[MESA_FORMAT_RGB_FXT1] = GL_TRUE;
779    ctx->TextureFormatSupported[MESA_FORMAT_RGBA_FXT1] = GL_TRUE;
780
781    /* GL_EXT_texture_compression_s3tc */
782    ctx->TextureFormatSupported[MESA_FORMAT_RGB_DXT1] = GL_TRUE;
783    ctx->TextureFormatSupported[MESA_FORMAT_RGBA_DXT1] = GL_TRUE;
784    ctx->TextureFormatSupported[MESA_FORMAT_RGBA_DXT3] = GL_TRUE;
785    ctx->TextureFormatSupported[MESA_FORMAT_RGBA_DXT5] = GL_TRUE;
786
787 #ifndef I915
788    /* GL_ARB_texture_compression_rgtc */
789    ctx->TextureFormatSupported[MESA_FORMAT_RED_RGTC1] = GL_TRUE;
790    ctx->TextureFormatSupported[MESA_FORMAT_SIGNED_RED_RGTC1] = GL_TRUE;
791    ctx->TextureFormatSupported[MESA_FORMAT_RG_RGTC2] = GL_TRUE;
792    ctx->TextureFormatSupported[MESA_FORMAT_SIGNED_RG_RGTC2] = GL_TRUE;
793
794    /* GL_ARB_texture_rg */
795    ctx->TextureFormatSupported[MESA_FORMAT_R8] = GL_TRUE;
796    ctx->TextureFormatSupported[MESA_FORMAT_R16] = GL_TRUE;
797    ctx->TextureFormatSupported[MESA_FORMAT_RG88] = GL_TRUE;
798    ctx->TextureFormatSupported[MESA_FORMAT_RG1616] = GL_TRUE;
799
800    /* GL_MESA_texture_signed_rgba / GL_EXT_texture_snorm */
801    ctx->TextureFormatSupported[MESA_FORMAT_DUDV8] = GL_TRUE;
802    ctx->TextureFormatSupported[MESA_FORMAT_SIGNED_RGBA8888_REV] = GL_TRUE;
803    ctx->TextureFormatSupported[MESA_FORMAT_SIGNED_R8] = GL_TRUE;
804    ctx->TextureFormatSupported[MESA_FORMAT_SIGNED_RG88_REV] = GL_TRUE;
805    ctx->TextureFormatSupported[MESA_FORMAT_SIGNED_R16] = GL_TRUE;
806    ctx->TextureFormatSupported[MESA_FORMAT_SIGNED_GR1616] = GL_TRUE;
807
808    /* GL_EXT_texture_sRGB */
809    ctx->TextureFormatSupported[MESA_FORMAT_SARGB8] = GL_TRUE;
810    if (intel->gen >= 5 || intel->is_g4x)
811       ctx->TextureFormatSupported[MESA_FORMAT_SRGB_DXT1] = GL_TRUE;
812    ctx->TextureFormatSupported[MESA_FORMAT_SRGBA_DXT1] = GL_TRUE;
813    ctx->TextureFormatSupported[MESA_FORMAT_SRGBA_DXT3] = GL_TRUE;
814    ctx->TextureFormatSupported[MESA_FORMAT_SRGBA_DXT5] = GL_TRUE;
815    if (intel->has_luminance_srgb) {
816       ctx->TextureFormatSupported[MESA_FORMAT_SL8] = GL_TRUE;
817       ctx->TextureFormatSupported[MESA_FORMAT_SLA8] = GL_TRUE;
818    }
819
820 #ifdef TEXTURE_FLOAT_ENABLED
821    ctx->TextureFormatSupported[MESA_FORMAT_RGBA_FLOAT32] = GL_TRUE;
822    ctx->TextureFormatSupported[MESA_FORMAT_RG_FLOAT32] = GL_TRUE;
823    ctx->TextureFormatSupported[MESA_FORMAT_R_FLOAT32] = GL_TRUE;
824    ctx->TextureFormatSupported[MESA_FORMAT_INTENSITY_FLOAT32] = GL_TRUE;
825    ctx->TextureFormatSupported[MESA_FORMAT_LUMINANCE_FLOAT32] = GL_TRUE;
826    ctx->TextureFormatSupported[MESA_FORMAT_ALPHA_FLOAT32] = GL_TRUE;
827    ctx->TextureFormatSupported[MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32] = GL_TRUE;
828 #endif
829
830 #endif /* !I915 */
831
832    driParseConfigFiles(&intel->optionCache, &intelScreen->optionCache,
833                        sPriv->myNum, (intel->gen >= 4) ? "i965" : "i915");
834    if (intel->gen < 4)
835       intel->maxBatchSize = 4096;
836    else
837       intel->maxBatchSize = sizeof(intel->batch.map);
838
839    intel->bufmgr = intelScreen->bufmgr;
840
841    bo_reuse_mode = driQueryOptioni(&intel->optionCache, "bo_reuse");
842    switch (bo_reuse_mode) {
843    case DRI_CONF_BO_REUSE_DISABLED:
844       break;
845    case DRI_CONF_BO_REUSE_ALL:
846       intel_bufmgr_gem_enable_reuse(intel->bufmgr);
847       break;
848    }
849
850    /* This doesn't yet catch all non-conformant rendering, but it's a
851     * start.
852     */
853    if (getenv("INTEL_STRICT_CONFORMANCE")) {
854       unsigned int value = atoi(getenv("INTEL_STRICT_CONFORMANCE"));
855       if (value > 0) {
856          intel->conformance_mode = value;
857       }
858       else {
859          intel->conformance_mode = 1;
860       }
861    }
862
863    if (intel->conformance_mode > 0) {
864       ctx->Const.MinLineWidth = 1.0;
865       ctx->Const.MinLineWidthAA = 1.0;
866       ctx->Const.MaxLineWidth = 1.0;
867       ctx->Const.MaxLineWidthAA = 1.0;
868       ctx->Const.LineWidthGranularity = 1.0;
869    }
870    else {
871       ctx->Const.MinLineWidth = 1.0;
872       ctx->Const.MinLineWidthAA = 1.0;
873       ctx->Const.MaxLineWidth = 5.0;
874       ctx->Const.MaxLineWidthAA = 5.0;
875       ctx->Const.LineWidthGranularity = 0.5;
876    }
877
878    ctx->Const.MinPointSize = 1.0;
879    ctx->Const.MinPointSizeAA = 1.0;
880    ctx->Const.MaxPointSize = 255.0;
881    ctx->Const.MaxPointSizeAA = 3.0;
882    ctx->Const.PointSizeGranularity = 1.0;
883
884    ctx->Const.MaxSamples = 1.0;
885
886    /* reinitialize the context point state.
887     * It depend on constants in __struct gl_contextRec::Const
888     */
889    _mesa_init_point(ctx);
890
891    if (intel->gen >= 4) {
892       ctx->Const.sRGBCapable = GL_TRUE;
893       if (MAX_WIDTH > 8192)
894          ctx->Const.MaxRenderbufferSize = 8192;
895    } else {
896       if (MAX_WIDTH > 2048)
897          ctx->Const.MaxRenderbufferSize = 2048;
898    }
899
900    /* Initialize the software rasterizer and helper modules. */
901    _swrast_CreateContext(ctx);
902    _vbo_CreateContext(ctx);
903    _tnl_CreateContext(ctx);
904    _swsetup_CreateContext(ctx);
905  
906    /* Configure swrast to match hardware characteristics: */
907    _swrast_allow_pixel_fog(ctx, GL_FALSE);
908    _swrast_allow_vertex_fog(ctx, GL_TRUE);
909
910    _mesa_meta_init(ctx);
911
912    intel->hw_stencil = mesaVis->stencilBits && mesaVis->depthBits == 24;
913    intel->hw_stipple = 1;
914
915    /* XXX FBO: this doesn't seem to be used anywhere */
916    switch (mesaVis->depthBits) {
917    case 0:                     /* what to do in this case? */
918    case 16:
919       intel->polygon_offset_scale = 1.0;
920       break;
921    case 24:
922       intel->polygon_offset_scale = 2.0;     /* req'd to pass glean */
923       break;
924    default:
925       assert(0);
926       break;
927    }
928
929    if (intel->gen >= 4)
930       intel->polygon_offset_scale /= 0xffff;
931
932    intel->RenderIndex = ~0;
933
934    switch (ctx->API) {
935    case API_OPENGL:
936       intelInitExtensions(ctx);
937       break;
938    case API_OPENGLES:
939       break;
940    case API_OPENGLES2:
941       intelInitExtensionsES2(ctx);
942       break;
943    }
944
945    INTEL_DEBUG = driParseDebugString(getenv("INTEL_DEBUG"), debug_control);
946    if (INTEL_DEBUG & DEBUG_BUFMGR)
947       dri_bufmgr_set_debug(intel->bufmgr, GL_TRUE);
948
949    intel_batchbuffer_reset(intel);
950
951    intel_fbo_init(intel);
952
953    if (intel->ctx.Mesa_DXTn) {
954       _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
955       _mesa_enable_extension(ctx, "GL_S3_s3tc");
956    }
957    else if (driQueryOptionb(&intel->optionCache, "force_s3tc_enable")) {
958       _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
959    }
960    intel->use_texture_tiling = driQueryOptionb(&intel->optionCache,
961                                                "texture_tiling");
962    intel->use_early_z = driQueryOptionb(&intel->optionCache, "early_z");
963
964    intel->prim.primitive = ~0;
965
966    /* Force all software fallbacks */
967    if (driQueryOptionb(&intel->optionCache, "no_rast")) {
968       fprintf(stderr, "disabling 3D rasterization\n");
969       intel->no_rast = 1;
970    }
971
972    if (driQueryOptionb(&intel->optionCache, "always_flush_batch")) {
973       fprintf(stderr, "flushing batchbuffer before/after each draw call\n");
974       intel->always_flush_batch = 1;
975    }
976
977    if (driQueryOptionb(&intel->optionCache, "always_flush_cache")) {
978       fprintf(stderr, "flushing GPU caches before/after each draw call\n");
979       intel->always_flush_cache = 1;
980    }
981
982    return GL_TRUE;
983 }
984
985 void
986 intelDestroyContext(__DRIcontext * driContextPriv)
987 {
988    struct intel_context *intel =
989       (struct intel_context *) driContextPriv->driverPrivate;
990
991    assert(intel);               /* should never be null */
992    if (intel) {
993       INTEL_FIREVERTICES(intel);
994
995       _mesa_meta_free(&intel->ctx);
996
997       intel->vtbl.destroy(intel);
998
999       _swsetup_DestroyContext(&intel->ctx);
1000       _tnl_DestroyContext(&intel->ctx);
1001       _vbo_DestroyContext(&intel->ctx);
1002
1003       _swrast_DestroyContext(&intel->ctx);
1004       intel->Fallback = 0x0;      /* don't call _swrast_Flush later */
1005
1006       intel_batchbuffer_free(intel);
1007
1008       free(intel->prim.vb);
1009       intel->prim.vb = NULL;
1010       drm_intel_bo_unreference(intel->prim.vb_bo);
1011       intel->prim.vb_bo = NULL;
1012       drm_intel_bo_unreference(intel->first_post_swapbuffers_batch);
1013       intel->first_post_swapbuffers_batch = NULL;
1014
1015       driDestroyOptionCache(&intel->optionCache);
1016
1017       /* free the Mesa context */
1018       _mesa_free_context_data(&intel->ctx);
1019
1020       FREE(intel);
1021       driContextPriv->driverPrivate = NULL;
1022    }
1023 }
1024
1025 GLboolean
1026 intelUnbindContext(__DRIcontext * driContextPriv)
1027 {
1028    /* Unset current context and dispath table */
1029    _mesa_make_current(NULL, NULL, NULL);
1030
1031    return GL_TRUE;
1032 }
1033
1034 GLboolean
1035 intelMakeCurrent(__DRIcontext * driContextPriv,
1036                  __DRIdrawable * driDrawPriv,
1037                  __DRIdrawable * driReadPriv)
1038 {
1039    struct intel_context *intel;
1040    GET_CURRENT_CONTEXT(curCtx);
1041
1042    if (driContextPriv)
1043       intel = (struct intel_context *) driContextPriv->driverPrivate;
1044    else
1045       intel = NULL;
1046
1047    /* According to the glXMakeCurrent() man page: "Pending commands to
1048     * the previous context, if any, are flushed before it is released."
1049     * But only flush if we're actually changing contexts.
1050     */
1051    if (intel_context(curCtx) && intel_context(curCtx) != intel) {
1052       _mesa_flush(curCtx);
1053    }
1054
1055    if (driContextPriv) {
1056       struct gl_framebuffer *fb, *readFb;
1057       
1058       if (driDrawPriv == NULL && driReadPriv == NULL) {
1059          fb = _mesa_get_incomplete_framebuffer();
1060          readFb = _mesa_get_incomplete_framebuffer();
1061       } else {
1062          fb = driDrawPriv->driverPrivate;
1063          readFb = driReadPriv->driverPrivate;
1064          driContextPriv->dri2.draw_stamp = driDrawPriv->dri2.stamp - 1;
1065          driContextPriv->dri2.read_stamp = driReadPriv->dri2.stamp - 1;
1066       }
1067
1068       intel_prepare_render(intel);
1069       _mesa_make_current(&intel->ctx, fb, readFb);
1070       
1071       /* We do this in intel_prepare_render() too, but intel->ctx.DrawBuffer
1072        * is NULL at that point.  We can't call _mesa_makecurrent()
1073        * first, since we need the buffer size for the initial
1074        * viewport.  So just call intel_draw_buffer() again here. */
1075       intel_draw_buffer(&intel->ctx, intel->ctx.DrawBuffer);
1076    }
1077    else {
1078       _mesa_make_current(NULL, NULL, NULL);
1079    }
1080
1081    return GL_TRUE;
1082 }