draw: add stream output decomposition file
[mesa.git] / src / gallium / auxiliary / draw / draw_pt_so_emit.c
1 /**************************************************************************
2 *
3 * Copyright 2010 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "draw/draw_context.h"
29 #include "draw/draw_private.h"
30 #include "draw/draw_vbuf.h"
31 #include "draw/draw_vertex.h"
32 #include "draw/draw_pt.h"
33
34 #include "util/u_math.h"
35 #include "util/u_memory.h"
36
37 struct pt_so_emit {
38 struct draw_context *draw;
39
40 void *buffers[PIPE_MAX_SO_BUFFERS];
41
42 unsigned input_vertex_stride;
43 const float (*inputs)[4];
44
45 boolean has_so;
46
47 boolean single_buffer;
48
49 unsigned emitted_primitives;
50 unsigned emitted_vertices;
51 };
52
53
54 void draw_pt_so_emit_prepare(struct pt_so_emit *emit)
55 {
56 struct draw_context *draw = emit->draw;
57
58 emit->has_so = (draw->so.state.num_outputs > 0);
59
60 if (!emit->has_so)
61 return;
62
63 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
64 */
65 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
66 }
67
68 static boolean
69 is_component_writable(unsigned mask,
70 unsigned compo)
71 {
72 switch (mask) {
73 case TGSI_WRITEMASK_NONE:
74 return FALSE;
75 case TGSI_WRITEMASK_X:
76 return compo == 0;
77 case TGSI_WRITEMASK_Y:
78 return compo == 1;
79 case TGSI_WRITEMASK_XY:
80 return compo == 0 || compo == 1;
81 case TGSI_WRITEMASK_Z:
82 return compo == 2;
83 case TGSI_WRITEMASK_XZ:
84 return compo == 0 || compo == 2;
85 case TGSI_WRITEMASK_YZ:
86 return compo == 1 || compo == 2;
87 case TGSI_WRITEMASK_XYZ:
88 return compo == 0 || compo == 1 || compo == 2;
89 case TGSI_WRITEMASK_W:
90 return compo == 3;
91 case TGSI_WRITEMASK_XW:
92 return compo == 0 || compo == 3;
93 case TGSI_WRITEMASK_YW:
94 return compo == 1 || compo == 3;
95 case TGSI_WRITEMASK_XYW:
96 return compo == 0 || compo == 1 || compo == 3;
97 case TGSI_WRITEMASK_ZW:
98 return compo == 2 || compo == 3;
99 case TGSI_WRITEMASK_XZW:
100 return compo == 0 || compo == 1 || compo == 3;
101 case TGSI_WRITEMASK_YZW:
102 return compo == 1 || compo == 2 || compo == 4;
103 case TGSI_WRITEMASK_XYZW:
104 return compo >= 0 && compo < 4;
105 default:
106 debug_assert(!"Unknown writemask in stream out");
107 return compo >= 0 && compo < 4;
108 }
109 }
110
111 static void so_emit_prim(struct pt_so_emit *so,
112 unsigned *indices,
113 unsigned num_vertices)
114 {
115 unsigned slot, i;
116 unsigned input_vertex_stride = so->input_vertex_stride;
117 struct draw_context *draw = so->draw;
118 const float (*input_ptr)[4];
119 const struct pipe_stream_output_state *state =
120 &draw->so.state;
121 float **buffer = 0;
122
123 input_ptr = so->inputs;
124
125 for (i = 0; i < num_vertices; ++i) {
126 const float (*input)[4];
127 /*debug_printf("%d) vertex index = %d (prim idx = %d)\n", i, indices[i], prim_idx);*/
128 input = (const float (*)[4])(
129 (const char *)input_ptr + (indices[i] * input_vertex_stride));
130 for (slot = 0; slot < state->num_outputs; ++slot) {
131 unsigned idx = state->register_index[slot];
132 unsigned writemask = state->register_mask[slot];
133 unsigned compo;
134 unsigned written_compos = 0;
135
136 buffer = (float**)&so->buffers[state->output_buffer[slot]];
137
138 /*debug_printf("\tSlot = %d, vs_slot = %d, idx = %d:\n",
139 slot, vs_slot, idx);*/
140 #if 1
141 assert(!util_is_inf_or_nan(input[idx][0]));
142 assert(!util_is_inf_or_nan(input[idx][1]));
143 assert(!util_is_inf_or_nan(input[idx][2]));
144 assert(!util_is_inf_or_nan(input[idx][3]));
145 #endif
146 for (compo = 0; compo < 4; ++compo) {
147 if (is_component_writable(writemask, compo)) {
148 float *buf = *buffer;
149 buf[written_compos++] = input[idx][compo];
150 }
151 }
152 #if 0
153 debug_printf("\t\t(writemask = %d)%f %f %f %f\n",
154 writemask,
155 input[idx][0],
156 input[idx][1],
157 input[idx][2],
158 input[idx][3]);
159 #endif
160 if (!so->single_buffer)
161 *buffer += written_compos;
162 }
163 if (so->single_buffer)
164 *buffer = (float*) (((char*)*buffer) + state->stride);
165 }
166 so->emitted_vertices += num_vertices;
167 ++so->emitted_primitives;
168 }
169
170 static void so_point(struct pt_so_emit *so, int idx)
171 {
172 unsigned indices[1];
173
174 indices[0] = idx;
175
176 so_emit_prim(so, indices, 1);
177 }
178
179 static void so_line(struct pt_so_emit *so, int i0, int i1)
180 {
181 unsigned indices[2];
182
183 indices[0] = i0;
184 indices[1] = i1;
185
186 so_emit_prim(so, indices, 2);
187 }
188
189 static void so_tri(struct pt_so_emit *so, int i0, int i1, int i2)
190 {
191 unsigned indices[3];
192
193 indices[0] = i0;
194 indices[1] = i1;
195 indices[2] = i2;
196
197 so_emit_prim(so, indices, 3);
198 }
199
200
201 #define TRIANGLE(gs,i0,i1,i2) so_tri(so,i0,i1,i2)
202 #define LINE(gs,i0,i1) so_line(so,i0,i1)
203 #define POINT(gs,i0) so_point(so,i0)
204 #define FUNC so_run_linear
205 #define LOCAL_VARS
206 #include "draw_so_emit_tmp.h"
207 #undef LOCAL_VARS
208 #undef FUNC
209
210
211 #define TRIANGLE(gs,i0,i1,i2) so_tri(gs,elts[i0],elts[i1],elts[i2])
212 #define LINE(gs,i0,i1) so_line(gs,elts[i0],elts[i1])
213 #define POINT(gs,i0) so_point(gs,elts[i0])
214 #define FUNC so_run_elts
215 #define LOCAL_VARS \
216 const ushort *elts = input_prims->elts;
217 #include "draw_so_emit_tmp.h"
218 #undef LOCAL_VARS
219 #undef FUNC
220
221
222 void draw_pt_so_emit( struct pt_so_emit *emit,
223 const struct draw_vertex_info *input_verts,
224 const struct draw_prim_info *input_prims )
225 {
226 struct draw_context *draw = emit->draw;
227 struct vbuf_render *render = draw->render;
228 unsigned start, i;
229
230 if (!emit->has_so)
231 return;
232
233 emit->emitted_vertices = 0;
234 emit->emitted_primitives = 0;
235 emit->input_vertex_stride = input_verts->stride;
236 emit->inputs = (const float (*)[4])input_verts->verts->data;
237 for (i = 0; i < draw->so.num_buffers; ++i)
238 emit->buffers[i] = draw->so.buffers[i];
239 emit->single_buffer = TRUE;
240 for (i = 0; i < draw->so.state.num_outputs; ++i) {
241 if (draw->so.state.output_buffer[i] != 0)
242 emit->single_buffer = FALSE;
243 }
244
245 /* XXX: need to flush to get prim_vbuf.c to release its allocation??*/
246 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
247
248 for (start = i = 0; i < input_prims->primitive_count;
249 start += input_prims->primitive_lengths[i], i++)
250 {
251 unsigned count = input_prims->primitive_lengths[i];
252
253 if (input_prims->linear) {
254 so_run_linear(emit, input_prims, input_verts,
255 start, count);
256 } else {
257 so_run_elts(emit, input_prims, input_verts,
258 start, count);
259 }
260 }
261
262 render->set_stream_output_info(render,
263 emit->emitted_primitives,
264 emit->emitted_vertices);
265 }
266
267
268 struct pt_so_emit *draw_pt_so_emit_create( struct draw_context *draw )
269 {
270 struct pt_so_emit *emit = CALLOC_STRUCT(pt_so_emit);
271 if (!emit)
272 return NULL;
273
274 emit->draw = draw;
275
276 return emit;
277 }
278
279 void draw_pt_so_emit_destroy( struct pt_so_emit *emit )
280 {
281 FREE(emit);
282 }