st/mesa: unify common code in st_draw_vbo functions
[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 struct gl_context *ctx = st->ctx;
139 GLenum texBaseFormat;
140
141 texBaseFormat = _mesa_texture_base_format(texobj);
142
143 memset(sampler, 0, sizeof(*sampler));
144 sampler->wrap_s = gl_wrap_xlate(msamp->WrapS);
145 sampler->wrap_t = gl_wrap_xlate(msamp->WrapT);
146 sampler->wrap_r = gl_wrap_xlate(msamp->WrapR);
147
148 sampler->min_img_filter = gl_filter_to_img_filter(msamp->MinFilter);
149 sampler->min_mip_filter = gl_filter_to_mip_filter(msamp->MinFilter);
150 sampler->mag_img_filter = gl_filter_to_img_filter(msamp->MagFilter);
151
152 if (texobj->Target != GL_TEXTURE_RECTANGLE_ARB)
153 sampler->normalized_coords = 1;
154
155 sampler->lod_bias = msamp->LodBias;
156 /* Reduce the number of states by allowing only the values that AMD GCN
157 * can represent. Apps use lod_bias for smooth transitions to bigger mipmap
158 * levels.
159 */
160 sampler->lod_bias = CLAMP(sampler->lod_bias, -16, 16);
161 sampler->lod_bias = floorf(sampler->lod_bias * 256) / 256;
162
163 sampler->min_lod = MAX2(msamp->MinLod, 0.0f);
164 sampler->max_lod = msamp->MaxLod;
165 if (sampler->max_lod < sampler->min_lod) {
166 /* The GL spec doesn't seem to specify what to do in this case.
167 * Swap the values.
168 */
169 float tmp = sampler->max_lod;
170 sampler->max_lod = sampler->min_lod;
171 sampler->min_lod = tmp;
172 assert(sampler->min_lod <= sampler->max_lod);
173 }
174
175 /* For non-black borders... */
176 if (msamp->BorderColor.ui[0] ||
177 msamp->BorderColor.ui[1] ||
178 msamp->BorderColor.ui[2] ||
179 msamp->BorderColor.ui[3]) {
180 const struct st_texture_object *stobj = st_texture_object_const(texobj);
181 const GLboolean is_integer = texobj->_IsIntegerFormat;
182 const struct pipe_sampler_view *sv = NULL;
183 union pipe_color_union border_color;
184 GLuint i;
185
186 /* Just search for the first used view. We can do this because the
187 swizzle is per-texture, not per context. */
188 /* XXX: clean that up to not use the sampler view at all */
189 for (i = 0; i < stobj->num_sampler_views; ++i) {
190 if (stobj->sampler_views[i]) {
191 sv = stobj->sampler_views[i];
192 break;
193 }
194 }
195
196 if (st->apply_texture_swizzle_to_border_color && sv) {
197 const unsigned char swz[4] =
198 {
199 sv->swizzle_r,
200 sv->swizzle_g,
201 sv->swizzle_b,
202 sv->swizzle_a,
203 };
204
205 st_translate_color(&msamp->BorderColor,
206 &border_color,
207 texBaseFormat, is_integer);
208
209 util_format_apply_color_swizzle(&sampler->border_color,
210 &border_color, swz, is_integer);
211 } else {
212 st_translate_color(&msamp->BorderColor,
213 &sampler->border_color,
214 texBaseFormat, is_integer);
215 }
216 }
217
218 sampler->max_anisotropy = (msamp->MaxAnisotropy == 1.0 ?
219 0 : (GLuint) msamp->MaxAnisotropy);
220
221 /* If sampling a depth texture and using shadow comparison */
222 if ((texBaseFormat == GL_DEPTH_COMPONENT ||
223 (texBaseFormat == GL_DEPTH_STENCIL && !texobj->StencilSampling)) &&
224 msamp->CompareMode == GL_COMPARE_R_TO_TEXTURE) {
225 sampler->compare_mode = PIPE_TEX_COMPARE_R_TO_TEXTURE;
226 sampler->compare_func = st_compare_func_to_pipe(msamp->CompareFunc);
227 }
228
229 sampler->seamless_cube_map =
230 ctx->Texture.CubeMapSeamless || msamp->CubeMapSeamless;
231 }
232
233
234 static void
235 convert_sampler_from_unit(const struct st_context *st,
236 struct pipe_sampler_state *sampler,
237 GLuint texUnit)
238 {
239 const struct gl_texture_object *texobj;
240 struct gl_context *ctx = st->ctx;
241 const struct gl_sampler_object *msamp;
242
243 texobj = ctx->Texture.Unit[texUnit]._Current;
244 assert(texobj);
245
246 msamp = _mesa_get_samplerobj(ctx, texUnit);
247
248 st_convert_sampler(st, texobj, msamp, sampler);
249
250 sampler->lod_bias += ctx->Texture.Unit[texUnit].LodBias;
251 }
252
253
254 /**
255 * Update the gallium driver's sampler state for fragment, vertex or
256 * geometry shader stage.
257 */
258 static void
259 update_shader_samplers(struct st_context *st,
260 enum pipe_shader_type shader_stage,
261 const struct gl_program *prog,
262 unsigned max_units,
263 struct pipe_sampler_state *samplers,
264 unsigned *num_samplers)
265 {
266 GLbitfield samplers_used = prog->SamplersUsed;
267 GLbitfield free_slots = ~prog->SamplersUsed;
268 GLbitfield external_samplers_used = prog->ExternalSamplersUsed;
269 GLuint unit;
270 const GLuint old_max = *num_samplers;
271 const struct pipe_sampler_state *states[PIPE_MAX_SAMPLERS];
272
273 if (*num_samplers == 0 && samplers_used == 0x0)
274 return;
275
276 *num_samplers = 0;
277
278 /* loop over sampler units (aka tex image units) */
279 for (unit = 0; unit < max_units; unit++, samplers_used >>= 1) {
280 struct pipe_sampler_state *sampler = samplers + unit;
281
282 if (samplers_used & 1) {
283 const GLuint texUnit = prog->SamplerUnits[unit];
284
285 convert_sampler_from_unit(st, sampler, texUnit);
286 states[unit] = sampler;
287 *num_samplers = unit + 1;
288 }
289 else if (samplers_used != 0 || unit < old_max) {
290 states[unit] = NULL;
291 }
292 else {
293 /* if we've reset all the old samplers and we have no more new ones */
294 break;
295 }
296 }
297
298 /* For any external samplers with multiplaner YUV, stuff the additional
299 * sampler states we need at the end.
300 *
301 * Just re-use the existing sampler-state from the primary slot.
302 */
303 while (unlikely(external_samplers_used)) {
304 GLuint unit = u_bit_scan(&external_samplers_used);
305 GLuint extra = 0;
306 struct st_texture_object *stObj =
307 st_get_texture_object(st->ctx, prog, unit);
308 struct pipe_sampler_state *sampler = samplers + unit;
309
310 if (!stObj)
311 continue;
312
313 switch (st_get_view_format(stObj)) {
314 case PIPE_FORMAT_NV12:
315 /* we need one additional sampler: */
316 extra = u_bit_scan(&free_slots);
317 states[extra] = sampler;
318 break;
319 case PIPE_FORMAT_IYUV:
320 /* we need two additional samplers: */
321 extra = u_bit_scan(&free_slots);
322 states[extra] = sampler;
323 extra = u_bit_scan(&free_slots);
324 states[extra] = sampler;
325 break;
326 default:
327 break;
328 }
329
330 *num_samplers = MAX2(*num_samplers, extra + 1);
331 }
332
333 cso_set_samplers(st->cso_context, shader_stage, *num_samplers, states);
334 }
335
336
337 void
338 st_update_vertex_samplers(struct st_context *st)
339 {
340 const struct gl_context *ctx = st->ctx;
341
342 update_shader_samplers(st,
343 PIPE_SHADER_VERTEX,
344 ctx->VertexProgram._Current,
345 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits,
346 st->state.samplers[PIPE_SHADER_VERTEX],
347 &st->state.num_samplers[PIPE_SHADER_VERTEX]);
348 }
349
350
351 void
352 st_update_tessctrl_samplers(struct st_context *st)
353 {
354 const struct gl_context *ctx = st->ctx;
355
356 if (ctx->TessCtrlProgram._Current) {
357 update_shader_samplers(st,
358 PIPE_SHADER_TESS_CTRL,
359 ctx->TessCtrlProgram._Current,
360 ctx->Const.Program[MESA_SHADER_TESS_CTRL].MaxTextureImageUnits,
361 st->state.samplers[PIPE_SHADER_TESS_CTRL],
362 &st->state.num_samplers[PIPE_SHADER_TESS_CTRL]);
363 }
364 }
365
366
367 void
368 st_update_tesseval_samplers(struct st_context *st)
369 {
370 const struct gl_context *ctx = st->ctx;
371
372 if (ctx->TessEvalProgram._Current) {
373 update_shader_samplers(st,
374 PIPE_SHADER_TESS_EVAL,
375 ctx->TessEvalProgram._Current,
376 ctx->Const.Program[MESA_SHADER_TESS_EVAL].MaxTextureImageUnits,
377 st->state.samplers[PIPE_SHADER_TESS_EVAL],
378 &st->state.num_samplers[PIPE_SHADER_TESS_EVAL]);
379 }
380 }
381
382
383 void
384 st_update_geometry_samplers(struct st_context *st)
385 {
386 const struct gl_context *ctx = st->ctx;
387
388 if (ctx->GeometryProgram._Current) {
389 update_shader_samplers(st,
390 PIPE_SHADER_GEOMETRY,
391 ctx->GeometryProgram._Current,
392 ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxTextureImageUnits,
393 st->state.samplers[PIPE_SHADER_GEOMETRY],
394 &st->state.num_samplers[PIPE_SHADER_GEOMETRY]);
395 }
396 }
397
398
399 void
400 st_update_fragment_samplers(struct st_context *st)
401 {
402 const struct gl_context *ctx = st->ctx;
403
404 update_shader_samplers(st,
405 PIPE_SHADER_FRAGMENT,
406 ctx->FragmentProgram._Current,
407 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits,
408 st->state.samplers[PIPE_SHADER_FRAGMENT],
409 &st->state.num_samplers[PIPE_SHADER_FRAGMENT]);
410 }
411
412
413 void
414 st_update_compute_samplers(struct st_context *st)
415 {
416 const struct gl_context *ctx = st->ctx;
417
418 if (ctx->ComputeProgram._Current) {
419 update_shader_samplers(st,
420 PIPE_SHADER_COMPUTE,
421 ctx->ComputeProgram._Current,
422 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxTextureImageUnits,
423 st->state.samplers[PIPE_SHADER_COMPUTE],
424 &st->state.num_samplers[PIPE_SHADER_COMPUTE]);
425 }
426 }