Пример #1
0
    def _create(self):
        Texture._create(self)

        log.debug("GPU: Resizing texture(%s)"% (self.width))
        gl.glBindTexture(self.target, self._handle)
        gl.glTexImage1D(self.target, 0, self.format, self.width,
                        0, self.format, self.gtype, None)
Пример #2
0
    def _update(self):
        """ Actual upload of data to GPU memory  """

        log.debug("GPU: Updating %s" % self.name)

        # Check active status (mandatory)
#        if not self._active:
#            raise RuntimeError("Attribute variable is not active")
#        if self._data is None:
#            raise RuntimeError("Attribute variable data is not set")

        # Generic vertex attribute (all vertices receive the same value)
        if self._generic:
            if self._handle >= 0:
                gl.glDisableVertexAttribArray(self._handle)
                self._afunction(self._handle, *self._data)

        # Regular vertex buffer
        elif self.handle >= 0:
            #if self._need_update:
            #    self.data._update()
            #    self._need_update = False

            # Get relevant information from gl_typeinfo
            size, gtype, dtype = gl_typeinfo[self._gtype]
            stride = self.data.stride

            # Make offset a pointer, or it will be interpreted as a small array
            offset = ctypes.c_void_p(self.data.offset)
            gl.glEnableVertexAttribArray(self.handle)
            gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self.data.handle)
            gl.glVertexAttribPointer(self.handle, size, gtype,  gl.GL_FALSE, stride, offset)
Пример #3
0
    def _update(self):
        """ Actual upload of data to GPU memory  """

        log.debug("GPU: Updating %s" % self.name)

        # Check active status (mandatory)
        #        if not self._active:
        #            raise RuntimeError("Attribute variable is not active")
        #        if self._data is None:
        #            raise RuntimeError("Attribute variable data is not set")

        # Generic vertex attribute (all vertices receive the same value)
        if self._generic:
            if self._handle >= 0:
                gl.glDisableVertexAttribArray(self._handle)
                self._afunction(self._handle, *self._data)

        # Regular vertex buffer
        elif self.handle >= 0:
            #if self._need_update:
            #    self.data._update()
            #    self._need_update = False

            # Get relevant information from gl_typeinfo
            size, gtype, dtype = gl_typeinfo[self._gtype]
            stride = self.data.stride

            # Make offset a pointer, or it will be interpreted as a small array
            offset = ctypes.c_void_p(self.data.offset)
            gl.glEnableVertexAttribArray(self.handle)
            gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self.data.handle)
            gl.glVertexAttribPointer(self.handle, size, gtype, gl.GL_FALSE,
                                     stride, offset)
Пример #4
0
 def _activate(self):
     if self._gtype in (gl.GL_SAMPLER_1D, gl.GL_SAMPLER_2D,
                        gl.GL_SAMPLER_CUBE):
         if self.data is not None:
             log.debug("GPU: Active texture is %d" % self._texture_unit)
             gl.glActiveTexture(gl.GL_TEXTURE0 + self._texture_unit)
             self.data.activate()
Пример #5
0
    def _update(self):

        # Check active status (mandatory)
        if not self._active:
            raise RuntimeError("Uniform variable is not active")

        # WARNING : Uniform are supposed to keep their value between program
        #           activation/deactivation (from the GL documentation). It has
        #           been tested on some machines but if it is not the case on
        #           every machine, we can expect nasty bugs from this early
        #           return

        # Matrices (need a transpose argument)
        if self._gtype in (gl.GL_FLOAT_MAT2, gl.GL_FLOAT_MAT3,
                           gl.GL_FLOAT_MAT4):
            # OpenGL ES 2.0 does not support transpose
            transpose = False
            self._ufunction(self._handle, 1, transpose, self._data)

        # Textures (need to get texture count)
        elif self._gtype in (gl.GL_SAMPLER_1D, gl.GL_SAMPLER_2D,
                             gl.GL_SAMPLER_CUBE):
            # texture = self.data
            log.debug("GPU: Activactin texture %d" % self._texture_unit)
            # gl.glActiveTexture(gl.GL_TEXTURE0 + self._unit)
            # gl.glBindTexture(texture.target, texture.handle)
            gl.glUniform1i(self._handle, self._texture_unit)

        # Regular uniform
        else:
            self._ufunction(self._handle, 1, self._data)
