OSDN Git Service

gallium/llvm: implement geometry shaders in the llvm paths
[android-x86/external-mesa.git] / src / gallium / auxiliary / draw / draw_gs.c
1 /**************************************************************************
2  *
3  * Copyright 2009 VMware, Inc.
4  * All Rights Reserved.
5  *
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:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
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.
25  *
26  **************************************************************************/
27
28 #include "draw_gs.h"
29
30 #include "draw_private.h"
31 #include "draw_context.h"
32 #ifdef HAVE_LLVM
33 #include "draw_llvm.h"
34 #endif
35
36 #include "tgsi/tgsi_parse.h"
37 #include "tgsi/tgsi_exec.h"
38
39 #include "pipe/p_shader_tokens.h"
40
41 #include "util/u_math.h"
42 #include "util/u_memory.h"
43 #include "util/u_prim.h"
44
45 /* fixme: move it from here */
46 #define MAX_PRIMITIVES 64
47
48 static INLINE int
49 draw_gs_get_input_index(int semantic, int index,
50                         const struct tgsi_shader_info *input_info)
51 {
52    int i;
53    const ubyte *input_semantic_names = input_info->output_semantic_name;
54    const ubyte *input_semantic_indices = input_info->output_semantic_index;
55    for (i = 0; i < PIPE_MAX_SHADER_OUTPUTS; i++) {
56       if (input_semantic_names[i] == semantic &&
57           input_semantic_indices[i] == index)
58          return i;
59    }
60    debug_assert(0);
61    return -1;
62 }
63
64 /**
65  * We execute geometry shaders in the SOA mode, so ideally we want to
66  * flush when the number of currently fetched primitives is equal to
67  * the number of elements in the SOA vector. This ensures that the
68  * throughput is optimized for the given vector instrunction set.
69  */
70 static INLINE boolean
71 draw_gs_should_flush(struct draw_geometry_shader *shader)
72 {
73    return (shader->fetched_prim_count == shader->vector_length);
74 }
75
76 /*#define DEBUG_OUTPUTS 1*/
77 static void
78 tgsi_fetch_gs_outputs(struct draw_geometry_shader *shader,
79                       unsigned num_primitives,
80                       float (**p_output)[4])
81 {
82    struct tgsi_exec_machine *machine = shader->machine;
83    unsigned prim_idx, j, slot;
84    unsigned current_idx = 0;
85    float (*output)[4];
86
87    output = *p_output;
88
89    /* Unswizzle all output results.
90     */
91
92    for (prim_idx = 0; prim_idx < num_primitives; ++prim_idx) {
93       unsigned num_verts_per_prim = machine->Primitives[prim_idx];
94       shader->primitive_lengths[prim_idx +   shader->emitted_primitives] =
95          machine->Primitives[prim_idx];
96       shader->emitted_vertices += num_verts_per_prim;
97       for (j = 0; j < num_verts_per_prim; j++, current_idx++) {
98          int idx = current_idx * shader->info.num_outputs;
99 #ifdef DEBUG_OUTPUTS
100          debug_printf("%d) Output vert:\n", idx / shader->info.num_outputs);
101 #endif
102          for (slot = 0; slot < shader->info.num_outputs; slot++) {
103             output[slot][0] = machine->Outputs[idx + slot].xyzw[0].f[0];
104             output[slot][1] = machine->Outputs[idx + slot].xyzw[1].f[0];
105             output[slot][2] = machine->Outputs[idx + slot].xyzw[2].f[0];
106             output[slot][3] = machine->Outputs[idx + slot].xyzw[3].f[0];
107 #ifdef DEBUG_OUTPUTS
108             debug_printf("\t%d: %f %f %f %f\n", slot,
109                          output[slot][0],
110                          output[slot][1],
111                          output[slot][2],
112                          output[slot][3]);
113 #endif
114             debug_assert(!util_is_inf_or_nan(output[slot][0]));
115          }
116          output = (float (*)[4])((char *)output + shader->vertex_size);
117       }
118    }
119    *p_output = output;
120    shader->emitted_primitives += num_primitives;
121 }
122
123 /*#define DEBUG_INPUTS 1*/
124 static void tgsi_fetch_gs_input(struct draw_geometry_shader *shader,
125                                 unsigned *indices,
126                                 unsigned num_vertices,
127                                 unsigned prim_idx)
128 {
129    struct tgsi_exec_machine *machine = shader->machine;
130    unsigned slot, vs_slot, i;
131    unsigned input_vertex_stride = shader->input_vertex_stride;
132    const float (*input_ptr)[4];
133
134    input_ptr = shader->input;
135
136    for (i = 0; i < num_vertices; ++i) {
137       const float (*input)[4];
138 #if DEBUG_INPUTS
139       debug_printf("%d) vertex index = %d (prim idx = %d)\n",
140                    i, indices[i], prim_idx);
141 #endif
142       input = (const float (*)[4])(
143          (const char *)input_ptr + (indices[i] * input_vertex_stride));
144       for (slot = 0, vs_slot = 0; slot < shader->info.num_inputs; ++slot) {
145          unsigned idx = i * TGSI_EXEC_MAX_INPUT_ATTRIBS + slot;
146          if (shader->info.input_semantic_name[slot] == TGSI_SEMANTIC_PRIMID) {
147             machine->Inputs[idx].xyzw[0].f[prim_idx] =
148                (float)shader->in_prim_idx;
149             machine->Inputs[idx].xyzw[1].f[prim_idx] =
150                (float)shader->in_prim_idx;
151             machine->Inputs[idx].xyzw[2].f[prim_idx] =
152                (float)shader->in_prim_idx;
153             machine->Inputs[idx].xyzw[3].f[prim_idx] =
154                (float)shader->in_prim_idx;
155          } else {
156             vs_slot = draw_gs_get_input_index(
157                         shader->info.input_semantic_name[slot],
158                         shader->info.input_semantic_index[slot],
159                         shader->input_info);
160 #if DEBUG_INPUTS
161             debug_printf("\tSlot = %d, vs_slot = %d, idx = %d:\n",
162                          slot, vs_slot, idx);
163 #endif
164 #if 1
165             assert(!util_is_inf_or_nan(input[vs_slot][0]));
166             assert(!util_is_inf_or_nan(input[vs_slot][1]));
167             assert(!util_is_inf_or_nan(input[vs_slot][2]));
168             assert(!util_is_inf_or_nan(input[vs_slot][3]));
169 #endif
170             machine->Inputs[idx].xyzw[0].f[prim_idx] = input[vs_slot][0];
171             machine->Inputs[idx].xyzw[1].f[prim_idx] = input[vs_slot][1];
172             machine->Inputs[idx].xyzw[2].f[prim_idx] = input[vs_slot][2];
173             machine->Inputs[idx].xyzw[3].f[prim_idx] = input[vs_slot][3];
174 #if DEBUG_INPUTS
175             debug_printf("\t\t%f %f %f %f\n",
176                          machine->Inputs[idx].xyzw[0].f[prim_idx],
177                          machine->Inputs[idx].xyzw[1].f[prim_idx],
178                          machine->Inputs[idx].xyzw[2].f[prim_idx],
179                          machine->Inputs[idx].xyzw[3].f[prim_idx]);
180 #endif
181             ++vs_slot;
182          }
183       }
184    }
185 }
186
187 static void tgsi_gs_prepare(struct draw_geometry_shader *shader,
188                             const void *constants[PIPE_MAX_CONSTANT_BUFFERS],
189                             const unsigned constants_size[PIPE_MAX_CONSTANT_BUFFERS])
190 {
191    struct tgsi_exec_machine *machine = shader->machine;
192
193    tgsi_exec_set_constant_buffers(machine, PIPE_MAX_CONSTANT_BUFFERS,
194                                   constants, constants_size);
195 }
196
197 static unsigned tgsi_gs_run(struct draw_geometry_shader *shader,
198                             unsigned input_primitives)
199 {
200    struct tgsi_exec_machine *machine = shader->machine;
201
202    tgsi_set_exec_mask(machine,
203                       1,
204                       input_primitives > 1,
205                       input_primitives > 2,
206                       input_primitives > 3);
207
208    /* run interpreter */
209    tgsi_exec_machine_run(machine);
210
211    return
212       machine->Temps[TGSI_EXEC_TEMP_PRIMITIVE_I].xyzw[TGSI_EXEC_TEMP_PRIMITIVE_C].u[0];
213 }
214
215 #ifdef HAVE_LLVM
216
217 static void
218 llvm_fetch_gs_input(struct draw_geometry_shader *shader,
219                     unsigned *indices,
220                     unsigned num_vertices,
221                     unsigned prim_idx)
222 {
223    unsigned slot, vs_slot, i;
224    unsigned input_vertex_stride = shader->input_vertex_stride;
225    const float (*input_ptr)[4];
226    float (*input_data)[6][PIPE_MAX_SHADER_INPUTS][TGSI_NUM_CHANNELS][TGSI_NUM_CHANNELS] = &shader->gs_input->data;
227
228    input_ptr = shader->input;
229
230    for (i = 0; i < num_vertices; ++i) {
231       const float (*input)[4];
232 #if DEBUG_INPUTS
233       debug_printf("%d) vertex index = %d (prim idx = %d)\n",
234                    i, indices[i], prim_idx);
235 #endif
236       input = (const float (*)[4])(
237          (const char *)input_ptr + (indices[i] * input_vertex_stride));
238       for (slot = 0, vs_slot = 0; slot < shader->info.num_inputs; ++slot) {
239          if (shader->info.input_semantic_name[slot] == TGSI_SEMANTIC_PRIMID) {
240             (*input_data)[i][slot][0][prim_idx] = (float)shader->in_prim_idx;
241             (*input_data)[i][slot][1][prim_idx] = (float)shader->in_prim_idx;
242             (*input_data)[i][slot][2][prim_idx] = (float)shader->in_prim_idx;
243             (*input_data)[i][slot][3][prim_idx] = (float)shader->in_prim_idx;
244          } else {
245             vs_slot = draw_gs_get_input_index(
246                         shader->info.input_semantic_name[slot],
247                         shader->info.input_semantic_index[slot],
248                         shader->input_info);
249 #if DEBUG_INPUTS
250             debug_printf("\tSlot = %d, vs_slot = %d, idx = %d:\n",
251                          slot, vs_slot, idx);
252 #endif
253 #if 0
254             assert(!util_is_inf_or_nan(input[vs_slot][0]));
255             assert(!util_is_inf_or_nan(input[vs_slot][1]));
256             assert(!util_is_inf_or_nan(input[vs_slot][2]));
257             assert(!util_is_inf_or_nan(input[vs_slot][3]));
258 #endif
259             (*input_data)[i][slot][0][prim_idx] = input[vs_slot][0];
260             (*input_data)[i][slot][1][prim_idx] = input[vs_slot][1];
261             (*input_data)[i][slot][2][prim_idx] = input[vs_slot][2];
262             (*input_data)[i][slot][3][prim_idx] = input[vs_slot][3];
263 #if DEBUG_INPUTS
264             debug_printf("\t\t%f %f %f %f\n",
265                          (*input_data)[i][slot][0][prim_idx],
266                          (*input_data)[i][slot][1][prim_idx],
267                          (*input_data)[i][slot][2][prim_idx],
268                          (*input_data)[i][slot][3][prim_idx]);
269 #endif
270             ++vs_slot;
271          }
272       }
273    }
274 }
275
276 static void
277 llvm_fetch_gs_outputs(struct draw_geometry_shader *shader,
278                       unsigned num_primitives,
279                       float (**p_output)[4])
280 {
281    int total_verts = 0;
282    int vertex_count = 0;
283    int total_prims = 0;
284    int max_prims_per_invocation = 0;
285    char *output_ptr = (char*)shader->gs_output;
286    int i, j, prim_idx;
287
288    for (i = 0; i < shader->vector_length; ++i) {
289       int prims = shader->llvm_emitted_primitives[i];
290       total_prims += prims;
291       max_prims_per_invocation = MAX2(max_prims_per_invocation, prims);
292    }
293    for (i = 0; i < shader->vector_length; ++i) {
294       total_verts += shader->llvm_emitted_vertices[i];
295    }
296
297
298    output_ptr += shader->emitted_vertices * shader->vertex_size;
299    for (i = 0; i < shader->vector_length - 1; ++i) {
300       int current_verts = shader->llvm_emitted_vertices[i];
301
302       if (current_verts != shader->max_output_vertices) {
303          memcpy(output_ptr + (vertex_count + current_verts) * shader->vertex_size,
304                 output_ptr + (vertex_count + shader->max_output_vertices) * shader->vertex_size,
305                 shader->vertex_size * (total_verts - vertex_count - current_verts));
306       }
307       vertex_count += current_verts;
308    }
309
310    prim_idx = 0;
311    for (i = 0; i < shader->vector_length; ++i) {
312       int num_prims = shader->llvm_emitted_primitives[i];
313       for (j = 0; j < num_prims; ++j) {
314          int prim_length =
315             shader->llvm_prim_lengths[j][i];
316          shader->primitive_lengths[shader->emitted_primitives + prim_idx] =
317             prim_length;
318          ++prim_idx;
319       }
320    }
321
322    shader->emitted_primitives += total_prims;
323    shader->emitted_vertices += total_verts;
324 }
325
326 static void
327 llvm_gs_prepare(struct draw_geometry_shader *shader,
328                 const void *constants[PIPE_MAX_CONSTANT_BUFFERS],
329                 const unsigned constants_size[PIPE_MAX_CONSTANT_BUFFERS])
330 {
331 }
332
333 static unsigned
334 llvm_gs_run(struct draw_geometry_shader *shader,
335             unsigned input_primitives)
336 {
337    unsigned ret;
338    char *input = (char*)shader->gs_output;
339
340    input += (shader->emitted_vertices * shader->vertex_size);
341
342    ret = shader->current_variant->jit_func(
343       shader->jit_context, shader->gs_input->data,
344       (struct vertex_header*)input,
345       input_primitives,
346       shader->draw->instance_id);
347
348    return ret;
349 }
350
351 #endif
352
353 static void gs_flush(struct draw_geometry_shader *shader)
354 {
355    unsigned out_prim_count;
356
357    unsigned input_primitives = shader->fetched_prim_count;
358
359    debug_assert(input_primitives > 0 &&
360                 input_primitives <= 4);
361
362    out_prim_count = shader->run(shader, input_primitives);
363    shader->fetch_outputs(shader, out_prim_count,
364                          &shader->tmp_output);
365
366 #if 0
367    debug_printf("PRIM emitted prims = %d (verts=%d), cur prim count = %d\n",
368                 shader->emitted_primitives, shader->emitted_vertices,
369                 out_prim_count);
370 #endif
371
372    shader->fetched_prim_count = 0;
373 }
374
375 static void gs_point(struct draw_geometry_shader *shader,
376                      int idx)
377 {
378    unsigned indices[1];
379
380    indices[0] = idx;
381
382    shader->fetch_inputs(shader, indices, 1,
383                         shader->fetched_prim_count);
384    ++shader->in_prim_idx;
385    ++shader->fetched_prim_count;
386
387    gs_flush(shader);
388 }
389
390 static void gs_line(struct draw_geometry_shader *shader,
391                     int i0, int i1)
392 {
393    unsigned indices[2];
394
395    indices[0] = i0;
396    indices[1] = i1;
397
398    shader->fetch_inputs(shader, indices, 2,
399                         shader->fetched_prim_count);
400    ++shader->in_prim_idx;
401    ++shader->fetched_prim_count;
402
403    gs_flush(shader);
404 }
405
406 static void gs_line_adj(struct draw_geometry_shader *shader,
407                         int i0, int i1, int i2, int i3)
408 {
409    unsigned indices[4];
410
411    indices[0] = i0;
412    indices[1] = i1;
413    indices[2] = i2;
414    indices[3] = i3;
415
416    shader->fetch_inputs(shader, indices, 4,
417                         shader->fetched_prim_count);
418    ++shader->in_prim_idx;
419    ++shader->fetched_prim_count;
420
421    gs_flush(shader);
422 }
423
424 static void gs_tri(struct draw_geometry_shader *shader,
425                    int i0, int i1, int i2)
426 {
427    unsigned indices[3];
428
429    indices[0] = i0;
430    indices[1] = i1;
431    indices[2] = i2;
432
433    shader->fetch_inputs(shader, indices, 3,
434                         shader->fetched_prim_count);
435    ++shader->in_prim_idx;
436    ++shader->fetched_prim_count;
437
438    gs_flush(shader);
439 }
440
441 static void gs_tri_adj(struct draw_geometry_shader *shader,
442                        int i0, int i1, int i2,
443                        int i3, int i4, int i5)
444 {
445    unsigned indices[6];
446
447    indices[0] = i0;
448    indices[1] = i1;
449    indices[2] = i2;
450    indices[3] = i3;
451    indices[4] = i4;
452    indices[5] = i5;
453
454    shader->fetch_inputs(shader, indices, 6,
455                         shader->fetched_prim_count);
456    ++shader->in_prim_idx;
457    ++shader->fetched_prim_count;
458
459    gs_flush(shader);
460 }
461
462 #define FUNC         gs_run
463 #define GET_ELT(idx) (idx)
464 #include "draw_gs_tmp.h"
465
466
467 #define FUNC         gs_run_elts
468 #define LOCAL_VARS   const ushort *elts = input_prims->elts;
469 #define GET_ELT(idx) (elts[idx])
470 #include "draw_gs_tmp.h"
471
472
473 /**
474  * Execute geometry shader.
475  */
476 int draw_geometry_shader_run(struct draw_geometry_shader *shader,
477                              const void *constants[PIPE_MAX_CONSTANT_BUFFERS],
478                              const unsigned constants_size[PIPE_MAX_CONSTANT_BUFFERS],
479                              const struct draw_vertex_info *input_verts,
480                              const struct draw_prim_info *input_prim,
481                              const struct tgsi_shader_info *input_info,
482                              struct draw_vertex_info *output_verts,
483                              struct draw_prim_info *output_prims )
484 {
485    const float (*input)[4] = (const float (*)[4])input_verts->verts->data;
486    unsigned input_stride = input_verts->vertex_size;
487    unsigned num_outputs = shader->info.num_outputs;
488    unsigned vertex_size = sizeof(struct vertex_header) + num_outputs * 4 * sizeof(float);
489    unsigned num_input_verts = input_prim->linear ?
490       input_verts->count :
491       input_prim->count;
492    unsigned num_in_primitives =
493       align(
494          MAX2(u_gs_prims_for_vertices(input_prim->prim, num_input_verts),
495               u_gs_prims_for_vertices(shader->input_primitive, num_input_verts)),
496          shader->vector_length);
497    unsigned max_out_prims = u_gs_prims_for_vertices(shader->output_primitive,
498                                                     shader->max_output_vertices)
499       * num_in_primitives;
500
501    //Assume at least one primitive
502    max_out_prims = MAX2(max_out_prims, 1);
503
504
505    output_verts->vertex_size = vertex_size;
506    output_verts->stride = output_verts->vertex_size;
507    output_verts->verts =
508       (struct vertex_header *)MALLOC(output_verts->vertex_size *
509                                      num_in_primitives *
510                                      shader->max_output_vertices);
511
512
513 #if 0
514    debug_printf("%s count = %d (in prims # = %d)\n",
515                 __FUNCTION__, num_input_verts, num_in_primitives);
516    debug_printf("\tlinear = %d, prim_info->count = %d\n",
517                 input_prim->linear, input_prim->count);
518    debug_printf("\tprim pipe = %s, shader in = %s, shader out = %s, max out = %d\n",
519                 u_prim_name(input_prim->prim),
520                 u_prim_name(shader->input_primitive),
521                 u_prim_name(shader->output_primitive),
522                 shader->max_output_vertices);
523 #endif
524
525    shader->emitted_vertices = 0;
526    shader->emitted_primitives = 0;
527    shader->vertex_size = vertex_size;
528    shader->tmp_output = (float (*)[4])output_verts->verts->data;
529    shader->in_prim_idx = 0;
530    shader->fetched_prim_count = 0;
531    shader->input_vertex_stride = input_stride;
532    shader->input = input;
533    shader->input_info = input_info;
534    FREE(shader->primitive_lengths);
535    shader->primitive_lengths = MALLOC(max_out_prims * sizeof(unsigned));
536
537
538 #ifdef HAVE_LLVM
539    if (draw_get_option_use_llvm()) {
540       shader->gs_output = output_verts->verts;
541       if (max_out_prims > shader->max_out_prims) {
542          unsigned i;
543          if (shader->llvm_prim_lengths) {
544             for (i = 0; i < shader->max_out_prims; ++i) {
545                align_free(shader->llvm_prim_lengths[i]);
546             }
547             FREE(shader->llvm_prim_lengths);
548          }
549
550          shader->llvm_prim_lengths = MALLOC(max_out_prims * sizeof(unsigned*));
551          for (i = 0; i < max_out_prims; ++i) {
552             int vector_size = shader->vector_length * sizeof(unsigned);
553             shader->llvm_prim_lengths[i] =
554                align_malloc(vector_size, vector_size);
555          }
556
557          shader->max_out_prims = max_out_prims;
558       }
559       shader->jit_context->prim_lengths = shader->llvm_prim_lengths;
560       shader->jit_context->emitted_vertices = shader->llvm_emitted_vertices;
561       shader->jit_context->emitted_prims = shader->llvm_emitted_primitives;
562    }
563 #endif
564
565    shader->prepare(shader, constants, constants_size);
566
567    if (input_prim->linear)
568       gs_run(shader, input_prim, input_verts,
569              output_prims, output_verts);
570    else
571       gs_run_elts(shader, input_prim, input_verts,
572                   output_prims, output_verts);
573
574    /* Flush the remaining primitives. Will happen if
575     * num_input_primitives % 4 != 0
576     */
577    if (shader->fetched_prim_count > 0) {
578       gs_flush(shader);
579    }
580
581    debug_assert(shader->fetched_prim_count == 0);
582
583    /* Update prim_info:
584     */
585    output_prims->linear = TRUE;
586    output_prims->elts = NULL;
587    output_prims->start = 0;
588    output_prims->count = shader->emitted_vertices;
589    output_prims->prim = shader->output_primitive;
590    output_prims->flags = 0x0;
591    output_prims->primitive_lengths = shader->primitive_lengths;
592    output_prims->primitive_count = shader->emitted_primitives;
593    output_verts->count = shader->emitted_vertices;
594
595 #if 0
596    debug_printf("GS finished, prims = %d, verts = %d\n",
597                 output_prims->primitive_count,
598                 output_verts->count);
599 #endif
600
601    return shader->emitted_vertices;
602 }
603
604 void draw_geometry_shader_prepare(struct draw_geometry_shader *shader,
605                                   struct draw_context *draw)
606 {
607    if (shader && shader->machine->Tokens != shader->state.tokens) {
608       tgsi_exec_machine_bind_shader(shader->machine,
609                                     shader->state.tokens,
610                                     draw->gs.tgsi.sampler);
611    }
612 }
613
614
615 boolean
616 draw_gs_init( struct draw_context *draw )
617 {
618    draw->gs.tgsi.machine = tgsi_exec_machine_create();
619    if (!draw->gs.tgsi.machine)
620       return FALSE;
621
622    draw->gs.tgsi.machine->Primitives = align_malloc(
623       MAX_PRIMITIVES * sizeof(struct tgsi_exec_vector), 16);
624    if (!draw->gs.tgsi.machine->Primitives)
625       return FALSE;
626    memset(draw->gs.tgsi.machine->Primitives, 0,
627           MAX_PRIMITIVES * sizeof(struct tgsi_exec_vector));
628
629    return TRUE;
630 }
631
632 void draw_gs_destroy( struct draw_context *draw )
633 {
634    if (draw->gs.tgsi.machine) {
635       align_free(draw->gs.tgsi.machine->Primitives);
636       tgsi_exec_machine_destroy(draw->gs.tgsi.machine);
637    }
638 }
639
640 struct draw_geometry_shader *
641 draw_create_geometry_shader(struct draw_context *draw,
642                             const struct pipe_shader_state *state)
643 {
644 #ifdef HAVE_LLVM
645    struct llvm_geometry_shader *llvm_gs;
646 #endif
647    struct draw_geometry_shader *gs;
648    unsigned i;
649
650 #ifdef HAVE_LLVM
651    if (draw_get_option_use_llvm()) {
652       llvm_gs = CALLOC_STRUCT(llvm_geometry_shader);
653
654       if (llvm_gs == NULL)
655          return NULL;
656
657       gs = &llvm_gs->base;
658
659       make_empty_list(&llvm_gs->variants);
660    } else
661 #endif
662    {
663       gs = CALLOC_STRUCT(draw_geometry_shader);
664    }
665
666    if (!gs)
667       return NULL;
668
669    gs->draw = draw;
670    gs->state = *state;
671    gs->state.tokens = tgsi_dup_tokens(state->tokens);
672    if (!gs->state.tokens) {
673       FREE(gs);
674       return NULL;
675    }
676
677    tgsi_scan_shader(state->tokens, &gs->info);
678
679    /* setup the defaults */
680    gs->input_primitive = PIPE_PRIM_TRIANGLES;
681    gs->output_primitive = PIPE_PRIM_TRIANGLE_STRIP;
682    gs->max_output_vertices = 32;
683    gs->max_out_prims = 0;
684
685    if (draw_get_option_use_llvm()) {
686       /* TODO: change the input array to handle the following
687          vector length, instead of the currently hardcoded
688          TGSI_NUM_CHANNELS
689       gs->vector_length = lp_native_vector_width / 32;*/
690       gs->vector_length = TGSI_NUM_CHANNELS;
691    } else {
692       gs->vector_length = TGSI_NUM_CHANNELS;
693    }
694
695    for (i = 0; i < gs->info.num_properties; ++i) {
696       if (gs->info.properties[i].name ==
697           TGSI_PROPERTY_GS_INPUT_PRIM)
698          gs->input_primitive = gs->info.properties[i].data[0];
699       else if (gs->info.properties[i].name ==
700                TGSI_PROPERTY_GS_OUTPUT_PRIM)
701          gs->output_primitive = gs->info.properties[i].data[0];
702       else if (gs->info.properties[i].name ==
703                TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES)
704          gs->max_output_vertices = gs->info.properties[i].data[0];
705    }
706
707    for (i = 0; i < gs->info.num_outputs; i++) {
708       if (gs->info.output_semantic_name[i] == TGSI_SEMANTIC_POSITION &&
709           gs->info.output_semantic_index[i] == 0)
710          gs->position_output = i;
711    }
712
713    gs->machine = draw->gs.tgsi.machine;
714
715 #ifdef HAVE_LLVM
716    if (draw_get_option_use_llvm()) {
717       int vector_size = gs->vector_length * sizeof(float);
718       gs->gs_input = align_malloc(sizeof(struct draw_gs_inputs), 16);
719       memset(gs->gs_input, 0, sizeof(struct draw_gs_inputs));
720       gs->llvm_prim_lengths = 0;
721
722       gs->llvm_emitted_primitives = align_malloc(vector_size, vector_size);
723       gs->llvm_emitted_vertices = align_malloc(vector_size, vector_size);
724
725       gs->fetch_outputs = llvm_fetch_gs_outputs;
726       gs->fetch_inputs = llvm_fetch_gs_input;
727       gs->prepare = llvm_gs_prepare;
728       gs->run = llvm_gs_run;
729
730       gs->jit_context = &draw->llvm->gs_jit_context;
731
732
733       llvm_gs->variant_key_size =
734          draw_gs_llvm_variant_key_size(
735             MAX2(gs->info.file_max[TGSI_FILE_SAMPLER]+1,
736                  gs->info.file_max[TGSI_FILE_SAMPLER_VIEW]+1));
737    } else
738 #endif
739    {
740       gs->fetch_outputs = tgsi_fetch_gs_outputs;
741       gs->fetch_inputs = tgsi_fetch_gs_input;
742       gs->prepare = tgsi_gs_prepare;
743       gs->run = tgsi_gs_run;
744    }
745
746    return gs;
747 }
748
749 void draw_bind_geometry_shader(struct draw_context *draw,
750                                struct draw_geometry_shader *dgs)
751 {
752    draw_do_flush(draw, DRAW_FLUSH_STATE_CHANGE);
753
754    if (dgs) {
755       draw->gs.geometry_shader = dgs;
756       draw->gs.num_gs_outputs = dgs->info.num_outputs;
757       draw->gs.position_output = dgs->position_output;
758       draw_geometry_shader_prepare(dgs, draw);
759    }
760    else {
761       draw->gs.geometry_shader = NULL;
762       draw->gs.num_gs_outputs = 0;
763    }
764 }
765
766 void draw_delete_geometry_shader(struct draw_context *draw,
767                                  struct draw_geometry_shader *dgs)
768 {
769 #ifdef HAVE_LLVM
770    if (draw_get_option_use_llvm()) {
771       struct llvm_geometry_shader *shader = llvm_geometry_shader(dgs);
772       struct draw_gs_llvm_variant_list_item *li;
773
774       li = first_elem(&shader->variants);
775       while(!at_end(&shader->variants, li)) {
776          struct draw_gs_llvm_variant_list_item *next = next_elem(li);
777          draw_gs_llvm_destroy_variant(li->base);
778          li = next;
779       }
780
781       assert(shader->variants_cached == 0);
782
783       if (dgs->llvm_prim_lengths) {
784          unsigned i;
785          for (i = 0; i < dgs->max_out_prims; ++i) {
786             align_free(dgs->llvm_prim_lengths[i]);
787          }
788          FREE(dgs->llvm_prim_lengths);
789       }
790       align_free(dgs->llvm_emitted_primitives);
791       align_free(dgs->llvm_emitted_vertices);
792
793       align_free(dgs->gs_input);
794    }
795 #endif
796
797    FREE(dgs->primitive_lengths);
798    FREE((void*) dgs->state.tokens);
799    FREE(dgs);
800 }
801
802
803 void draw_gs_set_current_variant(struct draw_geometry_shader *shader,
804                                  struct draw_gs_llvm_variant *variant)
805 {
806    shader->current_variant = variant;
807 }