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