• 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 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 static inline int
draw_tes_get_input_index(int semantic,int index,const struct tgsi_shader_info * input_info)37 draw_tes_get_input_index(int semantic, int index,
38                          const struct tgsi_shader_info *input_info)
39 {
40    int i;
41    const ubyte *input_semantic_names = input_info->output_semantic_name;
42    const ubyte *input_semantic_indices = input_info->output_semantic_index;
43    for (i = 0; i < PIPE_MAX_SHADER_OUTPUTS; i++) {
44       if (input_semantic_names[i] == semantic &&
45           input_semantic_indices[i] == index)
46          return i;
47    }
48    return -1;
49 }
50 
51 #ifdef LLVM_AVAILABLE
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);
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 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 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 }
314 #endif
315 
316 /**
317  * Execute tess eval shader.
318  */
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)319 int draw_tess_eval_shader_run(struct draw_tess_eval_shader *shader,
320                               const void *constants[PIPE_MAX_CONSTANT_BUFFERS],
321                               const unsigned constants_size[PIPE_MAX_CONSTANT_BUFFERS],
322                               unsigned num_input_vertices_per_patch,
323                               const struct draw_vertex_info *input_verts,
324                               const struct draw_prim_info *input_prim,
325                               const struct tgsi_shader_info *input_info,
326                               struct draw_vertex_info *output_verts,
327                               struct draw_prim_info *output_prims,
328                               ushort **elts_out)
329 {
330    const float (*input)[4] = (const float (*)[4])input_verts->verts->data;
331    unsigned num_outputs = draw_total_tes_outputs(shader->draw);
332    unsigned input_stride = input_verts->vertex_size;
333    unsigned vertex_size = sizeof(struct vertex_header) + num_outputs * 4 * sizeof(float);
334    ushort *elts = NULL;
335    output_verts->vertex_size = vertex_size;
336    output_verts->stride = output_verts->vertex_size;
337    output_verts->count = 0;
338    output_verts->verts = NULL;
339 
340    output_prims->linear = FALSE;
341    output_prims->start = 0;
342    output_prims->elts = NULL;
343    output_prims->count = 0;
344    output_prims->prim = get_tes_output_prim(shader);
345    output_prims->flags = 0;
346    output_prims->primitive_lengths = NULL;
347    output_prims->primitive_count = 0;
348 
349    shader->input = input;
350    shader->input_vertex_stride = input_stride;
351    shader->input_info = input_info;
352 
353 #ifdef LLVM_AVAILABLE
354    struct pipe_tessellation_factors factors;
355    struct pipe_tessellator_data data = { 0 };
356    struct pipe_tessellator *ptess = p_tess_init(shader->prim_mode,
357                                                 shader->spacing,
358                                                 !shader->vertex_order_cw,
359                                                 shader->point_mode);
360    for (unsigned i = 0; i < input_prim->primitive_count; i++) {
361       uint32_t vert_start = output_verts->count;
362       uint32_t prim_start = output_prims->primitive_count;
363       uint32_t elt_start = output_prims->count;
364 
365       llvm_fetch_tess_factors(shader, i, num_input_vertices_per_patch, &factors);
366 
367       /* tessellate with the factors for this primitive */
368       p_tessellate(ptess, &factors, &data);
369 
370       if (data.num_domain_points == 0)
371          continue;
372 
373       uint32_t old_verts = vert_start;
374       uint32_t new_verts = vert_start + util_align_npot(data.num_domain_points, 4);
375       uint32_t old_size = output_verts->vertex_size * old_verts;
376       uint32_t new_size = output_verts->vertex_size * new_verts;
377       output_verts->verts = REALLOC(output_verts->verts, old_size, new_size);
378 
379       output_verts->count += data.num_domain_points;
380 
381       output_prims->count += data.num_indices;
382       elts = REALLOC(elts, elt_start * sizeof(uint16_t),
383                      output_prims->count * sizeof(uint16_t));
384 
385       for (unsigned i = 0; i < data.num_indices; i++)
386          elts[elt_start + i] = vert_start + data.indices[i];
387 
388       llvm_fetch_tes_input(shader, input_prim, i, num_input_vertices_per_patch);
389       /* run once per primitive? */
390       char *output = (char *)output_verts->verts;
391       output += vert_start * vertex_size;
392       llvm_tes_run(shader, i, num_input_vertices_per_patch, &data, &factors, (struct vertex_header *)output);
393 
394       if (shader->draw->collect_statistics) {
395          shader->draw->statistics.ds_invocations += data.num_domain_points;
396       }
397 
398       uint32_t prim_len = u_prim_vertex_count(output_prims->prim)->min;
399       output_prims->primitive_count += data.num_indices / prim_len;
400       output_prims->primitive_lengths = REALLOC(output_prims->primitive_lengths, prim_start * sizeof(uint32_t),
401                                                 output_prims->primitive_count * sizeof(uint32_t));
402       for (unsigned i = prim_start; i < output_prims->primitive_count; i++) {
403          output_prims->primitive_lengths[i] = prim_len;
404       }
405    }
406    p_tess_destroy(ptess);
407 #endif
408 
409    *elts_out = elts;
410    output_prims->elts = elts;
411    return 0;
412 }
413 
414 struct draw_tess_ctrl_shader *
draw_create_tess_ctrl_shader(struct draw_context * draw,const struct pipe_shader_state * state)415 draw_create_tess_ctrl_shader(struct draw_context *draw,
416                              const struct pipe_shader_state *state)
417 {
418 #ifdef LLVM_AVAILABLE
419    boolean use_llvm = draw->llvm != NULL;
420    struct llvm_tess_ctrl_shader *llvm_tcs = NULL;
421 #endif
422    struct draw_tess_ctrl_shader *tcs;
423 
424 #ifdef LLVM_AVAILABLE
425    if (use_llvm) {
426       llvm_tcs = CALLOC_STRUCT(llvm_tess_ctrl_shader);
427 
428       if (!llvm_tcs)
429          return NULL;
430 
431       tcs = &llvm_tcs->base;
432 
433       make_empty_list(&llvm_tcs->variants);
434    } else
435 #endif
436    {
437       tcs = CALLOC_STRUCT(draw_tess_ctrl_shader);
438    }
439 
440    if (!tcs)
441       return NULL;
442 
443    tcs->draw = draw;
444    tcs->state = *state;
445 
446    nir_tgsi_scan_shader(state->ir.nir, &tcs->info, true);
447 
448    tcs->vector_length = 4;
449    tcs->vertices_out = tcs->info.properties[TGSI_PROPERTY_TCS_VERTICES_OUT];
450 #ifdef LLVM_AVAILABLE
451    if (use_llvm) {
452 
453       tcs->tcs_input = align_malloc(sizeof(struct draw_tcs_inputs), 16);
454       memset(tcs->tcs_input, 0, sizeof(struct draw_tcs_inputs));
455 
456       tcs->tcs_output = align_malloc(sizeof(struct draw_tcs_outputs), 16);
457       memset(tcs->tcs_output, 0, sizeof(struct draw_tcs_outputs));
458 
459       tcs->jit_context = &draw->llvm->tcs_jit_context;
460       llvm_tcs->variant_key_size =
461          draw_tcs_llvm_variant_key_size(
462                                         MAX2(tcs->info.file_max[TGSI_FILE_SAMPLER]+1,
463                                              tcs->info.file_max[TGSI_FILE_SAMPLER_VIEW]+1),
464                                         tcs->info.file_max[TGSI_FILE_IMAGE]+1);
465    }
466 #endif
467    return tcs;
468 }
469 
draw_bind_tess_ctrl_shader(struct draw_context * draw,struct draw_tess_ctrl_shader * dtcs)470 void draw_bind_tess_ctrl_shader(struct draw_context *draw,
471                                 struct draw_tess_ctrl_shader *dtcs)
472 {
473    draw_do_flush(draw, DRAW_FLUSH_STATE_CHANGE);
474    if (dtcs) {
475       draw->tcs.tess_ctrl_shader = dtcs;
476    } else {
477       draw->tcs.tess_ctrl_shader = NULL;
478    }
479 }
480 
draw_delete_tess_ctrl_shader(struct draw_context * draw,struct draw_tess_ctrl_shader * dtcs)481 void draw_delete_tess_ctrl_shader(struct draw_context *draw,
482                                   struct draw_tess_ctrl_shader *dtcs)
483 {
484    if (!dtcs)
485       return;
486 
487 #ifdef LLVM_AVAILABLE
488    if (draw->llvm) {
489       struct llvm_tess_ctrl_shader *shader = llvm_tess_ctrl_shader(dtcs);
490 
491       struct draw_tcs_llvm_variant_list_item *li;
492 
493       li = first_elem(&shader->variants);
494       while(!at_end(&shader->variants, li)) {
495          struct draw_tcs_llvm_variant_list_item *next = next_elem(li);
496          draw_tcs_llvm_destroy_variant(li->base);
497          li = next;
498       }
499 
500       assert(shader->variants_cached == 0);
501       align_free(dtcs->tcs_input);
502       align_free(dtcs->tcs_output);
503    }
504 #endif
505 
506    if (dtcs->state.ir.nir)
507       ralloc_free(dtcs->state.ir.nir);
508    FREE(dtcs);
509 }
510 
511 #ifdef LLVM_AVAILABLE
draw_tcs_set_current_variant(struct draw_tess_ctrl_shader * shader,struct draw_tcs_llvm_variant * variant)512 void draw_tcs_set_current_variant(struct draw_tess_ctrl_shader *shader,
513                                   struct draw_tcs_llvm_variant *variant)
514 {
515    shader->current_variant = variant;
516 }
517 #endif
518 
519 struct draw_tess_eval_shader *
draw_create_tess_eval_shader(struct draw_context * draw,const struct pipe_shader_state * state)520 draw_create_tess_eval_shader(struct draw_context *draw,
521                              const struct pipe_shader_state *state)
522 {
523 #ifdef LLVM_AVAILABLE
524    boolean use_llvm = draw->llvm != NULL;
525    struct llvm_tess_eval_shader *llvm_tes = NULL;
526 #endif
527    struct draw_tess_eval_shader *tes;
528 
529 #ifdef LLVM_AVAILABLE
530    if (use_llvm) {
531       llvm_tes = CALLOC_STRUCT(llvm_tess_eval_shader);
532 
533       if (!llvm_tes)
534          return NULL;
535 
536       tes = &llvm_tes->base;
537       make_empty_list(&llvm_tes->variants);
538    } else
539 #endif
540    {
541       tes = CALLOC_STRUCT(draw_tess_eval_shader);
542    }
543 
544    if (!tes)
545       return NULL;
546 
547    tes->draw = draw;
548    tes->state = *state;
549 
550    nir_tgsi_scan_shader(state->ir.nir, &tes->info, true);
551 
552    tes->prim_mode = tes->info.properties[TGSI_PROPERTY_TES_PRIM_MODE];
553    tes->spacing = tes->info.properties[TGSI_PROPERTY_TES_SPACING];
554    tes->vertex_order_cw = tes->info.properties[TGSI_PROPERTY_TES_VERTEX_ORDER_CW];
555    tes->point_mode = tes->info.properties[TGSI_PROPERTY_TES_POINT_MODE];
556 
557    tes->vector_length = 4;
558 
559    tes->position_output = -1;
560    for (unsigned i = 0; i < tes->info.num_outputs; i++) {
561       if (tes->info.output_semantic_name[i] == TGSI_SEMANTIC_POSITION &&
562           tes->info.output_semantic_index[i] == 0)
563          tes->position_output = i;
564       if (tes->info.output_semantic_name[i] == TGSI_SEMANTIC_VIEWPORT_INDEX)
565          tes->viewport_index_output = i;
566       if (tes->info.output_semantic_name[i] == TGSI_SEMANTIC_CLIPDIST) {
567          debug_assert(tes->info.output_semantic_index[i] <
568                       PIPE_MAX_CLIP_OR_CULL_DISTANCE_ELEMENT_COUNT);
569          tes->ccdistance_output[tes->info.output_semantic_index[i]] = i;
570       }
571    }
572 
573 #ifdef LLVM_AVAILABLE
574    if (use_llvm) {
575 
576       tes->tes_input = align_malloc(sizeof(struct draw_tes_inputs), 16);
577       memset(tes->tes_input, 0, sizeof(struct draw_tes_inputs));
578 
579       tes->jit_context = &draw->llvm->tes_jit_context;
580       llvm_tes->variant_key_size =
581          draw_tes_llvm_variant_key_size(
582                                         MAX2(tes->info.file_max[TGSI_FILE_SAMPLER]+1,
583                                              tes->info.file_max[TGSI_FILE_SAMPLER_VIEW]+1),
584                                         tes->info.file_max[TGSI_FILE_IMAGE]+1);
585    }
586 #endif
587    return tes;
588 }
589 
draw_bind_tess_eval_shader(struct draw_context * draw,struct draw_tess_eval_shader * dtes)590 void draw_bind_tess_eval_shader(struct draw_context *draw,
591                                 struct draw_tess_eval_shader *dtes)
592 {
593    draw_do_flush(draw, DRAW_FLUSH_STATE_CHANGE);
594    if (dtes) {
595       draw->tes.tess_eval_shader = dtes;
596       draw->tes.position_output = dtes->position_output;
597    } else {
598       draw->tes.tess_eval_shader = NULL;
599    }
600 }
601 
draw_delete_tess_eval_shader(struct draw_context * draw,struct draw_tess_eval_shader * dtes)602 void draw_delete_tess_eval_shader(struct draw_context *draw,
603                                   struct draw_tess_eval_shader *dtes)
604 {
605    if (!dtes)
606       return;
607 
608 #ifdef LLVM_AVAILABLE
609    if (draw->llvm) {
610       struct llvm_tess_eval_shader *shader = llvm_tess_eval_shader(dtes);
611       struct draw_tes_llvm_variant_list_item *li;
612 
613       li = first_elem(&shader->variants);
614       while(!at_end(&shader->variants, li)) {
615          struct draw_tes_llvm_variant_list_item *next = next_elem(li);
616          draw_tes_llvm_destroy_variant(li->base);
617          li = next;
618       }
619 
620       assert(shader->variants_cached == 0);
621       align_free(dtes->tes_input);
622    }
623 #endif
624    if (dtes->state.ir.nir)
625       ralloc_free(dtes->state.ir.nir);
626    FREE(dtes);
627 }
628 
629 #ifdef LLVM_AVAILABLE
draw_tes_set_current_variant(struct draw_tess_eval_shader * shader,struct draw_tes_llvm_variant * variant)630 void draw_tes_set_current_variant(struct draw_tess_eval_shader *shader,
631                                   struct draw_tes_llvm_variant *variant)
632 {
633    shader->current_variant = variant;
634 }
635 #endif
636 
get_tes_output_prim(struct draw_tess_eval_shader * shader)637 enum pipe_prim_type get_tes_output_prim(struct draw_tess_eval_shader *shader)
638 {
639    if (shader->point_mode)
640       return PIPE_PRIM_POINTS;
641    else if (shader->prim_mode == PIPE_PRIM_LINES)
642       return PIPE_PRIM_LINES;
643    else
644       return PIPE_PRIM_TRIANGLES;
645 }
646