Merge branch 'mesa_7_7_branch'
[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
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 draw_vertex_shader *shader;
48 struct draw_context *draw;
49
50 /* Basic plan is to run these two translate functions before/after
51 * the vertex shader's existing run_linear() routine to simulate
52 * the inclusion of this functionality into the shader...
53 *
54 * Next will look at actually including it.
55 */
56 struct translate *fetch;
57 struct translate *emit;
58
59 unsigned temp_vertex_stride;
60 };
61
62
63
64
65
66 static void vsvg_set_buffer( struct draw_vs_varient *varient,
67 unsigned buffer,
68 const void *ptr,
69 unsigned stride )
70 {
71 struct draw_vs_varient_generic *vsvg = (struct draw_vs_varient_generic *)varient;
72
73 vsvg->fetch->set_buffer(vsvg->fetch,
74 buffer,
75 ptr,
76 stride);
77 }
78
79
80 /* Mainly for debug at this stage:
81 */
82 static void do_rhw_viewport( struct draw_vs_varient_generic *vsvg,
83 unsigned count,
84 void *output_buffer )
85 {
86 char *ptr = (char *)output_buffer;
87 const float *scale = vsvg->base.vs->draw->viewport.scale;
88 const float *trans = vsvg->base.vs->draw->viewport.translate;
89 unsigned stride = vsvg->temp_vertex_stride;
90 unsigned j;
91
92 ptr += vsvg->base.vs->position_output * 4 * sizeof(float);
93
94 for (j = 0; j < count; j++, ptr += stride) {
95 float *data = (float *)ptr;
96 float w = 1.0f / data[3];
97
98 data[0] = data[0] * w * scale[0] + trans[0];
99 data[1] = data[1] * w * scale[1] + trans[1];
100 data[2] = data[2] * w * scale[2] + trans[2];
101 data[3] = w;
102 }
103 }
104
105 static void do_viewport( struct draw_vs_varient_generic *vsvg,
106 unsigned count,
107 void *output_buffer )
108 {
109 char *ptr = (char *)output_buffer;
110 const float *scale = vsvg->base.vs->draw->viewport.scale;
111 const float *trans = vsvg->base.vs->draw->viewport.translate;
112 unsigned stride = vsvg->temp_vertex_stride;
113 unsigned j;
114
115 ptr += vsvg->base.vs->position_output * 4 * sizeof(float);
116
117 for (j = 0; j < count; j++, ptr += stride) {
118 float *data = (float *)ptr;
119
120 data[0] = data[0] * scale[0] + trans[0];
121 data[1] = data[1] * scale[1] + trans[1];
122 data[2] = data[2] * scale[2] + trans[2];
123 }
124 }
125
126
127 static void PIPE_CDECL vsvg_run_elts( struct draw_vs_varient *varient,
128 const unsigned *elts,
129 unsigned count,
130 void *output_buffer)
131 {
132 struct draw_vs_varient_generic *vsvg = (struct draw_vs_varient_generic *)varient;
133 unsigned temp_vertex_stride = vsvg->temp_vertex_stride;
134 void *temp_buffer = MALLOC( align(count,4) * temp_vertex_stride );
135
136 if (0) debug_printf("%s %d \n", __FUNCTION__, count);
137
138 /* Want to do this in small batches for cache locality?
139 */
140
141 vsvg->fetch->run_elts( vsvg->fetch,
142 elts,
143 count,
144 vsvg->draw->instance_id,
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 vsvg->draw->instance_id,
185 output_buffer );
186
187 FREE(temp_buffer);
188 }
189
190
191 static void PIPE_CDECL vsvg_run_linear( struct draw_vs_varient *varient,
192 unsigned start,
193 unsigned count,
194 void *output_buffer )
195 {
196 struct draw_vs_varient_generic *vsvg = (struct draw_vs_varient_generic *)varient;
197 unsigned temp_vertex_stride = vsvg->temp_vertex_stride;
198 void *temp_buffer = MALLOC( align(count,4) * temp_vertex_stride );
199
200 if (0) debug_printf("%s %d %d (sz %d, %d)\n", __FUNCTION__, start, count,
201 vsvg->base.key.output_stride,
202 temp_vertex_stride);
203
204 vsvg->fetch->run( vsvg->fetch,
205 start,
206 count,
207 vsvg->draw->instance_id,
208 temp_buffer );
209
210 vsvg->base.vs->run_linear( vsvg->base.vs,
211 temp_buffer,
212 temp_buffer,
213 (const float (*)[4])vsvg->base.vs->draw->pt.user.vs_constants,
214 count,
215 temp_vertex_stride,
216 temp_vertex_stride);
217
218 if (vsvg->base.key.clip) {
219 /* not really handling clipping, just do the rhw so we can
220 * see the results...
221 */
222 do_rhw_viewport( vsvg,
223 count,
224 temp_buffer );
225 }
226 else if (vsvg->base.key.viewport) {
227 do_viewport( vsvg,
228 count,
229 temp_buffer );
230 }
231
232 vsvg->emit->set_buffer( vsvg->emit,
233 0,
234 temp_buffer,
235 temp_vertex_stride );
236
237 vsvg->emit->set_buffer( vsvg->emit,
238 1,
239 &vsvg->draw->rasterizer->point_size,
240 0);
241
242 vsvg->emit->run( vsvg->emit,
243 0, count,
244 vsvg->draw->instance_id,
245 output_buffer );
246
247 FREE(temp_buffer);
248 }
249
250
251
252
253
254 static void vsvg_destroy( struct draw_vs_varient *varient )
255 {
256 FREE(varient);
257 }
258
259
260 struct draw_vs_varient *draw_vs_varient_generic( struct draw_vertex_shader *vs,
261 const struct draw_vs_varient_key *key )
262 {
263 unsigned i;
264 struct translate_key fetch, emit;
265
266 struct draw_vs_varient_generic *vsvg = CALLOC_STRUCT( draw_vs_varient_generic );
267 if (vsvg == NULL)
268 return NULL;
269
270 vsvg->base.key = *key;
271 vsvg->base.vs = vs;
272 vsvg->base.set_buffer = vsvg_set_buffer;
273 vsvg->base.run_elts = vsvg_run_elts;
274 vsvg->base.run_linear = vsvg_run_linear;
275 vsvg->base.destroy = vsvg_destroy;
276
277 vsvg->draw = vs->draw;
278
279 vsvg->temp_vertex_stride = MAX2(key->nr_inputs,
280 vsvg->base.vs->info.num_outputs) * 4 * sizeof(float);
281
282 /* Build free-standing fetch and emit functions:
283 */
284 fetch.nr_elements = key->nr_inputs;
285 fetch.output_stride = vsvg->temp_vertex_stride;
286 for (i = 0; i < key->nr_inputs; i++) {
287 fetch.element[i].type = TRANSLATE_ELEMENT_NORMAL;
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].instance_divisor = 0;
292 fetch.element[i].output_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
293 fetch.element[i].output_offset = i * 4 * sizeof(float);
294 assert(fetch.element[i].output_offset < fetch.output_stride);
295 }
296
297
298 emit.nr_elements = key->nr_outputs;
299 emit.output_stride = key->output_stride;
300 for (i = 0; i < key->nr_outputs; i++) {
301 if (key->element[i].out.format != EMIT_1F_PSIZE)
302 {
303 emit.element[i].type = TRANSLATE_ELEMENT_NORMAL;
304 emit.element[i].input_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
305 emit.element[i].input_buffer = 0;
306 emit.element[i].input_offset = key->element[i].out.vs_output * 4 * sizeof(float);
307 emit.element[i].instance_divisor = 0;
308 emit.element[i].output_format = draw_translate_vinfo_format(key->element[i].out.format);
309 emit.element[i].output_offset = key->element[i].out.offset;
310 assert(emit.element[i].input_offset <= fetch.output_stride);
311 }
312 else {
313 emit.element[i].type = TRANSLATE_ELEMENT_NORMAL;
314 emit.element[i].input_format = PIPE_FORMAT_R32_FLOAT;
315 emit.element[i].input_buffer = 1;
316 emit.element[i].input_offset = 0;
317 emit.element[i].instance_divisor = 0;
318 emit.element[i].output_format = PIPE_FORMAT_R32_FLOAT;
319 emit.element[i].output_offset = key->element[i].out.offset;
320 }
321 }
322
323 vsvg->fetch = draw_vs_get_fetch( vs->draw, &fetch );
324 vsvg->emit = draw_vs_get_emit( vs->draw, &emit );
325
326 return &vsvg->base;
327 }
328
329
330
331
332