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>
34 #include "util/u_math.h"
35 #include "util/u_memory.h"
36 #include "draw/draw_context.h"
37 #include "draw/draw_private.h"
38 #include "draw/draw_vbuf.h"
39 #include "draw/draw_vertex.h"
40 #include "draw/draw_pt.h"
41 #include "draw/draw_vs.h"
44 struct fetch_shade_emit
;
47 /* Prototype fetch, shade, emit-hw-verts all in one go.
49 struct fetch_shade_emit
{
50 struct draw_pt_middle_end base
;
51 struct draw_context
*draw
;
56 const float *constants
;
57 unsigned pitch
[PIPE_MAX_ATTRIBS
];
58 const ubyte
*src
[PIPE_MAX_ATTRIBS
];
61 struct draw_vs_varient_key key
;
62 struct draw_vs_varient
*active
;
65 const struct vertex_info
*vinfo
;
71 static void fse_prepare( struct draw_pt_middle_end
*middle
,
74 unsigned *max_vertices
)
76 struct fetch_shade_emit
*fse
= (struct fetch_shade_emit
*)middle
;
77 struct draw_context
*draw
= fse
->draw
;
78 unsigned num_vs_inputs
= draw
->vs
.vertex_shader
->info
.num_inputs
;
79 const struct vertex_info
*vinfo
;
84 if (!draw
->render
->set_primitive( draw
->render
,
90 /* Must do this after set_primitive() above:
92 fse
->vinfo
= vinfo
= draw
->render
->get_vertex_info(draw
->render
);
96 fse
->key
.output_stride
= vinfo
->size
* 4;
97 fse
->key
.nr_outputs
= vinfo
->num_attribs
;
98 fse
->key
.nr_inputs
= num_vs_inputs
;
100 fse
->key
.nr_elements
= MAX2(fse
->key
.nr_outputs
, /* outputs - translate to hw format */
101 fse
->key
.nr_inputs
); /* inputs - fetch from api format */
103 fse
->key
.viewport
= (!draw
->rasterizer
->bypass_vs_clip_and_viewport
&&
104 !draw
->identity_viewport
);
105 fse
->key
.clip
= !draw
->bypass_clipping
;
106 fse
->key
.const_vbuffers
= 0;
108 memset(fse
->key
.element
, 0,
109 fse
->key
.nr_elements
* sizeof(fse
->key
.element
[0]));
111 for (i
= 0; i
< num_vs_inputs
; i
++) {
112 const struct pipe_vertex_element
*src
= &draw
->pt
.vertex_element
[i
];
113 fse
->key
.element
[i
].in
.format
= src
->src_format
;
115 /* Consider ignoring these, ie make generated programs
116 * independent of this state:
118 fse
->key
.element
[i
].in
.buffer
= src
->vertex_buffer_index
;
119 fse
->key
.element
[i
].in
.offset
= src
->src_offset
;
120 nr_vbs
= MAX2(nr_vbs
, src
->vertex_buffer_index
+ 1);
123 for (i
= 0; i
< 5 && i
< nr_vbs
; i
++) {
124 if (draw
->pt
.vertex_buffer
[i
].stride
== 0)
125 fse
->key
.const_vbuffers
|= (1<<i
);
128 if (0) debug_printf("%s: lookup const_vbuffers: %x\n", __FUNCTION__
, fse
->key
.const_vbuffers
);
131 unsigned dst_offset
= 0;
133 for (i
= 0; i
< vinfo
->num_attribs
; i
++) {
134 unsigned emit_sz
= 0;
136 switch (vinfo
->attrib
[i
].emit
) {
138 emit_sz
= 4 * sizeof(float);
141 emit_sz
= 3 * sizeof(float);
144 emit_sz
= 2 * sizeof(float);
147 emit_sz
= 1 * sizeof(float);
150 emit_sz
= 1 * sizeof(float);
153 emit_sz
= 4 * sizeof(ubyte
);
160 /* The elements in the key correspond to vertex shader output
161 * numbers, not to positions in the hw vertex description --
162 * that's handled by the output_offset field.
164 fse
->key
.element
[i
].out
.format
= vinfo
->attrib
[i
].emit
;
165 fse
->key
.element
[i
].out
.vs_output
= vinfo
->attrib
[i
].src_index
;
166 fse
->key
.element
[i
].out
.offset
= dst_offset
;
168 dst_offset
+= emit_sz
;
169 assert(fse
->key
.output_stride
>= dst_offset
);
174 fse
->active
= draw_vs_lookup_varient( draw
->vs
.vertex_shader
,
182 if (0) debug_printf("%s: found const_vbuffers: %x\n", __FUNCTION__
,
183 fse
->active
->key
.const_vbuffers
);
185 /* Now set buffer pointers:
187 for (i
= 0; i
< draw
->pt
.nr_vertex_buffers
; i
++) {
188 fse
->active
->set_buffer( fse
->active
,
190 ((const ubyte
*) draw
->pt
.user
.vbuffer
[i
] +
191 draw
->pt
.vertex_buffer
[i
].buffer_offset
),
192 draw
->pt
.vertex_buffer
[i
].stride
);
195 *max_vertices
= (draw
->render
->max_vertex_buffer_bytes
/
198 /* Return an even number of verts.
199 * This prevents "parity" errors when splitting long triangle strips which
200 * can lead to front/back culling mix-ups.
201 * Every other triangle in a strip has an alternate front/back orientation
202 * so splitting at an odd position can cause the orientation of subsequent
203 * triangles to get reversed.
205 *max_vertices
= *max_vertices
& ~1;
207 /* Probably need to do this somewhere (or fix exec shader not to
211 struct draw_vertex_shader
*vs
= draw
->vs
.vertex_shader
;
212 vs
->prepare(vs
, draw
);
218 static void fse_run_linear( struct draw_pt_middle_end
*middle
,
222 struct fetch_shade_emit
*fse
= (struct fetch_shade_emit
*)middle
;
223 struct draw_context
*draw
= fse
->draw
;
226 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
228 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
230 if (count
>= UNDEFINED_VERTEX_ID
)
233 if (!draw
->render
->allocate_vertices( draw
->render
,
234 (ushort
)fse
->key
.output_stride
,
238 hw_verts
= draw
->render
->map_vertices( draw
->render
);
242 /* Single routine to fetch vertices, run shader and emit HW verts.
243 * Clipping is done elsewhere -- either by the API or on hardware,
244 * or for some other reason not required...
246 fse
->active
->run_linear( fse
->active
,
253 for (i
= 0; i
< count
; i
++) {
254 debug_printf("\n\n%s vertex %d: (stride %d, offset %d)\n", __FUNCTION__
, i
,
255 fse
->key
.output_stride
,
256 fse
->key
.output_stride
* i
);
258 draw_dump_emitted_vertex( fse
->vinfo
,
259 (const uint8_t *)hw_verts
+ fse
->key
.output_stride
* i
);
263 draw
->render
->unmap_vertices( draw
->render
, 0, (ushort
)(count
- 1) );
265 /* Draw arrays path to avoid re-emitting index list again and
268 draw
->render
->draw_arrays( draw
->render
,
273 draw
->render
->release_vertices( draw
->render
);
284 fse_run(struct draw_pt_middle_end
*middle
,
285 const unsigned *fetch_elts
,
286 unsigned fetch_count
,
287 const ushort
*draw_elts
,
288 unsigned draw_count
)
290 struct fetch_shade_emit
*fse
= (struct fetch_shade_emit
*)middle
;
291 struct draw_context
*draw
= fse
->draw
;
294 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
296 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
298 if (fetch_count
>= UNDEFINED_VERTEX_ID
)
301 if (!draw
->render
->allocate_vertices( draw
->render
,
302 (ushort
)fse
->key
.output_stride
,
303 (ushort
)fetch_count
))
306 hw_verts
= draw
->render
->map_vertices( draw
->render
);
311 /* Single routine to fetch vertices, run shader and emit HW verts.
313 fse
->active
->run_elts( fse
->active
,
321 for (i
= 0; i
< fetch_count
; i
++) {
322 debug_printf("\n\n%s vertex %d:\n", __FUNCTION__
, i
);
323 draw_dump_emitted_vertex( fse
->vinfo
,
324 (const uint8_t *)hw_verts
+
325 fse
->key
.output_stride
* i
);
329 draw
->render
->unmap_vertices( draw
->render
, 0, (ushort
)(fetch_count
- 1) );
331 draw
->render
->draw( draw
->render
,
336 draw
->render
->release_vertices( draw
->render
);
346 static boolean
fse_run_linear_elts( struct draw_pt_middle_end
*middle
,
349 const ushort
*draw_elts
,
350 unsigned draw_count
)
352 struct fetch_shade_emit
*fse
= (struct fetch_shade_emit
*)middle
;
353 struct draw_context
*draw
= fse
->draw
;
356 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
358 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
360 if (count
>= UNDEFINED_VERTEX_ID
)
363 if (!draw
->render
->allocate_vertices( draw
->render
,
364 (ushort
)fse
->key
.output_stride
,
368 hw_verts
= draw
->render
->map_vertices( draw
->render
);
372 /* Single routine to fetch vertices, run shader and emit HW verts.
373 * Clipping is done elsewhere -- either by the API or on hardware,
374 * or for some other reason not required...
376 fse
->active
->run_linear( fse
->active
,
381 draw
->render
->draw( draw
->render
,
386 draw
->render
->unmap_vertices( draw
->render
, 0, (ushort
)(count
- 1) );
388 draw
->render
->release_vertices( draw
->render
);
395 static void fse_finish( struct draw_pt_middle_end
*middle
)
401 fse_destroy( struct draw_pt_middle_end
*middle
)
406 struct draw_pt_middle_end
*draw_pt_middle_fse( struct draw_context
*draw
)
408 struct fetch_shade_emit
*fse
= CALLOC_STRUCT(fetch_shade_emit
);
412 fse
->base
.prepare
= fse_prepare
;
413 fse
->base
.run
= fse_run
;
414 fse
->base
.run_linear
= fse_run_linear
;
415 fse
->base
.run_linear_elts
= fse_run_linear_elts
;
416 fse
->base
.finish
= fse_finish
;
417 fse
->base
.destroy
= fse_destroy
;