Merge branch 'mesa_7_7_branch'
[mesa.git] / src / gallium / drivers / i965 / brw_wm_sampler_state.c
1 /*
2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
4 develop this 3D driver.
5
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:
13
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.
17
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.
25
26 **********************************************************************/
27 /*
28 * Authors:
29 * Keith Whitwell <keith@tungstengraphics.com>
30 */
31
32 #include "util/u_math.h"
33 #include "util/u_format.h"
34
35 #include "brw_context.h"
36 #include "brw_state.h"
37 #include "brw_defines.h"
38 #include "brw_screen.h"
39
40
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.
43 */
44
45
46
47 static enum pipe_error
48 upload_default_color( struct brw_context *brw,
49 const GLfloat *color,
50 struct brw_winsys_buffer **bo_out )
51 {
52 struct brw_sampler_default_color sdc;
53 enum pipe_error ret;
54
55 COPY_4V(sdc.color, color);
56
57 ret = brw_cache_data( &brw->cache, BRW_SAMPLER_DEFAULT_COLOR, &sdc,
58 NULL, 0, bo_out );
59 if (ret)
60 return ret;
61
62 return PIPE_OK;
63 }
64
65
66 struct wm_sampler_key {
67 int sampler_count;
68 struct brw_sampler_state sampler[BRW_MAX_TEX_UNIT];
69 };
70
71
72 /** Sets up the cache key for sampler state for all texture units */
73 static void
74 brw_wm_sampler_populate_key(struct brw_context *brw,
75 struct wm_sampler_key *key)
76 {
77 int i;
78
79 memset(key, 0, sizeof(*key));
80
81 key->sampler_count = MIN2(brw->curr.num_textures,
82 brw->curr.num_samplers);
83
84 for (i = 0; i < key->sampler_count; i++) {
85 const struct brw_texture *tex = brw_texture(brw->curr.texture[i]);
86 const struct brw_sampler *sampler = brw->curr.sampler[i];
87 struct brw_sampler_state *entry = &key->sampler[i];
88
89 entry->ss0 = sampler->ss0;
90 entry->ss1 = sampler->ss1;
91 entry->ss2.default_color_pointer = 0; /* reloc */
92 entry->ss3 = sampler->ss3;
93
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.
96 */
97 if (tex->base.target == PIPE_TEXTURE_CUBE) {
98 if (FALSE &&
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;
104 } else {
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;
108 }
109 } else if (tex->base.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.
114 */
115 entry->ss1.t_wrap_mode = BRW_TEXCOORDMODE_WRAP;
116 }
117 }
118 }
119
120
121 static enum pipe_error
122 brw_wm_sampler_update_default_colors(struct brw_context *brw)
123 {
124 enum pipe_error ret;
125 int nr = MIN2(brw->curr.num_textures,
126 brw->curr.num_samplers);
127 int i;
128
129 for (i = 0; i < nr; i++) {
130 const struct brw_texture *tex = brw_texture(brw->curr.texture[i]);
131 const struct brw_sampler *sampler = brw->curr.sampler[i];
132 const float *bc;
133
134 if (util_format_is_depth_or_stencil(tex->base.format)) {
135 float bordercolor[4] = {
136 sampler->border_color[0],
137 sampler->border_color[0],
138 sampler->border_color[0],
139 sampler->border_color[0]
140 };
141
142 bc = bordercolor;
143 }
144 else {
145 bc = sampler->border_color;
146 }
147
148 /* GL specs that border color for depth textures is taken from the
149 * R channel, while the hardware uses A. Spam R into all the
150 * channels for safety.
151 */
152 ret = upload_default_color(brw,
153 bc,
154 &brw->wm.sdc_bo[i]);
155 if (ret)
156 return ret;
157 }
158
159 return PIPE_OK;
160 }
161
162
163
164 /* All samplers must be uploaded in a single contiguous array.
165 */
166 static int upload_wm_samplers( struct brw_context *brw )
167 {
168 struct wm_sampler_key key;
169 struct brw_winsys_reloc reloc[BRW_MAX_TEX_UNIT];
170 enum pipe_error ret;
171 int i;
172
173 brw_wm_sampler_update_default_colors(brw);
174 brw_wm_sampler_populate_key(brw, &key);
175
176 if (brw->wm.sampler_count != key.sampler_count) {
177 brw->wm.sampler_count = key.sampler_count;
178 brw->state.dirty.cache |= CACHE_NEW_SAMPLER;
179 }
180
181 if (brw->wm.sampler_count == 0) {
182 bo_reference(&brw->wm.sampler_bo, NULL);
183 return PIPE_OK;
184 }
185
186 /* Emit SDC relocations */
187 for (i = 0; i < key.sampler_count; i++) {
188 make_reloc( &reloc[i],
189 BRW_USAGE_SAMPLER,
190 0,
191 i * sizeof(struct brw_sampler_state) +
192 offsetof(struct brw_sampler_state, ss2),
193 brw->wm.sdc_bo[i]);
194 }
195
196
197 if (brw_search_cache(&brw->cache, BRW_SAMPLER,
198 &key, sizeof(key),
199 reloc, key.sampler_count,
200 NULL,
201 &brw->wm.sampler_bo))
202 return PIPE_OK;
203
204 /* If we didnt find it in the cache, compute the state and put it in the
205 * cache.
206 */
207 ret = brw_upload_cache(&brw->cache, BRW_SAMPLER,
208 &key, sizeof(key),
209 reloc, key.sampler_count,
210 &key.sampler, sizeof(key.sampler),
211 NULL, NULL,
212 &brw->wm.sampler_bo);
213 if (ret)
214 return ret;
215
216
217 return 0;
218 }
219
220 const struct brw_tracked_state brw_wm_samplers = {
221 .dirty = {
222 .mesa = PIPE_NEW_BOUND_TEXTURES | PIPE_NEW_SAMPLERS,
223 .brw = 0,
224 .cache = 0
225 },
226 .prepare = upload_wm_samplers,
227 };
228
229