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