2 Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
3 Copyright (C) Intel Corp. 2006. All Rights Reserved.
4 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) 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 <keith@tungstengraphics.com>
34 #include "main/api_exec.h"
35 #include "main/context.h"
36 #include "main/fbobject.h"
37 #include "main/imports.h"
38 #include "main/macros.h"
39 #include "main/points.h"
40 #include "main/version.h"
41 #include "main/vtxfmt.h"
43 #include "vbo/vbo_context.h"
45 #include "drivers/common/driverfuncs.h"
46 #include "drivers/common/meta.h"
49 #include "brw_context.h"
50 #include "brw_defines.h"
52 #include "brw_state.h"
54 #include "intel_batchbuffer.h"
55 #include "intel_buffer_objects.h"
56 #include "intel_buffers.h"
57 #include "intel_fbo.h"
58 #include "intel_mipmap_tree.h"
59 #include "intel_pixel.h"
60 #include "intel_regions.h"
61 #include "intel_tex.h"
62 #include "intel_tex_obj.h"
64 #include "swrast_setup/swrast_setup.h"
66 #include "tnl/t_pipeline.h"
67 #include "glsl/ralloc.h"
69 /***************************************
70 * Mesa's Driver Functions
71 ***************************************/
74 brw_query_samples_for_format(struct gl_context
*ctx
, GLenum target
,
75 GLenum internalFormat
, int samples
[16])
77 struct brw_context
*brw
= brw_context(ctx
);
97 static const GLubyte
*
98 intelGetString(struct gl_context
* ctx
, GLenum name
)
100 const struct brw_context
*const brw
= brw_context(ctx
);
102 static char buffer
[128];
106 return (GLubyte
*) "Intel Open Source Technology Center";
110 switch (brw
->intelScreen
->deviceID
) {
112 #define CHIPSET(id, family, str) case id: chipset = str; break;
113 #include "pci_ids/i965_pci_ids.h"
115 chipset
= "Unknown Intel Chipset";
119 (void) driGetRendererString(buffer
, chipset
, 0);
120 return (GLubyte
*) buffer
;
128 intel_viewport(struct gl_context
*ctx
, GLint x
, GLint y
, GLsizei w
, GLsizei h
)
130 struct brw_context
*brw
= brw_context(ctx
);
131 __DRIcontext
*driContext
= brw
->driContext
;
138 if (_mesa_is_winsys_fbo(ctx
->DrawBuffer
)) {
139 dri2InvalidateDrawable(driContext
->driDrawablePriv
);
140 dri2InvalidateDrawable(driContext
->driReadablePriv
);
145 intelInvalidateState(struct gl_context
* ctx
, GLuint new_state
)
147 struct brw_context
*brw
= brw_context(ctx
);
149 if (ctx
->swrast_context
)
150 _swrast_InvalidateState(ctx
, new_state
);
151 _vbo_InvalidateState(ctx
, new_state
);
153 brw
->NewGLState
|= new_state
;
157 intel_flush_front(struct gl_context
*ctx
)
159 struct brw_context
*brw
= brw_context(ctx
);
160 __DRIcontext
*driContext
= brw
->driContext
;
161 __DRIdrawable
*driDrawable
= driContext
->driDrawablePriv
;
162 __DRIscreen
*const screen
= brw
->intelScreen
->driScrnPriv
;
164 if (brw
->front_buffer_dirty
&& _mesa_is_winsys_fbo(ctx
->DrawBuffer
)) {
165 if (screen
->dri2
.loader
->flushFrontBuffer
!= NULL
&&
167 driDrawable
->loaderPrivate
) {
169 /* Resolve before flushing FAKE_FRONT_LEFT to FRONT_LEFT.
171 * This potentially resolves both front and back buffer. It
172 * is unnecessary to resolve the back, but harms nothing except
173 * performance. And no one cares about front-buffer render
176 intel_resolve_for_dri2_flush(brw
, driDrawable
);
177 intel_batchbuffer_flush(brw
);
179 screen
->dri2
.loader
->flushFrontBuffer(driDrawable
,
180 driDrawable
->loaderPrivate
);
182 /* We set the dirty bit in intel_prepare_render() if we're
183 * front buffer rendering once we get there.
185 brw
->front_buffer_dirty
= false;
191 intel_glFlush(struct gl_context
*ctx
)
193 struct brw_context
*brw
= brw_context(ctx
);
195 intel_batchbuffer_flush(brw
);
196 intel_flush_front(ctx
);
197 if (brw
->is_front_buffer_rendering
)
198 brw
->need_throttle
= true;
202 intelFinish(struct gl_context
* ctx
)
204 struct brw_context
*brw
= brw_context(ctx
);
208 if (brw
->batch
.last_bo
)
209 drm_intel_bo_wait_rendering(brw
->batch
.last_bo
);
213 brw_init_driver_functions(struct brw_context
*brw
,
214 struct dd_function_table
*functions
)
216 _mesa_init_driver_functions(functions
);
218 /* GLX uses DRI2 invalidate events to handle window resizing.
219 * Unfortunately, EGL does not - libEGL is written in XCB (not Xlib),
220 * which doesn't provide a mechanism for snooping the event queues.
222 * So EGL still relies on viewport hacks to handle window resizing.
223 * This should go away with DRI3000.
225 if (!brw
->driContext
->driScreenPriv
->dri2
.useInvalidate
)
226 functions
->Viewport
= intel_viewport
;
228 functions
->Flush
= intel_glFlush
;
229 functions
->Finish
= intelFinish
;
230 functions
->GetString
= intelGetString
;
231 functions
->UpdateState
= intelInvalidateState
;
233 intelInitTextureFuncs(functions
);
234 intelInitTextureImageFuncs(functions
);
235 intelInitTextureSubImageFuncs(functions
);
236 intelInitTextureCopyImageFuncs(functions
);
237 intelInitClearFuncs(functions
);
238 intelInitBufferFuncs(functions
);
239 intelInitPixelFuncs(functions
);
240 intelInitBufferObjectFuncs(functions
);
241 intel_init_syncobj_functions(functions
);
242 brw_init_object_purgeable_functions(functions
);
244 brwInitFragProgFuncs( functions
);
245 brw_init_common_queryobj_functions(functions
);
247 gen6_init_queryobj_functions(functions
);
249 gen4_init_queryobj_functions(functions
);
251 functions
->QuerySamplesForFormat
= brw_query_samples_for_format
;
253 functions
->NewTransformFeedback
= brw_new_transform_feedback
;
254 functions
->DeleteTransformFeedback
= brw_delete_transform_feedback
;
255 functions
->GetTransformFeedbackVertexCount
=
256 brw_get_transform_feedback_vertex_count
;
258 functions
->BeginTransformFeedback
= gen7_begin_transform_feedback
;
259 functions
->EndTransformFeedback
= gen7_end_transform_feedback
;
260 functions
->PauseTransformFeedback
= gen7_pause_transform_feedback
;
261 functions
->ResumeTransformFeedback
= gen7_resume_transform_feedback
;
263 functions
->BeginTransformFeedback
= brw_begin_transform_feedback
;
264 functions
->EndTransformFeedback
= brw_end_transform_feedback
;
268 functions
->GetSamplePosition
= gen6_get_sample_position
;
272 * Return array of MSAA modes supported by the hardware. The array is
273 * zero-terminated and sorted in decreasing order.
276 brw_supported_msaa_modes(const struct brw_context
*brw
)
278 static const int gen7_samples
[] = {8, 4, 0};
279 static const int gen6_samples
[] = {4, 0};
280 static const int gen4_samples
[] = {0};
283 } else if (brw
->gen
== 6) {
291 * Override GL_MAX_SAMPLES and related constants according to value of driconf
292 * option 'clamp_max_samples'.
295 brw_override_max_samples(struct brw_context
*brw
)
297 const int clamp_max_samples
= driQueryOptioni(&brw
->optionCache
,
298 "clamp_max_samples");
299 if (clamp_max_samples
< 0)
302 const int *supported_msaa_modes
= brw_supported_msaa_modes(brw
);
305 /* Select the largest supported MSAA mode that does not exceed
308 for (int i
= 0; supported_msaa_modes
[i
] != 0; ++i
) {
309 if (supported_msaa_modes
[i
] <= clamp_max_samples
) {
310 max_samples
= supported_msaa_modes
[i
];
315 brw
->ctx
.Const
.MaxSamples
= max_samples
;
316 brw
->ctx
.Const
.MaxColorTextureSamples
= max_samples
;
317 brw
->ctx
.Const
.MaxDepthTextureSamples
= max_samples
;
318 brw
->ctx
.Const
.MaxIntegerSamples
= max_samples
;
322 brw_initialize_context_constants(struct brw_context
*brw
)
324 struct gl_context
*ctx
= &brw
->ctx
;
326 ctx
->Const
.QueryCounterBits
.Timestamp
= 36;
328 ctx
->Const
.StripTextureBorder
= true;
330 ctx
->Const
.MaxDualSourceDrawBuffers
= 1;
331 ctx
->Const
.MaxDrawBuffers
= BRW_MAX_DRAW_BUFFERS
;
332 ctx
->Const
.FragmentProgram
.MaxTextureImageUnits
= BRW_MAX_TEX_UNIT
;
333 ctx
->Const
.MaxTextureCoordUnits
= 8; /* Mesa limit */
334 ctx
->Const
.MaxTextureUnits
=
335 MIN2(ctx
->Const
.MaxTextureCoordUnits
,
336 ctx
->Const
.FragmentProgram
.MaxTextureImageUnits
);
337 ctx
->Const
.VertexProgram
.MaxTextureImageUnits
= BRW_MAX_TEX_UNIT
;
339 ctx
->Const
.GeometryProgram
.MaxTextureImageUnits
= BRW_MAX_TEX_UNIT
;
341 ctx
->Const
.GeometryProgram
.MaxTextureImageUnits
= 0;
342 ctx
->Const
.MaxCombinedTextureImageUnits
=
343 ctx
->Const
.VertexProgram
.MaxTextureImageUnits
+
344 ctx
->Const
.FragmentProgram
.MaxTextureImageUnits
+
345 ctx
->Const
.GeometryProgram
.MaxTextureImageUnits
;
347 ctx
->Const
.MaxTextureLevels
= 14; /* 8192 */
348 if (ctx
->Const
.MaxTextureLevels
> MAX_TEXTURE_LEVELS
)
349 ctx
->Const
.MaxTextureLevels
= MAX_TEXTURE_LEVELS
;
350 ctx
->Const
.Max3DTextureLevels
= 9;
351 ctx
->Const
.MaxCubeTextureLevels
= 12;
354 ctx
->Const
.MaxArrayTextureLayers
= 2048;
356 ctx
->Const
.MaxArrayTextureLayers
= 512;
358 ctx
->Const
.MaxTextureRectSize
= 1 << 12;
360 ctx
->Const
.MaxTextureMaxAnisotropy
= 16.0;
362 ctx
->Const
.MaxRenderbufferSize
= 8192;
364 /* Hardware only supports a limited number of transform feedback buffers.
365 * So we need to override the Mesa default (which is based only on software
368 ctx
->Const
.MaxTransformFeedbackBuffers
= BRW_MAX_SOL_BUFFERS
;
370 /* On Gen6, in the worst case, we use up one binding table entry per
371 * transform feedback component (see comments above the definition of
372 * BRW_MAX_SOL_BINDINGS, in brw_context.h), so we need to advertise a value
373 * for MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS equal to
374 * BRW_MAX_SOL_BINDINGS.
376 * In "separate components" mode, we need to divide this value by
377 * BRW_MAX_SOL_BUFFERS, so that the total number of binding table entries
378 * used up by all buffers will not exceed BRW_MAX_SOL_BINDINGS.
380 ctx
->Const
.MaxTransformFeedbackInterleavedComponents
= BRW_MAX_SOL_BINDINGS
;
381 ctx
->Const
.MaxTransformFeedbackSeparateComponents
=
382 BRW_MAX_SOL_BINDINGS
/ BRW_MAX_SOL_BUFFERS
;
384 ctx
->Const
.AlwaysUseGetTransformFeedbackVertexCount
= true;
386 const int max_samples
= brw_supported_msaa_modes(brw
)[0];
387 ctx
->Const
.MaxSamples
= max_samples
;
388 ctx
->Const
.MaxColorTextureSamples
= max_samples
;
389 ctx
->Const
.MaxDepthTextureSamples
= max_samples
;
390 ctx
->Const
.MaxIntegerSamples
= max_samples
;
393 ctx
->Const
.MaxProgramTextureGatherComponents
= 4;
395 ctx
->Const
.MinLineWidth
= 1.0;
396 ctx
->Const
.MinLineWidthAA
= 1.0;
397 ctx
->Const
.MaxLineWidth
= 5.0;
398 ctx
->Const
.MaxLineWidthAA
= 5.0;
399 ctx
->Const
.LineWidthGranularity
= 0.5;
401 ctx
->Const
.MinPointSize
= 1.0;
402 ctx
->Const
.MinPointSizeAA
= 1.0;
403 ctx
->Const
.MaxPointSize
= 255.0;
404 ctx
->Const
.MaxPointSizeAA
= 255.0;
405 ctx
->Const
.PointSizeGranularity
= 1.0;
407 if (brw
->gen
>= 5 || brw
->is_g4x
)
408 ctx
->Const
.MaxClipPlanes
= 8;
410 ctx
->Const
.VertexProgram
.MaxNativeInstructions
= 16 * 1024;
411 ctx
->Const
.VertexProgram
.MaxAluInstructions
= 0;
412 ctx
->Const
.VertexProgram
.MaxTexInstructions
= 0;
413 ctx
->Const
.VertexProgram
.MaxTexIndirections
= 0;
414 ctx
->Const
.VertexProgram
.MaxNativeAluInstructions
= 0;
415 ctx
->Const
.VertexProgram
.MaxNativeTexInstructions
= 0;
416 ctx
->Const
.VertexProgram
.MaxNativeTexIndirections
= 0;
417 ctx
->Const
.VertexProgram
.MaxNativeAttribs
= 16;
418 ctx
->Const
.VertexProgram
.MaxNativeTemps
= 256;
419 ctx
->Const
.VertexProgram
.MaxNativeAddressRegs
= 1;
420 ctx
->Const
.VertexProgram
.MaxNativeParameters
= 1024;
421 ctx
->Const
.VertexProgram
.MaxEnvParams
=
422 MIN2(ctx
->Const
.VertexProgram
.MaxNativeParameters
,
423 ctx
->Const
.VertexProgram
.MaxEnvParams
);
425 ctx
->Const
.FragmentProgram
.MaxNativeInstructions
= 1024;
426 ctx
->Const
.FragmentProgram
.MaxNativeAluInstructions
= 1024;
427 ctx
->Const
.FragmentProgram
.MaxNativeTexInstructions
= 1024;
428 ctx
->Const
.FragmentProgram
.MaxNativeTexIndirections
= 1024;
429 ctx
->Const
.FragmentProgram
.MaxNativeAttribs
= 12;
430 ctx
->Const
.FragmentProgram
.MaxNativeTemps
= 256;
431 ctx
->Const
.FragmentProgram
.MaxNativeAddressRegs
= 0;
432 ctx
->Const
.FragmentProgram
.MaxNativeParameters
= 1024;
433 ctx
->Const
.FragmentProgram
.MaxEnvParams
=
434 MIN2(ctx
->Const
.FragmentProgram
.MaxNativeParameters
,
435 ctx
->Const
.FragmentProgram
.MaxEnvParams
);
437 /* Fragment shaders use real, 32-bit twos-complement integers for all
440 ctx
->Const
.FragmentProgram
.LowInt
.RangeMin
= 31;
441 ctx
->Const
.FragmentProgram
.LowInt
.RangeMax
= 30;
442 ctx
->Const
.FragmentProgram
.LowInt
.Precision
= 0;
443 ctx
->Const
.FragmentProgram
.HighInt
= ctx
->Const
.FragmentProgram
.LowInt
;
444 ctx
->Const
.FragmentProgram
.MediumInt
= ctx
->Const
.FragmentProgram
.LowInt
;
447 ctx
->Const
.FragmentProgram
.MaxAtomicCounters
= MAX_ATOMIC_COUNTERS
;
448 ctx
->Const
.VertexProgram
.MaxAtomicCounters
= MAX_ATOMIC_COUNTERS
;
449 ctx
->Const
.GeometryProgram
.MaxAtomicCounters
= MAX_ATOMIC_COUNTERS
;
450 ctx
->Const
.FragmentProgram
.MaxAtomicBuffers
= BRW_MAX_ABO
;
451 ctx
->Const
.VertexProgram
.MaxAtomicBuffers
= BRW_MAX_ABO
;
452 ctx
->Const
.GeometryProgram
.MaxAtomicBuffers
= BRW_MAX_ABO
;
453 ctx
->Const
.MaxCombinedAtomicBuffers
= 3 * BRW_MAX_ABO
;
456 /* Gen6 converts quads to polygon in beginning of 3D pipeline,
457 * but we're not sure how it's actually done for vertex order,
458 * that affect provoking vertex decision. Always use last vertex
459 * convention for quad primitive which works as expected for now.
462 ctx
->Const
.QuadsFollowProvokingVertexConvention
= false;
464 ctx
->Const
.NativeIntegers
= true;
465 ctx
->Const
.UniformBooleanTrue
= 1;
467 /* From the gen4 PRM, volume 4 page 127:
469 * "For SURFTYPE_BUFFER non-rendertarget surfaces, this field specifies
470 * the base address of the first element of the surface, computed in
471 * software by adding the surface base address to the byte offset of
472 * the element in the buffer."
474 * However, unaligned accesses are slower, so enforce buffer alignment.
476 ctx
->Const
.UniformBufferOffsetAlignment
= 16;
477 ctx
->Const
.TextureBufferOffsetAlignment
= 16;
480 ctx
->Const
.MaxVarying
= 32;
481 ctx
->Const
.VertexProgram
.MaxOutputComponents
= 128;
482 ctx
->Const
.GeometryProgram
.MaxInputComponents
= 64;
483 ctx
->Const
.GeometryProgram
.MaxOutputComponents
= 128;
484 ctx
->Const
.FragmentProgram
.MaxInputComponents
= 128;
487 /* We want the GLSL compiler to emit code that uses condition codes */
488 for (int i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
489 ctx
->ShaderCompilerOptions
[i
].MaxIfDepth
= brw
->gen
< 6 ? 16 : UINT_MAX
;
490 ctx
->ShaderCompilerOptions
[i
].EmitCondCodes
= true;
491 ctx
->ShaderCompilerOptions
[i
].EmitNoNoise
= true;
492 ctx
->ShaderCompilerOptions
[i
].EmitNoMainReturn
= true;
493 ctx
->ShaderCompilerOptions
[i
].EmitNoIndirectInput
= true;
494 ctx
->ShaderCompilerOptions
[i
].EmitNoIndirectOutput
= true;
496 ctx
->ShaderCompilerOptions
[i
].EmitNoIndirectUniform
=
497 (i
== MESA_SHADER_FRAGMENT
);
498 ctx
->ShaderCompilerOptions
[i
].EmitNoIndirectTemp
=
499 (i
== MESA_SHADER_FRAGMENT
);
500 ctx
->ShaderCompilerOptions
[i
].LowerClipDistance
= true;
503 ctx
->ShaderCompilerOptions
[MESA_SHADER_VERTEX
].PreferDP4
= true;
507 * Process driconf (drirc) options, setting appropriate context flags.
509 * intelInitExtensions still pokes at optionCache directly, in order to
510 * avoid advertising various extensions. No flags are set, so it makes
511 * sense to continue doing that there.
514 brw_process_driconf_options(struct brw_context
*brw
)
516 struct gl_context
*ctx
= &brw
->ctx
;
518 driOptionCache
*options
= &brw
->optionCache
;
519 driParseConfigFiles(options
, &brw
->intelScreen
->optionCache
,
520 brw
->driContext
->driScreenPriv
->myNum
, "i965");
522 int bo_reuse_mode
= driQueryOptioni(options
, "bo_reuse");
523 switch (bo_reuse_mode
) {
524 case DRI_CONF_BO_REUSE_DISABLED
:
526 case DRI_CONF_BO_REUSE_ALL
:
527 intel_bufmgr_gem_enable_reuse(brw
->bufmgr
);
531 if (!driQueryOptionb(options
, "hiz")) {
532 brw
->has_hiz
= false;
533 /* On gen6, you can only do separate stencil with HIZ. */
535 brw
->has_separate_stencil
= false;
538 if (driQueryOptionb(options
, "always_flush_batch")) {
539 fprintf(stderr
, "flushing batchbuffer before/after each draw call\n");
540 brw
->always_flush_batch
= true;
543 if (driQueryOptionb(options
, "always_flush_cache")) {
544 fprintf(stderr
, "flushing GPU caches before/after each draw call\n");
545 brw
->always_flush_cache
= true;
548 if (driQueryOptionb(options
, "disable_throttling")) {
549 fprintf(stderr
, "disabling flush throttling\n");
550 brw
->disable_throttling
= true;
553 brw
->disable_derivative_optimization
=
554 driQueryOptionb(&brw
->optionCache
, "disable_derivative_optimization");
556 brw
->precompile
= driQueryOptionb(&brw
->optionCache
, "shader_precompile");
558 ctx
->Const
.ForceGLSLExtensionsWarn
=
559 driQueryOptionb(options
, "force_glsl_extensions_warn");
561 ctx
->Const
.DisableGLSLLineContinuations
=
562 driQueryOptionb(options
, "disable_glsl_line_continuations");
566 brwCreateContext(gl_api api
,
567 const struct gl_config
*mesaVis
,
568 __DRIcontext
*driContextPriv
,
569 unsigned major_version
,
570 unsigned minor_version
,
573 unsigned *dri_ctx_error
,
574 void *sharedContextPrivate
)
576 __DRIscreen
*sPriv
= driContextPriv
->driScreenPriv
;
577 struct gl_context
*shareCtx
= (struct gl_context
*) sharedContextPrivate
;
578 struct intel_screen
*screen
= sPriv
->driverPrivate
;
579 const struct brw_device_info
*devinfo
= screen
->devinfo
;
580 struct dd_function_table functions
;
581 struct gl_config visual
;
583 if (flags
& ~(__DRI_CTX_FLAG_DEBUG
| __DRI_CTX_FLAG_FORWARD_COMPATIBLE
)) {
584 *dri_ctx_error
= __DRI_CTX_ERROR_UNKNOWN_FLAG
;
589 *dri_ctx_error
= __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
;
593 struct brw_context
*brw
= rzalloc(NULL
, struct brw_context
);
595 printf("%s: failed to alloc context\n", __FUNCTION__
);
596 *dri_ctx_error
= __DRI_CTX_ERROR_NO_MEMORY
;
600 driContextPriv
->driverPrivate
= brw
;
601 brw
->driContext
= driContextPriv
;
602 brw
->intelScreen
= screen
;
603 brw
->bufmgr
= screen
->bufmgr
;
605 brw
->gen
= devinfo
->gen
;
606 brw
->gt
= devinfo
->gt
;
607 brw
->is_g4x
= devinfo
->is_g4x
;
608 brw
->is_baytrail
= devinfo
->is_baytrail
;
609 brw
->is_haswell
= devinfo
->is_haswell
;
610 brw
->has_llc
= devinfo
->has_llc
;
611 brw
->has_hiz
= devinfo
->has_hiz_and_separate_stencil
;
612 brw
->has_separate_stencil
= devinfo
->has_hiz_and_separate_stencil
;
613 brw
->has_pln
= devinfo
->has_pln
;
614 brw
->has_compr4
= devinfo
->has_compr4
;
615 brw
->has_surface_tile_offset
= devinfo
->has_surface_tile_offset
;
616 brw
->has_negative_rhw_bug
= devinfo
->has_negative_rhw_bug
;
617 brw
->needs_unlit_centroid_workaround
=
618 devinfo
->needs_unlit_centroid_workaround
;
620 brw
->must_use_separate_stencil
= screen
->hw_must_use_separate_stencil
;
621 brw
->has_swizzling
= screen
->hw_has_swizzling
;
624 gen7_init_vtable_surface_functions(brw
);
625 gen7_init_vtable_sampler_functions(brw
);
626 brw
->vtbl
.emit_depth_stencil_hiz
= gen7_emit_depth_stencil_hiz
;
628 gen4_init_vtable_surface_functions(brw
);
629 gen4_init_vtable_sampler_functions(brw
);
630 brw
->vtbl
.emit_depth_stencil_hiz
= brw_emit_depth_stencil_hiz
;
633 brw_init_driver_functions(brw
, &functions
);
635 struct gl_context
*ctx
= &brw
->ctx
;
637 if (mesaVis
== NULL
) {
638 memset(&visual
, 0, sizeof visual
);
642 if (!_mesa_initialize_context(ctx
, api
, mesaVis
, shareCtx
, &functions
)) {
643 *dri_ctx_error
= __DRI_CTX_ERROR_NO_MEMORY
;
644 printf("%s: failed to init mesa context\n", __FUNCTION__
);
645 intelDestroyContext(driContextPriv
);
649 /* Initialize the software rasterizer and helper modules.
651 * As of GL 3.1 core, the gen4+ driver doesn't need the swrast context for
652 * software fallbacks (which we have to support on legacy GL to do weird
653 * glDrawPixels(), glBitmap(), and other functions).
655 if (api
!= API_OPENGL_CORE
&& api
!= API_OPENGLES2
) {
656 _swrast_CreateContext(ctx
);
659 _vbo_CreateContext(ctx
);
660 if (ctx
->swrast_context
) {
661 _tnl_CreateContext(ctx
);
662 TNL_CONTEXT(ctx
)->Driver
.RunPipeline
= _tnl_run_pipeline
;
663 _swsetup_CreateContext(ctx
);
665 /* Configure swrast to match hardware characteristics: */
666 _swrast_allow_pixel_fog(ctx
, false);
667 _swrast_allow_vertex_fog(ctx
, true);
670 _mesa_meta_init(ctx
);
672 brw_process_driconf_options(brw
);
673 brw_process_intel_debug_variable(brw
);
674 brw_initialize_context_constants(brw
);
676 /* Reinitialize the context point state. It depends on ctx->Const values. */
677 _mesa_init_point(ctx
);
679 intel_batchbuffer_init(brw
);
683 intelInitExtensions(ctx
);
688 /* Create a new hardware context. Using a hardware context means that
689 * our GPU state will be saved/restored on context switch, allowing us
690 * to assume that the GPU is in the same state we left it in.
692 * This is required for transform feedback buffer offsets, query objects,
693 * and also allows us to reduce how much state we have to emit.
695 brw
->hw_ctx
= drm_intel_gem_context_create(brw
->bufmgr
);
698 fprintf(stderr
, "Gen6+ requires Kernel 3.6 or later.\n");
699 intelDestroyContext(driContextPriv
);
704 brw_init_surface_formats(brw
);
706 if (brw
->is_g4x
|| brw
->gen
>= 5) {
707 brw
->CMD_VF_STATISTICS
= GM45_3DSTATE_VF_STATISTICS
;
708 brw
->CMD_PIPELINE_SELECT
= CMD_PIPELINE_SELECT_GM45
;
710 brw
->CMD_VF_STATISTICS
= GEN4_3DSTATE_VF_STATISTICS
;
711 brw
->CMD_PIPELINE_SELECT
= CMD_PIPELINE_SELECT_965
;
714 brw
->max_vs_threads
= devinfo
->max_vs_threads
;
715 brw
->max_gs_threads
= devinfo
->max_gs_threads
;
716 brw
->max_wm_threads
= devinfo
->max_wm_threads
;
717 brw
->urb
.size
= devinfo
->urb
.size
;
718 brw
->urb
.min_vs_entries
= devinfo
->urb
.min_vs_entries
;
719 brw
->urb
.max_vs_entries
= devinfo
->urb
.max_vs_entries
;
720 brw
->urb
.max_gs_entries
= devinfo
->urb
.max_gs_entries
;
722 /* Estimate the size of the mappable aperture into the GTT. There's an
723 * ioctl to get the whole GTT size, but not one to get the mappable subset.
724 * It turns out it's basically always 256MB, though some ancient hardware
727 uint32_t gtt_size
= 256 * 1024 * 1024;
729 /* We don't want to map two objects such that a memcpy between them would
730 * just fault one mapping in and then the other over and over forever. So
731 * we would need to divide the GTT size by 2. Additionally, some GTT is
732 * taken up by things like the framebuffer and the ringbuffer and such, so
733 * be more conservative.
735 brw
->max_gtt_map_object_size
= gtt_size
/ 4;
738 brw
->urb
.gen6_gs_previously_active
= false;
740 brw
->prim_restart
.in_progress
= false;
741 brw
->prim_restart
.enable_cut_index
= false;
744 brw
->curbe
.last_buf
= calloc(1, 4096);
745 brw
->curbe
.next_buf
= calloc(1, 4096);
748 ctx
->VertexProgram
._MaintainTnlProgram
= true;
749 ctx
->FragmentProgram
._MaintainTexEnvProgram
= true;
751 brw_draw_init( brw
);
753 if ((flags
& __DRI_CTX_FLAG_DEBUG
) != 0) {
754 /* Turn on some extra GL_ARB_debug_output generation. */
755 brw
->perf_debug
= true;
758 brw_fs_alloc_reg_sets(brw
);
759 brw_vec4_alloc_reg_set(brw
);
761 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
)
762 brw_init_shader_time(brw
);
764 _mesa_compute_version(ctx
);
766 /* Here we override context constants. We apply the overrides after
767 * calculation of the context version because we do not want the overridden
768 * constants to change the version.
770 brw_override_max_samples(brw
);
772 _mesa_initialize_dispatch_tables(ctx
);
773 _mesa_initialize_vbo_vtxfmt(ctx
);
779 intelDestroyContext(__DRIcontext
* driContextPriv
)
781 struct brw_context
*brw
=
782 (struct brw_context
*) driContextPriv
->driverPrivate
;
783 struct gl_context
*ctx
= &brw
->ctx
;
785 assert(brw
); /* should never be null */
789 /* Dump a final BMP in case the application doesn't call SwapBuffers */
790 if (INTEL_DEBUG
& DEBUG_AUB
) {
791 intel_batchbuffer_flush(brw
);
792 aub_dump_bmp(&brw
->ctx
);
795 _mesa_meta_free(&brw
->ctx
);
797 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
) {
798 /* Force a report. */
799 brw
->shader_time
.report_time
= 0;
801 brw_collect_and_report_shader_time(brw
);
802 brw_destroy_shader_time(brw
);
805 brw_destroy_state(brw
);
806 brw_draw_destroy(brw
);
808 drm_intel_bo_unreference(brw
->curbe
.curbe_bo
);
809 drm_intel_bo_unreference(brw
->vs
.base
.const_bo
);
810 drm_intel_bo_unreference(brw
->wm
.base
.const_bo
);
812 free(brw
->curbe
.last_buf
);
813 free(brw
->curbe
.next_buf
);
815 drm_intel_gem_context_destroy(brw
->hw_ctx
);
817 if (ctx
->swrast_context
) {
818 _swsetup_DestroyContext(&brw
->ctx
);
819 _tnl_DestroyContext(&brw
->ctx
);
821 _vbo_DestroyContext(&brw
->ctx
);
823 if (ctx
->swrast_context
)
824 _swrast_DestroyContext(&brw
->ctx
);
826 intel_batchbuffer_free(brw
);
828 drm_intel_bo_unreference(brw
->first_post_swapbuffers_batch
);
829 brw
->first_post_swapbuffers_batch
= NULL
;
831 driDestroyOptionCache(&brw
->optionCache
);
833 /* free the Mesa context */
834 _mesa_free_context_data(&brw
->ctx
);
837 driContextPriv
->driverPrivate
= NULL
;
841 intelUnbindContext(__DRIcontext
* driContextPriv
)
843 /* Unset current context and dispath table */
844 _mesa_make_current(NULL
, NULL
, NULL
);
850 * Fixes up the context for GLES23 with our default-to-sRGB-capable behavior
851 * on window system framebuffers.
853 * Desktop GL is fairly reasonable in its handling of sRGB: You can ask if
854 * your renderbuffer can do sRGB encode, and you can flip a switch that does
855 * sRGB encode if the renderbuffer can handle it. You can ask specifically
856 * for a visual where you're guaranteed to be capable, but it turns out that
857 * everyone just makes all their ARGB8888 visuals capable and doesn't offer
858 * incapable ones, becuase there's no difference between the two in resources
859 * used. Applications thus get built that accidentally rely on the default
860 * visual choice being sRGB, so we make ours sRGB capable. Everything sounds
863 * But for GLES2/3, they decided that it was silly to not turn on sRGB encode
864 * for sRGB renderbuffers you made with the GL_EXT_texture_sRGB equivalent.
865 * So they removed the enable knob and made it "if the renderbuffer is sRGB
866 * capable, do sRGB encode". Then, for your window system renderbuffers, you
867 * can ask for sRGB visuals and get sRGB encode, or not ask for sRGB visuals
868 * and get no sRGB encode (assuming that both kinds of visual are available).
869 * Thus our choice to support sRGB by default on our visuals for desktop would
870 * result in broken rendering of GLES apps that aren't expecting sRGB encode.
872 * Unfortunately, renderbuffer setup happens before a context is created. So
873 * in intel_screen.c we always set up sRGB, and here, if you're a GLES2/3
874 * context (without an sRGB visual, though we don't have sRGB visuals exposed
875 * yet), we go turn that back off before anyone finds out.
878 intel_gles3_srgb_workaround(struct brw_context
*brw
,
879 struct gl_framebuffer
*fb
)
881 struct gl_context
*ctx
= &brw
->ctx
;
883 if (_mesa_is_desktop_gl(ctx
) || !fb
->Visual
.sRGBCapable
)
886 /* Some day when we support the sRGB capable bit on visuals available for
887 * GLES, we'll need to respect that and not disable things here.
889 fb
->Visual
.sRGBCapable
= false;
890 for (int i
= 0; i
< BUFFER_COUNT
; i
++) {
891 if (fb
->Attachment
[i
].Renderbuffer
&&
892 fb
->Attachment
[i
].Renderbuffer
->Format
== MESA_FORMAT_SARGB8
) {
893 fb
->Attachment
[i
].Renderbuffer
->Format
= MESA_FORMAT_ARGB8888
;
899 intelMakeCurrent(__DRIcontext
* driContextPriv
,
900 __DRIdrawable
* driDrawPriv
,
901 __DRIdrawable
* driReadPriv
)
903 struct brw_context
*brw
;
904 GET_CURRENT_CONTEXT(curCtx
);
907 brw
= (struct brw_context
*) driContextPriv
->driverPrivate
;
911 /* According to the glXMakeCurrent() man page: "Pending commands to
912 * the previous context, if any, are flushed before it is released."
913 * But only flush if we're actually changing contexts.
915 if (brw_context(curCtx
) && brw_context(curCtx
) != brw
) {
919 if (driContextPriv
) {
920 struct gl_context
*ctx
= &brw
->ctx
;
921 struct gl_framebuffer
*fb
, *readFb
;
923 if (driDrawPriv
== NULL
&& driReadPriv
== NULL
) {
924 fb
= _mesa_get_incomplete_framebuffer();
925 readFb
= _mesa_get_incomplete_framebuffer();
927 fb
= driDrawPriv
->driverPrivate
;
928 readFb
= driReadPriv
->driverPrivate
;
929 driContextPriv
->dri2
.draw_stamp
= driDrawPriv
->dri2
.stamp
- 1;
930 driContextPriv
->dri2
.read_stamp
= driReadPriv
->dri2
.stamp
- 1;
933 /* The sRGB workaround changes the renderbuffer's format. We must change
934 * the format before the renderbuffer's miptree get's allocated, otherwise
935 * the formats of the renderbuffer and its miptree will differ.
937 intel_gles3_srgb_workaround(brw
, fb
);
938 intel_gles3_srgb_workaround(brw
, readFb
);
940 intel_prepare_render(brw
);
941 _mesa_make_current(ctx
, fb
, readFb
);
943 _mesa_make_current(NULL
, NULL
, NULL
);
950 intel_resolve_for_dri2_flush(struct brw_context
*brw
,
951 __DRIdrawable
*drawable
)
954 /* MSAA and fast color clear are not supported, so don't waste time
955 * checking whether a resolve is needed.
960 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
961 struct intel_renderbuffer
*rb
;
963 /* Usually, only the back buffer will need to be downsampled. However,
964 * the front buffer will also need it if the user has rendered into it.
966 static const gl_buffer_index buffers
[2] = {
971 for (int i
= 0; i
< 2; ++i
) {
972 rb
= intel_get_renderbuffer(fb
, buffers
[i
]);
973 if (rb
== NULL
|| rb
->mt
== NULL
)
975 if (rb
->mt
->num_samples
<= 1)
976 intel_miptree_resolve_color(brw
, rb
->mt
);
978 intel_miptree_downsample(brw
, rb
->mt
);
983 intel_bits_per_pixel(const struct intel_renderbuffer
*rb
)
985 return _mesa_get_format_bytes(intel_rb_format(rb
)) * 8;
989 intel_query_dri2_buffers(struct brw_context
*brw
,
990 __DRIdrawable
*drawable
,
991 __DRIbuffer
**buffers
,
995 intel_process_dri2_buffer(struct brw_context
*brw
,
996 __DRIdrawable
*drawable
,
998 struct intel_renderbuffer
*rb
,
999 const char *buffer_name
);
1002 intel_update_renderbuffers(__DRIcontext
*context
, __DRIdrawable
*drawable
)
1004 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
1005 struct intel_renderbuffer
*rb
;
1006 struct brw_context
*brw
= context
->driverPrivate
;
1007 __DRIbuffer
*buffers
= NULL
;
1009 const char *region_name
;
1011 /* Set this up front, so that in case our buffers get invalidated
1012 * while we're getting new buffers, we don't clobber the stamp and
1013 * thus ignore the invalidate. */
1014 drawable
->lastStamp
= drawable
->dri2
.stamp
;
1016 if (unlikely(INTEL_DEBUG
& DEBUG_DRI
))
1017 fprintf(stderr
, "enter %s, drawable %p\n", __func__
, drawable
);
1019 intel_query_dri2_buffers(brw
, drawable
, &buffers
, &count
);
1021 if (buffers
== NULL
)
1024 for (i
= 0; i
< count
; i
++) {
1025 switch (buffers
[i
].attachment
) {
1026 case __DRI_BUFFER_FRONT_LEFT
:
1027 rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
1028 region_name
= "dri2 front buffer";
1031 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
1032 rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
1033 region_name
= "dri2 fake front buffer";
1036 case __DRI_BUFFER_BACK_LEFT
:
1037 rb
= intel_get_renderbuffer(fb
, BUFFER_BACK_LEFT
);
1038 region_name
= "dri2 back buffer";
1041 case __DRI_BUFFER_DEPTH
:
1042 case __DRI_BUFFER_HIZ
:
1043 case __DRI_BUFFER_DEPTH_STENCIL
:
1044 case __DRI_BUFFER_STENCIL
:
1045 case __DRI_BUFFER_ACCUM
:
1048 "unhandled buffer attach event, attachment type %d\n",
1049 buffers
[i
].attachment
);
1053 intel_process_dri2_buffer(brw
, drawable
, &buffers
[i
], rb
, region_name
);
1056 driUpdateFramebufferSize(&brw
->ctx
, drawable
);
1060 * intel_prepare_render should be called anywhere that curent read/drawbuffer
1061 * state is required.
1064 intel_prepare_render(struct brw_context
*brw
)
1066 __DRIcontext
*driContext
= brw
->driContext
;
1067 __DRIdrawable
*drawable
;
1069 drawable
= driContext
->driDrawablePriv
;
1070 if (drawable
&& drawable
->dri2
.stamp
!= driContext
->dri2
.draw_stamp
) {
1071 if (drawable
->lastStamp
!= drawable
->dri2
.stamp
)
1072 intel_update_renderbuffers(driContext
, drawable
);
1073 driContext
->dri2
.draw_stamp
= drawable
->dri2
.stamp
;
1076 drawable
= driContext
->driReadablePriv
;
1077 if (drawable
&& drawable
->dri2
.stamp
!= driContext
->dri2
.read_stamp
) {
1078 if (drawable
->lastStamp
!= drawable
->dri2
.stamp
)
1079 intel_update_renderbuffers(driContext
, drawable
);
1080 driContext
->dri2
.read_stamp
= drawable
->dri2
.stamp
;
1083 /* If we're currently rendering to the front buffer, the rendering
1084 * that will happen next will probably dirty the front buffer. So
1085 * mark it as dirty here.
1087 if (brw
->is_front_buffer_rendering
)
1088 brw
->front_buffer_dirty
= true;
1090 /* Wait for the swapbuffers before the one we just emitted, so we
1091 * don't get too many swaps outstanding for apps that are GPU-heavy
1092 * but not CPU-heavy.
1094 * We're using intelDRI2Flush (called from the loader before
1095 * swapbuffer) and glFlush (for front buffer rendering) as the
1096 * indicator that a frame is done and then throttle when we get
1097 * here as we prepare to render the next frame. At this point for
1098 * round trips for swap/copy and getting new buffers are done and
1099 * we'll spend less time waiting on the GPU.
1101 * Unfortunately, we don't have a handle to the batch containing
1102 * the swap, and getting our hands on that doesn't seem worth it,
1103 * so we just us the first batch we emitted after the last swap.
1105 if (brw
->need_throttle
&& brw
->first_post_swapbuffers_batch
) {
1106 if (!brw
->disable_throttling
)
1107 drm_intel_bo_wait_rendering(brw
->first_post_swapbuffers_batch
);
1108 drm_intel_bo_unreference(brw
->first_post_swapbuffers_batch
);
1109 brw
->first_post_swapbuffers_batch
= NULL
;
1110 brw
->need_throttle
= false;
1115 * \brief Query DRI2 to obtain a DRIdrawable's buffers.
1117 * To determine which DRI buffers to request, examine the renderbuffers
1118 * attached to the drawable's framebuffer. Then request the buffers with
1119 * DRI2GetBuffers() or DRI2GetBuffersWithFormat().
1121 * This is called from intel_update_renderbuffers().
1123 * \param drawable Drawable whose buffers are queried.
1124 * \param buffers [out] List of buffers returned by DRI2 query.
1125 * \param buffer_count [out] Number of buffers returned.
1127 * \see intel_update_renderbuffers()
1128 * \see DRI2GetBuffers()
1129 * \see DRI2GetBuffersWithFormat()
1132 intel_query_dri2_buffers(struct brw_context
*brw
,
1133 __DRIdrawable
*drawable
,
1134 __DRIbuffer
**buffers
,
1137 __DRIscreen
*screen
= brw
->intelScreen
->driScrnPriv
;
1138 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
1140 unsigned attachments
[8];
1142 struct intel_renderbuffer
*front_rb
;
1143 struct intel_renderbuffer
*back_rb
;
1145 front_rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
1146 back_rb
= intel_get_renderbuffer(fb
, BUFFER_BACK_LEFT
);
1148 memset(attachments
, 0, sizeof(attachments
));
1149 if ((brw
->is_front_buffer_rendering
||
1150 brw
->is_front_buffer_reading
||
1151 !back_rb
) && front_rb
) {
1152 /* If a fake front buffer is in use, then querying for
1153 * __DRI_BUFFER_FRONT_LEFT will cause the server to copy the image from
1154 * the real front buffer to the fake front buffer. So before doing the
1155 * query, we need to make sure all the pending drawing has landed in the
1156 * real front buffer.
1158 intel_batchbuffer_flush(brw
);
1159 intel_flush_front(&brw
->ctx
);
1161 attachments
[i
++] = __DRI_BUFFER_FRONT_LEFT
;
1162 attachments
[i
++] = intel_bits_per_pixel(front_rb
);
1163 } else if (front_rb
&& brw
->front_buffer_dirty
) {
1164 /* We have pending front buffer rendering, but we aren't querying for a
1165 * front buffer. If the front buffer we have is a fake front buffer,
1166 * the X server is going to throw it away when it processes the query.
1167 * So before doing the query, make sure all the pending drawing has
1168 * landed in the real front buffer.
1170 intel_batchbuffer_flush(brw
);
1171 intel_flush_front(&brw
->ctx
);
1175 attachments
[i
++] = __DRI_BUFFER_BACK_LEFT
;
1176 attachments
[i
++] = intel_bits_per_pixel(back_rb
);
1179 assert(i
<= ARRAY_SIZE(attachments
));
1181 *buffers
= screen
->dri2
.loader
->getBuffersWithFormat(drawable
,
1186 drawable
->loaderPrivate
);
1190 * \brief Assign a DRI buffer's DRM region to a renderbuffer.
1192 * This is called from intel_update_renderbuffers().
1195 * DRI buffers whose attachment point is DRI2BufferStencil or
1196 * DRI2BufferDepthStencil are handled as special cases.
1198 * \param buffer_name is a human readable name, such as "dri2 front buffer",
1199 * that is passed to intel_region_alloc_for_handle().
1201 * \see intel_update_renderbuffers()
1202 * \see intel_region_alloc_for_handle()
1205 intel_process_dri2_buffer(struct brw_context
*brw
,
1206 __DRIdrawable
*drawable
,
1207 __DRIbuffer
*buffer
,
1208 struct intel_renderbuffer
*rb
,
1209 const char *buffer_name
)
1211 struct intel_region
*region
= NULL
;
1216 unsigned num_samples
= rb
->Base
.Base
.NumSamples
;
1218 /* We try to avoid closing and reopening the same BO name, because the first
1219 * use of a mapping of the buffer involves a bunch of page faulting which is
1220 * moderately expensive.
1222 if (num_samples
== 0) {
1225 rb
->mt
->region
->name
== buffer
->name
)
1229 rb
->mt
->singlesample_mt
&&
1230 rb
->mt
->singlesample_mt
->region
&&
1231 rb
->mt
->singlesample_mt
->region
->name
== buffer
->name
)
1235 if (unlikely(INTEL_DEBUG
& DEBUG_DRI
)) {
1237 "attaching buffer %d, at %d, cpp %d, pitch %d\n",
1238 buffer
->name
, buffer
->attachment
,
1239 buffer
->cpp
, buffer
->pitch
);
1242 intel_miptree_release(&rb
->mt
);
1243 region
= intel_region_alloc_for_handle(brw
->intelScreen
,
1253 rb
->mt
= intel_miptree_create_for_dri2_buffer(brw
,
1255 intel_rb_format(rb
),
1258 intel_region_release(®ion
);