1 /**********************************************************
2 * Copyright 2018-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 "nir/nir_to_tgsi.h"
27 #include "pipe/p_context.h"
28 #include "util/u_memory.h"
29
30 #include "svga_context.h"
31 #include "svga_shader.h"
32
33 static void
svga_set_tess_state(struct pipe_context * pipe,const float default_outer_level[4],const float default_inner_level[2])34 svga_set_tess_state(struct pipe_context *pipe,
35 const float default_outer_level[4],
36 const float default_inner_level[2])
37 {
38 struct svga_context *svga = svga_context(pipe);
39 unsigned i;
40
41 for (i = 0; i < 4; i++) {
42 svga->curr.default_tesslevels[i] = default_outer_level[i];
43 }
44 for (i = 0; i < 2; i++) {
45 svga->curr.default_tesslevels[i + 4] = default_inner_level[i];
46 }
47 }
48
49
50 static void
svga_set_patch_vertices(struct pipe_context * pipe,uint8_t patch_vertices)51 svga_set_patch_vertices(struct pipe_context *pipe, uint8_t patch_vertices)
52 {
53 struct svga_context *svga = svga_context(pipe);
54
55 svga->patch_vertices = patch_vertices;
56 }
57
58
59 static void *
svga_create_tcs_state(struct pipe_context * pipe,const struct pipe_shader_state * templ)60 svga_create_tcs_state(struct pipe_context *pipe,
61 const struct pipe_shader_state *templ)
62 {
63 struct svga_context *svga = svga_context(pipe);
64 struct svga_tcs_shader *tcs;
65
66 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_CREATETCS);
67
68 tcs = (struct svga_tcs_shader *)
69 svga_create_shader(pipe, templ, PIPE_SHADER_TESS_CTRL,
70 sizeof(struct svga_tcs_shader));
71 if (!tcs)
72 goto done;
73
74 done:
75 SVGA_STATS_TIME_POP(svga_sws(svga));
76 (void) svga; /* silence unused var warning */
77
78 return tcs;
79 }
80
81
82 static void
svga_bind_tcs_state(struct pipe_context * pipe,void * shader)83 svga_bind_tcs_state(struct pipe_context *pipe, void *shader)
84 {
85 struct svga_tcs_shader *tcs = (struct svga_tcs_shader *) shader;
86 struct svga_context *svga = svga_context(pipe);
87
88 if (tcs == svga->curr.tcs)
89 return;
90
91 svga->curr.tcs = tcs;
92 svga->dirty |= SVGA_NEW_TCS;
93
94 /* Check if the shader uses samplers */
95 svga_set_curr_shader_use_samplers_flag(svga, PIPE_SHADER_TESS_CTRL,
96 svga_shader_use_samplers(&tcs->base));
97 }
98
99
100 static void
svga_delete_tcs_state(struct pipe_context * pipe,void * shader)101 svga_delete_tcs_state(struct pipe_context *pipe, void *shader)
102 {
103 struct svga_context *svga = svga_context(pipe);
104 struct svga_tcs_shader *tcs = (struct svga_tcs_shader *) shader;
105 struct svga_tcs_shader *next_tcs;
106 struct svga_shader_variant *variant, *tmp;
107
108 svga_hwtnl_flush_retry(svga);
109
110 assert(tcs->base.parent == NULL);
111
112 while (tcs) {
113 next_tcs = (struct svga_tcs_shader *)tcs->base.next;
114 for (variant = tcs->base.variants; variant; variant = tmp) {
115 tmp = variant->next;
116
117 /* Check if deleting currently bound shader */
118 if (variant == svga->state.hw_draw.tcs) {
119 SVGA_RETRY(svga, svga_set_shader(svga, SVGA3D_SHADERTYPE_HS, NULL));
120 svga->state.hw_draw.tcs = NULL;
121 }
122
123 svga_destroy_shader_variant(svga, variant);
124 }
125
126 FREE((void *)tcs->base.tokens);
127 FREE(tcs);
128 tcs = next_tcs;
129 }
130 }
131
132
133 void
svga_cleanup_tcs_state(struct svga_context * svga)134 svga_cleanup_tcs_state(struct svga_context *svga)
135 {
136 if (svga->tcs.passthrough_tcs) {
137 svga_delete_tcs_state(&svga->pipe, svga->tcs.passthrough_tcs);
138 }
139 }
140
141
142 static void *
svga_create_tes_state(struct pipe_context * pipe,const struct pipe_shader_state * templ)143 svga_create_tes_state(struct pipe_context *pipe,
144 const struct pipe_shader_state *templ)
145 {
146 struct svga_context *svga = svga_context(pipe);
147 struct svga_tes_shader *tes;
148
149 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_CREATETES);
150
151 tes = (struct svga_tes_shader *)
152 svga_create_shader(pipe, templ, PIPE_SHADER_TESS_EVAL,
153 sizeof(struct svga_tes_shader));
154
155 if (!tes)
156 goto done;
157
158 done:
159 SVGA_STATS_TIME_POP(svga_sws(svga));
160 (void) svga; /* silence unused var warning */
161
162 return tes;
163 }
164
165
166 static void
svga_bind_tes_state(struct pipe_context * pipe,void * shader)167 svga_bind_tes_state(struct pipe_context *pipe, void *shader)
168 {
169 struct svga_tes_shader *tes = (struct svga_tes_shader *) shader;
170 struct svga_context *svga = svga_context(pipe);
171
172 if (tes == svga->curr.tes)
173 return;
174
175 svga->curr.tes = tes;
176 svga->dirty |= SVGA_NEW_TES;
177
178 /* Check if the shader uses samplers */
179 svga_set_curr_shader_use_samplers_flag(svga, PIPE_SHADER_TESS_EVAL,
180 svga_shader_use_samplers(&tes->base));
181 }
182
183
184 static void
svga_delete_tes_state(struct pipe_context * pipe,void * shader)185 svga_delete_tes_state(struct pipe_context *pipe, void *shader)
186 {
187 struct svga_context *svga = svga_context(pipe);
188 struct svga_tes_shader *tes = (struct svga_tes_shader *) shader;
189 struct svga_tes_shader *next_tes;
190 struct svga_shader_variant *variant, *tmp;
191
192 svga_hwtnl_flush_retry(svga);
193
194 assert(tes->base.parent == NULL);
195
196 while (tes) {
197 next_tes = (struct svga_tes_shader *)tes->base.next;
198 for (variant = tes->base.variants; variant; variant = tmp) {
199 tmp = variant->next;
200
201 /* Check if deleting currently bound shader */
202 if (variant == svga->state.hw_draw.tes) {
203 SVGA_RETRY(svga, svga_set_shader(svga, SVGA3D_SHADERTYPE_DS, NULL));
204 svga->state.hw_draw.tes = NULL;
205 }
206
207 svga_destroy_shader_variant(svga, variant);
208 }
209
210 FREE((void *)tes->base.tokens);
211 FREE(tes);
212 tes = next_tes;
213 }
214 }
215
216
217 void
svga_init_ts_functions(struct svga_context * svga)218 svga_init_ts_functions(struct svga_context *svga)
219 {
220 svga->pipe.set_tess_state = svga_set_tess_state;
221 svga->pipe.set_patch_vertices = svga_set_patch_vertices;
222 svga->pipe.create_tcs_state = svga_create_tcs_state;
223 svga->pipe.bind_tcs_state = svga_bind_tcs_state;
224 svga->pipe.delete_tcs_state = svga_delete_tcs_state;
225 svga->pipe.create_tes_state = svga_create_tes_state;
226 svga->pipe.bind_tes_state = svga_bind_tes_state;
227 svga->pipe.delete_tes_state = svga_delete_tes_state;
228 }
229