Пример #6
0
    def _activate(self):
        """ Activate texture on GPU """

        log.debug("GPU: Activate texture")
        gl.glBindTexture(self.target, self._handle)
        if self._need_setup:
            self._setup()
Пример #7
0
    def _update(self):
        """ Update texture on GPU """

        if self.pending_data:
            log.debug("GPU: Updating texture")

            # offset, nbytes = self.pending_data

            # itemsize = self.strides[1]
            # offset /= itemsize
            # nbytes /= itemsize

            # nbytes += offset % self.width
            # offset -= offset % self.width
            # nbytes += (self.width - ((offset + nbytes) % self.width)) % self.width

            x = 0
            y = 0
            z = 0
            width = self.width
            height = self.height
            depth = self.depth
            gl.glBindTexture(self._target, self.handle)
            gl.glTexSubImage3D(self.target, 0, x, y, z, width, height, depth,
                               self._cpu_format, self.gtype, self)
            gl.glBindTexture(self._target, self.handle)

        self._pending_data = None
        self._need_update = False
Пример #8
0
    def _build_shaders(self, program):
        """ Build and attach shaders """

        # Check if we have at least something to attach
        if not self._verts:
            raise ValueError("No vertex shader has been given")
        if not self._frags:
            raise ValueError("No fragment shader has been given")

        log.debug("GPU: Attaching shaders to program")

        # Attach shaders
        attached = gl.glGetAttachedShaders(program)
        shaders = self._verts + self._frags + self._geoms
        for shader in shaders: #self._verts:
            if shader.need_update:
                if shader.handle in attached:
                    gl.glDetachShader(program, handle)
                shader.activate()
                if isinstance(shader, GeometryShader):
                    if shader.vertices_out is not None:
                        gl.glProgramParameteriEXT(self._handle,
                                                  gl.GL_GEOMETRY_VERTICES_OUT_EXT,
                                                  shader.vertices_out)
                    if shader.input_type is not None:
                        gl.glProgramParameteriEXT(self._handle,
                                                  gl.GL_GEOMETRY_INPUT_TYPE_EXT,
                                                  shader.input_type)
                    if shader.output_type is not None:
                        gl.glProgramParameteriEXT(self._handle,
                                                  gl.GL_GEOMETRY_OUTPUT_TYPE_EXT,
                                                  shader.output_type)
                gl.glAttachShader(program, shader.handle)
                shader._program = self
Пример #9
0
    def _update(self):

        # Check active status (mandatory)
        if not self._active:
            raise RuntimeError("Uniform variable is not active")

        # WARNING : Uniform are supposed to keep their value between program
        #           activation/deactivation (from the GL documentation). It has
        #           been tested on some machines but if it is not the case on
        #           every machine, we can expect nasty bugs from this early
        #           return

        # Matrices (need a transpose argument)
        if self._gtype in (gl.GL_FLOAT_MAT2, gl.GL_FLOAT_MAT3, gl.GL_FLOAT_MAT4):
            # OpenGL ES 2.0 does not support transpose
            transpose = False
            self._ufunction(self._handle, 1, transpose, self._data)

        # Textures (need to get texture count)
        elif self._gtype in (gl.GL_SAMPLER_1D, gl.GL_SAMPLER_2D, gl.GL_SAMPLER_CUBE):
            # texture = self.data
            log.debug("GPU: Activactin texture %d" % self._texture_unit)
            # gl.glActiveTexture(gl.GL_TEXTURE0 + self._unit)
            # gl.glBindTexture(texture.target, texture.handle)
            gl.glUniform1i(self._handle, self._texture_unit)

        # Regular uniform
        else:
            self._ufunction(self._handle, 1, self._data)
Пример #10
0
    def _update(self):
        """ Update texture on GPU """

        if self.pending_data:
            log.debug("GPU: Updating texture")

            start, stop = self.pending_data
            offset, nbytes = start, stop-start

            itemsize = self.strides[1]
            offset /= itemsize
            nbytes /= itemsize

            nbytes += offset % self.width
            offset -= offset % self.width
            nbytes += (self.width - ((offset + nbytes) % self.width)) % self.width

            x = 0
            y = offset // self.width
            width = self.width
            height = nbytes // self.width
            gl.glBindTexture(self._target, self.handle)
            gl.glTexSubImage2D(self.target, 0, x, y, width, height,
                               self._cpu_format, self.gtype, self)
            gl.glBindTexture(self._target, self.handle)

        self._pending_data = None
        self._need_update = False
