1 /**********************************************************
2 * Copyright 2014 VMware, Inc. All rights reserved.
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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
24 **********************************************************/
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"
32 #include "svga_context.h"
34 #include "svga_shader.h"
35 #include "svga_tgsi.h"
36 #include "svga_streamout.h"
37 #include "svga_format.h"
40 * If we fail to compile a geometry shader we'll use a dummy/fallback shader
41 * that simply emits the incoming vertices.
43 static const struct tgsi_token
*
44 get_dummy_geometry_shader(void)
51 static struct svga_shader_variant
*
52 translate_geometry_program(struct svga_context
*svga
,
53 const struct svga_geometry_shader
*gs
,
54 const struct svga_compile_key
*key
)
56 assert(svga_have_vgpu10(svga
));
57 return svga_tgsi_vgpu10_translate(svga
, &gs
->base
, key
,
58 PIPE_SHADER_GEOMETRY
);
63 * Translate TGSI shader into an svga shader variant.
65 static enum pipe_error
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
)
71 struct svga_shader_variant
*variant
;
72 enum pipe_error ret
= PIPE_ERROR
;
74 variant
= translate_geometry_program(svga
, gs
, key
);
76 /* some problem during translation, try the dummy shader */
77 const struct tgsi_token
*dummy
= get_dummy_geometry_shader();
79 return PIPE_ERROR_OUT_OF_MEMORY
;
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
);
90 ret
= svga_define_shader(svga
, SVGA3D_SHADERTYPE_GS
, variant
);
92 svga_destroy_shader_variant(svga
, SVGA3D_SHADERTYPE_GS
, variant
);
96 *out_variant
= variant
;
103 make_gs_key(struct svga_context
*svga
, struct svga_compile_key
*key
)
105 struct svga_geometry_shader
*gs
= svga
->curr
.gs
;
107 memset(key
, 0, sizeof *key
);
110 * SVGA_NEW_TEXTURE_BINDING | SVGA_NEW_SAMPLER
112 svga_init_shader_key_common(svga
, PIPE_SHADER_GEOMETRY
, key
);
114 memcpy(key
->generic_remap_table
, gs
->generic_remap_table
,
115 sizeof(gs
->generic_remap_table
));
117 key
->gs
.vs_generic_outputs
= svga
->curr
.vs
->generic_outputs
;
119 key
->gs
.need_prescale
= svga
->state
.hw_clear
.prescale
.enabled
;
121 key
->gs
.writes_psize
= gs
->base
.info
.writes_psize
;
122 key
->gs
.wide_point
= gs
->wide_point
;
123 key
->sprite_coord_enable
= svga
->curr
.rast
->templ
.sprite_coord_enable
;
124 key
->sprite_origin_lower_left
= (svga
->curr
.rast
->templ
.sprite_coord_mode
125 == PIPE_SPRITE_COORD_LOWER_LEFT
);
128 key
->clip_plane_enable
= svga
->curr
.rast
->templ
.clip_plane_enable
;
133 * svga_reemit_gs_bindings - Reemit the geometry shader bindings
136 svga_reemit_gs_bindings(struct svga_context
*svga
)
139 struct svga_winsys_gb_shader
*gbshader
= NULL
;
140 SVGA3dShaderId shaderId
= SVGA3D_INVALID_ID
;
142 assert(svga
->rebind
.flags
.gs
);
143 assert(svga_have_gb_objects(svga
));
145 /* Geometry Shader is only supported in vgpu10 */
146 assert(svga_have_vgpu10(svga
));
148 if (svga
->state
.hw_draw
.gs
) {
149 gbshader
= svga
->state
.hw_draw
.gs
->gb_shader
;
150 shaderId
= svga
->state
.hw_draw
.gs
->id
;
153 if (!svga_need_to_rebind_resources(svga
)) {
154 ret
= svga
->swc
->resource_rebind(svga
->swc
, NULL
, gbshader
,
159 ret
= SVGA3D_vgpu10_SetShader(svga
->swc
, SVGA3D_SHADERTYPE_GS
,
166 svga
->rebind
.flags
.gs
= FALSE
;
170 static enum pipe_error
171 emit_hw_gs(struct svga_context
*svga
, unsigned dirty
)
173 struct svga_shader_variant
*variant
;
174 struct svga_geometry_shader
*gs
= svga
->curr
.gs
;
175 enum pipe_error ret
= PIPE_OK
;
176 struct svga_compile_key key
;
178 SVGA_STATS_TIME_PUSH(svga_sws(svga
), SVGA_STATS_TIME_EMITGS
);
180 /* If there's a user-defined GS, we should have a pointer to a derived
181 * GS. This should have been resolved in update_tgsi_transform().
183 if (svga
->curr
.user_gs
)
184 assert(svga
->curr
.gs
);
187 if (svga
->state
.hw_draw
.gs
!= NULL
) {
189 /** The previous geometry shader is made inactive.
190 * Needs to unbind the geometry shader.
192 ret
= svga_set_shader(svga
, SVGA3D_SHADERTYPE_GS
, NULL
);
195 svga
->state
.hw_draw
.gs
= NULL
;
200 /* If there is stream output info for this geometry shader, then use
201 * it instead of the one from the vertex shader.
203 if (svga_have_gs_streamout(svga
)) {
204 ret
= svga_set_stream_output(svga
, gs
->base
.stream_output
);
205 if (ret
!= PIPE_OK
) {
209 else if (!svga_have_vs_streamout(svga
)) {
210 /* turn off stream out */
211 ret
= svga_set_stream_output(svga
, NULL
);
212 if (ret
!= PIPE_OK
) {
217 /* SVGA_NEW_NEED_SWTNL */
218 if (svga
->state
.sw
.need_swtnl
&& !svga_have_vgpu10(svga
)) {
219 /* No geometry shader is needed */
223 make_gs_key(svga
, &key
);
225 /* See if we already have a GS variant that matches the key */
226 variant
= svga_search_shader_key(&gs
->base
, &key
);
229 ret
= compile_gs(svga
, gs
, &key
, &variant
);
233 /* insert the new variant at head of linked list */
235 variant
->next
= gs
->base
.variants
;
236 gs
->base
.variants
= variant
;
240 if (variant
!= svga
->state
.hw_draw
.gs
) {
241 /* Bind the new variant */
242 ret
= svga_set_shader(svga
, SVGA3D_SHADERTYPE_GS
, variant
);
246 svga
->rebind
.flags
.gs
= FALSE
;
247 svga
->dirty
|= SVGA_NEW_GS_VARIANT
;
248 svga
->state
.hw_draw
.gs
= variant
;
252 SVGA_STATS_TIME_POP(svga_sws(svga
));
256 struct svga_tracked_state svga_hw_gs
=
258 "geometry shader (hwtnl)",
262 SVGA_NEW_TEXTURE_BINDING
|
265 SVGA_NEW_NEED_SWTNL
),