Move waterfall animation to GPU

- Refactored texture generation to linear wrap in GPU memory with
glTexSubImage2D (1 line per frame upload max)
- Texture upload per frame reduced nearly 700-800x (~2-4kb instead of
~1.6mb)
This commit is contained in:
Charles J. Cliffe 2015-02-08 13:49:12 -05:00
parent b9efb43a6b
commit 679ad64a1f
2 changed files with 35 additions and 41 deletions

View File

@ -6,7 +6,6 @@ WaterfallContext::WaterfallContext(WaterfallCanvas *canvas, wxGLContext *sharedC
PrimaryGLContext(canvas, sharedContext), waterfall_lines(0), fft_size(0), activeTheme(NULL) { PrimaryGLContext(canvas, sharedContext), waterfall_lines(0), fft_size(0), activeTheme(NULL) {
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
waterfall[i] = 0; waterfall[i] = 0;
waterfall_tex[i] = 0;
} }
} }
@ -21,20 +20,14 @@ void WaterfallContext::Setup(int fft_size_in, int num_waterfall_lines_in) {
glDeleteTextures(1, &waterfall[i]); glDeleteTextures(1, &waterfall[i]);
waterfall[i] = 0; waterfall[i] = 0;
} }
if (waterfall_tex[i]) {
delete waterfall_tex[i];
}
waterfall_tex[i] = new unsigned char[half_fft_size * waterfall_lines * 2]; waterfall_ofs[i] = waterfall_lines - 1;
memset(waterfall_tex[i], 0, half_fft_size * waterfall_lines * 2);
} }
// Stagger memory updates at half intervals for tiles // Stagger memory updates at half intervals for tiles
waterfall_ofs[0] = waterfall_lines;
waterfall_ofs[1] = waterfall_lines - waterfall_lines / 8;
} }
void WaterfallContext::refreshTheme() { void WaterfallContext::refreshTheme() {
glEnable(GL_TEXTURE_2D); glEnable (GL_TEXTURE_2D);
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
glBindTexture(GL_TEXTURE_2D, waterfall[i]); glBindTexture(GL_TEXTURE_2D, waterfall[i]);
@ -48,21 +41,33 @@ void WaterfallContext::refreshTheme() {
void WaterfallContext::Draw(std::vector<float> &points) { void WaterfallContext::Draw(std::vector<float> &points) {
glEnable(GL_TEXTURE_2D); int half_fft_size = fft_size / 2;
glEnable (GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
if (!waterfall[0]) { if (!waterfall[0]) {
glGenTextures(2, waterfall); glGenTextures(2, waterfall);
unsigned char *waterfall_tex;
waterfall_tex = new unsigned char[half_fft_size * waterfall_lines];
memset(waterfall_tex, 0, half_fft_size * waterfall_lines);
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
glBindTexture(GL_TEXTURE_2D, waterfall[i]); glBindTexture(GL_TEXTURE_2D, waterfall[i]);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, waterfall[i]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, half_fft_size, waterfall_lines, 0, GL_COLOR_INDEX, GL_UNSIGNED_BYTE, (GLvoid *) waterfall_tex);
} }
delete waterfall_tex;
} }
if (activeTheme != ThemeMgr::mgr.currentTheme) { if (activeTheme != ThemeMgr::mgr.currentTheme) {
@ -70,37 +75,24 @@ void WaterfallContext::Draw(std::vector<float> &points) {
activeTheme = ThemeMgr::mgr.currentTheme; activeTheme = ThemeMgr::mgr.currentTheme;
} }
int half_fft_size = fft_size / 2;
if (points.size()) { if (points.size()) {
unsigned char waterfall_slice[2][half_fft_size];
for (int j = 0; j < 2; j++) { for (int j = 0; j < 2; j++) {
int ofs = waterfall_ofs[j];
for (int i = 0, iMax = half_fft_size; i < iMax; i++) { for (int i = 0, iMax = half_fft_size; i < iMax; i++) {
float v = points[(j * half_fft_size + i) * 2 + 1]; float v = points[(j * half_fft_size + i) * 2 + 1];
float wv = v < 0 ? 0 : (v > 0.99 ? 0.99 : v); float wv = v < 0 ? 0 : (v > 0.99 ? 0.99 : v);
waterfall_tex[j][i + ofs * half_fft_size] = (unsigned char) floor(wv * 255.0); waterfall_slice[j][i] = (unsigned char) floor(wv * 255.0);
} }
int quarter_lines = (waterfall_lines / 4); glBindTexture(GL_TEXTURE_2D, waterfall[j]);
int k = 4; glTexSubImage2D(GL_TEXTURE_2D, 0, 0, waterfall_ofs[j], half_fft_size, 1, GL_COLOR_INDEX, GL_UNSIGNED_BYTE, (GLvoid *) waterfall_slice[j]);
while (k--) {
if (waterfall_ofs[j] == quarter_lines * k) {
memcpy(waterfall_tex[j] + (waterfall_lines * half_fft_size) + (quarter_lines * k * half_fft_size),
waterfall_tex[j] + (quarter_lines * k * half_fft_size), quarter_lines * half_fft_size);
}
}
if (waterfall_ofs[j] == 0) { if (waterfall_ofs[j] == 0) {
waterfall_ofs[j] = waterfall_lines; waterfall_ofs[j] = waterfall_lines;
} }
glBindTexture(GL_TEXTURE_2D, waterfall[j]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, half_fft_size, waterfall_lines, 0, GL_COLOR_INDEX, GL_UNSIGNED_BYTE,
(GLvoid *) (waterfall_tex[j] + (half_fft_size * (waterfall_ofs[j]))));
waterfall_ofs[j]--; waterfall_ofs[j]--;
} }
} }
@ -108,35 +100,39 @@ void WaterfallContext::Draw(std::vector<float> &points) {
glColor3f(1.0, 1.0, 1.0); glColor3f(1.0, 1.0, 1.0);
GLint vp[4]; GLint vp[4];
glGetIntegerv( GL_VIEWPORT, vp); glGetIntegerv(GL_VIEWPORT, vp);
float viewWidth = (float) vp[2]; float viewWidth = (float) vp[2];
float viewHeight = (float) vp[3];
// some bias to prevent seams at odd scales // some bias to prevent seams at odd scales
float half_pixel = 1.0 / (float) viewWidth; float half_pixel = 1.0 / viewWidth;
float half_texel = 1.0 / (float) half_fft_size; float half_texel = 1.0 / (float) half_fft_size;
float vtexel = 1.0 / (float) waterfall_lines;
float vofs = (float) (waterfall_ofs[0] + 1) * vtexel;
glBindTexture(GL_TEXTURE_2D, waterfall[0]); glBindTexture(GL_TEXTURE_2D, waterfall[0]);
glBegin(GL_QUADS); glBegin (GL_QUADS);
glTexCoord2f(0.0 + half_texel, 1.0 - half_texel); glTexCoord2f(0.0 + half_texel, 1.0 + vofs);
glVertex3f(-1.0, -1.0, 0.0); glVertex3f(-1.0, -1.0, 0.0);
glTexCoord2f(1.0 - half_texel, 1.0 - half_texel); glTexCoord2f(1.0 - half_texel, 1.0 + vofs);
glVertex3f(0.0 + half_pixel, -1.0, 0.0); glVertex3f(0.0 + half_pixel, -1.0, 0.0);
glTexCoord2f(1.0 - half_texel, 0.0 + half_texel); glTexCoord2f(1.0 - half_texel, 0.0 + vofs);
glVertex3f(0.0 + half_pixel, 1.0, 0.0); glVertex3f(0.0 + half_pixel, 1.0, 0.0);
glTexCoord2f(0.0 + half_texel, 0.0 + half_texel); glTexCoord2f(0.0 + half_texel, 0.0 + vofs);
glVertex3f(-1.0, 1.0, 0.0); glVertex3f(-1.0, 1.0, 0.0);
glEnd(); glEnd();
vofs = (float) (waterfall_ofs[1] + 1) * vtexel;
glBindTexture(GL_TEXTURE_2D, waterfall[1]); glBindTexture(GL_TEXTURE_2D, waterfall[1]);
glBegin(GL_QUADS); glBegin(GL_QUADS);
glTexCoord2f(0.0 + half_texel, 1.0 - half_texel); glTexCoord2f(0.0 + half_texel, 1.0 + vofs);
glVertex3f(0.0 - half_pixel, -1.0, 0.0); glVertex3f(0.0 - half_pixel, -1.0, 0.0);
glTexCoord2f(1.0 - half_texel, 1.0 - half_texel); glTexCoord2f(1.0 - half_texel, 1.0 + vofs);
glVertex3f(1.0, -1.0, 0.0); glVertex3f(1.0, -1.0, 0.0);
glTexCoord2f(1.0 - half_texel, 0.0 + half_texel); glTexCoord2f(1.0 - half_texel, 0.0 + vofs);
glVertex3f(1.0, 1.0, 0.0); glVertex3f(1.0, 1.0, 0.0);
glTexCoord2f(0.0 + half_texel, 0.0 + half_texel); glTexCoord2f(0.0 + half_texel, 0.0 + vofs);
glVertex3f(0.0 - half_pixel, 1.0, 0.0); glVertex3f(0.0 - half_pixel, 1.0, 0.0);
glEnd(); glEnd();
@ -144,5 +140,4 @@ void WaterfallContext::Draw(std::vector<float> &points) {
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glDisable(GL_TEXTURE_2D); glDisable(GL_TEXTURE_2D);
} }

View File

@ -16,7 +16,6 @@ public:
private: private:
GLuint waterfall[2]; GLuint waterfall[2];
unsigned char *waterfall_tex[2];
int waterfall_ofs[2]; int waterfall_ofs[2];
int fft_size; int fft_size;
int waterfall_lines; int waterfall_lines;