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_sampler_view.h"
47 #include "st_texture.h"
48 #include "pipe/p_context.h"
49 #include "pipe/p_defines.h"
51 #include "cso_cache/cso_context.h"
53 #include "util/u_format.h"
57 * Convert GLenum texcoord wrap tokens to pipe tokens.
60 gl_wrap_xlate(GLenum wrap
)
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
,
74 return table
[wrap
& 0x1f];
79 gl_filter_to_mip_filter(GLenum filter
)
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
;
87 return PIPE_TEX_MIPFILTER_LINEAR
;
92 gl_filter_to_img_filter(GLenum filter
)
94 /* Take advantage of how the enums are defined. */
96 return PIPE_TEX_FILTER_LINEAR
;
98 return PIPE_TEX_FILTER_NEAREST
;
103 * Convert a gl_sampler_object to a pipe_sampler_state object.
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
)
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
);
117 sampler
->min_img_filter
= gl_filter_to_img_filter(msamp
->MinFilter
);
118 sampler
->min_mip_filter
= gl_filter_to_mip_filter(msamp
->MinFilter
);
119 sampler
->mag_img_filter
= gl_filter_to_img_filter(msamp
->MagFilter
);
121 if (texobj
->Target
!= GL_TEXTURE_RECTANGLE_ARB
)
122 sampler
->normalized_coords
= 1;
124 sampler
->lod_bias
= msamp
->LodBias
+ tex_unit_lod_bias
;
125 /* Reduce the number of states by allowing only the values that AMD GCN
126 * can represent. Apps use lod_bias for smooth transitions to bigger mipmap
129 sampler
->lod_bias
= CLAMP(sampler
->lod_bias
, -16, 16);
130 sampler
->lod_bias
= floorf(sampler
->lod_bias
* 256) / 256;
132 sampler
->min_lod
= MAX2(msamp
->MinLod
, 0.0f
);
133 sampler
->max_lod
= msamp
->MaxLod
;
134 if (sampler
->max_lod
< sampler
->min_lod
) {
135 /* The GL spec doesn't seem to specify what to do in this case.
138 float tmp
= sampler
->max_lod
;
139 sampler
->max_lod
= sampler
->min_lod
;
140 sampler
->min_lod
= tmp
;
141 assert(sampler
->min_lod
<= sampler
->max_lod
);
144 /* Check that only wrap modes using the border color have the first bit
147 STATIC_ASSERT(PIPE_TEX_WRAP_CLAMP
& 0x1);
148 STATIC_ASSERT(PIPE_TEX_WRAP_CLAMP_TO_BORDER
& 0x1);
149 STATIC_ASSERT(PIPE_TEX_WRAP_MIRROR_CLAMP
& 0x1);
150 STATIC_ASSERT(PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
& 0x1);
151 STATIC_ASSERT(((PIPE_TEX_WRAP_REPEAT
|
152 PIPE_TEX_WRAP_CLAMP_TO_EDGE
|
153 PIPE_TEX_WRAP_MIRROR_REPEAT
|
154 PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
) & 0x1) == 0);
156 /* For non-black borders... */
157 if (/* This is true if wrap modes are using the border color: */
158 (sampler
->wrap_s
| sampler
->wrap_t
| sampler
->wrap_r
) & 0x1 &&
159 (msamp
->BorderColor
.ui
[0] ||
160 msamp
->BorderColor
.ui
[1] ||
161 msamp
->BorderColor
.ui
[2] ||
162 msamp
->BorderColor
.ui
[3])) {
163 const GLboolean is_integer
= texobj
->_IsIntegerFormat
;
164 GLenum texBaseFormat
= _mesa_base_tex_image(texobj
)->_BaseFormat
;
166 if (st
->apply_texture_swizzle_to_border_color
) {
167 const struct st_texture_object
*stobj
= st_texture_object_const(texobj
);
168 /* XXX: clean that up to not use the sampler view at all */
169 const struct st_sampler_view
*sv
= st_texture_get_current_sampler_view(st
, stobj
);
172 struct pipe_sampler_view
*view
= sv
->view
;
173 union pipe_color_union tmp
;
174 const unsigned char swz
[4] =
182 st_translate_color(&msamp
->BorderColor
, &tmp
,
183 texBaseFormat
, is_integer
);
185 util_format_apply_color_swizzle(&sampler
->border_color
,
186 &tmp
, swz
, is_integer
);
188 st_translate_color(&msamp
->BorderColor
,
189 &sampler
->border_color
,
190 texBaseFormat
, is_integer
);
193 st_translate_color(&msamp
->BorderColor
,
194 &sampler
->border_color
,
195 texBaseFormat
, is_integer
);
199 sampler
->max_anisotropy
= (msamp
->MaxAnisotropy
== 1.0 ?
200 0 : (GLuint
) msamp
->MaxAnisotropy
);
202 /* If sampling a depth texture and using shadow comparison */
203 if (msamp
->CompareMode
== GL_COMPARE_R_TO_TEXTURE
) {
204 GLenum texBaseFormat
= _mesa_base_tex_image(texobj
)->_BaseFormat
;
206 if (texBaseFormat
== GL_DEPTH_COMPONENT
||
207 (texBaseFormat
== GL_DEPTH_STENCIL
&& !texobj
->StencilSampling
)) {
208 sampler
->compare_mode
= PIPE_TEX_COMPARE_R_TO_TEXTURE
;
209 sampler
->compare_func
= st_compare_func_to_pipe(msamp
->CompareFunc
);
213 /* Only set the seamless cube map texture parameter because the per-context
214 * enable should be ignored and treated as disabled when using texture
215 * handles, as specified by ARB_bindless_texture.
217 sampler
->seamless_cube_map
= msamp
->CubeMapSeamless
;
221 * Get a pipe_sampler_state object from a texture unit.
224 st_convert_sampler_from_unit(const struct st_context
*st
,
225 struct pipe_sampler_state
*sampler
,
228 const struct gl_texture_object
*texobj
;
229 struct gl_context
*ctx
= st
->ctx
;
230 const struct gl_sampler_object
*msamp
;
232 texobj
= ctx
->Texture
.Unit
[texUnit
]._Current
;
234 assert(texobj
->Target
!= GL_TEXTURE_BUFFER
);
236 msamp
= _mesa_get_samplerobj(ctx
, texUnit
);
238 st_convert_sampler(st
, texobj
, msamp
, ctx
->Texture
.Unit
[texUnit
].LodBias
,
241 sampler
->seamless_cube_map
|= ctx
->Texture
.CubeMapSeamless
;
246 * Update the gallium driver's sampler state for fragment, vertex or
247 * geometry shader stage.
250 update_shader_samplers(struct st_context
*st
,
251 enum pipe_shader_type shader_stage
,
252 const struct gl_program
*prog
,
253 struct pipe_sampler_state
*samplers
,
254 unsigned *out_num_samplers
)
256 struct gl_context
*ctx
= st
->ctx
;
257 GLbitfield samplers_used
= prog
->SamplersUsed
;
258 GLbitfield free_slots
= ~prog
->SamplersUsed
;
259 GLbitfield external_samplers_used
= prog
->ExternalSamplersUsed
;
260 unsigned unit
, num_samplers
;
261 struct pipe_sampler_state local_samplers
[PIPE_MAX_SAMPLERS
];
262 const struct pipe_sampler_state
*states
[PIPE_MAX_SAMPLERS
];
264 if (samplers_used
== 0x0) {
265 if (out_num_samplers
)
266 *out_num_samplers
= 0;
271 samplers
= local_samplers
;
273 num_samplers
= util_last_bit(samplers_used
);
275 /* loop over sampler units (aka tex image units) */
276 for (unit
= 0; samplers_used
; unit
++, samplers_used
>>= 1) {
277 struct pipe_sampler_state
*sampler
= samplers
+ unit
;
278 unsigned tex_unit
= prog
->SamplerUnits
[unit
];
280 /* Don't update the sampler for TBOs. cso_context will not bind sampler
281 * states that are NULL.
283 if (samplers_used
& 1 &&
284 ctx
->Texture
.Unit
[tex_unit
]._Current
->Target
!= GL_TEXTURE_BUFFER
) {
285 st_convert_sampler_from_unit(st
, sampler
, tex_unit
);
286 states
[unit
] = sampler
;
292 /* For any external samplers with multiplaner YUV, stuff the additional
293 * sampler states we need at the end.
295 * Just re-use the existing sampler-state from the primary slot.
297 while (unlikely(external_samplers_used
)) {
298 GLuint unit
= u_bit_scan(&external_samplers_used
);
300 struct st_texture_object
*stObj
=
301 st_get_texture_object(st
->ctx
, prog
, unit
);
302 struct pipe_sampler_state
*sampler
= samplers
+ unit
;
307 switch (st_get_view_format(stObj
)) {
308 case PIPE_FORMAT_NV12
:
309 /* we need one additional sampler: */
310 extra
= u_bit_scan(&free_slots
);
311 states
[extra
] = sampler
;
313 case PIPE_FORMAT_IYUV
:
314 /* we need two additional samplers: */
315 extra
= u_bit_scan(&free_slots
);
316 states
[extra
] = sampler
;
317 extra
= u_bit_scan(&free_slots
);
318 states
[extra
] = sampler
;
324 num_samplers
= MAX2(num_samplers
, extra
+ 1);
327 cso_set_samplers(st
->cso_context
, shader_stage
, num_samplers
, states
);
329 if (out_num_samplers
)
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
, NULL
, NULL
);
346 st_update_tessctrl_samplers(struct st_context
*st
)
348 const struct gl_context
*ctx
= st
->ctx
;
350 if (ctx
->TessCtrlProgram
._Current
) {
351 update_shader_samplers(st
,
352 PIPE_SHADER_TESS_CTRL
,
353 ctx
->TessCtrlProgram
._Current
, NULL
, NULL
);
359 st_update_tesseval_samplers(struct st_context
*st
)
361 const struct gl_context
*ctx
= st
->ctx
;
363 if (ctx
->TessEvalProgram
._Current
) {
364 update_shader_samplers(st
,
365 PIPE_SHADER_TESS_EVAL
,
366 ctx
->TessEvalProgram
._Current
, NULL
, NULL
);
372 st_update_geometry_samplers(struct st_context
*st
)
374 const struct gl_context
*ctx
= st
->ctx
;
376 if (ctx
->GeometryProgram
._Current
) {
377 update_shader_samplers(st
,
378 PIPE_SHADER_GEOMETRY
,
379 ctx
->GeometryProgram
._Current
, NULL
, NULL
);
385 st_update_fragment_samplers(struct st_context
*st
)
387 const struct gl_context
*ctx
= st
->ctx
;
389 update_shader_samplers(st
,
390 PIPE_SHADER_FRAGMENT
,
391 ctx
->FragmentProgram
._Current
,
392 st
->state
.frag_samplers
,
393 &st
->state
.num_frag_samplers
);
398 st_update_compute_samplers(struct st_context
*st
)
400 const struct gl_context
*ctx
= st
->ctx
;
402 if (ctx
->ComputeProgram
._Current
) {
403 update_shader_samplers(st
,
405 ctx
->ComputeProgram
._Current
, NULL
, NULL
);