Merge commit 'origin/7.8'
[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_resource.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_fragment_sampler_views,
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.fragment_sampler_views[i]->texture);
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->b.b.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->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.
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_fragment_sampler_views,
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.fragment_sampler_views[i]->texture);
131 const struct brw_sampler *sampler = brw->curr.sampler[i];
132 const float *bc;
133 float bordercolor[4] = {
134 sampler->border_color[0],
135 sampler->border_color[0],
136 sampler->border_color[0],
137 sampler->border_color[0]
138 };
139
140 if (util_format_is_depth_or_stencil(tex->b.b.format)) {
141 bc = bordercolor;
142 }
143 else {
144 bc = sampler->border_color;
145 }
146
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.
150 */
151 ret = upload_default_color(brw,
152 bc,
153 &brw->wm.sdc_bo[i]);
154 if (ret)
155 return ret;
156 }
157
158 return PIPE_OK;
159 }
160
161
162
163 /* All samplers must be uploaded in a single contiguous array.
164 */
165 static int upload_wm_samplers( struct brw_context *brw )
166 {
167 struct wm_sampler_key key;
168 struct brw_winsys_reloc reloc[BRW_MAX_TEX_UNIT];
169 enum pipe_error ret;
170 int i;
171
172 brw_wm_sampler_update_default_colors(brw);
173 brw_wm_sampler_populate_key(brw, &key);
174
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;
178 }
179
180 if (brw->wm.sampler_count == 0) {
181 bo_reference(&brw->wm.sampler_bo, NULL);
182 return PIPE_OK;
183 }
184
185 /* Emit SDC relocations */
186 for (i = 0; i < key.sampler_count; i++) {
187 make_reloc( &reloc[i],
188 BRW_USAGE_SAMPLER,
189 0,
190 i * sizeof(struct brw_sampler_state) +
191 offsetof(struct brw_sampler_state, ss2),
192 brw->wm.sdc_bo[i]);
193 }
194
195
196 if (brw_search_cache(&brw->cache, BRW_SAMPLER,
197 &key, sizeof(key),
198 reloc, key.sampler_count,
199 NULL,
200 &brw->wm.sampler_bo))
201 return PIPE_OK;
202
203 /* If we didnt find it in the cache, compute the state and put it in the
204 * cache.
205 */
206 ret = brw_upload_cache(&brw->cache, BRW_SAMPLER,
207 &key, sizeof(key),
208 reloc, key.sampler_count,
209 &key.sampler, sizeof(key.sampler),
210 NULL, NULL,
211 &brw->wm.sampler_bo);
212 if (ret)
213 return ret;
214
215
216 return 0;
217 }
218
219 const struct brw_tracked_state brw_wm_samplers = {
220 .dirty = {
221 .mesa = PIPE_NEW_BOUND_TEXTURES | PIPE_NEW_SAMPLERS,
222 .brw = 0,
223 .cache = 0
224 },
225 .prepare = upload_wm_samplers,
226 };
227
228