xref: /aosp_15_r20/external/mesa3d/src/gallium/auxiliary/draw/draw_vs.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /**************************************************************************
2  *
3  * Copyright 2007 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  /*
29   * Authors:
30   *   Keith Whitwell <[email protected]>
31   *   Brian Paul
32   */
33 
34 #include "util/u_math.h"
35 #include "util/u_memory.h"
36 
37 #include "pipe/p_shader_tokens.h"
38 #include "pipe/p_context.h"
39 #include "pipe/p_screen.h"
40 
41 #include "draw_private.h"
42 #include "draw_context.h"
43 #include "draw_vs.h"
44 
45 #include "translate/translate.h"
46 #include "translate/translate_cache.h"
47 
48 #include "tgsi/tgsi_dump.h"
49 #include "tgsi/tgsi_exec.h"
50 #include "tgsi/tgsi_ureg.h"
51 
52 #include "nir/nir_to_tgsi.h"
53 
54 DEBUG_GET_ONCE_BOOL_OPTION(gallium_dump_vs, "GALLIUM_DUMP_VS", false)
55 
56 
57 struct draw_vertex_shader *
draw_create_vertex_shader(struct draw_context * draw,const struct pipe_shader_state * shader)58 draw_create_vertex_shader(struct draw_context *draw,
59                           const struct pipe_shader_state *shader)
60 {
61    struct draw_vertex_shader *vs = NULL;
62    struct pipe_shader_state state = *shader;
63 
64    if (draw->dump_vs) {
65       tgsi_dump(shader->tokens, 0);
66    }
67 
68 #if DRAW_LLVM_AVAILABLE
69    bool is_allocated = false;
70    if (draw->pt.middle.llvm) {
71       struct pipe_screen *screen = draw->pipe->screen;
72       if (shader->type == PIPE_SHADER_IR_NIR &&
73           !screen->get_shader_param(screen, PIPE_SHADER_VERTEX,
74                                     PIPE_SHADER_CAP_INTEGERS)) {
75         state.type = PIPE_SHADER_IR_TGSI;
76         state.tokens = nir_to_tgsi(shader->ir.nir, screen);
77         is_allocated = true;
78       }
79       vs = draw_create_vs_llvm(draw, &state);
80    }
81 #endif
82 
83    if (!vs) {
84       vs = draw_create_vs_exec(draw, &state);
85    }
86 
87 #if DRAW_LLVM_AVAILABLE
88    if (is_allocated) {
89       ureg_free_tokens(state.tokens);
90    }
91 #endif
92 
93    if (vs) {
94       bool found_clipvertex = false;
95       vs->position_output = -1;
96       for (unsigned i = 0; i < vs->info.num_outputs; i++) {
97          if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_POSITION &&
98              vs->info.output_semantic_index[i] == 0) {
99             vs->position_output = i;
100          } else if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_EDGEFLAG &&
101              vs->info.output_semantic_index[i] == 0) {
102             vs->edgeflag_output = i;
103          } else if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_CLIPVERTEX &&
104                   vs->info.output_semantic_index[i] == 0) {
105             found_clipvertex = true;
106             vs->clipvertex_output = i;
107          } else if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_VIEWPORT_INDEX) {
108             vs->viewport_index_output = i;
109          } else if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_CLIPDIST) {
110             assert(vs->info.output_semantic_index[i] <
111                          PIPE_MAX_CLIP_OR_CULL_DISTANCE_ELEMENT_COUNT);
112             vs->ccdistance_output[vs->info.output_semantic_index[i]] = i;
113          }
114       }
115       if (!found_clipvertex)
116          vs->clipvertex_output = vs->position_output;
117    }
118 
119    assert(vs);
120    return vs;
121 }
122 
123 
124 void
draw_bind_vertex_shader(struct draw_context * draw,struct draw_vertex_shader * dvs)125 draw_bind_vertex_shader(struct draw_context *draw,
126                         struct draw_vertex_shader *dvs)
127 {
128    draw_do_flush(draw, DRAW_FLUSH_STATE_CHANGE);
129 
130    if (dvs) {
131       draw->vs.vertex_shader = dvs;
132       draw->vs.num_vs_outputs = dvs->info.num_outputs;
133       draw->vs.position_output = dvs->position_output;
134       draw->vs.edgeflag_output = dvs->edgeflag_output;
135       draw->vs.clipvertex_output = dvs->clipvertex_output;
136       draw->vs.ccdistance_output[0] = dvs->ccdistance_output[0];
137       draw->vs.ccdistance_output[1] = dvs->ccdistance_output[1];
138       dvs->prepare(dvs, draw);
139       draw_update_clip_flags(draw);
140       draw_update_viewport_flags(draw);
141    } else {
142       draw->vs.vertex_shader = NULL;
143       draw->vs.num_vs_outputs = 0;
144    }
145 }
146 
147 
148 void
draw_delete_vertex_shader(struct draw_context * draw,struct draw_vertex_shader * dvs)149 draw_delete_vertex_shader(struct draw_context *draw,
150                           struct draw_vertex_shader *dvs)
151 {
152    for (unsigned i = 0; i < dvs->nr_variants; i++)
153       dvs->variant[i]->destroy(dvs->variant[i]);
154 
155    dvs->nr_variants = 0;
156 
157    dvs->delete(dvs);
158 }
159 
160 
161 bool
draw_vs_init(struct draw_context * draw)162 draw_vs_init(struct draw_context *draw)
163 {
164    draw->dump_vs = debug_get_option_gallium_dump_vs();
165 
166    if (!draw->llvm) {
167       draw->vs.tgsi.machine = tgsi_exec_machine_create(PIPE_SHADER_VERTEX);
168       if (!draw->vs.tgsi.machine)
169          return false;
170    }
171 
172    draw->vs.emit_cache = translate_cache_create();
173    if (!draw->vs.emit_cache)
174       return false;
175 
176    draw->vs.fetch_cache = translate_cache_create();
177    if (!draw->vs.fetch_cache)
178       return false;
179 
180    return true;
181 }
182 
183 
184 void
draw_vs_destroy(struct draw_context * draw)185 draw_vs_destroy(struct draw_context *draw)
186 {
187    if (draw->vs.fetch_cache)
188       translate_cache_destroy(draw->vs.fetch_cache);
189 
190    if (draw->vs.emit_cache)
191       translate_cache_destroy(draw->vs.emit_cache);
192 
193    if (!draw->llvm)
194       tgsi_exec_machine_destroy(draw->vs.tgsi.machine);
195 }
196 
197 
198 struct draw_vs_variant *
draw_vs_lookup_variant(struct draw_vertex_shader * vs,const struct draw_vs_variant_key * key)199 draw_vs_lookup_variant(struct draw_vertex_shader *vs,
200                        const struct draw_vs_variant_key *key)
201 {
202    /* Lookup existing variant:
203     */
204    for (unsigned i = 0; i < vs->nr_variants; i++)
205       if (draw_vs_variant_key_compare(key, &vs->variant[i]->key) == 0)
206          return vs->variant[i];
207 
208    /* Else have to create a new one:
209     */
210    struct draw_vs_variant *variant = vs->create_variant(vs, key);
211    if (!variant)
212       return NULL;
213 
214    /* Add it to our list, could be smarter:
215     */
216    if (vs->nr_variants < ARRAY_SIZE(vs->variant)) {
217       vs->variant[vs->nr_variants++] = variant;
218    } else {
219       vs->last_variant++;
220       vs->last_variant %= ARRAY_SIZE(vs->variant);
221       vs->variant[vs->last_variant]->destroy(vs->variant[vs->last_variant]);
222       vs->variant[vs->last_variant] = variant;
223    }
224 
225    return variant;
226 }
227 
228 
229 struct translate *
draw_vs_get_fetch(struct draw_context * draw,struct translate_key * key)230 draw_vs_get_fetch(struct draw_context *draw,
231                   struct translate_key *key)
232 {
233    if (!draw->vs.fetch ||
234        translate_key_compare(&draw->vs.fetch->key, key) != 0) {
235       translate_key_sanitize(key);
236       draw->vs.fetch = translate_cache_find(draw->vs.fetch_cache, key);
237    }
238 
239    return draw->vs.fetch;
240 }
241 
242 
243 struct translate *
draw_vs_get_emit(struct draw_context * draw,struct translate_key * key)244 draw_vs_get_emit(struct draw_context *draw,
245                  struct translate_key *key)
246 {
247    if (!draw->vs.emit ||
248        translate_key_compare(&draw->vs.emit->key, key) != 0) {
249       translate_key_sanitize(key);
250       draw->vs.emit = translate_cache_find(draw->vs.emit_cache, key);
251    }
252 
253    return draw->vs.emit;
254 }
255 
256 
257 void
draw_vs_attach_so(struct draw_vertex_shader * dvs,const struct pipe_stream_output_info * info)258 draw_vs_attach_so(struct draw_vertex_shader *dvs,
259                   const struct pipe_stream_output_info *info)
260 {
261    dvs->state.stream_output = *info;
262 }
263 
264 
265 void
draw_vs_reset_so(struct draw_vertex_shader * dvs)266 draw_vs_reset_so(struct draw_vertex_shader *dvs)
267 {
268    memset(&dvs->state.stream_output, 0, sizeof(dvs->state.stream_output));
269 }
270