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