1 /*
2 * Copyright (c) 2008-2024 Broadcom. All Rights Reserved.
3 * The term “Broadcom” refers to Broadcom Inc.
4 * and/or its subsidiaries.
5 * SPDX-License-Identifier: MIT
6 */
7
8 #include "draw/draw_context.h"
9 #include "nir/nir_to_tgsi.h"
10 #include "util/u_inlines.h"
11 #include "util/u_math.h"
12 #include "util/u_memory.h"
13 #include "util/u_bitmask.h"
14
15 #include "svga_context.h"
16 #include "svga_hw_reg.h"
17 #include "svga_cmd.h"
18 #include "svga_debug.h"
19 #include "svga_shader.h"
20 #include "svga_streamout.h"
21
22
23 static void *
svga_create_vs_state(struct pipe_context * pipe,const struct pipe_shader_state * templ)24 svga_create_vs_state(struct pipe_context *pipe,
25 const struct pipe_shader_state *templ)
26 {
27 struct svga_context *svga = svga_context(pipe);
28 struct svga_vertex_shader *vs;
29
30 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_CREATEVS);
31
32 vs = (struct svga_vertex_shader *)
33 svga_create_shader(pipe, templ, PIPE_SHADER_VERTEX,
34 sizeof(struct svga_vertex_shader));
35 if (!vs)
36 goto done;
37
38 vs->base.get_dummy_shader = svga_get_compiled_dummy_vertex_shader;
39
40 {
41 /* Need to do construct a new template in case we substituted a
42 * debug shader.
43 */
44 struct pipe_shader_state tmp2 = *templ;
45
46 /* shader IR has been converted to tgsi */
47 tmp2.type = PIPE_SHADER_IR_TGSI;
48 tmp2.tokens = vs->base.tokens;
49 vs->draw_shader = draw_create_vertex_shader(svga->swtnl.draw, &tmp2);
50 }
51
52 done:
53 SVGA_STATS_TIME_POP(svga_sws(svga));
54 return vs;
55 }
56
57
58 static void
svga_bind_vs_state(struct pipe_context * pipe,void * shader)59 svga_bind_vs_state(struct pipe_context *pipe, void *shader)
60 {
61 struct svga_vertex_shader *vs = (struct svga_vertex_shader *)shader;
62 struct svga_context *svga = svga_context(pipe);
63
64 if (vs == svga->curr.vs)
65 return;
66
67 /* If the currently bound vertex shader has a generated geometry shader,
68 * then unbind the geometry shader before binding a new vertex shader.
69 * We need to unbind the geometry shader here because there is no
70 * pipe_shader associated with the generated geometry shader.
71 */
72 if (svga->curr.vs != NULL && svga->curr.vs->gs != NULL)
73 svga->pipe.bind_gs_state(&svga->pipe, NULL);
74
75 svga->curr.vs = vs;
76 svga->dirty |= SVGA_NEW_VS;
77
78 /* Check if the shader uses samplers */
79 svga_set_curr_shader_use_samplers_flag(svga, PIPE_SHADER_VERTEX,
80 svga_shader_use_samplers(&vs->base));
81 }
82
83
84 static void
svga_delete_vs_state(struct pipe_context * pipe,void * shader)85 svga_delete_vs_state(struct pipe_context *pipe, void *shader)
86 {
87 struct svga_context *svga = svga_context(pipe);
88 struct svga_vertex_shader *vs = (struct svga_vertex_shader *)shader;
89 struct svga_vertex_shader *next_vs;
90 struct svga_shader_variant *variant, *tmp;
91
92 svga_hwtnl_flush_retry(svga);
93
94 assert(vs->base.parent == NULL);
95
96 while (vs) {
97 next_vs = (struct svga_vertex_shader *)vs->base.next;
98
99 /* Check if there is a generated geometry shader to go with this
100 * vertex shader. If there is, then delete the geometry shader as well.
101 */
102 if (vs->gs != NULL) {
103 svga->pipe.delete_gs_state(&svga->pipe, vs->gs);
104 }
105
106 if (vs->base.stream_output != NULL)
107 svga_delete_stream_output(svga, vs->base.stream_output);
108
109 draw_delete_vertex_shader(svga->swtnl.draw, vs->draw_shader);
110
111 for (variant = vs->base.variants; variant; variant = tmp) {
112 tmp = variant->next;
113
114 /* Check if deleting currently bound shader */
115 if (variant == svga->state.hw_draw.vs) {
116 SVGA_RETRY(svga, svga_set_shader(svga, SVGA3D_SHADERTYPE_VS, NULL));
117 svga->state.hw_draw.vs = NULL;
118 }
119
120 svga_destroy_shader_variant(svga, variant);
121 }
122
123 FREE((void *)vs->base.tokens);
124 FREE(vs);
125 vs = next_vs;
126 }
127 }
128
129
130 void
svga_init_vs_functions(struct svga_context * svga)131 svga_init_vs_functions(struct svga_context *svga)
132 {
133 svga->pipe.create_vs_state = svga_create_vs_state;
134 svga->pipe.bind_vs_state = svga_bind_vs_state;
135 svga->pipe.delete_vs_state = svga_delete_vs_state;
136 }
137
138