i965: drop brw->gen in favor of devinfo->gen
[mesa.git] / src / mesa / drivers / dri / i965 / gen6_sol.c
1 /*
2 * Copyright © 2011 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24 /** \file gen6_sol.c
25 *
26 * Code to initialize the binding table entries used by transform feedback.
27 */
28
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"
36
37 static void
38 gen6_update_sol_surfaces(struct brw_context *brw)
39 {
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;
44
45 if (xfb_active) {
46 /* BRW_NEW_TRANSFORM_FEEDBACK */
47 xfb_obj = ctx->TransformFeedback.CurrentObject;
48 linked_xfb_info = xfb_obj->program->sh.LinkedTransformFeedback;
49 }
50
51 for (int i = 0; i < BRW_MAX_SOL_BINDINGS; ++i) {
52 const int surf_index = BRW_GEN6_SOL_BINDING_START + 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);
64 } else {
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);
70 }
71 } else {
72 if (!brw->geometry_program)
73 brw->ff_gs.surf_offset[surf_index] = 0;
74 else
75 brw->gs.base.surf_offset[surf_index] = 0;
76 }
77 }
78
79 brw->ctx.NewDriverState |= BRW_NEW_SURFACES;
80 }
81
82 const struct brw_tracked_state gen6_sol_surface = {
83 .dirty = {
84 .mesa = 0,
85 .brw = BRW_NEW_BATCH |
86 BRW_NEW_BLORP |
87 BRW_NEW_TRANSFORM_FEEDBACK,
88 },
89 .emit = gen6_update_sol_surfaces,
90 };
91
92 /**
93 * Constructs the binding table for the WM surface state, which maps unit
94 * numbers to surface state objects.
95 */
96 static void
97 brw_gs_upload_binding_table(struct brw_context *brw)
98 {
99 uint32_t *bind;
100 struct gl_context *ctx = &brw->ctx;
101 const struct gl_program *prog;
102 bool need_binding_table = false;
103
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,
110 * in the GS stage.
111 */
112
113 if (!brw->geometry_program) {
114 /* BRW_NEW_VERTEX_PROGRAM */
115 prog = ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX];
116 if (prog) {
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;
121 }
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;
126 }
127 return;
128 }
129
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.
133 */
134 bind = brw_state_batch(brw, sizeof(uint32_t) * BRW_MAX_SOL_BINDINGS,
135 32, &brw->ff_gs.bind_bo_offset);
136
137 /* BRW_NEW_SURFACES */
138 memcpy(bind, brw->ff_gs.surf_offset,
139 BRW_MAX_SOL_BINDINGS * sizeof(uint32_t));
140 } else {
141 /* BRW_NEW_GEOMETRY_PROGRAM */
142 prog = ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY];
143 if (prog) {
144 /* Skip making a binding table if we don't have anything to put in it */
145 struct brw_stage_prog_data *prog_data = brw->gs.base.prog_data;
146 const struct gl_transform_feedback_info *linked_xfb_info =
147 prog->sh.LinkedTransformFeedback;
148 need_binding_table = linked_xfb_info->NumOutputs > 0 ||
149 prog_data->binding_table.size_bytes > 0;
150 }
151 if (!need_binding_table) {
152 if (brw->gs.base.bind_bo_offset != 0) {
153 brw->gs.base.bind_bo_offset = 0;
154 brw->ctx.NewDriverState |= BRW_NEW_BINDING_TABLE_POINTERS;
155 }
156 return;
157 }
158
159 /* Might want to calculate nr_surfaces first, to avoid taking up so much
160 * space for the binding table.
161 */
162 bind = brw_state_batch(brw, sizeof(uint32_t) * BRW_MAX_SURFACES,
163 32, &brw->gs.base.bind_bo_offset);
164
165 /* BRW_NEW_SURFACES */
166 memcpy(bind, brw->gs.base.surf_offset,
167 BRW_MAX_SURFACES * sizeof(uint32_t));
168 }
169
170 brw->ctx.NewDriverState |= BRW_NEW_BINDING_TABLE_POINTERS;
171 }
172
173 const struct brw_tracked_state gen6_gs_binding_table = {
174 .dirty = {
175 .mesa = 0,
176 .brw = BRW_NEW_BATCH |
177 BRW_NEW_BLORP |
178 BRW_NEW_GEOMETRY_PROGRAM |
179 BRW_NEW_VERTEX_PROGRAM |
180 BRW_NEW_SURFACES,
181 },
182 .emit = brw_gs_upload_binding_table,
183 };
184
185 struct gl_transform_feedback_object *
186 brw_new_transform_feedback(struct gl_context *ctx, GLuint name)
187 {
188 struct brw_context *brw = brw_context(ctx);
189 struct brw_transform_feedback_object *brw_obj =
190 CALLOC_STRUCT(brw_transform_feedback_object);
191 if (!brw_obj)
192 return NULL;
193
194 _mesa_init_transform_feedback_object(&brw_obj->base, name);
195
196 brw_obj->offset_bo =
197 brw_bo_alloc(brw->bufmgr, "transform feedback offsets", 16, 64);
198 brw_obj->prim_count_bo =
199 brw_bo_alloc(brw->bufmgr, "xfb primitive counts", 4096, 64);
200
201 return &brw_obj->base;
202 }
203
204 void
205 brw_delete_transform_feedback(struct gl_context *ctx,
206 struct gl_transform_feedback_object *obj)
207 {
208 struct brw_transform_feedback_object *brw_obj =
209 (struct brw_transform_feedback_object *) obj;
210
211 for (unsigned i = 0; i < ARRAY_SIZE(obj->Buffers); i++) {
212 _mesa_reference_buffer_object(ctx, &obj->Buffers[i], NULL);
213 }
214
215 brw_bo_unreference(brw_obj->offset_bo);
216 brw_bo_unreference(brw_obj->prim_count_bo);
217
218 free(brw_obj);
219 }
220
221 /**
222 * Tally the number of primitives generated so far.
223 *
224 * The buffer contains a series of pairs:
225 * (<start0, start1, start2, start3>, <end0, end1, end2, end3>) ;
226 * (<start0, start1, start2, start3>, <end0, end1, end2, end3>) ;
227 *
228 * For each stream, we subtract the pair of values (end - start) to get the
229 * number of primitives generated during one section. We accumulate these
230 * values, adding them up to get the total number of primitives generated.
231 *
232 * Note that we expose one stream pre-Gen7, so the above is just (start, end).
233 */
234 static void
235 tally_prims_generated(struct brw_context *brw,
236 struct brw_transform_feedback_object *obj)
237 {
238 const struct gl_context *ctx = &brw->ctx;
239 const int streams = ctx->Const.MaxVertexStreams;
240
241 /* If the current batch is still contributing to the number of primitives
242 * generated, flush it now so the results will be present when mapped.
243 */
244 if (brw_batch_references(&brw->batch, obj->prim_count_bo))
245 intel_batchbuffer_flush(brw);
246
247 if (unlikely(brw->perf_debug && brw_bo_busy(obj->prim_count_bo)))
248 perf_debug("Stalling for # of transform feedback primitives written.\n");
249
250 uint64_t *prim_counts = brw_bo_map(brw, obj->prim_count_bo, MAP_READ);
251
252 assert(obj->prim_count_buffer_index % (2 * streams) == 0);
253 int pairs = obj->prim_count_buffer_index / (2 * streams);
254
255 for (int i = 0; i < pairs; i++) {
256 for (int s = 0; s < streams; s++) {
257 obj->prims_generated[s] += prim_counts[streams + s] - prim_counts[s];
258 }
259 prim_counts += 2 * streams; /* move to the next pair */
260 }
261
262 brw_bo_unmap(obj->prim_count_bo);
263
264 /* We've already gathered up the old data; we can safely overwrite it now. */
265 obj->prim_count_buffer_index = 0;
266 }
267
268 /**
269 * Store the SO_NUM_PRIMS_WRITTEN counters for each stream (4 uint64_t values)
270 * to prim_count_bo.
271 *
272 * If prim_count_bo is out of space, gather up the results so far into
273 * prims_generated[] and allocate a new buffer with enough space.
274 *
275 * The number of primitives written is used to compute the number of vertices
276 * written to a transform feedback stream, which is required to implement
277 * DrawTransformFeedback().
278 */
279 void
280 brw_save_primitives_written_counters(struct brw_context *brw,
281 struct brw_transform_feedback_object *obj)
282 {
283 const struct gen_device_info *devinfo = &brw->screen->devinfo;
284 const struct gl_context *ctx = &brw->ctx;
285 const int streams = ctx->Const.MaxVertexStreams;
286
287 assert(obj->prim_count_bo != NULL);
288
289 /* Check if there's enough space for a new pair of four values. */
290 if (obj->prim_count_buffer_index + 2 * streams >= 4096 / sizeof(uint64_t)) {
291 /* Gather up the results so far and release the BO. */
292 tally_prims_generated(brw, obj);
293 }
294
295 /* Flush any drawing so that the counters have the right values. */
296 brw_emit_mi_flush(brw);
297
298 /* Emit MI_STORE_REGISTER_MEM commands to write the values. */
299 if (devinfo->gen >= 7) {
300 for (int i = 0; i < streams; i++) {
301 int offset = (obj->prim_count_buffer_index + i) * sizeof(uint64_t);
302 brw_store_register_mem64(brw, obj->prim_count_bo,
303 GEN7_SO_NUM_PRIMS_WRITTEN(i),
304 offset);
305 }
306 } else {
307 brw_store_register_mem64(brw, obj->prim_count_bo,
308 GEN6_SO_NUM_PRIMS_WRITTEN,
309 obj->prim_count_buffer_index * sizeof(uint64_t));
310 }
311
312 /* Update where to write data to. */
313 obj->prim_count_buffer_index += streams;
314 }
315
316 static void
317 compute_vertices_written_so_far(struct brw_context *brw,
318 struct brw_transform_feedback_object *obj,
319 uint64_t *vertices_written)
320 {
321 const struct gl_context *ctx = &brw->ctx;
322 unsigned vertices_per_prim = 0;
323
324 switch (obj->primitive_mode) {
325 case GL_POINTS:
326 vertices_per_prim = 1;
327 break;
328 case GL_LINES:
329 vertices_per_prim = 2;
330 break;
331 case GL_TRIANGLES:
332 vertices_per_prim = 3;
333 break;
334 default:
335 unreachable("Invalid transform feedback primitive mode.");
336 }
337
338 /* Get the number of primitives generated. */
339 tally_prims_generated(brw, obj);
340
341 for (int i = 0; i < ctx->Const.MaxVertexStreams; i++) {
342 vertices_written[i] = vertices_per_prim * obj->prims_generated[i];
343 }
344 }
345
346 /**
347 * Compute the number of vertices written by this transform feedback operation.
348 */
349 void
350 brw_compute_xfb_vertices_written(struct brw_context *brw,
351 struct brw_transform_feedback_object *obj)
352 {
353 if (obj->vertices_written_valid || !obj->base.EndedAnytime)
354 return;
355
356 compute_vertices_written_so_far(brw, obj, obj->vertices_written);
357
358 obj->vertices_written_valid = true;
359 }
360
361 /**
362 * GetTransformFeedbackVertexCount() driver hook.
363 *
364 * Returns the number of vertices written to a particular stream by the last
365 * Begin/EndTransformFeedback block. Used to implement DrawTransformFeedback().
366 */
367 GLsizei
368 brw_get_transform_feedback_vertex_count(struct gl_context *ctx,
369 struct gl_transform_feedback_object *obj,
370 GLuint stream)
371 {
372 struct brw_context *brw = brw_context(ctx);
373 struct brw_transform_feedback_object *brw_obj =
374 (struct brw_transform_feedback_object *) obj;
375
376 assert(obj->EndedAnytime);
377 assert(stream < ctx->Const.MaxVertexStreams);
378
379 brw_compute_xfb_vertices_written(brw, brw_obj);
380 return brw_obj->vertices_written[stream];
381 }
382
383 void
384 brw_begin_transform_feedback(struct gl_context *ctx, GLenum mode,
385 struct gl_transform_feedback_object *obj)
386 {
387 struct brw_context *brw = brw_context(ctx);
388 const struct gen_device_info *devinfo = &brw->screen->devinfo;
389 const struct gl_program *prog;
390 const struct gl_transform_feedback_info *linked_xfb_info;
391 struct gl_transform_feedback_object *xfb_obj =
392 ctx->TransformFeedback.CurrentObject;
393 struct brw_transform_feedback_object *brw_obj =
394 (struct brw_transform_feedback_object *) xfb_obj;
395
396 assert(devinfo->gen == 6);
397
398 if (ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY]) {
399 /* BRW_NEW_GEOMETRY_PROGRAM */
400 prog = ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY];
401 } else {
402 /* BRW_NEW_VERTEX_PROGRAM */
403 prog = ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX];
404 }
405 linked_xfb_info = prog->sh.LinkedTransformFeedback;
406
407 /* Compute the maximum number of vertices that we can write without
408 * overflowing any of the buffers currently being used for feedback.
409 */
410 brw_obj->max_index
411 = _mesa_compute_max_transform_feedback_vertices(ctx, xfb_obj,
412 linked_xfb_info);
413
414 /* Initialize the SVBI 0 register to zero and set the maximum index. */
415 BEGIN_BATCH(4);
416 OUT_BATCH(_3DSTATE_GS_SVB_INDEX << 16 | (4 - 2));
417 OUT_BATCH(0); /* SVBI 0 */
418 OUT_BATCH(0); /* starting index */
419 OUT_BATCH(brw_obj->max_index);
420 ADVANCE_BATCH();
421
422 /* Initialize the rest of the unused streams to sane values. Otherwise,
423 * they may indicate that there is no room to write data and prevent
424 * anything from happening at all.
425 */
426 for (int i = 1; i < 4; i++) {
427 BEGIN_BATCH(4);
428 OUT_BATCH(_3DSTATE_GS_SVB_INDEX << 16 | (4 - 2));
429 OUT_BATCH(i << SVB_INDEX_SHIFT);
430 OUT_BATCH(0); /* starting index */
431 OUT_BATCH(0xffffffff);
432 ADVANCE_BATCH();
433 }
434
435 /* We're about to lose the information needed to compute the number of
436 * vertices written during the last Begin/EndTransformFeedback section,
437 * so we can't delay it any further.
438 */
439 brw_compute_xfb_vertices_written(brw, brw_obj);
440
441 /* No primitives have been generated yet. */
442 for (int i = 0; i < BRW_MAX_XFB_STREAMS; i++) {
443 brw_obj->prims_generated[i] = 0;
444 }
445
446 /* Store the starting value of the SO_NUM_PRIMS_WRITTEN counters. */
447 brw_save_primitives_written_counters(brw, brw_obj);
448
449 brw_obj->primitive_mode = mode;
450 }
451
452 void
453 brw_end_transform_feedback(struct gl_context *ctx,
454 struct gl_transform_feedback_object *obj)
455 {
456 struct brw_context *brw = brw_context(ctx);
457 struct brw_transform_feedback_object *brw_obj =
458 (struct brw_transform_feedback_object *) obj;
459
460 /* Store the ending value of the SO_NUM_PRIMS_WRITTEN counters. */
461 if (!obj->Paused)
462 brw_save_primitives_written_counters(brw, brw_obj);
463
464 /* EndTransformFeedback() means that we need to update the number of
465 * vertices written. Since it's only necessary if DrawTransformFeedback()
466 * is called and it means mapping a buffer object, we delay computing it
467 * until it's absolutely necessary to try and avoid stalls.
468 */
469 brw_obj->vertices_written_valid = false;
470 }
471
472 void
473 brw_pause_transform_feedback(struct gl_context *ctx,
474 struct gl_transform_feedback_object *obj)
475 {
476 struct brw_context *brw = brw_context(ctx);
477 struct brw_transform_feedback_object *brw_obj =
478 (struct brw_transform_feedback_object *) obj;
479
480 /* Store the temporary ending value of the SO_NUM_PRIMS_WRITTEN counters.
481 * While this operation is paused, other transform feedback actions may
482 * occur, which will contribute to the counters. We need to exclude that
483 * from our counts.
484 */
485 brw_save_primitives_written_counters(brw, brw_obj);
486 }
487
488 void
489 brw_resume_transform_feedback(struct gl_context *ctx,
490 struct gl_transform_feedback_object *obj)
491 {
492 struct brw_context *brw = brw_context(ctx);
493 struct brw_transform_feedback_object *brw_obj =
494 (struct brw_transform_feedback_object *) obj;
495
496 /* Reload SVBI 0 with the count of vertices written so far. */
497 uint64_t svbi;
498 compute_vertices_written_so_far(brw, brw_obj, &svbi);
499
500 BEGIN_BATCH(4);
501 OUT_BATCH(_3DSTATE_GS_SVB_INDEX << 16 | (4 - 2));
502 OUT_BATCH(0); /* SVBI 0 */
503 OUT_BATCH((uint32_t) svbi); /* starting index */
504 OUT_BATCH(brw_obj->max_index);
505 ADVANCE_BATCH();
506
507 /* Initialize the rest of the unused streams to sane values. Otherwise,
508 * they may indicate that there is no room to write data and prevent
509 * anything from happening at all.
510 */
511 for (int i = 1; i < 4; i++) {
512 BEGIN_BATCH(4);
513 OUT_BATCH(_3DSTATE_GS_SVB_INDEX << 16 | (4 - 2));
514 OUT_BATCH(i << SVB_INDEX_SHIFT);
515 OUT_BATCH(0); /* starting index */
516 OUT_BATCH(0xffffffff);
517 ADVANCE_BATCH();
518 }
519
520 /* Store the new starting value of the SO_NUM_PRIMS_WRITTEN counters. */
521 brw_save_primitives_written_counters(brw, brw_obj);
522 }