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