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