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 "compiler/nir/nir.h"
35 #include "main/api_exec.h"
36 #include "main/context.h"
37 #include "main/fbobject.h"
38 #include "main/extensions.h"
39 #include "main/imports.h"
40 #include "main/macros.h"
41 #include "main/points.h"
42 #include "main/version.h"
43 #include "main/vtxfmt.h"
44 #include "main/texobj.h"
45 #include "main/framebuffer.h"
47 #include "vbo/vbo_context.h"
49 #include "drivers/common/driverfuncs.h"
50 #include "drivers/common/meta.h"
53 #include "brw_context.h"
54 #include "brw_defines.h"
55 #include "brw_blorp.h"
57 #include "brw_state.h"
59 #include "intel_batchbuffer.h"
60 #include "intel_buffer_objects.h"
61 #include "intel_buffers.h"
62 #include "intel_fbo.h"
63 #include "intel_mipmap_tree.h"
64 #include "intel_pixel.h"
65 #include "intel_image.h"
66 #include "intel_tex.h"
67 #include "intel_tex_obj.h"
69 #include "swrast_setup/swrast_setup.h"
71 #include "tnl/t_pipeline.h"
72 #include "util/ralloc.h"
73 #include "util/debug.h"
76 /***************************************
77 * Mesa's Driver Functions
78 ***************************************/
80 const char *const brw_vendor_string
= "Intel Open Source Technology Center";
83 get_bsw_model(const struct intel_screen
*screen
)
85 switch (screen
->eu_total
) {
96 brw_get_renderer_string(const struct intel_screen
*screen
)
99 static char buffer
[128];
102 switch (screen
->deviceID
) {
104 #define CHIPSET(id, symbol, str) case id: chipset = str; break;
105 #include "pci_ids/i965_pci_ids.h"
107 chipset
= "Unknown Intel Chipset";
111 /* Braswell branding is funny, so we have to fix it up here */
112 if (screen
->deviceID
== 0x22B1) {
113 bsw
= strdup(chipset
);
114 char *needle
= strstr(bsw
, "XXX");
116 memcpy(needle
, get_bsw_model(screen
), 3);
121 (void) driGetRendererString(buffer
, chipset
, 0);
126 static const GLubyte
*
127 intel_get_string(struct gl_context
* ctx
, GLenum name
)
129 const struct brw_context
*const brw
= brw_context(ctx
);
133 return (GLubyte
*) brw_vendor_string
;
137 (GLubyte
*) brw_get_renderer_string(brw
->screen
);
145 intel_viewport(struct gl_context
*ctx
)
147 struct brw_context
*brw
= brw_context(ctx
);
148 __DRIcontext
*driContext
= brw
->driContext
;
150 if (_mesa_is_winsys_fbo(ctx
->DrawBuffer
)) {
151 if (driContext
->driDrawablePriv
)
152 dri2InvalidateDrawable(driContext
->driDrawablePriv
);
153 if (driContext
->driReadablePriv
)
154 dri2InvalidateDrawable(driContext
->driReadablePriv
);
159 intel_update_framebuffer(struct gl_context
*ctx
,
160 struct gl_framebuffer
*fb
)
162 struct brw_context
*brw
= brw_context(ctx
);
164 /* Quantize the derived default number of samples
166 fb
->DefaultGeometry
._NumSamples
=
167 intel_quantize_num_samples(brw
->screen
,
168 fb
->DefaultGeometry
.NumSamples
);
172 intel_disable_rb_aux_buffer(struct brw_context
*brw
, const struct brw_bo
*bo
)
174 const struct gl_framebuffer
*fb
= brw
->ctx
.DrawBuffer
;
177 for (unsigned i
= 0; i
< fb
->_NumColorDrawBuffers
; i
++) {
178 const struct intel_renderbuffer
*irb
=
179 intel_renderbuffer(fb
->_ColorDrawBuffers
[i
]);
181 if (irb
&& irb
->mt
->bo
== bo
) {
182 found
= brw
->draw_aux_buffer_disabled
[i
] = true;
190 intel_update_state(struct gl_context
* ctx
, GLuint new_state
)
192 struct brw_context
*brw
= brw_context(ctx
);
193 struct intel_texture_object
*tex_obj
;
194 struct intel_renderbuffer
*depth_irb
;
196 if (ctx
->swrast_context
)
197 _swrast_InvalidateState(ctx
, new_state
);
198 _vbo_InvalidateState(ctx
, new_state
);
200 brw
->NewGLState
|= new_state
;
202 _mesa_unlock_context_textures(ctx
);
204 /* Resolve the depth buffer's HiZ buffer. */
205 depth_irb
= intel_get_renderbuffer(ctx
->DrawBuffer
, BUFFER_DEPTH
);
206 if (depth_irb
&& depth_irb
->mt
) {
207 intel_miptree_prepare_depth(brw
, depth_irb
->mt
,
210 depth_irb
->layer_count
);
213 memset(brw
->draw_aux_buffer_disabled
, 0,
214 sizeof(brw
->draw_aux_buffer_disabled
));
216 /* Resolve depth buffer and render cache of each enabled texture. */
217 int maxEnabledUnit
= ctx
->Texture
._MaxEnabledTexImageUnit
;
218 for (int i
= 0; i
<= maxEnabledUnit
; i
++) {
219 if (!ctx
->Texture
.Unit
[i
]._Current
)
221 tex_obj
= intel_texture_object(ctx
->Texture
.Unit
[i
]._Current
);
222 if (!tex_obj
|| !tex_obj
->mt
)
225 /* We need inte_texture_object::_Format to be valid */
226 intel_finalize_mipmap_tree(brw
, i
);
229 intel_miptree_prepare_texture(brw
, tex_obj
->mt
, tex_obj
->_Format
,
232 if (!aux_supported
&& brw
->gen
>= 9 &&
233 intel_disable_rb_aux_buffer(brw
, tex_obj
->mt
->bo
)) {
234 perf_debug("Sampling renderbuffer with non-compressible format - "
235 "turning off compression");
238 brw_render_cache_set_check_flush(brw
, tex_obj
->mt
->bo
);
240 if (tex_obj
->base
.StencilSampling
||
241 tex_obj
->mt
->format
== MESA_FORMAT_S_UINT8
) {
242 intel_update_r8stencil(brw
, tex_obj
->mt
);
246 /* Resolve color for each active shader image. */
247 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
248 const struct gl_program
*prog
= ctx
->_Shader
->CurrentProgram
[i
];
250 if (unlikely(prog
&& prog
->info
.num_images
)) {
251 for (unsigned j
= 0; j
< prog
->info
.num_images
; j
++) {
252 struct gl_image_unit
*u
=
253 &ctx
->ImageUnits
[prog
->sh
.ImageUnits
[j
]];
254 tex_obj
= intel_texture_object(u
->TexObj
);
256 if (tex_obj
&& tex_obj
->mt
) {
257 intel_miptree_prepare_image(brw
, tex_obj
->mt
);
259 if (intel_miptree_is_lossless_compressed(brw
, tex_obj
->mt
) &&
260 intel_disable_rb_aux_buffer(brw
, tex_obj
->mt
->bo
)) {
261 perf_debug("Using renderbuffer as shader image - turning "
262 "off lossless compression");
265 brw_render_cache_set_check_flush(brw
, tex_obj
->mt
->bo
);
271 /* Resolve color buffers for non-coherent framebuffer fetch. */
272 if (!ctx
->Extensions
.MESA_shader_framebuffer_fetch
&&
273 ctx
->FragmentProgram
._Current
&&
274 ctx
->FragmentProgram
._Current
->info
.outputs_read
) {
275 const struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
277 for (unsigned i
= 0; i
< fb
->_NumColorDrawBuffers
; i
++) {
278 const struct intel_renderbuffer
*irb
=
279 intel_renderbuffer(fb
->_ColorDrawBuffers
[i
]);
282 intel_miptree_prepare_fb_fetch(brw
, irb
->mt
, irb
->mt_level
,
283 irb
->mt_layer
, irb
->layer_count
);
288 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
289 for (int i
= 0; i
< fb
->_NumColorDrawBuffers
; i
++) {
290 struct intel_renderbuffer
*irb
=
291 intel_renderbuffer(fb
->_ColorDrawBuffers
[i
]);
293 if (irb
== NULL
|| irb
->mt
== NULL
)
296 intel_miptree_prepare_render(brw
, irb
->mt
, irb
->mt_level
,
297 irb
->mt_layer
, irb
->layer_count
,
298 ctx
->Color
.sRGBEnabled
);
301 _mesa_lock_context_textures(ctx
);
303 if (new_state
& _NEW_BUFFERS
) {
304 intel_update_framebuffer(ctx
, ctx
->DrawBuffer
);
305 if (ctx
->DrawBuffer
!= ctx
->ReadBuffer
)
306 intel_update_framebuffer(ctx
, ctx
->ReadBuffer
);
310 #define flushFront(screen) ((screen)->image.loader ? (screen)->image.loader->flushFrontBuffer : (screen)->dri2.loader->flushFrontBuffer)
313 intel_flush_front(struct gl_context
*ctx
)
315 struct brw_context
*brw
= brw_context(ctx
);
316 __DRIcontext
*driContext
= brw
->driContext
;
317 __DRIdrawable
*driDrawable
= driContext
->driDrawablePriv
;
318 __DRIscreen
*const dri_screen
= brw
->screen
->driScrnPriv
;
320 if (brw
->front_buffer_dirty
&& _mesa_is_winsys_fbo(ctx
->DrawBuffer
)) {
321 if (flushFront(dri_screen
) && driDrawable
&&
322 driDrawable
->loaderPrivate
) {
324 /* Resolve before flushing FAKE_FRONT_LEFT to FRONT_LEFT.
326 * This potentially resolves both front and back buffer. It
327 * is unnecessary to resolve the back, but harms nothing except
328 * performance. And no one cares about front-buffer render
331 intel_resolve_for_dri2_flush(brw
, driDrawable
);
332 intel_batchbuffer_flush(brw
);
334 flushFront(dri_screen
)(driDrawable
, driDrawable
->loaderPrivate
);
336 /* We set the dirty bit in intel_prepare_render() if we're
337 * front buffer rendering once we get there.
339 brw
->front_buffer_dirty
= false;
345 intel_glFlush(struct gl_context
*ctx
)
347 struct brw_context
*brw
= brw_context(ctx
);
349 intel_batchbuffer_flush(brw
);
350 intel_flush_front(ctx
);
352 brw
->need_flush_throttle
= true;
356 intel_finish(struct gl_context
* ctx
)
358 struct brw_context
*brw
= brw_context(ctx
);
362 if (brw
->batch
.last_bo
)
363 brw_bo_wait_rendering(brw
, brw
->batch
.last_bo
);
367 brw_init_driver_functions(struct brw_context
*brw
,
368 struct dd_function_table
*functions
)
370 _mesa_init_driver_functions(functions
);
372 /* GLX uses DRI2 invalidate events to handle window resizing.
373 * Unfortunately, EGL does not - libEGL is written in XCB (not Xlib),
374 * which doesn't provide a mechanism for snooping the event queues.
376 * So EGL still relies on viewport hacks to handle window resizing.
377 * This should go away with DRI3000.
379 if (!brw
->driContext
->driScreenPriv
->dri2
.useInvalidate
)
380 functions
->Viewport
= intel_viewport
;
382 functions
->Flush
= intel_glFlush
;
383 functions
->Finish
= intel_finish
;
384 functions
->GetString
= intel_get_string
;
385 functions
->UpdateState
= intel_update_state
;
387 intelInitTextureFuncs(functions
);
388 intelInitTextureImageFuncs(functions
);
389 intelInitTextureSubImageFuncs(functions
);
390 intelInitTextureCopyImageFuncs(functions
);
391 intelInitCopyImageFuncs(functions
);
392 intelInitClearFuncs(functions
);
393 intelInitBufferFuncs(functions
);
394 intelInitPixelFuncs(functions
);
395 intelInitBufferObjectFuncs(functions
);
396 brw_init_syncobj_functions(functions
);
397 brw_init_object_purgeable_functions(functions
);
399 brwInitFragProgFuncs( functions
);
400 brw_init_common_queryobj_functions(functions
);
401 if (brw
->gen
>= 8 || brw
->is_haswell
)
402 hsw_init_queryobj_functions(functions
);
403 else if (brw
->gen
>= 6)
404 gen6_init_queryobj_functions(functions
);
406 gen4_init_queryobj_functions(functions
);
407 brw_init_compute_functions(functions
);
409 brw_init_conditional_render_functions(functions
);
411 functions
->QueryInternalFormat
= brw_query_internal_format
;
413 functions
->NewTransformFeedback
= brw_new_transform_feedback
;
414 functions
->DeleteTransformFeedback
= brw_delete_transform_feedback
;
415 if (can_do_mi_math_and_lrr(brw
->screen
)) {
416 functions
->BeginTransformFeedback
= hsw_begin_transform_feedback
;
417 functions
->EndTransformFeedback
= hsw_end_transform_feedback
;
418 functions
->PauseTransformFeedback
= hsw_pause_transform_feedback
;
419 functions
->ResumeTransformFeedback
= hsw_resume_transform_feedback
;
420 } else if (brw
->gen
>= 7) {
421 functions
->BeginTransformFeedback
= gen7_begin_transform_feedback
;
422 functions
->EndTransformFeedback
= gen7_end_transform_feedback
;
423 functions
->PauseTransformFeedback
= gen7_pause_transform_feedback
;
424 functions
->ResumeTransformFeedback
= gen7_resume_transform_feedback
;
425 functions
->GetTransformFeedbackVertexCount
=
426 brw_get_transform_feedback_vertex_count
;
428 functions
->BeginTransformFeedback
= brw_begin_transform_feedback
;
429 functions
->EndTransformFeedback
= brw_end_transform_feedback
;
430 functions
->PauseTransformFeedback
= brw_pause_transform_feedback
;
431 functions
->ResumeTransformFeedback
= brw_resume_transform_feedback
;
432 functions
->GetTransformFeedbackVertexCount
=
433 brw_get_transform_feedback_vertex_count
;
437 functions
->GetSamplePosition
= gen6_get_sample_position
;
441 brw_initialize_context_constants(struct brw_context
*brw
)
443 struct gl_context
*ctx
= &brw
->ctx
;
444 const struct brw_compiler
*compiler
= brw
->screen
->compiler
;
446 const bool stage_exists
[MESA_SHADER_STAGES
] = {
447 [MESA_SHADER_VERTEX
] = true,
448 [MESA_SHADER_TESS_CTRL
] = brw
->gen
>= 7,
449 [MESA_SHADER_TESS_EVAL
] = brw
->gen
>= 7,
450 [MESA_SHADER_GEOMETRY
] = brw
->gen
>= 6,
451 [MESA_SHADER_FRAGMENT
] = true,
452 [MESA_SHADER_COMPUTE
] =
453 ((ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGL_CORE
) &&
454 ctx
->Const
.MaxComputeWorkGroupSize
[0] >= 1024) ||
455 (ctx
->API
== API_OPENGLES2
&&
456 ctx
->Const
.MaxComputeWorkGroupSize
[0] >= 128) ||
457 _mesa_extension_override_enables
.ARB_compute_shader
,
460 unsigned num_stages
= 0;
461 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
466 unsigned max_samplers
=
467 brw
->gen
>= 8 || brw
->is_haswell
? BRW_MAX_TEX_UNIT
: 16;
469 ctx
->Const
.MaxDualSourceDrawBuffers
= 1;
470 ctx
->Const
.MaxDrawBuffers
= BRW_MAX_DRAW_BUFFERS
;
471 ctx
->Const
.MaxCombinedShaderOutputResources
=
472 MAX_IMAGE_UNITS
+ BRW_MAX_DRAW_BUFFERS
;
474 /* The timestamp register we can read for glGetTimestamp() is
475 * sometimes only 32 bits, before scaling to nanoseconds (depending
478 * Once scaled to nanoseconds the timestamp would roll over at a
479 * non-power-of-two, so an application couldn't use
480 * GL_QUERY_COUNTER_BITS to handle rollover correctly. Instead, we
481 * report 36 bits and truncate at that (rolling over 5 times as
482 * often as the HW counter), and when the 32-bit counter rolls
483 * over, it happens to also be at a rollover in the reported value
484 * from near (1<<36) to 0.
486 * The low 32 bits rolls over in ~343 seconds. Our 36-bit result
487 * rolls over every ~69 seconds.
489 ctx
->Const
.QueryCounterBits
.Timestamp
= 36;
491 ctx
->Const
.MaxTextureCoordUnits
= 8; /* Mesa limit */
492 ctx
->Const
.MaxImageUnits
= MAX_IMAGE_UNITS
;
494 ctx
->Const
.MaxRenderbufferSize
= 16384;
495 ctx
->Const
.MaxTextureLevels
= MIN2(15 /* 16384 */, MAX_TEXTURE_LEVELS
);
496 ctx
->Const
.MaxCubeTextureLevels
= 15; /* 16384 */
498 ctx
->Const
.MaxRenderbufferSize
= 8192;
499 ctx
->Const
.MaxTextureLevels
= MIN2(14 /* 8192 */, MAX_TEXTURE_LEVELS
);
500 ctx
->Const
.MaxCubeTextureLevels
= 14; /* 8192 */
502 ctx
->Const
.Max3DTextureLevels
= 12; /* 2048 */
503 ctx
->Const
.MaxArrayTextureLayers
= brw
->gen
>= 7 ? 2048 : 512;
504 ctx
->Const
.MaxTextureMbytes
= 1536;
505 ctx
->Const
.MaxTextureRectSize
= 1 << 12;
506 ctx
->Const
.MaxTextureMaxAnisotropy
= 16.0;
507 ctx
->Const
.MaxTextureLodBias
= 15.0;
508 ctx
->Const
.StripTextureBorder
= true;
510 ctx
->Const
.MaxProgramTextureGatherComponents
= 4;
511 ctx
->Const
.MinProgramTextureGatherOffset
= -32;
512 ctx
->Const
.MaxProgramTextureGatherOffset
= 31;
513 } else if (brw
->gen
== 6) {
514 ctx
->Const
.MaxProgramTextureGatherComponents
= 1;
515 ctx
->Const
.MinProgramTextureGatherOffset
= -8;
516 ctx
->Const
.MaxProgramTextureGatherOffset
= 7;
519 ctx
->Const
.MaxUniformBlockSize
= 65536;
521 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
522 struct gl_program_constants
*prog
= &ctx
->Const
.Program
[i
];
524 if (!stage_exists
[i
])
527 prog
->MaxTextureImageUnits
= max_samplers
;
529 prog
->MaxUniformBlocks
= BRW_MAX_UBO
;
530 prog
->MaxCombinedUniformComponents
=
531 prog
->MaxUniformComponents
+
532 ctx
->Const
.MaxUniformBlockSize
/ 4 * prog
->MaxUniformBlocks
;
534 prog
->MaxAtomicCounters
= MAX_ATOMIC_COUNTERS
;
535 prog
->MaxAtomicBuffers
= BRW_MAX_ABO
;
536 prog
->MaxImageUniforms
= compiler
->scalar_stage
[i
] ? BRW_MAX_IMAGES
: 0;
537 prog
->MaxShaderStorageBlocks
= BRW_MAX_SSBO
;
540 ctx
->Const
.MaxTextureUnits
=
541 MIN2(ctx
->Const
.MaxTextureCoordUnits
,
542 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxTextureImageUnits
);
544 ctx
->Const
.MaxUniformBufferBindings
= num_stages
* BRW_MAX_UBO
;
545 ctx
->Const
.MaxCombinedUniformBlocks
= num_stages
* BRW_MAX_UBO
;
546 ctx
->Const
.MaxCombinedAtomicBuffers
= num_stages
* BRW_MAX_ABO
;
547 ctx
->Const
.MaxCombinedShaderStorageBlocks
= num_stages
* BRW_MAX_SSBO
;
548 ctx
->Const
.MaxShaderStorageBufferBindings
= num_stages
* BRW_MAX_SSBO
;
549 ctx
->Const
.MaxCombinedTextureImageUnits
= num_stages
* max_samplers
;
550 ctx
->Const
.MaxCombinedImageUniforms
= num_stages
* BRW_MAX_IMAGES
;
553 /* Hardware only supports a limited number of transform feedback buffers.
554 * So we need to override the Mesa default (which is based only on software
557 ctx
->Const
.MaxTransformFeedbackBuffers
= BRW_MAX_SOL_BUFFERS
;
559 /* On Gen6, in the worst case, we use up one binding table entry per
560 * transform feedback component (see comments above the definition of
561 * BRW_MAX_SOL_BINDINGS, in brw_context.h), so we need to advertise a value
562 * for MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS equal to
563 * BRW_MAX_SOL_BINDINGS.
565 * In "separate components" mode, we need to divide this value by
566 * BRW_MAX_SOL_BUFFERS, so that the total number of binding table entries
567 * used up by all buffers will not exceed BRW_MAX_SOL_BINDINGS.
569 ctx
->Const
.MaxTransformFeedbackInterleavedComponents
= BRW_MAX_SOL_BINDINGS
;
570 ctx
->Const
.MaxTransformFeedbackSeparateComponents
=
571 BRW_MAX_SOL_BINDINGS
/ BRW_MAX_SOL_BUFFERS
;
573 ctx
->Const
.AlwaysUseGetTransformFeedbackVertexCount
=
574 !can_do_mi_math_and_lrr(brw
->screen
);
577 const int *msaa_modes
= intel_supported_msaa_modes(brw
->screen
);
578 const int clamp_max_samples
=
579 driQueryOptioni(&brw
->optionCache
, "clamp_max_samples");
581 if (clamp_max_samples
< 0) {
582 max_samples
= msaa_modes
[0];
584 /* Select the largest supported MSAA mode that does not exceed
588 for (int i
= 0; msaa_modes
[i
] != 0; ++i
) {
589 if (msaa_modes
[i
] <= clamp_max_samples
) {
590 max_samples
= msaa_modes
[i
];
596 ctx
->Const
.MaxSamples
= max_samples
;
597 ctx
->Const
.MaxColorTextureSamples
= max_samples
;
598 ctx
->Const
.MaxDepthTextureSamples
= max_samples
;
599 ctx
->Const
.MaxIntegerSamples
= max_samples
;
600 ctx
->Const
.MaxImageSamples
= 0;
602 /* gen6_set_sample_maps() sets SampleMap{2,4,8}x variables which are used
603 * to map indices of rectangular grid to sample numbers within a pixel.
604 * These variables are used by GL_EXT_framebuffer_multisample_blit_scaled
605 * extension implementation. For more details see the comment above
606 * gen6_set_sample_maps() definition.
608 gen6_set_sample_maps(ctx
);
610 ctx
->Const
.MinLineWidth
= 1.0;
611 ctx
->Const
.MinLineWidthAA
= 1.0;
613 ctx
->Const
.MaxLineWidth
= 7.375;
614 ctx
->Const
.MaxLineWidthAA
= 7.375;
615 ctx
->Const
.LineWidthGranularity
= 0.125;
617 ctx
->Const
.MaxLineWidth
= 7.0;
618 ctx
->Const
.MaxLineWidthAA
= 7.0;
619 ctx
->Const
.LineWidthGranularity
= 0.5;
622 /* For non-antialiased lines, we have to round the line width to the
623 * nearest whole number. Make sure that we don't advertise a line
624 * width that, when rounded, will be beyond the actual hardware
627 assert(roundf(ctx
->Const
.MaxLineWidth
) <= ctx
->Const
.MaxLineWidth
);
629 ctx
->Const
.MinPointSize
= 1.0;
630 ctx
->Const
.MinPointSizeAA
= 1.0;
631 ctx
->Const
.MaxPointSize
= 255.0;
632 ctx
->Const
.MaxPointSizeAA
= 255.0;
633 ctx
->Const
.PointSizeGranularity
= 1.0;
635 if (brw
->gen
>= 5 || brw
->is_g4x
)
636 ctx
->Const
.MaxClipPlanes
= 8;
638 ctx
->Const
.GLSLTessLevelsAsInputs
= true;
639 ctx
->Const
.LowerTCSPatchVerticesIn
= brw
->gen
>= 8;
640 ctx
->Const
.LowerTESPatchVerticesIn
= true;
641 ctx
->Const
.PrimitiveRestartForPatches
= true;
643 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeInstructions
= 16 * 1024;
644 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxAluInstructions
= 0;
645 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTexInstructions
= 0;
646 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTexIndirections
= 0;
647 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeAluInstructions
= 0;
648 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeTexInstructions
= 0;
649 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeTexIndirections
= 0;
650 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeAttribs
= 16;
651 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeTemps
= 256;
652 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeAddressRegs
= 1;
653 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeParameters
= 1024;
654 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxEnvParams
=
655 MIN2(ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxNativeParameters
,
656 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxEnvParams
);
658 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeInstructions
= 1024;
659 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeAluInstructions
= 1024;
660 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeTexInstructions
= 1024;
661 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeTexIndirections
= 1024;
662 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeAttribs
= 12;
663 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeTemps
= 256;
664 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeAddressRegs
= 0;
665 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeParameters
= 1024;
666 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxEnvParams
=
667 MIN2(ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxNativeParameters
,
668 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxEnvParams
);
670 /* Fragment shaders use real, 32-bit twos-complement integers for all
673 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].LowInt
.RangeMin
= 31;
674 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].LowInt
.RangeMax
= 30;
675 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].LowInt
.Precision
= 0;
676 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].HighInt
= ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].LowInt
;
677 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MediumInt
= ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].LowInt
;
679 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].LowInt
.RangeMin
= 31;
680 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].LowInt
.RangeMax
= 30;
681 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].LowInt
.Precision
= 0;
682 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].HighInt
= ctx
->Const
.Program
[MESA_SHADER_VERTEX
].LowInt
;
683 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MediumInt
= ctx
->Const
.Program
[MESA_SHADER_VERTEX
].LowInt
;
685 /* Gen6 converts quads to polygon in beginning of 3D pipeline,
686 * but we're not sure how it's actually done for vertex order,
687 * that affect provoking vertex decision. Always use last vertex
688 * convention for quad primitive which works as expected for now.
691 ctx
->Const
.QuadsFollowProvokingVertexConvention
= false;
693 ctx
->Const
.NativeIntegers
= true;
694 ctx
->Const
.VertexID_is_zero_based
= true;
696 /* Regarding the CMP instruction, the Ivybridge PRM says:
698 * "For each enabled channel 0b or 1b is assigned to the appropriate flag
699 * bit and 0/all zeros or all ones (e.g, byte 0xFF, word 0xFFFF, DWord
700 * 0xFFFFFFFF) is assigned to dst."
702 * but PRMs for earlier generations say
704 * "In dword format, one GRF may store up to 8 results. When the register
705 * is used later as a vector of Booleans, as only LSB at each channel
706 * contains meaning [sic] data, software should make sure all higher bits
707 * are masked out (e.g. by 'and-ing' an [sic] 0x01 constant)."
709 * We select the representation of a true boolean uniform to be ~0, and fix
710 * the results of Gen <= 5 CMP instruction's with -(result & 1).
712 ctx
->Const
.UniformBooleanTrue
= ~0;
714 /* From the gen4 PRM, volume 4 page 127:
716 * "For SURFTYPE_BUFFER non-rendertarget surfaces, this field specifies
717 * the base address of the first element of the surface, computed in
718 * software by adding the surface base address to the byte offset of
719 * the element in the buffer."
721 * However, unaligned accesses are slower, so enforce buffer alignment.
723 ctx
->Const
.UniformBufferOffsetAlignment
= 16;
725 /* ShaderStorageBufferOffsetAlignment should be a cacheline (64 bytes) so
726 * that we can safely have the CPU and GPU writing the same SSBO on
727 * non-cachecoherent systems (our Atom CPUs). With UBOs, the GPU never
728 * writes, so there's no problem. For an SSBO, the GPU and the CPU can
729 * be updating disjoint regions of the buffer simultaneously and that will
730 * break if the regions overlap the same cacheline.
732 ctx
->Const
.ShaderStorageBufferOffsetAlignment
= 64;
733 ctx
->Const
.TextureBufferOffsetAlignment
= 16;
734 ctx
->Const
.MaxTextureBufferSize
= 128 * 1024 * 1024;
737 ctx
->Const
.MaxVarying
= 32;
738 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxOutputComponents
= 128;
739 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxInputComponents
= 64;
740 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxOutputComponents
= 128;
741 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxInputComponents
= 128;
742 ctx
->Const
.Program
[MESA_SHADER_TESS_CTRL
].MaxInputComponents
= 128;
743 ctx
->Const
.Program
[MESA_SHADER_TESS_CTRL
].MaxOutputComponents
= 128;
744 ctx
->Const
.Program
[MESA_SHADER_TESS_EVAL
].MaxInputComponents
= 128;
745 ctx
->Const
.Program
[MESA_SHADER_TESS_EVAL
].MaxOutputComponents
= 128;
748 /* We want the GLSL compiler to emit code that uses condition codes */
749 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
750 ctx
->Const
.ShaderCompilerOptions
[i
] =
751 brw
->screen
->compiler
->glsl_compiler_options
[i
];
755 ctx
->Const
.MaxViewportWidth
= 32768;
756 ctx
->Const
.MaxViewportHeight
= 32768;
759 /* ARB_viewport_array, OES_viewport_array */
761 ctx
->Const
.MaxViewports
= GEN6_NUM_VIEWPORTS
;
762 ctx
->Const
.ViewportSubpixelBits
= 0;
764 /* Cast to float before negating because MaxViewportWidth is unsigned.
766 ctx
->Const
.ViewportBounds
.Min
= -(float)ctx
->Const
.MaxViewportWidth
;
767 ctx
->Const
.ViewportBounds
.Max
= ctx
->Const
.MaxViewportWidth
;
770 /* ARB_gpu_shader5 */
772 ctx
->Const
.MaxVertexStreams
= MIN2(4, MAX_VERTEX_STREAMS
);
774 /* ARB_framebuffer_no_attachments */
775 ctx
->Const
.MaxFramebufferWidth
= 16384;
776 ctx
->Const
.MaxFramebufferHeight
= 16384;
777 ctx
->Const
.MaxFramebufferLayers
= ctx
->Const
.MaxArrayTextureLayers
;
778 ctx
->Const
.MaxFramebufferSamples
= max_samples
;
780 /* OES_primitive_bounding_box */
781 ctx
->Const
.NoPrimitiveBoundingBoxOutput
= true;
785 brw_initialize_cs_context_constants(struct brw_context
*brw
)
787 struct gl_context
*ctx
= &brw
->ctx
;
788 const struct intel_screen
*screen
= brw
->screen
;
789 struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
791 /* FINISHME: Do this for all platforms that the kernel supports */
792 if (brw
->is_cherryview
&&
793 screen
->subslice_total
> 0 && screen
->eu_total
> 0) {
794 /* Logical CS threads = EUs per subslice * 7 threads per EU */
795 uint32_t max_cs_threads
= screen
->eu_total
/ screen
->subslice_total
* 7;
797 /* Fuse configurations may give more threads than expected, never less. */
798 if (max_cs_threads
> devinfo
->max_cs_threads
)
799 devinfo
->max_cs_threads
= max_cs_threads
;
802 /* Maximum number of scalar compute shader invocations that can be run in
803 * parallel in the same subslice assuming SIMD32 dispatch.
805 * We don't advertise more than 64 threads, because we are limited to 64 by
806 * our usage of thread_width_max in the gpgpu walker command. This only
807 * currently impacts Haswell, which otherwise might be able to advertise 70
808 * threads. With SIMD32 and 64 threads, Haswell still provides twice the
809 * required the number of invocation needed for ARB_compute_shader.
811 const unsigned max_threads
= MIN2(64, devinfo
->max_cs_threads
);
812 const uint32_t max_invocations
= 32 * max_threads
;
813 ctx
->Const
.MaxComputeWorkGroupSize
[0] = max_invocations
;
814 ctx
->Const
.MaxComputeWorkGroupSize
[1] = max_invocations
;
815 ctx
->Const
.MaxComputeWorkGroupSize
[2] = max_invocations
;
816 ctx
->Const
.MaxComputeWorkGroupInvocations
= max_invocations
;
817 ctx
->Const
.MaxComputeSharedMemorySize
= 64 * 1024;
821 * Process driconf (drirc) options, setting appropriate context flags.
823 * intelInitExtensions still pokes at optionCache directly, in order to
824 * avoid advertising various extensions. No flags are set, so it makes
825 * sense to continue doing that there.
828 brw_process_driconf_options(struct brw_context
*brw
)
830 struct gl_context
*ctx
= &brw
->ctx
;
832 driOptionCache
*options
= &brw
->optionCache
;
833 driParseConfigFiles(options
, &brw
->screen
->optionCache
,
834 brw
->driContext
->driScreenPriv
->myNum
, "i965");
836 int bo_reuse_mode
= driQueryOptioni(options
, "bo_reuse");
837 switch (bo_reuse_mode
) {
838 case DRI_CONF_BO_REUSE_DISABLED
:
840 case DRI_CONF_BO_REUSE_ALL
:
841 brw_bufmgr_enable_reuse(brw
->bufmgr
);
845 if (INTEL_DEBUG
& DEBUG_NO_HIZ
) {
846 brw
->has_hiz
= false;
847 /* On gen6, you can only do separate stencil with HIZ. */
849 brw
->has_separate_stencil
= false;
852 if (driQueryOptionb(options
, "always_flush_batch")) {
853 fprintf(stderr
, "flushing batchbuffer before/after each draw call\n");
854 brw
->always_flush_batch
= true;
857 if (driQueryOptionb(options
, "always_flush_cache")) {
858 fprintf(stderr
, "flushing GPU caches before/after each draw call\n");
859 brw
->always_flush_cache
= true;
862 if (driQueryOptionb(options
, "disable_throttling")) {
863 fprintf(stderr
, "disabling flush throttling\n");
864 brw
->disable_throttling
= true;
867 brw
->precompile
= driQueryOptionb(&brw
->optionCache
, "shader_precompile");
869 if (driQueryOptionb(&brw
->optionCache
, "precise_trig"))
870 brw
->screen
->compiler
->precise_trig
= true;
872 ctx
->Const
.ForceGLSLExtensionsWarn
=
873 driQueryOptionb(options
, "force_glsl_extensions_warn");
875 ctx
->Const
.ForceGLSLVersion
=
876 driQueryOptioni(options
, "force_glsl_version");
878 ctx
->Const
.DisableGLSLLineContinuations
=
879 driQueryOptionb(options
, "disable_glsl_line_continuations");
881 ctx
->Const
.AllowGLSLExtensionDirectiveMidShader
=
882 driQueryOptionb(options
, "allow_glsl_extension_directive_midshader");
884 ctx
->Const
.AllowGLSLBuiltinVariableRedeclaration
=
885 driQueryOptionb(options
, "allow_glsl_builtin_variable_redeclaration");
887 ctx
->Const
.AllowHigherCompatVersion
=
888 driQueryOptionb(options
, "allow_higher_compat_version");
890 ctx
->Const
.ForceGLSLAbsSqrt
=
891 driQueryOptionb(options
, "force_glsl_abs_sqrt");
893 ctx
->Const
.GLSLZeroInit
= driQueryOptionb(options
, "glsl_zero_init");
895 brw
->dual_color_blend_by_location
=
896 driQueryOptionb(options
, "dual_color_blend_by_location");
900 brwCreateContext(gl_api api
,
901 const struct gl_config
*mesaVis
,
902 __DRIcontext
*driContextPriv
,
903 unsigned major_version
,
904 unsigned minor_version
,
907 unsigned *dri_ctx_error
,
908 void *sharedContextPrivate
)
910 struct gl_context
*shareCtx
= (struct gl_context
*) sharedContextPrivate
;
911 struct intel_screen
*screen
= driContextPriv
->driScreenPriv
->driverPrivate
;
912 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
913 struct dd_function_table functions
;
915 /* Only allow the __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS flag if the kernel
916 * provides us with context reset notifications.
918 uint32_t allowed_flags
= __DRI_CTX_FLAG_DEBUG
919 | __DRI_CTX_FLAG_FORWARD_COMPATIBLE
;
921 if (screen
->has_context_reset_notification
)
922 allowed_flags
|= __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS
;
924 if (flags
& ~allowed_flags
) {
925 *dri_ctx_error
= __DRI_CTX_ERROR_UNKNOWN_FLAG
;
929 struct brw_context
*brw
= rzalloc(NULL
, struct brw_context
);
931 fprintf(stderr
, "%s: failed to alloc context\n", __func__
);
932 *dri_ctx_error
= __DRI_CTX_ERROR_NO_MEMORY
;
936 driContextPriv
->driverPrivate
= brw
;
937 brw
->driContext
= driContextPriv
;
938 brw
->screen
= screen
;
939 brw
->bufmgr
= screen
->bufmgr
;
941 brw
->gen
= devinfo
->gen
;
942 brw
->gt
= devinfo
->gt
;
943 brw
->is_g4x
= devinfo
->is_g4x
;
944 brw
->is_baytrail
= devinfo
->is_baytrail
;
945 brw
->is_haswell
= devinfo
->is_haswell
;
946 brw
->is_cherryview
= devinfo
->is_cherryview
;
947 brw
->is_broxton
= devinfo
->is_broxton
;
948 brw
->has_llc
= devinfo
->has_llc
;
949 brw
->has_hiz
= devinfo
->has_hiz_and_separate_stencil
;
950 brw
->has_separate_stencil
= devinfo
->has_hiz_and_separate_stencil
;
951 brw
->has_pln
= devinfo
->has_pln
;
952 brw
->has_compr4
= devinfo
->has_compr4
;
953 brw
->has_surface_tile_offset
= devinfo
->has_surface_tile_offset
;
954 brw
->has_negative_rhw_bug
= devinfo
->has_negative_rhw_bug
;
955 brw
->needs_unlit_centroid_workaround
=
956 devinfo
->needs_unlit_centroid_workaround
;
958 brw
->must_use_separate_stencil
= devinfo
->must_use_separate_stencil
;
959 brw
->has_swizzling
= screen
->hw_has_swizzling
;
961 isl_device_init(&brw
->isl_dev
, devinfo
, screen
->hw_has_swizzling
);
963 brw
->vs
.base
.stage
= MESA_SHADER_VERTEX
;
964 brw
->tcs
.base
.stage
= MESA_SHADER_TESS_CTRL
;
965 brw
->tes
.base
.stage
= MESA_SHADER_TESS_EVAL
;
966 brw
->gs
.base
.stage
= MESA_SHADER_GEOMETRY
;
967 brw
->wm
.base
.stage
= MESA_SHADER_FRAGMENT
;
969 gen8_init_vtable_surface_functions(brw
);
970 brw
->vtbl
.emit_depth_stencil_hiz
= gen8_emit_depth_stencil_hiz
;
971 } else if (brw
->gen
>= 7) {
972 gen7_init_vtable_surface_functions(brw
);
973 brw
->vtbl
.emit_depth_stencil_hiz
= gen7_emit_depth_stencil_hiz
;
974 } else if (brw
->gen
>= 6) {
975 gen6_init_vtable_surface_functions(brw
);
976 brw
->vtbl
.emit_depth_stencil_hiz
= gen6_emit_depth_stencil_hiz
;
978 gen4_init_vtable_surface_functions(brw
);
979 brw
->vtbl
.emit_depth_stencil_hiz
= brw_emit_depth_stencil_hiz
;
982 brw_init_driver_functions(brw
, &functions
);
985 functions
.GetGraphicsResetStatus
= brw_get_graphics_reset_status
;
987 struct gl_context
*ctx
= &brw
->ctx
;
989 if (!_mesa_initialize_context(ctx
, api
, mesaVis
, shareCtx
, &functions
)) {
990 *dri_ctx_error
= __DRI_CTX_ERROR_NO_MEMORY
;
991 fprintf(stderr
, "%s: failed to init mesa context\n", __func__
);
992 intelDestroyContext(driContextPriv
);
996 driContextSetFlags(ctx
, flags
);
998 /* Initialize the software rasterizer and helper modules.
1000 * As of GL 3.1 core, the gen4+ driver doesn't need the swrast context for
1001 * software fallbacks (which we have to support on legacy GL to do weird
1002 * glDrawPixels(), glBitmap(), and other functions).
1004 if (api
!= API_OPENGL_CORE
&& api
!= API_OPENGLES2
) {
1005 _swrast_CreateContext(ctx
);
1008 _vbo_CreateContext(ctx
);
1009 if (ctx
->swrast_context
) {
1010 _tnl_CreateContext(ctx
);
1011 TNL_CONTEXT(ctx
)->Driver
.RunPipeline
= _tnl_run_pipeline
;
1012 _swsetup_CreateContext(ctx
);
1014 /* Configure swrast to match hardware characteristics: */
1015 _swrast_allow_pixel_fog(ctx
, false);
1016 _swrast_allow_vertex_fog(ctx
, true);
1019 _mesa_meta_init(ctx
);
1021 brw_process_driconf_options(brw
);
1023 if (INTEL_DEBUG
& DEBUG_PERF
)
1024 brw
->perf_debug
= true;
1026 brw_initialize_cs_context_constants(brw
);
1027 brw_initialize_context_constants(brw
);
1029 ctx
->Const
.ResetStrategy
= notify_reset
1030 ? GL_LOSE_CONTEXT_ON_RESET_ARB
: GL_NO_RESET_NOTIFICATION_ARB
;
1032 /* Reinitialize the context point state. It depends on ctx->Const values. */
1033 _mesa_init_point(ctx
);
1035 intel_fbo_init(brw
);
1037 intel_batchbuffer_init(&brw
->batch
, brw
->bufmgr
, brw
->has_llc
);
1039 if (brw
->gen
>= 6) {
1040 /* Create a new hardware context. Using a hardware context means that
1041 * our GPU state will be saved/restored on context switch, allowing us
1042 * to assume that the GPU is in the same state we left it in.
1044 * This is required for transform feedback buffer offsets, query objects,
1045 * and also allows us to reduce how much state we have to emit.
1047 brw
->hw_ctx
= brw_create_hw_context(brw
->bufmgr
);
1050 fprintf(stderr
, "Failed to create hardware context.\n");
1051 intelDestroyContext(driContextPriv
);
1056 if (brw_init_pipe_control(brw
, devinfo
)) {
1057 *dri_ctx_error
= __DRI_CTX_ERROR_NO_MEMORY
;
1058 intelDestroyContext(driContextPriv
);
1062 brw_init_state(brw
);
1064 intelInitExtensions(ctx
);
1066 brw_init_surface_formats(brw
);
1068 brw_blorp_init(brw
);
1070 brw
->urb
.size
= devinfo
->urb
.size
;
1073 brw
->urb
.gs_present
= false;
1075 brw
->prim_restart
.in_progress
= false;
1076 brw
->prim_restart
.enable_cut_index
= false;
1077 brw
->gs
.enabled
= false;
1078 brw
->clip
.viewport_count
= 1;
1080 brw
->predicate
.state
= BRW_PREDICATE_STATE_RENDER
;
1082 brw
->max_gtt_map_object_size
= screen
->max_gtt_map_object_size
;
1084 ctx
->VertexProgram
._MaintainTnlProgram
= true;
1085 ctx
->FragmentProgram
._MaintainTexEnvProgram
= true;
1087 brw_draw_init( brw
);
1089 if ((flags
& __DRI_CTX_FLAG_DEBUG
) != 0) {
1090 /* Turn on some extra GL_ARB_debug_output generation. */
1091 brw
->perf_debug
= true;
1094 if ((flags
& __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS
) != 0) {
1095 ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_ROBUST_ACCESS_BIT_ARB
;
1096 ctx
->Const
.RobustAccess
= GL_TRUE
;
1099 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
)
1100 brw_init_shader_time(brw
);
1102 _mesa_compute_version(ctx
);
1104 _mesa_initialize_dispatch_tables(ctx
);
1105 _mesa_initialize_vbo_vtxfmt(ctx
);
1107 if (ctx
->Extensions
.INTEL_performance_query
)
1108 brw_init_performance_queries(brw
);
1110 vbo_use_buffer_objects(ctx
);
1111 vbo_always_unmap_buffers(ctx
);
1117 intelDestroyContext(__DRIcontext
* driContextPriv
)
1119 struct brw_context
*brw
=
1120 (struct brw_context
*) driContextPriv
->driverPrivate
;
1121 struct gl_context
*ctx
= &brw
->ctx
;
1123 _mesa_meta_free(&brw
->ctx
);
1125 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
) {
1126 /* Force a report. */
1127 brw
->shader_time
.report_time
= 0;
1129 brw_collect_and_report_shader_time(brw
);
1130 brw_destroy_shader_time(brw
);
1134 blorp_finish(&brw
->blorp
);
1136 brw_destroy_state(brw
);
1137 brw_draw_destroy(brw
);
1139 brw_bo_unreference(brw
->curbe
.curbe_bo
);
1140 if (brw
->vs
.base
.scratch_bo
)
1141 brw_bo_unreference(brw
->vs
.base
.scratch_bo
);
1142 if (brw
->tcs
.base
.scratch_bo
)
1143 brw_bo_unreference(brw
->tcs
.base
.scratch_bo
);
1144 if (brw
->tes
.base
.scratch_bo
)
1145 brw_bo_unreference(brw
->tes
.base
.scratch_bo
);
1146 if (brw
->gs
.base
.scratch_bo
)
1147 brw_bo_unreference(brw
->gs
.base
.scratch_bo
);
1148 if (brw
->wm
.base
.scratch_bo
)
1149 brw_bo_unreference(brw
->wm
.base
.scratch_bo
);
1151 brw_destroy_hw_context(brw
->bufmgr
, brw
->hw_ctx
);
1153 if (ctx
->swrast_context
) {
1154 _swsetup_DestroyContext(&brw
->ctx
);
1155 _tnl_DestroyContext(&brw
->ctx
);
1157 _vbo_DestroyContext(&brw
->ctx
);
1159 if (ctx
->swrast_context
)
1160 _swrast_DestroyContext(&brw
->ctx
);
1162 brw_fini_pipe_control(brw
);
1163 intel_batchbuffer_free(&brw
->batch
);
1165 brw_bo_unreference(brw
->throttle_batch
[1]);
1166 brw_bo_unreference(brw
->throttle_batch
[0]);
1167 brw
->throttle_batch
[1] = NULL
;
1168 brw
->throttle_batch
[0] = NULL
;
1170 driDestroyOptionCache(&brw
->optionCache
);
1172 /* free the Mesa context */
1173 _mesa_free_context_data(&brw
->ctx
);
1176 driContextPriv
->driverPrivate
= NULL
;
1180 intelUnbindContext(__DRIcontext
* driContextPriv
)
1182 /* Unset current context and dispath table */
1183 _mesa_make_current(NULL
, NULL
, NULL
);
1189 * Fixes up the context for GLES23 with our default-to-sRGB-capable behavior
1190 * on window system framebuffers.
1192 * Desktop GL is fairly reasonable in its handling of sRGB: You can ask if
1193 * your renderbuffer can do sRGB encode, and you can flip a switch that does
1194 * sRGB encode if the renderbuffer can handle it. You can ask specifically
1195 * for a visual where you're guaranteed to be capable, but it turns out that
1196 * everyone just makes all their ARGB8888 visuals capable and doesn't offer
1197 * incapable ones, because there's no difference between the two in resources
1198 * used. Applications thus get built that accidentally rely on the default
1199 * visual choice being sRGB, so we make ours sRGB capable. Everything sounds
1202 * But for GLES2/3, they decided that it was silly to not turn on sRGB encode
1203 * for sRGB renderbuffers you made with the GL_EXT_texture_sRGB equivalent.
1204 * So they removed the enable knob and made it "if the renderbuffer is sRGB
1205 * capable, do sRGB encode". Then, for your window system renderbuffers, you
1206 * can ask for sRGB visuals and get sRGB encode, or not ask for sRGB visuals
1207 * and get no sRGB encode (assuming that both kinds of visual are available).
1208 * Thus our choice to support sRGB by default on our visuals for desktop would
1209 * result in broken rendering of GLES apps that aren't expecting sRGB encode.
1211 * Unfortunately, renderbuffer setup happens before a context is created. So
1212 * in intel_screen.c we always set up sRGB, and here, if you're a GLES2/3
1213 * context (without an sRGB visual, though we don't have sRGB visuals exposed
1214 * yet), we go turn that back off before anyone finds out.
1217 intel_gles3_srgb_workaround(struct brw_context
*brw
,
1218 struct gl_framebuffer
*fb
)
1220 struct gl_context
*ctx
= &brw
->ctx
;
1222 if (_mesa_is_desktop_gl(ctx
) || !fb
->Visual
.sRGBCapable
)
1225 /* Some day when we support the sRGB capable bit on visuals available for
1226 * GLES, we'll need to respect that and not disable things here.
1228 fb
->Visual
.sRGBCapable
= false;
1229 for (int i
= 0; i
< BUFFER_COUNT
; i
++) {
1230 struct gl_renderbuffer
*rb
= fb
->Attachment
[i
].Renderbuffer
;
1232 rb
->Format
= _mesa_get_srgb_format_linear(rb
->Format
);
1237 intelMakeCurrent(__DRIcontext
* driContextPriv
,
1238 __DRIdrawable
* driDrawPriv
,
1239 __DRIdrawable
* driReadPriv
)
1241 struct brw_context
*brw
;
1242 GET_CURRENT_CONTEXT(curCtx
);
1245 brw
= (struct brw_context
*) driContextPriv
->driverPrivate
;
1249 /* According to the glXMakeCurrent() man page: "Pending commands to
1250 * the previous context, if any, are flushed before it is released."
1251 * But only flush if we're actually changing contexts.
1253 if (brw_context(curCtx
) && brw_context(curCtx
) != brw
) {
1254 _mesa_flush(curCtx
);
1257 if (driContextPriv
) {
1258 struct gl_context
*ctx
= &brw
->ctx
;
1259 struct gl_framebuffer
*fb
, *readFb
;
1261 if (driDrawPriv
== NULL
) {
1262 fb
= _mesa_get_incomplete_framebuffer();
1264 fb
= driDrawPriv
->driverPrivate
;
1265 driContextPriv
->dri2
.draw_stamp
= driDrawPriv
->dri2
.stamp
- 1;
1268 if (driReadPriv
== NULL
) {
1269 readFb
= _mesa_get_incomplete_framebuffer();
1271 readFb
= driReadPriv
->driverPrivate
;
1272 driContextPriv
->dri2
.read_stamp
= driReadPriv
->dri2
.stamp
- 1;
1275 /* The sRGB workaround changes the renderbuffer's format. We must change
1276 * the format before the renderbuffer's miptree get's allocated, otherwise
1277 * the formats of the renderbuffer and its miptree will differ.
1279 intel_gles3_srgb_workaround(brw
, fb
);
1280 intel_gles3_srgb_workaround(brw
, readFb
);
1282 /* If the context viewport hasn't been initialized, force a call out to
1283 * the loader to get buffers so we have a drawable size for the initial
1285 if (!brw
->ctx
.ViewportInitialized
)
1286 intel_prepare_render(brw
);
1288 _mesa_make_current(ctx
, fb
, readFb
);
1290 _mesa_make_current(NULL
, NULL
, NULL
);
1297 intel_resolve_for_dri2_flush(struct brw_context
*brw
,
1298 __DRIdrawable
*drawable
)
1301 /* MSAA and fast color clear are not supported, so don't waste time
1302 * checking whether a resolve is needed.
1307 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
1308 struct intel_renderbuffer
*rb
;
1310 /* Usually, only the back buffer will need to be downsampled. However,
1311 * the front buffer will also need it if the user has rendered into it.
1313 static const gl_buffer_index buffers
[2] = {
1318 for (int i
= 0; i
< 2; ++i
) {
1319 rb
= intel_get_renderbuffer(fb
, buffers
[i
]);
1320 if (rb
== NULL
|| rb
->mt
== NULL
)
1322 if (rb
->mt
->num_samples
<= 1) {
1323 assert(rb
->mt_layer
== 0 && rb
->mt_level
== 0 &&
1324 rb
->layer_count
== 1);
1325 intel_miptree_prepare_access(brw
, rb
->mt
, 0, 1, 0, 1, false, false);
1327 intel_renderbuffer_downsample(brw
, rb
);
1333 intel_bits_per_pixel(const struct intel_renderbuffer
*rb
)
1335 return _mesa_get_format_bytes(intel_rb_format(rb
)) * 8;
1339 intel_query_dri2_buffers(struct brw_context
*brw
,
1340 __DRIdrawable
*drawable
,
1341 __DRIbuffer
**buffers
,
1345 intel_process_dri2_buffer(struct brw_context
*brw
,
1346 __DRIdrawable
*drawable
,
1347 __DRIbuffer
*buffer
,
1348 struct intel_renderbuffer
*rb
,
1349 const char *buffer_name
);
1352 intel_update_image_buffers(struct brw_context
*brw
, __DRIdrawable
*drawable
);
1355 intel_update_dri2_buffers(struct brw_context
*brw
, __DRIdrawable
*drawable
)
1357 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
1358 struct intel_renderbuffer
*rb
;
1359 __DRIbuffer
*buffers
= NULL
;
1361 const char *region_name
;
1363 /* Set this up front, so that in case our buffers get invalidated
1364 * while we're getting new buffers, we don't clobber the stamp and
1365 * thus ignore the invalidate. */
1366 drawable
->lastStamp
= drawable
->dri2
.stamp
;
1368 if (unlikely(INTEL_DEBUG
& DEBUG_DRI
))
1369 fprintf(stderr
, "enter %s, drawable %p\n", __func__
, drawable
);
1371 intel_query_dri2_buffers(brw
, drawable
, &buffers
, &count
);
1373 if (buffers
== NULL
)
1376 for (int i
= 0; i
< count
; i
++) {
1377 switch (buffers
[i
].attachment
) {
1378 case __DRI_BUFFER_FRONT_LEFT
:
1379 rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
1380 region_name
= "dri2 front buffer";
1383 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
1384 rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
1385 region_name
= "dri2 fake front buffer";
1388 case __DRI_BUFFER_BACK_LEFT
:
1389 rb
= intel_get_renderbuffer(fb
, BUFFER_BACK_LEFT
);
1390 region_name
= "dri2 back buffer";
1393 case __DRI_BUFFER_DEPTH
:
1394 case __DRI_BUFFER_HIZ
:
1395 case __DRI_BUFFER_DEPTH_STENCIL
:
1396 case __DRI_BUFFER_STENCIL
:
1397 case __DRI_BUFFER_ACCUM
:
1400 "unhandled buffer attach event, attachment type %d\n",
1401 buffers
[i
].attachment
);
1405 intel_process_dri2_buffer(brw
, drawable
, &buffers
[i
], rb
, region_name
);
1411 intel_update_renderbuffers(__DRIcontext
*context
, __DRIdrawable
*drawable
)
1413 struct brw_context
*brw
= context
->driverPrivate
;
1414 __DRIscreen
*dri_screen
= brw
->screen
->driScrnPriv
;
1416 /* Set this up front, so that in case our buffers get invalidated
1417 * while we're getting new buffers, we don't clobber the stamp and
1418 * thus ignore the invalidate. */
1419 drawable
->lastStamp
= drawable
->dri2
.stamp
;
1421 if (unlikely(INTEL_DEBUG
& DEBUG_DRI
))
1422 fprintf(stderr
, "enter %s, drawable %p\n", __func__
, drawable
);
1424 if (dri_screen
->image
.loader
)
1425 intel_update_image_buffers(brw
, drawable
);
1427 intel_update_dri2_buffers(brw
, drawable
);
1429 driUpdateFramebufferSize(&brw
->ctx
, drawable
);
1433 * intel_prepare_render should be called anywhere that curent read/drawbuffer
1434 * state is required.
1437 intel_prepare_render(struct brw_context
*brw
)
1439 struct gl_context
*ctx
= &brw
->ctx
;
1440 __DRIcontext
*driContext
= brw
->driContext
;
1441 __DRIdrawable
*drawable
;
1443 drawable
= driContext
->driDrawablePriv
;
1444 if (drawable
&& drawable
->dri2
.stamp
!= driContext
->dri2
.draw_stamp
) {
1445 if (drawable
->lastStamp
!= drawable
->dri2
.stamp
)
1446 intel_update_renderbuffers(driContext
, drawable
);
1447 driContext
->dri2
.draw_stamp
= drawable
->dri2
.stamp
;
1450 drawable
= driContext
->driReadablePriv
;
1451 if (drawable
&& drawable
->dri2
.stamp
!= driContext
->dri2
.read_stamp
) {
1452 if (drawable
->lastStamp
!= drawable
->dri2
.stamp
)
1453 intel_update_renderbuffers(driContext
, drawable
);
1454 driContext
->dri2
.read_stamp
= drawable
->dri2
.stamp
;
1457 /* If we're currently rendering to the front buffer, the rendering
1458 * that will happen next will probably dirty the front buffer. So
1459 * mark it as dirty here.
1461 if (_mesa_is_front_buffer_drawing(ctx
->DrawBuffer
))
1462 brw
->front_buffer_dirty
= true;
1466 * \brief Query DRI2 to obtain a DRIdrawable's buffers.
1468 * To determine which DRI buffers to request, examine the renderbuffers
1469 * attached to the drawable's framebuffer. Then request the buffers with
1470 * DRI2GetBuffers() or DRI2GetBuffersWithFormat().
1472 * This is called from intel_update_renderbuffers().
1474 * \param drawable Drawable whose buffers are queried.
1475 * \param buffers [out] List of buffers returned by DRI2 query.
1476 * \param buffer_count [out] Number of buffers returned.
1478 * \see intel_update_renderbuffers()
1479 * \see DRI2GetBuffers()
1480 * \see DRI2GetBuffersWithFormat()
1483 intel_query_dri2_buffers(struct brw_context
*brw
,
1484 __DRIdrawable
*drawable
,
1485 __DRIbuffer
**buffers
,
1488 __DRIscreen
*dri_screen
= brw
->screen
->driScrnPriv
;
1489 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
1491 unsigned attachments
[8];
1493 struct intel_renderbuffer
*front_rb
;
1494 struct intel_renderbuffer
*back_rb
;
1496 front_rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
1497 back_rb
= intel_get_renderbuffer(fb
, BUFFER_BACK_LEFT
);
1499 memset(attachments
, 0, sizeof(attachments
));
1500 if ((_mesa_is_front_buffer_drawing(fb
) ||
1501 _mesa_is_front_buffer_reading(fb
) ||
1502 !back_rb
) && front_rb
) {
1503 /* If a fake front buffer is in use, then querying for
1504 * __DRI_BUFFER_FRONT_LEFT will cause the server to copy the image from
1505 * the real front buffer to the fake front buffer. So before doing the
1506 * query, we need to make sure all the pending drawing has landed in the
1507 * real front buffer.
1509 intel_batchbuffer_flush(brw
);
1510 intel_flush_front(&brw
->ctx
);
1512 attachments
[i
++] = __DRI_BUFFER_FRONT_LEFT
;
1513 attachments
[i
++] = intel_bits_per_pixel(front_rb
);
1514 } else if (front_rb
&& brw
->front_buffer_dirty
) {
1515 /* We have pending front buffer rendering, but we aren't querying for a
1516 * front buffer. If the front buffer we have is a fake front buffer,
1517 * the X server is going to throw it away when it processes the query.
1518 * So before doing the query, make sure all the pending drawing has
1519 * landed in the real front buffer.
1521 intel_batchbuffer_flush(brw
);
1522 intel_flush_front(&brw
->ctx
);
1526 attachments
[i
++] = __DRI_BUFFER_BACK_LEFT
;
1527 attachments
[i
++] = intel_bits_per_pixel(back_rb
);
1530 assert(i
<= ARRAY_SIZE(attachments
));
1533 dri_screen
->dri2
.loader
->getBuffersWithFormat(drawable
,
1538 drawable
->loaderPrivate
);
1542 * \brief Assign a DRI buffer's DRM region to a renderbuffer.
1544 * This is called from intel_update_renderbuffers().
1547 * DRI buffers whose attachment point is DRI2BufferStencil or
1548 * DRI2BufferDepthStencil are handled as special cases.
1550 * \param buffer_name is a human readable name, such as "dri2 front buffer",
1551 * that is passed to brw_bo_gem_create_from_name().
1553 * \see intel_update_renderbuffers()
1556 intel_process_dri2_buffer(struct brw_context
*brw
,
1557 __DRIdrawable
*drawable
,
1558 __DRIbuffer
*buffer
,
1559 struct intel_renderbuffer
*rb
,
1560 const char *buffer_name
)
1562 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
1568 unsigned num_samples
= rb
->Base
.Base
.NumSamples
;
1570 /* We try to avoid closing and reopening the same BO name, because the first
1571 * use of a mapping of the buffer involves a bunch of page faulting which is
1572 * moderately expensive.
1574 struct intel_mipmap_tree
*last_mt
;
1575 if (num_samples
== 0)
1578 last_mt
= rb
->singlesample_mt
;
1580 uint32_t old_name
= 0;
1582 /* The bo already has a name because the miptree was created by a
1583 * previous call to intel_process_dri2_buffer(). If a bo already has a
1584 * name, then brw_bo_flink() is a low-cost getter. It does not
1585 * create a new name.
1587 brw_bo_flink(last_mt
->bo
, &old_name
);
1590 if (old_name
== buffer
->name
)
1593 if (unlikely(INTEL_DEBUG
& DEBUG_DRI
)) {
1595 "attaching buffer %d, at %d, cpp %d, pitch %d\n",
1596 buffer
->name
, buffer
->attachment
,
1597 buffer
->cpp
, buffer
->pitch
);
1600 bo
= brw_bo_gem_create_from_name(brw
->bufmgr
, buffer_name
,
1604 "Failed to open BO for returned DRI2 buffer "
1605 "(%dx%d, %s, named %d).\n"
1606 "This is likely a bug in the X Server that will lead to a "
1608 drawable
->w
, drawable
->h
, buffer_name
, buffer
->name
);
1612 intel_update_winsys_renderbuffer_miptree(brw
, rb
, bo
,
1613 drawable
->w
, drawable
->h
,
1616 if (_mesa_is_front_buffer_drawing(fb
) &&
1617 (buffer
->attachment
== __DRI_BUFFER_FRONT_LEFT
||
1618 buffer
->attachment
== __DRI_BUFFER_FAKE_FRONT_LEFT
) &&
1619 rb
->Base
.Base
.NumSamples
> 1) {
1620 intel_renderbuffer_upsample(brw
, rb
);
1625 brw_bo_unreference(bo
);
1629 * \brief Query DRI image loader to obtain a DRIdrawable's buffers.
1631 * To determine which DRI buffers to request, examine the renderbuffers
1632 * attached to the drawable's framebuffer. Then request the buffers from
1635 * This is called from intel_update_renderbuffers().
1637 * \param drawable Drawable whose buffers are queried.
1638 * \param buffers [out] List of buffers returned by DRI2 query.
1639 * \param buffer_count [out] Number of buffers returned.
1641 * \see intel_update_renderbuffers()
1645 intel_update_image_buffer(struct brw_context
*intel
,
1646 __DRIdrawable
*drawable
,
1647 struct intel_renderbuffer
*rb
,
1649 enum __DRIimageBufferMask buffer_type
)
1651 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
1653 if (!rb
|| !buffer
->bo
)
1656 unsigned num_samples
= rb
->Base
.Base
.NumSamples
;
1658 /* Check and see if we're already bound to the right
1661 struct intel_mipmap_tree
*last_mt
;
1662 if (num_samples
== 0)
1665 last_mt
= rb
->singlesample_mt
;
1667 if (last_mt
&& last_mt
->bo
== buffer
->bo
)
1670 intel_update_winsys_renderbuffer_miptree(intel
, rb
, buffer
->bo
,
1671 buffer
->width
, buffer
->height
,
1674 if (_mesa_is_front_buffer_drawing(fb
) &&
1675 buffer_type
== __DRI_IMAGE_BUFFER_FRONT
&&
1676 rb
->Base
.Base
.NumSamples
> 1) {
1677 intel_renderbuffer_upsample(intel
, rb
);
1682 intel_update_image_buffers(struct brw_context
*brw
, __DRIdrawable
*drawable
)
1684 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
1685 __DRIscreen
*dri_screen
= brw
->screen
->driScrnPriv
;
1686 struct intel_renderbuffer
*front_rb
;
1687 struct intel_renderbuffer
*back_rb
;
1688 struct __DRIimageList images
;
1690 uint32_t buffer_mask
= 0;
1693 front_rb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
1694 back_rb
= intel_get_renderbuffer(fb
, BUFFER_BACK_LEFT
);
1697 format
= intel_rb_format(back_rb
);
1699 format
= intel_rb_format(front_rb
);
1703 if (front_rb
&& (_mesa_is_front_buffer_drawing(fb
) ||
1704 _mesa_is_front_buffer_reading(fb
) || !back_rb
)) {
1705 buffer_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
1709 buffer_mask
|= __DRI_IMAGE_BUFFER_BACK
;
1711 ret
= dri_screen
->image
.loader
->getBuffers(drawable
,
1712 driGLFormatToImageFormat(format
),
1713 &drawable
->dri2
.stamp
,
1714 drawable
->loaderPrivate
,
1720 if (images
.image_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
1721 drawable
->w
= images
.front
->width
;
1722 drawable
->h
= images
.front
->height
;
1723 intel_update_image_buffer(brw
,
1727 __DRI_IMAGE_BUFFER_FRONT
);
1730 if (images
.image_mask
& __DRI_IMAGE_BUFFER_BACK
) {
1731 drawable
->w
= images
.back
->width
;
1732 drawable
->h
= images
.back
->height
;
1733 intel_update_image_buffer(brw
,
1737 __DRI_IMAGE_BUFFER_BACK
);