• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**********************************************************
2  * Copyright 2008-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 "draw/draw_context.h"
27 #include "nir/nir_to_tgsi.h"
28 #include "util/u_inlines.h"
29 #include "util/u_math.h"
30 #include "util/u_memory.h"
31 #include "util/u_bitmask.h"
32 
33 #include "svga_context.h"
34 #include "svga_hw_reg.h"
35 #include "svga_cmd.h"
36 #include "svga_debug.h"
37 #include "svga_shader.h"
38 #include "svga_streamout.h"
39 
40 
41 static void *
svga_create_vs_state(struct pipe_context * pipe,const struct pipe_shader_state * templ)42 svga_create_vs_state(struct pipe_context *pipe,
43                      const struct pipe_shader_state *templ)
44 {
45    struct svga_context *svga = svga_context(pipe);
46    struct svga_vertex_shader *vs;
47 
48    SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_CREATEVS);
49 
50    vs = (struct svga_vertex_shader *)
51             svga_create_shader(pipe, templ, PIPE_SHADER_VERTEX,
52                                sizeof(struct svga_vertex_shader));
53    if (!vs)
54       goto done;
55 
56    vs->base.get_dummy_shader = svga_get_compiled_dummy_vertex_shader;
57 
58    {
59       /* Need to do construct a new template in case we substituted a
60        * debug shader.
61        */
62       struct pipe_shader_state tmp2 = *templ;
63 
64       /* shader IR has been converted to tgsi */
65       tmp2.type = PIPE_SHADER_IR_TGSI;
66       tmp2.tokens = vs->base.tokens;
67       vs->draw_shader = draw_create_vertex_shader(svga->swtnl.draw, &tmp2);
68    }
69 
70 done:
71    SVGA_STATS_TIME_POP(svga_sws(svga));
72    return vs;
73 }
74 
75 
76 static void
svga_bind_vs_state(struct pipe_context * pipe,void * shader)77 svga_bind_vs_state(struct pipe_context *pipe, void *shader)
78 {
79    struct svga_vertex_shader *vs = (struct svga_vertex_shader *)shader;
80    struct svga_context *svga = svga_context(pipe);
81 
82    if (vs == svga->curr.vs)
83       return;
84 
85    /* If the currently bound vertex shader has a generated geometry shader,
86     * then unbind the geometry shader before binding a new vertex shader.
87     * We need to unbind the geometry shader here because there is no
88     * pipe_shader associated with the generated geometry shader.
89     */
90    if (svga->curr.vs != NULL && svga->curr.vs->gs != NULL)
91       svga->pipe.bind_gs_state(&svga->pipe, NULL);
92 
93    svga->curr.vs = vs;
94    svga->dirty |= SVGA_NEW_VS;
95 
96    /* Check if the shader uses samplers */
97    svga_set_curr_shader_use_samplers_flag(svga, PIPE_SHADER_VERTEX,
98                                           svga_shader_use_samplers(&vs->base));
99 }
100 
101 
102 static void
svga_delete_vs_state(struct pipe_context * pipe,void * shader)103 svga_delete_vs_state(struct pipe_context *pipe, void *shader)
104 {
105    struct svga_context *svga = svga_context(pipe);
106    struct svga_vertex_shader *vs = (struct svga_vertex_shader *)shader;
107    struct svga_vertex_shader *next_vs;
108    struct svga_shader_variant *variant, *tmp;
109 
110    svga_hwtnl_flush_retry(svga);
111 
112    assert(vs->base.parent == NULL);
113 
114    while (vs) {
115       next_vs = (struct svga_vertex_shader *)vs->base.next;
116 
117       /* Check if there is a generated geometry shader to go with this
118        * vertex shader. If there is, then delete the geometry shader as well.
119        */
120       if (vs->gs != NULL) {
121          svga->pipe.delete_gs_state(&svga->pipe, vs->gs);
122       }
123 
124       if (vs->base.stream_output != NULL)
125          svga_delete_stream_output(svga, vs->base.stream_output);
126 
127       draw_delete_vertex_shader(svga->swtnl.draw, vs->draw_shader);
128 
129       for (variant = vs->base.variants; variant; variant = tmp) {
130          tmp = variant->next;
131 
132          /* Check if deleting currently bound shader */
133          if (variant == svga->state.hw_draw.vs) {
134             SVGA_RETRY(svga, svga_set_shader(svga, SVGA3D_SHADERTYPE_VS, NULL));
135             svga->state.hw_draw.vs = NULL;
136          }
137 
138          svga_destroy_shader_variant(svga, variant);
139       }
140 
141       FREE((void *)vs->base.tokens);
142       FREE(vs);
143       vs = next_vs;
144    }
145 }
146 
147 
148 void
svga_init_vs_functions(struct svga_context * svga)149 svga_init_vs_functions(struct svga_context *svga)
150 {
151    svga->pipe.create_vs_state = svga_create_vs_state;
152    svga->pipe.bind_vs_state = svga_bind_vs_state;
153    svga->pipe.delete_vs_state = svga_delete_vs_state;
154 }
155 
156