i965: Alphabetize brw_tracked_state flags and use a consistent style.
[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 /* BRW_NEW_TRANSFORM_FEEDBACK */
42 struct gl_transform_feedback_object *xfb_obj =
43 ctx->TransformFeedback.CurrentObject;
44 const struct gl_shader_program *shaderprog;
45 const struct gl_transform_feedback_info *linked_xfb_info;
46 int i;
47
48 if (brw->geometry_program) {
49 /* BRW_NEW_GEOMETRY_PROGRAM */
50 shaderprog =
51 ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY];
52 } else {
53 /* BRW_NEW_VERTEX_PROGRAM */
54 shaderprog =
55 ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX];
56 }
57 linked_xfb_info = &shaderprog->LinkedTransformFeedback;
58
59 for (i = 0; i < BRW_MAX_SOL_BINDINGS; ++i) {
60 const int surf_index = SURF_INDEX_GEN6_SOL_BINDING(i);
61 if (_mesa_is_xfb_active_and_unpaused(ctx) &&
62 i < linked_xfb_info->NumOutputs) {
63 unsigned buffer = linked_xfb_info->Outputs[i].OutputBuffer;
64 unsigned buffer_offset =
65 xfb_obj->Offset[buffer] / 4 +
66 linked_xfb_info->Outputs[i].DstOffset;
67 if (brw->geometry_program) {
68 brw_update_sol_surface(
69 brw, xfb_obj->Buffers[buffer],
70 &brw->gs.base.surf_offset[surf_index],
71 linked_xfb_info->Outputs[i].NumComponents,
72 linked_xfb_info->BufferStride[buffer], buffer_offset);
73 } else {
74 brw_update_sol_surface(
75 brw, xfb_obj->Buffers[buffer],
76 &brw->ff_gs.surf_offset[surf_index],
77 linked_xfb_info->Outputs[i].NumComponents,
78 linked_xfb_info->BufferStride[buffer], buffer_offset);
79 }
80 } else {
81 if (!brw->geometry_program)
82 brw->ff_gs.surf_offset[surf_index] = 0;
83 else
84 brw->gs.base.surf_offset[surf_index] = 0;
85 }
86 }
87
88 brw->state.dirty.brw |= BRW_NEW_SURFACES;
89 }
90
91 const struct brw_tracked_state gen6_sol_surface = {
92 .dirty = {
93 .mesa = 0,
94 .brw = BRW_NEW_BATCH |
95 BRW_NEW_GEOMETRY_PROGRAM |
96 BRW_NEW_VERTEX_PROGRAM |
97 BRW_NEW_TRANSFORM_FEEDBACK,
98 .cache = 0
99 },
100 .emit = gen6_update_sol_surfaces,
101 };
102
103 /**
104 * Constructs the binding table for the WM surface state, which maps unit
105 * numbers to surface state objects.
106 */
107 static void
108 brw_gs_upload_binding_table(struct brw_context *brw)
109 {
110 uint32_t *bind;
111 struct gl_context *ctx = &brw->ctx;
112 const struct gl_shader_program *shaderprog;
113 bool need_binding_table = false;
114
115 /* We have two scenarios here:
116 * 1) We are using a geometry shader only to implement transform feedback
117 * for a vertex shader (brw->geometry_program == NULL). In this case, we
118 * only need surfaces for transform feedback in the GS stage.
119 * 2) We have a user-provided geometry shader. In this case we may need
120 * surfaces for transform feedback and/or other stuff, like textures,
121 * in the GS stage.
122 */
123
124 if (!brw->geometry_program) {
125 /* BRW_NEW_VERTEX_PROGRAM */
126 shaderprog = ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX];
127 if (shaderprog) {
128 /* Skip making a binding table if we don't have anything to put in it */
129 const struct gl_transform_feedback_info *linked_xfb_info =
130 &shaderprog->LinkedTransformFeedback;
131 need_binding_table = linked_xfb_info->NumOutputs > 0;
132 }
133 if (!need_binding_table) {
134 if (brw->ff_gs.bind_bo_offset != 0) {
135 brw->state.dirty.brw |= BRW_NEW_GS_BINDING_TABLE;
136 brw->ff_gs.bind_bo_offset = 0;
137 }
138 return;
139 }
140
141 /* Might want to calculate nr_surfaces first, to avoid taking up so much
142 * space for the binding table. Anyway, in this case we know that we only
143 * use BRW_MAX_SOL_BINDINGS surfaces at most.
144 */
145 bind = brw_state_batch(brw, AUB_TRACE_BINDING_TABLE,
146 sizeof(uint32_t) * BRW_MAX_SOL_BINDINGS,
147 32, &brw->ff_gs.bind_bo_offset);
148
149 /* BRW_NEW_SURFACES */
150 memcpy(bind, brw->ff_gs.surf_offset,
151 BRW_MAX_SOL_BINDINGS * sizeof(uint32_t));
152 } else {
153 /* BRW_NEW_GEOMETRY_PROGRAM */
154 shaderprog = ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY];
155 if (shaderprog) {
156 /* Skip making a binding table if we don't have anything to put in it */
157 struct brw_stage_prog_data *prog_data = brw->gs.base.prog_data;
158 const struct gl_transform_feedback_info *linked_xfb_info =
159 &shaderprog->LinkedTransformFeedback;
160 need_binding_table = linked_xfb_info->NumOutputs > 0 ||
161 prog_data->binding_table.size_bytes > 0;
162 }
163 if (!need_binding_table) {
164 if (brw->gs.base.bind_bo_offset != 0) {
165 brw->gs.base.bind_bo_offset = 0;
166 brw->state.dirty.brw |= BRW_NEW_GS_BINDING_TABLE;
167 }
168 return;
169 }
170
171 /* Might want to calculate nr_surfaces first, to avoid taking up so much
172 * space for the binding table.
173 */
174 bind = brw_state_batch(brw, AUB_TRACE_BINDING_TABLE,
175 sizeof(uint32_t) * BRW_MAX_SURFACES,
176 32, &brw->gs.base.bind_bo_offset);
177
178 /* BRW_NEW_SURFACES */
179 memcpy(bind, brw->gs.base.surf_offset,
180 BRW_MAX_SURFACES * sizeof(uint32_t));
181 }
182
183 brw->state.dirty.brw |= BRW_NEW_GS_BINDING_TABLE;
184 }
185
186 const struct brw_tracked_state gen6_gs_binding_table = {
187 .dirty = {
188 .mesa = 0,
189 .brw = BRW_NEW_BATCH |
190 BRW_NEW_GEOMETRY_PROGRAM |
191 BRW_NEW_VERTEX_PROGRAM |
192 BRW_NEW_SURFACES,
193 .cache = 0
194 },
195 .emit = brw_gs_upload_binding_table,
196 };
197
198 struct gl_transform_feedback_object *
199 brw_new_transform_feedback(struct gl_context *ctx, GLuint name)
200 {
201 struct brw_context *brw = brw_context(ctx);
202 struct brw_transform_feedback_object *brw_obj =
203 CALLOC_STRUCT(brw_transform_feedback_object);
204 if (!brw_obj)
205 return NULL;
206
207 _mesa_init_transform_feedback_object(&brw_obj->base, name);
208
209 brw_obj->offset_bo =
210 drm_intel_bo_alloc(brw->bufmgr, "transform feedback offsets", 16, 64);
211 brw_obj->prim_count_bo =
212 drm_intel_bo_alloc(brw->bufmgr, "xfb primitive counts", 4096, 64);
213
214 return &brw_obj->base;
215 }
216
217 void
218 brw_delete_transform_feedback(struct gl_context *ctx,
219 struct gl_transform_feedback_object *obj)
220 {
221 struct brw_transform_feedback_object *brw_obj =
222 (struct brw_transform_feedback_object *) obj;
223
224 for (unsigned i = 0; i < Elements(obj->Buffers); i++) {
225 _mesa_reference_buffer_object(ctx, &obj->Buffers[i], NULL);
226 }
227
228 drm_intel_bo_unreference(brw_obj->offset_bo);
229 drm_intel_bo_unreference(brw_obj->prim_count_bo);
230
231 free(brw_obj);
232 }
233
234 void
235 brw_begin_transform_feedback(struct gl_context *ctx, GLenum mode,
236 struct gl_transform_feedback_object *obj)
237 {
238 struct brw_context *brw = brw_context(ctx);
239 const struct gl_shader_program *shaderprog;
240 const struct gl_transform_feedback_info *linked_xfb_info;
241 struct gl_transform_feedback_object *xfb_obj =
242 ctx->TransformFeedback.CurrentObject;
243
244 assert(brw->gen == 6);
245
246 if (brw->geometry_program) {
247 /* BRW_NEW_GEOMETRY_PROGRAM */
248 shaderprog =
249 ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY];
250 } else {
251 /* BRW_NEW_VERTEX_PROGRAM */
252 shaderprog =
253 ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX];
254 }
255 linked_xfb_info = &shaderprog->LinkedTransformFeedback;
256
257 /* Compute the maximum number of vertices that we can write without
258 * overflowing any of the buffers currently being used for feedback.
259 */
260 unsigned max_index
261 = _mesa_compute_max_transform_feedback_vertices(xfb_obj,
262 linked_xfb_info);
263
264 /* 3DSTATE_GS_SVB_INDEX is non-pipelined. */
265 intel_emit_post_sync_nonzero_flush(brw);
266
267 /* Initialize the SVBI 0 register to zero and set the maximum index. */
268 BEGIN_BATCH(4);
269 OUT_BATCH(_3DSTATE_GS_SVB_INDEX << 16 | (4 - 2));
270 OUT_BATCH(0); /* SVBI 0 */
271 OUT_BATCH(0); /* starting index */
272 OUT_BATCH(max_index);
273 ADVANCE_BATCH();
274
275 /* Initialize the rest of the unused streams to sane values. Otherwise,
276 * they may indicate that there is no room to write data and prevent
277 * anything from happening at all.
278 */
279 for (int i = 1; i < 4; i++) {
280 BEGIN_BATCH(4);
281 OUT_BATCH(_3DSTATE_GS_SVB_INDEX << 16 | (4 - 2));
282 OUT_BATCH(i << SVB_INDEX_SHIFT);
283 OUT_BATCH(0); /* starting index */
284 OUT_BATCH(0xffffffff);
285 ADVANCE_BATCH();
286 }
287 }
288
289 void
290 brw_end_transform_feedback(struct gl_context *ctx,
291 struct gl_transform_feedback_object *obj)
292 {
293 /* After EndTransformFeedback, it's likely that the client program will try
294 * to draw using the contents of the transform feedback buffer as vertex
295 * input. In order for this to work, we need to flush the data through at
296 * least the GS stage of the pipeline, and flush out the render cache. For
297 * simplicity, just do a full flush.
298 */
299 struct brw_context *brw = brw_context(ctx);
300 intel_batchbuffer_emit_mi_flush(brw);
301 }