brw_context.c revision ebf91993c18bdf90c4699b42e58cb84d0b160f25
1/*
2 Copyright 2003 VMware, Inc.
3 Copyright (C) Intel Corp.  2006.  All Rights Reserved.
4 Intel funded Tungsten Graphics to
5 develop this 3D driver.
6
7 Permission is hereby granted, free of charge, to any person obtaining
8 a copy of this software and associated documentation files (the
9 "Software"), to deal in the Software without restriction, including
10 without limitation the rights to use, copy, modify, merge, publish,
11 distribute, sublicense, and/or sell copies of the Software, and to
12 permit persons to whom the Software is furnished to do so, subject to
13 the following conditions:
14
15 The above copyright notice and this permission notice (including the
16 next paragraph) shall be included in all copies or substantial
17 portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
23 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
27 **********************************************************************/
28 /*
29  * Authors:
30  *   Keith Whitwell <keithw@vmware.com>
31  */
32
33
34#include "main/api_exec.h"
35#include "main/context.h"
36#include "main/fbobject.h"
37#include "main/imports.h"
38#include "main/macros.h"
39#include "main/points.h"
40#include "main/version.h"
41#include "main/vtxfmt.h"
42
43#include "vbo/vbo_context.h"
44
45#include "drivers/common/driverfuncs.h"
46#include "drivers/common/meta.h"
47#include "utils.h"
48
49#include "brw_context.h"
50#include "brw_defines.h"
51#include "brw_draw.h"
52#include "brw_state.h"
53
54#include "intel_batchbuffer.h"
55#include "intel_buffer_objects.h"
56#include "intel_buffers.h"
57#include "intel_fbo.h"
58#include "intel_mipmap_tree.h"
59#include "intel_pixel.h"
60#include "intel_regions.h"
61#include "intel_tex.h"
62#include "intel_tex_obj.h"
63
64#include "swrast_setup/swrast_setup.h"
65#include "tnl/tnl.h"
66#include "tnl/t_pipeline.h"
67#include "glsl/ralloc.h"
68
69/***************************************
70 * Mesa's Driver Functions
71 ***************************************/
72
73static size_t
74brw_query_samples_for_format(struct gl_context *ctx, GLenum target,
75                             GLenum internalFormat, int samples[16])
76{
77   struct brw_context *brw = brw_context(ctx);
78
79   (void) target;
80
81   switch (brw->gen) {
82   case 7:
83      samples[0] = 8;
84      samples[1] = 4;
85      return 2;
86
87   case 6:
88      samples[0] = 4;
89      return 1;
90
91   default:
92      samples[0] = 1;
93      return 1;
94   }
95}
96
97const char *const brw_vendor_string = "Intel Open Source Technology Center";
98
99const char *
100brw_get_renderer_string(unsigned deviceID)
101{
102   const char *chipset;
103   static char buffer[128];
104
105   switch (deviceID) {
106#undef CHIPSET
107#define CHIPSET(id, symbol, str) case id: chipset = str; break;
108#include "pci_ids/i965_pci_ids.h"
109   default:
110      chipset = "Unknown Intel Chipset";
111      break;
112   }
113
114   (void) driGetRendererString(buffer, chipset, 0);
115   return buffer;
116}
117
118static const GLubyte *
119intelGetString(struct gl_context * ctx, GLenum name)
120{
121   const struct brw_context *const brw = brw_context(ctx);
122
123   switch (name) {
124   case GL_VENDOR:
125      return (GLubyte *) brw_vendor_string;
126
127   case GL_RENDERER:
128      return
129         (GLubyte *) brw_get_renderer_string(brw->intelScreen->deviceID);
130
131   default:
132      return NULL;
133   }
134}
135
136static void
137intel_viewport(struct gl_context *ctx)
138{
139   struct brw_context *brw = brw_context(ctx);
140   __DRIcontext *driContext = brw->driContext;
141
142   if (_mesa_is_winsys_fbo(ctx->DrawBuffer)) {
143      dri2InvalidateDrawable(driContext->driDrawablePriv);
144      dri2InvalidateDrawable(driContext->driReadablePriv);
145   }
146}
147
148static void
149intelInvalidateState(struct gl_context * ctx, GLuint new_state)
150{
151   struct brw_context *brw = brw_context(ctx);
152
153   if (ctx->swrast_context)
154      _swrast_InvalidateState(ctx, new_state);
155   _vbo_InvalidateState(ctx, new_state);
156
157   brw->NewGLState |= new_state;
158}
159
160#define flushFront(screen)      ((screen)->image.loader ? (screen)->image.loader->flushFrontBuffer : (screen)->dri2.loader->flushFrontBuffer)
161
162static void
163intel_flush_front(struct gl_context *ctx)
164{
165   struct brw_context *brw = brw_context(ctx);
166   __DRIcontext *driContext = brw->driContext;
167   __DRIdrawable *driDrawable = driContext->driDrawablePriv;
168   __DRIscreen *const screen = brw->intelScreen->driScrnPriv;
169
170   if (brw->front_buffer_dirty && _mesa_is_winsys_fbo(ctx->DrawBuffer)) {
171      if (flushFront(screen) && driDrawable &&
172          driDrawable->loaderPrivate) {
173
174         /* Resolve before flushing FAKE_FRONT_LEFT to FRONT_LEFT.
175          *
176          * This potentially resolves both front and back buffer. It
177          * is unnecessary to resolve the back, but harms nothing except
178          * performance. And no one cares about front-buffer render
179          * performance.
180          */
181         intel_resolve_for_dri2_flush(brw, driDrawable);
182         intel_batchbuffer_flush(brw);
183
184         flushFront(screen)(driDrawable, driDrawable->loaderPrivate);
185
186         /* We set the dirty bit in intel_prepare_render() if we're
187          * front buffer rendering once we get there.
188          */
189         brw->front_buffer_dirty = false;
190      }
191   }
192}
193
194static void
195intel_glFlush(struct gl_context *ctx)
196{
197   struct brw_context *brw = brw_context(ctx);
198
199   intel_batchbuffer_flush(brw);
200   intel_flush_front(ctx);
201   if (brw->is_front_buffer_rendering)
202      brw->need_throttle = true;
203}
204
205void
206intelFinish(struct gl_context * ctx)
207{
208   struct brw_context *brw = brw_context(ctx);
209
210   intel_glFlush(ctx);
211
212   if (brw->batch.last_bo)
213      drm_intel_bo_wait_rendering(brw->batch.last_bo);
214}
215
216static void
217brw_init_driver_functions(struct brw_context *brw,
218                          struct dd_function_table *functions)
219{
220   _mesa_init_driver_functions(functions);
221
222   /* GLX uses DRI2 invalidate events to handle window resizing.
223    * Unfortunately, EGL does not - libEGL is written in XCB (not Xlib),
224    * which doesn't provide a mechanism for snooping the event queues.
225    *
226    * So EGL still relies on viewport hacks to handle window resizing.
227    * This should go away with DRI3000.
228    */
229   if (!brw->driContext->driScreenPriv->dri2.useInvalidate)
230      functions->Viewport = intel_viewport;
231
232   functions->Flush = intel_glFlush;
233   functions->Finish = intelFinish;
234   functions->GetString = intelGetString;
235   functions->UpdateState = intelInvalidateState;
236
237   intelInitTextureFuncs(functions);
238   intelInitTextureImageFuncs(functions);
239   intelInitTextureSubImageFuncs(functions);
240   intelInitTextureCopyImageFuncs(functions);
241   intelInitClearFuncs(functions);
242   intelInitBufferFuncs(functions);
243   intelInitPixelFuncs(functions);
244   intelInitBufferObjectFuncs(functions);
245   intel_init_syncobj_functions(functions);
246   brw_init_object_purgeable_functions(functions);
247
248   brwInitFragProgFuncs( functions );
249   brw_init_common_queryobj_functions(functions);
250   if (brw->gen >= 6)
251      gen6_init_queryobj_functions(functions);
252   else
253      gen4_init_queryobj_functions(functions);
254
255   functions->QuerySamplesForFormat = brw_query_samples_for_format;
256
257   functions->NewTransformFeedback = brw_new_transform_feedback;
258   functions->DeleteTransformFeedback = brw_delete_transform_feedback;
259   functions->GetTransformFeedbackVertexCount =
260      brw_get_transform_feedback_vertex_count;
261   if (brw->gen >= 7) {
262      functions->BeginTransformFeedback = gen7_begin_transform_feedback;
263      functions->EndTransformFeedback = gen7_end_transform_feedback;
264      functions->PauseTransformFeedback = gen7_pause_transform_feedback;
265      functions->ResumeTransformFeedback = gen7_resume_transform_feedback;
266   } else {
267      functions->BeginTransformFeedback = brw_begin_transform_feedback;
268      functions->EndTransformFeedback = brw_end_transform_feedback;
269   }
270
271   if (brw->gen >= 6)
272      functions->GetSamplePosition = gen6_get_sample_position;
273}
274
275static void
276brw_initialize_context_constants(struct brw_context *brw)
277{
278   struct gl_context *ctx = &brw->ctx;
279
280   ctx->Const.QueryCounterBits.Timestamp = 36;
281
282   ctx->Const.StripTextureBorder = true;
283
284   ctx->Const.MaxDualSourceDrawBuffers = 1;
285   ctx->Const.MaxDrawBuffers = BRW_MAX_DRAW_BUFFERS;
286   ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = BRW_MAX_TEX_UNIT;
287   ctx->Const.MaxTextureCoordUnits = 8; /* Mesa limit */
288   ctx->Const.MaxTextureUnits =
289      MIN2(ctx->Const.MaxTextureCoordUnits,
290           ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits);
291   ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = BRW_MAX_TEX_UNIT;
292   if (brw->gen >= 7)
293      ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxTextureImageUnits = BRW_MAX_TEX_UNIT;
294   else
295      ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxTextureImageUnits = 0;
296   ctx->Const.MaxCombinedTextureImageUnits =
297      ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits +
298      ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits +
299      ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxTextureImageUnits;
300
301   ctx->Const.MaxTextureLevels = 14; /* 8192 */
302   if (ctx->Const.MaxTextureLevels > MAX_TEXTURE_LEVELS)
303      ctx->Const.MaxTextureLevels = MAX_TEXTURE_LEVELS;
304   ctx->Const.Max3DTextureLevels = 9;
305   ctx->Const.MaxCubeTextureLevels = 12;
306
307   if (brw->gen >= 7)
308      ctx->Const.MaxArrayTextureLayers = 2048;
309   else
310      ctx->Const.MaxArrayTextureLayers = 512;
311
312   ctx->Const.MaxTextureRectSize = 1 << 12;
313
314   ctx->Const.MaxTextureMaxAnisotropy = 16.0;
315
316   ctx->Const.MaxRenderbufferSize = 8192;
317
318   /* Hardware only supports a limited number of transform feedback buffers.
319    * So we need to override the Mesa default (which is based only on software
320    * limits).
321    */
322   ctx->Const.MaxTransformFeedbackBuffers = BRW_MAX_SOL_BUFFERS;
323
324   /* On Gen6, in the worst case, we use up one binding table entry per
325    * transform feedback component (see comments above the definition of
326    * BRW_MAX_SOL_BINDINGS, in brw_context.h), so we need to advertise a value
327    * for MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS equal to
328    * BRW_MAX_SOL_BINDINGS.
329    *
330    * In "separate components" mode, we need to divide this value by
331    * BRW_MAX_SOL_BUFFERS, so that the total number of binding table entries
332    * used up by all buffers will not exceed BRW_MAX_SOL_BINDINGS.
333    */
334   ctx->Const.MaxTransformFeedbackInterleavedComponents = BRW_MAX_SOL_BINDINGS;
335   ctx->Const.MaxTransformFeedbackSeparateComponents =
336      BRW_MAX_SOL_BINDINGS / BRW_MAX_SOL_BUFFERS;
337
338   ctx->Const.AlwaysUseGetTransformFeedbackVertexCount = true;
339
340   int max_samples;
341   const int *msaa_modes = intel_supported_msaa_modes(brw->intelScreen);
342   const int clamp_max_samples =
343      driQueryOptioni(&brw->optionCache, "clamp_max_samples");
344
345   if (clamp_max_samples < 0) {
346      max_samples = msaa_modes[0];
347   } else {
348      /* Select the largest supported MSAA mode that does not exceed
349       * clamp_max_samples.
350       */
351      max_samples = 0;
352      for (int i = 0; msaa_modes[i] != 0; ++i) {
353         if (msaa_modes[i] <= clamp_max_samples) {
354            max_samples = msaa_modes[i];
355            break;
356         }
357      }
358   }
359
360   ctx->Const.MaxSamples = max_samples;
361   ctx->Const.MaxColorTextureSamples = max_samples;
362   ctx->Const.MaxDepthTextureSamples = max_samples;
363   ctx->Const.MaxIntegerSamples = max_samples;
364
365   if (brw->gen >= 7)
366      ctx->Const.MaxProgramTextureGatherComponents = 4;
367
368   ctx->Const.MinLineWidth = 1.0;
369   ctx->Const.MinLineWidthAA = 1.0;
370   ctx->Const.MaxLineWidth = 5.0;
371   ctx->Const.MaxLineWidthAA = 5.0;
372   ctx->Const.LineWidthGranularity = 0.5;
373
374   ctx->Const.MinPointSize = 1.0;
375   ctx->Const.MinPointSizeAA = 1.0;
376   ctx->Const.MaxPointSize = 255.0;
377   ctx->Const.MaxPointSizeAA = 255.0;
378   ctx->Const.PointSizeGranularity = 1.0;
379
380   if (brw->gen >= 5 || brw->is_g4x)
381      ctx->Const.MaxClipPlanes = 8;
382
383   ctx->Const.Program[MESA_SHADER_VERTEX].MaxNativeInstructions = 16 * 1024;
384   ctx->Const.Program[MESA_SHADER_VERTEX].MaxAluInstructions = 0;
385   ctx->Const.Program[MESA_SHADER_VERTEX].MaxTexInstructions = 0;
386   ctx->Const.Program[MESA_SHADER_VERTEX].MaxTexIndirections = 0;
387   ctx->Const.Program[MESA_SHADER_VERTEX].MaxNativeAluInstructions = 0;
388   ctx->Const.Program[MESA_SHADER_VERTEX].MaxNativeTexInstructions = 0;
389   ctx->Const.Program[MESA_SHADER_VERTEX].MaxNativeTexIndirections = 0;
390   ctx->Const.Program[MESA_SHADER_VERTEX].MaxNativeAttribs = 16;
391   ctx->Const.Program[MESA_SHADER_VERTEX].MaxNativeTemps = 256;
392   ctx->Const.Program[MESA_SHADER_VERTEX].MaxNativeAddressRegs = 1;
393   ctx->Const.Program[MESA_SHADER_VERTEX].MaxNativeParameters = 1024;
394   ctx->Const.Program[MESA_SHADER_VERTEX].MaxEnvParams =
395      MIN2(ctx->Const.Program[MESA_SHADER_VERTEX].MaxNativeParameters,
396	   ctx->Const.Program[MESA_SHADER_VERTEX].MaxEnvParams);
397
398   ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxNativeInstructions = 1024;
399   ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxNativeAluInstructions = 1024;
400   ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxNativeTexInstructions = 1024;
401   ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxNativeTexIndirections = 1024;
402   ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxNativeAttribs = 12;
403   ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxNativeTemps = 256;
404   ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxNativeAddressRegs = 0;
405   ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxNativeParameters = 1024;
406   ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxEnvParams =
407      MIN2(ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxNativeParameters,
408	   ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxEnvParams);
409
410   /* Fragment shaders use real, 32-bit twos-complement integers for all
411    * integer types.
412    */
413   ctx->Const.Program[MESA_SHADER_FRAGMENT].LowInt.RangeMin = 31;
414   ctx->Const.Program[MESA_SHADER_FRAGMENT].LowInt.RangeMax = 30;
415   ctx->Const.Program[MESA_SHADER_FRAGMENT].LowInt.Precision = 0;
416   ctx->Const.Program[MESA_SHADER_FRAGMENT].HighInt = ctx->Const.Program[MESA_SHADER_FRAGMENT].LowInt;
417   ctx->Const.Program[MESA_SHADER_FRAGMENT].MediumInt = ctx->Const.Program[MESA_SHADER_FRAGMENT].LowInt;
418
419   if (brw->gen >= 7) {
420      ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxAtomicCounters = MAX_ATOMIC_COUNTERS;
421      ctx->Const.Program[MESA_SHADER_VERTEX].MaxAtomicCounters = MAX_ATOMIC_COUNTERS;
422      ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxAtomicCounters = MAX_ATOMIC_COUNTERS;
423      ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxAtomicBuffers = BRW_MAX_ABO;
424      ctx->Const.Program[MESA_SHADER_VERTEX].MaxAtomicBuffers = BRW_MAX_ABO;
425      ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxAtomicBuffers = BRW_MAX_ABO;
426      ctx->Const.MaxCombinedAtomicBuffers = 3 * BRW_MAX_ABO;
427   }
428
429   /* Gen6 converts quads to polygon in beginning of 3D pipeline,
430    * but we're not sure how it's actually done for vertex order,
431    * that affect provoking vertex decision. Always use last vertex
432    * convention for quad primitive which works as expected for now.
433    */
434   if (brw->gen >= 6)
435      ctx->Const.QuadsFollowProvokingVertexConvention = false;
436
437   ctx->Const.NativeIntegers = true;
438   ctx->Const.UniformBooleanTrue = 1;
439
440   /* From the gen4 PRM, volume 4 page 127:
441    *
442    *     "For SURFTYPE_BUFFER non-rendertarget surfaces, this field specifies
443    *      the base address of the first element of the surface, computed in
444    *      software by adding the surface base address to the byte offset of
445    *      the element in the buffer."
446    *
447    * However, unaligned accesses are slower, so enforce buffer alignment.
448    */
449   ctx->Const.UniformBufferOffsetAlignment = 16;
450   ctx->Const.TextureBufferOffsetAlignment = 16;
451
452   if (brw->gen >= 6) {
453      ctx->Const.MaxVarying = 32;
454      ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 128;
455      ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxInputComponents = 64;
456      ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxOutputComponents = 128;
457      ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents = 128;
458   }
459
460   /* We want the GLSL compiler to emit code that uses condition codes */
461   for (int i = 0; i < MESA_SHADER_STAGES; i++) {
462      ctx->ShaderCompilerOptions[i].MaxIfDepth = brw->gen < 6 ? 16 : UINT_MAX;
463      ctx->ShaderCompilerOptions[i].EmitCondCodes = true;
464      ctx->ShaderCompilerOptions[i].EmitNoNoise = true;
465      ctx->ShaderCompilerOptions[i].EmitNoMainReturn = true;
466      ctx->ShaderCompilerOptions[i].EmitNoIndirectInput = true;
467      ctx->ShaderCompilerOptions[i].EmitNoIndirectOutput = true;
468
469      ctx->ShaderCompilerOptions[i].EmitNoIndirectUniform =
470	 (i == MESA_SHADER_FRAGMENT);
471      ctx->ShaderCompilerOptions[i].EmitNoIndirectTemp =
472	 (i == MESA_SHADER_FRAGMENT);
473      ctx->ShaderCompilerOptions[i].LowerClipDistance = true;
474   }
475
476   ctx->ShaderCompilerOptions[MESA_SHADER_VERTEX].OptimizeForAOS = true;
477
478   /* ARB_viewport_array */
479   if (brw->gen >= 7 && ctx->API == API_OPENGL_CORE) {
480      ctx->Const.MaxViewports = GEN7_NUM_VIEWPORTS;
481      ctx->Const.ViewportSubpixelBits = 0;
482
483      /* Cast to float before negating becuase MaxViewportWidth is unsigned.
484       */
485      ctx->Const.ViewportBounds.Min = -(float)ctx->Const.MaxViewportWidth;
486      ctx->Const.ViewportBounds.Max = ctx->Const.MaxViewportWidth;
487   }
488}
489
490/**
491 * Process driconf (drirc) options, setting appropriate context flags.
492 *
493 * intelInitExtensions still pokes at optionCache directly, in order to
494 * avoid advertising various extensions.  No flags are set, so it makes
495 * sense to continue doing that there.
496 */
497static void
498brw_process_driconf_options(struct brw_context *brw)
499{
500   struct gl_context *ctx = &brw->ctx;
501
502   driOptionCache *options = &brw->optionCache;
503   driParseConfigFiles(options, &brw->intelScreen->optionCache,
504                       brw->driContext->driScreenPriv->myNum, "i965");
505
506   int bo_reuse_mode = driQueryOptioni(options, "bo_reuse");
507   switch (bo_reuse_mode) {
508   case DRI_CONF_BO_REUSE_DISABLED:
509      break;
510   case DRI_CONF_BO_REUSE_ALL:
511      intel_bufmgr_gem_enable_reuse(brw->bufmgr);
512      break;
513   }
514
515   if (!driQueryOptionb(options, "hiz")) {
516       brw->has_hiz = false;
517       /* On gen6, you can only do separate stencil with HIZ. */
518       if (brw->gen == 6)
519          brw->has_separate_stencil = false;
520   }
521
522   if (driQueryOptionb(options, "always_flush_batch")) {
523      fprintf(stderr, "flushing batchbuffer before/after each draw call\n");
524      brw->always_flush_batch = true;
525   }
526
527   if (driQueryOptionb(options, "always_flush_cache")) {
528      fprintf(stderr, "flushing GPU caches before/after each draw call\n");
529      brw->always_flush_cache = true;
530   }
531
532   if (driQueryOptionb(options, "disable_throttling")) {
533      fprintf(stderr, "disabling flush throttling\n");
534      brw->disable_throttling = true;
535   }
536
537   brw->disable_derivative_optimization =
538      driQueryOptionb(&brw->optionCache, "disable_derivative_optimization");
539
540   brw->precompile = driQueryOptionb(&brw->optionCache, "shader_precompile");
541
542   ctx->Const.ForceGLSLExtensionsWarn =
543      driQueryOptionb(options, "force_glsl_extensions_warn");
544
545   ctx->Const.DisableGLSLLineContinuations =
546      driQueryOptionb(options, "disable_glsl_line_continuations");
547}
548
549GLboolean
550brwCreateContext(gl_api api,
551	         const struct gl_config *mesaVis,
552		 __DRIcontext *driContextPriv,
553                 unsigned major_version,
554                 unsigned minor_version,
555                 uint32_t flags,
556                 bool notify_reset,
557                 unsigned *dri_ctx_error,
558	         void *sharedContextPrivate)
559{
560   __DRIscreen *sPriv = driContextPriv->driScreenPriv;
561   struct gl_context *shareCtx = (struct gl_context *) sharedContextPrivate;
562   struct intel_screen *screen = sPriv->driverPrivate;
563   const struct brw_device_info *devinfo = screen->devinfo;
564   struct dd_function_table functions;
565   struct gl_config visual;
566
567   /* Only allow the __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS flag if the kernel
568    * provides us with context reset notifications.
569    */
570   uint32_t allowed_flags = __DRI_CTX_FLAG_DEBUG
571      | __DRI_CTX_FLAG_FORWARD_COMPATIBLE;
572
573   if (screen->has_context_reset_notification)
574      allowed_flags |= __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS;
575
576   if (flags & ~allowed_flags) {
577      *dri_ctx_error = __DRI_CTX_ERROR_UNKNOWN_FLAG;
578      return false;
579   }
580
581   struct brw_context *brw = rzalloc(NULL, struct brw_context);
582   if (!brw) {
583      printf("%s: failed to alloc context\n", __FUNCTION__);
584      *dri_ctx_error = __DRI_CTX_ERROR_NO_MEMORY;
585      return false;
586   }
587
588   driContextPriv->driverPrivate = brw;
589   brw->driContext = driContextPriv;
590   brw->intelScreen = screen;
591   brw->bufmgr = screen->bufmgr;
592
593   brw->gen = devinfo->gen;
594   brw->gt = devinfo->gt;
595   brw->is_g4x = devinfo->is_g4x;
596   brw->is_baytrail = devinfo->is_baytrail;
597   brw->is_haswell = devinfo->is_haswell;
598   brw->has_llc = devinfo->has_llc;
599   brw->has_hiz = devinfo->has_hiz_and_separate_stencil && brw->gen < 8;
600   brw->has_separate_stencil = devinfo->has_hiz_and_separate_stencil;
601   brw->has_pln = devinfo->has_pln;
602   brw->has_compr4 = devinfo->has_compr4;
603   brw->has_surface_tile_offset = devinfo->has_surface_tile_offset;
604   brw->has_negative_rhw_bug = devinfo->has_negative_rhw_bug;
605   brw->needs_unlit_centroid_workaround =
606      devinfo->needs_unlit_centroid_workaround;
607
608   brw->must_use_separate_stencil = screen->hw_must_use_separate_stencil;
609   brw->has_swizzling = screen->hw_has_swizzling;
610
611   if (brw->gen >= 7) {
612      gen7_init_vtable_surface_functions(brw);
613      gen7_init_vtable_sampler_functions(brw);
614      brw->vtbl.emit_depth_stencil_hiz = gen7_emit_depth_stencil_hiz;
615   } else {
616      gen4_init_vtable_surface_functions(brw);
617      gen4_init_vtable_sampler_functions(brw);
618      brw->vtbl.emit_depth_stencil_hiz = brw_emit_depth_stencil_hiz;
619   }
620
621   brw_init_driver_functions(brw, &functions);
622
623   if (notify_reset)
624      functions.GetGraphicsResetStatus = brw_get_graphics_reset_status;
625
626   struct gl_context *ctx = &brw->ctx;
627
628   if (mesaVis == NULL) {
629      memset(&visual, 0, sizeof visual);
630      mesaVis = &visual;
631   }
632
633   if (!_mesa_initialize_context(ctx, api, mesaVis, shareCtx, &functions)) {
634      *dri_ctx_error = __DRI_CTX_ERROR_NO_MEMORY;
635      printf("%s: failed to init mesa context\n", __FUNCTION__);
636      intelDestroyContext(driContextPriv);
637      return false;
638   }
639
640   driContextSetFlags(ctx, flags);
641
642   /* Initialize the software rasterizer and helper modules.
643    *
644    * As of GL 3.1 core, the gen4+ driver doesn't need the swrast context for
645    * software fallbacks (which we have to support on legacy GL to do weird
646    * glDrawPixels(), glBitmap(), and other functions).
647    */
648   if (api != API_OPENGL_CORE && api != API_OPENGLES2) {
649      _swrast_CreateContext(ctx);
650   }
651
652   _vbo_CreateContext(ctx);
653   if (ctx->swrast_context) {
654      _tnl_CreateContext(ctx);
655      TNL_CONTEXT(ctx)->Driver.RunPipeline = _tnl_run_pipeline;
656      _swsetup_CreateContext(ctx);
657
658      /* Configure swrast to match hardware characteristics: */
659      _swrast_allow_pixel_fog(ctx, false);
660      _swrast_allow_vertex_fog(ctx, true);
661   }
662
663   _mesa_meta_init(ctx);
664
665   brw_process_driconf_options(brw);
666   brw_process_intel_debug_variable(brw);
667   brw_initialize_context_constants(brw);
668
669   ctx->Const.ResetStrategy = notify_reset
670      ? GL_LOSE_CONTEXT_ON_RESET_ARB : GL_NO_RESET_NOTIFICATION_ARB;
671
672   /* Reinitialize the context point state.  It depends on ctx->Const values. */
673   _mesa_init_point(ctx);
674
675   intel_batchbuffer_init(brw);
676
677   brw_init_state(brw);
678
679   intelInitExtensions(ctx);
680
681   intel_fbo_init(brw);
682
683   if (brw->gen >= 6) {
684      /* Create a new hardware context.  Using a hardware context means that
685       * our GPU state will be saved/restored on context switch, allowing us
686       * to assume that the GPU is in the same state we left it in.
687       *
688       * This is required for transform feedback buffer offsets, query objects,
689       * and also allows us to reduce how much state we have to emit.
690       */
691      brw->hw_ctx = drm_intel_gem_context_create(brw->bufmgr);
692
693      if (!brw->hw_ctx) {
694         fprintf(stderr, "Gen6+ requires Kernel 3.6 or later.\n");
695         intelDestroyContext(driContextPriv);
696         return false;
697      }
698   }
699
700   brw_init_surface_formats(brw);
701
702   if (brw->is_g4x || brw->gen >= 5) {
703      brw->CMD_VF_STATISTICS = GM45_3DSTATE_VF_STATISTICS;
704      brw->CMD_PIPELINE_SELECT = CMD_PIPELINE_SELECT_GM45;
705  } else {
706      brw->CMD_VF_STATISTICS = GEN4_3DSTATE_VF_STATISTICS;
707      brw->CMD_PIPELINE_SELECT = CMD_PIPELINE_SELECT_965;
708   }
709
710   brw->max_vs_threads = devinfo->max_vs_threads;
711   brw->max_gs_threads = devinfo->max_gs_threads;
712   brw->max_wm_threads = devinfo->max_wm_threads;
713   brw->urb.size = devinfo->urb.size;
714   brw->urb.min_vs_entries = devinfo->urb.min_vs_entries;
715   brw->urb.max_vs_entries = devinfo->urb.max_vs_entries;
716   brw->urb.max_gs_entries = devinfo->urb.max_gs_entries;
717
718   /* Estimate the size of the mappable aperture into the GTT.  There's an
719    * ioctl to get the whole GTT size, but not one to get the mappable subset.
720    * It turns out it's basically always 256MB, though some ancient hardware
721    * was smaller.
722    */
723   uint32_t gtt_size = 256 * 1024 * 1024;
724
725   /* We don't want to map two objects such that a memcpy between them would
726    * just fault one mapping in and then the other over and over forever.  So
727    * we would need to divide the GTT size by 2.  Additionally, some GTT is
728    * taken up by things like the framebuffer and the ringbuffer and such, so
729    * be more conservative.
730    */
731   brw->max_gtt_map_object_size = gtt_size / 4;
732
733   if (brw->gen == 6)
734      brw->urb.gen6_gs_previously_active = false;
735
736   brw->prim_restart.in_progress = false;
737   brw->prim_restart.enable_cut_index = false;
738   brw->gs.enabled = false;
739
740   if (brw->gen < 6) {
741      brw->curbe.last_buf = calloc(1, 4096);
742      brw->curbe.next_buf = calloc(1, 4096);
743   }
744
745   ctx->VertexProgram._MaintainTnlProgram = true;
746   ctx->FragmentProgram._MaintainTexEnvProgram = true;
747
748   brw_draw_init( brw );
749
750   if ((flags & __DRI_CTX_FLAG_DEBUG) != 0) {
751      /* Turn on some extra GL_ARB_debug_output generation. */
752      brw->perf_debug = true;
753   }
754
755   if ((flags & __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS) != 0)
756      ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_ROBUST_ACCESS_BIT_ARB;
757
758   brw_fs_alloc_reg_sets(brw);
759   brw_vec4_alloc_reg_set(brw);
760
761   if (INTEL_DEBUG & DEBUG_SHADER_TIME)
762      brw_init_shader_time(brw);
763
764   _mesa_compute_version(ctx);
765
766   _mesa_initialize_dispatch_tables(ctx);
767   _mesa_initialize_vbo_vtxfmt(ctx);
768
769   if (ctx->Extensions.AMD_performance_monitor) {
770      brw_init_performance_monitors(brw);
771   }
772
773   return true;
774}
775
776void
777intelDestroyContext(__DRIcontext * driContextPriv)
778{
779   struct brw_context *brw =
780      (struct brw_context *) driContextPriv->driverPrivate;
781   struct gl_context *ctx = &brw->ctx;
782
783   assert(brw); /* should never be null */
784   if (!brw)
785      return;
786
787   /* Dump a final BMP in case the application doesn't call SwapBuffers */
788   if (INTEL_DEBUG & DEBUG_AUB) {
789      intel_batchbuffer_flush(brw);
790      aub_dump_bmp(&brw->ctx);
791   }
792
793   _mesa_meta_free(&brw->ctx);
794
795   if (INTEL_DEBUG & DEBUG_SHADER_TIME) {
796      /* Force a report. */
797      brw->shader_time.report_time = 0;
798
799      brw_collect_and_report_shader_time(brw);
800      brw_destroy_shader_time(brw);
801   }
802
803   brw_destroy_state(brw);
804   brw_draw_destroy(brw);
805
806   drm_intel_bo_unreference(brw->curbe.curbe_bo);
807   drm_intel_bo_unreference(brw->vs.base.const_bo);
808   drm_intel_bo_unreference(brw->wm.base.const_bo);
809
810   free(brw->curbe.last_buf);
811   free(brw->curbe.next_buf);
812
813   drm_intel_gem_context_destroy(brw->hw_ctx);
814
815   if (ctx->swrast_context) {
816      _swsetup_DestroyContext(&brw->ctx);
817      _tnl_DestroyContext(&brw->ctx);
818   }
819   _vbo_DestroyContext(&brw->ctx);
820
821   if (ctx->swrast_context)
822      _swrast_DestroyContext(&brw->ctx);
823
824   intel_batchbuffer_free(brw);
825
826   drm_intel_bo_unreference(brw->first_post_swapbuffers_batch);
827   brw->first_post_swapbuffers_batch = NULL;
828
829   driDestroyOptionCache(&brw->optionCache);
830
831   /* free the Mesa context */
832   _mesa_free_context_data(&brw->ctx);
833
834   ralloc_free(brw);
835   driContextPriv->driverPrivate = NULL;
836}
837
838GLboolean
839intelUnbindContext(__DRIcontext * driContextPriv)
840{
841   /* Unset current context and dispath table */
842   _mesa_make_current(NULL, NULL, NULL);
843
844   return true;
845}
846
847/**
848 * Fixes up the context for GLES23 with our default-to-sRGB-capable behavior
849 * on window system framebuffers.
850 *
851 * Desktop GL is fairly reasonable in its handling of sRGB: You can ask if
852 * your renderbuffer can do sRGB encode, and you can flip a switch that does
853 * sRGB encode if the renderbuffer can handle it.  You can ask specifically
854 * for a visual where you're guaranteed to be capable, but it turns out that
855 * everyone just makes all their ARGB8888 visuals capable and doesn't offer
856 * incapable ones, becuase there's no difference between the two in resources
857 * used.  Applications thus get built that accidentally rely on the default
858 * visual choice being sRGB, so we make ours sRGB capable.  Everything sounds
859 * great...
860 *
861 * But for GLES2/3, they decided that it was silly to not turn on sRGB encode
862 * for sRGB renderbuffers you made with the GL_EXT_texture_sRGB equivalent.
863 * So they removed the enable knob and made it "if the renderbuffer is sRGB
864 * capable, do sRGB encode".  Then, for your window system renderbuffers, you
865 * can ask for sRGB visuals and get sRGB encode, or not ask for sRGB visuals
866 * and get no sRGB encode (assuming that both kinds of visual are available).
867 * Thus our choice to support sRGB by default on our visuals for desktop would
868 * result in broken rendering of GLES apps that aren't expecting sRGB encode.
869 *
870 * Unfortunately, renderbuffer setup happens before a context is created.  So
871 * in intel_screen.c we always set up sRGB, and here, if you're a GLES2/3
872 * context (without an sRGB visual, though we don't have sRGB visuals exposed
873 * yet), we go turn that back off before anyone finds out.
874 */
875static void
876intel_gles3_srgb_workaround(struct brw_context *brw,
877                            struct gl_framebuffer *fb)
878{
879   struct gl_context *ctx = &brw->ctx;
880
881   if (_mesa_is_desktop_gl(ctx) || !fb->Visual.sRGBCapable)
882      return;
883
884   /* Some day when we support the sRGB capable bit on visuals available for
885    * GLES, we'll need to respect that and not disable things here.
886    */
887   fb->Visual.sRGBCapable = false;
888   for (int i = 0; i < BUFFER_COUNT; i++) {
889      if (fb->Attachment[i].Renderbuffer &&
890          fb->Attachment[i].Renderbuffer->Format == MESA_FORMAT_SARGB8) {
891         fb->Attachment[i].Renderbuffer->Format = MESA_FORMAT_ARGB8888;
892      }
893   }
894}
895
896GLboolean
897intelMakeCurrent(__DRIcontext * driContextPriv,
898                 __DRIdrawable * driDrawPriv,
899                 __DRIdrawable * driReadPriv)
900{
901   struct brw_context *brw;
902   GET_CURRENT_CONTEXT(curCtx);
903
904   if (driContextPriv)
905      brw = (struct brw_context *) driContextPriv->driverPrivate;
906   else
907      brw = NULL;
908
909   /* According to the glXMakeCurrent() man page: "Pending commands to
910    * the previous context, if any, are flushed before it is released."
911    * But only flush if we're actually changing contexts.
912    */
913   if (brw_context(curCtx) && brw_context(curCtx) != brw) {
914      _mesa_flush(curCtx);
915   }
916
917   if (driContextPriv) {
918      struct gl_context *ctx = &brw->ctx;
919      struct gl_framebuffer *fb, *readFb;
920      struct intel_renderbuffer *rb = NULL;
921
922      if (driDrawPriv == NULL && driReadPriv == NULL) {
923         fb = _mesa_get_incomplete_framebuffer();
924         readFb = _mesa_get_incomplete_framebuffer();
925      } else {
926         fb = driDrawPriv->driverPrivate;
927         readFb = driReadPriv->driverPrivate;
928         rb = intel_get_renderbuffer(fb, BUFFER_BACK_LEFT);
929         driContextPriv->dri2.draw_stamp = driDrawPriv->dri2.stamp - 1;
930         driContextPriv->dri2.read_stamp = driReadPriv->dri2.stamp - 1;
931      }
932
933      /* The sRGB workaround changes the renderbuffer's format. We must change
934       * the format before the renderbuffer's miptree get's allocated, otherwise
935       * the formats of the renderbuffer and its miptree will differ.
936       */
937      intel_gles3_srgb_workaround(brw, fb);
938      intel_gles3_srgb_workaround(brw, readFb);
939
940      if (rb && !rb->mt) {
941         /* If we don't have buffers for the drawable yet, force a call to
942          * getbuffers here so we can have a default drawable size. */
943         intel_prepare_render(brw);
944      }
945
946      _mesa_make_current(ctx, fb, readFb);
947   } else {
948      _mesa_make_current(NULL, NULL, NULL);
949   }
950
951   return true;
952}
953
954void
955intel_resolve_for_dri2_flush(struct brw_context *brw,
956                             __DRIdrawable *drawable)
957{
958   if (brw->gen < 6) {
959      /* MSAA and fast color clear are not supported, so don't waste time
960       * checking whether a resolve is needed.
961       */
962      return;
963   }
964
965   struct gl_framebuffer *fb = drawable->driverPrivate;
966   struct intel_renderbuffer *rb;
967
968   /* Usually, only the back buffer will need to be downsampled. However,
969    * the front buffer will also need it if the user has rendered into it.
970    */
971   static const gl_buffer_index buffers[2] = {
972         BUFFER_BACK_LEFT,
973         BUFFER_FRONT_LEFT,
974   };
975
976   for (int i = 0; i < 2; ++i) {
977      rb = intel_get_renderbuffer(fb, buffers[i]);
978      if (rb == NULL || rb->mt == NULL)
979         continue;
980      if (rb->mt->num_samples <= 1)
981         intel_miptree_resolve_color(brw, rb->mt);
982      else
983         intel_miptree_downsample(brw, rb->mt);
984   }
985}
986
987static unsigned
988intel_bits_per_pixel(const struct intel_renderbuffer *rb)
989{
990   return _mesa_get_format_bytes(intel_rb_format(rb)) * 8;
991}
992
993static void
994intel_query_dri2_buffers(struct brw_context *brw,
995                         __DRIdrawable *drawable,
996                         __DRIbuffer **buffers,
997                         int *count);
998
999static void
1000intel_process_dri2_buffer(struct brw_context *brw,
1001                          __DRIdrawable *drawable,
1002                          __DRIbuffer *buffer,
1003                          struct intel_renderbuffer *rb,
1004                          const char *buffer_name);
1005
1006static void
1007intel_update_image_buffers(struct brw_context *brw, __DRIdrawable *drawable);
1008
1009static void
1010intel_update_dri2_buffers(struct brw_context *brw, __DRIdrawable *drawable)
1011{
1012   struct gl_framebuffer *fb = drawable->driverPrivate;
1013   struct intel_renderbuffer *rb;
1014   __DRIbuffer *buffers = NULL;
1015   int i, count;
1016   const char *region_name;
1017
1018   /* Set this up front, so that in case our buffers get invalidated
1019    * while we're getting new buffers, we don't clobber the stamp and
1020    * thus ignore the invalidate. */
1021   drawable->lastStamp = drawable->dri2.stamp;
1022
1023   if (unlikely(INTEL_DEBUG & DEBUG_DRI))
1024      fprintf(stderr, "enter %s, drawable %p\n", __func__, drawable);
1025
1026   intel_query_dri2_buffers(brw, drawable, &buffers, &count);
1027
1028   if (buffers == NULL)
1029      return;
1030
1031   for (i = 0; i < count; i++) {
1032       switch (buffers[i].attachment) {
1033       case __DRI_BUFFER_FRONT_LEFT:
1034           rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
1035           region_name = "dri2 front buffer";
1036           break;
1037
1038       case __DRI_BUFFER_FAKE_FRONT_LEFT:
1039           rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
1040           region_name = "dri2 fake front buffer";
1041           break;
1042
1043       case __DRI_BUFFER_BACK_LEFT:
1044           rb = intel_get_renderbuffer(fb, BUFFER_BACK_LEFT);
1045           region_name = "dri2 back buffer";
1046           break;
1047
1048       case __DRI_BUFFER_DEPTH:
1049       case __DRI_BUFFER_HIZ:
1050       case __DRI_BUFFER_DEPTH_STENCIL:
1051       case __DRI_BUFFER_STENCIL:
1052       case __DRI_BUFFER_ACCUM:
1053       default:
1054           fprintf(stderr,
1055                   "unhandled buffer attach event, attachment type %d\n",
1056                   buffers[i].attachment);
1057           return;
1058       }
1059
1060       intel_process_dri2_buffer(brw, drawable, &buffers[i], rb, region_name);
1061   }
1062
1063}
1064
1065void
1066intel_update_renderbuffers(__DRIcontext *context, __DRIdrawable *drawable)
1067{
1068   struct brw_context *brw = context->driverPrivate;
1069   __DRIscreen *screen = brw->intelScreen->driScrnPriv;
1070
1071   /* Set this up front, so that in case our buffers get invalidated
1072    * while we're getting new buffers, we don't clobber the stamp and
1073    * thus ignore the invalidate. */
1074   drawable->lastStamp = drawable->dri2.stamp;
1075
1076   if (unlikely(INTEL_DEBUG & DEBUG_DRI))
1077      fprintf(stderr, "enter %s, drawable %p\n", __func__, drawable);
1078
1079   if (screen->image.loader)
1080      intel_update_image_buffers(brw, drawable);
1081   else
1082      intel_update_dri2_buffers(brw, drawable);
1083
1084   driUpdateFramebufferSize(&brw->ctx, drawable);
1085}
1086
1087/**
1088 * intel_prepare_render should be called anywhere that curent read/drawbuffer
1089 * state is required.
1090 */
1091void
1092intel_prepare_render(struct brw_context *brw)
1093{
1094   __DRIcontext *driContext = brw->driContext;
1095   __DRIdrawable *drawable;
1096
1097   drawable = driContext->driDrawablePriv;
1098   if (drawable && drawable->dri2.stamp != driContext->dri2.draw_stamp) {
1099      if (drawable->lastStamp != drawable->dri2.stamp)
1100         intel_update_renderbuffers(driContext, drawable);
1101      driContext->dri2.draw_stamp = drawable->dri2.stamp;
1102   }
1103
1104   drawable = driContext->driReadablePriv;
1105   if (drawable && drawable->dri2.stamp != driContext->dri2.read_stamp) {
1106      if (drawable->lastStamp != drawable->dri2.stamp)
1107         intel_update_renderbuffers(driContext, drawable);
1108      driContext->dri2.read_stamp = drawable->dri2.stamp;
1109   }
1110
1111   /* If we're currently rendering to the front buffer, the rendering
1112    * that will happen next will probably dirty the front buffer.  So
1113    * mark it as dirty here.
1114    */
1115   if (brw->is_front_buffer_rendering)
1116      brw->front_buffer_dirty = true;
1117
1118   /* Wait for the swapbuffers before the one we just emitted, so we
1119    * don't get too many swaps outstanding for apps that are GPU-heavy
1120    * but not CPU-heavy.
1121    *
1122    * We're using intelDRI2Flush (called from the loader before
1123    * swapbuffer) and glFlush (for front buffer rendering) as the
1124    * indicator that a frame is done and then throttle when we get
1125    * here as we prepare to render the next frame.  At this point for
1126    * round trips for swap/copy and getting new buffers are done and
1127    * we'll spend less time waiting on the GPU.
1128    *
1129    * Unfortunately, we don't have a handle to the batch containing
1130    * the swap, and getting our hands on that doesn't seem worth it,
1131    * so we just us the first batch we emitted after the last swap.
1132    */
1133   if (brw->need_throttle && brw->first_post_swapbuffers_batch) {
1134      if (!brw->disable_throttling)
1135         drm_intel_bo_wait_rendering(brw->first_post_swapbuffers_batch);
1136      drm_intel_bo_unreference(brw->first_post_swapbuffers_batch);
1137      brw->first_post_swapbuffers_batch = NULL;
1138      brw->need_throttle = false;
1139   }
1140}
1141
1142/**
1143 * \brief Query DRI2 to obtain a DRIdrawable's buffers.
1144 *
1145 * To determine which DRI buffers to request, examine the renderbuffers
1146 * attached to the drawable's framebuffer. Then request the buffers with
1147 * DRI2GetBuffers() or DRI2GetBuffersWithFormat().
1148 *
1149 * This is called from intel_update_renderbuffers().
1150 *
1151 * \param drawable      Drawable whose buffers are queried.
1152 * \param buffers       [out] List of buffers returned by DRI2 query.
1153 * \param buffer_count  [out] Number of buffers returned.
1154 *
1155 * \see intel_update_renderbuffers()
1156 * \see DRI2GetBuffers()
1157 * \see DRI2GetBuffersWithFormat()
1158 */
1159static void
1160intel_query_dri2_buffers(struct brw_context *brw,
1161                         __DRIdrawable *drawable,
1162                         __DRIbuffer **buffers,
1163                         int *buffer_count)
1164{
1165   __DRIscreen *screen = brw->intelScreen->driScrnPriv;
1166   struct gl_framebuffer *fb = drawable->driverPrivate;
1167   int i = 0;
1168   unsigned attachments[8];
1169
1170   struct intel_renderbuffer *front_rb;
1171   struct intel_renderbuffer *back_rb;
1172
1173   front_rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
1174   back_rb = intel_get_renderbuffer(fb, BUFFER_BACK_LEFT);
1175
1176   memset(attachments, 0, sizeof(attachments));
1177   if ((brw->is_front_buffer_rendering ||
1178        brw->is_front_buffer_reading ||
1179        !back_rb) && front_rb) {
1180      /* If a fake front buffer is in use, then querying for
1181       * __DRI_BUFFER_FRONT_LEFT will cause the server to copy the image from
1182       * the real front buffer to the fake front buffer.  So before doing the
1183       * query, we need to make sure all the pending drawing has landed in the
1184       * real front buffer.
1185       */
1186      intel_batchbuffer_flush(brw);
1187      intel_flush_front(&brw->ctx);
1188
1189      attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
1190      attachments[i++] = intel_bits_per_pixel(front_rb);
1191   } else if (front_rb && brw->front_buffer_dirty) {
1192      /* We have pending front buffer rendering, but we aren't querying for a
1193       * front buffer.  If the front buffer we have is a fake front buffer,
1194       * the X server is going to throw it away when it processes the query.
1195       * So before doing the query, make sure all the pending drawing has
1196       * landed in the real front buffer.
1197       */
1198      intel_batchbuffer_flush(brw);
1199      intel_flush_front(&brw->ctx);
1200   }
1201
1202   if (back_rb) {
1203      attachments[i++] = __DRI_BUFFER_BACK_LEFT;
1204      attachments[i++] = intel_bits_per_pixel(back_rb);
1205   }
1206
1207   assert(i <= ARRAY_SIZE(attachments));
1208
1209   *buffers = screen->dri2.loader->getBuffersWithFormat(drawable,
1210                                                        &drawable->w,
1211                                                        &drawable->h,
1212                                                        attachments, i / 2,
1213                                                        buffer_count,
1214                                                        drawable->loaderPrivate);
1215}
1216
1217/**
1218 * \brief Assign a DRI buffer's DRM region to a renderbuffer.
1219 *
1220 * This is called from intel_update_renderbuffers().
1221 *
1222 * \par Note:
1223 *    DRI buffers whose attachment point is DRI2BufferStencil or
1224 *    DRI2BufferDepthStencil are handled as special cases.
1225 *
1226 * \param buffer_name is a human readable name, such as "dri2 front buffer",
1227 *        that is passed to intel_region_alloc_for_handle().
1228 *
1229 * \see intel_update_renderbuffers()
1230 * \see intel_region_alloc_for_handle()
1231 */
1232static void
1233intel_process_dri2_buffer(struct brw_context *brw,
1234                          __DRIdrawable *drawable,
1235                          __DRIbuffer *buffer,
1236                          struct intel_renderbuffer *rb,
1237                          const char *buffer_name)
1238{
1239   struct intel_region *region = NULL;
1240
1241   if (!rb)
1242      return;
1243
1244   unsigned num_samples = rb->Base.Base.NumSamples;
1245
1246   /* We try to avoid closing and reopening the same BO name, because the first
1247    * use of a mapping of the buffer involves a bunch of page faulting which is
1248    * moderately expensive.
1249    */
1250   if (num_samples == 0) {
1251       if (rb->mt &&
1252           rb->mt->region &&
1253           rb->mt->region->name == buffer->name)
1254          return;
1255   } else {
1256       if (rb->mt &&
1257           rb->mt->singlesample_mt &&
1258           rb->mt->singlesample_mt->region &&
1259           rb->mt->singlesample_mt->region->name == buffer->name)
1260          return;
1261   }
1262
1263   if (unlikely(INTEL_DEBUG & DEBUG_DRI)) {
1264      fprintf(stderr,
1265              "attaching buffer %d, at %d, cpp %d, pitch %d\n",
1266              buffer->name, buffer->attachment,
1267              buffer->cpp, buffer->pitch);
1268   }
1269
1270   intel_miptree_release(&rb->mt);
1271   region = intel_region_alloc_for_handle(brw->intelScreen,
1272                                          buffer->cpp,
1273                                          drawable->w,
1274                                          drawable->h,
1275                                          buffer->pitch,
1276                                          buffer->name,
1277                                          buffer_name);
1278   if (!region)
1279      return;
1280
1281   rb->mt = intel_miptree_create_for_dri2_buffer(brw,
1282                                                 buffer->attachment,
1283                                                 intel_rb_format(rb),
1284                                                 num_samples,
1285                                                 region);
1286   intel_region_release(&region);
1287}
1288
1289/**
1290 * \brief Query DRI image loader to obtain a DRIdrawable's buffers.
1291 *
1292 * To determine which DRI buffers to request, examine the renderbuffers
1293 * attached to the drawable's framebuffer. Then request the buffers from
1294 * the image loader
1295 *
1296 * This is called from intel_update_renderbuffers().
1297 *
1298 * \param drawable      Drawable whose buffers are queried.
1299 * \param buffers       [out] List of buffers returned by DRI2 query.
1300 * \param buffer_count  [out] Number of buffers returned.
1301 *
1302 * \see intel_update_renderbuffers()
1303 */
1304
1305static void
1306intel_update_image_buffer(struct brw_context *intel,
1307                          __DRIdrawable *drawable,
1308                          struct intel_renderbuffer *rb,
1309                          __DRIimage *buffer,
1310                          enum __DRIimageBufferMask buffer_type)
1311{
1312   struct intel_region *region = buffer->region;
1313
1314   if (!rb || !region)
1315      return;
1316
1317   unsigned num_samples = rb->Base.Base.NumSamples;
1318
1319   /* Check and see if we're already bound to the right
1320    * buffer object
1321    */
1322   if (num_samples == 0) {
1323       if (rb->mt &&
1324           rb->mt->region &&
1325           rb->mt->region->bo == region->bo)
1326          return;
1327   } else {
1328       if (rb->mt &&
1329           rb->mt->singlesample_mt &&
1330           rb->mt->singlesample_mt->region &&
1331           rb->mt->singlesample_mt->region->bo == region->bo)
1332          return;
1333   }
1334
1335   intel_miptree_release(&rb->mt);
1336   rb->mt = intel_miptree_create_for_image_buffer(intel,
1337                                                  buffer_type,
1338                                                  intel_rb_format(rb),
1339                                                  num_samples,
1340                                                  region);
1341}
1342
1343static void
1344intel_update_image_buffers(struct brw_context *brw, __DRIdrawable *drawable)
1345{
1346   struct gl_framebuffer *fb = drawable->driverPrivate;
1347   __DRIscreen *screen = brw->intelScreen->driScrnPriv;
1348   struct intel_renderbuffer *front_rb;
1349   struct intel_renderbuffer *back_rb;
1350   struct __DRIimageList images;
1351   unsigned int format;
1352   uint32_t buffer_mask = 0;
1353
1354   front_rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
1355   back_rb = intel_get_renderbuffer(fb, BUFFER_BACK_LEFT);
1356
1357   if (back_rb)
1358      format = intel_rb_format(back_rb);
1359   else if (front_rb)
1360      format = intel_rb_format(front_rb);
1361   else
1362      return;
1363
1364   if ((brw->is_front_buffer_rendering || brw->is_front_buffer_reading || !back_rb) && front_rb)
1365      buffer_mask |= __DRI_IMAGE_BUFFER_FRONT;
1366
1367   if (back_rb)
1368      buffer_mask |= __DRI_IMAGE_BUFFER_BACK;
1369
1370   (*screen->image.loader->getBuffers) (drawable,
1371                                        driGLFormatToImageFormat(format),
1372                                        &drawable->dri2.stamp,
1373                                        drawable->loaderPrivate,
1374                                        buffer_mask,
1375                                        &images);
1376
1377   if (images.image_mask & __DRI_IMAGE_BUFFER_FRONT) {
1378      drawable->w = images.front->width;
1379      drawable->h = images.front->height;
1380      intel_update_image_buffer(brw,
1381                                drawable,
1382                                front_rb,
1383                                images.front,
1384                                __DRI_IMAGE_BUFFER_FRONT);
1385   }
1386   if (images.image_mask & __DRI_IMAGE_BUFFER_BACK) {
1387      drawable->w = images.back->width;
1388      drawable->h = images.back->height;
1389      intel_update_image_buffer(brw,
1390                                drawable,
1391                                back_rb,
1392                                images.back,
1393                                __DRI_IMAGE_BUFFER_BACK);
1394   }
1395}
1396