1/**********************************************************
2 * Copyright 2008-2022 VMware, Inc.  All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26#include "draw/draw_context.h"
27#include "nir/nir_to_tgsi.h"
28#include "util/u_inlines.h"
29#include "util/u_math.h"
30#include "util/u_memory.h"
31#include "util/u_bitmask.h"
32#include "tgsi/tgsi_parse.h"
33#include "tgsi/tgsi_text.h"
34
35#include "svga_context.h"
36#include "svga_hw_reg.h"
37#include "svga_cmd.h"
38#include "svga_debug.h"
39#include "svga_shader.h"
40#include "svga_streamout.h"
41
42
43static void *
44svga_create_vs_state(struct pipe_context *pipe,
45                     const struct pipe_shader_state *templ)
46{
47   struct svga_context *svga = svga_context(pipe);
48   struct svga_vertex_shader *vs;
49
50   SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_CREATEVS);
51
52   vs = (struct svga_vertex_shader *)
53            svga_create_shader(pipe, templ, PIPE_SHADER_VERTEX,
54                               sizeof(struct svga_vertex_shader));
55   if (!vs)
56      goto done;
57
58   vs->base.get_dummy_shader = svga_get_compiled_dummy_vertex_shader;
59
60   {
61      /* Need to do construct a new template in case we substituted a
62       * debug shader.
63       */
64      struct pipe_shader_state tmp2 = *templ;
65
66      /* shader IR has been converted to tgsi */
67      tmp2.type = PIPE_SHADER_IR_TGSI;
68      tmp2.tokens = vs->base.tokens;
69      vs->draw_shader = draw_create_vertex_shader(svga->swtnl.draw, &tmp2);
70   }
71
72done:
73   SVGA_STATS_TIME_POP(svga_sws(svga));
74   return vs;
75}
76
77
78static void
79svga_bind_vs_state(struct pipe_context *pipe, void *shader)
80{
81   struct svga_vertex_shader *vs = (struct svga_vertex_shader *)shader;
82   struct svga_context *svga = svga_context(pipe);
83
84   if (vs == svga->curr.vs)
85      return;
86
87   /* If the currently bound vertex shader has a generated geometry shader,
88    * then unbind the geometry shader before binding a new vertex shader.
89    * We need to unbind the geometry shader here because there is no
90    * pipe_shader associated with the generated geometry shader.
91    */
92   if (svga->curr.vs != NULL && svga->curr.vs->gs != NULL)
93      svga->pipe.bind_gs_state(&svga->pipe, NULL);
94
95   svga->curr.vs = vs;
96   svga->dirty |= SVGA_NEW_VS;
97}
98
99
100static void
101svga_delete_vs_state(struct pipe_context *pipe, void *shader)
102{
103   struct svga_context *svga = svga_context(pipe);
104   struct svga_vertex_shader *vs = (struct svga_vertex_shader *)shader;
105   struct svga_vertex_shader *next_vs;
106   struct svga_shader_variant *variant, *tmp;
107
108   svga_hwtnl_flush_retry(svga);
109
110   assert(vs->base.parent == NULL);
111
112   while (vs) {
113      next_vs = (struct svga_vertex_shader *)vs->base.next;
114
115      /* Check if there is a generated geometry shader to go with this
116       * vertex shader. If there is, then delete the geometry shader as well.
117       */
118      if (vs->gs != NULL) {
119         svga->pipe.delete_gs_state(&svga->pipe, vs->gs);
120      }
121
122      if (vs->base.stream_output != NULL)
123         svga_delete_stream_output(svga, vs->base.stream_output);
124
125      draw_delete_vertex_shader(svga->swtnl.draw, vs->draw_shader);
126
127      for (variant = vs->base.variants; variant; variant = tmp) {
128         tmp = variant->next;
129
130         /* Check if deleting currently bound shader */
131         if (variant == svga->state.hw_draw.vs) {
132            SVGA_RETRY(svga, svga_set_shader(svga, SVGA3D_SHADERTYPE_VS, NULL));
133            svga->state.hw_draw.vs = NULL;
134         }
135
136         svga_destroy_shader_variant(svga, variant);
137      }
138
139      FREE((void *)vs->base.tokens);
140      FREE(vs);
141      vs = next_vs;
142   }
143}
144
145
146void
147svga_init_vs_functions(struct svga_context *svga)
148{
149   svga->pipe.create_vs_state = svga_create_vs_state;
150   svga->pipe.bind_vs_state = svga_bind_vs_state;
151   svga->pipe.delete_vs_state = svga_delete_vs_state;
152}
153
154