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 "tgsi/tgsi_parse.h"
33 #include "svga_context.h"
34 #include "svga_resource_texture.h"
36 #include "svga_debug.h"
38 static INLINE
unsigned
39 translate_wrap_mode(unsigned wrap
)
42 case PIPE_TEX_WRAP_REPEAT
:
43 return SVGA3D_TEX_ADDRESS_WRAP
;
45 case PIPE_TEX_WRAP_CLAMP
:
46 return SVGA3D_TEX_ADDRESS_CLAMP
;
48 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
49 /* Unfortunately SVGA3D_TEX_ADDRESS_EDGE not respected by
52 return SVGA3D_TEX_ADDRESS_CLAMP
;
54 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
55 return SVGA3D_TEX_ADDRESS_BORDER
;
57 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
58 return SVGA3D_TEX_ADDRESS_MIRROR
;
60 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
61 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
62 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
63 return SVGA3D_TEX_ADDRESS_MIRRORONCE
;
67 return SVGA3D_TEX_ADDRESS_WRAP
;
71 static INLINE
unsigned translate_img_filter( unsigned filter
)
74 case PIPE_TEX_FILTER_NEAREST
: return SVGA3D_TEX_FILTER_NEAREST
;
75 case PIPE_TEX_FILTER_LINEAR
: return SVGA3D_TEX_FILTER_LINEAR
;
78 return SVGA3D_TEX_FILTER_NEAREST
;
82 static INLINE
unsigned translate_mip_filter( unsigned filter
)
85 case PIPE_TEX_MIPFILTER_NONE
: return SVGA3D_TEX_FILTER_NONE
;
86 case PIPE_TEX_MIPFILTER_NEAREST
: return SVGA3D_TEX_FILTER_NEAREST
;
87 case PIPE_TEX_MIPFILTER_LINEAR
: return SVGA3D_TEX_FILTER_LINEAR
;
90 return SVGA3D_TEX_FILTER_NONE
;
95 svga_create_sampler_state(struct pipe_context
*pipe
,
96 const struct pipe_sampler_state
*sampler
)
98 struct svga_context
*svga
= svga_context(pipe
);
99 struct svga_sampler_state
*cso
= CALLOC_STRUCT( svga_sampler_state
);
101 cso
->mipfilter
= translate_mip_filter(sampler
->min_mip_filter
);
102 cso
->magfilter
= translate_img_filter( sampler
->mag_img_filter
);
103 cso
->minfilter
= translate_img_filter( sampler
->min_img_filter
);
104 cso
->aniso_level
= MAX2( sampler
->max_anisotropy
, 1 );
105 if(sampler
->max_anisotropy
)
106 cso
->magfilter
= cso
->minfilter
= SVGA3D_TEX_FILTER_ANISOTROPIC
;
107 cso
->lod_bias
= sampler
->lod_bias
;
108 cso
->addressu
= translate_wrap_mode(sampler
->wrap_s
);
109 cso
->addressv
= translate_wrap_mode(sampler
->wrap_t
);
110 cso
->addressw
= translate_wrap_mode(sampler
->wrap_r
);
111 cso
->normalized_coords
= sampler
->normalized_coords
;
112 cso
->compare_mode
= sampler
->compare_mode
;
113 cso
->compare_func
= sampler
->compare_func
;
116 uint32 r
= float_to_ubyte(sampler
->border_color
.f
[0]);
117 uint32 g
= float_to_ubyte(sampler
->border_color
.f
[1]);
118 uint32 b
= float_to_ubyte(sampler
->border_color
.f
[2]);
119 uint32 a
= float_to_ubyte(sampler
->border_color
.f
[3]);
121 cso
->bordercolor
= (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
124 /* No SVGA3D support for:
125 * - min/max LOD clamping
128 cso
->view_min_lod
= MAX2((int) (sampler
->min_lod
+ 0.5), 0);
129 cso
->view_max_lod
= MAX2((int) (sampler
->max_lod
+ 0.5), 0);
132 if (svga
->debug
.use_min_mipmap
) {
133 if (cso
->view_min_lod
== cso
->view_max_lod
) {
134 cso
->min_lod
= cso
->view_min_lod
;
135 cso
->view_min_lod
= 0;
136 cso
->view_max_lod
= 1000; /* Just a high number */
137 cso
->mipfilter
= SVGA3D_TEX_FILTER_NONE
;
141 SVGA_DBG(DEBUG_VIEWS
, "min %u, view(min %u, max %u) lod, mipfilter %s\n",
142 cso
->min_lod
, cso
->view_min_lod
, cso
->view_max_lod
,
143 cso
->mipfilter
== SVGA3D_TEX_FILTER_NONE
? "SVGA3D_TEX_FILTER_NONE" : "SOMETHING");
149 svga_bind_fragment_sampler_states(struct pipe_context
*pipe
,
150 unsigned num
, void **sampler
)
152 struct svga_context
*svga
= svga_context(pipe
);
155 assert(num
<= PIPE_MAX_SAMPLERS
);
157 /* Check for no-op */
158 if (num
== svga
->curr
.num_samplers
&&
159 !memcmp(svga
->curr
.sampler
, sampler
, num
* sizeof(void *))) {
160 if (0) debug_printf("sampler noop\n");
164 for (i
= 0; i
< num
; i
++)
165 svga
->curr
.sampler
[i
] = sampler
[i
];
167 for (i
= num
; i
< svga
->curr
.num_samplers
; i
++)
168 svga
->curr
.sampler
[i
] = NULL
;
170 svga
->curr
.num_samplers
= num
;
171 svga
->dirty
|= SVGA_NEW_SAMPLER
;
174 static void svga_delete_sampler_state(struct pipe_context
*pipe
,
181 static struct pipe_sampler_view
*
182 svga_create_sampler_view(struct pipe_context
*pipe
,
183 struct pipe_resource
*texture
,
184 const struct pipe_sampler_view
*templ
)
186 struct pipe_sampler_view
*view
= CALLOC_STRUCT(pipe_sampler_view
);
190 view
->reference
.count
= 1;
191 view
->texture
= NULL
;
192 pipe_resource_reference(&view
->texture
, texture
);
193 view
->context
= pipe
;
201 svga_sampler_view_destroy(struct pipe_context
*pipe
,
202 struct pipe_sampler_view
*view
)
204 pipe_resource_reference(&view
->texture
, NULL
);
209 svga_set_fragment_sampler_views(struct pipe_context
*pipe
,
211 struct pipe_sampler_view
**views
)
213 struct svga_context
*svga
= svga_context(pipe
);
214 unsigned flag_1d
= 0;
215 unsigned flag_srgb
= 0;
218 assert(num
<= PIPE_MAX_SAMPLERS
);
220 /* Check for no-op */
221 if (num
== svga
->curr
.num_sampler_views
&&
222 !memcmp(svga
->curr
.sampler_views
, views
, num
* sizeof(struct pipe_sampler_view
*))) {
223 if (0) debug_printf("texture noop\n");
227 for (i
= 0; i
< num
; i
++) {
228 pipe_sampler_view_reference(&svga
->curr
.sampler_views
[i
],
234 if (util_format_is_srgb(views
[i
]->format
))
237 if (views
[i
]->texture
->target
== PIPE_TEXTURE_1D
)
241 for (i
= num
; i
< svga
->curr
.num_sampler_views
; i
++)
242 pipe_sampler_view_reference(&svga
->curr
.sampler_views
[i
],
245 svga
->curr
.num_sampler_views
= num
;
246 svga
->dirty
|= SVGA_NEW_TEXTURE_BINDING
;
248 if (flag_srgb
!= svga
->curr
.tex_flags
.flag_srgb
||
249 flag_1d
!= svga
->curr
.tex_flags
.flag_1d
)
251 svga
->dirty
|= SVGA_NEW_TEXTURE_FLAGS
;
252 svga
->curr
.tex_flags
.flag_1d
= flag_1d
;
253 svga
->curr
.tex_flags
.flag_srgb
= flag_srgb
;
259 void svga_init_sampler_functions( struct svga_context
*svga
)
261 svga
->pipe
.create_sampler_state
= svga_create_sampler_state
;
262 svga
->pipe
.bind_fragment_sampler_states
= svga_bind_fragment_sampler_states
;
263 svga
->pipe
.delete_sampler_state
= svga_delete_sampler_state
;
264 svga
->pipe
.set_fragment_sampler_views
= svga_set_fragment_sampler_views
;
265 svga
->pipe
.create_sampler_view
= svga_create_sampler_view
;
266 svga
->pipe
.sampler_view_destroy
= svga_sampler_view_destroy
;