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"
42 /* A first pass at incorporating vertex fetch/emit functionality into
44 struct draw_vs_variant_generic
{
45 struct draw_vs_variant 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
;
59 unsigned temp_vertex_stride
;
66 static void vsvg_set_buffer( struct draw_vs_variant
*variant
,
72 struct draw_vs_variant_generic
*vsvg
= (struct draw_vs_variant_generic
*)variant
;
74 vsvg
->fetch
->set_buffer(vsvg
->fetch
,
82 /* Mainly for debug at this stage:
84 static void do_rhw_viewport( struct draw_vs_variant_generic
*vsvg
,
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
;
94 ptr
+= vsvg
->base
.vs
->position_output
* 4 * sizeof(float);
96 for (j
= 0; j
< count
; j
++, ptr
+= stride
) {
97 float *data
= (float *)ptr
;
98 float w
= 1.0f
/ data
[3];
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];
107 static void do_viewport( struct draw_vs_variant_generic
*vsvg
,
109 void *output_buffer
)
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
;
117 ptr
+= vsvg
->base
.vs
->position_output
* 4 * sizeof(float);
119 for (j
= 0; j
< count
; j
++, ptr
+= stride
) {
120 float *data
= (float *)ptr
;
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];
129 static void PIPE_CDECL
vsvg_run_elts( struct draw_vs_variant
*variant
,
130 const unsigned *elts
,
134 struct draw_vs_variant_generic
*vsvg
= (struct draw_vs_variant_generic
*)variant
;
135 unsigned temp_vertex_stride
= vsvg
->temp_vertex_stride
;
136 void *temp_buffer
= MALLOC( align(count
,4) * temp_vertex_stride
);
138 if (0) debug_printf("%s %d \n", __FUNCTION__
, count
);
140 /* Want to do this in small batches for cache locality?
143 vsvg
->fetch
->run_elts( vsvg
->fetch
,
146 vsvg
->draw
->instance_id
,
149 vsvg
->base
.vs
->run_linear( vsvg
->base
.vs
,
152 vsvg
->base
.vs
->draw
->pt
.user
.vs_constants
,
153 vsvg
->base
.vs
->draw
->pt
.user
.vs_constants_size
,
158 /* FIXME: geometry shading? */
160 if (vsvg
->base
.key
.clip
) {
161 /* not really handling clipping, just do the rhw so we can
164 do_rhw_viewport( vsvg
,
168 else if (vsvg
->base
.key
.viewport
) {
175 vsvg
->emit
->set_buffer( vsvg
->emit
,
181 vsvg
->emit
->set_buffer( vsvg
->emit
,
183 &vsvg
->draw
->rasterizer
->point_size
,
187 vsvg
->emit
->run( vsvg
->emit
,
189 vsvg
->draw
->instance_id
,
196 static void PIPE_CDECL
vsvg_run_linear( struct draw_vs_variant
*variant
,
199 void *output_buffer
)
201 struct draw_vs_variant_generic
*vsvg
= (struct draw_vs_variant_generic
*)variant
;
202 unsigned temp_vertex_stride
= vsvg
->temp_vertex_stride
;
203 void *temp_buffer
= MALLOC( align(count
,4) * temp_vertex_stride
);
205 if (0) debug_printf("%s %d %d (sz %d, %d)\n", __FUNCTION__
, start
, count
,
206 vsvg
->base
.key
.output_stride
,
209 vsvg
->fetch
->run( vsvg
->fetch
,
212 vsvg
->draw
->instance_id
,
215 vsvg
->base
.vs
->run_linear( vsvg
->base
.vs
,
218 vsvg
->base
.vs
->draw
->pt
.user
.vs_constants
,
219 vsvg
->base
.vs
->draw
->pt
.user
.vs_constants_size
,
224 if (vsvg
->base
.key
.clip
) {
225 /* not really handling clipping, just do the rhw so we can
228 do_rhw_viewport( vsvg
,
232 else if (vsvg
->base
.key
.viewport
) {
238 vsvg
->emit
->set_buffer( vsvg
->emit
,
244 vsvg
->emit
->set_buffer( vsvg
->emit
,
246 &vsvg
->draw
->rasterizer
->point_size
,
250 vsvg
->emit
->run( vsvg
->emit
,
252 vsvg
->draw
->instance_id
,
262 static void vsvg_destroy( struct draw_vs_variant
*variant
)
268 struct draw_vs_variant
*
269 draw_vs_create_variant_generic( struct draw_vertex_shader
*vs
,
270 const struct draw_vs_variant_key
*key
)
273 struct translate_key fetch
, emit
;
275 struct draw_vs_variant_generic
*vsvg
= CALLOC_STRUCT( draw_vs_variant_generic
);
279 vsvg
->base
.key
= *key
;
281 vsvg
->base
.set_buffer
= vsvg_set_buffer
;
282 vsvg
->base
.run_elts
= vsvg_run_elts
;
283 vsvg
->base
.run_linear
= vsvg_run_linear
;
284 vsvg
->base
.destroy
= vsvg_destroy
;
286 vsvg
->draw
= vs
->draw
;
288 vsvg
->temp_vertex_stride
= MAX2(key
->nr_inputs
,
289 vsvg
->base
.vs
->info
.num_outputs
) * 4 * sizeof(float);
291 /* Build free-standing fetch and emit functions:
293 fetch
.nr_elements
= key
->nr_inputs
;
294 fetch
.output_stride
= vsvg
->temp_vertex_stride
;
295 for (i
= 0; i
< key
->nr_inputs
; i
++) {
296 fetch
.element
[i
].type
= TRANSLATE_ELEMENT_NORMAL
;
297 fetch
.element
[i
].input_format
= key
->element
[i
].in
.format
;
298 fetch
.element
[i
].input_buffer
= key
->element
[i
].in
.buffer
;
299 fetch
.element
[i
].input_offset
= key
->element
[i
].in
.offset
;
300 fetch
.element
[i
].instance_divisor
= 0;
301 fetch
.element
[i
].output_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
302 fetch
.element
[i
].output_offset
= i
* 4 * sizeof(float);
303 assert(fetch
.element
[i
].output_offset
< fetch
.output_stride
);
307 emit
.nr_elements
= key
->nr_outputs
;
308 emit
.output_stride
= key
->output_stride
;
309 for (i
= 0; i
< key
->nr_outputs
; i
++) {
310 if (key
->element
[i
].out
.format
!= EMIT_1F_PSIZE
)
312 emit
.element
[i
].type
= TRANSLATE_ELEMENT_NORMAL
;
313 emit
.element
[i
].input_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
314 emit
.element
[i
].input_buffer
= 0;
315 emit
.element
[i
].input_offset
= key
->element
[i
].out
.vs_output
* 4 * sizeof(float);
316 emit
.element
[i
].instance_divisor
= 0;
317 emit
.element
[i
].output_format
= draw_translate_vinfo_format(key
->element
[i
].out
.format
);
318 emit
.element
[i
].output_offset
= key
->element
[i
].out
.offset
;
319 assert(emit
.element
[i
].input_offset
<= fetch
.output_stride
);
322 emit
.element
[i
].type
= TRANSLATE_ELEMENT_NORMAL
;
323 emit
.element
[i
].input_format
= PIPE_FORMAT_R32_FLOAT
;
324 emit
.element
[i
].input_buffer
= 1;
325 emit
.element
[i
].input_offset
= 0;
326 emit
.element
[i
].instance_divisor
= 0;
327 emit
.element
[i
].output_format
= PIPE_FORMAT_R32_FLOAT
;
328 emit
.element
[i
].output_offset
= key
->element
[i
].out
.offset
;
332 vsvg
->fetch
= draw_vs_get_fetch( vs
->draw
, &fetch
);
333 vsvg
->emit
= draw_vs_get_emit( vs
->draw
, &emit
);