draw: Decorate callbacks with PIPE_CDECL.
[mesa.git] / src / gallium / auxiliary / draw / draw_vs_varient.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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 /*
29 * Authors:
30 * Keith Whitwell <keith@tungstengraphics.com>
31 */
32
33 #include "pipe/p_util.h"
34 #include "draw/draw_context.h"
35 #include "draw/draw_private.h"
36 #include "draw/draw_vbuf.h"
37 #include "draw/draw_vertex.h"
38 #include "draw/draw_vs.h"
39 #include "translate/translate.h"
40 #include "translate/translate_cache.h"
41
42 /* A first pass at incorporating vertex fetch/emit functionality into
43 */
44 struct draw_vs_varient_generic {
45 struct draw_vs_varient base;
46
47 struct pipe_viewport_state viewport;
48
49 struct draw_vertex_shader *shader;
50 struct draw_context *draw;
51
52 /* Basic plan is to run these two translate functions before/after
53 * the vertex shader's existing run_linear() routine to simulate
54 * the inclusion of this functionality into the shader...
55 *
56 * Next will look at actually including it.
57 */
58 struct translate *fetch;
59 struct translate *emit;
60
61 const float (*constants)[4];
62 };
63
64
65
66
67 static void vsvg_set_constants( struct draw_vs_varient *varient,
68 const float (*constants)[4] )
69 {
70 struct draw_vs_varient_generic *vsvg = (struct draw_vs_varient_generic *)varient;
71
72 vsvg->constants = constants;
73 }
74
75
76 static void vsvg_set_input( struct draw_vs_varient *varient,
77 unsigned buffer,
78 const void *ptr,
79 unsigned stride )
80 {
81 struct draw_vs_varient_generic *vsvg = (struct draw_vs_varient_generic *)varient;
82
83 vsvg->fetch->set_buffer(vsvg->fetch,
84 buffer,
85 ptr,
86 stride);
87 }
88
89
90 /* Mainly for debug at this stage:
91 */
92 static void do_rhw_viewport( struct draw_vs_varient_generic *vsvg,
93 unsigned count,
94 void *output_buffer )
95 {
96 char *ptr = (char *)output_buffer;
97 const float *scale = vsvg->viewport.scale;
98 const float *trans = vsvg->viewport.translate;
99 unsigned stride = vsvg->base.key.output_stride;
100 unsigned j;
101
102 for (j = 0; j < count; j++, ptr += stride) {
103 float *data = (float *)ptr;
104 float w = 1.0f / data[3];
105
106 data[0] = data[0] * w * scale[0] + trans[0];
107 data[1] = data[1] * w * scale[1] + trans[1];
108 data[2] = data[2] * w * scale[2] + trans[2];
109 data[3] = w;
110 }
111 }
112
113 static void do_viewport( struct draw_vs_varient_generic *vsvg,
114 unsigned count,
115 void *output_buffer )
116 {
117 char *ptr = (char *)output_buffer;
118 const float *scale = vsvg->viewport.scale;
119 const float *trans = vsvg->viewport.translate;
120 unsigned stride = vsvg->base.key.output_stride;
121 unsigned j;
122
123 for (j = 0; j < count; j++, ptr += stride) {
124 float *data = (float *)ptr;
125
126 data[0] = data[0] * scale[0] + trans[0];
127 data[1] = data[1] * scale[1] + trans[1];
128 data[2] = data[2] * scale[2] + trans[2];
129 }
130 }
131
132
133 static void PIPE_CDECL vsvg_run_elts( struct draw_vs_varient *varient,
134 const unsigned *elts,
135 unsigned count,
136 void *output_buffer )
137 {
138 struct draw_vs_varient_generic *vsvg = (struct draw_vs_varient_generic *)varient;
139
140 /* Want to do this in small batches for cache locality?
141 */
142
143 vsvg->fetch->run_elts( vsvg->fetch,
144 elts,
145 count,
146 output_buffer );
147
148 //if (!vsvg->base.vs->is_passthrough)
149 {
150 vsvg->base.vs->run_linear( vsvg->base.vs,
151 output_buffer,
152 output_buffer,
153 vsvg->constants,
154 count,
155 vsvg->base.key.output_stride,
156 vsvg->base.key.output_stride);
157
158
159 if (vsvg->base.key.clip) {
160 /* not really handling clipping, just do the rhw so we can
161 * see the results...
162 */
163 do_rhw_viewport( vsvg,
164 count,
165 output_buffer );
166 }
167 else if (vsvg->base.key.viewport) {
168 do_viewport( vsvg,
169 count,
170 output_buffer );
171 }
172
173
174 //if (!vsvg->already_in_emit_format)
175
176 vsvg->emit->set_buffer( vsvg->emit,
177 0,
178 output_buffer,
179 vsvg->base.key.output_stride );
180
181
182 vsvg->emit->run( vsvg->emit,
183 0, count,
184 output_buffer );
185 }
186 }
187
188
189 static void PIPE_CDECL vsvg_run_linear( struct draw_vs_varient *varient,
190 unsigned start,
191 unsigned count,
192 void *output_buffer )
193 {
194 struct draw_vs_varient_generic *vsvg = (struct draw_vs_varient_generic *)varient;
195
196 //debug_printf("%s %d %d\n", __FUNCTION__, start, count);
197
198
199 vsvg->fetch->run( vsvg->fetch,
200 start,
201 count,
202 output_buffer );
203
204 //if (!vsvg->base.vs->is_passthrough)
205 {
206 vsvg->base.vs->run_linear( vsvg->base.vs,
207 output_buffer,
208 output_buffer,
209 vsvg->constants,
210 count,
211 vsvg->base.key.output_stride,
212 vsvg->base.key.output_stride);
213
214 if (vsvg->base.key.clip) {
215 /* not really handling clipping, just do the rhw so we can
216 * see the results...
217 */
218 do_rhw_viewport( vsvg,
219 count,
220 output_buffer );
221 }
222 else if (vsvg->base.key.viewport) {
223 do_viewport( vsvg,
224 count,
225 output_buffer );
226 }
227
228 //if (!vsvg->already_in_emit_format)
229 vsvg->emit->set_buffer( vsvg->emit,
230 0,
231 output_buffer,
232 vsvg->base.key.output_stride );
233
234 vsvg->emit->set_buffer( vsvg->emit,
235 1,
236 &vsvg->draw->rasterizer->point_size,
237 0);
238
239 vsvg->emit->run( vsvg->emit,
240 0, count,
241 output_buffer );
242 }
243 }
244
245
246
247
248 static void vsvg_set_viewport( struct draw_vs_varient *varient,
249 const struct pipe_viewport_state *viewport )
250 {
251 struct draw_vs_varient_generic *vsvg = (struct draw_vs_varient_generic *)varient;
252
253 vsvg->viewport = *viewport;
254 }
255
256 static void vsvg_destroy( struct draw_vs_varient *varient )
257 {
258 FREE(varient);
259 }
260
261
262 struct draw_vs_varient *draw_vs_varient_generic( struct draw_vertex_shader *vs,
263 const struct draw_vs_varient_key *key )
264 {
265 unsigned i;
266 struct translate_key fetch, emit;
267
268 struct draw_vs_varient_generic *vsvg = CALLOC_STRUCT( draw_vs_varient_generic );
269 if (vsvg == NULL)
270 return NULL;
271
272 vsvg->base.key = *key;
273 vsvg->base.vs = vs;
274 vsvg->base.set_input = vsvg_set_input;
275 vsvg->base.set_constants = vsvg_set_constants;
276 vsvg->base.set_viewport = vsvg_set_viewport;
277 vsvg->base.run_elts = vsvg_run_elts;
278 vsvg->base.run_linear = vsvg_run_linear;
279 vsvg->base.destroy = vsvg_destroy;
280
281
282
283 /* Build free-standing fetch and emit functions:
284 */
285 fetch.nr_elements = key->nr_inputs;
286 fetch.output_stride = 0;
287 for (i = 0; i < key->nr_inputs; i++) {
288 fetch.element[i].input_format = key->element[i].in.format;
289 fetch.element[i].input_buffer = key->element[i].in.buffer;
290 fetch.element[i].input_offset = key->element[i].in.offset;
291 fetch.element[i].output_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
292 fetch.element[i].output_offset = fetch.output_stride;
293 fetch.output_stride += 4 * sizeof(float);
294 }
295
296
297 emit.nr_elements = key->nr_outputs;
298 emit.output_stride = key->output_stride;
299 for (i = 0; i < key->nr_outputs; i++) {
300 if (key->element[i].out.format != EMIT_1F_PSIZE)
301 {
302 emit.element[i].input_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
303 emit.element[i].input_buffer = 0;
304 emit.element[i].input_offset = key->element[i].out.vs_output * 4 * sizeof(float);
305 emit.element[i].output_format = draw_translate_vinfo_format(key->element[i].out.format);
306 emit.element[i].output_offset = key->element[i].out.offset;
307 }
308 else {
309 emit.element[i].input_format = PIPE_FORMAT_R32_FLOAT;
310 emit.element[i].input_buffer = 1;
311 emit.element[i].input_offset = 0;
312 emit.element[i].output_format = PIPE_FORMAT_R32_FLOAT;
313 emit.element[i].output_offset = key->element[i].out.offset;
314 }
315 }
316
317 vsvg->fetch = draw_vs_get_fetch( vs->draw, &fetch );
318 vsvg->emit = draw_vs_get_emit( vs->draw, &emit );
319
320 return &vsvg->base;
321 }
322
323
324
325
326