geom shader support

main
Dominik Madarász 2023-09-08 11:19:33 +02:00
parent 5af4968eed
commit cf45869d14
13 changed files with 177 additions and 83 deletions

View File

@ -37,6 +37,7 @@
- [x] Render: 3D Debugdraw, batching and vectorial font.
- [x] Render: 2D Sprites, spritesheets, AA zooming and batching.
- [x] Render: 2D Tilemaps and tilesets: TMX, TSX.
- [x] Render: Compute shaders and SSBO support.
- [x] Compression: DEFLATE, LZMA, LZ4, ULZ, BALZ, BCM, CRUSH, LZW3, LZSS and PPP.
- [x] Virtual filesystem: ZIP, PAK, TAR and DIR.
- [x] Level data: JSON, JSON5, SJSON, XML, INI.

28
demos/99-geom.c 100644
View File

@ -0,0 +1,28 @@
#include "v4k.h"
int main() {
// 75% sized, MSAAx2
window_create(25, WINDOW_SQUARE);
window_title(__FILE__);
unsigned program = shader_geom(vfs_read("shaders/geom-test/line.glsl"), vfs_read("shaders/geom-test/line-vs.glsl"), vfs_read("shaders/geom-test/line-fs.glsl"), "aPos", "FragColor");
float points[] = {
-0.5f, 0.5f, // top-left
0.5f, 0.5f, // top-right
0.5f, -0.5f, // bottom-right
-0.5f, -0.5f // bottom-left
};
mesh_t m = mesh();
mesh_update(&m, "p2", 0, 4, points, 0, 0, 0);
while ( window_swap() && !input_down(KEY_ESC) ){
if (input(KEY_F5)) window_reload();
shader_bind(program);
mesh_render_prim(&m, GL_POINTS);
}
return 0;
}

View File

@ -0,0 +1,7 @@
#version 330 core
out vec4 FragColor;
void main()
{
FragColor = vec4(0.0, 1.0, 0.0, 1.0);
}

View File

@ -0,0 +1,7 @@
#version 330 core
layout (location = 0) in vec2 aPos;
void main()
{
gl_Position = vec4(aPos.x, aPos.y, 0.0, 1.0);
}

View File

@ -0,0 +1,13 @@
#version 330 core
layout (points) in;
layout (line_strip, max_vertices = 2) out;
void main() {
gl_Position = gl_in[0].gl_Position + vec4(-0.1, 0.0, 0.0, 0.0);
EmitVertex();
gl_Position = gl_in[0].gl_Position + vec4( 0.1, 0.0, 0.0, 0.0);
EmitVertex();
EndPrimitive();
}

View File

