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 dri2InvalidateDrawable(driContext
->driDrawablePriv
);
163 dri2InvalidateDrawable(driContext
->driReadablePriv
);
168 intel_update_state(struct gl_context
* ctx
, GLuint new_state
)
170 struct brw_context
*brw
= brw_context(ctx
);
171 struct intel_texture_object
*tex_obj
;
172 struct intel_renderbuffer
*depth_irb
;
174 if (ctx
->swrast_context
)
175 _swrast_InvalidateState(ctx
, new_state
);
176 _vbo_InvalidateState(ctx
, new_state
);
178 brw
->NewGLState
|= new_state
;
180 _mesa_unlock_context_textures(ctx
);
182 /* Resolve the depth buffer's HiZ buffer. */
183 depth_irb
= intel_get_renderbuffer(ctx
->DrawBuffer
, BUFFER_DEPTH
);
185 intel_renderbuffer_resolve_hiz(brw
, depth_irb
);
187 /* Resolve depth buffer and render cache of each enabled texture. */
188 int maxEnabledUnit
= ctx
->Texture
._MaxEnabledTexImageUnit
;
189 for (int i
= 0; i
<= maxEnabledUnit
; i
++) {
190 if (!ctx
->Texture
.Unit
[i
]._Current
)
192 tex_obj
= intel_texture_object(ctx
->Texture
.Unit
[i
]._Current
);
193 if (!tex_obj
|| !tex_obj
->mt
)
195 intel_miptree_all_slices_resolve_depth(brw
, tex_obj
->mt
);
196 intel_miptree_resolve_color(brw
, tex_obj
->mt
);
197 brw_render_cache_set_check_flush(brw
, tex_obj
->mt
->bo
);
200 _mesa_lock_context_textures(ctx
);
203 #define flushFront(screen) ((screen)->image.loader ? (screen)->image.loader->flushFrontBuffer : (screen)->dri2.loader->flushFrontBuffer)
206 intel_flush_front(struct gl_context
*ctx
)
208 struct brw_context
*brw
= brw_context(ctx
);
209 __DRIcontext
*driContext
= brw
->driContext
;
210 __DRIdrawable
*driDrawable
= driContext
->driDrawablePriv
;
211 __DRIscreen
*const screen
= brw
->intelScreen
->driScrnPriv
;
213 if (brw
->front_buffer_dirty
&& _mesa_is_winsys_fbo(ctx
->DrawBuffer
)) {
214 if (flushFront(screen
) && driDrawable
&&
215 driDrawable
->loaderPrivate
) {
217 /* Resolve before flushing FAKE_FRONT_LEFT to FRONT_LEFT.
219 * This potentially resolves both front and back buffer. It
220 * is unnecessary to resolve the back, but harms nothing except
221 * performance. And no one cares about front-buffer render
224 intel_resolve_for_dri2_flush(brw
, driDrawable
);
225 intel_batchbuffer_flush(brw
);
227 flushFront(screen
)(driDrawable
, driDrawable
->loaderPrivate
);
229 /* We set the dirty bit in intel_prepare_render() if we're
230 * front buffer rendering once we get there.
232 brw
->front_buffer_dirty
= false;
238 intel_glFlush(struct gl_context
*ctx
)
240 struct brw_context
*brw
= brw_context(ctx
);
242 intel_batchbuffer_flush(brw
);
243 intel_flush_front(ctx
);
245 brw
->need_flush_throttle
= true;
249 intel_finish(struct gl_context
* ctx
)
251 struct brw_context
*brw
= brw_context(ctx
);
255 if (brw
->batch
.last_bo
)
256 drm_intel_bo_wait_rendering(brw
->batch
.last_bo
);
260 brw_init_driver_functions(struct brw_context
*brw
,
261 struct dd_function_table
*functions
)
263 _mesa_init_driver_functions(functions
);
265 /* GLX uses DRI2 invalidate events to handle window resizing.
266 * Unfortunately, EGL does not - libEGL is written in XCB (not Xlib),
267 * which doesn't provide a mechanism for snooping the event queues.
269 * So EGL still relies on viewport hacks to handle window resizing.
270 * This should go away with DRI3000.
272 if (!brw
->driContext
->driScreenPriv
->dri2
.useInvalidate
)
273 functions
->Viewport
= intel_viewport
;
275 functions
->Flush
= intel_glFlush
;
276 functions
->Finish
= intel_finish
;
277 functions
->GetString
= intel_get_string
;
278 functions
->UpdateState
= intel_update_state
;
280 intelInitTextureFuncs(functions
);
281 intelInitTextureImageFuncs(functions
);
282 intelInitTextureSubImageFuncs(functions
);
283 intelInitTextureCopyImageFuncs(functions
);
284 intelInitCopyImageFuncs(functions
);
285 intelInitClearFuncs(functions
);
286 intelInitBufferFuncs(functions
);
287 intelInitPixelFuncs(functions
);
288 intelInitBufferObjectFuncs(functions
);
289 intel_init_syncobj_functions(functions
);
290 brw_init_object_purgeable_functions(functions
);
292 brwInitFragProgFuncs( functions
);
293 brw_init_common_queryobj_functions(functions
);
295 gen6_init_queryobj_functions(functions
);
297 gen4_init_queryobj_functions(functions
);
298 brw_init_compute_functions(functions
);
300 brw_init_conditional_render_functions(functions
);
302 functions
->QuerySamplesForFormat
= brw_query_samples_for_format
;
304 functions
->NewTransformFeedback
= brw_new_transform_feedback
;
305 functions
->DeleteTransformFeedback
= brw_delete_transform_feedback
;
306 functions
->GetTransformFeedbackVertexCount
=
307 brw_get_transform_feedback_vertex_count
;
309 functions
->BeginTransformFeedback
= gen7_begin_transform_feedback
;
310 functions
->EndTransformFeedback
= gen7_end_transform_feedback
;
311 functions
->PauseTransformFeedback
= gen7_pause_transform_feedback
;
312 functions
->ResumeTransformFeedback
= gen7_resume_transform_feedback
;
314 functions
->BeginTransformFeedback
= brw_begin_transform_feedback
;
315 functions
->EndTransformFeedback
= brw_end_transform_feedback
;
319 functions
->GetSamplePosition
= gen6_get_sample_position
;
323 brw_initialize_context_constants(struct brw_context
*brw
)
325 struct gl_context
*ctx
= &brw
->ctx
;
326 const struct brw_compiler
*compiler
= brw
->intelScreen
->compiler
;
328 const bool stage_exists
[MESA_SHADER_STAGES
] = {
329 [MESA_SHADER_VERTEX
] = true,
330 [MESA_SHADER_TESS_CTRL
] = brw
->gen
>= 8,
331 [MESA_SHADER_TESS_EVAL
] = brw
->gen
>= 8,
332 [MESA_SHADER_GEOMETRY
] = brw
->gen
>= 6,
333 [MESA_SHADER_FRAGMENT
] = true,
334 [MESA_SHADER_COMPUTE
] = _mesa_extension_override_enables
.ARB_compute_shader
,
337 unsigned num_stages
= 0;
338 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
343 unsigned max_samplers
=
344 brw
->gen
>= 8 || brw
->is_haswell
? BRW_MAX_TEX_UNIT
: 16;
346 ctx
->Const
.MaxDualSourceDrawBuffers
= 1;
347 ctx
->Const
.MaxDrawBuffers
= BRW_MAX_DRAW_BUFFERS
;
348 ctx
->Const
.MaxCombinedShaderOutputResources
=
349 MAX_IMAGE_UNITS
+ BRW_MAX_DRAW_BUFFERS
;
351 ctx
->Const
.QueryCounterBits
.Timestamp
= 36;
353 ctx
->Const
.MaxTextureCoordUnits
= 8; /* Mesa limit */
354 ctx
->Const
.MaxImageUnits
= MAX_IMAGE_UNITS
;
355 ctx
->Const
.MaxRenderbufferSize
= 8192;
356 ctx
->Const
.MaxTextureLevels
= MIN2(14 /* 8192 */, MAX_TEXTURE_LEVELS
);
357 ctx
->Const
.Max3DTextureLevels
= 12; /* 2048 */
358 ctx
->Const
.MaxCubeTextureLevels
= 14; /* 8192 */
359 ctx
->Const
.MaxArrayTextureLayers
= brw
->gen
>= 7 ? 2048 : 512;
360 ctx
->Const
.MaxTextureMbytes
= 1536;
361 ctx
->Const
.MaxTextureRectSize
= 1 << 12;
362 ctx
->Const
.MaxTextureMaxAnisotropy
= 16.0;
363 ctx
->Const
.StripTextureBorder
= true;
365 ctx
->Const
.MaxProgramTextureGatherComponents
= 4;
366 else if (brw
->gen
== 6)
367 ctx
->Const
.MaxProgramTextureGatherComponents
= 1;
369 ctx
->Const
.MaxUniformBlockSize
= 65536;
371 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
372 struct gl_program_constants
*prog
= &ctx
->Const
.Program
[i
];
374 if (!stage_exists
[i
])
377 prog
->MaxTextureImageUnits
= max_samplers
;
379 prog
->MaxUniformBlocks
= BRW_MAX_UBO
;
380 prog
->MaxCombinedUniformComponents
=
381 prog
->MaxUniformComponents
+
382 ctx
->Const
.MaxUniformBlockSize
/ 4 * prog
->MaxUniformBlocks
;
384 prog
->MaxAtomicCounters
= MAX_ATOMIC_COUNTERS
;
385 prog
->MaxAtomicBuffers
= BRW_MAX_ABO
;
386 prog
->MaxImageUniforms
= compiler
->scalar_stage
[i
] ? BRW_MAX_IMAGES
: 0;
387 prog
->MaxShaderStorageBlocks
= BRW_MAX_SSBO
;
390 ctx
->Const
.MaxTextureUnits
=
391 MIN2(ctx
->Const
.MaxTextureCoordUnits
,
392 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxTextureImageUnits
);
394 ctx
->Const
.MaxUniformBufferBindings
= num_stages
* BRW_MAX_UBO
;
395 ctx
->Const
.MaxCombinedUniformBlocks
= num_stages
* BRW_MAX_UBO
;
396 ctx
->Const
.MaxCombinedAtomicBuffers
= num_stages
* BRW_MAX_ABO
;
397 ctx
->Const
.MaxCombinedShaderStorageBlocks
= num_stages
* BRW_MAX_SSBO
;
398 ctx
->Const
.MaxShaderStorageBufferBindings
= num_stages
* BRW_MAX_SSBO
;
399 ctx
->Const
.MaxCombinedTextureImageUnits
= num_stages
* max_samplers
;
400 ctx
->Const
.MaxCombinedImageUniforms
= num_stages
* BRW_MAX_IMAGES
;
403 /* Hardware only supports a limited number of transform feedback buffers.
404 * So we need to override the Mesa default (which is based only on software
407 ctx
->Const
.MaxTransformFeedbackBuffers
= BRW_MAX_SOL_BUFFERS
;
409 /* On Gen6, in the worst case, we use up one binding table entry per
410 * transform feedback component (see comments above the definition of
411 * BRW_MAX_SOL_BINDINGS, in brw_context.h), so we need to advertise a value
412 * for MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS equal to
413 * BRW_MAX_SOL_BINDINGS.
415 * In "separate components" mode, we need to divide this value by
416 * BRW_MAX_SOL_BUFFERS, so that the total number of binding table entries
417 * used up by all buffers will not exceed BRW_MAX_SOL_BINDINGS.
419 ctx
->Const
.MaxTransformFeedbackInterleavedComponents
= BRW_MAX_SOL_BINDINGS
;
420 ctx
->Const
.MaxTransformFeedbackSeparateComponents
=
421 BRW_MAX_SOL_BINDINGS
/ BRW_MAX_SOL_BUFFERS
;
423 ctx
->Const
.AlwaysUseGetTransformFeedbackVertexCount
= true;
426 const int *msaa_modes
= intel_supported_msaa_modes(brw
->intelScreen
);
427 const int clamp_max_samples
=
428 driQueryOptioni(&brw
->optionCache
, "clamp_max_samples");
430 if (clamp_max_samples
< 0) {
431 max_samples
= msaa_modes
[0];
433 /* Select the largest supported MSAA mode that does not exceed
437 for (int i
= 0; msaa_modes
[i
] != 0; ++i
) {
438 if (msaa_modes
[i
] <= clamp_max_samples
) {
439 max_samples
= msaa_modes
[i
];
445 ctx
->Const
.MaxSamples
= max_samples
;
446 ctx
->Const
.MaxColorTextureSamples
= max_samples
;
447 ctx
->Const
.MaxDepthTextureSamples
= max_samples
;
448 ctx
->Const
.MaxIntegerSamples
= max_samples
;
449 ctx
->Const
.MaxImageSamples
= 0;
451 /* gen6_set_sample_maps() sets SampleMap{2,4,8}x variables which are used
452 * to map indices of rectangular grid to sample numbers within a pixel.
453 * These variables are used by GL_EXT_framebuffer_multisample_blit_scaled
454 * extension implementation. For more details see the comment above
455 * gen6_set_sample_maps() definition.
457 gen6_set_sample_maps(ctx
);
459 ctx
->Const
.MinLineWidth
= 1.0;
460 ctx
->Const
.MinLineWidthAA
= 1.0;
462 ctx
->Const
.MaxLineWidth
= 7.375;
463 ctx
->Const
.MaxLineWidthAA
= 7.375;
464 ctx
->Const
.LineWidthGranularity
= 0.125;
466 ctx
->Const
.MaxLineWidth
= 7.0;
467 ctx
->Const
.MaxLineWidthAA
= 7.0;
468 ctx
->Const
.LineWidthGranularity
= 0.5;
471 /* For non-antialiased lines, we have to round the line width to the
472 * nearest whole number. Make sure that we don't advertise a line
473 * width that, when rounded, will be beyond the actual hardware
476 assert(roundf(ctx
->Const
.MaxLineWidth
) <= ctx
->Const
.MaxLineWidth
);
478 ctx
->Const
.MinPointSize
= 1.0;
479 ctx
->Const
.MinPointSizeAA
= 1.0;
480 ctx
->Const
.MaxPointSize
= 255.0;
481 ctx
->Const
.MaxPointSizeAA
= 255.0;
482 ctx
->Const
.PointSizeGranularity
= 1.0;
484 if (brw
->gen
>= 5 || brw
->is_g4x
)
485 ctx
->Const
.MaxClipPlanes
= 8;
487 ctx
->Const
.LowerTessLevel
= true;
489 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeInstructions
= 16 * 1024;
490 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxAluInstructions
= 0;
491 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTexInstructions
= 0;
492 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTexIndirections
= 0;
493 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeAluInstructions
= 0;
494 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeTexInstructions
= 0;
495 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeTexIndirections
= 0;
496 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeAttribs
= 16;
497 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeTemps
= 256;
498 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeAddressRegs
= 1;
499 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeParameters
= 1024;
500 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxEnvParams
=
501 MIN2(ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeParameters
,
502 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxEnvParams
);
504 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeInstructions
= 1024;
505 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeAluInstructions
= 1024;
506 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeTexInstructions
= 1024;
507 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeTexIndirections
= 1024;
508 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeAttribs
= 12;
509 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeTemps
= 256;
510 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeAddressRegs
= 0;
511 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeParameters
= 1024;
512 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxEnvParams
=
513 MIN2(ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeParameters
,
514 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxEnvParams
);
516 /* Fragment shaders use real, 32-bit twos-complement integers for all
519 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].LowInt
.RangeMin
= 31;
520 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].LowInt
.RangeMax
= 30;
521 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].LowInt
.Precision
= 0;
522 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].HighInt
= ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].LowInt
;
523 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MediumInt
= ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].LowInt
;
525 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].LowInt
.RangeMin
= 31;
526 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].LowInt
.RangeMax
= 30;
527 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].LowInt
.Precision
= 0;
528 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].HighInt
= ctx
->Const
.Program
[MESA_SHADER_VERTEX
].LowInt
;
529 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MediumInt
= ctx
->Const
.Program
[MESA_SHADER_VERTEX
].LowInt
;
531 /* Gen6 converts quads to polygon in beginning of 3D pipeline,
532 * but we're not sure how it's actually done for vertex order,
533 * that affect provoking vertex decision. Always use last vertex
534 * convention for quad primitive which works as expected for now.
537 ctx
->Const
.QuadsFollowProvokingVertexConvention
= false;
539 ctx
->Const
.NativeIntegers
= true;
540 ctx
->Const
.VertexID_is_zero_based
= true;
542 /* Regarding the CMP instruction, the Ivybridge PRM says:
544 * "For each enabled channel 0b or 1b is assigned to the appropriate flag
545 * bit and 0/all zeros or all ones (e.g, byte 0xFF, word 0xFFFF, DWord
546 * 0xFFFFFFFF) is assigned to dst."
548 * but PRMs for earlier generations say
550 * "In dword format, one GRF may store up to 8 results. When the register
551 * is used later as a vector of Booleans, as only LSB at each channel
552 * contains meaning [sic] data, software should make sure all higher bits
553 * are masked out (e.g. by 'and-ing' an [sic] 0x01 constant)."
555 * We select the representation of a true boolean uniform to be ~0, and fix
556 * the results of Gen <= 5 CMP instruction's with -(result & 1).
558 ctx
->Const
.UniformBooleanTrue
= ~0;
560 /* From the gen4 PRM, volume 4 page 127:
562 * "For SURFTYPE_BUFFER non-rendertarget surfaces, this field specifies
563 * the base address of the first element of the surface, computed in
564 * software by adding the surface base address to the byte offset of
565 * the element in the buffer."
567 * However, unaligned accesses are slower, so enforce buffer alignment.
569 ctx
->Const
.UniformBufferOffsetAlignment
= 16;
571 /* ShaderStorageBufferOffsetAlignment should be a cacheline (64 bytes) so
572 * that we can safely have the CPU and GPU writing the same SSBO on
573 * non-cachecoherent systems (our Atom CPUs). With UBOs, the GPU never
574 * writes, so there's no problem. For an SSBO, the GPU and the CPU can
575 * be updating disjoint regions of the buffer simultaneously and that will
576 * break if the regions overlap the same cacheline.
578 ctx
->Const
.ShaderStorageBufferOffsetAlignment
= 64;
579 ctx
->Const
.TextureBufferOffsetAlignment
= 16;
580 ctx
->Const
.MaxTextureBufferSize
= 128 * 1024 * 1024;
583 ctx
->Const
.MaxVarying
= 32;
584 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxOutputComponents
= 128;
585 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxInputComponents
= 64;
586 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxOutputComponents
= 128;
587 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxInputComponents
= 128;
588 ctx
->Const
.Program
[MESA_SHADER_TESS_CTRL
].MaxInputComponents
= 128;
589 ctx
->Const
.Program
[MESA_SHADER_TESS_CTRL
].MaxOutputComponents
= 128;
590 ctx
->Const
.Program
[MESA_SHADER_TESS_EVAL
].MaxInputComponents
= 128;
591 ctx
->Const
.Program
[MESA_SHADER_TESS_EVAL
].MaxOutputComponents
= 128;
594 /* We want the GLSL compiler to emit code that uses condition codes */
595 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
596 ctx
->Const
.ShaderCompilerOptions
[i
] =
597 brw
->intelScreen
->compiler
->glsl_compiler_options
[i
];
600 /* ARB_viewport_array */
601 if (brw
->gen
>= 6 && ctx
->API
== API_OPENGL_CORE
) {
602 ctx
->Const
.MaxViewports
= GEN6_NUM_VIEWPORTS
;
603 ctx
->Const
.ViewportSubpixelBits
= 0;
605 /* Cast to float before negating because MaxViewportWidth is unsigned.
607 ctx
->Const
.ViewportBounds
.Min
= -(float)ctx
->Const
.MaxViewportWidth
;
608 ctx
->Const
.ViewportBounds
.Max
= ctx
->Const
.MaxViewportWidth
;
611 /* ARB_gpu_shader5 */
613 ctx
->Const
.MaxVertexStreams
= MIN2(4, MAX_VERTEX_STREAMS
);
615 /* ARB_framebuffer_no_attachments */
616 ctx
->Const
.MaxFramebufferWidth
= ctx
->Const
.MaxViewportWidth
;
617 ctx
->Const
.MaxFramebufferHeight
= ctx
->Const
.MaxViewportHeight
;
618 ctx
->Const
.MaxFramebufferLayers
= ctx
->Const
.MaxArrayTextureLayers
;
619 ctx
->Const
.MaxFramebufferSamples
= max_samples
;
623 brw_adjust_cs_context_constants(struct brw_context
*brw
)
625 struct gl_context
*ctx
= &brw
->ctx
;
627 /* For ES, we set these constants based on SIMD8.
629 * TODO: Once we can always generate SIMD16, we should update this.
631 * For GL, we assume we can generate a SIMD16 program, but this currently
632 * is not always true. This allows us to run more test cases, and will be
633 * required based on desktop GL compute shader requirements.
635 const int simd_size
= ctx
->API
== API_OPENGL_CORE
? 16 : 8;
637 const uint32_t max_invocations
= simd_size
* brw
->max_cs_threads
;
638 ctx
->Const
.MaxComputeWorkGroupSize
[0] = max_invocations
;
639 ctx
->Const
.MaxComputeWorkGroupSize
[1] = max_invocations
;
640 ctx
->Const
.MaxComputeWorkGroupSize
[2] = max_invocations
;
641 ctx
->Const
.MaxComputeWorkGroupInvocations
= max_invocations
;
645 * Process driconf (drirc) options, setting appropriate context flags.
647 * intelInitExtensions still pokes at optionCache directly, in order to
648 * avoid advertising various extensions. No flags are set, so it makes
649 * sense to continue doing that there.
652 brw_process_driconf_options(struct brw_context
*brw
)
654 struct gl_context
*ctx
= &brw
->ctx
;
656 driOptionCache
*options
= &brw
->optionCache
;
657 driParseConfigFiles(options
, &brw
->intelScreen
->optionCache
,
658 brw
->driContext
->driScreenPriv
->myNum
, "i965");
660 int bo_reuse_mode
= driQueryOptioni(options
, "bo_reuse");
661 switch (bo_reuse_mode
) {
662 case DRI_CONF_BO_REUSE_DISABLED
:
664 case DRI_CONF_BO_REUSE_ALL
:
665 intel_bufmgr_gem_enable_reuse(brw
->bufmgr
);
669 if (!driQueryOptionb(options
, "hiz")) {
670 brw
->has_hiz
= false;
671 /* On gen6, you can only do separate stencil with HIZ. */
673 brw
->has_separate_stencil
= false;
676 if (driQueryOptionb(options
, "always_flush_batch")) {
677 fprintf(stderr
, "flushing batchbuffer before/after each draw call\n");
678 brw
->always_flush_batch
= true;
681 if (driQueryOptionb(options
, "always_flush_cache")) {
682 fprintf(stderr
, "flushing GPU caches before/after each draw call\n");
683 brw
->always_flush_cache
= true;
686 if (driQueryOptionb(options
, "disable_throttling")) {
687 fprintf(stderr
, "disabling flush throttling\n");
688 brw
->disable_throttling
= true;
691 brw
->precompile
= driQueryOptionb(&brw
->optionCache
, "shader_precompile");
693 ctx
->Const
.ForceGLSLExtensionsWarn
=
694 driQueryOptionb(options
, "force_glsl_extensions_warn");
696 ctx
->Const
.DisableGLSLLineContinuations
=
697 driQueryOptionb(options
, "disable_glsl_line_continuations");
699 ctx
->Const
.AllowGLSLExtensionDirectiveMidShader
=
700 driQueryOptionb(options
, "allow_glsl_extension_directive_midshader");
704 brwCreateContext(gl_api api
,
705 const struct gl_config
*mesaVis
,
706 __DRIcontext
*driContextPriv
,
707 unsigned major_version
,
708 unsigned minor_version
,
711 unsigned *dri_ctx_error
,
712 void *sharedContextPrivate
)
714 __DRIscreen
*sPriv
= driContextPriv
->driScreenPriv
;
715 struct gl_context
*shareCtx
= (struct gl_context
*) sharedContextPrivate
;
716 struct intel_screen
*screen
= sPriv
->driverPrivate
;
717 const struct brw_device_info
*devinfo
= screen
->devinfo
;
718 struct dd_function_table functions
;
720 /* Only allow the __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS flag if the kernel
721 * provides us with context reset notifications.
723 uint32_t allowed_flags
= __DRI_CTX_FLAG_DEBUG
724 | __DRI_CTX_FLAG_FORWARD_COMPATIBLE
;
726 if (screen
->has_context_reset_notification
)
727 allowed_flags
|= __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS
;
729 if (flags
& ~allowed_flags
) {
730 *dri_ctx_error
= __DRI_CTX_ERROR_UNKNOWN_FLAG
;
734 struct brw_context
*brw
= rzalloc(NULL
, struct brw_context
);
736 fprintf(stderr
, "%s: failed to alloc context\n", __func__
);
737 *dri_ctx_error
= __DRI_CTX_ERROR_NO_MEMORY
;
741 driContextPriv
->driverPrivate
= brw
;
742 brw
->driContext
= driContextPriv
;
743 brw
->intelScreen
= screen
;
744 brw
->bufmgr
= screen
->bufmgr
;
746 brw
->gen
= devinfo
->gen
;
747 brw
->gt
= devinfo
->gt
;
748 brw
->is_g4x
= devinfo
->is_g4x
;
749 brw
->is_baytrail
= devinfo
->is_baytrail
;
750 brw
->is_haswell
= devinfo
->is_haswell
;
751 brw
->is_cherryview
= devinfo
->is_cherryview
;
752 brw
->is_broxton
= devinfo
->is_broxton
;
753 brw
->has_llc
= devinfo
->has_llc
;
754 brw
->has_hiz
= devinfo
->has_hiz_and_separate_stencil
;
755 brw
->has_separate_stencil
= devinfo
->has_hiz_and_separate_stencil
;
756 brw
->has_pln
= devinfo
->has_pln
;
757 brw
->has_compr4
= devinfo
->has_compr4
;
758 brw
->has_surface_tile_offset
= devinfo
->has_surface_tile_offset
;
759 brw
->has_negative_rhw_bug
= devinfo
->has_negative_rhw_bug
;
760 brw
->needs_unlit_centroid_workaround
=
761 devinfo
->needs_unlit_centroid_workaround
;
763 brw
->must_use_separate_stencil
= screen
->hw_must_use_separate_stencil
;
764 brw
->has_swizzling
= screen
->hw_has_swizzling
;
766 brw
->vs
.base
.stage
= MESA_SHADER_VERTEX
;
767 brw
->tcs
.base
.stage
= MESA_SHADER_TESS_CTRL
;
768 brw
->tes
.base
.stage
= MESA_SHADER_TESS_EVAL
;
769 brw
->gs
.base
.stage
= MESA_SHADER_GEOMETRY
;
770 brw
->wm
.base
.stage
= MESA_SHADER_FRAGMENT
;
772 gen8_init_vtable_surface_functions(brw
);
773 brw
->vtbl
.emit_depth_stencil_hiz
= gen8_emit_depth_stencil_hiz
;
774 } else if (brw
->gen
>= 7) {
775 gen7_init_vtable_surface_functions(brw
);
776 brw
->vtbl
.emit_depth_stencil_hiz
= gen7_emit_depth_stencil_hiz
;
777 } else if (brw
->gen
>= 6) {
778 gen6_init_vtable_surface_functions(brw
);
779 brw
->vtbl
.emit_depth_stencil_hiz
= gen6_emit_depth_stencil_hiz
;
781 gen4_init_vtable_surface_functions(brw
);
782 brw
->vtbl
.emit_depth_stencil_hiz
= brw_emit_depth_stencil_hiz
;
785 brw_init_driver_functions(brw
, &functions
);
788 functions
.GetGraphicsResetStatus
= brw_get_graphics_reset_status
;
790 struct gl_context
*ctx
= &brw
->ctx
;
792 if (!_mesa_initialize_context(ctx
, api
, mesaVis
, shareCtx
, &functions
)) {
793 *dri_ctx_error
= __DRI_CTX_ERROR_NO_MEMORY
;
794 fprintf(stderr
, "%s: failed to init mesa context\n", __func__
);
795 intelDestroyContext(driContextPriv
);
799 driContextSetFlags(ctx
, flags
);
801 /* Initialize the software rasterizer and helper modules.
803 * As of GL 3.1 core, the gen4+ driver doesn't need the swrast context for
804 * software fallbacks (which we have to support on legacy GL to do weird
805 * glDrawPixels(), glBitmap(), and other functions).
807 if (api
!= API_OPENGL_CORE
&& api
!= API_OPENGLES2
) {
808 _swrast_CreateContext(ctx
);
811 _vbo_CreateContext(ctx
);
812 if (ctx
->swrast_context
) {
813 _tnl_CreateContext(ctx
);
814 TNL_CONTEXT(ctx
)->Driver
.RunPipeline
= _tnl_run_pipeline
;
815 _swsetup_CreateContext(ctx
);
817 /* Configure swrast to match hardware characteristics: */
818 _swrast_allow_pixel_fog(ctx
, false);
819 _swrast_allow_vertex_fog(ctx
, true);
822 _mesa_meta_init(ctx
);
824 brw_process_driconf_options(brw
);
826 if (INTEL_DEBUG
& DEBUG_PERF
)
827 brw
->perf_debug
= true;
829 brw_initialize_context_constants(brw
);
831 ctx
->Const
.ResetStrategy
= notify_reset
832 ? GL_LOSE_CONTEXT_ON_RESET_ARB
: GL_NO_RESET_NOTIFICATION_ARB
;
834 /* Reinitialize the context point state. It depends on ctx->Const values. */
835 _mesa_init_point(ctx
);
839 intel_batchbuffer_init(brw
);
842 /* Create a new hardware context. Using a hardware context means that
843 * our GPU state will be saved/restored on context switch, allowing us
844 * to assume that the GPU is in the same state we left it in.
846 * This is required for transform feedback buffer offsets, query objects,
847 * and also allows us to reduce how much state we have to emit.
849 brw
->hw_ctx
= drm_intel_gem_context_create(brw
->bufmgr
);
852 fprintf(stderr
, "Gen6+ requires Kernel 3.6 or later.\n");
853 intelDestroyContext(driContextPriv
);
858 if (brw_init_pipe_control(brw
, devinfo
)) {
859 *dri_ctx_error
= __DRI_CTX_ERROR_NO_MEMORY
;
860 intelDestroyContext(driContextPriv
);
866 intelInitExtensions(ctx
);
868 brw_init_surface_formats(brw
);
870 brw
->max_vs_threads
= devinfo
->max_vs_threads
;
871 brw
->max_hs_threads
= devinfo
->max_hs_threads
;
872 brw
->max_ds_threads
= devinfo
->max_ds_threads
;
873 brw
->max_gs_threads
= devinfo
->max_gs_threads
;
874 brw
->max_wm_threads
= devinfo
->max_wm_threads
;
875 brw
->max_cs_threads
= devinfo
->max_cs_threads
;
876 brw
->urb
.size
= devinfo
->urb
.size
;
877 brw
->urb
.min_vs_entries
= devinfo
->urb
.min_vs_entries
;
878 brw
->urb
.max_vs_entries
= devinfo
->urb
.max_vs_entries
;
879 brw
->urb
.max_hs_entries
= devinfo
->urb
.max_hs_entries
;
880 brw
->urb
.max_ds_entries
= devinfo
->urb
.max_ds_entries
;
881 brw
->urb
.max_gs_entries
= devinfo
->urb
.max_gs_entries
;
883 brw_adjust_cs_context_constants(brw
);
885 /* Estimate the size of the mappable aperture into the GTT. There's an
886 * ioctl to get the whole GTT size, but not one to get the mappable subset.
887 * It turns out it's basically always 256MB, though some ancient hardware
890 uint32_t gtt_size
= 256 * 1024 * 1024;
892 /* We don't want to map two objects such that a memcpy between them would
893 * just fault one mapping in and then the other over and over forever. So
894 * we would need to divide the GTT size by 2. Additionally, some GTT is
895 * taken up by things like the framebuffer and the ringbuffer and such, so
896 * be more conservative.
898 brw
->max_gtt_map_object_size
= gtt_size
/ 4;
901 brw
->urb
.gs_present
= false;
903 brw
->prim_restart
.in_progress
= false;
904 brw
->prim_restart
.enable_cut_index
= false;
905 brw
->gs
.enabled
= false;
906 brw
->sf
.viewport_transform_enable
= true;
908 brw
->predicate
.state
= BRW_PREDICATE_STATE_RENDER
;
910 brw
->use_resource_streamer
= screen
->has_resource_streamer
&&
911 (env_var_as_boolean("INTEL_USE_HW_BT", false) ||
912 env_var_as_boolean("INTEL_USE_GATHER", false));
914 ctx
->VertexProgram
._MaintainTnlProgram
= true;
915 ctx
->FragmentProgram
._MaintainTexEnvProgram
= true;
917 brw_draw_init( brw
);
919 if ((flags
& __DRI_CTX_FLAG_DEBUG
) != 0) {
920 /* Turn on some extra GL_ARB_debug_output generation. */
921 brw
->perf_debug
= true;
924 if ((flags
& __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS
) != 0)
925 ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_ROBUST_ACCESS_BIT_ARB
;
927 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
)
928 brw_init_shader_time(brw
);
930 _mesa_compute_version(ctx
);
932 _mesa_initialize_dispatch_tables(ctx
);
933 _mesa_initialize_vbo_vtxfmt(ctx
);
935 if (ctx
->Extensions
.AMD_performance_monitor
) {
936 brw_init_performance_monitors(brw
);
939 vbo_use_buffer_objects(ctx
);
940 vbo_always_unmap_buffers(ctx
);
946 intelDestroyContext(__DRIcontext
* driContextPriv
)
948 struct brw_context
*brw
=
949 (struct brw_context
*) driContextPriv
->driverPrivate
;
950 struct gl_context
*ctx
= &brw
->ctx
;
952 /* Dump a final BMP in case the application doesn't call SwapBuffers */
953 if (INTEL_DEBUG
& DEBUG_AUB
) {
954 intel_batchbuffer_flush(brw
);
955 aub_dump_bmp(&brw
->ctx
);
958 _mesa_meta_free(&brw
->ctx
);
959 brw_meta_fast_clear_free(brw
);
961 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
) {
962 /* Force a report. */
963 brw
->shader_time
.report_time
= 0;
965 brw_collect_and_report_shader_time(brw
);
966 brw_destroy_shader_time(brw
);
969 brw_destroy_state(brw
);
970 brw_draw_destroy(brw
);
972 drm_intel_bo_unreference(brw
->curbe
.curbe_bo
);
973 if (brw
->vs
.base
.scratch_bo
)
974 drm_intel_bo_unreference(brw
->vs
.base
.scratch_bo
);
975 if (brw
->gs
.base
.scratch_bo
)
976 drm_intel_bo_unreference(brw
->gs
.base
.scratch_bo
);
977 if (brw
->wm
.base
.scratch_bo
)
978 drm_intel_bo_unreference(brw
->wm
.base
.scratch_bo
);
980 gen7_reset_hw_bt_pool_offsets(brw
);
981 drm_intel_bo_unreference(brw
->hw_bt_pool
.bo
);
982 brw
->hw_bt_pool
.bo
= NULL
;
984 drm_intel_gem_context_destroy(brw
->hw_ctx
);
986 if (ctx
->swrast_context
) {
987 _swsetup_DestroyContext(&brw
->ctx
);
988 _tnl_DestroyContext(&brw
->ctx
);
990 _vbo_DestroyContext(&brw
->ctx
);
992 if (ctx
->swrast_context
)
993 _swrast_DestroyContext(&brw
->ctx
);
995 brw_fini_pipe_control(brw
);
996 intel_batchbuffer_free(brw
);
998 drm_intel_bo_unreference(brw
->throttle_batch
[1]);
999 drm_intel_bo_unreference(brw
->throttle_batch
[0]);
1000 brw
->throttle_batch
[1] = NULL
;
1001 brw
->throttle_batch
[0] = NULL
;
1003 driDestroyOptionCache(&brw
->optionCache
);
1005 /* free the Mesa context */
1006 _mesa_free_context_data(&brw
->ctx
);
1009 driContextPriv
->driverPrivate
= NULL
;
1013 intelUnbindContext(__DRIcontext
* driContextPriv
)
1015 /* Unset current context and dispath table */
1016 _mesa_make_current(NULL
, NULL
, NULL
);
1022 * Fixes up the context for GLES23 with our default-to-sRGB-capable behavior
1023 * on window system framebuffers.
1025 * Desktop GL is fairly reasonable in its handling of sRGB: You can ask if
1026 * your renderbuffer can do sRGB encode, and you can flip a switch that does
1027 * sRGB encode if the renderbuffer can handle it. You can ask specifically
1028 * for a visual where you're guaranteed to be capable, but it turns out that
1029 * everyone just makes all their ARGB8888 visuals capable and doesn't offer
1030 * incapable ones, because there's no difference between the two in resources
1031 * used. Applications thus get built that accidentally rely on the default
1032 * visual choice being sRGB, so we make ours sRGB capable. Everything sounds
1035 * But for GLES2/3, they decided that it was silly to not turn on sRGB encode
1036 * for sRGB renderbuffers you made with the GL_EXT_texture_sRGB equivalent.
1037 * So they removed the enable knob and made it "if the renderbuffer is sRGB
1038 * capable, do sRGB encode". Then, for your window system renderbuffers, you
1039 * can ask for sRGB visuals and get sRGB encode, or not ask for sRGB visuals
1040 * and get no sRGB encode (assuming that both kinds of visual are available).
1041 * Thus our choice to support sRGB by default on our visuals for desktop would
1042 * result in broken rendering of GLES apps that aren't expecting sRGB encode.
1044 * Unfortunately, renderbuffer setup happens before a context is created. So
1045 * in intel_screen.c we always set up sRGB, and here, if you're a GLES2/3
1046 * context (without an sRGB visual, though we don't have sRGB visuals exposed
1047 * yet), we go turn that back off before anyone finds out.
1050 intel_gles3_srgb_workaround(struct brw_context
*brw
,
1051 struct gl_framebuffer
*fb
)
1053 struct gl_context
*ctx
= &brw
->ctx
;
1055 if (_mesa_is_desktop_gl(ctx
) || !fb
->Visual
.sRGBCapable
)
1058 /* Some day when we support the sRGB capable bit on visuals available for
1059 * GLES, we'll need to respect that and not disable things here.
1061 fb
->Visual
.sRGBCapable
= false;
1062 for (int i
= 0; i
< BUFFER_COUNT
; i
++) {
1063 if (fb
->Attachment
[i
].Renderbuffer
&&
1064 fb
->Attachment
[i
].Renderbuffer
->Format
== MESA_FORMAT_B8G8R8A8_SRGB
) {
1065 fb
->Attachment
[i
].Renderbuffer
->Format
= MESA_FORMAT_B8G8R8A8_UNORM
;
1071 intelMakeCurrent(__DRIcontext
* driContextPriv
,
1072 __DRIdrawable
* driDrawPriv
,
1073 __DRIdrawable
* driReadPriv
)
1075 struct brw_context
*brw
;
1076 GET_CURRENT_CONTEXT(curCtx
);
1079 brw
= (struct brw_context
*) driContextPriv
->driverPrivate
;
1083 /* According to the glXMakeCurrent() man page: "Pending commands to
1084 * the previous context, if any, are flushed before it is released."
1085 * But only flush if we're actually changing contexts.
1087 if (brw_context(curCtx
) && brw_context(curCtx
) != brw
) {
1088 _mesa_flush(curCtx
);
1091 if (driContextPriv
) {
1092 struct gl_context
*ctx
= &brw
->ctx
;
1093 struct gl_framebuffer
*fb
, *readFb
;
1095 if (driDrawPriv
== NULL
) {
1096 fb
= _mesa_get_incomplete_framebuffer();
1098 fb
= driDrawPriv
->driverPrivate
;
1099 driContextPriv
->dri2
.draw_stamp
= driDrawPriv
->dri2
.stamp
- 1;
1102 if (driReadPriv
== NULL
) {
1103 readFb
= _mesa_get_incomplete_framebuffer();
1105 readFb
= driReadPriv
->driverPrivate
;
1106 driContextPriv
->dri2
.read_stamp
= driReadPriv
->dri2
.stamp
- 1;
1109 /* The sRGB workaround changes the renderbuffer's format. We must change
1110 * the format before the renderbuffer's miptree get's allocated, otherwise
1111 * the formats of the renderbuffer and its miptree will differ.
1113 intel_gles3_srgb_workaround(brw
, fb
);
1114 intel_gles3_srgb_workaround(brw
, readFb
);
1116 /* If the context viewport hasn't been initialized, force a call out to
1117 * the loader to get buffers so we have a drawable size for the initial
1119 if (!brw
->ctx
.ViewportInitialized
)
1120 intel_prepare_render(brw
);
1122 _mesa_make_current(ctx
, fb
, readFb
);
1124 _mesa_make_current(NULL
, NULL
, NULL
);
1131 intel_resolve_for_dri2_flush(struct brw_context
*brw
,
1132 __DRIdrawable
*drawable
)
1135 /* MSAA and fast color clear are not supported, so don't waste time
1136 * checking whether a resolve is needed.
1141 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
1142 struct intel_renderbuffer
*rb
;
1144 /* Usually, only the back buffer will need to be downsampled. However,
1145 * the front buffer will also need it if the user has rendered into it.
1147 static const gl_buffer_index buffers
[2] = {
1152 for (int i
= 0; i
< 2; ++i
) {
1153 rb
= intel_get_renderbuffer(fb
, buffers
[i
]);
1154 if (rb
== NULL
|| rb
->mt
== NULL
)
1156 if (rb
->mt
->num_samples
<= 1)
1157 intel_miptree_resolve_color(brw
, rb
->mt
);
1159 intel_renderbuffer_downsample(brw
, rb
);
1164 intel_bits_per_pixel(const struct intel_renderbuffer
*rb
)
1166 return _mesa_get_format_bytes(intel_rb_format(rb
)) * 8;
1170 intel_query_dri2_buffers(struct brw_context
*brw
,
1171 __DRIdrawable
*drawable
,
1172 __DRIbuffer
**buffers
,
1176 intel_process_dri2_buffer(struct brw_context
*brw
,
1177 __DRIdrawable
*drawable
,
1178 __DRIbuffer
*buffer
,
1179 struct intel_renderbuffer
*rb
,
1180 const char *buffer_name
);
1183 intel_update_image_buffers(struct brw_context
*brw
, __DRIdrawable
*drawable
);
1186 intel_update_dri2_buffers(struct brw_context
*brw
, __DRIdrawable
*drawable
)
1188 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
1189 struct intel_renderbuffer
*rb
;
1190 __DRIbuffer
*buffers
= NULL
;
1192 const char *region_name
;
1194 /* Set this up front, so that in case our buffers get invalidated
1195 * while we're getting new buffers, we don't clobber the stamp and
1196 * thus ignore the invalidate. */
1197 drawable
->lastStamp
= drawable
->dri2
.stamp
;
1199 if (unlikely(INTEL_DEBUG
& DEBUG_DRI
))
1200 fprintf(stderr
, "enter %s, drawable %p\n", __func__
, drawable
);
1202 intel_query_dri2_buffers(brw
, drawable
, &buffers
, &count
);
1204 if (buffers
== NULL
)
1207 for (i
= 0; i
< count
; i
++) {
1208 switch (buffers
[i
].attachment
) {
1209 case __DRI_BUFFER_FRONT_LEFT
:
1210 rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
1211 region_name
= "dri2 front buffer";
1214 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
1215 rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
1216 region_name
= "dri2 fake front buffer";
1219 case __DRI_BUFFER_BACK_LEFT
:
1220 rb
= intel_get_renderbuffer(fb
, BUFFER_BACK_LEFT
);
1221 region_name
= "dri2 back buffer";
1224 case __DRI_BUFFER_DEPTH
:
1225 case __DRI_BUFFER_HIZ
:
1226 case __DRI_BUFFER_DEPTH_STENCIL
:
1227 case __DRI_BUFFER_STENCIL
:
1228 case __DRI_BUFFER_ACCUM
:
1231 "unhandled buffer attach event, attachment type %d\n",
1232 buffers
[i
].attachment
);
1236 intel_process_dri2_buffer(brw
, drawable
, &buffers
[i
], rb
, region_name
);
1242 intel_update_renderbuffers(__DRIcontext
*context
, __DRIdrawable
*drawable
)
1244 struct brw_context
*brw
= context
->driverPrivate
;
1245 __DRIscreen
*screen
= brw
->intelScreen
->driScrnPriv
;
1247 /* Set this up front, so that in case our buffers get invalidated
1248 * while we're getting new buffers, we don't clobber the stamp and
1249 * thus ignore the invalidate. */
1250 drawable
->lastStamp
= drawable
->dri2
.stamp
;
1252 if (unlikely(INTEL_DEBUG
& DEBUG_DRI
))
1253 fprintf(stderr
, "enter %s, drawable %p\n", __func__
, drawable
);
1255 if (screen
->image
.loader
)
1256 intel_update_image_buffers(brw
, drawable
);
1258 intel_update_dri2_buffers(brw
, drawable
);
1260 driUpdateFramebufferSize(&brw
->ctx
, drawable
);
1264 * intel_prepare_render should be called anywhere that curent read/drawbuffer
1265 * state is required.
1268 intel_prepare_render(struct brw_context
*brw
)
1270 struct gl_context
*ctx
= &brw
->ctx
;
1271 __DRIcontext
*driContext
= brw
->driContext
;
1272 __DRIdrawable
*drawable
;
1274 drawable
= driContext
->driDrawablePriv
;
1275 if (drawable
&& drawable
->dri2
.stamp
!= driContext
->dri2
.draw_stamp
) {
1276 if (drawable
->lastStamp
!= drawable
->dri2
.stamp
)
1277 intel_update_renderbuffers(driContext
, drawable
);
1278 driContext
->dri2
.draw_stamp
= drawable
->dri2
.stamp
;
1281 drawable
= driContext
->driReadablePriv
;
1282 if (drawable
&& drawable
->dri2
.stamp
!= driContext
->dri2
.read_stamp
) {
1283 if (drawable
->lastStamp
!= drawable
->dri2
.stamp
)
1284 intel_update_renderbuffers(driContext
, drawable
);
1285 driContext
->dri2
.read_stamp
= drawable
->dri2
.stamp
;
1288 /* If we're currently rendering to the front buffer, the rendering
1289 * that will happen next will probably dirty the front buffer. So
1290 * mark it as dirty here.
1292 if (_mesa_is_front_buffer_drawing(ctx
->DrawBuffer
))
1293 brw
->front_buffer_dirty
= true;
1297 * \brief Query DRI2 to obtain a DRIdrawable's buffers.
1299 * To determine which DRI buffers to request, examine the renderbuffers
1300 * attached to the drawable's framebuffer. Then request the buffers with
1301 * DRI2GetBuffers() or DRI2GetBuffersWithFormat().
1303 * This is called from intel_update_renderbuffers().
1305 * \param drawable Drawable whose buffers are queried.
1306 * \param buffers [out] List of buffers returned by DRI2 query.
1307 * \param buffer_count [out] Number of buffers returned.
1309 * \see intel_update_renderbuffers()
1310 * \see DRI2GetBuffers()
1311 * \see DRI2GetBuffersWithFormat()
1314 intel_query_dri2_buffers(struct brw_context
*brw
,
1315 __DRIdrawable
*drawable
,
1316 __DRIbuffer
**buffers
,
1319 __DRIscreen
*screen
= brw
->intelScreen
->driScrnPriv
;
1320 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
1322 unsigned attachments
[8];
1324 struct intel_renderbuffer
*front_rb
;
1325 struct intel_renderbuffer
*back_rb
;
1327 front_rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
1328 back_rb
= intel_get_renderbuffer(fb
, BUFFER_BACK_LEFT
);
1330 memset(attachments
, 0, sizeof(attachments
));
1331 if ((_mesa_is_front_buffer_drawing(fb
) ||
1332 _mesa_is_front_buffer_reading(fb
) ||
1333 !back_rb
) && front_rb
) {
1334 /* If a fake front buffer is in use, then querying for
1335 * __DRI_BUFFER_FRONT_LEFT will cause the server to copy the image from
1336 * the real front buffer to the fake front buffer. So before doing the
1337 * query, we need to make sure all the pending drawing has landed in the
1338 * real front buffer.
1340 intel_batchbuffer_flush(brw
);
1341 intel_flush_front(&brw
->ctx
);
1343 attachments
[i
++] = __DRI_BUFFER_FRONT_LEFT
;
1344 attachments
[i
++] = intel_bits_per_pixel(front_rb
);
1345 } else if (front_rb
&& brw
->front_buffer_dirty
) {
1346 /* We have pending front buffer rendering, but we aren't querying for a
1347 * front buffer. If the front buffer we have is a fake front buffer,
1348 * the X server is going to throw it away when it processes the query.
1349 * So before doing the query, make sure all the pending drawing has
1350 * landed in the real front buffer.
1352 intel_batchbuffer_flush(brw
);
1353 intel_flush_front(&brw
->ctx
);
1357 attachments
[i
++] = __DRI_BUFFER_BACK_LEFT
;
1358 attachments
[i
++] = intel_bits_per_pixel(back_rb
);
1361 assert(i
<= ARRAY_SIZE(attachments
));
1363 *buffers
= screen
->dri2
.loader
->getBuffersWithFormat(drawable
,
1368 drawable
->loaderPrivate
);
1372 * \brief Assign a DRI buffer's DRM region to a renderbuffer.
1374 * This is called from intel_update_renderbuffers().
1377 * DRI buffers whose attachment point is DRI2BufferStencil or
1378 * DRI2BufferDepthStencil are handled as special cases.
1380 * \param buffer_name is a human readable name, such as "dri2 front buffer",
1381 * that is passed to drm_intel_bo_gem_create_from_name().
1383 * \see intel_update_renderbuffers()
1386 intel_process_dri2_buffer(struct brw_context
*brw
,
1387 __DRIdrawable
*drawable
,
1388 __DRIbuffer
*buffer
,
1389 struct intel_renderbuffer
*rb
,
1390 const char *buffer_name
)
1392 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
1398 unsigned num_samples
= rb
->Base
.Base
.NumSamples
;
1400 /* We try to avoid closing and reopening the same BO name, because the first
1401 * use of a mapping of the buffer involves a bunch of page faulting which is
1402 * moderately expensive.
1404 struct intel_mipmap_tree
*last_mt
;
1405 if (num_samples
== 0)
1408 last_mt
= rb
->singlesample_mt
;
1410 uint32_t old_name
= 0;
1412 /* The bo already has a name because the miptree was created by a
1413 * previous call to intel_process_dri2_buffer(). If a bo already has a
1414 * name, then drm_intel_bo_flink() is a low-cost getter. It does not
1415 * create a new name.
1417 drm_intel_bo_flink(last_mt
->bo
, &old_name
);
1420 if (old_name
== buffer
->name
)
1423 if (unlikely(INTEL_DEBUG
& DEBUG_DRI
)) {
1425 "attaching buffer %d, at %d, cpp %d, pitch %d\n",
1426 buffer
->name
, buffer
->attachment
,
1427 buffer
->cpp
, buffer
->pitch
);
1430 bo
= drm_intel_bo_gem_create_from_name(brw
->bufmgr
, buffer_name
,
1434 "Failed to open BO for returned DRI2 buffer "
1435 "(%dx%d, %s, named %d).\n"
1436 "This is likely a bug in the X Server that will lead to a "
1438 drawable
->w
, drawable
->h
, buffer_name
, buffer
->name
);
1442 intel_update_winsys_renderbuffer_miptree(brw
, rb
, bo
,
1443 drawable
->w
, drawable
->h
,
1446 if (_mesa_is_front_buffer_drawing(fb
) &&
1447 (buffer
->attachment
== __DRI_BUFFER_FRONT_LEFT
||
1448 buffer
->attachment
== __DRI_BUFFER_FAKE_FRONT_LEFT
) &&
1449 rb
->Base
.Base
.NumSamples
> 1) {
1450 intel_renderbuffer_upsample(brw
, rb
);
1455 drm_intel_bo_unreference(bo
);
1459 * \brief Query DRI image loader to obtain a DRIdrawable's buffers.
1461 * To determine which DRI buffers to request, examine the renderbuffers
1462 * attached to the drawable's framebuffer. Then request the buffers from
1465 * This is called from intel_update_renderbuffers().
1467 * \param drawable Drawable whose buffers are queried.
1468 * \param buffers [out] List of buffers returned by DRI2 query.
1469 * \param buffer_count [out] Number of buffers returned.
1471 * \see intel_update_renderbuffers()
1475 intel_update_image_buffer(struct brw_context
*intel
,
1476 __DRIdrawable
*drawable
,
1477 struct intel_renderbuffer
*rb
,
1479 enum __DRIimageBufferMask buffer_type
)
1481 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
1483 if (!rb
|| !buffer
->bo
)
1486 unsigned num_samples
= rb
->Base
.Base
.NumSamples
;
1488 /* Check and see if we're already bound to the right
1491 struct intel_mipmap_tree
*last_mt
;
1492 if (num_samples
== 0)
1495 last_mt
= rb
->singlesample_mt
;
1497 if (last_mt
&& last_mt
->bo
== buffer
->bo
)
1500 intel_update_winsys_renderbuffer_miptree(intel
, rb
, buffer
->bo
,
1501 buffer
->width
, buffer
->height
,
1504 if (_mesa_is_front_buffer_drawing(fb
) &&
1505 buffer_type
== __DRI_IMAGE_BUFFER_FRONT
&&
1506 rb
->Base
.Base
.NumSamples
> 1) {
1507 intel_renderbuffer_upsample(intel
, rb
);
1512 intel_update_image_buffers(struct brw_context
*brw
, __DRIdrawable
*drawable
)
1514 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
1515 __DRIscreen
*screen
= brw
->intelScreen
->driScrnPriv
;
1516 struct intel_renderbuffer
*front_rb
;
1517 struct intel_renderbuffer
*back_rb
;
1518 struct __DRIimageList images
;
1519 unsigned int format
;
1520 uint32_t buffer_mask
= 0;
1522 front_rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
1523 back_rb
= intel_get_renderbuffer(fb
, BUFFER_BACK_LEFT
);
1526 format
= intel_rb_format(back_rb
);
1528 format
= intel_rb_format(front_rb
);
1532 if (front_rb
&& (_mesa_is_front_buffer_drawing(fb
) ||
1533 _mesa_is_front_buffer_reading(fb
) || !back_rb
)) {
1534 buffer_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
1538 buffer_mask
|= __DRI_IMAGE_BUFFER_BACK
;
1540 (*screen
->image
.loader
->getBuffers
) (drawable
,
1541 driGLFormatToImageFormat(format
),
1542 &drawable
->dri2
.stamp
,
1543 drawable
->loaderPrivate
,
1547 if (images
.image_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
1548 drawable
->w
= images
.front
->width
;
1549 drawable
->h
= images
.front
->height
;
1550 intel_update_image_buffer(brw
,
1554 __DRI_IMAGE_BUFFER_FRONT
);
1556 if (images
.image_mask
& __DRI_IMAGE_BUFFER_BACK
) {
1557 drawable
->w
= images
.back
->width
;
1558 drawable
->h
= images
.back
->height
;
1559 intel_update_image_buffer(brw
,
1563 __DRI_IMAGE_BUFFER_BACK
);