draw: Pass-through pipe_buffer::max_index to translate.
[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 unsigned max_index )
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 max_index );
79 }
80
81
82 /* Mainly for debug at this stage:
83 */
84 static void do_rhw_viewport( struct draw_vs_varient_generic *vsvg,
85 unsigned count,
86 void *output_buffer )
87 {
88 char *ptr = (char *)output_buffer;
89 const float *scale = vsvg->base.vs->draw->viewport.scale;
90 const float *trans = vsvg->base.vs->draw->viewport.translate;
91 unsigned stride = vsvg->temp_vertex_stride;
92 unsigned j;
93
94 ptr += vsvg->base.vs->position_output * 4 * sizeof(float);
95
96 for (j = 0; j < count; j++, ptr += stride) {
97 float *data = (float *)ptr;
98 float w = 1.0f / data[3];
99
100 data[0] = data[0] * w * scale[0] + trans[0];
101 data[1] = data[1] * w * scale[1] + trans[1];
102 data[2] = data[2] * w * scale[2] + trans[2];
103 data[3] = w;
104 }
105 }
106
107 static void do_viewport( struct draw_vs_varient_generic *vsvg,
108 unsigned count,
109 void *output_buffer )
110 {
111 char *ptr = (char *)output_buffer;
112 const float *scale = vsvg->base.vs->draw->viewport.scale;
113 const float *trans = vsvg->base.vs->draw->viewport.translate;
114 unsigned stride = vsvg->temp_vertex_stride;
115 unsigned j;
116
117 ptr += vsvg->base.vs->position_output * 4 * sizeof(float);
118
119 for (j = 0; j < count; j++, ptr += stride) {
120 float *data = (float *)ptr;
121
122 data[0] = data[0] * scale[0] + trans[0];
123 data[1] = data[1] * scale[1] + trans[1];
124 data[2] = data[2] * scale[2] + trans[2];
125 }
126 }
127
128
129 static void PIPE_CDECL vsvg_run_elts( struct draw_vs_varient *varient,
130 const unsigned *elts,
131 unsigned count,
132 void *output_buffer)
133 {
134 struct draw_vs_varient_generic *vsvg = (struct draw_vs_varient_generic *)varient;
135 unsigned temp_vertex_stride = vsvg->temp_vertex_stride;
136 void *temp_buffer = MALLOC( align(count,4) * temp_vertex_stride );
137
138 if (0) debug_printf("%s %d \n", __FUNCTION__, count);
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 vsvg->draw->instance_id,
147 temp_buffer );
148
149 vsvg->base.vs->run_linear( vsvg->base.vs,
150 temp_buffer,
151 temp_buffer,
152 vsvg->base.vs->draw->pt.user.vs_constants,
153 count,
154 temp_vertex_stride,
155 temp_vertex_stride);
156
157 /* FIXME: geometry shading? */
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 temp_buffer );
166 }
167 else if (vsvg->base.key.viewport) {
168 do_viewport( vsvg,
169 count,
170 temp_buffer );
171 }
172
173
174 vsvg->emit->set_buffer( vsvg->emit,
175 0,
176 temp_buffer,
177 temp_vertex_stride,
178 ~0 );
179
180 vsvg->emit->set_buffer( vsvg->emit,
181 1,
182 &vsvg->draw->rasterizer->point_size,
183 0,
184 ~0 );
185
186 vsvg->emit->run( vsvg->emit,
187 0, count,
188 vsvg->draw->instance_id,
189 output_buffer );
190
191 FREE(temp_buffer);
192 }
193
194
195 static void PIPE_CDECL vsvg_run_linear( struct draw_vs_varient *varient,
196 unsigned start,
197 unsigned count,
198 void *output_buffer )
199 {
200 struct draw_vs_varient_generic *vsvg = (struct draw_vs_varient_generic *)varient;
201 unsigned temp_vertex_stride = vsvg->temp_vertex_stride;
202 void *temp_buffer = MALLOC( align(count,4) * temp_vertex_stride );
203
204 if (0) debug_printf("%s %d %d (sz %d, %d)\n", __FUNCTION__, start, count,
205 vsvg->base.key.output_stride,
206 temp_vertex_stride);
207
208 vsvg->fetch->run( vsvg->fetch,
209 start,
210 count,
211 vsvg->draw->instance_id,
212 temp_buffer );
213
214 vsvg->base.vs->run_linear( vsvg->base.vs,
215 temp_buffer,
216 temp_buffer,
217 vsvg->base.vs->draw->pt.user.vs_constants,
218 count,
219 temp_vertex_stride,
220 temp_vertex_stride);
221
222 if (vsvg->base.key.clip) {
223 /* not really handling clipping, just do the rhw so we can
224 * see the results...
225 */
226 do_rhw_viewport( vsvg,
227 count,
228 temp_buffer );
229 }
230 else if (vsvg->base.key.viewport) {
231 do_viewport( vsvg,
232 count,
233 temp_buffer );
234 }
235
236 vsvg->emit->set_buffer( vsvg->emit,
237 0,
238 temp_buffer,
239 temp_vertex_stride,
240 ~0 );
241
242 vsvg->emit->set_buffer( vsvg->emit,
243 1,
244 &vsvg->draw->rasterizer->point_size,
245 0,
246 ~0 );
247
248 vsvg->emit->run( vsvg->emit,
249 0, count,
250 vsvg->draw->instance_id,
251 output_buffer );
252
253 FREE(temp_buffer);
254 }
255
256
257
258
259
260 static void vsvg_destroy( struct draw_vs_varient *varient )
261 {
262 FREE(varient);
263 }
264
265
266 struct draw_vs_varient *draw_vs_varient_generic( struct draw_vertex_shader *vs,
267 const struct draw_vs_varient_key *key )
268 {
269 unsigned i;
270 struct translate_key fetch, emit;
271
272 struct draw_vs_varient_generic *vsvg = CALLOC_STRUCT( draw_vs_varient_generic );
273 if (vsvg == NULL)
274 return NULL;
275
276 vsvg->base.key = *key;
277 vsvg->base.vs = vs;
278 vsvg->base.set_buffer = vsvg_set_buffer;
279 vsvg->base.run_elts = vsvg_run_elts;
280 vsvg->base.run_linear = vsvg_run_linear;
281 vsvg->base.destroy = vsvg_destroy;
282
283 vsvg->draw = vs->draw;
284
285 vsvg->temp_vertex_stride = MAX2(key->nr_inputs,
286 vsvg->base.vs->info.num_outputs) * 4 * sizeof(float);
287
288 /* Build free-standing fetch and emit functions:
289 */
290 fetch.nr_elements = key->nr_inputs;
291 fetch.output_stride = vsvg->temp_vertex_stride;
292 for (i = 0; i < key->nr_inputs; i++) {
293 fetch.element[i].type = TRANSLATE_ELEMENT_NORMAL;
294 fetch.element[i].input_format = key->element[i].in.format;
295 fetch.element[i].input_buffer = key->element[i].in.buffer;
296 fetch.element[i].input_offset = key->element[i].in.offset;
297 fetch.element[i].instance_divisor = 0;
298 fetch.element[i].output_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
299 fetch.element[i].output_offset = i * 4 * sizeof(float);
300 assert(fetch.element[i].output_offset < fetch.output_stride);
301 }
302
303
304 emit.nr_elements = key->nr_outputs;
305 emit.output_stride = key->output_stride;
306 for (i = 0; i < key->nr_outputs; i++) {
307 if (key->element[i].out.format != EMIT_1F_PSIZE)
308 {
309 emit.element[i].type = TRANSLATE_ELEMENT_NORMAL;
310 emit.element[i].input_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
311 emit.element[i].input_buffer = 0;
312 emit.element[i].input_offset = key->element[i].out.vs_output * 4 * sizeof(float);
313 emit.element[i].instance_divisor = 0;
314 emit.element[i].output_format = draw_translate_vinfo_format(key->element[i].out.format);
315 emit.element[i].output_offset = key->element[i].out.offset;
316 assert(emit.element[i].input_offset <= fetch.output_stride);
317 }
318 else {
319 emit.element[i].type = TRANSLATE_ELEMENT_NORMAL;
320 emit.element[i].input_format = PIPE_FORMAT_R32_FLOAT;
321 emit.element[i].input_buffer = 1;
322 emit.element[i].input_offset = 0;
323 emit.element[i].instance_divisor = 0;
324 emit.element[i].output_format = PIPE_FORMAT_R32_FLOAT;
325 emit.element[i].output_offset = key->element[i].out.offset;
326 }
327 }
328
329 vsvg->fetch = draw_vs_get_fetch( vs->draw, &fetch );
330 vsvg->emit = draw_vs_get_emit( vs->draw, &emit );
331
332 return &vsvg->base;
333 }
334
335
336
337
338