2 * Copyright 2003 VMware, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #include <sys/errno.h>
28 #include "main/context.h"
29 #include "main/condrender.h"
30 #include "main/samplerobj.h"
31 #include "main/state.h"
32 #include "main/enums.h"
33 #include "main/macros.h"
34 #include "main/transformfeedback.h"
35 #include "main/framebuffer.h"
37 #include "vbo/vbo_context.h"
38 #include "swrast/swrast.h"
39 #include "swrast_setup/swrast_setup.h"
40 #include "drivers/common/meta.h"
41 #include "util/bitscan.h"
43 #include "brw_blorp.h"
45 #include "brw_defines.h"
46 #include "compiler/brw_eu_defines.h"
47 #include "brw_context.h"
48 #include "brw_state.h"
50 #include "intel_batchbuffer.h"
51 #include "intel_buffers.h"
52 #include "intel_fbo.h"
53 #include "intel_mipmap_tree.h"
54 #include "intel_buffer_objects.h"
56 #define FILE_DEBUG_FLAG DEBUG_PRIMS
59 static const GLenum reduced_prim
[GL_POLYGON
+1] = {
60 [GL_POINTS
] = GL_POINTS
,
61 [GL_LINES
] = GL_LINES
,
62 [GL_LINE_LOOP
] = GL_LINES
,
63 [GL_LINE_STRIP
] = GL_LINES
,
64 [GL_TRIANGLES
] = GL_TRIANGLES
,
65 [GL_TRIANGLE_STRIP
] = GL_TRIANGLES
,
66 [GL_TRIANGLE_FAN
] = GL_TRIANGLES
,
67 [GL_QUADS
] = GL_TRIANGLES
,
68 [GL_QUAD_STRIP
] = GL_TRIANGLES
,
69 [GL_POLYGON
] = GL_TRIANGLES
72 /* When the primitive changes, set a state bit and re-validate. Not
73 * the nicest and would rather deal with this by having all the
74 * programs be immune to the active primitive (ie. cope with all
75 * possibilities). That may not be realistic however.
78 brw_set_prim(struct brw_context
*brw
, const struct _mesa_prim
*prim
)
80 struct gl_context
*ctx
= &brw
->ctx
;
81 uint32_t hw_prim
= get_hw_prim_for_gl_prim(prim
->mode
);
83 DBG("PRIM: %s\n", _mesa_enum_to_string(prim
->mode
));
85 /* Slight optimization to avoid the GS program when not needed:
87 if (prim
->mode
== GL_QUAD_STRIP
&&
88 ctx
->Light
.ShadeModel
!= GL_FLAT
&&
89 ctx
->Polygon
.FrontMode
== GL_FILL
&&
90 ctx
->Polygon
.BackMode
== GL_FILL
)
91 hw_prim
= _3DPRIM_TRISTRIP
;
93 if (prim
->mode
== GL_QUADS
&& prim
->count
== 4 &&
94 ctx
->Light
.ShadeModel
!= GL_FLAT
&&
95 ctx
->Polygon
.FrontMode
== GL_FILL
&&
96 ctx
->Polygon
.BackMode
== GL_FILL
) {
97 hw_prim
= _3DPRIM_TRIFAN
;
100 if (hw_prim
!= brw
->primitive
) {
101 brw
->primitive
= hw_prim
;
102 brw
->ctx
.NewDriverState
|= BRW_NEW_PRIMITIVE
;
104 if (reduced_prim
[prim
->mode
] != brw
->reduced_primitive
) {
105 brw
->reduced_primitive
= reduced_prim
[prim
->mode
];
106 brw
->ctx
.NewDriverState
|= BRW_NEW_REDUCED_PRIMITIVE
;
112 gen6_set_prim(struct brw_context
*brw
, const struct _mesa_prim
*prim
)
114 const struct gl_context
*ctx
= &brw
->ctx
;
117 DBG("PRIM: %s\n", _mesa_enum_to_string(prim
->mode
));
119 if (prim
->mode
== GL_PATCHES
) {
120 hw_prim
= _3DPRIM_PATCHLIST(ctx
->TessCtrlProgram
.patch_vertices
);
122 hw_prim
= get_hw_prim_for_gl_prim(prim
->mode
);
125 if (hw_prim
!= brw
->primitive
) {
126 brw
->primitive
= hw_prim
;
127 brw
->ctx
.NewDriverState
|= BRW_NEW_PRIMITIVE
;
128 if (prim
->mode
== GL_PATCHES
)
129 brw
->ctx
.NewDriverState
|= BRW_NEW_PATCH_PRIMITIVE
;
135 * The hardware is capable of removing dangling vertices on its own; however,
136 * prior to Gen6, we sometimes convert quads into trifans (and quad strips
137 * into tristrips), since pre-Gen6 hardware requires a GS to render quads.
138 * This function manually trims dangling vertices from a draw call involving
139 * quads so that those dangling vertices won't get drawn when we convert to
143 trim(GLenum prim
, GLuint length
)
145 if (prim
== GL_QUAD_STRIP
)
146 return length
> 3 ? (length
- length
% 2) : 0;
147 else if (prim
== GL_QUADS
)
148 return length
- length
% 4;
155 brw_emit_prim(struct brw_context
*brw
,
156 const struct _mesa_prim
*prim
,
158 struct brw_transform_feedback_object
*xfb_obj
,
161 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
162 int verts_per_instance
;
163 int vertex_access_type
;
166 DBG("PRIM: %s %d %d\n", _mesa_enum_to_string(prim
->mode
),
167 prim
->start
, prim
->count
);
169 int start_vertex_location
= prim
->start
;
170 int base_vertex_location
= prim
->basevertex
;
173 vertex_access_type
= devinfo
->gen
>= 7 ?
174 GEN7_3DPRIM_VERTEXBUFFER_ACCESS_RANDOM
:
175 GEN4_3DPRIM_VERTEXBUFFER_ACCESS_RANDOM
;
176 start_vertex_location
+= brw
->ib
.start_vertex_offset
;
177 base_vertex_location
+= brw
->vb
.start_vertex_bias
;
179 vertex_access_type
= devinfo
->gen
>= 7 ?
180 GEN7_3DPRIM_VERTEXBUFFER_ACCESS_SEQUENTIAL
:
181 GEN4_3DPRIM_VERTEXBUFFER_ACCESS_SEQUENTIAL
;
182 start_vertex_location
+= brw
->vb
.start_vertex_bias
;
185 /* We only need to trim the primitive count on pre-Gen6. */
186 if (devinfo
->gen
< 6)
187 verts_per_instance
= trim(prim
->mode
, prim
->count
);
189 verts_per_instance
= prim
->count
;
191 /* If nothing to emit, just return. */
192 if (verts_per_instance
== 0 && !prim
->is_indirect
&& !xfb_obj
)
195 /* If we're set to always flush, do it before and after the primitive emit.
196 * We want to catch both missed flushes that hurt instruction/state cache
197 * and missed flushes of the render cache as it heads to other parts of
198 * the besides the draw code.
200 if (brw
->always_flush_cache
)
201 brw_emit_mi_flush(brw
);
203 /* If indirect, emit a bunch of loads from the indirect BO. */
205 indirect_flag
= GEN7_3DPRIM_INDIRECT_PARAMETER_ENABLE
;
207 brw_load_register_mem(brw
, GEN7_3DPRIM_VERTEX_COUNT
,
208 xfb_obj
->prim_count_bo
,
209 stream
* sizeof(uint32_t));
211 OUT_BATCH(MI_LOAD_REGISTER_IMM
| (9 - 2));
212 OUT_BATCH(GEN7_3DPRIM_INSTANCE_COUNT
);
213 OUT_BATCH(prim
->num_instances
);
214 OUT_BATCH(GEN7_3DPRIM_START_VERTEX
);
216 OUT_BATCH(GEN7_3DPRIM_BASE_VERTEX
);
218 OUT_BATCH(GEN7_3DPRIM_START_INSTANCE
);
221 } else if (prim
->is_indirect
) {
222 struct gl_buffer_object
*indirect_buffer
= brw
->ctx
.DrawIndirectBuffer
;
223 struct brw_bo
*bo
= intel_bufferobj_buffer(brw
,
224 intel_buffer_object(indirect_buffer
),
225 prim
->indirect_offset
, 5 * sizeof(GLuint
), false);
227 indirect_flag
= GEN7_3DPRIM_INDIRECT_PARAMETER_ENABLE
;
229 brw_load_register_mem(brw
, GEN7_3DPRIM_VERTEX_COUNT
, bo
,
230 prim
->indirect_offset
+ 0);
231 brw_load_register_mem(brw
, GEN7_3DPRIM_INSTANCE_COUNT
, bo
,
232 prim
->indirect_offset
+ 4);
234 brw_load_register_mem(brw
, GEN7_3DPRIM_START_VERTEX
, bo
,
235 prim
->indirect_offset
+ 8);
237 brw_load_register_mem(brw
, GEN7_3DPRIM_BASE_VERTEX
, bo
,
238 prim
->indirect_offset
+ 12);
239 brw_load_register_mem(brw
, GEN7_3DPRIM_START_INSTANCE
, bo
,
240 prim
->indirect_offset
+ 16);
242 brw_load_register_mem(brw
, GEN7_3DPRIM_START_INSTANCE
, bo
,
243 prim
->indirect_offset
+ 12);
245 OUT_BATCH(MI_LOAD_REGISTER_IMM
| (3 - 2));
246 OUT_BATCH(GEN7_3DPRIM_BASE_VERTEX
);
254 BEGIN_BATCH(devinfo
->gen
>= 7 ? 7 : 6);
256 if (devinfo
->gen
>= 7) {
257 const int predicate_enable
=
258 (brw
->predicate
.state
== BRW_PREDICATE_STATE_USE_BIT
)
259 ? GEN7_3DPRIM_PREDICATE_ENABLE
: 0;
261 OUT_BATCH(CMD_3D_PRIM
<< 16 | (7 - 2) | indirect_flag
| predicate_enable
);
262 OUT_BATCH(hw_prim
| vertex_access_type
);
264 OUT_BATCH(CMD_3D_PRIM
<< 16 | (6 - 2) |
265 hw_prim
<< GEN4_3DPRIM_TOPOLOGY_TYPE_SHIFT
|
268 OUT_BATCH(verts_per_instance
);
269 OUT_BATCH(start_vertex_location
);
270 OUT_BATCH(prim
->num_instances
);
271 OUT_BATCH(prim
->base_instance
);
272 OUT_BATCH(base_vertex_location
);
275 if (brw
->always_flush_cache
)
276 brw_emit_mi_flush(brw
);
281 brw_merge_inputs(struct brw_context
*brw
,
282 const struct gl_vertex_array
*arrays
[])
284 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
285 const struct gl_context
*ctx
= &brw
->ctx
;
288 for (i
= 0; i
< brw
->vb
.nr_buffers
; i
++) {
289 brw_bo_unreference(brw
->vb
.buffers
[i
].bo
);
290 brw
->vb
.buffers
[i
].bo
= NULL
;
292 brw
->vb
.nr_buffers
= 0;
294 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++) {
295 brw
->vb
.inputs
[i
].buffer
= -1;
296 brw
->vb
.inputs
[i
].glarray
= arrays
[i
];
299 if (devinfo
->gen
< 8 && !devinfo
->is_haswell
) {
300 uint64_t mask
= ctx
->VertexProgram
._Current
->info
.inputs_read
;
301 /* Prior to Haswell, the hardware can't natively support GL_FIXED or
302 * 2_10_10_10_REV vertex formats. Set appropriate workaround flags.
305 uint8_t wa_flags
= 0;
307 i
= u_bit_scan64(&mask
);
309 switch (brw
->vb
.inputs
[i
].glarray
->Type
) {
312 wa_flags
= brw
->vb
.inputs
[i
].glarray
->Size
;
315 case GL_INT_2_10_10_10_REV
:
316 wa_flags
|= BRW_ATTRIB_WA_SIGN
;
319 case GL_UNSIGNED_INT_2_10_10_10_REV
:
320 if (brw
->vb
.inputs
[i
].glarray
->Format
== GL_BGRA
)
321 wa_flags
|= BRW_ATTRIB_WA_BGRA
;
323 if (brw
->vb
.inputs
[i
].glarray
->Normalized
)
324 wa_flags
|= BRW_ATTRIB_WA_NORMALIZE
;
325 else if (!brw
->vb
.inputs
[i
].glarray
->Integer
)
326 wa_flags
|= BRW_ATTRIB_WA_SCALE
;
331 if (brw
->vb
.attrib_wa_flags
[i
] != wa_flags
) {
332 brw
->vb
.attrib_wa_flags
[i
] = wa_flags
;
333 brw
->ctx
.NewDriverState
|= BRW_NEW_VS_ATTRIB_WORKAROUNDS
;
340 intel_disable_rb_aux_buffer(struct brw_context
*brw
, const struct brw_bo
*bo
)
342 const struct gl_framebuffer
*fb
= brw
->ctx
.DrawBuffer
;
345 for (unsigned i
= 0; i
< fb
->_NumColorDrawBuffers
; i
++) {
346 const struct intel_renderbuffer
*irb
=
347 intel_renderbuffer(fb
->_ColorDrawBuffers
[i
]);
349 if (irb
&& irb
->mt
->bo
== bo
) {
350 found
= brw
->draw_aux_buffer_disabled
[i
] = true;
358 * \brief Resolve buffers before drawing.
360 * Resolve the depth buffer's HiZ buffer, resolve the depth buffer of each
361 * enabled depth texture, and flush the render cache for any dirty textures.
364 brw_predraw_resolve_inputs(struct brw_context
*brw
)
366 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
367 struct gl_context
*ctx
= &brw
->ctx
;
368 struct intel_texture_object
*tex_obj
;
370 memset(brw
->draw_aux_buffer_disabled
, 0,
371 sizeof(brw
->draw_aux_buffer_disabled
));
373 /* Resolve depth buffer and render cache of each enabled texture. */
374 int maxEnabledUnit
= ctx
->Texture
._MaxEnabledTexImageUnit
;
375 for (int i
= 0; i
<= maxEnabledUnit
; i
++) {
376 if (!ctx
->Texture
.Unit
[i
]._Current
)
378 tex_obj
= intel_texture_object(ctx
->Texture
.Unit
[i
]._Current
);
379 if (!tex_obj
|| !tex_obj
->mt
)
382 struct gl_sampler_object
*sampler
= _mesa_get_samplerobj(ctx
, i
);
383 enum isl_format view_format
=
384 translate_tex_format(brw
, tex_obj
->_Format
, sampler
->sRGBDecode
);
387 intel_miptree_prepare_texture(brw
, tex_obj
->mt
, view_format
,
390 if (!aux_supported
&& devinfo
->gen
>= 9 &&
391 intel_disable_rb_aux_buffer(brw
, tex_obj
->mt
->bo
)) {
392 perf_debug("Sampling renderbuffer with non-compressible format - "
393 "turning off compression\n");
396 brw_render_cache_set_check_flush(brw
, tex_obj
->mt
->bo
);
398 if (tex_obj
->base
.StencilSampling
||
399 tex_obj
->mt
->format
== MESA_FORMAT_S_UINT8
) {
400 intel_update_r8stencil(brw
, tex_obj
->mt
);
404 /* Resolve color for each active shader image. */
405 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
406 const struct gl_program
*prog
= ctx
->_Shader
->CurrentProgram
[i
];
408 if (unlikely(prog
&& prog
->info
.num_images
)) {
409 for (unsigned j
= 0; j
< prog
->info
.num_images
; j
++) {
410 struct gl_image_unit
*u
=
411 &ctx
->ImageUnits
[prog
->sh
.ImageUnits
[j
]];
412 tex_obj
= intel_texture_object(u
->TexObj
);
414 if (tex_obj
&& tex_obj
->mt
) {
415 intel_miptree_prepare_image(brw
, tex_obj
->mt
);
417 if (tex_obj
->mt
->aux_usage
== ISL_AUX_USAGE_CCS_E
&&
418 intel_disable_rb_aux_buffer(brw
, tex_obj
->mt
->bo
)) {
419 perf_debug("Using renderbuffer as shader image - turning "
420 "off lossless compression\n");
423 brw_render_cache_set_check_flush(brw
, tex_obj
->mt
->bo
);
431 brw_predraw_resolve_framebuffer(struct brw_context
*brw
)
433 struct gl_context
*ctx
= &brw
->ctx
;
434 struct intel_renderbuffer
*depth_irb
;
436 /* Resolve the depth buffer's HiZ buffer. */
437 depth_irb
= intel_get_renderbuffer(ctx
->DrawBuffer
, BUFFER_DEPTH
);
438 if (depth_irb
&& depth_irb
->mt
) {
439 intel_miptree_prepare_depth(brw
, depth_irb
->mt
,
442 depth_irb
->layer_count
);
445 /* Resolve color buffers for non-coherent framebuffer fetch. */
446 if (!ctx
->Extensions
.MESA_shader_framebuffer_fetch
&&
447 ctx
->FragmentProgram
._Current
&&
448 ctx
->FragmentProgram
._Current
->info
.outputs_read
) {
449 const struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
451 for (unsigned i
= 0; i
< fb
->_NumColorDrawBuffers
; i
++) {
452 const struct intel_renderbuffer
*irb
=
453 intel_renderbuffer(fb
->_ColorDrawBuffers
[i
]);
456 intel_miptree_prepare_fb_fetch(brw
, irb
->mt
, irb
->mt_level
,
457 irb
->mt_layer
, irb
->layer_count
);
462 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
463 for (int i
= 0; i
< fb
->_NumColorDrawBuffers
; i
++) {
464 struct intel_renderbuffer
*irb
=
465 intel_renderbuffer(fb
->_ColorDrawBuffers
[i
]);
467 if (irb
== NULL
|| irb
->mt
== NULL
)
470 intel_miptree_prepare_render(brw
, irb
->mt
, irb
->mt_level
,
471 irb
->mt_layer
, irb
->layer_count
,
472 ctx
->Color
.sRGBEnabled
,
473 ctx
->Color
.BlendEnabled
& (1 << i
));
478 * \brief Call this after drawing to mark which buffers need resolving
480 * If the depth buffer was written to and if it has an accompanying HiZ
481 * buffer, then mark that it needs a depth resolve.
483 * If the color buffer is a multisample window system buffer, then
484 * mark that it needs a downsample.
486 * Also mark any render targets which will be textured as needing a render
490 brw_postdraw_set_buffers_need_resolve(struct brw_context
*brw
)
492 struct gl_context
*ctx
= &brw
->ctx
;
493 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
495 struct intel_renderbuffer
*front_irb
= NULL
;
496 struct intel_renderbuffer
*back_irb
= intel_get_renderbuffer(fb
, BUFFER_BACK_LEFT
);
497 struct intel_renderbuffer
*depth_irb
= intel_get_renderbuffer(fb
, BUFFER_DEPTH
);
498 struct intel_renderbuffer
*stencil_irb
= intel_get_renderbuffer(fb
, BUFFER_STENCIL
);
499 struct gl_renderbuffer_attachment
*depth_att
= &fb
->Attachment
[BUFFER_DEPTH
];
501 if (_mesa_is_front_buffer_drawing(fb
))
502 front_irb
= intel_get_renderbuffer(fb
, BUFFER_FRONT_LEFT
);
505 front_irb
->need_downsample
= true;
507 back_irb
->need_downsample
= true;
509 bool depth_written
= brw_depth_writes_enabled(brw
);
510 if (depth_att
->Layered
) {
511 intel_miptree_finish_depth(brw
, depth_irb
->mt
,
514 depth_irb
->layer_count
,
517 intel_miptree_finish_depth(brw
, depth_irb
->mt
,
519 depth_irb
->mt_layer
, 1,
523 brw_render_cache_set_add_bo(brw
, depth_irb
->mt
->bo
);
526 if (ctx
->Extensions
.ARB_stencil_texturing
&&
527 stencil_irb
&& brw
->stencil_write_enabled
) {
528 brw_render_cache_set_add_bo(brw
, stencil_irb
->mt
->bo
);
531 for (unsigned i
= 0; i
< fb
->_NumColorDrawBuffers
; i
++) {
532 struct intel_renderbuffer
*irb
=
533 intel_renderbuffer(fb
->_ColorDrawBuffers
[i
]);
538 brw_render_cache_set_add_bo(brw
, irb
->mt
->bo
);
539 intel_miptree_finish_render(brw
, irb
->mt
, irb
->mt_level
,
540 irb
->mt_layer
, irb
->layer_count
,
541 ctx
->Color
.sRGBEnabled
,
542 ctx
->Color
.BlendEnabled
& (1 << i
));
547 intel_renderbuffer_move_temp_back(struct brw_context
*brw
,
548 struct intel_renderbuffer
*irb
)
550 if (irb
->align_wa_mt
== NULL
)
553 brw_render_cache_set_check_flush(brw
, irb
->align_wa_mt
->bo
);
555 intel_miptree_copy_slice(brw
, irb
->align_wa_mt
, 0, 0,
557 irb
->Base
.Base
.TexImage
->Level
, irb
->mt_layer
);
559 intel_miptree_reference(&irb
->align_wa_mt
, NULL
);
561 /* Finally restore the x,y to correspond to full miptree. */
562 intel_renderbuffer_set_draw_offset(irb
);
564 /* Make sure render surface state gets re-emitted with updated miptree. */
565 brw
->NewGLState
|= _NEW_BUFFERS
;
569 brw_postdraw_reconcile_align_wa_slices(struct brw_context
*brw
)
571 struct gl_context
*ctx
= &brw
->ctx
;
572 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
574 struct intel_renderbuffer
*depth_irb
=
575 intel_get_renderbuffer(fb
, BUFFER_DEPTH
);
576 struct intel_renderbuffer
*stencil_irb
=
577 intel_get_renderbuffer(fb
, BUFFER_STENCIL
);
579 if (depth_irb
&& depth_irb
->align_wa_mt
)
580 intel_renderbuffer_move_temp_back(brw
, depth_irb
);
582 if (stencil_irb
&& stencil_irb
->align_wa_mt
)
583 intel_renderbuffer_move_temp_back(brw
, stencil_irb
);
585 for (unsigned i
= 0; i
< fb
->_NumColorDrawBuffers
; i
++) {
586 struct intel_renderbuffer
*irb
=
587 intel_renderbuffer(fb
->_ColorDrawBuffers
[i
]);
589 if (!irb
|| irb
->align_wa_mt
== NULL
)
592 intel_renderbuffer_move_temp_back(brw
, irb
);
596 /* May fail if out of video memory for texture or vbo upload, or on
597 * fallback conditions.
600 brw_try_draw_prims(struct gl_context
*ctx
,
601 const struct gl_vertex_array
*arrays
[],
602 const struct _mesa_prim
*prims
,
604 const struct _mesa_index_buffer
*ib
,
605 bool index_bounds_valid
,
608 struct brw_transform_feedback_object
*xfb_obj
,
610 struct gl_buffer_object
*indirect
)
612 struct brw_context
*brw
= brw_context(ctx
);
613 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
615 bool fail_next
= false;
618 _mesa_update_state(ctx
);
620 /* We have to validate the textures *before* checking for fallbacks;
621 * otherwise, the software fallback won't be able to rely on the
622 * texture state, the firstLevel and lastLevel fields won't be
623 * set in the intel texture object (they'll both be 0), and the
624 * software fallback will segfault if it attempts to access any
625 * texture level other than level 0.
627 brw_validate_textures(brw
);
629 /* Find the highest sampler unit used by each shader program. A bit-count
630 * won't work since ARB programs use the texture unit number as the sampler
633 brw
->wm
.base
.sampler_count
=
634 util_last_bit(ctx
->FragmentProgram
._Current
->SamplersUsed
);
635 brw
->gs
.base
.sampler_count
= ctx
->GeometryProgram
._Current
?
636 util_last_bit(ctx
->GeometryProgram
._Current
->SamplersUsed
) : 0;
637 brw
->tes
.base
.sampler_count
= ctx
->TessEvalProgram
._Current
?
638 util_last_bit(ctx
->TessEvalProgram
._Current
->SamplersUsed
) : 0;
639 brw
->tcs
.base
.sampler_count
= ctx
->TessCtrlProgram
._Current
?
640 util_last_bit(ctx
->TessCtrlProgram
._Current
->SamplersUsed
) : 0;
641 brw
->vs
.base
.sampler_count
=
642 util_last_bit(ctx
->VertexProgram
._Current
->SamplersUsed
);
644 intel_prepare_render(brw
);
646 /* This workaround has to happen outside of brw_upload_render_state()
647 * because it may flush the batchbuffer for a blit, affecting the state
650 brw_workaround_depthstencil_alignment(brw
, 0);
652 /* Resolves must occur after updating renderbuffers, updating context state,
653 * and finalizing textures but before setting up any hardware state for
656 brw_predraw_resolve_inputs(brw
);
657 brw_predraw_resolve_framebuffer(brw
);
659 /* Bind all inputs, derive varying and size information:
661 brw_merge_inputs(brw
, arrays
);
664 brw
->ctx
.NewDriverState
|= BRW_NEW_INDICES
;
666 brw
->vb
.index_bounds_valid
= index_bounds_valid
;
667 brw
->vb
.min_index
= min_index
;
668 brw
->vb
.max_index
= max_index
;
669 brw
->ctx
.NewDriverState
|= BRW_NEW_VERTICES
;
671 for (i
= 0; i
< nr_prims
; i
++) {
672 int estimated_max_prim_size
;
673 const int sampler_state_size
= 16;
675 estimated_max_prim_size
= 512; /* batchbuffer commands */
676 estimated_max_prim_size
+= BRW_MAX_TEX_UNIT
*
677 (sampler_state_size
+ sizeof(struct gen5_sampler_default_color
));
678 estimated_max_prim_size
+= 1024; /* gen6 VS push constants */
679 estimated_max_prim_size
+= 1024; /* gen6 WM push constants */
680 estimated_max_prim_size
+= 512; /* misc. pad */
682 /* Flag BRW_NEW_DRAW_CALL on every draw. This allows us to have
683 * atoms that happen on every draw call.
685 brw
->ctx
.NewDriverState
|= BRW_NEW_DRAW_CALL
;
687 /* Flush the batch if it's approaching full, so that we don't wrap while
688 * we've got validated state that needs to be in the same batch as the
691 intel_batchbuffer_require_space(brw
, estimated_max_prim_size
, RENDER_RING
);
692 intel_batchbuffer_save_state(brw
);
694 if (brw
->num_instances
!= prims
[i
].num_instances
||
695 brw
->basevertex
!= prims
[i
].basevertex
||
696 brw
->baseinstance
!= prims
[i
].base_instance
) {
697 brw
->num_instances
= prims
[i
].num_instances
;
698 brw
->basevertex
= prims
[i
].basevertex
;
699 brw
->baseinstance
= prims
[i
].base_instance
;
700 if (i
> 0) { /* For i == 0 we just did this before the loop */
701 brw
->ctx
.NewDriverState
|= BRW_NEW_VERTICES
;
702 brw_merge_inputs(brw
, arrays
);
706 /* Determine if we need to flag BRW_NEW_VERTICES for updating the
707 * gl_BaseVertexARB or gl_BaseInstanceARB values. For indirect draw, we
708 * always flag if the shader uses one of the values. For direct draws,
709 * we only flag if the values change.
711 const int new_basevertex
=
712 prims
[i
].indexed
? prims
[i
].basevertex
: prims
[i
].start
;
713 const int new_baseinstance
= prims
[i
].base_instance
;
714 const struct brw_vs_prog_data
*vs_prog_data
=
715 brw_vs_prog_data(brw
->vs
.base
.prog_data
);
717 const bool uses_draw_parameters
=
718 vs_prog_data
->uses_basevertex
||
719 vs_prog_data
->uses_baseinstance
;
721 if ((uses_draw_parameters
&& prims
[i
].is_indirect
) ||
722 (vs_prog_data
->uses_basevertex
&&
723 brw
->draw
.params
.gl_basevertex
!= new_basevertex
) ||
724 (vs_prog_data
->uses_baseinstance
&&
725 brw
->draw
.params
.gl_baseinstance
!= new_baseinstance
))
726 brw
->ctx
.NewDriverState
|= BRW_NEW_VERTICES
;
729 brw
->draw
.params
.gl_basevertex
= new_basevertex
;
730 brw
->draw
.params
.gl_baseinstance
= new_baseinstance
;
731 brw_bo_unreference(brw
->draw
.draw_params_bo
);
733 if (prims
[i
].is_indirect
) {
734 /* Point draw_params_bo at the indirect buffer. */
735 brw
->draw
.draw_params_bo
=
736 intel_buffer_object(ctx
->DrawIndirectBuffer
)->buffer
;
737 brw_bo_reference(brw
->draw
.draw_params_bo
);
738 brw
->draw
.draw_params_offset
=
739 prims
[i
].indirect_offset
+ (prims
[i
].indexed
? 12 : 8);
741 /* Set draw_params_bo to NULL so brw_prepare_vertices knows it
742 * has to upload gl_BaseVertex and such if they're needed.
744 brw
->draw
.draw_params_bo
= NULL
;
745 brw
->draw
.draw_params_offset
= 0;
748 /* gl_DrawID always needs its own vertex buffer since it's not part of
749 * the indirect parameter buffer. If the program uses gl_DrawID we need
750 * to flag BRW_NEW_VERTICES. For the first iteration, we don't have
751 * valid vs_prog_data, but we always flag BRW_NEW_VERTICES before
754 brw
->draw
.gl_drawid
= prims
[i
].draw_id
;
755 brw_bo_unreference(brw
->draw
.draw_id_bo
);
756 brw
->draw
.draw_id_bo
= NULL
;
757 if (i
> 0 && vs_prog_data
->uses_drawid
)
758 brw
->ctx
.NewDriverState
|= BRW_NEW_VERTICES
;
760 if (devinfo
->gen
< 6)
761 brw_set_prim(brw
, &prims
[i
]);
763 gen6_set_prim(brw
, &prims
[i
]);
767 /* Note that before the loop, brw->ctx.NewDriverState was set to != 0, and
768 * that the state updated in the loop outside of this block is that in
769 * *_set_prim or intel_batchbuffer_flush(), which only impacts
770 * brw->ctx.NewDriverState.
772 if (brw
->ctx
.NewDriverState
) {
773 brw
->no_batch_wrap
= true;
774 brw_upload_render_state(brw
);
777 brw_emit_prim(brw
, &prims
[i
], brw
->primitive
, xfb_obj
, stream
);
779 brw
->no_batch_wrap
= false;
781 if (!brw_batch_has_aperture_space(brw
, 0)) {
783 intel_batchbuffer_reset_to_saved(brw
);
784 intel_batchbuffer_flush(brw
);
788 int ret
= intel_batchbuffer_flush(brw
);
789 WARN_ONCE(ret
== -ENOSPC
,
790 "i965: Single primitive emit exceeded "
791 "available aperture space\n");
795 /* Now that we know we haven't run out of aperture space, we can safely
796 * reset the dirty bits.
798 if (brw
->ctx
.NewDriverState
)
799 brw_render_state_finished(brw
);
802 if (brw
->always_flush_batch
)
803 intel_batchbuffer_flush(brw
);
805 brw_program_cache_check_size(brw
);
806 brw_postdraw_reconcile_align_wa_slices(brw
);
807 brw_postdraw_set_buffers_need_resolve(brw
);
813 brw_draw_prims(struct gl_context
*ctx
,
814 const struct _mesa_prim
*prims
,
816 const struct _mesa_index_buffer
*ib
,
817 GLboolean index_bounds_valid
,
820 struct gl_transform_feedback_object
*gl_xfb_obj
,
822 struct gl_buffer_object
*indirect
)
824 struct brw_context
*brw
= brw_context(ctx
);
825 const struct gl_vertex_array
**arrays
= ctx
->Array
._DrawArrays
;
826 struct brw_transform_feedback_object
*xfb_obj
=
827 (struct brw_transform_feedback_object
*) gl_xfb_obj
;
829 if (!brw_check_conditional_render(brw
))
832 /* Handle primitive restart if needed */
833 if (brw_handle_primitive_restart(ctx
, prims
, nr_prims
, ib
, indirect
)) {
834 /* The draw was handled, so we can exit now */
838 /* Do GL_SELECT and GL_FEEDBACK rendering using swrast, even though it
839 * won't support all the extensions we support.
841 if (ctx
->RenderMode
!= GL_RENDER
) {
842 perf_debug("%s render mode not supported in hardware\n",
843 _mesa_enum_to_string(ctx
->RenderMode
));
844 _swsetup_Wakeup(ctx
);
846 _tnl_draw_prims(ctx
, prims
, nr_prims
, ib
,
847 index_bounds_valid
, min_index
, max_index
, NULL
, 0, NULL
);
851 /* If we're going to have to upload any of the user's vertex arrays, then
852 * get the minimum and maximum of their index buffer so we know what range
855 if (!index_bounds_valid
&& !vbo_all_varyings_in_vbos(arrays
)) {
856 perf_debug("Scanning index buffer to compute index buffer bounds. "
857 "Use glDrawRangeElements() to avoid this.\n");
858 vbo_get_minmax_indices(ctx
, prims
, ib
, &min_index
, &max_index
, nr_prims
);
859 index_bounds_valid
= true;
862 /* Try drawing with the hardware, but don't do anything else if we can't
863 * manage it. swrast doesn't support our featureset, so we can't fall back
866 brw_try_draw_prims(ctx
, arrays
, prims
, nr_prims
, ib
, index_bounds_valid
,
867 min_index
, max_index
, xfb_obj
, stream
, indirect
);
871 brw_draw_init(struct brw_context
*brw
)
873 struct gl_context
*ctx
= &brw
->ctx
;
874 struct vbo_context
*vbo
= vbo_context(ctx
);
876 /* Register our drawing function:
878 vbo
->draw_prims
= brw_draw_prims
;
880 for (int i
= 0; i
< VERT_ATTRIB_MAX
; i
++)
881 brw
->vb
.inputs
[i
].buffer
= -1;
882 brw
->vb
.nr_buffers
= 0;
883 brw
->vb
.nr_enabled
= 0;
887 brw_draw_destroy(struct brw_context
*brw
)
891 for (i
= 0; i
< brw
->vb
.nr_buffers
; i
++) {
892 brw_bo_unreference(brw
->vb
.buffers
[i
].bo
);
893 brw
->vb
.buffers
[i
].bo
= NULL
;
895 brw
->vb
.nr_buffers
= 0;
897 for (i
= 0; i
< brw
->vb
.nr_enabled
; i
++) {
898 brw
->vb
.enabled
[i
]->buffer
= -1;
900 brw
->vb
.nr_enabled
= 0;
902 brw_bo_unreference(brw
->ib
.bo
);