i965: Add some informative debug when the X Server botches DRI2 GetBuffers.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_context.c
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
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 const char *const brw_vendor_string = "Intel Open Source Technology Center";
98
99 const char *
100 brw_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
118 static const GLubyte *
119 intelGetString(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
136 static void
137 intel_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
148 static void
149 intelInvalidateState(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
162 static void
163 intel_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
194 static void
195 intel_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
205 void
206 intelFinish(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
216 static void
217 brw_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
275 static void
276 brw_initialize_context_constants(struct brw_context *brw)
277 {
278 struct gl_context *ctx = &brw->ctx;
279
280 unsigned max_samplers =
281 brw->gen >= 8 || brw->is_haswell ? BRW_MAX_TEX_UNIT : 16;
282
283 ctx->Const.QueryCounterBits.Timestamp = 36;
284
285 ctx->Const.StripTextureBorder = true;
286
287 ctx->Const.MaxDualSourceDrawBuffers = 1;
288 ctx->Const.MaxDrawBuffers = BRW_MAX_DRAW_BUFFERS;
289 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = max_samplers;
290 ctx->Const.MaxTextureCoordUnits = 8; /* Mesa limit */
291 ctx->Const.MaxTextureUnits =
292 MIN2(ctx->Const.MaxTextureCoordUnits,
293 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits);
294 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = max_samplers;
295 if (brw->gen >= 7)
296 ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxTextureImageUnits = max_samplers;
297 else
298 ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxTextureImageUnits = 0;
299 if (getenv("INTEL_COMPUTE_SHADER")) {
300 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxTextureImageUnits = BRW_MAX_TEX_UNIT;
301 ctx->Const.MaxUniformBufferBindings += 12;
302 } else {
303 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxTextureImageUnits = 0;
304 }
305 ctx->Const.MaxCombinedTextureImageUnits =
306 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits +
307 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits +
308 ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxTextureImageUnits +
309 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxTextureImageUnits;
310
311 ctx->Const.MaxTextureLevels = 14; /* 8192 */
312 if (ctx->Const.MaxTextureLevels > MAX_TEXTURE_LEVELS)
313 ctx->Const.MaxTextureLevels = MAX_TEXTURE_LEVELS;
314 ctx->Const.Max3DTextureLevels = 9;
315 ctx->Const.MaxCubeTextureLevels = 12;
316
317 if (brw->gen >= 7)
318 ctx->Const.MaxArrayTextureLayers = 2048;
319 else
320 ctx->Const.MaxArrayTextureLayers = 512;
321
322 ctx->Const.MaxTextureRectSize = 1 << 12;
323
324 ctx->Const.MaxTextureMaxAnisotropy = 16.0;
325
326 ctx->Const.MaxRenderbufferSize = 8192;
327
328 /* Hardware only supports a limited number of transform feedback buffers.
329 * So we need to override the Mesa default (which is based only on software
330 * limits).
331 */
332 ctx->Const.MaxTransformFeedbackBuffers = BRW_MAX_SOL_BUFFERS;
333
334 /* On Gen6, in the worst case, we use up one binding table entry per
335 * transform feedback component (see comments above the definition of
336 * BRW_MAX_SOL_BINDINGS, in brw_context.h), so we need to advertise a value
337 * for MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS equal to
338 * BRW_MAX_SOL_BINDINGS.
339 *
340 * In "separate components" mode, we need to divide this value by
341 * BRW_MAX_SOL_BUFFERS, so that the total number of binding table entries
342 * used up by all buffers will not exceed BRW_MAX_SOL_BINDINGS.
343 */
344 ctx->Const.MaxTransformFeedbackInterleavedComponents = BRW_MAX_SOL_BINDINGS;
345 ctx->Const.MaxTransformFeedbackSeparateComponents =
346 BRW_MAX_SOL_BINDINGS / BRW_MAX_SOL_BUFFERS;
347
348 ctx->Const.AlwaysUseGetTransformFeedbackVertexCount = true;
349
350 int max_samples;
351 const int *msaa_modes = intel_supported_msaa_modes(brw->intelScreen);
352 const int clamp_max_samples =
353 driQueryOptioni(&brw->optionCache, "clamp_max_samples");
354
355 if (clamp_max_samples < 0) {
356 max_samples = msaa_modes[0];
357 } else {
358 /* Select the largest supported MSAA mode that does not exceed
359 * clamp_max_samples.
360 */
361 max_samples = 0;
362 for (int i = 0; msaa_modes[i] != 0; ++i) {
363 if (msaa_modes[i] <= clamp_max_samples) {
364 max_samples = msaa_modes[i];
365 break;
366 }
367 }
368 }
369
370 ctx->Const.MaxSamples = max_samples;
371 ctx->Const.MaxColorTextureSamples = max_samples;
372 ctx->Const.MaxDepthTextureSamples = max_samples;
373 ctx->Const.MaxIntegerSamples = max_samples;
374
375 if (brw->gen >= 7)
376 ctx->Const.MaxProgramTextureGatherComponents = 4;
377
378 ctx->Const.MinLineWidth = 1.0;
379 ctx->Const.MinLineWidthAA = 1.0;
380 ctx->Const.MaxLineWidth = 5.0;
381 ctx->Const.MaxLineWidthAA = 5.0;
382 ctx->Const.LineWidthGranularity = 0.5;
383
384 ctx->Const.MinPointSize = 1.0;
385 ctx->Const.MinPointSizeAA = 1.0;
386 ctx->Const.MaxPointSize = 255.0;
387 ctx->Const.MaxPointSizeAA = 255.0;
388 ctx->Const.PointSizeGranularity = 1.0;
389
390 if (brw->gen >= 5 || brw->is_g4x)
391 ctx->Const.MaxClipPlanes = 8;
392
393 ctx->Const.Program[MESA_SHADER_VERTEX].MaxNativeInstructions = 16 * 1024;
394 ctx->Const.Program[MESA_SHADER_VERTEX].MaxAluInstructions = 0;
395 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTexInstructions = 0;
396 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTexIndirections = 0;
397 ctx->Const.Program[MESA_SHADER_VERTEX].MaxNativeAluInstructions = 0;
398 ctx->Const.Program[MESA_SHADER_VERTEX].MaxNativeTexInstructions = 0;
399 ctx->Const.Program[MESA_SHADER_VERTEX].MaxNativeTexIndirections = 0;
400 ctx->Const.Program[MESA_SHADER_VERTEX].MaxNativeAttribs = 16;
401 ctx->Const.Program[MESA_SHADER_VERTEX].MaxNativeTemps = 256;
402 ctx->Const.Program[MESA_SHADER_VERTEX].MaxNativeAddressRegs = 1;
403 ctx->Const.Program[MESA_SHADER_VERTEX].MaxNativeParameters = 1024;
404 ctx->Const.Program[MESA_SHADER_VERTEX].MaxEnvParams =
405 MIN2(ctx->Const.Program[MESA_SHADER_VERTEX].MaxNativeParameters,
406 ctx->Const.Program[MESA_SHADER_VERTEX].MaxEnvParams);
407
408 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxNativeInstructions = 1024;
409 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxNativeAluInstructions = 1024;
410 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxNativeTexInstructions = 1024;
411 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxNativeTexIndirections = 1024;
412 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxNativeAttribs = 12;
413 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxNativeTemps = 256;
414 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxNativeAddressRegs = 0;
415 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxNativeParameters = 1024;
416 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxEnvParams =
417 MIN2(ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxNativeParameters,
418 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxEnvParams);
419
420 /* Fragment shaders use real, 32-bit twos-complement integers for all
421 * integer types.
422 */
423 ctx->Const.Program[MESA_SHADER_FRAGMENT].LowInt.RangeMin = 31;
424 ctx->Const.Program[MESA_SHADER_FRAGMENT].LowInt.RangeMax = 30;
425 ctx->Const.Program[MESA_SHADER_FRAGMENT].LowInt.Precision = 0;
426 ctx->Const.Program[MESA_SHADER_FRAGMENT].HighInt = ctx->Const.Program[MESA_SHADER_FRAGMENT].LowInt;
427 ctx->Const.Program[MESA_SHADER_FRAGMENT].MediumInt = ctx->Const.Program[MESA_SHADER_FRAGMENT].LowInt;
428
429 if (brw->gen >= 7) {
430 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxAtomicCounters = MAX_ATOMIC_COUNTERS;
431 ctx->Const.Program[MESA_SHADER_VERTEX].MaxAtomicCounters = MAX_ATOMIC_COUNTERS;
432 ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxAtomicCounters = MAX_ATOMIC_COUNTERS;
433 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxAtomicCounters = MAX_ATOMIC_COUNTERS;
434 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxAtomicBuffers = BRW_MAX_ABO;
435 ctx->Const.Program[MESA_SHADER_VERTEX].MaxAtomicBuffers = BRW_MAX_ABO;
436 ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxAtomicBuffers = BRW_MAX_ABO;
437 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxAtomicBuffers = BRW_MAX_ABO;
438 ctx->Const.MaxCombinedAtomicBuffers = 3 * BRW_MAX_ABO;
439 }
440
441 /* Gen6 converts quads to polygon in beginning of 3D pipeline,
442 * but we're not sure how it's actually done for vertex order,
443 * that affect provoking vertex decision. Always use last vertex
444 * convention for quad primitive which works as expected for now.
445 */
446 if (brw->gen >= 6)
447 ctx->Const.QuadsFollowProvokingVertexConvention = false;
448
449 ctx->Const.NativeIntegers = true;
450 ctx->Const.UniformBooleanTrue = 1;
451
452 /* From the gen4 PRM, volume 4 page 127:
453 *
454 * "For SURFTYPE_BUFFER non-rendertarget surfaces, this field specifies
455 * the base address of the first element of the surface, computed in
456 * software by adding the surface base address to the byte offset of
457 * the element in the buffer."
458 *
459 * However, unaligned accesses are slower, so enforce buffer alignment.
460 */
461 ctx->Const.UniformBufferOffsetAlignment = 16;
462 ctx->Const.TextureBufferOffsetAlignment = 16;
463
464 if (brw->gen >= 6) {
465 ctx->Const.MaxVarying = 32;
466 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 128;
467 ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxInputComponents = 64;
468 ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxOutputComponents = 128;
469 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents = 128;
470 }
471
472 /* We want the GLSL compiler to emit code that uses condition codes */
473 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
474 ctx->ShaderCompilerOptions[i].MaxIfDepth = brw->gen < 6 ? 16 : UINT_MAX;
475 ctx->ShaderCompilerOptions[i].EmitCondCodes = true;
476 ctx->ShaderCompilerOptions[i].EmitNoNoise = true;
477 ctx->ShaderCompilerOptions[i].EmitNoMainReturn = true;
478 ctx->ShaderCompilerOptions[i].EmitNoIndirectInput = true;
479 ctx->ShaderCompilerOptions[i].EmitNoIndirectOutput = true;
480
481 ctx->ShaderCompilerOptions[i].EmitNoIndirectUniform =
482 (i == MESA_SHADER_FRAGMENT);
483 ctx->ShaderCompilerOptions[i].EmitNoIndirectTemp =
484 (i == MESA_SHADER_FRAGMENT);
485 ctx->ShaderCompilerOptions[i].LowerClipDistance = true;
486 }
487
488 ctx->ShaderCompilerOptions[MESA_SHADER_VERTEX].OptimizeForAOS = true;
489 ctx->ShaderCompilerOptions[MESA_SHADER_GEOMETRY].OptimizeForAOS = true;
490
491 /* ARB_viewport_array */
492 if (brw->gen >= 7 && ctx->API == API_OPENGL_CORE) {
493 ctx->Const.MaxViewports = GEN7_NUM_VIEWPORTS;
494 ctx->Const.ViewportSubpixelBits = 0;
495
496 /* Cast to float before negating becuase MaxViewportWidth is unsigned.
497 */
498 ctx->Const.ViewportBounds.Min = -(float)ctx->Const.MaxViewportWidth;
499 ctx->Const.ViewportBounds.Max = ctx->Const.MaxViewportWidth;
500 }
501 }
502
503 /**
504 * Process driconf (drirc) options, setting appropriate context flags.
505 *
506 * intelInitExtensions still pokes at optionCache directly, in order to
507 * avoid advertising various extensions. No flags are set, so it makes
508 * sense to continue doing that there.
509 */
510 static void
511 brw_process_driconf_options(struct brw_context *brw)
512 {
513 struct gl_context *ctx = &brw->ctx;
514
515 driOptionCache *options = &brw->optionCache;
516 driParseConfigFiles(options, &brw->intelScreen->optionCache,
517 brw->driContext->driScreenPriv->myNum, "i965");
518
519 int bo_reuse_mode = driQueryOptioni(options, "bo_reuse");
520 switch (bo_reuse_mode) {
521 case DRI_CONF_BO_REUSE_DISABLED:
522 break;
523 case DRI_CONF_BO_REUSE_ALL:
524 intel_bufmgr_gem_enable_reuse(brw->bufmgr);
525 break;
526 }
527
528 if (!driQueryOptionb(options, "hiz")) {
529 brw->has_hiz = false;
530 /* On gen6, you can only do separate stencil with HIZ. */
531 if (brw->gen == 6)
532 brw->has_separate_stencil = false;
533 }
534
535 if (driQueryOptionb(options, "always_flush_batch")) {
536 fprintf(stderr, "flushing batchbuffer before/after each draw call\n");
537 brw->always_flush_batch = true;
538 }
539
540 if (driQueryOptionb(options, "always_flush_cache")) {
541 fprintf(stderr, "flushing GPU caches before/after each draw call\n");
542 brw->always_flush_cache = true;
543 }
544
545 if (driQueryOptionb(options, "disable_throttling")) {
546 fprintf(stderr, "disabling flush throttling\n");
547 brw->disable_throttling = true;
548 }
549
550 brw->disable_derivative_optimization =
551 driQueryOptionb(&brw->optionCache, "disable_derivative_optimization");
552
553 brw->precompile = driQueryOptionb(&brw->optionCache, "shader_precompile");
554
555 ctx->Const.ForceGLSLExtensionsWarn =
556 driQueryOptionb(options, "force_glsl_extensions_warn");
557
558 ctx->Const.DisableGLSLLineContinuations =
559 driQueryOptionb(options, "disable_glsl_line_continuations");
560 }
561
562 GLboolean
563 brwCreateContext(gl_api api,
564 const struct gl_config *mesaVis,
565 __DRIcontext *driContextPriv,
566 unsigned major_version,
567 unsigned minor_version,
568 uint32_t flags,
569 bool notify_reset,
570 unsigned *dri_ctx_error,
571 void *sharedContextPrivate)
572 {
573 __DRIscreen *sPriv = driContextPriv->driScreenPriv;
574 struct gl_context *shareCtx = (struct gl_context *) sharedContextPrivate;
575 struct intel_screen *screen = sPriv->driverPrivate;
576 const struct brw_device_info *devinfo = screen->devinfo;
577 struct dd_function_table functions;
578 struct gl_config visual;
579
580 /* Only allow the __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS flag if the kernel
581 * provides us with context reset notifications.
582 */
583 uint32_t allowed_flags = __DRI_CTX_FLAG_DEBUG
584 | __DRI_CTX_FLAG_FORWARD_COMPATIBLE;
585
586 if (screen->has_context_reset_notification)
587 allowed_flags |= __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS;
588
589 if (flags & ~allowed_flags) {
590 *dri_ctx_error = __DRI_CTX_ERROR_UNKNOWN_FLAG;
591 return false;
592 }
593
594 struct brw_context *brw = rzalloc(NULL, struct brw_context);
595 if (!brw) {
596 printf("%s: failed to alloc context\n", __FUNCTION__);
597 *dri_ctx_error = __DRI_CTX_ERROR_NO_MEMORY;
598 return false;
599 }
600
601 driContextPriv->driverPrivate = brw;
602 brw->driContext = driContextPriv;
603 brw->intelScreen = screen;
604 brw->bufmgr = screen->bufmgr;
605
606 brw->gen = devinfo->gen;
607 brw->gt = devinfo->gt;
608 brw->is_g4x = devinfo->is_g4x;
609 brw->is_baytrail = devinfo->is_baytrail;
610 brw->is_haswell = devinfo->is_haswell;
611 brw->has_llc = devinfo->has_llc;
612 brw->has_hiz = devinfo->has_hiz_and_separate_stencil && brw->gen < 8;
613 brw->has_separate_stencil = devinfo->has_hiz_and_separate_stencil;
614 brw->has_pln = devinfo->has_pln;
615 brw->has_compr4 = devinfo->has_compr4;
616 brw->has_surface_tile_offset = devinfo->has_surface_tile_offset;
617 brw->has_negative_rhw_bug = devinfo->has_negative_rhw_bug;
618 brw->needs_unlit_centroid_workaround =
619 devinfo->needs_unlit_centroid_workaround;
620
621 brw->must_use_separate_stencil = screen->hw_must_use_separate_stencil;
622 brw->has_swizzling = screen->hw_has_swizzling;
623
624 if (brw->gen >= 8) {
625 gen8_init_vtable_surface_functions(brw);
626 gen7_init_vtable_sampler_functions(brw);
627 brw->vtbl.emit_depth_stencil_hiz = gen8_emit_depth_stencil_hiz;
628 } else if (brw->gen >= 7) {
629 gen7_init_vtable_surface_functions(brw);
630 gen7_init_vtable_sampler_functions(brw);
631 brw->vtbl.emit_depth_stencil_hiz = gen7_emit_depth_stencil_hiz;
632 } else {
633 gen4_init_vtable_surface_functions(brw);
634 gen4_init_vtable_sampler_functions(brw);
635 brw->vtbl.emit_depth_stencil_hiz = brw_emit_depth_stencil_hiz;
636 }
637
638 brw_init_driver_functions(brw, &functions);
639
640 if (notify_reset)
641 functions.GetGraphicsResetStatus = brw_get_graphics_reset_status;
642
643 struct gl_context *ctx = &brw->ctx;
644
645 if (mesaVis == NULL) {
646 memset(&visual, 0, sizeof visual);
647 mesaVis = &visual;
648 }
649
650 if (!_mesa_initialize_context(ctx, api, mesaVis, shareCtx, &functions)) {
651 *dri_ctx_error = __DRI_CTX_ERROR_NO_MEMORY;
652 printf("%s: failed to init mesa context\n", __FUNCTION__);
653 intelDestroyContext(driContextPriv);
654 return false;
655 }
656
657 driContextSetFlags(ctx, flags);
658
659 /* Initialize the software rasterizer and helper modules.
660 *
661 * As of GL 3.1 core, the gen4+ driver doesn't need the swrast context for
662 * software fallbacks (which we have to support on legacy GL to do weird
663 * glDrawPixels(), glBitmap(), and other functions).
664 */
665 if (api != API_OPENGL_CORE && api != API_OPENGLES2) {
666 _swrast_CreateContext(ctx);
667 }
668
669 _vbo_CreateContext(ctx);
670 if (ctx->swrast_context) {
671 _tnl_CreateContext(ctx);
672 TNL_CONTEXT(ctx)->Driver.RunPipeline = _tnl_run_pipeline;
673 _swsetup_CreateContext(ctx);
674
675 /* Configure swrast to match hardware characteristics: */
676 _swrast_allow_pixel_fog(ctx, false);
677 _swrast_allow_vertex_fog(ctx, true);
678 }
679
680 _mesa_meta_init(ctx);
681
682 brw_process_driconf_options(brw);
683 brw_process_intel_debug_variable(brw);
684 brw_initialize_context_constants(brw);
685
686 ctx->Const.ResetStrategy = notify_reset
687 ? GL_LOSE_CONTEXT_ON_RESET_ARB : GL_NO_RESET_NOTIFICATION_ARB;
688
689 /* Reinitialize the context point state. It depends on ctx->Const values. */
690 _mesa_init_point(ctx);
691
692 intel_batchbuffer_init(brw);
693
694 brw_init_state(brw);
695
696 intelInitExtensions(ctx);
697
698 intel_fbo_init(brw);
699
700 if (brw->gen >= 6) {
701 /* Create a new hardware context. Using a hardware context means that
702 * our GPU state will be saved/restored on context switch, allowing us
703 * to assume that the GPU is in the same state we left it in.
704 *
705 * This is required for transform feedback buffer offsets, query objects,
706 * and also allows us to reduce how much state we have to emit.
707 */
708 brw->hw_ctx = drm_intel_gem_context_create(brw->bufmgr);
709
710 if (!brw->hw_ctx) {
711 fprintf(stderr, "Gen6+ requires Kernel 3.6 or later.\n");
712 intelDestroyContext(driContextPriv);
713 return false;
714 }
715 }
716
717 brw_init_surface_formats(brw);
718
719 if (brw->is_g4x || brw->gen >= 5) {
720 brw->CMD_VF_STATISTICS = GM45_3DSTATE_VF_STATISTICS;
721 brw->CMD_PIPELINE_SELECT = CMD_PIPELINE_SELECT_GM45;
722 } else {
723 brw->CMD_VF_STATISTICS = GEN4_3DSTATE_VF_STATISTICS;
724 brw->CMD_PIPELINE_SELECT = CMD_PIPELINE_SELECT_965;
725 }
726
727 brw->max_vs_threads = devinfo->max_vs_threads;
728 brw->max_gs_threads = devinfo->max_gs_threads;
729 brw->max_wm_threads = devinfo->max_wm_threads;
730 brw->urb.size = devinfo->urb.size;
731 brw->urb.min_vs_entries = devinfo->urb.min_vs_entries;
732 brw->urb.max_vs_entries = devinfo->urb.max_vs_entries;
733 brw->urb.max_gs_entries = devinfo->urb.max_gs_entries;
734
735 /* Estimate the size of the mappable aperture into the GTT. There's an
736 * ioctl to get the whole GTT size, but not one to get the mappable subset.
737 * It turns out it's basically always 256MB, though some ancient hardware
738 * was smaller.
739 */
740 uint32_t gtt_size = 256 * 1024 * 1024;
741
742 /* We don't want to map two objects such that a memcpy between them would
743 * just fault one mapping in and then the other over and over forever. So
744 * we would need to divide the GTT size by 2. Additionally, some GTT is
745 * taken up by things like the framebuffer and the ringbuffer and such, so
746 * be more conservative.
747 */
748 brw->max_gtt_map_object_size = gtt_size / 4;
749
750 if (brw->gen == 6)
751 brw->urb.gen6_gs_previously_active = false;
752
753 brw->prim_restart.in_progress = false;
754 brw->prim_restart.enable_cut_index = false;
755 brw->gs.enabled = false;
756
757 if (brw->gen < 6) {
758 brw->curbe.last_buf = calloc(1, 4096);
759 brw->curbe.next_buf = calloc(1, 4096);
760 }
761
762 ctx->VertexProgram._MaintainTnlProgram = true;
763 ctx->FragmentProgram._MaintainTexEnvProgram = true;
764
765 brw_draw_init( brw );
766
767 if ((flags & __DRI_CTX_FLAG_DEBUG) != 0) {
768 /* Turn on some extra GL_ARB_debug_output generation. */
769 brw->perf_debug = true;
770 }
771
772 if ((flags & __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS) != 0)
773 ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_ROBUST_ACCESS_BIT_ARB;
774
775 brw_fs_alloc_reg_sets(brw);
776 brw_vec4_alloc_reg_set(brw);
777
778 if (INTEL_DEBUG & DEBUG_SHADER_TIME)
779 brw_init_shader_time(brw);
780
781 _mesa_compute_version(ctx);
782
783 _mesa_initialize_dispatch_tables(ctx);
784 _mesa_initialize_vbo_vtxfmt(ctx);
785
786 if (ctx->Extensions.AMD_performance_monitor) {
787 brw_init_performance_monitors(brw);
788 }
789
790 return true;
791 }
792
793 void
794 intelDestroyContext(__DRIcontext * driContextPriv)
795 {
796 struct brw_context *brw =
797 (struct brw_context *) driContextPriv->driverPrivate;
798 struct gl_context *ctx = &brw->ctx;
799
800 assert(brw); /* should never be null */
801 if (!brw)
802 return;
803
804 /* Dump a final BMP in case the application doesn't call SwapBuffers */
805 if (INTEL_DEBUG & DEBUG_AUB) {
806 intel_batchbuffer_flush(brw);
807 aub_dump_bmp(&brw->ctx);
808 }
809
810 _mesa_meta_free(&brw->ctx);
811
812 if (INTEL_DEBUG & DEBUG_SHADER_TIME) {
813 /* Force a report. */
814 brw->shader_time.report_time = 0;
815
816 brw_collect_and_report_shader_time(brw);
817 brw_destroy_shader_time(brw);
818 }
819
820 brw_destroy_state(brw);
821 brw_draw_destroy(brw);
822
823 drm_intel_bo_unreference(brw->curbe.curbe_bo);
824 drm_intel_bo_unreference(brw->vs.base.const_bo);
825 drm_intel_bo_unreference(brw->wm.base.const_bo);
826
827 free(brw->curbe.last_buf);
828 free(brw->curbe.next_buf);
829
830 drm_intel_gem_context_destroy(brw->hw_ctx);
831
832 if (ctx->swrast_context) {
833 _swsetup_DestroyContext(&brw->ctx);
834 _tnl_DestroyContext(&brw->ctx);
835 }
836 _vbo_DestroyContext(&brw->ctx);
837
838 if (ctx->swrast_context)
839 _swrast_DestroyContext(&brw->ctx);
840
841 intel_batchbuffer_free(brw);
842
843 drm_intel_bo_unreference(brw->first_post_swapbuffers_batch);
844 brw->first_post_swapbuffers_batch = NULL;
845
846 driDestroyOptionCache(&brw->optionCache);
847
848 /* free the Mesa context */
849 _mesa_free_context_data(&brw->ctx);
850
851 ralloc_free(brw);
852 driContextPriv->driverPrivate = NULL;
853 }
854
855 GLboolean
856 intelUnbindContext(__DRIcontext * driContextPriv)
857 {
858 /* Unset current context and dispath table */
859 _mesa_make_current(NULL, NULL, NULL);
860
861 return true;
862 }
863
864 /**
865 * Fixes up the context for GLES23 with our default-to-sRGB-capable behavior
866 * on window system framebuffers.
867 *
868 * Desktop GL is fairly reasonable in its handling of sRGB: You can ask if
869 * your renderbuffer can do sRGB encode, and you can flip a switch that does
870 * sRGB encode if the renderbuffer can handle it. You can ask specifically
871 * for a visual where you're guaranteed to be capable, but it turns out that
872 * everyone just makes all their ARGB8888 visuals capable and doesn't offer
873 * incapable ones, becuase there's no difference between the two in resources
874 * used. Applications thus get built that accidentally rely on the default
875 * visual choice being sRGB, so we make ours sRGB capable. Everything sounds
876 * great...
877 *
878 * But for GLES2/3, they decided that it was silly to not turn on sRGB encode
879 * for sRGB renderbuffers you made with the GL_EXT_texture_sRGB equivalent.
880 * So they removed the enable knob and made it "if the renderbuffer is sRGB
881 * capable, do sRGB encode". Then, for your window system renderbuffers, you
882 * can ask for sRGB visuals and get sRGB encode, or not ask for sRGB visuals
883 * and get no sRGB encode (assuming that both kinds of visual are available).
884 * Thus our choice to support sRGB by default on our visuals for desktop would
885 * result in broken rendering of GLES apps that aren't expecting sRGB encode.
886 *
887 * Unfortunately, renderbuffer setup happens before a context is created. So
888 * in intel_screen.c we always set up sRGB, and here, if you're a GLES2/3
889 * context (without an sRGB visual, though we don't have sRGB visuals exposed
890 * yet), we go turn that back off before anyone finds out.
891 */
892 static void
893 intel_gles3_srgb_workaround(struct brw_context *brw,
894 struct gl_framebuffer *fb)
895 {
896 struct gl_context *ctx = &brw->ctx;
897
898 if (_mesa_is_desktop_gl(ctx) || !fb->Visual.sRGBCapable)
899 return;
900
901 /* Some day when we support the sRGB capable bit on visuals available for
902 * GLES, we'll need to respect that and not disable things here.
903 */
904 fb->Visual.sRGBCapable = false;
905 for (int i = 0; i < BUFFER_COUNT; i++) {
906 if (fb->Attachment[i].Renderbuffer &&
907 fb->Attachment[i].Renderbuffer->Format == MESA_FORMAT_B8G8R8A8_SRGB) {
908 fb->Attachment[i].Renderbuffer->Format = MESA_FORMAT_B8G8R8A8_UNORM;
909 }
910 }
911 }
912
913 GLboolean
914 intelMakeCurrent(__DRIcontext * driContextPriv,
915 __DRIdrawable * driDrawPriv,
916 __DRIdrawable * driReadPriv)
917 {
918 struct brw_context *brw;
919 GET_CURRENT_CONTEXT(curCtx);
920
921 if (driContextPriv)
922 brw = (struct brw_context *) driContextPriv->driverPrivate;
923 else
924 brw = NULL;
925
926 /* According to the glXMakeCurrent() man page: "Pending commands to
927 * the previous context, if any, are flushed before it is released."
928 * But only flush if we're actually changing contexts.
929 */
930 if (brw_context(curCtx) && brw_context(curCtx) != brw) {
931 _mesa_flush(curCtx);
932 }
933
934 if (driContextPriv) {
935 struct gl_context *ctx = &brw->ctx;
936 struct gl_framebuffer *fb, *readFb;
937
938 if (driDrawPriv == NULL && driReadPriv == NULL) {
939 fb = _mesa_get_incomplete_framebuffer();
940 readFb = _mesa_get_incomplete_framebuffer();
941 } else {
942 fb = driDrawPriv->driverPrivate;
943 readFb = driReadPriv->driverPrivate;
944 driContextPriv->dri2.draw_stamp = driDrawPriv->dri2.stamp - 1;
945 driContextPriv->dri2.read_stamp = driReadPriv->dri2.stamp - 1;
946 }
947
948 /* The sRGB workaround changes the renderbuffer's format. We must change
949 * the format before the renderbuffer's miptree get's allocated, otherwise
950 * the formats of the renderbuffer and its miptree will differ.
951 */
952 intel_gles3_srgb_workaround(brw, fb);
953 intel_gles3_srgb_workaround(brw, readFb);
954
955 /* If the context viewport hasn't been initialized, force a call out to
956 * the loader to get buffers so we have a drawable size for the initial
957 * viewport. */
958 if (!brw->ctx.ViewportInitialized)
959 intel_prepare_render(brw);
960
961 _mesa_make_current(ctx, fb, readFb);
962 } else {
963 _mesa_make_current(NULL, NULL, NULL);
964 }
965
966 return true;
967 }
968
969 void
970 intel_resolve_for_dri2_flush(struct brw_context *brw,
971 __DRIdrawable *drawable)
972 {
973 if (brw->gen < 6) {
974 /* MSAA and fast color clear are not supported, so don't waste time
975 * checking whether a resolve is needed.
976 */
977 return;
978 }
979
980 struct gl_framebuffer *fb = drawable->driverPrivate;
981 struct intel_renderbuffer *rb;
982
983 /* Usually, only the back buffer will need to be downsampled. However,
984 * the front buffer will also need it if the user has rendered into it.
985 */
986 static const gl_buffer_index buffers[2] = {
987 BUFFER_BACK_LEFT,
988 BUFFER_FRONT_LEFT,
989 };
990
991 for (int i = 0; i < 2; ++i) {
992 rb = intel_get_renderbuffer(fb, buffers[i]);
993 if (rb == NULL || rb->mt == NULL)
994 continue;
995 if (rb->mt->num_samples <= 1)
996 intel_miptree_resolve_color(brw, rb->mt);
997 else
998 intel_miptree_downsample(brw, rb->mt);
999 }
1000 }
1001
1002 static unsigned
1003 intel_bits_per_pixel(const struct intel_renderbuffer *rb)
1004 {
1005 return _mesa_get_format_bytes(intel_rb_format(rb)) * 8;
1006 }
1007
1008 static void
1009 intel_query_dri2_buffers(struct brw_context *brw,
1010 __DRIdrawable *drawable,
1011 __DRIbuffer **buffers,
1012 int *count);
1013
1014 static void
1015 intel_process_dri2_buffer(struct brw_context *brw,
1016 __DRIdrawable *drawable,
1017 __DRIbuffer *buffer,
1018 struct intel_renderbuffer *rb,
1019 const char *buffer_name);
1020
1021 static void
1022 intel_update_image_buffers(struct brw_context *brw, __DRIdrawable *drawable);
1023
1024 static void
1025 intel_update_dri2_buffers(struct brw_context *brw, __DRIdrawable *drawable)
1026 {
1027 struct gl_framebuffer *fb = drawable->driverPrivate;
1028 struct intel_renderbuffer *rb;
1029 __DRIbuffer *buffers = NULL;
1030 int i, count;
1031 const char *region_name;
1032
1033 /* Set this up front, so that in case our buffers get invalidated
1034 * while we're getting new buffers, we don't clobber the stamp and
1035 * thus ignore the invalidate. */
1036 drawable->lastStamp = drawable->dri2.stamp;
1037
1038 if (unlikely(INTEL_DEBUG & DEBUG_DRI))
1039 fprintf(stderr, "enter %s, drawable %p\n", __func__, drawable);
1040
1041 intel_query_dri2_buffers(brw, drawable, &buffers, &count);
1042
1043 if (buffers == NULL)
1044 return;
1045
1046 for (i = 0; i < count; i++) {
1047 switch (buffers[i].attachment) {
1048 case __DRI_BUFFER_FRONT_LEFT:
1049 rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
1050 region_name = "dri2 front buffer";
1051 break;
1052
1053 case __DRI_BUFFER_FAKE_FRONT_LEFT:
1054 rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
1055 region_name = "dri2 fake front buffer";
1056 break;
1057
1058 case __DRI_BUFFER_BACK_LEFT:
1059 rb = intel_get_renderbuffer(fb, BUFFER_BACK_LEFT);
1060 region_name = "dri2 back buffer";
1061 break;
1062
1063 case __DRI_BUFFER_DEPTH:
1064 case __DRI_BUFFER_HIZ:
1065 case __DRI_BUFFER_DEPTH_STENCIL:
1066 case __DRI_BUFFER_STENCIL:
1067 case __DRI_BUFFER_ACCUM:
1068 default:
1069 fprintf(stderr,
1070 "unhandled buffer attach event, attachment type %d\n",
1071 buffers[i].attachment);
1072 return;
1073 }
1074
1075 intel_process_dri2_buffer(brw, drawable, &buffers[i], rb, region_name);
1076 }
1077
1078 }
1079
1080 void
1081 intel_update_renderbuffers(__DRIcontext *context, __DRIdrawable *drawable)
1082 {
1083 struct brw_context *brw = context->driverPrivate;
1084 __DRIscreen *screen = brw->intelScreen->driScrnPriv;
1085
1086 /* Set this up front, so that in case our buffers get invalidated
1087 * while we're getting new buffers, we don't clobber the stamp and
1088 * thus ignore the invalidate. */
1089 drawable->lastStamp = drawable->dri2.stamp;
1090
1091 if (unlikely(INTEL_DEBUG & DEBUG_DRI))
1092 fprintf(stderr, "enter %s, drawable %p\n", __func__, drawable);
1093
1094 if (screen->image.loader)
1095 intel_update_image_buffers(brw, drawable);
1096 else
1097 intel_update_dri2_buffers(brw, drawable);
1098
1099 driUpdateFramebufferSize(&brw->ctx, drawable);
1100 }
1101
1102 /**
1103 * intel_prepare_render should be called anywhere that curent read/drawbuffer
1104 * state is required.
1105 */
1106 void
1107 intel_prepare_render(struct brw_context *brw)
1108 {
1109 __DRIcontext *driContext = brw->driContext;
1110 __DRIdrawable *drawable;
1111
1112 drawable = driContext->driDrawablePriv;
1113 if (drawable && drawable->dri2.stamp != driContext->dri2.draw_stamp) {
1114 if (drawable->lastStamp != drawable->dri2.stamp)
1115 intel_update_renderbuffers(driContext, drawable);
1116 driContext->dri2.draw_stamp = drawable->dri2.stamp;
1117 }
1118
1119 drawable = driContext->driReadablePriv;
1120 if (drawable && drawable->dri2.stamp != driContext->dri2.read_stamp) {
1121 if (drawable->lastStamp != drawable->dri2.stamp)
1122 intel_update_renderbuffers(driContext, drawable);
1123 driContext->dri2.read_stamp = drawable->dri2.stamp;
1124 }
1125
1126 /* If we're currently rendering to the front buffer, the rendering
1127 * that will happen next will probably dirty the front buffer. So
1128 * mark it as dirty here.
1129 */
1130 if (brw->is_front_buffer_rendering)
1131 brw->front_buffer_dirty = true;
1132
1133 /* Wait for the swapbuffers before the one we just emitted, so we
1134 * don't get too many swaps outstanding for apps that are GPU-heavy
1135 * but not CPU-heavy.
1136 *
1137 * We're using intelDRI2Flush (called from the loader before
1138 * swapbuffer) and glFlush (for front buffer rendering) as the
1139 * indicator that a frame is done and then throttle when we get
1140 * here as we prepare to render the next frame. At this point for
1141 * round trips for swap/copy and getting new buffers are done and
1142 * we'll spend less time waiting on the GPU.
1143 *
1144 * Unfortunately, we don't have a handle to the batch containing
1145 * the swap, and getting our hands on that doesn't seem worth it,
1146 * so we just us the first batch we emitted after the last swap.
1147 */
1148 if (brw->need_throttle && brw->first_post_swapbuffers_batch) {
1149 if (!brw->disable_throttling)
1150 drm_intel_bo_wait_rendering(brw->first_post_swapbuffers_batch);
1151 drm_intel_bo_unreference(brw->first_post_swapbuffers_batch);
1152 brw->first_post_swapbuffers_batch = NULL;
1153 brw->need_throttle = false;
1154 }
1155 }
1156
1157 /**
1158 * \brief Query DRI2 to obtain a DRIdrawable's buffers.
1159 *
1160 * To determine which DRI buffers to request, examine the renderbuffers
1161 * attached to the drawable's framebuffer. Then request the buffers with
1162 * DRI2GetBuffers() or DRI2GetBuffersWithFormat().
1163 *
1164 * This is called from intel_update_renderbuffers().
1165 *
1166 * \param drawable Drawable whose buffers are queried.
1167 * \param buffers [out] List of buffers returned by DRI2 query.
1168 * \param buffer_count [out] Number of buffers returned.
1169 *
1170 * \see intel_update_renderbuffers()
1171 * \see DRI2GetBuffers()
1172 * \see DRI2GetBuffersWithFormat()
1173 */
1174 static void
1175 intel_query_dri2_buffers(struct brw_context *brw,
1176 __DRIdrawable *drawable,
1177 __DRIbuffer **buffers,
1178 int *buffer_count)
1179 {
1180 __DRIscreen *screen = brw->intelScreen->driScrnPriv;
1181 struct gl_framebuffer *fb = drawable->driverPrivate;
1182 int i = 0;
1183 unsigned attachments[8];
1184
1185 struct intel_renderbuffer *front_rb;
1186 struct intel_renderbuffer *back_rb;
1187
1188 front_rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
1189 back_rb = intel_get_renderbuffer(fb, BUFFER_BACK_LEFT);
1190
1191 memset(attachments, 0, sizeof(attachments));
1192 if ((brw->is_front_buffer_rendering ||
1193 brw->is_front_buffer_reading ||
1194 !back_rb) && front_rb) {
1195 /* If a fake front buffer is in use, then querying for
1196 * __DRI_BUFFER_FRONT_LEFT will cause the server to copy the image from
1197 * the real front buffer to the fake front buffer. So before doing the
1198 * query, we need to make sure all the pending drawing has landed in the
1199 * real front buffer.
1200 */
1201 intel_batchbuffer_flush(brw);
1202 intel_flush_front(&brw->ctx);
1203
1204 attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
1205 attachments[i++] = intel_bits_per_pixel(front_rb);
1206 } else if (front_rb && brw->front_buffer_dirty) {
1207 /* We have pending front buffer rendering, but we aren't querying for a
1208 * front buffer. If the front buffer we have is a fake front buffer,
1209 * the X server is going to throw it away when it processes the query.
1210 * So before doing the query, make sure all the pending drawing has
1211 * landed in the real front buffer.
1212 */
1213 intel_batchbuffer_flush(brw);
1214 intel_flush_front(&brw->ctx);
1215 }
1216
1217 if (back_rb) {
1218 attachments[i++] = __DRI_BUFFER_BACK_LEFT;
1219 attachments[i++] = intel_bits_per_pixel(back_rb);
1220 }
1221
1222 assert(i <= ARRAY_SIZE(attachments));
1223
1224 *buffers = screen->dri2.loader->getBuffersWithFormat(drawable,
1225 &drawable->w,
1226 &drawable->h,
1227 attachments, i / 2,
1228 buffer_count,
1229 drawable->loaderPrivate);
1230 }
1231
1232 /**
1233 * \brief Assign a DRI buffer's DRM region to a renderbuffer.
1234 *
1235 * This is called from intel_update_renderbuffers().
1236 *
1237 * \par Note:
1238 * DRI buffers whose attachment point is DRI2BufferStencil or
1239 * DRI2BufferDepthStencil are handled as special cases.
1240 *
1241 * \param buffer_name is a human readable name, such as "dri2 front buffer",
1242 * that is passed to intel_region_alloc_for_handle().
1243 *
1244 * \see intel_update_renderbuffers()
1245 * \see intel_region_alloc_for_handle()
1246 */
1247 static void
1248 intel_process_dri2_buffer(struct brw_context *brw,
1249 __DRIdrawable *drawable,
1250 __DRIbuffer *buffer,
1251 struct intel_renderbuffer *rb,
1252 const char *buffer_name)
1253 {
1254 struct intel_region *region = NULL;
1255
1256 if (!rb)
1257 return;
1258
1259 unsigned num_samples = rb->Base.Base.NumSamples;
1260
1261 /* We try to avoid closing and reopening the same BO name, because the first
1262 * use of a mapping of the buffer involves a bunch of page faulting which is
1263 * moderately expensive.
1264 */
1265 if (num_samples == 0) {
1266 if (rb->mt &&
1267 rb->mt->region &&
1268 rb->mt->region->name == buffer->name)
1269 return;
1270 } else {
1271 if (rb->mt &&
1272 rb->mt->singlesample_mt &&
1273 rb->mt->singlesample_mt->region &&
1274 rb->mt->singlesample_mt->region->name == buffer->name)
1275 return;
1276 }
1277
1278 if (unlikely(INTEL_DEBUG & DEBUG_DRI)) {
1279 fprintf(stderr,
1280 "attaching buffer %d, at %d, cpp %d, pitch %d\n",
1281 buffer->name, buffer->attachment,
1282 buffer->cpp, buffer->pitch);
1283 }
1284
1285 intel_miptree_release(&rb->mt);
1286 region = intel_region_alloc_for_handle(brw->intelScreen,
1287 buffer->cpp,
1288 drawable->w,
1289 drawable->h,
1290 buffer->pitch,
1291 buffer->name,
1292 buffer_name);
1293 if (!region) {
1294 fprintf(stderr,
1295 "Failed to make region for returned DRI2 buffer "
1296 "(%dx%d, named %d).\n"
1297 "This is likely a bug in the X Server that will lead to a "
1298 "crash soon.\n",
1299 drawable->w, drawable->h, buffer->name);
1300 return;
1301 }
1302
1303 rb->mt = intel_miptree_create_for_dri2_buffer(brw,
1304 buffer->attachment,
1305 intel_rb_format(rb),
1306 num_samples,
1307 region);
1308
1309 assert(rb->mt);
1310
1311 intel_region_release(&region);
1312 }
1313
1314 /**
1315 * \brief Query DRI image loader to obtain a DRIdrawable's buffers.
1316 *
1317 * To determine which DRI buffers to request, examine the renderbuffers
1318 * attached to the drawable's framebuffer. Then request the buffers from
1319 * the image loader
1320 *
1321 * This is called from intel_update_renderbuffers().
1322 *
1323 * \param drawable Drawable whose buffers are queried.
1324 * \param buffers [out] List of buffers returned by DRI2 query.
1325 * \param buffer_count [out] Number of buffers returned.
1326 *
1327 * \see intel_update_renderbuffers()
1328 */
1329
1330 static void
1331 intel_update_image_buffer(struct brw_context *intel,
1332 __DRIdrawable *drawable,
1333 struct intel_renderbuffer *rb,
1334 __DRIimage *buffer,
1335 enum __DRIimageBufferMask buffer_type)
1336 {
1337 struct intel_region *region = buffer->region;
1338
1339 if (!rb || !region)
1340 return;
1341
1342 unsigned num_samples = rb->Base.Base.NumSamples;
1343
1344 /* Check and see if we're already bound to the right
1345 * buffer object
1346 */
1347 if (num_samples == 0) {
1348 if (rb->mt &&
1349 rb->mt->region &&
1350 rb->mt->region->bo == region->bo)
1351 return;
1352 } else {
1353 if (rb->mt &&
1354 rb->mt->singlesample_mt &&
1355 rb->mt->singlesample_mt->region &&
1356 rb->mt->singlesample_mt->region->bo == region->bo)
1357 return;
1358 }
1359
1360 intel_miptree_release(&rb->mt);
1361 rb->mt = intel_miptree_create_for_image_buffer(intel,
1362 buffer_type,
1363 intel_rb_format(rb),
1364 num_samples,
1365 region);
1366 }
1367
1368 static void
1369 intel_update_image_buffers(struct brw_context *brw, __DRIdrawable *drawable)
1370 {
1371 struct gl_framebuffer *fb = drawable->driverPrivate;
1372 __DRIscreen *screen = brw->intelScreen->driScrnPriv;
1373 struct intel_renderbuffer *front_rb;
1374 struct intel_renderbuffer *back_rb;
1375 struct __DRIimageList images;
1376 unsigned int format;
1377 uint32_t buffer_mask = 0;
1378
1379 front_rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
1380 back_rb = intel_get_renderbuffer(fb, BUFFER_BACK_LEFT);
1381
1382 if (back_rb)
1383 format = intel_rb_format(back_rb);
1384 else if (front_rb)
1385 format = intel_rb_format(front_rb);
1386 else
1387 return;
1388
1389 if ((brw->is_front_buffer_rendering || brw->is_front_buffer_reading || !back_rb) && front_rb)
1390 buffer_mask |= __DRI_IMAGE_BUFFER_FRONT;
1391
1392 if (back_rb)
1393 buffer_mask |= __DRI_IMAGE_BUFFER_BACK;
1394
1395 (*screen->image.loader->getBuffers) (drawable,
1396 driGLFormatToImageFormat(format),
1397 &drawable->dri2.stamp,
1398 drawable->loaderPrivate,
1399 buffer_mask,
1400 &images);
1401
1402 if (images.image_mask & __DRI_IMAGE_BUFFER_FRONT) {
1403 drawable->w = images.front->width;
1404 drawable->h = images.front->height;
1405 intel_update_image_buffer(brw,
1406 drawable,
1407 front_rb,
1408 images.front,
1409 __DRI_IMAGE_BUFFER_FRONT);
1410 }
1411 if (images.image_mask & __DRI_IMAGE_BUFFER_BACK) {
1412 drawable->w = images.back->width;
1413 drawable->h = images.back->height;
1414 intel_update_image_buffer(brw,
1415 drawable,
1416 back_rb,
1417 images.back,
1418 __DRI_IMAGE_BUFFER_BACK);
1419 }
1420 }