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