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