@ -2344,6 +2344,7 @@ int texture_width;
void shadowmatrix_proj(mat44 shm_proj, float aLightFov, float znear, float zfar);
void shadowmatrix_ortho(mat44 shm_proj, float left, float right, float bottom, float top, float znear, float zfar);
unsigned shader(const char *vs, const char *fs, const char *attribs, const char *fragcolor);
unsigned shader_geom(const char *gs, const char *vs, const char *fs, const char *attribs, const char *fragcolor);
unsigned shader_bind(unsigned program);
void shader_bool(const char *uniform, bool i );
void shader_int(const char *uniform, int i);
@ -2419,6 +2420,7 @@ vec3* out_vertex3;
mesh_t mesh();
void mesh_update(mesh_t *m, const char *format, int vertex_stride,int vertex_count,const void *interleaved_vertex_data, int index_count,const void *index_data, int flags);
void mesh_render(mesh_t *m);
void mesh_render_prim(mesh_t *sm, unsigned prim);
void mesh_destroy(mesh_t *m);
aabb mesh_bounds(mesh_t *m);
enum MATERIAL_ENUMS {

View File

@ -16466,6 +16466,7 @@ API void shadowmatrix_ortho(mat44 shm_proj, float left, float right, float botto
// shaders
API unsigned shader(const char *vs, const char *fs, const char *attribs, const char *fragcolor);
API unsigned shader_geom(const char *gs, const char *vs, const char *fs, const char *attribs, const char *fragcolor);
API unsigned shader_bind(unsigned program);
API void shader_bool(const char *uniform, bool i );
API void shader_int(const char *uniform, int i);
@ -16558,6 +16559,7 @@ typedef struct mesh_t {
API mesh_t mesh();
API void mesh_update(mesh_t *m, const char *format, int vertex_stride,int vertex_count,const void *interleaved_vertex_data, int index_count,const void *index_data, int flags);
API void mesh_render(mesh_t *m);
API void mesh_render_prim(mesh_t *sm, unsigned prim);
API void mesh_destroy(mesh_t *m);
API aabb mesh_bounds(mesh_t *m);
@ -340023,43 +340025,32 @@ GLuint shader_compile( GLenum type, const char *source ) {
return shader;
}
unsigned shader(const char *vs, const char *fs, const char *attribs, const char *fragcolor){
return shader_geom(NULL, vs, fs, attribs, fragcolor);
}
unsigned shader(const char *vs, const char *fs, const char *attribs, const char *fragcolor) {
unsigned shader_geom(const char *gs, const char *vs, const char *fs, const char *attribs, const char *fragcolor) {
PRINTF(/*"!"*/"Compiling shader\n");
//char *vs = vfs_read(file_vs); if(!vs) vs = (char*)file_vs;
//char *fs = vfs_read(file_fs); if(!fs) fs = (char*)file_fs;
const char *glsl_version = ifdef(ems, "300 es", "150");
vs = vs[0] == '#' && vs[1] == 'v' ? vs : va("#version %s\n%s", glsl_version, vs ? vs : "");
fs = fs[0] == '#' && fs[1] == 'v' ? fs : va("#version %s\n%s", glsl_version, fs ? fs : "");
if (gs) gs = gs[0] == '#' && gs[1] == 'v' ? gs : va("#version %s\n%s", glsl_version, gs ? gs : "");
#if is(ems)
{
char *vs_ = REALLOC( 0, strlen(vs) + 512 ); strcpy(vs_, vs);
char *fs_ = REALLOC( 0, strlen(fs) + 512 ); strcpy(fs_, fs);
//strrepl(&vs_, "\nin ", "\nattribute ");
//strrepl(&vs_, "\nout ", "\nvarying ");
char *gs_ = 0; if (gs) REALLOC( 0, strlen(gs) + 512 ); strcpy(gs_, gs);
strrepl(&fs_, "#version 300 es\n", "#version 300 es\nprecision mediump float;\n");
//strrepl(&fs_, "\nin ", "\nattribute ");
//strrepl(&fs_, "\nout ", "\nvarying ");
//strrepl(&fs_, "FRAGCOLOR", "gl_FragColor");
//strrepl(&fs_, "fragcolor", "gl_FragColor" );
//strrepl(&fs_, "fragColor", "gl_FragColor" );
#if 0
//strrepl(&fs_, "outcolor", "gl_FragColor" );
//strrepl(&fs_, "outColor", "gl_FragColor" );
#endif
//strrepl(&fs_, "out vec4 gl_FragColor", "//out vec4 outcolor");
vs = vs_; fs = fs_;
vs = vs_; fs = fs_; gs = gs_;
}
#endif
GLuint vert = shader_compile(GL_VERTEX_SHADER, vs);
GLuint frag = shader_compile(GL_FRAGMENT_SHADER, fs);
//GLuint geom = shader_compile(GL_GEOMETRY_SHADER, gs);
GLuint geom = 0; if (gs) geom = shader_compile(GL_GEOMETRY_SHADER, gs);
GLuint program = 0;
if( vert && frag ) {
@ -340067,7 +340058,7 @@ unsigned shader(const char *vs, const char *fs, const char *attribs, const char
glAttachShader(program, vert);
glAttachShader(program, frag);
// glAttachShader(program, geom);
if (geom) glAttachShader(program, geom);
for( int i = 0; attribs && attribs[0]; ++i ) {
char attrib[128] = {0};
@ -340101,19 +340092,19 @@ unsigned shader(const char *vs, const char *fs, const char *attribs, const char
shader_print(vs);
puts("--- fs:");
shader_print(fs);
if (geom) {
puts("--- gs:");
shader_print(gs);
}
}
if (status == GL_FALSE) {
PANIC("ERROR: shader(): Shader/program link: %s\n", buf);
return 0;
}
// glDetachShader(program, vert);
// glDetachShader(program, frag);
// glDetachShader(program, geom);
glDeleteShader(vert);
glDeleteShader(frag);
// glDeleteShader(geom);
if (geom) glDeleteShader(geom);
//#ifdef DEBUG_ANY_SHADER
// PRINTF("Shader #%d:\n", program);
@ -342501,6 +342492,22 @@ void mesh_render(mesh_t *sm) {
}
}
void mesh_render_prim(mesh_t *sm, unsigned prim) {
if( sm->vao ) {
glBindVertexArray(sm->vao);
if( sm->ibo ) { // with indices
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, sm->ibo); // <-- why intel?
glDrawElements(prim, sm->index_count, GL_UNSIGNED_INT, (char*)0);
profile_incstat("Render.num_drawcalls", +1);
profile_incstat("Render.num_triangles", sm->index_count/3);
} else { // with vertices only
glDrawArrays(prim, 0, sm->vertex_count /* / 3 */);
profile_incstat("Render.num_drawcalls", +1);
profile_incstat("Render.num_triangles", sm->vertex_count/3);
}
}
}
void mesh_destroy(mesh_t *m) {
// @todo
(void)m;

View File

@ -85,43 +85,32 @@ GLuint shader_compile( GLenum type, const char *source ) {
return shader;
}
unsigned shader(const char *vs, const char *fs, const char *attribs, const char *fragcolor){
return shader_geom(NULL, vs, fs, attribs, fragcolor);
}
unsigned shader(const char *vs, const char *fs, const char *attribs, const char *fragcolor) {
unsigned shader_geom(const char *gs, const char *vs, const char *fs, const char *attribs, const char *fragcolor) {
PRINTF(/*"!"*/"Compiling shader\n");
//char *vs = vfs_read(file_vs); if(!vs) vs = (char*)file_vs;
//char *fs = vfs_read(file_fs); if(!fs) fs = (char*)file_fs;
const char *glsl_version = ifdef(ems, "300 es", "150");
vs = vs[0] == '#' && vs[1] == 'v' ? vs : va("#version %s\n%s", glsl_version, vs ? vs : "");
fs = fs[0] == '#' && fs[1] == 'v' ? fs : va("#version %s\n%s", glsl_version, fs ? fs : "");
if (gs) gs = gs[0] == '#' && gs[1] == 'v' ? gs : va("#version %s\n%s", glsl_version, gs ? gs : "");
#if is(ems)
{
char *vs_ = REALLOC( 0, strlen(vs) + 512 ); strcpy(vs_, vs);
char *fs_ = REALLOC( 0, strlen(fs) + 512 ); strcpy(fs_, fs);
//strrepl(&vs_, "\nin ", "\nattribute ");
//strrepl(&vs_, "\nout ", "\nvarying ");
char *gs_ = 0; if (gs) REALLOC( 0, strlen(gs) + 512 ); strcpy(gs_, gs);
strrepl(&fs_, "#version 300 es\n", "#version 300 es\nprecision mediump float;\n");
//strrepl(&fs_, "\nin ", "\nattribute ");
//strrepl(&fs_, "\nout ", "\nvarying ");
//strrepl(&fs_, "FRAGCOLOR", "gl_FragColor");
//strrepl(&fs_, "fragcolor", "gl_FragColor" );
//strrepl(&fs_, "fragColor", "gl_FragColor" );
#if 0
//strrepl(&fs_, "outcolor", "gl_FragColor" );
//strrepl(&fs_, "outColor", "gl_FragColor" );
#endif
//strrepl(&fs_, "out vec4 gl_FragColor", "//out vec4 outcolor");
vs = vs_; fs = fs_;
vs = vs_; fs = fs_; gs = gs_;
}
#endif
GLuint vert = shader_compile(GL_VERTEX_SHADER, vs);
GLuint frag = shader_compile(GL_FRAGMENT_SHADER, fs);
//GLuint geom = shader_compile(GL_GEOMETRY_SHADER, gs);
GLuint geom = 0; if (gs) geom = shader_compile(GL_GEOMETRY_SHADER, gs);
GLuint program = 0;
if( vert && frag ) {
@ -129,7 +118,7 @@ unsigned shader(const char *vs, const char *fs, const char *attribs, const char
glAttachShader(program, vert);
glAttachShader(program, frag);
// glAttachShader(program, geom);
if (geom) glAttachShader(program, geom);
for( int i = 0; attribs && attribs[0]; ++i ) {
char attrib[128] = {0};
@ -163,19 +152,19 @@ unsigned shader(const char *vs, const char *fs, const char *attribs, const char
shader_print(vs);
puts("--- fs:");
shader_print(fs);
if (geom) {
puts("--- gs:");
shader_print(gs);
}
}
if (status == GL_FALSE) {
PANIC("ERROR: shader(): Shader/program link: %s\n", buf);
return 0;
}
// glDetachShader(program, vert);
// glDetachShader(program, frag);
// glDetachShader(program, geom);
glDeleteShader(vert);
glDeleteShader(frag);
// glDeleteShader(geom);
if (geom) glDeleteShader(geom);
//#ifdef DEBUG_ANY_SHADER
// PRINTF("Shader #%d:\n", program);
@ -2563,6 +2552,22 @@ void mesh_render(mesh_t *sm) {
}
}
void mesh_render_prim(mesh_t *sm, unsigned prim) {
if( sm->vao ) {
glBindVertexArray(sm->vao);
if( sm->ibo ) { // with indices
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, sm->ibo); // <-- why intel?
glDrawElements(prim, sm->index_count, GL_UNSIGNED_INT, (char*)0);
profile_incstat("Render.num_drawcalls", +1);
profile_incstat("Render.num_triangles", sm->index_count/3);
} else { // with vertices only
glDrawArrays(prim, 0, sm->vertex_count /* / 3 */);
profile_incstat("Render.num_drawcalls", +1);
profile_incstat("Render.num_triangles", sm->vertex_count/3);
}
}
}
void mesh_destroy(mesh_t *m) {
// @todo
(void)m;

View File

@ -311,6 +311,7 @@ API void shadowmatrix_ortho(mat44 shm_proj, float left, float right, float botto
// shaders
API unsigned shader(const char *vs, const char *fs, const char *attribs, const char *fragcolor);
API unsigned shader_geom(const char *gs, const char *vs, const char *fs, const char *attribs, const char *fragcolor);
API unsigned shader_bind(unsigned program);
API void shader_bool(const char *uniform, bool i );
API void shader_int(const char *uniform, int i);
@ -403,6 +404,7 @@ typedef struct mesh_t {
API mesh_t mesh();
API void mesh_update(mesh_t *m, const char *format, int vertex_stride,int vertex_count,const void *interleaved_vertex_data, int index_count,const void *index_data, int flags);
API void mesh_render(mesh_t *m);
API void mesh_render_prim(mesh_t *sm, unsigned prim);
API void mesh_destroy(mesh_t *m);
API aabb mesh_bounds(mesh_t *m);

View File

@ -10999,43 +10999,32 @@ GLuint shader_compile( GLenum type, const char *source ) {
return shader;
}
unsigned shader(const char *vs, const char *fs, const char *attribs, const char *fragcolor){
return shader_geom(NULL, vs, fs, attribs, fragcolor);
}
unsigned shader(const char *vs, const char *fs, const char *attribs, const char *fragcolor) {
unsigned shader_geom(const char *gs, const char *vs, const char *fs, const char *attribs, const char *fragcolor) {
PRINTF(/*"!"*/"Compiling shader\n");
//char *vs = vfs_read(file_vs); if(!vs) vs = (char*)file_vs;
//char *fs = vfs_read(file_fs); if(!fs) fs = (char*)file_fs;
const char *glsl_version = ifdef(ems, "300 es", "150");
vs = vs[0] == '#' && vs[1] == 'v' ? vs : va("#version %s\n%s", glsl_version, vs ? vs : "");
fs = fs[0] == '#' && fs[1] == 'v' ? fs : va("#version %s\n%s", glsl_version, fs ? fs : "");
if (gs) gs = gs[0] == '#' && gs[1] == 'v' ? gs : va("#version %s\n%s", glsl_version, gs ? gs : "");
#if is(ems)
{
char *vs_ = REALLOC( 0, strlen(vs) + 512 ); strcpy(vs_, vs);
char *fs_ = REALLOC( 0, strlen(fs) + 512 ); strcpy(fs_, fs);
//strrepl(&vs_, "\nin ", "\nattribute ");
//strrepl(&vs_, "\nout ", "\nvarying ");
char *gs_ = 0; if (gs) REALLOC( 0, strlen(gs) + 512 ); strcpy(gs_, gs);
strrepl(&fs_, "#version 300 es\n", "#version 300 es\nprecision mediump float;\n");
//strrepl(&fs_, "\nin ", "\nattribute ");
//strrepl(&fs_, "\nout ", "\nvarying ");
//strrepl(&fs_, "FRAGCOLOR", "gl_FragColor");
//strrepl(&fs_, "fragcolor", "gl_FragColor" );
//strrepl(&fs_, "fragColor", "gl_FragColor" );
#if 0
//strrepl(&fs_, "outcolor", "gl_FragColor" );
//strrepl(&fs_, "outColor", "gl_FragColor" );
#endif
//strrepl(&fs_, "out vec4 gl_FragColor", "//out vec4 outcolor");
vs = vs_; fs = fs_;
vs = vs_; fs = fs_; gs = gs_;
}
#endif
GLuint vert = shader_compile(GL_VERTEX_SHADER, vs);
GLuint frag = shader_compile(GL_FRAGMENT_SHADER, fs);
//GLuint geom = shader_compile(GL_GEOMETRY_SHADER, gs);
GLuint geom = 0; if (gs) geom = shader_compile(GL_GEOMETRY_SHADER, gs);
GLuint program = 0;
if( vert && frag ) {
@ -11043,7 +11032,7 @@ unsigned shader(const char *vs, const char *fs, const char *attribs, const char
glAttachShader(program, vert);
glAttachShader(program, frag);
// glAttachShader(program, geom);
if (geom) glAttachShader(program, geom);
for( int i = 0; attribs && attribs[0]; ++i ) {
char attrib[128] = {0};
@ -11077,19 +11066,19 @@ unsigned shader(const char *vs, const char *fs, const char *attribs, const char
shader_print(vs);
puts("--- fs:");
shader_print(fs);
if (geom) {
puts("--- gs:");
shader_print(gs);
}
}
if (status == GL_FALSE) {
PANIC("ERROR: shader(): Shader/program link: %s\n", buf);
return 0;
}
// glDetachShader(program, vert);
// glDetachShader(program, frag);
// glDetachShader(program, geom);
glDeleteShader(vert);
glDeleteShader(frag);
// glDeleteShader(geom);
if (geom) glDeleteShader(geom);
//#ifdef DEBUG_ANY_SHADER
// PRINTF("Shader #%d:\n", program);
@ -13477,6 +13466,22 @@ void mesh_render(mesh_t *sm) {
}
}
void mesh_render_prim(mesh_t *sm, unsigned prim) {
if( sm->vao ) {
glBindVertexArray(sm->vao);
if( sm->ibo ) { // with indices
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, sm->ibo); // <-- why intel?
glDrawElements(prim, sm->index_count, GL_UNSIGNED_INT, (char*)0);
profile_incstat("Render.num_drawcalls", +1);
profile_incstat("Render.num_triangles", sm->index_count/3);
} else { // with vertices only
glDrawArrays(prim, 0, sm->vertex_count /* / 3 */);
profile_incstat("Render.num_drawcalls", +1);
profile_incstat("Render.num_triangles", sm->vertex_count/3);
}
}
}
void mesh_destroy(mesh_t *m) {
// @todo
(void)m;

View File

@ -2549,6 +2549,7 @@ API void shadowmatrix_ortho(mat44 shm_proj, float left, float right, float botto
// shaders
API unsigned shader(const char *vs, const char *fs, const char *attribs, const char *fragcolor);
API unsigned shader_geom(const char *gs, const char *vs, const char *fs, const char *attribs, const char *fragcolor);
API unsigned shader_bind(unsigned program);
API void shader_bool(const char *uniform, bool i );
API void shader_int(const char *uniform, int i);
@ -2641,6 +2642,7 @@ typedef struct mesh_t {
API mesh_t mesh();
API void mesh_update(mesh_t *m, const char *format, int vertex_stride,int vertex_count,const void *interleaved_vertex_data, int index_count,const void *index_data, int flags);
API void mesh_render(mesh_t *m);
API void mesh_render_prim(mesh_t *sm, unsigned prim);
API void mesh_destroy(mesh_t *m);
API aabb mesh_bounds(mesh_t *m);

File diff suppressed because one or more lines are too long

View File

@ -7,9 +7,7 @@
# [V·4·K {{VERSION}}](https://dev.v4.games/zaklaus/v4k)
## a b o u t
- https://dev.v4.games/zaklaus/v4k is a 3D game framework in C, with Luajit bindings.
![Image from demo.c](https://i.imgur.com/sInbRoA.gif)
- https://dev.v4.games/zaklaus/v4k is a multimedia workbench for prototyping and planning ideas.
<details><summary>Code style</summary>
```C linenumbers