i965/vs: Properly clear cur_value when propagating direct copies.
[mesa.git] / src / mesa / drivers / dri / i965 / gen7_sol_state.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 /**
25 * @file gen7_sol_state.c
26 *
27 * Controls the stream output logic (SOL) stage of the gen7 hardware, which is
28 * used to implement GL_EXT_transform_feedback.
29 */
30
31 #include "brw_context.h"
32 #include "brw_state.h"
33 #include "brw_defines.h"
34 #include "intel_batchbuffer.h"
35 #include "intel_buffer_objects.h"
36
37 static void
38 upload_3dstate_so_buffers(struct brw_context *brw)
39 {
40 struct intel_context *intel = &brw->intel;
41 struct gl_context *ctx = &intel->ctx;
42 /* BRW_NEW_VERTEX_PROGRAM */
43 const struct gl_shader_program *vs_prog =
44 ctx->Shader.CurrentVertexProgram;
45 const struct gl_transform_feedback_info *linked_xfb_info =
46 &vs_prog->LinkedTransformFeedback;
47 /* _NEW_TRANSFORM_FEEDBACK */
48 struct gl_transform_feedback_object *xfb_obj =
49 ctx->TransformFeedback.CurrentObject;
50 int i;
51
52 /* Set up the up to 4 output buffers. These are the ranges defined in the
53 * gl_transform_feedback_object.
54 */
55 for (i = 0; i < 4; i++) {
56 struct gl_buffer_object *bufferobj = xfb_obj->Buffers[i];
57 drm_intel_bo *bo;
58 uint32_t start, end;
59
60 if (!xfb_obj->Buffers[i]) {
61 /* The pitch of 0 in this command indicates that the buffer is
62 * unbound and won't be written to.
63 */
64 BEGIN_BATCH(4);
65 OUT_BATCH(_3DSTATE_SO_BUFFER << 16 | (4 - 2));
66 OUT_BATCH((i << SO_BUFFER_INDEX_SHIFT));
67 OUT_BATCH(0);
68 OUT_BATCH(0);
69 ADVANCE_BATCH();
70
71 continue;
72 }
73
74 bo = intel_buffer_object(bufferobj)->buffer;
75
76 start = xfb_obj->Offset[i];
77 assert(start % 4 == 0);
78 end = ALIGN(start + xfb_obj->Size[i], 4);
79 assert(end <= bo->size);
80
81 BEGIN_BATCH(4);
82 OUT_BATCH(_3DSTATE_SO_BUFFER << 16 | (4 - 2));
83 OUT_BATCH((i << SO_BUFFER_INDEX_SHIFT) |
84 ((linked_xfb_info->BufferStride[i] * 4) <<
85 SO_BUFFER_PITCH_SHIFT));
86 OUT_RELOC(bo, I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER, start);
87 OUT_RELOC(bo, I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER, end);
88 ADVANCE_BATCH();
89 }
90 }
91
92 /**
93 * Outputs the 3DSTATE_SO_DECL_LIST command.
94 *
95 * The data output is a series of 64-bit entries containing a SO_DECL per
96 * stream. We only have one stream of rendering coming out of the GS unit, so
97 * we only emit stream 0 (low 16 bits) SO_DECLs.
98 */
99 static void
100 upload_3dstate_so_decl_list(struct brw_context *brw,
101 struct brw_vue_map *vue_map)
102 {
103 struct intel_context *intel = &brw->intel;
104 struct gl_context *ctx = &intel->ctx;
105 /* BRW_NEW_VERTEX_PROGRAM */
106 const struct gl_shader_program *vs_prog =
107 ctx->Shader.CurrentVertexProgram;
108 /* _NEW_TRANSFORM_FEEDBACK */
109 const struct gl_transform_feedback_info *linked_xfb_info =
110 &vs_prog->LinkedTransformFeedback;
111 int i;
112 uint16_t so_decl[128];
113 int buffer_mask = 0;
114 int next_offset[4] = {0, 0, 0, 0};
115
116 STATIC_ASSERT(ARRAY_SIZE(so_decl) >= MAX_PROGRAM_OUTPUTS);
117
118 /* Construct the list of SO_DECLs to be emitted. The formatting of the
119 * command is feels strange -- each dword pair contains a SO_DECL per stream.
120 */
121 for (i = 0; i < linked_xfb_info->NumOutputs; i++) {
122 int buffer = linked_xfb_info->Outputs[i].OutputBuffer;
123 uint16_t decl = 0;
124 int vert_result = linked_xfb_info->Outputs[i].OutputRegister;
125
126 buffer_mask |= 1 << buffer;
127
128 decl |= buffer << SO_DECL_OUTPUT_BUFFER_SLOT_SHIFT;
129 decl |= vue_map->vert_result_to_slot[vert_result] <<
130 SO_DECL_REGISTER_INDEX_SHIFT;
131 decl |= ((1 << linked_xfb_info->Outputs[i].NumComponents) - 1) <<
132 SO_DECL_COMPONENT_MASK_SHIFT;
133
134 /* This assert should be true until GL_ARB_transform_feedback_instanced
135 * is added and we start using the hole flag.
136 */
137 assert(linked_xfb_info->Outputs[i].DstOffset == next_offset[buffer]);
138
139 next_offset[buffer] += linked_xfb_info->Outputs[i].NumComponents;
140
141 so_decl[i] = decl;
142 }
143
144 BEGIN_BATCH(linked_xfb_info->NumOutputs * 2 + 3);
145 OUT_BATCH(_3DSTATE_SO_DECL_LIST << 16 |
146 (linked_xfb_info->NumOutputs * 2 + 1));
147
148 OUT_BATCH((buffer_mask << SO_STREAM_TO_BUFFER_SELECTS_0_SHIFT) |
149 (0 << SO_STREAM_TO_BUFFER_SELECTS_1_SHIFT) |
150 (0 << SO_STREAM_TO_BUFFER_SELECTS_2_SHIFT) |
151 (0 << SO_STREAM_TO_BUFFER_SELECTS_3_SHIFT));
152
153 OUT_BATCH((linked_xfb_info->NumOutputs << SO_NUM_ENTRIES_0_SHIFT) |
154 (0 << SO_NUM_ENTRIES_1_SHIFT) |
155 (0 << SO_NUM_ENTRIES_2_SHIFT) |
156 (0 << SO_NUM_ENTRIES_3_SHIFT));
157
158 for (i = 0; i < linked_xfb_info->NumOutputs; i++) {
159 OUT_BATCH(so_decl[i]);
160 OUT_BATCH(0);
161 }
162
163 ADVANCE_BATCH();
164 }
165
166 static void
167 upload_3dstate_streamout(struct brw_context *brw, bool active,
168 struct brw_vue_map *vue_map)
169 {
170 struct intel_context *intel = &brw->intel;
171 struct gl_context *ctx = &intel->ctx;
172 /* _NEW_TRANSFORM_FEEDBACK */
173 struct gl_transform_feedback_object *xfb_obj =
174 ctx->TransformFeedback.CurrentObject;
175 uint32_t dw1 = 0, dw2 = 0;
176 int i;
177
178 /* _NEW_RASTERIZER_DISCARD */
179 if (ctx->RasterDiscard)
180 dw1 |= SO_RENDERING_DISABLE;
181
182 if (active) {
183 int urb_entry_read_offset = 0;
184 int urb_entry_read_length = (vue_map->num_slots + 1) / 2 -
185 urb_entry_read_offset;
186
187 dw1 |= SO_FUNCTION_ENABLE;
188 dw1 |= SO_STATISTICS_ENABLE;
189
190 /* _NEW_LIGHT */
191 if (ctx->Light.ProvokingVertex != GL_FIRST_VERTEX_CONVENTION)
192 dw1 |= SO_REORDER_TRAILING;
193
194 for (i = 0; i < 4; i++) {
195 if (xfb_obj->Buffers[i]) {
196 dw1 |= SO_BUFFER_ENABLE(i);
197 }
198 }
199
200 /* We always read the whole vertex. This could be reduced at some
201 * point by reading less and offsetting the register index in the
202 * SO_DECLs.
203 */
204 dw2 |= urb_entry_read_offset << SO_STREAM_0_VERTEX_READ_OFFSET_SHIFT;
205 dw2 |= (urb_entry_read_length - 1) <<
206 SO_STREAM_0_VERTEX_READ_LENGTH_SHIFT;
207 }
208
209 BEGIN_BATCH(3);
210 OUT_BATCH(_3DSTATE_STREAMOUT << 16 | (3 - 2));
211 OUT_BATCH(dw1);
212 OUT_BATCH(dw2);
213 ADVANCE_BATCH();
214 }
215
216 static void
217 upload_sol_state(struct brw_context *brw)
218 {
219 struct intel_context *intel = &brw->intel;
220 struct gl_context *ctx = &intel->ctx;
221 /* _NEW_TRANSFORM_FEEDBACK */
222 struct gl_transform_feedback_object *xfb_obj =
223 ctx->TransformFeedback.CurrentObject;
224 bool active = xfb_obj->Active && !xfb_obj->Paused;
225 struct brw_vue_map vue_map;
226
227 /* _NEW_TRANSFORM, CACHE_NEW_VS_PROG */
228 brw_compute_vue_map(&vue_map, intel, ctx->Transform.ClipPlanesEnabled != 0,
229 brw->vs.prog_data->outputs_written);
230
231 if (active) {
232 upload_3dstate_so_buffers(brw);
233 upload_3dstate_so_decl_list(brw, &vue_map);
234 }
235
236 /* Finally, set up the SOL stage. This command must always follow updates to
237 * the nonpipelined SOL state (3DSTATE_SO_BUFFER, 3DSTATE_SO_DECL_LIST) or
238 * MMIO register updates (current performed by the kernel at each batch
239 * emit).
240 */
241 upload_3dstate_streamout(brw, active, &vue_map);
242 }
243
244 const struct brw_tracked_state gen7_sol_state = {
245 .dirty = {
246 .mesa = (_NEW_RASTERIZER_DISCARD |
247 _NEW_LIGHT |
248 _NEW_TRANSFORM_FEEDBACK |
249 _NEW_TRANSFORM),
250 .brw = (BRW_NEW_BATCH |
251 BRW_NEW_VERTEX_PROGRAM),
252 .cache = CACHE_NEW_VS_PROG,
253 },
254 .emit = upload_sol_state,
255 };