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 intel_get_string(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 intel_update_state(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 intel_finish(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
= intel_finish
;
241 functions
->GetString
= intel_get_string
;
242 functions
->UpdateState
= intel_update_state
;
244 intelInitTextureFuncs(functions
);
245 intelInitTextureImageFuncs(functions
);
246 intelInitTextureSubImageFuncs(functions
);
247 intelInitTextureCopyImageFuncs(functions
);
248 intelInitCopyImageFuncs(functions
);
249 intelInitClearFuncs(functions
);
250 intelInitBufferFuncs(functions
);
251 intelInitPixelFuncs(functions
);
252 intelInitBufferObjectFuncs(functions
);
253 intel_init_syncobj_functions(functions
);
254 brw_init_object_purgeable_functions(functions
);
256 brwInitFragProgFuncs( functions
);
257 brw_init_common_queryobj_functions(functions
);
259 gen6_init_queryobj_functions(functions
);
261 gen4_init_queryobj_functions(functions
);
263 functions
->QuerySamplesForFormat
= brw_query_samples_for_format
;
265 functions
->NewTransformFeedback
= brw_new_transform_feedback
;
266 functions
->DeleteTransformFeedback
= brw_delete_transform_feedback
;
267 functions
->GetTransformFeedbackVertexCount
=
268 brw_get_transform_feedback_vertex_count
;
270 functions
->BeginTransformFeedback
= gen7_begin_transform_feedback
;
271 functions
->EndTransformFeedback
= gen7_end_transform_feedback
;
272 functions
->PauseTransformFeedback
= gen7_pause_transform_feedback
;
273 functions
->ResumeTransformFeedback
= gen7_resume_transform_feedback
;
275 functions
->BeginTransformFeedback
= brw_begin_transform_feedback
;
276 functions
->EndTransformFeedback
= brw_end_transform_feedback
;
280 functions
->GetSamplePosition
= gen6_get_sample_position
;
284 brw_initialize_context_constants(struct brw_context
*brw
)
286 struct gl_context
*ctx
= &brw
->ctx
;
288 unsigned max_samplers
=
289 brw
->gen
>= 8 || brw
->is_haswell
? BRW_MAX_TEX_UNIT
: 16;
291 ctx
->Const
.QueryCounterBits
.Timestamp
= 36;
293 ctx
->Const
.StripTextureBorder
= true;
295 ctx
->Const
.MaxDualSourceDrawBuffers
= 1;
296 ctx
->Const
.MaxDrawBuffers
= BRW_MAX_DRAW_BUFFERS
;
297 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxTextureImageUnits
= max_samplers
;
298 ctx
->Const
.MaxTextureCoordUnits
= 8; /* Mesa limit */
299 ctx
->Const
.MaxTextureUnits
=
300 MIN2(ctx
->Const
.MaxTextureCoordUnits
,
301 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxTextureImageUnits
);
302 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTextureImageUnits
= max_samplers
;
304 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxTextureImageUnits
= max_samplers
;
306 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxTextureImageUnits
= 0;
307 if (_mesa_extension_override_enables
.ARB_compute_shader
) {
308 ctx
->Const
.Program
[MESA_SHADER_COMPUTE
].MaxTextureImageUnits
= BRW_MAX_TEX_UNIT
;
309 ctx
->Const
.MaxUniformBufferBindings
+= 12;
311 ctx
->Const
.Program
[MESA_SHADER_COMPUTE
].MaxTextureImageUnits
= 0;
313 ctx
->Const
.MaxCombinedTextureImageUnits
=
314 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTextureImageUnits
+
315 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxTextureImageUnits
+
316 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxTextureImageUnits
+
317 ctx
->Const
.Program
[MESA_SHADER_COMPUTE
].MaxTextureImageUnits
;
319 ctx
->Const
.MaxTextureLevels
= 14; /* 8192 */
320 if (ctx
->Const
.MaxTextureLevels
> MAX_TEXTURE_LEVELS
)
321 ctx
->Const
.MaxTextureLevels
= MAX_TEXTURE_LEVELS
;
322 ctx
->Const
.Max3DTextureLevels
= 12; /* 2048 */
323 ctx
->Const
.MaxCubeTextureLevels
= 14; /* 8192 */
324 ctx
->Const
.MaxTextureMbytes
= 1536;
327 ctx
->Const
.MaxArrayTextureLayers
= 2048;
329 ctx
->Const
.MaxArrayTextureLayers
= 512;
331 ctx
->Const
.MaxTextureRectSize
= 1 << 12;
333 ctx
->Const
.MaxTextureMaxAnisotropy
= 16.0;
335 ctx
->Const
.MaxRenderbufferSize
= 8192;
337 /* Hardware only supports a limited number of transform feedback buffers.
338 * So we need to override the Mesa default (which is based only on software
341 ctx
->Const
.MaxTransformFeedbackBuffers
= BRW_MAX_SOL_BUFFERS
;
343 /* On Gen6, in the worst case, we use up one binding table entry per
344 * transform feedback component (see comments above the definition of
345 * BRW_MAX_SOL_BINDINGS, in brw_context.h), so we need to advertise a value
346 * for MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS equal to
347 * BRW_MAX_SOL_BINDINGS.
349 * In "separate components" mode, we need to divide this value by
350 * BRW_MAX_SOL_BUFFERS, so that the total number of binding table entries
351 * used up by all buffers will not exceed BRW_MAX_SOL_BINDINGS.
353 ctx
->Const
.MaxTransformFeedbackInterleavedComponents
= BRW_MAX_SOL_BINDINGS
;
354 ctx
->Const
.MaxTransformFeedbackSeparateComponents
=
355 BRW_MAX_SOL_BINDINGS
/ BRW_MAX_SOL_BUFFERS
;
357 ctx
->Const
.AlwaysUseGetTransformFeedbackVertexCount
= true;
360 const int *msaa_modes
= intel_supported_msaa_modes(brw
->intelScreen
);
361 const int clamp_max_samples
=
362 driQueryOptioni(&brw
->optionCache
, "clamp_max_samples");
364 if (clamp_max_samples
< 0) {
365 max_samples
= msaa_modes
[0];
367 /* Select the largest supported MSAA mode that does not exceed
371 for (int i
= 0; msaa_modes
[i
] != 0; ++i
) {
372 if (msaa_modes
[i
] <= clamp_max_samples
) {
373 max_samples
= msaa_modes
[i
];
379 ctx
->Const
.MaxSamples
= max_samples
;
380 ctx
->Const
.MaxColorTextureSamples
= max_samples
;
381 ctx
->Const
.MaxDepthTextureSamples
= max_samples
;
382 ctx
->Const
.MaxIntegerSamples
= max_samples
;
385 ctx
->Const
.MaxProgramTextureGatherComponents
= 4;
386 else if (brw
->gen
== 6)
387 ctx
->Const
.MaxProgramTextureGatherComponents
= 1;
389 ctx
->Const
.MinLineWidth
= 1.0;
390 ctx
->Const
.MinLineWidthAA
= 1.0;
391 ctx
->Const
.MaxLineWidth
= 5.0;
392 ctx
->Const
.MaxLineWidthAA
= 5.0;
393 ctx
->Const
.LineWidthGranularity
= 0.5;
395 ctx
->Const
.MinPointSize
= 1.0;
396 ctx
->Const
.MinPointSizeAA
= 1.0;
397 ctx
->Const
.MaxPointSize
= 255.0;
398 ctx
->Const
.MaxPointSizeAA
= 255.0;
399 ctx
->Const
.PointSizeGranularity
= 1.0;
401 if (brw
->gen
>= 5 || brw
->is_g4x
)
402 ctx
->Const
.MaxClipPlanes
= 8;
404 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeInstructions
= 16 * 1024;
405 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxAluInstructions
= 0;
406 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTexInstructions
= 0;
407 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTexIndirections
= 0;
408 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeAluInstructions
= 0;
409 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeTexInstructions
= 0;
410 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeTexIndirections
= 0;
411 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeAttribs
= 16;
412 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeTemps
= 256;
413 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeAddressRegs
= 1;
414 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeParameters
= 1024;
415 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxEnvParams
=
416 MIN2(ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeParameters
,
417 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxEnvParams
);
419 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeInstructions
= 1024;
420 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeAluInstructions
= 1024;
421 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeTexInstructions
= 1024;
422 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeTexIndirections
= 1024;
423 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeAttribs
= 12;
424 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeTemps
= 256;
425 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeAddressRegs
= 0;
426 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeParameters
= 1024;
427 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxEnvParams
=
428 MIN2(ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeParameters
,
429 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxEnvParams
);
431 /* Fragment shaders use real, 32-bit twos-complement integers for all
434 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].LowInt
.RangeMin
= 31;
435 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].LowInt
.RangeMax
= 30;
436 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].LowInt
.Precision
= 0;
437 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].HighInt
= ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].LowInt
;
438 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MediumInt
= ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].LowInt
;
441 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxAtomicCounters
= MAX_ATOMIC_COUNTERS
;
442 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxAtomicCounters
= MAX_ATOMIC_COUNTERS
;
443 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxAtomicCounters
= MAX_ATOMIC_COUNTERS
;
444 ctx
->Const
.Program
[MESA_SHADER_COMPUTE
].MaxAtomicCounters
= MAX_ATOMIC_COUNTERS
;
445 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxAtomicBuffers
= BRW_MAX_ABO
;
446 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxAtomicBuffers
= BRW_MAX_ABO
;
447 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxAtomicBuffers
= BRW_MAX_ABO
;
448 ctx
->Const
.Program
[MESA_SHADER_COMPUTE
].MaxAtomicBuffers
= BRW_MAX_ABO
;
449 ctx
->Const
.MaxCombinedAtomicBuffers
= 3 * BRW_MAX_ABO
;
452 /* Gen6 converts quads to polygon in beginning of 3D pipeline,
453 * but we're not sure how it's actually done for vertex order,
454 * that affect provoking vertex decision. Always use last vertex
455 * convention for quad primitive which works as expected for now.
458 ctx
->Const
.QuadsFollowProvokingVertexConvention
= false;
460 ctx
->Const
.NativeIntegers
= true;
461 ctx
->Const
.UniformBooleanTrue
= 1;
463 /* From the gen4 PRM, volume 4 page 127:
465 * "For SURFTYPE_BUFFER non-rendertarget surfaces, this field specifies
466 * the base address of the first element of the surface, computed in
467 * software by adding the surface base address to the byte offset of
468 * the element in the buffer."
470 * However, unaligned accesses are slower, so enforce buffer alignment.
472 ctx
->Const
.UniformBufferOffsetAlignment
= 16;
473 ctx
->Const
.TextureBufferOffsetAlignment
= 16;
476 ctx
->Const
.MaxVarying
= 32;
477 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxOutputComponents
= 128;
478 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxInputComponents
= 64;
479 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxOutputComponents
= 128;
480 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxInputComponents
= 128;
483 /* We want the GLSL compiler to emit code that uses condition codes */
484 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
485 ctx
->Const
.ShaderCompilerOptions
[i
].MaxIfDepth
= brw
->gen
< 6 ? 16 : UINT_MAX
;
486 ctx
->Const
.ShaderCompilerOptions
[i
].EmitCondCodes
= true;
487 ctx
->Const
.ShaderCompilerOptions
[i
].EmitNoNoise
= true;
488 ctx
->Const
.ShaderCompilerOptions
[i
].EmitNoMainReturn
= true;
489 ctx
->Const
.ShaderCompilerOptions
[i
].EmitNoIndirectInput
= true;
490 ctx
->Const
.ShaderCompilerOptions
[i
].EmitNoIndirectOutput
=
491 (i
== MESA_SHADER_FRAGMENT
);
492 ctx
->Const
.ShaderCompilerOptions
[i
].EmitNoIndirectTemp
=
493 (i
== MESA_SHADER_FRAGMENT
);
494 ctx
->Const
.ShaderCompilerOptions
[i
].EmitNoIndirectUniform
= false;
495 ctx
->Const
.ShaderCompilerOptions
[i
].LowerClipDistance
= true;
498 ctx
->Const
.ShaderCompilerOptions
[MESA_SHADER_VERTEX
].OptimizeForAOS
= true;
499 ctx
->Const
.ShaderCompilerOptions
[MESA_SHADER_GEOMETRY
].OptimizeForAOS
= true;
501 /* ARB_viewport_array */
502 if (brw
->gen
>= 7 && ctx
->API
== API_OPENGL_CORE
) {
503 ctx
->Const
.MaxViewports
= GEN7_NUM_VIEWPORTS
;
504 ctx
->Const
.ViewportSubpixelBits
= 0;
506 /* Cast to float before negating becuase MaxViewportWidth is unsigned.
508 ctx
->Const
.ViewportBounds
.Min
= -(float)ctx
->Const
.MaxViewportWidth
;
509 ctx
->Const
.ViewportBounds
.Max
= ctx
->Const
.MaxViewportWidth
;
512 /* ARB_gpu_shader5 */
514 ctx
->Const
.MaxVertexStreams
= MIN2(4, MAX_VERTEX_STREAMS
);
518 * Process driconf (drirc) options, setting appropriate context flags.
520 * intelInitExtensions still pokes at optionCache directly, in order to
521 * avoid advertising various extensions. No flags are set, so it makes
522 * sense to continue doing that there.
525 brw_process_driconf_options(struct brw_context
*brw
)
527 struct gl_context
*ctx
= &brw
->ctx
;
529 driOptionCache
*options
= &brw
->optionCache
;
530 driParseConfigFiles(options
, &brw
->intelScreen
->optionCache
,
531 brw
->driContext
->driScreenPriv
->myNum
, "i965");
533 int bo_reuse_mode
= driQueryOptioni(options
, "bo_reuse");
534 switch (bo_reuse_mode
) {
535 case DRI_CONF_BO_REUSE_DISABLED
:
537 case DRI_CONF_BO_REUSE_ALL
:
538 intel_bufmgr_gem_enable_reuse(brw
->bufmgr
);
542 if (!driQueryOptionb(options
, "hiz")) {
543 brw
->has_hiz
= false;
544 /* On gen6, you can only do separate stencil with HIZ. */
546 brw
->has_separate_stencil
= false;
549 if (driQueryOptionb(options
, "always_flush_batch")) {
550 fprintf(stderr
, "flushing batchbuffer before/after each draw call\n");
551 brw
->always_flush_batch
= true;
554 if (driQueryOptionb(options
, "always_flush_cache")) {
555 fprintf(stderr
, "flushing GPU caches before/after each draw call\n");
556 brw
->always_flush_cache
= true;
559 if (driQueryOptionb(options
, "disable_throttling")) {
560 fprintf(stderr
, "disabling flush throttling\n");
561 brw
->disable_throttling
= true;
564 brw
->disable_derivative_optimization
=
565 driQueryOptionb(&brw
->optionCache
, "disable_derivative_optimization");
567 brw
->precompile
= driQueryOptionb(&brw
->optionCache
, "shader_precompile");
569 ctx
->Const
.ForceGLSLExtensionsWarn
=
570 driQueryOptionb(options
, "force_glsl_extensions_warn");
572 ctx
->Const
.DisableGLSLLineContinuations
=
573 driQueryOptionb(options
, "disable_glsl_line_continuations");
575 ctx
->Const
.AllowGLSLExtensionDirectiveMidShader
=
576 driQueryOptionb(options
, "allow_glsl_extension_directive_midshader");
580 brwCreateContext(gl_api api
,
581 const struct gl_config
*mesaVis
,
582 __DRIcontext
*driContextPriv
,
583 unsigned major_version
,
584 unsigned minor_version
,
587 unsigned *dri_ctx_error
,
588 void *sharedContextPrivate
)
590 __DRIscreen
*sPriv
= driContextPriv
->driScreenPriv
;
591 struct gl_context
*shareCtx
= (struct gl_context
*) sharedContextPrivate
;
592 struct intel_screen
*screen
= sPriv
->driverPrivate
;
593 const struct brw_device_info
*devinfo
= screen
->devinfo
;
594 struct dd_function_table functions
;
596 /* Only allow the __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS flag if the kernel
597 * provides us with context reset notifications.
599 uint32_t allowed_flags
= __DRI_CTX_FLAG_DEBUG
600 | __DRI_CTX_FLAG_FORWARD_COMPATIBLE
;
602 if (screen
->has_context_reset_notification
)
603 allowed_flags
|= __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS
;
605 if (flags
& ~allowed_flags
) {
606 *dri_ctx_error
= __DRI_CTX_ERROR_UNKNOWN_FLAG
;
610 struct brw_context
*brw
= rzalloc(NULL
, struct brw_context
);
612 fprintf(stderr
, "%s: failed to alloc context\n", __FUNCTION__
);
613 *dri_ctx_error
= __DRI_CTX_ERROR_NO_MEMORY
;
617 driContextPriv
->driverPrivate
= brw
;
618 brw
->driContext
= driContextPriv
;
619 brw
->intelScreen
= screen
;
620 brw
->bufmgr
= screen
->bufmgr
;
622 brw
->gen
= devinfo
->gen
;
623 brw
->gt
= devinfo
->gt
;
624 brw
->is_g4x
= devinfo
->is_g4x
;
625 brw
->is_baytrail
= devinfo
->is_baytrail
;
626 brw
->is_haswell
= devinfo
->is_haswell
;
627 brw
->is_cherryview
= devinfo
->is_cherryview
;
628 brw
->has_llc
= devinfo
->has_llc
;
629 brw
->has_hiz
= devinfo
->has_hiz_and_separate_stencil
;
630 brw
->has_separate_stencil
= devinfo
->has_hiz_and_separate_stencil
;
631 brw
->has_pln
= devinfo
->has_pln
;
632 brw
->has_compr4
= devinfo
->has_compr4
;
633 brw
->has_surface_tile_offset
= devinfo
->has_surface_tile_offset
;
634 brw
->has_negative_rhw_bug
= devinfo
->has_negative_rhw_bug
;
635 brw
->needs_unlit_centroid_workaround
=
636 devinfo
->needs_unlit_centroid_workaround
;
638 brw
->must_use_separate_stencil
= screen
->hw_must_use_separate_stencil
;
639 brw
->has_swizzling
= screen
->hw_has_swizzling
;
641 brw
->vs
.base
.stage
= MESA_SHADER_VERTEX
;
642 brw
->gs
.base
.stage
= MESA_SHADER_GEOMETRY
;
643 brw
->wm
.base
.stage
= MESA_SHADER_FRAGMENT
;
645 gen8_init_vtable_surface_functions(brw
);
646 brw
->vtbl
.emit_depth_stencil_hiz
= gen8_emit_depth_stencil_hiz
;
647 } else if (brw
->gen
>= 7) {
648 gen7_init_vtable_surface_functions(brw
);
649 brw
->vtbl
.emit_depth_stencil_hiz
= gen7_emit_depth_stencil_hiz
;
651 gen4_init_vtable_surface_functions(brw
);
652 brw
->vtbl
.emit_depth_stencil_hiz
= brw_emit_depth_stencil_hiz
;
655 brw_init_driver_functions(brw
, &functions
);
658 functions
.GetGraphicsResetStatus
= brw_get_graphics_reset_status
;
660 struct gl_context
*ctx
= &brw
->ctx
;
662 if (!_mesa_initialize_context(ctx
, api
, mesaVis
, shareCtx
, &functions
)) {
663 *dri_ctx_error
= __DRI_CTX_ERROR_NO_MEMORY
;
664 fprintf(stderr
, "%s: failed to init mesa context\n", __FUNCTION__
);
665 intelDestroyContext(driContextPriv
);
669 driContextSetFlags(ctx
, flags
);
671 /* Initialize the software rasterizer and helper modules.
673 * As of GL 3.1 core, the gen4+ driver doesn't need the swrast context for
674 * software fallbacks (which we have to support on legacy GL to do weird
675 * glDrawPixels(), glBitmap(), and other functions).
677 if (api
!= API_OPENGL_CORE
&& api
!= API_OPENGLES2
) {
678 _swrast_CreateContext(ctx
);
681 _vbo_CreateContext(ctx
);
682 if (ctx
->swrast_context
) {
683 _tnl_CreateContext(ctx
);
684 TNL_CONTEXT(ctx
)->Driver
.RunPipeline
= _tnl_run_pipeline
;
685 _swsetup_CreateContext(ctx
);
687 /* Configure swrast to match hardware characteristics: */
688 _swrast_allow_pixel_fog(ctx
, false);
689 _swrast_allow_vertex_fog(ctx
, true);
692 _mesa_meta_init(ctx
);
694 brw_process_driconf_options(brw
);
695 brw_process_intel_debug_variable(brw
);
696 brw_initialize_context_constants(brw
);
698 ctx
->Const
.ResetStrategy
= notify_reset
699 ? GL_LOSE_CONTEXT_ON_RESET_ARB
: GL_NO_RESET_NOTIFICATION_ARB
;
701 /* Reinitialize the context point state. It depends on ctx->Const values. */
702 _mesa_init_point(ctx
);
706 intel_batchbuffer_init(brw
);
709 /* Create a new hardware context. Using a hardware context means that
710 * our GPU state will be saved/restored on context switch, allowing us
711 * to assume that the GPU is in the same state we left it in.
713 * This is required for transform feedback buffer offsets, query objects,
714 * and also allows us to reduce how much state we have to emit.
716 brw
->hw_ctx
= drm_intel_gem_context_create(brw
->bufmgr
);
719 fprintf(stderr
, "Gen6+ requires Kernel 3.6 or later.\n");
720 intelDestroyContext(driContextPriv
);
727 intelInitExtensions(ctx
);
729 brw_init_surface_formats(brw
);
731 brw
->max_vs_threads
= devinfo
->max_vs_threads
;
732 brw
->max_gs_threads
= devinfo
->max_gs_threads
;
733 brw
->max_wm_threads
= devinfo
->max_wm_threads
;
734 brw
->urb
.size
= devinfo
->urb
.size
;
735 brw
->urb
.min_vs_entries
= devinfo
->urb
.min_vs_entries
;
736 brw
->urb
.max_vs_entries
= devinfo
->urb
.max_vs_entries
;
737 brw
->urb
.max_gs_entries
= devinfo
->urb
.max_gs_entries
;
739 /* Estimate the size of the mappable aperture into the GTT. There's an
740 * ioctl to get the whole GTT size, but not one to get the mappable subset.
741 * It turns out it's basically always 256MB, though some ancient hardware
744 uint32_t gtt_size
= 256 * 1024 * 1024;
746 /* We don't want to map two objects such that a memcpy between them would
747 * just fault one mapping in and then the other over and over forever. So
748 * we would need to divide the GTT size by 2. Additionally, some GTT is
749 * taken up by things like the framebuffer and the ringbuffer and such, so
750 * be more conservative.
752 brw
->max_gtt_map_object_size
= gtt_size
/ 4;
755 brw
->urb
.gen6_gs_previously_active
= false;
757 brw
->prim_restart
.in_progress
= false;
758 brw
->prim_restart
.enable_cut_index
= false;
759 brw
->gs
.enabled
= false;
760 brw
->sf
.viewport_transform_enable
= true;
762 ctx
->VertexProgram
._MaintainTnlProgram
= true;
763 ctx
->FragmentProgram
._MaintainTexEnvProgram
= true;
765 brw_draw_init( brw
);
767 if ((flags
& __DRI_CTX_FLAG_DEBUG
) != 0) {
768 /* Turn on some extra GL_ARB_debug_output generation. */
769 brw
->perf_debug
= true;
772 if ((flags
& __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS
) != 0)
773 ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_ROBUST_ACCESS_BIT_ARB
;
775 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
)
776 brw_init_shader_time(brw
);
778 _mesa_compute_version(ctx
);
780 _mesa_initialize_dispatch_tables(ctx
);
781 _mesa_initialize_vbo_vtxfmt(ctx
);
783 if (ctx
->Extensions
.AMD_performance_monitor
) {
784 brw_init_performance_monitors(brw
);
787 vbo_use_buffer_objects(ctx
);
788 vbo_always_unmap_buffers(ctx
);
794 intelDestroyContext(__DRIcontext
* driContextPriv
)
796 struct brw_context
*brw
=
797 (struct brw_context
*) driContextPriv
->driverPrivate
;
798 struct gl_context
*ctx
= &brw
->ctx
;
800 assert(brw
); /* should never be null */
804 /* Dump a final BMP in case the application doesn't call SwapBuffers */
805 if (INTEL_DEBUG
& DEBUG_AUB
) {
806 intel_batchbuffer_flush(brw
);
807 aub_dump_bmp(&brw
->ctx
);
810 _mesa_meta_free(&brw
->ctx
);
812 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
) {
813 /* Force a report. */
814 brw
->shader_time
.report_time
= 0;
816 brw_collect_and_report_shader_time(brw
);
817 brw_destroy_shader_time(brw
);
820 brw_destroy_state(brw
);
821 brw_draw_destroy(brw
);
823 drm_intel_bo_unreference(brw
->curbe
.curbe_bo
);
825 drm_intel_gem_context_destroy(brw
->hw_ctx
);
827 if (ctx
->swrast_context
) {
828 _swsetup_DestroyContext(&brw
->ctx
);
829 _tnl_DestroyContext(&brw
->ctx
);
831 _vbo_DestroyContext(&brw
->ctx
);
833 if (ctx
->swrast_context
)
834 _swrast_DestroyContext(&brw
->ctx
);
836 intel_batchbuffer_free(brw
);
838 drm_intel_bo_unreference(brw
->first_post_swapbuffers_batch
);
839 brw
->first_post_swapbuffers_batch
= NULL
;
841 driDestroyOptionCache(&brw
->optionCache
);
843 /* free the Mesa context */
844 _mesa_free_context_data(&brw
->ctx
);
847 driContextPriv
->driverPrivate
= NULL
;
851 intelUnbindContext(__DRIcontext
* driContextPriv
)
853 /* Unset current context and dispath table */
854 _mesa_make_current(NULL
, NULL
, NULL
);
860 * Fixes up the context for GLES23 with our default-to-sRGB-capable behavior
861 * on window system framebuffers.
863 * Desktop GL is fairly reasonable in its handling of sRGB: You can ask if
864 * your renderbuffer can do sRGB encode, and you can flip a switch that does
865 * sRGB encode if the renderbuffer can handle it. You can ask specifically
866 * for a visual where you're guaranteed to be capable, but it turns out that
867 * everyone just makes all their ARGB8888 visuals capable and doesn't offer
868 * incapable ones, becuase there's no difference between the two in resources
869 * used. Applications thus get built that accidentally rely on the default
870 * visual choice being sRGB, so we make ours sRGB capable. Everything sounds
873 * But for GLES2/3, they decided that it was silly to not turn on sRGB encode
874 * for sRGB renderbuffers you made with the GL_EXT_texture_sRGB equivalent.
875 * So they removed the enable knob and made it "if the renderbuffer is sRGB
876 * capable, do sRGB encode". Then, for your window system renderbuffers, you
877 * can ask for sRGB visuals and get sRGB encode, or not ask for sRGB visuals
878 * and get no sRGB encode (assuming that both kinds of visual are available).
879 * Thus our choice to support sRGB by default on our visuals for desktop would
880 * result in broken rendering of GLES apps that aren't expecting sRGB encode.
882 * Unfortunately, renderbuffer setup happens before a context is created. So
883 * in intel_screen.c we always set up sRGB, and here, if you're a GLES2/3
884 * context (without an sRGB visual, though we don't have sRGB visuals exposed
885 * yet), we go turn that back off before anyone finds out.
888 intel_gles3_srgb_workaround(struct brw_context
*brw
,
889 struct gl_framebuffer
*fb
)
891 struct gl_context
*ctx
= &brw
->ctx
;
893 if (_mesa_is_desktop_gl(ctx
) || !fb
->Visual
.sRGBCapable
)
896 /* Some day when we support the sRGB capable bit on visuals available for
897 * GLES, we'll need to respect that and not disable things here.
899 fb
->Visual
.sRGBCapable
= false;
900 for (int i
= 0; i
< BUFFER_COUNT
; i
++) {
901 if (fb
->Attachment
[i
].Renderbuffer
&&
902 fb
->Attachment
[i
].Renderbuffer
->Format
== MESA_FORMAT_B8G8R8A8_SRGB
) {
903 fb
->Attachment
[i
].Renderbuffer
->Format
= MESA_FORMAT_B8G8R8A8_UNORM
;
909 intelMakeCurrent(__DRIcontext
* driContextPriv
,
910 __DRIdrawable
* driDrawPriv
,
911 __DRIdrawable
* driReadPriv
)
913 struct brw_context
*brw
;
914 GET_CURRENT_CONTEXT(curCtx
);
917 brw
= (struct brw_context
*) driContextPriv
->driverPrivate
;
921 /* According to the glXMakeCurrent() man page: "Pending commands to
922 * the previous context, if any, are flushed before it is released."
923 * But only flush if we're actually changing contexts.
925 if (brw_context(curCtx
) && brw_context(curCtx
) != brw
) {
929 if (driContextPriv
) {
930 struct gl_context
*ctx
= &brw
->ctx
;
931 struct gl_framebuffer
*fb
, *readFb
;
933 if (driDrawPriv
== NULL
&& driReadPriv
== NULL
) {
934 fb
= _mesa_get_incomplete_framebuffer();
935 readFb
= _mesa_get_incomplete_framebuffer();
937 fb
= driDrawPriv
->driverPrivate
;
938 readFb
= driReadPriv
->driverPrivate
;
939 driContextPriv
->dri2
.draw_stamp
= driDrawPriv
->dri2
.stamp
- 1;
940 driContextPriv
->dri2
.read_stamp
= driReadPriv
->dri2
.stamp
- 1;
943 /* The sRGB workaround changes the renderbuffer's format. We must change
944 * the format before the renderbuffer's miptree get's allocated, otherwise
945 * the formats of the renderbuffer and its miptree will differ.
947 intel_gles3_srgb_workaround(brw
, fb
);
948 intel_gles3_srgb_workaround(brw
, readFb
);
950 /* If the context viewport hasn't been initialized, force a call out to
951 * the loader to get buffers so we have a drawable size for the initial
953 if (!brw
->ctx
.ViewportInitialized
)
954 intel_prepare_render(brw
);
956 _mesa_make_current(ctx
, fb
, readFb
);
958 _mesa_make_current(NULL
, NULL
, NULL
);
965 intel_resolve_for_dri2_flush(struct brw_context
*brw
,
966 __DRIdrawable
*drawable
)
969 /* MSAA and fast color clear are not supported, so don't waste time
970 * checking whether a resolve is needed.
975 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
976 struct intel_renderbuffer
*rb
;
978 /* Usually, only the back buffer will need to be downsampled. However,
979 * the front buffer will also need it if the user has rendered into it.
981 static const gl_buffer_index buffers
[2] = {
986 for (int i
= 0; i
< 2; ++i
) {
987 rb
= intel_get_renderbuffer(fb
, buffers
[i
]);
988 if (rb
== NULL
|| rb
->mt
== NULL
)
990 if (rb
->mt
->num_samples
<= 1)
991 intel_miptree_resolve_color(brw
, rb
->mt
);
993 intel_renderbuffer_downsample(brw
, rb
);
998 intel_bits_per_pixel(const struct intel_renderbuffer
*rb
)
1000 return _mesa_get_format_bytes(intel_rb_format(rb
)) * 8;
1004 intel_query_dri2_buffers(struct brw_context
*brw
,
1005 __DRIdrawable
*drawable
,
1006 __DRIbuffer
**buffers
,
1010 intel_process_dri2_buffer(struct brw_context
*brw
,
1011 __DRIdrawable
*drawable
,
1012 __DRIbuffer
*buffer
,
1013 struct intel_renderbuffer
*rb
,
1014 const char *buffer_name
);
1017 intel_update_image_buffers(struct brw_context
*brw
, __DRIdrawable
*drawable
);
1020 intel_update_dri2_buffers(struct brw_context
*brw
, __DRIdrawable
*drawable
)
1022 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
1023 struct intel_renderbuffer
*rb
;
1024 __DRIbuffer
*buffers
= NULL
;
1026 const char *region_name
;
1028 /* Set this up front, so that in case our buffers get invalidated
1029 * while we're getting new buffers, we don't clobber the stamp and
1030 * thus ignore the invalidate. */
1031 drawable
->lastStamp
= drawable
->dri2
.stamp
;
1033 if (unlikely(INTEL_DEBUG
& DEBUG_DRI
))
1034 fprintf(stderr
, "enter %s, drawable %p\n", __func__
, drawable
);
1036 intel_query_dri2_buffers(brw
, drawable
, &buffers
, &count
);
1038 if (buffers
== NULL
)
1041 for (i
= 0; i
< count
; i
++) {
1042 switch (buffers
[i
].attachment
) {
1043 case __DRI_BUFFER_FRONT_LEFT
:
1044 rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
1045 region_name
= "dri2 front buffer";
1048 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
1049 rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
1050 region_name
= "dri2 fake front buffer";
1053 case __DRI_BUFFER_BACK_LEFT
:
1054 rb
= intel_get_renderbuffer(fb
, BUFFER_BACK_LEFT
);
1055 region_name
= "dri2 back buffer";
1058 case __DRI_BUFFER_DEPTH
:
1059 case __DRI_BUFFER_HIZ
:
1060 case __DRI_BUFFER_DEPTH_STENCIL
:
1061 case __DRI_BUFFER_STENCIL
:
1062 case __DRI_BUFFER_ACCUM
:
1065 "unhandled buffer attach event, attachment type %d\n",
1066 buffers
[i
].attachment
);
1070 intel_process_dri2_buffer(brw
, drawable
, &buffers
[i
], rb
, region_name
);
1076 intel_update_renderbuffers(__DRIcontext
*context
, __DRIdrawable
*drawable
)
1078 struct brw_context
*brw
= context
->driverPrivate
;
1079 __DRIscreen
*screen
= brw
->intelScreen
->driScrnPriv
;
1081 /* Set this up front, so that in case our buffers get invalidated
1082 * while we're getting new buffers, we don't clobber the stamp and
1083 * thus ignore the invalidate. */
1084 drawable
->lastStamp
= drawable
->dri2
.stamp
;
1086 if (unlikely(INTEL_DEBUG
& DEBUG_DRI
))
1087 fprintf(stderr
, "enter %s, drawable %p\n", __func__
, drawable
);
1089 if (screen
->image
.loader
)
1090 intel_update_image_buffers(brw
, drawable
);
1092 intel_update_dri2_buffers(brw
, drawable
);
1094 driUpdateFramebufferSize(&brw
->ctx
, drawable
);
1098 * intel_prepare_render should be called anywhere that curent read/drawbuffer
1099 * state is required.
1102 intel_prepare_render(struct brw_context
*brw
)
1104 struct gl_context
*ctx
= &brw
->ctx
;
1105 __DRIcontext
*driContext
= brw
->driContext
;
1106 __DRIdrawable
*drawable
;
1108 drawable
= driContext
->driDrawablePriv
;
1109 if (drawable
&& drawable
->dri2
.stamp
!= driContext
->dri2
.draw_stamp
) {
1110 if (drawable
->lastStamp
!= drawable
->dri2
.stamp
)
1111 intel_update_renderbuffers(driContext
, drawable
);
1112 driContext
->dri2
.draw_stamp
= drawable
->dri2
.stamp
;
1115 drawable
= driContext
->driReadablePriv
;
1116 if (drawable
&& drawable
->dri2
.stamp
!= driContext
->dri2
.read_stamp
) {
1117 if (drawable
->lastStamp
!= drawable
->dri2
.stamp
)
1118 intel_update_renderbuffers(driContext
, drawable
);
1119 driContext
->dri2
.read_stamp
= drawable
->dri2
.stamp
;
1122 /* If we're currently rendering to the front buffer, the rendering
1123 * that will happen next will probably dirty the front buffer. So
1124 * mark it as dirty here.
1126 if (brw_is_front_buffer_drawing(ctx
->DrawBuffer
))
1127 brw
->front_buffer_dirty
= true;
1129 /* Wait for the swapbuffers before the one we just emitted, so we
1130 * don't get too many swaps outstanding for apps that are GPU-heavy
1131 * but not CPU-heavy.
1133 * We're using intelDRI2Flush (called from the loader before
1134 * swapbuffer) and glFlush (for front buffer rendering) as the
1135 * indicator that a frame is done and then throttle when we get
1136 * here as we prepare to render the next frame. At this point for
1137 * round trips for swap/copy and getting new buffers are done and
1138 * we'll spend less time waiting on the GPU.
1140 * Unfortunately, we don't have a handle to the batch containing
1141 * the swap, and getting our hands on that doesn't seem worth it,
1142 * so we just us the first batch we emitted after the last swap.
1144 if (brw
->need_throttle
&& brw
->first_post_swapbuffers_batch
) {
1145 if (!brw
->disable_throttling
)
1146 drm_intel_bo_wait_rendering(brw
->first_post_swapbuffers_batch
);
1147 drm_intel_bo_unreference(brw
->first_post_swapbuffers_batch
);
1148 brw
->first_post_swapbuffers_batch
= NULL
;
1149 brw
->need_throttle
= false;
1154 * \brief Query DRI2 to obtain a DRIdrawable's buffers.
1156 * To determine which DRI buffers to request, examine the renderbuffers
1157 * attached to the drawable's framebuffer. Then request the buffers with
1158 * DRI2GetBuffers() or DRI2GetBuffersWithFormat().
1160 * This is called from intel_update_renderbuffers().
1162 * \param drawable Drawable whose buffers are queried.
1163 * \param buffers [out] List of buffers returned by DRI2 query.
1164 * \param buffer_count [out] Number of buffers returned.
1166 * \see intel_update_renderbuffers()
1167 * \see DRI2GetBuffers()
1168 * \see DRI2GetBuffersWithFormat()
1171 intel_query_dri2_buffers(struct brw_context
*brw
,
1172 __DRIdrawable
*drawable
,
1173 __DRIbuffer
**buffers
,
1176 __DRIscreen
*screen
= brw
->intelScreen
->driScrnPriv
;
1177 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
1179 unsigned attachments
[8];
1181 struct intel_renderbuffer
*front_rb
;
1182 struct intel_renderbuffer
*back_rb
;
1184 front_rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
1185 back_rb
= intel_get_renderbuffer(fb
, BUFFER_BACK_LEFT
);
1187 memset(attachments
, 0, sizeof(attachments
));
1188 if ((brw_is_front_buffer_drawing(fb
) ||
1189 brw_is_front_buffer_reading(fb
) ||
1190 !back_rb
) && front_rb
) {
1191 /* If a fake front buffer is in use, then querying for
1192 * __DRI_BUFFER_FRONT_LEFT will cause the server to copy the image from
1193 * the real front buffer to the fake front buffer. So before doing the
1194 * query, we need to make sure all the pending drawing has landed in the
1195 * real front buffer.
1197 intel_batchbuffer_flush(brw
);
1198 intel_flush_front(&brw
->ctx
);
1200 attachments
[i
++] = __DRI_BUFFER_FRONT_LEFT
;
1201 attachments
[i
++] = intel_bits_per_pixel(front_rb
);
1202 } else if (front_rb
&& brw
->front_buffer_dirty
) {
1203 /* We have pending front buffer rendering, but we aren't querying for a
1204 * front buffer. If the front buffer we have is a fake front buffer,
1205 * the X server is going to throw it away when it processes the query.
1206 * So before doing the query, make sure all the pending drawing has
1207 * landed in the real front buffer.
1209 intel_batchbuffer_flush(brw
);
1210 intel_flush_front(&brw
->ctx
);
1214 attachments
[i
++] = __DRI_BUFFER_BACK_LEFT
;
1215 attachments
[i
++] = intel_bits_per_pixel(back_rb
);
1218 assert(i
<= ARRAY_SIZE(attachments
));
1220 *buffers
= screen
->dri2
.loader
->getBuffersWithFormat(drawable
,
1225 drawable
->loaderPrivate
);
1229 * \brief Assign a DRI buffer's DRM region to a renderbuffer.
1231 * This is called from intel_update_renderbuffers().
1234 * DRI buffers whose attachment point is DRI2BufferStencil or
1235 * DRI2BufferDepthStencil are handled as special cases.
1237 * \param buffer_name is a human readable name, such as "dri2 front buffer",
1238 * that is passed to drm_intel_bo_gem_create_from_name().
1240 * \see intel_update_renderbuffers()
1243 intel_process_dri2_buffer(struct brw_context
*brw
,
1244 __DRIdrawable
*drawable
,
1245 __DRIbuffer
*buffer
,
1246 struct intel_renderbuffer
*rb
,
1247 const char *buffer_name
)
1249 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
1255 unsigned num_samples
= rb
->Base
.Base
.NumSamples
;
1257 /* We try to avoid closing and reopening the same BO name, because the first
1258 * use of a mapping of the buffer involves a bunch of page faulting which is
1259 * moderately expensive.
1261 struct intel_mipmap_tree
*last_mt
;
1262 if (num_samples
== 0)
1265 last_mt
= rb
->singlesample_mt
;
1267 uint32_t old_name
= 0;
1269 /* The bo already has a name because the miptree was created by a
1270 * previous call to intel_process_dri2_buffer(). If a bo already has a
1271 * name, then drm_intel_bo_flink() is a low-cost getter. It does not
1272 * create a new name.
1274 drm_intel_bo_flink(last_mt
->bo
, &old_name
);
1277 if (old_name
== buffer
->name
)
1280 if (unlikely(INTEL_DEBUG
& DEBUG_DRI
)) {
1282 "attaching buffer %d, at %d, cpp %d, pitch %d\n",
1283 buffer
->name
, buffer
->attachment
,
1284 buffer
->cpp
, buffer
->pitch
);
1287 intel_miptree_release(&rb
->mt
);
1288 bo
= drm_intel_bo_gem_create_from_name(brw
->bufmgr
, buffer_name
,
1292 "Failed to open BO for returned DRI2 buffer "
1293 "(%dx%d, %s, named %d).\n"
1294 "This is likely a bug in the X Server that will lead to a "
1296 drawable
->w
, drawable
->h
, buffer_name
, buffer
->name
);
1300 intel_update_winsys_renderbuffer_miptree(brw
, rb
, bo
,
1301 drawable
->w
, drawable
->h
,
1304 if (brw_is_front_buffer_drawing(fb
) &&
1305 (buffer
->attachment
== __DRI_BUFFER_FRONT_LEFT
||
1306 buffer
->attachment
== __DRI_BUFFER_FAKE_FRONT_LEFT
) &&
1307 rb
->Base
.Base
.NumSamples
> 1) {
1308 intel_renderbuffer_upsample(brw
, rb
);
1313 drm_intel_bo_unreference(bo
);
1317 * \brief Query DRI image loader to obtain a DRIdrawable's buffers.
1319 * To determine which DRI buffers to request, examine the renderbuffers
1320 * attached to the drawable's framebuffer. Then request the buffers from
1323 * This is called from intel_update_renderbuffers().
1325 * \param drawable Drawable whose buffers are queried.
1326 * \param buffers [out] List of buffers returned by DRI2 query.
1327 * \param buffer_count [out] Number of buffers returned.
1329 * \see intel_update_renderbuffers()
1333 intel_update_image_buffer(struct brw_context
*intel
,
1334 __DRIdrawable
*drawable
,
1335 struct intel_renderbuffer
*rb
,
1337 enum __DRIimageBufferMask buffer_type
)
1339 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
1341 if (!rb
|| !buffer
->bo
)
1344 unsigned num_samples
= rb
->Base
.Base
.NumSamples
;
1346 /* Check and see if we're already bound to the right
1349 struct intel_mipmap_tree
*last_mt
;
1350 if (num_samples
== 0)
1353 last_mt
= rb
->singlesample_mt
;
1355 if (last_mt
&& last_mt
->bo
== buffer
->bo
)
1358 intel_update_winsys_renderbuffer_miptree(intel
, rb
, buffer
->bo
,
1359 buffer
->width
, buffer
->height
,
1362 if (brw_is_front_buffer_drawing(fb
) &&
1363 buffer_type
== __DRI_IMAGE_BUFFER_FRONT
&&
1364 rb
->Base
.Base
.NumSamples
> 1) {
1365 intel_renderbuffer_upsample(intel
, rb
);
1370 intel_update_image_buffers(struct brw_context
*brw
, __DRIdrawable
*drawable
)
1372 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
1373 __DRIscreen
*screen
= brw
->intelScreen
->driScrnPriv
;
1374 struct intel_renderbuffer
*front_rb
;
1375 struct intel_renderbuffer
*back_rb
;
1376 struct __DRIimageList images
;
1377 unsigned int format
;
1378 uint32_t buffer_mask
= 0;
1380 front_rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
1381 back_rb
= intel_get_renderbuffer(fb
, BUFFER_BACK_LEFT
);
1384 format
= intel_rb_format(back_rb
);
1386 format
= intel_rb_format(front_rb
);
1390 if (front_rb
&& (brw_is_front_buffer_drawing(fb
) ||
1391 brw_is_front_buffer_reading(fb
) || !back_rb
)) {
1392 buffer_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
1396 buffer_mask
|= __DRI_IMAGE_BUFFER_BACK
;
1398 (*screen
->image
.loader
->getBuffers
) (drawable
,
1399 driGLFormatToImageFormat(format
),
1400 &drawable
->dri2
.stamp
,
1401 drawable
->loaderPrivate
,
1405 if (images
.image_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
1406 drawable
->w
= images
.front
->width
;
1407 drawable
->h
= images
.front
->height
;
1408 intel_update_image_buffer(brw
,
1412 __DRI_IMAGE_BUFFER_FRONT
);
1414 if (images
.image_mask
& __DRI_IMAGE_BUFFER_BACK
) {
1415 drawable
->w
= images
.back
->width
;
1416 drawable
->h
= images
.back
->height
;
1417 intel_update_image_buffer(brw
,
1421 __DRI_IMAGE_BUFFER_BACK
);