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