• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_gs.h"
31 #include "draw/draw_tess.h"
32 #include "draw/draw_context.h"
33 #include "draw/draw_vbuf.h"
34 #include "draw/draw_vertex.h"
35 #include "draw/draw_pt.h"
36 
37 #include "pipe/p_state.h"
38 
39 #include "util/u_math.h"
40 #include "util/u_prim.h"
41 #include "util/u_memory.h"
42 
43 struct pt_so_emit {
44    struct draw_context *draw;
45 
46    unsigned input_vertex_stride;
47    const float (*inputs)[4];
48    const float *pre_clip_pos;
49    boolean has_so;
50    boolean use_pre_clip_pos;
51    int pos_idx;
52    unsigned emitted_primitives;
53    unsigned generated_primitives;
54    unsigned stream;
55 };
56 
57 static const struct pipe_stream_output_info *
draw_so_info(const struct draw_context * draw)58 draw_so_info(const struct draw_context *draw)
59 {
60    const struct pipe_stream_output_info *state = NULL;
61 
62    if (draw->gs.geometry_shader) {
63       state = &draw->gs.geometry_shader->state.stream_output;
64    } else if (draw->tes.tess_eval_shader) {
65       state = &draw->tes.tess_eval_shader->state.stream_output;
66    } else {
67       state = &draw->vs.vertex_shader->state.stream_output;
68    }
69 
70    return state;
71 }
72 
73 static inline boolean
draw_has_so(const struct draw_context * draw)74 draw_has_so(const struct draw_context *draw)
75 {
76    const struct pipe_stream_output_info *state = draw_so_info(draw);
77 
78    if (state && state->num_outputs > 0)
79       return TRUE;
80 
81    return FALSE;
82 }
83 
draw_pt_so_emit_prepare(struct pt_so_emit * emit,boolean use_pre_clip_pos)84 void draw_pt_so_emit_prepare(struct pt_so_emit *emit, boolean use_pre_clip_pos)
85 {
86    struct draw_context *draw = emit->draw;
87 
88    emit->use_pre_clip_pos = use_pre_clip_pos;
89    emit->has_so = draw_has_so(draw);
90    if (use_pre_clip_pos)
91       emit->pos_idx = draw_current_shader_position_output(draw);
92 
93    /* if we have a state with outputs make sure we have
94     * buffers to output to */
95    if (emit->has_so) {
96       boolean has_valid_buffer = FALSE;
97       unsigned i;
98       for (i = 0; i < draw->so.num_targets; ++i) {
99          if (draw->so.targets[i]) {
100             has_valid_buffer = TRUE;
101             break;
102          }
103       }
104       emit->has_so = has_valid_buffer;
105    }
106 
107    if (!emit->has_so)
108       return;
109 
110    /* XXX: need to flush to get prim_vbuf.c to release its allocation??
111     */
112    draw_do_flush( draw, DRAW_FLUSH_BACKEND );
113 }
114 
so_emit_prim(struct pt_so_emit * so,unsigned * indices,unsigned num_vertices)115 static void so_emit_prim(struct pt_so_emit *so,
116                          unsigned *indices,
117                          unsigned num_vertices)
118 {
119    unsigned slot, i;
120    unsigned input_vertex_stride = so->input_vertex_stride;
121    struct draw_context *draw = so->draw;
122    const float (*input_ptr)[4];
123    const float *pcp_ptr = NULL;
124    const struct pipe_stream_output_info *state = draw_so_info(draw);
125    float *buffer;
126    int buffer_total_bytes[PIPE_MAX_SO_BUFFERS];
127    boolean buffer_written[PIPE_MAX_SO_BUFFERS] = {0};
128 
129    input_ptr = so->inputs;
130    if (so->use_pre_clip_pos)
131       pcp_ptr = so->pre_clip_pos;
132 
133    ++so->generated_primitives;
134 
135    for (i = 0; i < draw->so.num_targets; i++) {
136       struct draw_so_target *target = draw->so.targets[i];
137       if (target) {
138          buffer_total_bytes[i] = target->internal_offset;
139       } else {
140          buffer_total_bytes[i] = 0;
141       }
142    }
143 
144    /* check have we space to emit prim first - if not don't do anything */
145    for (i = 0; i < num_vertices; ++i) {
146       unsigned ob;
147       for (slot = 0; slot < state->num_outputs; ++slot) {
148          unsigned num_comps = state->output[slot].num_components;
149          int ob = state->output[slot].output_buffer;
150          unsigned dst_offset = state->output[slot].dst_offset * sizeof(float);
151          unsigned write_size = num_comps * sizeof(float);
152 
153          if (state->output[slot].stream != so->stream)
154             continue;
155          /* If a buffer is missing then that's equivalent to
156           * an overflow */
157          if (!draw->so.targets[ob]) {
158             return;
159          }
160          if ((buffer_total_bytes[ob] + write_size + dst_offset) >
161              draw->so.targets[ob]->target.buffer_size) {
162             return;
163          }
164       }
165       for (ob = 0; ob < draw->so.num_targets; ++ob) {
166          buffer_total_bytes[ob] += state->stride[ob] * sizeof(float);
167       }
168    }
169 
170    for (i = 0; i < num_vertices; ++i) {
171       const float (*input)[4];
172       const float *pre_clip_pos = NULL;
173       unsigned  ob;
174 
175       input = (const float (*)[4])(
176          (const char *)input_ptr + (indices[i] * input_vertex_stride));
177 
178       if (pcp_ptr)
179          pre_clip_pos = (const float *)(
180          (const char *)pcp_ptr + (indices[i] * input_vertex_stride));
181 
182       for (slot = 0; slot < state->num_outputs; ++slot) {
183          unsigned idx = state->output[slot].register_index;
184          unsigned start_comp = state->output[slot].start_component;
185          unsigned num_comps = state->output[slot].num_components;
186          unsigned stream = state->output[slot].stream;
187 
188          if (stream != so->stream)
189             continue;
190          ob = state->output[slot].output_buffer;
191          buffer_written[ob] = TRUE;
192 
193          buffer = (float *)((char *)draw->so.targets[ob]->mapping +
194                             draw->so.targets[ob]->target.buffer_offset +
195                             draw->so.targets[ob]->internal_offset) +
196             state->output[slot].dst_offset;
197 
198          if (idx == so->pos_idx && pcp_ptr && so->stream == 0)
199             memcpy(buffer, &pre_clip_pos[start_comp],
200                    num_comps * sizeof(float));
201          else
202             memcpy(buffer, &input[idx][start_comp],
203                    num_comps * sizeof(float));
204 #if 0
205          {
206             int j;
207             debug_printf("VERT[%d], stream = %d, offset = %d, slot[%d] sc = %d, num_c = %d, idx = %d = [",
208                          i, stream,
209                          draw->so.targets[ob]->internal_offset,
210                          slot, start_comp, num_comps, idx);
211             for (j = 0; j < num_comps; ++j) {
212                unsigned *ubuffer = (unsigned*)buffer;
213                debug_printf("%d (0x%x), ", ubuffer[j], ubuffer[j]);
214             }
215             debug_printf("]\n");
216          }
217 #endif
218       }
219       for (ob = 0; ob < draw->so.num_targets; ++ob) {
220          struct draw_so_target *target = draw->so.targets[ob];
221          if (target && buffer_written[ob]) {
222             target->internal_offset += state->stride[ob] * sizeof(float);
223          }
224       }
225    }
226    ++so->emitted_primitives;
227 }
228 
so_point(struct pt_so_emit * so,int idx)229 static void so_point(struct pt_so_emit *so, int idx)
230 {
231    unsigned indices[1];
232 
233    indices[0] = idx;
234 
235    so_emit_prim(so, indices, 1);
236 }
237 
so_line(struct pt_so_emit * so,int i0,int i1)238 static void so_line(struct pt_so_emit *so, int i0, int i1)
239 {
240    unsigned indices[2];
241 
242    indices[0] = i0;
243    indices[1] = i1;
244 
245    so_emit_prim(so, indices, 2);
246 }
247 
so_tri(struct pt_so_emit * so,int i0,int i1,int i2)248 static void so_tri(struct pt_so_emit *so, int i0, int i1, int i2)
249 {
250    unsigned indices[3];
251 
252    indices[0] = i0;
253    indices[1] = i1;
254    indices[2] = i2;
255 
256    so_emit_prim(so, indices, 3);
257 }
258 
259 
260 #define FUNC         so_run_linear
261 #define GET_ELT(idx) (start + (idx))
262 #include "draw_so_emit_tmp.h"
263 
264 
265 #define FUNC         so_run_elts
266 #define LOCAL_VARS   const ushort *elts = input_prims->elts;
267 #define GET_ELT(idx) (elts[start + (idx)])
268 #include "draw_so_emit_tmp.h"
269 
270 
draw_pt_so_emit(struct pt_so_emit * emit,int num_vertex_streams,const struct draw_vertex_info * input_verts,const struct draw_prim_info * input_prims)271 void draw_pt_so_emit( struct pt_so_emit *emit,
272                       int num_vertex_streams,
273                       const struct draw_vertex_info *input_verts,
274                       const struct draw_prim_info *input_prims )
275 {
276    struct draw_context *draw = emit->draw;
277    struct vbuf_render *render = draw->render;
278    unsigned start, i, stream;
279 
280    if (!emit->has_so) {
281       if (draw->collect_primgen) {
282          unsigned i;
283          unsigned total = 0;
284          for (i = 0; i < input_prims->primitive_count; i++) {
285             total +=
286                u_decomposed_prims_for_vertices(input_prims->prim,
287                                                input_prims->primitive_lengths[i]);
288          }
289          render->set_stream_output_info(render,
290                                         0, 0, total);
291       }
292       return;
293    }
294 
295    if (!draw->so.num_targets)
296       return;
297 
298    /* XXX: need to flush to get prim_vbuf.c to release its allocation??*/
299    draw_do_flush( draw, DRAW_FLUSH_BACKEND );
300 
301    for (stream = 0; stream < num_vertex_streams; stream++) {
302       emit->emitted_primitives = 0;
303       emit->generated_primitives = 0;
304       if (emit->use_pre_clip_pos)
305          emit->pre_clip_pos = input_verts[stream].verts->clip_pos;
306 
307       emit->input_vertex_stride = input_verts[stream].stride;
308       emit->inputs = (const float (*)[4])input_verts[stream].verts->data;
309       emit->stream = stream;
310       for (start = i = 0; i < input_prims[stream].primitive_count;
311            start += input_prims[stream].primitive_lengths[i], i++)
312       {
313          unsigned count = input_prims[stream].primitive_lengths[i];
314 
315          if (input_prims->linear) {
316             so_run_linear(emit, &input_prims[stream], &input_verts[stream],
317                           start, count);
318          } else {
319             so_run_elts(emit, &input_prims[stream], &input_verts[stream],
320                         start, count);
321          }
322       }
323       render->set_stream_output_info(render,
324                                      stream,
325                                      emit->emitted_primitives,
326                                      emit->generated_primitives);
327    }
328 }
329 
330 
draw_pt_so_emit_create(struct draw_context * draw)331 struct pt_so_emit *draw_pt_so_emit_create( struct draw_context *draw )
332 {
333    struct pt_so_emit *emit = CALLOC_STRUCT(pt_so_emit);
334    if (!emit)
335       return NULL;
336 
337    emit->draw = draw;
338 
339    return emit;
340 }
341 
draw_pt_so_emit_destroy(struct pt_so_emit * emit)342 void draw_pt_so_emit_destroy( struct pt_so_emit *emit )
343 {
344    FREE(emit);
345 }
346