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/simple_list.h"
41 #include "main/version.h"
42 #include "main/vtxfmt.h"
44 #include "vbo/vbo_context.h"
46 #include "drivers/common/driverfuncs.h"
47 #include "drivers/common/meta.h"
50 #include "brw_context.h"
51 #include "brw_defines.h"
53 #include "brw_state.h"
55 #include "intel_batchbuffer.h"
56 #include "intel_buffer_objects.h"
57 #include "intel_buffers.h"
58 #include "intel_fbo.h"
59 #include "intel_mipmap_tree.h"
60 #include "intel_pixel.h"
61 #include "intel_regions.h"
62 #include "intel_tex.h"
63 #include "intel_tex_obj.h"
65 #include "swrast_setup/swrast_setup.h"
67 #include "tnl/t_pipeline.h"
68 #include "glsl/ralloc.h"
70 /***************************************
71 * Mesa's Driver Functions
72 ***************************************/
75 brw_query_samples_for_format(struct gl_context
*ctx
, GLenum target
,
76 GLenum internalFormat
, int samples
[16])
78 struct brw_context
*brw
= brw_context(ctx
);
98 static const GLubyte
*
99 intelGetString(struct gl_context
* ctx
, GLenum name
)
101 const struct brw_context
*const brw
= brw_context(ctx
);
103 static char buffer
[128];
107 return (GLubyte
*) "Intel Open Source Technology Center";
111 switch (brw
->intelScreen
->deviceID
) {
113 #define CHIPSET(id, family, str) case id: chipset = str; break;
114 #include "pci_ids/i965_pci_ids.h"
116 chipset
= "Unknown Intel Chipset";
120 (void) driGetRendererString(buffer
, chipset
, 0);
121 return (GLubyte
*) buffer
;
129 intel_viewport(struct gl_context
*ctx
, GLint x
, GLint y
, GLsizei w
, GLsizei h
)
131 struct brw_context
*brw
= brw_context(ctx
);
132 __DRIcontext
*driContext
= brw
->driContext
;
134 if (brw
->saved_viewport
)
135 brw
->saved_viewport(ctx
, x
, y
, w
, h
);
137 if (_mesa_is_winsys_fbo(ctx
->DrawBuffer
)) {
138 dri2InvalidateDrawable(driContext
->driDrawablePriv
);
139 dri2InvalidateDrawable(driContext
->driReadablePriv
);
144 intelInvalidateState(struct gl_context
* ctx
, GLuint new_state
)
146 struct brw_context
*brw
= brw_context(ctx
);
148 if (ctx
->swrast_context
)
149 _swrast_InvalidateState(ctx
, new_state
);
150 _vbo_InvalidateState(ctx
, new_state
);
152 brw
->NewGLState
|= new_state
;
156 intel_flush_front(struct gl_context
*ctx
)
158 struct brw_context
*brw
= brw_context(ctx
);
159 __DRIcontext
*driContext
= brw
->driContext
;
160 __DRIdrawable
*driDrawable
= driContext
->driDrawablePriv
;
161 __DRIscreen
*const screen
= brw
->intelScreen
->driScrnPriv
;
163 if (brw
->front_buffer_dirty
&& _mesa_is_winsys_fbo(ctx
->DrawBuffer
)) {
164 if (screen
->dri2
.loader
->flushFrontBuffer
!= NULL
&&
166 driDrawable
->loaderPrivate
) {
168 /* Resolve before flushing FAKE_FRONT_LEFT to FRONT_LEFT.
170 * This potentially resolves both front and back buffer. It
171 * is unnecessary to resolve the back, but harms nothing except
172 * performance. And no one cares about front-buffer render
175 intel_resolve_for_dri2_flush(brw
, driDrawable
);
176 intel_batchbuffer_flush(brw
);
178 screen
->dri2
.loader
->flushFrontBuffer(driDrawable
,
179 driDrawable
->loaderPrivate
);
181 /* We set the dirty bit in intel_prepare_render() if we're
182 * front buffer rendering once we get there.
184 brw
->front_buffer_dirty
= false;
190 intel_glFlush(struct gl_context
*ctx
)
192 struct brw_context
*brw
= brw_context(ctx
);
194 intel_batchbuffer_flush(brw
);
195 intel_flush_front(ctx
);
196 if (brw
->is_front_buffer_rendering
)
197 brw
->need_throttle
= true;
201 intelFinish(struct gl_context
* ctx
)
203 struct brw_context
*brw
= brw_context(ctx
);
207 if (brw
->batch
.last_bo
)
208 drm_intel_bo_wait_rendering(brw
->batch
.last_bo
);
212 brw_init_driver_functions(struct brw_context
*brw
,
213 struct dd_function_table
*functions
)
215 _mesa_init_driver_functions(functions
);
217 /* GLX uses DRI2 invalidate events to handle window resizing.
218 * Unfortunately, EGL does not - libEGL is written in XCB (not Xlib),
219 * which doesn't provide a mechanism for snooping the event queues.
221 * So EGL still relies on viewport hacks to handle window resizing.
222 * This should go away with DRI3000.
224 if (!brw
->driContext
->driScreenPriv
->dri2
.useInvalidate
) {
225 brw
->saved_viewport
= functions
->Viewport
;
226 functions
->Viewport
= intel_viewport
;
229 functions
->Flush
= intel_glFlush
;
230 functions
->Finish
= intelFinish
;
231 functions
->GetString
= intelGetString
;
232 functions
->UpdateState
= intelInvalidateState
;
234 intelInitTextureFuncs(functions
);
235 intelInitTextureImageFuncs(functions
);
236 intelInitTextureSubImageFuncs(functions
);
237 intelInitTextureCopyImageFuncs(functions
);
238 intelInitClearFuncs(functions
);
239 intelInitBufferFuncs(functions
);
240 intelInitPixelFuncs(functions
);
241 intelInitBufferObjectFuncs(functions
);
242 intel_init_syncobj_functions(functions
);
243 brw_init_object_purgeable_functions(functions
);
245 brwInitFragProgFuncs( functions
);
246 brw_init_common_queryobj_functions(functions
);
248 gen6_init_queryobj_functions(functions
);
250 gen4_init_queryobj_functions(functions
);
252 functions
->QuerySamplesForFormat
= brw_query_samples_for_format
;
255 functions
->BeginTransformFeedback
= gen7_begin_transform_feedback
;
256 functions
->EndTransformFeedback
= gen7_end_transform_feedback
;
258 functions
->BeginTransformFeedback
= brw_begin_transform_feedback
;
259 functions
->EndTransformFeedback
= brw_end_transform_feedback
;
263 functions
->GetSamplePosition
= gen6_get_sample_position
;
267 brw_initialize_context_constants(struct brw_context
*brw
)
269 struct gl_context
*ctx
= &brw
->ctx
;
271 ctx
->Const
.QueryCounterBits
.Timestamp
= 36;
273 ctx
->Const
.StripTextureBorder
= true;
275 ctx
->Const
.MaxDualSourceDrawBuffers
= 1;
276 ctx
->Const
.MaxDrawBuffers
= BRW_MAX_DRAW_BUFFERS
;
277 ctx
->Const
.FragmentProgram
.MaxTextureImageUnits
= BRW_MAX_TEX_UNIT
;
278 ctx
->Const
.MaxTextureCoordUnits
= 8; /* Mesa limit */
279 ctx
->Const
.MaxTextureUnits
=
280 MIN2(ctx
->Const
.MaxTextureCoordUnits
,
281 ctx
->Const
.FragmentProgram
.MaxTextureImageUnits
);
282 ctx
->Const
.VertexProgram
.MaxTextureImageUnits
= BRW_MAX_TEX_UNIT
;
283 ctx
->Const
.MaxCombinedTextureImageUnits
=
284 ctx
->Const
.VertexProgram
.MaxTextureImageUnits
+
285 ctx
->Const
.FragmentProgram
.MaxTextureImageUnits
;
287 ctx
->Const
.MaxTextureLevels
= 14; /* 8192 */
288 if (ctx
->Const
.MaxTextureLevels
> MAX_TEXTURE_LEVELS
)
289 ctx
->Const
.MaxTextureLevels
= MAX_TEXTURE_LEVELS
;
290 ctx
->Const
.Max3DTextureLevels
= 9;
291 ctx
->Const
.MaxCubeTextureLevels
= 12;
294 ctx
->Const
.MaxArrayTextureLayers
= 2048;
296 ctx
->Const
.MaxArrayTextureLayers
= 512;
298 ctx
->Const
.MaxTextureRectSize
= 1 << 12;
300 ctx
->Const
.MaxTextureMaxAnisotropy
= 16.0;
302 ctx
->Const
.MaxRenderbufferSize
= 8192;
304 /* Hardware only supports a limited number of transform feedback buffers.
305 * So we need to override the Mesa default (which is based only on software
308 ctx
->Const
.MaxTransformFeedbackBuffers
= BRW_MAX_SOL_BUFFERS
;
310 /* On Gen6, in the worst case, we use up one binding table entry per
311 * transform feedback component (see comments above the definition of
312 * BRW_MAX_SOL_BINDINGS, in brw_context.h), so we need to advertise a value
313 * for MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS equal to
314 * BRW_MAX_SOL_BINDINGS.
316 * In "separate components" mode, we need to divide this value by
317 * BRW_MAX_SOL_BUFFERS, so that the total number of binding table entries
318 * used up by all buffers will not exceed BRW_MAX_SOL_BINDINGS.
320 ctx
->Const
.MaxTransformFeedbackInterleavedComponents
= BRW_MAX_SOL_BINDINGS
;
321 ctx
->Const
.MaxTransformFeedbackSeparateComponents
=
322 BRW_MAX_SOL_BINDINGS
/ BRW_MAX_SOL_BUFFERS
;
325 ctx
->Const
.MaxSamples
= 4;
326 ctx
->Const
.MaxColorTextureSamples
= 4;
327 ctx
->Const
.MaxDepthTextureSamples
= 4;
328 ctx
->Const
.MaxIntegerSamples
= 4;
329 } else if (brw
->gen
>= 7) {
330 ctx
->Const
.MaxSamples
= 8;
331 ctx
->Const
.MaxColorTextureSamples
= 8;
332 ctx
->Const
.MaxDepthTextureSamples
= 8;
333 ctx
->Const
.MaxIntegerSamples
= 8;
334 ctx
->Const
.MaxProgramTextureGatherComponents
= 4;
337 ctx
->Const
.MinLineWidth
= 1.0;
338 ctx
->Const
.MinLineWidthAA
= 1.0;
339 ctx
->Const
.MaxLineWidth
= 5.0;
340 ctx
->Const
.MaxLineWidthAA
= 5.0;
341 ctx
->Const
.LineWidthGranularity
= 0.5;
343 ctx
->Const
.MinPointSize
= 1.0;
344 ctx
->Const
.MinPointSizeAA
= 1.0;
345 ctx
->Const
.MaxPointSize
= 255.0;
346 ctx
->Const
.MaxPointSizeAA
= 255.0;
347 ctx
->Const
.PointSizeGranularity
= 1.0;
349 if (brw
->gen
>= 5 || brw
->is_g4x
)
350 ctx
->Const
.MaxClipPlanes
= 8;
352 ctx
->Const
.VertexProgram
.MaxNativeInstructions
= 16 * 1024;
353 ctx
->Const
.VertexProgram
.MaxAluInstructions
= 0;
354 ctx
->Const
.VertexProgram
.MaxTexInstructions
= 0;
355 ctx
->Const
.VertexProgram
.MaxTexIndirections
= 0;
356 ctx
->Const
.VertexProgram
.MaxNativeAluInstructions
= 0;
357 ctx
->Const
.VertexProgram
.MaxNativeTexInstructions
= 0;
358 ctx
->Const
.VertexProgram
.MaxNativeTexIndirections
= 0;
359 ctx
->Const
.VertexProgram
.MaxNativeAttribs
= 16;
360 ctx
->Const
.VertexProgram
.MaxNativeTemps
= 256;
361 ctx
->Const
.VertexProgram
.MaxNativeAddressRegs
= 1;
362 ctx
->Const
.VertexProgram
.MaxNativeParameters
= 1024;
363 ctx
->Const
.VertexProgram
.MaxEnvParams
=
364 MIN2(ctx
->Const
.VertexProgram
.MaxNativeParameters
,
365 ctx
->Const
.VertexProgram
.MaxEnvParams
);
367 ctx
->Const
.FragmentProgram
.MaxNativeInstructions
= 1024;
368 ctx
->Const
.FragmentProgram
.MaxNativeAluInstructions
= 1024;
369 ctx
->Const
.FragmentProgram
.MaxNativeTexInstructions
= 1024;
370 ctx
->Const
.FragmentProgram
.MaxNativeTexIndirections
= 1024;
371 ctx
->Const
.FragmentProgram
.MaxNativeAttribs
= 12;
372 ctx
->Const
.FragmentProgram
.MaxNativeTemps
= 256;
373 ctx
->Const
.FragmentProgram
.MaxNativeAddressRegs
= 0;
374 ctx
->Const
.FragmentProgram
.MaxNativeParameters
= 1024;
375 ctx
->Const
.FragmentProgram
.MaxEnvParams
=
376 MIN2(ctx
->Const
.FragmentProgram
.MaxNativeParameters
,
377 ctx
->Const
.FragmentProgram
.MaxEnvParams
);
379 /* Fragment shaders use real, 32-bit twos-complement integers for all
382 ctx
->Const
.FragmentProgram
.LowInt
.RangeMin
= 31;
383 ctx
->Const
.FragmentProgram
.LowInt
.RangeMax
= 30;
384 ctx
->Const
.FragmentProgram
.LowInt
.Precision
= 0;
385 ctx
->Const
.FragmentProgram
.HighInt
= ctx
->Const
.FragmentProgram
.LowInt
;
386 ctx
->Const
.FragmentProgram
.MediumInt
= ctx
->Const
.FragmentProgram
.LowInt
;
388 /* Gen6 converts quads to polygon in beginning of 3D pipeline,
389 * but we're not sure how it's actually done for vertex order,
390 * that affect provoking vertex decision. Always use last vertex
391 * convention for quad primitive which works as expected for now.
394 ctx
->Const
.QuadsFollowProvokingVertexConvention
= false;
396 ctx
->Const
.NativeIntegers
= true;
397 ctx
->Const
.UniformBooleanTrue
= 1;
398 ctx
->Const
.UniformBufferOffsetAlignment
= 16;
401 ctx
->Const
.MaxVarying
= 32;
402 ctx
->Const
.VertexProgram
.MaxOutputComponents
= 128;
403 ctx
->Const
.GeometryProgram
.MaxInputComponents
= 128;
404 ctx
->Const
.GeometryProgram
.MaxOutputComponents
= 128;
405 ctx
->Const
.FragmentProgram
.MaxInputComponents
= 128;
408 /* We want the GLSL compiler to emit code that uses condition codes */
409 for (int i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
410 ctx
->ShaderCompilerOptions
[i
].MaxIfDepth
= brw
->gen
< 6 ? 16 : UINT_MAX
;
411 ctx
->ShaderCompilerOptions
[i
].EmitCondCodes
= true;
412 ctx
->ShaderCompilerOptions
[i
].EmitNoNoise
= true;
413 ctx
->ShaderCompilerOptions
[i
].EmitNoMainReturn
= true;
414 ctx
->ShaderCompilerOptions
[i
].EmitNoIndirectInput
= true;
415 ctx
->ShaderCompilerOptions
[i
].EmitNoIndirectOutput
= true;
417 ctx
->ShaderCompilerOptions
[i
].EmitNoIndirectUniform
=
418 (i
== MESA_SHADER_FRAGMENT
);
419 ctx
->ShaderCompilerOptions
[i
].EmitNoIndirectTemp
=
420 (i
== MESA_SHADER_FRAGMENT
);
421 ctx
->ShaderCompilerOptions
[i
].LowerClipDistance
= true;
424 ctx
->ShaderCompilerOptions
[MESA_SHADER_VERTEX
].PreferDP4
= true;
428 * Process driconf (drirc) options, setting appropriate context flags.
430 * intelInitExtensions still pokes at optionCache directly, in order to
431 * avoid advertising various extensions. No flags are set, so it makes
432 * sense to continue doing that there.
435 brw_process_driconf_options(struct brw_context
*brw
)
437 struct gl_context
*ctx
= &brw
->ctx
;
439 driOptionCache
*options
= &brw
->optionCache
;
440 driParseConfigFiles(options
, &brw
->intelScreen
->optionCache
,
441 brw
->driContext
->driScreenPriv
->myNum
, "i965");
443 int bo_reuse_mode
= driQueryOptioni(options
, "bo_reuse");
444 switch (bo_reuse_mode
) {
445 case DRI_CONF_BO_REUSE_DISABLED
:
447 case DRI_CONF_BO_REUSE_ALL
:
448 intel_bufmgr_gem_enable_reuse(brw
->bufmgr
);
452 if (!driQueryOptionb(options
, "hiz")) {
453 brw
->has_hiz
= false;
454 /* On gen6, you can only do separate stencil with HIZ. */
456 brw
->has_separate_stencil
= false;
459 if (driQueryOptionb(options
, "always_flush_batch")) {
460 fprintf(stderr
, "flushing batchbuffer before/after each draw call\n");
461 brw
->always_flush_batch
= true;
464 if (driQueryOptionb(options
, "always_flush_cache")) {
465 fprintf(stderr
, "flushing GPU caches before/after each draw call\n");
466 brw
->always_flush_cache
= true;
469 if (driQueryOptionb(options
, "disable_throttling")) {
470 fprintf(stderr
, "disabling flush throttling\n");
471 brw
->disable_throttling
= true;
474 brw
->disable_derivative_optimization
=
475 driQueryOptionb(&brw
->optionCache
, "disable_derivative_optimization");
477 brw
->precompile
= driQueryOptionb(&brw
->optionCache
, "shader_precompile");
479 ctx
->Const
.ForceGLSLExtensionsWarn
=
480 driQueryOptionb(options
, "force_glsl_extensions_warn");
482 ctx
->Const
.DisableGLSLLineContinuations
=
483 driQueryOptionb(options
, "disable_glsl_line_continuations");
487 brwCreateContext(gl_api api
,
488 const struct gl_config
*mesaVis
,
489 __DRIcontext
*driContextPriv
,
490 unsigned major_version
,
491 unsigned minor_version
,
493 unsigned *dri_ctx_error
,
494 void *sharedContextPrivate
)
496 __DRIscreen
*sPriv
= driContextPriv
->driScreenPriv
;
497 struct gl_context
*shareCtx
= (struct gl_context
*) sharedContextPrivate
;
498 struct intel_screen
*screen
= sPriv
->driverPrivate
;
499 const struct brw_device_info
*devinfo
= screen
->devinfo
;
500 struct dd_function_table functions
;
501 struct gl_config visual
;
503 struct brw_context
*brw
= rzalloc(NULL
, struct brw_context
);
505 printf("%s: failed to alloc context\n", __FUNCTION__
);
506 *dri_ctx_error
= __DRI_CTX_ERROR_NO_MEMORY
;
510 driContextPriv
->driverPrivate
= brw
;
511 brw
->driContext
= driContextPriv
;
512 brw
->intelScreen
= screen
;
513 brw
->bufmgr
= screen
->bufmgr
;
515 brw
->gen
= devinfo
->gen
;
516 brw
->gt
= devinfo
->gt
;
517 brw
->is_g4x
= devinfo
->is_g4x
;
518 brw
->is_baytrail
= devinfo
->is_baytrail
;
519 brw
->is_haswell
= devinfo
->is_haswell
;
520 brw
->has_llc
= devinfo
->has_llc
;
521 brw
->has_hiz
= devinfo
->has_hiz_and_separate_stencil
;
522 brw
->has_separate_stencil
= devinfo
->has_hiz_and_separate_stencil
;
523 brw
->has_pln
= devinfo
->has_pln
;
524 brw
->has_compr4
= devinfo
->has_compr4
;
525 brw
->has_surface_tile_offset
= devinfo
->has_surface_tile_offset
;
526 brw
->has_negative_rhw_bug
= devinfo
->has_negative_rhw_bug
;
527 brw
->needs_unlit_centroid_workaround
=
528 devinfo
->needs_unlit_centroid_workaround
;
530 brw
->must_use_separate_stencil
= screen
->hw_must_use_separate_stencil
;
531 brw
->has_swizzling
= screen
->hw_has_swizzling
;
535 brw_init_driver_functions(brw
, &functions
);
537 struct gl_context
*ctx
= &brw
->ctx
;
539 if (mesaVis
== NULL
) {
540 memset(&visual
, 0, sizeof visual
);
544 if (!_mesa_initialize_context(ctx
, api
, mesaVis
, shareCtx
, &functions
)) {
545 *dri_ctx_error
= __DRI_CTX_ERROR_NO_MEMORY
;
546 printf("%s: failed to init mesa context\n", __FUNCTION__
);
547 intelDestroyContext(driContextPriv
);
551 /* Initialize the software rasterizer and helper modules.
553 * As of GL 3.1 core, the gen4+ driver doesn't need the swrast context for
554 * software fallbacks (which we have to support on legacy GL to do weird
555 * glDrawPixels(), glBitmap(), and other functions).
557 if (api
!= API_OPENGL_CORE
&& api
!= API_OPENGLES2
) {
558 _swrast_CreateContext(ctx
);
561 _vbo_CreateContext(ctx
);
562 if (ctx
->swrast_context
) {
563 _tnl_CreateContext(ctx
);
564 TNL_CONTEXT(ctx
)->Driver
.RunPipeline
= _tnl_run_pipeline
;
565 _swsetup_CreateContext(ctx
);
567 /* Configure swrast to match hardware characteristics: */
568 _swrast_allow_pixel_fog(ctx
, false);
569 _swrast_allow_vertex_fog(ctx
, true);
572 _mesa_meta_init(ctx
);
574 brw_process_driconf_options(brw
);
575 brw_process_intel_debug_variable(brw
);
576 brw_initialize_context_constants(brw
);
578 /* Reinitialize the context point state. It depends on ctx->Const values. */
579 _mesa_init_point(ctx
);
581 intelInitExtensions(ctx
);
583 intel_batchbuffer_init(brw
);
588 /* Create a new hardware context. Using a hardware context means that
589 * our GPU state will be saved/restored on context switch, allowing us
590 * to assume that the GPU is in the same state we left it in.
592 * This is required for transform feedback buffer offsets, query objects,
593 * and also allows us to reduce how much state we have to emit.
595 brw
->hw_ctx
= drm_intel_gem_context_create(brw
->bufmgr
);
598 fprintf(stderr
, "Gen6+ requires Kernel 3.6 or later.\n");
599 intelDestroyContext(driContextPriv
);
604 brw_init_surface_formats(brw
);
606 if (brw
->is_g4x
|| brw
->gen
>= 5) {
607 brw
->CMD_VF_STATISTICS
= GM45_3DSTATE_VF_STATISTICS
;
608 brw
->CMD_PIPELINE_SELECT
= CMD_PIPELINE_SELECT_GM45
;
610 brw
->CMD_VF_STATISTICS
= GEN4_3DSTATE_VF_STATISTICS
;
611 brw
->CMD_PIPELINE_SELECT
= CMD_PIPELINE_SELECT_965
;
614 brw
->max_vs_threads
= devinfo
->max_vs_threads
;
615 brw
->max_gs_threads
= devinfo
->max_gs_threads
;
616 brw
->max_wm_threads
= devinfo
->max_wm_threads
;
617 brw
->urb
.size
= devinfo
->urb
.size
;
618 brw
->urb
.min_vs_entries
= devinfo
->urb
.min_vs_entries
;
619 brw
->urb
.max_vs_entries
= devinfo
->urb
.max_vs_entries
;
620 brw
->urb
.max_gs_entries
= devinfo
->urb
.max_gs_entries
;
622 /* Estimate the size of the mappable aperture into the GTT. There's an
623 * ioctl to get the whole GTT size, but not one to get the mappable subset.
624 * It turns out it's basically always 256MB, though some ancient hardware
627 uint32_t gtt_size
= 256 * 1024 * 1024;
629 /* We don't want to map two objects such that a memcpy between them would
630 * just fault one mapping in and then the other over and over forever. So
631 * we would need to divide the GTT size by 2. Additionally, some GTT is
632 * taken up by things like the framebuffer and the ringbuffer and such, so
633 * be more conservative.
635 brw
->max_gtt_map_object_size
= gtt_size
/ 4;
638 brw
->urb
.gen6_gs_previously_active
= false;
640 brw
->prim_restart
.in_progress
= false;
641 brw
->prim_restart
.enable_cut_index
= false;
643 brw_init_state( brw
);
646 brw
->curbe
.last_buf
= calloc(1, 4096);
647 brw
->curbe
.next_buf
= calloc(1, 4096);
650 ctx
->VertexProgram
._MaintainTnlProgram
= true;
651 ctx
->FragmentProgram
._MaintainTexEnvProgram
= true;
653 brw_draw_init( brw
);
655 if ((flags
& __DRI_CTX_FLAG_DEBUG
) != 0) {
656 /* Turn on some extra GL_ARB_debug_output generation. */
657 brw
->perf_debug
= true;
660 brw_fs_alloc_reg_sets(brw
);
661 brw_vec4_alloc_reg_set(brw
);
663 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
)
664 brw_init_shader_time(brw
);
666 _mesa_compute_version(ctx
);
668 _mesa_initialize_dispatch_tables(ctx
);
669 _mesa_initialize_vbo_vtxfmt(ctx
);
671 if (ctx
->Extensions
.AMD_performance_monitor
) {
672 brw_init_performance_monitors(brw
);
679 intelDestroyContext(__DRIcontext
* driContextPriv
)
681 struct brw_context
*brw
=
682 (struct brw_context
*) driContextPriv
->driverPrivate
;
683 struct gl_context
*ctx
= &brw
->ctx
;
685 assert(brw
); /* should never be null */
689 /* Dump a final BMP in case the application doesn't call SwapBuffers */
690 if (INTEL_DEBUG
& DEBUG_AUB
) {
691 intel_batchbuffer_flush(brw
);
692 aub_dump_bmp(&brw
->ctx
);
695 _mesa_meta_free(&brw
->ctx
);
697 brw
->vtbl
.destroy(brw
);
699 if (ctx
->swrast_context
) {
700 _swsetup_DestroyContext(&brw
->ctx
);
701 _tnl_DestroyContext(&brw
->ctx
);
703 _vbo_DestroyContext(&brw
->ctx
);
705 if (ctx
->swrast_context
)
706 _swrast_DestroyContext(&brw
->ctx
);
708 intel_batchbuffer_free(brw
);
710 drm_intel_bo_unreference(brw
->first_post_swapbuffers_batch
);
711 brw
->first_post_swapbuffers_batch
= NULL
;
713 driDestroyOptionCache(&brw
->optionCache
);
715 /* free the Mesa context */
716 _mesa_free_context_data(&brw
->ctx
);
719 driContextPriv
->driverPrivate
= NULL
;
723 intelUnbindContext(__DRIcontext
* driContextPriv
)
725 /* Unset current context and dispath table */
726 _mesa_make_current(NULL
, NULL
, NULL
);
732 * Fixes up the context for GLES23 with our default-to-sRGB-capable behavior
733 * on window system framebuffers.
735 * Desktop GL is fairly reasonable in its handling of sRGB: You can ask if
736 * your renderbuffer can do sRGB encode, and you can flip a switch that does
737 * sRGB encode if the renderbuffer can handle it. You can ask specifically
738 * for a visual where you're guaranteed to be capable, but it turns out that
739 * everyone just makes all their ARGB8888 visuals capable and doesn't offer
740 * incapable ones, becuase there's no difference between the two in resources
741 * used. Applications thus get built that accidentally rely on the default
742 * visual choice being sRGB, so we make ours sRGB capable. Everything sounds
745 * But for GLES2/3, they decided that it was silly to not turn on sRGB encode
746 * for sRGB renderbuffers you made with the GL_EXT_texture_sRGB equivalent.
747 * So they removed the enable knob and made it "if the renderbuffer is sRGB
748 * capable, do sRGB encode". Then, for your window system renderbuffers, you
749 * can ask for sRGB visuals and get sRGB encode, or not ask for sRGB visuals
750 * and get no sRGB encode (assuming that both kinds of visual are available).
751 * Thus our choice to support sRGB by default on our visuals for desktop would
752 * result in broken rendering of GLES apps that aren't expecting sRGB encode.
754 * Unfortunately, renderbuffer setup happens before a context is created. So
755 * in intel_screen.c we always set up sRGB, and here, if you're a GLES2/3
756 * context (without an sRGB visual, though we don't have sRGB visuals exposed
757 * yet), we go turn that back off before anyone finds out.
760 intel_gles3_srgb_workaround(struct brw_context
*brw
,
761 struct gl_framebuffer
*fb
)
763 struct gl_context
*ctx
= &brw
->ctx
;
765 if (_mesa_is_desktop_gl(ctx
) || !fb
->Visual
.sRGBCapable
)
768 /* Some day when we support the sRGB capable bit on visuals available for
769 * GLES, we'll need to respect that and not disable things here.
771 fb
->Visual
.sRGBCapable
= false;
772 for (int i
= 0; i
< BUFFER_COUNT
; i
++) {
773 if (fb
->Attachment
[i
].Renderbuffer
&&
774 fb
->Attachment
[i
].Renderbuffer
->Format
== MESA_FORMAT_SARGB8
) {
775 fb
->Attachment
[i
].Renderbuffer
->Format
= MESA_FORMAT_ARGB8888
;
781 intelMakeCurrent(__DRIcontext
* driContextPriv
,
782 __DRIdrawable
* driDrawPriv
,
783 __DRIdrawable
* driReadPriv
)
785 struct brw_context
*brw
;
786 GET_CURRENT_CONTEXT(curCtx
);
789 brw
= (struct brw_context
*) driContextPriv
->driverPrivate
;
793 /* According to the glXMakeCurrent() man page: "Pending commands to
794 * the previous context, if any, are flushed before it is released."
795 * But only flush if we're actually changing contexts.
797 if (brw_context(curCtx
) && brw_context(curCtx
) != brw
) {
801 if (driContextPriv
) {
802 struct gl_context
*ctx
= &brw
->ctx
;
803 struct gl_framebuffer
*fb
, *readFb
;
805 if (driDrawPriv
== NULL
&& driReadPriv
== NULL
) {
806 fb
= _mesa_get_incomplete_framebuffer();
807 readFb
= _mesa_get_incomplete_framebuffer();
809 fb
= driDrawPriv
->driverPrivate
;
810 readFb
= driReadPriv
->driverPrivate
;
811 driContextPriv
->dri2
.draw_stamp
= driDrawPriv
->dri2
.stamp
- 1;
812 driContextPriv
->dri2
.read_stamp
= driReadPriv
->dri2
.stamp
- 1;
815 /* The sRGB workaround changes the renderbuffer's format. We must change
816 * the format before the renderbuffer's miptree get's allocated, otherwise
817 * the formats of the renderbuffer and its miptree will differ.
819 intel_gles3_srgb_workaround(brw
, fb
);
820 intel_gles3_srgb_workaround(brw
, readFb
);
822 intel_prepare_render(brw
);
823 _mesa_make_current(ctx
, fb
, readFb
);
825 _mesa_make_current(NULL
, NULL
, NULL
);
832 intel_resolve_for_dri2_flush(struct brw_context
*brw
,
833 __DRIdrawable
*drawable
)
836 /* MSAA and fast color clear are not supported, so don't waste time
837 * checking whether a resolve is needed.
842 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
843 struct intel_renderbuffer
*rb
;
845 /* Usually, only the back buffer will need to be downsampled. However,
846 * the front buffer will also need it if the user has rendered into it.
848 static const gl_buffer_index buffers
[2] = {
853 for (int i
= 0; i
< 2; ++i
) {
854 rb
= intel_get_renderbuffer(fb
, buffers
[i
]);
855 if (rb
== NULL
|| rb
->mt
== NULL
)
857 if (rb
->mt
->num_samples
<= 1)
858 intel_miptree_resolve_color(brw
, rb
->mt
);
860 intel_miptree_downsample(brw
, rb
->mt
);
865 intel_bits_per_pixel(const struct intel_renderbuffer
*rb
)
867 return _mesa_get_format_bytes(intel_rb_format(rb
)) * 8;
871 intel_query_dri2_buffers(struct brw_context
*brw
,
872 __DRIdrawable
*drawable
,
873 __DRIbuffer
**buffers
,
877 intel_process_dri2_buffer(struct brw_context
*brw
,
878 __DRIdrawable
*drawable
,
880 struct intel_renderbuffer
*rb
,
881 const char *buffer_name
);
884 intel_update_renderbuffers(__DRIcontext
*context
, __DRIdrawable
*drawable
)
886 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
887 struct intel_renderbuffer
*rb
;
888 struct brw_context
*brw
= context
->driverPrivate
;
889 __DRIbuffer
*buffers
= NULL
;
891 const char *region_name
;
893 /* Set this up front, so that in case our buffers get invalidated
894 * while we're getting new buffers, we don't clobber the stamp and
895 * thus ignore the invalidate. */
896 drawable
->lastStamp
= drawable
->dri2
.stamp
;
898 if (unlikely(INTEL_DEBUG
& DEBUG_DRI
))
899 fprintf(stderr
, "enter %s, drawable %p\n", __func__
, drawable
);
901 intel_query_dri2_buffers(brw
, drawable
, &buffers
, &count
);
906 for (i
= 0; i
< count
; i
++) {
907 switch (buffers
[i
].attachment
) {
908 case __DRI_BUFFER_FRONT_LEFT
:
909 rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
910 region_name
= "dri2 front buffer";
913 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
914 rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
915 region_name
= "dri2 fake front buffer";
918 case __DRI_BUFFER_BACK_LEFT
:
919 rb
= intel_get_renderbuffer(fb
, BUFFER_BACK_LEFT
);
920 region_name
= "dri2 back buffer";
923 case __DRI_BUFFER_DEPTH
:
924 case __DRI_BUFFER_HIZ
:
925 case __DRI_BUFFER_DEPTH_STENCIL
:
926 case __DRI_BUFFER_STENCIL
:
927 case __DRI_BUFFER_ACCUM
:
930 "unhandled buffer attach event, attachment type %d\n",
931 buffers
[i
].attachment
);
935 intel_process_dri2_buffer(brw
, drawable
, &buffers
[i
], rb
, region_name
);
938 driUpdateFramebufferSize(&brw
->ctx
, drawable
);
942 * intel_prepare_render should be called anywhere that curent read/drawbuffer
946 intel_prepare_render(struct brw_context
*brw
)
948 __DRIcontext
*driContext
= brw
->driContext
;
949 __DRIdrawable
*drawable
;
951 drawable
= driContext
->driDrawablePriv
;
952 if (drawable
&& drawable
->dri2
.stamp
!= driContext
->dri2
.draw_stamp
) {
953 if (drawable
->lastStamp
!= drawable
->dri2
.stamp
)
954 intel_update_renderbuffers(driContext
, drawable
);
955 driContext
->dri2
.draw_stamp
= drawable
->dri2
.stamp
;
958 drawable
= driContext
->driReadablePriv
;
959 if (drawable
&& drawable
->dri2
.stamp
!= driContext
->dri2
.read_stamp
) {
960 if (drawable
->lastStamp
!= drawable
->dri2
.stamp
)
961 intel_update_renderbuffers(driContext
, drawable
);
962 driContext
->dri2
.read_stamp
= drawable
->dri2
.stamp
;
965 /* If we're currently rendering to the front buffer, the rendering
966 * that will happen next will probably dirty the front buffer. So
967 * mark it as dirty here.
969 if (brw
->is_front_buffer_rendering
)
970 brw
->front_buffer_dirty
= true;
972 /* Wait for the swapbuffers before the one we just emitted, so we
973 * don't get too many swaps outstanding for apps that are GPU-heavy
976 * We're using intelDRI2Flush (called from the loader before
977 * swapbuffer) and glFlush (for front buffer rendering) as the
978 * indicator that a frame is done and then throttle when we get
979 * here as we prepare to render the next frame. At this point for
980 * round trips for swap/copy and getting new buffers are done and
981 * we'll spend less time waiting on the GPU.
983 * Unfortunately, we don't have a handle to the batch containing
984 * the swap, and getting our hands on that doesn't seem worth it,
985 * so we just us the first batch we emitted after the last swap.
987 if (brw
->need_throttle
&& brw
->first_post_swapbuffers_batch
) {
988 if (!brw
->disable_throttling
)
989 drm_intel_bo_wait_rendering(brw
->first_post_swapbuffers_batch
);
990 drm_intel_bo_unreference(brw
->first_post_swapbuffers_batch
);
991 brw
->first_post_swapbuffers_batch
= NULL
;
992 brw
->need_throttle
= false;
997 * \brief Query DRI2 to obtain a DRIdrawable's buffers.
999 * To determine which DRI buffers to request, examine the renderbuffers
1000 * attached to the drawable's framebuffer. Then request the buffers with
1001 * DRI2GetBuffers() or DRI2GetBuffersWithFormat().
1003 * This is called from intel_update_renderbuffers().
1005 * \param drawable Drawable whose buffers are queried.
1006 * \param buffers [out] List of buffers returned by DRI2 query.
1007 * \param buffer_count [out] Number of buffers returned.
1009 * \see intel_update_renderbuffers()
1010 * \see DRI2GetBuffers()
1011 * \see DRI2GetBuffersWithFormat()
1014 intel_query_dri2_buffers(struct brw_context
*brw
,
1015 __DRIdrawable
*drawable
,
1016 __DRIbuffer
**buffers
,
1019 __DRIscreen
*screen
= brw
->intelScreen
->driScrnPriv
;
1020 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
1022 unsigned attachments
[8];
1024 struct intel_renderbuffer
*front_rb
;
1025 struct intel_renderbuffer
*back_rb
;
1027 front_rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
1028 back_rb
= intel_get_renderbuffer(fb
, BUFFER_BACK_LEFT
);
1030 memset(attachments
, 0, sizeof(attachments
));
1031 if ((brw
->is_front_buffer_rendering
||
1032 brw
->is_front_buffer_reading
||
1033 !back_rb
) && front_rb
) {
1034 /* If a fake front buffer is in use, then querying for
1035 * __DRI_BUFFER_FRONT_LEFT will cause the server to copy the image from
1036 * the real front buffer to the fake front buffer. So before doing the
1037 * query, we need to make sure all the pending drawing has landed in the
1038 * real front buffer.
1040 intel_batchbuffer_flush(brw
);
1041 intel_flush_front(&brw
->ctx
);
1043 attachments
[i
++] = __DRI_BUFFER_FRONT_LEFT
;
1044 attachments
[i
++] = intel_bits_per_pixel(front_rb
);
1045 } else if (front_rb
&& brw
->front_buffer_dirty
) {
1046 /* We have pending front buffer rendering, but we aren't querying for a
1047 * front buffer. If the front buffer we have is a fake front buffer,
1048 * the X server is going to throw it away when it processes the query.
1049 * So before doing the query, make sure all the pending drawing has
1050 * landed in the real front buffer.
1052 intel_batchbuffer_flush(brw
);
1053 intel_flush_front(&brw
->ctx
);
1057 attachments
[i
++] = __DRI_BUFFER_BACK_LEFT
;
1058 attachments
[i
++] = intel_bits_per_pixel(back_rb
);
1061 assert(i
<= ARRAY_SIZE(attachments
));
1063 *buffers
= screen
->dri2
.loader
->getBuffersWithFormat(drawable
,
1068 drawable
->loaderPrivate
);
1072 * \brief Assign a DRI buffer's DRM region to a renderbuffer.
1074 * This is called from intel_update_renderbuffers().
1077 * DRI buffers whose attachment point is DRI2BufferStencil or
1078 * DRI2BufferDepthStencil are handled as special cases.
1080 * \param buffer_name is a human readable name, such as "dri2 front buffer",
1081 * that is passed to intel_region_alloc_for_handle().
1083 * \see intel_update_renderbuffers()
1084 * \see intel_region_alloc_for_handle()
1087 intel_process_dri2_buffer(struct brw_context
*brw
,
1088 __DRIdrawable
*drawable
,
1089 __DRIbuffer
*buffer
,
1090 struct intel_renderbuffer
*rb
,
1091 const char *buffer_name
)
1093 struct intel_region
*region
= NULL
;
1098 unsigned num_samples
= rb
->Base
.Base
.NumSamples
;
1100 /* We try to avoid closing and reopening the same BO name, because the first
1101 * use of a mapping of the buffer involves a bunch of page faulting which is
1102 * moderately expensive.
1104 if (num_samples
== 0) {
1107 rb
->mt
->region
->name
== buffer
->name
)
1111 rb
->mt
->singlesample_mt
&&
1112 rb
->mt
->singlesample_mt
->region
&&
1113 rb
->mt
->singlesample_mt
->region
->name
== buffer
->name
)
1117 if (unlikely(INTEL_DEBUG
& DEBUG_DRI
)) {
1119 "attaching buffer %d, at %d, cpp %d, pitch %d\n",
1120 buffer
->name
, buffer
->attachment
,
1121 buffer
->cpp
, buffer
->pitch
);
1124 intel_miptree_release(&rb
->mt
);
1125 region
= intel_region_alloc_for_handle(brw
->intelScreen
,
1135 rb
->mt
= intel_miptree_create_for_dri2_buffer(brw
,
1137 intel_rb_format(rb
),
1140 intel_region_release(®ion
);