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 "draw/draw_gs.h"
36 #include "translate/translate.h"
39 struct fetch_pipeline_middle_end
{
40 struct draw_pt_middle_end base
;
41 struct draw_context
*draw
;
44 struct pt_fetch
*fetch
;
45 struct pt_post_vs
*post_vs
;
47 unsigned vertex_data_offset
;
54 static void fetch_pipeline_prepare( struct draw_pt_middle_end
*middle
,
57 unsigned *max_vertices
)
59 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
60 struct draw_context
*draw
= fpme
->draw
;
61 struct draw_vertex_shader
*vs
= draw
->vs
.vertex_shader
;
63 /* Add one to num_outputs because the pipeline occasionally tags on
64 * an additional texcoord, eg for AA lines.
66 unsigned nr
= MAX2( vs
->info
.num_inputs
,
67 vs
->info
.num_outputs
+ 1 );
72 /* Always leave room for the vertex header whether we need it or
73 * not. It's hard to get rid of it in particular because of the
74 * viewport code in draw_pt_post_vs.c.
76 fpme
->vertex_size
= sizeof(struct vertex_header
) + nr
* 4 * sizeof(float);
80 draw_pt_fetch_prepare( fpme
->fetch
,
83 /* XXX: it's not really gl rasterization rules we care about here,
84 * but gl vs dx9 clip spaces.
86 draw_pt_post_vs_prepare( fpme
->post_vs
,
87 (boolean
)draw
->bypass_clipping
,
88 (boolean
)(draw
->identity_viewport
||
89 draw
->rasterizer
->bypass_vs_clip_and_viewport
),
90 (boolean
)draw
->rasterizer
->gl_rasterization_rules
,
91 (draw
->vs
.edgeflag_output
? true : false) );
93 if (!(opt
& PT_PIPELINE
)) {
94 draw_pt_emit_prepare( fpme
->emit
,
98 *max_vertices
= MAX2( *max_vertices
,
99 DRAW_PIPE_MAX_VERTICES
);
102 *max_vertices
= DRAW_PIPE_MAX_VERTICES
;
105 /* return even number */
106 *max_vertices
= *max_vertices
& ~1;
108 /* No need to prepare the shader.
110 vs
->prepare(vs
, draw
);
115 static void fetch_pipeline_run( struct draw_pt_middle_end
*middle
,
116 const unsigned *fetch_elts
,
117 unsigned fetch_count
,
118 const ushort
*draw_elts
,
119 unsigned draw_count
)
121 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
122 struct draw_context
*draw
= fpme
->draw
;
123 struct draw_vertex_shader
*vshader
= draw
->vs
.vertex_shader
;
124 struct draw_geometry_shader
*gshader
= draw
->gs
.geometry_shader
;
125 unsigned opt
= fpme
->opt
;
126 unsigned alloc_count
= align( fetch_count
, 4 );
128 struct vertex_header
*pipeline_verts
=
129 (struct vertex_header
*)MALLOC(fpme
->vertex_size
* alloc_count
);
131 if (!pipeline_verts
) {
132 /* Not much we can do here - just skip the rendering.
138 /* Fetch into our vertex buffer
140 draw_pt_fetch_run( fpme
->fetch
,
143 (char *)pipeline_verts
);
145 /* Run the shader, note that this overwrites the data[] parts of
146 * the pipeline verts. If there is no shader, eg if
147 * bypass_vs_clip_and_viewport, then the inputs == outputs, and are
148 * already in the correct place.
152 vshader
->run_linear(vshader
,
153 (const float (*)[4])pipeline_verts
->data
,
154 ( float (*)[4])pipeline_verts
->data
,
155 (const float (*)[4])draw
->pt
.user
.vs_constants
,
160 draw_geometry_shader_run(gshader
,
161 (const float (*)[4])pipeline_verts
->data
,
162 ( float (*)[4])pipeline_verts
->data
,
163 (const float (*)[4])draw
->pt
.user
.gs_constants
,
169 if (draw_pt_post_vs_run( fpme
->post_vs
,
177 /* Do we need to run the pipeline?
179 if (opt
& PT_PIPELINE
) {
180 draw_pipeline_run( fpme
->draw
,
189 draw_pt_emit( fpme
->emit
,
190 (const float (*)[4])pipeline_verts
->data
,
198 FREE(pipeline_verts
);
202 static void fetch_pipeline_linear_run( struct draw_pt_middle_end
*middle
,
206 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
207 struct draw_context
*draw
= fpme
->draw
;
208 struct draw_vertex_shader
*shader
= draw
->vs
.vertex_shader
;
209 struct draw_geometry_shader
*geometry_shader
= draw
->gs
.geometry_shader
;
210 unsigned opt
= fpme
->opt
;
211 unsigned alloc_count
= align( count
, 4 );
213 struct vertex_header
*pipeline_verts
=
214 (struct vertex_header
*)MALLOC(fpme
->vertex_size
* alloc_count
);
216 if (!pipeline_verts
) {
217 /* Not much we can do here - just skip the rendering.
223 /* Fetch into our vertex buffer
225 draw_pt_fetch_run_linear( fpme
->fetch
,
228 (char *)pipeline_verts
);
230 /* Run the shader, note that this overwrites the data[] parts of
231 * the pipeline verts. If there is no shader, ie if
232 * bypass_vs_clip_and_viewport, then the inputs == outputs, and are
233 * already in the correct place.
237 shader
->run_linear(shader
,
238 (const float (*)[4])pipeline_verts
->data
,
239 ( float (*)[4])pipeline_verts
->data
,
240 (const float (*)[4])draw
->pt
.user
.vs_constants
,
246 draw_geometry_shader_run(geometry_shader
,
247 (const float (*)[4])pipeline_verts
->data
,
248 ( float (*)[4])pipeline_verts
->data
,
249 (const float (*)[4])draw
->pt
.user
.gs_constants
,
255 if (draw_pt_post_vs_run( fpme
->post_vs
,
263 /* Do we need to run the pipeline?
265 if (opt
& PT_PIPELINE
) {
266 draw_pipeline_run_linear( fpme
->draw
,
273 draw_pt_emit_linear( fpme
->emit
,
274 (const float (*)[4])pipeline_verts
->data
,
279 FREE(pipeline_verts
);
284 static boolean
fetch_pipeline_linear_run_elts( struct draw_pt_middle_end
*middle
,
287 const ushort
*draw_elts
,
288 unsigned draw_count
)
290 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
291 struct draw_context
*draw
= fpme
->draw
;
292 struct draw_vertex_shader
*shader
= draw
->vs
.vertex_shader
;
293 struct draw_geometry_shader
*geometry_shader
= draw
->gs
.geometry_shader
;
294 unsigned opt
= fpme
->opt
;
295 unsigned alloc_count
= align( count
, 4 );
297 struct vertex_header
*pipeline_verts
=
298 (struct vertex_header
*)MALLOC(fpme
->vertex_size
* alloc_count
);
303 /* Fetch into our vertex buffer
305 draw_pt_fetch_run_linear( fpme
->fetch
,
308 (char *)pipeline_verts
);
310 /* Run the shader, note that this overwrites the data[] parts of
311 * the pipeline verts. If there is no shader, ie if
312 * bypass_vs_clip_and_viewport, then the inputs == outputs, and are
313 * already in the correct place.
317 shader
->run_linear(shader
,
318 (const float (*)[4])pipeline_verts
->data
,
319 ( float (*)[4])pipeline_verts
->data
,
320 (const float (*)[4])draw
->pt
.user
.vs_constants
,
326 draw_geometry_shader_run(geometry_shader
,
327 (const float (*)[4])pipeline_verts
->data
,
328 ( float (*)[4])pipeline_verts
->data
,
329 (const float (*)[4])draw
->pt
.user
.gs_constants
,
335 if (draw_pt_post_vs_run( fpme
->post_vs
,
343 /* Do we need to run the pipeline?
345 if (opt
& PT_PIPELINE
) {
346 draw_pipeline_run( fpme
->draw
,
355 draw_pt_emit( fpme
->emit
,
356 (const float (*)[4])pipeline_verts
->data
,
363 FREE(pipeline_verts
);
369 static void fetch_pipeline_finish( struct draw_pt_middle_end
*middle
)
374 static void fetch_pipeline_destroy( struct draw_pt_middle_end
*middle
)
376 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
379 draw_pt_fetch_destroy( fpme
->fetch
);
382 draw_pt_emit_destroy( fpme
->emit
);
385 draw_pt_post_vs_destroy( fpme
->post_vs
);
391 struct draw_pt_middle_end
*draw_pt_fetch_pipeline_or_emit( struct draw_context
*draw
)
393 struct fetch_pipeline_middle_end
*fpme
= CALLOC_STRUCT( fetch_pipeline_middle_end
);
397 fpme
->base
.prepare
= fetch_pipeline_prepare
;
398 fpme
->base
.run
= fetch_pipeline_run
;
399 fpme
->base
.run_linear
= fetch_pipeline_linear_run
;
400 fpme
->base
.run_linear_elts
= fetch_pipeline_linear_run_elts
;
401 fpme
->base
.finish
= fetch_pipeline_finish
;
402 fpme
->base
.destroy
= fetch_pipeline_destroy
;
406 fpme
->fetch
= draw_pt_fetch_create( draw
);
410 fpme
->post_vs
= draw_pt_post_vs_create( draw
);
414 fpme
->emit
= draw_pt_emit_create( draw
);
422 fetch_pipeline_destroy( &fpme
->base
);