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_fetch *fetch;
44 struct pt_post_vs *post_vs;
47 unsigned vertex_data_offset;
52 struct draw_llvm *llvm;
53 struct draw_llvm_variant *variants;
54 struct draw_llvm_variant *current_variant;
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_llvm_variant_key key;
69 struct draw_llvm_variant *variant = NULL;
71 unsigned instance_id_index = ~0;
73 /* Add one to num_outputs because the pipeline occasionally tags on
74 * an additional texcoord, eg for AA lines.
76 unsigned nr = MAX2( vs->info.num_inputs,
77 vs->info.num_outputs + 1 );
79 /* Scan for instanceID system value.
81 for (i = 0; i < vs->info.num_inputs; i++) {
82 if (vs->info.input_semantic_name[i] == TGSI_SEMANTIC_INSTANCEID) {
83 instance_id_index = i;
91 /* Always leave room for the vertex header whether we need it or
92 * not. It's hard to get rid of it in particular because of the
93 * viewport code in draw_pt_post_vs.c.
95 fpme->vertex_size = sizeof(struct vertex_header) + nr * 4 * sizeof(float);
98 /* XXX: it's not really gl rasterization rules we care about here,
99 * but gl vs dx9 clip spaces.
101 draw_pt_post_vs_prepare( fpme->post_vs,
102 (boolean)draw->bypass_clipping,
103 (boolean)(draw->identity_viewport),
104 (boolean)draw->rasterizer->gl_rasterization_rules,
105 (draw->vs.edgeflag_output ? true : false) );
107 if (!(opt & PT_PIPELINE)) {
108 draw_pt_emit_prepare( fpme->emit,
112 *max_vertices = MAX2( *max_vertices,
113 DRAW_PIPE_MAX_VERTICES );
116 *max_vertices = DRAW_PIPE_MAX_VERTICES;
119 /* return even number */
120 *max_vertices = *max_vertices & ~1;
122 draw_llvm_make_variant_key(fpme->llvm, &key);
124 variant = fpme->variants;
126 if(memcmp(&variant->key, &key, sizeof key) == 0)
129 variant = variant->next;
133 variant = draw_llvm_prepare(fpme->llvm, nr);
134 variant->next = fpme->variants;
135 fpme->variants = variant;
138 fpme->current_variant = variant;
140 /*XXX we only support one constant buffer */
141 fpme->llvm->jit_context.vs_constants =
142 draw->pt.user.vs_constants[0];
143 fpme->llvm->jit_context.gs_constants =
144 draw->pt.user.gs_constants[0];
149 static void llvm_middle_end_run( struct draw_pt_middle_end *middle,
150 const unsigned *fetch_elts,
151 unsigned fetch_count,
152 const ushort *draw_elts,
153 unsigned draw_count )
155 struct llvm_middle_end *fpme = (struct llvm_middle_end *)middle;
156 struct draw_context *draw = fpme->draw;
157 unsigned opt = fpme->opt;
158 unsigned alloc_count = align( fetch_count, 4 );
160 struct vertex_header *pipeline_verts =
161 (struct vertex_header *)MALLOC(fpme->vertex_size * alloc_count);
163 if (!pipeline_verts) {
164 /* Not much we can do here - just skip the rendering.
170 fpme->current_variant->jit_func_elts( &fpme->llvm->jit_context,
172 (const char **)draw->pt.user.vbuffer,
176 draw->pt.vertex_buffer );
178 if (draw_pt_post_vs_run( fpme->post_vs,
186 /* Do we need to run the pipeline?
188 if (opt & PT_PIPELINE) {
189 draw_pipeline_run( fpme->draw,
198 draw_pt_emit( fpme->emit,
199 (const float (*)[4])pipeline_verts->data,
207 FREE(pipeline_verts);
211 static void llvm_middle_end_linear_run( struct draw_pt_middle_end *middle,
215 struct llvm_middle_end *fpme = (struct llvm_middle_end *)middle;
216 struct draw_context *draw = fpme->draw;
217 unsigned opt = fpme->opt;
218 unsigned alloc_count = align( count, 4 );
220 struct vertex_header *pipeline_verts =
221 (struct vertex_header *)MALLOC(fpme->vertex_size * alloc_count);
223 if (!pipeline_verts) {
224 /* Not much we can do here - just skip the rendering.
231 debug_printf("#### Pipeline = %p (data = %p)\n",
232 pipeline_verts, pipeline_verts->data);
234 fpme->current_variant->jit_func( &fpme->llvm->jit_context,
236 (const char **)draw->pt.user.vbuffer,
240 draw->pt.vertex_buffer );
242 if (draw_pt_post_vs_run( fpme->post_vs,
250 /* Do we need to run the pipeline?
252 if (opt & PT_PIPELINE) {
253 draw_pipeline_run_linear( fpme->draw,
260 draw_pt_emit_linear( fpme->emit,
261 (const float (*)[4])pipeline_verts->data,
266 FREE(pipeline_verts);
272 llvm_middle_end_linear_run_elts( struct draw_pt_middle_end *middle,
275 const ushort *draw_elts,
276 unsigned draw_count )
278 struct llvm_middle_end *fpme = (struct llvm_middle_end *)middle;
279 struct draw_context *draw = fpme->draw;
280 unsigned opt = fpme->opt;
281 unsigned alloc_count = align( count, 4 );
283 struct vertex_header *pipeline_verts =
284 (struct vertex_header *)MALLOC(fpme->vertex_size * alloc_count);
289 fpme->current_variant->jit_func( &fpme->llvm->jit_context,
291 (const char **)draw->pt.user.vbuffer,
295 draw->pt.vertex_buffer );
297 if (draw_pt_post_vs_run( fpme->post_vs,
305 /* Do we need to run the pipeline?
307 if (opt & PT_PIPELINE) {
308 draw_pipeline_run( fpme->draw,
317 draw_pt_emit( fpme->emit,
318 (const float (*)[4])pipeline_verts->data,
325 FREE(pipeline_verts);
331 static void llvm_middle_end_finish( struct draw_pt_middle_end *middle )
336 static void llvm_middle_end_destroy( struct draw_pt_middle_end *middle )
338 struct llvm_middle_end *fpme = (struct llvm_middle_end *)middle;
339 struct draw_context *draw = fpme->draw;
340 struct draw_llvm_variant *variant = NULL;
342 variant = fpme->variants;
344 struct draw_llvm_variant *next = variant->next;
346 if (variant->function_elts) {
347 if (variant->function_elts)
348 LLVMFreeMachineCodeForFunction(draw->engine,
349 variant->function_elts);
350 LLVMDeleteFunction(variant->function_elts);
353 if (variant->function) {
354 if (variant->function)
355 LLVMFreeMachineCodeForFunction(draw->engine,
357 LLVMDeleteFunction(variant->function);
365 draw_pt_fetch_destroy( fpme->fetch );
368 draw_pt_emit_destroy( fpme->emit );
371 draw_pt_post_vs_destroy( fpme->post_vs );
374 draw_llvm_destroy( fpme->llvm );
380 struct draw_pt_middle_end *draw_pt_fetch_pipeline_or_emit_llvm( struct draw_context *draw )
382 struct llvm_middle_end *fpme = 0;
387 fpme = CALLOC_STRUCT( llvm_middle_end );
391 fpme->base.prepare = llvm_middle_end_prepare;
392 fpme->base.run = llvm_middle_end_run;
393 fpme->base.run_linear = llvm_middle_end_linear_run;
394 fpme->base.run_linear_elts = llvm_middle_end_linear_run_elts;
395 fpme->base.finish = llvm_middle_end_finish;
396 fpme->base.destroy = llvm_middle_end_destroy;
400 fpme->fetch = draw_pt_fetch_create( draw );
404 fpme->post_vs = draw_pt_post_vs_create( draw );
408 fpme->emit = draw_pt_emit_create( draw );
412 fpme->llvm = draw_llvm_create(draw);
416 fpme->variants = NULL;
417 fpme->current_variant = NULL;
418 fpme->nr_variants = 0;
424 llvm_middle_end_destroy( &fpme->base );