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
;
55 const float *constants
;
56 unsigned pitch
[PIPE_MAX_ATTRIBS
];
57 const ubyte
*src
[PIPE_MAX_ATTRIBS
];
60 struct draw_vs_variant_key key
;
61 struct draw_vs_variant
*active
;
63 const struct vertex_info
*vinfo
;
69 fse_prepare(struct draw_pt_middle_end
*middle
,
72 unsigned *max_vertices
)
74 struct fetch_shade_emit
*fse
= (struct fetch_shade_emit
*)middle
;
75 struct draw_context
*draw
= fse
->draw
;
76 unsigned num_vs_inputs
= draw
->vs
.vertex_shader
->info
.num_inputs
;
77 const struct vertex_info
*vinfo
;
81 /* Can't support geometry shader on this path.
83 assert(!draw
->gs
.geometry_shader
);
85 draw
->render
->set_primitive(draw
->render
, prim
);
87 /* Must do this after set_primitive() above:
89 fse
->vinfo
= vinfo
= draw
->render
->get_vertex_info(draw
->render
);
91 fse
->key
.output_stride
= vinfo
->size
* 4;
92 fse
->key
.nr_outputs
= vinfo
->num_attribs
;
93 fse
->key
.nr_inputs
= num_vs_inputs
;
95 fse
->key
.nr_elements
= MAX2(fse
->key
.nr_outputs
, /* outputs - translate to hw format */
96 fse
->key
.nr_inputs
); /* inputs - fetch from api format */
98 fse
->key
.viewport
= !draw
->identity_viewport
;
99 fse
->key
.clip
= draw
->clip_xy
|| draw
->clip_z
|| draw
->clip_user
;
100 fse
->key
.const_vbuffers
= 0;
102 memset(fse
->key
.element
, 0,
103 fse
->key
.nr_elements
* sizeof(fse
->key
.element
[0]));
105 for (i
= 0; i
< num_vs_inputs
; i
++) {
106 const struct pipe_vertex_element
*src
= &draw
->pt
.vertex_element
[i
];
107 fse
->key
.element
[i
].in
.format
= src
->src_format
;
109 /* Consider ignoring these, ie make generated programs
110 * independent of this state:
112 fse
->key
.element
[i
].in
.buffer
= src
->vertex_buffer_index
;
113 fse
->key
.element
[i
].in
.offset
= src
->src_offset
;
114 nr_vbs
= MAX2(nr_vbs
, src
->vertex_buffer_index
+ 1);
117 for (i
= 0; i
< 5 && i
< nr_vbs
; i
++) {
118 if (draw
->pt
.vertex_buffer
[i
].stride
== 0)
119 fse
->key
.const_vbuffers
|= (1<<i
);
122 if (0) debug_printf("%s: lookup const_vbuffers: %x\n", __FUNCTION__
, fse
->key
.const_vbuffers
);
125 unsigned dst_offset
= 0;
127 for (i
= 0; i
< vinfo
->num_attribs
; i
++) {
128 unsigned emit_sz
= draw_translate_vinfo_size(vinfo
->attrib
[i
].emit
);
130 /* doesn't handle EMIT_OMIT */
131 assert(emit_sz
!= 0);
133 /* The elements in the key correspond to vertex shader output
134 * numbers, not to positions in the hw vertex description --
135 * that's handled by the output_offset field.
137 fse
->key
.element
[i
].out
.format
= vinfo
->attrib
[i
].emit
;
138 fse
->key
.element
[i
].out
.vs_output
= vinfo
->attrib
[i
].src_index
;
139 fse
->key
.element
[i
].out
.offset
= dst_offset
;
141 dst_offset
+= emit_sz
;
142 assert(fse
->key
.output_stride
>= dst_offset
);
146 fse
->active
= draw_vs_lookup_variant( draw
->vs
.vertex_shader
,
154 if (0) debug_printf("%s: found const_vbuffers: %x\n", __FUNCTION__
,
155 fse
->active
->key
.const_vbuffers
);
157 /* Now set buffer pointers:
159 for (i
= 0; i
< draw
->pt
.nr_vertex_buffers
; i
++) {
160 fse
->active
->set_buffer( fse
->active
,
162 ((const ubyte
*) draw
->pt
.user
.vbuffer
[i
] +
163 draw
->pt
.vertex_buffer
[i
].buffer_offset
),
164 draw
->pt
.vertex_buffer
[i
].stride
,
165 draw
->pt
.max_index
);
168 *max_vertices
= (draw
->render
->max_vertex_buffer_bytes
/
171 /* Probably need to do this somewhere (or fix exec shader not to
175 struct draw_vertex_shader
*vs
= draw
->vs
.vertex_shader
;
176 vs
->prepare(vs
, draw
);
183 fse_run_linear(struct draw_pt_middle_end
*middle
,
188 struct fetch_shade_emit
*fse
= (struct fetch_shade_emit
*)middle
;
189 struct draw_context
*draw
= fse
->draw
;
192 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
194 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
196 if (!draw
->render
->allocate_vertices( draw
->render
,
197 (ushort
)fse
->key
.output_stride
,
201 hw_verts
= draw
->render
->map_vertices( draw
->render
);
205 /* Single routine to fetch vertices, run shader and emit HW verts.
206 * Clipping is done elsewhere -- either by the API or on hardware,
207 * or for some other reason not required...
209 fse
->active
->run_linear( fse
->active
,
215 for (i
= 0; i
< count
; i
++) {
216 debug_printf("\n\n%s vertex %d: (stride %d, offset %d)\n", __FUNCTION__
, i
,
217 fse
->key
.output_stride
,
218 fse
->key
.output_stride
* i
);
220 draw_dump_emitted_vertex( fse
->vinfo
,
221 (const uint8_t *)hw_verts
+ fse
->key
.output_stride
* i
);
225 draw
->render
->unmap_vertices( draw
->render
, 0, (ushort
)(count
- 1) );
227 /* Draw arrays path to avoid re-emitting index list again and
230 draw
->render
->draw_arrays( draw
->render
,
234 draw
->render
->release_vertices( draw
->render
);
239 debug_warn_once("allocate or map of vertex buffer failed (out of memory?)");
245 fse_run(struct draw_pt_middle_end
*middle
,
246 const unsigned *fetch_elts
,
247 unsigned fetch_count
,
248 const ushort
*draw_elts
,
250 unsigned prim_flags
)
252 struct fetch_shade_emit
*fse
= (struct fetch_shade_emit
*)middle
;
253 struct draw_context
*draw
= fse
->draw
;
256 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
258 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
260 if (!draw
->render
->allocate_vertices( draw
->render
,
261 (ushort
)fse
->key
.output_stride
,
262 (ushort
)fetch_count
))
265 hw_verts
= draw
->render
->map_vertices( draw
->render
);
269 /* Single routine to fetch vertices, run shader and emit HW verts.
271 fse
->active
->run_elts( fse
->active
,
278 for (i
= 0; i
< fetch_count
; i
++) {
279 debug_printf("\n\n%s vertex %d:\n", __FUNCTION__
, i
);
280 draw_dump_emitted_vertex( fse
->vinfo
,
281 (const uint8_t *)hw_verts
+
282 fse
->key
.output_stride
* i
);
286 draw
->render
->unmap_vertices( draw
->render
, 0, (ushort
)(fetch_count
- 1) );
288 draw
->render
->draw_elements( draw
->render
,
292 draw
->render
->release_vertices( draw
->render
);
296 debug_warn_once("allocate or map of vertex buffer failed (out of memory?)");
303 fse_run_linear_elts(struct draw_pt_middle_end
*middle
,
306 const ushort
*draw_elts
,
310 struct fetch_shade_emit
*fse
= (struct fetch_shade_emit
*)middle
;
311 struct draw_context
*draw
= fse
->draw
;
314 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
316 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
318 if (!draw
->render
->allocate_vertices( draw
->render
,
319 (ushort
)fse
->key
.output_stride
,
323 hw_verts
= draw
->render
->map_vertices( draw
->render
);
327 /* Single routine to fetch vertices, run shader and emit HW verts.
328 * Clipping is done elsewhere -- either by the API or on hardware,
329 * or for some other reason not required...
331 fse
->active
->run_linear( fse
->active
,
335 draw
->render
->draw_elements( draw
->render
,
339 draw
->render
->unmap_vertices( draw
->render
, 0, (ushort
)(count
- 1) );
341 draw
->render
->release_vertices( draw
->render
);
349 fse_finish(struct draw_pt_middle_end
*middle
)
355 fse_destroy(struct draw_pt_middle_end
*middle
)
361 struct draw_pt_middle_end
*
362 draw_pt_middle_fse(struct draw_context
*draw
)
364 struct fetch_shade_emit
*fse
= CALLOC_STRUCT(fetch_shade_emit
);
368 fse
->base
.prepare
= fse_prepare
;
369 fse
->base
.run
= fse_run
;
370 fse
->base
.run_linear
= fse_run_linear
;
371 fse
->base
.run_linear_elts
= fse_run_linear_elts
;
372 fse
->base
.finish
= fse_finish
;
373 fse
->base
.destroy
= fse_destroy
;