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 **************************************************************************/
28 #include "util/u_math.h"
29 #include "util/u_memory.h"
30 #include "draw/draw_context.h"
31 #include "draw/draw_vbuf.h"
32 #include "draw/draw_vertex.h"
33 #include "draw/draw_pt.h"
34 #include "draw/draw_vs.h"
35 #include "translate/translate.h"
38 struct fetch_pipeline_middle_end
{
39 struct draw_pt_middle_end base
;
40 struct draw_context
*draw
;
43 struct pt_fetch
*fetch
;
44 struct pt_post_vs
*post_vs
;
46 unsigned vertex_data_offset
;
53 static void fetch_pipeline_prepare( struct draw_pt_middle_end
*middle
,
56 unsigned *max_vertices
)
58 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
59 struct draw_context
*draw
= fpme
->draw
;
60 struct draw_vertex_shader
*vs
= draw
->vs
.vertex_shader
;
62 /* Add one to num_outputs because the pipeline occasionally tags on
63 * an additional texcoord, eg for AA lines.
65 unsigned nr
= MAX2( vs
->info
.num_inputs
,
66 vs
->info
.num_outputs
+ 1 );
71 /* Always leave room for the vertex header whether we need it or
72 * not. It's hard to get rid of it in particular because of the
73 * viewport code in draw_pt_post_vs.c.
75 fpme
->vertex_size
= sizeof(struct vertex_header
) + nr
* 4 * sizeof(float);
79 draw_pt_fetch_prepare( fpme
->fetch
,
82 /* XXX: it's not really gl rasterization rules we care about here,
83 * but gl vs dx9 clip spaces.
85 draw_pt_post_vs_prepare( fpme
->post_vs
,
86 (boolean
)draw
->bypass_clipping
,
87 (boolean
)draw
->identity_viewport
,
88 (boolean
)draw
->rasterizer
->gl_rasterization_rules
);
91 if (!(opt
& PT_PIPELINE
)) {
92 draw_pt_emit_prepare( fpme
->emit
,
96 *max_vertices
= MAX2( *max_vertices
,
97 DRAW_PIPE_MAX_VERTICES
);
100 *max_vertices
= DRAW_PIPE_MAX_VERTICES
;
103 /* return even number */
104 *max_vertices
= *max_vertices
& ~1;
106 /* No need to prepare the shader.
108 vs
->prepare(vs
, draw
);
113 static void fetch_pipeline_run( struct draw_pt_middle_end
*middle
,
114 const unsigned *fetch_elts
,
115 unsigned fetch_count
,
116 const ushort
*draw_elts
,
117 unsigned draw_count
)
119 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
120 struct draw_context
*draw
= fpme
->draw
;
121 struct draw_vertex_shader
*shader
= draw
->vs
.vertex_shader
;
122 unsigned opt
= fpme
->opt
;
123 unsigned alloc_count
= align( fetch_count
, 4 );
125 struct vertex_header
*pipeline_verts
=
126 (struct vertex_header
*)MALLOC(fpme
->vertex_size
* alloc_count
);
128 if (!pipeline_verts
) {
129 /* Not much we can do here - just skip the rendering.
135 /* Fetch into our vertex buffer
137 draw_pt_fetch_run( fpme
->fetch
,
140 (char *)pipeline_verts
);
142 /* Run the shader, note that this overwrites the data[] parts of
143 * the pipeline verts. If there is no shader, ie a bypass shader,
144 * then the inputs == outputs, and are already in the correct
149 shader
->run_linear(shader
,
150 (const float (*)[4])pipeline_verts
->data
,
151 ( float (*)[4])pipeline_verts
->data
,
152 (const float (*)[4])draw
->pt
.user
.constants
,
158 if (draw_pt_post_vs_run( fpme
->post_vs
,
166 /* Do we need to run the pipeline?
168 if (opt
& PT_PIPELINE
) {
169 draw_pipeline_run( fpme
->draw
,
178 draw_pt_emit( fpme
->emit
,
179 (const float (*)[4])pipeline_verts
->data
,
187 FREE(pipeline_verts
);
191 static void fetch_pipeline_linear_run( struct draw_pt_middle_end
*middle
,
195 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
196 struct draw_context
*draw
= fpme
->draw
;
197 struct draw_vertex_shader
*shader
= draw
->vs
.vertex_shader
;
198 unsigned opt
= fpme
->opt
;
199 unsigned alloc_count
= align( count
, 4 );
201 struct vertex_header
*pipeline_verts
=
202 (struct vertex_header
*)MALLOC(fpme
->vertex_size
* alloc_count
);
204 if (!pipeline_verts
) {
205 /* Not much we can do here - just skip the rendering.
211 /* Fetch into our vertex buffer
213 draw_pt_fetch_run_linear( fpme
->fetch
,
216 (char *)pipeline_verts
);
218 /* Run the shader, note that this overwrites the data[] parts of
219 * the pipeline verts. If there is no shader, ie a bypass shader,
220 * then the inputs == outputs, and are already in the correct
225 shader
->run_linear(shader
,
226 (const float (*)[4])pipeline_verts
->data
,
227 ( float (*)[4])pipeline_verts
->data
,
228 (const float (*)[4])draw
->pt
.user
.constants
,
234 if (draw_pt_post_vs_run( fpme
->post_vs
,
242 /* Do we need to run the pipeline?
244 if (opt
& PT_PIPELINE
) {
245 draw_pipeline_run_linear( fpme
->draw
,
252 draw_pt_emit_linear( fpme
->emit
,
253 (const float (*)[4])pipeline_verts
->data
,
258 FREE(pipeline_verts
);
263 static boolean
fetch_pipeline_linear_run_elts( struct draw_pt_middle_end
*middle
,
266 const ushort
*draw_elts
,
267 unsigned draw_count
)
269 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
270 struct draw_context
*draw
= fpme
->draw
;
271 struct draw_vertex_shader
*shader
= draw
->vs
.vertex_shader
;
272 unsigned opt
= fpme
->opt
;
273 unsigned alloc_count
= align( count
, 4 );
275 struct vertex_header
*pipeline_verts
=
276 (struct vertex_header
*)MALLOC(fpme
->vertex_size
* alloc_count
);
281 /* Fetch into our vertex buffer
283 draw_pt_fetch_run_linear( fpme
->fetch
,
286 (char *)pipeline_verts
);
288 /* Run the shader, note that this overwrites the data[] parts of
289 * the pipeline verts. If there is no shader, ie a bypass shader,
290 * then the inputs == outputs, and are already in the correct
295 shader
->run_linear(shader
,
296 (const float (*)[4])pipeline_verts
->data
,
297 ( float (*)[4])pipeline_verts
->data
,
298 (const float (*)[4])draw
->pt
.user
.constants
,
304 if (draw_pt_post_vs_run( fpme
->post_vs
,
312 /* Do we need to run the pipeline?
314 if (opt
& PT_PIPELINE
) {
315 draw_pipeline_run( fpme
->draw
,
324 draw_pt_emit( fpme
->emit
,
325 (const float (*)[4])pipeline_verts
->data
,
332 FREE(pipeline_verts
);
338 static void fetch_pipeline_finish( struct draw_pt_middle_end
*middle
)
343 static void fetch_pipeline_destroy( struct draw_pt_middle_end
*middle
)
345 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
348 draw_pt_fetch_destroy( fpme
->fetch
);
351 draw_pt_emit_destroy( fpme
->emit
);
354 draw_pt_post_vs_destroy( fpme
->post_vs
);
360 struct draw_pt_middle_end
*draw_pt_fetch_pipeline_or_emit( struct draw_context
*draw
)
362 struct fetch_pipeline_middle_end
*fpme
= CALLOC_STRUCT( fetch_pipeline_middle_end
);
366 fpme
->base
.prepare
= fetch_pipeline_prepare
;
367 fpme
->base
.run
= fetch_pipeline_run
;
368 fpme
->base
.run_linear
= fetch_pipeline_linear_run
;
369 fpme
->base
.run_linear_elts
= fetch_pipeline_linear_run_elts
;
370 fpme
->base
.finish
= fetch_pipeline_finish
;
371 fpme
->base
.destroy
= fetch_pipeline_destroy
;
375 fpme
->fetch
= draw_pt_fetch_create( draw
);
379 fpme
->post_vs
= draw_pt_post_vs_create( draw
);
383 fpme
->emit
= draw_pt_emit_create( draw
);
391 fetch_pipeline_destroy( &fpme
->base
);