1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * Keith Whitwell <keith@tungstengraphics.com>
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"
43 /* A first pass at incorporating vertex fetch/emit functionality into
45 struct draw_vs_varient_generic
{
46 struct draw_vs_varient base
;
48 struct draw_vertex_shader
*shader
;
49 struct draw_context
*draw
;
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...
55 * Next will look at actually including it.
57 struct translate
*fetch
;
58 struct translate
*emit
;
60 unsigned temp_vertex_stride
;
67 static void vsvg_set_buffer( struct draw_vs_varient
*varient
,
72 struct draw_vs_varient_generic
*vsvg
= (struct draw_vs_varient_generic
*)varient
;
74 vsvg
->fetch
->set_buffer(vsvg
->fetch
,
81 /* Mainly for debug at this stage:
83 static void do_rhw_viewport( struct draw_vs_varient_generic
*vsvg
,
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
;
93 ptr
+= vsvg
->base
.vs
->position_output
* 4 * sizeof(float);
95 for (j
= 0; j
< count
; j
++, ptr
+= stride
) {
96 float *data
= (float *)ptr
;
97 float w
= 1.0f
/ data
[3];
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];
106 static void do_viewport( struct draw_vs_varient_generic
*vsvg
,
108 void *output_buffer
)
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
;
116 ptr
+= vsvg
->base
.vs
->position_output
* 4 * sizeof(float);
118 for (j
= 0; j
< count
; j
++, ptr
+= stride
) {
119 float *data
= (float *)ptr
;
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];
128 static void PIPE_CDECL
vsvg_run_elts( struct draw_vs_varient
*varient
,
129 const unsigned *elts
,
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
);
137 if (0) debug_printf("%s %d \n", __FUNCTION__
, count
);
139 /* Want to do this in small batches for cache locality?
142 vsvg
->fetch
->run_elts( vsvg
->fetch
,
147 vsvg
->base
.vs
->run_linear( vsvg
->base
.vs
,
150 (const float (*)[4])vsvg
->base
.vs
->draw
->pt
.user
.vs_constants
,
155 /* FIXME: geometry shading? */
157 if (vsvg
->base
.key
.clip
) {
158 /* not really handling clipping, just do the rhw so we can
161 do_rhw_viewport( vsvg
,
165 else if (vsvg
->base
.key
.viewport
) {
172 vsvg
->emit
->set_buffer( vsvg
->emit
,
175 temp_vertex_stride
);
177 vsvg
->emit
->set_buffer( vsvg
->emit
,
179 &vsvg
->draw
->rasterizer
->point_size
,
182 vsvg
->emit
->run( vsvg
->emit
,
190 static void PIPE_CDECL
vsvg_run_linear( struct draw_vs_varient
*varient
,
193 void *output_buffer
)
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
);
199 if (0) debug_printf("%s %d %d (sz %d, %d)\n", __FUNCTION__
, start
, count
,
200 vsvg
->base
.key
.output_stride
,
203 vsvg
->fetch
->run( vsvg
->fetch
,
208 vsvg
->base
.vs
->run_linear( vsvg
->base
.vs
,
211 (const float (*)[4])vsvg
->base
.vs
->draw
->pt
.user
.vs_constants
,
216 if (vsvg
->base
.key
.clip
) {
217 /* not really handling clipping, just do the rhw so we can
220 do_rhw_viewport( vsvg
,
224 else if (vsvg
->base
.key
.viewport
) {
230 vsvg
->emit
->set_buffer( vsvg
->emit
,
233 temp_vertex_stride
);
235 vsvg
->emit
->set_buffer( vsvg
->emit
,
237 &vsvg
->draw
->rasterizer
->point_size
,
240 vsvg
->emit
->run( vsvg
->emit
,
251 static void vsvg_destroy( struct draw_vs_varient
*varient
)
257 struct draw_vs_varient
*draw_vs_varient_generic( struct draw_vertex_shader
*vs
,
258 const struct draw_vs_varient_key
*key
)
261 struct translate_key fetch
, emit
;
263 struct draw_vs_varient_generic
*vsvg
= CALLOC_STRUCT( draw_vs_varient_generic
);
267 vsvg
->base
.key
= *key
;
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
;
274 vsvg
->draw
= vs
->draw
;
276 vsvg
->temp_vertex_stride
= MAX2(key
->nr_inputs
,
277 vsvg
->base
.vs
->info
.num_outputs
) * 4 * sizeof(float);
279 /* Build free-standing fetch and emit functions:
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
);
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
)
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
);
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
;
314 vsvg
->fetch
= draw_vs_get_fetch( vs
->draw
, &fetch
);
315 vsvg
->emit
= draw_vs_get_emit( vs
->draw
, &emit
);