i965: Implement Pause/ResumeTransformfeedback driver hooks on Gen7+.
[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 /* BRW_NEW_VERTEX_PROGRAM */
45 const struct gl_shader_program *shaderprog =
46 ctx->Shader.CurrentVertexProgram;
47 const struct gl_transform_feedback_info *linked_xfb_info =
48 &shaderprog->LinkedTransformFeedback;
49 int i;
50
51 for (i = 0; i < BRW_MAX_SOL_BINDINGS; ++i) {
52 const int surf_index = SURF_INDEX_GEN6_SOL_BINDING(i);
53 if (_mesa_is_xfb_active_and_unpaused(ctx) &&
54 i < linked_xfb_info->NumOutputs) {
55 unsigned buffer = linked_xfb_info->Outputs[i].OutputBuffer;
56 unsigned buffer_offset =
57 xfb_obj->Offset[buffer] / 4 +
58 linked_xfb_info->Outputs[i].DstOffset;
59 brw_update_sol_surface(
60 brw, xfb_obj->Buffers[buffer], &brw->ff_gs.surf_offset[surf_index],
61 linked_xfb_info->Outputs[i].NumComponents,
62 linked_xfb_info->BufferStride[buffer], buffer_offset);
63 } else {
64 brw->ff_gs.surf_offset[surf_index] = 0;
65 }
66 }
67
68 brw->state.dirty.brw |= BRW_NEW_SURFACES;
69 }
70
71 const struct brw_tracked_state gen6_sol_surface = {
72 .dirty = {
73 .mesa = 0,
74 .brw = (BRW_NEW_BATCH |
75 BRW_NEW_VERTEX_PROGRAM |
76 BRW_NEW_TRANSFORM_FEEDBACK),
77 .cache = 0
78 },
79 .emit = gen6_update_sol_surfaces,
80 };
81
82 /**
83 * Constructs the binding table for the WM surface state, which maps unit
84 * numbers to surface state objects.
85 */
86 static void
87 brw_gs_upload_binding_table(struct brw_context *brw)
88 {
89 struct gl_context *ctx = &brw->ctx;
90 /* BRW_NEW_VERTEX_PROGRAM */
91 const struct gl_shader_program *shaderprog =
92 ctx->Shader.CurrentVertexProgram;
93 bool has_surfaces = false;
94 uint32_t *bind;
95
96 if (shaderprog) {
97 const struct gl_transform_feedback_info *linked_xfb_info =
98 &shaderprog->LinkedTransformFeedback;
99 /* Currently we only ever upload surfaces for SOL. */
100 has_surfaces = linked_xfb_info->NumOutputs != 0;
101 }
102
103 /* Skip making a binding table if we don't have anything to put in it. */
104 if (!has_surfaces) {
105 if (brw->ff_gs.bind_bo_offset != 0) {
106 brw->state.dirty.brw |= BRW_NEW_GS_BINDING_TABLE;
107 brw->ff_gs.bind_bo_offset = 0;
108 }
109 return;
110 }
111
112 /* Might want to calculate nr_surfaces first, to avoid taking up so much
113 * space for the binding table.
114 */
115 bind = brw_state_batch(brw, AUB_TRACE_BINDING_TABLE,
116 sizeof(uint32_t) * BRW_MAX_GEN6_GS_SURFACES,
117 32, &brw->ff_gs.bind_bo_offset);
118
119 /* BRW_NEW_SURFACES */
120 memcpy(bind, brw->ff_gs.surf_offset, BRW_MAX_GEN6_GS_SURFACES * sizeof(uint32_t));
121
122 brw->state.dirty.brw |= BRW_NEW_GS_BINDING_TABLE;
123 }
124
125 const struct brw_tracked_state gen6_gs_binding_table = {
126 .dirty = {
127 .mesa = 0,
128 .brw = (BRW_NEW_BATCH |
129 BRW_NEW_VERTEX_PROGRAM |
130 BRW_NEW_SURFACES),
131 .cache = 0
132 },
133 .emit = brw_gs_upload_binding_table,
134 };
135
136 struct gl_transform_feedback_object *
137 brw_new_transform_feedback(struct gl_context *ctx, GLuint name)
138 {
139 struct brw_context *brw = brw_context(ctx);
140 struct brw_transform_feedback_object *brw_obj =
141 CALLOC_STRUCT(brw_transform_feedback_object);
142 if (!brw_obj)
143 return NULL;
144
145 _mesa_init_transform_feedback_object(&brw_obj->base, name);
146
147 brw_obj->offset_bo =
148 drm_intel_bo_alloc(brw->bufmgr, "transform feedback offsets", 16, 64);
149
150 return &brw_obj->base;
151 }
152
153 void
154 brw_delete_transform_feedback(struct gl_context *ctx,
155 struct gl_transform_feedback_object *obj)
156 {
157 struct brw_transform_feedback_object *brw_obj =
158 (struct brw_transform_feedback_object *) obj;
159
160 for (unsigned i = 0; i < Elements(obj->Buffers); i++) {
161 _mesa_reference_buffer_object(ctx, &obj->Buffers[i], NULL);
162 }
163
164 drm_intel_bo_unreference(brw_obj->offset_bo);
165
166 free(brw_obj);
167 }
168
169 void
170 brw_begin_transform_feedback(struct gl_context *ctx, GLenum mode,
171 struct gl_transform_feedback_object *obj)
172 {
173 struct brw_context *brw = brw_context(ctx);
174 const struct gl_shader_program *vs_prog =
175 ctx->Shader.CurrentVertexProgram;
176 const struct gl_transform_feedback_info *linked_xfb_info =
177 &vs_prog->LinkedTransformFeedback;
178 struct gl_transform_feedback_object *xfb_obj =
179 ctx->TransformFeedback.CurrentObject;
180
181 assert(brw->gen == 6);
182
183 /* Compute the maximum number of vertices that we can write without
184 * overflowing any of the buffers currently being used for feedback.
185 */
186 unsigned max_index
187 = _mesa_compute_max_transform_feedback_vertices(xfb_obj,
188 linked_xfb_info);
189
190 /* 3DSTATE_GS_SVB_INDEX is non-pipelined. */
191 intel_emit_post_sync_nonzero_flush(brw);
192
193 /* Initialize the SVBI 0 register to zero and set the maximum index. */
194 BEGIN_BATCH(4);
195 OUT_BATCH(_3DSTATE_GS_SVB_INDEX << 16 | (4 - 2));
196 OUT_BATCH(0); /* SVBI 0 */
197 OUT_BATCH(0); /* starting index */
198 OUT_BATCH(max_index);
199 ADVANCE_BATCH();
200
201 /* Initialize the rest of the unused streams to sane values. Otherwise,
202 * they may indicate that there is no room to write data and prevent
203 * anything from happening at all.
204 */
205 for (int i = 1; i < 4; i++) {
206 BEGIN_BATCH(4);
207 OUT_BATCH(_3DSTATE_GS_SVB_INDEX << 16 | (4 - 2));
208 OUT_BATCH(i << SVB_INDEX_SHIFT);
209 OUT_BATCH(0); /* starting index */
210 OUT_BATCH(0xffffffff);
211 ADVANCE_BATCH();
212 }
213 }
214
215 void
216 brw_end_transform_feedback(struct gl_context *ctx,
217 struct gl_transform_feedback_object *obj)
218 {
219 /* After EndTransformFeedback, it's likely that the client program will try
220 * to draw using the contents of the transform feedback buffer as vertex
221 * input. In order for this to work, we need to flush the data through at
222 * least the GS stage of the pipeline, and flush out the render cache. For
223 * simplicity, just do a full flush.
224 */
225 struct brw_context *brw = brw_context(ctx);
226 intel_batchbuffer_emit_mi_flush(brw);
227 }