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"
33 #include "util/u_format.h"
35 #include "brw_context.h"
36 #include "brw_state.h"
37 #include "brw_defines.h"
38 #include "brw_resource.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 static enum pipe_error
48 upload_default_color( struct brw_context
*brw
,
50 struct brw_winsys_buffer
**bo_out
)
52 struct brw_sampler_default_color sdc
;
55 COPY_4V(sdc
.color
, color
);
57 ret
= brw_cache_data( &brw
->cache
, BRW_SAMPLER_DEFAULT_COLOR
, &sdc
,
66 struct wm_sampler_key
{
68 struct brw_sampler_state sampler
[BRW_MAX_TEX_UNIT
];
72 /** Sets up the cache key for sampler state for all texture units */
74 brw_wm_sampler_populate_key(struct brw_context
*brw
,
75 struct wm_sampler_key
*key
)
79 memset(key
, 0, sizeof(*key
));
81 key
->sampler_count
= MIN2(brw
->curr
.num_fragment_sampler_views
,
82 brw
->curr
.num_samplers
);
84 for (i
= 0; i
< key
->sampler_count
; i
++) {
85 const struct brw_texture
*tex
= brw_texture(brw
->curr
.fragment_sampler_views
[i
]->texture
);
86 const struct brw_sampler
*sampler
= brw
->curr
.sampler
[i
];
87 struct brw_sampler_state
*entry
= &key
->sampler
[i
];
89 entry
->ss0
= sampler
->ss0
;
90 entry
->ss1
= sampler
->ss1
;
91 entry
->ss2
.default_color_pointer
= 0; /* reloc */
92 entry
->ss3
= sampler
->ss3
;
94 /* Cube-maps on 965 and later must use the same wrap mode for all 3
95 * coordinate dimensions. Futher, only CUBE and CLAMP are valid.
97 if (tex
->b
.b
.target
== PIPE_TEXTURE_CUBE
) {
99 (sampler
->ss0
.min_filter
!= BRW_MAPFILTER_NEAREST
||
100 sampler
->ss0
.mag_filter
!= BRW_MAPFILTER_NEAREST
)) {
101 entry
->ss1
.r_wrap_mode
= BRW_TEXCOORDMODE_CUBE
;
102 entry
->ss1
.s_wrap_mode
= BRW_TEXCOORDMODE_CUBE
;
103 entry
->ss1
.t_wrap_mode
= BRW_TEXCOORDMODE_CUBE
;
105 entry
->ss1
.r_wrap_mode
= BRW_TEXCOORDMODE_CLAMP
;
106 entry
->ss1
.s_wrap_mode
= BRW_TEXCOORDMODE_CLAMP
;
107 entry
->ss1
.t_wrap_mode
= BRW_TEXCOORDMODE_CLAMP
;
109 } else if (tex
->b
.b
.target
== PIPE_TEXTURE_1D
) {
110 /* There's a bug in 1D texture sampling - it actually pays
111 * attention to the wrap_t value, though it should not.
112 * Override the wrap_t value here to GL_REPEAT to keep
113 * any nonexistent border pixels from floating in.
115 entry
->ss1
.t_wrap_mode
= BRW_TEXCOORDMODE_WRAP
;
121 static enum pipe_error
122 brw_wm_sampler_update_default_colors(struct brw_context
*brw
)
125 int nr
= MIN2(brw
->curr
.num_fragment_sampler_views
,
126 brw
->curr
.num_samplers
);
129 for (i
= 0; i
< nr
; i
++) {
130 const struct brw_texture
*tex
= brw_texture(brw
->curr
.fragment_sampler_views
[i
]->texture
);
131 const struct brw_sampler
*sampler
= brw
->curr
.sampler
[i
];
133 float bordercolor
[4] = {
134 sampler
->border_color
[0],
135 sampler
->border_color
[0],
136 sampler
->border_color
[0],
137 sampler
->border_color
[0]
140 if (util_format_is_depth_or_stencil(tex
->b
.b
.format
)) {
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
;
168 struct brw_winsys_reloc reloc
[BRW_MAX_TEX_UNIT
];
172 brw_wm_sampler_update_default_colors(brw
);
173 brw_wm_sampler_populate_key(brw
, &key
);
175 if (brw
->wm
.sampler_count
!= key
.sampler_count
) {
176 brw
->wm
.sampler_count
= key
.sampler_count
;
177 brw
->state
.dirty
.cache
|= CACHE_NEW_SAMPLER
;
180 if (brw
->wm
.sampler_count
== 0) {
181 bo_reference(&brw
->wm
.sampler_bo
, NULL
);
185 /* Emit SDC relocations */
186 for (i
= 0; i
< key
.sampler_count
; i
++) {
187 make_reloc( &reloc
[i
],
190 i
* sizeof(struct brw_sampler_state
) +
191 offsetof(struct brw_sampler_state
, ss2
),
196 if (brw_search_cache(&brw
->cache
, BRW_SAMPLER
,
198 reloc
, key
.sampler_count
,
200 &brw
->wm
.sampler_bo
))
203 /* If we didnt find it in the cache, compute the state and put it in the
206 ret
= brw_upload_cache(&brw
->cache
, BRW_SAMPLER
,
208 reloc
, key
.sampler_count
,
209 &key
.sampler
, sizeof(key
.sampler
),
211 &brw
->wm
.sampler_bo
);
219 const struct brw_tracked_state brw_wm_samplers
= {
221 .mesa
= PIPE_NEW_BOUND_TEXTURES
| PIPE_NEW_SAMPLERS
,
225 .prepare
= upload_wm_samplers
,