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