Пример #11
0
    def _update(self):
        """ Update texture on GPU """

        if self.pending_data:
            log.debug("GPU: Updating texture")

            start, stop = self.pending_data
            offset, nbytes = start, stop - start

            itemsize = self.strides[1]
            offset /= itemsize
            nbytes /= itemsize

            nbytes += offset % self.width
            offset -= offset % self.width
            nbytes += (self.width -
                       ((offset + nbytes) % self.width)) % self.width

            x = 0
            y = offset // self.width
            width = self.width
            height = nbytes // self.width
            gl.glBindTexture(self._target, self.handle)
            gl.glTexSubImage2D(self.target, 0, x, y, width, height,
                               self._cpu_format, self.gtype, self)
            gl.glBindTexture(self._target, self.handle)

        self._pending_data = None
        self._need_update = False
Пример #12
0
    def _activate(self):
        """ Activate texture on GPU """

        log.debug("GPU: Activate texture")
        gl.glBindTexture(self.target, self._handle)
        if self._need_setup:
            self._setup()
Пример #13
0
    def _create(self):
        """ Create vertex array on GPU """

        self._handle = gl.glGenVertexArrays(1)
        log.debug("GPU: Creating vertex array (id=%d)" % self._id)
        self._deactivate()
        self._buffer._create()
Пример #14
0
    def _create(self):
        """ Create buffer on GPU """

        self._handle = gl.glGenBuffers(1)
        self._activate()
        log.debug("GPU: Creating buffer (id=%d)" % self._id)
        gl.glBufferData(self._target, self.nbytes, None, self._usage)
        self._deactivate()
Пример #15
0
    def _activate(self):
        """ Activate texture on GPU """

        log.debug("GPU: Activate texture cube")
        gl.glEnable(gl.GL_TEXTURE_CUBE_MAP)
        gl.glBindTexture(self.target, self._handle)
        if self._need_setup:
            self._setup()
Пример #16
0
    def _activate(self):
        """ Activate texture on GPU """

        log.debug("GPU: Activate texture cube")
        gl.glEnable(gl.GL_TEXTURE_CUBE_MAP)
        gl.glBindTexture(self.target, self._handle)
        if self._need_setup:
            self._setup()
Пример #17
0
    def _resize(self):
        """ Buffer resize on GPU """

        # WARNING: width/height should be checked against maximum size
        # maxsize = gl.glGetParameter(gl.GL_MAX_RENDERBUFFER_SIZE)
        log.debug("GPU: Resize render buffer")
        gl.glRenderbufferStorage(self._target, self._format, self._width,
                                 self._height)
Пример #18
0
    def _activate(self):
        """ Activate framebuffer on GPU """

        log.debug("GPU: Activate render framebuffer")
        gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, self._handle)
        if self._need_attach:
            self._attach()
            self._need_attach = False
Пример #19
0
    def _create(self):
        """ Create buffer on GPU """

        self._handle = gl.glGenBuffers(1)
        self._activate()
        log.debug("GPU: Creating buffer (id=%d)" % self._id)
        gl.glBufferData(self._target, self.nbytes, None, self._usage)
        self._deactivate()
Пример #20
0
    def _create(self):
        """ Create texture on GPU """

        Texture._create(self)
        log.debug("GPU: Resizing texture(%sx%s)"% (self.width,self.height))
        gl.glBindTexture(self.target, self._handle)
        gl.glTexImage2D(self.target, 0, self.format, self.width, self.height,
                        0, self.format, self.gtype, None)
Пример #21
0
    def _activate(self):
        """ Activate buffer on GPU """

        log.debug("GPU: Activate render buffer")
        gl.glBindRenderbuffer(gl.GL_RENDERBUFFER, self._handle)
        if self._need_resize:
            self._resize()
            self._need_resize = False
Пример #22
0
    def _resize(self):
        """ Buffer resize on GPU """

        # WARNING: width/height should be checked against maximum size
        # maxsize = gl.glGetParameter(gl.GL_MAX_RENDERBUFFER_SIZE)
        log.debug("GPU: Resize render buffer")
        gl.glRenderbufferStorage(self._target, self._format,
                                 self._width, self._height)
