intel_context.c revision 2b311fd8024ab20065568236bdc60b9f69706d4c
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#include "main/renderbuffer.h"
37
38#include "swrast/swrast.h"
39#include "swrast_setup/swrast_setup.h"
40#include "tnl/tnl.h"
41#include "drivers/common/driverfuncs.h"
42#include "drivers/common/meta.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#include "intel_mipmap_tree.h"
57
58#include "utils.h"
59#include "../glsl/ralloc.h"
60
61#ifndef INTEL_DEBUG
62int INTEL_DEBUG = (0);
63#endif
64
65
66static const GLubyte *
67intelGetString(struct gl_context * ctx, GLenum name)
68{
69   const struct intel_context *const intel = intel_context(ctx);
70   const char *chipset;
71   static char buffer[128];
72
73   switch (name) {
74   case GL_VENDOR:
75      return (GLubyte *) "Intel Open Source Technology Center";
76      break;
77
78   case GL_RENDERER:
79      switch (intel->intelScreen->deviceID) {
80      case PCI_CHIP_845_G:
81         chipset = "Intel(R) 845G";
82         break;
83      case PCI_CHIP_I830_M:
84         chipset = "Intel(R) 830M";
85         break;
86      case PCI_CHIP_I855_GM:
87         chipset = "Intel(R) 852GM/855GM";
88         break;
89      case PCI_CHIP_I865_G:
90         chipset = "Intel(R) 865G";
91         break;
92      case PCI_CHIP_I915_G:
93         chipset = "Intel(R) 915G";
94         break;
95      case PCI_CHIP_E7221_G:
96	 chipset = "Intel (R) E7221G (i915)";
97	 break;
98      case PCI_CHIP_I915_GM:
99         chipset = "Intel(R) 915GM";
100         break;
101      case PCI_CHIP_I945_G:
102         chipset = "Intel(R) 945G";
103         break;
104      case PCI_CHIP_I945_GM:
105         chipset = "Intel(R) 945GM";
106         break;
107      case PCI_CHIP_I945_GME:
108         chipset = "Intel(R) 945GME";
109         break;
110      case PCI_CHIP_G33_G:
111	 chipset = "Intel(R) G33";
112	 break;
113      case PCI_CHIP_Q35_G:
114	 chipset = "Intel(R) Q35";
115	 break;
116      case PCI_CHIP_Q33_G:
117	 chipset = "Intel(R) Q33";
118	 break;
119      case PCI_CHIP_IGD_GM:
120      case PCI_CHIP_IGD_G:
121	 chipset = "Intel(R) IGD";
122	 break;
123      case PCI_CHIP_I965_Q:
124	 chipset = "Intel(R) 965Q";
125	 break;
126      case PCI_CHIP_I965_G:
127      case PCI_CHIP_I965_G_1:
128	 chipset = "Intel(R) 965G";
129	 break;
130      case PCI_CHIP_I946_GZ:
131	 chipset = "Intel(R) 946GZ";
132	 break;
133      case PCI_CHIP_I965_GM:
134	 chipset = "Intel(R) 965GM";
135	 break;
136      case PCI_CHIP_I965_GME:
137	 chipset = "Intel(R) 965GME/GLE";
138	 break;
139      case PCI_CHIP_GM45_GM:
140	 chipset = "Mobile Intel® GM45 Express Chipset";
141	 break;
142      case PCI_CHIP_IGD_E_G:
143	 chipset = "Intel(R) Integrated Graphics Device";
144	 break;
145      case PCI_CHIP_G45_G:
146         chipset = "Intel(R) G45/G43";
147         break;
148      case PCI_CHIP_Q45_G:
149         chipset = "Intel(R) Q45/Q43";
150         break;
151      case PCI_CHIP_G41_G:
152         chipset = "Intel(R) G41";
153         break;
154      case PCI_CHIP_B43_G:
155      case PCI_CHIP_B43_G1:
156         chipset = "Intel(R) B43";
157         break;
158      case PCI_CHIP_ILD_G:
159         chipset = "Intel(R) Ironlake Desktop";
160         break;
161      case PCI_CHIP_ILM_G:
162         chipset = "Intel(R) Ironlake Mobile";
163         break;
164      case PCI_CHIP_SANDYBRIDGE_GT1:
165      case PCI_CHIP_SANDYBRIDGE_GT2:
166      case PCI_CHIP_SANDYBRIDGE_GT2_PLUS:
167	 chipset = "Intel(R) Sandybridge Desktop";
168	 break;
169      case PCI_CHIP_SANDYBRIDGE_M_GT1:
170      case PCI_CHIP_SANDYBRIDGE_M_GT2:
171      case PCI_CHIP_SANDYBRIDGE_M_GT2_PLUS:
172	 chipset = "Intel(R) Sandybridge Mobile";
173	 break;
174      case PCI_CHIP_SANDYBRIDGE_S:
175	 chipset = "Intel(R) Sandybridge Server";
176	 break;
177      case PCI_CHIP_IVYBRIDGE_GT1:
178      case PCI_CHIP_IVYBRIDGE_GT2:
179	 chipset = "Intel(R) Ivybridge Desktop";
180	 break;
181      case PCI_CHIP_IVYBRIDGE_M_GT1:
182      case PCI_CHIP_IVYBRIDGE_M_GT2:
183	 chipset = "Intel(R) Ivybridge Mobile";
184	 break;
185      case PCI_CHIP_IVYBRIDGE_S_GT1:
186      case PCI_CHIP_IVYBRIDGE_S_GT2:
187	 chipset = "Intel(R) Ivybridge Server";
188	 break;
189      case PCI_CHIP_HASWELL_GT1:
190      case PCI_CHIP_HASWELL_GT2:
191	 chipset = "Intel(R) Haswell Desktop";
192	 break;
193      case PCI_CHIP_HASWELL_M_GT1:
194      case PCI_CHIP_HASWELL_M_GT2:
195      case PCI_CHIP_HASWELL_M_ULT_GT2:
196	 chipset = "Intel(R) Haswell Mobile";
197	 break;
198      default:
199         chipset = "Unknown Intel Chipset";
200         break;
201      }
202
203      (void) driGetRendererString(buffer, chipset, 0);
204      return (GLubyte *) buffer;
205
206   default:
207      return NULL;
208   }
209}
210
211static void
212intel_flush_front(struct gl_context *ctx)
213{
214   struct intel_context *intel = intel_context(ctx);
215    __DRIcontext *driContext = intel->driContext;
216    __DRIscreen *const screen = intel->intelScreen->driScrnPriv;
217
218    if (_mesa_is_winsys_fbo(ctx->DrawBuffer) && intel->front_buffer_dirty) {
219      if (screen->dri2.loader->flushFrontBuffer != NULL &&
220          driContext->driDrawablePriv &&
221	  driContext->driDrawablePriv->loaderPrivate) {
222	 (*screen->dri2.loader->flushFrontBuffer)(driContext->driDrawablePriv,
223						  driContext->driDrawablePriv->loaderPrivate);
224
225	 /* We set the dirty bit in intel_prepare_render() if we're
226	  * front buffer rendering once we get there.
227	  */
228	 intel->front_buffer_dirty = false;
229      }
230   }
231}
232
233static unsigned
234intel_bits_per_pixel(const struct intel_renderbuffer *rb)
235{
236   return _mesa_get_format_bytes(intel_rb_format(rb)) * 8;
237}
238
239static void
240intel_query_dri2_buffers(struct intel_context *intel,
241			 __DRIdrawable *drawable,
242			 __DRIbuffer **buffers,
243			 int *count);
244
245static void
246intel_process_dri2_buffer(struct intel_context *intel,
247			  __DRIdrawable *drawable,
248			  __DRIbuffer *buffer,
249			  struct intel_renderbuffer *rb,
250			  const char *buffer_name);
251
252void
253intel_update_renderbuffers(__DRIcontext *context, __DRIdrawable *drawable)
254{
255   struct gl_framebuffer *fb = drawable->driverPrivate;
256   struct intel_renderbuffer *rb;
257   struct intel_context *intel = context->driverPrivate;
258   __DRIbuffer *buffers = NULL;
259   int i, count;
260   const char *region_name;
261
262   /* If we're rendering to the fake front buffer, make sure all the
263    * pending drawing has landed on the real front buffer.  Otherwise
264    * when we eventually get to DRI2GetBuffersWithFormat the stale
265    * real front buffer contents will get copied to the new fake front
266    * buffer.
267    */
268   if (intel->is_front_buffer_rendering) {
269      intel_flush(&intel->ctx);
270      intel_flush_front(&intel->ctx);
271   }
272
273   /* Set this up front, so that in case our buffers get invalidated
274    * while we're getting new buffers, we don't clobber the stamp and
275    * thus ignore the invalidate. */
276   drawable->lastStamp = drawable->dri2.stamp;
277
278   if (unlikely(INTEL_DEBUG & DEBUG_DRI))
279      fprintf(stderr, "enter %s, drawable %p\n", __func__, drawable);
280
281   intel_query_dri2_buffers(intel, drawable, &buffers, &count);
282
283   if (buffers == NULL)
284      return;
285
286   for (i = 0; i < count; i++) {
287       switch (buffers[i].attachment) {
288       case __DRI_BUFFER_FRONT_LEFT:
289	   rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
290	   region_name = "dri2 front buffer";
291	   break;
292
293       case __DRI_BUFFER_FAKE_FRONT_LEFT:
294	   rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
295	   region_name = "dri2 fake front buffer";
296	   break;
297
298       case __DRI_BUFFER_BACK_LEFT:
299	   rb = intel_get_renderbuffer(fb, BUFFER_BACK_LEFT);
300	   region_name = "dri2 back buffer";
301	   break;
302
303       case __DRI_BUFFER_DEPTH:
304       case __DRI_BUFFER_HIZ:
305       case __DRI_BUFFER_DEPTH_STENCIL:
306       case __DRI_BUFFER_STENCIL:
307       case __DRI_BUFFER_ACCUM:
308       default:
309	   fprintf(stderr,
310		   "unhandled buffer attach event, attachment type %d\n",
311		   buffers[i].attachment);
312	   return;
313       }
314
315       intel_process_dri2_buffer(intel, drawable, &buffers[i], rb, region_name);
316   }
317
318   driUpdateFramebufferSize(&intel->ctx, drawable);
319}
320
321/**
322 * intel_prepare_render should be called anywhere that curent read/drawbuffer
323 * state is required.
324 */
325void
326intel_prepare_render(struct intel_context *intel)
327{
328   __DRIcontext *driContext = intel->driContext;
329   __DRIdrawable *drawable;
330
331   drawable = driContext->driDrawablePriv;
332   if (drawable && drawable->dri2.stamp != driContext->dri2.draw_stamp) {
333      if (drawable->lastStamp != drawable->dri2.stamp)
334	 intel_update_renderbuffers(driContext, drawable);
335      intel_draw_buffer(&intel->ctx);
336      driContext->dri2.draw_stamp = drawable->dri2.stamp;
337   }
338
339   drawable = driContext->driReadablePriv;
340   if (drawable && drawable->dri2.stamp != driContext->dri2.read_stamp) {
341      if (drawable->lastStamp != drawable->dri2.stamp)
342	 intel_update_renderbuffers(driContext, drawable);
343      driContext->dri2.read_stamp = drawable->dri2.stamp;
344   }
345
346   /* If we're currently rendering to the front buffer, the rendering
347    * that will happen next will probably dirty the front buffer.  So
348    * mark it as dirty here.
349    */
350   if (intel->is_front_buffer_rendering)
351      intel->front_buffer_dirty = true;
352
353   /* Wait for the swapbuffers before the one we just emitted, so we
354    * don't get too many swaps outstanding for apps that are GPU-heavy
355    * but not CPU-heavy.
356    *
357    * We're using intelDRI2Flush (called from the loader before
358    * swapbuffer) and glFlush (for front buffer rendering) as the
359    * indicator that a frame is done and then throttle when we get
360    * here as we prepare to render the next frame.  At this point for
361    * round trips for swap/copy and getting new buffers are done and
362    * we'll spend less time waiting on the GPU.
363    *
364    * Unfortunately, we don't have a handle to the batch containing
365    * the swap, and getting our hands on that doesn't seem worth it,
366    * so we just us the first batch we emitted after the last swap.
367    */
368   if (intel->need_throttle && intel->first_post_swapbuffers_batch) {
369      drm_intel_bo_wait_rendering(intel->first_post_swapbuffers_batch);
370      drm_intel_bo_unreference(intel->first_post_swapbuffers_batch);
371      intel->first_post_swapbuffers_batch = NULL;
372      intel->need_throttle = false;
373   }
374}
375
376static void
377intel_viewport(struct gl_context *ctx, GLint x, GLint y, GLsizei w, GLsizei h)
378{
379    struct intel_context *intel = intel_context(ctx);
380    __DRIcontext *driContext = intel->driContext;
381
382    if (intel->saved_viewport)
383	intel->saved_viewport(ctx, x, y, w, h);
384
385    if (_mesa_is_winsys_fbo(ctx->DrawBuffer)) {
386       dri2InvalidateDrawable(driContext->driDrawablePriv);
387       dri2InvalidateDrawable(driContext->driReadablePriv);
388    }
389}
390
391static const struct dri_debug_control debug_control[] = {
392   { "tex",   DEBUG_TEXTURE},
393   { "state", DEBUG_STATE},
394   { "ioctl", DEBUG_IOCTL},
395   { "blit",  DEBUG_BLIT},
396   { "mip",   DEBUG_MIPTREE},
397   { "fall",  DEBUG_FALLBACKS},
398   { "verb",  DEBUG_VERBOSE},
399   { "bat",   DEBUG_BATCH},
400   { "pix",   DEBUG_PIXEL},
401   { "buf",   DEBUG_BUFMGR},
402   { "reg",   DEBUG_REGION},
403   { "fbo",   DEBUG_FBO},
404   { "gs",    DEBUG_GS},
405   { "sync",  DEBUG_SYNC},
406   { "prim",  DEBUG_PRIMS },
407   { "vert",  DEBUG_VERTS },
408   { "dri",   DEBUG_DRI },
409   { "sf",    DEBUG_SF },
410   { "san",   DEBUG_SANITY },
411   { "sleep", DEBUG_SLEEP },
412   { "stats", DEBUG_STATS },
413   { "tile",  DEBUG_TILE },
414   { "wm",    DEBUG_WM },
415   { "urb",   DEBUG_URB },
416   { "vs",    DEBUG_VS },
417   { "clip",  DEBUG_CLIP },
418   { "aub",   DEBUG_AUB },
419   { NULL,    0 }
420};
421
422
423static void
424intelInvalidateState(struct gl_context * ctx, GLuint new_state)
425{
426    struct intel_context *intel = intel_context(ctx);
427
428   _swrast_InvalidateState(ctx, new_state);
429   _vbo_InvalidateState(ctx, new_state);
430
431   intel->NewGLState |= new_state;
432
433   if (intel->vtbl.invalidate_state)
434      intel->vtbl.invalidate_state( intel, new_state );
435}
436
437void
438intel_flush_rendering_to_batch(struct gl_context *ctx)
439{
440   struct intel_context *intel = intel_context(ctx);
441
442   if (intel->Fallback)
443      _swrast_flush(ctx);
444
445   if (intel->gen < 4)
446      INTEL_FIREVERTICES(intel);
447}
448
449void
450_intel_flush(struct gl_context *ctx, const char *file, int line)
451{
452   struct intel_context *intel = intel_context(ctx);
453
454   intel_flush_rendering_to_batch(ctx);
455
456   if (intel->batch.used)
457      _intel_batchbuffer_flush(intel, file, line);
458}
459
460static void
461intel_glFlush(struct gl_context *ctx)
462{
463   struct intel_context *intel = intel_context(ctx);
464
465   intel_flush(ctx);
466   intel_flush_front(ctx);
467   if (intel->is_front_buffer_rendering)
468      intel->need_throttle = true;
469}
470
471void
472intelFinish(struct gl_context * ctx)
473{
474   struct intel_context *intel = intel_context(ctx);
475
476   intel_flush(ctx);
477   intel_flush_front(ctx);
478
479   if (intel->batch.last_bo)
480      drm_intel_bo_wait_rendering(intel->batch.last_bo);
481}
482
483void
484intelInitDriverFunctions(struct dd_function_table *functions)
485{
486   _mesa_init_driver_functions(functions);
487
488   functions->Flush = intel_glFlush;
489   functions->Finish = intelFinish;
490   functions->GetString = intelGetString;
491   functions->UpdateState = intelInvalidateState;
492
493   intelInitTextureFuncs(functions);
494   intelInitTextureImageFuncs(functions);
495   intelInitTextureSubImageFuncs(functions);
496   intelInitTextureCopyImageFuncs(functions);
497   intelInitStateFuncs(functions);
498   intelInitClearFuncs(functions);
499   intelInitBufferFuncs(functions);
500   intelInitPixelFuncs(functions);
501   intelInitBufferObjectFuncs(functions);
502   intel_init_syncobj_functions(functions);
503}
504
505bool
506intelInitContext(struct intel_context *intel,
507		 int api,
508                 const struct gl_config * mesaVis,
509                 __DRIcontext * driContextPriv,
510                 void *sharedContextPrivate,
511                 struct dd_function_table *functions)
512{
513   struct gl_context *ctx = &intel->ctx;
514   struct gl_context *shareCtx = (struct gl_context *) sharedContextPrivate;
515   __DRIscreen *sPriv = driContextPriv->driScreenPriv;
516   struct intel_screen *intelScreen = sPriv->driverPrivate;
517   int bo_reuse_mode;
518   struct gl_config visual;
519
520   /* we can't do anything without a connection to the device */
521   if (intelScreen->bufmgr == NULL)
522      return false;
523
524   /* Can't rely on invalidate events, fall back to glViewport hack */
525   if (!driContextPriv->driScreenPriv->dri2.useInvalidate) {
526      intel->saved_viewport = functions->Viewport;
527      functions->Viewport = intel_viewport;
528   }
529
530   if (mesaVis == NULL) {
531      memset(&visual, 0, sizeof visual);
532      mesaVis = &visual;
533   }
534
535   if (!_mesa_initialize_context(&intel->ctx, api, mesaVis, shareCtx,
536                                 functions, (void *) intel)) {
537      printf("%s: failed to init mesa context\n", __FUNCTION__);
538      return false;
539   }
540
541   driContextPriv->driverPrivate = intel;
542   intel->intelScreen = intelScreen;
543   intel->driContext = driContextPriv;
544   intel->driFd = sPriv->fd;
545
546   intel->gen = intelScreen->gen;
547
548   const int devID = intelScreen->deviceID;
549   if (IS_SNB_GT1(devID) || IS_IVB_GT1(devID) || IS_HSW_GT1(devID))
550      intel->gt = 1;
551   else if (IS_SNB_GT2(devID) || IS_IVB_GT2(devID) || IS_HSW_GT2(devID))
552      intel->gt = 2;
553   else
554      intel->gt = 0;
555
556   if (IS_HASWELL(devID)) {
557      intel->is_haswell = true;
558   } else if (IS_G4X(devID)) {
559      intel->is_g4x = true;
560   } else if (IS_945(devID)) {
561      intel->is_945 = true;
562   }
563
564   if (intel->gen >= 5) {
565      intel->needs_ff_sync = true;
566   }
567
568   intel->has_separate_stencil = intel->intelScreen->hw_has_separate_stencil;
569   intel->must_use_separate_stencil = intel->intelScreen->hw_must_use_separate_stencil;
570   intel->has_hiz = intel->gen >= 6 && !intel->is_haswell;
571   intel->has_llc = intel->intelScreen->hw_has_llc;
572   intel->has_swizzling = intel->intelScreen->hw_has_swizzling;
573
574   memset(&ctx->TextureFormatSupported,
575	  0, sizeof(ctx->TextureFormatSupported));
576
577   driParseConfigFiles(&intel->optionCache, &intelScreen->optionCache,
578                       sPriv->myNum, (intel->gen >= 4) ? "i965" : "i915");
579   if (intel->gen < 4)
580      intel->maxBatchSize = 4096;
581   else
582      intel->maxBatchSize = sizeof(intel->batch.map);
583
584   intel->bufmgr = intelScreen->bufmgr;
585
586   bo_reuse_mode = driQueryOptioni(&intel->optionCache, "bo_reuse");
587   switch (bo_reuse_mode) {
588   case DRI_CONF_BO_REUSE_DISABLED:
589      break;
590   case DRI_CONF_BO_REUSE_ALL:
591      intel_bufmgr_gem_enable_reuse(intel->bufmgr);
592      break;
593   }
594
595   ctx->Const.MinLineWidth = 1.0;
596   ctx->Const.MinLineWidthAA = 1.0;
597   ctx->Const.MaxLineWidth = 5.0;
598   ctx->Const.MaxLineWidthAA = 5.0;
599   ctx->Const.LineWidthGranularity = 0.5;
600
601   ctx->Const.MinPointSize = 1.0;
602   ctx->Const.MinPointSizeAA = 1.0;
603   ctx->Const.MaxPointSize = 255.0;
604   ctx->Const.MaxPointSizeAA = 3.0;
605   ctx->Const.PointSizeGranularity = 1.0;
606
607   ctx->Const.MaxSamples = 1.0;
608
609   if (intel->gen >= 6)
610      ctx->Const.MaxClipPlanes = 8;
611
612   ctx->Const.StripTextureBorder = GL_TRUE;
613
614   /* reinitialize the context point state.
615    * It depend on constants in __struct gl_contextRec::Const
616    */
617   _mesa_init_point(ctx);
618
619   if (intel->gen >= 4) {
620      ctx->Const.MaxRenderbufferSize = 8192;
621   } else {
622      ctx->Const.MaxRenderbufferSize = 2048;
623   }
624
625   /* Initialize the software rasterizer and helper modules. */
626   _swrast_CreateContext(ctx);
627   _vbo_CreateContext(ctx);
628   _tnl_CreateContext(ctx);
629   _swsetup_CreateContext(ctx);
630
631   /* Configure swrast to match hardware characteristics: */
632   _swrast_allow_pixel_fog(ctx, false);
633   _swrast_allow_vertex_fog(ctx, true);
634
635   _mesa_meta_init(ctx);
636
637   intel->hw_stencil = mesaVis->stencilBits && mesaVis->depthBits == 24;
638   intel->hw_stipple = 1;
639
640   /* XXX FBO: this doesn't seem to be used anywhere */
641   switch (mesaVis->depthBits) {
642   case 0:                     /* what to do in this case? */
643   case 16:
644      intel->polygon_offset_scale = 1.0;
645      break;
646   case 24:
647      intel->polygon_offset_scale = 2.0;     /* req'd to pass glean */
648      break;
649   default:
650      assert(0);
651      break;
652   }
653
654   if (intel->gen >= 4)
655      intel->polygon_offset_scale /= 0xffff;
656
657   intel->RenderIndex = ~0;
658
659   intelInitExtensions(ctx);
660
661   INTEL_DEBUG = driParseDebugString(getenv("INTEL_DEBUG"), debug_control);
662   if (INTEL_DEBUG & DEBUG_BUFMGR)
663      dri_bufmgr_set_debug(intel->bufmgr, true);
664
665   if (INTEL_DEBUG & DEBUG_AUB)
666      drm_intel_bufmgr_gem_set_aub_dump(intel->bufmgr, true);
667
668   intel_batchbuffer_init(intel);
669
670   intel_fbo_init(intel);
671
672   intel->use_texture_tiling = driQueryOptionb(&intel->optionCache,
673					       "texture_tiling");
674   intel->use_early_z = driQueryOptionb(&intel->optionCache, "early_z");
675
676   if (!driQueryOptionb(&intel->optionCache, "hiz")) {
677       intel->has_hiz = false;
678       /* On gen6, you can only do separate stencil with HIZ. */
679       if (intel->gen == 6)
680	  intel->has_separate_stencil = false;
681   }
682
683   intel->prim.primitive = ~0;
684
685   /* Force all software fallbacks */
686   if (driQueryOptionb(&intel->optionCache, "no_rast")) {
687      fprintf(stderr, "disabling 3D rasterization\n");
688      intel->no_rast = 1;
689   }
690
691   if (driQueryOptionb(&intel->optionCache, "always_flush_batch")) {
692      fprintf(stderr, "flushing batchbuffer before/after each draw call\n");
693      intel->always_flush_batch = 1;
694   }
695
696   if (driQueryOptionb(&intel->optionCache, "always_flush_cache")) {
697      fprintf(stderr, "flushing GPU caches before/after each draw call\n");
698      intel->always_flush_cache = 1;
699   }
700
701   return true;
702}
703
704void
705intelDestroyContext(__DRIcontext * driContextPriv)
706{
707   struct intel_context *intel =
708      (struct intel_context *) driContextPriv->driverPrivate;
709
710   assert(intel);               /* should never be null */
711   if (intel) {
712      INTEL_FIREVERTICES(intel);
713
714      /* Dump a final BMP in case the application doesn't call SwapBuffers */
715      if (INTEL_DEBUG & DEBUG_AUB) {
716         intel_batchbuffer_flush(intel);
717	 aub_dump_bmp(&intel->ctx);
718      }
719
720      _mesa_meta_free(&intel->ctx);
721
722      intel->vtbl.destroy(intel);
723
724      _swsetup_DestroyContext(&intel->ctx);
725      _tnl_DestroyContext(&intel->ctx);
726      _vbo_DestroyContext(&intel->ctx);
727
728      _swrast_DestroyContext(&intel->ctx);
729      intel->Fallback = 0x0;      /* don't call _swrast_Flush later */
730
731      intel_batchbuffer_free(intel);
732
733      free(intel->prim.vb);
734      intel->prim.vb = NULL;
735      drm_intel_bo_unreference(intel->prim.vb_bo);
736      intel->prim.vb_bo = NULL;
737      drm_intel_bo_unreference(intel->first_post_swapbuffers_batch);
738      intel->first_post_swapbuffers_batch = NULL;
739
740      driDestroyOptionCache(&intel->optionCache);
741
742      /* free the Mesa context */
743      _mesa_free_context_data(&intel->ctx);
744
745      _math_matrix_dtr(&intel->ViewportMatrix);
746
747      ralloc_free(intel);
748      driContextPriv->driverPrivate = NULL;
749   }
750}
751
752GLboolean
753intelUnbindContext(__DRIcontext * driContextPriv)
754{
755   /* Unset current context and dispath table */
756   _mesa_make_current(NULL, NULL, NULL);
757
758   return true;
759}
760
761GLboolean
762intelMakeCurrent(__DRIcontext * driContextPriv,
763                 __DRIdrawable * driDrawPriv,
764                 __DRIdrawable * driReadPriv)
765{
766   struct intel_context *intel;
767   GET_CURRENT_CONTEXT(curCtx);
768
769   if (driContextPriv)
770      intel = (struct intel_context *) driContextPriv->driverPrivate;
771   else
772      intel = NULL;
773
774   /* According to the glXMakeCurrent() man page: "Pending commands to
775    * the previous context, if any, are flushed before it is released."
776    * But only flush if we're actually changing contexts.
777    */
778   if (intel_context(curCtx) && intel_context(curCtx) != intel) {
779      _mesa_flush(curCtx);
780   }
781
782   if (driContextPriv) {
783      struct gl_framebuffer *fb, *readFb;
784
785      if (driDrawPriv == NULL && driReadPriv == NULL) {
786	 fb = _mesa_get_incomplete_framebuffer();
787	 readFb = _mesa_get_incomplete_framebuffer();
788      } else {
789	 fb = driDrawPriv->driverPrivate;
790	 readFb = driReadPriv->driverPrivate;
791	 driContextPriv->dri2.draw_stamp = driDrawPriv->dri2.stamp - 1;
792	 driContextPriv->dri2.read_stamp = driReadPriv->dri2.stamp - 1;
793      }
794
795      intel_prepare_render(intel);
796      _mesa_make_current(&intel->ctx, fb, readFb);
797
798      /* We do this in intel_prepare_render() too, but intel->ctx.DrawBuffer
799       * is NULL at that point.  We can't call _mesa_makecurrent()
800       * first, since we need the buffer size for the initial
801       * viewport.  So just call intel_draw_buffer() again here. */
802      intel_draw_buffer(&intel->ctx);
803   }
804   else {
805      _mesa_make_current(NULL, NULL, NULL);
806   }
807
808   return true;
809}
810
811/**
812 * \brief Query DRI2 to obtain a DRIdrawable's buffers.
813 *
814 * To determine which DRI buffers to request, examine the renderbuffers
815 * attached to the drawable's framebuffer. Then request the buffers with
816 * DRI2GetBuffers() or DRI2GetBuffersWithFormat().
817 *
818 * This is called from intel_update_renderbuffers().
819 *
820 * \param drawable      Drawable whose buffers are queried.
821 * \param buffers       [out] List of buffers returned by DRI2 query.
822 * \param buffer_count  [out] Number of buffers returned.
823 *
824 * \see intel_update_renderbuffers()
825 * \see DRI2GetBuffers()
826 * \see DRI2GetBuffersWithFormat()
827 */
828static void
829intel_query_dri2_buffers(struct intel_context *intel,
830			 __DRIdrawable *drawable,
831			 __DRIbuffer **buffers,
832			 int *buffer_count)
833{
834   __DRIscreen *screen = intel->intelScreen->driScrnPriv;
835   struct gl_framebuffer *fb = drawable->driverPrivate;
836   int i = 0;
837   const int max_attachments = 4;
838   unsigned *attachments = calloc(2 * max_attachments, sizeof(unsigned));
839
840   struct intel_renderbuffer *front_rb;
841   struct intel_renderbuffer *back_rb;
842
843   front_rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
844   back_rb = intel_get_renderbuffer(fb, BUFFER_BACK_LEFT);
845
846   if ((intel->is_front_buffer_rendering ||
847	intel->is_front_buffer_reading ||
848	!back_rb) && front_rb) {
849      attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
850      attachments[i++] = intel_bits_per_pixel(front_rb);
851   }
852
853   if (back_rb) {
854      attachments[i++] = __DRI_BUFFER_BACK_LEFT;
855      attachments[i++] = intel_bits_per_pixel(back_rb);
856   }
857
858   assert(i <= 2 * max_attachments);
859
860   *buffers = screen->dri2.loader->getBuffersWithFormat(drawable,
861							&drawable->w,
862							&drawable->h,
863							attachments, i / 2,
864							buffer_count,
865							drawable->loaderPrivate);
866   free(attachments);
867}
868
869/**
870 * \brief Assign a DRI buffer's DRM region to a renderbuffer.
871 *
872 * This is called from intel_update_renderbuffers().
873 *
874 * \par Note:
875 *    DRI buffers whose attachment point is DRI2BufferStencil or
876 *    DRI2BufferDepthStencil are handled as special cases.
877 *
878 * \param buffer_name is a human readable name, such as "dri2 front buffer",
879 *        that is passed to intel_region_alloc_for_handle().
880 *
881 * \see intel_update_renderbuffers()
882 * \see intel_region_alloc_for_handle()
883 */
884static void
885intel_process_dri2_buffer(struct intel_context *intel,
886			  __DRIdrawable *drawable,
887			  __DRIbuffer *buffer,
888			  struct intel_renderbuffer *rb,
889			  const char *buffer_name)
890{
891   struct intel_region *region = NULL;
892
893   if (!rb)
894      return;
895
896   if (rb->mt &&
897       rb->mt->region &&
898       rb->mt->region->name == buffer->name)
899      return;
900
901   if (unlikely(INTEL_DEBUG & DEBUG_DRI)) {
902      fprintf(stderr,
903	      "attaching buffer %d, at %d, cpp %d, pitch %d\n",
904	      buffer->name, buffer->attachment,
905	      buffer->cpp, buffer->pitch);
906   }
907
908   intel_miptree_release(&rb->mt);
909   region = intel_region_alloc_for_handle(intel->intelScreen,
910                                          buffer->cpp,
911                                          drawable->w,
912                                          drawable->h,
913                                          buffer->pitch / buffer->cpp,
914                                          buffer->name,
915                                          buffer_name);
916   if (!region)
917      return;
918
919   rb->mt = intel_miptree_create_for_region(intel,
920                                            GL_TEXTURE_2D,
921                                            intel_rb_format(rb),
922                                            region);
923   intel_region_release(&region);
924}
925