draw: add support for later transform feedback extensions
[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_private.h"
29 #include "draw/draw_vs.h"
30 #include "draw/draw_context.h"
31 #include "draw/draw_vbuf.h"
32 #include "draw/draw_vertex.h"
33 #include "draw/draw_pt.h"
34
35 #include "pipe/p_state.h"
36
37 #include "util/u_math.h"
38 #include "util/u_memory.h"
39
40 struct pt_so_emit {
41 struct draw_context *draw;
42
43 unsigned input_vertex_stride;
44 const float (*inputs)[4];
45
46 boolean has_so;
47
48 unsigned emitted_primitives;
49 unsigned emitted_vertices;
50 unsigned generated_primitives;
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->vs.vertex_shader->state.stream_output.num_outputs > 0);
59
60 /* if we have a state with outputs make sure we have
61 * buffers to output to */
62 if (emit->has_so) {
63 boolean has_valid_buffer = FALSE;
64 unsigned i;
65 for (i = 0; i < draw->so.num_targets; ++i) {
66 if (draw->so.targets[i]) {
67 has_valid_buffer = TRUE;
68 break;
69 }
70 }
71 emit->has_so = has_valid_buffer;
72 }
73
74 if (!emit->has_so)
75 return;
76
77 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
78 */
79 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
80 }
81
82 static void so_emit_prim(struct pt_so_emit *so,
83 unsigned *indices,
84 unsigned num_vertices)
85 {
86 unsigned slot, i;
87 unsigned input_vertex_stride = so->input_vertex_stride;
88 struct draw_context *draw = so->draw;
89 const float (*input_ptr)[4];
90 const struct pipe_stream_output_info *state =
91 &draw->vs.vertex_shader->state.stream_output;
92 float *buffer;
93 int buffer_total_bytes[PIPE_MAX_SO_BUFFERS];
94
95 input_ptr = so->inputs;
96
97 ++so->generated_primitives;
98
99 for (i = 0; i < draw->so.num_targets; i++) {
100 struct draw_so_target *target = draw->so.targets[i];
101 buffer_total_bytes[i] = target->internal_offset;
102 }
103
104 /* check have we space to emit prim first - if not don't do anything */
105 for (i = 0; i < num_vertices; ++i) {
106 for (slot = 0; slot < state->num_outputs; ++slot) {
107 unsigned num_comps = state->output[slot].num_components;
108 int ob = state->output[slot].output_buffer;
109
110 if ((buffer_total_bytes[ob] + num_comps * sizeof(float)) >
111 draw->so.targets[ob]->target.buffer_size) {
112 return;
113 }
114 buffer_total_bytes[ob] += num_comps * sizeof(float);
115 }
116 }
117
118 for (i = 0; i < num_vertices; ++i) {
119 const float (*input)[4];
120 unsigned total_written_compos = 0;
121 int ob;
122 /*debug_printf("%d) vertex index = %d (prim idx = %d)\n", i, indices[i], prim_idx);*/
123 input = (const float (*)[4])(
124 (const char *)input_ptr + (indices[i] * input_vertex_stride));
125
126 for (slot = 0; slot < state->num_outputs; ++slot) {
127 unsigned idx = state->output[slot].register_index;
128 unsigned start_comp = state->output[slot].start_component;
129 unsigned num_comps = state->output[slot].num_components;
130
131 ob = state->output[slot].output_buffer;
132
133 buffer = (float *)((char *)draw->so.targets[ob]->mapping +
134 draw->so.targets[ob]->target.buffer_offset +
135 draw->so.targets[ob]->internal_offset) + state->output[slot].dst_offset;
136 memcpy(buffer, &input[idx][start_comp], num_comps * sizeof(float));
137 total_written_compos += num_comps;
138 }
139 for (ob = 0; ob < draw->so.num_targets; ++ob)
140 draw->so.targets[ob]->internal_offset += state->stride[ob] * sizeof(float);
141 }
142 so->emitted_vertices += num_vertices;
143 ++so->emitted_primitives;
144 }
145
146 static void so_point(struct pt_so_emit *so, int idx)
147 {
148 unsigned indices[1];
149
150 indices[0] = idx;
151
152 so_emit_prim(so, indices, 1);
153 }
154
155 static void so_line(struct pt_so_emit *so, int i0, int i1)
156 {
157 unsigned indices[2];
158
159 indices[0] = i0;
160 indices[1] = i1;
161
162 so_emit_prim(so, indices, 2);
163 }
164
165 static void so_tri(struct pt_so_emit *so, int i0, int i1, int i2)
166 {
167 unsigned indices[3];
168
169 indices[0] = i0;
170 indices[1] = i1;
171 indices[2] = i2;
172
173 so_emit_prim(so, indices, 3);
174 }
175
176
177 #define FUNC so_run_linear
178 #define GET_ELT(idx) (start + (idx))
179 #include "draw_so_emit_tmp.h"
180
181
182 #define FUNC so_run_elts
183 #define LOCAL_VARS const ushort *elts = input_prims->elts;
184 #define GET_ELT(idx) (elts[start + (idx)])
185 #include "draw_so_emit_tmp.h"
186
187
188 void draw_pt_so_emit( struct pt_so_emit *emit,
189 const struct draw_vertex_info *input_verts,
190 const struct draw_prim_info *input_prims )
191 {
192 struct draw_context *draw = emit->draw;
193 struct vbuf_render *render = draw->render;
194 unsigned start, i;
195
196 if (!emit->has_so)
197 return;
198
199 if (!draw->so.num_targets)
200 return;
201
202 emit->emitted_vertices = 0;
203 emit->emitted_primitives = 0;
204 emit->generated_primitives = 0;
205 emit->input_vertex_stride = input_verts->stride;
206 emit->inputs = (const float (*)[4])input_verts->verts->data;
207
208 /* XXX: need to flush to get prim_vbuf.c to release its allocation??*/
209 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
210
211 for (start = i = 0; i < input_prims->primitive_count;
212 start += input_prims->primitive_lengths[i], i++)
213 {
214 unsigned count = input_prims->primitive_lengths[i];
215
216 if (input_prims->linear) {
217 so_run_linear(emit, input_prims, input_verts,
218 start, count);
219 } else {
220 so_run_elts(emit, input_prims, input_verts,
221 start, count);
222 }
223 }
224
225 render->set_stream_output_info(render,
226 emit->emitted_primitives,
227 emit->emitted_vertices,
228 emit->generated_primitives);
229 }
230
231
232 struct pt_so_emit *draw_pt_so_emit_create( struct draw_context *draw )
233 {
234 struct pt_so_emit *emit = CALLOC_STRUCT(pt_so_emit);
235 if (!emit)
236 return NULL;
237
238 emit->draw = draw;
239
240 return emit;
241 }
242
243 void draw_pt_so_emit_destroy( struct pt_so_emit *emit )
244 {
245 FREE(emit);
246 }