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"
41 /* Samplers aren't strictly wm state from the hardware's perspective,
42 * but that is the only situation in which we use them in this driver.
47 /* The brw (and related graphics cores) do not support GL_CLAMP. The
48 * Intel drivers for "other operating systems" implement GL_CLAMP as
49 * GL_CLAMP_TO_EDGE, so the same is done here.
51 static GLuint
translate_wrap_mode( GLenum wrap
)
55 return BRW_TEXCOORDMODE_WRAP
;
57 return BRW_TEXCOORDMODE_CLAMP
;
58 case GL_CLAMP_TO_EDGE
:
59 return BRW_TEXCOORDMODE_CLAMP
; /* conform likes it this way */
60 case GL_CLAMP_TO_BORDER
:
61 return BRW_TEXCOORDMODE_CLAMP_BORDER
;
62 case GL_MIRRORED_REPEAT
:
63 return BRW_TEXCOORDMODE_MIRROR
;
65 return BRW_TEXCOORDMODE_WRAP
;
70 static GLuint
U_FIXED(GLfloat value
, GLuint frac_bits
)
72 value
*= (1<<frac_bits
);
73 return value
< 0 ? 0 : value
;
76 static GLint
S_FIXED(GLfloat value
, GLuint frac_bits
)
78 return value
* (1<<frac_bits
);
82 static GLuint
upload_default_color( struct brw_context
*brw
,
83 const GLfloat
*color
)
85 struct brw_sampler_default_color sdc
;
87 COPY_4V(sdc
.color
, color
);
89 return brw_cache_data( &brw
->cache
[BRW_SAMPLER_DEFAULT_COLOR
], &sdc
);
95 static void brw_update_sampler_state( struct gl_texture_unit
*texUnit
,
96 struct gl_texture_object
*texObj
,
98 struct brw_sampler_state
*sampler
)
100 _mesa_memset(sampler
, 0, sizeof(*sampler
));
102 switch (texObj
->MinFilter
) {
104 sampler
->ss0
.min_filter
= BRW_MAPFILTER_NEAREST
;
105 sampler
->ss0
.mip_filter
= BRW_MIPFILTER_NONE
;
108 sampler
->ss0
.min_filter
= BRW_MAPFILTER_LINEAR
;
109 sampler
->ss0
.mip_filter
= BRW_MIPFILTER_NONE
;
111 case GL_NEAREST_MIPMAP_NEAREST
:
112 sampler
->ss0
.min_filter
= BRW_MAPFILTER_NEAREST
;
113 sampler
->ss0
.mip_filter
= BRW_MIPFILTER_NEAREST
;
115 case GL_LINEAR_MIPMAP_NEAREST
:
116 sampler
->ss0
.min_filter
= BRW_MAPFILTER_LINEAR
;
117 sampler
->ss0
.mip_filter
= BRW_MIPFILTER_NEAREST
;
119 case GL_NEAREST_MIPMAP_LINEAR
:
120 sampler
->ss0
.min_filter
= BRW_MAPFILTER_NEAREST
;
121 sampler
->ss0
.mip_filter
= BRW_MIPFILTER_LINEAR
;
123 case GL_LINEAR_MIPMAP_LINEAR
:
124 sampler
->ss0
.min_filter
= BRW_MAPFILTER_LINEAR
;
125 sampler
->ss0
.mip_filter
= BRW_MIPFILTER_LINEAR
;
133 if ( texObj
->MaxAnisotropy
> 1.0 ) {
134 sampler
->ss0
.min_filter
= BRW_MAPFILTER_ANISOTROPIC
;
135 sampler
->ss0
.mag_filter
= BRW_MAPFILTER_ANISOTROPIC
;
137 if (texObj
->MaxAnisotropy
> 2.0) {
138 sampler
->ss3
.max_aniso
= MAX2((texObj
->MaxAnisotropy
- 2) / 2,
143 switch (texObj
->MagFilter
) {
145 sampler
->ss0
.mag_filter
= BRW_MAPFILTER_NEAREST
;
148 sampler
->ss0
.mag_filter
= BRW_MAPFILTER_LINEAR
;
155 sampler
->ss1
.r_wrap_mode
= translate_wrap_mode(texObj
->WrapR
);
156 sampler
->ss1
.s_wrap_mode
= translate_wrap_mode(texObj
->WrapS
);
157 sampler
->ss1
.t_wrap_mode
= translate_wrap_mode(texObj
->WrapT
);
159 /* Fulsim complains if I don't do this. Hardware doesn't mind:
162 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
163 sampler
->ss1
.r_wrap_mode
= BRW_TEXCOORDMODE_CUBE
;
164 sampler
->ss1
.s_wrap_mode
= BRW_TEXCOORDMODE_CUBE
;
165 sampler
->ss1
.t_wrap_mode
= BRW_TEXCOORDMODE_CUBE
;
169 /* Set shadow function:
171 if (texObj
->CompareMode
== GL_COMPARE_R_TO_TEXTURE_ARB
) {
172 /* Shadowing is "enabled" by emitting a particular sampler
173 * message (sample_c). So need to recompile WM program when
174 * shadow comparison is enabled on each/any texture unit.
176 sampler
->ss0
.shadow_function
= intel_translate_compare_func(texObj
->CompareFunc
);
181 sampler
->ss0
.lod_bias
= S_FIXED(CLAMP(texUnit
->LodBias
+ texObj
->LodBias
, -16, 15), 6);
183 sampler
->ss0
.lod_preclamp
= 1; /* OpenGL mode */
184 sampler
->ss0
.default_color_mode
= 0; /* OpenGL/DX10 mode */
186 /* Set BaseMipLevel, MaxLOD, MinLOD:
188 * XXX: I don't think that using firstLevel, lastLevel works,
189 * because we always setup the surface state as if firstLevel ==
190 * level zero. Probably have to subtract firstLevel from each of
193 sampler
->ss0
.base_level
= U_FIXED(0, 1);
195 sampler
->ss1
.max_lod
= U_FIXED(MIN2(MAX2(texObj
->MaxLod
, 0), 13), 6);
196 sampler
->ss1
.min_lod
= U_FIXED(MIN2(MAX2(texObj
->MinLod
, 0), 13), 6);
198 sampler
->ss2
.default_color_pointer
= sdc_gs_offset
>> 5;
203 /* All samplers must be uploaded in a single contiguous array, which
204 * complicates various things. However, this is still too confusing -
205 * FIXME: simplify all the different new texture state flags.
207 static void upload_wm_samplers( struct brw_context
*brw
)
210 GLuint sampler_count
= 0;
213 for (unit
= 0; unit
< BRW_MAX_TEX_UNIT
; unit
++) {
214 if (brw
->attribs
.Texture
->Unit
[unit
]._ReallyEnabled
) {
215 struct gl_texture_unit
*texUnit
= &brw
->attribs
.Texture
->Unit
[unit
];
216 struct gl_texture_object
*texObj
= texUnit
->_Current
;
218 GLuint sdc_gs_offset
= upload_default_color(brw
, texObj
->BorderColor
);
220 brw_update_sampler_state(texUnit
,
223 &brw
->wm
.sampler
[unit
]);
225 sampler_count
= unit
+ 1;
229 if (brw
->wm
.sampler_count
!= sampler_count
) {
230 brw
->wm
.sampler_count
= sampler_count
;
231 brw
->state
.dirty
.cache
|= CACHE_NEW_SAMPLER
;
234 brw
->wm
.sampler_gs_offset
= 0;
236 if (brw
->wm
.sampler_count
)
237 brw
->wm
.sampler_gs_offset
=
238 brw_cache_data_sz(&brw
->cache
[BRW_SAMPLER
],
240 sizeof(struct brw_sampler_state
) * brw
->wm
.sampler_count
);
244 const struct brw_tracked_state brw_wm_samplers
= {
246 .mesa
= _NEW_TEXTURE
,
250 .update
= upload_wm_samplers