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
26 * Code to initialize the binding table entries used by transform feedback.
29 #include "main/bufferobj.h"
30 #include "main/macros.h"
31 #include "brw_context.h"
32 #include "intel_batchbuffer.h"
33 #include "brw_defines.h"
34 #include "brw_state.h"
35 #include "main/transformfeedback.h"
38 gen6_update_sol_surfaces(struct brw_context
*brw
)
40 struct gl_context
*ctx
= &brw
->ctx
;
41 bool xfb_active
= _mesa_is_xfb_active_and_unpaused(ctx
);
42 struct gl_transform_feedback_object
*xfb_obj
;
43 const struct gl_transform_feedback_info
*linked_xfb_info
= NULL
;
46 /* BRW_NEW_TRANSFORM_FEEDBACK */
47 xfb_obj
= ctx
->TransformFeedback
.CurrentObject
;
48 linked_xfb_info
= xfb_obj
->program
->sh
.LinkedTransformFeedback
;
51 for (int i
= 0; i
< BRW_MAX_SOL_BINDINGS
; ++i
) {
52 const int surf_index
= SURF_INDEX_GEN6_SOL_BINDING(i
);
53 if (xfb_active
&& i
< linked_xfb_info
->NumOutputs
) {
54 unsigned buffer
= linked_xfb_info
->Outputs
[i
].OutputBuffer
;
55 unsigned buffer_offset
=
56 xfb_obj
->Offset
[buffer
] / 4 +
57 linked_xfb_info
->Outputs
[i
].DstOffset
;
58 if (brw
->geometry_program
) {
59 brw_update_sol_surface(
60 brw
, xfb_obj
->Buffers
[buffer
],
61 &brw
->gs
.base
.surf_offset
[surf_index
],
62 linked_xfb_info
->Outputs
[i
].NumComponents
,
63 linked_xfb_info
->Buffers
[buffer
].Stride
, buffer_offset
);
65 brw_update_sol_surface(
66 brw
, xfb_obj
->Buffers
[buffer
],
67 &brw
->ff_gs
.surf_offset
[surf_index
],
68 linked_xfb_info
->Outputs
[i
].NumComponents
,
69 linked_xfb_info
->Buffers
[buffer
].Stride
, buffer_offset
);
72 if (!brw
->geometry_program
)
73 brw
->ff_gs
.surf_offset
[surf_index
] = 0;
75 brw
->gs
.base
.surf_offset
[surf_index
] = 0;
79 brw
->ctx
.NewDriverState
|= BRW_NEW_SURFACES
;
82 const struct brw_tracked_state gen6_sol_surface
= {
85 .brw
= BRW_NEW_BATCH
|
87 BRW_NEW_TRANSFORM_FEEDBACK
,
89 .emit
= gen6_update_sol_surfaces
,
93 * Constructs the binding table for the WM surface state, which maps unit
94 * numbers to surface state objects.
97 brw_gs_upload_binding_table(struct brw_context
*brw
)
100 struct gl_context
*ctx
= &brw
->ctx
;
101 const struct gl_program
*prog
;
102 bool need_binding_table
= false;
104 /* We have two scenarios here:
105 * 1) We are using a geometry shader only to implement transform feedback
106 * for a vertex shader (brw->geometry_program == NULL). In this case, we
107 * only need surfaces for transform feedback in the GS stage.
108 * 2) We have a user-provided geometry shader. In this case we may need
109 * surfaces for transform feedback and/or other stuff, like textures,
113 if (!brw
->geometry_program
) {
114 /* BRW_NEW_VERTEX_PROGRAM */
115 prog
= ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
];
117 /* Skip making a binding table if we don't have anything to put in it */
118 const struct gl_transform_feedback_info
*linked_xfb_info
=
119 prog
->sh
.LinkedTransformFeedback
;
120 need_binding_table
= linked_xfb_info
->NumOutputs
> 0;
122 if (!need_binding_table
) {
123 if (brw
->ff_gs
.bind_bo_offset
!= 0) {
124 brw
->ctx
.NewDriverState
|= BRW_NEW_BINDING_TABLE_POINTERS
;
125 brw
->ff_gs
.bind_bo_offset
= 0;
130 /* Might want to calculate nr_surfaces first, to avoid taking up so much
131 * space for the binding table. Anyway, in this case we know that we only
132 * use BRW_MAX_SOL_BINDINGS surfaces at most.
134 bind
= brw_state_batch(brw
, AUB_TRACE_BINDING_TABLE
,
135 sizeof(uint32_t) * BRW_MAX_SOL_BINDINGS
,
136 32, &brw
->ff_gs
.bind_bo_offset
);
138 /* BRW_NEW_SURFACES */
139 memcpy(bind
, brw
->ff_gs
.surf_offset
,
140 BRW_MAX_SOL_BINDINGS
* sizeof(uint32_t));
142 /* BRW_NEW_GEOMETRY_PROGRAM */
143 prog
= ctx
->_Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
];
145 /* Skip making a binding table if we don't have anything to put in it */
146 struct brw_stage_prog_data
*prog_data
= brw
->gs
.base
.prog_data
;
147 const struct gl_transform_feedback_info
*linked_xfb_info
=
148 prog
->sh
.LinkedTransformFeedback
;
149 need_binding_table
= linked_xfb_info
->NumOutputs
> 0 ||
150 prog_data
->binding_table
.size_bytes
> 0;
152 if (!need_binding_table
) {
153 if (brw
->gs
.base
.bind_bo_offset
!= 0) {
154 brw
->gs
.base
.bind_bo_offset
= 0;
155 brw
->ctx
.NewDriverState
|= BRW_NEW_BINDING_TABLE_POINTERS
;
160 /* Might want to calculate nr_surfaces first, to avoid taking up so much
161 * space for the binding table.
163 bind
= brw_state_batch(brw
, AUB_TRACE_BINDING_TABLE
,
164 sizeof(uint32_t) * BRW_MAX_SURFACES
,
165 32, &brw
->gs
.base
.bind_bo_offset
);
167 /* BRW_NEW_SURFACES */
168 memcpy(bind
, brw
->gs
.base
.surf_offset
,
169 BRW_MAX_SURFACES
* sizeof(uint32_t));
172 brw
->ctx
.NewDriverState
|= BRW_NEW_BINDING_TABLE_POINTERS
;
175 const struct brw_tracked_state gen6_gs_binding_table
= {
178 .brw
= BRW_NEW_BATCH
|
180 BRW_NEW_GEOMETRY_PROGRAM
|
181 BRW_NEW_VERTEX_PROGRAM
|
184 .emit
= brw_gs_upload_binding_table
,
187 struct gl_transform_feedback_object
*
188 brw_new_transform_feedback(struct gl_context
*ctx
, GLuint name
)
190 struct brw_context
*brw
= brw_context(ctx
);
191 struct brw_transform_feedback_object
*brw_obj
=
192 CALLOC_STRUCT(brw_transform_feedback_object
);
196 _mesa_init_transform_feedback_object(&brw_obj
->base
, name
);
199 drm_intel_bo_alloc(brw
->bufmgr
, "transform feedback offsets", 16, 64);
200 brw_obj
->prim_count_bo
=
201 drm_intel_bo_alloc(brw
->bufmgr
, "xfb primitive counts", 4096, 64);
203 return &brw_obj
->base
;
207 brw_delete_transform_feedback(struct gl_context
*ctx
,
208 struct gl_transform_feedback_object
*obj
)
210 struct brw_transform_feedback_object
*brw_obj
=
211 (struct brw_transform_feedback_object
*) obj
;
213 for (unsigned i
= 0; i
< ARRAY_SIZE(obj
->Buffers
); i
++) {
214 _mesa_reference_buffer_object(ctx
, &obj
->Buffers
[i
], NULL
);
217 drm_intel_bo_unreference(brw_obj
->offset_bo
);
218 drm_intel_bo_unreference(brw_obj
->prim_count_bo
);
224 * Tally the number of primitives generated so far.
226 * The buffer contains a series of pairs:
227 * (<start0, start1, start2, start3>, <end0, end1, end2, end3>) ;
228 * (<start0, start1, start2, start3>, <end0, end1, end2, end3>) ;
230 * For each stream, we subtract the pair of values (end - start) to get the
231 * number of primitives generated during one section. We accumulate these
232 * values, adding them up to get the total number of primitives generated.
234 * Note that we expose one stream pre-Gen7, so the above is just (start, end).
237 tally_prims_generated(struct brw_context
*brw
,
238 struct brw_transform_feedback_object
*obj
)
240 const struct gl_context
*ctx
= &brw
->ctx
;
241 const int streams
= ctx
->Const
.MaxVertexStreams
;
243 /* If the current batch is still contributing to the number of primitives
244 * generated, flush it now so the results will be present when mapped.
246 if (drm_intel_bo_references(brw
->batch
.bo
, obj
->prim_count_bo
))
247 intel_batchbuffer_flush(brw
);
249 if (unlikely(brw
->perf_debug
&& drm_intel_bo_busy(obj
->prim_count_bo
)))
250 perf_debug("Stalling for # of transform feedback primitives written.\n");
252 drm_intel_bo_map(obj
->prim_count_bo
, false);
253 uint64_t *prim_counts
= obj
->prim_count_bo
->virtual;
255 assert(obj
->prim_count_buffer_index
% (2 * streams
) == 0);
256 int pairs
= obj
->prim_count_buffer_index
/ (2 * streams
);
258 for (int i
= 0; i
< pairs
; i
++) {
259 for (int s
= 0; s
< streams
; s
++) {
260 obj
->prims_generated
[s
] += prim_counts
[streams
+ s
] - prim_counts
[s
];
262 prim_counts
+= 2 * streams
; /* move to the next pair */
265 drm_intel_bo_unmap(obj
->prim_count_bo
);
267 /* We've already gathered up the old data; we can safely overwrite it now. */
268 obj
->prim_count_buffer_index
= 0;
272 * Store the SO_NUM_PRIMS_WRITTEN counters for each stream (4 uint64_t values)
275 * If prim_count_bo is out of space, gather up the results so far into
276 * prims_generated[] and allocate a new buffer with enough space.
278 * The number of primitives written is used to compute the number of vertices
279 * written to a transform feedback stream, which is required to implement
280 * DrawTransformFeedback().
283 brw_save_primitives_written_counters(struct brw_context
*brw
,
284 struct brw_transform_feedback_object
*obj
)
286 const struct gl_context
*ctx
= &brw
->ctx
;
287 const int streams
= ctx
->Const
.MaxVertexStreams
;
289 /* Check if there's enough space for a new pair of four values. */
290 if (obj
->prim_count_bo
!= NULL
&&
291 obj
->prim_count_buffer_index
+ 2 * streams
>= 4096 / sizeof(uint64_t)) {
292 /* Gather up the results so far and release the BO. */
293 tally_prims_generated(brw
, obj
);
296 /* Flush any drawing so that the counters have the right values. */
297 brw_emit_mi_flush(brw
);
299 /* Emit MI_STORE_REGISTER_MEM commands to write the values. */
301 for (int i
= 0; i
< streams
; i
++) {
302 int offset
= (obj
->prim_count_buffer_index
+ i
) * sizeof(uint64_t);
303 brw_store_register_mem64(brw
, obj
->prim_count_bo
,
304 GEN7_SO_NUM_PRIMS_WRITTEN(i
),
308 brw_store_register_mem64(brw
, obj
->prim_count_bo
,
309 GEN6_SO_NUM_PRIMS_WRITTEN
,
310 obj
->prim_count_buffer_index
* sizeof(uint64_t));
313 /* Update where to write data to. */
314 obj
->prim_count_buffer_index
+= streams
;
318 * Compute the number of vertices written by this transform feedback operation.
321 brw_compute_xfb_vertices_written(struct brw_context
*brw
,
322 struct brw_transform_feedback_object
*obj
)
324 const struct gl_context
*ctx
= &brw
->ctx
;
326 if (obj
->vertices_written_valid
|| !obj
->base
.EndedAnytime
)
329 unsigned vertices_per_prim
= 0;
331 switch (obj
->primitive_mode
) {
333 vertices_per_prim
= 1;
336 vertices_per_prim
= 2;
339 vertices_per_prim
= 3;
342 unreachable("Invalid transform feedback primitive mode.");
345 /* Get the number of primitives generated. */
346 tally_prims_generated(brw
, obj
);
348 for (int i
= 0; i
< ctx
->Const
.MaxVertexStreams
; i
++) {
349 obj
->vertices_written
[i
] = vertices_per_prim
* obj
->prims_generated
[i
];
351 obj
->vertices_written_valid
= true;
355 * GetTransformFeedbackVertexCount() driver hook.
357 * Returns the number of vertices written to a particular stream by the last
358 * Begin/EndTransformFeedback block. Used to implement DrawTransformFeedback().
361 brw_get_transform_feedback_vertex_count(struct gl_context
*ctx
,
362 struct gl_transform_feedback_object
*obj
,
365 struct brw_context
*brw
= brw_context(ctx
);
366 struct brw_transform_feedback_object
*brw_obj
=
367 (struct brw_transform_feedback_object
*) obj
;
369 assert(obj
->EndedAnytime
);
370 assert(stream
< ctx
->Const
.MaxVertexStreams
);
372 brw_compute_xfb_vertices_written(brw
, brw_obj
);
373 return brw_obj
->vertices_written
[stream
];
377 brw_begin_transform_feedback(struct gl_context
*ctx
, GLenum mode
,
378 struct gl_transform_feedback_object
*obj
)
380 struct brw_context
*brw
= brw_context(ctx
);
381 const struct gl_program
*prog
;
382 const struct gl_transform_feedback_info
*linked_xfb_info
;
383 struct gl_transform_feedback_object
*xfb_obj
=
384 ctx
->TransformFeedback
.CurrentObject
;
385 struct brw_transform_feedback_object
*brw_obj
=
386 (struct brw_transform_feedback_object
*) xfb_obj
;
388 assert(brw
->gen
== 6);
390 if (ctx
->_Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
]) {
391 /* BRW_NEW_GEOMETRY_PROGRAM */
392 prog
= ctx
->_Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
];
394 /* BRW_NEW_VERTEX_PROGRAM */
395 prog
= ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
];
397 linked_xfb_info
= prog
->sh
.LinkedTransformFeedback
;
399 /* Compute the maximum number of vertices that we can write without
400 * overflowing any of the buffers currently being used for feedback.
403 = _mesa_compute_max_transform_feedback_vertices(ctx
, xfb_obj
,
406 /* Initialize the SVBI 0 register to zero and set the maximum index. */
408 OUT_BATCH(_3DSTATE_GS_SVB_INDEX
<< 16 | (4 - 2));
409 OUT_BATCH(0); /* SVBI 0 */
410 OUT_BATCH(0); /* starting index */
411 OUT_BATCH(brw_obj
->max_index
);
414 /* Initialize the rest of the unused streams to sane values. Otherwise,
415 * they may indicate that there is no room to write data and prevent
416 * anything from happening at all.
418 for (int i
= 1; i
< 4; i
++) {
420 OUT_BATCH(_3DSTATE_GS_SVB_INDEX
<< 16 | (4 - 2));
421 OUT_BATCH(i
<< SVB_INDEX_SHIFT
);
422 OUT_BATCH(0); /* starting index */
423 OUT_BATCH(0xffffffff);
429 brw_end_transform_feedback(struct gl_context
*ctx
,
430 struct gl_transform_feedback_object
*obj
)
432 /* After EndTransformFeedback, it's likely that the client program will try
433 * to draw using the contents of the transform feedback buffer as vertex
434 * input. In order for this to work, we need to flush the data through at
435 * least the GS stage of the pipeline, and flush out the render cache. For
436 * simplicity, just do a full flush.
438 struct brw_context
*brw
= brw_context(ctx
);
439 brw_emit_mi_flush(brw
);