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