2 * Copyright © 2011 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 * @file gen7_sol_state.c
27 * Controls the stream output logic (SOL) stage of the gen7 hardware, which is
28 * used to implement GL_EXT_transform_feedback.
31 #include "brw_context.h"
32 #include "brw_state.h"
33 #include "brw_defines.h"
34 #include "intel_batchbuffer.h"
35 #include "intel_buffer_objects.h"
36 #include "main/transformfeedback.h"
39 upload_3dstate_so_buffers(struct brw_context
*brw
)
41 struct gl_context
*ctx
= &brw
->ctx
;
42 /* BRW_NEW_TRANSFORM_FEEDBACK */
43 struct gl_transform_feedback_object
*xfb_obj
=
44 ctx
->TransformFeedback
.CurrentObject
;
45 const struct gl_transform_feedback_info
*linked_xfb_info
=
46 &xfb_obj
->shader_program
->LinkedTransformFeedback
;
49 /* Set up the up to 4 output buffers. These are the ranges defined in the
50 * gl_transform_feedback_object.
52 for (i
= 0; i
< 4; i
++) {
53 struct intel_buffer_object
*bufferobj
=
54 intel_buffer_object(xfb_obj
->Buffers
[i
]);
59 if (!xfb_obj
->Buffers
[i
]) {
60 /* The pitch of 0 in this command indicates that the buffer is
61 * unbound and won't be written to.
64 OUT_BATCH(_3DSTATE_SO_BUFFER
<< 16 | (4 - 2));
65 OUT_BATCH((i
<< SO_BUFFER_INDEX_SHIFT
));
73 stride
= linked_xfb_info
->BufferStride
[i
] * 4;
75 start
= xfb_obj
->Offset
[i
];
76 assert(start
% 4 == 0);
77 end
= ALIGN(start
+ xfb_obj
->Size
[i
], 4);
78 bo
= intel_bufferobj_buffer(brw
, bufferobj
, start
, end
- start
);
79 assert(end
<= bo
->size
);
82 OUT_BATCH(_3DSTATE_SO_BUFFER
<< 16 | (4 - 2));
83 OUT_BATCH((i
<< SO_BUFFER_INDEX_SHIFT
) | stride
);
84 OUT_RELOC(bo
, I915_GEM_DOMAIN_RENDER
, I915_GEM_DOMAIN_RENDER
, start
);
85 OUT_RELOC(bo
, I915_GEM_DOMAIN_RENDER
, I915_GEM_DOMAIN_RENDER
, end
);
91 * Outputs the 3DSTATE_SO_DECL_LIST command.
93 * The data output is a series of 64-bit entries containing a SO_DECL per
94 * stream. We only have one stream of rendering coming out of the GS unit, so
95 * we only emit stream 0 (low 16 bits) SO_DECLs.
98 gen7_upload_3dstate_so_decl_list(struct brw_context
*brw
,
99 const struct brw_vue_map
*vue_map
)
101 struct gl_context
*ctx
= &brw
->ctx
;
102 /* BRW_NEW_TRANSFORM_FEEDBACK */
103 struct gl_transform_feedback_object
*xfb_obj
=
104 ctx
->TransformFeedback
.CurrentObject
;
105 const struct gl_transform_feedback_info
*linked_xfb_info
=
106 &xfb_obj
->shader_program
->LinkedTransformFeedback
;
107 uint16_t so_decl
[MAX_VERTEX_STREAMS
][128];
108 int buffer_mask
[MAX_VERTEX_STREAMS
] = {0, 0, 0, 0};
109 int next_offset
[MAX_VERTEX_STREAMS
] = {0, 0, 0, 0};
110 int decls
[MAX_VERTEX_STREAMS
] = {0, 0, 0, 0};
112 STATIC_ASSERT(ARRAY_SIZE(so_decl
[0]) >= MAX_PROGRAM_OUTPUTS
);
114 memset(so_decl
, 0, sizeof(so_decl
));
116 /* Construct the list of SO_DECLs to be emitted. The formatting of the
117 * command is feels strange -- each dword pair contains a SO_DECL per stream.
119 for (int i
= 0; i
< linked_xfb_info
->NumOutputs
; i
++) {
120 int buffer
= linked_xfb_info
->Outputs
[i
].OutputBuffer
;
122 int varying
= linked_xfb_info
->Outputs
[i
].OutputRegister
;
123 const unsigned components
= linked_xfb_info
->Outputs
[i
].NumComponents
;
124 unsigned component_mask
= (1 << components
) - 1;
125 unsigned stream_id
= linked_xfb_info
->Outputs
[i
].StreamId
;
127 assert(stream_id
< MAX_VERTEX_STREAMS
);
129 /* gl_PointSize is stored in VARYING_SLOT_PSIZ.w
130 * gl_Layer is stored in VARYING_SLOT_PSIZ.y
131 * gl_ViewportIndex is stored in VARYING_SLOT_PSIZ.z
133 if (varying
== VARYING_SLOT_PSIZ
) {
134 assert(components
== 1);
135 component_mask
<<= 3;
136 } else if (varying
== VARYING_SLOT_LAYER
) {
137 assert(components
== 1);
138 component_mask
<<= 1;
139 } else if (varying
== VARYING_SLOT_VIEWPORT
) {
140 assert(components
== 1);
141 component_mask
<<= 2;
143 component_mask
<<= linked_xfb_info
->Outputs
[i
].ComponentOffset
;
146 buffer_mask
[stream_id
] |= 1 << buffer
;
148 decl
|= buffer
<< SO_DECL_OUTPUT_BUFFER_SLOT_SHIFT
;
149 if (varying
== VARYING_SLOT_LAYER
|| varying
== VARYING_SLOT_VIEWPORT
) {
150 decl
|= vue_map
->varying_to_slot
[VARYING_SLOT_PSIZ
] <<
151 SO_DECL_REGISTER_INDEX_SHIFT
;
153 assert(vue_map
->varying_to_slot
[varying
] >= 0);
154 decl
|= vue_map
->varying_to_slot
[varying
] <<
155 SO_DECL_REGISTER_INDEX_SHIFT
;
157 decl
|= component_mask
<< SO_DECL_COMPONENT_MASK_SHIFT
;
159 /* Mesa doesn't store entries for gl_SkipComponents in the Outputs[]
160 * array. Instead, it simply increments DstOffset for the following
161 * input by the number of components that should be skipped.
163 * Our hardware is unusual in that it requires us to program SO_DECLs
164 * for fake "hole" components, rather than simply taking the offset
165 * for each real varying. Each hole can have size 1, 2, 3, or 4; we
166 * program as many size = 4 holes as we can, then a final hole to
167 * accommodate the final 1, 2, or 3 remaining.
169 int skip_components
=
170 linked_xfb_info
->Outputs
[i
].DstOffset
- next_offset
[buffer
];
172 next_offset
[buffer
] += skip_components
;
174 while (skip_components
>= 4) {
175 so_decl
[stream_id
][decls
[stream_id
]++] = SO_DECL_HOLE_FLAG
| 0xf;
176 skip_components
-= 4;
178 if (skip_components
> 0)
179 so_decl
[stream_id
][decls
[stream_id
]++] =
180 SO_DECL_HOLE_FLAG
| ((1 << skip_components
) - 1);
182 assert(linked_xfb_info
->Outputs
[i
].DstOffset
== next_offset
[buffer
]);
184 next_offset
[buffer
] += components
;
186 so_decl
[stream_id
][decls
[stream_id
]++] = decl
;
188 if (decls
[stream_id
] > max_decls
)
189 max_decls
= decls
[stream_id
];
192 BEGIN_BATCH(max_decls
* 2 + 3);
193 OUT_BATCH(_3DSTATE_SO_DECL_LIST
<< 16 | (max_decls
* 2 + 1));
195 OUT_BATCH((buffer_mask
[0] << SO_STREAM_TO_BUFFER_SELECTS_0_SHIFT
) |
196 (buffer_mask
[1] << SO_STREAM_TO_BUFFER_SELECTS_1_SHIFT
) |
197 (buffer_mask
[2] << SO_STREAM_TO_BUFFER_SELECTS_2_SHIFT
) |
198 (buffer_mask
[3] << SO_STREAM_TO_BUFFER_SELECTS_3_SHIFT
));
200 OUT_BATCH((decls
[0] << SO_NUM_ENTRIES_0_SHIFT
) |
201 (decls
[1] << SO_NUM_ENTRIES_1_SHIFT
) |
202 (decls
[2] << SO_NUM_ENTRIES_2_SHIFT
) |
203 (decls
[3] << SO_NUM_ENTRIES_3_SHIFT
));
205 for (int i
= 0; i
< max_decls
; i
++) {
206 /* Stream 1 | Stream 0 */
207 OUT_BATCH(((uint32_t) so_decl
[1][i
]) << 16 | so_decl
[0][i
]);
208 /* Stream 3 | Stream 2 */
209 OUT_BATCH(((uint32_t) so_decl
[3][i
]) << 16 | so_decl
[2][i
]);
216 upload_3dstate_streamout(struct brw_context
*brw
, bool active
,
217 const struct brw_vue_map
*vue_map
)
219 struct gl_context
*ctx
= &brw
->ctx
;
220 /* BRW_NEW_TRANSFORM_FEEDBACK */
221 struct gl_transform_feedback_object
*xfb_obj
=
222 ctx
->TransformFeedback
.CurrentObject
;
223 uint32_t dw1
= 0, dw2
= 0;
227 int urb_entry_read_offset
= 0;
228 int urb_entry_read_length
= (vue_map
->num_slots
+ 1) / 2 -
229 urb_entry_read_offset
;
231 dw1
|= SO_FUNCTION_ENABLE
;
232 dw1
|= SO_STATISTICS_ENABLE
;
235 if (ctx
->Light
.ProvokingVertex
!= GL_FIRST_VERTEX_CONVENTION
)
236 dw1
|= SO_REORDER_TRAILING
;
238 for (i
= 0; i
< 4; i
++) {
239 if (xfb_obj
->Buffers
[i
]) {
240 dw1
|= SO_BUFFER_ENABLE(i
);
244 /* We always read the whole vertex. This could be reduced at some
245 * point by reading less and offsetting the register index in the
248 dw2
|= SET_FIELD(urb_entry_read_offset
, SO_STREAM_0_VERTEX_READ_OFFSET
);
249 dw2
|= SET_FIELD(urb_entry_read_length
- 1, SO_STREAM_0_VERTEX_READ_LENGTH
);
251 dw2
|= SET_FIELD(urb_entry_read_offset
, SO_STREAM_1_VERTEX_READ_OFFSET
);
252 dw2
|= SET_FIELD(urb_entry_read_length
- 1, SO_STREAM_1_VERTEX_READ_LENGTH
);
254 dw2
|= SET_FIELD(urb_entry_read_offset
, SO_STREAM_2_VERTEX_READ_OFFSET
);
255 dw2
|= SET_FIELD(urb_entry_read_length
- 1, SO_STREAM_2_VERTEX_READ_LENGTH
);
257 dw2
|= SET_FIELD(urb_entry_read_offset
, SO_STREAM_3_VERTEX_READ_OFFSET
);
258 dw2
|= SET_FIELD(urb_entry_read_length
- 1, SO_STREAM_3_VERTEX_READ_LENGTH
);
262 OUT_BATCH(_3DSTATE_STREAMOUT
<< 16 | (3 - 2));
269 upload_sol_state(struct brw_context
*brw
)
271 struct gl_context
*ctx
= &brw
->ctx
;
272 /* BRW_NEW_TRANSFORM_FEEDBACK */
273 bool active
= _mesa_is_xfb_active_and_unpaused(ctx
);
276 upload_3dstate_so_buffers(brw
);
277 /* BRW_NEW_VUE_MAP_GEOM_OUT */
278 gen7_upload_3dstate_so_decl_list(brw
, &brw
->vue_map_geom_out
);
281 /* Finally, set up the SOL stage. This command must always follow updates to
282 * the nonpipelined SOL state (3DSTATE_SO_BUFFER, 3DSTATE_SO_DECL_LIST) or
283 * MMIO register updates (current performed by the kernel at each batch
286 upload_3dstate_streamout(brw
, active
, &brw
->vue_map_geom_out
);
289 const struct brw_tracked_state gen7_sol_state
= {
292 .brw
= BRW_NEW_BATCH
|
293 BRW_NEW_VUE_MAP_GEOM_OUT
|
294 BRW_NEW_TRANSFORM_FEEDBACK
,
296 .emit
= upload_sol_state
,
300 * Tally the number of primitives generated so far.
302 * The buffer contains a series of pairs:
303 * (<start0, start1, start2, start3>, <end0, end1, end2, end3>) ;
304 * (<start0, start1, start2, start3>, <end0, end1, end2, end3>) ;
306 * For each stream, we subtract the pair of values (end - start) to get the
307 * number of primitives generated during one section. We accumulate these
308 * values, adding them up to get the total number of primitives generated.
311 gen7_tally_prims_generated(struct brw_context
*brw
,
312 struct brw_transform_feedback_object
*obj
)
314 /* If the current batch is still contributing to the number of primitives
315 * generated, flush it now so the results will be present when mapped.
317 if (drm_intel_bo_references(brw
->batch
.bo
, obj
->prim_count_bo
))
318 intel_batchbuffer_flush(brw
);
320 if (unlikely(brw
->perf_debug
&& drm_intel_bo_busy(obj
->prim_count_bo
)))
321 perf_debug("Stalling for # of transform feedback primitives written.\n");
323 drm_intel_bo_map(obj
->prim_count_bo
, false);
324 uint64_t *prim_counts
= obj
->prim_count_bo
->virtual;
326 assert(obj
->prim_count_buffer_index
% (2 * BRW_MAX_XFB_STREAMS
) == 0);
327 int pairs
= obj
->prim_count_buffer_index
/ (2 * BRW_MAX_XFB_STREAMS
);
329 for (int i
= 0; i
< pairs
; i
++) {
330 for (int s
= 0; s
< BRW_MAX_XFB_STREAMS
; s
++) {
331 obj
->prims_generated
[s
] +=
332 prim_counts
[BRW_MAX_XFB_STREAMS
+ s
] - prim_counts
[s
];
334 prim_counts
+= 2 * BRW_MAX_XFB_STREAMS
; /* move to the next pair */
337 drm_intel_bo_unmap(obj
->prim_count_bo
);
339 /* We've already gathered up the old data; we can safely overwrite it now. */
340 obj
->prim_count_buffer_index
= 0;
344 * Store the SO_NUM_PRIMS_WRITTEN counters for each stream (4 uint64_t values)
347 * If prim_count_bo is out of space, gather up the results so far into
348 * prims_generated[] and allocate a new buffer with enough space.
350 * The number of primitives written is used to compute the number of vertices
351 * written to a transform feedback stream, which is required to implement
352 * DrawTransformFeedback().
355 gen7_save_primitives_written_counters(struct brw_context
*brw
,
356 struct brw_transform_feedback_object
*obj
)
358 const int streams
= BRW_MAX_XFB_STREAMS
;
360 /* Check if there's enough space for a new pair of four values. */
361 if (obj
->prim_count_bo
!= NULL
&&
362 obj
->prim_count_buffer_index
+ 2 * streams
>= 4096 / sizeof(uint64_t)) {
363 /* Gather up the results so far and release the BO. */
364 gen7_tally_prims_generated(brw
, obj
);
367 /* Flush any drawing so that the counters have the right values. */
368 brw_emit_mi_flush(brw
);
370 /* Emit MI_STORE_REGISTER_MEM commands to write the values. */
371 for (int i
= 0; i
< streams
; i
++) {
372 brw_store_register_mem64(brw
, obj
->prim_count_bo
,
373 GEN7_SO_NUM_PRIMS_WRITTEN(i
),
374 obj
->prim_count_buffer_index
+ i
);
377 /* Update where to write data to. */
378 obj
->prim_count_buffer_index
+= streams
;
382 * Compute the number of vertices written by this transform feedback operation.
385 brw_compute_xfb_vertices_written(struct brw_context
*brw
,
386 struct brw_transform_feedback_object
*obj
)
388 if (obj
->vertices_written_valid
|| !obj
->base
.EndedAnytime
)
391 unsigned vertices_per_prim
= 0;
393 switch (obj
->primitive_mode
) {
395 vertices_per_prim
= 1;
398 vertices_per_prim
= 2;
401 vertices_per_prim
= 3;
404 unreachable("Invalid transform feedback primitive mode.");
407 /* Get the number of primitives generated. */
408 gen7_tally_prims_generated(brw
, obj
);
410 for (int i
= 0; i
< BRW_MAX_XFB_STREAMS
; i
++) {
411 obj
->vertices_written
[i
] = vertices_per_prim
* obj
->prims_generated
[i
];
413 obj
->vertices_written_valid
= true;
417 * GetTransformFeedbackVertexCount() driver hook.
419 * Returns the number of vertices written to a particular stream by the last
420 * Begin/EndTransformFeedback block. Used to implement DrawTransformFeedback().
423 brw_get_transform_feedback_vertex_count(struct gl_context
*ctx
,
424 struct gl_transform_feedback_object
*obj
,
427 struct brw_context
*brw
= brw_context(ctx
);
428 struct brw_transform_feedback_object
*brw_obj
=
429 (struct brw_transform_feedback_object
*) obj
;
431 assert(obj
->EndedAnytime
);
432 assert(stream
< BRW_MAX_XFB_STREAMS
);
434 brw_compute_xfb_vertices_written(brw
, brw_obj
);
435 return brw_obj
->vertices_written
[stream
];
439 gen7_begin_transform_feedback(struct gl_context
*ctx
, GLenum mode
,
440 struct gl_transform_feedback_object
*obj
)
442 struct brw_context
*brw
= brw_context(ctx
);
443 struct brw_transform_feedback_object
*brw_obj
=
444 (struct brw_transform_feedback_object
*) obj
;
446 /* Reset the SO buffer offsets to 0. */
448 brw_obj
->zero_offsets
= true;
450 intel_batchbuffer_flush(brw
);
451 brw
->batch
.needs_sol_reset
= true;
454 /* We're about to lose the information needed to compute the number of
455 * vertices written during the last Begin/EndTransformFeedback section,
456 * so we can't delay it any further.
458 brw_compute_xfb_vertices_written(brw
, brw_obj
);
460 /* No primitives have been generated yet. */
461 for (int i
= 0; i
< BRW_MAX_XFB_STREAMS
; i
++) {
462 brw_obj
->prims_generated
[i
] = 0;
465 /* Store the starting value of the SO_NUM_PRIMS_WRITTEN counters. */
466 gen7_save_primitives_written_counters(brw
, brw_obj
);
468 brw_obj
->primitive_mode
= mode
;
472 gen7_end_transform_feedback(struct gl_context
*ctx
,
473 struct gl_transform_feedback_object
*obj
)
475 /* After EndTransformFeedback, it's likely that the client program will try
476 * to draw using the contents of the transform feedback buffer as vertex
477 * input. In order for this to work, we need to flush the data through at
478 * least the GS stage of the pipeline, and flush out the render cache. For
479 * simplicity, just do a full flush.
481 struct brw_context
*brw
= brw_context(ctx
);
482 struct brw_transform_feedback_object
*brw_obj
=
483 (struct brw_transform_feedback_object
*) obj
;
485 /* Store the ending value of the SO_NUM_PRIMS_WRITTEN counters. */
486 gen7_save_primitives_written_counters(brw
, brw_obj
);
488 /* EndTransformFeedback() means that we need to update the number of
489 * vertices written. Since it's only necessary if DrawTransformFeedback()
490 * is called and it means mapping a buffer object, we delay computing it
491 * until it's absolutely necessary to try and avoid stalls.
493 brw_obj
->vertices_written_valid
= false;
497 gen7_pause_transform_feedback(struct gl_context
*ctx
,
498 struct gl_transform_feedback_object
*obj
)
500 struct brw_context
*brw
= brw_context(ctx
);
501 struct brw_transform_feedback_object
*brw_obj
=
502 (struct brw_transform_feedback_object
*) obj
;
504 /* Flush any drawing so that the counters have the right values. */
505 brw_emit_mi_flush(brw
);
507 /* Save the SOL buffer offset register values. */
509 for (int i
= 0; i
< 4; i
++) {
511 OUT_BATCH(MI_STORE_REGISTER_MEM
| (3 - 2));
512 OUT_BATCH(GEN7_SO_WRITE_OFFSET(i
));
513 OUT_RELOC(brw_obj
->offset_bo
,
514 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
515 i
* sizeof(uint32_t));
520 /* Store the temporary ending value of the SO_NUM_PRIMS_WRITTEN counters.
521 * While this operation is paused, other transform feedback actions may
522 * occur, which will contribute to the counters. We need to exclude that
525 gen7_save_primitives_written_counters(brw
, brw_obj
);
529 gen7_resume_transform_feedback(struct gl_context
*ctx
,
530 struct gl_transform_feedback_object
*obj
)
532 struct brw_context
*brw
= brw_context(ctx
);
533 struct brw_transform_feedback_object
*brw_obj
=
534 (struct brw_transform_feedback_object
*) obj
;
536 /* Reload the SOL buffer offset registers. */
538 for (int i
= 0; i
< 4; i
++) {
540 OUT_BATCH(GEN7_MI_LOAD_REGISTER_MEM
| (3 - 2));
541 OUT_BATCH(GEN7_SO_WRITE_OFFSET(i
));
542 OUT_RELOC(brw_obj
->offset_bo
,
543 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
544 i
* sizeof(uint32_t));
549 /* Store the new starting value of the SO_NUM_PRIMS_WRITTEN counters. */
550 gen7_save_primitives_written_counters(brw
, brw_obj
);