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 dri_bo
*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
,
96 static void brw_update_sampler_state( struct gl_texture_unit
*texUnit
,
97 struct gl_texture_object
*texObj
,
99 struct brw_sampler_state
*sampler
)
101 _mesa_memset(sampler
, 0, sizeof(*sampler
));
103 switch (texObj
->MinFilter
) {
105 sampler
->ss0
.min_filter
= BRW_MAPFILTER_NEAREST
;
106 sampler
->ss0
.mip_filter
= BRW_MIPFILTER_NONE
;
109 sampler
->ss0
.min_filter
= BRW_MAPFILTER_LINEAR
;
110 sampler
->ss0
.mip_filter
= BRW_MIPFILTER_NONE
;
112 case GL_NEAREST_MIPMAP_NEAREST
:
113 sampler
->ss0
.min_filter
= BRW_MAPFILTER_NEAREST
;
114 sampler
->ss0
.mip_filter
= BRW_MIPFILTER_NEAREST
;
116 case GL_LINEAR_MIPMAP_NEAREST
:
117 sampler
->ss0
.min_filter
= BRW_MAPFILTER_LINEAR
;
118 sampler
->ss0
.mip_filter
= BRW_MIPFILTER_NEAREST
;
120 case GL_NEAREST_MIPMAP_LINEAR
:
121 sampler
->ss0
.min_filter
= BRW_MAPFILTER_NEAREST
;
122 sampler
->ss0
.mip_filter
= BRW_MIPFILTER_LINEAR
;
124 case GL_LINEAR_MIPMAP_LINEAR
:
125 sampler
->ss0
.min_filter
= BRW_MAPFILTER_LINEAR
;
126 sampler
->ss0
.mip_filter
= BRW_MIPFILTER_LINEAR
;
134 if ( texObj
->MaxAnisotropy
> 1.0 ) {
135 sampler
->ss0
.min_filter
= BRW_MAPFILTER_ANISOTROPIC
;
136 sampler
->ss0
.mag_filter
= BRW_MAPFILTER_ANISOTROPIC
;
138 if (texObj
->MaxAnisotropy
> 2.0) {
139 sampler
->ss3
.max_aniso
= MAX2((texObj
->MaxAnisotropy
- 2) / 2,
144 switch (texObj
->MagFilter
) {
146 sampler
->ss0
.mag_filter
= BRW_MAPFILTER_NEAREST
;
149 sampler
->ss0
.mag_filter
= BRW_MAPFILTER_LINEAR
;
156 sampler
->ss1
.r_wrap_mode
= translate_wrap_mode(texObj
->WrapR
);
157 sampler
->ss1
.s_wrap_mode
= translate_wrap_mode(texObj
->WrapS
);
158 sampler
->ss1
.t_wrap_mode
= translate_wrap_mode(texObj
->WrapT
);
160 /* Fulsim complains if I don't do this. Hardware doesn't mind:
163 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
164 sampler
->ss1
.r_wrap_mode
= BRW_TEXCOORDMODE_CUBE
;
165 sampler
->ss1
.s_wrap_mode
= BRW_TEXCOORDMODE_CUBE
;
166 sampler
->ss1
.t_wrap_mode
= BRW_TEXCOORDMODE_CUBE
;
170 /* Set shadow function:
172 if (texObj
->CompareMode
== GL_COMPARE_R_TO_TEXTURE_ARB
) {
173 /* Shadowing is "enabled" by emitting a particular sampler
174 * message (sample_c). So need to recompile WM program when
175 * shadow comparison is enabled on each/any texture unit.
177 sampler
->ss0
.shadow_function
= intel_translate_shadow_compare_func(texObj
->CompareFunc
);
182 sampler
->ss0
.lod_bias
= S_FIXED(CLAMP(texUnit
->LodBias
+ texObj
->LodBias
, -16, 15), 6);
184 sampler
->ss0
.lod_preclamp
= 1; /* OpenGL mode */
185 sampler
->ss0
.default_color_mode
= 0; /* OpenGL/DX10 mode */
187 /* Set BaseMipLevel, MaxLOD, MinLOD:
189 * XXX: I don't think that using firstLevel, lastLevel works,
190 * because we always setup the surface state as if firstLevel ==
191 * level zero. Probably have to subtract firstLevel from each of
194 sampler
->ss0
.base_level
= U_FIXED(0, 1);
196 sampler
->ss1
.max_lod
= U_FIXED(MIN2(MAX2(texObj
->MaxLod
, 0), 13), 6);
197 sampler
->ss1
.min_lod
= U_FIXED(MIN2(MAX2(texObj
->MinLod
, 0), 13), 6);
199 sampler
->ss2
.default_color_pointer
= sdc_bo
->offset
>> 5; /* reloc */
204 /* All samplers must be uploaded in a single contiguous array, which
205 * complicates various things. However, this is still too confusing -
206 * FIXME: simplify all the different new texture state flags.
208 static void upload_wm_samplers( struct brw_context
*brw
)
211 GLuint sampler_count
= 0;
212 dri_bo
*reloc_bufs
[BRW_MAX_TEX_UNIT
];
215 for (unit
= 0; unit
< BRW_MAX_TEX_UNIT
; unit
++) {
216 if (brw
->attribs
.Texture
->Unit
[unit
]._ReallyEnabled
) {
217 struct gl_texture_unit
*texUnit
= &brw
->attribs
.Texture
->Unit
[unit
];
218 struct gl_texture_object
*texObj
= texUnit
->_Current
;
220 dri_bo_unreference(brw
->wm
.sdc_bo
[unit
]);
221 brw
->wm
.sdc_bo
[unit
] = upload_default_color(brw
, texObj
->BorderColor
);
223 brw_update_sampler_state(texUnit
,
225 brw
->wm
.sdc_bo
[unit
],
226 &brw
->wm
.sampler
[unit
]);
228 sampler_count
= unit
+ 1;
230 dri_bo_unreference(brw
->wm
.sdc_bo
[unit
]);
231 brw
->wm
.sdc_bo
[unit
] = NULL
;
233 reloc_bufs
[unit
] = brw
->wm
.sdc_bo
[unit
];
236 if (brw
->wm
.sampler_count
!= sampler_count
) {
237 brw
->wm
.sampler_count
= sampler_count
;
238 brw
->state
.dirty
.cache
|= CACHE_NEW_SAMPLER
;
241 dri_bo_unreference(brw
->wm
.sampler_bo
);
242 if (brw
->wm
.sampler_count
) {
244 brw_cache_data_sz(&brw
->cache
, BRW_SAMPLER
,
246 sizeof(struct brw_sampler_state
) *
247 brw
->wm
.sampler_count
,
248 reloc_bufs
, BRW_MAX_TEX_UNIT
);
250 brw
->wm
.sampler_bo
= NULL
;
254 static void emit_reloc_wm_samplers(struct brw_context
*brw
)
258 if (brw
->wm
.sampler_count
== 0)
261 /* Emit SDC relocations */
262 for (unit
= 0; unit
< BRW_MAX_TEX_UNIT
; unit
++) {
263 if (!brw
->attribs
.Texture
->Unit
[unit
]._ReallyEnabled
)
266 dri_emit_reloc(brw
->wm
.sampler_bo
,
267 DRM_BO_FLAG_MEM_TT
| DRM_BO_FLAG_READ
,
269 unit
* sizeof(struct brw_sampler_state
) +
270 offsetof(struct brw_sampler_state
, ss2
),
271 brw
->wm
.sdc_bo
[unit
]);
275 const struct brw_tracked_state brw_wm_samplers
= {
277 .mesa
= _NEW_TEXTURE
,
281 .update
= upload_wm_samplers
,
282 .emit_reloc
= emit_reloc_wm_samplers
,