Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

C++ struct to hold GLSL shader string

27 views
Skip to first unread message

bitrex

unread,
Jun 12, 2017, 8:37:02 PM6/12/17
to
I'm interfacing with a straight-C99 API that makes some OpenGL calls. I
need to pass the source for a GLSL pixel shader in the form of an array
of char* to one of the functions. Basically in C code the shader source
is stored like this:

const char *shader_src[] =
{
"uniform sampler2D backBuffer;",
"uniform float r;",
"uniform float g;",
"uniform float b;",
"uniform float ratio;",
"void main() {",
" vec4 color;",

etc.


" gl_FragColor = color;",
"}"
};

const int source_len = number_of_lines_above;

I was hoping there was a good RAII-respectful way to pass
arbitrary-length source in this form to a class constructor and have it
held in some kind of structure, that can return a char** and length to
the API's compiler call when required.

Christian Gollwitzer

unread,
Jun 13, 2017, 4:05:47 AM6/13/17
to
Am 13.06.17 um 02:36 schrieb bitrex:
Why not std::vector<const char *> ?

&buffer[0] and buffer.size() should give you what's needed. OTOH why do
you pass individual lines and not a string for the shader?

Christian

Juha Nieminen

unread,
Jun 14, 2017, 3:37:57 AM6/14/17
to
Christian Gollwitzer <auri...@gmx.de> wrote:
> Why not std::vector<const char *> ?

If the source consists of static string literals, another more efficient
option is using std::initializer_list<const char*> instead. In this
situation it kind of works like std::vector, except that it doesn't
need to allocate any additional memory (it simply contains a pointer
and a size, or possibly two pointers, which point to the static
data.) As a bonus, you get a nicer initialization syntax.
0 new messages