2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
4 develop this 3D driver.
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
14 The above copyright notice and this permission notice (including the
15 next paragraph) shall be included in all copies or substantial
16 portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **********************************************************************/
29 * Keith Whitwell <keith@tungstengraphics.com>
33 #include "brw_context.h"
34 #include "brw_state.h"
35 #include "brw_defines.h"
37 #include "pipe/p_util.h"
40 #define COMPAREFUNC_ALWAYS 0
41 #define COMPAREFUNC_NEVER 0x1
42 #define COMPAREFUNC_LESS 0x2
43 #define COMPAREFUNC_EQUAL 0x3
44 #define COMPAREFUNC_LEQUAL 0x4
45 #define COMPAREFUNC_GREATER 0x5
46 #define COMPAREFUNC_NOTEQUAL 0x6
47 #define COMPAREFUNC_GEQUAL 0x7
49 /* Samplers aren't strictly wm state from the hardware's perspective,
50 * but that is the only situation in which we use them in this driver.
53 static int intel_translate_shadow_compare_func(unsigned func
)
57 return COMPAREFUNC_ALWAYS
;
59 return COMPAREFUNC_LEQUAL
;
60 case PIPE_FUNC_LEQUAL
:
61 return COMPAREFUNC_LESS
;
62 case PIPE_FUNC_GREATER
:
63 return COMPAREFUNC_GEQUAL
;
64 case PIPE_FUNC_GEQUAL
:
65 return COMPAREFUNC_GREATER
;
66 case PIPE_FUNC_NOTEQUAL
:
67 return COMPAREFUNC_EQUAL
;
69 return COMPAREFUNC_NOTEQUAL
;
70 case PIPE_FUNC_ALWAYS
:
71 return COMPAREFUNC_NEVER
;
74 debug_printf("Unknown value in %s: %x\n", __FUNCTION__
, func
);
75 return COMPAREFUNC_NEVER
;
78 /* The brw (and related graphics cores) do not support GL_CLAMP. The
79 * Intel drivers for "other operating systems" implement GL_CLAMP as
80 * GL_CLAMP_TO_EDGE, so the same is done here.
82 static unsigned translate_wrap_mode( int wrap
)
85 case PIPE_TEX_WRAP_REPEAT
:
86 return BRW_TEXCOORDMODE_WRAP
;
87 case PIPE_TEX_WRAP_CLAMP
:
88 return BRW_TEXCOORDMODE_CLAMP
;
89 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
90 return BRW_TEXCOORDMODE_CLAMP
; /* conform likes it this way */
91 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
92 return BRW_TEXCOORDMODE_CLAMP_BORDER
;
93 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
94 return BRW_TEXCOORDMODE_MIRROR
;
96 return BRW_TEXCOORDMODE_WRAP
;
101 static unsigned U_FIXED(float value
, unsigned frac_bits
)
103 value
*= (1<<frac_bits
);
104 return value
< 0 ? 0 : value
;
107 static int S_FIXED(float value
, unsigned frac_bits
)
109 return value
* (1<<frac_bits
);
113 static unsigned upload_default_color( struct brw_context
*brw
,
116 struct brw_sampler_default_color sdc
;
118 COPY_4V(sdc
.color
, color
);
120 return brw_cache_data( &brw
->cache
[BRW_SAMPLER_DEFAULT_COLOR
], &sdc
);
126 static void brw_update_sampler_state( const struct pipe_sampler_state
*pipe_sampler
,
127 unsigned sdc_gs_offset
,
128 struct brw_sampler_state
*sampler
)
130 memset(sampler
, 0, sizeof(*sampler
));
132 switch (pipe_sampler
->min_mip_filter
) {
133 case PIPE_TEX_FILTER_NEAREST
:
134 sampler
->ss0
.min_filter
= BRW_MAPFILTER_NEAREST
;
136 case PIPE_TEX_FILTER_LINEAR
:
137 sampler
->ss0
.min_filter
= BRW_MAPFILTER_LINEAR
;
139 case PIPE_TEX_FILTER_ANISO
:
140 sampler
->ss0
.min_filter
= BRW_MAPFILTER_ANISOTROPIC
;
146 switch (pipe_sampler
->min_mip_filter
) {
147 case PIPE_TEX_MIPFILTER_NEAREST
:
148 sampler
->ss0
.mip_filter
= BRW_MIPFILTER_NEAREST
;
150 case PIPE_TEX_MIPFILTER_LINEAR
:
151 sampler
->ss0
.mip_filter
= BRW_MIPFILTER_LINEAR
;
153 case PIPE_TEX_MIPFILTER_NONE
:
154 sampler
->ss0
.mip_filter
= BRW_MIPFILTER_NONE
;
161 switch (pipe_sampler
->mag_img_filter
) {
162 case PIPE_TEX_FILTER_NEAREST
:
163 sampler
->ss0
.mag_filter
= BRW_MAPFILTER_NEAREST
;
165 case PIPE_TEX_FILTER_LINEAR
:
166 sampler
->ss0
.mag_filter
= BRW_MAPFILTER_LINEAR
;
168 case PIPE_TEX_FILTER_ANISO
:
169 sampler
->ss0
.mag_filter
= BRW_MAPFILTER_LINEAR
;
175 if (pipe_sampler
->max_anisotropy
> 2.0) {
176 sampler
->ss3
.max_aniso
= MAX2((pipe_sampler
->max_anisotropy
- 2) / 2,
180 sampler
->ss1
.s_wrap_mode
= translate_wrap_mode(pipe_sampler
->wrap_s
);
181 sampler
->ss1
.r_wrap_mode
= translate_wrap_mode(pipe_sampler
->wrap_r
);
182 sampler
->ss1
.t_wrap_mode
= translate_wrap_mode(pipe_sampler
->wrap_t
);
184 /* Fulsim complains if I don't do this. Hardware doesn't mind:
187 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
188 sampler
->ss1
.r_wrap_mode
= BRW_TEXCOORDMODE_CUBE
;
189 sampler
->ss1
.s_wrap_mode
= BRW_TEXCOORDMODE_CUBE
;
190 sampler
->ss1
.t_wrap_mode
= BRW_TEXCOORDMODE_CUBE
;
194 /* Set shadow function:
196 if (pipe_sampler
->compare_mode
== PIPE_TEX_COMPARE_R_TO_TEXTURE
) {
197 /* Shadowing is "enabled" by emitting a particular sampler
198 * message (sample_c). So need to recompile WM program when
199 * shadow comparison is enabled on each/any texture unit.
201 sampler
->ss0
.shadow_function
= intel_translate_shadow_compare_func(pipe_sampler
->compare_func
);
206 sampler
->ss0
.lod_bias
= S_FIXED(CLAMP(pipe_sampler
->lod_bias
, -16, 15), 6);
208 sampler
->ss0
.lod_preclamp
= 1; /* OpenGL mode */
209 sampler
->ss0
.default_color_mode
= 0; /* OpenGL/DX10 mode */
211 /* Set BaseMipLevel, MaxLOD, MinLOD:
213 * XXX: I don't think that using firstLevel, lastLevel works,
214 * because we always setup the surface state as if firstLevel ==
215 * level zero. Probably have to subtract firstLevel from each of
218 sampler
->ss0
.base_level
= U_FIXED(0, 1);
220 sampler
->ss1
.max_lod
= U_FIXED(MIN2(MAX2(pipe_sampler
->max_lod
, 0), 13), 6);
221 sampler
->ss1
.min_lod
= U_FIXED(MIN2(MAX2(pipe_sampler
->min_lod
, 0), 13), 6);
223 sampler
->ss2
.default_color_pointer
= sdc_gs_offset
>> 5;
228 /* All samplers must be uploaded in a single contiguous array, which
229 * complicates various things. However, this is still too confusing -
230 * FIXME: simplify all the different new texture state flags.
232 static void upload_wm_samplers(struct brw_context
*brw
)
235 unsigned sampler_count
= 0;
237 /* BRW_NEW_SAMPLER */
238 for (unit
= 0; unit
< brw
->num_textures
&& unit
< brw
->num_samplers
;
240 /* determine unit enable/disable by looking for a bound texture */
241 if (brw
->attribs
.Texture
[unit
]) {
242 const struct pipe_sampler_state
*sampler
= brw
->attribs
.Samplers
[unit
];
243 unsigned sdc_gs_offset
= upload_default_color(brw
, sampler
->border_color
);
245 brw_update_sampler_state(sampler
,
247 &brw
->wm
.sampler
[unit
]);
249 sampler_count
= unit
+ 1;
253 if (brw
->wm
.sampler_count
!= sampler_count
) {
254 brw
->wm
.sampler_count
= sampler_count
;
255 brw
->state
.dirty
.cache
|= CACHE_NEW_SAMPLER
;
258 brw
->wm
.sampler_gs_offset
= 0;
260 if (brw
->wm
.sampler_count
)
261 brw
->wm
.sampler_gs_offset
=
262 brw_cache_data_sz(&brw
->cache
[BRW_SAMPLER
],
264 sizeof(struct brw_sampler_state
) * brw
->wm
.sampler_count
);
267 const struct brw_tracked_state brw_wm_samplers
= {
269 .brw
= BRW_NEW_SAMPLER
,
272 .update
= upload_wm_samplers