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