]> git.lizzy.rs Git - dragonblocks_alpha.git/commitdiff
Make 3D fragment shader portable
authorElias Fleckenstein <eliasfleckenstein@web.de>
Tue, 24 Aug 2021 17:07:46 +0000 (19:07 +0200)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Tue, 24 Aug 2021 17:07:46 +0000 (19:07 +0200)
17 files changed:
.gitignore
shaders/3d/fragment.glsl
shaders/3d/vertex.glsl
shaders/hud/font/fragment.glsl
shaders/hud/font/vertex.glsl
shaders/hud/image/fragment.glsl
shaders/hud/image/vertex.glsl
src/client/client.c
src/client/game.c
src/client/game.h
src/client/hud.c
src/client/scene.c
src/client/shader.c
src/client/shader.h
src/debug.sh [new file with mode: 0755]
src/debug_loop.sh [new file with mode: 0755]
src/debug_mapgen.sh [new file with mode: 0755]

index 6ae23a0bd5f230d51e8fc685a36f78a6a74055f6..51071ea510e6eb3ed36d11bf064897312fc1477a 100644 (file)
@@ -13,4 +13,3 @@ DragonblocksServer
 Dragonblocks
 world.sqlite
 DragonblocksAlpha-*.zip
-
index 774ecaaa647194b34a7537037cb32bbb84ecdd14..20ec2af8031149d21737dcdab554706053638e82 100755 (executable)
@@ -1,12 +1,10 @@
-#version 460 core
-
 in float fragmentTextureIndex;
 in vec2 fragmentTextureCoords;
 in vec3 fragmentColor;
 
 out vec4 outColor;
 
-uniform sampler2D textures[8]; // ToDo: Replace 8 by max_texture_units
+uniform sampler2D textures[MAX_TEXTURE_UNITS];
 
 vec3 hsv2rgb(vec3 c)
 {
index 91948c0fb12015efb324cdeab5d6da45cc0b2a4a..301d492db785a6b58dd703fd25183dafd8d5cfba 100755 (executable)
@@ -1,5 +1,3 @@
-#version 460 core
-
 layout(location = 0) in vec3 vertexPosition;
 layout(location = 1) in float vertexTextureIndex;
 layout(location = 2) in vec2 vertexTextureCoords;
index a7310009b4e7bfcc9c1fa4accafeef332864856e..1d90ac39c7a9621ac63517effda8e96d966a175b 100755 (executable)
@@ -1,5 +1,3 @@
-#version 460 core
-
 in vec2 fragmentTextureCoords;
 
 out vec4 outColor;
index 498bce41c5e5529c2d4ccbb7ea94a896e9b4772c..c897d0d7ca85f81c134a57f31ae5a831a6700284 100644 (file)
@@ -1,5 +1,3 @@
-#version 460 core
-
 layout(location = 0) in vec2 vertexPosition;
 layout(location = 1) in vec2 vertexTextureCoords;
 
index e222a186e9e58b42c81a68e08daad85e22fa0629..3cb7c7e89baa9eb5648992fe211ce620e156bd6a 100755 (executable)
@@ -1,5 +1,3 @@
-#version 460 core
-
 in vec2 fragmentTextureCoords;
 
 out vec4 outColor;
index f96f2ccdd57f10caca34340baa82f9aa0080153a..a1be259a19234d178884f3c57bf91c869f11a2a5 100755 (executable)
@@ -1,5 +1,3 @@
-#version 460 core
-
 layout(location = 0) in vec2 vertexPosition;
 layout(location = 1) in vec2 vertexTextureCoords;
 
index 0b843b84d50f2add1feff4349ba7102c96a926cd..778f4e122aac01d6a5bdb484f6b2847ee2c1e455 100644 (file)
@@ -89,7 +89,7 @@ static bool client_authenticate()
        return false;
 }
 
-static void client_start(int fd)
+static bool client_start(int fd)
 {
        client.fd = fd;
        pthread_mutex_init(&client.mtx, NULL);
@@ -102,8 +102,7 @@ static void client_start(int fd)
        pthread_t recv_thread;
        pthread_create(&recv_thread, NULL, &reciever_thread, NULL);
 
-       if (client_authenticate())
-               game(&client);
+       bool return_value = client_authenticate() && game(&client);
 
        if (client.state != CS_DISCONNECTED)
                client_disconnect(true, NULL);
@@ -117,6 +116,8 @@ static void client_start(int fd)
        client_map_deinit();
 
        pthread_mutex_destroy(&client.mtx);
+
+       return return_value;
 }
 
 int main(int argc, char **argv)
@@ -155,7 +156,6 @@ int main(int argc, char **argv)
        freeaddrinfo(info);
 
        signal_handlers_init();
