st/mesa: don't set the border color if it's unused
[mesa.git] / src / mesa / state_tracker / st_atom_sampler.c
1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * 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, sub license, 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 portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /*
29 * Authors:
30 * Keith Whitwell <keithw@vmware.com>
31 * Brian Paul
32 */
33
34
35 #include "main/macros.h"
36 #include "main/mtypes.h"
37 #include "main/glformats.h"
38 #include "main/samplerobj.h"
39 #include "main/teximage.h"
40 #include "main/texobj.h"
41
42 #include "st_context.h"
43 #include "st_cb_texture.h"
44 #include "st_format.h"
45 #include "st_atom.h"
46 #include "st_texture.h"
47 #include "pipe/p_context.h"
48 #include "pipe/p_defines.h"
49
50 #include "cso_cache/cso_context.h"
51
52 #include "util/u_format.h"
53
54
55 /**
56 * Convert GLenum texcoord wrap tokens to pipe tokens.
57 */
58 static GLuint
59 gl_wrap_xlate(GLenum wrap)
60 {
61 /* Take advantage of how the enums are defined. */
62 static const unsigned table[32] = {
63 [GL_REPEAT & 0x1f] = PIPE_TEX_WRAP_REPEAT,
64 [GL_CLAMP & 0x1f] = PIPE_TEX_WRAP_CLAMP,
65 [GL_CLAMP_TO_EDGE & 0x1f] = PIPE_TEX_WRAP_CLAMP_TO_EDGE,
66 [GL_CLAMP_TO_BORDER & 0x1f] = PIPE_TEX_WRAP_CLAMP_TO_BORDER,
67 [GL_MIRRORED_REPEAT & 0x1f] = PIPE_TEX_WRAP_MIRROR_REPEAT,
68 [GL_MIRROR_CLAMP_EXT & 0x1f] = PIPE_TEX_WRAP_MIRROR_CLAMP,
69 [GL_MIRROR_CLAMP_TO_EDGE & 0x1f] = PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE,
70 [GL_MIRROR_CLAMP_TO_BORDER_EXT & 0x1f] = PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER,
71 };
72
73 return table[wrap & 0x1f];
74 }
75
76
77 static GLuint
78 gl_filter_to_mip_filter(GLenum filter)
79 {
80 /* Take advantage of how the enums are defined. */
81 if (filter <= GL_LINEAR)
82 return PIPE_TEX_MIPFILTER_NONE;
83 if (filter <= GL_LINEAR_MIPMAP_NEAREST)
84 return PIPE_TEX_MIPFILTER_NEAREST;
85
86 return PIPE_TEX_MIPFILTER_LINEAR;
87 }
88
89
90 static GLuint
91 gl_filter_to_img_filter(GLenum filter)
92 {
93 /* Take advantage of how the enums are defined. */
94 if (filter & 1)
95 return PIPE_TEX_FILTER_LINEAR;
96
97 return PIPE_TEX_FILTER_NEAREST;
98 }
99
100
101 /**
102 * Convert a gl_sampler_object to a pipe_sampler_state object.
103 */
104 void
105 st_convert_sampler(const struct st_context *st,
106 const struct gl_texture_object *texobj,
107 const struct gl_sampler_object *msamp,
108 struct pipe_sampler_state *sampler)
109 {
110 memset(sampler, 0, sizeof(*sampler));
111 sampler->wrap_s = gl_wrap_xlate(msamp->WrapS);
112 sampler->wrap_t = gl_wrap_xlate(msamp->WrapT);
113 sampler->wrap_r = gl_wrap_xlate(msamp->WrapR);
114
115 sampler->min_img_filter = gl_filter_to_img_filter(msamp->MinFilter);
116 sampler->min_mip_filter = gl_filter_to_mip_filter(msamp->MinFilter);
117 sampler->mag_img_filter = gl_filter_to_img_filter(msamp->MagFilter);
118
119 if (texobj->Target != GL_TEXTURE_RECTANGLE_ARB)
120 sampler->normalized_coords = 1;
121
122 sampler->lod_bias = msamp->LodBias;
123 /* Reduce the number of states by allowing only the values that AMD GCN
124 * can represent. Apps use lod_bias for smooth transitions to bigger mipmap
125 * levels.
126 */
127 sampler->lod_bias = CLAMP(sampler->lod_bias, -16, 16);
128 sampler->lod_bias = floorf(sampler->lod_bias * 256) / 256;
129
130 sampler->min_lod = MAX2(msamp->MinLod, 0.0f);
131 sampler->max_lod = msamp->MaxLod;
132 if (sampler->max_lod < sampler->min_lod) {
133 /* The GL spec doesn't seem to specify what to do in this case.
134 * Swap the values.
135 */
136 float tmp = sampler->max_lod;
137 sampler->max_lod = sampler->min_lod;
138 sampler->min_lod = tmp;
139 assert(sampler->min_lod <= sampler->max_lod);
140 }
141
142 /* Check that only wrap modes using the border color have the first bit
143 * set.
144 */
145 STATIC_ASSERT(PIPE_TEX_WRAP_CLAMP & 0x1);
146 STATIC_ASSERT(PIPE_TEX_WRAP_CLAMP_TO_BORDER & 0x1);
147 STATIC_ASSERT(PIPE_TEX_WRAP_MIRROR_CLAMP & 0x1);
148 STATIC_ASSERT(PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER & 0x1);
149 STATIC_ASSERT(((PIPE_TEX_WRAP_REPEAT |
150 PIPE_TEX_WRAP_CLAMP_TO_EDGE |
151 PIPE_TEX_WRAP_MIRROR_REPEAT |
152 PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE) & 0x1) == 0);
153
154 /* For non-black borders... */
155 if (/* This is true if wrap modes are using the border color: */
156 (sampler->wrap_s | sampler->wrap_t | sampler->wrap_r) & 0x1 &&
157 (msamp->BorderColor.ui[0] ||
158 msamp->BorderColor.ui[1] ||
159 msamp->BorderColor.ui[2] ||
160 msamp->BorderColor.ui[3])) {
161 const GLboolean is_integer = texobj->_IsIntegerFormat;
162 GLenum texBaseFormat = _mesa_base_tex_image(texobj)->_BaseFormat;
163
164 if (st->apply_texture_swizzle_to_border_color) {
165 const struct st_texture_object *stobj = st_texture_object_const(texobj);
166 const struct pipe_sampler_view *sv = NULL;
167
168 /* Just search for the first used view. We can do this because the
169 swizzle is per-texture, not per context. */
170 /* XXX: clean that up to not use the sampler view at all */
171 for (unsigned i = 0; i < stobj->num_sampler_views; ++i) {
172 if (stobj->sampler_views[i]) {
173 sv = stobj->sampler_views[i];
174 break;
175 }
176 }
177
178 if (sv) {
179 union pipe_color_union tmp;
180 const unsigned char swz[4] =
181 {
182 sv->swizzle_r,
183 sv->swizzle_g,
184 sv->swizzle_b,
185 sv->swizzle_a,
186 };
187
188 st_translate_color(&msamp->BorderColor, &tmp,
189 texBaseFormat, is_integer);
190
191 util_format_apply_color_swizzle(&sampler->border_color,
192 &tmp, swz, is_integer);
193 } else {
194 st_translate_color(&msamp->BorderColor,
195 &sampler->border_color,
196 texBaseFormat, is_integer);
197 }
198 } else {
199 st_translate_color(&msamp->BorderColor,
200 &sampler->border_color,
201 texBaseFormat, is_integer);
202 }
203 }
204
205 sampler->max_anisotropy = (msamp->MaxAnisotropy == 1.0 ?
206 0 : (GLuint) msamp->MaxAnisotropy);
207
208 /* If sampling a depth texture and using shadow comparison */
209 if (msamp->CompareMode == GL_COMPARE_R_TO_TEXTURE) {
210 GLenum texBaseFormat = _mesa_base_tex_image(texobj)->_BaseFormat;
211
212 if (texBaseFormat == GL_DEPTH_COMPONENT ||
213 (texBaseFormat == GL_DEPTH_STENCIL && !texobj->StencilSampling)) {
214 sampler->compare_mode = PIPE_TEX_COMPARE_R_TO_TEXTURE;
215 sampler->compare_func = st_compare_func_to_pipe(msamp->CompareFunc);
216 }
217 }
218
219 /* Only set the seamless cube map texture parameter because the per-context
220 * enable should be ignored and treated as disabled when using texture
221 * handles, as specified by ARB_bindless_texture.
222 */
223 sampler->seamless_cube_map = msamp->CubeMapSeamless;
224 }
225
226 /**
227 * Get a pipe_sampler_state object from a texture unit.
228 */
229 void
230 st_convert_sampler_from_unit(const struct st_context *st,
231 struct pipe_sampler_state *sampler,
232 GLuint texUnit)
233 {
234 const struct gl_texture_object *texobj;
235 struct gl_context *ctx = st->ctx;
236 const struct gl_sampler_object *msamp;
237
238 texobj = ctx->Texture.Unit[texUnit]._Current;
239 assert(texobj);
240 assert(texobj->Target != GL_TEXTURE_BUFFER);
241
242 msamp = _mesa_get_samplerobj(ctx, texUnit);
243
244 st_convert_sampler(st, texobj, msamp, sampler);
245
246 sampler->lod_bias += ctx->Texture.Unit[texUnit].LodBias;
247 sampler->seamless_cube_map |= ctx->Texture.CubeMapSeamless;
248 }
249
250
251 /**
252 * Update the gallium driver's sampler state for fragment, vertex or
253 * geometry shader stage.
254 */
255 static void
256 update_shader_samplers(struct st_context *st,
257 enum pipe_shader_type shader_stage,
258 const struct gl_program *prog,
259 struct pipe_sampler_state *samplers,
260 unsigned *out_num_samplers)
261 {
262 struct gl_context *ctx = st->ctx;
263 GLbitfield samplers_used = prog->SamplersUsed;
264 GLbitfield free_slots = ~prog->SamplersUsed;
265 GLbitfield external_samplers_used = prog->ExternalSamplersUsed;
266 unsigned unit, num_samplers;
267 const struct pipe_sampler_state *states[PIPE_MAX_SAMPLERS];
268
269 if (samplers_used == 0x0) {
270 *out_num_samplers = 0;
271 return;
272 }
273
274 num_samplers = util_last_bit(samplers_used);
275
276 /* loop over sampler units (aka tex image units) */
277 for (unit = 0; samplers_used; unit++, samplers_used >>= 1) {
278 struct pipe_sampler_state *sampler = samplers + unit;
279 unsigned tex_unit = prog->SamplerUnits[unit];
280
281 /* Don't update the sampler for TBOs. cso_context will not bind sampler
282 * states that are NULL.
283 */
284 if (samplers_used & 1 &&
285 ctx->Texture.Unit[tex_unit]._Current->Target != GL_TEXTURE_BUFFER) {
286 st_convert_sampler_from_unit(st, sampler, tex_unit);
287 states[unit] = sampler;
288 } else {
289 states[unit] = NULL;
290 }
291 }
292
293 /* For any external samplers with multiplaner YUV, stuff the additional
294 * sampler states we need at the end.
295 *
296 * Just re-use the existing sampler-state from the primary slot.
297 */
298 while (unlikely(external_samplers_used)) {
299 GLuint unit = u_bit_scan(&external_samplers_used);
300 GLuint extra = 0;
301 struct st_texture_object *stObj =
302 st_get_texture_object(st->ctx, prog, unit);
303 struct pipe_sampler_state *sampler = samplers + unit;
304
305 if (!stObj)
306 continue;
307
308 switch (st_get_view_format(stObj)) {
309 case PIPE_FORMAT_NV12:
310 /* we need one additional sampler: */
311 extra = u_bit_scan(&free_slots);
312 states[extra] = sampler;
313 break;
314 case PIPE_FORMAT_IYUV:
315 /* we need two additional samplers: */
316 extra = u_bit_scan(&free_slots);
317 states[extra] = sampler;
318 extra = u_bit_scan(&free_slots);
319 states[extra] = sampler;
320 break;
321 default:
322 break;
323 }
324
325 num_samplers = MAX2(num_samplers, extra + 1);
326 }
327
328 cso_set_samplers(st->cso_context, shader_stage, num_samplers, states);
329 *out_num_samplers = num_samplers;
330 }
331
332
333 void
334 st_update_vertex_samplers(struct st_context *st)
335 {
336 const struct gl_context *ctx = st->ctx;
337
338 update_shader_samplers(st,
339 PIPE_SHADER_VERTEX,
340 ctx->VertexProgram._Current,
341 st->state.samplers[PIPE_SHADER_VERTEX],
342 &st->state.num_samplers[PIPE_SHADER_VERTEX]);
343 }
344
345
346 void
347 st_update_tessctrl_samplers(struct st_context *st)
348 {
349 const struct gl_context *ctx = st->ctx;
350
351 if (ctx->TessCtrlProgram._Current) {
352 update_shader_samplers(st,
353 PIPE_SHADER_TESS_CTRL,
354 ctx->TessCtrlProgram._Current,
355 st->state.samplers[PIPE_SHADER_TESS_CTRL],
356 &st->state.num_samplers[PIPE_SHADER_TESS_CTRL]);
357 }
358 }
359
360
361 void
362 st_update_tesseval_samplers(struct st_context *st)
363 {
364 const struct gl_context *ctx = st->ctx;
365
366 if (ctx->TessEvalProgram._Current) {
367 update_shader_samplers(st,
368 PIPE_SHADER_TESS_EVAL,
369 ctx->TessEvalProgram._Current,
370 st->state.samplers[PIPE_SHADER_TESS_EVAL],
371 &st->state.num_samplers[PIPE_SHADER_TESS_EVAL]);
372 }
373 }
374
375
376 void
377 st_update_geometry_samplers(struct st_context *st)
378 {
379 const struct gl_context *ctx = st->ctx;
380
381 if (ctx->GeometryProgram._Current) {
382 update_shader_samplers(st,
383 PIPE_SHADER_GEOMETRY,
384 ctx->GeometryProgram._Current,
385 st->state.samplers[PIPE_SHADER_GEOMETRY],
386 &st->state.num_samplers[PIPE_SHADER_GEOMETRY]);
387 }
388 }
389
390
391 void
392 st_update_fragment_samplers(struct st_context *st)
393 {
394 const struct gl_context *ctx = st->ctx;
395
396 update_shader_samplers(st,
397 PIPE_SHADER_FRAGMENT,
398 ctx->FragmentProgram._Current,
399 st->state.samplers[PIPE_SHADER_FRAGMENT],
400 &st->state.num_samplers[PIPE_SHADER_FRAGMENT]);
401 }
402
403
404 void
405 st_update_compute_samplers(struct st_context *st)
406 {
407 const struct gl_context *ctx = st->ctx;
408
409 if (ctx->ComputeProgram._Current) {
410 update_shader_samplers(st,
411 PIPE_SHADER_COMPUTE,
412 ctx->ComputeProgram._Current,
413 st->state.samplers[PIPE_SHADER_COMPUTE],
414 &st->state.num_samplers[PIPE_SHADER_COMPUTE]);
415 }
416 }