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
[128];
109 int next_offset
[4] = {0, 0, 0, 0};
112 STATIC_ASSERT(ARRAY_SIZE(so_decl
) >= MAX_PROGRAM_OUTPUTS
);
114 /* Construct the list of SO_DECLs to be emitted. The formatting of the
115 * command is feels strange -- each dword pair contains a SO_DECL per stream.
117 for (int i
= 0; i
< linked_xfb_info
->NumOutputs
; i
++) {
118 int buffer
= linked_xfb_info
->Outputs
[i
].OutputBuffer
;
120 int varying
= linked_xfb_info
->Outputs
[i
].OutputRegister
;
121 const unsigned components
= linked_xfb_info
->Outputs
[i
].NumComponents
;
122 unsigned component_mask
= (1 << components
) - 1;
124 /* gl_PointSize is stored in VARYING_SLOT_PSIZ.w
125 * gl_Layer is stored in VARYING_SLOT_PSIZ.y
127 if (varying
== VARYING_SLOT_PSIZ
) {
128 assert(components
== 1);
129 component_mask
<<= 3;
130 } else if (varying
== VARYING_SLOT_LAYER
) {
131 assert(components
== 1);
132 component_mask
<<= 1;
134 component_mask
<<= linked_xfb_info
->Outputs
[i
].ComponentOffset
;
137 buffer_mask
|= 1 << buffer
;
139 decl
|= buffer
<< SO_DECL_OUTPUT_BUFFER_SLOT_SHIFT
;
140 if (varying
== VARYING_SLOT_LAYER
) {
141 decl
|= vue_map
->varying_to_slot
[VARYING_SLOT_PSIZ
] <<
142 SO_DECL_REGISTER_INDEX_SHIFT
;
144 assert(vue_map
->varying_to_slot
[varying
] >= 0);
145 decl
|= vue_map
->varying_to_slot
[varying
] <<
146 SO_DECL_REGISTER_INDEX_SHIFT
;
148 decl
|= component_mask
<< SO_DECL_COMPONENT_MASK_SHIFT
;
150 /* Mesa doesn't store entries for gl_SkipComponents in the Outputs[]
151 * array. Instead, it simply increments DstOffset for the following
152 * input by the number of components that should be skipped.
154 * Our hardware is unusual in that it requires us to program SO_DECLs
155 * for fake "hole" components, rather than simply taking the offset
156 * for each real varying. Each hole can have size 1, 2, 3, or 4; we
157 * program as many size = 4 holes as we can, then a final hole to
158 * accomodate the final 1, 2, or 3 remaining.
160 int skip_components
=
161 linked_xfb_info
->Outputs
[i
].DstOffset
- next_offset
[buffer
];
163 next_offset
[buffer
] += skip_components
;
165 while (skip_components
>= 4) {
166 so_decl
[decls
++] = SO_DECL_HOLE_FLAG
| 0xf;
167 skip_components
-= 4;
169 if (skip_components
> 0)
170 so_decl
[decls
++] = SO_DECL_HOLE_FLAG
| ((1 << skip_components
) - 1);
172 assert(linked_xfb_info
->Outputs
[i
].DstOffset
== next_offset
[buffer
]);
174 next_offset
[buffer
] += components
;
176 so_decl
[decls
++] = decl
;
179 BEGIN_BATCH(decls
* 2 + 3);
180 OUT_BATCH(_3DSTATE_SO_DECL_LIST
<< 16 | (decls
* 2 + 1));
182 OUT_BATCH((buffer_mask
<< SO_STREAM_TO_BUFFER_SELECTS_0_SHIFT
) |
183 (0 << SO_STREAM_TO_BUFFER_SELECTS_1_SHIFT
) |
184 (0 << SO_STREAM_TO_BUFFER_SELECTS_2_SHIFT
) |
185 (0 << SO_STREAM_TO_BUFFER_SELECTS_3_SHIFT
));
187 OUT_BATCH((decls
<< SO_NUM_ENTRIES_0_SHIFT
) |
188 (0 << SO_NUM_ENTRIES_1_SHIFT
) |
189 (0 << SO_NUM_ENTRIES_2_SHIFT
) |
190 (0 << SO_NUM_ENTRIES_3_SHIFT
));
192 for (int i
= 0; i
< decls
; i
++) {
193 OUT_BATCH(so_decl
[i
]);
201 upload_3dstate_streamout(struct brw_context
*brw
, bool active
,
202 const struct brw_vue_map
*vue_map
)
204 struct gl_context
*ctx
= &brw
->ctx
;
205 /* BRW_NEW_TRANSFORM_FEEDBACK */
206 struct gl_transform_feedback_object
*xfb_obj
=
207 ctx
->TransformFeedback
.CurrentObject
;
208 uint32_t dw1
= 0, dw2
= 0;
212 int urb_entry_read_offset
= 0;
213 int urb_entry_read_length
= (vue_map
->num_slots
+ 1) / 2 -
214 urb_entry_read_offset
;
216 dw1
|= SO_FUNCTION_ENABLE
;
217 dw1
|= SO_STATISTICS_ENABLE
;
220 if (ctx
->Light
.ProvokingVertex
!= GL_FIRST_VERTEX_CONVENTION
)
221 dw1
|= SO_REORDER_TRAILING
;
223 for (i
= 0; i
< 4; i
++) {
224 if (xfb_obj
->Buffers
[i
]) {
225 dw1
|= SO_BUFFER_ENABLE(i
);
229 /* We always read the whole vertex. This could be reduced at some
230 * point by reading less and offsetting the register index in the
233 dw2
|= urb_entry_read_offset
<< SO_STREAM_0_VERTEX_READ_OFFSET_SHIFT
;
234 dw2
|= (urb_entry_read_length
- 1) <<
235 SO_STREAM_0_VERTEX_READ_LENGTH_SHIFT
;
239 OUT_BATCH(_3DSTATE_STREAMOUT
<< 16 | (3 - 2));
246 upload_sol_state(struct brw_context
*brw
)
248 struct gl_context
*ctx
= &brw
->ctx
;
249 /* BRW_NEW_TRANSFORM_FEEDBACK */
250 bool active
= _mesa_is_xfb_active_and_unpaused(ctx
);
253 upload_3dstate_so_buffers(brw
);
254 /* BRW_NEW_VUE_MAP_GEOM_OUT */
255 gen7_upload_3dstate_so_decl_list(brw
, &brw
->vue_map_geom_out
);
258 /* Finally, set up the SOL stage. This command must always follow updates to
259 * the nonpipelined SOL state (3DSTATE_SO_BUFFER, 3DSTATE_SO_DECL_LIST) or
260 * MMIO register updates (current performed by the kernel at each batch
263 upload_3dstate_streamout(brw
, active
, &brw
->vue_map_geom_out
);
266 const struct brw_tracked_state gen7_sol_state
= {
268 .mesa
= (_NEW_LIGHT
),
269 .brw
= (BRW_NEW_BATCH
|
270 BRW_NEW_VUE_MAP_GEOM_OUT
|
271 BRW_NEW_TRANSFORM_FEEDBACK
)
273 .emit
= upload_sol_state
,
277 * Tally the number of primitives generated so far.
279 * The buffer contains a series of pairs:
280 * (<start0, start1, start2, start3>, <end0, end1, end2, end3>) ;
281 * (<start0, start1, start2, start3>, <end0, end1, end2, end3>) ;
283 * For each stream, we subtract the pair of values (end - start) to get the
284 * number of primitives generated during one section. We accumulate these
285 * values, adding them up to get the total number of primitives generated.
288 gen7_tally_prims_generated(struct brw_context
*brw
,
289 struct brw_transform_feedback_object
*obj
)
291 /* If the current batch is still contributing to the number of primitives
292 * generated, flush it now so the results will be present when mapped.
294 if (drm_intel_bo_references(brw
->batch
.bo
, obj
->prim_count_bo
))
295 intel_batchbuffer_flush(brw
);
297 if (unlikely(brw
->perf_debug
&& drm_intel_bo_busy(obj
->prim_count_bo
)))
298 perf_debug("Stalling for # of transform feedback primitives written.\n");
300 drm_intel_bo_map(obj
->prim_count_bo
, false);
301 uint64_t *prim_counts
= obj
->prim_count_bo
->virtual;
303 assert(obj
->prim_count_buffer_index
% (2 * BRW_MAX_XFB_STREAMS
) == 0);
304 int pairs
= obj
->prim_count_buffer_index
/ (2 * BRW_MAX_XFB_STREAMS
);
306 for (int i
= 0; i
< pairs
; i
++) {
307 for (int s
= 0; s
< BRW_MAX_XFB_STREAMS
; s
++) {
308 obj
->prims_generated
[s
] +=
309 prim_counts
[BRW_MAX_XFB_STREAMS
+ s
] - prim_counts
[s
];
311 prim_counts
+= 2 * BRW_MAX_XFB_STREAMS
; /* move to the next pair */
314 drm_intel_bo_unmap(obj
->prim_count_bo
);
316 /* We've already gathered up the old data; we can safely overwrite it now. */
317 obj
->prim_count_buffer_index
= 0;
321 * Store the SO_NUM_PRIMS_WRITTEN counters for each stream (4 uint64_t values)
324 * If prim_count_bo is out of space, gather up the results so far into
325 * prims_generated[] and allocate a new buffer with enough space.
327 * The number of primitives written is used to compute the number of vertices
328 * written to a transform feedback stream, which is required to implement
329 * DrawTransformFeedback().
332 gen7_save_primitives_written_counters(struct brw_context
*brw
,
333 struct brw_transform_feedback_object
*obj
)
335 const int streams
= BRW_MAX_XFB_STREAMS
;
337 /* Check if there's enough space for a new pair of four values. */
338 if (obj
->prim_count_bo
!= NULL
&&
339 obj
->prim_count_buffer_index
+ 2 * streams
>= 4096 / sizeof(uint64_t)) {
340 /* Gather up the results so far and release the BO. */
341 gen7_tally_prims_generated(brw
, obj
);
344 /* Flush any drawing so that the counters have the right values. */
345 intel_batchbuffer_emit_mi_flush(brw
);
347 /* Emit MI_STORE_REGISTER_MEM commands to write the values. */
348 for (int i
= 0; i
< streams
; i
++) {
349 brw_store_register_mem64(brw
, obj
->prim_count_bo
,
350 GEN7_SO_NUM_PRIMS_WRITTEN(i
),
351 obj
->prim_count_buffer_index
+ i
);
354 /* Update where to write data to. */
355 obj
->prim_count_buffer_index
+= streams
;
359 * Compute the number of vertices written by this transform feedback operation.
362 brw_compute_xfb_vertices_written(struct brw_context
*brw
,
363 struct brw_transform_feedback_object
*obj
)
365 if (obj
->vertices_written_valid
|| !obj
->base
.EndedAnytime
)
368 unsigned vertices_per_prim
= 0;
370 switch (obj
->primitive_mode
) {
372 vertices_per_prim
= 1;
375 vertices_per_prim
= 2;
378 vertices_per_prim
= 3;
381 assert(!"Invalid transform feedback primitive mode.");
384 /* Get the number of primitives generated. */
385 gen7_tally_prims_generated(brw
, obj
);
387 for (int i
= 0; i
< BRW_MAX_XFB_STREAMS
; i
++) {
388 obj
->vertices_written
[i
] = vertices_per_prim
* obj
->prims_generated
[i
];
390 obj
->vertices_written_valid
= true;
394 * GetTransformFeedbackVertexCount() driver hook.
396 * Returns the number of vertices written to a particular stream by the last
397 * Begin/EndTransformFeedback block. Used to implement DrawTransformFeedback().
400 brw_get_transform_feedback_vertex_count(struct gl_context
*ctx
,
401 struct gl_transform_feedback_object
*obj
,
404 struct brw_context
*brw
= brw_context(ctx
);
405 struct brw_transform_feedback_object
*brw_obj
=
406 (struct brw_transform_feedback_object
*) obj
;
408 assert(obj
->EndedAnytime
);
409 assert(stream
< BRW_MAX_XFB_STREAMS
);
411 brw_compute_xfb_vertices_written(brw
, brw_obj
);
412 return brw_obj
->vertices_written
[stream
];
416 gen7_begin_transform_feedback(struct gl_context
*ctx
, GLenum mode
,
417 struct gl_transform_feedback_object
*obj
)
419 struct brw_context
*brw
= brw_context(ctx
);
420 struct brw_transform_feedback_object
*brw_obj
=
421 (struct brw_transform_feedback_object
*) obj
;
423 /* Reset the SO buffer offsets to 0. */
425 brw_obj
->zero_offsets
= true;
427 intel_batchbuffer_flush(brw
);
428 brw
->batch
.needs_sol_reset
= true;
431 /* We're about to lose the information needed to compute the number of
432 * vertices written during the last Begin/EndTransformFeedback section,
433 * so we can't delay it any further.
435 brw_compute_xfb_vertices_written(brw
, brw_obj
);
437 /* No primitives have been generated yet. */
438 for (int i
= 0; i
< BRW_MAX_XFB_STREAMS
; i
++) {
439 brw_obj
->prims_generated
[i
] = 0;
442 /* Store the starting value of the SO_NUM_PRIMS_WRITTEN counters. */
443 gen7_save_primitives_written_counters(brw
, brw_obj
);
445 brw_obj
->primitive_mode
= mode
;
449 gen7_end_transform_feedback(struct gl_context
*ctx
,
450 struct gl_transform_feedback_object
*obj
)
452 /* After EndTransformFeedback, it's likely that the client program will try
453 * to draw using the contents of the transform feedback buffer as vertex
454 * input. In order for this to work, we need to flush the data through at
455 * least the GS stage of the pipeline, and flush out the render cache. For
456 * simplicity, just do a full flush.
458 struct brw_context
*brw
= brw_context(ctx
);
459 struct brw_transform_feedback_object
*brw_obj
=
460 (struct brw_transform_feedback_object
*) obj
;
462 /* Store the ending value of the SO_NUM_PRIMS_WRITTEN counters. */
463 gen7_save_primitives_written_counters(brw
, brw_obj
);
465 /* EndTransformFeedback() means that we need to update the number of
466 * vertices written. Since it's only necessary if DrawTransformFeedback()
467 * is called and it means mapping a buffer object, we delay computing it
468 * until it's absolutely necessary to try and avoid stalls.
470 brw_obj
->vertices_written_valid
= false;
474 gen7_pause_transform_feedback(struct gl_context
*ctx
,
475 struct gl_transform_feedback_object
*obj
)
477 struct brw_context
*brw
= brw_context(ctx
);
478 struct brw_transform_feedback_object
*brw_obj
=
479 (struct brw_transform_feedback_object
*) obj
;
481 /* Flush any drawing so that the counters have the right values. */
482 intel_batchbuffer_emit_mi_flush(brw
);
484 /* Save the SOL buffer offset register values. */
486 for (int i
= 0; i
< 4; i
++) {
488 OUT_BATCH(MI_STORE_REGISTER_MEM
| (3 - 2));
489 OUT_BATCH(GEN7_SO_WRITE_OFFSET(i
));
490 OUT_RELOC(brw_obj
->offset_bo
,
491 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
492 i
* sizeof(uint32_t));
497 /* Store the temporary ending value of the SO_NUM_PRIMS_WRITTEN counters.
498 * While this operation is paused, other transform feedback actions may
499 * occur, which will contribute to the counters. We need to exclude that
502 gen7_save_primitives_written_counters(brw
, brw_obj
);
506 gen7_resume_transform_feedback(struct gl_context
*ctx
,
507 struct gl_transform_feedback_object
*obj
)
509 struct brw_context
*brw
= brw_context(ctx
);
510 struct brw_transform_feedback_object
*brw_obj
=
511 (struct brw_transform_feedback_object
*) obj
;
513 /* Reload the SOL buffer offset registers. */
515 for (int i
= 0; i
< 4; i
++) {
517 OUT_BATCH(GEN7_MI_LOAD_REGISTER_MEM
| (3 - 2));
518 OUT_BATCH(GEN7_SO_WRITE_OFFSET(i
));
519 OUT_RELOC(brw_obj
->offset_bo
,
520 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
521 i
* sizeof(uint32_t));
526 /* Store the new starting value of the SO_NUM_PRIMS_WRITTEN counters. */
527 gen7_save_primitives_written_counters(brw
, brw_obj
);