Пример #23
0
    def _activate(self):
        """ Activate buffer on GPU """

        log.debug("GPU: Activate render buffer")
        gl.glBindRenderbuffer(gl.GL_RENDERBUFFER, self._handle)
        if self._need_resize:
            self._resize()
            self._need_resize = False
Пример #24
0
    def _deactivate(self):
        """Deactivate the program."""

        gl.glUseProgram(0)

        for uniform in self._uniforms.values():
            uniform.deactivate()
        for attribute in self._attributes.values():
            attribute.deactivate()
        log.debug("GPU: Deactivating program (id=%d)" % self._id)
Пример #25
0
    def _deactivate(self):
        """Deactivate the program."""

        gl.glUseProgram(0)

        for uniform in self._uniforms.values():
            uniform.deactivate()
        for attribute in self._attributes.values():
            attribute.deactivate()
        log.debug("GPU: Deactivating program (id=%d)" % self._id)
Пример #26
0
    def _activate(self):
        """ Activate framebuffer on GPU """

        log.debug("GPU: Activate render framebuffer")
        gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, self._handle)

        if self._need_attach:
            self._attach()
            self._need_attach = False
        attachments = [gl.GL_COLOR_ATTACHMENT0+i for i in range(len(self.color))]
        gl.glDrawBuffers(np.array(attachments,dtype=np.uint32))
Пример #27
0
    def _update(self):

        log.debug("GPU: Updating texture")
        if self.pending_data:
            x,width = self.pending_data
            itemsize = self.strides[0]
            x /= itemsize
            width /= itemsize
            gl.glTexSubImage1D(self.target, 0, x, width, self.format, self.gtype, self)
        self._pending_data = None
        self._need_update = False
Пример #28
0
    def _deactivate(self):
        """Deactivate the program."""

        gl.glUseProgram(0)

        for uniform in self._uniforms.values():
            uniform.deactivate()

        # Need fix when dealing with vertex arrays (only need to active the array)
        for attribute in self._attributes.values():
            attribute.deactivate()
        log.debug("GPU: Deactivating program (id=%d)" % self._id)
Пример #29
0
    def _deactivate(self):
        """Deactivate the program."""

        gl.glUseProgram(0)

        for uniform in self._uniforms.values():
            uniform.deactivate()

        # Need fix when dealing with vertex arrays (only need to active the array)
        for attribute in self._attributes.values():
            attribute.deactivate()
        log.debug("GPU: Deactivating program (id=%d)" % self._id)
Пример #30
0
    def _activate(self):
        """Activate the program as part of current rendering state."""

        log.debug("GPU: Activating program (id=%d)" % self._id)
        gl.glUseProgram(self.handle)

        for uniform in self._uniforms.values():
            if uniform.active:
                uniform.activate()

        for attribute in self._attributes.values():
            if attribute.active:
                attribute.activate()
Пример #31
0
    def _activate(self):
        """Activate the program as part of current rendering state."""

        log.debug("GPU: Activating program (id=%d)" % self._id)
        gl.glUseProgram(self.handle)

        for uniform in self._uniforms.values():
            if uniform.active:
                uniform.activate()

        for attribute in self._attributes.values():
            if attribute.active:
                attribute.activate()
Пример #32
0
    def _activate(self):
        """ Activate framebuffer on GPU """

        log.debug("GPU: Activate render framebuffer")
        gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, self._handle)

        if self._need_attach:
            self._attach()
            self._need_attach = False
        attachments = [
            gl.GL_COLOR_ATTACHMENT0 + i for i in range(len(self.color))
        ]
        gl.glDrawBuffers(np.array(attachments, dtype=np.uint32))
Пример #33
0
    def _activate(self):
        """Activate the program as part of current rendering state."""

        log.debug("GPU: Activating program (id=%d)" % self._id)
        gl.glUseProgram(self.handle)

        for uniform in self._uniforms.values():
            if uniform.active:
                uniform.activate()

        # Need fix when dealing with vertex arrays (only need to active the array)
        for attribute in self._attributes.values():
            if attribute.active:
                attribute.activate()
Пример #34
0
    def _activate(self):
        """Activate the program as part of current rendering state."""

        log.debug("GPU: Activating program (id=%d)" % self._id)
        gl.glUseProgram(self.handle)

        for uniform in self._uniforms.values():
            if uniform.active:
                uniform.activate()

        # Need fix when dealing with vertex arrays (only need to active the array)
        for attribute in self._attributes.values():
            if attribute.active:
                attribute.activate()
