i965/gen7: Skip resetting SOL offsets at batch start with HW contexts.
[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/macros.h"
30 #include "brw_context.h"
31 #include "intel_batchbuffer.h"
32 #include "brw_defines.h"
33 #include "brw_state.h"
34 #include "main/transformfeedback.h"
35
36 static void
37 gen6_update_sol_surfaces(struct brw_context *brw)
38 {
39 struct gl_context *ctx = &brw->intel.ctx;
40 /* _NEW_TRANSFORM_FEEDBACK */
41 struct gl_transform_feedback_object *xfb_obj =
42 ctx->TransformFeedback.CurrentObject;
43 /* BRW_NEW_VERTEX_PROGRAM */
44 const struct gl_shader_program *shaderprog =
45 ctx->Shader.CurrentVertexProgram;
46 const struct gl_transform_feedback_info *linked_xfb_info =
47 &shaderprog->LinkedTransformFeedback;
48 int i;
49
50 for (i = 0; i < BRW_MAX_SOL_BINDINGS; ++i) {
51 const int surf_index = SURF_INDEX_SOL_BINDING(i);
52 if (_mesa_is_xfb_active_and_unpaused(ctx) &&
53 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 brw_update_sol_surface(
59 brw, xfb_obj->Buffers[buffer], &brw->gs.surf_offset[surf_index],
60 linked_xfb_info->Outputs[i].NumComponents,
61 linked_xfb_info->BufferStride[buffer], buffer_offset);
62 } else {
63 brw->gs.surf_offset[surf_index] = 0;
64 }
65 }
66
67 brw->state.dirty.brw |= BRW_NEW_SURFACES;
68 }
69
70 const struct brw_tracked_state gen6_sol_surface = {
71 .dirty = {
72 .mesa = _NEW_TRANSFORM_FEEDBACK,
73 .brw = (BRW_NEW_BATCH |
74 BRW_NEW_VERTEX_PROGRAM),
75 .cache = 0
76 },
77 .emit = gen6_update_sol_surfaces,
78 };
79
80 /**
81 * Constructs the binding table for the WM surface state, which maps unit
82 * numbers to surface state objects.
83 */
84 static void
85 brw_gs_upload_binding_table(struct brw_context *brw)
86 {
87 struct gl_context *ctx = &brw->intel.ctx;
88 /* BRW_NEW_VERTEX_PROGRAM */
89 const struct gl_shader_program *shaderprog =
90 ctx->Shader.CurrentVertexProgram;
91 bool has_surfaces = false;
92 uint32_t *bind;
93
94 if (shaderprog) {
95 const struct gl_transform_feedback_info *linked_xfb_info =
96 &shaderprog->LinkedTransformFeedback;
97 /* Currently we only ever upload surfaces for SOL. */
98 has_surfaces = linked_xfb_info->NumOutputs != 0;
99 }
100
101 /* Skip making a binding table if we don't have anything to put in it. */
102 if (!has_surfaces) {
103 if (brw->gs.bind_bo_offset != 0) {
104 brw->state.dirty.brw |= BRW_NEW_GS_BINDING_TABLE;
105 brw->gs.bind_bo_offset = 0;
106 }
107 return;
108 }
109
110 /* Might want to calculate nr_surfaces first, to avoid taking up so much
111 * space for the binding table.
112 */
113 bind = brw_state_batch(brw, AUB_TRACE_BINDING_TABLE,
114 sizeof(uint32_t) * BRW_MAX_GS_SURFACES,
115 32, &brw->gs.bind_bo_offset);
116
117 /* BRW_NEW_SURFACES */
118 memcpy(bind, brw->gs.surf_offset, BRW_MAX_GS_SURFACES * sizeof(uint32_t));
119
120 brw->state.dirty.brw |= BRW_NEW_GS_BINDING_TABLE;
121 }
122
123 const struct brw_tracked_state gen6_gs_binding_table = {
124 .dirty = {
125 .mesa = 0,
126 .brw = (BRW_NEW_BATCH |
127 BRW_NEW_VERTEX_PROGRAM |
128 BRW_NEW_SURFACES),
129 .cache = 0
130 },
131 .emit = brw_gs_upload_binding_table,
132 };
133
134 static void
135 gen6_update_sol_indices(struct brw_context *brw)
136 {
137 struct intel_context *intel = &brw->intel;
138
139 BEGIN_BATCH(4);
140 OUT_BATCH(_3DSTATE_GS_SVB_INDEX << 16 | (4 - 2));
141 OUT_BATCH(0);
142 OUT_BATCH(brw->sol.svbi_0_starting_index); /* BRW_NEW_SOL_INDICES */
143 OUT_BATCH(brw->sol.svbi_0_max_index); /* BRW_NEW_SOL_INDICES */
144 ADVANCE_BATCH();
145 }
146
147 const struct brw_tracked_state gen6_sol_indices = {
148 .dirty = {
149 .mesa = 0,
150 .brw = (BRW_NEW_BATCH |
151 BRW_NEW_SOL_INDICES),
152 .cache = 0
153 },
154 .emit = gen6_update_sol_indices,
155 };
156
157 void
158 brw_begin_transform_feedback(struct gl_context *ctx, GLenum mode,
159 struct gl_transform_feedback_object *obj)
160 {
161 struct brw_context *brw = brw_context(ctx);
162 struct intel_context *intel = &brw->intel;
163 const struct gl_shader_program *vs_prog =
164 ctx->Shader.CurrentVertexProgram;
165 const struct gl_transform_feedback_info *linked_xfb_info =
166 &vs_prog->LinkedTransformFeedback;
167 struct gl_transform_feedback_object *xfb_obj =
168 ctx->TransformFeedback.CurrentObject;
169
170 /* Compute the maximum number of vertices that we can write without
171 * overflowing any of the buffers currently being used for feedback.
172 */
173 unsigned max_index
174 = _mesa_compute_max_transform_feedback_vertices(xfb_obj,
175 linked_xfb_info);
176
177 /* Initialize the SVBI 0 register to zero and set the maximum index.
178 * These values will be sent to the hardware on the next draw.
179 */
180 brw->state.dirty.brw |= BRW_NEW_SOL_INDICES;
181 brw->sol.svbi_0_starting_index = 0;
182 brw->sol.svbi_0_max_index = max_index;
183 brw->sol.offset_0_batch_start = 0;
184
185 if (intel->gen >= 7) {
186 /* Ask the kernel to reset the SO offsets for any previous transform
187 * feedback, so we start at the start of the user's buffer. (note: these
188 * are not the query counters)
189 */
190 intel->batch.needs_sol_reset = true;
191 }
192 }
193
194 void
195 brw_end_transform_feedback(struct gl_context *ctx,
196 struct gl_transform_feedback_object *obj)
197 {
198 /* After EndTransformFeedback, it's likely that the client program will try
199 * to draw using the contents of the transform feedback buffer as vertex
200 * input. In order for this to work, we need to flush the data through at
201 * least the GS stage of the pipeline, and flush out the render cache. For
202 * simplicity, just do a full flush.
203 */
204 struct brw_context *brw = brw_context(ctx);
205 struct intel_context *intel = &brw->intel;
206 intel_batchbuffer_emit_mi_flush(intel);
207 }