<commit_before>from gl_pbuffer import Pbuffer
from gl_framebuffer_object import FramebufferObject
from pyglet.gl import *

# Auxiliar classes for render-to-texture

_best_grabber = None

def TextureGrabber():
    """Return an instance of the best texture grabbing class"""
    # Why this isn't done on module import? Because we need an initialized
    # GL Context to query availability of extensions
    global _best_grabber
    if _best_grabber is not None:
        return _best_grabber()
    # Preferred method: framebuffer object
    try:
        _best_grabber = FBOGrabber
        return _best_grabber()
    except:
        pass
    # Fallback: GL generic grabber
    _best_grabber = GenericGrabber
    return _best_grabber()

class GenericGrabber(object):
    def grab (self, texture):
        pass
    
    def before_render (self, texture):
        director.window.clear()
        
    def after_render (self, texture):
        buffer = image.get_buffer_manager().get_color_buffer()
        texture.blit_into(buffer, 0, 0, 0)

class PbufferGrabber(object):
    """Requires pbuffer extensions
    
    NOT WORKING
    """
    def grab (self, texture):
        self.pbuf = Pbuffer(director.window, [
            GLX_CONFIG_CAVEAT, GLX_NONE,
            GLX_RED_SIZE, 8,
            GLX_GREEN_SIZE, 8,
            GLX_BLUE_SIZE, 8,
            GLX_DEPTH_SIZE, 24,
            GLX_DOUBLEBUFFER, 1,
            ])
    
    def before_render (self, texture):
        self.pbuf.switch_to()
        #gl.glViewport(0, 0, self.pbuf.width, self.pbuf.height)
        #gl.glMatrixMode(gl.GL_PROJECTION)
        #gl.glLoadIdentity()
        #gl.glOrtho(0, self.pbuf.width, 0, self.pbuf.height, -1, 1)
        #gl.glMatrixMode(gl.GL_MODELVIEW)
        
    def after_render (self, texture):
        buffer = image.get_buffer_manager().get_color_buffer()
        texture.blit_into (buffer, 0, 0, 0)
        director.window.switch_to()


class FBOGrabber(object):
    """Base class for texture based effects. Prepare setups self.texture,
    with a window sized capture. Show just blits the texture, override to
    do more interesting things.
    Requires framebuffer_object extensions"""
    def grab (self, texture):
        self.fbuf = FramebufferObject()
        self.fbuf.bind()
        self.fbuf.texture2d (texture)
        self.fbuf.check_status()
        self.fbuf.unbind()
    
    def before_render (self, texture):<commit_msg>Improved autodetection of RTT best method. Printed a warning when using fallback<commit_after>from gl_pbuffer import Pbuffer
from gl_framebuffer_object import FramebufferObject
from pyglet.gl import *
from director import director
from pyglet import image

# Auxiliar classes for render-to-texture

_best_grabber = None

def TextureGrabber():
    """Return an instance of the best texture grabbing class"""
    # Why this isn't done on module import? Because we need an initialized
    # GL Context to query availability of extensions
    global _best_grabber
    if _best_grabber is not None:
        return _best_grabber()
    # Preferred method: framebuffer object
    try:
        _best_grabber = FBOGrabber
        return _best_grabber()
    except:
        pass
    # Fallback: GL generic grabber
    print "Warning: using fallback texture grabber. Effects will treat" \
          "layer transparency as black"
    _best_grabber = GenericGrabber
    return _best_grabber()

class GenericGrabber(object):
    def grab (self, texture):
        pass
    
    def before_render (self, texture):
        director.window.clear()
        
    def after_render (self, texture):
        buffer = image.get_buffer_manager().get_color_buffer()
        texture.blit_into(buffer, 0, 0, 0)

class PbufferGrabber(object):
    """Requires pbuffer extensions
    
    NOT WORKING
    """
    def grab (self, texture):
        self.pbuf = Pbuffer(director.window, [
            GLX_CONFIG_CAVEAT, GLX_NONE,
            GLX_RED_SIZE, 8,
            GLX_GREEN_SIZE, 8,
            GLX_BLUE_SIZE, 8,
            GLX_DEPTH_SIZE, 24,
            GLX_DOUBLEBUFFER, 1,
            ])
    
    def before_render (self, texture):
        self.pbuf.switch_to()
        #gl.glViewport(0, 0, self.pbuf.width, self.pbuf.height)
        #gl.glMatrixMode(gl.GL_PROJECTION)
        #gl.glLoadIdentity()
        #gl.glOrtho(0, self.pbuf.width, 0, self.pbuf.height, -1, 1)
        #gl.glMatrixMode(gl.GL_MODELVIEW)
        
    def after_render (self, texture):
        buffer = image.get_buffer_manager().get_color_buffer()
        texture.blit_into (buffer, 0, 0, 0)
        director.window.switch_to()


class FBOGrabber(object):
    """Base class for texture based effects. Prepare setups self.texture,
    with a window sized capture. Show just blits the texture, override to
    do more interesting things.
    Requires framebuffer_object extensions"""
    def __init__ (self):
        # This code is on init to make creation fail if FBOs are not available
        self.fbuf = FramebufferObject()

    def grab (self, texture):
        self.fbuf.bind()
        self.fbuf.texture2d (texture)
        self.fbuf.check_status()
        self.fbuf.unbind()
    
    def before_render (self, texture):
{'fraction_non_alphanumeric': 0.08424697057126371, 'fraction_numerical': 0.007309097903442969, 'mean_word_length': 2.631284916201117, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1509950', 'n_tokens_mistral': 1661, 'n_tokens_neox': 1613, 'n_words': 442}