Пример #35
0
    def _create(self):
        """ Create the shader """

        log.debug("GPU: Creating shader")

        # Check if we have something to compile
        if not self.code:
            raise RuntimeError("No code has been given")

        # Check that shader object has been created
        if self._handle <= 0:
            self._handle = gl.glCreateShader(self._target)
            if self._handle <= 0:
                raise RuntimeError("Cannot create shader object")
Пример #36
0
    def _create(self):
        """ Create the shader """

        log.debug("GPU: Creating shader")

        # Check if we have something to compile
        if not self.code:
            raise RuntimeError("No code has been given")

        # Check that shader object has been created
        if self._handle <= 0:
            self._handle = gl.glCreateShader(self._target)
            if self._handle <= 0:
                raise RuntimeError("Cannot create shader object")
Пример #37
0
    def _update(self):

        log.debug("GPU: Updating texture")
        if self.pending_data:
            start, stop = self.pending_data
            offset, nbytes = start, stop-start
            itemsize = self.strides[0]
            x = offset / itemsize
            width = nbytes/itemsize
            gl.glTexSubImage1D(self.target, 0, x, width, self._cpu_format, self.gtype, self)

            # x,width = self.pending_data
            # itemsize = self.strides[0]
            # x /= itemsize
            # width /= itemsize
            # gl.glTexSubImage1D(self.target, 0, x, width, self._cpu_format, self.gtype, self)
        self._pending_data = None
        self._need_update = False
Пример #38
0
    def _update(self):

        log.debug("GPU: Updating texture")
        if self.pending_data:
            start, stop = self.pending_data
            offset, nbytes = start, stop-start
            itemsize = self.strides[0]
            x = offset // itemsize
            width = nbytes//itemsize
            gl.glTexSubImage1D(self.target, 0, x, width, self._cpu_format, self.gtype, self)

            # x,width = self.pending_data
            # itemsize = self.strides[0]
            # x /= itemsize
            # width /= itemsize
            # gl.glTexSubImage1D(self.target, 0, x, width, self._cpu_format, self.gtype, self)
        self._pending_data = None
        self._need_update = False
Пример #39
0
    def _attach(self):
        """ Attach render buffers to framebuffer """

        log.debug("GPU: Attach render buffers")
        while self._pending_attachments:
            attachment, buffer = self._pending_attachments.pop(0)
            if buffer is None:
                gl.glFramebufferRenderbuffer(gl.GL_FRAMEBUFFER, attachment,
                                             gl.GL_RENDERBUFFER, 0)
            elif isinstance(buffer, RenderBuffer):
                buffer.activate()
                gl.glFramebufferRenderbuffer(gl.GL_FRAMEBUFFER, attachment,
                                             gl.GL_RENDERBUFFER, buffer.handle)
                buffer.deactivate()
            elif isinstance(buffer, Texture2D):
                buffer.activate()
                # INFO: 0 is for mipmap level 0 (default) of the texture
                gl.glFramebufferTexture2D(gl.GL_FRAMEBUFFER, attachment,
                                          buffer.target, buffer.handle, 0)
                buffer.deactivate()
            else:
                raise ValueError("Invalid attachment")


        res = gl.glCheckFramebufferStatus(gl.GL_FRAMEBUFFER)
        if res == gl.GL_FRAMEBUFFER_COMPLETE:
            pass
        elif res == 0:
            raise RuntimeError('Target not equal to GL_FRAMEBUFFER')
        elif res == gl.GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
            raise RuntimeError(
                'FrameBuffer attachments are incomplete.')
        elif res == gl.GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
            raise RuntimeError(
                'No valid attachments in the FrameBuffer.')
        elif res == gl.GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS:
            raise RuntimeError(
                'attachments do not have the same width and height.')
        elif res == gl.GL_FRAMEBUFFER_INCOMPLETE_FORMATS:
            raise RuntimeError('Internal format of attachment '
                               'is not renderable.')
        elif res == gl.GL_FRAMEBUFFER_UNSUPPORTED:
            raise RuntimeError('Combination of internal formats used '
                               'by attachments is not supported.')
