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