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