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 "pipe/p_inlines.h"
27 #include "pipe/p_defines.h"
28 #include "util/u_format.h"
29 #include "util/u_math.h"
30 #include "util/u_bitmask.h"
31 #include "translate/translate.h"
33 #include "svga_context.h"
34 #include "svga_state.h"
36 #include "svga_tgsi.h"
38 #include "svga_hw_reg.h"
40 /***********************************************************************
44 static INLINE
int 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 static struct svga_shader_result
*search_vs_key( struct svga_vertex_shader
*vs
,
53 const struct svga_vs_compile_key
*key
)
55 struct svga_shader_result
*result
= vs
->base
.results
;
59 for ( ; result
; result
= result
->next
) {
60 if (compare_vs_keys( key
, &result
->key
.vkey
) == 0)
68 static enum pipe_error
compile_vs( struct svga_context
*svga
,
69 struct svga_vertex_shader
*vs
,
70 const struct svga_vs_compile_key
*key
,
71 struct svga_shader_result
**out_result
)
73 struct svga_shader_result
*result
;
74 enum pipe_error ret
= PIPE_OK
;
76 result
= svga_translate_vertex_program( vs
, key
);
78 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
82 result
->id
= util_bitmask_add(svga
->vs_bm
);
83 if(result
->id
== UTIL_BITMASK_INVALID_INDEX
)
86 ret
= SVGA3D_DefineShader(svga
->swc
,
90 result
->nr_tokens
* sizeof result
->tokens
[0]);
95 result
->next
= vs
->base
.results
;
96 vs
->base
.results
= result
;
101 if (result
->id
!= UTIL_BITMASK_INVALID_INDEX
)
102 util_bitmask_clear( svga
->vs_bm
, result
->id
);
103 svga_destroy_shader_result( result
);
108 /* SVGA_NEW_PRESCALE, SVGA_NEW_RAST, SVGA_NEW_ZERO_STRIDE
110 static int make_vs_key( struct svga_context
*svga
,
111 struct svga_vs_compile_key
*key
)
113 memset(key
, 0, sizeof *key
);
114 key
->need_prescale
= svga
->state
.hw_clear
.prescale
.enabled
;
115 key
->allow_psiz
= svga
->curr
.rast
->templ
.point_size_per_vertex
;
116 key
->zero_stride_vertex_elements
=
117 svga
->curr
.zero_stride_vertex_elements
;
118 key
->num_zero_stride_vertex_elements
=
119 svga
->curr
.num_zero_stride_vertex_elements
;
125 static int emit_hw_vs( struct svga_context
*svga
,
128 struct svga_shader_result
*result
= NULL
;
129 unsigned id
= SVGA3D_INVALID_ID
;
132 /* SVGA_NEW_NEED_SWTNL */
133 if (!svga
->state
.sw
.need_swtnl
) {
134 struct svga_vertex_shader
*vs
= svga
->curr
.vs
;
135 struct svga_vs_compile_key key
;
137 ret
= make_vs_key( svga
, &key
);
141 result
= search_vs_key( vs
, &key
);
143 ret
= compile_vs( svga
, vs
, &key
, &result
);
152 if (result
!= svga
->state
.hw_draw
.vs
) {
153 if (id
!= svga
->state
.hw_draw
.shader_id
[PIPE_SHADER_VERTEX
]) {
154 ret
= SVGA3D_SetShader(svga
->swc
,
155 SVGA3D_SHADERTYPE_VS
,
161 svga
->dirty
|= SVGA_NEW_VS_RESULT
;
162 svga
->state
.hw_draw
.shader_id
[PIPE_SHADER_VERTEX
] = id
;
163 svga
->state
.hw_draw
.vs
= result
;
169 struct svga_tracked_state svga_hw_vs
=
171 "vertex shader (hwtnl)",
174 SVGA_NEW_NEED_SWTNL
|
175 SVGA_NEW_ZERO_STRIDE
),
180 /***********************************************************************
182 static int update_zero_stride( struct svga_context
*svga
,
187 svga
->curr
.zero_stride_vertex_elements
= 0;
188 svga
->curr
.num_zero_stride_vertex_elements
= 0;
190 for (i
= 0; i
< svga
->curr
.num_vertex_elements
; i
++) {
191 const struct pipe_vertex_element
*vel
= &svga
->curr
.ve
[i
];
192 const struct pipe_vertex_buffer
*vbuffer
= &svga
->curr
.vb
[
193 vel
->vertex_buffer_index
];
194 if (vbuffer
->stride
== 0) {
196 svga
->curr
.num_zero_stride_vertex_elements
;
197 struct translate
*translate
;
198 struct translate_key key
;
201 svga
->curr
.zero_stride_vertex_elements
|= (1 << i
);
202 ++svga
->curr
.num_zero_stride_vertex_elements
;
204 key
.output_stride
= 4 * sizeof(float);
206 key
.element
[0].input_format
= vel
->src_format
;
207 key
.element
[0].output_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
208 key
.element
[0].input_buffer
= vel
->vertex_buffer_index
;
209 key
.element
[0].input_offset
= vel
->src_offset
;
210 key
.element
[0].output_offset
= const_idx
* 4 * sizeof(float);
212 translate_key_sanitize(&key
);
213 /* translate_generic_create is technically private but
214 * we don't want to code-generate, just want generic
216 translate
= translate_generic_create(&key
);
218 assert(vel
->src_offset
== 0);
220 mapped_buffer
= pipe_buffer_map_range(svga
->pipe
.screen
,
223 util_format_get_blocksize(vel
->src_format
),
224 PIPE_BUFFER_USAGE_CPU_READ
);
225 translate
->set_buffer(translate
, vel
->vertex_buffer_index
,
228 translate
->run(translate
, 0, 1,
229 svga
->curr
.zero_stride_constants
);
231 pipe_buffer_unmap(svga
->pipe
.screen
,
233 translate
->release(translate
);
237 if (svga
->curr
.num_zero_stride_vertex_elements
)
238 svga
->dirty
|= SVGA_NEW_ZERO_STRIDE
;
243 struct svga_tracked_state svga_hw_update_zero_stride
=
245 "update zero_stride",
246 ( SVGA_NEW_VELEMENT
|