1 /**************************************************************************
3 * Copyright 2010 VMWare, Inc.
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 "draw/draw_llvm.h"
38 #include "translate/translate.h"
41 struct llvm_middle_end
{
42 struct draw_pt_middle_end base
;
43 struct draw_context
*draw
;
46 struct pt_fetch
*fetch
;
47 struct pt_post_vs
*post_vs
;
50 unsigned vertex_data_offset
;
55 struct draw_llvm
*llvm
;
60 llvm_middle_end_prepare( struct draw_pt_middle_end
*middle
,
63 unsigned *max_vertices
)
65 struct llvm_middle_end
*fpme
= (struct llvm_middle_end
*)middle
;
66 struct draw_context
*draw
= fpme
->draw
;
67 struct draw_vertex_shader
*vs
= draw
->vs
.vertex_shader
;
68 struct draw_geometry_shader
*gs
= draw
->gs
.geometry_shader
;
70 unsigned instance_id_index
= ~0;
72 /* Add one to num_outputs because the pipeline occasionally tags on
73 * an additional texcoord, eg for AA lines.
75 unsigned nr
= MAX2( vs
->info
.num_inputs
,
76 vs
->info
.num_outputs
+ 1 );
78 /* Scan for instanceID system value.
80 for (i
= 0; i
< vs
->info
.num_inputs
; i
++) {
81 if (vs
->info
.input_semantic_name
[i
] == TGSI_SEMANTIC_INSTANCEID
) {
82 instance_id_index
= i
;
90 /* Always leave room for the vertex header whether we need it or
91 * not. It's hard to get rid of it in particular because of the
92 * viewport code in draw_pt_post_vs.c.
94 fpme
->vertex_size
= sizeof(struct vertex_header
) + nr
* 4 * sizeof(float);
98 draw_pt_fetch_prepare( fpme
->fetch
,
102 if (opt
& PT_SHADE
) {
103 vs
->prepare(vs
, draw
);
104 draw_geometry_shader_prepare(gs
, draw
);
108 /* XXX: it's not really gl rasterization rules we care about here,
109 * but gl vs dx9 clip spaces.
111 draw_pt_post_vs_prepare( fpme
->post_vs
,
112 (boolean
)draw
->bypass_clipping
,
113 (boolean
)(draw
->identity_viewport
),
114 (boolean
)draw
->rasterizer
->gl_rasterization_rules
,
115 (draw
->vs
.edgeflag_output
? true : false) );
117 if (!(opt
& PT_PIPELINE
)) {
118 draw_pt_emit_prepare( fpme
->emit
,
122 *max_vertices
= MAX2( *max_vertices
,
123 DRAW_PIPE_MAX_VERTICES
);
126 *max_vertices
= DRAW_PIPE_MAX_VERTICES
;
129 /* return even number */
130 *max_vertices
= *max_vertices
& ~1;
132 draw_llvm_prepare(fpme
->llvm
);
134 /*XXX we only support one constant buffer */
135 fpme
->llvm
->jit_context
.vs_constants
=
136 draw
->pt
.user
.vs_constants
[0];
137 fpme
->llvm
->jit_context
.gs_constants
=
138 draw
->pt
.user
.gs_constants
[0];
143 static void llvm_middle_end_run( struct draw_pt_middle_end
*middle
,
144 const unsigned *fetch_elts
,
145 unsigned fetch_count
,
146 const ushort
*draw_elts
,
147 unsigned draw_count
)
149 struct llvm_middle_end
*fpme
= (struct llvm_middle_end
*)middle
;
150 struct draw_context
*draw
= fpme
->draw
;
151 struct draw_vertex_shader
*vshader
= draw
->vs
.vertex_shader
;
152 struct draw_geometry_shader
*gshader
= draw
->gs
.geometry_shader
;
153 unsigned opt
= fpme
->opt
;
154 unsigned alloc_count
= align( fetch_count
, 4 );
156 struct vertex_header
*pipeline_verts
=
157 (struct vertex_header
*)MALLOC(fpme
->vertex_size
* alloc_count
);
159 if (!pipeline_verts
) {
160 /* Not much we can do here - just skip the rendering.
166 /* Fetch into our vertex buffer
168 draw_pt_fetch_run( fpme
->fetch
,
171 (char *)pipeline_verts
);
173 /* Run the shader, note that this overwrites the data[] parts of
174 * the pipeline verts. If there is no shader, eg if
175 * bypass_vs_clip_and_viewport, then the inputs == outputs, and are
176 * already in the correct place.*/
179 vshader
->run_linear(vshader
,
180 (const float (*)[4])pipeline_verts
->data
,
181 ( float (*)[4])pipeline_verts
->data
,
182 draw
->pt
.user
.vs_constants
,
187 draw_geometry_shader_run(gshader
,
188 (const float (*)[4])pipeline_verts
->data
,
189 ( float (*)[4])pipeline_verts
->data
,
190 draw
->pt
.user
.gs_constants
,
196 if (draw_pt_post_vs_run( fpme
->post_vs
,
204 /* Do we need to run the pipeline?
206 if (opt
& PT_PIPELINE
) {
207 draw_pipeline_run( fpme
->draw
,
216 draw_pt_emit( fpme
->emit
,
217 (const float (*)[4])pipeline_verts
->data
,
225 FREE(pipeline_verts
);
229 static void llvm_middle_end_linear_run( struct draw_pt_middle_end
*middle
,
233 struct llvm_middle_end
*fpme
= (struct llvm_middle_end
*)middle
;
234 struct draw_context
*draw
= fpme
->draw
;
235 unsigned opt
= fpme
->opt
;
236 unsigned alloc_count
= align( count
, 4 );
238 struct vertex_header
*pipeline_verts
=
239 (struct vertex_header
*)MALLOC(fpme
->vertex_size
* alloc_count
);
241 if (!pipeline_verts
) {
242 /* Not much we can do here - just skip the rendering.
248 debug_printf("--- pipe verts data[0] = %p, data[1] = %p\n",
249 pipeline_verts
->data
[0], pipeline_verts
->data
[1]);
250 fpme
->llvm
->jit_func( &fpme
->llvm
->jit_context
,
252 (const char **)draw
->pt
.user
.vbuffer
,
257 if (draw_pt_post_vs_run( fpme
->post_vs
,
265 /* Do we need to run the pipeline?
267 if (opt
& PT_PIPELINE
) {
268 draw_pipeline_run_linear( fpme
->draw
,
275 draw_pt_emit_linear( fpme
->emit
,
276 (const float (*)[4])pipeline_verts
->data
,
281 FREE(pipeline_verts
);
287 llvm_middle_end_linear_run_elts( struct draw_pt_middle_end
*middle
,
290 const ushort
*draw_elts
,
291 unsigned draw_count
)
293 struct llvm_middle_end
*fpme
= (struct llvm_middle_end
*)middle
;
294 struct draw_context
*draw
= fpme
->draw
;
295 struct draw_vertex_shader
*shader
= draw
->vs
.vertex_shader
;
296 struct draw_geometry_shader
*geometry_shader
= draw
->gs
.geometry_shader
;
297 unsigned opt
= fpme
->opt
;
298 unsigned alloc_count
= align( count
, 4 );
300 struct vertex_header
*pipeline_verts
=
301 (struct vertex_header
*)MALLOC(fpme
->vertex_size
* alloc_count
);
306 /* Fetch into our vertex buffer
308 draw_pt_fetch_run_linear( fpme
->fetch
,
311 (char *)pipeline_verts
);
313 /* Run the shader, note that this overwrites the data[] parts of
314 * the pipeline verts. If there is no shader, ie if
315 * bypass_vs_clip_and_viewport, then the inputs == outputs, and are
316 * already in the correct place.
320 shader
->run_linear(shader
,
321 (const float (*)[4])pipeline_verts
->data
,
322 ( float (*)[4])pipeline_verts
->data
,
323 draw
->pt
.user
.vs_constants
,
329 draw_geometry_shader_run(geometry_shader
,
330 (const float (*)[4])pipeline_verts
->data
,
331 ( float (*)[4])pipeline_verts
->data
,
332 draw
->pt
.user
.gs_constants
,
338 if (draw_pt_post_vs_run( fpme
->post_vs
,
346 /* Do we need to run the pipeline?
348 if (opt
& PT_PIPELINE
) {
349 draw_pipeline_run( fpme
->draw
,
358 draw_pt_emit( fpme
->emit
,
359 (const float (*)[4])pipeline_verts
->data
,
366 FREE(pipeline_verts
);
372 static void llvm_middle_end_finish( struct draw_pt_middle_end
*middle
)
377 static void llvm_middle_end_destroy( struct draw_pt_middle_end
*middle
)
379 struct llvm_middle_end
*fpme
= (struct llvm_middle_end
*)middle
;
382 draw_pt_fetch_destroy( fpme
->fetch
);
385 draw_pt_emit_destroy( fpme
->emit
);
388 draw_pt_post_vs_destroy( fpme
->post_vs
);
391 draw_llvm_destroy( fpme
->llvm
);
397 struct draw_pt_middle_end
*draw_pt_fetch_pipeline_or_emit_llvm( struct draw_context
*draw
)
399 struct llvm_middle_end
*fpme
= 0;
404 fpme
= CALLOC_STRUCT( llvm_middle_end
);
408 fpme
->base
.prepare
= llvm_middle_end_prepare
;
409 fpme
->base
.run
= llvm_middle_end_run
;
410 fpme
->base
.run_linear
= llvm_middle_end_linear_run
;
411 fpme
->base
.run_linear_elts
= llvm_middle_end_linear_run_elts
;
412 fpme
->base
.finish
= llvm_middle_end_finish
;
413 fpme
->base
.destroy
= llvm_middle_end_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
);
429 fpme
->llvm
= draw_llvm_create(draw
);
437 llvm_middle_end_destroy( &fpme
->base
);