fosphor: Refactor to introduce more shared state between global/cl/gl

Originally I wanted the CL/GL part to be as separate as possible and
have no shared data. But it's too inconvenient since a beside the
CL / GL objects there is also configuration data that should be shared
and distributed between the two.

So instead we still have separate gl/cl state that are restricted but
the function act on a shared 'struct fosphor' object that contains
everything that's shared and exposed to both cl.c and gl.c

Signed-off-by: Sylvain Munaut <tnt@246tNt.com>
This commit is contained in:
Sylvain Munaut 2013-11-07 23:46:29 +01:00
parent 68f0302bce
commit 3ccf1aa52d
7 changed files with 101 additions and 70 deletions

View File

@ -44,9 +44,9 @@
# include <GL/glx.h> # include <GL/glx.h>
#endif #endif
#include "config.h"
#include "cl.h" #include "cl.h"
#include "gl.h" #include "gl.h"
#include "private.h"
#include "resource.h" #include "resource.h"
@ -349,8 +349,9 @@ error:
} }
static int static int
cl_do_init(struct fosphor_cl_state *cl, struct fosphor_gl_state *gl) cl_do_init(struct fosphor *self)
{ {
struct fosphor_cl_state *cl = self->cl;
cl_context_properties ctx_props[7]; cl_context_properties ctx_props[7];
const char *disp_opts; const char *disp_opts;
cl_int err; cl_int err;
@ -429,7 +430,7 @@ cl_do_init(struct fosphor_cl_state *cl, struct fosphor_gl_state *gl)
/* Waterfall texture */ /* Waterfall texture */
cl->mem_waterfall = clCreateFromGLTexture2D(cl->ctx, cl->mem_waterfall = clCreateFromGLTexture2D(cl->ctx,
CL_MEM_READ_WRITE, GL_TEXTURE_2D, 0, CL_MEM_READ_WRITE, GL_TEXTURE_2D, 0,
fosphor_gl_get_shared_id(gl, GL_ID_TEX_WATERFALL), fosphor_gl_get_shared_id(self, GL_ID_TEX_WATERFALL),
&err &err
); );
CL_ERR_CHECK(err, "Unable to share waterfall texture into OpenCL context"); CL_ERR_CHECK(err, "Unable to share waterfall texture into OpenCL context");
@ -437,7 +438,7 @@ cl_do_init(struct fosphor_cl_state *cl, struct fosphor_gl_state *gl)
/* Histogram texture */ /* Histogram texture */
cl->mem_histogram = clCreateFromGLTexture2D(cl->ctx, cl->mem_histogram = clCreateFromGLTexture2D(cl->ctx,
CL_MEM_READ_WRITE, GL_TEXTURE_2D, 0, CL_MEM_READ_WRITE, GL_TEXTURE_2D, 0,
fosphor_gl_get_shared_id(gl, GL_ID_TEX_HISTOGRAM), fosphor_gl_get_shared_id(self, GL_ID_TEX_HISTOGRAM),
&err &err
); );
CL_ERR_CHECK(err, "Unable to share histogram texture into OpenCL context"); CL_ERR_CHECK(err, "Unable to share histogram texture into OpenCL context");
@ -445,7 +446,7 @@ cl_do_init(struct fosphor_cl_state *cl, struct fosphor_gl_state *gl)
/* Spectrum VBO */ /* Spectrum VBO */
cl->mem_spectrum = clCreateFromGLBuffer(cl->ctx, cl->mem_spectrum = clCreateFromGLBuffer(cl->ctx,
CL_MEM_WRITE_ONLY, CL_MEM_WRITE_ONLY,
fosphor_gl_get_shared_id(gl, GL_ID_VBO_SPECTRUM), fosphor_gl_get_shared_id(self, GL_ID_VBO_SPECTRUM),
&err &err
); );
CL_ERR_CHECK(err, "Unable to share spectrum VBO into OpenCL context"); CL_ERR_CHECK(err, "Unable to share spectrum VBO into OpenCL context");
@ -535,8 +536,8 @@ cl_do_release(struct fosphor_cl_state *cl)
/* Exposed API */ /* Exposed API */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
struct fosphor_cl_state * int
fosphor_cl_init(struct fosphor_gl_state *gl) fosphor_cl_init(struct fosphor *self)
{ {
struct fosphor_cl_state *cl; struct fosphor_cl_state *cl;
char dev_name[128]; char dev_name[128];
@ -545,7 +546,9 @@ fosphor_cl_init(struct fosphor_gl_state *gl)
/* Allocate structure */ /* Allocate structure */
cl = malloc(sizeof(struct fosphor_cl_state)); cl = malloc(sizeof(struct fosphor_cl_state));
if (!cl) if (!cl)
return NULL; return -ENOMEM;
self->cl = cl;
memset(cl, 0, sizeof(struct fosphor_cl_state)); memset(cl, 0, sizeof(struct fosphor_cl_state));
@ -562,23 +565,25 @@ fosphor_cl_init(struct fosphor_gl_state *gl)
fprintf(stderr, "[+] Selected device: %s\n", dev_name); fprintf(stderr, "[+] Selected device: %s\n", dev_name);
/* Initialize selected platform / device */ /* Initialize selected platform / device */
err = cl_do_init(cl, gl); err = cl_do_init(self);
if (err) if (err)
goto error; goto error;
/* Done */ /* Done */
return cl; return 0;
/* Error path */ /* Error path */
error: error:
fosphor_cl_release(cl); fosphor_cl_release(self);
return NULL; return -EIO;
} }
void void
fosphor_cl_release(struct fosphor_cl_state *cl) fosphor_cl_release(struct fosphor *self)
{ {
struct fosphor_cl_state *cl = self->cl;
/* Safety */ /* Safety */
if (!cl) if (!cl)
return; return;
@ -591,9 +596,11 @@ fosphor_cl_release(struct fosphor_cl_state *cl)
} }
int int
fosphor_cl_process(struct fosphor_cl_state *cl, fosphor_cl_process(struct fosphor *self,
void *samples, int len) void *samples, int len)
{ {
struct fosphor_cl_state *cl = self->cl;
cl_int err; cl_int err;
size_t local[2], global[2]; size_t local[2], global[2];
int n_spectra = len / FOSPHOR_FFT_LEN; int n_spectra = len / FOSPHOR_FFT_LEN;
@ -669,15 +676,19 @@ error:
} }
int int
fosphor_cl_get_waterfall_position(struct fosphor_cl_state *cl) fosphor_cl_get_waterfall_position(struct fosphor *self)
{ {
struct fosphor_cl_state *cl = self->cl;
return cl->waterfall_pos; return cl->waterfall_pos;
} }
void void
fosphor_cl_set_histogram_range(struct fosphor_cl_state *cl, fosphor_cl_set_histogram_range(struct fosphor *self,
float scale, float offset) float scale, float offset)
{ {
struct fosphor_cl_state *cl = self->cl;
cl->histo_scale = scale * 128.0f; cl->histo_scale = scale * 128.0f;
cl->histo_offset = offset; cl->histo_offset = offset;
} }

View File

@ -30,17 +30,16 @@
* \brief OpenCL base routines * \brief OpenCL base routines
*/ */
struct fosphor_gl_state; struct fosphor;
struct fosphor_cl_state;
struct fosphor_cl_state *fosphor_cl_init(struct fosphor_gl_state *gl); int fosphor_cl_init(struct fosphor *self);
void fosphor_cl_release(struct fosphor_cl_state *cl); void fosphor_cl_release(struct fosphor *self);
int fosphor_cl_process(struct fosphor_cl_state *cl, int fosphor_cl_process(struct fosphor *self,
void *samples, int len); void *samples, int len);
int fosphor_cl_get_waterfall_position(struct fosphor_cl_state *cl); int fosphor_cl_get_waterfall_position(struct fosphor *self);
void fosphor_cl_set_histogram_range(struct fosphor_cl_state *cl, void fosphor_cl_set_histogram_range(struct fosphor *self,
float scale, float offset); float scale, float offset);
/*! @} */ /*! @} */

View File

@ -32,21 +32,17 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "config.h"
#include "cl.h" #include "cl.h"
#include "gl.h" #include "gl.h"
#include "fosphor.h" #include "fosphor.h"
#include "private.h"
struct fosphor
{
struct fosphor_cl_state *cl;
struct fosphor_gl_state *gl;
};
struct fosphor * struct fosphor *
fosphor_init(void) fosphor_init(void)
{ {
struct fosphor *self; struct fosphor *self;
int rv;
/* Allocate structure */ /* Allocate structure */
self = malloc(sizeof(struct fosphor)); self = malloc(sizeof(struct fosphor));
@ -56,12 +52,12 @@ fosphor_init(void)
memset(self, 0, sizeof(struct fosphor)); memset(self, 0, sizeof(struct fosphor));
/* Init GL/CL sub-states */ /* Init GL/CL sub-states */
self->gl = fosphor_gl_init(); rv = fosphor_gl_init(self);
if (!self->gl) if (rv)
goto error; goto error;
self->cl = fosphor_cl_init(self->gl); rv = fosphor_cl_init(self);
if (!self->cl) if (rv)
goto error; goto error;
/* Initial state */ /* Initial state */
@ -81,8 +77,8 @@ fosphor_release(struct fosphor *self)
if (!self) if (!self)
return; return;
fosphor_cl_release(self->cl); fosphor_cl_release(self);
fosphor_gl_release(self->gl); fosphor_gl_release(self);
free(self); free(self);
} }
@ -90,14 +86,14 @@ fosphor_release(struct fosphor *self)
int int
fosphor_process(struct fosphor *self, void *samples, int len) fosphor_process(struct fosphor *self, void *samples, int len)
{ {
return fosphor_cl_process(self->cl, samples, len); return fosphor_cl_process(self, samples, len);
} }
void void
fosphor_draw(struct fosphor *self, int w, int h) fosphor_draw(struct fosphor *self, int w, int h)
{ {
int wf_pos = fosphor_cl_get_waterfall_position(self->cl); int wf_pos = fosphor_cl_get_waterfall_position(self);
fosphor_gl_draw(self->gl, w, h, wf_pos); fosphor_gl_draw(self, w, h, wf_pos);
} }
@ -116,8 +112,8 @@ fosphor_set_range(struct fosphor *self, int db_ref, int db_per_div)
offset = - ( k + ((float)db0 / 20.0f) ); offset = - ( k + ((float)db0 / 20.0f) );
scale = 20.0f / (float)(db1 - db0); scale = 20.0f / (float)(db1 - db0);
fosphor_cl_set_histogram_range(self->cl, scale, offset); fosphor_cl_set_histogram_range(self, scale, offset);
fosphor_gl_set_range(self->gl, scale, offset, db_ref, db_per_div); fosphor_gl_set_range(self, scale, offset, db_ref, db_per_div);
} }
/*! @} */ /*! @} */

