13d8470e4ecb900e093155e6c5f9e86097f87e8d
1 /**************************************************************************
3 * Copyright 2010 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
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"
35 #include "pipe/p_state.h"
37 #include "util/u_math.h"
38 #include "util/u_memory.h"
41 struct draw_context
*draw
;
43 unsigned input_vertex_stride
;
44 const float (*inputs
)[4];
45 const float (*pre_clip_pos
)[4];
47 boolean use_pre_clip_pos
;
49 unsigned emitted_primitives
;
50 unsigned emitted_vertices
;
51 unsigned generated_primitives
;
55 void draw_pt_so_emit_prepare(struct pt_so_emit
*emit
, boolean use_pre_clip_pos
)
57 struct draw_context
*draw
= emit
->draw
;
59 emit
->use_pre_clip_pos
= use_pre_clip_pos
;
60 emit
->has_so
= (draw
->vs
.vertex_shader
->state
.stream_output
.num_outputs
> 0);
62 emit
->pos_idx
= draw_current_shader_position_output(draw
);
64 /* if we have a state with outputs make sure we have
65 * buffers to output to */
67 boolean has_valid_buffer
= FALSE
;
69 for (i
= 0; i
< draw
->so
.num_targets
; ++i
) {
70 if (draw
->so
.targets
[i
]) {
71 has_valid_buffer
= TRUE
;
75 emit
->has_so
= has_valid_buffer
;
81 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
83 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
86 static void so_emit_prim(struct pt_so_emit
*so
,
88 unsigned num_vertices
)
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
;
98 int buffer_total_bytes
[PIPE_MAX_SO_BUFFERS
];
100 input_ptr
= so
->inputs
;
101 if (so
->use_pre_clip_pos
)
102 pcp_ptr
= so
->pre_clip_pos
;
104 ++so
->generated_primitives
;
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
;
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
;
117 if ((buffer_total_bytes
[ob
] + num_comps
* sizeof(float)) >
118 draw
->so
.targets
[ob
]->target
.buffer_size
) {
121 buffer_total_bytes
[ob
] += num_comps
* sizeof(float);
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;
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
));
135 pre_clip_pos
= (const float (*)[4])(
136 (const char *)pcp_ptr
+ (indices
[i
] * input_vertex_stride
));
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
;
143 ob
= state
->output
[slot
].output_buffer
;
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));
151 memcpy(buffer
, &input
[idx
][start_comp
], num_comps
* sizeof(float));
152 total_written_compos
+= num_comps
;
154 for (ob
= 0; ob
< draw
->so
.num_targets
; ++ob
)
155 draw
->so
.targets
[ob
]->internal_offset
+= state
->stride
[ob
] * sizeof(float);
157 so
->emitted_vertices
+= num_vertices
;
158 ++so
->emitted_primitives
;
161 static void so_point(struct pt_so_emit
*so
, int idx
)
167 so_emit_prim(so
, indices
, 1);
170 static void so_line(struct pt_so_emit
*so
, int i0
, int i1
)
177 so_emit_prim(so
, indices
, 2);
180 static void so_tri(struct pt_so_emit
*so
, int i0
, int i1
, int i2
)
188 so_emit_prim(so
, indices
, 3);
192 #define FUNC so_run_linear
193 #define GET_ELT(idx) (start + (idx))
194 #include "draw_so_emit_tmp.h"
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"
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
)
207 struct draw_context
*draw
= emit
->draw
;
208 struct vbuf_render
*render
= draw
->render
;
214 if (!draw
->so
.num_targets
)
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
;
224 emit
->inputs
= (const float (*)[4])input_verts
->verts
->data
;
226 /* XXX: need to flush to get prim_vbuf.c to release its allocation??*/
227 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
229 for (start
= i
= 0; i
< input_prims
->primitive_count
;
230 start
+= input_prims
->primitive_lengths
[i
], i
++)
232 unsigned count
= input_prims
->primitive_lengths
[i
];
234 if (input_prims
->linear
) {
235 so_run_linear(emit
, input_prims
, input_verts
,
238 so_run_elts(emit
, input_prims
, input_verts
,
243 render
->set_stream_output_info(render
,
244 emit
->emitted_primitives
,
245 emit
->emitted_vertices
,
246 emit
->generated_primitives
);
250 struct pt_so_emit
*draw_pt_so_emit_create( struct draw_context
*draw
)
252 struct pt_so_emit
*emit
= CALLOC_STRUCT(pt_so_emit
);
261 void draw_pt_so_emit_destroy( struct pt_so_emit
*emit
)