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
->render
->set_primitive( draw
->render
,
87 /* Must do this after set_primitive() above:
89 vinfo
= draw
->render
->get_vertex_info(draw
->render
);
93 fse
->key
.output_stride
= vinfo
->size
* 4;
94 fse
->key
.nr_elements
= MAX2(num_vs_outputs
, /* outputs - translate to hw format */
95 num_vs_inputs
); /* inputs - fetch from api format */
97 fse
->key
.viewport
= !draw
->identity_viewport
;
98 fse
->key
.clip
= !draw
->bypass_clipping
;
101 memset(fse
->key
.element
, 0,
102 fse
->key
.nr_elements
* sizeof(fse
->key
.element
[0]));
104 for (i
= 0; i
< num_vs_inputs
; i
++) {
105 const struct pipe_vertex_element
*src
= &draw
->pt
.vertex_element
[i
];
106 fse
->key
.element
[i
].in
.format
= src
->src_format
;
108 /* Consider ignoring these, ie make generated programs
109 * independent of this state:
111 fse
->key
.element
[i
].in
.buffer
= src
->vertex_buffer_index
;
112 fse
->key
.element
[i
].in
.offset
= src
->src_offset
;
117 unsigned dst_offset
= 0;
119 for (i
= 0; i
< vinfo
->num_attribs
; i
++) {
120 unsigned emit_sz
= 0;
121 unsigned output_format
= PIPE_FORMAT_NONE
;
122 unsigned vs_output
= vinfo
->src_index
[i
];
124 switch (vinfo
->emit
[i
]) {
126 output_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
127 emit_sz
= 4 * sizeof(float);
130 output_format
= PIPE_FORMAT_R32G32B32_FLOAT
;
131 emit_sz
= 3 * sizeof(float);
134 output_format
= PIPE_FORMAT_R32G32_FLOAT
;
135 emit_sz
= 2 * sizeof(float);
138 output_format
= PIPE_FORMAT_R32_FLOAT
;
139 emit_sz
= 1 * sizeof(float);
143 output_format
= PIPE_FORMAT_R32_FLOAT
;
144 emit_sz
= 1 * sizeof(float);
145 vs_output
= num_vs_outputs
+ 1;
153 /* The elements in the key correspond to vertex shader output
154 * numbers, not to positions in the hw vertex description --
155 * that's handled by the output_offset field.
157 fse
->key
.element
[vs_output
].out
.format
= output_format
;
158 fse
->key
.element
[vs_output
].out
.offset
= dst_offset
;
160 dst_offset
+= emit_sz
;
161 assert(fse
->key
.output_stride
>= dst_offset
);
165 /* To make psize work, really need to tell the vertex shader to
166 * copy that value from input->output. For 'translate' this was
167 * implicit for all elements.
171 unsigned input
= num_vs_inputs
+ 1;
172 const struct pipe_vertex_element
*src
= &draw
->pt
.vertex_element
[i
];
173 fse
->key
.element
[i
].input_format
= PIPE_FORMAT_R32_FLOAT
;
174 fse
->key
.element
[i
].input_buffer
= 0; //nr_buffers + 1;
175 fse
->key
.element
[i
].input_offset
= 0;
177 fse
->key
.nr_elements
+= 1;
182 /* Would normally look up a vertex shader and peruse its list of
183 * varients somehow. We omitted that step and put all the
184 * hardcoded "shaders" into an array. We're just making the
185 * assumption that this happens to be a matching shader... ie
186 * you're running isosurf, aren't you?
188 fse
->active
= draw_vs_lookup_varient( draw
->vs
.vertex_shader
,
196 /* Now set buffer pointers:
198 for (i
= 0; i
< num_vs_inputs
; i
++) {
199 unsigned buf
= draw
->pt
.vertex_element
[i
].vertex_buffer_index
;
201 fse
->active
->set_input( fse
->active
,
204 ((const ubyte
*) draw
->pt
.user
.vbuffer
[buf
] +
205 draw
->pt
.vertex_buffer
[buf
].buffer_offset
),
207 draw
->pt
.vertex_buffer
[buf
].pitch
);
210 fse
->active
->set_constants( fse
->active
,
211 (const float (*)[4])draw
->pt
.user
.constants
);
213 fse
->active
->set_viewport( fse
->active
,
225 static void fse_run_linear( struct draw_pt_middle_end
*middle
,
229 struct fetch_shade_emit
*fse
= (struct fetch_shade_emit
*)middle
;
230 struct draw_context
*draw
= fse
->draw
;
231 unsigned alloc_count
= align(count
, 4);
234 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
236 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
238 hw_verts
= draw
->render
->allocate_vertices( draw
->render
,
239 (ushort
)fse
->key
.output_stride
,
240 (ushort
)alloc_count
);
247 /* Single routine to fetch vertices, run shader and emit HW verts.
248 * Clipping is done elsewhere -- either by the API or on hardware,
249 * or for some other reason not required...
251 fse
->active
->run_linear( fse
->active
,
255 /* Draw arrays path to avoid re-emitting index list again and
258 draw
->render
->draw_arrays( draw
->render
,
263 draw
->render
->release_vertices( draw
->render
,
265 fse
->key
.output_stride
,
271 fse_run(struct draw_pt_middle_end
*middle
,
272 const unsigned *fetch_elts
,
273 unsigned fetch_count
,
274 const ushort
*draw_elts
,
275 unsigned draw_count
)
277 struct fetch_shade_emit
*fse
= (struct fetch_shade_emit
*)middle
;
278 struct draw_context
*draw
= fse
->draw
;
279 unsigned alloc_count
= align(fetch_count
, 4);
282 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
284 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
286 hw_verts
= draw
->render
->allocate_vertices( draw
->render
,
287 (ushort
)fse
->key
.output_stride
,
288 (ushort
)alloc_count
);
295 /* Single routine to fetch vertices, run shader and emit HW verts.
297 fse
->active
->run_elts( fse
->active
,
302 draw
->render
->draw( draw
->render
,
306 draw
->render
->release_vertices( draw
->render
,
308 fse
->key
.output_stride
,
314 static void fse_finish( struct draw_pt_middle_end
*middle
)
320 fse_destroy( struct draw_pt_middle_end
*middle
)
325 struct draw_pt_middle_end
*draw_pt_middle_fse( struct draw_context
*draw
)
327 struct fetch_shade_emit
*fse
= CALLOC_STRUCT(fetch_shade_emit
);
331 fse
->base
.prepare
= fse_prepare
;
332 fse
->base
.run
= fse_run
;
333 fse
->base
.run_linear
= fse_run_linear
;
334 fse
->base
.finish
= fse_finish
;
335 fse
->base
.destroy
= fse_destroy
;