intel: Add a batch flush between front-buffer downsample and X protocol.
[mesa.git] / src / mesa / drivers / dri / 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
33 #include "brw_context.h"
34 #include "brw_state.h"
35 #include "brw_defines.h"
36
37 #include "main/macros.h"
38 #include "main/samplerobj.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 uint32_t
48 translate_wrap_mode(GLenum wrap, bool using_nearest)
49 {
50 switch( wrap ) {
51 case GL_REPEAT:
52 return BRW_TEXCOORDMODE_WRAP;
53 case GL_CLAMP:
54 /* GL_CLAMP is the weird mode where coordinates are clamped to
55 * [0.0, 1.0], so linear filtering of coordinates outside of
56 * [0.0, 1.0] give you half edge texel value and half border
57 * color. The fragment shader will clamp the coordinates, and
58 * we set clamp_border here, which gets the result desired. We
59 * just use clamp(_to_edge) for nearest, because for nearest
60 * clamping to 1.0 gives border color instead of the desired
61 * edge texels.
62 */
63 if (using_nearest)
64 return BRW_TEXCOORDMODE_CLAMP;
65 else
66 return BRW_TEXCOORDMODE_CLAMP_BORDER;
67 case GL_CLAMP_TO_EDGE:
68 return BRW_TEXCOORDMODE_CLAMP;
69 case GL_CLAMP_TO_BORDER:
70 return BRW_TEXCOORDMODE_CLAMP_BORDER;
71 case GL_MIRRORED_REPEAT:
72 return BRW_TEXCOORDMODE_MIRROR;
73 default:
74 return BRW_TEXCOORDMODE_WRAP;
75 }
76 }
77
78 /**
79 * Upload SAMPLER_BORDER_COLOR_STATE.
80 */
81 void
82 upload_default_color(struct brw_context *brw,
83 struct gl_sampler_object *sampler,
84 int unit,
85 uint32_t *sdc_offset)
86 {
87 struct gl_context *ctx = &brw->ctx;
88 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
89 struct gl_texture_object *texObj = texUnit->_Current;
90 struct gl_texture_image *firstImage = texObj->Image[0][texObj->BaseLevel];
91 float color[4];
92
93 switch (firstImage->_BaseFormat) {
94 case GL_DEPTH_COMPONENT:
95 /* GL specs that border color for depth textures is taken from the
96 * R channel, while the hardware uses A. Spam R into all the
97 * channels for safety.
98 */
99 color[0] = sampler->BorderColor.f[0];
100 color[1] = sampler->BorderColor.f[0];
101 color[2] = sampler->BorderColor.f[0];
102 color[3] = sampler->BorderColor.f[0];
103 break;
104 case GL_ALPHA:
105 color[0] = 0.0;
106 color[1] = 0.0;
107 color[2] = 0.0;
108 color[3] = sampler->BorderColor.f[3];
109 break;
110 case GL_INTENSITY:
111 color[0] = sampler->BorderColor.f[0];
112 color[1] = sampler->BorderColor.f[0];
113 color[2] = sampler->BorderColor.f[0];
114 color[3] = sampler->BorderColor.f[0];
115 break;
116 case GL_LUMINANCE:
117 color[0] = sampler->BorderColor.f[0];
118 color[1] = sampler->BorderColor.f[0];
119 color[2] = sampler->BorderColor.f[0];
120 color[3] = 1.0;
121 break;
122 case GL_LUMINANCE_ALPHA:
123 color[0] = sampler->BorderColor.f[0];
124 color[1] = sampler->BorderColor.f[0];
125 color[2] = sampler->BorderColor.f[0];
126 color[3] = sampler->BorderColor.f[3];
127 break;
128 default:
129 color[0] = sampler->BorderColor.f[0];
130 color[1] = sampler->BorderColor.f[1];
131 color[2] = sampler->BorderColor.f[2];
132 color[3] = sampler->BorderColor.f[3];
133 break;
134 }
135
136 /* In some cases we use an RGBA surface format for GL RGB textures,
137 * where we've initialized the A channel to 1.0. We also have to set
138 * the border color alpha to 1.0 in that case.
139 */
140 if (firstImage->_BaseFormat == GL_RGB)
141 color[3] = 1.0;
142
143 if (brw->gen == 5 || brw->gen == 6) {
144 struct gen5_sampler_default_color *sdc;
145
146 sdc = brw_state_batch(brw, AUB_TRACE_SAMPLER_DEFAULT_COLOR,
147 sizeof(*sdc), 32, sdc_offset);
148
149 memset(sdc, 0, sizeof(*sdc));
150
151 UNCLAMPED_FLOAT_TO_UBYTE(sdc->ub[0], color[0]);
152 UNCLAMPED_FLOAT_TO_UBYTE(sdc->ub[1], color[1]);
153 UNCLAMPED_FLOAT_TO_UBYTE(sdc->ub[2], color[2]);
154 UNCLAMPED_FLOAT_TO_UBYTE(sdc->ub[3], color[3]);
155
156 UNCLAMPED_FLOAT_TO_USHORT(sdc->us[0], color[0]);
157 UNCLAMPED_FLOAT_TO_USHORT(sdc->us[1], color[1]);
158 UNCLAMPED_FLOAT_TO_USHORT(sdc->us[2], color[2]);
159 UNCLAMPED_FLOAT_TO_USHORT(sdc->us[3], color[3]);
160
161 UNCLAMPED_FLOAT_TO_SHORT(sdc->s[0], color[0]);
162 UNCLAMPED_FLOAT_TO_SHORT(sdc->s[1], color[1]);
163 UNCLAMPED_FLOAT_TO_SHORT(sdc->s[2], color[2]);
164 UNCLAMPED_FLOAT_TO_SHORT(sdc->s[3], color[3]);
165
166 sdc->hf[0] = _mesa_float_to_half(color[0]);
167 sdc->hf[1] = _mesa_float_to_half(color[1]);
168 sdc->hf[2] = _mesa_float_to_half(color[2]);
169 sdc->hf[3] = _mesa_float_to_half(color[3]);
170
171 sdc->b[0] = sdc->s[0] >> 8;
172 sdc->b[1] = sdc->s[1] >> 8;
173 sdc->b[2] = sdc->s[2] >> 8;
174 sdc->b[3] = sdc->s[3] >> 8;
175
176 sdc->f[0] = color[0];
177 sdc->f[1] = color[1];
178 sdc->f[2] = color[2];
179 sdc->f[3] = color[3];
180 } else {
181 struct brw_sampler_default_color *sdc;
182
183 sdc = brw_state_batch(brw, AUB_TRACE_SAMPLER_DEFAULT_COLOR,
184 sizeof(*sdc), 32, sdc_offset);
185
186 COPY_4V(sdc->color, color);
187 }
188 }
189
190 /**
191 * Sets the sampler state for a single unit based off of the sampler key
192 * entry.
193 */
194 static void brw_update_sampler_state(struct brw_context *brw,
195 int unit,
196 int ss_index,
197 struct brw_sampler_state *sampler,
198 uint32_t sampler_state_table_offset,
199 uint32_t *sdc_offset)
200 {
201 struct gl_context *ctx = &brw->ctx;
202 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
203 struct gl_texture_object *texObj = texUnit->_Current;
204 struct gl_sampler_object *gl_sampler = _mesa_get_samplerobj(ctx, unit);
205 bool using_nearest = false;
206
207 /* These don't use samplers at all. */
208 if (texObj->Target == GL_TEXTURE_BUFFER)
209 return;
210
211 switch (gl_sampler->MinFilter) {
212 case GL_NEAREST:
213 sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
214 sampler->ss0.mip_filter = BRW_MIPFILTER_NONE;
215 using_nearest = true;
216 break;
217 case GL_LINEAR:
218 sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
219 sampler->ss0.mip_filter = BRW_MIPFILTER_NONE;
220 break;
221 case GL_NEAREST_MIPMAP_NEAREST:
222 sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
223 sampler->ss0.mip_filter = BRW_MIPFILTER_NEAREST;
224 break;
225 case GL_LINEAR_MIPMAP_NEAREST:
226 sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
227 sampler->ss0.mip_filter = BRW_MIPFILTER_NEAREST;
228 break;
229 case GL_NEAREST_MIPMAP_LINEAR:
230 sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
231 sampler->ss0.mip_filter = BRW_MIPFILTER_LINEAR;
232 break;
233 case GL_LINEAR_MIPMAP_LINEAR:
234 sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
235 sampler->ss0.mip_filter = BRW_MIPFILTER_LINEAR;
236 break;
237 default:
238 break;
239 }
240
241 /* Set Anisotropy:
242 */
243 if (gl_sampler->MaxAnisotropy > 1.0) {
244 sampler->ss0.min_filter = BRW_MAPFILTER_ANISOTROPIC;
245 sampler->ss0.mag_filter = BRW_MAPFILTER_ANISOTROPIC;
246
247 if (gl_sampler->MaxAnisotropy > 2.0) {
248 sampler->ss3.max_aniso = MIN2((gl_sampler->MaxAnisotropy - 2) / 2,
249 BRW_ANISORATIO_16);
250 }
251 }
252 else {
253 switch (gl_sampler->MagFilter) {
254 case GL_NEAREST:
255 sampler->ss0.mag_filter = BRW_MAPFILTER_NEAREST;
256 using_nearest = true;
257 break;
258 case GL_LINEAR:
259 sampler->ss0.mag_filter = BRW_MAPFILTER_LINEAR;
260 break;
261 default:
262 break;
263 }
264 }
265
266 sampler->ss1.r_wrap_mode = translate_wrap_mode(gl_sampler->WrapR,
267 using_nearest);
268 sampler->ss1.s_wrap_mode = translate_wrap_mode(gl_sampler->WrapS,
269 using_nearest);
270 sampler->ss1.t_wrap_mode = translate_wrap_mode(gl_sampler->WrapT,
271 using_nearest);
272
273 if (brw->gen >= 6 &&
274 sampler->ss0.min_filter != sampler->ss0.mag_filter)
275 sampler->ss0.min_mag_neq = 1;
276
277 /* Cube-maps on 965 and later must use the same wrap mode for all 3
278 * coordinate dimensions. Futher, only CUBE and CLAMP are valid.
279 */
280 if (texObj->Target == GL_TEXTURE_CUBE_MAP ||
281 texObj->Target == GL_TEXTURE_CUBE_MAP_ARRAY) {
282 if (ctx->Texture.CubeMapSeamless &&
283 (gl_sampler->MinFilter != GL_NEAREST ||
284 gl_sampler->MagFilter != GL_NEAREST)) {
285 sampler->ss1.r_wrap_mode = BRW_TEXCOORDMODE_CUBE;
286 sampler->ss1.s_wrap_mode = BRW_TEXCOORDMODE_CUBE;
287 sampler->ss1.t_wrap_mode = BRW_TEXCOORDMODE_CUBE;
288 } else {
289 sampler->ss1.r_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
290 sampler->ss1.s_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
291 sampler->ss1.t_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
292 }
293 } else if (texObj->Target == GL_TEXTURE_1D) {
294 /* There's a bug in 1D texture sampling - it actually pays
295 * attention to the wrap_t value, though it should not.
296 * Override the wrap_t value here to GL_REPEAT to keep
297 * any nonexistent border pixels from floating in.
298 */
299 sampler->ss1.t_wrap_mode = BRW_TEXCOORDMODE_WRAP;
300 }
301
302
303 /* Set shadow function:
304 */
305 if (gl_sampler->CompareMode == GL_COMPARE_R_TO_TEXTURE_ARB) {
306 /* Shadowing is "enabled" by emitting a particular sampler
307 * message (sample_c). So need to recompile WM program when
308 * shadow comparison is enabled on each/any texture unit.
309 */
310 sampler->ss0.shadow_function =
311 intel_translate_shadow_compare_func(gl_sampler->CompareFunc);
312 }
313
314 /* Set LOD bias:
315 */
316 sampler->ss0.lod_bias = S_FIXED(CLAMP(texUnit->LodBias +
317 gl_sampler->LodBias, -16, 15), 6);
318
319 sampler->ss0.lod_preclamp = 1; /* OpenGL mode */
320 sampler->ss0.default_color_mode = 0; /* OpenGL/DX10 mode */
321
322 /* Set BaseMipLevel, MaxLOD, MinLOD:
323 *
324 * XXX: I don't think that using firstLevel, lastLevel works,
325 * because we always setup the surface state as if firstLevel ==
326 * level zero. Probably have to subtract firstLevel from each of
327 * these:
328 */
329 sampler->ss0.base_level = U_FIXED(0, 1);
330
331 sampler->ss1.max_lod = U_FIXED(CLAMP(gl_sampler->MaxLod, 0, 13), 6);
332 sampler->ss1.min_lod = U_FIXED(CLAMP(gl_sampler->MinLod, 0, 13), 6);
333
334 /* On Gen6+, the sampler can handle non-normalized texture
335 * rectangle coordinates natively
336 */
337 if (brw->gen >= 6 && texObj->Target == GL_TEXTURE_RECTANGLE) {
338 sampler->ss3.non_normalized_coord = 1;
339 }
340
341 upload_default_color(brw, gl_sampler, unit, sdc_offset);
342
343 if (brw->gen >= 6) {
344 sampler->ss2.default_color_pointer = *sdc_offset >> 5;
345 } else {
346 /* reloc */
347 sampler->ss2.default_color_pointer = (brw->batch.bo->offset +
348 *sdc_offset) >> 5;
349
350 drm_intel_bo_emit_reloc(brw->batch.bo,
351 sampler_state_table_offset +
352 ss_index * sizeof(struct brw_sampler_state) +
353 offsetof(struct brw_sampler_state, ss2),
354 brw->batch.bo, *sdc_offset,
355 I915_GEM_DOMAIN_SAMPLER, 0);
356 }
357
358 if (sampler->ss0.min_filter != BRW_MAPFILTER_NEAREST)
359 sampler->ss3.address_round |= BRW_ADDRESS_ROUNDING_ENABLE_U_MIN |
360 BRW_ADDRESS_ROUNDING_ENABLE_V_MIN |
361 BRW_ADDRESS_ROUNDING_ENABLE_R_MIN;
362 if (sampler->ss0.mag_filter != BRW_MAPFILTER_NEAREST)
363 sampler->ss3.address_round |= BRW_ADDRESS_ROUNDING_ENABLE_U_MAG |
364 BRW_ADDRESS_ROUNDING_ENABLE_V_MAG |
365 BRW_ADDRESS_ROUNDING_ENABLE_R_MAG;
366 }
367
368
369 static void
370 brw_upload_sampler_state_table(struct brw_context *brw,
371 struct gl_program *prog,
372 uint32_t sampler_count,
373 uint32_t *sst_offset,
374 uint32_t *sdc_offset)
375 {
376 struct gl_context *ctx = &brw->ctx;
377 struct brw_sampler_state *samplers;
378
379 GLbitfield SamplersUsed = prog->SamplersUsed;
380
381 if (sampler_count == 0)
382 return;
383
384 samplers = brw_state_batch(brw, AUB_TRACE_SAMPLER_STATE,
385 sampler_count * sizeof(*samplers),
386 32, sst_offset);
387 memset(samplers, 0, sampler_count * sizeof(*samplers));
388
389 for (unsigned s = 0; s < sampler_count; s++) {
390 if (SamplersUsed & (1 << s)) {
391 const unsigned unit = prog->SamplerUnits[s];
392 if (ctx->Texture.Unit[unit]._ReallyEnabled)
393 brw_update_sampler_state(brw, unit, s, &samplers[s],
394 *sst_offset, &sdc_offset[s]);
395 }
396 }
397
398 brw->state.dirty.cache |= CACHE_NEW_SAMPLER;
399 }
400
401 static void
402 brw_upload_fs_samplers(struct brw_context *brw)
403 {
404 /* BRW_NEW_FRAGMENT_PROGRAM */
405 struct gl_program *fs = (struct gl_program *) brw->fragment_program;
406 brw->vtbl.upload_sampler_state_table(brw, fs,
407 brw->wm.sampler_count,
408 &brw->wm.sampler_offset,
409 brw->wm.sdc_offset);
410 }
411
412 const struct brw_tracked_state brw_fs_samplers = {
413 .dirty = {
414 .mesa = _NEW_TEXTURE,
415 .brw = BRW_NEW_BATCH |
416 BRW_NEW_FRAGMENT_PROGRAM,
417 .cache = 0
418 },
419 .emit = brw_upload_fs_samplers,
420 };
421
422 static void
423 brw_upload_vs_samplers(struct brw_context *brw)
424 {
425 /* BRW_NEW_VERTEX_PROGRAM */
426 struct gl_program *vs = (struct gl_program *) brw->vertex_program;
427 brw->vtbl.upload_sampler_state_table(brw, vs,
428 brw->vs.sampler_count,
429 &brw->vs.sampler_offset,
430 brw->vs.sdc_offset);
431 }
432
433
434 const struct brw_tracked_state brw_vs_samplers = {
435 .dirty = {
436 .mesa = _NEW_TEXTURE,
437 .brw = BRW_NEW_BATCH |
438 BRW_NEW_VERTEX_PROGRAM,
439 .cache = 0
440 },
441 .emit = brw_upload_vs_samplers,
442 };
443
444
445 void
446 gen4_init_vtable_sampler_functions(struct brw_context *brw)
447 {
448 brw->vtbl.upload_sampler_state_table = brw_upload_sampler_state_table;
449 }