Skip to content

Commit 62878c2

Browse files
author
gabriel pettier
committed
progress, not crashing anymore, just a black screen
1 parent 1b908a7 commit 62878c2

File tree

8 files changed

+132
-6
lines changed

8 files changed

+132
-6
lines changed

kivy/graphics/c_opengl.pxd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ cdef extern from "gl_redirect.h":
367367
int GL_MAX_RENDERBUFFER_SIZE
368368

369369
int GL_INVALID_FRAMEBUFFER_OPERATION
370+
int GL_PIXEL_UNPACK_BUFFER
370371

371372
IF USE_OPENGL_ES2:
372373
int GL_FIXED
@@ -528,3 +529,5 @@ cdef extern from "gl_redirect.h":
528529
cdef void glVertexAttrib4fv(GLuint indx, GLfloat* values) nogil
529530
cdef void glVertexAttribPointer(GLuint indx, GLint size, GLenum type, GLboolean normalized, GLsizei stride, GLvoid* ptr) nogil
530531
cdef void glViewport(GLint x, GLint y, GLsizei width, GLsizei height) nogil
532+
cdef void *glMapBuffer(GLenum target, GLbitfield access) nogil
533+
cdef GLboolean glUnmapBuffer(GLenum target) nogil

kivy/graphics/gl_redirect.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
# define GL_SHADER_BINARY_FORMATS 0x8DF8
2424
# define GL_RGB565 0x8D62
2525
# define GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS 0x8CD9
26+
# define GL_PIXEL_UNPACK_BUFFER 0x88EC
2627
#else
2728
# if __USE_OPENGL_ES2
2829
# if __APPLE__

kivy/graphics/opengl.pyx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ GL_FRAMEBUFFER_BINDING = c_opengl.GL_FRAMEBUFFER_BINDING
338338
GL_RENDERBUFFER_BINDING = c_opengl.GL_RENDERBUFFER_BINDING
339339
GL_MAX_RENDERBUFFER_SIZE = c_opengl.GL_MAX_RENDERBUFFER_SIZE
340340
GL_INVALID_FRAMEBUFFER_OPERATION = c_opengl.GL_INVALID_FRAMEBUFFER_OPERATION
341+
GL_PIXEL_UNPACK_BUFFER = c_opengl.GL_PIXEL_UNPACK_BUFFER
341342

342343
# not working with GL standard include
343344
GL_SHADER_BINARY_FORMATS = c_opengl.GL_SHADER_BINARY_FORMATS
@@ -777,6 +778,15 @@ def glGenBuffers(GLsizei n):
777778
c_opengl.glGenBuffers(n, d)
778779
return _genEnd(n, d)
779780

