• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**************************************************************************
2  *
3  * Copyright 2020 Red Hat.
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 "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  **************************************************************************/
25 #include "draw_tess.h"
26 #ifdef DRAW_LLVM_AVAILABLE
27 #include "draw_llvm.h"
28 #endif
29 
30 #include "tessellator/p_tessellator.h"
31 #include "nir/nir_to_tgsi_info.h"
32 #include "util/u_prim.h"
33 #include "util/u_math.h"
34 #include "util/u_memory.h"
35 #include "util/ralloc.h"
36 #ifdef DRAW_LLVM_AVAILABLE
37 static inline int
draw_tes_get_input_index(int semantic,int index,const struct tgsi_shader_info * input_info)38 draw_tes_get_input_index(int semantic, int index,
39                          const struct tgsi_shader_info *input_info)
40 {
41    int i;
42    const ubyte *input_semantic_names = input_info->output_semantic_name;
43    const ubyte *input_semantic_indices = input_info->output_semantic_index;
44    for (i = 0; i < PIPE_MAX_SHADER_OUTPUTS; i++) {
45       if (input_semantic_names[i] == semantic &&
46           input_semantic_indices[i] == index)
47          return i;
48    }
49    return -1;
50 }
51 
52 #define DEBUG_INPUTS 0
53 static void
llvm_fetch_tcs_input(struct draw_tess_ctrl_shader * shader,const struct draw_prim_info * input_prim_info,unsigned prim_id,unsigned num_vertices)54 llvm_fetch_tcs_input(struct draw_tess_ctrl_shader *shader,
55                      const struct draw_prim_info *input_prim_info,
56                      unsigned prim_id,
57                      unsigned num_vertices)
58 {
59    const float (*input_ptr)[4];
60    float (*input_data)[32][NUM_TCS_INPUTS][TGSI_NUM_CHANNELS] = &shader->tcs_input->data;
61    unsigned slot, i;
62    int vs_slot;
63    unsigned input_vertex_stride = shader->input_vertex_stride;
64 
65    input_ptr = shader->input;
66    for (i = 0; i < num_vertices; i++) {
67       const float (*input)[4];
68       int vertex_idx = prim_id * num_vertices + i;
69       if (input_prim_info->linear == FALSE)
70          vertex_idx = input_prim_info->elts[vertex_idx];
71 #if DEBUG_INPUTS
72       debug_printf("%d) tcs vertex index = %d (prim idx = %d)\n",
73                    i, prim_id, 0);
74 #endif
75       input = (const float (*)[4])((const char *)input_ptr + (vertex_idx * input_vertex_stride));
76       for (slot = 0, vs_slot = 0; slot < shader->info.num_inputs; ++slot) {
77          vs_slot = draw_tes_get_input_index(
78                                             shader->info.input_semantic_name[slot],
79                                             shader->info.input_semantic_index[slot],
80                                             shader->input_info);
81          if (vs_slot < 0) {
82             debug_printf("VS/TCS signature mismatch!\n");
83             (*input_data)[i][slot][0] = 0;
84             (*input_data)[i][slot][1] = 0;
85             (*input_data)[i][slot][2] = 0;
86             (*input_data)[i][slot][3] = 0;
87          } else {
88             (*input_data)[i][slot][0] = input[vs_slot][0];
89             (*input_data)[i][slot][1] = input[vs_slot][1];
90             (*input_data)[i][slot][2] = input[vs_slot][2];
91             (*input_data)[i][slot][3] = input[vs_slot][3];
92 #if DEBUG_INPUTS
93             debug_printf("\t\t%p = %f %f %f %f\n", &(*input_data)[i][slot][0],
94                          (*input_data)[i][slot][0],
95                          (*input_data)[i][slot][1],
96                          (*input_data)[i][slot][2],
97                          (*input_data)[i][slot][3]);
98 #endif
99             ++vs_slot;
100          }
101       }
102    }
103 }
104 
105 #define DEBUG_OUTPUTS 0
106 static void
llvm_store_tcs_output(struct draw_tess_ctrl_shader * shader,unsigned prim_id,struct draw_vertex_info * output_verts,unsigned vert_start)107 llvm_store_tcs_output(struct draw_tess_ctrl_shader *shader,
108                       unsigned prim_id,
109                       struct draw_vertex_info *output_verts,
110                       unsigned vert_start)
111 {
112    float (*output_ptr)[4];
113    float (*output_data)[32][PIPE_MAX_SHADER_INPUTS][TGSI_NUM_CHANNELS] = &shader->tcs_output->data;
114    unsigned slot, i;
115    unsigned num_vertices = shader->vertices_out;
116 
117    char *output = (char *)output_verts->verts->data;
118    output += vert_start * output_verts->stride;
119 
120    for (i = 0; i < num_vertices; i++) {
121 
122 #if DEBUG_OUTPUTS
123       debug_printf("%d) tcs store vertex index = %d (prim idx = %d)\n",
124                    i, prim_id, 0);
125 #endif
126       output_ptr = (float(*)[4])(output + (i * output_verts->stride));
127 
128       for (slot = 0; slot < shader->info.num_outputs; ++slot) {
129          output_ptr[slot][0] = (*output_data)[i][slot][0];
130          output_ptr[slot][1] = (*output_data)[i][slot][1];
131          output_ptr[slot][2] = (*output_data)[i][slot][2];
132          output_ptr[slot][3] = (*output_data)[i][slot][3];
133 #if DEBUG_OUTPUTS
134          debug_printf("\t\t%p = %f %f %f %f\n",
135                       &output_ptr[slot][0],
136                       output_ptr[slot][0],
137                       output_ptr[slot][1],
138                       output_ptr[slot][2],
139                       output_ptr[slot][3]);
140 #endif
141       }
142    }
143 }
144 
145 static void
llvm_tcs_run(struct draw_tess_ctrl_shader * shader,uint32_t prim_id)146 llvm_tcs_run(struct draw_tess_ctrl_shader *shader, uint32_t prim_id)
147 {
148    shader->current_variant->jit_func(shader->jit_context, shader->tcs_input->data, shader->tcs_output->data, prim_id,
149                                      shader->draw->pt.vertices_per_patch, shader->draw->pt.user.viewid);
150 }
151 #endif
152 
153 /**
154  * Execute tess ctrl shader.
155  */
draw_tess_ctrl_shader_run(struct draw_tess_ctrl_shader * shader,const void * constants[PIPE_MAX_CONSTANT_BUFFERS],const unsigned constants_size[PIPE_MAX_CONSTANT_BUFFERS],const struct draw_vertex_info * input_verts,const struct draw_prim_info * input_prim,const struct tgsi_shader_info * input_info,struct draw_vertex_info * output_verts,struct draw_prim_info * output_prims)156 int draw_tess_ctrl_shader_run(struct draw_tess_ctrl_shader *shader,
157                               const void *constants[PIPE_MAX_CONSTANT_BUFFERS],
158                               const unsigned constants_size[PIPE_MAX_CONSTANT_BUFFERS],
159                               const struct draw_vertex_info *input_verts,
160                               const struct draw_prim_info *input_prim,
161                               const struct tgsi_shader_info *input_info,
162                               struct draw_vertex_info *output_verts,
163                               struct draw_prim_info *output_prims )
164 {
165    const float (*input)[4] = (const float (*)[4])input_verts->verts->data;
166    unsigned num_outputs = draw_total_tcs_outputs(shader->draw);
167    unsigned input_stride = input_verts->vertex_size;
168    unsigned vertex_size = sizeof(struct vertex_header) + num_outputs * 4 * sizeof(float);
169    unsigned num_patches = input_prim->count / shader->draw->pt.vertices_per_patch;
170 
171    output_verts->vertex_size = vertex_size;
172    output_verts->stride = output_verts->vertex_size;
173    output_verts->verts = NULL;
174    output_verts->count = 0;
175    shader->input = input;
176    shader->input_vertex_stride = input_stride;
177    shader->input_info = input_info;
178 
179    output_prims->linear = TRUE;
180    output_prims->start = 0;
181    output_prims->elts = NULL;
182    output_prims->count = 0;
183    output_prims->prim = PIPE_PRIM_PATCHES;
184    output_prims->flags = 0;
185    output_prims->primitive_lengths = NULL;
186    output_prims->primitive_count = 0;
187 
188    if (shader->draw->collect_statistics) {
189       shader->draw->statistics.hs_invocations += num_patches;
190    }
191 #ifdef DRAW_LLVM_AVAILABLE
192    for (unsigned i = 0; i < num_patches; i++) {
193       uint32_t vert_start = output_verts->count;
194 
195       output_verts->count += shader->vertices_out;
196 
197       llvm_fetch_tcs_input(shader, input_prim, i, shader->draw->pt.vertices_per_patch);
198 
199       llvm_tcs_run(shader, i);
200 
201       uint32_t old_verts = util_align_npot(vert_start, 16);
202       uint32_t new_verts = util_align_npot(output_verts->count, 16);
203       uint32_t old_size = output_verts->vertex_size * old_verts;
204       uint32_t new_size = output_verts->vertex_size * new_verts;
205       output_verts->verts = REALLOC(output_verts->verts, old_size, new_size);
206 
207       llvm_store_tcs_output(shader, i, output_verts, vert_start);
208    }
209 #endif
210 
211    output_prims->primitive_count = num_patches;
212    return 0;
213 }
214 
215 #ifdef DRAW_LLVM_AVAILABLE
216 #define DEBUG_INPUTS 0
217 static void
llvm_fetch_tes_input(struct draw_tess_eval_shader * shader,const struct draw_prim_info * input_prim_info,unsigned prim_id,unsigned num_vertices)218 llvm_fetch_tes_input(struct draw_tess_eval_shader *shader,
219                      const struct draw_prim_info *input_prim_info,
220                      unsigned prim_id,
221                      unsigned num_vertices)
222 {
223    const float (*input_ptr)[4];
224    float (*input_data)[32][PIPE_MAX_SHADER_INPUTS][TGSI_NUM_CHANNELS] = &shader->tes_input->data;
225    unsigned slot, i;
226    int vs_slot;
227    unsigned input_vertex_stride = shader->input_vertex_stride;
228 
229    input_ptr = shader->input;
230    for (i = 0; i < num_vertices; i++) {
231       const float (*input)[4];
232       int vertex_idx = prim_id * num_vertices + i;
233 
234       if (input_prim_info->linear == FALSE)
235          vertex_idx = input_prim_info->elts[vertex_idx];
236 #if DEBUG_INPUTS
237       debug_printf("%d) tes vertex index = %d (prim idx = %d)\n",
238                    i, prim_id, 0);
239 #endif
240       input = (const float (*)[4])((const char *)input_ptr + (vertex_idx * input_vertex_stride));
241       for (slot = 0, vs_slot = 0; slot < shader->info.num_inputs; ++slot) {
242          vs_slot = draw_tes_get_input_index(
243                                             shader->info.input_semantic_name[slot],
244                                             shader->info.input_semantic_index[slot],
245                                             shader->input_info);
246          if (vs_slot < 0) {
247             debug_printf("TCS/TES signature mismatch!\n");
248             (*input_data)[i][slot][0] = 0;
249             (*input_data)[i][slot][1] = 0;
250             (*input_data)[i][slot][2] = 0;
251             (*input_data)[i][slot][3] = 0;
252          } else {
253             (*input_data)[i][slot][0] = input[vs_slot][0];
254             (*input_data)[i][slot][1] = input[vs_slot][1];
255             (*input_data)[i][slot][2] = input[vs_slot][2];
256             (*input_data)[i][slot][3] = input[vs_slot][3];
257 #if DEBUG_INPUTS
258             debug_printf("\t\t%p = %f %f %f %f\n",
259                          &input[vs_slot][0],
260                          (*input_data)[i][slot][0],
261                          (*input_data)[i][slot][1],
262                          (*input_data)[i][slot][2],
263                          (*input_data)[i][slot][3]);
264 #endif
265             ++vs_slot;
266          }
267       }
268    }
269 }
270 
271 static void
llvm_fetch_tess_factors(struct draw_tess_eval_shader * shader,unsigned patch_id,unsigned num_vertices,struct pipe_tessellation_factors * factors)272 llvm_fetch_tess_factors(struct draw_tess_eval_shader *shader,
273                         unsigned patch_id,
274                         unsigned num_vertices,
275                         struct pipe_tessellation_factors *factors)
276 {
277    int outer_slot = draw_tes_get_input_index(
278       TGSI_SEMANTIC_TESSOUTER, 0, shader->input_info);
279    int inner_slot = draw_tes_get_input_index(
280       TGSI_SEMANTIC_TESSINNER, 0, shader->input_info);
281    const float (*input_ptr)[4];
282    const float (*input)[4];
283    input_ptr = shader->input;
284    input = (const float (*)[4])((const char *)input_ptr + ((patch_id * num_vertices) * shader->input_vertex_stride));
285 
286    if (outer_slot != -1) {
287       for (unsigned i = 0; i < 4; i++)
288          factors->outer_tf[i] = input[outer_slot][i];
289    } else {
290       for (unsigned i = 0; i < 4; i++)
291          factors->outer_tf[i] = shader->draw->default_outer_tess_level[i];
292    }
293    if (inner_slot != -1) {
294       for (unsigned i = 0; i < 2; i++)
295          factors->inner_tf[i] = input[inner_slot][i];
296    } else {
297       for (unsigned i = 0; i < 2; i++)
298          factors->inner_tf[i] = shader->draw->default_inner_tess_level[i];
299    }
300 }
301 
302 static void
llvm_tes_run(struct draw_tess_eval_shader * shader,uint32_t prim_id,uint32_t patch_vertices_in,struct pipe_tessellator_data * tess_data,struct pipe_tessellation_factors * tess_factors,struct vertex_header * output)303 llvm_tes_run(struct draw_tess_eval_shader *shader,
304              uint32_t prim_id,
305              uint32_t patch_vertices_in,
306              struct pipe_tessellator_data *tess_data,
307              struct pipe_tessellation_factors *tess_factors,
308              struct vertex_header *output)
309 {
310    shader->current_variant->jit_func(shader->jit_context, shader->tes_input->data, output, prim_id,
311                                      tess_data->num_domain_points, tess_data->domain_points_u, tess_data->domain_points_v,
312                                      tess_factors->outer_tf, tess_factors->inner_tf, patch_vertices_in,
313                                      shader->draw->pt.user.viewid);
314 }
315 #endif
316 
317 /**
318  * Execute tess eval shader.
319  */
draw_tess_eval_shader_run(struct draw_tess_eval_shader * shader,const void * constants[PIPE_MAX_CONSTANT_BUFFERS],const unsigned constants_size[PIPE_MAX_CONSTANT_BUFFERS],unsigned num_input_vertices_per_patch,const struct draw_vertex_info * input_verts,const struct draw_prim_info * input_prim,const struct tgsi_shader_info * input_info,struct draw_vertex_info * output_verts,struct draw_prim_info * output_prims,ushort ** elts_out)320 int draw_tess_eval_shader_run(struct draw_tess_eval_shader *shader,
321                               const void *constants[PIPE_MAX_CONSTANT_BUFFERS],
322                               const unsigned constants_size[PIPE_MAX_CONSTANT_BUFFERS],
323                               unsigned num_input_vertices_per_patch,
324                               const struct draw_vertex_info *input_verts,
325                               const struct draw_prim_info *input_prim,
326                               const struct tgsi_shader_info *input_info,
327                               struct draw_vertex_info *output_verts,
328                               struct draw_prim_info *output_prims,
329                               ushort **elts_out)
330 {
331    const float (*input)[4] = (const float (*)[4])input_verts->verts->data;
332    unsigned num_outputs = draw_total_tes_outputs(shader->draw);
333    unsigned input_stride = input_verts->vertex_size;
334    unsigned vertex_size = sizeof(struct vertex_header) + num_outputs * 4 * sizeof(float);
335    ushort *elts = NULL;
336    output_verts->vertex_size = vertex_size;
337    output_verts->stride = output_verts->vertex_size;
338    output_verts->count = 0;
339    output_verts->verts = NULL;
340 
341    output_prims->linear = FALSE;
342    output_prims->start = 0;
343    output_prims->elts = NULL;
344    output_prims->count = 0;
345    output_prims->prim = get_tes_output_prim(shader);
346    output_prims->flags = 0;
347    output_prims->primitive_lengths = NULL;
348    output_prims->primitive_count = 0;
349 
350    shader->input = input;
351    shader->input_vertex_stride = input_stride;
352    shader->input_info = input_info;
353 
354 #ifdef DRAW_LLVM_AVAILABLE
355    struct pipe_tessellation_factors factors;
356    struct pipe_tessellator_data data = { 0 };
357    struct pipe_tessellator *ptess = p_tess_init(shader->prim_mode,
358                                                 shader->spacing,
359                                                 !shader->vertex_order_cw,
360                                                 shader->point_mode);
361    for (unsigned i = 0; i < input_prim->primitive_count; i++) {
362       uint32_t vert_start = output_verts->count;
363       uint32_t prim_start = output_prims->primitive_count;
364       uint32_t elt_start = output_prims->count;
365 
366       llvm_fetch_tess_factors(shader, i, num_input_vertices_per_patch, &factors);
367 
368       /* tessellate with the factors for this primitive */
369       p_tessellate(ptess, &factors, &data);
370 
371       if (data.num_domain_points == 0)
372          continue;
373 
374       uint32_t old_verts = vert_start;
375       uint32_t new_verts = vert_start + util_align_npot(data.num_domain_points, 4);
376       uint32_t old_size = output_verts->vertex_size * old_verts;
377       uint32_t new_size = output_verts->vertex_size * new_verts;
378       output_verts->verts = REALLOC(output_verts->verts, old_size, new_size);
379 
380       output_verts->count += data.num_domain_points;
381 
382       output_prims->count += data.num_indices;
383       elts = REALLOC(elts, elt_start * sizeof(uint16_t),
384                      output_prims->count * sizeof(uint16_t));
385 
386       for (unsigned i = 0; i < data.num_indices; i++)
387          elts[elt_start + i] = vert_start + data.indices[i];
388 
389       llvm_fetch_tes_input(shader, input_prim, i, num_input_vertices_per_patch);
390       /* run once per primitive? */
391       char *output = (char *)output_verts->verts;
392       output += vert_start * vertex_size;
393       llvm_tes_run(shader, i, num_input_vertices_per_patch, &data, &factors, (struct vertex_header *)output);
394 
395       if (shader->draw->collect_statistics) {
396          shader->draw->statistics.ds_invocations += data.num_domain_points;
397       }
398 
399       uint32_t prim_len = u_prim_vertex_count(output_prims->prim)->min;
400       output_prims->primitive_count += data.num_indices / prim_len;
401       output_prims->primitive_lengths = REALLOC(output_prims->primitive_lengths, prim_start * sizeof(uint32_t),
402                                                 output_prims->primitive_count * sizeof(uint32_t));
403       for (unsigned i = prim_start; i < output_prims->primitive_count; i++) {
404          output_prims->primitive_lengths[i] = prim_len;
405       }
406    }
407    p_tess_destroy(ptess);
408 #endif
409 
410    *elts_out = elts;
411    output_prims->elts = elts;
412    return 0;
413 }
414 
415 struct draw_tess_ctrl_shader *
draw_create_tess_ctrl_shader(struct draw_context * draw,const struct pipe_shader_state * state)416 draw_create_tess_ctrl_shader(struct draw_context *draw,
417                              const struct pipe_shader_state *state)
418 {
419 #ifdef DRAW_LLVM_AVAILABLE
420    boolean use_llvm = draw->llvm != NULL;
421    struct llvm_tess_ctrl_shader *llvm_tcs = NULL;
422 #endif
423    struct draw_tess_ctrl_shader *tcs;
424 
425 #ifdef DRAW_LLVM_AVAILABLE
426    if (use_llvm) {
427       llvm_tcs = CALLOC_STRUCT(llvm_tess_ctrl_shader);
428 
429       if (!llvm_tcs)
430          return NULL;
431 
432       tcs = &llvm_tcs->base;
433 
434       make_empty_list(&llvm_tcs->variants);
435    } else
436 #endif
437    {
438       tcs = CALLOC_STRUCT(draw_tess_ctrl_shader);
439    }
440 
441    if (!tcs)
442       return NULL;
443 
444    tcs->draw = draw;
445    tcs->state = *state;
446 
447    nir_tgsi_scan_shader(state->ir.nir, &tcs->info, true);
448 
449    tcs->vector_length = 4;
450    tcs->vertices_out = tcs->info.properties[TGSI_PROPERTY_TCS_VERTICES_OUT];
451 #ifdef DRAW_LLVM_AVAILABLE
452    if (use_llvm) {
453 
454       tcs->tcs_input = align_malloc(sizeof(struct draw_tcs_inputs), 16);
455       memset(tcs->tcs_input, 0, sizeof(struct draw_tcs_inputs));
456 
457       tcs->tcs_output = align_malloc(sizeof(struct draw_tcs_outputs), 16);
458       memset(tcs->tcs_output, 0, sizeof(struct draw_tcs_outputs));
459 
460       tcs->jit_context = &draw->llvm->tcs_jit_context;
461       llvm_tcs->variant_key_size =
462          draw_tcs_llvm_variant_key_size(
463                                         MAX2(tcs->info.file_max[TGSI_FILE_SAMPLER]+1,
464                                              tcs->info.file_max[TGSI_FILE_SAMPLER_VIEW]+1),
465                                         tcs->info.file_max[TGSI_FILE_IMAGE]+1);
466    }
467 #endif
468    return tcs;
469 }
470 
draw_bind_tess_ctrl_shader(struct draw_context * draw,struct draw_tess_ctrl_shader * dtcs)471 void draw_bind_tess_ctrl_shader(struct draw_context *draw,
472                                 struct draw_tess_ctrl_shader *dtcs)
473 {
474    draw_do_flush(draw, DRAW_FLUSH_STATE_CHANGE);
475    if (dtcs) {
476       draw->tcs.tess_ctrl_shader = dtcs;
477    } else {
478       draw->tcs.tess_ctrl_shader = NULL;
479    }
480 }
481 
draw_delete_tess_ctrl_shader(struct draw_context * draw,struct draw_tess_ctrl_shader * dtcs)482 void draw_delete_tess_ctrl_shader(struct draw_context *draw,
483                                   struct draw_tess_ctrl_shader *dtcs)
484 {
485    if (!dtcs)
486       return;
487 
488 #ifdef DRAW_LLVM_AVAILABLE
489    if (draw->llvm) {
490       struct llvm_tess_ctrl_shader *shader = llvm_tess_ctrl_shader(dtcs);
491 
492       struct draw_tcs_llvm_variant_list_item *li;
493 
494       li = first_elem(&shader->variants);
495       while(!at_end(&shader->variants, li)) {
496          struct draw_tcs_llvm_variant_list_item *next = next_elem(li);
497          draw_tcs_llvm_destroy_variant(li->base);
498          li = next;
499       }
500 
501       assert(shader->variants_cached == 0);
502       align_free(dtcs->tcs_input);
503       align_free(dtcs->tcs_output);
504    }
505 #endif
506 
507    if (dtcs->state.ir.nir)
508       ralloc_free(dtcs->state.ir.nir);
509    FREE(dtcs);
510 }
511 
512 #ifdef DRAW_LLVM_AVAILABLE
draw_tcs_set_current_variant(struct draw_tess_ctrl_shader * shader,struct draw_tcs_llvm_variant * variant)513 void draw_tcs_set_current_variant(struct draw_tess_ctrl_shader *shader,
514                                   struct draw_tcs_llvm_variant *variant)
515 {
516    shader->current_variant = variant;
517 }
518 #endif
519 
520 struct draw_tess_eval_shader *
draw_create_tess_eval_shader(struct draw_context * draw,const struct pipe_shader_state * state)521 draw_create_tess_eval_shader(struct draw_context *draw,
522                              const struct pipe_shader_state *state)
523 {
524 #ifdef DRAW_LLVM_AVAILABLE
525    boolean use_llvm = draw->llvm != NULL;
526    struct llvm_tess_eval_shader *llvm_tes = NULL;
527 #endif
528    struct draw_tess_eval_shader *tes;
529 
530 #ifdef DRAW_LLVM_AVAILABLE
531    if (use_llvm) {
532       llvm_tes = CALLOC_STRUCT(llvm_tess_eval_shader);
533 
534       if (!llvm_tes)
535          return NULL;
536 
537       tes = &llvm_tes->base;
538       make_empty_list(&llvm_tes->variants);
539    } else
540 #endif
541    {
542       tes = CALLOC_STRUCT(draw_tess_eval_shader);
543    }
544 
545    if (!tes)
546       return NULL;
547 
548    tes->draw = draw;
549    tes->state = *state;
550 
551    nir_tgsi_scan_shader(state->ir.nir, &tes->info, true);
552 
553    tes->prim_mode = tes->info.properties[TGSI_PROPERTY_TES_PRIM_MODE];
554    tes->spacing = tes->info.properties[TGSI_PROPERTY_TES_SPACING];
555    tes->vertex_order_cw = tes->info.properties[TGSI_PROPERTY_TES_VERTEX_ORDER_CW];
556    tes->point_mode = tes->info.properties[TGSI_PROPERTY_TES_POINT_MODE];
557 
558    tes->vector_length = 4;
559 
560    tes->position_output = -1;
561    bool found_clipvertex = false;
562    for (unsigned i = 0; i < tes->info.num_outputs; i++) {
563       if (tes->info.output_semantic_name[i] == TGSI_SEMANTIC_POSITION &&
564           tes->info.output_semantic_index[i] == 0)
565          tes->position_output = i;
566       if (tes->info.output_semantic_name[i] == TGSI_SEMANTIC_VIEWPORT_INDEX)
567          tes->viewport_index_output = i;
568       if (tes->info.output_semantic_name[i] == TGSI_SEMANTIC_CLIPVERTEX &&
569           tes->info.output_semantic_index[i] == 0) {
570          found_clipvertex = true;
571          tes->clipvertex_output = i;
572       }
573       if (tes->info.output_semantic_name[i] == TGSI_SEMANTIC_CLIPDIST) {
574          debug_assert(tes->info.output_semantic_index[i] <
575                       PIPE_MAX_CLIP_OR_CULL_DISTANCE_ELEMENT_COUNT);
576          tes->ccdistance_output[tes->info.output_semantic_index[i]] = i;
577       }
578    }
579    if (!found_clipvertex)
580       tes->clipvertex_output = tes->position_output;
581 
582 #ifdef DRAW_LLVM_AVAILABLE
583    if (use_llvm) {
584 
585       tes->tes_input = align_malloc(sizeof(struct draw_tes_inputs), 16);
586       memset(tes->tes_input, 0, sizeof(struct draw_tes_inputs));
587 
588       tes->jit_context = &draw->llvm->tes_jit_context;
589       llvm_tes->variant_key_size =
590          draw_tes_llvm_variant_key_size(
591                                         MAX2(tes->info.file_max[TGSI_FILE_SAMPLER]+1,
592                                              tes->info.file_max[TGSI_FILE_SAMPLER_VIEW]+1),
593                                         tes->info.file_max[TGSI_FILE_IMAGE]+1);
594    }
595 #endif
596    return tes;
597 }
598 
draw_bind_tess_eval_shader(struct draw_context * draw,struct draw_tess_eval_shader * dtes)599 void draw_bind_tess_eval_shader(struct draw_context *draw,
600                                 struct draw_tess_eval_shader *dtes)
601 {
602    draw_do_flush(draw, DRAW_FLUSH_STATE_CHANGE);
603    if (dtes) {
604       draw->tes.tess_eval_shader = dtes;
605       draw->tes.position_output = dtes->position_output;
606       draw->tes.clipvertex_output = dtes->clipvertex_output;
607    } else {
608       draw->tes.tess_eval_shader = NULL;
609    }
610 }
611 
draw_delete_tess_eval_shader(struct draw_context * draw,struct draw_tess_eval_shader * dtes)612 void draw_delete_tess_eval_shader(struct draw_context *draw,
613                                   struct draw_tess_eval_shader *dtes)
614 {
615    if (!dtes)
616       return;
617 
618 #ifdef DRAW_LLVM_AVAILABLE
619    if (draw->llvm) {
620       struct llvm_tess_eval_shader *shader = llvm_tess_eval_shader(dtes);
621       struct draw_tes_llvm_variant_list_item *li;
622 
623       li = first_elem(&shader->variants);
624       while(!at_end(&shader->variants, li)) {
625          struct draw_tes_llvm_variant_list_item *next = next_elem(li);
626          draw_tes_llvm_destroy_variant(li->base);
627          li = next;
628       }
629 
630       assert(shader->variants_cached == 0);
631       align_free(dtes->tes_input);
632    }
633 #endif
634    if (dtes->state.ir.nir)
635       ralloc_free(dtes->state.ir.nir);
636    FREE(dtes);
637 }
638 
639 #ifdef DRAW_LLVM_AVAILABLE
draw_tes_set_current_variant(struct draw_tess_eval_shader * shader,struct draw_tes_llvm_variant * variant)640 void draw_tes_set_current_variant(struct draw_tess_eval_shader *shader,
641                                   struct draw_tes_llvm_variant *variant)
642 {
643    shader->current_variant = variant;
644 }
645 #endif
646 
get_tes_output_prim(struct draw_tess_eval_shader * shader)647 enum pipe_prim_type get_tes_output_prim(struct draw_tess_eval_shader *shader)
648 {
649    if (shader->point_mode)
650       return PIPE_PRIM_POINTS;
651    else if (shader->prim_mode == PIPE_PRIM_LINES)
652       return PIPE_PRIM_LINES;
653    else
654       return PIPE_PRIM_TRIANGLES;
655 }
656