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 "pipe/p_util.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_pt.h"
40 #include "draw/draw_vs.h"
42 #include "translate/translate.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
;
68 static void fse_prepare( struct draw_pt_middle_end
*middle
,
72 struct fetch_shade_emit
*fse
= (struct fetch_shade_emit
*)middle
;
73 struct draw_context
*draw
= fse
->draw
;
74 unsigned num_vs_inputs
= draw
->vs
.vertex_shader
->info
.num_inputs
;
75 unsigned num_vs_outputs
= draw
->vs
.vertex_shader
->info
.num_outputs
;
76 const struct vertex_info
*vinfo
;
78 boolean need_psize
= 0;
81 if (draw
->pt
.user
.elts
) {
86 if (!draw
->render
->set_primitive( draw
->render
,
92 /* Must do this after set_primitive() above:
94 vinfo
= draw
->render
->get_vertex_info(draw
->render
);
98 fse
->key
.output_stride
= vinfo
->size
* 4;
99 fse
->key
.nr_elements
= MAX2(num_vs_outputs
, /* outputs - translate to hw format */
100 num_vs_inputs
); /* inputs - fetch from api format */
102 fse
->key
.viewport
= 1;
106 memset(fse
->key
.element
, 0,
107 fse
->key
.nr_elements
* sizeof(fse
->key
.element
[0]));
109 for (i
= 0; i
< num_vs_inputs
; i
++) {
110 const struct pipe_vertex_element
*src
= &draw
->pt
.vertex_element
[i
];
111 fse
->key
.element
[i
].in
.format
= src
->src_format
;
113 /* Consider ignoring these, ie make generated programs
114 * independent of this state:
116 fse
->key
.element
[i
].in
.buffer
= src
->vertex_buffer_index
;
117 fse
->key
.element
[i
].in
.offset
= src
->src_offset
;
122 unsigned dst_offset
= 0;
124 for (i
= 0; i
< vinfo
->num_attribs
; i
++) {
125 unsigned emit_sz
= 0;
126 unsigned output_format
= PIPE_FORMAT_NONE
;
127 unsigned vs_output
= vinfo
->src_index
[i
];
129 switch (vinfo
->emit
[i
]) {
131 output_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
132 emit_sz
= 4 * sizeof(float);
135 output_format
= PIPE_FORMAT_R32G32B32_FLOAT
;
136 emit_sz
= 3 * sizeof(float);
139 output_format
= PIPE_FORMAT_R32G32_FLOAT
;
140 emit_sz
= 2 * sizeof(float);
143 output_format
= PIPE_FORMAT_R32_FLOAT
;
144 emit_sz
= 1 * sizeof(float);
148 output_format
= PIPE_FORMAT_R32_FLOAT
;
149 emit_sz
= 1 * sizeof(float);
150 vs_output
= num_vs_outputs
+ 1;
158 /* The elements in the key correspond to vertex shader output
159 * numbers, not to positions in the hw vertex description --
160 * that's handled by the output_offset field.
162 fse
->key
.element
[vs_output
].out
.format
= output_format
;
163 fse
->key
.element
[vs_output
].out
.offset
= dst_offset
;
165 dst_offset
+= emit_sz
;
166 assert(fse
->key
.output_stride
>= dst_offset
);
170 /* To make psize work, really need to tell the vertex shader to
171 * copy that value from input->output. For 'translate' this was
172 * implicit for all elements.
176 unsigned input
= num_vs_inputs
+ 1;
177 const struct pipe_vertex_element
*src
= &draw
->pt
.vertex_element
[i
];
178 fse
->key
.element
[i
].input_format
= PIPE_FORMAT_R32_FLOAT
;
179 fse
->key
.element
[i
].input_buffer
= 0; //nr_buffers + 1;
180 fse
->key
.element
[i
].input_offset
= 0;
182 fse
->key
.nr_elements
+= 1;
187 /* Would normally look up a vertex shader and peruse its list of
188 * varients somehow. We omitted that step and put all the
189 * hardcoded "shaders" into an array. We're just making the
190 * assumption that this happens to be a matching shader... ie
191 * you're running isosurf, aren't you?
193 fse
->active
= draw_vs_lookup_varient( draw
->vs
.vertex_shader
,
201 /* Now set buffer pointers:
203 for (i
= 0; i
< num_vs_inputs
; i
++) {
204 unsigned buf
= draw
->pt
.vertex_element
[i
].vertex_buffer_index
;
206 fse
->active
->set_input( fse
->active
,
209 ((const ubyte
*) draw
->pt
.user
.vbuffer
[buf
] +
210 draw
->pt
.vertex_buffer
[buf
].buffer_offset
),
212 draw
->pt
.vertex_buffer
[buf
].pitch
);
215 fse
->active
->set_constants( fse
->active
,
216 (const float (*)[4])draw
->pt
.user
.constants
);
218 fse
->active
->set_viewport( fse
->active
,
230 static void fse_run_linear( struct draw_pt_middle_end
*middle
,
234 struct fetch_shade_emit
*fse
= (struct fetch_shade_emit
*)middle
;
235 struct draw_context
*draw
= fse
->draw
;
236 unsigned alloc_count
= align(count
, 4);
239 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
241 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
243 hw_verts
= draw
->render
->allocate_vertices( draw
->render
,
244 (ushort
)fse
->key
.output_stride
,
245 (ushort
)alloc_count
);
252 /* Single routine to fetch vertices, run shader and emit HW verts.
253 * Clipping and viewport transformation are done elsewhere --
254 * either by the API or on hardware, or for some other reason not
257 fse
->active
->run_linear( fse
->active
,
261 /* Draw arrays path to avoid re-emitting index list again and
264 draw
->render
->draw_arrays( draw
->render
,
269 draw
->render
->release_vertices( draw
->render
,
271 fse
->key
.output_stride
,
277 fse_run(struct draw_pt_middle_end
*middle
,
278 const unsigned *fetch_elts
,
279 unsigned fetch_count
,
280 const ushort
*draw_elts
,
281 unsigned draw_count
)
283 struct fetch_shade_emit
*fse
= (struct fetch_shade_emit
*)middle
;
284 struct draw_context
*draw
= fse
->draw
;
285 unsigned alloc_count
= align(fetch_count
, 4);
288 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
290 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
292 hw_verts
= draw
->render
->allocate_vertices( draw
->render
,
293 (ushort
)fse
->key
.output_stride
,
294 (ushort
)alloc_count
);
301 /* Single routine to fetch vertices, run shader and emit HW verts.
303 fse
->active
->run_elts( fse
->active
,
308 draw
->render
->draw( draw
->render
,
312 draw
->render
->release_vertices( draw
->render
,
314 fse
->key
.output_stride
,
320 static void fse_finish( struct draw_pt_middle_end
*middle
)
326 fse_destroy( struct draw_pt_middle_end
*middle
)
331 struct draw_pt_middle_end
*draw_pt_middle_fse( struct draw_context
*draw
)
333 struct fetch_shade_emit
*fse
= CALLOC_STRUCT(fetch_shade_emit
);
337 fse
->base
.prepare
= fse_prepare
;
338 fse
->base
.run
= fse_run
;
339 fse
->base
.run_linear
= fse_run_linear
;
340 fse
->base
.finish
= fse_finish
;
341 fse
->base
.destroy
= fse_destroy
;