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 draw
->rasterizer
->bypass_vs_clip_and_viewport
),
89 (boolean
)draw
->rasterizer
->gl_rasterization_rules
);
92 if (!(opt
& PT_PIPELINE
)) {
93 draw_pt_emit_prepare( fpme
->emit
,
97 *max_vertices
= MAX2( *max_vertices
,
98 DRAW_PIPE_MAX_VERTICES
);
101 *max_vertices
= DRAW_PIPE_MAX_VERTICES
;
104 /* return even number */
105 *max_vertices
= *max_vertices
& ~1;
107 /* No need to prepare the shader.
109 vs
->prepare(vs
, draw
);
114 static void fetch_pipeline_run( struct draw_pt_middle_end
*middle
,
115 const unsigned *fetch_elts
,
116 unsigned fetch_count
,
117 const ushort
*draw_elts
,
118 unsigned draw_count
)
120 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
121 struct draw_context
*draw
= fpme
->draw
;
122 struct draw_vertex_shader
*shader
= draw
->vs
.vertex_shader
;
123 unsigned opt
= fpme
->opt
;
124 unsigned alloc_count
= align( fetch_count
, 4 );
126 struct vertex_header
*pipeline_verts
=
127 (struct vertex_header
*)MALLOC(fpme
->vertex_size
* alloc_count
);
129 if (!pipeline_verts
) {
130 /* Not much we can do here - just skip the rendering.
136 /* Fetch into our vertex buffer
138 draw_pt_fetch_run( fpme
->fetch
,
141 (char *)pipeline_verts
);
143 /* Run the shader, note that this overwrites the data[] parts of
144 * the pipeline verts. If there is no shader, eg if
145 * bypass_vs_clip_and_viewport, then the inputs == outputs, and are
146 * already in the correct place.
150 shader
->run_linear(shader
,
151 (const float (*)[4])pipeline_verts
->data
,
152 ( float (*)[4])pipeline_verts
->data
,
153 (const float (*)[4])draw
->pt
.user
.constants
,
159 if (draw_pt_post_vs_run( fpme
->post_vs
,
167 /* Do we need to run the pipeline?
169 if (opt
& PT_PIPELINE
) {
170 draw_pipeline_run( fpme
->draw
,
179 draw_pt_emit( fpme
->emit
,
180 (const float (*)[4])pipeline_verts
->data
,
188 FREE(pipeline_verts
);
192 static void fetch_pipeline_linear_run( struct draw_pt_middle_end
*middle
,
196 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
197 struct draw_context
*draw
= fpme
->draw
;
198 struct draw_vertex_shader
*shader
= draw
->vs
.vertex_shader
;
199 unsigned opt
= fpme
->opt
;
200 unsigned alloc_count
= align( count
, 4 );
202 struct vertex_header
*pipeline_verts
=
203 (struct vertex_header
*)MALLOC(fpme
->vertex_size
* alloc_count
);
205 if (!pipeline_verts
) {
206 /* Not much we can do here - just skip the rendering.
212 /* Fetch into our vertex buffer
214 draw_pt_fetch_run_linear( fpme
->fetch
,
217 (char *)pipeline_verts
);
219 /* Run the shader, note that this overwrites the data[] parts of
220 * the pipeline verts. If there is no shader, ie if
221 * bypass_vs_clip_and_viewport, then the inputs == outputs, and are
222 * already in the correct place.
226 shader
->run_linear(shader
,
227 (const float (*)[4])pipeline_verts
->data
,
228 ( float (*)[4])pipeline_verts
->data
,
229 (const float (*)[4])draw
->pt
.user
.constants
,
235 if (draw_pt_post_vs_run( fpme
->post_vs
,
243 /* Do we need to run the pipeline?
245 if (opt
& PT_PIPELINE
) {
246 draw_pipeline_run_linear( fpme
->draw
,
253 draw_pt_emit_linear( fpme
->emit
,
254 (const float (*)[4])pipeline_verts
->data
,
259 FREE(pipeline_verts
);
264 static boolean
fetch_pipeline_linear_run_elts( struct draw_pt_middle_end
*middle
,
267 const ushort
*draw_elts
,
268 unsigned draw_count
)
270 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
271 struct draw_context
*draw
= fpme
->draw
;
272 struct draw_vertex_shader
*shader
= draw
->vs
.vertex_shader
;
273 unsigned opt
= fpme
->opt
;
274 unsigned alloc_count
= align( count
, 4 );
276 struct vertex_header
*pipeline_verts
=
277 (struct vertex_header
*)MALLOC(fpme
->vertex_size
* alloc_count
);
282 /* Fetch into our vertex buffer
284 draw_pt_fetch_run_linear( fpme
->fetch
,
287 (char *)pipeline_verts
);
289 /* Run the shader, note that this overwrites the data[] parts of
290 * the pipeline verts. If there is no shader, ie if
291 * bypass_vs_clip_and_viewport, then the inputs == outputs, and are
292 * already in the correct place.
296 shader
->run_linear(shader
,
297 (const float (*)[4])pipeline_verts
->data
,
298 ( float (*)[4])pipeline_verts
->data
,
299 (const float (*)[4])draw
->pt
.user
.constants
,
305 if (draw_pt_post_vs_run( fpme
->post_vs
,
313 /* Do we need to run the pipeline?
315 if (opt
& PT_PIPELINE
) {
316 draw_pipeline_run( fpme
->draw
,
325 draw_pt_emit( fpme
->emit
,
326 (const float (*)[4])pipeline_verts
->data
,
333 FREE(pipeline_verts
);
339 static void fetch_pipeline_finish( struct draw_pt_middle_end
*middle
)
344 static void fetch_pipeline_destroy( struct draw_pt_middle_end
*middle
)
346 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
349 draw_pt_fetch_destroy( fpme
->fetch
);
352 draw_pt_emit_destroy( fpme
->emit
);
355 draw_pt_post_vs_destroy( fpme
->post_vs
);
361 struct draw_pt_middle_end
*draw_pt_fetch_pipeline_or_emit( struct draw_context
*draw
)
363 struct fetch_pipeline_middle_end
*fpme
= CALLOC_STRUCT( fetch_pipeline_middle_end
);
367 fpme
->base
.prepare
= fetch_pipeline_prepare
;
368 fpme
->base
.run
= fetch_pipeline_run
;
369 fpme
->base
.run_linear
= fetch_pipeline_linear_run
;
370 fpme
->base
.run_linear_elts
= fetch_pipeline_linear_run_elts
;
371 fpme
->base
.finish
= fetch_pipeline_finish
;
372 fpme
->base
.destroy
= fetch_pipeline_destroy
;
376 fpme
->fetch
= draw_pt_fetch_create( draw
);
380 fpme
->post_vs
= draw_pt_post_vs_create( draw
);
384 fpme
->emit
= draw_pt_emit_create( draw
);
392 fetch_pipeline_destroy( &fpme
->base
);