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"
42 /***********************************************************************
46 static INLINE
int compare_vs_keys( const struct svga_vs_compile_key
*a
,
47 const struct svga_vs_compile_key
*b
)
49 unsigned keysize
= svga_vs_key_size( a
);
50 return memcmp( a
, b
, keysize
);
54 static struct svga_shader_result
*search_vs_key( struct svga_vertex_shader
*vs
,
55 const struct svga_vs_compile_key
*key
)
57 struct svga_shader_result
*result
= vs
->base
.results
;
61 for ( ; result
; result
= result
->next
) {
62 if (compare_vs_keys( key
, &result
->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
);
101 static enum pipe_error
compile_vs( struct svga_context
*svga
,
102 struct svga_vertex_shader
*vs
,
103 const struct svga_vs_compile_key
*key
,
104 struct svga_shader_result
**out_result
)
106 struct svga_shader_result
*result
;
107 enum pipe_error ret
= PIPE_ERROR
;
109 result
= svga_translate_vertex_program( vs
, key
);
110 if (result
== NULL
) {
111 /* some problem during translation, try the dummy shader */
112 const struct tgsi_token
*dummy
= get_dummy_vertex_shader();
114 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
117 debug_printf("Failed to compile vertex shader, using dummy shader instead.\n");
118 FREE((void *) vs
->base
.tokens
);
119 vs
->base
.tokens
= dummy
;
120 result
= svga_translate_vertex_program(vs
, key
);
121 if (result
== NULL
) {
127 result
->id
= util_bitmask_add(svga
->vs_bm
);
128 if(result
->id
== UTIL_BITMASK_INVALID_INDEX
) {
129 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
133 ret
= SVGA3D_DefineShader(svga
->swc
,
135 SVGA3D_SHADERTYPE_VS
,
137 result
->nr_tokens
* sizeof result
->tokens
[0]);
141 *out_result
= result
;
142 result
->next
= vs
->base
.results
;
143 vs
->base
.results
= result
;
148 if (result
->id
!= UTIL_BITMASK_INVALID_INDEX
)
149 util_bitmask_clear( svga
->vs_bm
, result
->id
);
150 svga_destroy_shader_result( result
);
155 /* SVGA_NEW_PRESCALE, SVGA_NEW_RAST, SVGA_NEW_ZERO_STRIDE, SVGA_NEW_FS
158 make_vs_key(struct svga_context
*svga
, struct svga_vs_compile_key
*key
)
160 memset(key
, 0, sizeof *key
);
161 key
->need_prescale
= svga
->state
.hw_clear
.prescale
.enabled
;
162 key
->allow_psiz
= svga
->curr
.rast
->templ
.point_size_per_vertex
;
163 key
->zero_stride_vertex_elements
=
164 svga
->curr
.zero_stride_vertex_elements
;
165 key
->num_zero_stride_vertex_elements
=
166 svga
->curr
.num_zero_stride_vertex_elements
;
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_result
*result
= 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 result
= search_vs_key( vs
, &key
);
190 ret
= compile_vs( svga
, vs
, &key
, &result
);
199 if (result
!= svga
->state
.hw_draw
.vs
) {
200 ret
= SVGA3D_SetShader(svga
->swc
,
201 SVGA3D_SHADERTYPE_VS
,
206 svga
->dirty
|= SVGA_NEW_VS_RESULT
;
207 svga
->state
.hw_draw
.vs
= result
;
213 struct svga_tracked_state svga_hw_vs
=
215 "vertex shader (hwtnl)",
219 SVGA_NEW_NEED_SWTNL
|
220 SVGA_NEW_ZERO_STRIDE
),
226 * This function handles the special case of vertex attributes
227 * with stride=0. Basically, copy those values into the constant
228 * buffer and modify the vertex shader to get the values from the
229 * constant buffer rather than a vertex array.
231 static enum pipe_error
232 update_zero_stride( struct svga_context
*svga
,
237 svga
->curr
.zero_stride_vertex_elements
= 0;
238 svga
->curr
.num_zero_stride_vertex_elements
= 0;
240 for (i
= 0; i
< svga
->curr
.velems
->count
; i
++) {
241 const struct pipe_vertex_element
*vel
= &svga
->curr
.velems
->velem
[i
];
242 const struct pipe_vertex_buffer
*vbuffer
= &svga
->curr
.vb
[
243 vel
->vertex_buffer_index
];
245 if (vbuffer
->stride
== 0) {
247 svga
->curr
.num_zero_stride_vertex_elements
;
248 struct pipe_transfer
*transfer
;
249 struct translate
*translate
;
250 struct translate_key key
;
253 svga
->curr
.zero_stride_vertex_elements
|= (1 << i
);
254 ++svga
->curr
.num_zero_stride_vertex_elements
;
256 key
.output_stride
= 4 * sizeof(float);
258 key
.element
[0].type
= TRANSLATE_ELEMENT_NORMAL
;
259 key
.element
[0].input_format
= vel
->src_format
;
260 key
.element
[0].output_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
261 key
.element
[0].input_buffer
= vel
->vertex_buffer_index
;
262 key
.element
[0].input_offset
= vel
->src_offset
;
263 key
.element
[0].instance_divisor
= vel
->instance_divisor
;
264 key
.element
[0].output_offset
= const_idx
* 4 * sizeof(float);
266 translate_key_sanitize(&key
);
267 /* translate_generic_create is technically private but
268 * we don't want to code-generate, just want generic
270 translate
= translate_generic_create(&key
);
272 assert(vel
->src_offset
== 0);
274 mapped_buffer
= pipe_buffer_map_range(&svga
->pipe
,
276 vel
->src_offset
+ vbuffer
->buffer_offset
,
277 util_format_get_blocksize(vel
->src_format
),
280 mapped_buffer
= (uint8_t*)mapped_buffer
- vel
->src_offset
;
282 translate
->set_buffer(translate
, vel
->vertex_buffer_index
,
284 vbuffer
->stride
, ~0);
285 translate
->run(translate
, 0, 1, 0,
286 svga
->curr
.zero_stride_constants
);
288 pipe_buffer_unmap(&svga
->pipe
, transfer
);
290 translate
->release(translate
);
294 if (svga
->curr
.num_zero_stride_vertex_elements
)
295 svga
->dirty
|= SVGA_NEW_ZERO_STRIDE
;
300 struct svga_tracked_state svga_hw_update_zero_stride
=
302 "update zero_stride",
303 ( SVGA_NEW_VELEMENT
|