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
;
65 const struct vertex_info
*vinfo
;
71 static void fse_prepare( struct draw_pt_middle_end
*middle
,
75 struct fetch_shade_emit
*fse
= (struct fetch_shade_emit
*)middle
;
76 struct draw_context
*draw
= fse
->draw
;
77 unsigned num_vs_inputs
= draw
->vs
.vertex_shader
->info
.num_inputs
;
78 const struct vertex_info
*vinfo
;
80 boolean need_psize
= 0;
83 if (!draw
->render
->set_primitive( draw
->render
,
89 /* Must do this after set_primitive() above:
91 fse
->vinfo
= vinfo
= draw
->render
->get_vertex_info(draw
->render
);
95 fse
->key
.output_stride
= vinfo
->size
* 4;
96 fse
->key
.nr_outputs
= vinfo
->num_attribs
;
97 fse
->key
.nr_inputs
= num_vs_inputs
;
99 fse
->key
.nr_elements
= MAX2(fse
->key
.nr_outputs
, /* outputs - translate to hw format */
100 fse
->key
.nr_inputs
); /* inputs - fetch from api format */
102 fse
->key
.viewport
= !draw
->identity_viewport
;
103 fse
->key
.clip
= !draw
->bypass_clipping
;
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
= vinfo
->num_attribs
+ 1;
153 output_format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
154 emit_sz
= 4 * sizeof(ubyte
);
161 /* The elements in the key correspond to vertex shader output
162 * numbers, not to positions in the hw vertex description --
163 * that's handled by the output_offset field.
165 fse
->key
.element
[vs_output
].out
.format
= output_format
;
166 fse
->key
.element
[vs_output
].out
.vs_output
= vs_output
;
167 fse
->key
.element
[vs_output
].out
.offset
= dst_offset
;
169 dst_offset
+= emit_sz
;
170 assert(fse
->key
.output_stride
>= dst_offset
);
174 /* To make psize work, really need to tell the vertex shader to
175 * copy that value from input->output. For 'translate' this was
176 * implicit for all elements.
180 unsigned input
= num_vs_inputs
+ 1;
181 const struct pipe_vertex_element
*src
= &draw
->pt
.vertex_element
[i
];
182 fse
->key
.element
[i
].input_format
= PIPE_FORMAT_R32_FLOAT
;
183 fse
->key
.element
[i
].input_buffer
= 0; //nr_buffers + 1;
184 fse
->key
.element
[i
].input_offset
= 0;
186 fse
->key
.nr_inputs
+= 1;
187 fse
->key
.nr_elements
= MAX2(fse
->key
.nr_inputs
,
188 fse
->key
.nr_outputs
);
193 /* Would normally look up a vertex shader and peruse its list of
194 * varients somehow. We omitted that step and put all the
195 * hardcoded "shaders" into an array. We're just making the
196 * assumption that this happens to be a matching shader... ie
197 * you're running isosurf, aren't you?
199 fse
->active
= draw_vs_lookup_varient( draw
->vs
.vertex_shader
,
207 /* Now set buffer pointers:
209 for (i
= 0; i
< num_vs_inputs
; i
++) {
210 unsigned buf
= draw
->pt
.vertex_element
[i
].vertex_buffer_index
;
212 fse
->active
->set_input( fse
->active
,
215 ((const ubyte
*) draw
->pt
.user
.vbuffer
[buf
] +
216 draw
->pt
.vertex_buffer
[buf
].buffer_offset
),
218 draw
->pt
.vertex_buffer
[buf
].pitch
);
221 fse
->active
->set_constants( fse
->active
,
222 (const float (*)[4])draw
->pt
.user
.constants
);
224 fse
->active
->set_viewport( fse
->active
,
236 static void fse_run_linear( struct draw_pt_middle_end
*middle
,
240 struct fetch_shade_emit
*fse
= (struct fetch_shade_emit
*)middle
;
241 struct draw_context
*draw
= fse
->draw
;
242 unsigned alloc_count
= align(count
, 4);
245 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
247 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
249 hw_verts
= draw
->render
->allocate_vertices( draw
->render
,
250 (ushort
)fse
->key
.output_stride
,
251 (ushort
)alloc_count
);
258 /* Single routine to fetch vertices, run shader and emit HW verts.
259 * Clipping is done elsewhere -- either by the API or on hardware,
260 * or for some other reason not required...
262 fse
->active
->run_linear( fse
->active
,
266 /* Draw arrays path to avoid re-emitting index list again and
269 draw
->render
->draw_arrays( draw
->render
,
275 for (i
= 0; i
< count
; i
++) {
276 debug_printf("\n\n%s vertex %d: (stride %d, offset %d)\n", __FUNCTION__
, i
,
277 fse
->key
.output_stride
,
278 fse
->key
.output_stride
* i
);
280 draw_dump_emitted_vertex( fse
->vinfo
,
281 (const uint8_t *)hw_verts
+ fse
->key
.output_stride
* i
);
286 draw
->render
->release_vertices( draw
->render
,
288 fse
->key
.output_stride
,
294 fse_run(struct draw_pt_middle_end
*middle
,
295 const unsigned *fetch_elts
,
296 unsigned fetch_count
,
297 const ushort
*draw_elts
,
298 unsigned draw_count
)
300 struct fetch_shade_emit
*fse
= (struct fetch_shade_emit
*)middle
;
301 struct draw_context
*draw
= fse
->draw
;
302 unsigned alloc_count
= align(fetch_count
, 4);
305 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
307 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
309 hw_verts
= draw
->render
->allocate_vertices( draw
->render
,
310 (ushort
)fse
->key
.output_stride
,
311 (ushort
)alloc_count
);
318 /* Single routine to fetch vertices, run shader and emit HW verts.
320 fse
->active
->run_elts( fse
->active
,
325 draw
->render
->draw( draw
->render
,
331 for (i
= 0; i
< fetch_count
; i
++) {
332 debug_printf("\n\n%s vertex %d:\n", __FUNCTION__
, i
);
333 draw_dump_emitted_vertex( fse
->vinfo
,
334 (const uint8_t *)hw_verts
+
335 fse
->key
.output_stride
* i
);
340 draw
->render
->release_vertices( draw
->render
,
342 fse
->key
.output_stride
,
348 static void fse_finish( struct draw_pt_middle_end
*middle
)
354 fse_destroy( struct draw_pt_middle_end
*middle
)
359 struct draw_pt_middle_end
*draw_pt_middle_fse( struct draw_context
*draw
)
361 struct fetch_shade_emit
*fse
= CALLOC_STRUCT(fetch_shade_emit
);
365 fse
->base
.prepare
= fse_prepare
;
366 fse
->base
.run
= fse_run
;
367 fse
->base
.run_linear
= fse_run_linear
;
368 fse
->base
.finish
= fse_finish
;
369 fse
->base
.destroy
= fse_destroy
;