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_gs.h"
32 #include "draw/draw_vbuf.h"
33 #include "draw/draw_vertex.h"
34 #include "draw/draw_pt.h"
35 #include "draw/draw_vs.h"
36 #include "draw/draw_llvm.h"
39 struct llvm_middle_end {
40 struct draw_pt_middle_end base;
41 struct draw_context *draw;
44 struct pt_so_emit *so_emit;
45 struct pt_fetch *fetch;
46 struct pt_post_vs *post_vs;
49 unsigned vertex_data_offset;
54 struct draw_llvm *llvm;
55 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 llvm_vertex_shader *shader =
68 llvm_vertex_shader(draw->vs.vertex_shader);
69 struct draw_llvm_variant_key key;
70 struct draw_llvm_variant *variant = NULL;
71 struct draw_llvm_variant_list_item *li;
73 unsigned instance_id_index = ~0;
76 unsigned out_prim = (draw->gs.geometry_shader ?
77 draw->gs.geometry_shader->output_primitive :
80 /* Add one to num_outputs because the pipeline occasionally tags on
81 * an additional texcoord, eg for AA lines.
83 unsigned nr = MAX2( shader->base.info.num_inputs,
84 shader->base.info.num_outputs + 1 );
86 /* Scan for instanceID system value.
88 for (i = 0; i < shader->base.info.num_inputs; i++) {
89 if (shader->base.info.input_semantic_name[i] == TGSI_SEMANTIC_INSTANCEID) {
90 instance_id_index = i;
95 fpme->input_prim = in_prim;
98 /* Always leave room for the vertex header whether we need it or
99 * not. It's hard to get rid of it in particular because of the
100 * viewport code in draw_pt_post_vs.c.
102 fpme->vertex_size = sizeof(struct vertex_header) + nr * 4 * sizeof(float);
105 /* XXX: it's not really gl rasterization rules we care about here,
106 * but gl vs dx9 clip spaces.
108 draw_pt_post_vs_prepare( fpme->post_vs,
109 (boolean)draw->bypass_clipping,
110 (boolean)(draw->identity_viewport),
111 (boolean)draw->rasterizer->gl_rasterization_rules,
112 (draw->vs.edgeflag_output ? TRUE : FALSE) );
114 draw_pt_so_emit_prepare( fpme->so_emit );
116 if (!(opt & PT_PIPELINE)) {
117 draw_pt_emit_prepare( fpme->emit,
121 *max_vertices = MAX2( *max_vertices,
122 DRAW_PIPE_MAX_VERTICES );
125 *max_vertices = DRAW_PIPE_MAX_VERTICES;
128 /* return even number */
129 *max_vertices = *max_vertices & ~1;
131 draw_llvm_make_variant_key(fpme->llvm, &key);
133 li = first_elem(&shader->variants);
134 while(!at_end(&shader->variants, li)) {
135 if(memcmp(&li->base->key, &key, sizeof key) == 0) {
143 move_to_head(&fpme->llvm->vs_variants_list, &variant->list_item_global);
147 if (fpme->llvm->nr_variants >= DRAW_MAX_SHADER_VARIANTS) {
149 * XXX: should we flush here ?
151 for (i = 0; i < DRAW_MAX_SHADER_VARIANTS / 4; i++) {
152 struct draw_llvm_variant_list_item *item =
153 last_elem(&fpme->llvm->vs_variants_list);
154 draw_llvm_destroy_variant(item->base);
158 variant = draw_llvm_create_variant(fpme->llvm, nr);
161 insert_at_head(&shader->variants, &variant->list_item_local);
162 insert_at_head(&fpme->llvm->vs_variants_list, &variant->list_item_global);
163 fpme->llvm->nr_variants++;
164 shader->variants_cached++;
168 fpme->current_variant = variant;
170 /*XXX we only support one constant buffer */
171 fpme->llvm->jit_context.vs_constants =
172 draw->pt.user.vs_constants[0];
173 fpme->llvm->jit_context.gs_constants =
174 draw->pt.user.gs_constants[0];
178 static void pipeline(struct llvm_middle_end *llvm,
179 const struct draw_vertex_info *vert_info,
180 const struct draw_prim_info *prim_info)
182 if (prim_info->linear)
183 draw_pipeline_run_linear( llvm->draw,
187 draw_pipeline_run( llvm->draw,
192 static void emit(struct pt_emit *emit,
193 const struct draw_vertex_info *vert_info,
194 const struct draw_prim_info *prim_info)
196 if (prim_info->linear) {
197 draw_pt_emit_linear(emit, vert_info, prim_info);
200 draw_pt_emit(emit, vert_info, prim_info);
205 llvm_pipeline_generic( struct draw_pt_middle_end *middle,
206 const struct draw_fetch_info *fetch_info,
207 const struct draw_prim_info *prim_info )
209 struct llvm_middle_end *fpme = (struct llvm_middle_end *)middle;
210 struct draw_context *draw = fpme->draw;
211 struct draw_geometry_shader *gshader = draw->gs.geometry_shader;
212 struct draw_prim_info gs_prim_info;
213 struct draw_vertex_info llvm_vert_info;
214 struct draw_vertex_info gs_vert_info;
215 struct draw_vertex_info *vert_info;
216 unsigned opt = fpme->opt;
218 llvm_vert_info.count = fetch_info->count;
219 llvm_vert_info.vertex_size = fpme->vertex_size;
220 llvm_vert_info.stride = fpme->vertex_size;
221 llvm_vert_info.verts =
222 (struct vertex_header *)MALLOC(fpme->vertex_size *
223 align(fetch_info->count, 4));
224 if (!llvm_vert_info.verts) {
229 if (fetch_info->linear)
230 fpme->current_variant->jit_func( &fpme->llvm->jit_context,
231 llvm_vert_info.verts,
232 (const char **)draw->pt.user.vbuffer,
236 draw->pt.vertex_buffer );
238 fpme->current_variant->jit_func_elts( &fpme->llvm->jit_context,
239 llvm_vert_info.verts,
240 (const char **)draw->pt.user.vbuffer,
244 draw->pt.vertex_buffer);
246 /* Finished with fetch and vs:
249 vert_info = &llvm_vert_info;
252 if ((opt & PT_SHADE) && gshader) {
253 draw_geometry_shader_run(gshader,
254 draw->pt.user.gs_constants,
260 FREE(vert_info->verts);
261 vert_info = &gs_vert_info;
262 prim_info = &gs_prim_info;
265 /* stream output needs to be done before clipping */
266 draw_pt_so_emit( fpme->so_emit,
270 if (draw_pt_post_vs_run( fpme->post_vs, vert_info )) {
274 /* Do we need to run the pipeline?
276 if (opt & PT_PIPELINE) {
286 FREE(vert_info->verts);
290 static void llvm_middle_end_run( struct draw_pt_middle_end *middle,
291 const unsigned *fetch_elts,
292 unsigned fetch_count,
293 const ushort *draw_elts,
294 unsigned draw_count )
296 struct llvm_middle_end *fpme = (struct llvm_middle_end *)middle;
297 struct draw_fetch_info fetch_info;
298 struct draw_prim_info prim_info;
300 fetch_info.linear = FALSE;
301 fetch_info.start = 0;
302 fetch_info.elts = fetch_elts;
303 fetch_info.count = fetch_count;
305 prim_info.linear = FALSE;
307 prim_info.count = draw_count;
308 prim_info.elts = draw_elts;
309 prim_info.prim = fpme->input_prim;
310 prim_info.primitive_count = 1;
311 prim_info.primitive_lengths = &draw_count;
313 llvm_pipeline_generic( middle, &fetch_info, &prim_info );
317 static void llvm_middle_end_linear_run( struct draw_pt_middle_end *middle,
321 struct llvm_middle_end *fpme = (struct llvm_middle_end *)middle;
322 struct draw_fetch_info fetch_info;
323 struct draw_prim_info prim_info;
325 fetch_info.linear = TRUE;
326 fetch_info.start = start;
327 fetch_info.count = count;
328 fetch_info.elts = NULL;
330 prim_info.linear = TRUE;
332 prim_info.count = count;
333 prim_info.elts = NULL;
334 prim_info.prim = fpme->input_prim;
335 prim_info.primitive_count = 1;
336 prim_info.primitive_lengths = &count;
338 llvm_pipeline_generic( middle, &fetch_info, &prim_info );
344 llvm_middle_end_linear_run_elts( struct draw_pt_middle_end *middle,
347 const ushort *draw_elts,
348 unsigned draw_count )
350 struct llvm_middle_end *fpme = (struct llvm_middle_end *)middle;
351 struct draw_fetch_info fetch_info;
352 struct draw_prim_info prim_info;
354 fetch_info.linear = TRUE;
355 fetch_info.start = start;
356 fetch_info.count = count;
357 fetch_info.elts = NULL;
359 prim_info.linear = FALSE;
361 prim_info.count = draw_count;
362 prim_info.elts = draw_elts;
363 prim_info.prim = fpme->input_prim;
364 prim_info.primitive_count = 1;
365 prim_info.primitive_lengths = &draw_count;
367 llvm_pipeline_generic( middle, &fetch_info, &prim_info );
374 static void llvm_middle_end_finish( struct draw_pt_middle_end *middle )
379 static void llvm_middle_end_destroy( struct draw_pt_middle_end *middle )
381 struct llvm_middle_end *fpme = (struct llvm_middle_end *)middle;
384 draw_pt_fetch_destroy( fpme->fetch );
387 draw_pt_emit_destroy( fpme->emit );
390 draw_pt_so_emit_destroy( fpme->so_emit );
393 draw_pt_post_vs_destroy( fpme->post_vs );
396 draw_llvm_destroy( fpme->llvm );
402 struct draw_pt_middle_end *
403 draw_pt_fetch_pipeline_or_emit_llvm(struct draw_context *draw)
405 struct llvm_middle_end *fpme = 0;
410 fpme = CALLOC_STRUCT( llvm_middle_end );
414 fpme->base.prepare = llvm_middle_end_prepare;
415 fpme->base.run = llvm_middle_end_run;
416 fpme->base.run_linear = llvm_middle_end_linear_run;
417 fpme->base.run_linear_elts = llvm_middle_end_linear_run_elts;
418 fpme->base.finish = llvm_middle_end_finish;
419 fpme->base.destroy = llvm_middle_end_destroy;
423 fpme->fetch = draw_pt_fetch_create( draw );
427 fpme->post_vs = draw_pt_post_vs_create( draw );
431 fpme->emit = draw_pt_emit_create( draw );
435 fpme->so_emit = draw_pt_so_emit_create( draw );
439 fpme->llvm = draw_llvm_create(draw);
443 fpme->current_variant = NULL;
449 llvm_middle_end_destroy( &fpme->base );