Пример #40
0
    def _update(self):
        """ Compile the source and checks everything's ok """

        log.debug("GPU: Compiling shader")

        if len(self.hooks):
            raise RuntimeError("Shader has pending hooks, cannot compile")

        # Set shader source
        gl.glShaderSource(self._handle, self.code)

        # Actual compilation
        gl.glCompileShader(self._handle)
        status = gl.glGetShaderiv(self._handle, gl.GL_COMPILE_STATUS)
        if not status:
            error = gl.glGetShaderInfoLog(self._handle)
            lineno, mesg = self._parse_error(error)
            self._print_error(mesg, lineno-1)
            raise RuntimeError("Shader compilation error")
Пример #41
0
    def _update(self):
        log.debug("GPU: Updating texture cube")

        if self.need_update:
            gl.glEnable(gl.GL_TEXTURE_CUBE_MAP)
            gl.glBindTexture(self.target, self.handle)

            targets = [
                gl.GL_TEXTURE_CUBE_MAP_POSITIVE_X,
                gl.GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
                gl.GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
                gl.GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
                gl.GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
                gl.GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
            ]

            for i, target in enumerate(targets):
                face = self[i]
                pending = self.pending_data
                extents = face._extents
                if pending is None: continue
                if pending[1] < extents[0]: continue
                if pending[0] > extents[1]: continue
                start = max(extents[0], pending[0]) - extents[0]
                stop = min(extents[1], pending[1]) - extents[0]
                offset, nbytes = start, stop - start
                itemsize = face.strides[1]
                offset /= itemsize
                nbytes /= itemsize
                nbytes += offset % self.width
                offset -= offset % self.width
                nbytes += (self.width -
                           ((offset + nbytes) % self.width)) % self.width
                x = 0
                y = offset // self.width
                width = self.width
                height = nbytes // self.width
                gl.glTexSubImage2D(target, 0, x, y, width, height,
                                   self._cpu_format, self.gtype, face)

        self._pending_data = None
        self._need_update = False
Пример #42
0
    def _attach(self):
        """ Attach render buffers to framebuffer """

        log.debug("GPU: Attach render buffers")
        while self._pending_attachments:
            attachment, buffer = self._pending_attachments.pop(0)
            if buffer is None:
                gl.glFramebufferRenderbuffer(gl.GL_FRAMEBUFFER, attachment,
                                             gl.GL_RENDERBUFFER, 0)
            elif isinstance(buffer, RenderBuffer):
                buffer.activate()
                gl.glFramebufferRenderbuffer(gl.GL_FRAMEBUFFER, attachment,
                                             gl.GL_RENDERBUFFER, buffer.handle)
                buffer.deactivate()
            elif isinstance(buffer, Texture2D):
                buffer.activate()
                # INFO: 0 is for mipmap level 0 (default) of the texture
                gl.glFramebufferTexture2D(gl.GL_FRAMEBUFFER, attachment,
                                          buffer.target, buffer.handle, 0)
                buffer.deactivate()
            else:
                raise ValueError("Invalid attachment")

        res = gl.glCheckFramebufferStatus(gl.GL_FRAMEBUFFER)
        if res == gl.GL_FRAMEBUFFER_COMPLETE:
            pass
        elif res == 0:
            raise RuntimeError('Target not equal to GL_FRAMEBUFFER')
        elif res == gl.GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
            raise RuntimeError('FrameBuffer attachments are incomplete.')
        elif res == gl.GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
            raise RuntimeError('No valid attachments in the FrameBuffer.')
        elif res == gl.GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS:
            raise RuntimeError(
                'attachments do not have the same width and height.')
        elif res == gl.GL_FRAMEBUFFER_INCOMPLETE_FORMATS:
            raise RuntimeError('Internal format of attachment '
                               'is not renderable.')
        elif res == gl.GL_FRAMEBUFFER_UNSUPPORTED:
            raise RuntimeError('Combination of internal formats used '
                               'by attachments is not supported.')
