890d153c7d676547ee8973a33d5b9f9370d74020
[mesa.git] / src / gallium / drivers / svga / svga_state_ts.c
1 /**********************************************************
2 * Copyright 2018-2020 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 "util/u_inlines.h"
27 #include "util/u_memory.h"
28 #include "util/u_simple_shaders.h"
29
30 #include "svga_context.h"
31 #include "svga_cmd.h"
32 #include "svga_tgsi.h"
33 #include "svga_shader.h"
34
35
36 /**
37 * Translate TGSI shader into an svga shader variant.
38 */
39 static enum pipe_error
40 compile_tcs(struct svga_context *svga,
41 struct svga_tcs_shader *tcs,
42 const struct svga_compile_key *key,
43 struct svga_shader_variant **out_variant)
44 {
45 struct svga_shader_variant *variant;
46 enum pipe_error ret = PIPE_ERROR;
47
48 variant = svga_tgsi_vgpu10_translate(svga, &tcs->base, key,
49 PIPE_SHADER_TESS_CTRL);
50 if (!variant)
51 return PIPE_ERROR;
52
53 ret = svga_define_shader(svga, variant);
54 if (ret != PIPE_OK) {
55 svga_destroy_shader_variant(svga, variant);
56 return ret;
57 }
58
59 *out_variant = variant;
60
61 return PIPE_OK;
62 }
63
64
65 static void
66 make_tcs_key(struct svga_context *svga, struct svga_compile_key *key)
67 {
68 struct svga_tcs_shader *tcs = svga->curr.tcs;
69
70 memset(key, 0, sizeof *key);
71
72 /*
73 * SVGA_NEW_TEXTURE_BINDING | SVGA_NEW_SAMPLER
74 */
75 svga_init_shader_key_common(svga, PIPE_SHADER_TESS_CTRL, &tcs->base, key);
76
77 /* SVGA_NEW_TCS_PARAM */
78 key->tcs.vertices_per_patch = svga->curr.vertices_per_patch;
79
80 /* The tessellator parameters come from the layout section in the
81 * tessellation evaluation shader. Get these parameters from the
82 * current tessellation evaluation shader variant.
83 * Note: this requires the tessellation evaluation shader to be
84 * compiled first.
85 */
86 struct svga_tes_variant *tes = svga_tes_variant(svga->state.hw_draw.tes);
87 key->tcs.prim_mode = tes->prim_mode;
88 key->tcs.spacing = tes->spacing;
89 key->tcs.vertices_order_cw = tes->vertices_order_cw;
90 key->tcs.point_mode = tes->point_mode;
91
92 if (svga->tcs.passthrough)
93 key->tcs.passthrough = 1;
94
95 key->clip_plane_enable = svga->curr.rast->templ.clip_plane_enable;
96
97 /* tcs is always followed by tes */
98 key->last_vertex_stage = 0;
99 }
100
101
102 static enum pipe_error
103 emit_hw_tcs(struct svga_context *svga, uint64_t dirty)
104 {
105 struct svga_shader_variant *variant;
106 struct svga_tcs_shader *tcs = svga->curr.tcs;
107 enum pipe_error ret = PIPE_OK;
108 struct svga_compile_key key;
109
110 assert(svga_have_sm5(svga));
111
112 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_EMITTCS);
113
114 if (!tcs) {
115 /* If there is no active tcs, then there should not be
116 * active tes either
117 */
118 assert(!svga->curr.tes);
119 if (svga->state.hw_draw.tcs != NULL) {
120
121 /** The previous tessellation control shader is made inactive.
122 * Needs to unbind the tessellation control shader.
123 */
124 ret = svga_set_shader(svga, SVGA3D_SHADERTYPE_HS, NULL);
125 if (ret != PIPE_OK)
126 goto done;
127 svga->state.hw_draw.tcs = NULL;
128 }
129 goto done;
130 }
131
132 make_tcs_key(svga, &key);
133
134 /* See if we already have a TCS variant that matches the key */
135 variant = svga_search_shader_key(&tcs->base, &key);
136
137 if (!variant) {
138 ret = compile_tcs(svga, tcs, &key, &variant);
139 if (ret != PIPE_OK)
140 goto done;
141
142 /* insert the new variant at head of linked list */
143 assert(variant);
144 variant->next = tcs->base.variants;
145 tcs->base.variants = variant;
146 }
147
148 if (variant != svga->state.hw_draw.tcs) {
149 /* Bind the new variant */
150 ret = svga_set_shader(svga, SVGA3D_SHADERTYPE_HS, variant);
151 if (ret != PIPE_OK)
152 goto done;
153
154 svga->rebind.flags.tcs = FALSE;
155 svga->dirty |= SVGA_NEW_TCS_VARIANT;
156 svga->state.hw_draw.tcs = variant;
157 }
158
159 done:
160 SVGA_STATS_TIME_POP(svga_sws(svga));
161 return ret;
162 }
163
164
165 struct svga_tracked_state svga_hw_tcs =
166 {
167 "tessellation control shader (hwtnl)",
168 (SVGA_NEW_VS |
169 SVGA_NEW_TCS |
170 SVGA_NEW_TES |
171 SVGA_NEW_TEXTURE_BINDING |
172 SVGA_NEW_SAMPLER |
173 SVGA_NEW_RAST),
174 emit_hw_tcs
175 };
176
177
178 /**
179 * Translate TGSI shader into an svga shader variant.
180 */
181 static enum pipe_error
182 compile_tes(struct svga_context *svga,
183 struct svga_tes_shader *tes,
184 const struct svga_compile_key *key,
185 struct svga_shader_variant **out_variant)
186 {
187 struct svga_shader_variant *variant;
188 enum pipe_error ret = PIPE_ERROR;
189
190 variant = svga_tgsi_vgpu10_translate(svga, &tes->base, key,
191 PIPE_SHADER_TESS_EVAL);
192 if (!variant)
193 return PIPE_ERROR;
194
195 ret = svga_define_shader(svga, variant);
196 if (ret != PIPE_OK) {
197 svga_destroy_shader_variant(svga, variant);
198 return ret;
199 }
200
201 *out_variant = variant;
202
203 return PIPE_OK;
204 }
205
206
207 static void
208 make_tes_key(struct svga_context *svga, struct svga_compile_key *key)
209 {
210 struct svga_tes_shader *tes = svga->curr.tes;
211
212 memset(key, 0, sizeof *key);
213
214 /*
215 * SVGA_NEW_TEXTURE_BINDING | SVGA_NEW_SAMPLER
216 */
217 svga_init_shader_key_common(svga, PIPE_SHADER_TESS_EVAL, &tes->base, key);
218
219 assert(svga->curr.tcs);
220 key->tes.vertices_per_patch =
221 svga->curr.tcs->base.info.properties[TGSI_PROPERTY_TCS_VERTICES_OUT];
222
223 key->tes.need_prescale = svga->state.hw_clear.prescale[0].enabled &&
224 (svga->curr.gs == NULL);
225
226 /* tcs emits tessellation factors as extra outputs.
227 * Since tes depends on them, save the tessFactor output index
228 * from tcs in the tes compile key, so that if a different
229 * tcs is bound and if the tessFactor index is different,
230 * a different tes variant will be generated.
231 */
232 key->tes.tessfactor_index = svga->curr.tcs->base.info.num_outputs;
233
234 key->clip_plane_enable = svga->curr.rast->templ.clip_plane_enable;
235
236 /* This is the last vertex stage if there is no geometry shader. */
237 key->last_vertex_stage = !svga->curr.gs;
238
239 key->tes.need_tessinner = 0;
240 key->tes.need_tessouter = 0;
241
242 for (int i = 0; i < svga->curr.tcs->base.info.num_outputs; i++) {
243 switch (svga->curr.tcs->base.info.output_semantic_name[i]) {
244 case TGSI_SEMANTIC_TESSOUTER:
245 key->tes.need_tessouter = 1;
246 break;
247 case TGSI_SEMANTIC_TESSINNER:
248 key->tes.need_tessinner = 1;
249 break;
250 default:
251 break;
252 }
253 }
254
255 }
256
257
258 static void
259 get_passthrough_tcs(struct svga_context *svga)
260 {
261 if (svga->tcs.passthrough_tcs &&
262 svga->tcs.vs == svga->curr.vs &&
263 svga->tcs.tes == svga->curr.tes &&
264 svga->tcs.vertices_per_patch == svga->curr.vertices_per_patch) {
265 svga->pipe.bind_tcs_state(&svga->pipe,
266 svga->tcs.passthrough_tcs);
267 }
268 else {
269 struct svga_tcs_shader *new_tcs;
270
271 /* delete older passthrough shader*/
272 if (svga->tcs.passthrough_tcs) {
273 svga->pipe.delete_tcs_state(&svga->pipe,
274 svga->tcs.passthrough_tcs);
275 }
276
277 new_tcs = (struct svga_tcs_shader *)
278 util_make_tess_ctrl_passthrough_shader(&svga->pipe,
279 svga->curr.vs->base.info.num_outputs,
280 svga->curr.tes->base.info.num_inputs,
281 svga->curr.vs->base.info.output_semantic_name,
282 svga->curr.vs->base.info.output_semantic_index,
283 svga->curr.tes->base.info.input_semantic_name,
284 svga->curr.tes->base.info.input_semantic_index,
285 svga->curr.vertices_per_patch);
286 svga->pipe.bind_tcs_state(&svga->pipe, new_tcs);
287 svga->tcs.passthrough_tcs = new_tcs;
288 svga->tcs.vs = svga->curr.vs;
289 svga->tcs.tes = svga->curr.tes;
290 svga->tcs.vertices_per_patch = svga->curr.vertices_per_patch;
291 }
292
293 struct pipe_constant_buffer cb;
294
295 cb.buffer = NULL;
296 cb.user_buffer = (void *) svga->curr.default_tesslevels;
297 cb.buffer_offset = 0;
298 cb.buffer_size = 2 * 4 * sizeof(float);
299 svga->pipe.set_constant_buffer(&svga->pipe, PIPE_SHADER_TESS_CTRL, 0, &cb);
300 }
301
302
303 static enum pipe_error
304 emit_hw_tes(struct svga_context *svga, uint64_t dirty)
305 {
306 struct svga_shader_variant *variant;
307 struct svga_tes_shader *tes = svga->curr.tes;
308 enum pipe_error ret = PIPE_OK;
309 struct svga_compile_key key;
310
311 assert(svga_have_sm5(svga));
312
313 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_EMITTES);
314
315 if (!tes) {
316 /* The GL spec implies that TES is optional when there's a TCS,
317 * but that's apparently a spec error. Assert if we have a TCS
318 * but no TES.
319 */
320 assert(!svga->curr.tcs);
321 if (svga->state.hw_draw.tes != NULL) {
322
323 /** The previous tessellation evaluation shader is made inactive.
324 * Needs to unbind the tessellation evaluation shader.
325 */
326 ret = svga_set_shader(svga, SVGA3D_SHADERTYPE_DS, NULL);
327 if (ret != PIPE_OK)
328 goto done;
329 svga->state.hw_draw.tes = NULL;
330 }
331 goto done;
332 }
333
334 if (!svga->curr.tcs) {
335 /* TES state is processed before the TCS
336 * shader and that's why we're checking for and creating the
337 * passthough TCS in the emit_hw_tes() function.
338 */
339 get_passthrough_tcs(svga);
340 svga->tcs.passthrough = TRUE;
341 }
342 else {
343 svga->tcs.passthrough = FALSE;
344 }
345
346 make_tes_key(svga, &key);
347
348 /* See if we already have a TES variant that matches the key */
349 variant = svga_search_shader_key(&tes->base, &key);
350
351 if (!variant) {
352 ret = compile_tes(svga, tes, &key, &variant);
353 if (ret != PIPE_OK)
354 goto done;
355
356 /* insert the new variant at head of linked list */
357 assert(variant);
358 variant->next = tes->base.variants;
359 tes->base.variants = variant;
360 }
361
362 if (variant != svga->state.hw_draw.tes) {
363 /* Bind the new variant */
364 ret = svga_set_shader(svga, SVGA3D_SHADERTYPE_DS, variant);
365 if (ret != PIPE_OK)
366 goto done;
367
368 svga->rebind.flags.tes = FALSE;
369 svga->dirty |= SVGA_NEW_TES_VARIANT;
370 svga->state.hw_draw.tes = variant;
371 }
372
373 done:
374 SVGA_STATS_TIME_POP(svga_sws(svga));
375 return ret;
376 }
377
378
379 struct svga_tracked_state svga_hw_tes =
380 {
381 "tessellation evaluation shader (hwtnl)",
382 /* TBD SVGA_NEW_VS/SVGA_NEW_FS/SVGA_NEW_GS are required or not*/
383 (SVGA_NEW_VS |
384 SVGA_NEW_FS |
385 SVGA_NEW_GS |
386 SVGA_NEW_TCS |
387 SVGA_NEW_TES |
388 SVGA_NEW_TEXTURE_BINDING |
389 SVGA_NEW_SAMPLER |
390 SVGA_NEW_RAST),
391 emit_hw_tes
392 };