<commit_before>#=== SHADER ==========================================================================================
# Fragment shaders, filters, Frame Buffer Object (FBO)
# Authors: <NAME>, <NAME>
# License: GPL (see LICENSE.txt for details).
# Copyright (c) 2008 City In A Bottle (cityinabottle.org)

from pyglet.gl import *
from geometry  import lerp
from math      import radians

#=====================================================================================================

pow2 = [2**n for n in range(20)] # [1, 2, 4, 8, 16, 32, 64, ...]

def ceil2(x):
    """ Returns the nearest power of 2 that is higher than x, e.g. 700 => 1024.
    """
    for y in pow2:
        if y >= x: return y
            
def extent2(texture):
    """ Returns the extent of the image data (0.0-1.0, 0.0-1.0) inside its texture owner.
        Textures have a size power of 2 (512, 1024, ...), but the actual image can be smaller.
        For example: a 400x250 image will be loaded in a 512x256 texture.
        Its extent is (0.78, 0.98), the remainder of the texture is transparent.
    """
    return (texture.tex_coords[3], texture.tex_coords[7])
    
def ratio2(texture1, texture2):
    """ Returns the size ratio (0.0-1.0, 0.0-1.0) of two texture owners.
    """
    return (
        float(ceil2(texture1.width)) / ceil2(texture2.width), 
        float(ceil2(texture1.height)) / ceil2(texture2.height)
    )
    
def find(f, seq):
    """ Return first item in the sequence where f(item) == True.
    """
    for item in seq:
        if f(item): return item
        
def clamp(value, a, b):
    """ Returns value clamped between a (minimum) and b (maximum).
    """
    return max(a, min(value, b))

#=====================================================================================================

#--- SHADER ------------------------------------------------------------------------------------------
# A shader is a pixel effect (motion blur, fog, glow) executed on the GPU.
# The effect has two distinct parts: a vertex shader and a fragment shader.
# The vertex shader retrieves the coordinates of the current pixel.
# The fragment shader manipulates the color of the current pixel.
# http://www.lighthouse3d.com/opengl/glsl/index.php?fragmentp
# Shaders are written in GLSL and expect their variables to be set from glUniform() calls.
# The Shader class compiles the source code and has an easy way to pass variables to GLSL.
# e.g. shader = Shader(fragment=open("colorize.frag").read())
#      shader.set("color", vec4(1, 0.8, 1, 1))
#      shader.push()
#      image("box.png", 0, 0)
#      shader.pop()

DEFAULT_VERTEX_SHADER = '''
void main() { 
    gl_TexCoord[0] = gl_MultiTexCoord0; 
    gl_Position = ftransform(); 
}'''

DEFAULT_FRAGMENT_SHADER = '''
uniform sampler2D src;
void main() {
    gl_FragColor = texture2D(src, gl_TexCoord[0].xy);
}'''

class vector(tuple): 
    pass
    
def vec2(f1, f2):
    return vector((f1, f2))
def vec3(f1, f2, f3):
    return vector((f1, f2, f3))
def vec4(f1, f2, f3, f4):
    return vector((f1, f2, f3, f4))

COMPILE = "compile" # Error occurs during glCompileShader().
BUILD   = "build"   # Error occurs during glLinkProgram().
class ShaderError(Exception):
    def __init__(self, value, type=COMPILE):
        Exception.__init__(self, "%s error: %s" % (type, value))
        self.value = value
        self.type  = type

class Shader(object):
   
    def __init__(self, vertex=DEFAULT_VERTEX_SHADER, fragment=DEFAULT_FRAGMENT_SHADER):
        """ A per-pixel shader effect (blur, fog, glow, ...) executed on the GPU.
            Shader wraps a compiled GLSL program and facilitates passing parameters to it.
            The fragment and vertex parameters contain the GLSL source code to execute.
            Raises a ShaderError if the source fails to compile.
            Once compiled, you can set uniform variables in the GLSL source with Shader.set().
        """
        self._vertex   = vertex   # GLSL source for vertex shader.
        self._fragment = fragment # GLSL source for fragment shader.
        self._compiled = []
        self._program  = None
        self._active   = False
        self.variables = {}
        self._build()
    
    def _compile(self, source, type=GL_VERTEX_SHADER):
        # Compile the GLSL source code, either as GL_FRAGMENT_SHADER or GL_VERTEX_SHADER.
        # If the source fails to compile, retrieve the error message and raise ShaderError.
        # Store the compiled shader so we can delete it later on.
        shader = glCreateShader(type)
        length = c_int(-1)
        status = c_int(-1)
        glShaderSource(shader, 1, cast(byref(c_char_p(source)), POINTER(POINTER(c_char))), byref(length))
        glCompileShader(shader)
        glGetShaderiv(shader, GL_COMPILE_STATUS, byref(status))
        if status.value == 0:
            raise self._error(shader, type=COMPILE)
        self._compiled.append(shader)
        return shader
        
    def _build(self):
        # Each Shader has its own OpenGL rendering program and you need to switch between them.
        # Compile fragment and vertex shaders and build the program.
        program = glCreateProgram()
        status  = c_int(-1)
        if self._vertex:
            glAttachShader(program, self._compile(self._vertex, GL_VERTEX_SHADER))
        if self._fragment:
            glAttachShader(program, self._compile(self._fragment, GL_FRAGMENT_SHADER))
        glLinkProgram(program)
        glGetProgramiv(program, GL_LINK_STATUS, byref(status))
        if status.value == 0:
            raise self._error(program, type=BUILD)
        self._program = program