View File

@ -27,17 +27,18 @@
* \brief OpenGL part of fosphor * \brief OpenGL part of fosphor
*/ */
#include <errno.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "gl_platform.h" #include "gl_platform.h"
#include "config.h"
#include "gl.h" #include "gl.h"
#include "gl_cmap.h" #include "gl_cmap.h"
#include "gl_cmap_gen.h" #include "gl_cmap_gen.h"
#include "gl_font.h" #include "gl_font.h"
#include "private.h"
#include "resource.h" #include "resource.h"
@ -122,8 +123,9 @@ gl_vbo_read(GLuint vbo_id, int size, void *dst)
#endif #endif
static void static void
gl_deferred_init(struct fosphor_gl_state *gl) gl_deferred_init(struct fosphor *self)
{ {
struct fosphor_gl_state *gl = self->gl;
int len; int len;
/* Prevent double init */ /* Prevent double init */
@ -176,8 +178,8 @@ gl_deferred_init(struct fosphor_gl_state *gl)
/* Exposed API */ /* Exposed API */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
struct fosphor_gl_state * int
fosphor_gl_init(void) fosphor_gl_init(struct fosphor *self)
{ {
struct fosphor_gl_state *gl; struct fosphor_gl_state *gl;
const void *font_data; const void *font_data;
@ -186,18 +188,24 @@ fosphor_gl_init(void)
/* Allocate structure */ /* Allocate structure */
gl = malloc(sizeof(struct fosphor_gl_state)); gl = malloc(sizeof(struct fosphor_gl_state));
if (!gl) if (!gl)
return NULL; return -ENOMEM;
self->gl = gl;
memset(gl, 0, sizeof(struct fosphor_gl_state)); memset(gl, 0, sizeof(struct fosphor_gl_state));
/* Font */ /* Font */
gl->font = glf_alloc(8, GLF_FLG_LCD); gl->font = glf_alloc(8, GLF_FLG_LCD);
if (!gl->font) if (!gl->font) {
rv = -ENOMEM;
goto error; goto error;
}
font_data = resource_get("DroidSansMonoDotted.ttf", &len); font_data = resource_get("DroidSansMonoDotted.ttf", &len);
if (!font_data) if (!font_data) {
rv = -ENOENT;
goto error; goto error;
}
rv = glf_load_face_mem(gl->font, font_data, len); rv = glf_load_face_mem(gl->font, font_data, len);
if (rv) if (rv)
@ -217,16 +225,19 @@ fosphor_gl_init(void)
goto error; goto error;
/* Done */ /* Done */
return gl; return 0;
error: error:
fosphor_gl_release(gl); fosphor_gl_release(self);
return NULL;
return rv;
} }
void void
fosphor_gl_release(struct fosphor_gl_state *gl) fosphor_gl_release(struct fosphor *self)
{ {
struct fosphor_gl_state *gl = self->gl;
/* Safety */ /* Safety */
if (!gl) if (!gl)
return; return;
@ -249,12 +260,14 @@ fosphor_gl_release(struct fosphor_gl_state *gl)
GLuint GLuint
fosphor_gl_get_shared_id(struct fosphor_gl_state *gl, fosphor_gl_get_shared_id(struct fosphor *self,
enum fosphor_gl_id id) enum fosphor_gl_id id)
{ {
struct fosphor_gl_state *gl = self->gl;
/* CL is not sufficiently booted to complete the GL init /* CL is not sufficiently booted to complete the GL init
* in a CL context */ * in a CL context */
gl_deferred_init(gl); gl_deferred_init(self);
/* Select ID to return */ /* Select ID to return */
switch (id) { switch (id) {
@ -273,8 +286,9 @@ fosphor_gl_get_shared_id(struct fosphor_gl_state *gl,
void void
fosphor_gl_draw(struct fosphor_gl_state *gl, int w, int h, int wf_pos) fosphor_gl_draw(struct fosphor *self, int w, int h, int wf_pos)
{ {
struct fosphor_gl_state *gl = self->gl;
float x_div_width, y_div_width; float x_div_width, y_div_width;
float x[2], y[4]; float x[2], y[4];
int i; int i;
@ -406,10 +420,12 @@ fosphor_gl_draw(struct fosphor_gl_state *gl, int w, int h, int wf_pos)
void void
fosphor_gl_set_range(struct fosphor_gl_state *gl, fosphor_gl_set_range(struct fosphor *self,
float scale, float offset, float scale, float offset,
int db_ref, int db_per_div) int db_ref, int db_per_div)
{ {
struct fosphor_gl_state *gl = self->gl;
gl->scale = scale; gl->scale = scale;
gl->offset = offset; gl->offset = offset;
gl->db_ref = db_ref; gl->db_ref = db_ref;

View File

@ -32,10 +32,10 @@
#include "gl_platform.h" #include "gl_platform.h"
struct fosphor_gl_state; struct fosphor;
struct fosphor_gl_state *fosphor_gl_init(void); int fosphor_gl_init(struct fosphor *self);
void fosphor_gl_release(struct fosphor_gl_state *gl); void fosphor_gl_release(struct fosphor *self);
enum fosphor_gl_id { enum fosphor_gl_id {
@ -44,12 +44,12 @@ enum fosphor_gl_id {
GL_ID_VBO_SPECTRUM, GL_ID_VBO_SPECTRUM,
}; };
GLuint fosphor_gl_get_shared_id(struct fosphor_gl_state *gl, GLuint fosphor_gl_get_shared_id(struct fosphor *self,
enum fosphor_gl_id id); enum fosphor_gl_id id);
void fosphor_gl_draw(struct fosphor_gl_state *gl, int w, int h, int wf_pos); void fosphor_gl_draw(struct fosphor *self, int w, int h, int wf_pos);
void fosphor_gl_set_range(struct fosphor_gl_state *gl, void fosphor_gl_set_range(struct fosphor *self,
float scale, float offset, float scale, float offset,
int db_ref, int db_per_div); int db_ref, int db_per_div);

View File

@ -16,8 +16,8 @@
#endif #endif
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include "config.h"
#include "fosphor.h" #include "fosphor.h"
#include "private.h"
struct app_state struct app_state
{ {

View File

@ -1,7 +1,7 @@
/* /*
* config.h * private.h
* *
* Global fosphor configuration constants * Private fosphor definitions
* *
* Copyright (C) 2013 Sylvain Munaut * Copyright (C) 2013 Sylvain Munaut
* *
@ -19,15 +19,15 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef __FOSPHOR_CONFIG_H__ #ifndef __FOSPHOR_PRIVATE_H__
#define __FOSPHOR_CONFIG_H__ #define __FOSPHOR_PRIVATE_H__
/*! \defgroup config /*! \defgroup private
* @{ * @{
*/ */
/*! \file config.h /*! \file private.h
* \brief Global fosphor configuration constants * \brief Private fosphor definitions
*/ */
@ -37,7 +37,16 @@
#define FOSPHOR_FFT_MULT_BATCH 16 #define FOSPHOR_FFT_MULT_BATCH 16
#define FOSPHOR_FFT_MAX_BATCH 1024 #define FOSPHOR_FFT_MAX_BATCH 1024
struct fosphor_cl_state;
struct fosphor_gl_state;
struct fosphor
{
struct fosphor_cl_state *cl;
struct fosphor_gl_state *gl;
};
/*! @} */ /*! @} */
#endif /* __FOSPHOR_CONFIG_H__ */ #endif /* __FOSPHOR_PRIVATE_H__ */