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