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 "translate/translate.h"
30 #include "tgsi/tgsi_ureg.h"
31
32 #include "svga_context.h"
33 #include "svga_cmd.h"
34 #include "svga_shader.h"
35 #include "svga_tgsi.h"
36 #include "svga_streamout.h"
37 #include "svga_format.h"
38
39 /**
40 * If we fail to compile a geometry shader we'll use a dummy/fallback shader
41 * that simply emits the incoming vertices.
42 */
43 static const struct tgsi_token *
get_dummy_geometry_shader(void)44 get_dummy_geometry_shader(void)
45 {
46 //XXX
47 return NULL;
48 }
49
50
51 struct svga_shader_variant *
svga_get_compiled_dummy_geometry_shader(struct svga_context * svga,struct svga_shader * shader,const struct svga_compile_key * key)52 svga_get_compiled_dummy_geometry_shader(struct svga_context *svga,
53 struct svga_shader *shader,
54 const struct svga_compile_key *key)
55 {
56 const struct tgsi_token *dummy = get_dummy_geometry_shader();
57 struct svga_shader_variant *variant;
58 struct svga_geometry_shader *gs = (struct svga_geometry_shader *)shader;
59
60 if (!dummy)
61 return NULL;
62
63 FREE((void *) gs->base.tokens);
64 gs->base.tokens = dummy;
65 svga_tgsi_scan_shader(&gs->base);
66 variant = svga_tgsi_compile_shader(svga, shader, key);
67
68 return variant;
69 }
70
71
72 static void
make_gs_key(struct svga_context * svga,struct svga_compile_key * key)73 make_gs_key(struct svga_context *svga, struct svga_compile_key *key)
74 {
75 struct svga_geometry_shader *gs = svga->curr.gs;
76
77 memset(key, 0, sizeof *key);
78
79 /*
80 * SVGA_NEW_TEXTURE_BINDING | SVGA_NEW_SAMPLER
81 */
82 svga_init_shader_key_common(svga, PIPE_SHADER_GEOMETRY, &gs->base, key);
83
84 memcpy(key->generic_remap_table, gs->generic_remap_table,
85 sizeof(gs->generic_remap_table));
86
87 key->gs.vs_generic_outputs = svga->curr.vs->base.info.generic_outputs_mask;
88
89 key->gs.need_prescale = svga->state.hw_clear.prescale[0].enabled;
90
91 key->gs.writes_psize = gs->base.info.writes_psize;
92 key->gs.wide_point = gs->wide_point;
93 key->gs.writes_viewport_index = gs->base.info.writes_viewport_index;
94 if (key->gs.writes_viewport_index) {
95 key->gs.num_prescale = svga->state.hw_clear.num_prescale;
96 } else {
97 key->gs.num_prescale = 1;
98 }
99 key->sprite_coord_enable = svga->curr.rast->templ.sprite_coord_enable;
100 key->sprite_origin_lower_left = (svga->curr.rast->templ.sprite_coord_mode
101 == PIPE_SPRITE_COORD_LOWER_LEFT);
102
103 /* SVGA_NEW_RAST */
104 key->clip_plane_enable = svga->curr.rast->templ.clip_plane_enable;
105
106 /* Mark this as the last shader in the vertex processing stage */
107 key->last_vertex_stage = 1;
108 }
109
110
111 static enum pipe_error
emit_hw_gs(struct svga_context * svga,uint64_t dirty)112 emit_hw_gs(struct svga_context *svga, uint64_t dirty)
113 {
114 struct svga_shader_variant *variant;
115 struct svga_geometry_shader *gs = svga->curr.gs;
116 enum pipe_error ret = PIPE_OK;
117 struct svga_compile_key key;
118
119 assert(svga_have_vgpu10(svga));
120
121 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_EMITGS);
122
123 /* If there's a user-defined GS, we should have a pointer to a derived
124 * GS. This should have been resolved in update_tgsi_transform().
125 */
126 if (svga->curr.user_gs)
127 assert(svga->curr.gs);
128
129 if (!gs) {
130 if (svga->state.hw_draw.gs != NULL) {
131
132 /** The previous geometry shader is made inactive.
133 * Needs to unbind the geometry shader.
134 */
135 ret = svga_set_shader(svga, SVGA3D_SHADERTYPE_GS, NULL);
136 if (ret != PIPE_OK)
137 goto done;
138 svga->state.hw_draw.gs = NULL;
139 }
140 goto done;
141 }
142
143 /* If there is stream output info for this geometry shader, then use
144 * it instead of the one from the vertex shader.
145 */
146 if (svga_have_gs_streamout(svga)) {
147 ret = svga_set_stream_output(svga, gs->base.stream_output);
148 if (ret != PIPE_OK) {
149 goto done;
150 }
151 }
152 else if (!svga_have_vs_streamout(svga)) {
153 /* turn off stream out */
154 ret = svga_set_stream_output(svga, NULL);
155 if (ret != PIPE_OK) {
156 goto done;
157 }
158 }
159
160 /* SVGA_NEW_NEED_SWTNL */
161 if (svga->state.sw.need_swtnl && !svga_have_vgpu10(svga)) {
162 /* No geometry shader is needed */
163 variant = NULL;
164 }
165 else {
166 make_gs_key(svga, &key);
167
168 /* See if we already have a GS variant that matches the key */
169 variant = svga_search_shader_key(&gs->base, &key);
170
171 if (!variant) {
172 ret = svga_compile_shader(svga, &gs->base, &key, &variant);
173 if (ret != PIPE_OK)
174 goto done;
175 }
176 }
177
178 if (variant != svga->state.hw_draw.gs) {
179 /* Bind the new variant */
180 ret = svga_set_shader(svga, SVGA3D_SHADERTYPE_GS, variant);
181 if (ret != PIPE_OK)
182 goto done;
183
184 svga->rebind.flags.gs = FALSE;
185 svga->dirty |= SVGA_NEW_GS_VARIANT;
186 svga->state.hw_draw.gs = variant;
187 }
188
189 done:
190 SVGA_STATS_TIME_POP(svga_sws(svga));
191 return ret;
192 }
193
194 struct svga_tracked_state svga_hw_gs =
195 {
196 "geometry shader (hwtnl)",
197 (SVGA_NEW_VS |
198 SVGA_NEW_FS |
199 SVGA_NEW_GS |
200 SVGA_NEW_TEXTURE_BINDING |
201 SVGA_NEW_SAMPLER |
202 SVGA_NEW_RAST |
203 SVGA_NEW_NEED_SWTNL |
204 SVGA_NEW_GS_RAW_BUFFER),
205 emit_hw_gs
206 };
207