intel/isl: Rename supports_lossless_compression to supports_ccs_e
[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 "compiler/nir/nir.h"
35 #include "main/api_exec.h"
36 #include "main/context.h"
37 #include "main/fbobject.h"
38 #include "main/extensions.h"
39 #include "main/imports.h"
40 #include "main/macros.h"
41 #include "main/points.h"
42 #include "main/version.h"
43 #include "main/vtxfmt.h"
44 #include "main/texobj.h"
45 #include "main/framebuffer.h"
46
47 #include "vbo/vbo_context.h"
48
49 #include "drivers/common/driverfuncs.h"
50 #include "drivers/common/meta.h"
51 #include "utils.h"
52
53 #include "brw_context.h"
54 #include "brw_defines.h"
55 #include "brw_blorp.h"
56 #include "brw_compiler.h"
57 #include "brw_draw.h"
58 #include "brw_state.h"
59
60 #include "intel_batchbuffer.h"
61 #include "intel_buffer_objects.h"
62 #include "intel_buffers.h"
63 #include "intel_fbo.h"
64 #include "intel_mipmap_tree.h"
65 #include "intel_pixel.h"
66 #include "intel_image.h"
67 #include "intel_tex.h"
68 #include "intel_tex_obj.h"
69
70 #include "swrast_setup/swrast_setup.h"
71 #include "tnl/tnl.h"
72 #include "tnl/t_pipeline.h"
73 #include "util/ralloc.h"
74 #include "util/debug.h"
75 #include "isl/isl.h"
76
77 /***************************************
78 * Mesa's Driver Functions
79 ***************************************/
80
81 const char *const brw_vendor_string = "Intel Open Source Technology Center";
82
83 static const char *
84 get_bsw_model(const struct intel_screen *screen)
85 {
86 switch (screen->eu_total) {
87 case 16:
88 return "405";
89 case 12:
90 return "400";
91 default:
92 return " ";
93 }
94 }
95
96 const char *
97 brw_get_renderer_string(const struct intel_screen *screen)
98 {
99 const char *chipset;
100 static char buffer[128];
101 char *bsw = NULL;
102
103 switch (screen->deviceID) {
104 #undef CHIPSET
105 #define CHIPSET(id, symbol, str) case id: chipset = str; break;
106 #include "pci_ids/i965_pci_ids.h"
107 default:
108 chipset = "Unknown Intel Chipset";
109 break;
110 }
111
112 /* Braswell branding is funny, so we have to fix it up here */
113 if (screen->deviceID == 0x22B1) {
114 bsw = strdup(chipset);
115 char *needle = strstr(bsw, "XXX");
116 if (needle) {
117 memcpy(needle, get_bsw_model(screen), 3);
118 chipset = bsw;
119 }
120 }
121
122 (void) driGetRendererString(buffer, chipset, 0);
123 free(bsw);
124 return buffer;
125 }
126
127 static const GLubyte *
128 intel_get_string(struct gl_context * ctx, GLenum name)
129 {
130 const struct brw_context *const brw = brw_context(ctx);
131
132 switch (name) {
133 case GL_VENDOR:
134 return (GLubyte *) brw_vendor_string;
135
136 case GL_RENDERER:
137 return
138 (GLubyte *) brw_get_renderer_string(brw->screen);
139
140 default:
141 return NULL;
142 }
143 }
144
145 static void
146 intel_viewport(struct gl_context *ctx)
147 {
148 struct brw_context *brw = brw_context(ctx);
149 __DRIcontext *driContext = brw->driContext;
150
151 if (_mesa_is_winsys_fbo(ctx->DrawBuffer)) {
152 if (driContext->driDrawablePriv)
153 dri2InvalidateDrawable(driContext->driDrawablePriv);
154 if (driContext->driReadablePriv)
155 dri2InvalidateDrawable(driContext->driReadablePriv);
156 }
157 }
158
159 static void
160 intel_update_framebuffer(struct gl_context *ctx,
161 struct gl_framebuffer *fb)
162 {
163 struct brw_context *brw = brw_context(ctx);
164
165 /* Quantize the derived default number of samples
166 */
167 fb->DefaultGeometry._NumSamples =
168 intel_quantize_num_samples(brw->screen,
169 fb->DefaultGeometry.NumSamples);
170 }
171
172 static bool
173 intel_disable_rb_aux_buffer(struct brw_context *brw, const drm_intel_bo *bo)
174 {
175 const struct gl_framebuffer *fb = brw->ctx.DrawBuffer;
176 bool found = false;
177
178 for (unsigned i = 0; i < fb->_NumColorDrawBuffers; i++) {
179 const struct intel_renderbuffer *irb =
180 intel_renderbuffer(fb->_ColorDrawBuffers[i]);
181
182 if (irb && irb->mt->bo == bo) {
183 found = brw->draw_aux_buffer_disabled[i] = true;
184 }
185 }
186
187 return found;
188 }
189
190 /* On Gen9 color buffers may be compressed by the hardware (lossless
191 * compression). There are, however, format restrictions and care needs to be
192 * taken that the sampler engine is capable for re-interpreting a buffer with
193 * format different the buffer was originally written with.
194 *
195 * For example, SRGB formats are not compressible and the sampler engine isn't
196 * capable of treating RGBA_UNORM as SRGB_ALPHA. In such a case the underlying
197 * color buffer needs to be resolved so that the sampling surface can be
198 * sampled as non-compressed (i.e., without the auxiliary MCS buffer being
199 * set).
200 */
201 static bool
202 intel_texture_view_requires_resolve(struct brw_context *brw,
203 struct intel_texture_object *intel_tex)
204 {
205 if (brw->gen < 9 ||
206 !intel_miptree_is_lossless_compressed(brw, intel_tex->mt))
207 return false;
208
209 const uint32_t brw_format = brw_format_for_mesa_format(intel_tex->_Format);
210
211 if (isl_format_supports_ccs_e(&brw->screen->devinfo, brw_format))
212 return false;
213
214 perf_debug("Incompatible sampling format (%s) for rbc (%s)\n",
215 _mesa_get_format_name(intel_tex->_Format),
216 _mesa_get_format_name(intel_tex->mt->format));
217
218 if (intel_disable_rb_aux_buffer(brw, intel_tex->mt->bo))
219 perf_debug("Sampling renderbuffer with non-compressible format - "
220 "turning off compression");
221
222 return true;
223 }
224
225 static void
226 intel_update_state(struct gl_context * ctx, GLuint new_state)
227 {
228 struct brw_context *brw = brw_context(ctx);
229 struct intel_texture_object *tex_obj;
230 struct intel_renderbuffer *depth_irb;
231
232 if (ctx->swrast_context)
233 _swrast_InvalidateState(ctx, new_state);
234 _vbo_InvalidateState(ctx, new_state);
235
236 brw->NewGLState |= new_state;
237
238 _mesa_unlock_context_textures(ctx);
239
240 /* Resolve the depth buffer's HiZ buffer. */
241 depth_irb = intel_get_renderbuffer(ctx->DrawBuffer, BUFFER_DEPTH);
242 if (depth_irb)
243 intel_renderbuffer_resolve_hiz(brw, depth_irb);
244
245 memset(brw->draw_aux_buffer_disabled, 0,
246 sizeof(brw->draw_aux_buffer_disabled));
247
248 /* Resolve depth buffer and render cache of each enabled texture. */
249 int maxEnabledUnit = ctx->Texture._MaxEnabledTexImageUnit;
250 for (int i = 0; i <= maxEnabledUnit; i++) {
251 if (!ctx->Texture.Unit[i]._Current)
252 continue;
253 tex_obj = intel_texture_object(ctx->Texture.Unit[i]._Current);
254 if (!tex_obj || !tex_obj->mt)
255 continue;
256 if (intel_miptree_sample_with_hiz(brw, tex_obj->mt))
257 intel_miptree_all_slices_resolve_hiz(brw, tex_obj->mt);
258 else
259 intel_miptree_all_slices_resolve_depth(brw, tex_obj->mt);
260 /* Sampling engine understands lossless compression and resolving
261 * those surfaces should be skipped for performance reasons.
262 */
263 const int flags = intel_texture_view_requires_resolve(brw, tex_obj) ?
264 0 : INTEL_MIPTREE_IGNORE_CCS_E;
265 intel_miptree_all_slices_resolve_color(brw, tex_obj->mt, flags);
266 brw_render_cache_set_check_flush(brw, tex_obj->mt->bo);
267
268 if (tex_obj->base.StencilSampling ||
269 tex_obj->mt->format == MESA_FORMAT_S_UINT8) {
270 intel_update_r8stencil(brw, tex_obj->mt);
271 }
272 }
273
274 /* Resolve color for each active shader image. */
275 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
276 const struct gl_program *prog = ctx->_Shader->CurrentProgram[i];
277
278 if (unlikely(prog && prog->info.num_images)) {
279 for (unsigned j = 0; j < prog->info.num_images; j++) {
280 struct gl_image_unit *u =
281 &ctx->ImageUnits[prog->sh.ImageUnits[j]];
282 tex_obj = intel_texture_object(u->TexObj);
283
284 if (tex_obj && tex_obj->mt) {
285 /* Access to images is implemented using indirect messages
286 * against data port. Normal render target write understands
287 * lossless compression but unfortunately the typed/untyped
288 * read/write interface doesn't. Therefore even lossless
289 * compressed surfaces need to be resolved prior to accessing
290 * them. Hence skip setting INTEL_MIPTREE_IGNORE_CCS_E.
291 */
292 intel_miptree_all_slices_resolve_color(brw, tex_obj->mt, 0);
293
294 if (intel_miptree_is_lossless_compressed(brw, tex_obj->mt) &&
295 intel_disable_rb_aux_buffer(brw, tex_obj->mt->bo)) {
296 perf_debug("Using renderbuffer as shader image - turning "
297 "off lossless compression");
298 }
299
300 brw_render_cache_set_check_flush(brw, tex_obj->mt->bo);
301 }
302 }
303 }
304 }
305
306 /* Resolve color buffers for non-coherent framebuffer fetch. */
307 if (!ctx->Extensions.MESA_shader_framebuffer_fetch &&
308 ctx->FragmentProgram._Current &&
309 ctx->FragmentProgram._Current->info.outputs_read) {
310 const struct gl_framebuffer *fb = ctx->DrawBuffer;
311
312 for (unsigned i = 0; i < fb->_NumColorDrawBuffers; i++) {
313 const struct intel_renderbuffer *irb =
314 intel_renderbuffer(fb->_ColorDrawBuffers[i]);
315
316 if (irb &&
317 intel_miptree_resolve_color(
318 brw, irb->mt, irb->mt_level, irb->mt_layer, irb->layer_count,
319 INTEL_MIPTREE_IGNORE_CCS_E))
320 brw_render_cache_set_check_flush(brw, irb->mt->bo);
321 }
322 }
323
324 /* If FRAMEBUFFER_SRGB is used on Gen9+ then we need to resolve any of the
325 * single-sampled color renderbuffers because the CCS buffer isn't
326 * supported for SRGB formats. This only matters if FRAMEBUFFER_SRGB is
327 * enabled because otherwise the surface state will be programmed with the
328 * linear equivalent format anyway.
329 */
330 if (brw->gen >= 9 && ctx->Color.sRGBEnabled) {
331 struct gl_framebuffer *fb = ctx->DrawBuffer;
332 for (int i = 0; i < fb->_NumColorDrawBuffers; i++) {
333 struct gl_renderbuffer *rb = fb->_ColorDrawBuffers[i];
334
335 if (rb == NULL)
336 continue;
337
338 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
339 struct intel_mipmap_tree *mt = irb->mt;
340
341 if (mt == NULL ||
342 mt->num_samples > 1 ||
343 _mesa_get_srgb_format_linear(mt->format) == mt->format)
344 continue;
345
346 /* Lossless compression is not supported for SRGB formats, it
347 * should be impossible to get here with such surfaces.
348 */
349 assert(!intel_miptree_is_lossless_compressed(brw, mt));
350 intel_miptree_all_slices_resolve_color(brw, mt, 0);
351 brw_render_cache_set_check_flush(brw, mt->bo);
352 }
353 }
354
355 _mesa_lock_context_textures(ctx);
356
357 if (new_state & _NEW_BUFFERS) {
358 intel_update_framebuffer(ctx, ctx->DrawBuffer);
359 if (ctx->DrawBuffer != ctx->ReadBuffer)
360 intel_update_framebuffer(ctx, ctx->ReadBuffer);
361 }
362 }
363
364 #define flushFront(screen) ((screen)->image.loader ? (screen)->image.loader->flushFrontBuffer : (screen)->dri2.loader->flushFrontBuffer)
365
366 static void
367 intel_flush_front(struct gl_context *ctx)
368 {
369 struct brw_context *brw = brw_context(ctx);
370 __DRIcontext *driContext = brw->driContext;
371 __DRIdrawable *driDrawable = driContext->driDrawablePriv;
372 __DRIscreen *const dri_screen = brw->screen->driScrnPriv;
373
374 if (brw->front_buffer_dirty && _mesa_is_winsys_fbo(ctx->DrawBuffer)) {
375 if (flushFront(dri_screen) && driDrawable &&
376 driDrawable->loaderPrivate) {
377
378 /* Resolve before flushing FAKE_FRONT_LEFT to FRONT_LEFT.
379 *
380 * This potentially resolves both front and back buffer. It
381 * is unnecessary to resolve the back, but harms nothing except
382 * performance. And no one cares about front-buffer render
383 * performance.
384 */
385 intel_resolve_for_dri2_flush(brw, driDrawable);
386 intel_batchbuffer_flush(brw);
387
388 flushFront(dri_screen)(driDrawable, driDrawable->loaderPrivate);
389
390 /* We set the dirty bit in intel_prepare_render() if we're
391 * front buffer rendering once we get there.
392 */
393 brw->front_buffer_dirty = false;
394 }
395 }
396 }
397
398 static void
399 intel_glFlush(struct gl_context *ctx)
400 {
401 struct brw_context *brw = brw_context(ctx);
402
403 intel_batchbuffer_flush(brw);
404 intel_flush_front(ctx);
405
406 brw->need_flush_throttle = true;
407 }
408
409 static void
410 intel_finish(struct gl_context * ctx)
411 {
412 struct brw_context *brw = brw_context(ctx);
413
414 intel_glFlush(ctx);
415
416 if (brw->batch.last_bo)
417 drm_intel_bo_wait_rendering(brw->batch.last_bo);
418 }
419
420 static void
421 brw_init_driver_functions(struct brw_context *brw,
422 struct dd_function_table *functions)
423 {
424 _mesa_init_driver_functions(functions);
425
426 /* GLX uses DRI2 invalidate events to handle window resizing.
427 * Unfortunately, EGL does not - libEGL is written in XCB (not Xlib),
428 * which doesn't provide a mechanism for snooping the event queues.
429 *
430 * So EGL still relies on viewport hacks to handle window resizing.
431 * This should go away with DRI3000.
432 */
433 if (!brw->driContext->driScreenPriv->dri2.useInvalidate)
434 functions->Viewport = intel_viewport;
435
436 functions->Flush = intel_glFlush;
437 functions->Finish = intel_finish;
438 functions->GetString = intel_get_string;
439 functions->UpdateState = intel_update_state;
440
441 intelInitTextureFuncs(functions);
442 intelInitTextureImageFuncs(functions);
443 intelInitTextureSubImageFuncs(functions);
444 intelInitTextureCopyImageFuncs(functions);
445 intelInitCopyImageFuncs(functions);
446 intelInitClearFuncs(functions);
447 intelInitBufferFuncs(functions);
448 intelInitPixelFuncs(functions);
449 intelInitBufferObjectFuncs(functions);
450 brw_init_syncobj_functions(functions);
451 brw_init_object_purgeable_functions(functions);
452
453 brwInitFragProgFuncs( functions );
454 brw_init_common_queryobj_functions(functions);
455 if (brw->gen >= 8 || brw->is_haswell)
456 hsw_init_queryobj_functions(functions);
457 else if (brw->gen >= 6)
458 gen6_init_queryobj_functions(functions);
459 else
460 gen4_init_queryobj_functions(functions);
461 brw_init_compute_functions(functions);
462 if (brw->gen >= 7)
463 brw_init_conditional_render_functions(functions);
464
465 functions->QueryInternalFormat = brw_query_internal_format;
466
467 functions->NewTransformFeedback = brw_new_transform_feedback;
468 functions->DeleteTransformFeedback = brw_delete_transform_feedback;
469 if (can_do_mi_math_and_lrr(brw->screen)) {
470 functions->BeginTransformFeedback = hsw_begin_transform_feedback;
471 functions->EndTransformFeedback = hsw_end_transform_feedback;
472 functions->PauseTransformFeedback = hsw_pause_transform_feedback;
473 functions->ResumeTransformFeedback = hsw_resume_transform_feedback;
474 } else if (brw->gen >= 7) {
475 functions->BeginTransformFeedback = gen7_begin_transform_feedback;
476 functions->EndTransformFeedback = gen7_end_transform_feedback;
477 functions->PauseTransformFeedback = gen7_pause_transform_feedback;
478 functions->ResumeTransformFeedback = gen7_resume_transform_feedback;
479 functions->GetTransformFeedbackVertexCount =
480 brw_get_transform_feedback_vertex_count;
481 } else {
482 functions->BeginTransformFeedback = brw_begin_transform_feedback;
483 functions->EndTransformFeedback = brw_end_transform_feedback;
484 }
485
486 if (brw->gen >= 6)
487 functions->GetSamplePosition = gen6_get_sample_position;
488 }
489
490 static void
491 brw_initialize_context_constants(struct brw_context *brw)
492 {
493 struct gl_context *ctx = &brw->ctx;
494 const struct brw_compiler *compiler = brw->screen->compiler;
495
496 const bool stage_exists[MESA_SHADER_STAGES] = {
497 [MESA_SHADER_VERTEX] = true,
498 [MESA_SHADER_TESS_CTRL] = brw->gen >= 7,
499 [MESA_SHADER_TESS_EVAL] = brw->gen >= 7,
500 [MESA_SHADER_GEOMETRY] = brw->gen >= 6,
501 [MESA_SHADER_FRAGMENT] = true,
502 [MESA_SHADER_COMPUTE] =
503 ((ctx->API == API_OPENGL_COMPAT || ctx->API == API_OPENGL_CORE) &&
504 ctx->Const.MaxComputeWorkGroupSize[0] >= 1024) ||
505 (ctx->API == API_OPENGLES2 &&
506 ctx->Const.MaxComputeWorkGroupSize[0] >= 128) ||
507 _mesa_extension_override_enables.ARB_compute_shader,
508 };
509
510 unsigned num_stages = 0;
511 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
512 if (stage_exists[i])
513 num_stages++;
514 }
515
516 unsigned max_samplers =
517 brw->gen >= 8 || brw->is_haswell ? BRW_MAX_TEX_UNIT : 16;
518
519 ctx->Const.MaxDualSourceDrawBuffers = 1;
520 ctx->Const.MaxDrawBuffers = BRW_MAX_DRAW_BUFFERS;
521 ctx->Const.MaxCombinedShaderOutputResources =
522 MAX_IMAGE_UNITS + BRW_MAX_DRAW_BUFFERS;
523
524 ctx->Const.QueryCounterBits.Timestamp = 36;
525
526 ctx->Const.MaxTextureCoordUnits = 8; /* Mesa limit */
527 ctx->Const.MaxImageUnits = MAX_IMAGE_UNITS;
528 if (brw->gen >= 7) {
529 ctx->Const.MaxRenderbufferSize = 16384;
530 ctx->Const.MaxTextureLevels = MIN2(15 /* 16384 */, MAX_TEXTURE_LEVELS);
531 ctx->Const.MaxCubeTextureLevels = 15; /* 16384 */
532 } else {
533 ctx->Const.MaxRenderbufferSize = 8192;
534 ctx->Const.MaxTextureLevels = MIN2(14 /* 8192 */, MAX_TEXTURE_LEVELS);
535 ctx->Const.MaxCubeTextureLevels = 14; /* 8192 */
536 }
537 ctx->Const.Max3DTextureLevels = 12; /* 2048 */
538 ctx->Const.MaxArrayTextureLayers = brw->gen >= 7 ? 2048 : 512;
539 ctx->Const.MaxTextureMbytes = 1536;
540 ctx->Const.MaxTextureRectSize = 1 << 12;
541 ctx->Const.MaxTextureMaxAnisotropy = 16.0;
542 ctx->Const.MaxTextureLodBias = 15.0;
543 ctx->Const.StripTextureBorder = true;
544 if (brw->gen >= 7) {
545 ctx->Const.MaxProgramTextureGatherComponents = 4;
546 ctx->Const.MinProgramTextureGatherOffset = -32;
547 ctx->Const.MaxProgramTextureGatherOffset = 31;
548 } else if (brw->gen == 6) {
549 ctx->Const.MaxProgramTextureGatherComponents = 1;
550 ctx->Const.MinProgramTextureGatherOffset = -8;
551 ctx->Const.MaxProgramTextureGatherOffset = 7;
552 }
553
554 ctx->Const.MaxUniformBlockSize = 65536;
555
556 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
557 struct gl_program_constants *prog = &ctx->Const.Program[i];
558
559 if (!stage_exists[i])
560 continue;
561
562 prog->MaxTextureImageUnits = max_samplers;
563
564 prog->MaxUniformBlocks = BRW_MAX_UBO;
565 prog->MaxCombinedUniformComponents =
566 prog->MaxUniformComponents +
567 ctx->Const.MaxUniformBlockSize / 4 * prog->MaxUniformBlocks;
568
569 prog->MaxAtomicCounters = MAX_ATOMIC_COUNTERS;
570 prog->MaxAtomicBuffers = BRW_MAX_ABO;
571 prog->MaxImageUniforms = compiler->scalar_stage[i] ? BRW_MAX_IMAGES : 0;
572 prog->MaxShaderStorageBlocks = BRW_MAX_SSBO;
573 }
574
575 ctx->Const.MaxTextureUnits =
576 MIN2(ctx->Const.MaxTextureCoordUnits,
577 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits);
578
579 ctx->Const.MaxUniformBufferBindings = num_stages * BRW_MAX_UBO;
580 ctx->Const.MaxCombinedUniformBlocks = num_stages * BRW_MAX_UBO;
581 ctx->Const.MaxCombinedAtomicBuffers = num_stages * BRW_MAX_ABO;
582 ctx->Const.MaxCombinedShaderStorageBlocks = num_stages * BRW_MAX_SSBO;
583 ctx->Const.MaxShaderStorageBufferBindings = num_stages * BRW_MAX_SSBO;
584 ctx->Const.MaxCombinedTextureImageUnits = num_stages * max_samplers;
585 ctx->Const.MaxCombinedImageUniforms = num_stages * BRW_MAX_IMAGES;
586
587
588 /* Hardware only supports a limited number of transform feedback buffers.
589 * So we need to override the Mesa default (which is based only on software
590 * limits).
591 */
592 ctx->Const.MaxTransformFeedbackBuffers = BRW_MAX_SOL_BUFFERS;
593
594 /* On Gen6, in the worst case, we use up one binding table entry per
595 * transform feedback component (see comments above the definition of
596 * BRW_MAX_SOL_BINDINGS, in brw_context.h), so we need to advertise a value
597 * for MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS equal to
598 * BRW_MAX_SOL_BINDINGS.
599 *
600 * In "separate components" mode, we need to divide this value by
601 * BRW_MAX_SOL_BUFFERS, so that the total number of binding table entries
602 * used up by all buffers will not exceed BRW_MAX_SOL_BINDINGS.
603 */
604 ctx->Const.MaxTransformFeedbackInterleavedComponents = BRW_MAX_SOL_BINDINGS;
605 ctx->Const.MaxTransformFeedbackSeparateComponents =
606 BRW_MAX_SOL_BINDINGS / BRW_MAX_SOL_BUFFERS;
607
608 ctx->Const.AlwaysUseGetTransformFeedbackVertexCount =
609 !can_do_mi_math_and_lrr(brw->screen);
610
611 int max_samples;
612 const int *msaa_modes = intel_supported_msaa_modes(brw->screen);
613 const int clamp_max_samples =
614 driQueryOptioni(&brw->optionCache, "clamp_max_samples");
615
616 if (clamp_max_samples < 0) {
617 max_samples = msaa_modes[0];
618 } else {
619 /* Select the largest supported MSAA mode that does not exceed
620 * clamp_max_samples.
621 */
622 max_samples = 0;
623 for (int i = 0; msaa_modes[i] != 0; ++i) {
624 if (msaa_modes[i] <= clamp_max_samples) {
625 max_samples = msaa_modes[i];
626 break;
627 }
628 }
629 }
630
631 ctx->Const.MaxSamples = max_samples;
632 ctx->Const.MaxColorTextureSamples = max_samples;
633 ctx->Const.MaxDepthTextureSamples = max_samples;
634 ctx->Const.MaxIntegerSamples = max_samples;
635 ctx->Const.MaxImageSamples = 0;
636
637 /* gen6_set_sample_maps() sets SampleMap{2,4,8}x variables which are used
638 * to map indices of rectangular grid to sample numbers within a pixel.
639 * These variables are used by GL_EXT_framebuffer_multisample_blit_scaled
640 * extension implementation. For more details see the comment above
641 * gen6_set_sample_maps() definition.
642 */
643 gen6_set_sample_maps(ctx);
644
645 ctx->Const.MinLineWidth = 1.0;
646 ctx->Const.MinLineWidthAA = 1.0;
647 if (brw->gen >= 6) {
648 ctx->Const.MaxLineWidth = 7.375;
649 ctx->Const.MaxLineWidthAA = 7.375;
650 ctx->Const.LineWidthGranularity = 0.125;
651 } else {
652 ctx->Const.MaxLineWidth = 7.0;
653 ctx->Const.MaxLineWidthAA = 7.0;
654 ctx->Const.LineWidthGranularity = 0.5;
655 }
656
657 /* For non-antialiased lines, we have to round the line width to the
658 * nearest whole number. Make sure that we don't advertise a line
659 * width that, when rounded, will be beyond the actual hardware
660 * maximum.
661 */
662 assert(roundf(ctx->Const.MaxLineWidth) <= ctx->Const.MaxLineWidth);
663
664 ctx->Const.MinPointSize = 1.0;
665 ctx->Const.MinPointSizeAA = 1.0;
666 ctx->Const.MaxPointSize = 255.0;
667 ctx->Const.MaxPointSizeAA = 255.0;
668 ctx->Const.PointSizeGranularity = 1.0;
669
670 if (brw->gen >= 5 || brw->is_g4x)
671 ctx->Const.MaxClipPlanes = 8;
672
673 ctx->Const.GLSLTessLevelsAsInputs = true;
674 ctx->Const.LowerTCSPatchVerticesIn = brw->gen >= 8;
675 ctx->Const.LowerTESPatchVerticesIn = true;
676 ctx->Const.PrimitiveRestartForPatches = true;
677
678 ctx->Const.Program[MESA_SHADER_VERTEX].MaxNativeInstructions = 16 * 1024;
679 ctx->Const.Program[MESA_SHADER_VERTEX].MaxAluInstructions = 0;
680 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTexInstructions = 0;
681 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTexIndirections = 0;
682 ctx->Const.Program[MESA_SHADER_VERTEX].MaxNativeAluInstructions = 0;
683 ctx->Const.Program[MESA_SHADER_VERTEX].MaxNativeTexInstructions = 0;
684 ctx->Const.Program[MESA_SHADER_VERTEX].MaxNativeTexIndirections = 0;
685 ctx->Const.Program[MESA_SHADER_VERTEX].MaxNativeAttribs = 16;
686 ctx->Const.Program[MESA_SHADER_VERTEX].MaxNativeTemps = 256;
687 ctx->Const.Program[MESA_SHADER_VERTEX].MaxNativeAddressRegs = 1;
688 ctx->Const.Program[MESA_SHADER_VERTEX].MaxNativeParameters = 1024;
689 ctx->Const.Program[MESA_SHADER_VERTEX].MaxEnvParams =
690 MIN2(ctx->Const.Program[MESA_SHADER_VERTEX].MaxNativeParameters,
691 ctx->Const.Program[MESA_SHADER_VERTEX].MaxEnvParams);
692
693 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxNativeInstructions = 1024;
694 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxNativeAluInstructions = 1024;
695 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxNativeTexInstructions = 1024;
696 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxNativeTexIndirections = 1024;
697 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxNativeAttribs = 12;
698 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxNativeTemps = 256;
699 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxNativeAddressRegs = 0;
700 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxNativeParameters = 1024;
701 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxEnvParams =
702 MIN2(ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxNativeParameters,
703 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxEnvParams);
704
705 /* Fragment shaders use real, 32-bit twos-complement integers for all
706 * integer types.
707 */
708 ctx->Const.Program[MESA_SHADER_FRAGMENT].LowInt.RangeMin = 31;
709 ctx->Const.Program[MESA_SHADER_FRAGMENT].LowInt.RangeMax = 30;
710 ctx->Const.Program[MESA_SHADER_FRAGMENT].LowInt.Precision = 0;
711 ctx->Const.Program[MESA_SHADER_FRAGMENT].HighInt = ctx->Const.Program[MESA_SHADER_FRAGMENT].LowInt;
712 ctx->Const.Program[MESA_SHADER_FRAGMENT].MediumInt = ctx->Const.Program[MESA_SHADER_FRAGMENT].LowInt;
713
714 ctx->Const.Program[MESA_SHADER_VERTEX].LowInt.RangeMin = 31;
715 ctx->Const.Program[MESA_SHADER_VERTEX].LowInt.RangeMax = 30;
716 ctx->Const.Program[MESA_SHADER_VERTEX].LowInt.Precision = 0;
717 ctx->Const.Program[MESA_SHADER_VERTEX].HighInt = ctx->Const.Program[MESA_SHADER_VERTEX].LowInt;
718 ctx->Const.Program[MESA_SHADER_VERTEX].MediumInt = ctx->Const.Program[MESA_SHADER_VERTEX].LowInt;
719
720 /* Gen6 converts quads to polygon in beginning of 3D pipeline,
721 * but we're not sure how it's actually done for vertex order,
722 * that affect provoking vertex decision. Always use last vertex
723 * convention for quad primitive which works as expected for now.
724 */
725 if (brw->gen >= 6)
726 ctx->Const.QuadsFollowProvokingVertexConvention = false;
727
728 ctx->Const.NativeIntegers = true;
729 ctx->Const.VertexID_is_zero_based = true;
730
731 /* Regarding the CMP instruction, the Ivybridge PRM says:
732 *
733 * "For each enabled channel 0b or 1b is assigned to the appropriate flag
734 * bit and 0/all zeros or all ones (e.g, byte 0xFF, word 0xFFFF, DWord
735 * 0xFFFFFFFF) is assigned to dst."
736 *
737 * but PRMs for earlier generations say
738 *
739 * "In dword format, one GRF may store up to 8 results. When the register
740 * is used later as a vector of Booleans, as only LSB at each channel
741 * contains meaning [sic] data, software should make sure all higher bits
742 * are masked out (e.g. by 'and-ing' an [sic] 0x01 constant)."
743 *
744 * We select the representation of a true boolean uniform to be ~0, and fix
745 * the results of Gen <= 5 CMP instruction's with -(result & 1).
746 */
747 ctx->Const.UniformBooleanTrue = ~0;
748
749 /* From the gen4 PRM, volume 4 page 127:
750 *
751 * "For SURFTYPE_BUFFER non-rendertarget surfaces, this field specifies
752 * the base address of the first element of the surface, computed in
753 * software by adding the surface base address to the byte offset of
754 * the element in the buffer."
755 *
756 * However, unaligned accesses are slower, so enforce buffer alignment.
757 */
758 ctx->Const.UniformBufferOffsetAlignment = 16;
759
760 /* ShaderStorageBufferOffsetAlignment should be a cacheline (64 bytes) so
761 * that we can safely have the CPU and GPU writing the same SSBO on
762 * non-cachecoherent systems (our Atom CPUs). With UBOs, the GPU never
763 * writes, so there's no problem. For an SSBO, the GPU and the CPU can
764 * be updating disjoint regions of the buffer simultaneously and that will
765 * break if the regions overlap the same cacheline.
766 */
767 ctx->Const.ShaderStorageBufferOffsetAlignment = 64;
768 ctx->Const.TextureBufferOffsetAlignment = 16;
769 ctx->Const.MaxTextureBufferSize = 128 * 1024 * 1024;
770
771 if (brw->gen >= 6) {
772 ctx->Const.MaxVarying = 32;
773 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 128;
774 ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxInputComponents = 64;
775 ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxOutputComponents = 128;
776 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents = 128;
777 ctx->Const.Program[MESA_SHADER_TESS_CTRL].MaxInputComponents = 128;
778 ctx->Const.Program[MESA_SHADER_TESS_CTRL].MaxOutputComponents = 128;
779 ctx->Const.Program[MESA_SHADER_TESS_EVAL].MaxInputComponents = 128;
780 ctx->Const.Program[MESA_SHADER_TESS_EVAL].MaxOutputComponents = 128;
781 }
782
783 /* We want the GLSL compiler to emit code that uses condition codes */
784 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
785 ctx->Const.ShaderCompilerOptions[i] =
786 brw->screen->compiler->glsl_compiler_options[i];
787 }
788
789 if (brw->gen >= 7) {
790 ctx->Const.MaxViewportWidth = 32768;
791 ctx->Const.MaxViewportHeight = 32768;
792 }
793
794 /* ARB_viewport_array, OES_viewport_array */
795 if (brw->gen >= 6) {
796 ctx->Const.MaxViewports = GEN6_NUM_VIEWPORTS;
797 ctx->Const.ViewportSubpixelBits = 0;
798
799 /* Cast to float before negating because MaxViewportWidth is unsigned.
800 */
801 ctx->Const.ViewportBounds.Min = -(float)ctx->Const.MaxViewportWidth;
802 ctx->Const.ViewportBounds.Max = ctx->Const.MaxViewportWidth;
803 }
804
805 /* ARB_gpu_shader5 */
806 if (brw->gen >= 7)
807 ctx->Const.MaxVertexStreams = MIN2(4, MAX_VERTEX_STREAMS);
808
809 /* ARB_framebuffer_no_attachments */
810 ctx->Const.MaxFramebufferWidth = 16384;
811 ctx->Const.MaxFramebufferHeight = 16384;
812 ctx->Const.MaxFramebufferLayers = ctx->Const.MaxArrayTextureLayers;
813 ctx->Const.MaxFramebufferSamples = max_samples;
814
815 /* OES_primitive_bounding_box */
816 ctx->Const.NoPrimitiveBoundingBoxOutput = true;
817 }
818
819 static void
820 brw_initialize_cs_context_constants(struct brw_context *brw)
821 {
822 struct gl_context *ctx = &brw->ctx;
823 const struct intel_screen *screen = brw->screen;
824 struct gen_device_info *devinfo = &brw->screen->devinfo;
825
826 /* FINISHME: Do this for all platforms that the kernel supports */
827 if (brw->is_cherryview &&
828 screen->subslice_total > 0 && screen->eu_total > 0) {
829 /* Logical CS threads = EUs per subslice * 7 threads per EU */
830 uint32_t max_cs_threads = screen->eu_total / screen->subslice_total * 7;
831
832 /* Fuse configurations may give more threads than expected, never less. */
833 if (max_cs_threads > devinfo->max_cs_threads)
834 devinfo->max_cs_threads = max_cs_threads;
835 }
836
837 /* Maximum number of scalar compute shader invocations that can be run in
838 * parallel in the same subslice assuming SIMD32 dispatch.
839 *
840 * We don't advertise more than 64 threads, because we are limited to 64 by
841 * our usage of thread_width_max in the gpgpu walker command. This only
842 * currently impacts Haswell, which otherwise might be able to advertise 70
843 * threads. With SIMD32 and 64 threads, Haswell still provides twice the
844 * required the number of invocation needed for ARB_compute_shader.
845 */
846 const unsigned max_threads = MIN2(64, devinfo->max_cs_threads);
847 const uint32_t max_invocations = 32 * max_threads;
848 ctx->Const.MaxComputeWorkGroupSize[0] = max_invocations;
849 ctx->Const.MaxComputeWorkGroupSize[1] = max_invocations;
850 ctx->Const.MaxComputeWorkGroupSize[2] = max_invocations;
851 ctx->Const.MaxComputeWorkGroupInvocations = max_invocations;
852 ctx->Const.MaxComputeSharedMemorySize = 64 * 1024;
853 }
854
855 /**
856 * Process driconf (drirc) options, setting appropriate context flags.
857 *
858 * intelInitExtensions still pokes at optionCache directly, in order to
859 * avoid advertising various extensions. No flags are set, so it makes
860 * sense to continue doing that there.
861 */
862 static void
863 brw_process_driconf_options(struct brw_context *brw)
864 {
865 struct gl_context *ctx = &brw->ctx;
866
867 driOptionCache *options = &brw->optionCache;
868 driParseConfigFiles(options, &brw->screen->optionCache,
869 brw->driContext->driScreenPriv->myNum, "i965");
870
871 int bo_reuse_mode = driQueryOptioni(options, "bo_reuse");
872 switch (bo_reuse_mode) {
873 case DRI_CONF_BO_REUSE_DISABLED:
874 break;
875 case DRI_CONF_BO_REUSE_ALL:
876 intel_bufmgr_gem_enable_reuse(brw->bufmgr);
877 break;
878 }
879
880 if (!driQueryOptionb(options, "hiz")) {
881 brw->has_hiz = false;
882 /* On gen6, you can only do separate stencil with HIZ. */
883 if (brw->gen == 6)
884 brw->has_separate_stencil = false;
885 }
886
887 if (driQueryOptionb(options, "always_flush_batch")) {
888 fprintf(stderr, "flushing batchbuffer before/after each draw call\n");
889 brw->always_flush_batch = true;
890 }
891
892 if (driQueryOptionb(options, "always_flush_cache")) {
893 fprintf(stderr, "flushing GPU caches before/after each draw call\n");
894 brw->always_flush_cache = true;
895 }
896
897 if (driQueryOptionb(options, "disable_throttling")) {
898 fprintf(stderr, "disabling flush throttling\n");
899 brw->disable_throttling = true;
900 }
901
902 brw->precompile = driQueryOptionb(&brw->optionCache, "shader_precompile");
903
904 if (driQueryOptionb(&brw->optionCache, "precise_trig"))
905 brw->screen->compiler->precise_trig = true;
906
907 ctx->Const.ForceGLSLExtensionsWarn =
908 driQueryOptionb(options, "force_glsl_extensions_warn");
909
910 ctx->Const.ForceGLSLVersion =
911 driQueryOptioni(options, "force_glsl_version");
912
913 ctx->Const.DisableGLSLLineContinuations =
914 driQueryOptionb(options, "disable_glsl_line_continuations");
915
916 ctx->Const.AllowGLSLExtensionDirectiveMidShader =
917 driQueryOptionb(options, "allow_glsl_extension_directive_midshader");
918
919 ctx->Const.GLSLZeroInit = driQueryOptionb(options, "glsl_zero_init");
920
921 brw->dual_color_blend_by_location =
922 driQueryOptionb(options, "dual_color_blend_by_location");
923 }
924
925 GLboolean
926 brwCreateContext(gl_api api,
927 const struct gl_config *mesaVis,
928 __DRIcontext *driContextPriv,
929 unsigned major_version,
930 unsigned minor_version,
931 uint32_t flags,
932 bool notify_reset,
933 unsigned *dri_ctx_error,
934 void *sharedContextPrivate)
935 {
936 struct gl_context *shareCtx = (struct gl_context *) sharedContextPrivate;
937 struct intel_screen *screen = driContextPriv->driScreenPriv->driverPrivate;
938 const struct gen_device_info *devinfo = &screen->devinfo;
939 struct dd_function_table functions;
940
941 /* Only allow the __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS flag if the kernel
942 * provides us with context reset notifications.
943 */
944 uint32_t allowed_flags = __DRI_CTX_FLAG_DEBUG
945 | __DRI_CTX_FLAG_FORWARD_COMPATIBLE;
946
947 if (screen->has_context_reset_notification)
948 allowed_flags |= __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS;
949
950 if (flags & ~allowed_flags) {
951 *dri_ctx_error = __DRI_CTX_ERROR_UNKNOWN_FLAG;
952 return false;
953 }
954
955 struct brw_context *brw = rzalloc(NULL, struct brw_context);
956 if (!brw) {
957 fprintf(stderr, "%s: failed to alloc context\n", __func__);
958 *dri_ctx_error = __DRI_CTX_ERROR_NO_MEMORY;
959 return false;
960 }
961
962 driContextPriv->driverPrivate = brw;
963 brw->driContext = driContextPriv;
964 brw->screen = screen;
965 brw->bufmgr = screen->bufmgr;
966
967 brw->gen = devinfo->gen;
968 brw->gt = devinfo->gt;
969 brw->is_g4x = devinfo->is_g4x;
970 brw->is_baytrail = devinfo->is_baytrail;
971 brw->is_haswell = devinfo->is_haswell;
972 brw->is_cherryview = devinfo->is_cherryview;
973 brw->is_broxton = devinfo->is_broxton;
974 brw->has_llc = devinfo->has_llc;
975 brw->has_hiz = devinfo->has_hiz_and_separate_stencil;
976 brw->has_separate_stencil = devinfo->has_hiz_and_separate_stencil;
977 brw->has_pln = devinfo->has_pln;
978 brw->has_compr4 = devinfo->has_compr4;
979 brw->has_surface_tile_offset = devinfo->has_surface_tile_offset;
980 brw->has_negative_rhw_bug = devinfo->has_negative_rhw_bug;
981 brw->needs_unlit_centroid_workaround =
982 devinfo->needs_unlit_centroid_workaround;
983
984 brw->must_use_separate_stencil = devinfo->must_use_separate_stencil;
985 brw->has_swizzling = screen->hw_has_swizzling;
986
987 isl_device_init(&brw->isl_dev, devinfo, screen->hw_has_swizzling);
988
989 brw->vs.base.stage = MESA_SHADER_VERTEX;
990 brw->tcs.base.stage = MESA_SHADER_TESS_CTRL;
991 brw->tes.base.stage = MESA_SHADER_TESS_EVAL;
992 brw->gs.base.stage = MESA_SHADER_GEOMETRY;
993 brw->wm.base.stage = MESA_SHADER_FRAGMENT;
994 if (brw->gen >= 8) {
995 gen8_init_vtable_surface_functions(brw);
996 brw->vtbl.emit_depth_stencil_hiz = gen8_emit_depth_stencil_hiz;
997 } else if (brw->gen >= 7) {
998 gen7_init_vtable_surface_functions(brw);
999 brw->vtbl.emit_depth_stencil_hiz = gen7_emit_depth_stencil_hiz;
1000 } else if (brw->gen >= 6) {
1001 gen6_init_vtable_surface_functions(brw);
1002 brw->vtbl.emit_depth_stencil_hiz = gen6_emit_depth_stencil_hiz;
1003 } else {
1004 gen4_init_vtable_surface_functions(brw);
1005 brw->vtbl.emit_depth_stencil_hiz = brw_emit_depth_stencil_hiz;
1006 }
1007
1008 brw_init_driver_functions(brw, &functions);
1009
1010 if (notify_reset)
1011 functions.GetGraphicsResetStatus = brw_get_graphics_reset_status;
1012
1013 struct gl_context *ctx = &brw->ctx;
1014
1015 if (!_mesa_initialize_context(ctx, api, mesaVis, shareCtx, &functions)) {
1016 *dri_ctx_error = __DRI_CTX_ERROR_NO_MEMORY;
1017 fprintf(stderr, "%s: failed to init mesa context\n", __func__);
1018 intelDestroyContext(driContextPriv);
1019 return false;
1020 }
1021
1022 driContextSetFlags(ctx, flags);
1023
1024 /* Initialize the software rasterizer and helper modules.
1025 *
1026 * As of GL 3.1 core, the gen4+ driver doesn't need the swrast context for
1027 * software fallbacks (which we have to support on legacy GL to do weird
1028 * glDrawPixels(), glBitmap(), and other functions).
1029 */
1030 if (api != API_OPENGL_CORE && api != API_OPENGLES2) {
1031 _swrast_CreateContext(ctx);
1032 }
1033
1034 _vbo_CreateContext(ctx);
1035 if (ctx->swrast_context) {
1036 _tnl_CreateContext(ctx);
1037 TNL_CONTEXT(ctx)->Driver.RunPipeline = _tnl_run_pipeline;
1038 _swsetup_CreateContext(ctx);
1039
1040 /* Configure swrast to match hardware characteristics: */
1041 _swrast_allow_pixel_fog(ctx, false);
1042 _swrast_allow_vertex_fog(ctx, true);
1043 }
1044
1045 _mesa_meta_init(ctx);
1046
1047 brw_process_driconf_options(brw);
1048
1049 if (INTEL_DEBUG & DEBUG_PERF)
1050 brw->perf_debug = true;
1051
1052 brw_initialize_cs_context_constants(brw);
1053 brw_initialize_context_constants(brw);
1054
1055 ctx->Const.ResetStrategy = notify_reset
1056 ? GL_LOSE_CONTEXT_ON_RESET_ARB : GL_NO_RESET_NOTIFICATION_ARB;
1057
1058 /* Reinitialize the context point state. It depends on ctx->Const values. */
1059 _mesa_init_point(ctx);
1060
1061 intel_fbo_init(brw);
1062
1063 intel_batchbuffer_init(&brw->batch, brw->bufmgr, brw->has_llc);
1064
1065 if (brw->gen >= 6) {
1066 /* Create a new hardware context. Using a hardware context means that
1067 * our GPU state will be saved/restored on context switch, allowing us
1068 * to assume that the GPU is in the same state we left it in.
1069 *
1070 * This is required for transform feedback buffer offsets, query objects,
1071 * and also allows us to reduce how much state we have to emit.
1072 */
1073 brw->hw_ctx = drm_intel_gem_context_create(brw->bufmgr);
1074
1075 if (!brw->hw_ctx) {
1076 fprintf(stderr, "Gen6+ requires Kernel 3.6 or later.\n");
1077 intelDestroyContext(driContextPriv);
1078 return false;
1079 }
1080 }
1081
1082 if (brw_init_pipe_control(brw, devinfo)) {
1083 *dri_ctx_error = __DRI_CTX_ERROR_NO_MEMORY;
1084 intelDestroyContext(driContextPriv);
1085 return false;
1086 }
1087
1088 brw_init_state(brw);
1089
1090 intelInitExtensions(ctx);
1091
1092 brw_init_surface_formats(brw);
1093
1094 if (brw->gen >= 6)
1095 brw_blorp_init(brw);
1096
1097 brw->urb.size = devinfo->urb.size;
1098
1099 if (brw->gen == 6)
1100 brw->urb.gs_present = false;
1101
1102 brw->prim_restart.in_progress = false;
1103 brw->prim_restart.enable_cut_index = false;
1104 brw->gs.enabled = false;
1105 brw->sf.viewport_transform_enable = true;
1106 brw->clip.viewport_count = 1;
1107
1108 brw->predicate.state = BRW_PREDICATE_STATE_RENDER;
1109
1110 brw->max_gtt_map_object_size = screen->max_gtt_map_object_size;
1111
1112 brw->use_resource_streamer = screen->has_resource_streamer &&
1113 (env_var_as_boolean("INTEL_USE_HW_BT", false) ||
1114 env_var_as_boolean("INTEL_USE_GATHER", false));
1115
1116 ctx->VertexProgram._MaintainTnlProgram = true;
1117 ctx->FragmentProgram._MaintainTexEnvProgram = true;
1118
1119 brw_draw_init( brw );
1120
1121 if ((flags & __DRI_CTX_FLAG_DEBUG) != 0) {
1122 /* Turn on some extra GL_ARB_debug_output generation. */
1123 brw->perf_debug = true;
1124 }
1125
1126 if ((flags & __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS) != 0) {
1127 ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_ROBUST_ACCESS_BIT_ARB;
1128 ctx->Const.RobustAccess = GL_TRUE;
1129 }
1130
1131 if (INTEL_DEBUG & DEBUG_SHADER_TIME)
1132 brw_init_shader_time(brw);
1133
1134 _mesa_compute_version(ctx);
1135
1136 _mesa_initialize_dispatch_tables(ctx);
1137 _mesa_initialize_vbo_vtxfmt(ctx);
1138
1139 vbo_use_buffer_objects(ctx);
1140 vbo_always_unmap_buffers(ctx);
1141
1142 return true;
1143 }
1144
1145 void
1146 intelDestroyContext(__DRIcontext * driContextPriv)
1147 {
1148 struct brw_context *brw =
1149 (struct brw_context *) driContextPriv->driverPrivate;
1150 struct gl_context *ctx = &brw->ctx;
1151
1152 /* Dump a final BMP in case the application doesn't call SwapBuffers */
1153 if (INTEL_DEBUG & DEBUG_AUB) {
1154 intel_batchbuffer_flush(brw);
1155 aub_dump_bmp(&brw->ctx);
1156 }
1157
1158 _mesa_meta_free(&brw->ctx);
1159
1160 if (INTEL_DEBUG & DEBUG_SHADER_TIME) {
1161 /* Force a report. */
1162 brw->shader_time.report_time = 0;
1163
1164 brw_collect_and_report_shader_time(brw);
1165 brw_destroy_shader_time(brw);
1166 }
1167
1168 if (brw->gen >= 6)
1169 blorp_finish(&brw->blorp);
1170
1171 brw_destroy_state(brw);
1172 brw_draw_destroy(brw);
1173
1174 drm_intel_bo_unreference(brw->curbe.curbe_bo);
1175 if (brw->vs.base.scratch_bo)
1176 drm_intel_bo_unreference(brw->vs.base.scratch_bo);
1177 if (brw->tcs.base.scratch_bo)
1178 drm_intel_bo_unreference(brw->tcs.base.scratch_bo);
1179 if (brw->tes.base.scratch_bo)
1180 drm_intel_bo_unreference(brw->tes.base.scratch_bo);
1181 if (brw->gs.base.scratch_bo)
1182 drm_intel_bo_unreference(brw->gs.base.scratch_bo);
1183 if (brw->wm.base.scratch_bo)
1184 drm_intel_bo_unreference(brw->wm.base.scratch_bo);
1185
1186 gen7_reset_hw_bt_pool_offsets(brw);
1187 drm_intel_bo_unreference(brw->hw_bt_pool.bo);
1188 brw->hw_bt_pool.bo = NULL;
1189
1190 drm_intel_gem_context_destroy(brw->hw_ctx);
1191
1192 if (ctx->swrast_context) {
1193 _swsetup_DestroyContext(&brw->ctx);
1194 _tnl_DestroyContext(&brw->ctx);
1195 }
1196 _vbo_DestroyContext(&brw->ctx);
1197
1198 if (ctx->swrast_context)
1199 _swrast_DestroyContext(&brw->ctx);
1200
1201 brw_fini_pipe_control(brw);
1202 intel_batchbuffer_free(&brw->batch);
1203
1204 drm_intel_bo_unreference(brw->throttle_batch[1]);
1205 drm_intel_bo_unreference(brw->throttle_batch[0]);
1206 brw->throttle_batch[1] = NULL;
1207 brw->throttle_batch[0] = NULL;
1208
1209 driDestroyOptionCache(&brw->optionCache);
1210
1211 /* free the Mesa context */
1212 _mesa_free_context_data(&brw->ctx);
1213
1214 ralloc_free(brw);
1215 driContextPriv->driverPrivate = NULL;
1216 }
1217
1218 GLboolean
1219 intelUnbindContext(__DRIcontext * driContextPriv)
1220 {
1221 /* Unset current context and dispath table */
1222 _mesa_make_current(NULL, NULL, NULL);
1223
1224 return true;
1225 }
1226
1227 /**
1228 * Fixes up the context for GLES23 with our default-to-sRGB-capable behavior
1229 * on window system framebuffers.
1230 *
1231 * Desktop GL is fairly reasonable in its handling of sRGB: You can ask if
1232 * your renderbuffer can do sRGB encode, and you can flip a switch that does
1233 * sRGB encode if the renderbuffer can handle it. You can ask specifically
1234 * for a visual where you're guaranteed to be capable, but it turns out that
1235 * everyone just makes all their ARGB8888 visuals capable and doesn't offer
1236 * incapable ones, because there's no difference between the two in resources
1237 * used. Applications thus get built that accidentally rely on the default
1238 * visual choice being sRGB, so we make ours sRGB capable. Everything sounds
1239 * great...
1240 *
1241 * But for GLES2/3, they decided that it was silly to not turn on sRGB encode
1242 * for sRGB renderbuffers you made with the GL_EXT_texture_sRGB equivalent.
1243 * So they removed the enable knob and made it "if the renderbuffer is sRGB
1244 * capable, do sRGB encode". Then, for your window system renderbuffers, you
1245 * can ask for sRGB visuals and get sRGB encode, or not ask for sRGB visuals
1246 * and get no sRGB encode (assuming that both kinds of visual are available).
1247 * Thus our choice to support sRGB by default on our visuals for desktop would
1248 * result in broken rendering of GLES apps that aren't expecting sRGB encode.
1249 *
1250 * Unfortunately, renderbuffer setup happens before a context is created. So
1251 * in intel_screen.c we always set up sRGB, and here, if you're a GLES2/3
1252 * context (without an sRGB visual, though we don't have sRGB visuals exposed
1253 * yet), we go turn that back off before anyone finds out.
1254 */
1255 static void
1256 intel_gles3_srgb_workaround(struct brw_context *brw,
1257 struct gl_framebuffer *fb)
1258 {
1259 struct gl_context *ctx = &brw->ctx;
1260
1261 if (_mesa_is_desktop_gl(ctx) || !fb->Visual.sRGBCapable)
1262 return;
1263
1264 /* Some day when we support the sRGB capable bit on visuals available for
1265 * GLES, we'll need to respect that and not disable things here.
1266 */
1267 fb->Visual.sRGBCapable = false;
1268 for (int i = 0; i < BUFFER_COUNT; i++) {
1269 struct gl_renderbuffer *rb = fb->Attachment[i].Renderbuffer;
1270 if (rb)
1271 rb->Format = _mesa_get_srgb_format_linear(rb->Format);
1272 }
1273 }
1274
1275 GLboolean
1276 intelMakeCurrent(__DRIcontext * driContextPriv,
1277 __DRIdrawable * driDrawPriv,
1278 __DRIdrawable * driReadPriv)
1279 {
1280 struct brw_context *brw;
1281 GET_CURRENT_CONTEXT(curCtx);
1282
1283 if (driContextPriv)
1284 brw = (struct brw_context *) driContextPriv->driverPrivate;
1285 else
1286 brw = NULL;
1287
1288 /* According to the glXMakeCurrent() man page: "Pending commands to
1289 * the previous context, if any, are flushed before it is released."
1290 * But only flush if we're actually changing contexts.
1291 */
1292 if (brw_context(curCtx) && brw_context(curCtx) != brw) {
1293 _mesa_flush(curCtx);
1294 }
1295
1296 if (driContextPriv) {
1297 struct gl_context *ctx = &brw->ctx;
1298 struct gl_framebuffer *fb, *readFb;
1299
1300 if (driDrawPriv == NULL) {
1301 fb = _mesa_get_incomplete_framebuffer();
1302 } else {
1303 fb = driDrawPriv->driverPrivate;
1304 driContextPriv->dri2.draw_stamp = driDrawPriv->dri2.stamp - 1;
1305 }
1306
1307 if (driReadPriv == NULL) {
1308 readFb = _mesa_get_incomplete_framebuffer();
1309 } else {
1310 readFb = driReadPriv->driverPrivate;
1311 driContextPriv->dri2.read_stamp = driReadPriv->dri2.stamp - 1;
1312 }
1313
1314 /* The sRGB workaround changes the renderbuffer's format. We must change
1315 * the format before the renderbuffer's miptree get's allocated, otherwise
1316 * the formats of the renderbuffer and its miptree will differ.
1317 */
1318 intel_gles3_srgb_workaround(brw, fb);
1319 intel_gles3_srgb_workaround(brw, readFb);
1320
1321 /* If the context viewport hasn't been initialized, force a call out to
1322 * the loader to get buffers so we have a drawable size for the initial
1323 * viewport. */
1324 if (!brw->ctx.ViewportInitialized)
1325 intel_prepare_render(brw);
1326
1327 _mesa_make_current(ctx, fb, readFb);
1328 } else {
1329 _mesa_make_current(NULL, NULL, NULL);
1330 }
1331
1332 return true;
1333 }
1334
1335 void
1336 intel_resolve_for_dri2_flush(struct brw_context *brw,
1337 __DRIdrawable *drawable)
1338 {
1339 if (brw->gen < 6) {
1340 /* MSAA and fast color clear are not supported, so don't waste time
1341 * checking whether a resolve is needed.
1342 */
1343 return;
1344 }
1345
1346 struct gl_framebuffer *fb = drawable->driverPrivate;
1347 struct intel_renderbuffer *rb;
1348
1349 /* Usually, only the back buffer will need to be downsampled. However,
1350 * the front buffer will also need it if the user has rendered into it.
1351 */
1352 static const gl_buffer_index buffers[2] = {
1353 BUFFER_BACK_LEFT,
1354 BUFFER_FRONT_LEFT,
1355 };
1356
1357 for (int i = 0; i < 2; ++i) {
1358 rb = intel_get_renderbuffer(fb, buffers[i]);
1359 if (rb == NULL || rb->mt == NULL)
1360 continue;
1361 if (rb->mt->num_samples <= 1) {
1362 assert(rb->mt_layer == 0 && rb->mt_level == 0 &&
1363 rb->layer_count == 1);
1364 intel_miptree_resolve_color(brw, rb->mt, 0, 0, 1, 0);
1365 } else {
1366 intel_renderbuffer_downsample(brw, rb);
1367 }
1368 }
1369 }
1370
1371 static unsigned
1372 intel_bits_per_pixel(const struct intel_renderbuffer *rb)
1373 {
1374 return _mesa_get_format_bytes(intel_rb_format(rb)) * 8;
1375 }
1376
1377 static void
1378 intel_query_dri2_buffers(struct brw_context *brw,
1379 __DRIdrawable *drawable,
1380 __DRIbuffer **buffers,
1381 int *count);
1382
1383 static void
1384 intel_process_dri2_buffer(struct brw_context *brw,
1385 __DRIdrawable *drawable,
1386 __DRIbuffer *buffer,
1387 struct intel_renderbuffer *rb,
1388 const char *buffer_name);
1389
1390 static void
1391 intel_update_image_buffers(struct brw_context *brw, __DRIdrawable *drawable);
1392
1393 static void
1394 intel_update_dri2_buffers(struct brw_context *brw, __DRIdrawable *drawable)
1395 {
1396 struct gl_framebuffer *fb = drawable->driverPrivate;
1397 struct intel_renderbuffer *rb;
1398 __DRIbuffer *buffers = NULL;
1399 int i, count;
1400 const char *region_name;
1401
1402 /* Set this up front, so that in case our buffers get invalidated
1403 * while we're getting new buffers, we don't clobber the stamp and
1404 * thus ignore the invalidate. */
1405 drawable->lastStamp = drawable->dri2.stamp;
1406
1407 if (unlikely(INTEL_DEBUG & DEBUG_DRI))
1408 fprintf(stderr, "enter %s, drawable %p\n", __func__, drawable);
1409
1410 intel_query_dri2_buffers(brw, drawable, &buffers, &count);
1411
1412 if (buffers == NULL)
1413 return;
1414
1415 for (i = 0; i < count; i++) {
1416 switch (buffers[i].attachment) {
1417 case __DRI_BUFFER_FRONT_LEFT:
1418 rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
1419 region_name = "dri2 front buffer";
1420 break;
1421
1422 case __DRI_BUFFER_FAKE_FRONT_LEFT:
1423 rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
1424 region_name = "dri2 fake front buffer";
1425 break;
1426
1427 case __DRI_BUFFER_BACK_LEFT:
1428 rb = intel_get_renderbuffer(fb, BUFFER_BACK_LEFT);
1429 region_name = "dri2 back buffer";
1430 break;
1431
1432 case __DRI_BUFFER_DEPTH:
1433 case __DRI_BUFFER_HIZ:
1434 case __DRI_BUFFER_DEPTH_STENCIL:
1435 case __DRI_BUFFER_STENCIL:
1436 case __DRI_BUFFER_ACCUM:
1437 default:
1438 fprintf(stderr,
1439 "unhandled buffer attach event, attachment type %d\n",
1440 buffers[i].attachment);
1441 return;
1442 }
1443
1444 intel_process_dri2_buffer(brw, drawable, &buffers[i], rb, region_name);
1445 }
1446
1447 }
1448
1449 void
1450 intel_update_renderbuffers(__DRIcontext *context, __DRIdrawable *drawable)
1451 {
1452 struct brw_context *brw = context->driverPrivate;
1453 __DRIscreen *dri_screen = brw->screen->driScrnPriv;
1454
1455 /* Set this up front, so that in case our buffers get invalidated
1456 * while we're getting new buffers, we don't clobber the stamp and
1457 * thus ignore the invalidate. */
1458 drawable->lastStamp = drawable->dri2.stamp;
1459
1460 if (unlikely(INTEL_DEBUG & DEBUG_DRI))
1461 fprintf(stderr, "enter %s, drawable %p\n", __func__, drawable);
1462
1463 if (dri_screen->image.loader)
1464 intel_update_image_buffers(brw, drawable);
1465 else
1466 intel_update_dri2_buffers(brw, drawable);
1467
1468 driUpdateFramebufferSize(&brw->ctx, drawable);
1469 }
1470
1471 /**
1472 * intel_prepare_render should be called anywhere that curent read/drawbuffer
1473 * state is required.
1474 */
1475 void
1476 intel_prepare_render(struct brw_context *brw)
1477 {
1478 struct gl_context *ctx = &brw->ctx;
1479 __DRIcontext *driContext = brw->driContext;
1480 __DRIdrawable *drawable;
1481
1482 drawable = driContext->driDrawablePriv;
1483 if (drawable && drawable->dri2.stamp != driContext->dri2.draw_stamp) {
1484 if (drawable->lastStamp != drawable->dri2.stamp)
1485 intel_update_renderbuffers(driContext, drawable);
1486 driContext->dri2.draw_stamp = drawable->dri2.stamp;
1487 }
1488
1489 drawable = driContext->driReadablePriv;
1490 if (drawable && drawable->dri2.stamp != driContext->dri2.read_stamp) {
1491 if (drawable->lastStamp != drawable->dri2.stamp)
1492 intel_update_renderbuffers(driContext, drawable);
1493 driContext->dri2.read_stamp = drawable->dri2.stamp;
1494 }
1495
1496 /* If we're currently rendering to the front buffer, the rendering
1497 * that will happen next will probably dirty the front buffer. So
1498 * mark it as dirty here.
1499 */
1500 if (_mesa_is_front_buffer_drawing(ctx->DrawBuffer))
1501 brw->front_buffer_dirty = true;
1502 }
1503
1504 /**
1505 * \brief Query DRI2 to obtain a DRIdrawable's buffers.
1506 *
1507 * To determine which DRI buffers to request, examine the renderbuffers
1508 * attached to the drawable's framebuffer. Then request the buffers with
1509 * DRI2GetBuffers() or DRI2GetBuffersWithFormat().
1510 *
1511 * This is called from intel_update_renderbuffers().
1512 *
1513 * \param drawable Drawable whose buffers are queried.
1514 * \param buffers [out] List of buffers returned by DRI2 query.
1515 * \param buffer_count [out] Number of buffers returned.
1516 *
1517 * \see intel_update_renderbuffers()
1518 * \see DRI2GetBuffers()
1519 * \see DRI2GetBuffersWithFormat()
1520 */
1521 static void
1522 intel_query_dri2_buffers(struct brw_context *brw,
1523 __DRIdrawable *drawable,
1524 __DRIbuffer **buffers,
1525 int *buffer_count)
1526 {
1527 __DRIscreen *dri_screen = brw->screen->driScrnPriv;
1528 struct gl_framebuffer *fb = drawable->driverPrivate;
1529 int i = 0;
1530 unsigned attachments[8];
1531
1532 struct intel_renderbuffer *front_rb;
1533 struct intel_renderbuffer *back_rb;
1534
1535 front_rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
1536 back_rb = intel_get_renderbuffer(fb, BUFFER_BACK_LEFT);
1537
1538 memset(attachments, 0, sizeof(attachments));
1539 if ((_mesa_is_front_buffer_drawing(fb) ||
1540 _mesa_is_front_buffer_reading(fb) ||
1541 !back_rb) && front_rb) {
1542 /* If a fake front buffer is in use, then querying for
1543 * __DRI_BUFFER_FRONT_LEFT will cause the server to copy the image from
1544 * the real front buffer to the fake front buffer. So before doing the
1545 * query, we need to make sure all the pending drawing has landed in the
1546 * real front buffer.
1547 */
1548 intel_batchbuffer_flush(brw);
1549 intel_flush_front(&brw->ctx);
1550
1551 attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
1552 attachments[i++] = intel_bits_per_pixel(front_rb);
1553 } else if (front_rb && brw->front_buffer_dirty) {
1554 /* We have pending front buffer rendering, but we aren't querying for a
1555 * front buffer. If the front buffer we have is a fake front buffer,
1556 * the X server is going to throw it away when it processes the query.
1557 * So before doing the query, make sure all the pending drawing has
1558 * landed in the real front buffer.
1559 */
1560 intel_batchbuffer_flush(brw);
1561 intel_flush_front(&brw->ctx);
1562 }
1563
1564 if (back_rb) {
1565 attachments[i++] = __DRI_BUFFER_BACK_LEFT;
1566 attachments[i++] = intel_bits_per_pixel(back_rb);
1567 }
1568
1569 assert(i <= ARRAY_SIZE(attachments));
1570
1571 *buffers =
1572 dri_screen->dri2.loader->getBuffersWithFormat(drawable,
1573 &drawable->w,
1574 &drawable->h,
1575 attachments, i / 2,
1576 buffer_count,
1577 drawable->loaderPrivate);
1578 }
1579
1580 /**
1581 * \brief Assign a DRI buffer's DRM region to a renderbuffer.
1582 *
1583 * This is called from intel_update_renderbuffers().
1584 *
1585 * \par Note:
1586 * DRI buffers whose attachment point is DRI2BufferStencil or
1587 * DRI2BufferDepthStencil are handled as special cases.
1588 *
1589 * \param buffer_name is a human readable name, such as "dri2 front buffer",
1590 * that is passed to drm_intel_bo_gem_create_from_name().
1591 *
1592 * \see intel_update_renderbuffers()
1593 */
1594 static void
1595 intel_process_dri2_buffer(struct brw_context *brw,
1596 __DRIdrawable *drawable,
1597 __DRIbuffer *buffer,
1598 struct intel_renderbuffer *rb,
1599 const char *buffer_name)
1600 {
1601 struct gl_framebuffer *fb = drawable->driverPrivate;
1602 drm_intel_bo *bo;
1603
1604 if (!rb)
1605 return;
1606
1607 unsigned num_samples = rb->Base.Base.NumSamples;
1608
1609 /* We try to avoid closing and reopening the same BO name, because the first
1610 * use of a mapping of the buffer involves a bunch of page faulting which is
1611 * moderately expensive.
1612 */
1613 struct intel_mipmap_tree *last_mt;
1614 if (num_samples == 0)
1615 last_mt = rb->mt;
1616 else
1617 last_mt = rb->singlesample_mt;
1618
1619 uint32_t old_name = 0;
1620 if (last_mt) {
1621 /* The bo already has a name because the miptree was created by a
1622 * previous call to intel_process_dri2_buffer(). If a bo already has a
1623 * name, then drm_intel_bo_flink() is a low-cost getter. It does not
1624 * create a new name.
1625 */
1626 drm_intel_bo_flink(last_mt->bo, &old_name);
1627 }
1628
1629 if (old_name == buffer->name)
1630 return;
1631
1632 if (unlikely(INTEL_DEBUG & DEBUG_DRI)) {
1633 fprintf(stderr,
1634 "attaching buffer %d, at %d, cpp %d, pitch %d\n",
1635 buffer->name, buffer->attachment,
1636 buffer->cpp, buffer->pitch);
1637 }
1638
1639 bo = drm_intel_bo_gem_create_from_name(brw->bufmgr, buffer_name,
1640 buffer->name);
1641 if (!bo) {
1642 fprintf(stderr,
1643 "Failed to open BO for returned DRI2 buffer "
1644 "(%dx%d, %s, named %d).\n"
1645 "This is likely a bug in the X Server that will lead to a "
1646 "crash soon.\n",
1647 drawable->w, drawable->h, buffer_name, buffer->name);
1648 return;
1649 }
1650
1651 intel_update_winsys_renderbuffer_miptree(brw, rb, bo,
1652 drawable->w, drawable->h,
1653 buffer->pitch);
1654
1655 if (_mesa_is_front_buffer_drawing(fb) &&
1656 (buffer->attachment == __DRI_BUFFER_FRONT_LEFT ||
1657 buffer->attachment == __DRI_BUFFER_FAKE_FRONT_LEFT) &&
1658 rb->Base.Base.NumSamples > 1) {
1659 intel_renderbuffer_upsample(brw, rb);
1660 }
1661
1662 assert(rb->mt);
1663
1664 drm_intel_bo_unreference(bo);
1665 }
1666
1667 /**
1668 * \brief Query DRI image loader to obtain a DRIdrawable's buffers.
1669 *
1670 * To determine which DRI buffers to request, examine the renderbuffers
1671 * attached to the drawable's framebuffer. Then request the buffers from
1672 * the image loader
1673 *
1674 * This is called from intel_update_renderbuffers().
1675 *
1676 * \param drawable Drawable whose buffers are queried.
1677 * \param buffers [out] List of buffers returned by DRI2 query.
1678 * \param buffer_count [out] Number of buffers returned.
1679 *
1680 * \see intel_update_renderbuffers()
1681 */
1682
1683 static void
1684 intel_update_image_buffer(struct brw_context *intel,
1685 __DRIdrawable *drawable,
1686 struct intel_renderbuffer *rb,
1687 __DRIimage *buffer,
1688 enum __DRIimageBufferMask buffer_type)
1689 {
1690 struct gl_framebuffer *fb = drawable->driverPrivate;
1691
1692 if (!rb || !buffer->bo)
1693 return;
1694
1695 unsigned num_samples = rb->Base.Base.NumSamples;
1696
1697 /* Check and see if we're already bound to the right
1698 * buffer object
1699 */
1700 struct intel_mipmap_tree *last_mt;
1701 if (num_samples == 0)
1702 last_mt = rb->mt;
1703 else
1704 last_mt = rb->singlesample_mt;
1705
1706 if (last_mt && last_mt->bo == buffer->bo)
1707 return;
1708
1709 intel_update_winsys_renderbuffer_miptree(intel, rb, buffer->bo,
1710 buffer->width, buffer->height,
1711 buffer->pitch);
1712
1713 if (_mesa_is_front_buffer_drawing(fb) &&
1714 buffer_type == __DRI_IMAGE_BUFFER_FRONT &&
1715 rb->Base.Base.NumSamples > 1) {
1716 intel_renderbuffer_upsample(intel, rb);
1717 }
1718 }
1719
1720 static void
1721 intel_update_image_buffers(struct brw_context *brw, __DRIdrawable *drawable)
1722 {
1723 struct gl_framebuffer *fb = drawable->driverPrivate;
1724 __DRIscreen *dri_screen = brw->screen->driScrnPriv;
1725 struct intel_renderbuffer *front_rb;
1726 struct intel_renderbuffer *back_rb;
1727 struct __DRIimageList images;
1728 unsigned int format;
1729 uint32_t buffer_mask = 0;
1730 int ret;
1731
1732 front_rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
1733 back_rb = intel_get_renderbuffer(fb, BUFFER_BACK_LEFT);
1734
1735 if (back_rb)
1736 format = intel_rb_format(back_rb);
1737 else if (front_rb)
1738 format = intel_rb_format(front_rb);
1739 else
1740 return;
1741
1742 if (front_rb && (_mesa_is_front_buffer_drawing(fb) ||
1743 _mesa_is_front_buffer_reading(fb) || !back_rb)) {
1744 buffer_mask |= __DRI_IMAGE_BUFFER_FRONT;
1745 }
1746
1747 if (back_rb)
1748 buffer_mask |= __DRI_IMAGE_BUFFER_BACK;
1749
1750 ret = dri_screen->image.loader->getBuffers(drawable,
1751 driGLFormatToImageFormat(format),
1752 &drawable->dri2.stamp,
1753 drawable->loaderPrivate,
1754 buffer_mask,
1755 &images);
1756 if (!ret)
1757 return;
1758
1759 if (images.image_mask & __DRI_IMAGE_BUFFER_FRONT) {
1760 drawable->w = images.front->width;
1761 drawable->h = images.front->height;
1762 intel_update_image_buffer(brw,
1763 drawable,
1764 front_rb,
1765 images.front,
1766 __DRI_IMAGE_BUFFER_FRONT);
1767 }
1768 if (images.image_mask & __DRI_IMAGE_BUFFER_BACK) {
1769 drawable->w = images.back->width;
1770 drawable->h = images.back->height;
1771 intel_update_image_buffer(brw,
1772 drawable,
1773 back_rb,
1774 images.back,
1775 __DRI_IMAGE_BUFFER_BACK);
1776 }
1777 }