781+
782+
#def glMapBuffer(GLenum target, GLbitfield access):
783+
# return c_opengl.glMapBuffer(target, access)
784+
785+
786+
#def glUnmapBuffer(GLenum target):
787+
# return c_opengl.glUnmapBuffer(target)
788+
789+
780790
def glGenerateMipmap(GLenum target):
781791
'''See: `glGenerateMipmap() on Kronos website
782792
<http://www.khronos.org/opengles/sdk/docs/man/xhtml/glGenerateMipmap.xml>`_

kivy/graphics/pbo.pxd

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from c_opengl cimport GLuint
2+
3+
cdef class PBO:
4+
cdef object __weakref__
5+
cdef GLuint _id
6+
cdef int width
7+
cdef int height
8+
cdef int data_size
9+
10+
cdef void release(self)
11+
cpdef GLuint get_id(self)

kivy/graphics/pbo.pyx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
include "config.pxi"
2+
include "common.pxi"
3+
include "gl_debug_logger.pxi"
4+
5+
from kivy.graphics.c_opengl cimport *
6+
IF USE_OPENGL_DEBUG == 1:
7+
from kivy.graphics.c_opengl_debug cimport *
8+
9+
class PBOException(Exception):
10+
pass
11+
12+
13+
# cdef pbo_update(int _id, int data_size, char *target) nogil:
14+
# cdef char *ptr
15+
# glBindBuffer(GL_PIXEL_UNPACK_BUFFER, _id)
16+
# ptr = glMapBuffers(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY)
17+
# if ptr:
18+
# memcpy(ptr, target, data_size * pixel_size)
19+
# glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER)
20+
# else:
21+
# raise PBOException('unable to map PBO buffer')
22+
# glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0)
23+
#
24+
25+
cdef class PBO:
26+
def __init__(self, width, height):
27+
cdef int data_size = width * height
28+
self.width = width
29+
self.height = height
30+
glGenBuffers(1, &self._id)
31+
32+
if self._id <= 0:
33+
raise PBOException('invalid glGenBuffers result')
34+
35+
36+
cdef void release(self):
37+
glDeleteBuffers(1, &self._id)
38+
39+
cpdef GLuint get_id(self):
40+
#print "get_id called", self._id
41+
return self._id

kivy/graphics/texture.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ cdef class Texture:
2727
cdef list observers
2828
cdef object _proxyimage
2929
cdef object _callback
30+
cdef object _pbo
3031

3132
cdef void update_tex_coords(self)
3233
cdef void set_min_filter(self, x)

kivy/graphics/texture.pyx

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,15 @@ include "opengl_utils_def.pxi"
225225
include "img_tools.pxi"
226226
include "gl_debug_logger.pxi"
227227

228+
#from libc.stdio cimport puts, printf
228229
cimport cython
229230
from os import environ
230231
from kivy.utils import platform
231232
from kivy.weakmethod import WeakMethod
232233
from kivy.graphics.context cimport get_context
233234

235+
from kivy.graphics.pbo import PBO
236+
234237
from kivy.graphics.c_opengl cimport *
235238
IF USE_OPENGL_DEBUG == 1:
236239
from kivy.graphics.c_opengl_debug cimport *
@@ -277,6 +280,9 @@ DEF GL_UNPACK_ROW_LENGTH = 0x0CF2
277280
DEF GL_UNPACK_SKIP_ROWS = 0x0CF3
278281
DEF GL_UNPACK_SKIP_PIXELS = 0x0CF4
279282

283+
DEF GL_PIXEL_UNPACK_BUFFER = 0x88EC
284+
DEF GL_WRITE_ONLY = 0x88B9
285+
280286
cdef dict _gl_color_fmt = {
281287
'rgba': GL_RGBA, 'bgra': GL_BGRA, 'rgb': GL_RGB, 'bgr': GL_BGR,
282288
'luminance': GL_LUMINANCE, 'luminance_alpha': GL_LUMINANCE_ALPHA,
@@ -638,6 +644,7 @@ cdef class Texture:
638644
self._source = source
639645
self._nofree = 0
640646
self._callback = callback
647+
self._pbo = None
641648

642649
if texid == 0:
643650
self.flags |= TI_NEED_GEN
@@ -647,6 +654,14 @@ cdef class Texture:
647654
self.update_tex_coords()
648655
get_context().register_texture(self)
649656

657+
658+
if (
659+
gl_has_extension('ARB_pixel_buffer_object') or
660+
gl_has_extension('EXT_pixel_buffer_object')
661+
):
662+
# gl_has_extension('GL_OES_mapbuffer')
663+
self._pbo = pbo = PBO(width, height)
664+
650665
def __dealloc__(self):
651666
get_context().dealloc_texture(self)
652667

@@ -971,6 +986,11 @@ cdef class Texture:
971986
cdef int i
972987
cdef int require_subimage = 0
973988

989+
cdef GLuint pbo = 0
990+
cdef void *ptr
991+
if self._pbo:
992+
pbo = self._pbo.get_id()
993+
974994
# if the hardware doesn't support native unpack, use alternative method.
975995
if need_unpack and not gl_has_capability(GLCAP_UNPACK_SUBIMAGE):
976996
require_subimage = 1
@@ -1006,12 +1026,49 @@ cdef class Texture:
10061026
glCompressedTexImage2D(target, _mipmap_level, glfmt, w, h, 0,
10071027
<GLsizei>datasize, cdata)
10081028
elif is_allocated:
1009-
glTexSubImage2D(target, _mipmap_level, x, y, w, h, glfmt,
1010-
glbufferfmt, cdata)
1029+
if pbo:
1030+
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo)
1031+
ptr = glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY)
1032+
if ptr:
1033+
memcpy(ptr, cdata, datasize)
1034+
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER)
1035+
else:
1036+
pass
1037+
#raise PBOException('unable to map PBO buffer')
1038+
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0)
1039+
#pbo_update(pbo, datasize, cdata) XXX
1040+
1041+
else:
1042+
glTexSubImage2D(target, _mipmap_level, x, y, w, h, glfmt,
1043+
glbufferfmt, cdata)
10111044
else:
1012-
glTexImage2D(target, _mipmap_level, iglfmt, w, h, 0, glfmt,
1013-
glbufferfmt, cdata)
1014-
1045+
if pbo:
1046+
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo)
1047+
#print glIsBuffer(pbo) == GL_TRUE
1048+
1049+
# XXX STREAM may not be the best choice, for example for videos,
1050+
# DYNAMIC may be better, allow to pick the PBO type?
1051+
glBufferData(GL_PIXEL_UNPACK_BUFFER, datasize, <GLvoid*> 0, GL_STREAM_DRAW)
1052+
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0)
1053+
1054+
#puts("bind buffer")
1055+
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo)
1056+
#puts("map buffer")
1057+
ptr = glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY)
1058+
if ptr:
1059+
#puts("memcpy")
1060+
memcpy(ptr, cdata, datasize)
1061+
#puts("unmap")
1062+
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER)
1063+
else:
1064+
pass
1065+
# raise PBOException('unable to map PBO buffer')
1066+
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0)
1067+
# pbo_update(pbo, datasize, cdata) XXX
1068+
else:
1069+
glTexImage2D(target, _mipmap_level, iglfmt, w, h, 0, glfmt,
1070+
glbufferfmt, cdata)
1071+
10151072
if _mipmap_generation:
10161073
glGenerateMipmap(target)
10171074

setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,7 @@ def determine_sdl2():
613613
'fbo.pyx': [
614614
'config.pxi', 'opcodes.pxi', 'transformation.pxd', 'context.pxd',
615615
'c_opengl_debug.pxd'],
616+
'pbo.pyx': ['c_opengl.pxd'],
616617
'gl_instructions.pyx': [
617618
'config.pxi', 'opcodes.pxi', 'c_opengl.pxd', 'c_opengl_debug.pxd',
618619
'instructions.pxd'],
@@ -640,7 +641,7 @@ def determine_sdl2():
640641
'texture.pyx': [
641642
'config.pxi', 'common.pxi', 'opengl_utils_def.pxi', 'context.pxd',
642643
'c_opengl.pxd', 'c_opengl_debug.pxd', 'opengl_utils.pxd',
643-
'img_tools.pxi'],
644+
'img_tools.pxi', 'pbo.pyx'],
644645
'vbo.pxd': ['buffer.pxd', 'c_opengl.pxd', 'vertex.pxd'],
645646
'vbo.pyx': [
646647
'config.pxi', 'common.pxi', 'c_opengl_debug.pxd', 'context.pxd',
@@ -674,6 +675,7 @@ def determine_sdl2():
674675
'graphics/texture.pyx': merge(base_flags, gl_flags),
675676
'graphics/transformation.pyx': merge(base_flags, gl_flags),
676677
'graphics/vbo.pyx': merge(base_flags, gl_flags),
678+
'graphics/pbo.pyx': merge(base_flags, gl_flags),
677679
'graphics/vertex.pyx': merge(base_flags, gl_flags),
678680
'graphics/vertex_instructions.pyx': merge(base_flags, gl_flags),
679681
'core/text/text_layout.pyx': base_flags,

0 commit comments

Comments
 (0)