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