1bf215546Sopenharmony_ci/**********************************************************
2bf215546Sopenharmony_ci * Copyright 2014-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_memory.h"
30bf215546Sopenharmony_ci#include "util/u_bitmask.h"
31bf215546Sopenharmony_ci#include "tgsi/tgsi_parse.h"
32bf215546Sopenharmony_ci#include "tgsi/tgsi_text.h"
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci#include "svga_context.h"
35bf215546Sopenharmony_ci#include "svga_cmd.h"
36bf215546Sopenharmony_ci#include "svga_debug.h"
37bf215546Sopenharmony_ci#include "svga_shader.h"
38bf215546Sopenharmony_ci#include "svga_streamout.h"
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_cistatic void *
41bf215546Sopenharmony_cisvga_create_gs_state(struct pipe_context *pipe,
42bf215546Sopenharmony_ci                     const struct pipe_shader_state *templ)
43bf215546Sopenharmony_ci{
44bf215546Sopenharmony_ci   struct svga_context *svga = svga_context(pipe);
45bf215546Sopenharmony_ci   struct svga_geometry_shader *gs;
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ci   SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_CREATEGS);
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_ci   gs = (struct svga_geometry_shader *)
50bf215546Sopenharmony_ci            svga_create_shader(pipe, templ, PIPE_SHADER_GEOMETRY,
51bf215546Sopenharmony_ci                               sizeof(struct svga_geometry_shader));
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_ci   if (!gs)
54bf215546Sopenharmony_ci      goto done;
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci   /* Original shader IR could have been deleted if it is converted from
57bf215546Sopenharmony_ci    * NIR to TGSI. So need to explicitly set the shader state type to TGSI
58bf215546Sopenharmony_ci    * before passing it to draw.
59bf215546Sopenharmony_ci    */
60bf215546Sopenharmony_ci   struct pipe_shader_state tmp = *templ;
61bf215546Sopenharmony_ci   tmp.type = PIPE_SHADER_IR_TGSI;
62bf215546Sopenharmony_ci   tmp.tokens = gs->base.tokens;
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci   gs->base.get_dummy_shader = svga_get_compiled_dummy_geometry_shader;
65bf215546Sopenharmony_ci   gs->draw_shader = draw_create_geometry_shader(svga->swtnl.draw, &tmp);
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_cidone:
68bf215546Sopenharmony_ci   SVGA_STATS_TIME_POP(svga_sws(svga));
69bf215546Sopenharmony_ci   return gs;
70bf215546Sopenharmony_ci}
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_cistatic void
74bf215546Sopenharmony_cisvga_bind_gs_state(struct pipe_context *pipe, void *shader)
75bf215546Sopenharmony_ci{
76bf215546Sopenharmony_ci   struct svga_geometry_shader *gs = (struct svga_geometry_shader *)shader;
77bf215546Sopenharmony_ci   struct svga_context *svga = svga_context(pipe);
78bf215546Sopenharmony_ci
79bf215546Sopenharmony_ci   svga->curr.user_gs = gs;
80bf215546Sopenharmony_ci   svga->dirty |= SVGA_NEW_GS;
81bf215546Sopenharmony_ci}
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_cistatic void
85bf215546Sopenharmony_cisvga_delete_gs_state(struct pipe_context *pipe, void *shader)
86bf215546Sopenharmony_ci{
87bf215546Sopenharmony_ci   struct svga_context *svga = svga_context(pipe);
88bf215546Sopenharmony_ci   struct svga_geometry_shader *gs = (struct svga_geometry_shader *)shader;
89bf215546Sopenharmony_ci   struct svga_geometry_shader *next_gs;
90bf215546Sopenharmony_ci   struct svga_shader_variant *variant, *tmp;
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ci   svga_hwtnl_flush_retry(svga);
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_ci   /* Start deletion from the original geometry shader state */
95bf215546Sopenharmony_ci   if (gs->base.parent != NULL)
96bf215546Sopenharmony_ci      gs = (struct svga_geometry_shader *)gs->base.parent;
97bf215546Sopenharmony_ci
98bf215546Sopenharmony_ci   /* Free the list of geometry shaders */
99bf215546Sopenharmony_ci   while (gs) {
100bf215546Sopenharmony_ci      next_gs = (struct svga_geometry_shader *)gs->base.next;
101bf215546Sopenharmony_ci
102bf215546Sopenharmony_ci      if (gs->base.stream_output != NULL)
103bf215546Sopenharmony_ci         svga_delete_stream_output(svga, gs->base.stream_output);
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci      draw_delete_geometry_shader(svga->swtnl.draw, gs->draw_shader);
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_ci      for (variant = gs->base.variants; variant; variant = tmp) {
108bf215546Sopenharmony_ci         tmp = variant->next;
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_ci         /* Check if deleting currently bound shader */
111bf215546Sopenharmony_ci         if (variant == svga->state.hw_draw.gs) {
112bf215546Sopenharmony_ci            SVGA_RETRY(svga, svga_set_shader(svga, SVGA3D_SHADERTYPE_GS, NULL));
113bf215546Sopenharmony_ci            svga->state.hw_draw.gs = NULL;
114bf215546Sopenharmony_ci         }
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_ci         svga_destroy_shader_variant(svga, variant);
117bf215546Sopenharmony_ci      }
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_ci      FREE((void *)gs->base.tokens);
120bf215546Sopenharmony_ci      FREE(gs);
121bf215546Sopenharmony_ci      gs = next_gs;
122bf215546Sopenharmony_ci   }
123bf215546Sopenharmony_ci}
124bf215546Sopenharmony_ci
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_civoid
127bf215546Sopenharmony_cisvga_init_gs_functions(struct svga_context *svga)
128bf215546Sopenharmony_ci{
129bf215546Sopenharmony_ci   svga->pipe.create_gs_state = svga_create_gs_state;
130bf215546Sopenharmony_ci   svga->pipe.bind_gs_state = svga_bind_gs_state;
131bf215546Sopenharmony_ci   svga->pipe.delete_gs_state = svga_delete_gs_state;
132bf215546Sopenharmony_ci}
133