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");
576 brwCreateContext(gl_api api
,
577 const struct gl_config
*mesaVis
,
578 __DRIcontext
*driContextPriv
,
579 unsigned major_version
,
580 unsigned minor_version
,
583 unsigned *dri_ctx_error
,
584 void *sharedContextPrivate
)
586 __DRIscreen
*sPriv
= driContextPriv
->driScreenPriv
;
587 struct gl_context
*shareCtx
= (struct gl_context
*) sharedContextPrivate
;
588 struct intel_screen
*screen
= sPriv
->driverPrivate
;
589 const struct brw_device_info
*devinfo
= screen
->devinfo
;
590 struct dd_function_table functions
;
592 /* Only allow the __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS flag if the kernel
593 * provides us with context reset notifications.
595 uint32_t allowed_flags
= __DRI_CTX_FLAG_DEBUG
596 | __DRI_CTX_FLAG_FORWARD_COMPATIBLE
;
598 if (screen
->has_context_reset_notification
)
599 allowed_flags
|= __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS
;
601 if (flags
& ~allowed_flags
) {
602 *dri_ctx_error
= __DRI_CTX_ERROR_UNKNOWN_FLAG
;
606 struct brw_context
*brw
= rzalloc(NULL
, struct brw_context
);
608 fprintf(stderr
, "%s: failed to alloc context\n", __FUNCTION__
);
609 *dri_ctx_error
= __DRI_CTX_ERROR_NO_MEMORY
;
613 driContextPriv
->driverPrivate
= brw
;
614 brw
->driContext
= driContextPriv
;
615 brw
->intelScreen
= screen
;
616 brw
->bufmgr
= screen
->bufmgr
;
618 brw
->gen
= devinfo
->gen
;
619 brw
->gt
= devinfo
->gt
;
620 brw
->is_g4x
= devinfo
->is_g4x
;
621 brw
->is_baytrail
= devinfo
->is_baytrail
;
622 brw
->is_haswell
= devinfo
->is_haswell
;
623 brw
->is_cherryview
= devinfo
->is_cherryview
;
624 brw
->has_llc
= devinfo
->has_llc
;
625 brw
->has_hiz
= devinfo
->has_hiz_and_separate_stencil
;
626 brw
->has_separate_stencil
= devinfo
->has_hiz_and_separate_stencil
;
627 brw
->has_pln
= devinfo
->has_pln
;
628 brw
->has_compr4
= devinfo
->has_compr4
;
629 brw
->has_surface_tile_offset
= devinfo
->has_surface_tile_offset
;
630 brw
->has_negative_rhw_bug
= devinfo
->has_negative_rhw_bug
;
631 brw
->needs_unlit_centroid_workaround
=
632 devinfo
->needs_unlit_centroid_workaround
;
634 brw
->must_use_separate_stencil
= screen
->hw_must_use_separate_stencil
;
635 brw
->has_swizzling
= screen
->hw_has_swizzling
;
637 brw
->vs
.base
.stage
= MESA_SHADER_VERTEX
;
638 brw
->gs
.base
.stage
= MESA_SHADER_GEOMETRY
;
639 brw
->wm
.base
.stage
= MESA_SHADER_FRAGMENT
;
641 gen8_init_vtable_surface_functions(brw
);
642 brw
->vtbl
.emit_depth_stencil_hiz
= gen8_emit_depth_stencil_hiz
;
643 } else if (brw
->gen
>= 7) {
644 gen7_init_vtable_surface_functions(brw
);
645 brw
->vtbl
.emit_depth_stencil_hiz
= gen7_emit_depth_stencil_hiz
;
647 gen4_init_vtable_surface_functions(brw
);
648 brw
->vtbl
.emit_depth_stencil_hiz
= brw_emit_depth_stencil_hiz
;
651 brw_init_driver_functions(brw
, &functions
);
654 functions
.GetGraphicsResetStatus
= brw_get_graphics_reset_status
;
656 struct gl_context
*ctx
= &brw
->ctx
;
658 if (!_mesa_initialize_context(ctx
, api
, mesaVis
, shareCtx
, &functions
)) {
659 *dri_ctx_error
= __DRI_CTX_ERROR_NO_MEMORY
;
660 fprintf(stderr
, "%s: failed to init mesa context\n", __FUNCTION__
);
661 intelDestroyContext(driContextPriv
);
665 driContextSetFlags(ctx
, flags
);
667 /* Initialize the software rasterizer and helper modules.
669 * As of GL 3.1 core, the gen4+ driver doesn't need the swrast context for
670 * software fallbacks (which we have to support on legacy GL to do weird
671 * glDrawPixels(), glBitmap(), and other functions).
673 if (api
!= API_OPENGL_CORE
&& api
!= API_OPENGLES2
) {
674 _swrast_CreateContext(ctx
);
677 _vbo_CreateContext(ctx
);
678 if (ctx
->swrast_context
) {
679 _tnl_CreateContext(ctx
);
680 TNL_CONTEXT(ctx
)->Driver
.RunPipeline
= _tnl_run_pipeline
;
681 _swsetup_CreateContext(ctx
);
683 /* Configure swrast to match hardware characteristics: */
684 _swrast_allow_pixel_fog(ctx
, false);
685 _swrast_allow_vertex_fog(ctx
, true);
688 _mesa_meta_init(ctx
);
690 brw_process_driconf_options(brw
);
691 brw_process_intel_debug_variable(brw
);
692 brw_initialize_context_constants(brw
);
694 ctx
->Const
.ResetStrategy
= notify_reset
695 ? GL_LOSE_CONTEXT_ON_RESET_ARB
: GL_NO_RESET_NOTIFICATION_ARB
;
697 /* Reinitialize the context point state. It depends on ctx->Const values. */
698 _mesa_init_point(ctx
);
702 intel_batchbuffer_init(brw
);
705 /* Create a new hardware context. Using a hardware context means that
706 * our GPU state will be saved/restored on context switch, allowing us
707 * to assume that the GPU is in the same state we left it in.
709 * This is required for transform feedback buffer offsets, query objects,
710 * and also allows us to reduce how much state we have to emit.
712 brw
->hw_ctx
= drm_intel_gem_context_create(brw
->bufmgr
);
715 fprintf(stderr
, "Gen6+ requires Kernel 3.6 or later.\n");
716 intelDestroyContext(driContextPriv
);
723 intelInitExtensions(ctx
);
725 brw_init_surface_formats(brw
);
727 brw
->max_vs_threads
= devinfo
->max_vs_threads
;
728 brw
->max_gs_threads
= devinfo
->max_gs_threads
;
729 brw
->max_wm_threads
= devinfo
->max_wm_threads
;
730 brw
->urb
.size
= devinfo
->urb
.size
;
731 brw
->urb
.min_vs_entries
= devinfo
->urb
.min_vs_entries
;
732 brw
->urb
.max_vs_entries
= devinfo
->urb
.max_vs_entries
;
733 brw
->urb
.max_gs_entries
= devinfo
->urb
.max_gs_entries
;
735 /* Estimate the size of the mappable aperture into the GTT. There's an
736 * ioctl to get the whole GTT size, but not one to get the mappable subset.
737 * It turns out it's basically always 256MB, though some ancient hardware
740 uint32_t gtt_size
= 256 * 1024 * 1024;
742 /* We don't want to map two objects such that a memcpy between them would
743 * just fault one mapping in and then the other over and over forever. So
744 * we would need to divide the GTT size by 2. Additionally, some GTT is
745 * taken up by things like the framebuffer and the ringbuffer and such, so
746 * be more conservative.
748 brw
->max_gtt_map_object_size
= gtt_size
/ 4;
751 brw
->urb
.gen6_gs_previously_active
= false;
753 brw
->prim_restart
.in_progress
= false;
754 brw
->prim_restart
.enable_cut_index
= false;
755 brw
->gs
.enabled
= false;
757 ctx
->VertexProgram
._MaintainTnlProgram
= true;
758 ctx
->FragmentProgram
._MaintainTexEnvProgram
= true;
760 brw_draw_init( brw
);
762 if ((flags
& __DRI_CTX_FLAG_DEBUG
) != 0) {
763 /* Turn on some extra GL_ARB_debug_output generation. */
764 brw
->perf_debug
= true;
767 if ((flags
& __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS
) != 0)
768 ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_ROBUST_ACCESS_BIT_ARB
;
770 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
)
771 brw_init_shader_time(brw
);
773 _mesa_compute_version(ctx
);
775 _mesa_initialize_dispatch_tables(ctx
);
776 _mesa_initialize_vbo_vtxfmt(ctx
);
778 if (ctx
->Extensions
.AMD_performance_monitor
) {
779 brw_init_performance_monitors(brw
);
782 vbo_use_buffer_objects(ctx
);
783 vbo_always_unmap_buffers(ctx
);
789 intelDestroyContext(__DRIcontext
* driContextPriv
)
791 struct brw_context
*brw
=
792 (struct brw_context
*) driContextPriv
->driverPrivate
;
793 struct gl_context
*ctx
= &brw
->ctx
;
795 assert(brw
); /* should never be null */
799 /* Dump a final BMP in case the application doesn't call SwapBuffers */
800 if (INTEL_DEBUG
& DEBUG_AUB
) {
801 intel_batchbuffer_flush(brw
);
802 aub_dump_bmp(&brw
->ctx
);
805 _mesa_meta_free(&brw
->ctx
);
807 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
) {
808 /* Force a report. */
809 brw
->shader_time
.report_time
= 0;
811 brw_collect_and_report_shader_time(brw
);
812 brw_destroy_shader_time(brw
);
815 brw_destroy_state(brw
);
816 brw_draw_destroy(brw
);
818 drm_intel_bo_unreference(brw
->curbe
.curbe_bo
);
820 drm_intel_gem_context_destroy(brw
->hw_ctx
);
822 if (ctx
->swrast_context
) {
823 _swsetup_DestroyContext(&brw
->ctx
);
824 _tnl_DestroyContext(&brw
->ctx
);
826 _vbo_DestroyContext(&brw
->ctx
);
828 if (ctx
->swrast_context
)
829 _swrast_DestroyContext(&brw
->ctx
);
831 intel_batchbuffer_free(brw
);
833 drm_intel_bo_unreference(brw
->first_post_swapbuffers_batch
);
834 brw
->first_post_swapbuffers_batch
= NULL
;
836 driDestroyOptionCache(&brw
->optionCache
);
838 /* free the Mesa context */
839 _mesa_free_context_data(&brw
->ctx
);
842 driContextPriv
->driverPrivate
= NULL
;
846 intelUnbindContext(__DRIcontext
* driContextPriv
)
848 /* Unset current context and dispath table */
849 _mesa_make_current(NULL
, NULL
, NULL
);
855 * Fixes up the context for GLES23 with our default-to-sRGB-capable behavior
856 * on window system framebuffers.
858 * Desktop GL is fairly reasonable in its handling of sRGB: You can ask if
859 * your renderbuffer can do sRGB encode, and you can flip a switch that does
860 * sRGB encode if the renderbuffer can handle it. You can ask specifically
861 * for a visual where you're guaranteed to be capable, but it turns out that
862 * everyone just makes all their ARGB8888 visuals capable and doesn't offer
863 * incapable ones, becuase there's no difference between the two in resources
864 * used. Applications thus get built that accidentally rely on the default
865 * visual choice being sRGB, so we make ours sRGB capable. Everything sounds
868 * But for GLES2/3, they decided that it was silly to not turn on sRGB encode
869 * for sRGB renderbuffers you made with the GL_EXT_texture_sRGB equivalent.
870 * So they removed the enable knob and made it "if the renderbuffer is sRGB
871 * capable, do sRGB encode". Then, for your window system renderbuffers, you
872 * can ask for sRGB visuals and get sRGB encode, or not ask for sRGB visuals
873 * and get no sRGB encode (assuming that both kinds of visual are available).
874 * Thus our choice to support sRGB by default on our visuals for desktop would
875 * result in broken rendering of GLES apps that aren't expecting sRGB encode.
877 * Unfortunately, renderbuffer setup happens before a context is created. So
878 * in intel_screen.c we always set up sRGB, and here, if you're a GLES2/3
879 * context (without an sRGB visual, though we don't have sRGB visuals exposed
880 * yet), we go turn that back off before anyone finds out.
883 intel_gles3_srgb_workaround(struct brw_context
*brw
,
884 struct gl_framebuffer
*fb
)
886 struct gl_context
*ctx
= &brw
->ctx
;
888 if (_mesa_is_desktop_gl(ctx
) || !fb
->Visual
.sRGBCapable
)
891 /* Some day when we support the sRGB capable bit on visuals available for
892 * GLES, we'll need to respect that and not disable things here.
894 fb
->Visual
.sRGBCapable
= false;
895 for (int i
= 0; i
< BUFFER_COUNT
; i
++) {
896 if (fb
->Attachment
[i
].Renderbuffer
&&
897 fb
->Attachment
[i
].Renderbuffer
->Format
== MESA_FORMAT_B8G8R8A8_SRGB
) {
898 fb
->Attachment
[i
].Renderbuffer
->Format
= MESA_FORMAT_B8G8R8A8_UNORM
;
904 intelMakeCurrent(__DRIcontext
* driContextPriv
,
905 __DRIdrawable
* driDrawPriv
,
906 __DRIdrawable
* driReadPriv
)
908 struct brw_context
*brw
;
909 GET_CURRENT_CONTEXT(curCtx
);
912 brw
= (struct brw_context
*) driContextPriv
->driverPrivate
;
916 /* According to the glXMakeCurrent() man page: "Pending commands to
917 * the previous context, if any, are flushed before it is released."
918 * But only flush if we're actually changing contexts.
920 if (brw_context(curCtx
) && brw_context(curCtx
) != brw
) {
924 if (driContextPriv
) {
925 struct gl_context
*ctx
= &brw
->ctx
;
926 struct gl_framebuffer
*fb
, *readFb
;
928 if (driDrawPriv
== NULL
&& driReadPriv
== NULL
) {
929 fb
= _mesa_get_incomplete_framebuffer();
930 readFb
= _mesa_get_incomplete_framebuffer();
932 fb
= driDrawPriv
->driverPrivate
;
933 readFb
= driReadPriv
->driverPrivate
;
934 driContextPriv
->dri2
.draw_stamp
= driDrawPriv
->dri2
.stamp
- 1;
935 driContextPriv
->dri2
.read_stamp
= driReadPriv
->dri2
.stamp
- 1;
938 /* The sRGB workaround changes the renderbuffer's format. We must change
939 * the format before the renderbuffer's miptree get's allocated, otherwise
940 * the formats of the renderbuffer and its miptree will differ.
942 intel_gles3_srgb_workaround(brw
, fb
);
943 intel_gles3_srgb_workaround(brw
, readFb
);
945 /* If the context viewport hasn't been initialized, force a call out to
946 * the loader to get buffers so we have a drawable size for the initial
948 if (!brw
->ctx
.ViewportInitialized
)
949 intel_prepare_render(brw
);
951 _mesa_make_current(ctx
, fb
, readFb
);
953 _mesa_make_current(NULL
, NULL
, NULL
);
960 intel_resolve_for_dri2_flush(struct brw_context
*brw
,
961 __DRIdrawable
*drawable
)
964 /* MSAA and fast color clear are not supported, so don't waste time
965 * checking whether a resolve is needed.
970 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
971 struct intel_renderbuffer
*rb
;
973 /* Usually, only the back buffer will need to be downsampled. However,
974 * the front buffer will also need it if the user has rendered into it.
976 static const gl_buffer_index buffers
[2] = {
981 for (int i
= 0; i
< 2; ++i
) {
982 rb
= intel_get_renderbuffer(fb
, buffers
[i
]);
983 if (rb
== NULL
|| rb
->mt
== NULL
)
985 if (rb
->mt
->num_samples
<= 1)
986 intel_miptree_resolve_color(brw
, rb
->mt
);
988 intel_renderbuffer_downsample(brw
, rb
);
993 intel_bits_per_pixel(const struct intel_renderbuffer
*rb
)
995 return _mesa_get_format_bytes(intel_rb_format(rb
)) * 8;
999 intel_query_dri2_buffers(struct brw_context
*brw
,
1000 __DRIdrawable
*drawable
,
1001 __DRIbuffer
**buffers
,
1005 intel_process_dri2_buffer(struct brw_context
*brw
,
1006 __DRIdrawable
*drawable
,
1007 __DRIbuffer
*buffer
,
1008 struct intel_renderbuffer
*rb
,
1009 const char *buffer_name
);
1012 intel_update_image_buffers(struct brw_context
*brw
, __DRIdrawable
*drawable
);
1015 intel_update_dri2_buffers(struct brw_context
*brw
, __DRIdrawable
*drawable
)
1017 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
1018 struct intel_renderbuffer
*rb
;
1019 __DRIbuffer
*buffers
= NULL
;
1021 const char *region_name
;
1023 /* Set this up front, so that in case our buffers get invalidated
1024 * while we're getting new buffers, we don't clobber the stamp and
1025 * thus ignore the invalidate. */
1026 drawable
->lastStamp
= drawable
->dri2
.stamp
;
1028 if (unlikely(INTEL_DEBUG
& DEBUG_DRI
))
1029 fprintf(stderr
, "enter %s, drawable %p\n", __func__
, drawable
);
1031 intel_query_dri2_buffers(brw
, drawable
, &buffers
, &count
);
1033 if (buffers
== NULL
)
1036 for (i
= 0; i
< count
; i
++) {
1037 switch (buffers
[i
].attachment
) {
1038 case __DRI_BUFFER_FRONT_LEFT
:
1039 rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
1040 region_name
= "dri2 front buffer";
1043 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
1044 rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
1045 region_name
= "dri2 fake front buffer";
1048 case __DRI_BUFFER_BACK_LEFT
:
1049 rb
= intel_get_renderbuffer(fb
, BUFFER_BACK_LEFT
);
1050 region_name
= "dri2 back buffer";
1053 case __DRI_BUFFER_DEPTH
:
1054 case __DRI_BUFFER_HIZ
:
1055 case __DRI_BUFFER_DEPTH_STENCIL
:
1056 case __DRI_BUFFER_STENCIL
:
1057 case __DRI_BUFFER_ACCUM
:
1060 "unhandled buffer attach event, attachment type %d\n",
1061 buffers
[i
].attachment
);
1065 intel_process_dri2_buffer(brw
, drawable
, &buffers
[i
], rb
, region_name
);
1071 intel_update_renderbuffers(__DRIcontext
*context
, __DRIdrawable
*drawable
)
1073 struct brw_context
*brw
= context
->driverPrivate
;
1074 __DRIscreen
*screen
= brw
->intelScreen
->driScrnPriv
;
1076 /* Set this up front, so that in case our buffers get invalidated
1077 * while we're getting new buffers, we don't clobber the stamp and
1078 * thus ignore the invalidate. */
1079 drawable
->lastStamp
= drawable
->dri2
.stamp
;
1081 if (unlikely(INTEL_DEBUG
& DEBUG_DRI
))
1082 fprintf(stderr
, "enter %s, drawable %p\n", __func__
, drawable
);
1084 if (screen
->image
.loader
)
1085 intel_update_image_buffers(brw
, drawable
);
1087 intel_update_dri2_buffers(brw
, drawable
);
1089 driUpdateFramebufferSize(&brw
->ctx
, drawable
);
1093 * intel_prepare_render should be called anywhere that curent read/drawbuffer
1094 * state is required.
1097 intel_prepare_render(struct brw_context
*brw
)
1099 struct gl_context
*ctx
= &brw
->ctx
;
1100 __DRIcontext
*driContext
= brw
->driContext
;
1101 __DRIdrawable
*drawable
;
1103 drawable
= driContext
->driDrawablePriv
;
1104 if (drawable
&& drawable
->dri2
.stamp
!= driContext
->dri2
.draw_stamp
) {
1105 if (drawable
->lastStamp
!= drawable
->dri2
.stamp
)
1106 intel_update_renderbuffers(driContext
, drawable
);
1107 driContext
->dri2
.draw_stamp
= drawable
->dri2
.stamp
;
1110 drawable
= driContext
->driReadablePriv
;
1111 if (drawable
&& drawable
->dri2
.stamp
!= driContext
->dri2
.read_stamp
) {
1112 if (drawable
->lastStamp
!= drawable
->dri2
.stamp
)
1113 intel_update_renderbuffers(driContext
, drawable
);
1114 driContext
->dri2
.read_stamp
= drawable
->dri2
.stamp
;
1117 /* If we're currently rendering to the front buffer, the rendering
1118 * that will happen next will probably dirty the front buffer. So
1119 * mark it as dirty here.
1121 if (brw_is_front_buffer_drawing(ctx
->DrawBuffer
))
1122 brw
->front_buffer_dirty
= true;
1124 /* Wait for the swapbuffers before the one we just emitted, so we
1125 * don't get too many swaps outstanding for apps that are GPU-heavy
1126 * but not CPU-heavy.
1128 * We're using intelDRI2Flush (called from the loader before
1129 * swapbuffer) and glFlush (for front buffer rendering) as the
1130 * indicator that a frame is done and then throttle when we get
1131 * here as we prepare to render the next frame. At this point for
1132 * round trips for swap/copy and getting new buffers are done and
1133 * we'll spend less time waiting on the GPU.
1135 * Unfortunately, we don't have a handle to the batch containing
1136 * the swap, and getting our hands on that doesn't seem worth it,
1137 * so we just us the first batch we emitted after the last swap.
1139 if (brw
->need_throttle
&& brw
->first_post_swapbuffers_batch
) {
1140 if (!brw
->disable_throttling
)
1141 drm_intel_bo_wait_rendering(brw
->first_post_swapbuffers_batch
);
1142 drm_intel_bo_unreference(brw
->first_post_swapbuffers_batch
);
1143 brw
->first_post_swapbuffers_batch
= NULL
;
1144 brw
->need_throttle
= false;
1149 * \brief Query DRI2 to obtain a DRIdrawable's buffers.
1151 * To determine which DRI buffers to request, examine the renderbuffers
1152 * attached to the drawable's framebuffer. Then request the buffers with
1153 * DRI2GetBuffers() or DRI2GetBuffersWithFormat().
1155 * This is called from intel_update_renderbuffers().
1157 * \param drawable Drawable whose buffers are queried.
1158 * \param buffers [out] List of buffers returned by DRI2 query.
1159 * \param buffer_count [out] Number of buffers returned.
1161 * \see intel_update_renderbuffers()
1162 * \see DRI2GetBuffers()
1163 * \see DRI2GetBuffersWithFormat()
1166 intel_query_dri2_buffers(struct brw_context
*brw
,
1167 __DRIdrawable
*drawable
,
1168 __DRIbuffer
**buffers
,
1171 __DRIscreen
*screen
= brw
->intelScreen
->driScrnPriv
;
1172 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
1174 unsigned attachments
[8];
1176 struct intel_renderbuffer
*front_rb
;
1177 struct intel_renderbuffer
*back_rb
;
1179 front_rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
1180 back_rb
= intel_get_renderbuffer(fb
, BUFFER_BACK_LEFT
);
1182 memset(attachments
, 0, sizeof(attachments
));
1183 if ((brw_is_front_buffer_drawing(fb
) ||
1184 brw_is_front_buffer_reading(fb
) ||
1185 !back_rb
) && front_rb
) {
1186 /* If a fake front buffer is in use, then querying for
1187 * __DRI_BUFFER_FRONT_LEFT will cause the server to copy the image from
1188 * the real front buffer to the fake front buffer. So before doing the
1189 * query, we need to make sure all the pending drawing has landed in the
1190 * real front buffer.
1192 intel_batchbuffer_flush(brw
);
1193 intel_flush_front(&brw
->ctx
);
1195 attachments
[i
++] = __DRI_BUFFER_FRONT_LEFT
;
1196 attachments
[i
++] = intel_bits_per_pixel(front_rb
);
1197 } else if (front_rb
&& brw
->front_buffer_dirty
) {
1198 /* We have pending front buffer rendering, but we aren't querying for a
1199 * front buffer. If the front buffer we have is a fake front buffer,
1200 * the X server is going to throw it away when it processes the query.
1201 * So before doing the query, make sure all the pending drawing has
1202 * landed in the real front buffer.
1204 intel_batchbuffer_flush(brw
);
1205 intel_flush_front(&brw
->ctx
);
1209 attachments
[i
++] = __DRI_BUFFER_BACK_LEFT
;
1210 attachments
[i
++] = intel_bits_per_pixel(back_rb
);
1213 assert(i
<= ARRAY_SIZE(attachments
));
1215 *buffers
= screen
->dri2
.loader
->getBuffersWithFormat(drawable
,
1220 drawable
->loaderPrivate
);
1224 * \brief Assign a DRI buffer's DRM region to a renderbuffer.
1226 * This is called from intel_update_renderbuffers().
1229 * DRI buffers whose attachment point is DRI2BufferStencil or
1230 * DRI2BufferDepthStencil are handled as special cases.
1232 * \param buffer_name is a human readable name, such as "dri2 front buffer",
1233 * that is passed to drm_intel_bo_gem_create_from_name().
1235 * \see intel_update_renderbuffers()
1238 intel_process_dri2_buffer(struct brw_context
*brw
,
1239 __DRIdrawable
*drawable
,
1240 __DRIbuffer
*buffer
,
1241 struct intel_renderbuffer
*rb
,
1242 const char *buffer_name
)
1244 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
1250 unsigned num_samples
= rb
->Base
.Base
.NumSamples
;
1252 /* We try to avoid closing and reopening the same BO name, because the first
1253 * use of a mapping of the buffer involves a bunch of page faulting which is
1254 * moderately expensive.
1256 struct intel_mipmap_tree
*last_mt
;
1257 if (num_samples
== 0)
1260 last_mt
= rb
->singlesample_mt
;
1262 uint32_t old_name
= 0;
1264 /* The bo already has a name because the miptree was created by a
1265 * previous call to intel_process_dri2_buffer(). If a bo already has a
1266 * name, then drm_intel_bo_flink() is a low-cost getter. It does not
1267 * create a new name.
1269 drm_intel_bo_flink(last_mt
->bo
, &old_name
);
1272 if (old_name
== buffer
->name
)
1275 if (unlikely(INTEL_DEBUG
& DEBUG_DRI
)) {
1277 "attaching buffer %d, at %d, cpp %d, pitch %d\n",
1278 buffer
->name
, buffer
->attachment
,
1279 buffer
->cpp
, buffer
->pitch
);
1282 intel_miptree_release(&rb
->mt
);
1283 bo
= drm_intel_bo_gem_create_from_name(brw
->bufmgr
, buffer_name
,
1287 "Failed to open BO for returned DRI2 buffer "
1288 "(%dx%d, %s, named %d).\n"
1289 "This is likely a bug in the X Server that will lead to a "
1291 drawable
->w
, drawable
->h
, buffer_name
, buffer
->name
);
1295 intel_update_winsys_renderbuffer_miptree(brw
, rb
, bo
,
1296 drawable
->w
, drawable
->h
,
1299 if (brw_is_front_buffer_drawing(fb
) &&
1300 (buffer
->attachment
== __DRI_BUFFER_FRONT_LEFT
||
1301 buffer
->attachment
== __DRI_BUFFER_FAKE_FRONT_LEFT
) &&
1302 rb
->Base
.Base
.NumSamples
> 1) {
1303 intel_renderbuffer_upsample(brw
, rb
);
1308 drm_intel_bo_unreference(bo
);
1312 * \brief Query DRI image loader to obtain a DRIdrawable's buffers.
1314 * To determine which DRI buffers to request, examine the renderbuffers
1315 * attached to the drawable's framebuffer. Then request the buffers from
1318 * This is called from intel_update_renderbuffers().
1320 * \param drawable Drawable whose buffers are queried.
1321 * \param buffers [out] List of buffers returned by DRI2 query.
1322 * \param buffer_count [out] Number of buffers returned.
1324 * \see intel_update_renderbuffers()
1328 intel_update_image_buffer(struct brw_context
*intel
,
1329 __DRIdrawable
*drawable
,
1330 struct intel_renderbuffer
*rb
,
1332 enum __DRIimageBufferMask buffer_type
)
1334 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
1336 if (!rb
|| !buffer
->bo
)
1339 unsigned num_samples
= rb
->Base
.Base
.NumSamples
;
1341 /* Check and see if we're already bound to the right
1344 struct intel_mipmap_tree
*last_mt
;
1345 if (num_samples
== 0)
1348 last_mt
= rb
->singlesample_mt
;
1350 if (last_mt
&& last_mt
->bo
== buffer
->bo
)
1353 intel_update_winsys_renderbuffer_miptree(intel
, rb
, buffer
->bo
,
1354 buffer
->width
, buffer
->height
,
1357 if (brw_is_front_buffer_drawing(fb
) &&
1358 buffer_type
== __DRI_IMAGE_BUFFER_FRONT
&&
1359 rb
->Base
.Base
.NumSamples
> 1) {
1360 intel_renderbuffer_upsample(intel
, rb
);
1365 intel_update_image_buffers(struct brw_context
*brw
, __DRIdrawable
*drawable
)
1367 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
1368 __DRIscreen
*screen
= brw
->intelScreen
->driScrnPriv
;
1369 struct intel_renderbuffer
*front_rb
;
1370 struct intel_renderbuffer
*back_rb
;
1371 struct __DRIimageList images
;
1372 unsigned int format
;
1373 uint32_t buffer_mask
= 0;
1375 front_rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
1376 back_rb
= intel_get_renderbuffer(fb
, BUFFER_BACK_LEFT
);
1379 format
= intel_rb_format(back_rb
);
1381 format
= intel_rb_format(front_rb
);
1385 if (front_rb
&& (brw_is_front_buffer_drawing(fb
) ||
1386 brw_is_front_buffer_reading(fb
) || !back_rb
)) {
1387 buffer_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
1391 buffer_mask
|= __DRI_IMAGE_BUFFER_BACK
;
1393 (*screen
->image
.loader
->getBuffers
) (drawable
,
1394 driGLFormatToImageFormat(format
),
1395 &drawable
->dri2
.stamp
,
1396 drawable
->loaderPrivate
,
1400 if (images
.image_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
1401 drawable
->w
= images
.front
->width
;
1402 drawable
->h
= images
.front
->height
;
1403 intel_update_image_buffer(brw
,
1407 __DRI_IMAGE_BUFFER_FRONT
);
1409 if (images
.image_mask
& __DRI_IMAGE_BUFFER_BACK
) {
1410 drawable
->w
= images
.back
->width
;
1411 drawable
->h
= images
.back
->height
;
1412 intel_update_image_buffer(brw
,
1416 __DRI_IMAGE_BUFFER_BACK
);