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.
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:
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.
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.
27 **********************************************************************/
30 * Keith Whitwell <keithw@vmware.com>
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"
44 #include "vbo/vbo_context.h"
46 #include "drivers/common/driverfuncs.h"
47 #include "drivers/common/meta.h"
50 #include "brw_context.h"
51 #include "brw_defines.h"
53 #include "brw_state.h"
55 #include "intel_batchbuffer.h"
56 #include "intel_buffer_objects.h"
57 #include "intel_buffers.h"
58 #include "intel_fbo.h"
59 #include "intel_mipmap_tree.h"
60 #include "intel_pixel.h"
61 #include "intel_image.h"
62 #include "intel_tex.h"
63 #include "intel_tex_obj.h"
65 #include "swrast_setup/swrast_setup.h"
67 #include "tnl/t_pipeline.h"
68 #include "util/ralloc.h"
70 /***************************************
71 * Mesa's Driver Functions
72 ***************************************/
75 brw_query_samples_for_format(struct gl_context
*ctx
, GLenum target
,
76 GLenum internalFormat
, int samples
[16])
78 struct brw_context
*brw
= brw_context(ctx
);
104 const char *const brw_vendor_string
= "Intel Open Source Technology Center";
107 brw_get_renderer_string(unsigned deviceID
)
110 static char buffer
[128];
114 #define CHIPSET(id, symbol, str) case id: chipset = str; break;
115 #include "pci_ids/i965_pci_ids.h"
117 chipset
= "Unknown Intel Chipset";
121 (void) driGetRendererString(buffer
, chipset
, 0);
125 static const GLubyte
*
126 intelGetString(struct gl_context
* ctx
, GLenum name
)
128 const struct brw_context
*const brw
= brw_context(ctx
);
132 return (GLubyte
*) brw_vendor_string
;
136 (GLubyte
*) brw_get_renderer_string(brw
->intelScreen
->deviceID
);
144 intel_viewport(struct gl_context
*ctx
)
146 struct brw_context
*brw
= brw_context(ctx
);
147 __DRIcontext
*driContext
= brw
->driContext
;
149 if (_mesa_is_winsys_fbo(ctx
->DrawBuffer
)) {
150 dri2InvalidateDrawable(driContext
->driDrawablePriv
);
151 dri2InvalidateDrawable(driContext
->driReadablePriv
);
156 intelInvalidateState(struct gl_context
* ctx
, GLuint new_state
)
158 struct brw_context
*brw
= brw_context(ctx
);
160 if (ctx
->swrast_context
)
161 _swrast_InvalidateState(ctx
, new_state
);
162 _vbo_InvalidateState(ctx
, new_state
);
164 brw
->NewGLState
|= new_state
;
167 #define flushFront(screen) ((screen)->image.loader ? (screen)->image.loader->flushFrontBuffer : (screen)->dri2.loader->flushFrontBuffer)
170 intel_flush_front(struct gl_context
*ctx
)
172 struct brw_context
*brw
= brw_context(ctx
);
173 __DRIcontext
*driContext
= brw
->driContext
;
174 __DRIdrawable
*driDrawable
= driContext
->driDrawablePriv
;
175 __DRIscreen
*const screen
= brw
->intelScreen
->driScrnPriv
;
177 if (brw
->front_buffer_dirty
&& _mesa_is_winsys_fbo(ctx
->DrawBuffer
)) {
178 if (flushFront(screen
) && driDrawable
&&
179 driDrawable
->loaderPrivate
) {
181 /* Resolve before flushing FAKE_FRONT_LEFT to FRONT_LEFT.
183 * This potentially resolves both front and back buffer. It
184 * is unnecessary to resolve the back, but harms nothing except
185 * performance. And no one cares about front-buffer render
188 intel_resolve_for_dri2_flush(brw
, driDrawable
);
189 intel_batchbuffer_flush(brw
);
191 flushFront(screen
)(driDrawable
, driDrawable
->loaderPrivate
);
193 /* We set the dirty bit in intel_prepare_render() if we're
194 * front buffer rendering once we get there.
196 brw
->front_buffer_dirty
= false;
202 intel_glFlush(struct gl_context
*ctx
)
204 struct brw_context
*brw
= brw_context(ctx
);
206 intel_batchbuffer_flush(brw
);
207 intel_flush_front(ctx
);
208 if (brw_is_front_buffer_drawing(ctx
->DrawBuffer
))
209 brw
->need_throttle
= true;
213 intelFinish(struct gl_context
* ctx
)
215 struct brw_context
*brw
= brw_context(ctx
);
219 if (brw
->batch
.last_bo
)
220 drm_intel_bo_wait_rendering(brw
->batch
.last_bo
);
224 brw_init_driver_functions(struct brw_context
*brw
,
225 struct dd_function_table
*functions
)
227 _mesa_init_driver_functions(functions
);
229 /* GLX uses DRI2 invalidate events to handle window resizing.
230 * Unfortunately, EGL does not - libEGL is written in XCB (not Xlib),
231 * which doesn't provide a mechanism for snooping the event queues.
233 * So EGL still relies on viewport hacks to handle window resizing.
234 * This should go away with DRI3000.
236 if (!brw
->driContext
->driScreenPriv
->dri2
.useInvalidate
)
237 functions
->Viewport
= intel_viewport
;
239 functions
->Flush
= intel_glFlush
;
240 functions
->Finish
= intelFinish
;
241 functions
->GetString
= intelGetString
;
242 functions
->UpdateState
= intelInvalidateState
;
244 intelInitTextureFuncs(functions
);
245 intelInitTextureImageFuncs(functions
);
246 intelInitTextureSubImageFuncs(functions
);
247 intelInitTextureCopyImageFuncs(functions
);
248 intelInitClearFuncs(functions
);
249 intelInitBufferFuncs(functions
);
250 intelInitPixelFuncs(functions
);
251 intelInitBufferObjectFuncs(functions
);
252 intel_init_syncobj_functions(functions
);
253 brw_init_object_purgeable_functions(functions
);
255 brwInitFragProgFuncs( functions
);
256 brw_init_common_queryobj_functions(functions
);
258 gen6_init_queryobj_functions(functions
);
260 gen4_init_queryobj_functions(functions
);
262 functions
->QuerySamplesForFormat
= brw_query_samples_for_format
;
264 functions
->NewTransformFeedback
= brw_new_transform_feedback
;
265 functions
->DeleteTransformFeedback
= brw_delete_transform_feedback
;
266 functions
->GetTransformFeedbackVertexCount
=
267 brw_get_transform_feedback_vertex_count
;
269 functions
->BeginTransformFeedback
= gen7_begin_transform_feedback
;
270 functions
->EndTransformFeedback
= gen7_end_transform_feedback
;
271 functions
->PauseTransformFeedback
= gen7_pause_transform_feedback
;
272 functions
->ResumeTransformFeedback
= gen7_resume_transform_feedback
;
274 functions
->BeginTransformFeedback
= brw_begin_transform_feedback
;
275 functions
->EndTransformFeedback
= brw_end_transform_feedback
;
279 functions
->GetSamplePosition
= gen6_get_sample_position
;
283 brw_initialize_context_constants(struct brw_context
*brw
)
285 struct gl_context
*ctx
= &brw
->ctx
;
287 unsigned max_samplers
=
288 brw
->gen
>= 8 || brw
->is_haswell
? BRW_MAX_TEX_UNIT
: 16;
290 ctx
->Const
.QueryCounterBits
.Timestamp
= 36;
292 ctx
->Const
.StripTextureBorder
= true;
294 ctx
->Const
.MaxDualSourceDrawBuffers
= 1;
295 ctx
->Const
.MaxDrawBuffers
= BRW_MAX_DRAW_BUFFERS
;
296 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxTextureImageUnits
= max_samplers
;
297 ctx
->Const
.MaxTextureCoordUnits
= 8; /* Mesa limit */
298 ctx
->Const
.MaxTextureUnits
=
299 MIN2(ctx
->Const
.MaxTextureCoordUnits
,
300 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxTextureImageUnits
);
301 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTextureImageUnits
= max_samplers
;
303 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxTextureImageUnits
= max_samplers
;
305 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxTextureImageUnits
= 0;
306 if (_mesa_extension_override_enables
.ARB_compute_shader
) {
307 ctx
->Const
.Program
[MESA_SHADER_COMPUTE
].MaxTextureImageUnits
= BRW_MAX_TEX_UNIT
;
308 ctx
->Const
.MaxUniformBufferBindings
+= 12;
310 ctx
->Const
.Program
[MESA_SHADER_COMPUTE
].MaxTextureImageUnits
= 0;
312 ctx
->Const
.MaxCombinedTextureImageUnits
=
313 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTextureImageUnits
+
314 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxTextureImageUnits
+
315 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxTextureImageUnits
+
316 ctx
->Const
.Program
[MESA_SHADER_COMPUTE
].MaxTextureImageUnits
;
318 ctx
->Const
.MaxTextureLevels
= 14; /* 8192 */
319 if (ctx
->Const
.MaxTextureLevels
> MAX_TEXTURE_LEVELS
)
320 ctx
->Const
.MaxTextureLevels
= MAX_TEXTURE_LEVELS
;
321 ctx
->Const
.Max3DTextureLevels
= 12; /* 2048 */
322 ctx
->Const
.MaxCubeTextureLevels
= 14; /* 8192 */
323 ctx
->Const
.MaxTextureMbytes
= 1536;
326 ctx
->Const
.MaxArrayTextureLayers
= 2048;
328 ctx
->Const
.MaxArrayTextureLayers
= 512;
330 ctx
->Const
.MaxTextureRectSize
= 1 << 12;
332 ctx
->Const
.MaxTextureMaxAnisotropy
= 16.0;
334 ctx
->Const
.MaxRenderbufferSize
= 8192;
336 /* Hardware only supports a limited number of transform feedback buffers.
337 * So we need to override the Mesa default (which is based only on software
340 ctx
->Const
.MaxTransformFeedbackBuffers
= BRW_MAX_SOL_BUFFERS
;
342 /* On Gen6, in the worst case, we use up one binding table entry per
343 * transform feedback component (see comments above the definition of
344 * BRW_MAX_SOL_BINDINGS, in brw_context.h), so we need to advertise a value
345 * for MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS equal to
346 * BRW_MAX_SOL_BINDINGS.
348 * In "separate components" mode, we need to divide this value by
349 * BRW_MAX_SOL_BUFFERS, so that the total number of binding table entries
350 * used up by all buffers will not exceed BRW_MAX_SOL_BINDINGS.
352 ctx
->Const
.MaxTransformFeedbackInterleavedComponents
= BRW_MAX_SOL_BINDINGS
;
353 ctx
->Const
.MaxTransformFeedbackSeparateComponents
=
354 BRW_MAX_SOL_BINDINGS
/ BRW_MAX_SOL_BUFFERS
;
356 ctx
->Const
.AlwaysUseGetTransformFeedbackVertexCount
= true;
359 const int *msaa_modes
= intel_supported_msaa_modes(brw
->intelScreen
);
360 const int clamp_max_samples
=
361 driQueryOptioni(&brw
->optionCache
, "clamp_max_samples");
363 if (clamp_max_samples
< 0) {
364 max_samples
= msaa_modes
[0];
366 /* Select the largest supported MSAA mode that does not exceed
370 for (int i
= 0; msaa_modes
[i
] != 0; ++i
) {
371 if (msaa_modes
[i
] <= clamp_max_samples
) {
372 max_samples
= msaa_modes
[i
];
378 ctx
->Const
.MaxSamples
= max_samples
;
379 ctx
->Const
.MaxColorTextureSamples
= max_samples
;
380 ctx
->Const
.MaxDepthTextureSamples
= max_samples
;
381 ctx
->Const
.MaxIntegerSamples
= max_samples
;
384 ctx
->Const
.MaxProgramTextureGatherComponents
= 4;
385 else if (brw
->gen
== 6)
386 ctx
->Const
.MaxProgramTextureGatherComponents
= 1;
388 ctx
->Const
.MinLineWidth
= 1.0;
389 ctx
->Const
.MinLineWidthAA
= 1.0;
390 ctx
->Const
.MaxLineWidth
= 5.0;
391 ctx
->Const
.MaxLineWidthAA
= 5.0;
392 ctx
->Const
.LineWidthGranularity
= 0.5;
394 ctx
->Const
.MinPointSize
= 1.0;
395 ctx
->Const
.MinPointSizeAA
= 1.0;
396 ctx
->Const
.MaxPointSize
= 255.0;
397 ctx
->Const
.MaxPointSizeAA
= 255.0;
398 ctx
->Const
.PointSizeGranularity
= 1.0;
400 if (brw
->gen
>= 5 || brw
->is_g4x
)
401 ctx
->Const
.MaxClipPlanes
= 8;
403 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeInstructions
= 16 * 1024;
404 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxAluInstructions
= 0;
405 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTexInstructions
= 0;
406 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTexIndirections
= 0;
407 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeAluInstructions
= 0;
408 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeTexInstructions
= 0;
409 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeTexIndirections
= 0;
410 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeAttribs
= 16;
411 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeTemps
= 256;
412 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeAddressRegs
= 1;
413 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeParameters
= 1024;
414 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxEnvParams
=
415 MIN2(ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeParameters
,
416 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxEnvParams
);
418 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeInstructions
= 1024;
419 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeAluInstructions
= 1024;
420 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeTexInstructions
= 1024;
421 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeTexIndirections
= 1024;
422 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeAttribs
= 12;
423 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeTemps
= 256;
424 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeAddressRegs
= 0;
425 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeParameters
= 1024;
426 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxEnvParams
=
427 MIN2(ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeParameters
,
428 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxEnvParams
);
430 /* Fragment shaders use real, 32-bit twos-complement integers for all
433 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].LowInt
.RangeMin
= 31;
434 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].LowInt
.RangeMax
= 30;
435 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].LowInt
.Precision
= 0;
436 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].HighInt
= ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].LowInt
;
437 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MediumInt
= ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].LowInt
;
440 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxAtomicCounters
= MAX_ATOMIC_COUNTERS
;
441 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxAtomicCounters
= MAX_ATOMIC_COUNTERS
;
442 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxAtomicCounters
= MAX_ATOMIC_COUNTERS
;
443 ctx
->Const
.Program
[MESA_SHADER_COMPUTE
].MaxAtomicCounters
= MAX_ATOMIC_COUNTERS
;
444 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxAtomicBuffers
= BRW_MAX_ABO
;
445 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxAtomicBuffers
= BRW_MAX_ABO
;
446 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxAtomicBuffers
= BRW_MAX_ABO
;
447 ctx
->Const
.Program
[MESA_SHADER_COMPUTE
].MaxAtomicBuffers
= BRW_MAX_ABO
;
448 ctx
->Const
.MaxCombinedAtomicBuffers
= 3 * BRW_MAX_ABO
;
451 /* Gen6 converts quads to polygon in beginning of 3D pipeline,
452 * but we're not sure how it's actually done for vertex order,
453 * that affect provoking vertex decision. Always use last vertex
454 * convention for quad primitive which works as expected for now.
457 ctx
->Const
.QuadsFollowProvokingVertexConvention
= false;
459 ctx
->Const
.NativeIntegers
= true;
460 ctx
->Const
.UniformBooleanTrue
= 1;
462 /* From the gen4 PRM, volume 4 page 127:
464 * "For SURFTYPE_BUFFER non-rendertarget surfaces, this field specifies
465 * the base address of the first element of the surface, computed in
466 * software by adding the surface base address to the byte offset of
467 * the element in the buffer."
469 * However, unaligned accesses are slower, so enforce buffer alignment.
471 ctx
->Const
.UniformBufferOffsetAlignment
= 16;
472 ctx
->Const
.TextureBufferOffsetAlignment
= 16;
475 ctx
->Const
.MaxVarying
= 32;
476 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxOutputComponents
= 128;
477 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxInputComponents
= 64;
478 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxOutputComponents
= 128;
479 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxInputComponents
= 128;
482 /* We want the GLSL compiler to emit code that uses condition codes */
483 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
484 ctx
->ShaderCompilerOptions
[i
].MaxIfDepth
= brw
->gen
< 6 ? 16 : UINT_MAX
;
485 ctx
->ShaderCompilerOptions
[i
].EmitCondCodes
= true;
486 ctx
->ShaderCompilerOptions
[i
].EmitNoNoise
= true;
487 ctx
->ShaderCompilerOptions
[i
].EmitNoMainReturn
= true;
488 ctx
->ShaderCompilerOptions
[i
].EmitNoIndirectInput
= true;
489 ctx
->ShaderCompilerOptions
[i
].EmitNoIndirectOutput
=
490 (i
== MESA_SHADER_FRAGMENT
);
491 ctx
->ShaderCompilerOptions
[i
].EmitNoIndirectTemp
=
492 (i
== MESA_SHADER_FRAGMENT
);
493 ctx
->ShaderCompilerOptions
[i
].EmitNoIndirectUniform
= false;
494 ctx
->ShaderCompilerOptions
[i
].LowerClipDistance
= true;
497 ctx
->ShaderCompilerOptions
[MESA_SHADER_VERTEX
].OptimizeForAOS
= true;
498 ctx
->ShaderCompilerOptions
[MESA_SHADER_GEOMETRY
].OptimizeForAOS
= true;
500 /* ARB_viewport_array */
501 if (brw
->gen
>= 7 && ctx
->API
== API_OPENGL_CORE
) {
502 ctx
->Const
.MaxViewports
= GEN7_NUM_VIEWPORTS
;
503 ctx
->Const
.ViewportSubpixelBits
= 0;
505 /* Cast to float before negating becuase MaxViewportWidth is unsigned.
507 ctx
->Const
.ViewportBounds
.Min
= -(float)ctx
->Const
.MaxViewportWidth
;
508 ctx
->Const
.ViewportBounds
.Max
= ctx
->Const
.MaxViewportWidth
;
511 /* ARB_gpu_shader5 */
513 ctx
->Const
.MaxVertexStreams
= MIN2(4, MAX_VERTEX_STREAMS
);
517 * Process driconf (drirc) options, setting appropriate context flags.
519 * intelInitExtensions still pokes at optionCache directly, in order to
520 * avoid advertising various extensions. No flags are set, so it makes
521 * sense to continue doing that there.
524 brw_process_driconf_options(struct brw_context
*brw
)
526 struct gl_context
*ctx
= &brw
->ctx
;
528 driOptionCache
*options
= &brw
->optionCache
;
529 driParseConfigFiles(options
, &brw
->intelScreen
->optionCache
,
530 brw
->driContext
->driScreenPriv
->myNum
, "i965");
532 int bo_reuse_mode
= driQueryOptioni(options
, "bo_reuse");
533 switch (bo_reuse_mode
) {
534 case DRI_CONF_BO_REUSE_DISABLED
:
536 case DRI_CONF_BO_REUSE_ALL
:
537 intel_bufmgr_gem_enable_reuse(brw
->bufmgr
);
541 if (!driQueryOptionb(options
, "hiz")) {
542 brw
->has_hiz
= false;
543 /* On gen6, you can only do separate stencil with HIZ. */
545 brw
->has_separate_stencil
= false;
548 if (driQueryOptionb(options
, "always_flush_batch")) {
549 fprintf(stderr
, "flushing batchbuffer before/after each draw call\n");
550 brw
->always_flush_batch
= true;
553 if (driQueryOptionb(options
, "always_flush_cache")) {
554 fprintf(stderr
, "flushing GPU caches before/after each draw call\n");
555 brw
->always_flush_cache
= true;
558 if (driQueryOptionb(options
, "disable_throttling")) {
559 fprintf(stderr
, "disabling flush throttling\n");
560 brw
->disable_throttling
= true;
563 brw
->disable_derivative_optimization
=
564 driQueryOptionb(&brw
->optionCache
, "disable_derivative_optimization");
566 brw
->precompile
= driQueryOptionb(&brw
->optionCache
, "shader_precompile");
568 ctx
->Const
.ForceGLSLExtensionsWarn
=
569 driQueryOptionb(options
, "force_glsl_extensions_warn");
571 ctx
->Const
.DisableGLSLLineContinuations
=
572 driQueryOptionb(options
, "disable_glsl_line_continuations");
574 ctx
->Const
.AllowGLSLExtensionDirectiveMidShader
=
575 driQueryOptionb(options
, "allow_glsl_extension_directive_midshader");
579 brwCreateContext(gl_api api
,
580 const struct gl_config
*mesaVis
,
581 __DRIcontext
*driContextPriv
,
582 unsigned major_version
,
583 unsigned minor_version
,
586 unsigned *dri_ctx_error
,
587 void *sharedContextPrivate
)
589 __DRIscreen
*sPriv
= driContextPriv
->driScreenPriv
;
590 struct gl_context
*shareCtx
= (struct gl_context
*) sharedContextPrivate
;
591 struct intel_screen
*screen
= sPriv
->driverPrivate
;
592 const struct brw_device_info
*devinfo
= screen
->devinfo
;
593 struct dd_function_table functions
;
595 /* Only allow the __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS flag if the kernel
596 * provides us with context reset notifications.
598 uint32_t allowed_flags
= __DRI_CTX_FLAG_DEBUG
599 | __DRI_CTX_FLAG_FORWARD_COMPATIBLE
;
601 if (screen
->has_context_reset_notification
)
602 allowed_flags
|= __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS
;
604 if (flags
& ~allowed_flags
) {
605 *dri_ctx_error
= __DRI_CTX_ERROR_UNKNOWN_FLAG
;
609 struct brw_context
*brw
= rzalloc(NULL
, struct brw_context
);
611 fprintf(stderr
, "%s: failed to alloc context\n", __FUNCTION__
);
612 *dri_ctx_error
= __DRI_CTX_ERROR_NO_MEMORY
;
616 driContextPriv
->driverPrivate
= brw
;
617 brw
->driContext
= driContextPriv
;
618 brw
->intelScreen
= screen
;
619 brw
->bufmgr
= screen
->bufmgr
;
621 brw
->gen
= devinfo
->gen
;
622 brw
->gt
= devinfo
->gt
;
623 brw
->is_g4x
= devinfo
->is_g4x
;
624 brw
->is_baytrail
= devinfo
->is_baytrail
;
625 brw
->is_haswell
= devinfo
->is_haswell
;
626 brw
->is_cherryview
= devinfo
->is_cherryview
;
627 brw
->has_llc
= devinfo
->has_llc
;
628 brw
->has_hiz
= devinfo
->has_hiz_and_separate_stencil
;
629 brw
->has_separate_stencil
= devinfo
->has_hiz_and_separate_stencil
;
630 brw
->has_pln
= devinfo
->has_pln
;
631 brw
->has_compr4
= devinfo
->has_compr4
;
632 brw
->has_surface_tile_offset
= devinfo
->has_surface_tile_offset
;
633 brw
->has_negative_rhw_bug
= devinfo
->has_negative_rhw_bug
;
634 brw
->needs_unlit_centroid_workaround
=
635 devinfo
->needs_unlit_centroid_workaround
;
637 brw
->must_use_separate_stencil
= screen
->hw_must_use_separate_stencil
;
638 brw
->has_swizzling
= screen
->hw_has_swizzling
;
640 brw
->vs
.base
.stage
= MESA_SHADER_VERTEX
;
641 brw
->gs
.base
.stage
= MESA_SHADER_GEOMETRY
;
642 brw
->wm
.base
.stage
= MESA_SHADER_FRAGMENT
;
644 gen8_init_vtable_surface_functions(brw
);
645 brw
->vtbl
.emit_depth_stencil_hiz
= gen8_emit_depth_stencil_hiz
;
646 } else if (brw
->gen
>= 7) {
647 gen7_init_vtable_surface_functions(brw
);
648 brw
->vtbl
.emit_depth_stencil_hiz
= gen7_emit_depth_stencil_hiz
;
650 gen4_init_vtable_surface_functions(brw
);
651 brw
->vtbl
.emit_depth_stencil_hiz
= brw_emit_depth_stencil_hiz
;
654 brw_init_driver_functions(brw
, &functions
);
657 functions
.GetGraphicsResetStatus
= brw_get_graphics_reset_status
;
659 struct gl_context
*ctx
= &brw
->ctx
;
661 if (!_mesa_initialize_context(ctx
, api
, mesaVis
, shareCtx
, &functions
)) {
662 *dri_ctx_error
= __DRI_CTX_ERROR_NO_MEMORY
;
663 fprintf(stderr
, "%s: failed to init mesa context\n", __FUNCTION__
);
664 intelDestroyContext(driContextPriv
);
668 driContextSetFlags(ctx
, flags
);
670 /* Initialize the software rasterizer and helper modules.
672 * As of GL 3.1 core, the gen4+ driver doesn't need the swrast context for
673 * software fallbacks (which we have to support on legacy GL to do weird
674 * glDrawPixels(), glBitmap(), and other functions).
676 if (api
!= API_OPENGL_CORE
&& api
!= API_OPENGLES2
) {
677 _swrast_CreateContext(ctx
);
680 _vbo_CreateContext(ctx
);
681 if (ctx
->swrast_context
) {
682 _tnl_CreateContext(ctx
);
683 TNL_CONTEXT(ctx
)->Driver
.RunPipeline
= _tnl_run_pipeline
;
684 _swsetup_CreateContext(ctx
);
686 /* Configure swrast to match hardware characteristics: */
687 _swrast_allow_pixel_fog(ctx
, false);
688 _swrast_allow_vertex_fog(ctx
, true);
691 _mesa_meta_init(ctx
);
693 brw_process_driconf_options(brw
);
694 brw_process_intel_debug_variable(brw
);
695 brw_initialize_context_constants(brw
);
697 ctx
->Const
.ResetStrategy
= notify_reset
698 ? GL_LOSE_CONTEXT_ON_RESET_ARB
: GL_NO_RESET_NOTIFICATION_ARB
;
700 /* Reinitialize the context point state. It depends on ctx->Const values. */
701 _mesa_init_point(ctx
);
705 intel_batchbuffer_init(brw
);
708 /* Create a new hardware context. Using a hardware context means that
709 * our GPU state will be saved/restored on context switch, allowing us
710 * to assume that the GPU is in the same state we left it in.
712 * This is required for transform feedback buffer offsets, query objects,
713 * and also allows us to reduce how much state we have to emit.
715 brw
->hw_ctx
= drm_intel_gem_context_create(brw
->bufmgr
);
718 fprintf(stderr
, "Gen6+ requires Kernel 3.6 or later.\n");
719 intelDestroyContext(driContextPriv
);
726 intelInitExtensions(ctx
);
728 brw_init_surface_formats(brw
);
730 brw
->max_vs_threads
= devinfo
->max_vs_threads
;
731 brw
->max_gs_threads
= devinfo
->max_gs_threads
;
732 brw
->max_wm_threads
= devinfo
->max_wm_threads
;
733 brw
->urb
.size
= devinfo
->urb
.size
;
734 brw
->urb
.min_vs_entries
= devinfo
->urb
.min_vs_entries
;
735 brw
->urb
.max_vs_entries
= devinfo
->urb
.max_vs_entries
;
736 brw
->urb
.max_gs_entries
= devinfo
->urb
.max_gs_entries
;
738 /* Estimate the size of the mappable aperture into the GTT. There's an
739 * ioctl to get the whole GTT size, but not one to get the mappable subset.
740 * It turns out it's basically always 256MB, though some ancient hardware
743 uint32_t gtt_size
= 256 * 1024 * 1024;
745 /* We don't want to map two objects such that a memcpy between them would
746 * just fault one mapping in and then the other over and over forever. So
747 * we would need to divide the GTT size by 2. Additionally, some GTT is
748 * taken up by things like the framebuffer and the ringbuffer and such, so
749 * be more conservative.
751 brw
->max_gtt_map_object_size
= gtt_size
/ 4;
754 brw
->urb
.gen6_gs_previously_active
= false;
756 brw
->prim_restart
.in_progress
= false;
757 brw
->prim_restart
.enable_cut_index
= false;
758 brw
->gs
.enabled
= false;
760 ctx
->VertexProgram
._MaintainTnlProgram
= true;
761 ctx
->FragmentProgram
._MaintainTexEnvProgram
= true;
763 brw_draw_init( brw
);
765 if ((flags
& __DRI_CTX_FLAG_DEBUG
) != 0) {
766 /* Turn on some extra GL_ARB_debug_output generation. */
767 brw
->perf_debug
= true;
770 if ((flags
& __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS
) != 0)
771 ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_ROBUST_ACCESS_BIT_ARB
;
773 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
)
774 brw_init_shader_time(brw
);
776 _mesa_compute_version(ctx
);
778 _mesa_initialize_dispatch_tables(ctx
);
779 _mesa_initialize_vbo_vtxfmt(ctx
);
781 if (ctx
->Extensions
.AMD_performance_monitor
) {
782 brw_init_performance_monitors(brw
);
785 vbo_use_buffer_objects(ctx
);
786 vbo_always_unmap_buffers(ctx
);
792 intelDestroyContext(__DRIcontext
* driContextPriv
)
794 struct brw_context
*brw
=
795 (struct brw_context
*) driContextPriv
->driverPrivate
;
796 struct gl_context
*ctx
= &brw
->ctx
;
798 assert(brw
); /* should never be null */
802 /* Dump a final BMP in case the application doesn't call SwapBuffers */
803 if (INTEL_DEBUG
& DEBUG_AUB
) {
804 intel_batchbuffer_flush(brw
);
805 aub_dump_bmp(&brw
->ctx
);
808 _mesa_meta_free(&brw
->ctx
);
810 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
) {
811 /* Force a report. */
812 brw
->shader_time
.report_time
= 0;
814 brw_collect_and_report_shader_time(brw
);
815 brw_destroy_shader_time(brw
);
818 brw_destroy_state(brw
);
819 brw_draw_destroy(brw
);
821 drm_intel_bo_unreference(brw
->curbe
.curbe_bo
);
823 drm_intel_gem_context_destroy(brw
->hw_ctx
);
825 if (ctx
->swrast_context
) {
826 _swsetup_DestroyContext(&brw
->ctx
);
827 _tnl_DestroyContext(&brw
->ctx
);
829 _vbo_DestroyContext(&brw
->ctx
);
831 if (ctx
->swrast_context
)
832 _swrast_DestroyContext(&brw
->ctx
);
834 intel_batchbuffer_free(brw
);
836 drm_intel_bo_unreference(brw
->first_post_swapbuffers_batch
);
837 brw
->first_post_swapbuffers_batch
= NULL
;
839 driDestroyOptionCache(&brw
->optionCache
);
841 /* free the Mesa context */
842 _mesa_free_context_data(&brw
->ctx
);
845 driContextPriv
->driverPrivate
= NULL
;
849 intelUnbindContext(__DRIcontext
* driContextPriv
)
851 /* Unset current context and dispath table */
852 _mesa_make_current(NULL
, NULL
, NULL
);
858 * Fixes up the context for GLES23 with our default-to-sRGB-capable behavior
859 * on window system framebuffers.
861 * Desktop GL is fairly reasonable in its handling of sRGB: You can ask if
862 * your renderbuffer can do sRGB encode, and you can flip a switch that does
863 * sRGB encode if the renderbuffer can handle it. You can ask specifically
864 * for a visual where you're guaranteed to be capable, but it turns out that
865 * everyone just makes all their ARGB8888 visuals capable and doesn't offer
866 * incapable ones, becuase there's no difference between the two in resources
867 * used. Applications thus get built that accidentally rely on the default
868 * visual choice being sRGB, so we make ours sRGB capable. Everything sounds
871 * But for GLES2/3, they decided that it was silly to not turn on sRGB encode
872 * for sRGB renderbuffers you made with the GL_EXT_texture_sRGB equivalent.
873 * So they removed the enable knob and made it "if the renderbuffer is sRGB
874 * capable, do sRGB encode". Then, for your window system renderbuffers, you
875 * can ask for sRGB visuals and get sRGB encode, or not ask for sRGB visuals
876 * and get no sRGB encode (assuming that both kinds of visual are available).
877 * Thus our choice to support sRGB by default on our visuals for desktop would
878 * result in broken rendering of GLES apps that aren't expecting sRGB encode.
880 * Unfortunately, renderbuffer setup happens before a context is created. So
881 * in intel_screen.c we always set up sRGB, and here, if you're a GLES2/3
882 * context (without an sRGB visual, though we don't have sRGB visuals exposed
883 * yet), we go turn that back off before anyone finds out.
886 intel_gles3_srgb_workaround(struct brw_context
*brw
,
887 struct gl_framebuffer
*fb
)
889 struct gl_context
*ctx
= &brw
->ctx
;
891 if (_mesa_is_desktop_gl(ctx
) || !fb
->Visual
.sRGBCapable
)
894 /* Some day when we support the sRGB capable bit on visuals available for
895 * GLES, we'll need to respect that and not disable things here.
897 fb
->Visual
.sRGBCapable
= false;
898 for (int i
= 0; i
< BUFFER_COUNT
; i
++) {
899 if (fb
->Attachment
[i
].Renderbuffer
&&
900 fb
->Attachment
[i
].Renderbuffer
->Format
== MESA_FORMAT_B8G8R8A8_SRGB
) {
901 fb
->Attachment
[i
].Renderbuffer
->Format
= MESA_FORMAT_B8G8R8A8_UNORM
;
907 intelMakeCurrent(__DRIcontext
* driContextPriv
,
908 __DRIdrawable
* driDrawPriv
,
909 __DRIdrawable
* driReadPriv
)
911 struct brw_context
*brw
;
912 GET_CURRENT_CONTEXT(curCtx
);
915 brw
= (struct brw_context
*) driContextPriv
->driverPrivate
;
919 /* According to the glXMakeCurrent() man page: "Pending commands to
920 * the previous context, if any, are flushed before it is released."
921 * But only flush if we're actually changing contexts.
923 if (brw_context(curCtx
) && brw_context(curCtx
) != brw
) {
927 if (driContextPriv
) {
928 struct gl_context
*ctx
= &brw
->ctx
;
929 struct gl_framebuffer
*fb
, *readFb
;
931 if (driDrawPriv
== NULL
&& driReadPriv
== NULL
) {
932 fb
= _mesa_get_incomplete_framebuffer();
933 readFb
= _mesa_get_incomplete_framebuffer();
935 fb
= driDrawPriv
->driverPrivate
;
936 readFb
= driReadPriv
->driverPrivate
;
937 driContextPriv
->dri2
.draw_stamp
= driDrawPriv
->dri2
.stamp
- 1;
938 driContextPriv
->dri2
.read_stamp
= driReadPriv
->dri2
.stamp
- 1;
941 /* The sRGB workaround changes the renderbuffer's format. We must change
942 * the format before the renderbuffer's miptree get's allocated, otherwise
943 * the formats of the renderbuffer and its miptree will differ.
945 intel_gles3_srgb_workaround(brw
, fb
);
946 intel_gles3_srgb_workaround(brw
, readFb
);
948 /* If the context viewport hasn't been initialized, force a call out to
949 * the loader to get buffers so we have a drawable size for the initial
951 if (!brw
->ctx
.ViewportInitialized
)
952 intel_prepare_render(brw
);
954 _mesa_make_current(ctx
, fb
, readFb
);
956 _mesa_make_current(NULL
, NULL
, NULL
);
963 intel_resolve_for_dri2_flush(struct brw_context
*brw
,
964 __DRIdrawable
*drawable
)
967 /* MSAA and fast color clear are not supported, so don't waste time
968 * checking whether a resolve is needed.
973 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
974 struct intel_renderbuffer
*rb
;
976 /* Usually, only the back buffer will need to be downsampled. However,
977 * the front buffer will also need it if the user has rendered into it.
979 static const gl_buffer_index buffers
[2] = {
984 for (int i
= 0; i
< 2; ++i
) {
985 rb
= intel_get_renderbuffer(fb
, buffers
[i
]);
986 if (rb
== NULL
|| rb
->mt
== NULL
)
988 if (rb
->mt
->num_samples
<= 1)
989 intel_miptree_resolve_color(brw
, rb
->mt
);
991 intel_renderbuffer_downsample(brw
, rb
);
996 intel_bits_per_pixel(const struct intel_renderbuffer
*rb
)
998 return _mesa_get_format_bytes(intel_rb_format(rb
)) * 8;
1002 intel_query_dri2_buffers(struct brw_context
*brw
,
1003 __DRIdrawable
*drawable
,
1004 __DRIbuffer
**buffers
,
1008 intel_process_dri2_buffer(struct brw_context
*brw
,
1009 __DRIdrawable
*drawable
,
1010 __DRIbuffer
*buffer
,
1011 struct intel_renderbuffer
*rb
,
1012 const char *buffer_name
);
1015 intel_update_image_buffers(struct brw_context
*brw
, __DRIdrawable
*drawable
);
1018 intel_update_dri2_buffers(struct brw_context
*brw
, __DRIdrawable
*drawable
)
1020 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
1021 struct intel_renderbuffer
*rb
;
1022 __DRIbuffer
*buffers
= NULL
;
1024 const char *region_name
;
1026 /* Set this up front, so that in case our buffers get invalidated
1027 * while we're getting new buffers, we don't clobber the stamp and
1028 * thus ignore the invalidate. */
1029 drawable
->lastStamp
= drawable
->dri2
.stamp
;
1031 if (unlikely(INTEL_DEBUG
& DEBUG_DRI
))
1032 fprintf(stderr
, "enter %s, drawable %p\n", __func__
, drawable
);
1034 intel_query_dri2_buffers(brw
, drawable
, &buffers
, &count
);
1036 if (buffers
== NULL
)
1039 for (i
= 0; i
< count
; i
++) {
1040 switch (buffers
[i
].attachment
) {
1041 case __DRI_BUFFER_FRONT_LEFT
:
1042 rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
1043 region_name
= "dri2 front buffer";
1046 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
1047 rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
1048 region_name
= "dri2 fake front buffer";
1051 case __DRI_BUFFER_BACK_LEFT
:
1052 rb
= intel_get_renderbuffer(fb
, BUFFER_BACK_LEFT
);
1053 region_name
= "dri2 back buffer";
1056 case __DRI_BUFFER_DEPTH
:
1057 case __DRI_BUFFER_HIZ
:
1058 case __DRI_BUFFER_DEPTH_STENCIL
:
1059 case __DRI_BUFFER_STENCIL
:
1060 case __DRI_BUFFER_ACCUM
:
1063 "unhandled buffer attach event, attachment type %d\n",
1064 buffers
[i
].attachment
);
1068 intel_process_dri2_buffer(brw
, drawable
, &buffers
[i
], rb
, region_name
);
1074 intel_update_renderbuffers(__DRIcontext
*context
, __DRIdrawable
*drawable
)
1076 struct brw_context
*brw
= context
->driverPrivate
;
1077 __DRIscreen
*screen
= brw
->intelScreen
->driScrnPriv
;
1079 /* Set this up front, so that in case our buffers get invalidated
1080 * while we're getting new buffers, we don't clobber the stamp and
1081 * thus ignore the invalidate. */
1082 drawable
->lastStamp
= drawable
->dri2
.stamp
;
1084 if (unlikely(INTEL_DEBUG
& DEBUG_DRI
))
1085 fprintf(stderr
, "enter %s, drawable %p\n", __func__
, drawable
);
1087 if (screen
->image
.loader
)
1088 intel_update_image_buffers(brw
, drawable
);
1090 intel_update_dri2_buffers(brw
, drawable
);
1092 driUpdateFramebufferSize(&brw
->ctx
, drawable
);
1096 * intel_prepare_render should be called anywhere that curent read/drawbuffer
1097 * state is required.
1100 intel_prepare_render(struct brw_context
*brw
)
1102 struct gl_context
*ctx
= &brw
->ctx
;
1103 __DRIcontext
*driContext
= brw
->driContext
;
1104 __DRIdrawable
*drawable
;
1106 drawable
= driContext
->driDrawablePriv
;
1107 if (drawable
&& drawable
->dri2
.stamp
!= driContext
->dri2
.draw_stamp
) {
1108 if (drawable
->lastStamp
!= drawable
->dri2
.stamp
)
1109 intel_update_renderbuffers(driContext
, drawable
);
1110 driContext
->dri2
.draw_stamp
= drawable
->dri2
.stamp
;
1113 drawable
= driContext
->driReadablePriv
;
1114 if (drawable
&& drawable
->dri2
.stamp
!= driContext
->dri2
.read_stamp
) {
1115 if (drawable
->lastStamp
!= drawable
->dri2
.stamp
)
1116 intel_update_renderbuffers(driContext
, drawable
);
1117 driContext
->dri2
.read_stamp
= drawable
->dri2
.stamp
;
1120 /* If we're currently rendering to the front buffer, the rendering
1121 * that will happen next will probably dirty the front buffer. So
1122 * mark it as dirty here.
1124 if (brw_is_front_buffer_drawing(ctx
->DrawBuffer
))
1125 brw
->front_buffer_dirty
= true;
1127 /* Wait for the swapbuffers before the one we just emitted, so we
1128 * don't get too many swaps outstanding for apps that are GPU-heavy
1129 * but not CPU-heavy.
1131 * We're using intelDRI2Flush (called from the loader before
1132 * swapbuffer) and glFlush (for front buffer rendering) as the
1133 * indicator that a frame is done and then throttle when we get
1134 * here as we prepare to render the next frame. At this point for
1135 * round trips for swap/copy and getting new buffers are done and
1136 * we'll spend less time waiting on the GPU.
1138 * Unfortunately, we don't have a handle to the batch containing
1139 * the swap, and getting our hands on that doesn't seem worth it,
1140 * so we just us the first batch we emitted after the last swap.
1142 if (brw
->need_throttle
&& brw
->first_post_swapbuffers_batch
) {
1143 if (!brw
->disable_throttling
)
1144 drm_intel_bo_wait_rendering(brw
->first_post_swapbuffers_batch
);
1145 drm_intel_bo_unreference(brw
->first_post_swapbuffers_batch
);
1146 brw
->first_post_swapbuffers_batch
= NULL
;
1147 brw
->need_throttle
= false;
1152 * \brief Query DRI2 to obtain a DRIdrawable's buffers.
1154 * To determine which DRI buffers to request, examine the renderbuffers
1155 * attached to the drawable's framebuffer. Then request the buffers with
1156 * DRI2GetBuffers() or DRI2GetBuffersWithFormat().
1158 * This is called from intel_update_renderbuffers().
1160 * \param drawable Drawable whose buffers are queried.
1161 * \param buffers [out] List of buffers returned by DRI2 query.
1162 * \param buffer_count [out] Number of buffers returned.
1164 * \see intel_update_renderbuffers()
1165 * \see DRI2GetBuffers()
1166 * \see DRI2GetBuffersWithFormat()
1169 intel_query_dri2_buffers(struct brw_context
*brw
,
1170 __DRIdrawable
*drawable
,
1171 __DRIbuffer
**buffers
,
1174 __DRIscreen
*screen
= brw
->intelScreen
->driScrnPriv
;
1175 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
1177 unsigned attachments
[8];
1179 struct intel_renderbuffer
*front_rb
;
1180 struct intel_renderbuffer
*back_rb
;
1182 front_rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
1183 back_rb
= intel_get_renderbuffer(fb
, BUFFER_BACK_LEFT
);
1185 memset(attachments
, 0, sizeof(attachments
));
1186 if ((brw_is_front_buffer_drawing(fb
) ||
1187 brw_is_front_buffer_reading(fb
) ||
1188 !back_rb
) && front_rb
) {
1189 /* If a fake front buffer is in use, then querying for
1190 * __DRI_BUFFER_FRONT_LEFT will cause the server to copy the image from
1191 * the real front buffer to the fake front buffer. So before doing the
1192 * query, we need to make sure all the pending drawing has landed in the
1193 * real front buffer.
1195 intel_batchbuffer_flush(brw
);
1196 intel_flush_front(&brw
->ctx
);
1198 attachments
[i
++] = __DRI_BUFFER_FRONT_LEFT
;
1199 attachments
[i
++] = intel_bits_per_pixel(front_rb
);
1200 } else if (front_rb
&& brw
->front_buffer_dirty
) {
1201 /* We have pending front buffer rendering, but we aren't querying for a
1202 * front buffer. If the front buffer we have is a fake front buffer,
1203 * the X server is going to throw it away when it processes the query.
1204 * So before doing the query, make sure all the pending drawing has
1205 * landed in the real front buffer.
1207 intel_batchbuffer_flush(brw
);
1208 intel_flush_front(&brw
->ctx
);
1212 attachments
[i
++] = __DRI_BUFFER_BACK_LEFT
;
1213 attachments
[i
++] = intel_bits_per_pixel(back_rb
);
1216 assert(i
<= ARRAY_SIZE(attachments
));
1218 *buffers
= screen
->dri2
.loader
->getBuffersWithFormat(drawable
,
1223 drawable
->loaderPrivate
);
1227 * \brief Assign a DRI buffer's DRM region to a renderbuffer.
1229 * This is called from intel_update_renderbuffers().
1232 * DRI buffers whose attachment point is DRI2BufferStencil or
1233 * DRI2BufferDepthStencil are handled as special cases.
1235 * \param buffer_name is a human readable name, such as "dri2 front buffer",
1236 * that is passed to drm_intel_bo_gem_create_from_name().
1238 * \see intel_update_renderbuffers()
1241 intel_process_dri2_buffer(struct brw_context
*brw
,
1242 __DRIdrawable
*drawable
,
1243 __DRIbuffer
*buffer
,
1244 struct intel_renderbuffer
*rb
,
1245 const char *buffer_name
)
1247 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
1253 unsigned num_samples
= rb
->Base
.Base
.NumSamples
;
1255 /* We try to avoid closing and reopening the same BO name, because the first
1256 * use of a mapping of the buffer involves a bunch of page faulting which is
1257 * moderately expensive.
1259 struct intel_mipmap_tree
*last_mt
;
1260 if (num_samples
== 0)
1263 last_mt
= rb
->singlesample_mt
;
1265 uint32_t old_name
= 0;
1267 /* The bo already has a name because the miptree was created by a
1268 * previous call to intel_process_dri2_buffer(). If a bo already has a
1269 * name, then drm_intel_bo_flink() is a low-cost getter. It does not
1270 * create a new name.
1272 drm_intel_bo_flink(last_mt
->bo
, &old_name
);
1275 if (old_name
== buffer
->name
)
1278 if (unlikely(INTEL_DEBUG
& DEBUG_DRI
)) {
1280 "attaching buffer %d, at %d, cpp %d, pitch %d\n",
1281 buffer
->name
, buffer
->attachment
,
1282 buffer
->cpp
, buffer
->pitch
);
1285 intel_miptree_release(&rb
->mt
);
1286 bo
= drm_intel_bo_gem_create_from_name(brw
->bufmgr
, buffer_name
,
1290 "Failed to open BO for returned DRI2 buffer "
1291 "(%dx%d, %s, named %d).\n"
1292 "This is likely a bug in the X Server that will lead to a "
1294 drawable
->w
, drawable
->h
, buffer_name
, buffer
->name
);
1298 intel_update_winsys_renderbuffer_miptree(brw
, rb
, bo
,
1299 drawable
->w
, drawable
->h
,
1302 if (brw_is_front_buffer_drawing(fb
) &&
1303 (buffer
->attachment
== __DRI_BUFFER_FRONT_LEFT
||
1304 buffer
->attachment
== __DRI_BUFFER_FAKE_FRONT_LEFT
) &&
1305 rb
->Base
.Base
.NumSamples
> 1) {
1306 intel_renderbuffer_upsample(brw
, rb
);
1311 drm_intel_bo_unreference(bo
);
1315 * \brief Query DRI image loader to obtain a DRIdrawable's buffers.
1317 * To determine which DRI buffers to request, examine the renderbuffers
1318 * attached to the drawable's framebuffer. Then request the buffers from
1321 * This is called from intel_update_renderbuffers().
1323 * \param drawable Drawable whose buffers are queried.
1324 * \param buffers [out] List of buffers returned by DRI2 query.
1325 * \param buffer_count [out] Number of buffers returned.
1327 * \see intel_update_renderbuffers()
1331 intel_update_image_buffer(struct brw_context
*intel
,
1332 __DRIdrawable
*drawable
,
1333 struct intel_renderbuffer
*rb
,
1335 enum __DRIimageBufferMask buffer_type
)
1337 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
1339 if (!rb
|| !buffer
->bo
)
1342 unsigned num_samples
= rb
->Base
.Base
.NumSamples
;
1344 /* Check and see if we're already bound to the right
1347 struct intel_mipmap_tree
*last_mt
;
1348 if (num_samples
== 0)
1351 last_mt
= rb
->singlesample_mt
;
1353 if (last_mt
&& last_mt
->bo
== buffer
->bo
)
1356 intel_update_winsys_renderbuffer_miptree(intel
, rb
, buffer
->bo
,
1357 buffer
->width
, buffer
->height
,
1360 if (brw_is_front_buffer_drawing(fb
) &&
1361 buffer_type
== __DRI_IMAGE_BUFFER_FRONT
&&
1362 rb
->Base
.Base
.NumSamples
> 1) {
1363 intel_renderbuffer_upsample(intel
, rb
);
1368 intel_update_image_buffers(struct brw_context
*brw
, __DRIdrawable
*drawable
)
1370 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
1371 __DRIscreen
*screen
= brw
->intelScreen
->driScrnPriv
;
1372 struct intel_renderbuffer
*front_rb
;
1373 struct intel_renderbuffer
*back_rb
;
1374 struct __DRIimageList images
;
1375 unsigned int format
;
1376 uint32_t buffer_mask
= 0;
1378 front_rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
1379 back_rb
= intel_get_renderbuffer(fb
, BUFFER_BACK_LEFT
);
1382 format
= intel_rb_format(back_rb
);
1384 format
= intel_rb_format(front_rb
);
1388 if (front_rb
&& (brw_is_front_buffer_drawing(fb
) ||
1389 brw_is_front_buffer_reading(fb
) || !back_rb
)) {
1390 buffer_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
1394 buffer_mask
|= __DRI_IMAGE_BUFFER_BACK
;
1396 (*screen
->image
.loader
->getBuffers
) (drawable
,
1397 driGLFormatToImageFormat(format
),
1398 &drawable
->dri2
.stamp
,
1399 drawable
->loaderPrivate
,
1403 if (images
.image_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
1404 drawable
->w
= images
.front
->width
;
1405 drawable
->h
= images
.front
->height
;
1406 intel_update_image_buffer(brw
,
1410 __DRI_IMAGE_BUFFER_FRONT
);
1412 if (images
.image_mask
& __DRI_IMAGE_BUFFER_BACK
) {
1413 drawable
->w
= images
.back
->width
;
1414 drawable
->h
= images
.back
->height
;
1415 intel_update_image_buffer(brw
,
1419 __DRI_IMAGE_BUFFER_BACK
);