comparison pytouhou/ui/opengl/framebuffer.pyx @ 587:6c9d8a3d853f

Use ARB_framebuffer_blit instead of a second rendering pass for scaled rendering, if supported, and remove framebuffer stuff from the Renderer.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 08 Oct 2014 18:34:27 +0200
parents 4b0593da29d5
children 23b9418e4b2f
comparison
equal deleted inserted replaced
586:4b0593da29d5 587:6c9d8a3d853f
24 glCheckFramebufferStatus, GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, 24 glCheckFramebufferStatus, GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
25 GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_COMPLETE, glGenBuffers, 25 GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_COMPLETE, glGenBuffers,
26 glDeleteBuffers, glBindBuffer, glBufferData, GL_ARRAY_BUFFER, 26 glDeleteBuffers, glBindBuffer, glBufferData, GL_ARRAY_BUFFER,
27 GL_STATIC_DRAW, glGenVertexArrays, glDeleteVertexArrays, 27 GL_STATIC_DRAW, glGenVertexArrays, glDeleteVertexArrays,
28 glBindVertexArray, glVertexAttribPointer, GL_SHORT, GL_FLOAT, 28 glBindVertexArray, glVertexAttribPointer, GL_SHORT, GL_FLOAT,
29 glEnableVertexAttribArray, glDrawArrays, GL_TRIANGLE_STRIP) 29 glEnableVertexAttribArray, glDrawArrays, GL_TRIANGLE_STRIP,
30 glBlitFramebuffer, GL_DRAW_FRAMEBUFFER, glClear, GL_COLOR_BUFFER_BIT,
31 GL_DEPTH_BUFFER_BIT, glViewport, glBlendFunc, GL_ONE, GL_ZERO)
30 32
31 from .backend cimport use_debug_group, use_vao 33 from .backend cimport use_debug_group, use_vao, use_framebuffer_blit
32 34
33 cdef class Framebuffer: 35 cdef class Framebuffer:
34 def __init__(self, int x, int y, int width, int height): 36 def __init__(self, int x, int y, int width, int height):
35 if use_debug_group: 37 if use_debug_group:
36 glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Framebuffer creation") 38 glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Framebuffer creation")
60 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, self.texture, 0) 62 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, self.texture, 0)
61 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, self.rbo) 63 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, self.rbo)
62 assert glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE 64 assert glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE
63 glBindFramebuffer(GL_FRAMEBUFFER, 0) 65 glBindFramebuffer(GL_FRAMEBUFFER, 0)
64 66
65 # We’ll use only those vertices, everytime. 67 if not use_framebuffer_blit:
66 self.buf[0] = PassthroughVertex(x, y, 0, 1) 68 # We’ll use only those vertices, everytime.
67 self.buf[1] = PassthroughVertex(x + width, y, 1, 1) 69 self.buf[0] = PassthroughVertex(x, y, 0, 1)
68 self.buf[2] = PassthroughVertex(x, y + height, 0, 0) 70 self.buf[1] = PassthroughVertex(x + width, y, 1, 1)
69 self.buf[3] = PassthroughVertex(x + width, y + height, 1, 0) 71 self.buf[2] = PassthroughVertex(x, y + height, 0, 0)
72 self.buf[3] = PassthroughVertex(x + width, y + height, 1, 0)
70 73
71 # Now we upload those vertices into a static vbo. 74 # Now we upload those vertices into a static vbo.
72 glGenBuffers(1, &self.vbo) 75 glGenBuffers(1, &self.vbo)
73 glBindBuffer(GL_ARRAY_BUFFER, self.vbo) 76 glBindBuffer(GL_ARRAY_BUFFER, self.vbo)
74 glBufferData(GL_ARRAY_BUFFER, sizeof(self.buf), self.buf, GL_STATIC_DRAW) 77 glBufferData(GL_ARRAY_BUFFER, sizeof(self.buf), self.buf, GL_STATIC_DRAW)
75 78
76 # As a performance optimisation, if supported, store the rendering state into a vao. 79 # As a performance optimisation, if supported, store the rendering state into a vao.
77 if use_vao: 80 if use_vao and not use_framebuffer_blit:
78 glGenVertexArrays(1, &self.vao) 81 glGenVertexArrays(1, &self.vao)
79 glBindVertexArray(self.vao) 82 glBindVertexArray(self.vao)
80 self.set_state() 83 self.set_state()
81 glBindVertexArray(0) 84 glBindVertexArray(0)
82 85
83 glBindBuffer(GL_ARRAY_BUFFER, 0) 86 glBindBuffer(GL_ARRAY_BUFFER, 0)
87 else:
88 self.x = x
89 self.y = y
90 self.width = width
91 self.height = height
84 92
85 if use_debug_group: 93 if use_debug_group:
86 glPopDebugGroup() 94 glPopDebugGroup()
87 95
88 def __dealloc__(self): 96 def __dealloc__(self):
89 if use_vao: 97 if not use_framebuffer_blit:
90 glDeleteVertexArrays(1, &self.vao) 98 glDeleteBuffers(1, &self.vbo)
99 if use_vao:
100 glDeleteVertexArrays(1, &self.vao)
91 glDeleteTextures(1, &self.texture) 101 glDeleteTextures(1, &self.texture)
92 glDeleteRenderbuffers(1, &self.rbo) 102 glDeleteRenderbuffers(1, &self.rbo)
93 glDeleteFramebuffers(1, &self.fbo) 103 glDeleteFramebuffers(1, &self.fbo)
94 glDeleteBuffers(1, &self.vbo)
95 104
96 cpdef bind(self): 105 cpdef bind(self):
97 glBindFramebuffer(GL_FRAMEBUFFER, self.fbo) 106 glBindFramebuffer(GL_FRAMEBUFFER, self.fbo)
98 107
99 cdef void set_state(self) nogil: 108 cdef void set_state(self) nogil:
103 glVertexAttribPointer(0, 2, GL_SHORT, False, sizeof(PassthroughVertex), <void*>0) 112 glVertexAttribPointer(0, 2, GL_SHORT, False, sizeof(PassthroughVertex), <void*>0)
104 glEnableVertexAttribArray(0) 113 glEnableVertexAttribArray(0)
105 glVertexAttribPointer(1, 2, GL_FLOAT, False, sizeof(PassthroughVertex), <void*>4) 114 glVertexAttribPointer(1, 2, GL_FLOAT, False, sizeof(PassthroughVertex), <void*>4)
106 glEnableVertexAttribArray(1) 115 glEnableVertexAttribArray(1)
107 116
108 cdef void render(self) nogil: 117 cdef void render(self, int x, int y, int width, int height) nogil:
109 if use_vao: 118 if use_debug_group:
110 glBindVertexArray(self.vao) 119 glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Framebuffer drawing")
120
121 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0)
122 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
123
124 if use_framebuffer_blit:
125 glBlitFramebuffer(self.x, self.y, self.width, self.height,
126 x, y, x + width, y + height,
127 GL_COLOR_BUFFER_BIT, GL_LINEAR)
111 else: 128 else:
112 self.set_state() 129 glViewport(x, y, width, height)
130 glBlendFunc(GL_ONE, GL_ZERO)
113 131
114 glBindTexture(GL_TEXTURE_2D, self.texture) 132 if use_vao:
115 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4) 133 glBindVertexArray(self.vao)
116 glBindTexture(GL_TEXTURE_2D, 0) 134 else:
135 self.set_state()
117 136
118 if use_vao: 137 glBindTexture(GL_TEXTURE_2D, self.texture)
119 glBindVertexArray(0) 138 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4)
120 else: 139 glBindTexture(GL_TEXTURE_2D, 0)
121 glBindBuffer(GL_ARRAY_BUFFER, 0) 140
141 if use_vao:
142 glBindVertexArray(0)
143 else:
144 glBindBuffer(GL_ARRAY_BUFFER, 0)
145
146 if use_debug_group:
147 glPopDebugGroup()