Пример #43
0
    def _create(self):
        """
        Build (link) the program and checks everything's ok.

        A GL context must be available to be able to build (link)
        """

        log.debug("GPU: Creating program")

        # Check if program has been created
        if self._handle <= 0:
            self._handle = gl.glCreateProgram()
            if not self._handle:
                raise ValueError("Cannot create program object")

        self._build_shaders(self._handle)

        log.debug("GPU: Linking program")

        # Link the program
        gl.glLinkProgram(self._handle)
        if not gl.glGetProgramiv(self._handle, gl.GL_LINK_STATUS):
            print(gl.glGetProgramInfoLog(self._handle))
            raise ValueError('Linking error')

        # Activate uniforms
        active_uniforms = [name for (name,gtype) in self.active_uniforms]
        for uniform in self._uniforms.values():
            if uniform.name in active_uniforms:
                uniform.active = True
            else:
                uniform.active = False

        # Activate attributes
        active_attributes = [name for (name,gtype) in self.active_attributes]
        for attribute in self._attributes.values():
            if attribute.name in active_attributes:
                attribute.active = True
            else:
                attribute.active = False
Пример #44
0
    def _create(self):
        """
        Build (link) the program and checks everything's ok.

        A GL context must be available to be able to build (link)
        """

        log.debug("GPU: Creating program")

        # Check if program has been created
        if self._handle <= 0:
            self._handle = gl.glCreateProgram()
            if not self._handle:
                raise ValueError("Cannot create program object")

        self._build_shaders(self._handle)

        log.debug("GPU: Linking program")

        # Link the program
        gl.glLinkProgram(self._handle)
        if not gl.glGetProgramiv(self._handle, gl.GL_LINK_STATUS):
            print(gl.glGetProgramInfoLog(self._handle))
            raise ValueError('Linking error')

        # Activate uniforms
        active_uniforms = [name for (name, gtype) in self.active_uniforms]
        for uniform in self._uniforms.values():
            if uniform.name in active_uniforms:
                uniform.active = True
            else:
                uniform.active = False

        # Activate attributes
        active_attributes = [name for (name, gtype) in self.active_attributes]
        for attribute in self._attributes.values():
            if attribute.name in active_attributes:
                attribute.active = True
            else:
                attribute.active = False
Пример #45
0
    def _update(self):
        log.debug("GPU: Updating texture cube")

        if self.need_update:
            gl.glEnable(gl.GL_TEXTURE_CUBE_MAP)
            gl.glBindTexture(self.target, self.handle)

            targets = [ gl.GL_TEXTURE_CUBE_MAP_POSITIVE_X,
                        gl.GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
                        gl.GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
                        gl.GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
                        gl.GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
                        gl.GL_TEXTURE_CUBE_MAP_NEGATIVE_Z ]

            for i,target in enumerate(targets):
                face = self[i]
                pending = self.pending_data
                extents = face._extents
                if pending is None:         continue
                if pending[1] < extents[0]: continue
                if pending[0] > extents[1]: continue
                start = max(extents[0], pending[0]) - extents[0]
                stop = min(extents[1], pending[1]) - extents[0]
                offset,nbytes = start, stop-start
                itemsize = face.strides[1]
                offset /= itemsize
                nbytes /= itemsize
                nbytes += offset % self.width
                offset -= offset % self.width
                nbytes += (self.width - ((offset + nbytes) % self.width)) % self.width
                x = 0
                y = offset // self.width
                width = self.width
                height = nbytes // self.width
                gl.glTexSubImage2D(target, 0, x, y, width, height,
                                   self._cpu_format, self.gtype, face)

        self._pending_data = None
        self._need_update = False
Пример #46
0
    def _update(self):
        """ Compile the source and checks everything's ok """

        log.debug("GPU: Compiling shader")

        if len(self.hooks):
            hooks = [name for name,snippet in self.hooks]
            error = "Shader has pending hooks (%s), cannot compile" % hooks
            raise RuntimeError(error)

        # Set shader version
        code = "#version 120\n" + self.code
        gl.glShaderSource(self._handle, code)

        # Actual compilation
        gl.glCompileShader(self._handle)
        status = gl.glGetShaderiv(self._handle, gl.GL_COMPILE_STATUS)
        if not status:
            error = gl.glGetShaderInfoLog(self._handle).decode()
            lineno, mesg = self._parse_error(error)
            self._print_error(mesg, lineno-1)
            raise RuntimeError("Shader compilation error")
