Merge branch 'master' into opengl-es-v2
[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.vs_constants,
151 count,
152 temp_vertex_stride,
153 temp_vertex_stride);
154
155 /* FIXME: geometry shading? */
156
157 if (vsvg->base.key.clip) {
158 /* not really handling clipping, just do the rhw so we can
159 * see the results...
160 */
161 do_rhw_viewport( vsvg,
162 count,
163 temp_buffer );
164 }
165 else if (vsvg->base.key.viewport) {
166 do_viewport( vsvg,
167 count,
168 temp_buffer );
169 }
170
171
172 vsvg->emit->set_buffer( vsvg->emit,
173 0,
174 temp_buffer,
175 temp_vertex_stride );
176
177 vsvg->emit->set_buffer( vsvg->emit,
178 1,
179 &vsvg->draw->rasterizer->point_size,
180 0);
181
182 vsvg->emit->run( vsvg->emit,
183 0, count,
184 output_buffer );
185
186 FREE(temp_buffer);
187 }
188
189
190 static void PIPE_CDECL vsvg_run_linear( struct draw_vs_varient *varient,
191 unsigned start,
192 unsigned count,
193 void *output_buffer )
194 {
195 struct draw_vs_varient_generic *vsvg = (struct draw_vs_varient_generic *)varient;
196 unsigned temp_vertex_stride = vsvg->temp_vertex_stride;
197 void *temp_buffer = MALLOC( align(count,4) * temp_vertex_stride );
198
199 if (0) debug_printf("%s %d %d (sz %d, %d)\n", __FUNCTION__, start, count,
200 vsvg->base.key.output_stride,
201 temp_vertex_stride);
202
203 vsvg->fetch->run( vsvg->fetch,
204 start,
205 count,
206 temp_buffer );
207
208 vsvg->base.vs->run_linear( vsvg->base.vs,
209 temp_buffer,
210 temp_buffer,
211 (const float (*)[4])vsvg->base.vs->draw->pt.user.vs_constants,
212 count,
213 temp_vertex_stride,
214 temp_vertex_stride);
215
216 if (vsvg->base.key.clip) {
217 /* not really handling clipping, just do the rhw so we can
218 * see the results...
219 */
220 do_rhw_viewport( vsvg,
221 count,
222 temp_buffer );
223 }
224 else if (vsvg->base.key.viewport) {
225 do_viewport( vsvg,
226 count,
227 temp_buffer );
228 }
229
230 vsvg->emit->set_buffer( vsvg->emit,
231 0,
232 temp_buffer,
233 temp_vertex_stride );
234
235 vsvg->emit->set_buffer( vsvg->emit,
236 1,
237 &vsvg->draw->rasterizer->point_size,
238 0);
239
240 vsvg->emit->run( vsvg->emit,
241 0, count,
242 output_buffer );
243
244 FREE(temp_buffer);
245 }
246
247
248
249
250
251 static void vsvg_destroy( struct draw_vs_varient *varient )
252 {
253 FREE(varient);
254 }
255
256
257 struct draw_vs_varient *draw_vs_varient_generic( struct draw_vertex_shader *vs,
258 const struct draw_vs_varient_key *key )
259 {
260 unsigned i;
261 struct translate_key fetch, emit;
262
263 struct draw_vs_varient_generic *vsvg = CALLOC_STRUCT( draw_vs_varient_generic );
264 if (vsvg == NULL)
265 return NULL;
266
267 vsvg->base.key = *key;
268 vsvg->base.vs = vs;
269 vsvg->base.set_buffer = vsvg_set_buffer;
270 vsvg->base.run_elts = vsvg_run_elts;
271 vsvg->base.run_linear = vsvg_run_linear;
272 vsvg->base.destroy = vsvg_destroy;
273
274 vsvg->draw = vs->draw;
275
276 vsvg->temp_vertex_stride = MAX2(key->nr_inputs,
277 vsvg->base.vs->info.num_outputs) * 4 * sizeof(float);
278
279 /* Build free-standing fetch and emit functions:
280 */
281 fetch.nr_elements = key->nr_inputs;
282 fetch.output_stride = vsvg->temp_vertex_stride;
283 for (i = 0; i < key->nr_inputs; i++) {
284 fetch.element[i].input_format = key->element[i].in.format;
285 fetch.element[i].input_buffer = key->element[i].in.buffer;
286 fetch.element[i].input_offset = key->element[i].in.offset;
287 fetch.element[i].output_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
288 fetch.element[i].output_offset = i * 4 * sizeof(float);
289 assert(fetch.element[i].output_offset < fetch.output_stride);
290 }
291
292
293 emit.nr_elements = key->nr_outputs;
294 emit.output_stride = key->output_stride;
295 for (i = 0; i < key->nr_outputs; i++) {
296 if (key->element[i].out.format != EMIT_1F_PSIZE)
297 {
298 emit.element[i].input_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
299 emit.element[i].input_buffer = 0;
300 emit.element[i].input_offset = key->element[i].out.vs_output * 4 * sizeof(float);
301 emit.element[i].output_format = draw_translate_vinfo_format(key->element[i].out.format);
302 emit.element[i].output_offset = key->element[i].out.offset;
303 assert(emit.element[i].input_offset <= fetch.output_stride);
304 }
305 else {
306 emit.element[i].input_format = PIPE_FORMAT_R32_FLOAT;
307 emit.element[i].input_buffer = 1;
308 emit.element[i].input_offset = 0;
309 emit.element[i].output_format = PIPE_FORMAT_R32_FLOAT;
310 emit.element[i].output_offset = key->element[i].out.offset;
311 }
312 }
313
314 vsvg->fetch = draw_vs_get_fetch( vs->draw, &fetch );
315 vsvg->emit = draw_vs_get_emit( vs->draw, &emit );
316
317 return &vsvg->base;
318 }
319
320
321
322
323