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 "main/macros.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
,
94 struct wm_sampler_key
{
97 struct wm_sampler_entry
{
99 GLenum wrap_r
, wrap_s
, wrap_t
;
100 float maxlod
, minlod
;
103 GLenum minfilter
, magfilter
;
104 GLenum comparemode
, comparefunc
;
106 } sampler
[BRW_MAX_TEX_UNIT
];
110 * Sets the sampler state for a single unit based off of the sampler key
113 static void brw_update_sampler_state(struct wm_sampler_entry
*key
,
115 struct brw_sampler_state
*sampler
)
117 _mesa_memset(sampler
, 0, sizeof(*sampler
));
119 switch (key
->minfilter
) {
121 sampler
->ss0
.min_filter
= BRW_MAPFILTER_NEAREST
;
122 sampler
->ss0
.mip_filter
= BRW_MIPFILTER_NONE
;
125 sampler
->ss0
.min_filter
= BRW_MAPFILTER_LINEAR
;
126 sampler
->ss0
.mip_filter
= BRW_MIPFILTER_NONE
;
128 case GL_NEAREST_MIPMAP_NEAREST
:
129 sampler
->ss0
.min_filter
= BRW_MAPFILTER_NEAREST
;
130 sampler
->ss0
.mip_filter
= BRW_MIPFILTER_NEAREST
;
132 case GL_LINEAR_MIPMAP_NEAREST
:
133 sampler
->ss0
.min_filter
= BRW_MAPFILTER_LINEAR
;
134 sampler
->ss0
.mip_filter
= BRW_MIPFILTER_NEAREST
;
136 case GL_NEAREST_MIPMAP_LINEAR
:
137 sampler
->ss0
.min_filter
= BRW_MAPFILTER_NEAREST
;
138 sampler
->ss0
.mip_filter
= BRW_MIPFILTER_LINEAR
;
140 case GL_LINEAR_MIPMAP_LINEAR
:
141 sampler
->ss0
.min_filter
= BRW_MAPFILTER_LINEAR
;
142 sampler
->ss0
.mip_filter
= BRW_MIPFILTER_LINEAR
;
150 if (key
->max_aniso
> 1.0) {
151 sampler
->ss0
.min_filter
= BRW_MAPFILTER_ANISOTROPIC
;
152 sampler
->ss0
.mag_filter
= BRW_MAPFILTER_ANISOTROPIC
;
154 if (key
->max_aniso
> 2.0) {
155 sampler
->ss3
.max_aniso
= MAX2((key
->max_aniso
- 2) / 2,
160 switch (key
->magfilter
) {
162 sampler
->ss0
.mag_filter
= BRW_MAPFILTER_NEAREST
;
165 sampler
->ss0
.mag_filter
= BRW_MAPFILTER_LINEAR
;
172 if (key
->tex_target
== GL_TEXTURE_CUBE_MAP
&&
173 (key
->minfilter
!= GL_NEAREST
|| key
->magfilter
!= GL_NEAREST
)) {
174 /* If we're using anything but nearest sampling for a cube map, we
175 * need to set this wrap mode to avoid GPU lock-ups.
177 sampler
->ss1
.r_wrap_mode
= BRW_TEXCOORDMODE_CUBE
;
178 sampler
->ss1
.s_wrap_mode
= BRW_TEXCOORDMODE_CUBE
;
179 sampler
->ss1
.t_wrap_mode
= BRW_TEXCOORDMODE_CUBE
;
182 sampler
->ss1
.r_wrap_mode
= translate_wrap_mode(key
->wrap_r
);
183 sampler
->ss1
.s_wrap_mode
= translate_wrap_mode(key
->wrap_s
);
184 sampler
->ss1
.t_wrap_mode
= translate_wrap_mode(key
->wrap_t
);
187 /* Set shadow function:
189 if (key
->comparemode
== GL_COMPARE_R_TO_TEXTURE_ARB
) {
190 /* Shadowing is "enabled" by emitting a particular sampler
191 * message (sample_c). So need to recompile WM program when
192 * shadow comparison is enabled on each/any texture unit.
194 sampler
->ss0
.shadow_function
=
195 intel_translate_shadow_compare_func(key
->comparefunc
);
200 sampler
->ss0
.lod_bias
= S_FIXED(CLAMP(key
->lod_bias
, -16, 15), 6);
202 sampler
->ss0
.lod_preclamp
= 1; /* OpenGL mode */
203 sampler
->ss0
.default_color_mode
= 0; /* OpenGL/DX10 mode */
205 /* Set BaseMipLevel, MaxLOD, MinLOD:
207 * XXX: I don't think that using firstLevel, lastLevel works,
208 * because we always setup the surface state as if firstLevel ==
209 * level zero. Probably have to subtract firstLevel from each of
212 sampler
->ss0
.base_level
= U_FIXED(0, 1);
214 sampler
->ss1
.max_lod
= U_FIXED(MIN2(MAX2(key
->maxlod
, 0), 13), 6);
215 sampler
->ss1
.min_lod
= U_FIXED(MIN2(MAX2(key
->minlod
, 0), 13), 6);
217 sampler
->ss2
.default_color_pointer
= sdc_bo
->offset
>> 5; /* reloc */
223 * Setup the sampler state for the "constant buffer texture".
224 * The constant buffer texture is basically a 1D float[4] texture whose
225 * width == the number of program parameters/constants.
228 brwm_wm_setup_constant_sampler(struct brw_context
*brw
,
229 struct wm_sampler_key
*key
)
231 static const GLfloat black
[4] = {0.0F
, 0.0F
, 0.0F
, 0.0F
};
232 const GLuint unit
= BRW_MAX_TEX_UNIT
- 1;
233 struct wm_sampler_entry
*entry
= &key
->sampler
[unit
];
235 entry
->tex_target
= GL_TEXTURE_1D
;
237 entry
->wrap_r
= GL_CLAMP_TO_EDGE
;
238 entry
->wrap_s
= GL_CLAMP_TO_EDGE
;
239 entry
->wrap_t
= GL_CLAMP_TO_EDGE
;
241 entry
->maxlod
= 0.0F
;
242 entry
->minlod
= 0.0F
;
243 entry
->lod_bias
= 0.0F
;
244 entry
->max_aniso
= 0.0F
;
245 entry
->minfilter
= GL_NEAREST
;
246 entry
->magfilter
= GL_NEAREST
;
247 entry
->comparemode
= GL_NONE
;
248 entry
->comparefunc
= GL_ALWAYS
;
250 dri_bo_unreference(brw
->wm
.sdc_bo
[unit
]);
252 brw
->wm
.sdc_bo
[unit
] = upload_default_color(brw
, black
);
254 key
->sampler_count
= unit
+ 1;
259 /** Sets up the cache key for sampler state for all texture units */
261 brw_wm_sampler_populate_key(struct brw_context
*brw
,
262 struct wm_sampler_key
*key
)
264 GLcontext
*ctx
= &brw
->intel
.ctx
;
267 memset(key
, 0, sizeof(*key
));
269 for (unit
= 0; unit
< BRW_MAX_TEX_UNIT
; unit
++) {
270 if (ctx
->Texture
.Unit
[unit
]._ReallyEnabled
) {
271 struct wm_sampler_entry
*entry
= &key
->sampler
[unit
];
272 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
273 struct gl_texture_object
*texObj
= texUnit
->_Current
;
274 struct intel_texture_object
*intelObj
= intel_texture_object(texObj
);
275 struct gl_texture_image
*firstImage
=
276 texObj
->Image
[0][intelObj
->firstLevel
];
278 entry
->tex_target
= texObj
->Target
;
280 entry
->wrap_r
= texObj
->WrapR
;
281 entry
->wrap_s
= texObj
->WrapS
;
282 entry
->wrap_t
= texObj
->WrapT
;
284 entry
->maxlod
= texObj
->MaxLod
;
285 entry
->minlod
= texObj
->MinLod
;
286 entry
->lod_bias
= texUnit
->LodBias
+ texObj
->LodBias
;
287 entry
->max_aniso
= texObj
->MaxAnisotropy
;
288 entry
->minfilter
= texObj
->MinFilter
;
289 entry
->magfilter
= texObj
->MagFilter
;
290 entry
->comparemode
= texObj
->CompareMode
;
291 entry
->comparefunc
= texObj
->CompareFunc
;
293 dri_bo_unreference(brw
->wm
.sdc_bo
[unit
]);
294 if (firstImage
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
295 float bordercolor
[4] = {
296 texObj
->BorderColor
[0],
297 texObj
->BorderColor
[0],
298 texObj
->BorderColor
[0],
299 texObj
->BorderColor
[0]
301 /* GL specs that border color for depth textures is taken from the
302 * R channel, while the hardware uses A. Spam R into all the
303 * channels for safety.
305 brw
->wm
.sdc_bo
[unit
] = upload_default_color(brw
, bordercolor
);
307 brw
->wm
.sdc_bo
[unit
] = upload_default_color(brw
,
308 texObj
->BorderColor
);
310 key
->sampler_count
= unit
+ 1;
315 brwm_wm_setup_constant_sampler(brw
, key
);
319 /* All samplers must be uploaded in a single contiguous array, which
320 * complicates various things. However, this is still too confusing -
321 * FIXME: simplify all the different new texture state flags.
323 static void upload_wm_samplers( struct brw_context
*brw
)
325 GLcontext
*ctx
= &brw
->intel
.ctx
;
326 struct wm_sampler_key key
;
329 brw_wm_sampler_populate_key(brw
, &key
);
331 if (brw
->wm
.sampler_count
!= key
.sampler_count
) {
332 brw
->wm
.sampler_count
= key
.sampler_count
;
333 brw
->state
.dirty
.cache
|= CACHE_NEW_SAMPLER
;
336 dri_bo_unreference(brw
->wm
.sampler_bo
);
337 brw
->wm
.sampler_bo
= NULL
;
338 if (brw
->wm
.sampler_count
== 0)
341 brw
->wm
.sampler_bo
= brw_search_cache(&brw
->cache
, BRW_SAMPLER
,
343 brw
->wm
.sdc_bo
, key
.sampler_count
,
346 /* If we didnt find it in the cache, compute the state and put it in the
349 if (brw
->wm
.sampler_bo
== NULL
) {
350 struct brw_sampler_state sampler
[BRW_MAX_TEX_UNIT
];
352 memset(sampler
, 0, sizeof(sampler
));
353 for (i
= 0; i
< key
.sampler_count
; i
++) {
354 if (brw
->wm
.sdc_bo
[i
] == NULL
)
357 brw_update_sampler_state(&key
.sampler
[i
], brw
->wm
.sdc_bo
[i
],
361 brw
->wm
.sampler_bo
= brw_upload_cache(&brw
->cache
, BRW_SAMPLER
,
363 brw
->wm
.sdc_bo
, key
.sampler_count
,
364 &sampler
, sizeof(sampler
),
367 /* Emit SDC relocations */
368 for (i
= 0; i
< BRW_MAX_TEX_UNIT
; i
++) {
369 if (!ctx
->Texture
.Unit
[i
]._ReallyEnabled
)
372 dri_bo_emit_reloc(brw
->wm
.sampler_bo
,
373 I915_GEM_DOMAIN_SAMPLER
, 0,
375 i
* sizeof(struct brw_sampler_state
) +
376 offsetof(struct brw_sampler_state
, ss2
),
382 const struct brw_tracked_state brw_wm_samplers
= {
384 .mesa
= _NEW_TEXTURE
,
388 .prepare
= upload_wm_samplers
,