1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * Keith Whitwell <keithw@vmware.com>
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"
42 #include "st_context.h"
43 #include "st_cb_texture.h"
44 #include "st_format.h"
46 #include "st_texture.h"
47 #include "pipe/p_context.h"
48 #include "pipe/p_defines.h"
50 #include "cso_cache/cso_context.h"
52 #include "util/u_format.h"
56 * Convert GLenum texcoord wrap tokens to pipe tokens.
59 gl_wrap_xlate(GLenum wrap
)
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
,
73 return table
[wrap
& 0x1f];
78 gl_filter_to_mip_filter(GLenum filter
)
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
;
86 return PIPE_TEX_MIPFILTER_LINEAR
;
91 gl_filter_to_img_filter(GLenum filter
)
93 /* Take advantage of how the enums are defined. */
95 return PIPE_TEX_FILTER_LINEAR
;
97 return PIPE_TEX_FILTER_NEAREST
;
102 * Convert a gl_sampler_object to a pipe_sampler_state object.
105 st_convert_sampler(const struct st_context
*st
,
106 const struct gl_texture_object
*texobj
,
107 const struct gl_sampler_object
*msamp
,
108 float tex_unit_lod_bias
,
109 struct pipe_sampler_state
*sampler
)
111 memset(sampler
, 0, sizeof(*sampler
));
112 sampler
->wrap_s
= gl_wrap_xlate(msamp
->WrapS
);
113 sampler
->wrap_t
= gl_wrap_xlate(msamp
->WrapT
);
114 sampler
->wrap_r
= gl_wrap_xlate(msamp
->WrapR
);
116 sampler
->min_img_filter
= gl_filter_to_img_filter(msamp
->MinFilter
);
117 sampler
->min_mip_filter
= gl_filter_to_mip_filter(msamp
->MinFilter
);
118 sampler
->mag_img_filter
= gl_filter_to_img_filter(msamp
->MagFilter
);
120 if (texobj
->Target
!= GL_TEXTURE_RECTANGLE_ARB
)
121 sampler
->normalized_coords
= 1;
123 sampler
->lod_bias
= msamp
->LodBias
+ tex_unit_lod_bias
;
124 /* Reduce the number of states by allowing only the values that AMD GCN
125 * can represent. Apps use lod_bias for smooth transitions to bigger mipmap
128 sampler
->lod_bias
= CLAMP(sampler
->lod_bias
, -16, 16);
129 sampler
->lod_bias
= floorf(sampler
->lod_bias
* 256) / 256;
131 sampler
->min_lod
= MAX2(msamp
->MinLod
, 0.0f
);
132 sampler
->max_lod
= msamp
->MaxLod
;
133 if (sampler
->max_lod
< sampler
->min_lod
) {
134 /* The GL spec doesn't seem to specify what to do in this case.
137 float tmp
= sampler
->max_lod
;
138 sampler
->max_lod
= sampler
->min_lod
;
139 sampler
->min_lod
= tmp
;
140 assert(sampler
->min_lod
<= sampler
->max_lod
);
143 /* Check that only wrap modes using the border color have the first bit
146 STATIC_ASSERT(PIPE_TEX_WRAP_CLAMP
& 0x1);
147 STATIC_ASSERT(PIPE_TEX_WRAP_CLAMP_TO_BORDER
& 0x1);
148 STATIC_ASSERT(PIPE_TEX_WRAP_MIRROR_CLAMP
& 0x1);
149 STATIC_ASSERT(PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
& 0x1);
150 STATIC_ASSERT(((PIPE_TEX_WRAP_REPEAT
|
151 PIPE_TEX_WRAP_CLAMP_TO_EDGE
|
152 PIPE_TEX_WRAP_MIRROR_REPEAT
|
153 PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
) & 0x1) == 0);
155 /* For non-black borders... */
156 if (/* This is true if wrap modes are using the border color: */
157 (sampler
->wrap_s
| sampler
->wrap_t
| sampler
->wrap_r
) & 0x1 &&
158 (msamp
->BorderColor
.ui
[0] ||
159 msamp
->BorderColor
.ui
[1] ||
160 msamp
->BorderColor
.ui
[2] ||
161 msamp
->BorderColor
.ui
[3])) {
162 const GLboolean is_integer
= texobj
->_IsIntegerFormat
;
163 GLenum texBaseFormat
= _mesa_base_tex_image(texobj
)->_BaseFormat
;
165 if (st
->apply_texture_swizzle_to_border_color
) {
166 const struct st_texture_object
*stobj
= st_texture_object_const(texobj
);
167 const struct pipe_sampler_view
*sv
= NULL
;
169 /* Just search for the first used view. We can do this because the
170 swizzle is per-texture, not per context. */
171 /* XXX: clean that up to not use the sampler view at all */
172 for (unsigned i
= 0; i
< stobj
->num_sampler_views
; ++i
) {
173 if (stobj
->sampler_views
[i
].view
) {
174 sv
= stobj
->sampler_views
[i
].view
;
180 union pipe_color_union tmp
;
181 const unsigned char swz
[4] =
189 st_translate_color(&msamp
->BorderColor
, &tmp
,
190 texBaseFormat
, is_integer
);
192 util_format_apply_color_swizzle(&sampler
->border_color
,
193 &tmp
, swz
, is_integer
);
195 st_translate_color(&msamp
->BorderColor
,
196 &sampler
->border_color
,
197 texBaseFormat
, is_integer
);
200 st_translate_color(&msamp
->BorderColor
,
201 &sampler
->border_color
,
202 texBaseFormat
, is_integer
);
206 sampler
->max_anisotropy
= (msamp
->MaxAnisotropy
== 1.0 ?
207 0 : (GLuint
) msamp
->MaxAnisotropy
);
209 /* If sampling a depth texture and using shadow comparison */
210 if (msamp
->CompareMode
== GL_COMPARE_R_TO_TEXTURE
) {
211 GLenum texBaseFormat
= _mesa_base_tex_image(texobj
)->_BaseFormat
;
213 if (texBaseFormat
== GL_DEPTH_COMPONENT
||
214 (texBaseFormat
== GL_DEPTH_STENCIL
&& !texobj
->StencilSampling
)) {
215 sampler
->compare_mode
= PIPE_TEX_COMPARE_R_TO_TEXTURE
;
216 sampler
->compare_func
= st_compare_func_to_pipe(msamp
->CompareFunc
);
220 /* Only set the seamless cube map texture parameter because the per-context
221 * enable should be ignored and treated as disabled when using texture
222 * handles, as specified by ARB_bindless_texture.
224 sampler
->seamless_cube_map
= msamp
->CubeMapSeamless
;
228 * Get a pipe_sampler_state object from a texture unit.
231 st_convert_sampler_from_unit(const struct st_context
*st
,
232 struct pipe_sampler_state
*sampler
,
235 const struct gl_texture_object
*texobj
;
236 struct gl_context
*ctx
= st
->ctx
;
237 const struct gl_sampler_object
*msamp
;
239 texobj
= ctx
->Texture
.Unit
[texUnit
]._Current
;
241 assert(texobj
->Target
!= GL_TEXTURE_BUFFER
);
243 msamp
= _mesa_get_samplerobj(ctx
, texUnit
);
245 st_convert_sampler(st
, texobj
, msamp
, ctx
->Texture
.Unit
[texUnit
].LodBias
,
248 sampler
->seamless_cube_map
|= ctx
->Texture
.CubeMapSeamless
;
253 * Update the gallium driver's sampler state for fragment, vertex or
254 * geometry shader stage.
257 update_shader_samplers(struct st_context
*st
,
258 enum pipe_shader_type shader_stage
,
259 const struct gl_program
*prog
,
260 struct pipe_sampler_state
*samplers
,
261 unsigned *out_num_samplers
)
263 struct gl_context
*ctx
= st
->ctx
;
264 GLbitfield samplers_used
= prog
->SamplersUsed
;
265 GLbitfield free_slots
= ~prog
->SamplersUsed
;
266 GLbitfield external_samplers_used
= prog
->ExternalSamplersUsed
;
267 unsigned unit
, num_samplers
;
268 const struct pipe_sampler_state
*states
[PIPE_MAX_SAMPLERS
];
270 if (samplers_used
== 0x0) {
271 *out_num_samplers
= 0;
275 num_samplers
= util_last_bit(samplers_used
);
277 /* loop over sampler units (aka tex image units) */
278 for (unit
= 0; samplers_used
; unit
++, samplers_used
>>= 1) {
279 struct pipe_sampler_state
*sampler
= samplers
+ unit
;
280 unsigned tex_unit
= prog
->SamplerUnits
[unit
];
282 /* Don't update the sampler for TBOs. cso_context will not bind sampler
283 * states that are NULL.
285 if (samplers_used
& 1 &&
286 ctx
->Texture
.Unit
[tex_unit
]._Current
->Target
!= GL_TEXTURE_BUFFER
) {
287 st_convert_sampler_from_unit(st
, sampler
, tex_unit
);
288 states
[unit
] = sampler
;
294 /* For any external samplers with multiplaner YUV, stuff the additional
295 * sampler states we need at the end.
297 * Just re-use the existing sampler-state from the primary slot.
299 while (unlikely(external_samplers_used
)) {
300 GLuint unit
= u_bit_scan(&external_samplers_used
);
302 struct st_texture_object
*stObj
=
303 st_get_texture_object(st
->ctx
, prog
, unit
);
304 struct pipe_sampler_state
*sampler
= samplers
+ unit
;
309 switch (st_get_view_format(stObj
)) {
310 case PIPE_FORMAT_NV12
:
311 /* we need one additional sampler: */
312 extra
= u_bit_scan(&free_slots
);
313 states
[extra
] = sampler
;
315 case PIPE_FORMAT_IYUV
:
316 /* we need two additional samplers: */
317 extra
= u_bit_scan(&free_slots
);
318 states
[extra
] = sampler
;
319 extra
= u_bit_scan(&free_slots
);
320 states
[extra
] = sampler
;
326 num_samplers
= MAX2(num_samplers
, extra
+ 1);
329 cso_set_samplers(st
->cso_context
, shader_stage
, num_samplers
, states
);
330 *out_num_samplers
= num_samplers
;
335 st_update_vertex_samplers(struct st_context
*st
)
337 const struct gl_context
*ctx
= st
->ctx
;
339 update_shader_samplers(st
,
341 ctx
->VertexProgram
._Current
,
342 st
->state
.samplers
[PIPE_SHADER_VERTEX
],
343 &st
->state
.num_samplers
[PIPE_SHADER_VERTEX
]);
348 st_update_tessctrl_samplers(struct st_context
*st
)
350 const struct gl_context
*ctx
= st
->ctx
;
352 if (ctx
->TessCtrlProgram
._Current
) {
353 update_shader_samplers(st
,
354 PIPE_SHADER_TESS_CTRL
,
355 ctx
->TessCtrlProgram
._Current
,
356 st
->state
.samplers
[PIPE_SHADER_TESS_CTRL
],
357 &st
->state
.num_samplers
[PIPE_SHADER_TESS_CTRL
]);
363 st_update_tesseval_samplers(struct st_context
*st
)
365 const struct gl_context
*ctx
= st
->ctx
;
367 if (ctx
->TessEvalProgram
._Current
) {
368 update_shader_samplers(st
,
369 PIPE_SHADER_TESS_EVAL
,
370 ctx
->TessEvalProgram
._Current
,
371 st
->state
.samplers
[PIPE_SHADER_TESS_EVAL
],
372 &st
->state
.num_samplers
[PIPE_SHADER_TESS_EVAL
]);
378 st_update_geometry_samplers(struct st_context
*st
)
380 const struct gl_context
*ctx
= st
->ctx
;
382 if (ctx
->GeometryProgram
._Current
) {
383 update_shader_samplers(st
,
384 PIPE_SHADER_GEOMETRY
,
385 ctx
->GeometryProgram
._Current
,
386 st
->state
.samplers
[PIPE_SHADER_GEOMETRY
],
387 &st
->state
.num_samplers
[PIPE_SHADER_GEOMETRY
]);
393 st_update_fragment_samplers(struct st_context
*st
)
395 const struct gl_context
*ctx
= st
->ctx
;
397 update_shader_samplers(st
,
398 PIPE_SHADER_FRAGMENT
,
399 ctx
->FragmentProgram
._Current
,
400 st
->state
.samplers
[PIPE_SHADER_FRAGMENT
],
401 &st
->state
.num_samplers
[PIPE_SHADER_FRAGMENT
]);
406 st_update_compute_samplers(struct st_context
*st
)
408 const struct gl_context
*ctx
= st
->ctx
;
410 if (ctx
->ComputeProgram
._Current
) {
411 update_shader_samplers(st
,
413 ctx
->ComputeProgram
._Current
,
414 st
->state
.samplers
[PIPE_SHADER_COMPUTE
],
415 &st
->state
.num_samplers
[PIPE_SHADER_COMPUTE
]);