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