• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
compile_tcs(struct svga_context * svga,struct svga_tcs_shader * tcs,const struct svga_compile_key * key,struct svga_shader_variant ** out_variant)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
make_tcs_key(struct svga_context * svga,struct svga_compile_key * key)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    /* The number of control point output from tcs is determined by the
93     * number of control point input expected in tes. If tes does not expect
94     * any control point input, then vertices_per_patch in the tes key will
95     * be 0, otherwise it will contain the number of vertices out as specified
96     * in the tcs property.
97     */
98    key->tcs.vertices_out = tes->base.key.tes.vertices_per_patch;
99 
100    if (svga->tcs.passthrough)
101       key->tcs.passthrough = 1;
102 
103    key->clip_plane_enable = svga->curr.rast->templ.clip_plane_enable;
104 
105    /* tcs is always followed by tes */
106    key->last_vertex_stage = 0;
107 }
108 
109 
110 static enum pipe_error
emit_hw_tcs(struct svga_context * svga,uint64_t dirty)111 emit_hw_tcs(struct svga_context *svga, uint64_t dirty)
112 {
113    struct svga_shader_variant *variant;
114    struct svga_tcs_shader *tcs = svga->curr.tcs;
115    enum pipe_error ret = PIPE_OK;
116    struct svga_compile_key key;
117 
118    assert(svga_have_sm5(svga));
119 
120    SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_EMITTCS);
121 
122    if (!tcs) {
123       /* If there is no active tcs, then there should not be
124        * active tes either
125        */
126       assert(!svga->curr.tes);
127       if (svga->state.hw_draw.tcs != NULL) {
128 
129          /** The previous tessellation control shader is made inactive.
130           *  Needs to unbind the tessellation control shader.
131           */
132          ret = svga_set_shader(svga, SVGA3D_SHADERTYPE_HS, NULL);
133          if (ret != PIPE_OK)
134             goto done;
135          svga->state.hw_draw.tcs = NULL;
136       }
137       goto done;
138    }
139 
140    make_tcs_key(svga, &key);
141 
142    /* See if we already have a TCS variant that matches the key */
143    variant = svga_search_shader_key(&tcs->base, &key);
144 
145    if (!variant) {
146       ret = compile_tcs(svga, tcs, &key, &variant);
147       if (ret != PIPE_OK)
148          goto done;
149 
150       /* insert the new variant at head of linked list */
151       assert(variant);
152       variant->next = tcs->base.variants;
153       tcs->base.variants = variant;
154    }
155 
156    if (variant != svga->state.hw_draw.tcs) {
157       /* Bind the new variant */
158       ret = svga_set_shader(svga, SVGA3D_SHADERTYPE_HS, variant);
159       if (ret != PIPE_OK)
160          goto done;
161 
162       svga->rebind.flags.tcs = FALSE;
163       svga->dirty |= SVGA_NEW_TCS_VARIANT;
164       svga->state.hw_draw.tcs = variant;
165    }
166 
167 done:
168    SVGA_STATS_TIME_POP(svga_sws(svga));
169    return ret;
170 }
171 
172 
173 struct svga_tracked_state svga_hw_tcs =
174 {
175    "tessellation control shader (hwtnl)",
176    (SVGA_NEW_VS |
177     SVGA_NEW_TCS |
178     SVGA_NEW_TES |
179     SVGA_NEW_TEXTURE_BINDING |
180     SVGA_NEW_SAMPLER |
181     SVGA_NEW_RAST),
182    emit_hw_tcs
183 };
184 
185 
186 /**
187  * Translate TGSI shader into an svga shader variant.
188  */
189 static enum pipe_error
compile_tes(struct svga_context * svga,struct svga_tes_shader * tes,const struct svga_compile_key * key,struct svga_shader_variant ** out_variant)190 compile_tes(struct svga_context *svga,
191            struct svga_tes_shader *tes,
192            const struct svga_compile_key *key,
193            struct svga_shader_variant **out_variant)
194 {
195    struct svga_shader_variant *variant;
196    enum pipe_error ret = PIPE_ERROR;
197 
198    variant = svga_tgsi_vgpu10_translate(svga, &tes->base, key,
199                                         PIPE_SHADER_TESS_EVAL);
200    if (!variant)
201       return PIPE_ERROR;
202 
203    ret = svga_define_shader(svga, variant);
204    if (ret != PIPE_OK) {
205       svga_destroy_shader_variant(svga, variant);
206       return ret;
207    }
208 
209    *out_variant = variant;
210 
211    return PIPE_OK;
212 }
213 
214 
215 static void
make_tes_key(struct svga_context * svga,struct svga_compile_key * key)216 make_tes_key(struct svga_context *svga, struct svga_compile_key *key)
217 {
218    struct svga_tes_shader *tes = svga->curr.tes;
219    boolean has_control_point_inputs = FALSE;
220 
221    memset(key, 0, sizeof *key);
222 
223    /*
224     * SVGA_NEW_TEXTURE_BINDING | SVGA_NEW_SAMPLER
225     */
226    svga_init_shader_key_common(svga, PIPE_SHADER_TESS_EVAL, &tes->base, key);
227 
228    assert(svga->curr.tcs);
229 
230    /*
231     * Check if this tes expects any output control points from tcs.
232     */
233    for (unsigned i = 0; i < tes->base.info.num_inputs; i++) {
234       switch (tes->base.info.input_semantic_name[i]) {
235       case TGSI_SEMANTIC_PATCH:
236       case TGSI_SEMANTIC_TESSOUTER:
237       case TGSI_SEMANTIC_TESSINNER:
238          break;
239       default:
240          has_control_point_inputs = TRUE;
241       }
242    }
243 
244    key->tes.vertices_per_patch = has_control_point_inputs ?
245       svga->curr.tcs->base.info.properties[TGSI_PROPERTY_TCS_VERTICES_OUT] : 0;
246 
247    key->tes.need_prescale = svga->state.hw_clear.prescale[0].enabled &&
248                             (svga->curr.gs == NULL);
249 
250    /* tcs emits tessellation factors as extra outputs.
251     * Since tes depends on them, save the tessFactor output index
252     * from tcs in the tes compile key, so that if a different
253     * tcs is bound and if the tessFactor index is different,
254     * a different tes variant will be generated.
255     */
256    key->tes.tessfactor_index = svga->curr.tcs->base.info.num_outputs;
257 
258    key->clip_plane_enable = svga->curr.rast->templ.clip_plane_enable;
259 
260    /* This is the last vertex stage if there is no geometry shader. */
261    key->last_vertex_stage = !svga->curr.gs;
262 
263    key->tes.need_tessinner = 0;
264    key->tes.need_tessouter = 0;
265 
266    for (unsigned i = 0; i < svga->curr.tcs->base.info.num_outputs; i++) {
267       switch (svga->curr.tcs->base.info.output_semantic_name[i]) {
268       case TGSI_SEMANTIC_TESSOUTER:
269          key->tes.need_tessouter = 1;
270          break;
271       case TGSI_SEMANTIC_TESSINNER:
272          key->tes.need_tessinner = 1;
273          break;
274       default:
275          break;
276       }
277    }
278 
279 }
280 
281 
282 static void
get_passthrough_tcs(struct svga_context * svga)283 get_passthrough_tcs(struct svga_context *svga)
284 {
285    if (svga->tcs.passthrough_tcs &&
286        svga->tcs.vs == svga->curr.vs &&
287        svga->tcs.tes == svga->curr.tes &&
288        svga->tcs.vertices_per_patch == svga->curr.vertices_per_patch) {
289       svga->pipe.bind_tcs_state(&svga->pipe,
290                                 svga->tcs.passthrough_tcs);
291    }
292    else {
293       struct svga_tcs_shader *new_tcs;
294 
295       /* delete older passthrough shader*/
296       if (svga->tcs.passthrough_tcs) {
297          svga->pipe.delete_tcs_state(&svga->pipe,
298                                      svga->tcs.passthrough_tcs);
299       }
300 
301       new_tcs = (struct svga_tcs_shader *)
302          util_make_tess_ctrl_passthrough_shader(&svga->pipe,
303             svga->curr.vs->base.info.num_outputs,
304             svga->curr.tes->base.info.num_inputs,
305             svga->curr.vs->base.info.output_semantic_name,
306             svga->curr.vs->base.info.output_semantic_index,
307             svga->curr.tes->base.info.input_semantic_name,
308             svga->curr.tes->base.info.input_semantic_index,
309             svga->curr.vertices_per_patch);
310       svga->pipe.bind_tcs_state(&svga->pipe, new_tcs);
311       svga->tcs.passthrough_tcs = new_tcs;
312       svga->tcs.vs = svga->curr.vs;
313       svga->tcs.tes = svga->curr.tes;
314       svga->tcs.vertices_per_patch = svga->curr.vertices_per_patch;
315    }
316 
317    struct pipe_constant_buffer cb;
318 
319    cb.buffer = NULL;
320    cb.user_buffer = (void *) svga->curr.default_tesslevels;
321    cb.buffer_offset = 0;
322    cb.buffer_size = 2 * 4 * sizeof(float);
323    svga->pipe.set_constant_buffer(&svga->pipe, PIPE_SHADER_TESS_CTRL, 0, &cb);
324 }
325 
326 
327 static enum pipe_error
emit_hw_tes(struct svga_context * svga,uint64_t dirty)328 emit_hw_tes(struct svga_context *svga, uint64_t dirty)
329 {
330    struct svga_shader_variant *variant;
331    struct svga_tes_shader *tes = svga->curr.tes;
332    enum pipe_error ret = PIPE_OK;
333    struct svga_compile_key key;
334 
335    assert(svga_have_sm5(svga));
336 
337    SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_EMITTES);
338 
339    if (!tes) {
340       /* The GL spec implies that TES is optional when there's a TCS,
341        * but that's apparently a spec error. Assert if we have a TCS
342        * but no TES.
343        */
344       assert(!svga->curr.tcs);
345       if (svga->state.hw_draw.tes != NULL) {
346 
347          /** The previous tessellation evaluation shader is made inactive.
348           *  Needs to unbind the tessellation evaluation shader.
349           */
350          ret = svga_set_shader(svga, SVGA3D_SHADERTYPE_DS, NULL);
351          if (ret != PIPE_OK)
352             goto done;
353          svga->state.hw_draw.tes = NULL;
354       }
355       goto done;
356    }
357 
358    if (!svga->curr.tcs) {
359       /* TES state is processed before the TCS
360        * shader and that's why we're checking for and creating the
361        * passthough TCS in the emit_hw_tes() function.
362        */
363       get_passthrough_tcs(svga);
364       svga->tcs.passthrough = TRUE;
365    }
366    else {
367       svga->tcs.passthrough = FALSE;
368    }
369 
370    make_tes_key(svga, &key);
371 
372    /* See if we already have a TES variant that matches the key */
373    variant = svga_search_shader_key(&tes->base, &key);
374 
375    if (!variant) {
376       ret = compile_tes(svga, tes, &key, &variant);
377       if (ret != PIPE_OK)
378          goto done;
379 
380       /* insert the new variant at head of linked list */
381       assert(variant);
382       variant->next = tes->base.variants;
383       tes->base.variants = variant;
384    }
385 
386    if (variant != svga->state.hw_draw.tes) {
387       /* Bind the new variant */
388       ret = svga_set_shader(svga, SVGA3D_SHADERTYPE_DS, variant);
389       if (ret != PIPE_OK)
390          goto done;
391 
392       svga->rebind.flags.tes = FALSE;
393       svga->dirty |= SVGA_NEW_TES_VARIANT;
394       svga->state.hw_draw.tes = variant;
395    }
396 
397 done:
398    SVGA_STATS_TIME_POP(svga_sws(svga));
399    return ret;
400 }
401 
402 
403 struct svga_tracked_state svga_hw_tes =
404 {
405    "tessellation evaluation shader (hwtnl)",
406    /* TBD SVGA_NEW_VS/SVGA_NEW_FS/SVGA_NEW_GS are required or not*/
407    (SVGA_NEW_VS |
408     SVGA_NEW_FS |
409     SVGA_NEW_GS |
410     SVGA_NEW_TCS |
411     SVGA_NEW_TES |
412     SVGA_NEW_TEXTURE_BINDING |
413     SVGA_NEW_SAMPLER |
414     SVGA_NEW_RAST),
415    emit_hw_tes
416 };
417