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
.constants
,
156 if (vsvg
->base
.key
.clip
) {
157 /* not really handling clipping, just do the rhw so we can
160 do_rhw_viewport( vsvg
,
164 else if (vsvg
->base
.key
.viewport
) {
171 vsvg
->emit
->set_buffer( vsvg
->emit
,
174 temp_vertex_stride
);
176 vsvg
->emit
->set_buffer( vsvg
->emit
,
178 &vsvg
->draw
->rasterizer
->point_size
,
181 vsvg
->emit
->run( vsvg
->emit
,
189 static void PIPE_CDECL
vsvg_run_linear( struct draw_vs_varient
*varient
,
192 void *output_buffer
)
194 struct draw_vs_varient_generic
*vsvg
= (struct draw_vs_varient_generic
*)varient
;
195 unsigned temp_vertex_stride
= vsvg
->temp_vertex_stride
;
196 void *temp_buffer
= MALLOC( align(count
,4) * temp_vertex_stride
);
198 if (0) debug_printf("%s %d %d (sz %d, %d)\n", __FUNCTION__
, start
, count
,
199 vsvg
->base
.key
.output_stride
,
202 vsvg
->fetch
->run( vsvg
->fetch
,
207 vsvg
->base
.vs
->run_linear( vsvg
->base
.vs
,
210 (const float (*)[4])vsvg
->base
.vs
->draw
->pt
.user
.constants
,
215 if (vsvg
->base
.key
.clip
) {
216 /* not really handling clipping, just do the rhw so we can
219 do_rhw_viewport( vsvg
,
223 else if (vsvg
->base
.key
.viewport
) {
229 vsvg
->emit
->set_buffer( vsvg
->emit
,
232 temp_vertex_stride
);
234 vsvg
->emit
->set_buffer( vsvg
->emit
,
236 &vsvg
->draw
->rasterizer
->point_size
,
239 vsvg
->emit
->run( vsvg
->emit
,
250 static void vsvg_destroy( struct draw_vs_varient
*varient
)
256 struct draw_vs_varient
*draw_vs_varient_generic( struct draw_vertex_shader
*vs
,
257 const struct draw_vs_varient_key
*key
)
260 struct translate_key fetch
, emit
;
262 struct draw_vs_varient_generic
*vsvg
= CALLOC_STRUCT( draw_vs_varient_generic
);
266 vsvg
->base
.key
= *key
;
268 vsvg
->base
.set_buffer
= vsvg_set_buffer
;
269 vsvg
->base
.run_elts
= vsvg_run_elts
;
270 vsvg
->base
.run_linear
= vsvg_run_linear
;
271 vsvg
->base
.destroy
= vsvg_destroy
;
273 vsvg
->draw
= vs
->draw
;
275 vsvg
->temp_vertex_stride
= MAX2(key
->nr_inputs
,
276 vsvg
->base
.vs
->info
.num_outputs
) * 4 * sizeof(float);
278 /* Build free-standing fetch and emit functions:
280 fetch
.nr_elements
= key
->nr_inputs
;
281 fetch
.output_stride
= vsvg
->temp_vertex_stride
;
282 for (i
= 0; i
< key
->nr_inputs
; i
++) {
283 fetch
.element
[i
].input_format
= key
->element
[i
].in
.format
;
284 fetch
.element
[i
].input_buffer
= key
->element
[i
].in
.buffer
;
285 fetch
.element
[i
].input_offset
= key
->element
[i
].in
.offset
;
286 fetch
.element
[i
].output_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
287 fetch
.element
[i
].output_offset
= i
* 4 * sizeof(float);
288 assert(fetch
.element
[i
].output_offset
< fetch
.output_stride
);
292 emit
.nr_elements
= key
->nr_outputs
;
293 emit
.output_stride
= key
->output_stride
;
294 for (i
= 0; i
< key
->nr_outputs
; i
++) {
295 if (key
->element
[i
].out
.format
!= EMIT_1F_PSIZE
)
297 emit
.element
[i
].input_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
298 emit
.element
[i
].input_buffer
= 0;
299 emit
.element
[i
].input_offset
= key
->element
[i
].out
.vs_output
* 4 * sizeof(float);
300 emit
.element
[i
].output_format
= draw_translate_vinfo_format(key
->element
[i
].out
.format
);
301 emit
.element
[i
].output_offset
= key
->element
[i
].out
.offset
;
302 assert(emit
.element
[i
].input_offset
<= fetch
.output_stride
);
305 emit
.element
[i
].input_format
= PIPE_FORMAT_R32_FLOAT
;
306 emit
.element
[i
].input_buffer
= 1;
307 emit
.element
[i
].input_offset
= 0;
308 emit
.element
[i
].output_format
= PIPE_FORMAT_R32_FLOAT
;
309 emit
.element
[i
].output_offset
= key
->element
[i
].out
.offset
;
313 vsvg
->fetch
= draw_vs_get_fetch( vs
->draw
, &fetch
);
314 vsvg
->emit
= draw_vs_get_emit( vs
->draw
, &emit
);