Пример #47
0
    def _update(self):
        """ Compile the source and checks everything's ok """

        log.debug("GPU: Compiling shader")

        if len(self.hooks):
            hooks = [name for name, snippet in self.hooks]
            error = "Shader has pending hooks (%s), cannot compile" % hooks
            raise RuntimeError(error)

        # Set shader version
        code = "#version 120\n" + self.code
        gl.glShaderSource(self._handle, code)

        # Actual compilation
        gl.glCompileShader(self._handle)
        status = gl.glGetShaderiv(self._handle, gl.GL_COMPILE_STATUS)
        if not status:
            error = gl.glGetShaderInfoLog(self._handle).decode()
            lineno, mesg = self._parse_error(error)
            self._print_error(mesg, lineno - 1)
            raise RuntimeError("Shader compilation error")
Пример #48
0
    def _build_shaders(self, program):
        """ Build and attach shaders """

        # Check if we have at least something to attach
        if not self._vertex:
            raise ValueError("No vertex shader has been given")
        if not self._fragment:
            raise ValueError("No fragment shader has been given")

        log.debug("GPU: Attaching shaders to program")

        # Attach shaders
        attached = gl.glGetAttachedShaders(program)
        shaders = [self._vertex, self._fragment]
        if self._geometry is not None:
            shaders.append(self._geometry)

        for shader in shaders:
            if shader.need_update:
                if shader.handle in attached:
                    gl.glDetachShader(program, shader.handle)
                shader.activate()
                if isinstance(shader, GeometryShader):
                    if shader.vertices_out is not None:
                        gl.glProgramParameteriEXT(
                            self._handle, gl.GL_GEOMETRY_VERTICES_OUT_EXT,
                            shader.vertices_out)
                    if shader.input_type is not None:
                        gl.glProgramParameteriEXT(
                            self._handle, gl.GL_GEOMETRY_INPUT_TYPE_EXT,
                            shader.input_type)
                    if shader.output_type is not None:
                        gl.glProgramParameteriEXT(
                            self._handle, gl.GL_GEOMETRY_OUTPUT_TYPE_EXT,
                            shader.output_type)
                gl.glAttachShader(program, shader.handle)
                shader._program = self
Пример #49
0
    def _delete(self):
        """ Delete texture from GPU """

        log.debug("GPU: Deleting texture")
        if self.handle > -1:
            gl.glDeleteTextures([self.handle])
Пример #50
0
    def _delete(self):
        """ Delete texture from GPU """

        log.debug("GPU: Deleting texture")
        if self.handle > -1:
            gl.glDeleteTextures(np.array([self.handle], dtype=np.uint32))
Пример #51
0
    def _delete(self):
        """ Delete texture from GPU """

        log.debug("GPU: Deleting texture")
        if self.handle > -1:
            gl.glDeleteTextures(np.array([self.handle], dtype=np.uint32))
Пример #52
0
    def _create(self):
        """ Create texture on GPU """

        log.debug("GPU: Creating texture")
        self._handle = gl.glGenTextures(1)
Пример #53
0
    def _activate(self):
        """ Bind the buffer to some target """

        log.debug("GPU: Activating buffer (id=%d)" % self._id)
        gl.glBindBuffer(self._target, self._handle)
Пример #54
0
    def _deactivate(self):
        """ Deactivate buffer on GPU """

        log.debug("GPU: Deactivate render buffer")
        gl.glBindRenderbuffer(gl.GL_RENDERBUFFER, 0)
Пример #55
0
    def _deactivate(self):
        """ Deactivate texture on GPU """

        log.debug("GPU: Deactivate texture cube")
        gl.glBindTexture(self._target, 0)
        gl.glDisable(gl.GL_TEXTURE_CUBE_MAP)
Пример #56
0
    def _deactivate(self):
        """ Unbind the current bound buffer """

        log.debug("GPU: Deactivating buffer (id=%d)" % self._id)
        gl.glBindBuffer(self._target, 0)
Пример #57
0
    def _create(self):
        """ Create texture on GPU """

        log.debug("GPU: Creating texture")
        self._handle = gl.glGenTextures(1)
Пример #58
0
    def _deactivate(self):
        """ Deactivate texture on GPU """

        log.debug("GPU: Deactivate texture cube")
        gl.glBindTexture(self._target, 0)
        gl.glDisable(gl.GL_TEXTURE_CUBE_MAP)
Пример #59
0
    def _deactivate(self):
        """ Deactivate texture on GPU """

        log.debug("GPU: Deactivate texture")
        gl.glBindTexture(self._target, 0)