|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import weakref |
| 4 | +from ctypes import byref, c_uint32 |
| 5 | +from typing import TYPE_CHECKING |
| 6 | + |
| 7 | +from pyglet import gl |
| 8 | + |
| 9 | +from .types import PyGLuint, compare_funcs |
| 10 | + |
| 11 | +if TYPE_CHECKING: |
| 12 | + from arcade.gl import Context, Texture2D |
| 13 | + |
| 14 | + |
| 15 | +class Sampler: |
| 16 | + """ |
| 17 | + OpenGL sampler object. |
| 18 | +
|
| 19 | + When bound to a texture unit it overrides all the |
| 20 | + sampling parameters of the texture channel. |
| 21 | + """ |
| 22 | + |
| 23 | + def __init__( |
| 24 | + self, |
| 25 | + ctx: "Context", |
| 26 | + texture: Texture2D, |
| 27 | + *, |
| 28 | + filter: tuple[PyGLuint, PyGLuint] | None = None, |
| 29 | + wrap_x: PyGLuint | None = None, |
| 30 | + wrap_y: PyGLuint | None = None, |
| 31 | + ): |
| 32 | + self._ctx = ctx |
| 33 | + self._glo = -1 |
| 34 | + |
| 35 | + value = c_uint32() |
| 36 | + gl.glGenSamplers(1, byref(value)) |
| 37 | + self._glo = value.value |
| 38 | + |
| 39 | + self.texture = texture |
| 40 | + |
| 41 | + # Default filters for float and integer textures |
| 42 | + # Integer textures should have NEAREST interpolation |
| 43 | + # by default 3.3 core doesn't really support it consistently. |
| 44 | + if "f" in self.texture._dtype: |
| 45 | + self._filter = gl.GL_LINEAR, gl.GL_LINEAR |
| 46 | + else: |
| 47 | + self._filter = gl.GL_NEAREST, gl.GL_NEAREST |
| 48 | + |
| 49 | + self._wrap_x = gl.GL_REPEAT |
| 50 | + self._wrap_y = gl.GL_REPEAT |
| 51 | + self._anisotropy = 1.0 |
| 52 | + self._compare_func: str | None = None |
| 53 | + |
| 54 | + # Only set texture parameters on non-multisample textures |
| 55 | + if self.texture._samples == 0: |
| 56 | + self.filter = filter or self._filter |
| 57 | + self.wrap_x = wrap_x or self._wrap_x |
| 58 | + self.wrap_y = wrap_y or self._wrap_y |
| 59 | + |
| 60 | + if self._ctx.gc_mode == "auto": |
| 61 | + weakref.finalize(self, Sampler.delete_glo, self._glo) |
| 62 | + |
| 63 | + @property |
| 64 | + def glo(self) -> PyGLuint: |
| 65 | + """The OpenGL sampler id""" |
| 66 | + return self._glo |
| 67 | + |
| 68 | + def use(self, unit: int): |
| 69 | + """ |
| 70 | + Bind the sampler to a texture unit |
| 71 | + """ |
| 72 | + gl.glBindSampler(unit, self._glo) |
| 73 | + |
| 74 | + def clear(self, unit: int): |
| 75 | + """ |
| 76 | + Unbind the sampler from a texture unit |
| 77 | + """ |
| 78 | + gl.glBindSampler(unit, 0) |
| 79 | + |
| 80 | + @property |
| 81 | + def filter(self) -> tuple[int, int]: |
| 82 | + """ |
| 83 | + Get or set the ``(min, mag)`` filter for this texture. |
| 84 | +
|
| 85 | + These are rules for how a texture interpolates. |
| 86 | + The filter is specified for minification and magnification. |
| 87 | +
|
| 88 | + Default value is ``LINEAR, LINEAR``. |
| 89 | + Can be set to ``NEAREST, NEAREST`` for pixelated graphics. |
| 90 | +
|
| 91 | + When mipmapping is used the min filter needs to be one of the |
| 92 | + ``MIPMAP`` variants. |
| 93 | +
|
| 94 | + Accepted values:: |
| 95 | +
|
| 96 | + # Enums can be accessed on the context or arcade.gl |
| 97 | + NEAREST # Nearest pixel |
| 98 | + LINEAR # Linear interpolate |
| 99 | + NEAREST_MIPMAP_NEAREST # Minification filter for mipmaps |
| 100 | + LINEAR_MIPMAP_NEAREST # Minification filter for mipmaps |
| 101 | + NEAREST_MIPMAP_LINEAR # Minification filter for mipmaps |
| 102 | + LINEAR_MIPMAP_LINEAR # Minification filter for mipmaps |
| 103 | +
|
| 104 | + Also see |
| 105 | +
|
| 106 | + * https://www.khronos.org/opengl/wiki/Texture#Mip_maps |
| 107 | + * https://www.khronos.org/opengl/wiki/Sampler_Object#Filtering |
| 108 | + """ |
| 109 | + return self._filter |
| 110 | + |
| 111 | + @filter.setter |
| 112 | + def filter(self, value: tuple[int, int]): |
| 113 | + if not isinstance(value, tuple) or not len(value) == 2: |
| 114 | + raise ValueError("Texture filter must be a 2 component tuple (min, mag)") |
| 115 | + |
| 116 | + self._filter = value |
| 117 | + gl.glSamplerParameteri(self._glo, gl.GL_TEXTURE_MIN_FILTER, self._filter[0]) |
| 118 | + gl.glSamplerParameteri(self._glo, gl.GL_TEXTURE_MAG_FILTER, self._filter[1]) |
| 119 | + |
| 120 | + @property |
| 121 | + def wrap_x(self) -> int: |
| 122 | + """ |
| 123 | + Get or set the horizontal wrapping of the texture. |
| 124 | +
|
| 125 | + This decides how textures are read when texture coordinates are outside |
| 126 | + the ``[0.0, 1.0]`` area. Default value is ``REPEAT``. |
| 127 | +
|
| 128 | + Valid options are:: |
| 129 | +
|
| 130 | + # Note: Enums can also be accessed in arcade.gl |
| 131 | + # Repeat pixels on the y axis |
| 132 | + texture.wrap_x = ctx.REPEAT |
| 133 | + # Repeat pixels on the y axis mirrored |
| 134 | + texture.wrap_x = ctx.MIRRORED_REPEAT |
| 135 | + # Repeat the edge pixels when reading outside the texture |
| 136 | + texture.wrap_x = ctx.CLAMP_TO_EDGE |
| 137 | + # Use the border color (black by default) when reading outside the texture |
| 138 | + texture.wrap_x = ctx.CLAMP_TO_BORDER |
| 139 | + """ |
| 140 | + return self._wrap_x |
| 141 | + |
| 142 | + @wrap_x.setter |
| 143 | + def wrap_x(self, value: int): |
| 144 | + self._wrap_x = value |
| 145 | + gl.glSamplerParameteri(self._glo, gl.GL_TEXTURE_WRAP_S, value) |
| 146 | + |
| 147 | + @property |
| 148 | + def wrap_y(self) -> int: |
| 149 | + """ |
| 150 | + Get or set the horizontal wrapping of the texture. |
| 151 | +
|
| 152 | + This decides how textures are read when texture coordinates are outside the |
| 153 | + ``[0.0, 1.0]`` area. Default value is ``REPEAT``. |
| 154 | +
|
| 155 | + Valid options are:: |
| 156 | +
|
| 157 | + # Note: Enums can also be accessed in arcade.gl |
| 158 | + # Repeat pixels on the x axis |
| 159 | + texture.wrap_x = ctx.REPEAT |
| 160 | + # Repeat pixels on the x axis mirrored |
| 161 | + texture.wrap_x = ctx.MIRRORED_REPEAT |
| 162 | + # Repeat the edge pixels when reading outside the texture |
| 163 | + texture.wrap_x = ctx.CLAMP_TO_EDGE |
| 164 | + # Use the border color (black by default) when reading outside the texture |
| 165 | + texture.wrap_x = ctx.CLAMP_TO_BORDER |
| 166 | + """ |
| 167 | + return self._wrap_y |
| 168 | + |
| 169 | + @wrap_y.setter |
| 170 | + def wrap_y(self, value: int): |
| 171 | + self._wrap_y = value |
| 172 | + gl.glSamplerParameteri(self._glo, gl.GL_TEXTURE_WRAP_T, value) |
| 173 | + |
| 174 | + @property |
| 175 | + def anisotropy(self) -> float: |
| 176 | + """Get or set the anisotropy for this texture.""" |
| 177 | + return self._anisotropy |
| 178 | + |
| 179 | + @anisotropy.setter |
| 180 | + def anisotropy(self, value): |
| 181 | + self._anisotropy = max(1.0, min(value, self._ctx.info.MAX_TEXTURE_MAX_ANISOTROPY)) |
| 182 | + gl.glSamplerParameterf(self._glo, gl.GL_TEXTURE_MAX_ANISOTROPY, self._anisotropy) |
| 183 | + |
| 184 | + @property |
| 185 | + def compare_func(self) -> str | None: |
| 186 | + """ |
| 187 | + Get or set the compare function for a depth texture:: |
| 188 | +
|
| 189 | + texture.compare_func = None # Disable depth comparison completely |
| 190 | + texture.compare_func = '<=' # GL_LEQUAL |
| 191 | + texture.compare_func = '<' # GL_LESS |
| 192 | + texture.compare_func = '>=' # GL_GEQUAL |
| 193 | + texture.compare_func = '>' # GL_GREATER |
| 194 | + texture.compare_func = '==' # GL_EQUAL |
| 195 | + texture.compare_func = '!=' # GL_NOTEQUAL |
| 196 | + texture.compare_func = '0' # GL_NEVER |
| 197 | + texture.compare_func = '1' # GL_ALWAYS |
| 198 | + """ |
| 199 | + return self._compare_func |
| 200 | + |
| 201 | + @compare_func.setter |
| 202 | + def compare_func(self, value: str | None): |
| 203 | + if not self.texture._depth: |
| 204 | + raise ValueError("Depth comparison function can only be set on depth textures") |
| 205 | + |
| 206 | + if not isinstance(value, str) and value is not None: |
| 207 | + raise ValueError(f"value must be as string: {self._compare_funcs.keys()}") |
| 208 | + |
| 209 | + func = compare_funcs.get(value, None) |
| 210 | + if func is None: |
| 211 | + raise ValueError(f"value must be as string: {compare_funcs.keys()}") |
| 212 | + |
| 213 | + self._compare_func = value |
| 214 | + if value is None: |
| 215 | + gl.glSamplerParameteri(self._glo, gl.GL_TEXTURE_COMPARE_MODE, gl.GL_NONE) |
| 216 | + else: |
| 217 | + gl.glSamplerParameteri( |
| 218 | + self._glo, gl.GL_TEXTURE_COMPARE_MODE, gl.GL_COMPARE_REF_TO_TEXTURE |
| 219 | + ) |
| 220 | + gl.glSamplerParameteri(self._glo, gl.GL_TEXTURE_COMPARE_FUNC, func) |
| 221 | + |
| 222 | + @staticmethod |
| 223 | + def delete_glo(glo: int) -> None: |
| 224 | + """ |
| 225 | + Delete the OpenGL object |
| 226 | + """ |
| 227 | + gl.glDeleteSamplers(1, glo) |
0 commit comments