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
)
63 return PIPE_TEX_WRAP_REPEAT
;
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
;
86 gl_filter_to_mip_filter(GLenum filter
)
91 return PIPE_TEX_MIPFILTER_NONE
;
93 case GL_NEAREST_MIPMAP_NEAREST
:
94 case GL_LINEAR_MIPMAP_NEAREST
:
95 return PIPE_TEX_MIPFILTER_NEAREST
;
97 case GL_NEAREST_MIPMAP_LINEAR
:
98 case GL_LINEAR_MIPMAP_LINEAR
:
99 return PIPE_TEX_MIPFILTER_LINEAR
;
103 return PIPE_TEX_MIPFILTER_NONE
;
109 gl_filter_to_img_filter(GLenum filter
)
113 case GL_NEAREST_MIPMAP_NEAREST
:
114 case GL_NEAREST_MIPMAP_LINEAR
:
115 return PIPE_TEX_FILTER_NEAREST
;
118 case GL_LINEAR_MIPMAP_NEAREST
:
119 case GL_LINEAR_MIPMAP_LINEAR
:
120 return PIPE_TEX_FILTER_LINEAR
;
124 return PIPE_TEX_FILTER_NEAREST
;
130 convert_sampler(struct st_context
*st
,
131 struct pipe_sampler_state
*sampler
,
134 const struct gl_texture_object
*texobj
;
135 struct gl_context
*ctx
= st
->ctx
;
136 const struct gl_sampler_object
*msamp
;
137 GLenum texBaseFormat
;
139 texobj
= ctx
->Texture
.Unit
[texUnit
]._Current
;
142 msamp
= _mesa_get_samplerobj(ctx
, texUnit
);
143 texBaseFormat
= _mesa_texture_base_format(texobj
);
145 memset(sampler
, 0, sizeof(*sampler
));
146 sampler
->wrap_s
= gl_wrap_xlate(msamp
->WrapS
);
147 sampler
->wrap_t
= gl_wrap_xlate(msamp
->WrapT
);
148 sampler
->wrap_r
= gl_wrap_xlate(msamp
->WrapR
);
150 sampler
->min_img_filter
= gl_filter_to_img_filter(msamp
->MinFilter
);
151 sampler
->min_mip_filter
= gl_filter_to_mip_filter(msamp
->MinFilter
);
152 sampler
->mag_img_filter
= gl_filter_to_img_filter(msamp
->MagFilter
);
154 if (texobj
->Target
!= GL_TEXTURE_RECTANGLE_ARB
)
155 sampler
->normalized_coords
= 1;
157 sampler
->lod_bias
= ctx
->Texture
.Unit
[texUnit
].LodBias
+ msamp
->LodBias
;
158 /* Reduce the number of states by allowing only the values that AMD GCN
159 * can represent. Apps use lod_bias for smooth transitions to bigger mipmap
162 sampler
->lod_bias
= CLAMP(sampler
->lod_bias
, -16, 16);
163 sampler
->lod_bias
= floorf(sampler
->lod_bias
* 256) / 256;
165 sampler
->min_lod
= MAX2(msamp
->MinLod
, 0.0f
);
166 sampler
->max_lod
= msamp
->MaxLod
;
167 if (sampler
->max_lod
< sampler
->min_lod
) {
168 /* The GL spec doesn't seem to specify what to do in this case.
171 float tmp
= sampler
->max_lod
;
172 sampler
->max_lod
= sampler
->min_lod
;
173 sampler
->min_lod
= tmp
;
174 assert(sampler
->min_lod
<= sampler
->max_lod
);
177 /* For non-black borders... */
178 if (msamp
->BorderColor
.ui
[0] ||
179 msamp
->BorderColor
.ui
[1] ||
180 msamp
->BorderColor
.ui
[2] ||
181 msamp
->BorderColor
.ui
[3]) {
182 const struct st_texture_object
*stobj
= st_texture_object_const(texobj
);
183 const GLboolean is_integer
= texobj
->_IsIntegerFormat
;
184 const struct pipe_sampler_view
*sv
= NULL
;
185 union pipe_color_union border_color
;
188 /* Just search for the first used view. We can do this because the
189 swizzle is per-texture, not per context. */
190 /* XXX: clean that up to not use the sampler view at all */
191 for (i
= 0; i
< stobj
->num_sampler_views
; ++i
) {
192 if (stobj
->sampler_views
[i
]) {
193 sv
= stobj
->sampler_views
[i
];
198 if (st
->apply_texture_swizzle_to_border_color
&& sv
) {
199 const unsigned char swz
[4] =
207 st_translate_color(&msamp
->BorderColor
,
209 texBaseFormat
, is_integer
);
211 util_format_apply_color_swizzle(&sampler
->border_color
,
212 &border_color
, swz
, is_integer
);
214 st_translate_color(&msamp
->BorderColor
,
215 &sampler
->border_color
,
216 texBaseFormat
, is_integer
);
220 sampler
->max_anisotropy
= (msamp
->MaxAnisotropy
== 1.0 ?
221 0 : (GLuint
) msamp
->MaxAnisotropy
);
223 /* If sampling a depth texture and using shadow comparison */
224 if ((texBaseFormat
== GL_DEPTH_COMPONENT
||
225 (texBaseFormat
== GL_DEPTH_STENCIL
&& !texobj
->StencilSampling
)) &&
226 msamp
->CompareMode
== GL_COMPARE_R_TO_TEXTURE
) {
227 sampler
->compare_mode
= PIPE_TEX_COMPARE_R_TO_TEXTURE
;
228 sampler
->compare_func
= st_compare_func_to_pipe(msamp
->CompareFunc
);
231 sampler
->seamless_cube_map
=
232 ctx
->Texture
.CubeMapSeamless
|| msamp
->CubeMapSeamless
;
237 * Update the gallium driver's sampler state for fragment, vertex or
238 * geometry shader stage.
241 update_shader_samplers(struct st_context
*st
,
242 enum pipe_shader_type shader_stage
,
243 const struct gl_program
*prog
,
245 struct pipe_sampler_state
*samplers
,
246 unsigned *num_samplers
)
248 GLbitfield samplers_used
= prog
->SamplersUsed
;
249 GLbitfield free_slots
= ~prog
->SamplersUsed
;
250 GLbitfield external_samplers_used
= prog
->ExternalSamplersUsed
;
252 const GLuint old_max
= *num_samplers
;
253 const struct pipe_sampler_state
*states
[PIPE_MAX_SAMPLERS
];
255 if (*num_samplers
== 0 && samplers_used
== 0x0)
260 /* loop over sampler units (aka tex image units) */
261 for (unit
= 0; unit
< max_units
; unit
++, samplers_used
>>= 1) {
262 struct pipe_sampler_state
*sampler
= samplers
+ unit
;
264 if (samplers_used
& 1) {
265 const GLuint texUnit
= prog
->SamplerUnits
[unit
];
267 convert_sampler(st
, sampler
, texUnit
);
268 states
[unit
] = sampler
;
269 *num_samplers
= unit
+ 1;
271 else if (samplers_used
!= 0 || unit
< old_max
) {
275 /* if we've reset all the old samplers and we have no more new ones */
280 /* For any external samplers with multiplaner YUV, stuff the additional
281 * sampler states we need at the end.
283 * Just re-use the existing sampler-state from the primary slot.
285 while (unlikely(external_samplers_used
)) {
286 GLuint unit
= u_bit_scan(&external_samplers_used
);
288 struct st_texture_object
*stObj
=
289 st_get_texture_object(st
->ctx
, prog
, unit
);
290 struct pipe_sampler_state
*sampler
= samplers
+ unit
;
295 switch (st_get_view_format(stObj
)) {
296 case PIPE_FORMAT_NV12
:
297 /* we need one additional sampler: */
298 extra
= u_bit_scan(&free_slots
);
299 states
[extra
] = sampler
;
301 case PIPE_FORMAT_IYUV
:
302 /* we need two additional samplers: */
303 extra
= u_bit_scan(&free_slots
);
304 states
[extra
] = sampler
;
305 extra
= u_bit_scan(&free_slots
);
306 states
[extra
] = sampler
;
312 *num_samplers
= MAX2(*num_samplers
, extra
+ 1);
315 cso_set_samplers(st
->cso_context
, shader_stage
, *num_samplers
, states
);
320 update_vertex_samplers(struct st_context
*st
)
322 const struct gl_context
*ctx
= st
->ctx
;
324 update_shader_samplers(st
,
326 ctx
->VertexProgram
._Current
,
327 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTextureImageUnits
,
328 st
->state
.samplers
[PIPE_SHADER_VERTEX
],
329 &st
->state
.num_samplers
[PIPE_SHADER_VERTEX
]);
334 update_tessctrl_samplers(struct st_context
*st
)
336 const struct gl_context
*ctx
= st
->ctx
;
338 if (ctx
->TessCtrlProgram
._Current
) {
339 update_shader_samplers(st
,
340 PIPE_SHADER_TESS_CTRL
,
341 ctx
->TessCtrlProgram
._Current
,
342 ctx
->Const
.Program
[MESA_SHADER_TESS_CTRL
].MaxTextureImageUnits
,
343 st
->state
.samplers
[PIPE_SHADER_TESS_CTRL
],
344 &st
->state
.num_samplers
[PIPE_SHADER_TESS_CTRL
]);
350 update_tesseval_samplers(struct st_context
*st
)
352 const struct gl_context
*ctx
= st
->ctx
;
354 if (ctx
->TessEvalProgram
._Current
) {
355 update_shader_samplers(st
,
356 PIPE_SHADER_TESS_EVAL
,
357 ctx
->TessEvalProgram
._Current
,
358 ctx
->Const
.Program
[MESA_SHADER_TESS_EVAL
].MaxTextureImageUnits
,
359 st
->state
.samplers
[PIPE_SHADER_TESS_EVAL
],
360 &st
->state
.num_samplers
[PIPE_SHADER_TESS_EVAL
]);
366 update_geometry_samplers(struct st_context
*st
)
368 const struct gl_context
*ctx
= st
->ctx
;
370 if (ctx
->GeometryProgram
._Current
) {
371 update_shader_samplers(st
,
372 PIPE_SHADER_GEOMETRY
,
373 ctx
->GeometryProgram
._Current
,
374 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxTextureImageUnits
,
375 st
->state
.samplers
[PIPE_SHADER_GEOMETRY
],
376 &st
->state
.num_samplers
[PIPE_SHADER_GEOMETRY
]);
382 update_fragment_samplers(struct st_context
*st
)
384 const struct gl_context
*ctx
= st
->ctx
;
386 update_shader_samplers(st
,
387 PIPE_SHADER_FRAGMENT
,
388 ctx
->FragmentProgram
._Current
,
389 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxTextureImageUnits
,
390 st
->state
.samplers
[PIPE_SHADER_FRAGMENT
],
391 &st
->state
.num_samplers
[PIPE_SHADER_FRAGMENT
]);
396 update_compute_samplers(struct st_context
*st
)
398 const struct gl_context
*ctx
= st
->ctx
;
400 if (ctx
->ComputeProgram
._Current
) {
401 update_shader_samplers(st
,
403 ctx
->ComputeProgram
._Current
,
404 ctx
->Const
.Program
[MESA_SHADER_COMPUTE
].MaxTextureImageUnits
,
405 st
->state
.samplers
[PIPE_SHADER_COMPUTE
],
406 &st
->state
.num_samplers
[PIPE_SHADER_COMPUTE
]);
411 const struct st_tracked_state st_update_vertex_sampler
= {
412 update_vertex_samplers
/* update */
415 const struct st_tracked_state st_update_tessctrl_sampler
= {
416 update_tessctrl_samplers
/* update */
419 const struct st_tracked_state st_update_tesseval_sampler
= {
420 update_tesseval_samplers
/* update */
423 const struct st_tracked_state st_update_geometry_sampler
= {
424 update_geometry_samplers
/* update */
427 const struct st_tracked_state st_update_fragment_sampler
= {
428 update_fragment_samplers
/* update */
431 const struct st_tracked_state st_update_compute_sampler
= {
432 update_compute_samplers
/* update */