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"
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 unsigned instance_id_index
= ~0;
64 /* Add one to num_outputs because the pipeline occasionally tags on
65 * an additional texcoord, eg for AA lines.
67 unsigned nr
= MAX2( vs
->info
.num_inputs
,
68 vs
->info
.num_outputs
+ 1 );
70 /* Scan for instanceID system value.
72 for (i
= 0; i
< vs
->info
.num_inputs
; i
++) {
73 if (vs
->info
.input_semantic_name
[i
] == TGSI_SEMANTIC_INSTANCEID
) {
74 instance_id_index
= i
;
82 /* Always leave room for the vertex header whether we need it or
83 * not. It's hard to get rid of it in particular because of the
84 * viewport code in draw_pt_post_vs.c.
86 fpme
->vertex_size
= sizeof(struct vertex_header
) + nr
* 4 * sizeof(float);
90 draw_pt_fetch_prepare( fpme
->fetch
,
94 /* XXX: it's not really gl rasterization rules we care about here,
95 * but gl vs dx9 clip spaces.
97 draw_pt_post_vs_prepare( fpme
->post_vs
,
98 (boolean
)draw
->bypass_clipping
,
99 (boolean
)(draw
->identity_viewport
||
100 draw
->rasterizer
->bypass_vs_clip_and_viewport
),
101 (boolean
)draw
->rasterizer
->gl_rasterization_rules
,
102 (draw
->vs
.edgeflag_output
? true : false) );
104 if (!(opt
& PT_PIPELINE
)) {
105 draw_pt_emit_prepare( fpme
->emit
,
109 *max_vertices
= MAX2( *max_vertices
,
110 DRAW_PIPE_MAX_VERTICES
);
113 *max_vertices
= DRAW_PIPE_MAX_VERTICES
;
116 /* return even number */
117 *max_vertices
= *max_vertices
& ~1;
119 /* No need to prepare the shader.
121 vs
->prepare(vs
, draw
);
126 static void fetch_pipeline_run( struct draw_pt_middle_end
*middle
,
127 const unsigned *fetch_elts
,
128 unsigned fetch_count
,
129 const ushort
*draw_elts
,
130 unsigned draw_count
)
132 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
133 struct draw_context
*draw
= fpme
->draw
;
134 struct draw_vertex_shader
*vshader
= draw
->vs
.vertex_shader
;
135 struct draw_geometry_shader
*gshader
= draw
->gs
.geometry_shader
;
136 unsigned opt
= fpme
->opt
;
137 unsigned alloc_count
= align( fetch_count
, 4 );
139 struct vertex_header
*pipeline_verts
=
140 (struct vertex_header
*)MALLOC(fpme
->vertex_size
* alloc_count
);
142 if (!pipeline_verts
) {
143 /* Not much we can do here - just skip the rendering.
149 /* Fetch into our vertex buffer
151 draw_pt_fetch_run( fpme
->fetch
,
154 (char *)pipeline_verts
);
156 /* Run the shader, note that this overwrites the data[] parts of
157 * the pipeline verts. If there is no shader, eg if
158 * bypass_vs_clip_and_viewport, then the inputs == outputs, and are
159 * already in the correct place.
163 vshader
->run_linear(vshader
,
164 (const float (*)[4])pipeline_verts
->data
,
165 ( float (*)[4])pipeline_verts
->data
,
166 draw
->pt
.user
.vs_constants
,
171 draw_geometry_shader_run(gshader
,
172 (const float (*)[4])pipeline_verts
->data
,
173 ( float (*)[4])pipeline_verts
->data
,
174 draw
->pt
.user
.gs_constants
,
180 if (draw_pt_post_vs_run( fpme
->post_vs
,
188 /* Do we need to run the pipeline?
190 if (opt
& PT_PIPELINE
) {
191 draw_pipeline_run( fpme
->draw
,
200 draw_pt_emit( fpme
->emit
,
201 (const float (*)[4])pipeline_verts
->data
,
209 FREE(pipeline_verts
);
213 static void fetch_pipeline_linear_run( struct draw_pt_middle_end
*middle
,
217 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
218 struct draw_context
*draw
= fpme
->draw
;
219 struct draw_vertex_shader
*shader
= draw
->vs
.vertex_shader
;
220 struct draw_geometry_shader
*geometry_shader
= draw
->gs
.geometry_shader
;
221 unsigned opt
= fpme
->opt
;
222 unsigned alloc_count
= align( count
, 4 );
224 struct vertex_header
*pipeline_verts
=
225 (struct vertex_header
*)MALLOC(fpme
->vertex_size
* alloc_count
);
227 if (!pipeline_verts
) {
228 /* Not much we can do here - just skip the rendering.
234 /* Fetch into our vertex buffer
236 draw_pt_fetch_run_linear( fpme
->fetch
,
239 (char *)pipeline_verts
);
241 /* Run the shader, note that this overwrites the data[] parts of
242 * the pipeline verts. If there is no shader, ie if
243 * bypass_vs_clip_and_viewport, then the inputs == outputs, and are
244 * already in the correct place.
248 shader
->run_linear(shader
,
249 (const float (*)[4])pipeline_verts
->data
,
250 ( float (*)[4])pipeline_verts
->data
,
251 draw
->pt
.user
.vs_constants
,
257 draw_geometry_shader_run(geometry_shader
,
258 (const float (*)[4])pipeline_verts
->data
,
259 ( float (*)[4])pipeline_verts
->data
,
260 draw
->pt
.user
.gs_constants
,
266 if (draw_pt_post_vs_run( fpme
->post_vs
,
274 /* Do we need to run the pipeline?
276 if (opt
& PT_PIPELINE
) {
277 draw_pipeline_run_linear( fpme
->draw
,
284 draw_pt_emit_linear( fpme
->emit
,
285 (const float (*)[4])pipeline_verts
->data
,
290 FREE(pipeline_verts
);
295 static boolean
fetch_pipeline_linear_run_elts( struct draw_pt_middle_end
*middle
,
298 const ushort
*draw_elts
,
299 unsigned draw_count
)
301 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
302 struct draw_context
*draw
= fpme
->draw
;
303 struct draw_vertex_shader
*shader
= draw
->vs
.vertex_shader
;
304 struct draw_geometry_shader
*geometry_shader
= draw
->gs
.geometry_shader
;
305 unsigned opt
= fpme
->opt
;
306 unsigned alloc_count
= align( count
, 4 );
308 struct vertex_header
*pipeline_verts
=
309 (struct vertex_header
*)MALLOC(fpme
->vertex_size
* alloc_count
);
314 /* Fetch into our vertex buffer
316 draw_pt_fetch_run_linear( fpme
->fetch
,
319 (char *)pipeline_verts
);
321 /* Run the shader, note that this overwrites the data[] parts of
322 * the pipeline verts. If there is no shader, ie if
323 * bypass_vs_clip_and_viewport, then the inputs == outputs, and are
324 * already in the correct place.
328 shader
->run_linear(shader
,
329 (const float (*)[4])pipeline_verts
->data
,
330 ( float (*)[4])pipeline_verts
->data
,
331 draw
->pt
.user
.vs_constants
,
337 draw_geometry_shader_run(geometry_shader
,
338 (const float (*)[4])pipeline_verts
->data
,
339 ( float (*)[4])pipeline_verts
->data
,
340 draw
->pt
.user
.gs_constants
,
346 if (draw_pt_post_vs_run( fpme
->post_vs
,
354 /* Do we need to run the pipeline?
356 if (opt
& PT_PIPELINE
) {
357 draw_pipeline_run( fpme
->draw
,
366 draw_pt_emit( fpme
->emit
,
367 (const float (*)[4])pipeline_verts
->data
,
374 FREE(pipeline_verts
);
380 static void fetch_pipeline_finish( struct draw_pt_middle_end
*middle
)
385 static void fetch_pipeline_destroy( struct draw_pt_middle_end
*middle
)
387 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
390 draw_pt_fetch_destroy( fpme
->fetch
);
393 draw_pt_emit_destroy( fpme
->emit
);
396 draw_pt_post_vs_destroy( fpme
->post_vs
);
402 struct draw_pt_middle_end
*draw_pt_fetch_pipeline_or_emit( struct draw_context
*draw
)
404 struct fetch_pipeline_middle_end
*fpme
= CALLOC_STRUCT( fetch_pipeline_middle_end
);
408 fpme
->base
.prepare
= fetch_pipeline_prepare
;
409 fpme
->base
.run
= fetch_pipeline_run
;
410 fpme
->base
.run_linear
= fetch_pipeline_linear_run
;
411 fpme
->base
.run_linear_elts
= fetch_pipeline_linear_run_elts
;
412 fpme
->base
.finish
= fetch_pipeline_finish
;
413 fpme
->base
.destroy
= fetch_pipeline_destroy
;
417 fpme
->fetch
= draw_pt_fetch_create( draw
);
421 fpme
->post_vs
= draw_pt_post_vs_create( draw
);
425 fpme
->emit
= draw_pt_emit_create( draw
);
433 fetch_pipeline_destroy( &fpme
->base
);