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