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