"""framegen/aa_gl.py: the GLSL anti-aliasing against aa.py's numpy reference (needs an OpenGL context: skipped without a display), and who applies the Anti-aliasing setting (no OpenGL needed).""" import pathlib import sys import numpy as np import pytest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from framegen import aa, compare as C # noqa: E402 from framegen.settings import Settings # noqa: E402 def test_the_window_applies_the_setting_only_where_the_pipeline_leaves_it(): st = Settings() live = C.Live(st, lambda: C.Caps()) img = np.random.default_rng(0).integers(0, 255, (32, 48, 3), dtype=np.uint8) st.override("aa", "fxaa") assert live.window_aa(None) is None # no OpenGL pass: torch does it live.gl_aa = ("fxaa", "cas") assert live.window_aa(None) == "fxaa" assert live.source_side(img, None, img, 2) is img # left to the window st.override("aa", "smaa") assert live.window_aa(None) is None # SMAA-style stays torch st.override("aa", "fxaa") for k, v in C.settings_for("aa:off,fxaa").items(): st.override(k, v) assert live.window_aa(live.plan()) is None # the AA comparison is torch's, strip by strip for k, v in C.settings_for("scaler:off,bilinear").items(): st.override(k, v) assert live.window_aa(live.plan()) == "fxaa" def _context(): glfw = pytest.importorskip("glfw") pytest.importorskip("OpenGL") if not glfw.init(): pytest.skip("no display for OpenGL") glfw.window_hint(glfw.VISIBLE, glfw.FALSE) glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3) glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) win = glfw.create_window(16, 16, "aa_gl test", None, None) if not win: glfw.terminate() pytest.skip("no OpenGL 3.3 context") glfw.make_context_current(win) return glfw, win @pytest.mark.parametrize("mode", ["fxaa", "cas"]) def test_glsl_matches_the_numpy_reference(mode): glfw, win = _context() try: from OpenGL import GL from framegen.aa_gl import AaGl from test_compare import ALIASED img = np.ascontiguousarray(ALIASED) h, w = img.shape[:2] GL.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1) GL.glPixelStorei(GL.GL_PACK_ALIGNMENT, 1) texs = [] for data in (img, None): t = GL.glGenTextures(1) GL.glBindTexture(GL.GL_TEXTURE_2D, t) GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST) GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA8, w, h, 0, GL.GL_BGR, GL.GL_UNSIGNED_BYTE, data) texs.append(t) fbo = GL.glGenFramebuffers(1) GL.glBindFramebuffer(GL.GL_FRAMEBUFFER, fbo) GL.glFramebufferTexture2D(GL.GL_FRAMEBUFFER, GL.GL_COLOR_ATTACHMENT0, GL.GL_TEXTURE_2D, texs[1], 0) AaGl(GL).draw(mode, texs[0], fbo, w, h) GL.glBindFramebuffer(GL.GL_READ_FRAMEBUFFER, fbo) out = np.frombuffer(GL.glReadPixels(0, 0, w, h, GL.GL_BGR, GL.GL_UNSIGNED_BYTE), np.uint8).reshape(h, w, 3) ref = aa.apply(img, mode, gpu=False) diff = np.abs(out.astype(int) - ref) assert (diff > 1).mean() < 0.001 # the same filter, to within float rounding finally: glfw.destroy_window(win) glfw.terminate()