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 "util/u_inlines.h"
27 #include "util/u_memory.h"
28 #include "util/u_bitmask.h"
29 #include "util/u_simple_shaders.h"
30 #include "tgsi/tgsi_point_sprite.h"
31 #include "tgsi/tgsi_dynamic_indexing.h"
32 #include "tgsi/tgsi_vpos.h"
33 #include "tgsi/tgsi_dump.h"
34
35 #include "svga_context.h"
36 #include "svga_shader.h"
37 #include "svga_tgsi.h"
38
39
40 /**
41 * Bind a new GS. This updates the derived current gs state, not the
42 * user-specified GS state.
43 */
44 static void
bind_gs_state(struct svga_context * svga,struct svga_geometry_shader * gs)45 bind_gs_state(struct svga_context *svga,
46 struct svga_geometry_shader *gs)
47 {
48 svga->curr.gs = gs;
49 svga->dirty |= SVGA_NEW_GS;
50 }
51
52
53 static void
insert_at_head(struct svga_shader * head,struct svga_shader * shader)54 insert_at_head(struct svga_shader *head, struct svga_shader *shader)
55 {
56 shader->parent = head;
57 shader->next = head->next;
58 head->next = shader;
59 }
60
61
62 /**
63 * Bind shader
64 */
65 static void
bind_shader(struct svga_context * svga,const enum pipe_shader_type shader_type,struct svga_shader * shader)66 bind_shader(struct svga_context *svga,
67 const enum pipe_shader_type shader_type,
68 struct svga_shader *shader)
69 {
70 switch (shader_type) {
71 case PIPE_SHADER_VERTEX:
72 svga->pipe.bind_vs_state(&svga->pipe, shader);
73 break;
74 case PIPE_SHADER_FRAGMENT:
75 /**
76 * Avoid pipe->bind_fs_state call because it goes through aapoint
77 * layer. We loose linked list of all transformed shaders if aapoint
78 * is used.
79 */
80 svga_bind_fs_state(&svga->pipe, shader);
81 break;
82 case PIPE_SHADER_GEOMETRY:
83 svga->pipe.bind_gs_state(&svga->pipe, shader);
84 break;
85 case PIPE_SHADER_TESS_CTRL:
86 svga->pipe.bind_tcs_state(&svga->pipe, shader);
87 break;
88 case PIPE_SHADER_TESS_EVAL:
89 svga->pipe.bind_tes_state(&svga->pipe, shader);
90 break;
91 default:
92 return;
93 }
94 }
95
96
97
98 /**
99 * Create shader
100 */
101 static void *
create_shader(struct svga_context * svga,const enum pipe_shader_type shader_type,struct pipe_shader_state * state)102 create_shader(struct svga_context *svga,
103 const enum pipe_shader_type shader_type,
104 struct pipe_shader_state *state)
105 {
106 switch (shader_type) {
107 case PIPE_SHADER_VERTEX:
108 return svga->pipe.create_vs_state(&svga->pipe, state);
109 case PIPE_SHADER_FRAGMENT:
110 /**
111 * Avoid pipe->create_fs_state call because it goes through aapoint
112 * layer. We loose linked list of all transformed shaders if aapoint
113 * is used.
114 */
115 return svga_create_fs_state(&svga->pipe, state);
116 case PIPE_SHADER_GEOMETRY:
117 return svga->pipe.create_gs_state(&svga->pipe, state);
118 case PIPE_SHADER_TESS_CTRL:
119 return svga->pipe.create_tcs_state(&svga->pipe, state);
120 case PIPE_SHADER_TESS_EVAL:
121 return svga->pipe.create_tes_state(&svga->pipe, state);
122 default:
123 return NULL;
124 }
125 }
126
127
128 static void
write_vpos(struct svga_context * svga,struct svga_shader * shader)129 write_vpos(struct svga_context *svga,
130 struct svga_shader *shader)
131 {
132 struct svga_token_key key;
133 bool use_existing = false;
134 struct svga_shader *transform_shader;
135 const struct tgsi_shader_info *info = &shader->tgsi_info;
136
137 /* Create a token key */
138 memset(&key, 0, sizeof key);
139 key.vs.write_position = 1;
140
141 if (shader->next) {
142 transform_shader = svga_search_shader_token_key(shader->next, &key);
143 if (transform_shader) {
144 use_existing = true;
145 }
146 }
147
148 if (!use_existing) {
149 struct pipe_shader_state state = {0};
150 struct tgsi_token *new_tokens = NULL;
151
152 new_tokens = tgsi_write_vpos(shader->tokens,
153 info->immediate_count);
154 if (!new_tokens)
155 return;
156
157 pipe_shader_state_from_tgsi(&state, new_tokens);
158
159 transform_shader = create_shader(svga, info->processor, &state);
160 insert_at_head(shader, transform_shader);
161 FREE(new_tokens);
162 }
163 transform_shader->token_key = key;
164 bind_shader(svga, info->processor, transform_shader);
165 }
166
167
168 /**
169 * transform_dynamic_indexing searches shader variant list to see if
170 * we have transformed shader for dynamic indexing and reuse/bind it. If we
171 * don't have transformed shader, then it will create new shader from which
172 * dynamic indexing will be removed. It will also be added to the shader
173 * variant list and this new shader will be bind to current svga state.
174 */
175 static void
transform_dynamic_indexing(struct svga_context * svga,struct svga_shader * shader)176 transform_dynamic_indexing(struct svga_context *svga,
177 struct svga_shader *shader)
178 {
179 struct svga_token_key key;
180 bool use_existing = false;
181 struct svga_shader *transform_shader;
182 const struct tgsi_shader_info *info = &shader->tgsi_info;
183
184 /* Create a token key */
185 memset(&key, 0, sizeof key);
186 key.dynamic_indexing = 1;
187
188 if (shader->next) {
189 transform_shader = svga_search_shader_token_key(shader->next, &key);
190 if (transform_shader) {
191 use_existing = true;
192 }
193 }
194
195 struct tgsi_token *new_tokens = NULL;
196
197 if (!use_existing) {
198 struct pipe_shader_state state = {0};
199 new_tokens = tgsi_remove_dynamic_indexing(shader->tokens,
200 info->const_buffers_declared,
201 info->samplers_declared,
202 info->immediate_count);
203 if (!new_tokens)
204 return;
205
206 pipe_shader_state_from_tgsi(&state, new_tokens);
207
208 transform_shader = create_shader(svga, info->processor, &state);
209 insert_at_head(shader, transform_shader);
210 }
211 transform_shader->token_key = key;
212 bind_shader(svga, info->processor, transform_shader);
213 if (new_tokens)
214 FREE(new_tokens);
215 }
216
217
218 /**
219 * emulate_point_sprite searches the shader variants list to see it there is
220 * a shader variant with a token string that matches the emulation
221 * requirement. It there isn't, then it will use a tgsi utility
222 * tgsi_add_point_sprite to transform the original token string to support
223 * point sprite. A new geometry shader state will be created with the
224 * transformed token string and added to the shader variants list of the
225 * original geometry shader. The new geometry shader state will then be
226 * bound as the current geometry shader.
227 */
228 static struct svga_shader *
emulate_point_sprite(struct svga_context * svga,struct svga_shader * shader,const struct tgsi_token * tokens)229 emulate_point_sprite(struct svga_context *svga,
230 struct svga_shader *shader,
231 const struct tgsi_token *tokens)
232 {
233 struct svga_token_key key;
234 struct tgsi_token *new_tokens;
235 const struct tgsi_token *orig_tokens;
236 struct svga_geometry_shader *orig_gs = (struct svga_geometry_shader *)shader;
237 struct svga_geometry_shader *gs = NULL;
238 struct pipe_shader_state templ = {0};
239 struct svga_stream_output *streamout = NULL;
240 int pos_out_index = -1;
241 int aa_point_coord_index = -1;
242 struct pipe_screen *screen = svga->pipe.screen;
243 bool has_texcoord_semantic =
244 screen->get_param(screen, PIPE_CAP_TGSI_TEXCOORD);
245
246 assert(tokens != NULL);
247
248 orig_tokens = tokens;
249
250 /* Create a token key */
251 memset(&key, 0, sizeof key);
252 key.gs.writes_psize = 1;
253 key.gs.sprite_coord_enable = svga->curr.rast->templ.sprite_coord_enable;
254 if (has_texcoord_semantic)
255 key.gs.sprite_coord_enable |= 0x1; /* For TGSI_SEMANTIC_PCOORD */
256
257 key.gs.sprite_origin_upper_left =
258 !(svga->curr.rast->templ.sprite_coord_mode == PIPE_SPRITE_COORD_LOWER_LEFT);
259
260 key.gs.aa_point = svga->curr.rast->templ.point_smooth;
261
262 if (orig_gs) {
263
264 /* Check if the original geometry shader has stream output and
265 * if position is one of the outputs.
266 */
267 streamout = orig_gs->base.stream_output;
268 if (streamout) {
269 pos_out_index = streamout->pos_out_index;
270 key.gs.point_pos_stream_out = pos_out_index != -1;
271 }
272
273 /* Search the shader lists to see if there is a variant that matches
274 * this token key.
275 */
276 gs = (struct svga_geometry_shader *)
277 svga_search_shader_token_key(&orig_gs->base, &key);
278 }
279
280 /* If there isn't, then call the tgsi utility tgsi_add_point_sprite
281 * to transform the original tokens to support point sprite.
282 * Flip the sprite origin as SVGA3D device only supports an
283 * upper-left origin.
284 */
285 if (!gs) {
286 new_tokens = tgsi_add_point_sprite(orig_tokens,
287 key.gs.sprite_coord_enable,
288 key.gs.sprite_origin_upper_left,
289 key.gs.point_pos_stream_out,
290 has_texcoord_semantic,
291 key.gs.aa_point ?
292 &aa_point_coord_index : NULL);
293
294 if (!new_tokens) {
295 /* if no new tokens are generated for whatever reason, just return */
296 return NULL;
297 }
298
299 if (0) {
300 debug_printf("Before tgsi_add_point_sprite ---------------\n");
301 tgsi_dump(orig_tokens, 0);
302 debug_printf("After tgsi_add_point_sprite --------------\n");
303 tgsi_dump(new_tokens, 0);
304 }
305
306 pipe_shader_state_from_tgsi(&templ, new_tokens);
307 templ.stream_output.num_outputs = 0;
308
309 if (streamout) {
310 templ.stream_output = streamout->info;
311 /* The tgsi_add_point_sprite utility adds an extra output
312 * for the original point position for stream output purpose.
313 * We need to replace the position output register index in the
314 * stream output declaration with the new register index.
315 */
316 if (pos_out_index != -1) {
317 assert(orig_gs != NULL);
318 templ.stream_output.output[pos_out_index].register_index =
319 orig_gs->base.tgsi_info.num_outputs;
320 }
321 }
322
323 /* Create a new geometry shader state with the new tokens */
324 gs = svga->pipe.create_gs_state(&svga->pipe, &templ);
325
326 /* Don't need the token string anymore. There is a local copy
327 * in the shader state.
328 */
329 FREE(new_tokens);
330
331 if (!gs) {
332 return NULL;
333 }
334
335 gs->wide_point = true;
336 gs->aa_point_coord_index = aa_point_coord_index;
337 gs->base.token_key = key;
338 gs->base.parent = &orig_gs->base;
339 gs->base.next = NULL;
340
341 /* Add the new geometry shader to the head of the shader list
342 * pointed to by the original geometry shader.
343 */
344 if (orig_gs) {
345 gs->base.next = orig_gs->base.next;
346 orig_gs->base.next = &gs->base;
347 }
348 }
349
350 /* Bind the new geometry shader state */
351 bind_gs_state(svga, gs);
352
353 return &gs->base;
354 }
355
356 /**
357 * Generate a geometry shader that emits a wide point by drawing a quad.
358 * This function first creates a passthrough geometry shader and then
359 * calls emulate_point_sprite() to transform the geometry shader to
360 * support point sprite.
361 */
362 static struct svga_shader *
add_point_sprite_shader(struct svga_context * svga)363 add_point_sprite_shader(struct svga_context *svga)
364 {
365 struct svga_vertex_shader *vs = svga->curr.vs;
366 struct svga_geometry_shader *orig_gs = vs->gs;
367 struct svga_geometry_shader *new_gs;
368 const struct tgsi_token *tokens;
369
370 if (orig_gs == NULL) {
371
372 /* If this is the first time adding a geometry shader to this
373 * vertex shader to support point sprite, then create
374 * a passthrough geometry shader first.
375 */
376 orig_gs = (struct svga_geometry_shader *)
377 util_make_geometry_passthrough_shader(
378 &svga->pipe, vs->base.tgsi_info.num_outputs,
379 vs->base.tgsi_info.output_semantic_name,
380 vs->base.tgsi_info.output_semantic_index);
381
382 if (!orig_gs)
383 return NULL;
384 }
385 else {
386 if (orig_gs->base.parent)
387 orig_gs = (struct svga_geometry_shader *)orig_gs->base.parent;
388 }
389 tokens = orig_gs->base.tokens;
390
391 /* Call emulate_point_sprite to find or create a transformed
392 * geometry shader for supporting point sprite.
393 */
394 new_gs = (struct svga_geometry_shader *)
395 emulate_point_sprite(svga, &orig_gs->base, tokens);
396
397 /* If this is the first time creating a geometry shader to
398 * support vertex point size, then add the new geometry shader
399 * to the vertex shader.
400 */
401 if (vs->gs == NULL) {
402 vs->gs = new_gs;
403 }
404
405 return &new_gs->base;
406 }
407
408
409 static bool
has_dynamic_indexing(const struct tgsi_shader_info * info)410 has_dynamic_indexing(const struct tgsi_shader_info *info)
411 {
412 return (info->dim_indirect_files & (1u << TGSI_FILE_CONSTANT)) ||
413 (info->indirect_files & (1u << TGSI_FILE_SAMPLER));
414 }
415
416
417 /* update_tgsi_transform provides a hook to transform a shader if needed.
418 */
419 static enum pipe_error
update_tgsi_transform(struct svga_context * svga,uint64_t dirty)420 update_tgsi_transform(struct svga_context *svga, uint64_t dirty)
421 {
422 struct svga_geometry_shader *gs = svga->curr.user_gs; /* current gs */
423 struct svga_vertex_shader *vs = svga->curr.vs; /* currently bound vs */
424 struct svga_fragment_shader *fs = svga->curr.fs; /* currently bound fs */
425 struct svga_tcs_shader *tcs = svga->curr.tcs; /* currently bound tcs */
426 struct svga_tes_shader *tes = svga->curr.tes; /* currently bound tes */
427 struct svga_shader *orig_gs; /* original gs */
428 struct svga_shader *new_gs; /* new gs */
429
430 assert(svga_have_vgpu10(svga));
431
432 if (vs->base.tgsi_info.num_outputs == 0) {
433 write_vpos(svga, &vs->base);
434 }
435
436 if (vs && has_dynamic_indexing(&vs->base.tgsi_info)) {
437 transform_dynamic_indexing(svga, &vs->base);
438 }
439 if (fs && has_dynamic_indexing(&fs->base.tgsi_info)) {
440 transform_dynamic_indexing(svga, &fs->base);
441 }
442 if (gs && has_dynamic_indexing(&gs->base.tgsi_info)) {
443 transform_dynamic_indexing(svga, &gs->base);
444 }
445 if (tcs && has_dynamic_indexing(&tcs->base.tgsi_info)) {
446 transform_dynamic_indexing(svga, &tcs->base);
447 }
448 if (tes && has_dynamic_indexing(&tes->base.tgsi_info)) {
449 transform_dynamic_indexing(svga, &tes->base);
450 }
451
452 if (svga->curr.reduced_prim == MESA_PRIM_POINTS) {
453 /* If the current prim type is POINTS and the current geometry shader
454 * emits wide points, transform the shader to emulate wide points using
455 * quads. NOTE: we don't do emulation of wide points in GS when
456 * transform feedback is enabled.
457 */
458 if (gs != NULL && !gs->base.stream_output &&
459 (gs->base.tgsi_info.writes_psize || gs->wide_point)) {
460 orig_gs = gs->base.parent ? gs->base.parent : &gs->base;
461 new_gs = emulate_point_sprite(svga, orig_gs, orig_gs->tokens);
462 }
463
464 /* If there is not an active geometry shader and the current vertex
465 * shader emits wide point then create a new geometry shader to emulate
466 * wide point.
467 */
468 else if (gs == NULL && !vs->base.stream_output &&
469 (svga->curr.rast->pointsize > 1.0 ||
470 vs->base.tgsi_info.writes_psize)) {
471 new_gs = add_point_sprite_shader(svga);
472 }
473 else {
474 /* use the user's GS */
475 bind_gs_state(svga, svga->curr.user_gs);
476 }
477 }
478 else if (svga->curr.gs != svga->curr.user_gs) {
479 /* If current primitive type is not POINTS, then make sure
480 * we don't bind to any of the generated geometry shader
481 */
482 bind_gs_state(svga, svga->curr.user_gs);
483 }
484 (void) new_gs; /* silence the unused var warning */
485
486 return PIPE_OK;
487 }
488
489 struct svga_tracked_state svga_need_tgsi_transform =
490 {
491 "transform shader for optimization",
492 (SVGA_NEW_VS |
493 SVGA_NEW_FS |
494 SVGA_NEW_GS |
495 SVGA_NEW_REDUCED_PRIMITIVE |
496 SVGA_NEW_RAST),
497 update_tgsi_transform
498 };
499