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 "pipe/p_util.h"
34 #include "draw/draw_context.h"
35 #include "draw/draw_private.h"
36 #include "draw/draw_vbuf.h"
37 #include "draw/draw_vertex.h"
38 #include "draw/draw_vs.h"
39 #include "translate/translate.h"
40 #include "translate/translate_cache.h"
42 /* A first pass at incorporating vertex fetch/emit functionality into
44 struct draw_vs_varient_generic
{
45 struct draw_vs_varient base
;
47 struct draw_vertex_shader
*shader
;
48 struct draw_context
*draw
;
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...
54 * Next will look at actually including it.
56 struct translate
*fetch
;
57 struct translate
*emit
;
64 static void vsvg_set_input( struct draw_vs_varient
*varient
,
69 struct draw_vs_varient_generic
*vsvg
= (struct draw_vs_varient_generic
*)varient
;
71 vsvg
->fetch
->set_buffer(vsvg
->fetch
,
78 /* Mainly for debug at this stage:
80 static void do_rhw_viewport( struct draw_vs_varient_generic
*vsvg
,
84 char *ptr
= (char *)output_buffer
;
85 const float *scale
= vsvg
->base
.vs
->draw
->viewport
.scale
;
86 const float *trans
= vsvg
->base
.vs
->draw
->viewport
.translate
;
87 unsigned stride
= vsvg
->base
.key
.output_stride
;
90 for (j
= 0; j
< count
; j
++, ptr
+= stride
) {
91 float *data
= (float *)ptr
;
92 float w
= 1.0f
/ data
[3];
94 data
[0] = data
[0] * w
* scale
[0] + trans
[0];
95 data
[1] = data
[1] * w
* scale
[1] + trans
[1];
96 data
[2] = data
[2] * w
* scale
[2] + trans
[2];
101 static void do_viewport( struct draw_vs_varient_generic
*vsvg
,
103 void *output_buffer
)
105 char *ptr
= (char *)output_buffer
;
106 const float *scale
= vsvg
->base
.vs
->draw
->viewport
.scale
;
107 const float *trans
= vsvg
->base
.vs
->draw
->viewport
.translate
;
108 unsigned stride
= vsvg
->base
.key
.output_stride
;
111 for (j
= 0; j
< count
; j
++, ptr
+= stride
) {
112 float *data
= (float *)ptr
;
114 data
[0] = data
[0] * scale
[0] + trans
[0];
115 data
[1] = data
[1] * scale
[1] + trans
[1];
116 data
[2] = data
[2] * scale
[2] + trans
[2];
121 static void PIPE_CDECL
vsvg_run_elts( struct draw_vs_varient
*varient
,
122 const unsigned *elts
,
126 struct draw_vs_varient_generic
*vsvg
= (struct draw_vs_varient_generic
*)varient
;
128 /* Want to do this in small batches for cache locality?
131 vsvg
->fetch
->run_elts( vsvg
->fetch
,
136 //if (!vsvg->base.vs->is_passthrough)
138 vsvg
->base
.vs
->run_linear( vsvg
->base
.vs
,
141 (const float (*)[4])vsvg
->base
.vs
->draw
->pt
.user
.constants
,
143 vsvg
->base
.key
.output_stride
,
144 vsvg
->base
.key
.output_stride
);
147 if (vsvg
->base
.key
.clip
) {
148 /* not really handling clipping, just do the rhw so we can
151 do_rhw_viewport( vsvg
,
155 else if (vsvg
->base
.key
.viewport
) {
162 //if (!vsvg->already_in_emit_format)
164 vsvg
->emit
->set_buffer( vsvg
->emit
,
167 vsvg
->base
.key
.output_stride
);
170 vsvg
->emit
->run( vsvg
->emit
,
177 static void PIPE_CDECL
vsvg_run_linear( struct draw_vs_varient
*varient
,
180 void *output_buffer
)
182 struct draw_vs_varient_generic
*vsvg
= (struct draw_vs_varient_generic
*)varient
;
184 //debug_printf("%s %d %d\n", __FUNCTION__, start, count);
187 vsvg
->fetch
->run( vsvg
->fetch
,
192 //if (!vsvg->base.vs->is_passthrough)
194 vsvg
->base
.vs
->run_linear( vsvg
->base
.vs
,
197 (const float (*)[4])vsvg
->base
.vs
->draw
->pt
.user
.constants
,
199 vsvg
->base
.key
.output_stride
,
200 vsvg
->base
.key
.output_stride
);
202 if (vsvg
->base
.key
.clip
) {
203 /* not really handling clipping, just do the rhw so we can
206 do_rhw_viewport( vsvg
,
210 else if (vsvg
->base
.key
.viewport
) {
216 //if (!vsvg->already_in_emit_format)
217 vsvg
->emit
->set_buffer( vsvg
->emit
,
220 vsvg
->base
.key
.output_stride
);
222 vsvg
->emit
->set_buffer( vsvg
->emit
,
224 &vsvg
->draw
->rasterizer
->point_size
,
227 vsvg
->emit
->run( vsvg
->emit
,
237 static void vsvg_destroy( struct draw_vs_varient
*varient
)
243 struct draw_vs_varient
*draw_vs_varient_generic( struct draw_vertex_shader
*vs
,
244 const struct draw_vs_varient_key
*key
)
247 struct translate_key fetch
, emit
;
249 struct draw_vs_varient_generic
*vsvg
= CALLOC_STRUCT( draw_vs_varient_generic
);
253 vsvg
->base
.key
= *key
;
255 vsvg
->base
.set_input
= vsvg_set_input
;
256 vsvg
->base
.run_elts
= vsvg_run_elts
;
257 vsvg
->base
.run_linear
= vsvg_run_linear
;
258 vsvg
->base
.destroy
= vsvg_destroy
;
262 /* Build free-standing fetch and emit functions:
264 fetch
.nr_elements
= key
->nr_inputs
;
265 fetch
.output_stride
= 0;
266 for (i
= 0; i
< key
->nr_inputs
; i
++) {
267 fetch
.element
[i
].input_format
= key
->element
[i
].in
.format
;
268 fetch
.element
[i
].input_buffer
= key
->element
[i
].in
.buffer
;
269 fetch
.element
[i
].input_offset
= key
->element
[i
].in
.offset
;
270 fetch
.element
[i
].output_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
271 fetch
.element
[i
].output_offset
= fetch
.output_stride
;
272 fetch
.output_stride
+= 4 * sizeof(float);
276 emit
.nr_elements
= key
->nr_outputs
;
277 emit
.output_stride
= key
->output_stride
;
278 for (i
= 0; i
< key
->nr_outputs
; i
++) {
279 if (key
->element
[i
].out
.format
!= EMIT_1F_PSIZE
)
281 emit
.element
[i
].input_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
282 emit
.element
[i
].input_buffer
= 0;
283 emit
.element
[i
].input_offset
= key
->element
[i
].out
.vs_output
* 4 * sizeof(float);
284 emit
.element
[i
].output_format
= draw_translate_vinfo_format(key
->element
[i
].out
.format
);
285 emit
.element
[i
].output_offset
= key
->element
[i
].out
.offset
;
288 emit
.element
[i
].input_format
= PIPE_FORMAT_R32_FLOAT
;
289 emit
.element
[i
].input_buffer
= 1;
290 emit
.element
[i
].input_offset
= 0;
291 emit
.element
[i
].output_format
= PIPE_FORMAT_R32_FLOAT
;
292 emit
.element
[i
].output_offset
= key
->element
[i
].out
.offset
;
296 vsvg
->fetch
= draw_vs_get_fetch( vs
->draw
, &fetch
);
297 vsvg
->emit
= draw_vs_get_emit( vs
->draw
, &emit
);