i965: Stop using the kernel SOL reset feature.
[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 /* BRW_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 = 0,
73 .brw = (BRW_NEW_BATCH |
74 BRW_NEW_VERTEX_PROGRAM |
75 BRW_NEW_TRANSFORM_FEEDBACK),
76 .cache = 0
77 },
78 .emit = gen6_update_sol_surfaces,
79 };
80
81 /**
82 * Constructs the binding table for the WM surface state, which maps unit
83 * numbers to surface state objects.
84 */
85 static void
86 brw_gs_upload_binding_table(struct brw_context *brw)
87 {
88 struct gl_context *ctx = &brw->intel.ctx;
89 /* BRW_NEW_VERTEX_PROGRAM */
90 const struct gl_shader_program *shaderprog =
91 ctx->Shader.CurrentVertexProgram;
92 bool has_surfaces = false;
93 uint32_t *bind;
94
95 if (shaderprog) {
96 const struct gl_transform_feedback_info *linked_xfb_info =
97 &shaderprog->LinkedTransformFeedback;
98 /* Currently we only ever upload surfaces for SOL. */
99 has_surfaces = linked_xfb_info->NumOutputs != 0;
100 }
101
102 /* Skip making a binding table if we don't have anything to put in it. */
103 if (!has_surfaces) {
104 if (brw->gs.bind_bo_offset != 0) {
105 brw->state.dirty.brw |= BRW_NEW_GS_BINDING_TABLE;
106 brw->gs.bind_bo_offset = 0;
107 }
108 return;
109 }
110
111 /* Might want to calculate nr_surfaces first, to avoid taking up so much
112 * space for the binding table.
113 */
114 bind = brw_state_batch(brw, AUB_TRACE_BINDING_TABLE,
115 sizeof(uint32_t) * BRW_MAX_GS_SURFACES,
116 32, &brw->gs.bind_bo_offset);
117
118 /* BRW_NEW_SURFACES */
119 memcpy(bind, brw->gs.surf_offset, BRW_MAX_GS_SURFACES * sizeof(uint32_t));
120
121 brw->state.dirty.brw |= BRW_NEW_GS_BINDING_TABLE;
122 }
123
124 const struct brw_tracked_state gen6_gs_binding_table = {
125 .dirty = {
126 .mesa = 0,
127 .brw = (BRW_NEW_BATCH |
128 BRW_NEW_VERTEX_PROGRAM |
129 BRW_NEW_SURFACES),
130 .cache = 0
131 },
132 .emit = brw_gs_upload_binding_table,
133 };
134
135 static void
136 gen6_update_sol_indices(struct brw_context *brw)
137 {
138 struct intel_context *intel = &brw->intel;
139
140 BEGIN_BATCH(4);
141 OUT_BATCH(_3DSTATE_GS_SVB_INDEX << 16 | (4 - 2));
142 OUT_BATCH(0);
143 OUT_BATCH(brw->sol.svbi_0_starting_index); /* BRW_NEW_SOL_INDICES */
144 OUT_BATCH(brw->sol.svbi_0_max_index); /* BRW_NEW_SOL_INDICES */
145 ADVANCE_BATCH();
146 }
147
148 const struct brw_tracked_state gen6_sol_indices = {
149 .dirty = {
150 .mesa = 0,
151 .brw = (BRW_NEW_CONTEXT |
152 BRW_NEW_SOL_INDICES),
153 .cache = 0
154 },
155 .emit = gen6_update_sol_indices,
156 };
157
158 void
159 brw_begin_transform_feedback(struct gl_context *ctx, GLenum mode,
160 struct gl_transform_feedback_object *obj)
161 {
162 struct brw_context *brw = brw_context(ctx);
163 struct intel_context *intel = &brw->intel;
164 const struct gl_shader_program *vs_prog =
165 ctx->Shader.CurrentVertexProgram;
166 const struct gl_transform_feedback_info *linked_xfb_info =
167 &vs_prog->LinkedTransformFeedback;
168 struct gl_transform_feedback_object *xfb_obj =
169 ctx->TransformFeedback.CurrentObject;
170
171 /* Compute the maximum number of vertices that we can write without
172 * overflowing any of the buffers currently being used for feedback.
173 */
174 unsigned max_index
175 = _mesa_compute_max_transform_feedback_vertices(xfb_obj,
176 linked_xfb_info);
177
178 /* Initialize the SVBI 0 register to zero and set the maximum index.
179 * These values will be sent to the hardware on the next draw.
180 */
181 brw->state.dirty.brw |= BRW_NEW_SOL_INDICES;
182 brw->sol.svbi_0_starting_index = 0;
183 brw->sol.svbi_0_max_index = max_index;
184 brw->sol.offset_0_batch_start = 0;
185
186 if (intel->gen >= 7) {
187 /* Reset the SOL buffer offset register. */
188 for (int i = 0; i < 4; i++) {
189 BEGIN_BATCH(3);
190 OUT_BATCH(MI_LOAD_REGISTER_IMM | (3 - 2));
191 OUT_BATCH(GEN7_SO_WRITE_OFFSET(i));
192 OUT_BATCH(0);
193 ADVANCE_BATCH();
194 }
195 }
196 }
197
198 void
199 brw_end_transform_feedback(struct gl_context *ctx,
200 struct gl_transform_feedback_object *obj)
201 {
202 /* After EndTransformFeedback, it's likely that the client program will try
203 * to draw using the contents of the transform feedback buffer as vertex
204 * input. In order for this to work, we need to flush the data through at
205 * least the GS stage of the pipeline, and flush out the render cache. For
206 * simplicity, just do a full flush.
207 */
208 struct brw_context *brw = brw_context(ctx);
209 struct intel_context *intel = &brw->intel;
210 intel_batchbuffer_emit_mi_flush(intel);
211 }