1 /**********************************************************
2 * Copyright 2018-2020 VMware, Inc. All rights reserved.
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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
24 **********************************************************/
26 #include "util/u_inlines.h"
27 #include "util/u_memory.h"
28 #include "util/u_simple_shaders.h"
30 #include "svga_context.h"
32 #include "svga_tgsi.h"
33 #include "svga_shader.h"
37 * Translate TGSI shader into an svga shader variant.
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
)
45 struct svga_shader_variant
*variant
;
46 enum pipe_error ret
= PIPE_ERROR
;
48 variant
= svga_tgsi_vgpu10_translate(svga
, &tcs
->base
, key
,
49 PIPE_SHADER_TESS_CTRL
);
53 ret
= svga_define_shader(svga
, variant
);
55 svga_destroy_shader_variant(svga
, variant
);
59 *out_variant
= variant
;
66 make_tcs_key(struct svga_context
*svga
, struct svga_compile_key
*key
)
68 struct svga_tcs_shader
*tcs
= svga
->curr
.tcs
;
70 memset(key
, 0, sizeof *key
);
73 * SVGA_NEW_TEXTURE_BINDING | SVGA_NEW_SAMPLER
75 svga_init_shader_key_common(svga
, PIPE_SHADER_TESS_CTRL
, &tcs
->base
, key
);
77 /* SVGA_NEW_TCS_PARAM */
78 key
->tcs
.vertices_per_patch
= svga
->curr
.vertices_per_patch
;
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
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
;
92 if (svga
->tcs
.passthrough
)
93 key
->tcs
.passthrough
= 1;
95 key
->clip_plane_enable
= svga
->curr
.rast
->templ
.clip_plane_enable
;
97 /* tcs is always followed by tes */
98 key
->last_vertex_stage
= 0;
102 static enum pipe_error
103 emit_hw_tcs(struct svga_context
*svga
, uint64_t dirty
)
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
;
110 assert(svga_have_sm5(svga
));
112 SVGA_STATS_TIME_PUSH(svga_sws(svga
), SVGA_STATS_TIME_EMITTCS
);
115 /* If there is no active tcs, then there should not be
118 assert(!svga
->curr
.tes
);
119 if (svga
->state
.hw_draw
.tcs
!= NULL
) {
121 /** The previous tessellation control shader is made inactive.
122 * Needs to unbind the tessellation control shader.
124 ret
= svga_set_shader(svga
, SVGA3D_SHADERTYPE_HS
, NULL
);
127 svga
->state
.hw_draw
.tcs
= NULL
;
132 make_tcs_key(svga
, &key
);
134 /* See if we already have a TCS variant that matches the key */
135 variant
= svga_search_shader_key(&tcs
->base
, &key
);
138 ret
= compile_tcs(svga
, tcs
, &key
, &variant
);
142 /* insert the new variant at head of linked list */
144 variant
->next
= tcs
->base
.variants
;
145 tcs
->base
.variants
= variant
;
148 if (variant
!= svga
->state
.hw_draw
.tcs
) {
149 /* Bind the new variant */
150 ret
= svga_set_shader(svga
, SVGA3D_SHADERTYPE_HS
, variant
);
154 svga
->rebind
.flags
.tcs
= FALSE
;
155 svga
->dirty
|= SVGA_NEW_TCS_VARIANT
;
156 svga
->state
.hw_draw
.tcs
= variant
;
160 SVGA_STATS_TIME_POP(svga_sws(svga
));
165 struct svga_tracked_state svga_hw_tcs
=
167 "tessellation control shader (hwtnl)",
171 SVGA_NEW_TEXTURE_BINDING
|
179 * Translate TGSI shader into an svga shader variant.
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
)
187 struct svga_shader_variant
*variant
;
188 enum pipe_error ret
= PIPE_ERROR
;
190 variant
= svga_tgsi_vgpu10_translate(svga
, &tes
->base
, key
,
191 PIPE_SHADER_TESS_EVAL
);
195 ret
= svga_define_shader(svga
, variant
);
196 if (ret
!= PIPE_OK
) {
197 svga_destroy_shader_variant(svga
, variant
);
201 *out_variant
= variant
;
208 make_tes_key(struct svga_context
*svga
, struct svga_compile_key
*key
)
210 struct svga_tes_shader
*tes
= svga
->curr
.tes
;
212 memset(key
, 0, sizeof *key
);
215 * SVGA_NEW_TEXTURE_BINDING | SVGA_NEW_SAMPLER
217 svga_init_shader_key_common(svga
, PIPE_SHADER_TESS_EVAL
, &tes
->base
, key
);
219 assert(svga
->curr
.tcs
);
220 key
->tes
.vertices_per_patch
=
221 svga
->curr
.tcs
->base
.info
.properties
[TGSI_PROPERTY_TCS_VERTICES_OUT
];
223 key
->tes
.need_prescale
= svga
->state
.hw_clear
.prescale
[0].enabled
&&
224 (svga
->curr
.gs
== NULL
);
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.
232 key
->tes
.tessfactor_index
= svga
->curr
.tcs
->base
.info
.num_outputs
;
234 key
->clip_plane_enable
= svga
->curr
.rast
->templ
.clip_plane_enable
;
236 /* This is the last vertex stage if there is no geometry shader. */
237 key
->last_vertex_stage
= !svga
->curr
.gs
;
239 key
->tes
.need_tessinner
= 0;
240 key
->tes
.need_tessouter
= 0;
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;
247 case TGSI_SEMANTIC_TESSINNER
:
248 key
->tes
.need_tessinner
= 1;
259 get_passthrough_tcs(struct svga_context
*svga
)
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
);
269 struct svga_tcs_shader
*new_tcs
;
271 /* delete older passthrough shader*/
272 if (svga
->tcs
.passthrough_tcs
) {
273 svga
->pipe
.delete_tcs_state(&svga
->pipe
,
274 svga
->tcs
.passthrough_tcs
);
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
;
293 struct pipe_constant_buffer cb
;
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
);
303 static enum pipe_error
304 emit_hw_tes(struct svga_context
*svga
, uint64_t dirty
)
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
;
311 assert(svga_have_sm5(svga
));
313 SVGA_STATS_TIME_PUSH(svga_sws(svga
), SVGA_STATS_TIME_EMITTES
);
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
320 assert(!svga
->curr
.tcs
);
321 if (svga
->state
.hw_draw
.tes
!= NULL
) {
323 /** The previous tessellation evaluation shader is made inactive.
324 * Needs to unbind the tessellation evaluation shader.
326 ret
= svga_set_shader(svga
, SVGA3D_SHADERTYPE_DS
, NULL
);
329 svga
->state
.hw_draw
.tes
= NULL
;
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.
339 get_passthrough_tcs(svga
);
340 svga
->tcs
.passthrough
= TRUE
;
343 svga
->tcs
.passthrough
= FALSE
;
346 make_tes_key(svga
, &key
);
348 /* See if we already have a TES variant that matches the key */
349 variant
= svga_search_shader_key(&tes
->base
, &key
);
352 ret
= compile_tes(svga
, tes
, &key
, &variant
);
356 /* insert the new variant at head of linked list */
358 variant
->next
= tes
->base
.variants
;
359 tes
->base
.variants
= variant
;
362 if (variant
!= svga
->state
.hw_draw
.tes
) {
363 /* Bind the new variant */
364 ret
= svga_set_shader(svga
, SVGA3D_SHADERTYPE_DS
, variant
);
368 svga
->rebind
.flags
.tes
= FALSE
;
369 svga
->dirty
|= SVGA_NEW_TES_VARIANT
;
370 svga
->state
.hw_draw
.tes
= variant
;
374 SVGA_STATS_TIME_POP(svga_sws(svga
));
379 struct svga_tracked_state svga_hw_tes
=
381 "tessellation evaluation shader (hwtnl)",
382 /* TBD SVGA_NEW_VS/SVGA_NEW_FS/SVGA_NEW_GS are required or not*/
388 SVGA_NEW_TEXTURE_BINDING
|