comparison pytouhou/ui/opengl/framebuffer.pyx @ 610:1b31169dc344

Move the passthrough shader to the Framebuffer class, since it isn’t used in the use_framebuffer_blit path.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 21 Dec 2014 18:52:18 +0100
parents 23b9418e4b2f
children a6af3ff86612
comparison
equal deleted inserted replaced
609:23b9418e4b2f 610:1b31169dc344
9 ## This program is distributed in the hope that it will be useful, 9 ## This program is distributed in the hope that it will be useful,
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of 10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details. 12 ## GNU General Public License for more details.
13 ## 13 ##
14
15 from libc.stdlib cimport free
14 16
15 from pytouhou.lib.opengl cimport \ 17 from pytouhou.lib.opengl cimport \
16 (glPushDebugGroup, glPopDebugGroup, GL_DEBUG_SOURCE_APPLICATION, 18 (glPushDebugGroup, glPopDebugGroup, GL_DEBUG_SOURCE_APPLICATION,
17 glGenTextures, glDeleteTextures, glBindTexture, glTexParameteri, 19 glGenTextures, glDeleteTextures, glBindTexture, glTexParameteri,
18 glTexImage2D, GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, 20 glTexImage2D, GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
28 glBindVertexArray, glVertexAttribPointer, GL_SHORT, GL_FLOAT, 30 glBindVertexArray, glVertexAttribPointer, GL_SHORT, GL_FLOAT,
29 glEnableVertexAttribArray, glDrawArrays, GL_TRIANGLE_STRIP, 31 glEnableVertexAttribArray, glDrawArrays, GL_TRIANGLE_STRIP,
30 glBlitFramebuffer, GL_READ_FRAMEBUFFER, glClear, GL_COLOR_BUFFER_BIT, 32 glBlitFramebuffer, GL_READ_FRAMEBUFFER, glClear, GL_COLOR_BUFFER_BIT,
31 GL_DEPTH_BUFFER_BIT, glViewport, glBlendFunc, GL_ONE, GL_ZERO) 33 GL_DEPTH_BUFFER_BIT, glViewport, glBlendFunc, GL_ONE, GL_ZERO)
32 34
35 from pytouhou.utils.maths cimport ortho_2d
36
37 from .shaders.eosd import PassthroughShader
33 from .backend cimport use_debug_group, use_vao, use_framebuffer_blit 38 from .backend cimport use_debug_group, use_vao, use_framebuffer_blit
34 39
35 cdef class Framebuffer: 40 cdef class Framebuffer:
36 def __init__(self, int x, int y, int width, int height): 41 def __init__(self, int x, int y, int width, int height):
37 if use_debug_group: 42 if use_debug_group:
75 glGenBuffers(1, &self.vbo) 80 glGenBuffers(1, &self.vbo)
76 glBindBuffer(GL_ARRAY_BUFFER, self.vbo) 81 glBindBuffer(GL_ARRAY_BUFFER, self.vbo)
77 glBufferData(GL_ARRAY_BUFFER, sizeof(self.buf), self.buf, GL_STATIC_DRAW) 82 glBufferData(GL_ARRAY_BUFFER, sizeof(self.buf), self.buf, GL_STATIC_DRAW)
78 83
79 # As a performance optimisation, if supported, store the rendering state into a vao. 84 # As a performance optimisation, if supported, store the rendering state into a vao.
80 if use_vao and not use_framebuffer_blit: 85 if use_vao:
81 glGenVertexArrays(1, &self.vao) 86 glGenVertexArrays(1, &self.vao)
82 glBindVertexArray(self.vao) 87 glBindVertexArray(self.vao)
83 self.set_state() 88 self.set_state()
84 glBindVertexArray(0) 89 glBindVertexArray(0)
85 90
86 glBindBuffer(GL_ARRAY_BUFFER, 0) 91 glBindBuffer(GL_ARRAY_BUFFER, 0)
92
93 # And finally the shader we’ll use to display everything.
94 self.shader = PassthroughShader()
95 self.mvp = ortho_2d(0., float(width), float(height), 0.)
87 else: 96 else:
88 self.x = x 97 self.x = x
89 self.y = y 98 self.y = y
90 self.width = width 99 self.width = width
91 self.height = height 100 self.height = height
96 def __dealloc__(self): 105 def __dealloc__(self):
97 if not use_framebuffer_blit: 106 if not use_framebuffer_blit:
98 glDeleteBuffers(1, &self.vbo) 107 glDeleteBuffers(1, &self.vbo)
99 if use_vao: 108 if use_vao:
100 glDeleteVertexArrays(1, &self.vao) 109 glDeleteVertexArrays(1, &self.vao)
110 if self.mvp != NULL:
111 free(self.mvp)
101 glDeleteTextures(1, &self.texture) 112 glDeleteTextures(1, &self.texture)
102 glDeleteRenderbuffers(1, &self.rbo) 113 glDeleteRenderbuffers(1, &self.rbo)
103 glDeleteFramebuffers(1, &self.fbo) 114 glDeleteFramebuffers(1, &self.fbo)
104 115
105 cpdef bind(self): 116 cpdef bind(self):
112 glVertexAttribPointer(0, 2, GL_SHORT, False, sizeof(PassthroughVertex), <void*>0) 123 glVertexAttribPointer(0, 2, GL_SHORT, False, sizeof(PassthroughVertex), <void*>0)
113 glEnableVertexAttribArray(0) 124 glEnableVertexAttribArray(0)
114 glVertexAttribPointer(1, 2, GL_FLOAT, False, sizeof(PassthroughVertex), <void*>4) 125 glVertexAttribPointer(1, 2, GL_FLOAT, False, sizeof(PassthroughVertex), <void*>4)
115 glEnableVertexAttribArray(1) 126 glEnableVertexAttribArray(1)
116 127
117 cdef void render(self, int x, int y, int width, int height) nogil: 128 cdef void render(self, int x, int y, int width, int height) except *:
118 if use_debug_group: 129 if use_debug_group:
119 glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Framebuffer drawing") 130 glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Framebuffer drawing")
120 131
121 glBindFramebuffer(GL_FRAMEBUFFER, 0) 132 glBindFramebuffer(GL_FRAMEBUFFER, 0)
122 glClear(GL_COLOR_BUFFER_BIT) 133 glClear(GL_COLOR_BUFFER_BIT)
125 glBindFramebuffer(GL_READ_FRAMEBUFFER, self.fbo) 136 glBindFramebuffer(GL_READ_FRAMEBUFFER, self.fbo)
126 glBlitFramebuffer(self.x, self.y, self.width, self.height, 137 glBlitFramebuffer(self.x, self.y, self.width, self.height,
127 x, y, x + width, y + height, 138 x, y, x + width, y + height,
128 GL_COLOR_BUFFER_BIT, GL_LINEAR) 139 GL_COLOR_BUFFER_BIT, GL_LINEAR)
129 else: 140 else:
141 self.shader.bind()
142 self.shader.uniform_matrix('mvp', self.mvp)
143
130 glViewport(x, y, width, height) 144 glViewport(x, y, width, height)
131 glBlendFunc(GL_ONE, GL_ZERO) 145 glBlendFunc(GL_ONE, GL_ZERO)
132 146
133 if use_vao: 147 if use_vao:
134 glBindVertexArray(self.vao) 148 glBindVertexArray(self.vao)