1 /**********************************************************
2 * Copyright 2008-2009 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 "pipe/p_defines.h"
28 #include "util/u_format.h"
29 #include "util/u_math.h"
30 #include "util/u_memory.h"
31 #include "util/u_bitmask.h"
32 #include "translate/translate.h"
33 #include "tgsi/tgsi_ureg.h"
35 #include "svga_context.h"
36 #include "svga_state.h"
38 #include "svga_tgsi.h"
40 #include "svga_hw_reg.h"
44 compare_vs_keys(const struct svga_vs_compile_key
*a
,
45 const struct svga_vs_compile_key
*b
)
47 unsigned keysize
= svga_vs_key_size( a
);
48 return memcmp( a
, b
, keysize
);
52 /** Search for a vertex shader variant */
53 static struct svga_shader_variant
*
54 search_vs_key(const struct svga_vertex_shader
*vs
,
55 const struct svga_vs_compile_key
*key
)
57 struct svga_shader_variant
*variant
= vs
->base
.variants
;
61 for ( ; variant
; variant
= variant
->next
) {
62 if (compare_vs_keys( key
, &variant
->key
.vkey
) == 0)
71 * If we fail to compile a vertex shader we'll use a dummy/fallback shader
72 * that simply emits a (0,0,0,1) vertex position.
74 static const struct tgsi_token
*
75 get_dummy_vertex_shader(void)
77 static const float zero
[4] = { 0.0, 0.0, 0.0, 1.0 };
78 struct ureg_program
*ureg
;
79 const struct tgsi_token
*tokens
;
84 ureg
= ureg_create(TGSI_PROCESSOR_VERTEX
);
88 dst
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_POSITION
, 0);
89 src
= ureg_DECL_immediate(ureg
, zero
, 4);
90 ureg_MOV(ureg
, dst
, src
);
93 tokens
= ureg_get_tokens(ureg
, &num_tokens
);
102 * Translate TGSI shader into an svga shader variant.
104 static enum pipe_error
105 compile_vs(struct svga_context
*svga
,
106 struct svga_vertex_shader
*vs
,
107 const struct svga_vs_compile_key
*key
,
108 struct svga_shader_variant
**out_variant
)
110 struct svga_shader_variant
*variant
;
111 enum pipe_error ret
= PIPE_ERROR
;
113 variant
= svga_translate_vertex_program( vs
, key
);
114 if (variant
== NULL
) {
115 /* some problem during translation, try the dummy shader */
116 const struct tgsi_token
*dummy
= get_dummy_vertex_shader();
118 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
121 debug_printf("Failed to compile vertex shader, using dummy shader instead.\n");
122 FREE((void *) vs
->base
.tokens
);
123 vs
->base
.tokens
= dummy
;
124 variant
= svga_translate_vertex_program(vs
, key
);
125 if (variant
== NULL
) {
131 variant
->id
= util_bitmask_add(svga
->vs_bm
);
132 if(variant
->id
== UTIL_BITMASK_INVALID_INDEX
) {
133 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
137 ret
= SVGA3D_DefineShader(svga
->swc
,
139 SVGA3D_SHADERTYPE_VS
,
141 variant
->nr_tokens
* sizeof variant
->tokens
[0]);
145 *out_variant
= variant
;
146 variant
->next
= vs
->base
.variants
;
147 vs
->base
.variants
= variant
;
152 if (variant
->id
!= UTIL_BITMASK_INVALID_INDEX
)
153 util_bitmask_clear( svga
->vs_bm
, variant
->id
);
154 svga_destroy_shader_variant( variant
);
159 /* SVGA_NEW_PRESCALE, SVGA_NEW_RAST, SVGA_NEW_FS
162 make_vs_key(struct svga_context
*svga
, struct svga_vs_compile_key
*key
)
164 memset(key
, 0, sizeof *key
);
165 key
->need_prescale
= svga
->state
.hw_clear
.prescale
.enabled
;
166 key
->allow_psiz
= svga
->curr
.rast
->templ
.point_size_per_vertex
;
169 key
->fs_generic_inputs
= svga
->curr
.fs
->generic_inputs
;
174 static enum pipe_error
175 emit_hw_vs(struct svga_context
*svga
, unsigned dirty
)
177 struct svga_shader_variant
*variant
= NULL
;
178 unsigned id
= SVGA3D_INVALID_ID
;
179 enum pipe_error ret
= PIPE_OK
;
181 /* SVGA_NEW_NEED_SWTNL */
182 if (!svga
->state
.sw
.need_swtnl
) {
183 struct svga_vertex_shader
*vs
= svga
->curr
.vs
;
184 struct svga_vs_compile_key key
;
186 make_vs_key( svga
, &key
);
188 variant
= search_vs_key( vs
, &key
);
190 ret
= compile_vs( svga
, vs
, &key
, &variant
);
199 if (variant
!= svga
->state
.hw_draw
.vs
) {
200 ret
= SVGA3D_SetShader(svga
->swc
,
201 SVGA3D_SHADERTYPE_VS
,
206 svga
->dirty
|= SVGA_NEW_VS_VARIANT
;
207 svga
->state
.hw_draw
.vs
= variant
;
213 struct svga_tracked_state svga_hw_vs
=
215 "vertex shader (hwtnl)",
219 SVGA_NEW_NEED_SWTNL
),