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