i965: Fix component mask and varying_to_slot mapping for gl_ViewportIndex
[mesa.git] / src / mesa / drivers / dri / i965 / gen7_sol_state.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 /**
25 * @file gen7_sol_state.c
26 *
27 * Controls the stream output logic (SOL) stage of the gen7 hardware, which is
28 * used to implement GL_EXT_transform_feedback.
29 */
30
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"
37
38 static void
39 upload_3dstate_so_buffers(struct brw_context *brw)
40 {
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;
47 int i;
48
49 /* Set up the up to 4 output buffers. These are the ranges defined in the
50 * gl_transform_feedback_object.
51 */
52 for (i = 0; i < 4; i++) {
53 struct intel_buffer_object *bufferobj =
54 intel_buffer_object(xfb_obj->Buffers[i]);
55 drm_intel_bo *bo;
56 uint32_t start, end;
57 uint32_t stride;
58
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.
62 */
63 BEGIN_BATCH(4);
64 OUT_BATCH(_3DSTATE_SO_BUFFER << 16 | (4 - 2));
65 OUT_BATCH((i << SO_BUFFER_INDEX_SHIFT));
66 OUT_BATCH(0);
67 OUT_BATCH(0);
68 ADVANCE_BATCH();
69
70 continue;
71 }
72
73 stride = linked_xfb_info->BufferStride[i] * 4;
74
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);
80
81 BEGIN_BATCH(4);
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);
86 ADVANCE_BATCH();
87 }
88 }
89
90 /**
91 * Outputs the 3DSTATE_SO_DECL_LIST command.
92 *
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.
96 */
97 void
98 gen7_upload_3dstate_so_decl_list(struct brw_context *brw,
99 const struct brw_vue_map *vue_map)
100 {
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];
108 int buffer_mask = 0;
109 int next_offset[4] = {0, 0, 0, 0};
110 int decls = 0;
111
112 STATIC_ASSERT(ARRAY_SIZE(so_decl) >= MAX_PROGRAM_OUTPUTS);
113
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.
116 */
117 for (int i = 0; i < linked_xfb_info->NumOutputs; i++) {
118 int buffer = linked_xfb_info->Outputs[i].OutputBuffer;
119 uint16_t decl = 0;
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;
123
124 /* gl_PointSize is stored in VARYING_SLOT_PSIZ.w
125 * gl_Layer is stored in VARYING_SLOT_PSIZ.y
126 * gl_ViewportIndex is stored in VARYING_SLOT_PSIZ.z
127 */
128 if (varying == VARYING_SLOT_PSIZ) {
129 assert(components == 1);
130 component_mask <<= 3;
131 } else if (varying == VARYING_SLOT_LAYER) {
132 assert(components == 1);
133 component_mask <<= 1;
134 } else if (varying == VARYING_SLOT_VIEWPORT) {
135 assert(components == 1);
136 component_mask <<= 2;
137 } else {
138 component_mask <<= linked_xfb_info->Outputs[i].ComponentOffset;
139 }
140
141 buffer_mask |= 1 << buffer;
142
143 decl |= buffer << SO_DECL_OUTPUT_BUFFER_SLOT_SHIFT;
144 if (varying == VARYING_SLOT_LAYER || varying == VARYING_SLOT_VIEWPORT) {
145 decl |= vue_map->varying_to_slot[VARYING_SLOT_PSIZ] <<
146 SO_DECL_REGISTER_INDEX_SHIFT;
147 } else {
148 assert(vue_map->varying_to_slot[varying] >= 0);
149 decl |= vue_map->varying_to_slot[varying] <<
150 SO_DECL_REGISTER_INDEX_SHIFT;
151 }
152 decl |= component_mask << SO_DECL_COMPONENT_MASK_SHIFT;
153
154 /* Mesa doesn't store entries for gl_SkipComponents in the Outputs[]
155 * array. Instead, it simply increments DstOffset for the following
156 * input by the number of components that should be skipped.
157 *
158 * Our hardware is unusual in that it requires us to program SO_DECLs
159 * for fake "hole" components, rather than simply taking the offset
160 * for each real varying. Each hole can have size 1, 2, 3, or 4; we
161 * program as many size = 4 holes as we can, then a final hole to
162 * accomodate the final 1, 2, or 3 remaining.
163 */
164 int skip_components =
165 linked_xfb_info->Outputs[i].DstOffset - next_offset[buffer];
166
167 next_offset[buffer] += skip_components;
168
169 while (skip_components >= 4) {
170 so_decl[decls++] = SO_DECL_HOLE_FLAG | 0xf;
171 skip_components -= 4;
172 }
173 if (skip_components > 0)
174 so_decl[decls++] = SO_DECL_HOLE_FLAG | ((1 << skip_components) - 1);
175
176 assert(linked_xfb_info->Outputs[i].DstOffset == next_offset[buffer]);
177
178 next_offset[buffer] += components;
179
180 so_decl[decls++] = decl;
181 }
182
183 BEGIN_BATCH(decls * 2 + 3);
184 OUT_BATCH(_3DSTATE_SO_DECL_LIST << 16 | (decls * 2 + 1));
185
186 OUT_BATCH((buffer_mask << SO_STREAM_TO_BUFFER_SELECTS_0_SHIFT) |
187 (0 << SO_STREAM_TO_BUFFER_SELECTS_1_SHIFT) |
188 (0 << SO_STREAM_TO_BUFFER_SELECTS_2_SHIFT) |
189 (0 << SO_STREAM_TO_BUFFER_SELECTS_3_SHIFT));
190
191 OUT_BATCH((decls << SO_NUM_ENTRIES_0_SHIFT) |
192 (0 << SO_NUM_ENTRIES_1_SHIFT) |
193 (0 << SO_NUM_ENTRIES_2_SHIFT) |
194 (0 << SO_NUM_ENTRIES_3_SHIFT));
195
196 for (int i = 0; i < decls; i++) {
197 OUT_BATCH(so_decl[i]);
198 OUT_BATCH(0);
199 }
200
201 ADVANCE_BATCH();
202 }
203
204 static void
205 upload_3dstate_streamout(struct brw_context *brw, bool active,
206 const struct brw_vue_map *vue_map)
207 {
208 struct gl_context *ctx = &brw->ctx;
209 /* BRW_NEW_TRANSFORM_FEEDBACK */
210 struct gl_transform_feedback_object *xfb_obj =
211 ctx->TransformFeedback.CurrentObject;
212 uint32_t dw1 = 0, dw2 = 0;
213 int i;
214
215 if (active) {
216 int urb_entry_read_offset = 0;
217 int urb_entry_read_length = (vue_map->num_slots + 1) / 2 -
218 urb_entry_read_offset;
219
220 dw1 |= SO_FUNCTION_ENABLE;
221 dw1 |= SO_STATISTICS_ENABLE;
222
223 /* _NEW_LIGHT */
224 if (ctx->Light.ProvokingVertex != GL_FIRST_VERTEX_CONVENTION)
225 dw1 |= SO_REORDER_TRAILING;
226
227 for (i = 0; i < 4; i++) {
228 if (xfb_obj->Buffers[i]) {
229 dw1 |= SO_BUFFER_ENABLE(i);
230 }
231 }
232
233 /* We always read the whole vertex. This could be reduced at some
234 * point by reading less and offsetting the register index in the
235 * SO_DECLs.
236 */
237 dw2 |= urb_entry_read_offset << SO_STREAM_0_VERTEX_READ_OFFSET_SHIFT;
238 dw2 |= (urb_entry_read_length - 1) <<
239 SO_STREAM_0_VERTEX_READ_LENGTH_SHIFT;
240 }
241
242 BEGIN_BATCH(3);
243 OUT_BATCH(_3DSTATE_STREAMOUT << 16 | (3 - 2));
244 OUT_BATCH(dw1);
245 OUT_BATCH(dw2);
246 ADVANCE_BATCH();
247 }
248
249 static void
250 upload_sol_state(struct brw_context *brw)
251 {
252 struct gl_context *ctx = &brw->ctx;
253 /* BRW_NEW_TRANSFORM_FEEDBACK */
254 bool active = _mesa_is_xfb_active_and_unpaused(ctx);
255
256 if (active) {
257 upload_3dstate_so_buffers(brw);
258 /* BRW_NEW_VUE_MAP_GEOM_OUT */
259 gen7_upload_3dstate_so_decl_list(brw, &brw->vue_map_geom_out);
260 }
261
262 /* Finally, set up the SOL stage. This command must always follow updates to
263 * the nonpipelined SOL state (3DSTATE_SO_BUFFER, 3DSTATE_SO_DECL_LIST) or
264 * MMIO register updates (current performed by the kernel at each batch
265 * emit).
266 */
267 upload_3dstate_streamout(brw, active, &brw->vue_map_geom_out);
268 }
269
270 const struct brw_tracked_state gen7_sol_state = {
271 .dirty = {
272 .mesa = (_NEW_LIGHT),
273 .brw = (BRW_NEW_BATCH |
274 BRW_NEW_VUE_MAP_GEOM_OUT |
275 BRW_NEW_TRANSFORM_FEEDBACK)
276 },
277 .emit = upload_sol_state,
278 };
279
280 /**
281 * Tally the number of primitives generated so far.
282 *
283 * The buffer contains a series of pairs:
284 * (<start0, start1, start2, start3>, <end0, end1, end2, end3>) ;
285 * (<start0, start1, start2, start3>, <end0, end1, end2, end3>) ;
286 *
287 * For each stream, we subtract the pair of values (end - start) to get the
288 * number of primitives generated during one section. We accumulate these
289 * values, adding them up to get the total number of primitives generated.
290 */
291 static void
292 gen7_tally_prims_generated(struct brw_context *brw,
293 struct brw_transform_feedback_object *obj)
294 {
295 /* If the current batch is still contributing to the number of primitives
296 * generated, flush it now so the results will be present when mapped.
297 */
298 if (drm_intel_bo_references(brw->batch.bo, obj->prim_count_bo))
299 intel_batchbuffer_flush(brw);
300
301 if (unlikely(brw->perf_debug && drm_intel_bo_busy(obj->prim_count_bo)))
302 perf_debug("Stalling for # of transform feedback primitives written.\n");
303
304 drm_intel_bo_map(obj->prim_count_bo, false);
305 uint64_t *prim_counts = obj->prim_count_bo->virtual;
306
307 assert(obj->prim_count_buffer_index % (2 * BRW_MAX_XFB_STREAMS) == 0);
308 int pairs = obj->prim_count_buffer_index / (2 * BRW_MAX_XFB_STREAMS);
309
310 for (int i = 0; i < pairs; i++) {
311 for (int s = 0; s < BRW_MAX_XFB_STREAMS; s++) {
312 obj->prims_generated[s] +=
313 prim_counts[BRW_MAX_XFB_STREAMS + s] - prim_counts[s];
314 }
315 prim_counts += 2 * BRW_MAX_XFB_STREAMS; /* move to the next pair */
316 }
317
318 drm_intel_bo_unmap(obj->prim_count_bo);
319
320 /* We've already gathered up the old data; we can safely overwrite it now. */
321 obj->prim_count_buffer_index = 0;
322 }
323
324 /**
325 * Store the SO_NUM_PRIMS_WRITTEN counters for each stream (4 uint64_t values)
326 * to prim_count_bo.
327 *
328 * If prim_count_bo is out of space, gather up the results so far into
329 * prims_generated[] and allocate a new buffer with enough space.
330 *
331 * The number of primitives written is used to compute the number of vertices
332 * written to a transform feedback stream, which is required to implement
333 * DrawTransformFeedback().
334 */
335 static void
336 gen7_save_primitives_written_counters(struct brw_context *brw,
337 struct brw_transform_feedback_object *obj)
338 {
339 const int streams = BRW_MAX_XFB_STREAMS;
340
341 /* Check if there's enough space for a new pair of four values. */
342 if (obj->prim_count_bo != NULL &&
343 obj->prim_count_buffer_index + 2 * streams >= 4096 / sizeof(uint64_t)) {
344 /* Gather up the results so far and release the BO. */
345 gen7_tally_prims_generated(brw, obj);
346 }
347
348 /* Flush any drawing so that the counters have the right values. */
349 intel_batchbuffer_emit_mi_flush(brw);
350
351 /* Emit MI_STORE_REGISTER_MEM commands to write the values. */
352 for (int i = 0; i < streams; i++) {
353 brw_store_register_mem64(brw, obj->prim_count_bo,
354 GEN7_SO_NUM_PRIMS_WRITTEN(i),
355 obj->prim_count_buffer_index + i);
356 }
357
358 /* Update where to write data to. */
359 obj->prim_count_buffer_index += streams;
360 }
361
362 /**
363 * Compute the number of vertices written by this transform feedback operation.
364 */
365 static void
366 brw_compute_xfb_vertices_written(struct brw_context *brw,
367 struct brw_transform_feedback_object *obj)
368 {
369 if (obj->vertices_written_valid || !obj->base.EndedAnytime)
370 return;
371
372 unsigned vertices_per_prim = 0;
373
374 switch (obj->primitive_mode) {
375 case GL_POINTS:
376 vertices_per_prim = 1;
377 break;
378 case GL_LINES:
379 vertices_per_prim = 2;
380 break;
381 case GL_TRIANGLES:
382 vertices_per_prim = 3;
383 break;
384 default:
385 assert(!"Invalid transform feedback primitive mode.");
386 }
387
388 /* Get the number of primitives generated. */
389 gen7_tally_prims_generated(brw, obj);
390
391 for (int i = 0; i < BRW_MAX_XFB_STREAMS; i++) {
392 obj->vertices_written[i] = vertices_per_prim * obj->prims_generated[i];
393 }
394 obj->vertices_written_valid = true;
395 }
396
397 /**
398 * GetTransformFeedbackVertexCount() driver hook.
399 *
400 * Returns the number of vertices written to a particular stream by the last
401 * Begin/EndTransformFeedback block. Used to implement DrawTransformFeedback().
402 */
403 GLsizei
404 brw_get_transform_feedback_vertex_count(struct gl_context *ctx,
405 struct gl_transform_feedback_object *obj,
406 GLuint stream)
407 {
408 struct brw_context *brw = brw_context(ctx);
409 struct brw_transform_feedback_object *brw_obj =
410 (struct brw_transform_feedback_object *) obj;
411
412 assert(obj->EndedAnytime);
413 assert(stream < BRW_MAX_XFB_STREAMS);
414
415 brw_compute_xfb_vertices_written(brw, brw_obj);
416 return brw_obj->vertices_written[stream];
417 }
418
419 void
420 gen7_begin_transform_feedback(struct gl_context *ctx, GLenum mode,
421 struct gl_transform_feedback_object *obj)
422 {
423 struct brw_context *brw = brw_context(ctx);
424 struct brw_transform_feedback_object *brw_obj =
425 (struct brw_transform_feedback_object *) obj;
426
427 /* Reset the SO buffer offsets to 0. */
428 if (brw->gen >= 8) {
429 brw_obj->zero_offsets = true;
430 } else {
431 intel_batchbuffer_flush(brw);
432 brw->batch.needs_sol_reset = true;
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 gen7_save_primitives_written_counters(brw, brw_obj);
448
449 brw_obj->primitive_mode = mode;
450 }
451
452 void
453 gen7_end_transform_feedback(struct gl_context *ctx,
454 struct gl_transform_feedback_object *obj)
455 {
456 /* After EndTransformFeedback, it's likely that the client program will try
457 * to draw using the contents of the transform feedback buffer as vertex
458 * input. In order for this to work, we need to flush the data through at
459 * least the GS stage of the pipeline, and flush out the render cache. For
460 * simplicity, just do a full flush.
461 */
462 struct brw_context *brw = brw_context(ctx);
463 struct brw_transform_feedback_object *brw_obj =
464 (struct brw_transform_feedback_object *) obj;
465
466 /* Store the ending value of the SO_NUM_PRIMS_WRITTEN counters. */
467 gen7_save_primitives_written_counters(brw, brw_obj);
468
469 /* EndTransformFeedback() means that we need to update the number of
470 * vertices written. Since it's only necessary if DrawTransformFeedback()
471 * is called and it means mapping a buffer object, we delay computing it
472 * until it's absolutely necessary to try and avoid stalls.
473 */
474 brw_obj->vertices_written_valid = false;
475 }
476
477 void
478 gen7_pause_transform_feedback(struct gl_context *ctx,
479 struct gl_transform_feedback_object *obj)
480 {
481 struct brw_context *brw = brw_context(ctx);
482 struct brw_transform_feedback_object *brw_obj =
483 (struct brw_transform_feedback_object *) obj;
484
485 /* Flush any drawing so that the counters have the right values. */
486 intel_batchbuffer_emit_mi_flush(brw);
487
488 /* Save the SOL buffer offset register values. */
489 if (brw->gen < 8) {
490 for (int i = 0; i < 4; i++) {
491 BEGIN_BATCH(3);
492 OUT_BATCH(MI_STORE_REGISTER_MEM | (3 - 2));
493 OUT_BATCH(GEN7_SO_WRITE_OFFSET(i));
494 OUT_RELOC(brw_obj->offset_bo,
495 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
496 i * sizeof(uint32_t));
497 ADVANCE_BATCH();
498 }
499 }
500
501 /* Store the temporary ending value of the SO_NUM_PRIMS_WRITTEN counters.
502 * While this operation is paused, other transform feedback actions may
503 * occur, which will contribute to the counters. We need to exclude that
504 * from our counts.
505 */
506 gen7_save_primitives_written_counters(brw, brw_obj);
507 }
508
509 void
510 gen7_resume_transform_feedback(struct gl_context *ctx,
511 struct gl_transform_feedback_object *obj)
512 {
513 struct brw_context *brw = brw_context(ctx);
514 struct brw_transform_feedback_object *brw_obj =
515 (struct brw_transform_feedback_object *) obj;
516
517 /* Reload the SOL buffer offset registers. */
518 if (brw->gen < 8) {
519 for (int i = 0; i < 4; i++) {
520 BEGIN_BATCH(3);
521 OUT_BATCH(GEN7_MI_LOAD_REGISTER_MEM | (3 - 2));
522 OUT_BATCH(GEN7_SO_WRITE_OFFSET(i));
523 OUT_RELOC(brw_obj->offset_bo,
524 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
525 i * sizeof(uint32_t));
526 ADVANCE_BATCH();
527 }
528 }
529
530 /* Store the new starting value of the SO_NUM_PRIMS_WRITTEN counters. */
531 gen7_save_primitives_written_counters(brw, brw_obj);
532 }