xref: /aosp_15_r20/external/mesa3d/src/gallium/auxiliary/draw/draw_vs_llvm.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /**************************************************************************
2  *
3  * Copyright 2010 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 VMWARE 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 "util/u_math.h"
29 #include "util/u_memory.h"
30 #include "pipe/p_shader_tokens.h"
31 #include "pipe/p_screen.h"
32 
33 #include "draw_private.h"
34 #include "draw_context.h"
35 #include "draw_vs.h"
36 #include "draw_llvm.h"
37 
38 #include "tgsi/tgsi_parse.h"
39 #include "tgsi/tgsi_scan.h"
40 #include "nir/nir_to_tgsi_info.h"
41 #include "nir.h"
42 
43 
44 static void
vs_llvm_prepare(struct draw_vertex_shader * shader,struct draw_context * draw)45 vs_llvm_prepare(struct draw_vertex_shader *shader,
46                 struct draw_context *draw)
47 {
48    /*struct llvm_vertex_shader *evs = llvm_vertex_shader(shader);*/
49 }
50 
51 
52 static void
vs_llvm_run_linear(struct draw_vertex_shader * shader,const float (* input)[4],float (* output)[4],const struct draw_buffer_info * constants,unsigned count,unsigned input_stride,unsigned output_stride,const unsigned * elts)53 vs_llvm_run_linear(struct draw_vertex_shader *shader,
54                    const float (*input)[4],
55                    float (*output)[4],
56                    const struct draw_buffer_info *constants,
57                    unsigned count,
58                    unsigned input_stride,
59                    unsigned output_stride,
60                    const unsigned *elts)
61 {
62    /* we should never get here since the entire pipeline is
63     * generated in draw_pt_fetch_shade_pipeline_llvm.c */
64    assert(0);
65 }
66 
67 
68 static void
vs_llvm_delete(struct draw_vertex_shader * dvs)69 vs_llvm_delete(struct draw_vertex_shader *dvs)
70 {
71    struct llvm_vertex_shader *shader = llvm_vertex_shader(dvs);
72    struct draw_llvm_variant_list_item *li, *next;
73 
74    LIST_FOR_EACH_ENTRY_SAFE(li, next, &shader->variants.list, list) {
75       draw_llvm_destroy_variant(li->base);
76    }
77 
78    assert(shader->variants_cached == 0);
79    if (dvs->state.ir.nir)
80       ralloc_free(dvs->state.ir.nir);
81    FREE((void*) dvs->state.tokens);
82    FREE(dvs);
83 }
84 
85 
86 struct draw_vertex_shader *
draw_create_vs_llvm(struct draw_context * draw,const struct pipe_shader_state * state)87 draw_create_vs_llvm(struct draw_context *draw,
88                     const struct pipe_shader_state *state)
89 {
90    struct llvm_vertex_shader *vs = CALLOC_STRUCT(llvm_vertex_shader);
91 
92    if (!vs)
93       return NULL;
94 
95    if (state->type == PIPE_SHADER_IR_NIR) {
96       vs->base.state.ir.nir = state->ir.nir;
97       nir_shader *nir = (nir_shader *)state->ir.nir;
98       if (!nir->options->lower_uniforms_to_ubo)
99          NIR_PASS_V(state->ir.nir, nir_lower_uniforms_to_ubo, false, false);
100       nir_tgsi_scan_shader(state->ir.nir, &vs->base.info, true);
101    } else {
102       /* we make a private copy of the tokens */
103       vs->base.state.tokens = tgsi_dup_tokens(state->tokens);
104       if (!vs->base.state.tokens) {
105          FREE(vs);
106          return NULL;
107       }
108 
109       tgsi_scan_shader(state->tokens, &vs->base.info);
110    }
111 
112    vs->variant_key_size =
113       draw_llvm_variant_key_size(
114          vs->base.info.file_max[TGSI_FILE_INPUT]+1,
115          vs->base.info.file_max[TGSI_FILE_SAMPLER]+1,
116          vs->base.info.file_max[TGSI_FILE_SAMPLER_VIEW]+1,
117          vs->base.info.file_max[TGSI_FILE_IMAGE]+1);
118 
119    vs->base.state.type = state->type;
120    vs->base.state.stream_output = state->stream_output;
121    vs->base.draw = draw;
122    vs->base.prepare = vs_llvm_prepare;
123    vs->base.run_linear = vs_llvm_run_linear;
124    vs->base.delete = vs_llvm_delete;
125    vs->base.create_variant = draw_vs_create_variant_generic;
126 
127    list_inithead(&vs->variants.list);
128 
129    return &vs->base;
130 }
131