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"
43 #include "main/texobj.h"
44 #include "main/framebuffer.h"
46 #include "vbo/vbo_context.h"
48 #include "drivers/common/driverfuncs.h"
49 #include "drivers/common/meta.h"
52 #include "brw_context.h"
53 #include "brw_defines.h"
54 #include "brw_compiler.h"
56 #include "brw_state.h"
58 #include "intel_batchbuffer.h"
59 #include "intel_buffer_objects.h"
60 #include "intel_buffers.h"
61 #include "intel_fbo.h"
62 #include "intel_mipmap_tree.h"
63 #include "intel_pixel.h"
64 #include "intel_image.h"
65 #include "intel_tex.h"
66 #include "intel_tex_obj.h"
68 #include "swrast_setup/swrast_setup.h"
70 #include "tnl/t_pipeline.h"
71 #include "util/ralloc.h"
72 #include "util/debug.h"
74 /***************************************
75 * Mesa's Driver Functions
76 ***************************************/
79 brw_query_samples_for_format(struct gl_context
*ctx
, GLenum target
,
80 GLenum internalFormat
, int samples
[16])
82 struct brw_context
*brw
= brw_context(ctx
);
110 assert(brw
->gen
< 6);
116 const char *const brw_vendor_string
= "Intel Open Source Technology Center";
119 brw_get_renderer_string(unsigned deviceID
)
122 static char buffer
[128];
126 #define CHIPSET(id, symbol, str) case id: chipset = str; break;
127 #include "pci_ids/i965_pci_ids.h"
129 chipset
= "Unknown Intel Chipset";
133 (void) driGetRendererString(buffer
, chipset
, 0);
137 static const GLubyte
*
138 intel_get_string(struct gl_context
* ctx
, GLenum name
)
140 const struct brw_context
*const brw
= brw_context(ctx
);
144 return (GLubyte
*) brw_vendor_string
;
148 (GLubyte
*) brw_get_renderer_string(brw
->intelScreen
->deviceID
);
156 intel_viewport(struct gl_context
*ctx
)
158 struct brw_context
*brw
= brw_context(ctx
);
159 __DRIcontext
*driContext
= brw
->driContext
;
161 if (_mesa_is_winsys_fbo(ctx
->DrawBuffer
)) {
162 if (driContext
->driDrawablePriv
)
163 dri2InvalidateDrawable(driContext
->driDrawablePriv
);
164 if (driContext
->driReadablePriv
)
165 dri2InvalidateDrawable(driContext
->driReadablePriv
);
170 intel_update_state(struct gl_context
* ctx
, GLuint new_state
)
172 struct brw_context
*brw
= brw_context(ctx
);
173 struct intel_texture_object
*tex_obj
;
174 struct intel_renderbuffer
*depth_irb
;
176 if (ctx
->swrast_context
)
177 _swrast_InvalidateState(ctx
, new_state
);
178 _vbo_InvalidateState(ctx
, new_state
);
180 brw
->NewGLState
|= new_state
;
182 _mesa_unlock_context_textures(ctx
);
184 /* Resolve the depth buffer's HiZ buffer. */
185 depth_irb
= intel_get_renderbuffer(ctx
->DrawBuffer
, BUFFER_DEPTH
);
187 intel_renderbuffer_resolve_hiz(brw
, depth_irb
);
189 /* Resolve depth buffer and render cache of each enabled texture. */
190 int maxEnabledUnit
= ctx
->Texture
._MaxEnabledTexImageUnit
;
191 for (int i
= 0; i
<= maxEnabledUnit
; i
++) {
192 if (!ctx
->Texture
.Unit
[i
]._Current
)
194 tex_obj
= intel_texture_object(ctx
->Texture
.Unit
[i
]._Current
);
195 if (!tex_obj
|| !tex_obj
->mt
)
197 intel_miptree_all_slices_resolve_depth(brw
, tex_obj
->mt
);
198 intel_miptree_resolve_color(brw
, tex_obj
->mt
);
199 brw_render_cache_set_check_flush(brw
, tex_obj
->mt
->bo
);
202 /* Resolve color for each active shader image. */
203 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
204 const struct gl_shader
*shader
= ctx
->_Shader
->CurrentProgram
[i
] ?
205 ctx
->_Shader
->CurrentProgram
[i
]->_LinkedShaders
[i
] : NULL
;
207 if (unlikely(shader
&& shader
->NumImages
)) {
208 for (unsigned j
= 0; j
< shader
->NumImages
; j
++) {
209 struct gl_image_unit
*u
= &ctx
->ImageUnits
[shader
->ImageUnits
[j
]];
210 tex_obj
= intel_texture_object(u
->TexObj
);
212 if (tex_obj
&& tex_obj
->mt
) {
213 intel_miptree_resolve_color(brw
, tex_obj
->mt
);
214 brw_render_cache_set_check_flush(brw
, tex_obj
->mt
->bo
);
220 /* If FRAMEBUFFER_SRGB is used on Gen9+ then we need to resolve any of the
221 * single-sampled color renderbuffers because the CCS buffer isn't
222 * supported for SRGB formats. This only matters if FRAMEBUFFER_SRGB is
223 * enabled because otherwise the surface state will be programmed with the
224 * linear equivalent format anyway.
226 if (brw
->gen
>= 9 && ctx
->Color
.sRGBEnabled
) {
227 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
228 for (int i
= 0; i
< fb
->_NumColorDrawBuffers
; i
++) {
229 struct gl_renderbuffer
*rb
= fb
->_ColorDrawBuffers
[i
];
234 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
235 struct intel_mipmap_tree
*mt
= irb
->mt
;
238 mt
->num_samples
> 1 ||
239 _mesa_get_srgb_format_linear(mt
->format
) == mt
->format
)
242 intel_miptree_resolve_color(brw
, mt
);
243 brw_render_cache_set_check_flush(brw
, mt
->bo
);
247 _mesa_lock_context_textures(ctx
);
250 #define flushFront(screen) ((screen)->image.loader ? (screen)->image.loader->flushFrontBuffer : (screen)->dri2.loader->flushFrontBuffer)
253 intel_flush_front(struct gl_context
*ctx
)
255 struct brw_context
*brw
= brw_context(ctx
);
256 __DRIcontext
*driContext
= brw
->driContext
;
257 __DRIdrawable
*driDrawable
= driContext
->driDrawablePriv
;
258 __DRIscreen
*const screen
= brw
->intelScreen
->driScrnPriv
;
260 if (brw
->front_buffer_dirty
&& _mesa_is_winsys_fbo(ctx
->DrawBuffer
)) {
261 if (flushFront(screen
) && driDrawable
&&
262 driDrawable
->loaderPrivate
) {
264 /* Resolve before flushing FAKE_FRONT_LEFT to FRONT_LEFT.
266 * This potentially resolves both front and back buffer. It
267 * is unnecessary to resolve the back, but harms nothing except
268 * performance. And no one cares about front-buffer render
271 intel_resolve_for_dri2_flush(brw
, driDrawable
);
272 intel_batchbuffer_flush(brw
);
274 flushFront(screen
)(driDrawable
, driDrawable
->loaderPrivate
);
276 /* We set the dirty bit in intel_prepare_render() if we're
277 * front buffer rendering once we get there.
279 brw
->front_buffer_dirty
= false;
285 intel_glFlush(struct gl_context
*ctx
)
287 struct brw_context
*brw
= brw_context(ctx
);
289 intel_batchbuffer_flush(brw
);
290 intel_flush_front(ctx
);
292 brw
->need_flush_throttle
= true;
296 intel_finish(struct gl_context
* ctx
)
298 struct brw_context
*brw
= brw_context(ctx
);
302 if (brw
->batch
.last_bo
)
303 drm_intel_bo_wait_rendering(brw
->batch
.last_bo
);
307 brw_init_driver_functions(struct brw_context
*brw
,
308 struct dd_function_table
*functions
)
310 _mesa_init_driver_functions(functions
);
312 /* GLX uses DRI2 invalidate events to handle window resizing.
313 * Unfortunately, EGL does not - libEGL is written in XCB (not Xlib),
314 * which doesn't provide a mechanism for snooping the event queues.
316 * So EGL still relies on viewport hacks to handle window resizing.
317 * This should go away with DRI3000.
319 if (!brw
->driContext
->driScreenPriv
->dri2
.useInvalidate
)
320 functions
->Viewport
= intel_viewport
;
322 functions
->Flush
= intel_glFlush
;
323 functions
->Finish
= intel_finish
;
324 functions
->GetString
= intel_get_string
;
325 functions
->UpdateState
= intel_update_state
;
327 intelInitTextureFuncs(functions
);
328 intelInitTextureImageFuncs(functions
);
329 intelInitTextureSubImageFuncs(functions
);
330 intelInitTextureCopyImageFuncs(functions
);
331 intelInitCopyImageFuncs(functions
);
332 intelInitClearFuncs(functions
);
333 intelInitBufferFuncs(functions
);
334 intelInitPixelFuncs(functions
);
335 intelInitBufferObjectFuncs(functions
);
336 intel_init_syncobj_functions(functions
);
337 brw_init_object_purgeable_functions(functions
);
339 brwInitFragProgFuncs( functions
);
340 brw_init_common_queryobj_functions(functions
);
342 gen6_init_queryobj_functions(functions
);
344 gen4_init_queryobj_functions(functions
);
345 brw_init_compute_functions(functions
);
347 brw_init_conditional_render_functions(functions
);
349 functions
->QuerySamplesForFormat
= brw_query_samples_for_format
;
351 functions
->NewTransformFeedback
= brw_new_transform_feedback
;
352 functions
->DeleteTransformFeedback
= brw_delete_transform_feedback
;
353 functions
->GetTransformFeedbackVertexCount
=
354 brw_get_transform_feedback_vertex_count
;
356 functions
->BeginTransformFeedback
= gen7_begin_transform_feedback
;
357 functions
->EndTransformFeedback
= gen7_end_transform_feedback
;
358 functions
->PauseTransformFeedback
= gen7_pause_transform_feedback
;
359 functions
->ResumeTransformFeedback
= gen7_resume_transform_feedback
;
361 functions
->BeginTransformFeedback
= brw_begin_transform_feedback
;
362 functions
->EndTransformFeedback
= brw_end_transform_feedback
;
366 functions
->GetSamplePosition
= gen6_get_sample_position
;
370 brw_initialize_context_constants(struct brw_context
*brw
)
372 struct gl_context
*ctx
= &brw
->ctx
;
373 const struct brw_compiler
*compiler
= brw
->intelScreen
->compiler
;
375 const bool stage_exists
[MESA_SHADER_STAGES
] = {
376 [MESA_SHADER_VERTEX
] = true,
377 [MESA_SHADER_TESS_CTRL
] = brw
->gen
>= 8,
378 [MESA_SHADER_TESS_EVAL
] = brw
->gen
>= 8,
379 [MESA_SHADER_GEOMETRY
] = brw
->gen
>= 6,
380 [MESA_SHADER_FRAGMENT
] = true,
381 [MESA_SHADER_COMPUTE
] =
382 (ctx
->Const
.MaxComputeWorkGroupSize
[0] >= 1024) ||
383 _mesa_extension_override_enables
.ARB_compute_shader
,
386 unsigned num_stages
= 0;
387 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
392 unsigned max_samplers
=
393 brw
->gen
>= 8 || brw
->is_haswell
? BRW_MAX_TEX_UNIT
: 16;
395 ctx
->Const
.MaxDualSourceDrawBuffers
= 1;
396 ctx
->Const
.MaxDrawBuffers
= BRW_MAX_DRAW_BUFFERS
;
397 ctx
->Const
.MaxCombinedShaderOutputResources
=
398 MAX_IMAGE_UNITS
+ BRW_MAX_DRAW_BUFFERS
;
400 ctx
->Const
.QueryCounterBits
.Timestamp
= 36;
402 ctx
->Const
.MaxTextureCoordUnits
= 8; /* Mesa limit */
403 ctx
->Const
.MaxImageUnits
= MAX_IMAGE_UNITS
;
404 ctx
->Const
.MaxRenderbufferSize
= 8192;
405 ctx
->Const
.MaxTextureLevels
= MIN2(14 /* 8192 */, MAX_TEXTURE_LEVELS
);
406 ctx
->Const
.Max3DTextureLevels
= 12; /* 2048 */
407 ctx
->Const
.MaxCubeTextureLevels
= 14; /* 8192 */
408 ctx
->Const
.MaxArrayTextureLayers
= brw
->gen
>= 7 ? 2048 : 512;
409 ctx
->Const
.MaxTextureMbytes
= 1536;
410 ctx
->Const
.MaxTextureRectSize
= 1 << 12;
411 ctx
->Const
.MaxTextureMaxAnisotropy
= 16.0;
412 ctx
->Const
.StripTextureBorder
= true;
414 ctx
->Const
.MaxProgramTextureGatherComponents
= 4;
415 else if (brw
->gen
== 6)
416 ctx
->Const
.MaxProgramTextureGatherComponents
= 1;
418 ctx
->Const
.MaxUniformBlockSize
= 65536;
420 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
421 struct gl_program_constants
*prog
= &ctx
->Const
.Program
[i
];
423 if (!stage_exists
[i
])
426 prog
->MaxTextureImageUnits
= max_samplers
;
428 prog
->MaxUniformBlocks
= BRW_MAX_UBO
;
429 prog
->MaxCombinedUniformComponents
=
430 prog
->MaxUniformComponents
+
431 ctx
->Const
.MaxUniformBlockSize
/ 4 * prog
->MaxUniformBlocks
;
433 prog
->MaxAtomicCounters
= MAX_ATOMIC_COUNTERS
;
434 prog
->MaxAtomicBuffers
= BRW_MAX_ABO
;
435 prog
->MaxImageUniforms
= compiler
->scalar_stage
[i
] ? BRW_MAX_IMAGES
: 0;
436 prog
->MaxShaderStorageBlocks
= BRW_MAX_SSBO
;
439 ctx
->Const
.MaxTextureUnits
=
440 MIN2(ctx
->Const
.MaxTextureCoordUnits
,
441 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxTextureImageUnits
);
443 ctx
->Const
.MaxUniformBufferBindings
= num_stages
* BRW_MAX_UBO
;
444 ctx
->Const
.MaxCombinedUniformBlocks
= num_stages
* BRW_MAX_UBO
;
445 ctx
->Const
.MaxCombinedAtomicBuffers
= num_stages
* BRW_MAX_ABO
;
446 ctx
->Const
.MaxCombinedShaderStorageBlocks
= num_stages
* BRW_MAX_SSBO
;
447 ctx
->Const
.MaxShaderStorageBufferBindings
= num_stages
* BRW_MAX_SSBO
;
448 ctx
->Const
.MaxCombinedTextureImageUnits
= num_stages
* max_samplers
;
449 ctx
->Const
.MaxCombinedImageUniforms
= num_stages
* BRW_MAX_IMAGES
;
452 /* Hardware only supports a limited number of transform feedback buffers.
453 * So we need to override the Mesa default (which is based only on software
456 ctx
->Const
.MaxTransformFeedbackBuffers
= BRW_MAX_SOL_BUFFERS
;
458 /* On Gen6, in the worst case, we use up one binding table entry per
459 * transform feedback component (see comments above the definition of
460 * BRW_MAX_SOL_BINDINGS, in brw_context.h), so we need to advertise a value
461 * for MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS equal to
462 * BRW_MAX_SOL_BINDINGS.
464 * In "separate components" mode, we need to divide this value by
465 * BRW_MAX_SOL_BUFFERS, so that the total number of binding table entries
466 * used up by all buffers will not exceed BRW_MAX_SOL_BINDINGS.
468 ctx
->Const
.MaxTransformFeedbackInterleavedComponents
= BRW_MAX_SOL_BINDINGS
;
469 ctx
->Const
.MaxTransformFeedbackSeparateComponents
=
470 BRW_MAX_SOL_BINDINGS
/ BRW_MAX_SOL_BUFFERS
;
472 ctx
->Const
.AlwaysUseGetTransformFeedbackVertexCount
= true;
475 const int *msaa_modes
= intel_supported_msaa_modes(brw
->intelScreen
);
476 const int clamp_max_samples
=
477 driQueryOptioni(&brw
->optionCache
, "clamp_max_samples");
479 if (clamp_max_samples
< 0) {
480 max_samples
= msaa_modes
[0];
482 /* Select the largest supported MSAA mode that does not exceed
486 for (int i
= 0; msaa_modes
[i
] != 0; ++i
) {
487 if (msaa_modes
[i
] <= clamp_max_samples
) {
488 max_samples
= msaa_modes
[i
];
494 ctx
->Const
.MaxSamples
= max_samples
;
495 ctx
->Const
.MaxColorTextureSamples
= max_samples
;
496 ctx
->Const
.MaxDepthTextureSamples
= max_samples
;
497 ctx
->Const
.MaxIntegerSamples
= max_samples
;
498 ctx
->Const
.MaxImageSamples
= 0;
500 /* gen6_set_sample_maps() sets SampleMap{2,4,8}x variables which are used
501 * to map indices of rectangular grid to sample numbers within a pixel.
502 * These variables are used by GL_EXT_framebuffer_multisample_blit_scaled
503 * extension implementation. For more details see the comment above
504 * gen6_set_sample_maps() definition.
506 gen6_set_sample_maps(ctx
);
508 ctx
->Const
.MinLineWidth
= 1.0;
509 ctx
->Const
.MinLineWidthAA
= 1.0;
511 ctx
->Const
.MaxLineWidth
= 7.375;
512 ctx
->Const
.MaxLineWidthAA
= 7.375;
513 ctx
->Const
.LineWidthGranularity
= 0.125;
515 ctx
->Const
.MaxLineWidth
= 7.0;
516 ctx
->Const
.MaxLineWidthAA
= 7.0;
517 ctx
->Const
.LineWidthGranularity
= 0.5;
520 /* For non-antialiased lines, we have to round the line width to the
521 * nearest whole number. Make sure that we don't advertise a line
522 * width that, when rounded, will be beyond the actual hardware
525 assert(roundf(ctx
->Const
.MaxLineWidth
) <= ctx
->Const
.MaxLineWidth
);
527 ctx
->Const
.MinPointSize
= 1.0;
528 ctx
->Const
.MinPointSizeAA
= 1.0;
529 ctx
->Const
.MaxPointSize
= 255.0;
530 ctx
->Const
.MaxPointSizeAA
= 255.0;
531 ctx
->Const
.PointSizeGranularity
= 1.0;
533 if (brw
->gen
>= 5 || brw
->is_g4x
)
534 ctx
->Const
.MaxClipPlanes
= 8;
536 ctx
->Const
.LowerTessLevel
= true;
538 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeInstructions
= 16 * 1024;
539 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxAluInstructions
= 0;
540 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTexInstructions
= 0;
541 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTexIndirections
= 0;
542 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeAluInstructions
= 0;
543 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeTexInstructions
= 0;
544 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeTexIndirections
= 0;
545 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeAttribs
= 16;
546 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeTemps
= 256;
547 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeAddressRegs
= 1;
548 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeParameters
= 1024;
549 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxEnvParams
=
550 MIN2(ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeParameters
,
551 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxEnvParams
);
553 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeInstructions
= 1024;
554 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeAluInstructions
= 1024;
555 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeTexInstructions
= 1024;
556 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeTexIndirections
= 1024;
557 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeAttribs
= 12;
558 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeTemps
= 256;
559 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeAddressRegs
= 0;
560 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeParameters
= 1024;
561 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxEnvParams
=
562 MIN2(ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeParameters
,
563 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxEnvParams
);
565 /* Fragment shaders use real, 32-bit twos-complement integers for all
568 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].LowInt
.RangeMin
= 31;
569 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].LowInt
.RangeMax
= 30;
570 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].LowInt
.Precision
= 0;
571 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].HighInt
= ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].LowInt
;
572 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MediumInt
= ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].LowInt
;
574 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].LowInt
.RangeMin
= 31;
575 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].LowInt
.RangeMax
= 30;
576 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].LowInt
.Precision
= 0;
577 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].HighInt
= ctx
->Const
.Program
[MESA_SHADER_VERTEX
].LowInt
;
578 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MediumInt
= ctx
->Const
.Program
[MESA_SHADER_VERTEX
].LowInt
;
580 /* Gen6 converts quads to polygon in beginning of 3D pipeline,
581 * but we're not sure how it's actually done for vertex order,
582 * that affect provoking vertex decision. Always use last vertex
583 * convention for quad primitive which works as expected for now.
586 ctx
->Const
.QuadsFollowProvokingVertexConvention
= false;
588 ctx
->Const
.NativeIntegers
= true;
589 ctx
->Const
.VertexID_is_zero_based
= true;
591 /* Regarding the CMP instruction, the Ivybridge PRM says:
593 * "For each enabled channel 0b or 1b is assigned to the appropriate flag
594 * bit and 0/all zeros or all ones (e.g, byte 0xFF, word 0xFFFF, DWord
595 * 0xFFFFFFFF) is assigned to dst."
597 * but PRMs for earlier generations say
599 * "In dword format, one GRF may store up to 8 results. When the register
600 * is used later as a vector of Booleans, as only LSB at each channel
601 * contains meaning [sic] data, software should make sure all higher bits
602 * are masked out (e.g. by 'and-ing' an [sic] 0x01 constant)."
604 * We select the representation of a true boolean uniform to be ~0, and fix
605 * the results of Gen <= 5 CMP instruction's with -(result & 1).
607 ctx
->Const
.UniformBooleanTrue
= ~0;
609 /* From the gen4 PRM, volume 4 page 127:
611 * "For SURFTYPE_BUFFER non-rendertarget surfaces, this field specifies
612 * the base address of the first element of the surface, computed in
613 * software by adding the surface base address to the byte offset of
614 * the element in the buffer."
616 * However, unaligned accesses are slower, so enforce buffer alignment.
618 ctx
->Const
.UniformBufferOffsetAlignment
= 16;
620 /* ShaderStorageBufferOffsetAlignment should be a cacheline (64 bytes) so
621 * that we can safely have the CPU and GPU writing the same SSBO on
622 * non-cachecoherent systems (our Atom CPUs). With UBOs, the GPU never
623 * writes, so there's no problem. For an SSBO, the GPU and the CPU can
624 * be updating disjoint regions of the buffer simultaneously and that will
625 * break if the regions overlap the same cacheline.
627 ctx
->Const
.ShaderStorageBufferOffsetAlignment
= 64;
628 ctx
->Const
.TextureBufferOffsetAlignment
= 16;
629 ctx
->Const
.MaxTextureBufferSize
= 128 * 1024 * 1024;
632 ctx
->Const
.MaxVarying
= 32;
633 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxOutputComponents
= 128;
634 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxInputComponents
= 64;
635 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxOutputComponents
= 128;
636 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxInputComponents
= 128;
637 ctx
->Const
.Program
[MESA_SHADER_TESS_CTRL
].MaxInputComponents
= 128;
638 ctx
->Const
.Program
[MESA_SHADER_TESS_CTRL
].MaxOutputComponents
= 128;
639 ctx
->Const
.Program
[MESA_SHADER_TESS_EVAL
].MaxInputComponents
= 128;
640 ctx
->Const
.Program
[MESA_SHADER_TESS_EVAL
].MaxOutputComponents
= 128;
643 /* We want the GLSL compiler to emit code that uses condition codes */
644 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
645 ctx
->Const
.ShaderCompilerOptions
[i
] =
646 brw
->intelScreen
->compiler
->glsl_compiler_options
[i
];
649 /* ARB_viewport_array */
650 if (brw
->gen
>= 6 && ctx
->API
== API_OPENGL_CORE
) {
651 ctx
->Const
.MaxViewports
= GEN6_NUM_VIEWPORTS
;
652 ctx
->Const
.ViewportSubpixelBits
= 0;
654 /* Cast to float before negating because MaxViewportWidth is unsigned.
656 ctx
->Const
.ViewportBounds
.Min
= -(float)ctx
->Const
.MaxViewportWidth
;
657 ctx
->Const
.ViewportBounds
.Max
= ctx
->Const
.MaxViewportWidth
;
660 /* ARB_gpu_shader5 */
662 ctx
->Const
.MaxVertexStreams
= MIN2(4, MAX_VERTEX_STREAMS
);
664 /* ARB_framebuffer_no_attachments */
665 ctx
->Const
.MaxFramebufferWidth
= ctx
->Const
.MaxViewportWidth
;
666 ctx
->Const
.MaxFramebufferHeight
= ctx
->Const
.MaxViewportHeight
;
667 ctx
->Const
.MaxFramebufferLayers
= ctx
->Const
.MaxArrayTextureLayers
;
668 ctx
->Const
.MaxFramebufferSamples
= max_samples
;
672 brw_initialize_cs_context_constants(struct brw_context
*brw
, unsigned max_threads
)
674 struct gl_context
*ctx
= &brw
->ctx
;
676 /* For ES, we set these constants based on SIMD8.
678 * TODO: Once we can always generate SIMD16, we should update this.
680 * For GL, we assume we can generate a SIMD16 program, but this currently
681 * is not always true. This allows us to run more test cases, and will be
682 * required based on desktop GL compute shader requirements.
684 const int simd_size
= ctx
->API
== API_OPENGL_CORE
? 16 : 8;
686 const uint32_t max_invocations
= simd_size
* max_threads
;
687 ctx
->Const
.MaxComputeWorkGroupSize
[0] = max_invocations
;
688 ctx
->Const
.MaxComputeWorkGroupSize
[1] = max_invocations
;
689 ctx
->Const
.MaxComputeWorkGroupSize
[2] = max_invocations
;
690 ctx
->Const
.MaxComputeWorkGroupInvocations
= max_invocations
;
694 * Process driconf (drirc) options, setting appropriate context flags.
696 * intelInitExtensions still pokes at optionCache directly, in order to
697 * avoid advertising various extensions. No flags are set, so it makes
698 * sense to continue doing that there.
701 brw_process_driconf_options(struct brw_context
*brw
)
703 struct gl_context
*ctx
= &brw
->ctx
;
705 driOptionCache
*options
= &brw
->optionCache
;
706 driParseConfigFiles(options
, &brw
->intelScreen
->optionCache
,
707 brw
->driContext
->driScreenPriv
->myNum
, "i965");
709 int bo_reuse_mode
= driQueryOptioni(options
, "bo_reuse");
710 switch (bo_reuse_mode
) {
711 case DRI_CONF_BO_REUSE_DISABLED
:
713 case DRI_CONF_BO_REUSE_ALL
:
714 intel_bufmgr_gem_enable_reuse(brw
->bufmgr
);
718 if (!driQueryOptionb(options
, "hiz")) {
719 brw
->has_hiz
= false;
720 /* On gen6, you can only do separate stencil with HIZ. */
722 brw
->has_separate_stencil
= false;
725 if (driQueryOptionb(options
, "always_flush_batch")) {
726 fprintf(stderr
, "flushing batchbuffer before/after each draw call\n");
727 brw
->always_flush_batch
= true;
730 if (driQueryOptionb(options
, "always_flush_cache")) {
731 fprintf(stderr
, "flushing GPU caches before/after each draw call\n");
732 brw
->always_flush_cache
= true;
735 if (driQueryOptionb(options
, "disable_throttling")) {
736 fprintf(stderr
, "disabling flush throttling\n");
737 brw
->disable_throttling
= true;
740 brw
->precompile
= driQueryOptionb(&brw
->optionCache
, "shader_precompile");
742 ctx
->Const
.ForceGLSLExtensionsWarn
=
743 driQueryOptionb(options
, "force_glsl_extensions_warn");
745 ctx
->Const
.DisableGLSLLineContinuations
=
746 driQueryOptionb(options
, "disable_glsl_line_continuations");
748 ctx
->Const
.AllowGLSLExtensionDirectiveMidShader
=
749 driQueryOptionb(options
, "allow_glsl_extension_directive_midshader");
753 brwCreateContext(gl_api api
,
754 const struct gl_config
*mesaVis
,
755 __DRIcontext
*driContextPriv
,
756 unsigned major_version
,
757 unsigned minor_version
,
760 unsigned *dri_ctx_error
,
761 void *sharedContextPrivate
)
763 __DRIscreen
*sPriv
= driContextPriv
->driScreenPriv
;
764 struct gl_context
*shareCtx
= (struct gl_context
*) sharedContextPrivate
;
765 struct intel_screen
*screen
= sPriv
->driverPrivate
;
766 const struct brw_device_info
*devinfo
= screen
->devinfo
;
767 struct dd_function_table functions
;
769 /* Only allow the __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS flag if the kernel
770 * provides us with context reset notifications.
772 uint32_t allowed_flags
= __DRI_CTX_FLAG_DEBUG
773 | __DRI_CTX_FLAG_FORWARD_COMPATIBLE
;
775 if (screen
->has_context_reset_notification
)
776 allowed_flags
|= __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS
;
778 if (flags
& ~allowed_flags
) {
779 *dri_ctx_error
= __DRI_CTX_ERROR_UNKNOWN_FLAG
;
783 struct brw_context
*brw
= rzalloc(NULL
, struct brw_context
);
785 fprintf(stderr
, "%s: failed to alloc context\n", __func__
);
786 *dri_ctx_error
= __DRI_CTX_ERROR_NO_MEMORY
;
790 driContextPriv
->driverPrivate
= brw
;
791 brw
->driContext
= driContextPriv
;
792 brw
->intelScreen
= screen
;
793 brw
->bufmgr
= screen
->bufmgr
;
795 brw
->gen
= devinfo
->gen
;
796 brw
->gt
= devinfo
->gt
;
797 brw
->is_g4x
= devinfo
->is_g4x
;
798 brw
->is_baytrail
= devinfo
->is_baytrail
;
799 brw
->is_haswell
= devinfo
->is_haswell
;
800 brw
->is_cherryview
= devinfo
->is_cherryview
;
801 brw
->is_broxton
= devinfo
->is_broxton
;
802 brw
->has_llc
= devinfo
->has_llc
;
803 brw
->has_hiz
= devinfo
->has_hiz_and_separate_stencil
;
804 brw
->has_separate_stencil
= devinfo
->has_hiz_and_separate_stencil
;
805 brw
->has_pln
= devinfo
->has_pln
;
806 brw
->has_compr4
= devinfo
->has_compr4
;
807 brw
->has_surface_tile_offset
= devinfo
->has_surface_tile_offset
;
808 brw
->has_negative_rhw_bug
= devinfo
->has_negative_rhw_bug
;
809 brw
->needs_unlit_centroid_workaround
=
810 devinfo
->needs_unlit_centroid_workaround
;
812 brw
->must_use_separate_stencil
= screen
->hw_must_use_separate_stencil
;
813 brw
->has_swizzling
= screen
->hw_has_swizzling
;
815 brw
->vs
.base
.stage
= MESA_SHADER_VERTEX
;
816 brw
->tcs
.base
.stage
= MESA_SHADER_TESS_CTRL
;
817 brw
->tes
.base
.stage
= MESA_SHADER_TESS_EVAL
;
818 brw
->gs
.base
.stage
= MESA_SHADER_GEOMETRY
;
819 brw
->wm
.base
.stage
= MESA_SHADER_FRAGMENT
;
821 gen8_init_vtable_surface_functions(brw
);
822 brw
->vtbl
.emit_depth_stencil_hiz
= gen8_emit_depth_stencil_hiz
;
823 } else if (brw
->gen
>= 7) {
824 gen7_init_vtable_surface_functions(brw
);
825 brw
->vtbl
.emit_depth_stencil_hiz
= gen7_emit_depth_stencil_hiz
;
826 } else if (brw
->gen
>= 6) {
827 gen6_init_vtable_surface_functions(brw
);
828 brw
->vtbl
.emit_depth_stencil_hiz
= gen6_emit_depth_stencil_hiz
;
830 gen4_init_vtable_surface_functions(brw
);
831 brw
->vtbl
.emit_depth_stencil_hiz
= brw_emit_depth_stencil_hiz
;
834 brw_init_driver_functions(brw
, &functions
);
837 functions
.GetGraphicsResetStatus
= brw_get_graphics_reset_status
;
839 struct gl_context
*ctx
= &brw
->ctx
;
841 if (!_mesa_initialize_context(ctx
, api
, mesaVis
, shareCtx
, &functions
)) {
842 *dri_ctx_error
= __DRI_CTX_ERROR_NO_MEMORY
;
843 fprintf(stderr
, "%s: failed to init mesa context\n", __func__
);
844 intelDestroyContext(driContextPriv
);
848 driContextSetFlags(ctx
, flags
);
850 /* Initialize the software rasterizer and helper modules.
852 * As of GL 3.1 core, the gen4+ driver doesn't need the swrast context for
853 * software fallbacks (which we have to support on legacy GL to do weird
854 * glDrawPixels(), glBitmap(), and other functions).
856 if (api
!= API_OPENGL_CORE
&& api
!= API_OPENGLES2
) {
857 _swrast_CreateContext(ctx
);
860 _vbo_CreateContext(ctx
);
861 if (ctx
->swrast_context
) {
862 _tnl_CreateContext(ctx
);
863 TNL_CONTEXT(ctx
)->Driver
.RunPipeline
= _tnl_run_pipeline
;
864 _swsetup_CreateContext(ctx
);
866 /* Configure swrast to match hardware characteristics: */
867 _swrast_allow_pixel_fog(ctx
, false);
868 _swrast_allow_vertex_fog(ctx
, true);
871 _mesa_meta_init(ctx
);
873 brw_process_driconf_options(brw
);
875 if (INTEL_DEBUG
& DEBUG_PERF
)
876 brw
->perf_debug
= true;
878 brw_initialize_cs_context_constants(brw
, devinfo
->max_cs_threads
);
879 brw_initialize_context_constants(brw
);
881 ctx
->Const
.ResetStrategy
= notify_reset
882 ? GL_LOSE_CONTEXT_ON_RESET_ARB
: GL_NO_RESET_NOTIFICATION_ARB
;
884 /* Reinitialize the context point state. It depends on ctx->Const values. */
885 _mesa_init_point(ctx
);
889 intel_batchbuffer_init(brw
);
892 /* Create a new hardware context. Using a hardware context means that
893 * our GPU state will be saved/restored on context switch, allowing us
894 * to assume that the GPU is in the same state we left it in.
896 * This is required for transform feedback buffer offsets, query objects,
897 * and also allows us to reduce how much state we have to emit.
899 brw
->hw_ctx
= drm_intel_gem_context_create(brw
->bufmgr
);
902 fprintf(stderr
, "Gen6+ requires Kernel 3.6 or later.\n");
903 intelDestroyContext(driContextPriv
);
908 if (brw_init_pipe_control(brw
, devinfo
)) {
909 *dri_ctx_error
= __DRI_CTX_ERROR_NO_MEMORY
;
910 intelDestroyContext(driContextPriv
);
916 intelInitExtensions(ctx
);
918 brw_init_surface_formats(brw
);
920 brw
->max_vs_threads
= devinfo
->max_vs_threads
;
921 brw
->max_hs_threads
= devinfo
->max_hs_threads
;
922 brw
->max_ds_threads
= devinfo
->max_ds_threads
;
923 brw
->max_gs_threads
= devinfo
->max_gs_threads
;
924 brw
->max_wm_threads
= devinfo
->max_wm_threads
;
925 brw
->max_cs_threads
= devinfo
->max_cs_threads
;
926 brw
->urb
.size
= devinfo
->urb
.size
;
927 brw
->urb
.min_vs_entries
= devinfo
->urb
.min_vs_entries
;
928 brw
->urb
.max_vs_entries
= devinfo
->urb
.max_vs_entries
;
929 brw
->urb
.max_hs_entries
= devinfo
->urb
.max_hs_entries
;
930 brw
->urb
.max_ds_entries
= devinfo
->urb
.max_ds_entries
;
931 brw
->urb
.max_gs_entries
= devinfo
->urb
.max_gs_entries
;
933 /* Estimate the size of the mappable aperture into the GTT. There's an
934 * ioctl to get the whole GTT size, but not one to get the mappable subset.
935 * It turns out it's basically always 256MB, though some ancient hardware
938 uint32_t gtt_size
= 256 * 1024 * 1024;
940 /* We don't want to map two objects such that a memcpy between them would
941 * just fault one mapping in and then the other over and over forever. So
942 * we would need to divide the GTT size by 2. Additionally, some GTT is
943 * taken up by things like the framebuffer and the ringbuffer and such, so
944 * be more conservative.
946 brw
->max_gtt_map_object_size
= gtt_size
/ 4;
949 brw
->urb
.gs_present
= false;
951 brw
->prim_restart
.in_progress
= false;
952 brw
->prim_restart
.enable_cut_index
= false;
953 brw
->gs
.enabled
= false;
954 brw
->sf
.viewport_transform_enable
= true;
956 brw
->predicate
.state
= BRW_PREDICATE_STATE_RENDER
;
958 brw
->use_resource_streamer
= screen
->has_resource_streamer
&&
959 (env_var_as_boolean("INTEL_USE_HW_BT", false) ||
960 env_var_as_boolean("INTEL_USE_GATHER", false));
962 ctx
->VertexProgram
._MaintainTnlProgram
= true;
963 ctx
->FragmentProgram
._MaintainTexEnvProgram
= true;
965 brw_draw_init( brw
);
967 if ((flags
& __DRI_CTX_FLAG_DEBUG
) != 0) {
968 /* Turn on some extra GL_ARB_debug_output generation. */
969 brw
->perf_debug
= true;
972 if ((flags
& __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS
) != 0)
973 ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_ROBUST_ACCESS_BIT_ARB
;
975 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
)
976 brw_init_shader_time(brw
);
978 _mesa_compute_version(ctx
);
980 _mesa_initialize_dispatch_tables(ctx
);
981 _mesa_initialize_vbo_vtxfmt(ctx
);
983 if (ctx
->Extensions
.AMD_performance_monitor
) {
984 brw_init_performance_monitors(brw
);
987 vbo_use_buffer_objects(ctx
);
988 vbo_always_unmap_buffers(ctx
);
994 intelDestroyContext(__DRIcontext
* driContextPriv
)
996 struct brw_context
*brw
=
997 (struct brw_context
*) driContextPriv
->driverPrivate
;
998 struct gl_context
*ctx
= &brw
->ctx
;
1000 /* Dump a final BMP in case the application doesn't call SwapBuffers */
1001 if (INTEL_DEBUG
& DEBUG_AUB
) {
1002 intel_batchbuffer_flush(brw
);
1003 aub_dump_bmp(&brw
->ctx
);
1006 _mesa_meta_free(&brw
->ctx
);
1007 brw_meta_fast_clear_free(brw
);
1009 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
) {
1010 /* Force a report. */
1011 brw
->shader_time
.report_time
= 0;
1013 brw_collect_and_report_shader_time(brw
);
1014 brw_destroy_shader_time(brw
);
1017 brw_destroy_state(brw
);
1018 brw_draw_destroy(brw
);
1020 drm_intel_bo_unreference(brw
->curbe
.curbe_bo
);
1021 if (brw
->vs
.base
.scratch_bo
)
1022 drm_intel_bo_unreference(brw
->vs
.base
.scratch_bo
);
1023 if (brw
->gs
.base
.scratch_bo
)
1024 drm_intel_bo_unreference(brw
->gs
.base
.scratch_bo
);
1025 if (brw
->wm
.base
.scratch_bo
)
1026 drm_intel_bo_unreference(brw
->wm
.base
.scratch_bo
);
1028 gen7_reset_hw_bt_pool_offsets(brw
);
1029 drm_intel_bo_unreference(brw
->hw_bt_pool
.bo
);
1030 brw
->hw_bt_pool
.bo
= NULL
;
1032 drm_intel_gem_context_destroy(brw
->hw_ctx
);
1034 if (ctx
->swrast_context
) {
1035 _swsetup_DestroyContext(&brw
->ctx
);
1036 _tnl_DestroyContext(&brw
->ctx
);
1038 _vbo_DestroyContext(&brw
->ctx
);
1040 if (ctx
->swrast_context
)
1041 _swrast_DestroyContext(&brw
->ctx
);
1043 brw_fini_pipe_control(brw
);
1044 intel_batchbuffer_free(brw
);
1046 drm_intel_bo_unreference(brw
->throttle_batch
[1]);
1047 drm_intel_bo_unreference(brw
->throttle_batch
[0]);
1048 brw
->throttle_batch
[1] = NULL
;
1049 brw
->throttle_batch
[0] = NULL
;
1051 driDestroyOptionCache(&brw
->optionCache
);
1053 /* free the Mesa context */
1054 _mesa_free_context_data(&brw
->ctx
);
1057 driContextPriv
->driverPrivate
= NULL
;
1061 intelUnbindContext(__DRIcontext
* driContextPriv
)
1063 /* Unset current context and dispath table */
1064 _mesa_make_current(NULL
, NULL
, NULL
);
1070 * Fixes up the context for GLES23 with our default-to-sRGB-capable behavior
1071 * on window system framebuffers.
1073 * Desktop GL is fairly reasonable in its handling of sRGB: You can ask if
1074 * your renderbuffer can do sRGB encode, and you can flip a switch that does
1075 * sRGB encode if the renderbuffer can handle it. You can ask specifically
1076 * for a visual where you're guaranteed to be capable, but it turns out that
1077 * everyone just makes all their ARGB8888 visuals capable and doesn't offer
1078 * incapable ones, because there's no difference between the two in resources
1079 * used. Applications thus get built that accidentally rely on the default
1080 * visual choice being sRGB, so we make ours sRGB capable. Everything sounds
1083 * But for GLES2/3, they decided that it was silly to not turn on sRGB encode
1084 * for sRGB renderbuffers you made with the GL_EXT_texture_sRGB equivalent.
1085 * So they removed the enable knob and made it "if the renderbuffer is sRGB
1086 * capable, do sRGB encode". Then, for your window system renderbuffers, you
1087 * can ask for sRGB visuals and get sRGB encode, or not ask for sRGB visuals
1088 * and get no sRGB encode (assuming that both kinds of visual are available).
1089 * Thus our choice to support sRGB by default on our visuals for desktop would
1090 * result in broken rendering of GLES apps that aren't expecting sRGB encode.
1092 * Unfortunately, renderbuffer setup happens before a context is created. So
1093 * in intel_screen.c we always set up sRGB, and here, if you're a GLES2/3
1094 * context (without an sRGB visual, though we don't have sRGB visuals exposed
1095 * yet), we go turn that back off before anyone finds out.
1098 intel_gles3_srgb_workaround(struct brw_context
*brw
,
1099 struct gl_framebuffer
*fb
)
1101 struct gl_context
*ctx
= &brw
->ctx
;
1103 if (_mesa_is_desktop_gl(ctx
) || !fb
->Visual
.sRGBCapable
)
1106 /* Some day when we support the sRGB capable bit on visuals available for
1107 * GLES, we'll need to respect that and not disable things here.
1109 fb
->Visual
.sRGBCapable
= false;
1110 for (int i
= 0; i
< BUFFER_COUNT
; i
++) {
1111 if (fb
->Attachment
[i
].Renderbuffer
&&
1112 fb
->Attachment
[i
].Renderbuffer
->Format
== MESA_FORMAT_B8G8R8A8_SRGB
) {
1113 fb
->Attachment
[i
].Renderbuffer
->Format
= MESA_FORMAT_B8G8R8A8_UNORM
;
1119 intelMakeCurrent(__DRIcontext
* driContextPriv
,
1120 __DRIdrawable
* driDrawPriv
,
1121 __DRIdrawable
* driReadPriv
)
1123 struct brw_context
*brw
;
1124 GET_CURRENT_CONTEXT(curCtx
);
1127 brw
= (struct brw_context
*) driContextPriv
->driverPrivate
;
1131 /* According to the glXMakeCurrent() man page: "Pending commands to
1132 * the previous context, if any, are flushed before it is released."
1133 * But only flush if we're actually changing contexts.
1135 if (brw_context(curCtx
) && brw_context(curCtx
) != brw
) {
1136 _mesa_flush(curCtx
);
1139 if (driContextPriv
) {
1140 struct gl_context
*ctx
= &brw
->ctx
;
1141 struct gl_framebuffer
*fb
, *readFb
;
1143 if (driDrawPriv
== NULL
) {
1144 fb
= _mesa_get_incomplete_framebuffer();
1146 fb
= driDrawPriv
->driverPrivate
;
1147 driContextPriv
->dri2
.draw_stamp
= driDrawPriv
->dri2
.stamp
- 1;
1150 if (driReadPriv
== NULL
) {
1151 readFb
= _mesa_get_incomplete_framebuffer();
1153 readFb
= driReadPriv
->driverPrivate
;
1154 driContextPriv
->dri2
.read_stamp
= driReadPriv
->dri2
.stamp
- 1;
1157 /* The sRGB workaround changes the renderbuffer's format. We must change
1158 * the format before the renderbuffer's miptree get's allocated, otherwise
1159 * the formats of the renderbuffer and its miptree will differ.
1161 intel_gles3_srgb_workaround(brw
, fb
);
1162 intel_gles3_srgb_workaround(brw
, readFb
);
1164 /* If the context viewport hasn't been initialized, force a call out to
1165 * the loader to get buffers so we have a drawable size for the initial
1167 if (!brw
->ctx
.ViewportInitialized
)
1168 intel_prepare_render(brw
);
1170 _mesa_make_current(ctx
, fb
, readFb
);
1172 _mesa_make_current(NULL
, NULL
, NULL
);
1179 intel_resolve_for_dri2_flush(struct brw_context
*brw
,
1180 __DRIdrawable
*drawable
)
1183 /* MSAA and fast color clear are not supported, so don't waste time
1184 * checking whether a resolve is needed.
1189 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
1190 struct intel_renderbuffer
*rb
;
1192 /* Usually, only the back buffer will need to be downsampled. However,
1193 * the front buffer will also need it if the user has rendered into it.
1195 static const gl_buffer_index buffers
[2] = {
1200 for (int i
= 0; i
< 2; ++i
) {
1201 rb
= intel_get_renderbuffer(fb
, buffers
[i
]);
1202 if (rb
== NULL
|| rb
->mt
== NULL
)
1204 if (rb
->mt
->num_samples
<= 1)
1205 intel_miptree_resolve_color(brw
, rb
->mt
);
1207 intel_renderbuffer_downsample(brw
, rb
);
1212 intel_bits_per_pixel(const struct intel_renderbuffer
*rb
)
1214 return _mesa_get_format_bytes(intel_rb_format(rb
)) * 8;
1218 intel_query_dri2_buffers(struct brw_context
*brw
,
1219 __DRIdrawable
*drawable
,
1220 __DRIbuffer
**buffers
,
1224 intel_process_dri2_buffer(struct brw_context
*brw
,
1225 __DRIdrawable
*drawable
,
1226 __DRIbuffer
*buffer
,
1227 struct intel_renderbuffer
*rb
,
1228 const char *buffer_name
);
1231 intel_update_image_buffers(struct brw_context
*brw
, __DRIdrawable
*drawable
);
1234 intel_update_dri2_buffers(struct brw_context
*brw
, __DRIdrawable
*drawable
)
1236 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
1237 struct intel_renderbuffer
*rb
;
1238 __DRIbuffer
*buffers
= NULL
;
1240 const char *region_name
;
1242 /* Set this up front, so that in case our buffers get invalidated
1243 * while we're getting new buffers, we don't clobber the stamp and
1244 * thus ignore the invalidate. */
1245 drawable
->lastStamp
= drawable
->dri2
.stamp
;
1247 if (unlikely(INTEL_DEBUG
& DEBUG_DRI
))
1248 fprintf(stderr
, "enter %s, drawable %p\n", __func__
, drawable
);
1250 intel_query_dri2_buffers(brw
, drawable
, &buffers
, &count
);
1252 if (buffers
== NULL
)
1255 for (i
= 0; i
< count
; i
++) {
1256 switch (buffers
[i
].attachment
) {
1257 case __DRI_BUFFER_FRONT_LEFT
:
1258 rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
1259 region_name
= "dri2 front buffer";
1262 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
1263 rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
1264 region_name
= "dri2 fake front buffer";
1267 case __DRI_BUFFER_BACK_LEFT
:
1268 rb
= intel_get_renderbuffer(fb
, BUFFER_BACK_LEFT
);
1269 region_name
= "dri2 back buffer";
1272 case __DRI_BUFFER_DEPTH
:
1273 case __DRI_BUFFER_HIZ
:
1274 case __DRI_BUFFER_DEPTH_STENCIL
:
1275 case __DRI_BUFFER_STENCIL
:
1276 case __DRI_BUFFER_ACCUM
:
1279 "unhandled buffer attach event, attachment type %d\n",
1280 buffers
[i
].attachment
);
1284 intel_process_dri2_buffer(brw
, drawable
, &buffers
[i
], rb
, region_name
);
1290 intel_update_renderbuffers(__DRIcontext
*context
, __DRIdrawable
*drawable
)
1292 struct brw_context
*brw
= context
->driverPrivate
;
1293 __DRIscreen
*screen
= brw
->intelScreen
->driScrnPriv
;
1295 /* Set this up front, so that in case our buffers get invalidated
1296 * while we're getting new buffers, we don't clobber the stamp and
1297 * thus ignore the invalidate. */
1298 drawable
->lastStamp
= drawable
->dri2
.stamp
;
1300 if (unlikely(INTEL_DEBUG
& DEBUG_DRI
))
1301 fprintf(stderr
, "enter %s, drawable %p\n", __func__
, drawable
);
1303 if (screen
->image
.loader
)
1304 intel_update_image_buffers(brw
, drawable
);
1306 intel_update_dri2_buffers(brw
, drawable
);
1308 driUpdateFramebufferSize(&brw
->ctx
, drawable
);
1312 * intel_prepare_render should be called anywhere that curent read/drawbuffer
1313 * state is required.
1316 intel_prepare_render(struct brw_context
*brw
)
1318 struct gl_context
*ctx
= &brw
->ctx
;
1319 __DRIcontext
*driContext
= brw
->driContext
;
1320 __DRIdrawable
*drawable
;
1322 drawable
= driContext
->driDrawablePriv
;
1323 if (drawable
&& drawable
->dri2
.stamp
!= driContext
->dri2
.draw_stamp
) {
1324 if (drawable
->lastStamp
!= drawable
->dri2
.stamp
)
1325 intel_update_renderbuffers(driContext
, drawable
);
1326 driContext
->dri2
.draw_stamp
= drawable
->dri2
.stamp
;
1329 drawable
= driContext
->driReadablePriv
;
1330 if (drawable
&& drawable
->dri2
.stamp
!= driContext
->dri2
.read_stamp
) {
1331 if (drawable
->lastStamp
!= drawable
->dri2
.stamp
)
1332 intel_update_renderbuffers(driContext
, drawable
);
1333 driContext
->dri2
.read_stamp
= drawable
->dri2
.stamp
;
1336 /* If we're currently rendering to the front buffer, the rendering
1337 * that will happen next will probably dirty the front buffer. So
1338 * mark it as dirty here.
1340 if (_mesa_is_front_buffer_drawing(ctx
->DrawBuffer
))
1341 brw
->front_buffer_dirty
= true;
1345 * \brief Query DRI2 to obtain a DRIdrawable's buffers.
1347 * To determine which DRI buffers to request, examine the renderbuffers
1348 * attached to the drawable's framebuffer. Then request the buffers with
1349 * DRI2GetBuffers() or DRI2GetBuffersWithFormat().
1351 * This is called from intel_update_renderbuffers().
1353 * \param drawable Drawable whose buffers are queried.
1354 * \param buffers [out] List of buffers returned by DRI2 query.
1355 * \param buffer_count [out] Number of buffers returned.
1357 * \see intel_update_renderbuffers()
1358 * \see DRI2GetBuffers()
1359 * \see DRI2GetBuffersWithFormat()
1362 intel_query_dri2_buffers(struct brw_context
*brw
,
1363 __DRIdrawable
*drawable
,
1364 __DRIbuffer
**buffers
,
1367 __DRIscreen
*screen
= brw
->intelScreen
->driScrnPriv
;
1368 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
1370 unsigned attachments
[8];
1372 struct intel_renderbuffer
*front_rb
;
1373 struct intel_renderbuffer
*back_rb
;
1375 front_rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
1376 back_rb
= intel_get_renderbuffer(fb
, BUFFER_BACK_LEFT
);
1378 memset(attachments
, 0, sizeof(attachments
));
1379 if ((_mesa_is_front_buffer_drawing(fb
) ||
1380 _mesa_is_front_buffer_reading(fb
) ||
1381 !back_rb
) && front_rb
) {
1382 /* If a fake front buffer is in use, then querying for
1383 * __DRI_BUFFER_FRONT_LEFT will cause the server to copy the image from
1384 * the real front buffer to the fake front buffer. So before doing the
1385 * query, we need to make sure all the pending drawing has landed in the
1386 * real front buffer.
1388 intel_batchbuffer_flush(brw
);
1389 intel_flush_front(&brw
->ctx
);
1391 attachments
[i
++] = __DRI_BUFFER_FRONT_LEFT
;
1392 attachments
[i
++] = intel_bits_per_pixel(front_rb
);
1393 } else if (front_rb
&& brw
->front_buffer_dirty
) {
1394 /* We have pending front buffer rendering, but we aren't querying for a
1395 * front buffer. If the front buffer we have is a fake front buffer,
1396 * the X server is going to throw it away when it processes the query.
1397 * So before doing the query, make sure all the pending drawing has
1398 * landed in the real front buffer.
1400 intel_batchbuffer_flush(brw
);
1401 intel_flush_front(&brw
->ctx
);
1405 attachments
[i
++] = __DRI_BUFFER_BACK_LEFT
;
1406 attachments
[i
++] = intel_bits_per_pixel(back_rb
);
1409 assert(i
<= ARRAY_SIZE(attachments
));
1411 *buffers
= screen
->dri2
.loader
->getBuffersWithFormat(drawable
,
1416 drawable
->loaderPrivate
);
1420 * \brief Assign a DRI buffer's DRM region to a renderbuffer.
1422 * This is called from intel_update_renderbuffers().
1425 * DRI buffers whose attachment point is DRI2BufferStencil or
1426 * DRI2BufferDepthStencil are handled as special cases.
1428 * \param buffer_name is a human readable name, such as "dri2 front buffer",
1429 * that is passed to drm_intel_bo_gem_create_from_name().
1431 * \see intel_update_renderbuffers()
1434 intel_process_dri2_buffer(struct brw_context
*brw
,
1435 __DRIdrawable
*drawable
,
1436 __DRIbuffer
*buffer
,
1437 struct intel_renderbuffer
*rb
,
1438 const char *buffer_name
)
1440 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
1446 unsigned num_samples
= rb
->Base
.Base
.NumSamples
;
1448 /* We try to avoid closing and reopening the same BO name, because the first
1449 * use of a mapping of the buffer involves a bunch of page faulting which is
1450 * moderately expensive.
1452 struct intel_mipmap_tree
*last_mt
;
1453 if (num_samples
== 0)
1456 last_mt
= rb
->singlesample_mt
;
1458 uint32_t old_name
= 0;
1460 /* The bo already has a name because the miptree was created by a
1461 * previous call to intel_process_dri2_buffer(). If a bo already has a
1462 * name, then drm_intel_bo_flink() is a low-cost getter. It does not
1463 * create a new name.
1465 drm_intel_bo_flink(last_mt
->bo
, &old_name
);
1468 if (old_name
== buffer
->name
)
1471 if (unlikely(INTEL_DEBUG
& DEBUG_DRI
)) {
1473 "attaching buffer %d, at %d, cpp %d, pitch %d\n",
1474 buffer
->name
, buffer
->attachment
,
1475 buffer
->cpp
, buffer
->pitch
);
1478 bo
= drm_intel_bo_gem_create_from_name(brw
->bufmgr
, buffer_name
,
1482 "Failed to open BO for returned DRI2 buffer "
1483 "(%dx%d, %s, named %d).\n"
1484 "This is likely a bug in the X Server that will lead to a "
1486 drawable
->w
, drawable
->h
, buffer_name
, buffer
->name
);
1490 intel_update_winsys_renderbuffer_miptree(brw
, rb
, bo
,
1491 drawable
->w
, drawable
->h
,
1494 if (_mesa_is_front_buffer_drawing(fb
) &&
1495 (buffer
->attachment
== __DRI_BUFFER_FRONT_LEFT
||
1496 buffer
->attachment
== __DRI_BUFFER_FAKE_FRONT_LEFT
) &&
1497 rb
->Base
.Base
.NumSamples
> 1) {
1498 intel_renderbuffer_upsample(brw
, rb
);
1503 drm_intel_bo_unreference(bo
);
1507 * \brief Query DRI image loader to obtain a DRIdrawable's buffers.
1509 * To determine which DRI buffers to request, examine the renderbuffers
1510 * attached to the drawable's framebuffer. Then request the buffers from
1513 * This is called from intel_update_renderbuffers().
1515 * \param drawable Drawable whose buffers are queried.
1516 * \param buffers [out] List of buffers returned by DRI2 query.
1517 * \param buffer_count [out] Number of buffers returned.
1519 * \see intel_update_renderbuffers()
1523 intel_update_image_buffer(struct brw_context
*intel
,
1524 __DRIdrawable
*drawable
,
1525 struct intel_renderbuffer
*rb
,
1527 enum __DRIimageBufferMask buffer_type
)
1529 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
1531 if (!rb
|| !buffer
->bo
)
1534 unsigned num_samples
= rb
->Base
.Base
.NumSamples
;
1536 /* Check and see if we're already bound to the right
1539 struct intel_mipmap_tree
*last_mt
;
1540 if (num_samples
== 0)
1543 last_mt
= rb
->singlesample_mt
;
1545 if (last_mt
&& last_mt
->bo
== buffer
->bo
)
1548 intel_update_winsys_renderbuffer_miptree(intel
, rb
, buffer
->bo
,
1549 buffer
->width
, buffer
->height
,
1552 if (_mesa_is_front_buffer_drawing(fb
) &&
1553 buffer_type
== __DRI_IMAGE_BUFFER_FRONT
&&
1554 rb
->Base
.Base
.NumSamples
> 1) {
1555 intel_renderbuffer_upsample(intel
, rb
);
1560 intel_update_image_buffers(struct brw_context
*brw
, __DRIdrawable
*drawable
)
1562 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
1563 __DRIscreen
*screen
= brw
->intelScreen
->driScrnPriv
;
1564 struct intel_renderbuffer
*front_rb
;
1565 struct intel_renderbuffer
*back_rb
;
1566 struct __DRIimageList images
;
1567 unsigned int format
;
1568 uint32_t buffer_mask
= 0;
1570 front_rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
1571 back_rb
= intel_get_renderbuffer(fb
, BUFFER_BACK_LEFT
);
1574 format
= intel_rb_format(back_rb
);
1576 format
= intel_rb_format(front_rb
);
1580 if (front_rb
&& (_mesa_is_front_buffer_drawing(fb
) ||
1581 _mesa_is_front_buffer_reading(fb
) || !back_rb
)) {
1582 buffer_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
1586 buffer_mask
|= __DRI_IMAGE_BUFFER_BACK
;
1588 (*screen
->image
.loader
->getBuffers
) (drawable
,
1589 driGLFormatToImageFormat(format
),
1590 &drawable
->dri2
.stamp
,
1591 drawable
->loaderPrivate
,
1595 if (images
.image_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
1596 drawable
->w
= images
.front
->width
;
1597 drawable
->h
= images
.front
->height
;
1598 intel_update_image_buffer(brw
,
1602 __DRI_IMAGE_BUFFER_FRONT
);
1604 if (images
.image_mask
& __DRI_IMAGE_BUFFER_BACK
) {
1605 drawable
->w
= images
.back
->width
;
1606 drawable
->h
= images
.back
->height
;
1607 intel_update_image_buffer(brw
,
1611 __DRI_IMAGE_BUFFER_BACK
);