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 if (varying
== VARYING_SLOT_PSIZ
) {
126 assert(components
== 1);
127 component_mask
<<= 3;
129 component_mask
<<= linked_xfb_info
->Outputs
[i
].ComponentOffset
;
132 buffer_mask
|= 1 << buffer
;
134 decl
|= buffer
<< SO_DECL_OUTPUT_BUFFER_SLOT_SHIFT
;
135 decl
|= vue_map
->varying_to_slot
[varying
] <<
136 SO_DECL_REGISTER_INDEX_SHIFT
;
137 decl
|= component_mask
<< SO_DECL_COMPONENT_MASK_SHIFT
;
139 /* Mesa doesn't store entries for gl_SkipComponents in the Outputs[]
140 * array. Instead, it simply increments DstOffset for the following
141 * input by the number of components that should be skipped.
143 * Our hardware is unusual in that it requires us to program SO_DECLs
144 * for fake "hole" components, rather than simply taking the offset
145 * for each real varying. Each hole can have size 1, 2, 3, or 4; we
146 * program as many size = 4 holes as we can, then a final hole to
147 * accomodate the final 1, 2, or 3 remaining.
149 int skip_components
=
150 linked_xfb_info
->Outputs
[i
].DstOffset
- next_offset
[buffer
];
152 next_offset
[buffer
] += skip_components
;
154 while (skip_components
>= 4) {
155 so_decl
[decls
++] = SO_DECL_HOLE_FLAG
| 0xf;
156 skip_components
-= 4;
158 if (skip_components
> 0)
159 so_decl
[decls
++] = SO_DECL_HOLE_FLAG
| ((1 << skip_components
) - 1);
161 assert(linked_xfb_info
->Outputs
[i
].DstOffset
== next_offset
[buffer
]);
163 next_offset
[buffer
] += components
;
165 so_decl
[decls
++] = decl
;
168 BEGIN_BATCH(decls
* 2 + 3);
169 OUT_BATCH(_3DSTATE_SO_DECL_LIST
<< 16 | (decls
* 2 + 1));
171 OUT_BATCH((buffer_mask
<< SO_STREAM_TO_BUFFER_SELECTS_0_SHIFT
) |
172 (0 << SO_STREAM_TO_BUFFER_SELECTS_1_SHIFT
) |
173 (0 << SO_STREAM_TO_BUFFER_SELECTS_2_SHIFT
) |
174 (0 << SO_STREAM_TO_BUFFER_SELECTS_3_SHIFT
));
176 OUT_BATCH((decls
<< SO_NUM_ENTRIES_0_SHIFT
) |
177 (0 << SO_NUM_ENTRIES_1_SHIFT
) |
178 (0 << SO_NUM_ENTRIES_2_SHIFT
) |
179 (0 << SO_NUM_ENTRIES_3_SHIFT
));
181 for (int i
= 0; i
< decls
; i
++) {
182 OUT_BATCH(so_decl
[i
]);
190 upload_3dstate_streamout(struct brw_context
*brw
, bool active
,
191 const struct brw_vue_map
*vue_map
)
193 struct gl_context
*ctx
= &brw
->ctx
;
194 /* BRW_NEW_TRANSFORM_FEEDBACK */
195 struct gl_transform_feedback_object
*xfb_obj
=
196 ctx
->TransformFeedback
.CurrentObject
;
197 uint32_t dw1
= 0, dw2
= 0;
201 int urb_entry_read_offset
= 0;
202 int urb_entry_read_length
= (vue_map
->num_slots
+ 1) / 2 -
203 urb_entry_read_offset
;
205 dw1
|= SO_FUNCTION_ENABLE
;
206 dw1
|= SO_STATISTICS_ENABLE
;
209 if (ctx
->Light
.ProvokingVertex
!= GL_FIRST_VERTEX_CONVENTION
)
210 dw1
|= SO_REORDER_TRAILING
;
212 for (i
= 0; i
< 4; i
++) {
213 if (xfb_obj
->Buffers
[i
]) {
214 dw1
|= SO_BUFFER_ENABLE(i
);
218 /* We always read the whole vertex. This could be reduced at some
219 * point by reading less and offsetting the register index in the
222 dw2
|= urb_entry_read_offset
<< SO_STREAM_0_VERTEX_READ_OFFSET_SHIFT
;
223 dw2
|= (urb_entry_read_length
- 1) <<
224 SO_STREAM_0_VERTEX_READ_LENGTH_SHIFT
;
228 OUT_BATCH(_3DSTATE_STREAMOUT
<< 16 | (3 - 2));
235 upload_sol_state(struct brw_context
*brw
)
237 struct gl_context
*ctx
= &brw
->ctx
;
238 /* BRW_NEW_TRANSFORM_FEEDBACK */
239 bool active
= _mesa_is_xfb_active_and_unpaused(ctx
);
242 upload_3dstate_so_buffers(brw
);
243 /* BRW_NEW_VUE_MAP_GEOM_OUT */
244 gen7_upload_3dstate_so_decl_list(brw
, &brw
->vue_map_geom_out
);
247 /* Finally, set up the SOL stage. This command must always follow updates to
248 * the nonpipelined SOL state (3DSTATE_SO_BUFFER, 3DSTATE_SO_DECL_LIST) or
249 * MMIO register updates (current performed by the kernel at each batch
252 upload_3dstate_streamout(brw
, active
, &brw
->vue_map_geom_out
);
255 const struct brw_tracked_state gen7_sol_state
= {
257 .mesa
= (_NEW_LIGHT
),
258 .brw
= (BRW_NEW_BATCH
|
259 BRW_NEW_VUE_MAP_GEOM_OUT
|
260 BRW_NEW_TRANSFORM_FEEDBACK
)
262 .emit
= upload_sol_state
,
266 * Tally the number of primitives generated so far.
268 * The buffer contains a series of pairs:
269 * (<start0, start1, start2, start3>, <end0, end1, end2, end3>) ;
270 * (<start0, start1, start2, start3>, <end0, end1, end2, end3>) ;
272 * For each stream, we subtract the pair of values (end - start) to get the
273 * number of primitives generated during one section. We accumulate these
274 * values, adding them up to get the total number of primitives generated.
277 gen7_tally_prims_generated(struct brw_context
*brw
,
278 struct brw_transform_feedback_object
*obj
)
280 /* If the current batch is still contributing to the number of primitives
281 * generated, flush it now so the results will be present when mapped.
283 if (drm_intel_bo_references(brw
->batch
.bo
, obj
->prim_count_bo
))
284 intel_batchbuffer_flush(brw
);
286 if (unlikely(brw
->perf_debug
&& drm_intel_bo_busy(obj
->prim_count_bo
)))
287 perf_debug("Stalling for # of transform feedback primitives written.\n");
289 drm_intel_bo_map(obj
->prim_count_bo
, false);
290 uint64_t *prim_counts
= obj
->prim_count_bo
->virtual;
292 assert(obj
->prim_count_buffer_index
% (2 * BRW_MAX_XFB_STREAMS
) == 0);
293 int pairs
= obj
->prim_count_buffer_index
/ (2 * BRW_MAX_XFB_STREAMS
);
295 for (int i
= 0; i
< pairs
; i
++) {
296 for (int s
= 0; s
< BRW_MAX_XFB_STREAMS
; s
++) {
297 obj
->prims_generated
[s
] +=
298 prim_counts
[BRW_MAX_XFB_STREAMS
+ s
] - prim_counts
[s
];
300 prim_counts
+= 2 * BRW_MAX_XFB_STREAMS
; /* move to the next pair */
303 drm_intel_bo_unmap(obj
->prim_count_bo
);
305 /* We've already gathered up the old data; we can safely overwrite it now. */
306 obj
->prim_count_buffer_index
= 0;
310 * Store the SO_NUM_PRIMS_WRITTEN counters for each stream (4 uint64_t values)
313 * If prim_count_bo is out of space, gather up the results so far into
314 * prims_generated[] and allocate a new buffer with enough space.
316 * The number of primitives written is used to compute the number of vertices
317 * written to a transform feedback stream, which is required to implement
318 * DrawTransformFeedback().
321 gen7_save_primitives_written_counters(struct brw_context
*brw
,
322 struct brw_transform_feedback_object
*obj
)
324 const int streams
= BRW_MAX_XFB_STREAMS
;
326 /* Check if there's enough space for a new pair of four values. */
327 if (obj
->prim_count_bo
!= NULL
&&
328 obj
->prim_count_buffer_index
+ 2 * streams
>= 4096 / sizeof(uint64_t)) {
329 /* Gather up the results so far and release the BO. */
330 gen7_tally_prims_generated(brw
, obj
);
333 /* Flush any drawing so that the counters have the right values. */
334 intel_batchbuffer_emit_mi_flush(brw
);
336 /* Emit MI_STORE_REGISTER_MEM commands to write the values. */
337 for (int i
= 0; i
< streams
; i
++) {
338 brw_store_register_mem64(brw
, obj
->prim_count_bo
,
339 GEN7_SO_NUM_PRIMS_WRITTEN(i
),
340 obj
->prim_count_buffer_index
+ i
);
343 /* Update where to write data to. */
344 obj
->prim_count_buffer_index
+= streams
;
348 * Compute the number of vertices written by this transform feedback operation.
351 brw_compute_xfb_vertices_written(struct brw_context
*brw
,
352 struct brw_transform_feedback_object
*obj
)
354 if (obj
->vertices_written_valid
|| !obj
->base
.EndedAnytime
)
357 unsigned vertices_per_prim
= 0;
359 switch (obj
->primitive_mode
) {
361 vertices_per_prim
= 1;
364 vertices_per_prim
= 2;
367 vertices_per_prim
= 3;
370 assert(!"Invalid transform feedback primitive mode.");
373 /* Get the number of primitives generated. */
374 gen7_tally_prims_generated(brw
, obj
);
376 for (int i
= 0; i
< BRW_MAX_XFB_STREAMS
; i
++) {
377 obj
->vertices_written
[i
] = vertices_per_prim
* obj
->prims_generated
[i
];
379 obj
->vertices_written_valid
= true;
383 * GetTransformFeedbackVertexCount() driver hook.
385 * Returns the number of vertices written to a particular stream by the last
386 * Begin/EndTransformFeedback block. Used to implement DrawTransformFeedback().
389 brw_get_transform_feedback_vertex_count(struct gl_context
*ctx
,
390 struct gl_transform_feedback_object
*obj
,
393 struct brw_context
*brw
= brw_context(ctx
);
394 struct brw_transform_feedback_object
*brw_obj
=
395 (struct brw_transform_feedback_object
*) obj
;
397 assert(obj
->EndedAnytime
);
398 assert(stream
< BRW_MAX_XFB_STREAMS
);
400 brw_compute_xfb_vertices_written(brw
, brw_obj
);
401 return brw_obj
->vertices_written
[stream
];
405 gen7_begin_transform_feedback(struct gl_context
*ctx
, GLenum mode
,
406 struct gl_transform_feedback_object
*obj
)
408 struct brw_context
*brw
= brw_context(ctx
);
409 struct brw_transform_feedback_object
*brw_obj
=
410 (struct brw_transform_feedback_object
*) obj
;
412 /* Reset the SO buffer offsets to 0. */
414 brw_obj
->zero_offsets
= true;
416 intel_batchbuffer_flush(brw
);
417 brw
->batch
.needs_sol_reset
= true;
420 /* We're about to lose the information needed to compute the number of
421 * vertices written during the last Begin/EndTransformFeedback section,
422 * so we can't delay it any further.
424 brw_compute_xfb_vertices_written(brw
, brw_obj
);
426 /* No primitives have been generated yet. */
427 for (int i
= 0; i
< BRW_MAX_XFB_STREAMS
; i
++) {
428 brw_obj
->prims_generated
[i
] = 0;
431 /* Store the starting value of the SO_NUM_PRIMS_WRITTEN counters. */
432 gen7_save_primitives_written_counters(brw
, brw_obj
);
434 brw_obj
->primitive_mode
= mode
;
438 gen7_end_transform_feedback(struct gl_context
*ctx
,
439 struct gl_transform_feedback_object
*obj
)
441 /* After EndTransformFeedback, it's likely that the client program will try
442 * to draw using the contents of the transform feedback buffer as vertex
443 * input. In order for this to work, we need to flush the data through at
444 * least the GS stage of the pipeline, and flush out the render cache. For
445 * simplicity, just do a full flush.
447 struct brw_context
*brw
= brw_context(ctx
);
448 struct brw_transform_feedback_object
*brw_obj
=
449 (struct brw_transform_feedback_object
*) obj
;
451 /* Store the ending value of the SO_NUM_PRIMS_WRITTEN counters. */
452 gen7_save_primitives_written_counters(brw
, brw_obj
);
454 /* EndTransformFeedback() means that we need to update the number of
455 * vertices written. Since it's only necessary if DrawTransformFeedback()
456 * is called and it means mapping a buffer object, we delay computing it
457 * until it's absolutely necessary to try and avoid stalls.
459 brw_obj
->vertices_written_valid
= false;
463 gen7_pause_transform_feedback(struct gl_context
*ctx
,
464 struct gl_transform_feedback_object
*obj
)
466 struct brw_context
*brw
= brw_context(ctx
);
467 struct brw_transform_feedback_object
*brw_obj
=
468 (struct brw_transform_feedback_object
*) obj
;
470 /* Flush any drawing so that the counters have the right values. */
471 intel_batchbuffer_emit_mi_flush(brw
);
473 /* Save the SOL buffer offset register values. */
475 for (int i
= 0; i
< 4; i
++) {
477 OUT_BATCH(MI_STORE_REGISTER_MEM
| (3 - 2));
478 OUT_BATCH(GEN7_SO_WRITE_OFFSET(i
));
479 OUT_RELOC(brw_obj
->offset_bo
,
480 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
481 i
* sizeof(uint32_t));
486 /* Store the temporary ending value of the SO_NUM_PRIMS_WRITTEN counters.
487 * While this operation is paused, other transform feedback actions may
488 * occur, which will contribute to the counters. We need to exclude that
491 gen7_save_primitives_written_counters(brw
, brw_obj
);
495 gen7_resume_transform_feedback(struct gl_context
*ctx
,
496 struct gl_transform_feedback_object
*obj
)
498 struct brw_context
*brw
= brw_context(ctx
);
499 struct brw_transform_feedback_object
*brw_obj
=
500 (struct brw_transform_feedback_object
*) obj
;
502 /* Reload the SOL buffer offset registers. */
504 for (int i
= 0; i
< 4; i
++) {
506 OUT_BATCH(GEN7_MI_LOAD_REGISTER_MEM
| (3 - 2));
507 OUT_BATCH(GEN7_SO_WRITE_OFFSET(i
));
508 OUT_RELOC(brw_obj
->offset_bo
,
509 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
510 i
* sizeof(uint32_t));
515 /* Store the new starting value of the SO_NUM_PRIMS_WRITTEN counters. */
516 gen7_save_primitives_written_counters(brw
, brw_obj
);