From b500704008b039c549d7d4b771e9940b1641faba Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 14 Aug 2025 14:55:37 +0200 Subject: Add plugin support (-p option) --- plugin/examples/hello_triangle/triangle.c | 108 ++++++++++++++++++++++++++++++ plugin/plugin.h | 50 ++++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 plugin/examples/hello_triangle/triangle.c create mode 100644 plugin/plugin.h (limited to 'plugin') diff --git a/plugin/examples/hello_triangle/triangle.c b/plugin/examples/hello_triangle/triangle.c new file mode 100644 index 0000000..4c18f20 --- /dev/null +++ b/plugin/examples/hello_triangle/triangle.c @@ -0,0 +1,108 @@ +#include +#include +#include +#include +#define GL_GLEXT_PROTOTYPES +#include + +const char vertex_shader_source[] = + "attribute vec4 vertex_pos; \n" + "void main() { \n" + " gl_Position = vertex_pos; \n" + "}"; + +const char fragment_shader_source[] = + "precision mediump float; \n" + "uniform vec3 color; \n" + "void main() { \n" + " gl_FragColor = vec4(color, 1.0); \n" + "}"; + +typedef struct { + GLuint shader_program; + GLuint vao; + GLuint vbo; + GLint color_uniform; + unsigned int counter; +} Triangle; + +static GLuint load_shader(const char *shaderSrc, GLenum type) { + GLuint shader = glCreateShader(type); + glShaderSource(shader, 1, &shaderSrc, NULL); + glCompileShader(shader); + return shader; +} + +static void draw(const gsr_plugin_draw_params *params, void *userdata) { + Triangle *triangle = userdata; + + GLfloat glverts[6]; + + glverts[0] = -0.5f; + glverts[1] = -0.5f; + + glverts[2] = 0.5f; + glverts[3] = -0.5f; + + glverts[4] = 0.0f; + glverts[5] = 0.5f; + + glBindVertexArray(triangle->vao); + glBindBuffer(GL_ARRAY_BUFFER, triangle->vbo); + glBufferSubData(GL_ARRAY_BUFFER, 0, 6 * sizeof(float), glverts); + + glUseProgram(triangle->shader_program); + const double pp = triangle->counter * 0.05; + glUniform3f(triangle->color_uniform, 0.5 + sin(pp)*0.5, 0.5 + cos(pp)*0.5, 0.5 + sin(0.2 + pp)*0.5); + glDrawArrays(GL_TRIANGLES, 0, 3); + + glBindVertexArray(0); + glUseProgram(0); + + ++triangle->counter; +} + +bool gsr_plugin_init(const gsr_plugin_init_params *params, gsr_plugin_init_return *ret) { + Triangle *triangle = calloc(1, sizeof(Triangle)); + if(!triangle) + return false; + + triangle->shader_program = glCreateProgram(); + + const GLuint vertex_shader = load_shader(vertex_shader_source, GL_VERTEX_SHADER); + const GLuint fragment_shader = load_shader(fragment_shader_source, GL_FRAGMENT_SHADER); + + glAttachShader(triangle->shader_program, vertex_shader); + glAttachShader(triangle->shader_program, fragment_shader); + glBindAttribLocation(triangle->shader_program, 0, "vertex_pos"); + glLinkProgram(triangle->shader_program); + + glGenVertexArrays(1, &triangle->vao); + glBindVertexArray(triangle->vao); + + glGenBuffers(1, &triangle->vbo); + glBindBuffer(GL_ARRAY_BUFFER, triangle->vbo); + glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), NULL, GL_DYNAMIC_DRAW); + + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void *)0); + + glBindVertexArray(0); + + glDeleteShader(vertex_shader); + glDeleteShader(fragment_shader); + + triangle->color_uniform = glGetUniformLocation(triangle->shader_program, "color"); + + ret->name = "hello_triangle"; + ret->version = 1; + ret->userdata = triangle; + ret->draw = draw; + return true; +} + +void gsr_plugin_deinit(void *userdata) { + Triangle *triangle = userdata; + glDeleteProgram(triangle->shader_program); + free(triangle); +} diff --git a/plugin/plugin.h b/plugin/plugin.h new file mode 100644 index 0000000..bbf212d --- /dev/null +++ b/plugin/plugin.h @@ -0,0 +1,50 @@ +#ifndef GSR_PLUGIN_H +#define GSR_PLUGIN_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +typedef enum { + GSR_PLUGIN_GRAPHICS_API_EGL_ES, + GSR_PLUGIN_GRAPHICS_API_GLX, +} gsr_plugin_graphics_api; + +typedef enum { + GSR_PLUGIN_COLOR_DEPTH_8_BITS, + GSR_PLUGIN_COLOR_DEPTH_10_BITS, +} gsr_plugin_color_depth; + +typedef struct { + unsigned int width; + unsigned int height; +} gsr_plugin_draw_params; + +typedef struct { + unsigned int width; + unsigned int height; + unsigned int fps; + gsr_plugin_color_depth color_depth; + gsr_plugin_graphics_api graphics_api; +} gsr_plugin_init_params; + +typedef struct { + const char *name; /* Mandatory */ + unsigned int version; /* Mandatory, can't be 0 */ + void *userdata; /* Optional */ + + /* Optional, called when the plugin is expected to draw something to the current framebuffer */ + void (*draw)(const gsr_plugin_draw_params *params, void *userdata); +} gsr_plugin_init_return; + +/* The plugin is expected to implement these functions and export them: */ +bool gsr_plugin_init(const gsr_plugin_init_params *params, gsr_plugin_init_return *ret); +void gsr_plugin_deinit(void *userdata); + +#ifdef __cplusplus +} +#endif + +#endif /* GSR_PLUGIN_H */ -- cgit v1.2.3