<commit_msg>Correct pointer magic incantation when compiling shaders for Windows systems.<commit_after>#=== SHADER ==========================================================================================
# Fragment shaders, filters, Frame Buffer Object (FBO)
# Authors: <NAME>, <NAME>
# License: GPL (see LICENSE.txt for details).
# Copyright (c) 2008 City In A Bottle (cityinabottle.org)

from pyglet.gl import *
from geometry  import lerp
from math      import radians

#=====================================================================================================

pow2 = [2**n for n in range(20)] # [1, 2, 4, 8, 16, 32, 64, ...]

def ceil2(x):
    """ Returns the nearest power of 2 that is higher than x, e.g. 700 => 1024.
    """
    for y in pow2:
        if y >= x: return y
            
def extent2(texture):
    """ Returns the extent of the image data (0.0-1.0, 0.0-1.0) inside its texture owner.
        Textures have a size power of 2 (512, 1024, ...), but the actual image can be smaller.
        For example: a 400x250 image will be loaded in a 512x256 texture.
        Its extent is (0.78, 0.98), the remainder of the texture is transparent.
    """
    return (texture.tex_coords[3], texture.tex_coords[7])
    
def ratio2(texture1, texture2):
    """ Returns the size ratio (0.0-1.0, 0.0-1.0) of two texture owners.
    """
    return (
        float(ceil2(texture1.width)) / ceil2(texture2.width), 
        float(ceil2(texture1.height)) / ceil2(texture2.height)
    )
    
def find(f, seq):
    """ Return first item in the sequence where f(item) == True.
    """
    for item in seq:
        if f(item): return item
        
def clamp(value, a, b):
    """ Returns value clamped between a (minimum) and b (maximum).
    """
    return max(a, min(value, b))

#=====================================================================================================

#--- SHADER ------------------------------------------------------------------------------------------
# A shader is a pixel effect (motion blur, fog, glow) executed on the GPU.
# The effect has two distinct parts: a vertex shader and a fragment shader.
# The vertex shader retrieves the coordinates of the current pixel.
# The fragment shader manipulates the color of the current pixel.
# http://www.lighthouse3d.com/opengl/glsl/index.php?fragmentp
# Shaders are written in GLSL and expect their variables to be set from glUniform() calls.
# The Shader class compiles the source code and has an easy way to pass variables to GLSL.
# e.g. shader = Shader(fragment=open("colorize.frag").read())
#      shader.set("color", vec4(1, 0.8, 1, 1))
#      shader.push()
#      image("box.png", 0, 0)
#      shader.pop()

DEFAULT_VERTEX_SHADER = '''
void main() { 
    gl_TexCoord[0] = gl_MultiTexCoord0; 
    gl_Position = ftransform(); 
}'''

DEFAULT_FRAGMENT_SHADER = '''
uniform sampler2D src;
void main() {
    gl_FragColor = texture2D(src, gl_TexCoord[0].xy);
}'''

class vector(tuple): 
    pass
    
def vec2(f1, f2):
    return vector((f1, f2))
def vec3(f1, f2, f3):
    return vector((f1, f2, f3))
def vec4(f1, f2, f3, f4):
    return vector((f1, f2, f3, f4))

COMPILE = "compile" # Error occurs during glCompileShader().
BUILD   = "build"   # Error occurs during glLinkProgram().
class ShaderError(Exception):
    def __init__(self, value, type=COMPILE):
        Exception.__init__(self, "%s error: %s" % (type, value))
        self.value = value
        self.type  = type

class Shader(object):
   
    def __init__(self, vertex=DEFAULT_VERTEX_SHADER, fragment=DEFAULT_FRAGMENT_SHADER):
        """ A per-pixel shader effect (blur, fog, glow, ...) executed on the GPU.
            Shader wraps a compiled GLSL program and facilitates passing parameters to it.
            The fragment and vertex parameters contain the GLSL source code to execute.
            Raises a ShaderError if the source fails to compile.
            Once compiled, you can set uniform variables in the GLSL source with Shader.set().
        """
        self._vertex   = vertex   # GLSL source for vertex shader.
        self._fragment = fragment # GLSL source for fragment shader.
        self._compiled = []
        self._program  = None
        self._active   = False
        self.variables = {}
        self._build()
    
    def _compile(self, source, type=GL_VERTEX_SHADER):
        # Compile the GLSL source code, either as GL_FRAGMENT_SHADER or GL_VERTEX_SHADER.
        # If the source fails to compile, retrieve the error message and raise ShaderError.
        # Store the compiled shader so we can delete it later on.
        shader = glCreateShader(type)
        status = c_int(-1)
        glShaderSource(shader, 1, cast(pointer(c_char_p(source)), POINTER(POINTER(c_char))), None)
        glCompileShader(shader)
        glGetShaderiv(shader, GL_COMPILE_STATUS, byref(status))
        if status.value == 0:
            raise self._error(shader, type=COMPILE)
        self._compiled.append(shader)
        return shader
        
    def _build(self):
        # Each Shader has its own OpenGL rendering program and you need to switch between them.
        # Compile fragment and vertex shaders and build the program.
        program = glCreateProgram()
        status  = c_int(-1)
        if self._vertex:
            glAttachShader(program, self._compile(self._vertex, GL_VERTEX_SHADER))
        if self._fragment:
            glAttachShader(program, self._compile(self._fragment, GL_FRAGMENT_SHADER))
        glLinkProgram(program)
        glGetProgramiv(program, GL_LINK_STATUS, byref(status))
        if status.value == 0:
            raise self._error(program, type=BUILD)
        self._program = program

{'fraction_non_alphanumeric': 0.16752031084422467, 'fraction_numerical': 0.021988696573648887, 'mean_word_length': 2.814415628157629, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6313801', 'n_tokens_mistral': 3461, 'n_tokens_neox': 3288, 'n_words': 1109}