From 48bc3a05916f24389ea06144d52858dde7ff3433 Mon Sep 17 00:00:00 2001 From: Mathieu Virbel Date: Thu, 13 Jan 2011 11:24:46 +0100 Subject: [PATCH 1/4] texture: correcly allocate the memory needed for initial texture data --- kivy/graphics/texture.pyx | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/kivy/graphics/texture.pyx b/kivy/graphics/texture.pyx index 71181c412..d91233aac 100644 --- a/kivy/graphics/texture.pyx +++ b/kivy/graphics/texture.pyx @@ -94,6 +94,23 @@ cdef inline int _buffer_type_to_gl_format(str x): except KeyError: raise Exception('Unknown <%s> format' % x) +cdef dict _gl_buffer_size = { + 'ubyte': sizeof(GLubyte), + 'ushort': sizeof(GLushort), + 'uint': sizeof(GLuint), + 'byte': sizeof(GLbyte), + 'short': sizeof(GLshort), + 'int': sizeof(GLint), + 'float': sizeof(GLfloat) +} + +cdef inline int _buffer_type_to_gl_size(str x): + x = x.lower() + try: + return _gl_buffer_size[x] + except KeyError: + raise Exception('Unknown <%s> format' % x) + cdef inline int _gl_format_size(GLuint x): if x in (GL_RGB, GL_BGR): return 3 @@ -220,7 +237,8 @@ cdef _texture_create(int width, int height, str fmt, str buffertype, int # ok, allocate memory for initial texture cdef int glfmt = _fmt_to_gl_format(fmt) - cdef int datasize = sizeof(GLubyte) * texture_width * texture_height * _gl_format_size(glfmt) + cdef int datasize = texture_width * texture_height * \ + _gl_format_size(glfmt) * _buffer_type_to_gl_size(buffertype) cdef void *data = NULL cdef int dataerr = 0 From 97ea6f4d49dd4b16b59f6d596417918d5331aa78 Mon Sep 17 00:00:00 2001 From: Mathieu Virbel Date: Thu, 13 Jan 2011 11:25:09 +0100 Subject: [PATCH 2/4] context: use the cimport Texture --- kivy/graphics/context_instructions.pxd | 3 ++- kivy/graphics/context_instructions.pyx | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/kivy/graphics/context_instructions.pxd b/kivy/graphics/context_instructions.pxd index 63f2fcf64..98e5a1c6b 100644 --- a/kivy/graphics/context_instructions.pxd +++ b/kivy/graphics/context_instructions.pxd @@ -4,6 +4,7 @@ cdef class BindTexture from transformation cimport Matrix from instructions cimport ContextInstruction +from texture cimport Texture cdef class LineWidth(ContextInstruction): cdef apply(self) @@ -13,7 +14,7 @@ cdef class Color(ContextInstruction): cdef class BindTexture(ContextInstruction): cdef str _source - cdef object _texture + cdef Texture _texture cdef apply(self) diff --git a/kivy/graphics/context_instructions.pyx b/kivy/graphics/context_instructions.pyx index 9045a16de..046d797bb 100644 --- a/kivy/graphics/context_instructions.pyx +++ b/kivy/graphics/context_instructions.pyx @@ -113,12 +113,12 @@ cdef class BindTexture(ContextInstruction): 'texture property') self.source = kwargs.get('source', None) - if self.source == None: + if self.source is None: self.texture = kwargs.get('texture', None) cdef apply(self): glActiveTexture(GL_TEXTURE0) - glBindTexture(self._texture.target, self._texture.id) + glBindTexture(self._texture.target, self._texture._id) property texture: def __get__(self): From 1ad67a3c4f2858e12e8a6649647bb31e4e17ce2e Mon Sep 17 00:00:00 2001 From: Mathieu Virbel Date: Thu, 13 Jan 2011 11:25:45 +0100 Subject: [PATCH 3/4] fbo: allow the user to call bind/release --- kivy/graphics/fbo.pxd | 4 ++-- kivy/graphics/fbo.pyx | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/kivy/graphics/fbo.pxd b/kivy/graphics/fbo.pxd index 4a6f55c09..63e939105 100644 --- a/kivy/graphics/fbo.pxd +++ b/kivy/graphics/fbo.pxd @@ -15,9 +15,9 @@ cdef class Fbo(RenderContext): cdef int _is_bound cpdef clear_buffer(self) + cpdef bind(self) + cpdef release(self) - cdef bind(self) - cdef release(self) cdef create_fbo(self) cdef delete_fbo(self) cdef apply(self) diff --git a/kivy/graphics/fbo.pyx b/kivy/graphics/fbo.pyx index 744985b9a..dd5e9eb05 100644 --- a/kivy/graphics/fbo.pyx +++ b/kivy/graphics/fbo.pyx @@ -180,7 +180,7 @@ cdef class Fbo(RenderContext): projection_mat.view_clip(0.0, self._width, 0.0, self._height, -1.0, 1.0, 0) self.set_state('projection_mat', projection_mat) - cdef bind(self): + cpdef bind(self): if self._is_bound: raise FboException('FBO is already binded.') else: @@ -195,7 +195,7 @@ cdef class Fbo(RenderContext): glGetIntegerv(GL_VIEWPORT, &self._viewport) glViewport(0, 0, self._width, self._height) - cdef release(self): + cpdef release(self): if self._is_bound == 0: raise FboException('Cannot release a FBO not binded.') else: From 5fa5e02d88909698a0ad9b8c734c6a25e8dae59e Mon Sep 17 00:00:00 2001 From: Mathieu Virbel Date: Thu, 13 Jan 2011 11:26:07 +0100 Subject: [PATCH 4/4] fbo: don't clear the fbo is a texture is provided --- kivy/graphics/fbo.pyx | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/kivy/graphics/fbo.pyx b/kivy/graphics/fbo.pyx index dd5e9eb05..a2af0dd4f 100644 --- a/kivy/graphics/fbo.pyx +++ b/kivy/graphics/fbo.pyx @@ -140,10 +140,12 @@ cdef class Fbo(RenderContext): cdef create_fbo(self): cdef GLuint f_id cdef int status + cdef int do_clear = 0 # create texture if self._texture is None: self._texture = Texture.create(size=(self._width, self._height)) + do_clear = 1 # create framebuffer glGenFramebuffers(1, &f_id) @@ -171,7 +173,8 @@ cdef class Fbo(RenderContext): raise FboException('FBO Initialization failed', status) # clear the fbo - self.clear_buffer() + if do_clear: + self.clear_buffer() # unbind the framebuffer glBindFramebuffer(GL_FRAMEBUFFER, 0)