Created
May 22, 2013 12:57
-
-
Save richardgv/5627338 to your computer and use it in GitHub Desktop.
chjj/compton #107: Use OpenGL VBO (Broken code, do not use in productive environment.)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/compton.c b/src/compton.c | |
index c55d045..a9be15c 100644 | |
--- a/src/compton.c | |
+++ b/src/compton.c | |
@@ -5118,6 +5118,7 @@ get_cfg(session_t *ps, int argc, char *const *argv, bool first_pass) { | |
{ "resize-damage", required_argument, NULL, 302 }, | |
{ "glx-use-gpushader4", no_argument, NULL, 303 }, | |
{ "opacity-rule", required_argument, NULL, 304 }, | |
+ { "glx-use-vbo", no_argument, NULL, 305 }, | |
// Must terminate with a NULL entry | |
{ NULL, 0, NULL, 0 }, | |
}; | |
@@ -5356,6 +5357,7 @@ get_cfg(session_t *ps, int argc, char *const *argv, bool first_pass) { | |
if (!parse_rule_opacity(ps, optarg)) | |
exit(1); | |
break; | |
+ P_CASEBOOL(305, glx_use_vbo); | |
default: | |
usage(1); | |
break; | |
diff --git a/src/opengl.c b/src/opengl.c | |
index e273f2d..fae5acb 100644 | |
--- a/src/opengl.c | |
+++ b/src/opengl.c | |
@@ -933,6 +933,8 @@ glx_blur_dst(session_t *ps, int dx, int dy, int width, int height, float z, | |
const bool have_stencil = glIsEnabled(GL_STENCIL_TEST); | |
bool ret = false; | |
+ P_PAINTREG_START(); | |
+ | |
// Calculate copy region size | |
glx_blur_cache_t ibc = { .width = 0, .height = 0 }; | |
if (!pbc) | |
@@ -1074,7 +1076,6 @@ glx_blur_dst(session_t *ps, int dx, int dy, int width, int height, float z, | |
glUniform1f(ppass->unifm_factor_center, factor_center); | |
{ | |
- P_PAINTREG_START(); | |
P_PAINTREG_STARTLOOP(); | |
{ | |
const GLfloat rx = (crect.x - mdx) * texfac_x; | |
@@ -1110,7 +1111,6 @@ glx_blur_dst(session_t *ps, int dx, int dy, int width, int height, float z, | |
glVertex3f(rdx, rdye, z); | |
} | |
P_PAINTREG_ENDLOOP(); | |
- P_PAINTREG_END(); | |
} | |
glUseProgram(0); | |
@@ -1126,6 +1126,8 @@ glx_blur_dst(session_t *ps, int dx, int dy, int width, int height, float z, | |
ret = true; | |
glx_blur_dst_end: | |
+ P_PAINTREG_END(); | |
+ | |
#ifdef CONFIG_VSYNC_OPENGL_FBO | |
glBindFramebuffer(GL_FRAMEBUFFER, 0); | |
#endif | |
@@ -1314,6 +1316,22 @@ glx_render(session_t *ps, const glx_texture_t *ptex, | |
// Painting | |
{ | |
P_PAINTREG_START(); | |
+ | |
+ GLfloat (*vertices)[5] = NULL; | |
+ GLuint vbov = 0; | |
+ int cv = 0; | |
+ if (ps->o.glx_use_vbo /* && nrects > 4 */) { | |
+ vertices = malloc(nrects * 4 * 5 * sizeof(GLfloat)); | |
+ /* if (vertices) | |
+ glGenBuffersARB(1, &vbov); */ | |
+ if (!vertices /* || !vbov */) { | |
+ printf_errf("(): Failed create allocate memory for vertices or " | |
+ "create VBOs. Falling back to immediate mode."); | |
+ } | |
+ } | |
+ | |
+ /* if (ps->o.glx_use_vbo && nrects <= 4) | |
+ glBegin(GL_QUADS); */ | |
P_PAINTREG_STARTLOOP(); | |
{ | |
GLfloat rx = (double) (crect.x - dx + x); | |
@@ -1344,27 +1362,71 @@ glx_render(session_t *ps, const glx_texture_t *ptex, | |
printf_dbgf("(): Rect %d: %f, %f, %f, %f -> %d, %d, %d, %d\n", ri, rx, ry, rxe, rye, rdx, rdy, rdxe, rdye); | |
#endif | |
+#define P_VTEX(cx, cy, cz) { \ | |
+ if (vertices) { \ | |
+ vertices[cv][0] = cx; \ | |
+ vertices[cv][1] = cy; \ | |
+ vertices[cv][2] = cz; \ | |
+ ++cv; \ | |
+ } \ | |
+ else glVertex3i(cx, cy, cz); \ | |
+} | |
+ | |
#define P_TEXCOORD(cx, cy) { \ | |
- if (dual_texture) { \ | |
+ if (vertices) { \ | |
+ vertices[cv][3] = cx; \ | |
+ vertices[cv][4] = cy; \ | |
+ } \ | |
+ else if (dual_texture) { \ | |
glMultiTexCoord2f(GL_TEXTURE0, cx, cy); \ | |
glMultiTexCoord2f(GL_TEXTURE1, cx, cy); \ | |
} \ | |
else glTexCoord2f(cx, cy); \ | |
} | |
P_TEXCOORD(rx, ry); | |
- glVertex3i(rdx, rdy, z); | |
+ P_VTEX(rdx, rdy, z); | |
P_TEXCOORD(rxe, ry); | |
- glVertex3i(rdxe, rdy, z); | |
+ P_VTEX(rdxe, rdy, z); | |
P_TEXCOORD(rxe, rye); | |
- glVertex3i(rdxe, rdye, z); | |
+ P_VTEX(rdxe, rdye, z); | |
P_TEXCOORD(rx, rye); | |
- glVertex3i(rdx, rdye, z); | |
+ P_VTEX(rdx, rdye, z); | |
} | |
P_PAINTREG_ENDLOOP(); | |
+ // if (ps->o.glx_use_vbo && nrects <= 4) glEnd(); | |
P_PAINTREG_END(); | |
+ | |
+ assert(cv <= nrects * 4); | |
+ if (/* vbov && */ cv) { | |
+ /* glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbov); | |
+ glBufferDataARB(GL_ARRAY_BUFFER_ARB, 5 * cv * sizeof(GLfloat), | |
+ vertices, GL_STATIC_DRAW_ARB); */ | |
+ glEnableClientState(GL_VERTEX_ARRAY); | |
+ // glVertexPointer(3, GL_FLOAT, 5 * sizeof(GLfloat), (const void *) 0); | |
+ glVertexPointer(3, GL_FLOAT, 5 * sizeof(GLfloat), vertices[0]); | |
+ glEnableClientState(GL_TEXTURE_COORD_ARRAY); | |
+ /* glTexCoordPointer(2, GL_FLOAT, 5 * sizeof(GLfloat), | |
+ (const void *) (3 * sizeof(GLfloat))); */ | |
+ glTexCoordPointer(2, GL_FLOAT, 5 * sizeof(GLfloat), &vertices[0][3]); | |
+ if (dual_texture) { | |
+ glClientActiveTexture(GL_TEXTURE1); | |
+ glTexCoordPointer(2, GL_FLOAT, 5 * sizeof(GLfloat), &vertices[0][3]); | |
+ /* glTexCoordPointer(2, GL_FLOAT, 5 * sizeof(GLfloat), | |
+ (const void *) (3 * sizeof(GLfloat))); */ | |
+ glClientActiveTexture(GL_TEXTURE0); | |
+ } | |
+ glDrawArrays(GL_QUADS, 0, cv); | |
+ glDisableClientState(GL_TEXTURE_COORD_ARRAY); | |
+ glDisableClientState(GL_VERTEX_ARRAY); | |
+ // glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); | |
+ } | |
+ | |
+ /* if (vbov) | |
+ glDeleteBuffersARB(1, &vbov); */ | |
+ free(vertices); | |
} | |
// Cleanup | |
@@ -1593,4 +1655,3 @@ glx_create_program_end: | |
return program; | |
} | |
#endif | |
- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/common.h b/src/common.h | |
index 547ecee..2c1df9f 100644 | |
--- a/src/common.h | |
+++ b/src/common.h | |
@@ -657,6 +657,7 @@ typedef struct { | |
#endif | |
// Cached blur convolution kernels. | |
XFixed *blur_kerns_cache[MAX_BLUR_PASS]; | |
+ GLuint vbo; | |
/// Reset program after next paint. | |
bool reset; | |
diff --git a/src/compton.c b/src/compton.c | |
index c55d045..a9be15c 100644 | |
--- a/src/compton.c | |
+++ b/src/compton.c | |
@@ -5118,6 +5118,7 @@ get_cfg(session_t *ps, int argc, char *const *argv, bool first_pass) { | |
{ "resize-damage", required_argument, NULL, 302 }, | |
{ "glx-use-gpushader4", no_argument, NULL, 303 }, | |
{ "opacity-rule", required_argument, NULL, 304 }, | |
+ { "glx-use-vbo", no_argument, NULL, 305 }, | |
// Must terminate with a NULL entry | |
{ NULL, 0, NULL, 0 }, | |
}; | |
@@ -5356,6 +5357,7 @@ get_cfg(session_t *ps, int argc, char *const *argv, bool first_pass) { | |
if (!parse_rule_opacity(ps, optarg)) | |
exit(1); | |
break; | |
+ P_CASEBOOL(305, glx_use_vbo); | |
default: | |
usage(1); | |
break; | |
diff --git a/src/opengl.c b/src/opengl.c | |
index e273f2d..583718a 100644 | |
--- a/src/opengl.c | |
+++ b/src/opengl.c | |
@@ -933,6 +933,8 @@ glx_blur_dst(session_t *ps, int dx, int dy, int width, int height, float z, | |
const bool have_stencil = glIsEnabled(GL_STENCIL_TEST); | |
bool ret = false; | |
+ P_PAINTREG_START(); | |
+ | |
// Calculate copy region size | |
glx_blur_cache_t ibc = { .width = 0, .height = 0 }; | |
if (!pbc) | |
@@ -1074,7 +1076,6 @@ glx_blur_dst(session_t *ps, int dx, int dy, int width, int height, float z, | |
glUniform1f(ppass->unifm_factor_center, factor_center); | |
{ | |
- P_PAINTREG_START(); | |
P_PAINTREG_STARTLOOP(); | |
{ | |
const GLfloat rx = (crect.x - mdx) * texfac_x; | |
@@ -1110,7 +1111,6 @@ glx_blur_dst(session_t *ps, int dx, int dy, int width, int height, float z, | |
glVertex3f(rdx, rdye, z); | |
} | |
P_PAINTREG_ENDLOOP(); | |
- P_PAINTREG_END(); | |
} | |
glUseProgram(0); | |
@@ -1126,6 +1126,8 @@ glx_blur_dst(session_t *ps, int dx, int dy, int width, int height, float z, | |
ret = true; | |
glx_blur_dst_end: | |
+ P_PAINTREG_END(); | |
+ | |
#ifdef CONFIG_VSYNC_OPENGL_FBO | |
glBindFramebuffer(GL_FRAMEBUFFER, 0); | |
#endif | |
@@ -1314,6 +1316,28 @@ glx_render(session_t *ps, const glx_texture_t *ptex, | |
// Painting | |
{ | |
P_PAINTREG_START(); | |
+ | |
+ GLfloat (*vertices)[5] = NULL; | |
+ GLuint vbov = 0; | |
+ int cv = 0; | |
+ if (ps->o.glx_use_vbo /* && nrects > 4 */) { | |
+ vertices = malloc(nrects * 4 * 5 * sizeof(GLfloat)); | |
+ if (vertices && !ps->vbo) { | |
+ glGenBuffersARB(1, &ps->vbo); | |
+ glBindBufferARB(GL_ARRAY_BUFFER_ARB, ps->vbo); | |
+ glBufferDataARB(GL_ARRAY_BUFFER_ARB, 5 * 100 * sizeof(GLfloat), | |
+ NULL, GL_STREAM_DRAW_ARB); | |
+ glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); | |
+ } | |
+ vbov = ps->vbo; | |
+ if (!vertices || !vbov) { | |
+ printf_errf("(): Failed create allocate memory for vertices or " | |
+ "create VBOs. Falling back to immediate mode."); | |
+ } | |
+ } | |
+ | |
+ /* if (ps->o.glx_use_vbo && nrects <= 4) | |
+ glBegin(GL_QUADS); */ | |
P_PAINTREG_STARTLOOP(); | |
{ | |
GLfloat rx = (double) (crect.x - dx + x); | |
@@ -1344,27 +1368,73 @@ glx_render(session_t *ps, const glx_texture_t *ptex, | |
printf_dbgf("(): Rect %d: %f, %f, %f, %f -> %d, %d, %d, %d\n", ri, rx, ry, rxe, rye, rdx, rdy, rdxe, rdye); | |
#endif | |
+#define P_VTEX(cx, cy, cz) { \ | |
+ if (vertices) { \ | |
+ vertices[cv][0] = cx; \ | |
+ vertices[cv][1] = cy; \ | |
+ vertices[cv][2] = cz; \ | |
+ ++cv; \ | |
+ } \ | |
+ else glVertex3i(cx, cy, cz); \ | |
+} | |
+ | |
#define P_TEXCOORD(cx, cy) { \ | |
- if (dual_texture) { \ | |
+ if (vertices) { \ | |
+ vertices[cv][3] = cx; \ | |
+ vertices[cv][4] = cy; \ | |
+ } \ | |
+ else if (dual_texture) { \ | |
glMultiTexCoord2f(GL_TEXTURE0, cx, cy); \ | |
glMultiTexCoord2f(GL_TEXTURE1, cx, cy); \ | |
} \ | |
else glTexCoord2f(cx, cy); \ | |
} | |
P_TEXCOORD(rx, ry); | |
- glVertex3i(rdx, rdy, z); | |
+ P_VTEX(rdx, rdy, z); | |
P_TEXCOORD(rxe, ry); | |
- glVertex3i(rdxe, rdy, z); | |
+ P_VTEX(rdxe, rdy, z); | |
P_TEXCOORD(rxe, rye); | |
- glVertex3i(rdxe, rdye, z); | |
+ P_VTEX(rdxe, rdye, z); | |
P_TEXCOORD(rx, rye); | |
- glVertex3i(rdx, rdye, z); | |
+ P_VTEX(rdx, rdye, z); | |
} | |
P_PAINTREG_ENDLOOP(); | |
+ // if (ps->o.glx_use_vbo && nrects <= 4) glEnd(); | |
P_PAINTREG_END(); | |
+ | |
+ assert(cv <= nrects * 4); | |
+ if (vbov && cv) { | |
+ glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbov); | |
+ /* glBufferDataARB(GL_ARRAY_BUFFER_ARB, 5 * cv * sizeof(GLfloat), | |
+ vertices, GL_STATIC_DRAW_ARB); */ | |
+ glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, 5 * cv * sizeof(GLfloat), | |
+ vertices); | |
+ glEnableClientState(GL_VERTEX_ARRAY); | |
+ glVertexPointer(3, GL_FLOAT, 5 * sizeof(GLfloat), (const void *) 0); | |
+ // glVertexPointer(3, GL_FLOAT, 5 * sizeof(GLfloat), vertices[0]); | |
+ glEnableClientState(GL_TEXTURE_COORD_ARRAY); | |
+ glTexCoordPointer(2, GL_FLOAT, 5 * sizeof(GLfloat), | |
+ (const void *) (3 * sizeof(GLfloat))); | |
+ // glTexCoordPointer(2, GL_FLOAT, 5 * sizeof(GLfloat), &vertices[0][3]); | |
+ if (dual_texture) { | |
+ glClientActiveTexture(GL_TEXTURE1); | |
+ // glTexCoordPointer(2, GL_FLOAT, 5 * sizeof(GLfloat), &vertices[0][3]); | |
+ glTexCoordPointer(2, GL_FLOAT, 5 * sizeof(GLfloat), | |
+ (const void *) (3 * sizeof(GLfloat))); | |
+ glClientActiveTexture(GL_TEXTURE0); | |
+ } | |
+ glDrawArrays(GL_QUADS, 0, cv); | |
+ glDisableClientState(GL_TEXTURE_COORD_ARRAY); | |
+ glDisableClientState(GL_VERTEX_ARRAY); | |
+ glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); | |
+ } | |
+ | |
+ /* if (vbov) | |
+ glDeleteBuffersARB(1, &vbov); */ | |
+ free(vertices); | |
} | |
// Cleanup | |
@@ -1593,4 +1663,3 @@ glx_create_program_end: | |
return program; | |
} | |
#endif | |
- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment