11"""
22Simple cached factory for creating samplers
33"""
4+ import OpenGL
45from OpenGL import GL
56from OpenGL .GL .EXT import texture_filter_anisotropic as tfa
67
8+ OpenGL .ERROR_CHECKING = False
9+
710
811def create (mipmap = None , anisotropy = None ,
912 min_filter = None , mag_filter = None ,
10- wrap_s = None , wrap_t = None , wrap_r = None ):
13+ wrap_s = None , wrap_t = None , wrap_r = None ,
14+ texture_compare_mode = None ,
15+ texture_compare_func = None ):
1116 """Create sampler or get from cache"""
1217 return Sampler (
1318 mipmap = mipmap ,
@@ -17,47 +22,63 @@ def create(mipmap=None, anisotropy=None,
1722 wrap_s = wrap_s ,
1823 wrap_t = wrap_t ,
1924 wrap_r = wrap_r ,
25+ texture_compare_mode = texture_compare_mode ,
26+ texture_compare_func = texture_compare_func ,
2027 )
2128
2229
2330class Sampler :
2431 """Represents an immutable sampler we pre-set states"""
2532 def __init__ (self , mipmap = None , anisotropy = None ,
2633 min_filter = None , mag_filter = None ,
27- wrap_s = None , wrap_t = None , wrap_r = None ):
34+ wrap_s = None , wrap_t = None , wrap_r = None ,
35+ texture_compare_mode = None , texture_compare_func = None ):
2836 """Set sampler states"""
29- self .sid = None
37+ self ._id = None
38+
3039 self .mipmap = mipmap
3140 self .anisotropy = anisotropy
3241 self .min_filter = min_filter
3342 self .mag_filter = mag_filter
3443 self .wrap_s = wrap_s
3544 self .wrap_t = wrap_t
3645 self .wrap_r = wrap_r
46+ self .texture_compare_mode = texture_compare_mode
47+ self .texture_compare_func = texture_compare_func
48+
3749 self .states ()
3850
3951 def use (self , location = 0 ):
40- GL .glBindSampler (location , self .sid )
52+ GL .glBindSampler (location , self ._id )
53+
54+ def release (self , location = 0 ):
55+ GL .glBindSampler (location , 0 )
4156
4257 def states (self ):
43- self .sid = GL .glGenSamplers (1 )
58+ self ._id = GL .glGenSamplers (1 )
4459
4560 if self .wrap_s is not None :
46- GL .glSamplerParameteri (self .sid , GL .GL_TEXTURE_WRAP_S , self .wrap_s )
61+ GL .glSamplerParameteri (self ._id , GL .GL_TEXTURE_WRAP_S , self .wrap_s )
4762 if self .wrap_t is not None :
48- GL .glSamplerParameteri (self .sid , GL .GL_TEXTURE_WRAP_T , self .wrap_t )
63+ GL .glSamplerParameteri (self ._id , GL .GL_TEXTURE_WRAP_T , self .wrap_t )
4964 if self .wrap_r is not None :
50- GL .glSamplerParameteri (self .sid , GL .GL_TEXTURE_WRAP_R , self .wrap_r )
65+ GL .glSamplerParameteri (self ._id , GL .GL_TEXTURE_WRAP_R , self .wrap_r )
66+
67+ if self .min_filter is not None :
68+ GL .glSamplerParameteri (self ._id , GL .GL_TEXTURE_MIN_FILTER , self .min_filter )
5169
5270 if self .mag_filter is not None :
53- GL .glSamplerParameteri (self .sid , GL .GL_TEXTURE_MAG_FILTER , self .mag_filter )
71+ GL .glSamplerParameteri (self ._id , GL .GL_TEXTURE_MAG_FILTER , self .mag_filter )
5472
5573 if self .mipmap :
56- GL .glSamplerParameteri (self .sid , GL .GL_TEXTURE_MIN_FILTER , GL .GL_LINEAR_MIPMAP_LINEAR )
57- elif self .mipmap is not None :
58- GL .glSamplerParameteri (self .sid , GL .GL_TEXTURE_MIN_FILTER , self .min_filter )
74+ GL .glSamplerParameteri (self ._id , GL .GL_TEXTURE_MIN_FILTER , GL .GL_LINEAR_MIPMAP_LINEAR )
5975
6076 if self .anisotropy is not None and self .anisotropy > 0 :
6177 max_ani = GL .glGetFloatv (tfa .GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT )
6278 self .anisotropy = min (max_ani , self .anisotropy )
63- GL .glSamplerParameterf (self .sid , tfa .GL_TEXTURE_MAX_ANISOTROPY_EXT , self .anisotropy )
79+ GL .glSamplerParameterf (self ._id , tfa .GL_TEXTURE_MAX_ANISOTROPY_EXT , self .anisotropy )
80+
81+ if self .texture_compare_mode is False :
82+ GL .glSamplerParameteri (self ._id , GL .GL_TEXTURE_COMPARE_MODE , GL .GL_NONE )
83+ elif self .texture_compare_mode is True :
84+ GL .glSamplerParameteri (self ._id , GL .GL_TEXTURE_COMPARE_MODE , GL .GL_COMPARE_REF_TO_TEXTURE )
0 commit comments