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_llvm.h"
38 struct llvm_middle_end
{
39 struct draw_pt_middle_end base
;
40 struct draw_context
*draw
;
43 struct pt_so_emit
*so_emit
;
44 struct pt_fetch
*fetch
;
45 struct pt_post_vs
*post_vs
;
48 unsigned vertex_data_offset
;
54 struct draw_llvm
*llvm
;
55 struct draw_llvm_variant
*variants
;
56 struct draw_llvm_variant
*current_variant
;
62 llvm_middle_end_prepare( struct draw_pt_middle_end
*middle
,
66 unsigned *max_vertices
)
68 struct llvm_middle_end
*fpme
= (struct llvm_middle_end
*)middle
;
69 struct draw_context
*draw
= fpme
->draw
;
70 struct draw_vertex_shader
*vs
= draw
->vs
.vertex_shader
;
71 struct draw_llvm_variant_key key
;
72 struct draw_llvm_variant
*variant
= NULL
;
74 unsigned instance_id_index
= ~0;
76 /* Add one to num_outputs because the pipeline occasionally tags on
77 * an additional texcoord, eg for AA lines.
79 unsigned nr
= MAX2( vs
->info
.num_inputs
,
80 vs
->info
.num_outputs
+ 1 );
82 /* Scan for instanceID system value.
84 for (i
= 0; i
< vs
->info
.num_inputs
; i
++) {
85 if (vs
->info
.input_semantic_name
[i
] == TGSI_SEMANTIC_INSTANCEID
) {
86 instance_id_index
= i
;
91 fpme
->input_prim
= in_prim
;
92 fpme
->output_prim
= out_prim
;
95 /* Always leave room for the vertex header whether we need it or
96 * not. It's hard to get rid of it in particular because of the
97 * viewport code in draw_pt_post_vs.c.
99 fpme
->vertex_size
= sizeof(struct vertex_header
) + nr
* 4 * sizeof(float);
102 /* XXX: it's not really gl rasterization rules we care about here,
103 * but gl vs dx9 clip spaces.
105 draw_pt_post_vs_prepare( fpme
->post_vs
,
106 (boolean
)draw
->bypass_clipping
,
107 (boolean
)(draw
->identity_viewport
),
108 (boolean
)draw
->rasterizer
->gl_rasterization_rules
,
109 (draw
->vs
.edgeflag_output
? true : false) );
111 draw_pt_so_emit_prepare( fpme
->so_emit
, out_prim
);
112 if (!(opt
& PT_PIPELINE
)) {
113 draw_pt_emit_prepare( fpme
->emit
,
117 *max_vertices
= MAX2( *max_vertices
,
118 DRAW_PIPE_MAX_VERTICES
);
121 *max_vertices
= DRAW_PIPE_MAX_VERTICES
;
124 /* return even number */
125 *max_vertices
= *max_vertices
& ~1;
127 draw_llvm_make_variant_key(fpme
->llvm
, &key
);
129 variant
= fpme
->variants
;
131 if(memcmp(&variant
->key
, &key
, sizeof key
) == 0)
134 variant
= variant
->next
;
138 variant
= draw_llvm_prepare(fpme
->llvm
, nr
);
139 variant
->next
= fpme
->variants
;
140 fpme
->variants
= variant
;
143 fpme
->current_variant
= variant
;
145 /*XXX we only support one constant buffer */
146 fpme
->llvm
->jit_context
.vs_constants
=
147 draw
->pt
.user
.vs_constants
[0];
148 fpme
->llvm
->jit_context
.gs_constants
=
149 draw
->pt
.user
.gs_constants
[0];
154 static void llvm_middle_end_run( struct draw_pt_middle_end
*middle
,
155 const unsigned *fetch_elts
,
156 unsigned fetch_count
,
157 const ushort
*draw_elts
,
158 unsigned draw_count
)
160 struct llvm_middle_end
*fpme
= (struct llvm_middle_end
*)middle
;
161 struct draw_context
*draw
= fpme
->draw
;
162 unsigned opt
= fpme
->opt
;
163 unsigned alloc_count
= align( fetch_count
, 4 );
165 struct vertex_header
*pipeline_verts
=
166 (struct vertex_header
*)MALLOC(fpme
->vertex_size
* alloc_count
);
168 if (!pipeline_verts
) {
169 /* Not much we can do here - just skip the rendering.
175 fpme
->current_variant
->jit_func_elts( &fpme
->llvm
->jit_context
,
177 (const char **)draw
->pt
.user
.vbuffer
,
181 draw
->pt
.vertex_buffer
);
183 /* stream output needs to be done before clipping */
184 draw_pt_so_emit( fpme
->so_emit
,
185 (const float (*)[4])pipeline_verts
->data
,
189 if (draw_pt_post_vs_run( fpme
->post_vs
,
197 /* Do we need to run the pipeline?
199 if (opt
& PT_PIPELINE
) {
200 draw_pipeline_run( fpme
->draw
,
209 draw_pt_emit( fpme
->emit
,
210 (const float (*)[4])pipeline_verts
->data
,
218 FREE(pipeline_verts
);
222 static void llvm_middle_end_linear_run( struct draw_pt_middle_end
*middle
,
226 struct llvm_middle_end
*fpme
= (struct llvm_middle_end
*)middle
;
227 struct draw_context
*draw
= fpme
->draw
;
228 unsigned opt
= fpme
->opt
;
229 unsigned alloc_count
= align( count
, 4 );
231 struct vertex_header
*pipeline_verts
=
232 (struct vertex_header
*)MALLOC(fpme
->vertex_size
* alloc_count
);
234 if (!pipeline_verts
) {
235 /* Not much we can do here - just skip the rendering.
242 debug_printf("#### Pipeline = %p (data = %p)\n",
243 pipeline_verts
, pipeline_verts
->data
);
245 fpme
->current_variant
->jit_func( &fpme
->llvm
->jit_context
,
247 (const char **)draw
->pt
.user
.vbuffer
,
251 draw
->pt
.vertex_buffer
);
253 /* stream output needs to be done before clipping */
254 draw_pt_so_emit( fpme
->so_emit
,
255 (const float (*)[4])pipeline_verts
->data
,
259 if (draw_pt_post_vs_run( fpme
->post_vs
,
267 /* Do we need to run the pipeline?
269 if (opt
& PT_PIPELINE
) {
270 draw_pipeline_run_linear( fpme
->draw
,
277 draw_pt_emit_linear( fpme
->emit
,
278 (const float (*)[4])pipeline_verts
->data
,
283 FREE(pipeline_verts
);
289 llvm_middle_end_linear_run_elts( struct draw_pt_middle_end
*middle
,
292 const ushort
*draw_elts
,
293 unsigned draw_count
)
295 struct llvm_middle_end
*fpme
= (struct llvm_middle_end
*)middle
;
296 struct draw_context
*draw
= fpme
->draw
;
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 fpme
->current_variant
->jit_func( &fpme
->llvm
->jit_context
,
308 (const char **)draw
->pt
.user
.vbuffer
,
312 draw
->pt
.vertex_buffer
);
314 /* stream output needs to be done before clipping */
315 draw_pt_so_emit( fpme
->so_emit
,
316 (const float (*)[4])pipeline_verts
->data
,
320 if (draw_pt_post_vs_run( fpme
->post_vs
,
328 /* Do we need to run the pipeline?
330 if (opt
& PT_PIPELINE
) {
331 draw_pipeline_run( fpme
->draw
,
340 draw_pt_emit( fpme
->emit
,
341 (const float (*)[4])pipeline_verts
->data
,
348 FREE(pipeline_verts
);
354 static void llvm_middle_end_finish( struct draw_pt_middle_end
*middle
)
359 static void llvm_middle_end_destroy( struct draw_pt_middle_end
*middle
)
361 struct llvm_middle_end
*fpme
= (struct llvm_middle_end
*)middle
;
362 struct draw_context
*draw
= fpme
->draw
;
363 struct draw_llvm_variant
*variant
= NULL
;
365 variant
= fpme
->variants
;
367 struct draw_llvm_variant
*next
= variant
->next
;
369 if (variant
->function_elts
) {
370 if (variant
->function_elts
)
371 LLVMFreeMachineCodeForFunction(draw
->engine
,
372 variant
->function_elts
);
373 LLVMDeleteFunction(variant
->function_elts
);
376 if (variant
->function
) {
377 if (variant
->function
)
378 LLVMFreeMachineCodeForFunction(draw
->engine
,
380 LLVMDeleteFunction(variant
->function
);
388 draw_pt_fetch_destroy( fpme
->fetch
);
391 draw_pt_emit_destroy( fpme
->emit
);
394 draw_pt_so_emit_destroy( fpme
->so_emit
);
397 draw_pt_post_vs_destroy( fpme
->post_vs
);
400 draw_llvm_destroy( fpme
->llvm
);
406 struct draw_pt_middle_end
*draw_pt_fetch_pipeline_or_emit_llvm( struct draw_context
*draw
)
408 struct llvm_middle_end
*fpme
= 0;
413 fpme
= CALLOC_STRUCT( llvm_middle_end
);
417 fpme
->base
.prepare
= llvm_middle_end_prepare
;
418 fpme
->base
.run
= llvm_middle_end_run
;
419 fpme
->base
.run_linear
= llvm_middle_end_linear_run
;
420 fpme
->base
.run_linear_elts
= llvm_middle_end_linear_run_elts
;
421 fpme
->base
.finish
= llvm_middle_end_finish
;
422 fpme
->base
.destroy
= llvm_middle_end_destroy
;
426 fpme
->fetch
= draw_pt_fetch_create( draw
);
430 fpme
->post_vs
= draw_pt_post_vs_create( draw
);
434 fpme
->emit
= draw_pt_emit_create( draw
);
438 fpme
->so_emit
= draw_pt_so_emit_create( draw
);
442 fpme
->llvm
= draw_llvm_create(draw
);
446 fpme
->variants
= NULL
;
447 fpme
->current_variant
= NULL
;
448 fpme
->nr_variants
= 0;
454 llvm_middle_end_destroy( &fpme
->base
);