ssbo impl

main
Dominik Madarász 2023-09-08 10:00:26 +02:00
parent 34c58f04a9
commit 5af4968eed
10 changed files with 315 additions and 25 deletions

View File

@ -5,16 +5,30 @@ int main() {
window_create(50, WINDOW_SQUARE); window_create(50, WINDOW_SQUARE);
window_title(__FILE__); window_title(__FILE__);
unsigned TEX_WIDTH = 1000;
unsigned comp = compute(vfs_read("shaders/compute-test.glsl")); unsigned comp = compute(vfs_read("shaders/compute-test.glsl"));
texture_t tex = texture_create(512, 512, 4, 0, TEXTURE_LINEAR|TEXTURE_FLOAT); texture_t tex = texture_create(TEX_WIDTH, TEX_WIDTH, 4, 0, TEXTURE_LINEAR|TEXTURE_FLOAT);
shader_bind(comp);
shader_image(tex, 0, 0, 0, READ);
struct {
float f;
} data;
unsigned buf = ssbo_create(&data, sizeof(data), STREAM_DRAW);
while ( window_swap() && !input_down(KEY_ESC) ){ while ( window_swap() && !input_down(KEY_ESC) ){
if (input(KEY_F5)) window_reload(); if (input(KEY_F5)) window_reload();
shader_bind(comp); shader_bind(comp);
shader_float("t", (float)window_time()); data.f = (float)window_time();
shader_image(tex, 0, 0, -1, READ); ssbo_bind(buf, 1);
dispatch(512, 512, 1); ssbo_update(0, sizeof(data), &data);
dispatch(TEX_WIDTH/10, TEX_WIDTH/10, 1);
image_write_barrier(); image_write_barrier();
fullscreen_quad_rgb(tex, 2.2); fullscreen_quad_rgb(tex, 2.2);
} }

View File

@ -2,7 +2,9 @@ layout (local_size_x = 10, local_size_y = 10, local_size_z = 1) in;
layout(rgba32f, binding = 0) uniform image2D imgOutput; layout(rgba32f, binding = 0) uniform image2D imgOutput;
layout (location = 0) uniform float t; /** Time */ layout (binding = 1) buffer ssbo_data {
float t;
};
void main() { void main() {
vec4 value = vec4(0.0, 0.0, 0.0, 1.0); vec4 value = vec4(0.0, 0.0, 0.0, 1.0);

View File

@ -2370,6 +2370,24 @@ READ_WRITE
void shader_image_unit(unsigned texture, unsigned unit, unsigned level, int layer , unsigned texel_type, unsigned access); void shader_image_unit(unsigned texture, unsigned unit, unsigned level, int layer , unsigned texel_type, unsigned access);
void image_write_barrier(); void image_write_barrier();
void write_barrier(); void write_barrier();
enum USAGE_MODE {
STATIC_DRAW,
STATIC_READ,
STATIC_COPY,
DYNAMIC_DRAW,
DYNAMIC_READ,
DYNAMIC_COPY,
STREAM_DRAW,
STREAM_READ,
STREAM_COPY
};
unsigned ssbo_create(const void *data, int len, unsigned usage);
void ssbo_destroy(unsigned ssbo);
void ssbo_update(int offset, int len, const void *data);
void ssbo_bind(unsigned ssbo, unsigned unit);
void *ssbo_map(unsigned access);
void ssbo_unmap();
void ssbo_unbind();
enum MESH_FLAGS { enum MESH_FLAGS {
MESH_STATIC = 0, MESH_STATIC = 0,
MESH_STREAM = 1, MESH_STREAM = 1,

View File

@ -16496,6 +16496,29 @@ API void shader_image_unit(unsigned texture, unsigned unit, unsigned level, int
API void image_write_barrier(); API void image_write_barrier();
API void write_barrier(); API void write_barrier();
// ssbo
enum USAGE_MODE {
STATIC_DRAW,
STATIC_READ,
STATIC_COPY,
DYNAMIC_DRAW,
DYNAMIC_READ,
DYNAMIC_COPY,
STREAM_DRAW,
STREAM_READ,
STREAM_COPY
};
API unsigned ssbo_create(const void *data, int len, unsigned usage);
API void ssbo_destroy(unsigned ssbo);
API void ssbo_update(int offset, int len, const void *data);
API void ssbo_bind(unsigned ssbo, unsigned unit);
API void *ssbo_map(unsigned access);
API void ssbo_unmap();
API void ssbo_unbind();
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// meshes (@fixme: deprecate?) // meshes (@fixme: deprecate?)
@ -340115,7 +340138,7 @@ unsigned compute(const char *cs){
#else #else
PRINTF(/*"!"*/"Compiling compute shader\n"); PRINTF(/*"!"*/"Compiling compute shader\n");
cs = cs[0] == '#' && cs[1] == 'c' ? cs : va("#version 430 core\n%s", cs ? cs : ""); cs = cs[0] == '#' && cs[1] == 'c' ? cs : va("#version 450 core\n%s", cs ? cs : "");
GLuint comp = shader_compile(GL_COMPUTE_SHADER, cs); GLuint comp = shader_compile(GL_COMPUTE_SHADER, cs);
GLuint program = 0; GLuint program = 0;
@ -340169,6 +340192,40 @@ void shader_destroy(unsigned program){
// if(s->name) FREE(s->name), s->name = NULL; // if(s->name) FREE(s->name), s->name = NULL;
} }
unsigned ssbo_create(const void *data, int len, unsigned usage){
static GLuint gl_usage[] = { GL_STATIC_DRAW, GL_STATIC_READ, GL_STATIC_COPY, GL_DYNAMIC_DRAW, GL_DYNAMIC_READ, GL_DYNAMIC_COPY, GL_STREAM_DRAW, GL_STREAM_READ, GL_STREAM_COPY };
GLuint ssbo;
glGenBuffers(1, &ssbo);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);
glBufferData(GL_SHADER_STORAGE_BUFFER, len, data, gl_usage[usage]);
return ssbo;
}
void ssbo_destroy(unsigned ssbo){
glDeleteBuffers(1, &ssbo);
}
void ssbo_update(int offset, int len, const void *data){
glBufferSubData(GL_SHADER_STORAGE_BUFFER, offset, len, data);
}
void *ssbo_map(unsigned access){
static GLenum gl_access[] = {GL_READ_ONLY, GL_WRITE_ONLY, GL_READ_WRITE};
return glMapBuffer(GL_SHADER_STORAGE_BUFFER, gl_access[access]);
}
void ssbo_unmap(){
glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
}
void ssbo_bind(unsigned ssbo, unsigned unit){
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, unit, ssbo);
}
void ssbo_unbind(){
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
}
static __thread unsigned last_shader = -1; static __thread unsigned last_shader = -1;
static static
int shader_uniform(const char *name) { int shader_uniform(const char *name) {
@ -340199,7 +340256,7 @@ void shader_image(texture_t t, unsigned unit, unsigned level, int layer /* -1 to
shader_image_unit(t.id, unit, level, layer, t.texel_type, access); shader_image_unit(t.id, unit, level, layer, t.texel_type, access);
} }
void shader_image_unit(unsigned texture, unsigned unit, unsigned level, int layer, unsigned texel_type, unsigned access){ void shader_image_unit(unsigned texture, unsigned unit, unsigned level, int layer, unsigned texel_type, unsigned access){
GLenum gl_access[] = {GL_READ_ONLY, GL_WRITE_ONLY, GL_READ_WRITE}; static GLenum gl_access[] = {GL_READ_ONLY, GL_WRITE_ONLY, GL_READ_WRITE};
glBindImageTexture(unit, texture, level, layer!=-1, layer!=-1?layer:0, gl_access[access], texel_type); glBindImageTexture(unit, texture, level, layer!=-1, layer!=-1?layer:0, gl_access[access], texel_type);
} }
@ -349545,7 +349602,7 @@ void window_hints(unsigned flags) {
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); // osx, 2:#version150,3:330 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); // osx, 2:#version150,3:330
#else #else
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
#endif #endif
#ifdef __APPLE__ #ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); //osx glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); //osx

View File

@ -200,7 +200,7 @@ unsigned compute(const char *cs){
#else #else
PRINTF(/*"!"*/"Compiling compute shader\n"); PRINTF(/*"!"*/"Compiling compute shader\n");
cs = cs[0] == '#' && cs[1] == 'c' ? cs : va("#version 430 core\n%s", cs ? cs : ""); cs = cs[0] == '#' && cs[1] == 'c' ? cs : va("#version 450 core\n%s", cs ? cs : "");
GLuint comp = shader_compile(GL_COMPUTE_SHADER, cs); GLuint comp = shader_compile(GL_COMPUTE_SHADER, cs);
GLuint program = 0; GLuint program = 0;
@ -254,6 +254,40 @@ void shader_destroy(unsigned program){
// if(s->name) FREE(s->name), s->name = NULL; // if(s->name) FREE(s->name), s->name = NULL;
} }
unsigned ssbo_create(const void *data, int len, unsigned usage){
static GLuint gl_usage[] = { GL_STATIC_DRAW, GL_STATIC_READ, GL_STATIC_COPY, GL_DYNAMIC_DRAW, GL_DYNAMIC_READ, GL_DYNAMIC_COPY, GL_STREAM_DRAW, GL_STREAM_READ, GL_STREAM_COPY };
GLuint ssbo;
glGenBuffers(1, &ssbo);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);
glBufferData(GL_SHADER_STORAGE_BUFFER, len, data, gl_usage[usage]);
return ssbo;
}
void ssbo_destroy(unsigned ssbo){
glDeleteBuffers(1, &ssbo);
}
void ssbo_update(int offset, int len, const void *data){
glBufferSubData(GL_SHADER_STORAGE_BUFFER, offset, len, data);
}
void *ssbo_map(unsigned access){
static GLenum gl_access[] = {GL_READ_ONLY, GL_WRITE_ONLY, GL_READ_WRITE};
return glMapBuffer(GL_SHADER_STORAGE_BUFFER, gl_access[access]);
}
void ssbo_unmap(){
glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
}
void ssbo_bind(unsigned ssbo, unsigned unit){
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, unit, ssbo);
}
void ssbo_unbind(){
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
}
static __thread unsigned last_shader = -1; static __thread unsigned last_shader = -1;
static static
int shader_uniform(const char *name) { int shader_uniform(const char *name) {
@ -284,7 +318,7 @@ void shader_image(texture_t t, unsigned unit, unsigned level, int layer /* -1 to
shader_image_unit(t.id, unit, level, layer, t.texel_type, access); shader_image_unit(t.id, unit, level, layer, t.texel_type, access);
} }
void shader_image_unit(unsigned texture, unsigned unit, unsigned level, int layer, unsigned texel_type, unsigned access){ void shader_image_unit(unsigned texture, unsigned unit, unsigned level, int layer, unsigned texel_type, unsigned access){
GLenum gl_access[] = {GL_READ_ONLY, GL_WRITE_ONLY, GL_READ_WRITE}; static GLenum gl_access[] = {GL_READ_ONLY, GL_WRITE_ONLY, GL_READ_WRITE};
glBindImageTexture(unit, texture, level, layer!=-1, layer!=-1?layer:0, gl_access[access], texel_type); glBindImageTexture(unit, texture, level, layer!=-1, layer!=-1?layer:0, gl_access[access], texel_type);
} }

View File

@ -341,6 +341,29 @@ API void shader_image_unit(unsigned texture, unsigned unit, unsigned level, int
API void image_write_barrier(); API void image_write_barrier();
API void write_barrier(); API void write_barrier();
// ssbo
enum USAGE_MODE {
STATIC_DRAW,
STATIC_READ,
STATIC_COPY,
DYNAMIC_DRAW,
DYNAMIC_READ,
DYNAMIC_COPY,
STREAM_DRAW,
STREAM_READ,
STREAM_COPY
};
API unsigned ssbo_create(const void *data, int len, unsigned usage);
API void ssbo_destroy(unsigned ssbo);
API void ssbo_update(int offset, int len, const void *data);
API void ssbo_bind(unsigned ssbo, unsigned unit);
API void *ssbo_map(unsigned access);
API void ssbo_unmap();
API void ssbo_unbind();
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// meshes (@fixme: deprecate?) // meshes (@fixme: deprecate?)

View File

@ -175,7 +175,7 @@ void window_hints(unsigned flags) {
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); // osx, 2:#version150,3:330 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); // osx, 2:#version150,3:330
#else #else
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
#endif #endif
#ifdef __APPLE__ #ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); //osx glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); //osx

View File

@ -11114,7 +11114,7 @@ unsigned compute(const char *cs){
#else #else
PRINTF(/*"!"*/"Compiling compute shader\n"); PRINTF(/*"!"*/"Compiling compute shader\n");
cs = cs[0] == '#' && cs[1] == 'c' ? cs : va("#version 430 core\n%s", cs ? cs : ""); cs = cs[0] == '#' && cs[1] == 'c' ? cs : va("#version 450 core\n%s", cs ? cs : "");
GLuint comp = shader_compile(GL_COMPUTE_SHADER, cs); GLuint comp = shader_compile(GL_COMPUTE_SHADER, cs);
GLuint program = 0; GLuint program = 0;
@ -11168,6 +11168,40 @@ void shader_destroy(unsigned program){
// if(s->name) FREE(s->name), s->name = NULL; // if(s->name) FREE(s->name), s->name = NULL;
} }
unsigned ssbo_create(const void *data, int len, unsigned usage){
static GLuint gl_usage[] = { GL_STATIC_DRAW, GL_STATIC_READ, GL_STATIC_COPY, GL_DYNAMIC_DRAW, GL_DYNAMIC_READ, GL_DYNAMIC_COPY, GL_STREAM_DRAW, GL_STREAM_READ, GL_STREAM_COPY };
GLuint ssbo;
glGenBuffers(1, &ssbo);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);
glBufferData(GL_SHADER_STORAGE_BUFFER, len, data, gl_usage[usage]);
return ssbo;
}
void ssbo_destroy(unsigned ssbo){
glDeleteBuffers(1, &ssbo);
}
void ssbo_update(int offset, int len, const void *data){
glBufferSubData(GL_SHADER_STORAGE_BUFFER, offset, len, data);
}
void *ssbo_map(unsigned access){
static GLenum gl_access[] = {GL_READ_ONLY, GL_WRITE_ONLY, GL_READ_WRITE};
return glMapBuffer(GL_SHADER_STORAGE_BUFFER, gl_access[access]);
}
void ssbo_unmap(){
glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
}
void ssbo_bind(unsigned ssbo, unsigned unit){
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, unit, ssbo);
}
void ssbo_unbind(){
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
}
static __thread unsigned last_shader = -1; static __thread unsigned last_shader = -1;
static static
int shader_uniform(const char *name) { int shader_uniform(const char *name) {
@ -11198,7 +11232,7 @@ void shader_image(texture_t t, unsigned unit, unsigned level, int layer /* -1 to
shader_image_unit(t.id, unit, level, layer, t.texel_type, access); shader_image_unit(t.id, unit, level, layer, t.texel_type, access);
} }
void shader_image_unit(unsigned texture, unsigned unit, unsigned level, int layer, unsigned texel_type, unsigned access){ void shader_image_unit(unsigned texture, unsigned unit, unsigned level, int layer, unsigned texel_type, unsigned access){
GLenum gl_access[] = {GL_READ_ONLY, GL_WRITE_ONLY, GL_READ_WRITE}; static GLenum gl_access[] = {GL_READ_ONLY, GL_WRITE_ONLY, GL_READ_WRITE};
glBindImageTexture(unit, texture, level, layer!=-1, layer!=-1?layer:0, gl_access[access], texel_type); glBindImageTexture(unit, texture, level, layer!=-1, layer!=-1?layer:0, gl_access[access], texel_type);
} }
@ -20544,7 +20578,7 @@ void window_hints(unsigned flags) {
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); // osx, 2:#version150,3:330 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); // osx, 2:#version150,3:330
#else #else
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
#endif #endif
#ifdef __APPLE__ #ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); //osx glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); //osx

View File

@ -2579,6 +2579,29 @@ API void shader_image_unit(unsigned texture, unsigned unit, unsigned level, int
API void image_write_barrier(); API void image_write_barrier();
API void write_barrier(); API void write_barrier();
// ssbo
enum USAGE_MODE {
STATIC_DRAW,
STATIC_READ,
STATIC_COPY,
DYNAMIC_DRAW,
DYNAMIC_READ,
DYNAMIC_COPY,
STREAM_DRAW,
STREAM_READ,
STREAM_COPY
};
API unsigned ssbo_create(const void *data, int len, unsigned usage);
API void ssbo_destroy(unsigned ssbo);
API void ssbo_update(int offset, int len, const void *data);
API void ssbo_bind(unsigned ssbo, unsigned unit);
API void *ssbo_map(unsigned access);
API void ssbo_unmap();
API void ssbo_unbind();
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// meshes (@fixme: deprecate?) // meshes (@fixme: deprecate?)

File diff suppressed because one or more lines are too long