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 (boolean
)draw
->rasterizer
->gl_rasterization_rules
,
101 (draw
->vs
.edgeflag_output
? true : false) );
103 if (!(opt
& PT_PIPELINE
)) {
104 draw_pt_emit_prepare( fpme
->emit
,
108 *max_vertices
= MAX2( *max_vertices
,
109 DRAW_PIPE_MAX_VERTICES
);
112 *max_vertices
= DRAW_PIPE_MAX_VERTICES
;
115 /* return even number */
116 *max_vertices
= *max_vertices
& ~1;
118 /* No need to prepare the shader.
120 vs
->prepare(vs
, draw
);
125 static void fetch_pipeline_run( struct draw_pt_middle_end
*middle
,
126 const unsigned *fetch_elts
,
127 unsigned fetch_count
,
128 const ushort
*draw_elts
,
129 unsigned draw_count
)
131 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
132 struct draw_context
*draw
= fpme
->draw
;
133 struct draw_vertex_shader
*vshader
= draw
->vs
.vertex_shader
;
134 struct draw_geometry_shader
*gshader
= draw
->gs
.geometry_shader
;
135 unsigned opt
= fpme
->opt
;
136 unsigned alloc_count
= align( fetch_count
, 4 );
138 struct vertex_header
*pipeline_verts
=
139 (struct vertex_header
*)MALLOC(fpme
->vertex_size
* alloc_count
);
141 if (!pipeline_verts
) {
142 /* Not much we can do here - just skip the rendering.
148 /* Fetch into our vertex buffer
150 draw_pt_fetch_run( fpme
->fetch
,
153 (char *)pipeline_verts
);
155 /* Run the shader, note that this overwrites the data[] parts of
156 * the pipeline verts.
160 vshader
->run_linear(vshader
,
161 (const float (*)[4])pipeline_verts
->data
,
162 ( float (*)[4])pipeline_verts
->data
,
163 draw
->pt
.user
.vs_constants
,
168 draw_geometry_shader_run(gshader
,
169 (const float (*)[4])pipeline_verts
->data
,
170 ( float (*)[4])pipeline_verts
->data
,
171 draw
->pt
.user
.gs_constants
,
177 if (draw_pt_post_vs_run( fpme
->post_vs
,
185 /* Do we need to run the pipeline?
187 if (opt
& PT_PIPELINE
) {
188 draw_pipeline_run( fpme
->draw
,
197 draw_pt_emit( fpme
->emit
,
198 (const float (*)[4])pipeline_verts
->data
,
206 FREE(pipeline_verts
);
210 static void fetch_pipeline_linear_run( struct draw_pt_middle_end
*middle
,
214 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
215 struct draw_context
*draw
= fpme
->draw
;
216 struct draw_vertex_shader
*shader
= draw
->vs
.vertex_shader
;
217 struct draw_geometry_shader
*geometry_shader
= draw
->gs
.geometry_shader
;
218 unsigned opt
= fpme
->opt
;
219 unsigned alloc_count
= align( count
, 4 );
221 struct vertex_header
*pipeline_verts
=
222 (struct vertex_header
*)MALLOC(fpme
->vertex_size
* alloc_count
);
224 if (!pipeline_verts
) {
225 /* Not much we can do here - just skip the rendering.
231 /* Fetch into our vertex buffer
233 draw_pt_fetch_run_linear( fpme
->fetch
,
236 (char *)pipeline_verts
);
238 /* Run the shader, note that this overwrites the data[] parts of
239 * the pipeline verts.
243 shader
->run_linear(shader
,
244 (const float (*)[4])pipeline_verts
->data
,
245 ( float (*)[4])pipeline_verts
->data
,
246 draw
->pt
.user
.vs_constants
,
252 draw_geometry_shader_run(geometry_shader
,
253 (const float (*)[4])pipeline_verts
->data
,
254 ( float (*)[4])pipeline_verts
->data
,
255 draw
->pt
.user
.gs_constants
,
261 if (draw_pt_post_vs_run( fpme
->post_vs
,
269 /* Do we need to run the pipeline?
271 if (opt
& PT_PIPELINE
) {
272 draw_pipeline_run_linear( fpme
->draw
,
279 draw_pt_emit_linear( fpme
->emit
,
280 (const float (*)[4])pipeline_verts
->data
,
285 FREE(pipeline_verts
);
290 static boolean
fetch_pipeline_linear_run_elts( struct draw_pt_middle_end
*middle
,
293 const ushort
*draw_elts
,
294 unsigned draw_count
)
296 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
297 struct draw_context
*draw
= fpme
->draw
;
298 struct draw_vertex_shader
*shader
= draw
->vs
.vertex_shader
;
299 struct draw_geometry_shader
*geometry_shader
= draw
->gs
.geometry_shader
;
300 unsigned opt
= fpme
->opt
;
301 unsigned alloc_count
= align( count
, 4 );
303 struct vertex_header
*pipeline_verts
=
304 (struct vertex_header
*)MALLOC(fpme
->vertex_size
* alloc_count
);
309 /* Fetch into our vertex buffer
311 draw_pt_fetch_run_linear( fpme
->fetch
,
314 (char *)pipeline_verts
);
316 /* Run the shader, note that this overwrites the data[] parts of
317 * the pipeline verts.
321 shader
->run_linear(shader
,
322 (const float (*)[4])pipeline_verts
->data
,
323 ( float (*)[4])pipeline_verts
->data
,
324 draw
->pt
.user
.vs_constants
,
330 draw_geometry_shader_run(geometry_shader
,
331 (const float (*)[4])pipeline_verts
->data
,
332 ( float (*)[4])pipeline_verts
->data
,
333 draw
->pt
.user
.gs_constants
,
339 if (draw_pt_post_vs_run( fpme
->post_vs
,
347 /* Do we need to run the pipeline?
349 if (opt
& PT_PIPELINE
) {
350 draw_pipeline_run( fpme
->draw
,
359 draw_pt_emit( fpme
->emit
,
360 (const float (*)[4])pipeline_verts
->data
,
367 FREE(pipeline_verts
);
373 static void fetch_pipeline_finish( struct draw_pt_middle_end
*middle
)
378 static void fetch_pipeline_destroy( struct draw_pt_middle_end
*middle
)
380 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
383 draw_pt_fetch_destroy( fpme
->fetch
);
386 draw_pt_emit_destroy( fpme
->emit
);
389 draw_pt_post_vs_destroy( fpme
->post_vs
);
395 struct draw_pt_middle_end
*draw_pt_fetch_pipeline_or_emit( struct draw_context
*draw
)
397 struct fetch_pipeline_middle_end
*fpme
= CALLOC_STRUCT( fetch_pipeline_middle_end
);
401 fpme
->base
.prepare
= fetch_pipeline_prepare
;
402 fpme
->base
.run
= fetch_pipeline_run
;
403 fpme
->base
.run_linear
= fetch_pipeline_linear_run
;
404 fpme
->base
.run_linear_elts
= fetch_pipeline_linear_run_elts
;
405 fpme
->base
.finish
= fetch_pipeline_finish
;
406 fpme
->base
.destroy
= fetch_pipeline_destroy
;
410 fpme
->fetch
= draw_pt_fetch_create( draw
);
414 fpme
->post_vs
= draw_pt_post_vs_create( draw
);
418 fpme
->emit
= draw_pt_emit_create( draw
);
426 fetch_pipeline_destroy( &fpme
->base
);