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>
32 #include "util/u_math.h"
34 #include "brw_context.h"
35 #include "brw_state.h"
36 #include "brw_defines.h"
37 #include "brw_screen.h"
40 /* Samplers aren't strictly wm state from the hardware's perspective,
41 * but that is the only situation in which we use them in this driver.
46 static enum pipe_error
47 upload_default_color( struct brw_context
*brw
,
49 struct brw_winsys_buffer
**bo_out
)
51 struct brw_sampler_default_color sdc
;
54 COPY_4V(sdc
.color
, color
);
56 ret
= brw_cache_data( &brw
->cache
, BRW_SAMPLER_DEFAULT_COLOR
, &sdc
,
65 struct wm_sampler_key
{
67 struct brw_sampler_state sampler
[BRW_MAX_TEX_UNIT
];
71 /** Sets up the cache key for sampler state for all texture units */
73 brw_wm_sampler_populate_key(struct brw_context
*brw
,
74 struct wm_sampler_key
*key
)
78 memset(key
, 0, sizeof(*key
));
80 key
->sampler_count
= MIN2(brw
->curr
.num_textures
,
81 brw
->curr
.num_samplers
);
83 for (i
= 0; i
< key
->sampler_count
; i
++) {
84 const struct brw_texture
*tex
= brw_texture(brw
->curr
.texture
[i
]);
85 const struct brw_sampler
*sampler
= brw
->curr
.sampler
[i
];
86 struct brw_sampler_state
*entry
= &key
->sampler
[i
];
88 entry
->ss0
= sampler
->ss0
;
89 entry
->ss1
= sampler
->ss1
;
90 entry
->ss2
.default_color_pointer
= 0; /* reloc */
91 entry
->ss3
= sampler
->ss3
;
93 /* Cube-maps on 965 and later must use the same wrap mode for all 3
94 * coordinate dimensions. Futher, only CUBE and CLAMP are valid.
96 if (tex
->base
.target
== PIPE_TEXTURE_CUBE
) {
98 (sampler
->ss0
.min_filter
!= BRW_MAPFILTER_NEAREST
||
99 sampler
->ss0
.mag_filter
!= BRW_MAPFILTER_NEAREST
)) {
100 entry
->ss1
.r_wrap_mode
= BRW_TEXCOORDMODE_CUBE
;
101 entry
->ss1
.s_wrap_mode
= BRW_TEXCOORDMODE_CUBE
;
102 entry
->ss1
.t_wrap_mode
= BRW_TEXCOORDMODE_CUBE
;
104 entry
->ss1
.r_wrap_mode
= BRW_TEXCOORDMODE_CLAMP
;
105 entry
->ss1
.s_wrap_mode
= BRW_TEXCOORDMODE_CLAMP
;
106 entry
->ss1
.t_wrap_mode
= BRW_TEXCOORDMODE_CLAMP
;
108 } else if (tex
->base
.target
== PIPE_TEXTURE_1D
) {
109 /* There's a bug in 1D texture sampling - it actually pays
110 * attention to the wrap_t value, though it should not.
111 * Override the wrap_t value here to GL_REPEAT to keep
112 * any nonexistent border pixels from floating in.
114 entry
->ss1
.t_wrap_mode
= BRW_TEXCOORDMODE_WRAP
;
120 static enum pipe_error
121 brw_wm_sampler_update_default_colors(struct brw_context
*brw
)
124 int nr
= MIN2(brw
->curr
.num_textures
,
125 brw
->curr
.num_samplers
);
128 for (i
= 0; i
< nr
; i
++) {
129 const struct brw_texture
*tex
= brw_texture(brw
->curr
.texture
[i
]);
130 const struct brw_sampler
*sampler
= brw
->curr
.sampler
[i
];
133 if (pf_is_depth_or_stencil(tex
->base
.format
)) {
134 float bordercolor
[4] = {
135 sampler
->border_color
[0],
136 sampler
->border_color
[0],
137 sampler
->border_color
[0],
138 sampler
->border_color
[0]
144 bc
= sampler
->border_color
;
147 /* GL specs that border color for depth textures is taken from the
148 * R channel, while the hardware uses A. Spam R into all the
149 * channels for safety.
151 ret
= upload_default_color(brw
,
163 /* All samplers must be uploaded in a single contiguous array.
165 static int upload_wm_samplers( struct brw_context
*brw
)
167 struct wm_sampler_key key
;
171 brw_wm_sampler_update_default_colors(brw
);
172 brw_wm_sampler_populate_key(brw
, &key
);
174 if (brw
->wm
.sampler_count
!= key
.sampler_count
) {
175 brw
->wm
.sampler_count
= key
.sampler_count
;
176 brw
->state
.dirty
.cache
|= CACHE_NEW_SAMPLER
;
179 if (brw
->wm
.sampler_count
== 0) {
180 bo_reference(&brw
->wm
.sampler_bo
, NULL
);
184 if (brw_search_cache(&brw
->cache
, BRW_SAMPLER
,
186 brw
->wm
.sdc_bo
, key
.sampler_count
,
188 &brw
->wm
.sampler_bo
))
191 /* If we didnt find it in the cache, compute the state and put it in the
194 ret
= brw_upload_cache(&brw
->cache
, BRW_SAMPLER
,
196 brw
->wm
.sdc_bo
, key
.sampler_count
,
197 &key
.sampler
, sizeof(key
.sampler
),
199 &brw
->wm
.sampler_bo
);
203 /* Emit SDC relocations */
204 for (i
= 0; i
< key
.sampler_count
; i
++) {
205 ret
= brw
->sws
->bo_emit_reloc(brw
->wm
.sampler_bo
,
208 i
* sizeof(struct brw_sampler_state
) +
209 offsetof(struct brw_sampler_state
, ss2
),
218 const struct brw_tracked_state brw_wm_samplers
= {
220 .mesa
= PIPE_NEW_BOUND_TEXTURES
| PIPE_NEW_SAMPLERS
,
224 .prepare
= upload_wm_samplers
,