-       client_start(fd);
 
-       return EXIT_SUCCESS;
+       return client_start(fd) ? EXIT_SUCCESS : EXIT_FAILURE;
 }
index 9edf6dab4f1e38cd4e088541ed07ab23b4d0f270..48f80fde82af0a0cf64bc98e20ed0461e9e777df 100644 (file)
@@ -69,20 +69,20 @@ static void game_loop(Client *client)
        }
 }
 
-void game(Client *client)
+bool game(Client *client)
 {
        int width, height;
        width = 1250;
        height = 750;
 
        if (! window_init(width, height))
-               return;
+               return false;
 
        if (! font_init())
-               return;
+               return false;
 
        if (! scene_init())
-               return;
+               return false;
 
        scene_on_resize(width, height);
 
@@ -92,7 +92,9 @@ void game(Client *client)
        camera_set_position((v3f32) {0.0f, 0.0f, 0.0f});
        camera_set_angle(0.0f, 0.0f);
 
-       hud_init();
+       if (! hud_init())
+               return false;
+
        hud_on_resize(width, height);
 
        hud_add((HUDElementDefinition) {
@@ -131,4 +133,6 @@ void game(Client *client)
        font_deinit();
        hud_deinit();
        scene_deinit();
+
+       return true;
 }
index 9d713eb45cb14febe9450314654d266f2780a5f7..ad4bb0541808a3013d02b1cf592a6dcf3776e467 100644 (file)
@@ -3,6 +3,6 @@
 
 #include "client/client.h"
 
-void game(Client *client);
+bool game(Client *client);
 
 #endif
index be7ed3c8a646e15d1d70af3442c6d2105f181b3e..071a45eb28a8621ff87f9a3274dc53617671c3e8 100644 (file)
@@ -77,7 +77,7 @@ static VertexImage image_vertices[6] = {
 
 bool hud_init()
 {
-       if (! shader_program_create(RESSOURCEPATH "shaders/hud/image", &hud.image_prog)) {
+       if (! shader_program_create(RESSOURCEPATH "shaders/hud/image", &hud.image_prog, NULL)) {
                fprintf(stderr, "Failed to create HUD image shader program\n");
                return false;
        }
@@ -85,7 +85,7 @@ bool hud_init()
        hud.image_loc_model = glGetUniformLocation(hud.image_prog, "model");
        hud.image_loc_projection = glGetUniformLocation(hud.image_prog, "projection");
 
-       if (! shader_program_create(RESSOURCEPATH "shaders/hud/font", &hud.font_prog)) {
+       if (! shader_program_create(RESSOURCEPATH "shaders/hud/font", &hud.font_prog, NULL)) {
                fprintf(stderr, "Failed to create HUD font shader program\n");
                return false;
        }
index 99935f2e2d382862cbbd8e07d4a187402b414fad..8653df3f3a8eda519dd5e515dee67d8f8847a7a2 100644 (file)
@@ -26,15 +26,18 @@ bool scene_init()
        scene.objects = list_create(NULL),
        pthread_mutex_init(&scene.mtx, NULL);
 
-       if (! shader_program_create(RESSOURCEPATH "shaders/3d", &scene.prog)) {
+       glGetIntegerv(GL_MAX_TEXTURE_UNITS, &scene.max_texture_units);
+
+       char max_texture_units_def[BUFSIZ];
+       sprintf(max_texture_units_def, "#define MAX_TEXTURE_UNITS %d\n", scene.max_texture_units);
+
+       if (! shader_program_create(RESSOURCEPATH "shaders/3d", &scene.prog, max_texture_units_def)) {
                fprintf(stderr, "Failed to create 3D shader program\n");
                return false;
        }
 
        scene.loc_MVP = glGetUniformLocation(scene.prog, "MVP");
 
-       glGetIntegerv(GL_MAX_TEXTURE_UNITS, &scene.max_texture_units);
-
        GLint texture_indices[scene.max_texture_units];
        for (GLint i = 0; i < scene.max_texture_units; i++)
                texture_indices[i] = i;
index acdd2611f887767b11853ace35a6a688d30b365b..0f5b98696c88222ebd12915636b912d0aa3041a8 100644 (file)
@@ -4,7 +4,7 @@
 #include <stdlib.h>
 #include "client/shader.h"
 
-static GLuint compile_and_attach_shader(GLenum type, const char *path, const char *name, GLuint program)
+static GLuint compile_and_attach_shader(GLenum type, const char *path, const char *name, GLuint program, const char *definitions)
 {
        char full_path[strlen(path) + 1 + strlen(name) + 1 + 4 + 1];
        sprintf(full_path, "%s/%s.glsl", path, name);
@@ -47,9 +47,21 @@ static GLuint compile_and_attach_shader(GLenum type, const char *path, const cha
 
        GLuint id = glCreateShader(type);
 
-       char const *codeptr = code;
-       const int isize = (int) size;
-       glShaderSource(id, 1, &codeptr, &isize);
+       const char *version = "#version 460 core\n";
+
+       const char *code_list[3] = {
+               version,
+               definitions,
+               code,
+       };
+
+       int size_list[3] = {
+               18,
+               strlen(definitions),
+               size,
+       };
+
+       glShaderSource(id, 3, code_list, size_list);
 
        glCompileShader(id);
 
@@ -58,7 +70,7 @@ static GLuint compile_and_attach_shader(GLenum type, const char *path, const cha
        if (! success) {
                char errbuf[BUFSIZ];
                glGetShaderInfoLog(id, BUFSIZ, NULL, errbuf);
-               fprintf(stderr, "Failed to compile %s shader: %s\n", name, errbuf);
+               fprintf(stderr, "Failed to compile %s shader: %s", name, errbuf);
                glDeleteShader(id);
                return 0;
        }
@@ -68,19 +80,21 @@ static GLuint compile_and_attach_shader(GLenum type, const char *path, const cha
        return id;
 }
 
-
-bool shader_program_create(const char *path, GLuint *idptr)
+bool shader_program_create(const char *path, GLuint *idptr, const char *definitions)
 {
        GLuint id = glCreateProgram();
 
+       if (! definitions)
+               definitions = "";
+
        GLuint vert, frag;
 
-       if (! (vert = compile_and_attach_shader(GL_VERTEX_SHADER, path, "vertex", id))) {
+       if (! (vert = compile_and_attach_shader(GL_VERTEX_SHADER, path, "vertex", id, definitions))) {
                glDeleteProgram(id);
                return false;
        }
 
-       if (! (frag = compile_and_attach_shader(GL_FRAGMENT_SHADER, path, "fragment", id))) {
+       if (! (frag = compile_and_attach_shader(GL_FRAGMENT_SHADER, path, "fragment", id, definitions))) {
                glDeleteShader(vert);
                glDeleteProgram(id);
                return false;
index 515c786c4a42230c55628e75819bbdc4fbf8500f..cfc1abb70722b6e3f28b9962b791452ba2ff36bf 100644 (file)
@@ -5,6 +5,6 @@
 #include <GL/glew.h>
 #include <GL/gl.h>
 
-bool shader_program_create(const char *path, GLuint *idptr);
+bool shader_program_create(const char *path, GLuint *idptr, const char *definitions);
 
 #endif
diff --git a/src/debug.sh b/src/debug.sh
new file mode 100755 (executable)
index 0000000..0f82ee7
--- /dev/null
@@ -0,0 +1,39 @@
+#! /bin/bash
+if ! make -j$(nproc); then
+       exit 1
+fi
+
+DEBUG_DIR=/tmp/dragonblocks_alpha_debug_$$/
+mkdir $DEBUG_DIR
+
+echo "singleplayer" > $DEBUG_DIR/name
+
+COMMON="set confirm off
+handle SIGTERM nostop print pass
+handle SIGPIPE nostop noprint pass
+set height 0
+define hook-stop
+    if \$_exitcode == 0
+        quit
+    end
+end
+"
+
+echo "$COMMON
+run ::1 4000 < $DEBUG_DIR/name
+" > $DEBUG_DIR/client_script
+
+echo "$COMMON
+run 4000
+" > $DEBUG_DIR/server_script
+
+konsole -e bash -c "
+       echo \$\$ > $DEBUG_DIR/server_pid
+       exec gdb --command $DEBUG_DIR/server_script ./DragonblocksServer
+" & sleep 0.5
+
+gdb --command $DEBUG_DIR/client_script ./Dragonblocks
+
+kill `cat $DEBUG_DIR/server_pid`
+
+rm -rf $DEBUG_DIR
diff --git a/src/debug_loop.sh b/src/debug_loop.sh
new file mode 100755 (executable)
index 0000000..199a3bd
--- /dev/null
@@ -0,0 +1,12 @@
+#! /bin/bash
+COMMAND="./debug.sh"
+
+if [[ $1 == "mapgen" ]]; then
+       COMMAND="./debug_mapgen.sh"
+fi
+
+while true; do
+       if ! $COMMAND; then
+               read
+       fi
+done
diff --git a/src/debug_mapgen.sh b/src/debug_mapgen.sh
new file mode 100755 (executable)
index 0000000..60865ad
--- /dev/null
@@ -0,0 +1,3 @@
+#! /bin/bash
+rm -f world.sqlite
+./debug.sh