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/imports.h"
38 #include "main/macros.h"
39 #include "main/points.h"
40 #include "main/version.h"
41 #include "main/vtxfmt.h"
43 #include "vbo/vbo_context.h"
45 #include "drivers/common/driverfuncs.h"
46 #include "drivers/common/meta.h"
49 #include "brw_context.h"
50 #include "brw_defines.h"
52 #include "brw_state.h"
54 #include "intel_batchbuffer.h"
55 #include "intel_buffer_objects.h"
56 #include "intel_buffers.h"
57 #include "intel_fbo.h"
58 #include "intel_mipmap_tree.h"
59 #include "intel_pixel.h"
60 #include "intel_regions.h"
61 #include "intel_tex.h"
62 #include "intel_tex_obj.h"
64 #include "swrast_setup/swrast_setup.h"
66 #include "tnl/t_pipeline.h"
67 #include "glsl/ralloc.h"
69 /***************************************
70 * Mesa's Driver Functions
71 ***************************************/
74 brw_query_samples_for_format(struct gl_context
*ctx
, GLenum target
,
75 GLenum internalFormat
, int samples
[16])
77 struct brw_context
*brw
= brw_context(ctx
);
103 const char *const brw_vendor_string
= "Intel Open Source Technology Center";
106 brw_get_renderer_string(unsigned deviceID
)
109 static char buffer
[128];
113 #define CHIPSET(id, symbol, str) case id: chipset = str; break;
114 #include "pci_ids/i965_pci_ids.h"
116 chipset
= "Unknown Intel Chipset";
120 (void) driGetRendererString(buffer
, chipset
, 0);
124 static const GLubyte
*
125 intelGetString(struct gl_context
* ctx
, GLenum name
)
127 const struct brw_context
*const brw
= brw_context(ctx
);
131 return (GLubyte
*) brw_vendor_string
;
135 (GLubyte
*) brw_get_renderer_string(brw
->intelScreen
->deviceID
);
143 intel_viewport(struct gl_context
*ctx
)
145 struct brw_context
*brw
= brw_context(ctx
);
146 __DRIcontext
*driContext
= brw
->driContext
;
148 if (_mesa_is_winsys_fbo(ctx
->DrawBuffer
)) {
149 dri2InvalidateDrawable(driContext
->driDrawablePriv
);
150 dri2InvalidateDrawable(driContext
->driReadablePriv
);
155 intelInvalidateState(struct gl_context
* ctx
, GLuint new_state
)
157 struct brw_context
*brw
= brw_context(ctx
);
159 if (ctx
->swrast_context
)
160 _swrast_InvalidateState(ctx
, new_state
);
161 _vbo_InvalidateState(ctx
, new_state
);
163 brw
->NewGLState
|= new_state
;
166 #define flushFront(screen) ((screen)->image.loader ? (screen)->image.loader->flushFrontBuffer : (screen)->dri2.loader->flushFrontBuffer)
169 intel_flush_front(struct gl_context
*ctx
)
171 struct brw_context
*brw
= brw_context(ctx
);
172 __DRIcontext
*driContext
= brw
->driContext
;
173 __DRIdrawable
*driDrawable
= driContext
->driDrawablePriv
;
174 __DRIscreen
*const screen
= brw
->intelScreen
->driScrnPriv
;
176 if (brw
->front_buffer_dirty
&& _mesa_is_winsys_fbo(ctx
->DrawBuffer
)) {
177 if (flushFront(screen
) && driDrawable
&&
178 driDrawable
->loaderPrivate
) {
180 /* Resolve before flushing FAKE_FRONT_LEFT to FRONT_LEFT.
182 * This potentially resolves both front and back buffer. It
183 * is unnecessary to resolve the back, but harms nothing except
184 * performance. And no one cares about front-buffer render
187 intel_resolve_for_dri2_flush(brw
, driDrawable
);
188 intel_batchbuffer_flush(brw
);
190 flushFront(screen
)(driDrawable
, driDrawable
->loaderPrivate
);
192 /* We set the dirty bit in intel_prepare_render() if we're
193 * front buffer rendering once we get there.
195 brw
->front_buffer_dirty
= false;
201 intel_glFlush(struct gl_context
*ctx
)
203 struct brw_context
*brw
= brw_context(ctx
);
205 intel_batchbuffer_flush(brw
);
206 intel_flush_front(ctx
);
207 if (brw
->is_front_buffer_rendering
)
208 brw
->need_throttle
= true;
212 intelFinish(struct gl_context
* ctx
)
214 struct brw_context
*brw
= brw_context(ctx
);
218 if (brw
->batch
.last_bo
)
219 drm_intel_bo_wait_rendering(brw
->batch
.last_bo
);
223 brw_init_driver_functions(struct brw_context
*brw
,
224 struct dd_function_table
*functions
)
226 _mesa_init_driver_functions(functions
);
228 /* GLX uses DRI2 invalidate events to handle window resizing.
229 * Unfortunately, EGL does not - libEGL is written in XCB (not Xlib),
230 * which doesn't provide a mechanism for snooping the event queues.
232 * So EGL still relies on viewport hacks to handle window resizing.
233 * This should go away with DRI3000.
235 if (!brw
->driContext
->driScreenPriv
->dri2
.useInvalidate
)
236 functions
->Viewport
= intel_viewport
;
238 functions
->Flush
= intel_glFlush
;
239 functions
->Finish
= intelFinish
;
240 functions
->GetString
= intelGetString
;
241 functions
->UpdateState
= intelInvalidateState
;
243 intelInitTextureFuncs(functions
);
244 intelInitTextureImageFuncs(functions
);
245 intelInitTextureSubImageFuncs(functions
);
246 intelInitTextureCopyImageFuncs(functions
);
247 intelInitClearFuncs(functions
);
248 intelInitBufferFuncs(functions
);
249 intelInitPixelFuncs(functions
);
250 intelInitBufferObjectFuncs(functions
);
251 intel_init_syncobj_functions(functions
);
252 brw_init_object_purgeable_functions(functions
);
254 brwInitFragProgFuncs( functions
);
255 brw_init_common_queryobj_functions(functions
);
257 gen6_init_queryobj_functions(functions
);
259 gen4_init_queryobj_functions(functions
);
261 functions
->QuerySamplesForFormat
= brw_query_samples_for_format
;
263 functions
->NewTransformFeedback
= brw_new_transform_feedback
;
264 functions
->DeleteTransformFeedback
= brw_delete_transform_feedback
;
265 functions
->GetTransformFeedbackVertexCount
=
266 brw_get_transform_feedback_vertex_count
;
268 functions
->BeginTransformFeedback
= gen7_begin_transform_feedback
;
269 functions
->EndTransformFeedback
= gen7_end_transform_feedback
;
270 functions
->PauseTransformFeedback
= gen7_pause_transform_feedback
;
271 functions
->ResumeTransformFeedback
= gen7_resume_transform_feedback
;
273 functions
->BeginTransformFeedback
= brw_begin_transform_feedback
;
274 functions
->EndTransformFeedback
= brw_end_transform_feedback
;
278 functions
->GetSamplePosition
= gen6_get_sample_position
;
282 brw_initialize_context_constants(struct brw_context
*brw
)
284 struct gl_context
*ctx
= &brw
->ctx
;
286 unsigned max_samplers
=
287 brw
->gen
>= 8 || brw
->is_haswell
? BRW_MAX_TEX_UNIT
: 16;
289 ctx
->Const
.QueryCounterBits
.Timestamp
= 36;
291 ctx
->Const
.StripTextureBorder
= true;
293 ctx
->Const
.MaxDualSourceDrawBuffers
= 1;
294 ctx
->Const
.MaxDrawBuffers
= BRW_MAX_DRAW_BUFFERS
;
295 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxTextureImageUnits
= max_samplers
;
296 ctx
->Const
.MaxTextureCoordUnits
= 8; /* Mesa limit */
297 ctx
->Const
.MaxTextureUnits
=
298 MIN2(ctx
->Const
.MaxTextureCoordUnits
,
299 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxTextureImageUnits
);
300 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTextureImageUnits
= max_samplers
;
302 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxTextureImageUnits
= max_samplers
;
304 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxTextureImageUnits
= 0;
305 if (getenv("INTEL_COMPUTE_SHADER")) {
306 ctx
->Const
.Program
[MESA_SHADER_COMPUTE
].MaxTextureImageUnits
= BRW_MAX_TEX_UNIT
;
307 ctx
->Const
.MaxUniformBufferBindings
+= 12;
309 ctx
->Const
.Program
[MESA_SHADER_COMPUTE
].MaxTextureImageUnits
= 0;
311 ctx
->Const
.MaxCombinedTextureImageUnits
=
312 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTextureImageUnits
+
313 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxTextureImageUnits
+
314 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxTextureImageUnits
+
315 ctx
->Const
.Program
[MESA_SHADER_COMPUTE
].MaxTextureImageUnits
;
317 ctx
->Const
.MaxTextureLevels
= 14; /* 8192 */
318 if (ctx
->Const
.MaxTextureLevels
> MAX_TEXTURE_LEVELS
)
319 ctx
->Const
.MaxTextureLevels
= MAX_TEXTURE_LEVELS
;
320 ctx
->Const
.Max3DTextureLevels
= 12; /* 2048 */
321 ctx
->Const
.MaxCubeTextureLevels
= 14; /* 8192 */
322 ctx
->Const
.MaxTextureMbytes
= 1536;
325 ctx
->Const
.MaxArrayTextureLayers
= 2048;
327 ctx
->Const
.MaxArrayTextureLayers
= 512;
329 ctx
->Const
.MaxTextureRectSize
= 1 << 12;
331 ctx
->Const
.MaxTextureMaxAnisotropy
= 16.0;
333 ctx
->Const
.MaxRenderbufferSize
= 8192;
335 /* Hardware only supports a limited number of transform feedback buffers.
336 * So we need to override the Mesa default (which is based only on software
339 ctx
->Const
.MaxTransformFeedbackBuffers
= BRW_MAX_SOL_BUFFERS
;
341 /* On Gen6, in the worst case, we use up one binding table entry per
342 * transform feedback component (see comments above the definition of
343 * BRW_MAX_SOL_BINDINGS, in brw_context.h), so we need to advertise a value
344 * for MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS equal to
345 * BRW_MAX_SOL_BINDINGS.
347 * In "separate components" mode, we need to divide this value by
348 * BRW_MAX_SOL_BUFFERS, so that the total number of binding table entries
349 * used up by all buffers will not exceed BRW_MAX_SOL_BINDINGS.
351 ctx
->Const
.MaxTransformFeedbackInterleavedComponents
= BRW_MAX_SOL_BINDINGS
;
352 ctx
->Const
.MaxTransformFeedbackSeparateComponents
=
353 BRW_MAX_SOL_BINDINGS
/ BRW_MAX_SOL_BUFFERS
;
355 ctx
->Const
.AlwaysUseGetTransformFeedbackVertexCount
= true;
358 const int *msaa_modes
= intel_supported_msaa_modes(brw
->intelScreen
);
359 const int clamp_max_samples
=
360 driQueryOptioni(&brw
->optionCache
, "clamp_max_samples");
362 if (clamp_max_samples
< 0) {
363 max_samples
= msaa_modes
[0];
365 /* Select the largest supported MSAA mode that does not exceed
369 for (int i
= 0; msaa_modes
[i
] != 0; ++i
) {
370 if (msaa_modes
[i
] <= clamp_max_samples
) {
371 max_samples
= msaa_modes
[i
];
377 ctx
->Const
.MaxSamples
= max_samples
;
378 ctx
->Const
.MaxColorTextureSamples
= max_samples
;
379 ctx
->Const
.MaxDepthTextureSamples
= max_samples
;
380 ctx
->Const
.MaxIntegerSamples
= max_samples
;
383 ctx
->Const
.MaxProgramTextureGatherComponents
= 4;
384 else if (brw
->gen
== 6)
385 ctx
->Const
.MaxProgramTextureGatherComponents
= 1;
387 ctx
->Const
.MinLineWidth
= 1.0;
388 ctx
->Const
.MinLineWidthAA
= 1.0;
389 ctx
->Const
.MaxLineWidth
= 5.0;
390 ctx
->Const
.MaxLineWidthAA
= 5.0;
391 ctx
->Const
.LineWidthGranularity
= 0.5;
393 ctx
->Const
.MinPointSize
= 1.0;
394 ctx
->Const
.MinPointSizeAA
= 1.0;
395 ctx
->Const
.MaxPointSize
= 255.0;
396 ctx
->Const
.MaxPointSizeAA
= 255.0;
397 ctx
->Const
.PointSizeGranularity
= 1.0;
399 if (brw
->gen
>= 5 || brw
->is_g4x
)
400 ctx
->Const
.MaxClipPlanes
= 8;
402 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeInstructions
= 16 * 1024;
403 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxAluInstructions
= 0;
404 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTexInstructions
= 0;
405 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTexIndirections
= 0;
406 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeAluInstructions
= 0;
407 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeTexInstructions
= 0;
408 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeTexIndirections
= 0;
409 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeAttribs
= 16;
410 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeTemps
= 256;
411 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeAddressRegs
= 1;
412 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeParameters
= 1024;
413 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxEnvParams
=
414 MIN2(ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeParameters
,
415 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxEnvParams
);
417 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeInstructions
= 1024;
418 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeAluInstructions
= 1024;
419 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeTexInstructions
= 1024;
420 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeTexIndirections
= 1024;
421 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeAttribs
= 12;
422 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeTemps
= 256;
423 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeAddressRegs
= 0;
424 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeParameters
= 1024;
425 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxEnvParams
=
426 MIN2(ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeParameters
,
427 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxEnvParams
);
429 /* Fragment shaders use real, 32-bit twos-complement integers for all
432 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].LowInt
.RangeMin
= 31;
433 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].LowInt
.RangeMax
= 30;
434 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].LowInt
.Precision
= 0;
435 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].HighInt
= ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].LowInt
;
436 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MediumInt
= ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].LowInt
;
439 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxAtomicCounters
= MAX_ATOMIC_COUNTERS
;
440 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxAtomicCounters
= MAX_ATOMIC_COUNTERS
;
441 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxAtomicCounters
= MAX_ATOMIC_COUNTERS
;
442 ctx
->Const
.Program
[MESA_SHADER_COMPUTE
].MaxAtomicCounters
= MAX_ATOMIC_COUNTERS
;
443 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxAtomicBuffers
= BRW_MAX_ABO
;
444 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxAtomicBuffers
= BRW_MAX_ABO
;
445 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxAtomicBuffers
= BRW_MAX_ABO
;
446 ctx
->Const
.Program
[MESA_SHADER_COMPUTE
].MaxAtomicBuffers
= BRW_MAX_ABO
;
447 ctx
->Const
.MaxCombinedAtomicBuffers
= 3 * BRW_MAX_ABO
;
450 /* Gen6 converts quads to polygon in beginning of 3D pipeline,
451 * but we're not sure how it's actually done for vertex order,
452 * that affect provoking vertex decision. Always use last vertex
453 * convention for quad primitive which works as expected for now.
456 ctx
->Const
.QuadsFollowProvokingVertexConvention
= false;
458 ctx
->Const
.NativeIntegers
= true;
459 ctx
->Const
.UniformBooleanTrue
= 1;
461 /* From the gen4 PRM, volume 4 page 127:
463 * "For SURFTYPE_BUFFER non-rendertarget surfaces, this field specifies
464 * the base address of the first element of the surface, computed in
465 * software by adding the surface base address to the byte offset of
466 * the element in the buffer."
468 * However, unaligned accesses are slower, so enforce buffer alignment.
470 ctx
->Const
.UniformBufferOffsetAlignment
= 16;
471 ctx
->Const
.TextureBufferOffsetAlignment
= 16;
474 ctx
->Const
.MaxVarying
= 32;
475 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxOutputComponents
= 128;
476 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxInputComponents
= 64;
477 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxOutputComponents
= 128;
478 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxInputComponents
= 128;
481 /* We want the GLSL compiler to emit code that uses condition codes */
482 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
483 ctx
->ShaderCompilerOptions
[i
].MaxIfDepth
= brw
->gen
< 6 ? 16 : UINT_MAX
;
484 ctx
->ShaderCompilerOptions
[i
].EmitCondCodes
= true;
485 ctx
->ShaderCompilerOptions
[i
].EmitNoNoise
= true;
486 ctx
->ShaderCompilerOptions
[i
].EmitNoMainReturn
= true;
487 ctx
->ShaderCompilerOptions
[i
].EmitNoIndirectInput
= true;
488 ctx
->ShaderCompilerOptions
[i
].EmitNoIndirectOutput
= true;
490 ctx
->ShaderCompilerOptions
[i
].EmitNoIndirectUniform
=
491 (i
== MESA_SHADER_FRAGMENT
);
492 ctx
->ShaderCompilerOptions
[i
].EmitNoIndirectTemp
=
493 (i
== MESA_SHADER_FRAGMENT
);
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
;
513 * Process driconf (drirc) options, setting appropriate context flags.
515 * intelInitExtensions still pokes at optionCache directly, in order to
516 * avoid advertising various extensions. No flags are set, so it makes
517 * sense to continue doing that there.
520 brw_process_driconf_options(struct brw_context
*brw
)
522 struct gl_context
*ctx
= &brw
->ctx
;
524 driOptionCache
*options
= &brw
->optionCache
;
525 driParseConfigFiles(options
, &brw
->intelScreen
->optionCache
,
526 brw
->driContext
->driScreenPriv
->myNum
, "i965");
528 int bo_reuse_mode
= driQueryOptioni(options
, "bo_reuse");
529 switch (bo_reuse_mode
) {
530 case DRI_CONF_BO_REUSE_DISABLED
:
532 case DRI_CONF_BO_REUSE_ALL
:
533 intel_bufmgr_gem_enable_reuse(brw
->bufmgr
);
537 if (!driQueryOptionb(options
, "hiz")) {
538 brw
->has_hiz
= false;
539 /* On gen6, you can only do separate stencil with HIZ. */
541 brw
->has_separate_stencil
= false;
544 if (driQueryOptionb(options
, "always_flush_batch")) {
545 fprintf(stderr
, "flushing batchbuffer before/after each draw call\n");
546 brw
->always_flush_batch
= true;
549 if (driQueryOptionb(options
, "always_flush_cache")) {
550 fprintf(stderr
, "flushing GPU caches before/after each draw call\n");
551 brw
->always_flush_cache
= true;
554 if (driQueryOptionb(options
, "disable_throttling")) {
555 fprintf(stderr
, "disabling flush throttling\n");
556 brw
->disable_throttling
= true;
559 brw
->disable_derivative_optimization
=
560 driQueryOptionb(&brw
->optionCache
, "disable_derivative_optimization");
562 brw
->precompile
= driQueryOptionb(&brw
->optionCache
, "shader_precompile");
564 ctx
->Const
.ForceGLSLExtensionsWarn
=
565 driQueryOptionb(options
, "force_glsl_extensions_warn");
567 ctx
->Const
.DisableGLSLLineContinuations
=
568 driQueryOptionb(options
, "disable_glsl_line_continuations");
572 brwCreateContext(gl_api api
,
573 const struct gl_config
*mesaVis
,
574 __DRIcontext
*driContextPriv
,
575 unsigned major_version
,
576 unsigned minor_version
,
579 unsigned *dri_ctx_error
,
580 void *sharedContextPrivate
)
582 __DRIscreen
*sPriv
= driContextPriv
->driScreenPriv
;
583 struct gl_context
*shareCtx
= (struct gl_context
*) sharedContextPrivate
;
584 struct intel_screen
*screen
= sPriv
->driverPrivate
;
585 const struct brw_device_info
*devinfo
= screen
->devinfo
;
586 struct dd_function_table functions
;
587 struct gl_config visual
;
589 /* Only allow the __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS flag if the kernel
590 * provides us with context reset notifications.
592 uint32_t allowed_flags
= __DRI_CTX_FLAG_DEBUG
593 | __DRI_CTX_FLAG_FORWARD_COMPATIBLE
;
595 if (screen
->has_context_reset_notification
)
596 allowed_flags
|= __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS
;
598 if (flags
& ~allowed_flags
) {
599 *dri_ctx_error
= __DRI_CTX_ERROR_UNKNOWN_FLAG
;
603 struct brw_context
*brw
= rzalloc(NULL
, struct brw_context
);
605 fprintf(stderr
, "%s: failed to alloc context\n", __FUNCTION__
);
606 *dri_ctx_error
= __DRI_CTX_ERROR_NO_MEMORY
;
610 driContextPriv
->driverPrivate
= brw
;
611 brw
->driContext
= driContextPriv
;
612 brw
->intelScreen
= screen
;
613 brw
->bufmgr
= screen
->bufmgr
;
615 brw
->gen
= devinfo
->gen
;
616 brw
->gt
= devinfo
->gt
;
617 brw
->is_g4x
= devinfo
->is_g4x
;
618 brw
->is_baytrail
= devinfo
->is_baytrail
;
619 brw
->is_haswell
= devinfo
->is_haswell
;
620 brw
->has_llc
= devinfo
->has_llc
;
621 brw
->has_hiz
= devinfo
->has_hiz_and_separate_stencil
;
622 brw
->has_separate_stencil
= devinfo
->has_hiz_and_separate_stencil
;
623 brw
->has_pln
= devinfo
->has_pln
;
624 brw
->has_compr4
= devinfo
->has_compr4
;
625 brw
->has_surface_tile_offset
= devinfo
->has_surface_tile_offset
;
626 brw
->has_negative_rhw_bug
= devinfo
->has_negative_rhw_bug
;
627 brw
->needs_unlit_centroid_workaround
=
628 devinfo
->needs_unlit_centroid_workaround
;
630 brw
->must_use_separate_stencil
= screen
->hw_must_use_separate_stencil
;
631 brw
->has_swizzling
= screen
->hw_has_swizzling
;
634 gen8_init_vtable_surface_functions(brw
);
635 gen7_init_vtable_sampler_functions(brw
);
636 brw
->vtbl
.emit_depth_stencil_hiz
= gen8_emit_depth_stencil_hiz
;
637 } else if (brw
->gen
>= 7) {
638 gen7_init_vtable_surface_functions(brw
);
639 gen7_init_vtable_sampler_functions(brw
);
640 brw
->vtbl
.emit_depth_stencil_hiz
= gen7_emit_depth_stencil_hiz
;
642 gen4_init_vtable_surface_functions(brw
);
643 gen4_init_vtable_sampler_functions(brw
);
644 brw
->vtbl
.emit_depth_stencil_hiz
= brw_emit_depth_stencil_hiz
;
647 brw_init_driver_functions(brw
, &functions
);
650 functions
.GetGraphicsResetStatus
= brw_get_graphics_reset_status
;
652 struct gl_context
*ctx
= &brw
->ctx
;
654 if (mesaVis
== NULL
) {
655 memset(&visual
, 0, sizeof visual
);
659 if (!_mesa_initialize_context(ctx
, api
, mesaVis
, shareCtx
, &functions
)) {
660 *dri_ctx_error
= __DRI_CTX_ERROR_NO_MEMORY
;
661 fprintf(stderr
, "%s: failed to init mesa context\n", __FUNCTION__
);
662 intelDestroyContext(driContextPriv
);
666 driContextSetFlags(ctx
, flags
);
668 /* Initialize the software rasterizer and helper modules.
670 * As of GL 3.1 core, the gen4+ driver doesn't need the swrast context for
671 * software fallbacks (which we have to support on legacy GL to do weird
672 * glDrawPixels(), glBitmap(), and other functions).
674 if (api
!= API_OPENGL_CORE
&& api
!= API_OPENGLES2
) {
675 _swrast_CreateContext(ctx
);
678 _vbo_CreateContext(ctx
);
679 if (ctx
->swrast_context
) {
680 _tnl_CreateContext(ctx
);
681 TNL_CONTEXT(ctx
)->Driver
.RunPipeline
= _tnl_run_pipeline
;
682 _swsetup_CreateContext(ctx
);
684 /* Configure swrast to match hardware characteristics: */
685 _swrast_allow_pixel_fog(ctx
, false);
686 _swrast_allow_vertex_fog(ctx
, true);
689 _mesa_meta_init(ctx
);
691 brw_process_driconf_options(brw
);
692 brw_process_intel_debug_variable(brw
);
693 brw_initialize_context_constants(brw
);
695 ctx
->Const
.ResetStrategy
= notify_reset
696 ? GL_LOSE_CONTEXT_ON_RESET_ARB
: GL_NO_RESET_NOTIFICATION_ARB
;
698 /* Reinitialize the context point state. It depends on ctx->Const values. */
699 _mesa_init_point(ctx
);
701 intel_batchbuffer_init(brw
);
704 /* Create a new hardware context. Using a hardware context means that
705 * our GPU state will be saved/restored on context switch, allowing us
706 * to assume that the GPU is in the same state we left it in.
708 * This is required for transform feedback buffer offsets, query objects,
709 * and also allows us to reduce how much state we have to emit.
711 brw
->hw_ctx
= drm_intel_gem_context_create(brw
->bufmgr
);
714 fprintf(stderr
, "Gen6+ requires Kernel 3.6 or later.\n");
715 intelDestroyContext(driContextPriv
);
722 intelInitExtensions(ctx
);
726 brw_init_surface_formats(brw
);
728 if (brw
->is_g4x
|| brw
->gen
>= 5) {
729 brw
->CMD_VF_STATISTICS
= GM45_3DSTATE_VF_STATISTICS
;
730 brw
->CMD_PIPELINE_SELECT
= CMD_PIPELINE_SELECT_GM45
;
732 brw
->CMD_VF_STATISTICS
= GEN4_3DSTATE_VF_STATISTICS
;
733 brw
->CMD_PIPELINE_SELECT
= CMD_PIPELINE_SELECT_965
;
736 brw
->max_vs_threads
= devinfo
->max_vs_threads
;
737 brw
->max_gs_threads
= devinfo
->max_gs_threads
;
738 brw
->max_wm_threads
= devinfo
->max_wm_threads
;
739 brw
->urb
.size
= devinfo
->urb
.size
;
740 brw
->urb
.min_vs_entries
= devinfo
->urb
.min_vs_entries
;
741 brw
->urb
.max_vs_entries
= devinfo
->urb
.max_vs_entries
;
742 brw
->urb
.max_gs_entries
= devinfo
->urb
.max_gs_entries
;
744 /* Estimate the size of the mappable aperture into the GTT. There's an
745 * ioctl to get the whole GTT size, but not one to get the mappable subset.
746 * It turns out it's basically always 256MB, though some ancient hardware
749 uint32_t gtt_size
= 256 * 1024 * 1024;
751 /* We don't want to map two objects such that a memcpy between them would
752 * just fault one mapping in and then the other over and over forever. So
753 * we would need to divide the GTT size by 2. Additionally, some GTT is
754 * taken up by things like the framebuffer and the ringbuffer and such, so
755 * be more conservative.
757 brw
->max_gtt_map_object_size
= gtt_size
/ 4;
760 brw
->urb
.gen6_gs_previously_active
= false;
762 brw
->prim_restart
.in_progress
= false;
763 brw
->prim_restart
.enable_cut_index
= false;
764 brw
->gs
.enabled
= false;
767 brw
->curbe
.last_buf
= calloc(1, 4096);
768 brw
->curbe
.next_buf
= calloc(1, 4096);
771 ctx
->VertexProgram
._MaintainTnlProgram
= true;
772 ctx
->FragmentProgram
._MaintainTexEnvProgram
= true;
774 brw_draw_init( brw
);
776 if ((flags
& __DRI_CTX_FLAG_DEBUG
) != 0) {
777 /* Turn on some extra GL_ARB_debug_output generation. */
778 brw
->perf_debug
= true;
781 if ((flags
& __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS
) != 0)
782 ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_ROBUST_ACCESS_BIT_ARB
;
784 brw_fs_alloc_reg_sets(brw
);
785 brw_vec4_alloc_reg_set(brw
);
787 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
)
788 brw_init_shader_time(brw
);
790 _mesa_compute_version(ctx
);
792 _mesa_initialize_dispatch_tables(ctx
);
793 _mesa_initialize_vbo_vtxfmt(ctx
);
795 if (ctx
->Extensions
.AMD_performance_monitor
) {
796 brw_init_performance_monitors(brw
);
803 intelDestroyContext(__DRIcontext
* driContextPriv
)
805 struct brw_context
*brw
=
806 (struct brw_context
*) driContextPriv
->driverPrivate
;
807 struct gl_context
*ctx
= &brw
->ctx
;
809 assert(brw
); /* should never be null */
813 /* Dump a final BMP in case the application doesn't call SwapBuffers */
814 if (INTEL_DEBUG
& DEBUG_AUB
) {
815 intel_batchbuffer_flush(brw
);
816 aub_dump_bmp(&brw
->ctx
);
819 _mesa_meta_free(&brw
->ctx
);
821 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
) {
822 /* Force a report. */
823 brw
->shader_time
.report_time
= 0;
825 brw_collect_and_report_shader_time(brw
);
826 brw_destroy_shader_time(brw
);
829 brw_destroy_state(brw
);
830 brw_draw_destroy(brw
);
832 drm_intel_bo_unreference(brw
->curbe
.curbe_bo
);
833 drm_intel_bo_unreference(brw
->vs
.base
.const_bo
);
834 drm_intel_bo_unreference(brw
->wm
.base
.const_bo
);
836 free(brw
->curbe
.last_buf
);
837 free(brw
->curbe
.next_buf
);
839 drm_intel_gem_context_destroy(brw
->hw_ctx
);
841 if (ctx
->swrast_context
) {
842 _swsetup_DestroyContext(&brw
->ctx
);
843 _tnl_DestroyContext(&brw
->ctx
);
845 _vbo_DestroyContext(&brw
->ctx
);
847 if (ctx
->swrast_context
)
848 _swrast_DestroyContext(&brw
->ctx
);
850 intel_batchbuffer_free(brw
);
852 drm_intel_bo_unreference(brw
->first_post_swapbuffers_batch
);
853 brw
->first_post_swapbuffers_batch
= NULL
;
855 driDestroyOptionCache(&brw
->optionCache
);
857 /* free the Mesa context */
858 _mesa_free_context_data(&brw
->ctx
);
861 driContextPriv
->driverPrivate
= NULL
;
865 intelUnbindContext(__DRIcontext
* driContextPriv
)
867 /* Unset current context and dispath table */
868 _mesa_make_current(NULL
, NULL
, NULL
);
874 * Fixes up the context for GLES23 with our default-to-sRGB-capable behavior
875 * on window system framebuffers.
877 * Desktop GL is fairly reasonable in its handling of sRGB: You can ask if
878 * your renderbuffer can do sRGB encode, and you can flip a switch that does
879 * sRGB encode if the renderbuffer can handle it. You can ask specifically
880 * for a visual where you're guaranteed to be capable, but it turns out that
881 * everyone just makes all their ARGB8888 visuals capable and doesn't offer
882 * incapable ones, becuase there's no difference between the two in resources
883 * used. Applications thus get built that accidentally rely on the default
884 * visual choice being sRGB, so we make ours sRGB capable. Everything sounds
887 * But for GLES2/3, they decided that it was silly to not turn on sRGB encode
888 * for sRGB renderbuffers you made with the GL_EXT_texture_sRGB equivalent.
889 * So they removed the enable knob and made it "if the renderbuffer is sRGB
890 * capable, do sRGB encode". Then, for your window system renderbuffers, you
891 * can ask for sRGB visuals and get sRGB encode, or not ask for sRGB visuals
892 * and get no sRGB encode (assuming that both kinds of visual are available).
893 * Thus our choice to support sRGB by default on our visuals for desktop would
894 * result in broken rendering of GLES apps that aren't expecting sRGB encode.
896 * Unfortunately, renderbuffer setup happens before a context is created. So
897 * in intel_screen.c we always set up sRGB, and here, if you're a GLES2/3
898 * context (without an sRGB visual, though we don't have sRGB visuals exposed
899 * yet), we go turn that back off before anyone finds out.
902 intel_gles3_srgb_workaround(struct brw_context
*brw
,
903 struct gl_framebuffer
*fb
)
905 struct gl_context
*ctx
= &brw
->ctx
;
907 if (_mesa_is_desktop_gl(ctx
) || !fb
->Visual
.sRGBCapable
)
910 /* Some day when we support the sRGB capable bit on visuals available for
911 * GLES, we'll need to respect that and not disable things here.
913 fb
->Visual
.sRGBCapable
= false;
914 for (int i
= 0; i
< BUFFER_COUNT
; i
++) {
915 if (fb
->Attachment
[i
].Renderbuffer
&&
916 fb
->Attachment
[i
].Renderbuffer
->Format
== MESA_FORMAT_B8G8R8A8_SRGB
) {
917 fb
->Attachment
[i
].Renderbuffer
->Format
= MESA_FORMAT_B8G8R8A8_UNORM
;
923 intelMakeCurrent(__DRIcontext
* driContextPriv
,
924 __DRIdrawable
* driDrawPriv
,
925 __DRIdrawable
* driReadPriv
)
927 struct brw_context
*brw
;
928 GET_CURRENT_CONTEXT(curCtx
);
931 brw
= (struct brw_context
*) driContextPriv
->driverPrivate
;
935 /* According to the glXMakeCurrent() man page: "Pending commands to
936 * the previous context, if any, are flushed before it is released."
937 * But only flush if we're actually changing contexts.
939 if (brw_context(curCtx
) && brw_context(curCtx
) != brw
) {
943 if (driContextPriv
) {
944 struct gl_context
*ctx
= &brw
->ctx
;
945 struct gl_framebuffer
*fb
, *readFb
;
947 if (driDrawPriv
== NULL
&& driReadPriv
== NULL
) {
948 fb
= _mesa_get_incomplete_framebuffer();
949 readFb
= _mesa_get_incomplete_framebuffer();
951 fb
= driDrawPriv
->driverPrivate
;
952 readFb
= driReadPriv
->driverPrivate
;
953 driContextPriv
->dri2
.draw_stamp
= driDrawPriv
->dri2
.stamp
- 1;
954 driContextPriv
->dri2
.read_stamp
= driReadPriv
->dri2
.stamp
- 1;
957 /* The sRGB workaround changes the renderbuffer's format. We must change
958 * the format before the renderbuffer's miptree get's allocated, otherwise
959 * the formats of the renderbuffer and its miptree will differ.
961 intel_gles3_srgb_workaround(brw
, fb
);
962 intel_gles3_srgb_workaround(brw
, readFb
);
964 /* If the context viewport hasn't been initialized, force a call out to
965 * the loader to get buffers so we have a drawable size for the initial
967 if (!brw
->ctx
.ViewportInitialized
)
968 intel_prepare_render(brw
);
970 _mesa_make_current(ctx
, fb
, readFb
);
972 _mesa_make_current(NULL
, NULL
, NULL
);
979 intel_resolve_for_dri2_flush(struct brw_context
*brw
,
980 __DRIdrawable
*drawable
)
983 /* MSAA and fast color clear are not supported, so don't waste time
984 * checking whether a resolve is needed.
989 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
990 struct intel_renderbuffer
*rb
;
992 /* Usually, only the back buffer will need to be downsampled. However,
993 * the front buffer will also need it if the user has rendered into it.
995 static const gl_buffer_index buffers
[2] = {
1000 for (int i
= 0; i
< 2; ++i
) {
1001 rb
= intel_get_renderbuffer(fb
, buffers
[i
]);
1002 if (rb
== NULL
|| rb
->mt
== NULL
)
1004 if (rb
->mt
->num_samples
<= 1)
1005 intel_miptree_resolve_color(brw
, rb
->mt
);
1007 intel_renderbuffer_downsample(brw
, rb
);
1012 intel_bits_per_pixel(const struct intel_renderbuffer
*rb
)
1014 return _mesa_get_format_bytes(intel_rb_format(rb
)) * 8;
1018 intel_query_dri2_buffers(struct brw_context
*brw
,
1019 __DRIdrawable
*drawable
,
1020 __DRIbuffer
**buffers
,
1024 intel_process_dri2_buffer(struct brw_context
*brw
,
1025 __DRIdrawable
*drawable
,
1026 __DRIbuffer
*buffer
,
1027 struct intel_renderbuffer
*rb
,
1028 const char *buffer_name
);
1031 intel_update_image_buffers(struct brw_context
*brw
, __DRIdrawable
*drawable
);
1034 intel_update_dri2_buffers(struct brw_context
*brw
, __DRIdrawable
*drawable
)
1036 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
1037 struct intel_renderbuffer
*rb
;
1038 __DRIbuffer
*buffers
= NULL
;
1040 const char *region_name
;
1042 /* Set this up front, so that in case our buffers get invalidated
1043 * while we're getting new buffers, we don't clobber the stamp and
1044 * thus ignore the invalidate. */
1045 drawable
->lastStamp
= drawable
->dri2
.stamp
;
1047 if (unlikely(INTEL_DEBUG
& DEBUG_DRI
))
1048 fprintf(stderr
, "enter %s, drawable %p\n", __func__
, drawable
);
1050 intel_query_dri2_buffers(brw
, drawable
, &buffers
, &count
);
1052 if (buffers
== NULL
)
1055 for (i
= 0; i
< count
; i
++) {
1056 switch (buffers
[i
].attachment
) {
1057 case __DRI_BUFFER_FRONT_LEFT
:
1058 rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
1059 region_name
= "dri2 front buffer";
1062 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
1063 rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
1064 region_name
= "dri2 fake front buffer";
1067 case __DRI_BUFFER_BACK_LEFT
:
1068 rb
= intel_get_renderbuffer(fb
, BUFFER_BACK_LEFT
);
1069 region_name
= "dri2 back buffer";
1072 case __DRI_BUFFER_DEPTH
:
1073 case __DRI_BUFFER_HIZ
:
1074 case __DRI_BUFFER_DEPTH_STENCIL
:
1075 case __DRI_BUFFER_STENCIL
:
1076 case __DRI_BUFFER_ACCUM
:
1079 "unhandled buffer attach event, attachment type %d\n",
1080 buffers
[i
].attachment
);
1084 intel_process_dri2_buffer(brw
, drawable
, &buffers
[i
], rb
, region_name
);
1090 intel_update_renderbuffers(__DRIcontext
*context
, __DRIdrawable
*drawable
)
1092 struct brw_context
*brw
= context
->driverPrivate
;
1093 __DRIscreen
*screen
= brw
->intelScreen
->driScrnPriv
;
1095 /* Set this up front, so that in case our buffers get invalidated
1096 * while we're getting new buffers, we don't clobber the stamp and
1097 * thus ignore the invalidate. */
1098 drawable
->lastStamp
= drawable
->dri2
.stamp
;
1100 if (unlikely(INTEL_DEBUG
& DEBUG_DRI
))
1101 fprintf(stderr
, "enter %s, drawable %p\n", __func__
, drawable
);
1103 if (screen
->image
.loader
)
1104 intel_update_image_buffers(brw
, drawable
);
1106 intel_update_dri2_buffers(brw
, drawable
);
1108 driUpdateFramebufferSize(&brw
->ctx
, drawable
);
1112 * intel_prepare_render should be called anywhere that curent read/drawbuffer
1113 * state is required.
1116 intel_prepare_render(struct brw_context
*brw
)
1118 __DRIcontext
*driContext
= brw
->driContext
;
1119 __DRIdrawable
*drawable
;
1121 drawable
= driContext
->driDrawablePriv
;
1122 if (drawable
&& drawable
->dri2
.stamp
!= driContext
->dri2
.draw_stamp
) {
1123 if (drawable
->lastStamp
!= drawable
->dri2
.stamp
)
1124 intel_update_renderbuffers(driContext
, drawable
);
1125 driContext
->dri2
.draw_stamp
= drawable
->dri2
.stamp
;
1128 drawable
= driContext
->driReadablePriv
;
1129 if (drawable
&& drawable
->dri2
.stamp
!= driContext
->dri2
.read_stamp
) {
1130 if (drawable
->lastStamp
!= drawable
->dri2
.stamp
)
1131 intel_update_renderbuffers(driContext
, drawable
);
1132 driContext
->dri2
.read_stamp
= drawable
->dri2
.stamp
;
1135 /* If we're currently rendering to the front buffer, the rendering
1136 * that will happen next will probably dirty the front buffer. So
1137 * mark it as dirty here.
1139 if (brw
->is_front_buffer_rendering
)
1140 brw
->front_buffer_dirty
= true;
1142 /* Wait for the swapbuffers before the one we just emitted, so we
1143 * don't get too many swaps outstanding for apps that are GPU-heavy
1144 * but not CPU-heavy.
1146 * We're using intelDRI2Flush (called from the loader before
1147 * swapbuffer) and glFlush (for front buffer rendering) as the
1148 * indicator that a frame is done and then throttle when we get
1149 * here as we prepare to render the next frame. At this point for
1150 * round trips for swap/copy and getting new buffers are done and
1151 * we'll spend less time waiting on the GPU.
1153 * Unfortunately, we don't have a handle to the batch containing
1154 * the swap, and getting our hands on that doesn't seem worth it,
1155 * so we just us the first batch we emitted after the last swap.
1157 if (brw
->need_throttle
&& brw
->first_post_swapbuffers_batch
) {
1158 if (!brw
->disable_throttling
)
1159 drm_intel_bo_wait_rendering(brw
->first_post_swapbuffers_batch
);
1160 drm_intel_bo_unreference(brw
->first_post_swapbuffers_batch
);
1161 brw
->first_post_swapbuffers_batch
= NULL
;
1162 brw
->need_throttle
= false;
1167 * \brief Query DRI2 to obtain a DRIdrawable's buffers.
1169 * To determine which DRI buffers to request, examine the renderbuffers
1170 * attached to the drawable's framebuffer. Then request the buffers with
1171 * DRI2GetBuffers() or DRI2GetBuffersWithFormat().
1173 * This is called from intel_update_renderbuffers().
1175 * \param drawable Drawable whose buffers are queried.
1176 * \param buffers [out] List of buffers returned by DRI2 query.
1177 * \param buffer_count [out] Number of buffers returned.
1179 * \see intel_update_renderbuffers()
1180 * \see DRI2GetBuffers()
1181 * \see DRI2GetBuffersWithFormat()
1184 intel_query_dri2_buffers(struct brw_context
*brw
,
1185 __DRIdrawable
*drawable
,
1186 __DRIbuffer
**buffers
,
1189 __DRIscreen
*screen
= brw
->intelScreen
->driScrnPriv
;
1190 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
1192 unsigned attachments
[8];
1194 struct intel_renderbuffer
*front_rb
;
1195 struct intel_renderbuffer
*back_rb
;
1197 front_rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
1198 back_rb
= intel_get_renderbuffer(fb
, BUFFER_BACK_LEFT
);
1200 memset(attachments
, 0, sizeof(attachments
));
1201 if ((brw
->is_front_buffer_rendering
||
1202 brw
->is_front_buffer_reading
||
1203 !back_rb
) && front_rb
) {
1204 /* If a fake front buffer is in use, then querying for
1205 * __DRI_BUFFER_FRONT_LEFT will cause the server to copy the image from
1206 * the real front buffer to the fake front buffer. So before doing the
1207 * query, we need to make sure all the pending drawing has landed in the
1208 * real front buffer.
1210 intel_batchbuffer_flush(brw
);
1211 intel_flush_front(&brw
->ctx
);
1213 attachments
[i
++] = __DRI_BUFFER_FRONT_LEFT
;
1214 attachments
[i
++] = intel_bits_per_pixel(front_rb
);
1215 } else if (front_rb
&& brw
->front_buffer_dirty
) {
1216 /* We have pending front buffer rendering, but we aren't querying for a
1217 * front buffer. If the front buffer we have is a fake front buffer,
1218 * the X server is going to throw it away when it processes the query.
1219 * So before doing the query, make sure all the pending drawing has
1220 * landed in the real front buffer.
1222 intel_batchbuffer_flush(brw
);
1223 intel_flush_front(&brw
->ctx
);
1227 attachments
[i
++] = __DRI_BUFFER_BACK_LEFT
;
1228 attachments
[i
++] = intel_bits_per_pixel(back_rb
);
1231 assert(i
<= ARRAY_SIZE(attachments
));
1233 *buffers
= screen
->dri2
.loader
->getBuffersWithFormat(drawable
,
1238 drawable
->loaderPrivate
);
1242 * \brief Assign a DRI buffer's DRM region to a renderbuffer.
1244 * This is called from intel_update_renderbuffers().
1247 * DRI buffers whose attachment point is DRI2BufferStencil or
1248 * DRI2BufferDepthStencil are handled as special cases.
1250 * \param buffer_name is a human readable name, such as "dri2 front buffer",
1251 * that is passed to intel_region_alloc_for_handle().
1253 * \see intel_update_renderbuffers()
1254 * \see intel_region_alloc_for_handle()
1257 intel_process_dri2_buffer(struct brw_context
*brw
,
1258 __DRIdrawable
*drawable
,
1259 __DRIbuffer
*buffer
,
1260 struct intel_renderbuffer
*rb
,
1261 const char *buffer_name
)
1263 struct intel_region
*region
= NULL
;
1268 unsigned num_samples
= rb
->Base
.Base
.NumSamples
;
1270 /* We try to avoid closing and reopening the same BO name, because the first
1271 * use of a mapping of the buffer involves a bunch of page faulting which is
1272 * moderately expensive.
1274 if (num_samples
== 0) {
1277 rb
->mt
->region
->name
== buffer
->name
)
1280 if (rb
->singlesample_mt
&&
1281 rb
->singlesample_mt
->region
&&
1282 rb
->singlesample_mt
->region
->name
== buffer
->name
)
1286 if (unlikely(INTEL_DEBUG
& DEBUG_DRI
)) {
1288 "attaching buffer %d, at %d, cpp %d, pitch %d\n",
1289 buffer
->name
, buffer
->attachment
,
1290 buffer
->cpp
, buffer
->pitch
);
1293 intel_miptree_release(&rb
->mt
);
1294 region
= intel_region_alloc_for_handle(brw
->intelScreen
,
1303 "Failed to make region for returned DRI2 buffer "
1304 "(%dx%d, named %d).\n"
1305 "This is likely a bug in the X Server that will lead to a "
1307 drawable
->w
, drawable
->h
, buffer
->name
);
1311 intel_update_winsys_renderbuffer_miptree(brw
, rb
, region
);
1313 if (brw
->is_front_buffer_rendering
&&
1314 (buffer
->attachment
== __DRI_BUFFER_FRONT_LEFT
||
1315 buffer
->attachment
== __DRI_BUFFER_FAKE_FRONT_LEFT
) &&
1316 rb
->Base
.Base
.NumSamples
> 1) {
1317 intel_renderbuffer_upsample(brw
, rb
);
1322 intel_region_release(®ion
);
1326 * \brief Query DRI image loader to obtain a DRIdrawable's buffers.
1328 * To determine which DRI buffers to request, examine the renderbuffers
1329 * attached to the drawable's framebuffer. Then request the buffers from
1332 * This is called from intel_update_renderbuffers().
1334 * \param drawable Drawable whose buffers are queried.
1335 * \param buffers [out] List of buffers returned by DRI2 query.
1336 * \param buffer_count [out] Number of buffers returned.
1338 * \see intel_update_renderbuffers()
1342 intel_update_image_buffer(struct brw_context
*intel
,
1343 __DRIdrawable
*drawable
,
1344 struct intel_renderbuffer
*rb
,
1346 enum __DRIimageBufferMask buffer_type
)
1348 struct intel_region
*region
= buffer
->region
;
1353 unsigned num_samples
= rb
->Base
.Base
.NumSamples
;
1355 /* Check and see if we're already bound to the right
1358 if (num_samples
== 0) {
1361 rb
->mt
->region
->bo
== region
->bo
)
1364 if (rb
->singlesample_mt
&&
1365 rb
->singlesample_mt
->region
&&
1366 rb
->singlesample_mt
->region
->bo
== region
->bo
)
1370 intel_update_winsys_renderbuffer_miptree(intel
, rb
, region
);
1372 if (intel
->is_front_buffer_rendering
&&
1373 buffer_type
== __DRI_IMAGE_BUFFER_FRONT
&&
1374 rb
->Base
.Base
.NumSamples
> 1) {
1375 intel_renderbuffer_upsample(intel
, rb
);
1380 intel_update_image_buffers(struct brw_context
*brw
, __DRIdrawable
*drawable
)
1382 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
1383 __DRIscreen
*screen
= brw
->intelScreen
->driScrnPriv
;
1384 struct intel_renderbuffer
*front_rb
;
1385 struct intel_renderbuffer
*back_rb
;
1386 struct __DRIimageList images
;
1387 unsigned int format
;
1388 uint32_t buffer_mask
= 0;
1390 front_rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
1391 back_rb
= intel_get_renderbuffer(fb
, BUFFER_BACK_LEFT
);
1394 format
= intel_rb_format(back_rb
);
1396 format
= intel_rb_format(front_rb
);
1400 if ((brw
->is_front_buffer_rendering
|| brw
->is_front_buffer_reading
|| !back_rb
) && front_rb
)
1401 buffer_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
1404 buffer_mask
|= __DRI_IMAGE_BUFFER_BACK
;
1406 (*screen
->image
.loader
->getBuffers
) (drawable
,
1407 driGLFormatToImageFormat(format
),
1408 &drawable
->dri2
.stamp
,
1409 drawable
->loaderPrivate
,
1413 if (images
.image_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
1414 drawable
->w
= images
.front
->width
;
1415 drawable
->h
= images
.front
->height
;
1416 intel_update_image_buffer(brw
,
1420 __DRI_IMAGE_BUFFER_FRONT
);
1422 if (images
.image_mask
& __DRI_IMAGE_BUFFER_BACK
) {
1423 drawable
->w
= images
.back
->width
;
1424 drawable
->h
= images
.back
->height
;
1425 intel_update_image_buffer(brw
,
1429 __DRI_IMAGE_BUFFER_BACK
);