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_math.h"
29 #include "util/u_memory.h"
30 #include "tgsi/tgsi_parse.h"
32 #include "svga_context.h"
33 #include "svga_screen_texture.h"
35 #include "svga_debug.h"
37 static INLINE
unsigned
38 translate_wrap_mode(unsigned wrap
)
41 case PIPE_TEX_WRAP_REPEAT
:
42 return SVGA3D_TEX_ADDRESS_WRAP
;
44 case PIPE_TEX_WRAP_CLAMP
:
45 return SVGA3D_TEX_ADDRESS_CLAMP
;
47 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
48 /* Unfortunately SVGA3D_TEX_ADDRESS_EDGE not respected by
51 return SVGA3D_TEX_ADDRESS_CLAMP
;
53 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
54 return SVGA3D_TEX_ADDRESS_BORDER
;
56 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
57 return SVGA3D_TEX_ADDRESS_MIRROR
;
59 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
60 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
61 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
62 return SVGA3D_TEX_ADDRESS_MIRRORONCE
;
66 return SVGA3D_TEX_ADDRESS_WRAP
;
70 static INLINE
unsigned translate_img_filter( unsigned filter
)
73 case PIPE_TEX_FILTER_NEAREST
: return SVGA3D_TEX_FILTER_NEAREST
;
74 case PIPE_TEX_FILTER_LINEAR
: return SVGA3D_TEX_FILTER_LINEAR
;
77 return SVGA3D_TEX_FILTER_NEAREST
;
81 static INLINE
unsigned translate_mip_filter( unsigned filter
)
84 case PIPE_TEX_MIPFILTER_NONE
: return SVGA3D_TEX_FILTER_NONE
;
85 case PIPE_TEX_MIPFILTER_NEAREST
: return SVGA3D_TEX_FILTER_NEAREST
;
86 case PIPE_TEX_MIPFILTER_LINEAR
: return SVGA3D_TEX_FILTER_LINEAR
;
89 return SVGA3D_TEX_FILTER_NONE
;
94 svga_create_sampler_state(struct pipe_context
*pipe
,
95 const struct pipe_sampler_state
*sampler
)
97 struct svga_context
*svga
= svga_context(pipe
);
98 struct svga_sampler_state
*cso
= CALLOC_STRUCT( svga_sampler_state
);
100 cso
->mipfilter
= translate_mip_filter(sampler
->min_mip_filter
);
101 cso
->magfilter
= translate_img_filter( sampler
->mag_img_filter
);
102 cso
->minfilter
= translate_img_filter( sampler
->min_img_filter
);
103 cso
->aniso_level
= MAX2( sampler
->max_anisotropy
, 1 );
104 if(sampler
->max_anisotropy
)
105 cso
->magfilter
= cso
->minfilter
= SVGA3D_TEX_FILTER_ANISOTROPIC
;
106 cso
->lod_bias
= sampler
->lod_bias
;
107 cso
->addressu
= translate_wrap_mode(sampler
->wrap_s
);
108 cso
->addressv
= translate_wrap_mode(sampler
->wrap_t
);
109 cso
->addressw
= translate_wrap_mode(sampler
->wrap_r
);
110 cso
->normalized_coords
= sampler
->normalized_coords
;
111 cso
->compare_mode
= sampler
->compare_mode
;
112 cso
->compare_func
= sampler
->compare_func
;
115 uint32 r
= float_to_ubyte(sampler
->border_color
[0]);
116 uint32 g
= float_to_ubyte(sampler
->border_color
[1]);
117 uint32 b
= float_to_ubyte(sampler
->border_color
[2]);
118 uint32 a
= float_to_ubyte(sampler
->border_color
[3]);
120 cso
->bordercolor
= (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
123 /* No SVGA3D support for:
124 * - min/max LOD clamping
127 cso
->view_min_lod
= MAX2(sampler
->min_lod
, 0);
128 cso
->view_max_lod
= MAX2(sampler
->max_lod
, 0);
131 if (svga
->debug
.use_min_mipmap
) {
132 if (cso
->view_min_lod
== cso
->view_max_lod
) {
133 cso
->min_lod
= cso
->view_min_lod
;
134 cso
->view_min_lod
= 0;
135 cso
->view_max_lod
= 1000; /* Just a high number */
136 cso
->mipfilter
= SVGA3D_TEX_FILTER_NONE
;
140 SVGA_DBG(DEBUG_VIEWS
, "min %u, view(min %u, max %u) lod, mipfilter %s\n",
141 cso
->min_lod
, cso
->view_min_lod
, cso
->view_max_lod
,
142 cso
->mipfilter
== SVGA3D_TEX_FILTER_NONE
? "SVGA3D_TEX_FILTER_NONE" : "SOMETHING");
147 static void svga_bind_sampler_states(struct pipe_context
*pipe
,
148 unsigned num
, void **sampler
)
150 struct svga_context
*svga
= svga_context(pipe
);
153 assert(num
<= PIPE_MAX_SAMPLERS
);
155 /* Check for no-op */
156 if (num
== svga
->curr
.num_samplers
&&
157 !memcmp(svga
->curr
.sampler
, sampler
, num
* sizeof(void *))) {
158 if (0) debug_printf("sampler noop\n");
162 for (i
= 0; i
< num
; i
++)
163 svga
->curr
.sampler
[i
] = sampler
[i
];
165 for (i
= num
; i
< svga
->curr
.num_samplers
; i
++)
166 svga
->curr
.sampler
[i
] = NULL
;
168 svga
->curr
.num_samplers
= num
;
169 svga
->dirty
|= SVGA_NEW_SAMPLER
;
172 static void svga_delete_sampler_state(struct pipe_context
*pipe
,
179 static struct pipe_sampler_view
*
180 svga_create_sampler_view(struct pipe_context
*pipe
,
181 struct pipe_texture
*texture
,
182 const struct pipe_sampler_view
*templ
)
184 struct pipe_sampler_view
*view
= CALLOC_STRUCT(pipe_sampler_view
);
188 view
->reference
.count
= 1;
189 view
->texture
= NULL
;
190 pipe_texture_reference(&view
->texture
, texture
);
191 view
->context
= pipe
;
199 svga_sampler_view_destroy(struct pipe_context
*pipe
,
200 struct pipe_sampler_view
*view
)
202 pipe_texture_reference(&view
->texture
, NULL
);
206 static void svga_set_sampler_views(struct pipe_context
*pipe
,
208 struct pipe_sampler_view
**views
)
210 struct svga_context
*svga
= svga_context(pipe
);
211 unsigned flag_1d
= 0;
212 unsigned flag_srgb
= 0;
215 assert(num
<= PIPE_MAX_SAMPLERS
);
217 /* Check for no-op */
218 if (num
== svga
->curr
.num_sampler_views
&&
219 !memcmp(svga
->curr
.sampler_views
, views
, num
* sizeof(struct pipe_sampler_view
*))) {
220 if (0) debug_printf("texture noop\n");
224 for (i
= 0; i
< num
; i
++) {
225 pipe_sampler_view_reference(&svga
->curr
.sampler_views
[i
],
231 if (views
[i
]->texture
->format
== PIPE_FORMAT_B8G8R8A8_SRGB
)
234 if (views
[i
]->texture
->target
== PIPE_TEXTURE_1D
)
238 for (i
= num
; i
< svga
->curr
.num_sampler_views
; i
++)
239 pipe_sampler_view_reference(&svga
->curr
.sampler_views
[i
],
242 svga
->curr
.num_sampler_views
= num
;
243 svga
->dirty
|= SVGA_NEW_TEXTURE_BINDING
;
245 if (flag_srgb
!= svga
->curr
.tex_flags
.flag_srgb
||
246 flag_1d
!= svga
->curr
.tex_flags
.flag_1d
)
248 svga
->dirty
|= SVGA_NEW_TEXTURE_FLAGS
;
249 svga
->curr
.tex_flags
.flag_1d
= flag_1d
;
250 svga
->curr
.tex_flags
.flag_srgb
= flag_srgb
;
256 void svga_init_sampler_functions( struct svga_context
*svga
)
258 svga
->pipe
.create_sampler_state
= svga_create_sampler_state
;
259 svga
->pipe
.bind_fragment_sampler_states
= svga_bind_sampler_states
;
260 svga
->pipe
.delete_sampler_state
= svga_delete_sampler_state
;
261 svga
->pipe
.set_fragment_sampler_views
= svga_set_sampler_views
;
262 svga
->pipe
.create_sampler_view
= svga_create_sampler_view
;
263 svga
->pipe
.sampler_view_destroy
= svga_sampler_view_destroy
;