1 /**********************************************************
2 * Copyright 2014 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 static struct svga_shader_variant *
translate_geometry_program(struct svga_context * svga,const struct svga_geometry_shader * gs,const struct svga_compile_key * key)52 translate_geometry_program(struct svga_context *svga,
53 const struct svga_geometry_shader *gs,
54 const struct svga_compile_key *key)
55 {
56 assert(svga_have_vgpu10(svga));
57 return svga_tgsi_vgpu10_translate(svga, &gs->base, key,
58 PIPE_SHADER_GEOMETRY);
59 }
60
61
62 /**
63 * Translate TGSI shader into an svga shader variant.
64 */
65 static enum pipe_error
compile_gs(struct svga_context * svga,struct svga_geometry_shader * gs,const struct svga_compile_key * key,struct svga_shader_variant ** out_variant)66 compile_gs(struct svga_context *svga,
67 struct svga_geometry_shader *gs,
68 const struct svga_compile_key *key,
69 struct svga_shader_variant **out_variant)
70 {
71 struct svga_shader_variant *variant;
72 enum pipe_error ret = PIPE_ERROR;
73
74 variant = translate_geometry_program(svga, gs, key);
75 if (!variant) {
76 /* some problem during translation, try the dummy shader */
77 const struct tgsi_token *dummy = get_dummy_geometry_shader();
78 if (!dummy) {
79 return PIPE_ERROR_OUT_OF_MEMORY;
80 }
81 debug_printf("Failed to compile geometry shader, using dummy shader instead.\n");
82 FREE((void *) gs->base.tokens);
83 gs->base.tokens = dummy;
84 variant = translate_geometry_program(svga, gs, key);
85 if (!variant) {
86 return PIPE_ERROR;
87 }
88 }
89
90 ret = svga_define_shader(svga, variant);
91 if (ret != PIPE_OK) {
92 svga_destroy_shader_variant(svga, variant);
93 return ret;
94 }
95
96 *out_variant = variant;
97
98 return PIPE_OK;
99 }
100
101
102 static void
make_gs_key(struct svga_context * svga,struct svga_compile_key * key)103 make_gs_key(struct svga_context *svga, struct svga_compile_key *key)
104 {
105 struct svga_geometry_shader *gs = svga->curr.gs;
106
107 memset(key, 0, sizeof *key);
108
109 /*
110 * SVGA_NEW_TEXTURE_BINDING | SVGA_NEW_SAMPLER
111 */
112 svga_init_shader_key_common(svga, PIPE_SHADER_GEOMETRY, &gs->base, key);
113
114 memcpy(key->generic_remap_table, gs->generic_remap_table,
115 sizeof(gs->generic_remap_table));
116
117 key->gs.vs_generic_outputs = svga->curr.vs->generic_outputs;
118
119 key->gs.need_prescale = svga->state.hw_clear.prescale[0].enabled;
120
121 key->gs.writes_psize = gs->base.info.writes_psize;
122 key->gs.wide_point = gs->wide_point;
123 key->gs.writes_viewport_index = gs->base.info.writes_viewport_index;
124 if (key->gs.writes_viewport_index) {
125 key->gs.num_prescale = svga->state.hw_clear.num_prescale;
126 } else {
127 key->gs.num_prescale = 1;
128 }
129 key->sprite_coord_enable = svga->curr.rast->templ.sprite_coord_enable;
130 key->sprite_origin_lower_left = (svga->curr.rast->templ.sprite_coord_mode
131 == PIPE_SPRITE_COORD_LOWER_LEFT);
132
133 /* SVGA_NEW_RAST */
134 key->clip_plane_enable = svga->curr.rast->templ.clip_plane_enable;
135
136 /* Mark this as the last shader in the vertex processing stage */
137 key->last_vertex_stage = 1;
138 }
139
140
141 static enum pipe_error
emit_hw_gs(struct svga_context * svga,uint64_t dirty)142 emit_hw_gs(struct svga_context *svga, uint64_t dirty)
143 {
144 struct svga_shader_variant *variant;
145 struct svga_geometry_shader *gs = svga->curr.gs;
146 enum pipe_error ret = PIPE_OK;
147 struct svga_compile_key key;
148
149 assert(svga_have_vgpu10(svga));
150
151 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_EMITGS);
152
153 /* If there's a user-defined GS, we should have a pointer to a derived
154 * GS. This should have been resolved in update_tgsi_transform().
155 */
156 if (svga->curr.user_gs)
157 assert(svga->curr.gs);
158
159 if (!gs) {
160 if (svga->state.hw_draw.gs != NULL) {
161
162 /** The previous geometry shader is made inactive.
163 * Needs to unbind the geometry shader.
164 */
165 ret = svga_set_shader(svga, SVGA3D_SHADERTYPE_GS, NULL);
166 if (ret != PIPE_OK)
167 goto done;
168 svga->state.hw_draw.gs = NULL;
169 }
170 goto done;
171 }
172
173 /* If there is stream output info for this geometry shader, then use
174 * it instead of the one from the vertex shader.
175 */
176 if (svga_have_gs_streamout(svga)) {
177 ret = svga_set_stream_output(svga, gs->base.stream_output);
178 if (ret != PIPE_OK) {
179 goto done;
180 }
181 }
182 else if (!svga_have_vs_streamout(svga)) {
183 /* turn off stream out */
184 ret = svga_set_stream_output(svga, NULL);
185 if (ret != PIPE_OK) {
186 goto done;
187 }
188 }
189
190 /* SVGA_NEW_NEED_SWTNL */
191 if (svga->state.sw.need_swtnl && !svga_have_vgpu10(svga)) {
192 /* No geometry shader is needed */
193 variant = NULL;
194 }
195 else {
196 make_gs_key(svga, &key);
197
198 /* See if we already have a GS variant that matches the key */
199 variant = svga_search_shader_key(&gs->base, &key);
200
201 if (!variant) {
202 ret = compile_gs(svga, gs, &key, &variant);
203 if (ret != PIPE_OK)
204 goto done;
205
206 /* insert the new variant at head of linked list */
207 assert(variant);
208 variant->next = gs->base.variants;
209 gs->base.variants = variant;
210 }
211 }
212
213 if (variant != svga->state.hw_draw.gs) {
214 /* Bind the new variant */
215 ret = svga_set_shader(svga, SVGA3D_SHADERTYPE_GS, variant);
216 if (ret != PIPE_OK)
217 goto done;
218
219 svga->rebind.flags.gs = FALSE;
220 svga->dirty |= SVGA_NEW_GS_VARIANT;
221 svga->state.hw_draw.gs = variant;
222 }
223
224 done:
225 SVGA_STATS_TIME_POP(svga_sws(svga));
226 return ret;
227 }
228
229 struct svga_tracked_state svga_hw_gs =
230 {
231 "geometry shader (hwtnl)",
232 (SVGA_NEW_VS |
233 SVGA_NEW_FS |
234 SVGA_NEW_GS |
235 SVGA_NEW_TEXTURE_BINDING |
236 SVGA_NEW_SAMPLER |
237 SVGA_NEW_RAST |
238 SVGA_NEW_NEED_SWTNL),
239 emit_hw_gs
240 };
241