From 6837cb104205f52c6522a0fd12bbe02109b09b94 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 20 Jan 2024 20:44:31 +0100 Subject: [PATCH] Now Shaders and materials use Strings. --- sfw/render_core/color_material.cpp | 8 +-- sfw/render_core/color_material.h | 4 +- sfw/render_core/color_material_2d.cpp | 8 +-- sfw/render_core/color_material_2d.h | 4 +- sfw/render_core/colored_material.cpp | 8 +-- sfw/render_core/colored_material.h | 4 +- sfw/render_core/font_material.cpp | 8 +-- sfw/render_core/font_material.h | 4 +- sfw/render_core/material.cpp | 8 ++- sfw/render_core/material.h | 7 +-- sfw/render_core/shader.cpp | 51 +++++++++---------- sfw/render_core/shader.h | 16 +++--- sfw/render_core/texture_material.cpp | 8 +-- sfw/render_core/texture_material.h | 4 +- sfw/render_core/texture_material_2d.cpp | 8 +-- sfw/render_core/texture_material_2d.h | 4 +- .../transparent_texture_material.cpp | 8 +-- .../transparent_texture_material.h | 4 +- 18 files changed, 81 insertions(+), 85 deletions(-) diff --git a/sfw/render_core/color_material.cpp b/sfw/render_core/color_material.cpp index 408a613..ec2691a 100644 --- a/sfw/render_core/color_material.cpp +++ b/sfw/render_core/color_material.cpp @@ -15,7 +15,7 @@ void ColorMaterial::setup_uniforms() { model_view_matrix_location = get_uniform("u_model_view_matrix"); } -const char **ColorMaterial::get_vertex_shader_source() { +String ColorMaterial::get_vertex_shader_source() { static const char *vertex_shader_source[] = { "uniform mat4 u_proj_matrix;\n" "uniform mat4 u_camera_matrix;\n" @@ -32,10 +32,10 @@ const char **ColorMaterial::get_vertex_shader_source() { "}\n" }; - return vertex_shader_source; + return String(*vertex_shader_source); } -const char **ColorMaterial::get_fragment_shader_source() { +String ColorMaterial::get_fragment_shader_source() { static const char *fragment_shader_source[] = { #ifndef __APPLE__ "precision mediump float;\n" @@ -46,7 +46,7 @@ const char **ColorMaterial::get_fragment_shader_source() { "void main() { gl_FragColor = v_color; }\n" }; - return fragment_shader_source; + return String(*fragment_shader_source); } ColorMaterial::ColorMaterial() { diff --git a/sfw/render_core/color_material.h b/sfw/render_core/color_material.h index ebeaba1..4879ea0 100644 --- a/sfw/render_core/color_material.h +++ b/sfw/render_core/color_material.h @@ -20,8 +20,8 @@ public: void bind_uniforms(); void setup_uniforms(); - const char **get_vertex_shader_source(); - const char **get_fragment_shader_source(); + String get_vertex_shader_source(); + String get_fragment_shader_source(); ColorMaterial(); diff --git a/sfw/render_core/color_material_2d.cpp b/sfw/render_core/color_material_2d.cpp index 3194aae..ec8f54a 100644 --- a/sfw/render_core/color_material_2d.cpp +++ b/sfw/render_core/color_material_2d.cpp @@ -21,7 +21,7 @@ void ColorMaterial2D::setup_state() { glEnable(GL_TEXTURE_2D); } -const char **ColorMaterial2D::get_vertex_shader_source() { +String ColorMaterial2D::get_vertex_shader_source() { static const char *vertex_shader_source[] = { "uniform mat4 u_proj_matrix;\n" "uniform mat4 u_model_view_matrix;\n" @@ -37,10 +37,10 @@ const char **ColorMaterial2D::get_vertex_shader_source() { "}" }; - return vertex_shader_source; + return String(*vertex_shader_source); } -const char **ColorMaterial2D::get_fragment_shader_source() { +String ColorMaterial2D::get_fragment_shader_source() { static const char *fragment_shader_source[] = { #ifndef __APPLE__ "precision mediump float;\n" @@ -53,7 +53,7 @@ const char **ColorMaterial2D::get_fragment_shader_source() { "}" }; - return fragment_shader_source; + return String(*fragment_shader_source); } ColorMaterial2D::ColorMaterial2D() { diff --git a/sfw/render_core/color_material_2d.h b/sfw/render_core/color_material_2d.h index 0dca8bf..03ea704 100644 --- a/sfw/render_core/color_material_2d.h +++ b/sfw/render_core/color_material_2d.h @@ -23,8 +23,8 @@ public: void unbind(); void setup_state(); - const char **get_vertex_shader_source(); - const char **get_fragment_shader_source(); + String get_vertex_shader_source(); + String get_fragment_shader_source(); ColorMaterial2D(); diff --git a/sfw/render_core/colored_material.cpp b/sfw/render_core/colored_material.cpp index 813454f..a1067b7 100644 --- a/sfw/render_core/colored_material.cpp +++ b/sfw/render_core/colored_material.cpp @@ -16,7 +16,7 @@ void ColoredMaterial::setup_uniforms() { tri_color_uniform_location = get_uniform("fragment_color"); } -const char **ColoredMaterial::get_vertex_shader_source() { +String ColoredMaterial::get_vertex_shader_source() { static const char *vertex_shader_source[] = { "uniform mat4 u_proj_matrix;\n" "uniform mat4 u_model_view_matrix;\n" @@ -28,10 +28,10 @@ const char **ColoredMaterial::get_vertex_shader_source() { "}" }; - return vertex_shader_source; + return String(*vertex_shader_source); } -const char **ColoredMaterial::get_fragment_shader_source() { +String ColoredMaterial::get_fragment_shader_source() { static const char *fragment_shader_source[] = { #ifndef __APPLE__ "precision mediump float;\n" @@ -45,7 +45,7 @@ const char **ColoredMaterial::get_fragment_shader_source() { "}" }; - return fragment_shader_source; + return String(*fragment_shader_source); } ColoredMaterial::ColoredMaterial() { diff --git a/sfw/render_core/colored_material.h b/sfw/render_core/colored_material.h index 7e5b2f1..88c5378 100644 --- a/sfw/render_core/colored_material.h +++ b/sfw/render_core/colored_material.h @@ -20,8 +20,8 @@ public: void bind_uniforms(); void setup_uniforms(); - const char **get_vertex_shader_source(); - const char **get_fragment_shader_source(); + String get_vertex_shader_source(); + String get_fragment_shader_source(); ColoredMaterial(); diff --git a/sfw/render_core/font_material.cpp b/sfw/render_core/font_material.cpp index 709cc97..fb8a13b 100644 --- a/sfw/render_core/font_material.cpp +++ b/sfw/render_core/font_material.cpp @@ -28,7 +28,7 @@ void FontMaterial::setup_state() { glEnable(GL_TEXTURE_2D); } -const char **FontMaterial::get_vertex_shader_source() { +String FontMaterial::get_vertex_shader_source() { static const char *vertex_shader_source[] = { "uniform mat4 u_proj_matrix;\n" "uniform mat4 u_model_view_matrix;\n" @@ -47,10 +47,10 @@ const char **FontMaterial::get_vertex_shader_source() { "}" }; - return vertex_shader_source; + return String(*vertex_shader_source); } -const char **FontMaterial::get_fragment_shader_source() { +String FontMaterial::get_fragment_shader_source() { static const char *fragment_shader_source[] = { #ifndef __APPLE__ "precision mediump float;\n" @@ -73,7 +73,7 @@ const char **FontMaterial::get_fragment_shader_source() { "}" }; - return fragment_shader_source; + return String(*fragment_shader_source); } FontMaterial::FontMaterial() { diff --git a/sfw/render_core/font_material.h b/sfw/render_core/font_material.h index 3b7349e..31542b3 100644 --- a/sfw/render_core/font_material.h +++ b/sfw/render_core/font_material.h @@ -23,8 +23,8 @@ public: void unbind(); void setup_state(); - const char **get_vertex_shader_source(); - const char **get_fragment_shader_source(); + String get_vertex_shader_source(); + String get_fragment_shader_source(); FontMaterial(); diff --git a/sfw/render_core/material.cpp b/sfw/render_core/material.cpp index 29663c0..148e9b3 100644 --- a/sfw/render_core/material.cpp +++ b/sfw/render_core/material.cpp @@ -5,8 +5,6 @@ //--STRIP void Material::bind() { - //csak main thread! - if (!shader) { shader = ShaderCache::get_singleton()->get_shader(get_material_id()); @@ -46,11 +44,11 @@ void Material::setup_uniforms() { void Material::setup_state() { } -int32_t Material::get_uniform(const char *name) { - int32_t uniform = glGetUniformLocation(shader->program, name); +int32_t Material::get_uniform(const String &name) { + int32_t uniform = glGetUniformLocation(shader->program, name.utf8().get_data()); if (uniform == -1) { - printf("%s is not a valid glsl program variable!\n", name); + ERR_PRINT(name + " is not a valid glsl program variable!\n"); } return uniform; diff --git a/sfw/render_core/material.h b/sfw/render_core/material.h index b3ae3c3..e0dfb53 100644 --- a/sfw/render_core/material.h +++ b/sfw/render_core/material.h @@ -7,6 +7,7 @@ #include "core/projection.h" #include "core/transform.h" #include "core/transform_2d.h" +#include "core/ustring.h" #include "object/reference.h" #include "render_core/shader.h" //--STRIP @@ -22,10 +23,10 @@ public: virtual void bind_uniforms(); virtual void setup_uniforms(); virtual void setup_state(); - virtual const char **get_vertex_shader_source() = 0; - virtual const char **get_fragment_shader_source() = 0; + virtual String get_vertex_shader_source() = 0; + virtual String get_fragment_shader_source() = 0; - int32_t get_uniform(const char *name); + int32_t get_uniform(const String &name); Material(); virtual ~Material(); diff --git a/sfw/render_core/shader.cpp b/sfw/render_core/shader.cpp index df3c042..8de3b4a 100644 --- a/sfw/render_core/shader.cpp +++ b/sfw/render_core/shader.cpp @@ -25,6 +25,9 @@ void Shader::unbind() { } void Shader::compile() { + ERR_FAIL_COND(_vertex_shader_source.empty()); + ERR_FAIL_COND(_fragment_shader_source.empty()); + if (!program) { program = glCreateProgram(); } @@ -37,9 +40,10 @@ void Shader::compile() { fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); } - const char **vertex_shader_source = get_vertex_shader_source(); + CharString vertex_shader_source = _vertex_shader_source.utf8(); + const char *vss = vertex_shader_source.get_data(); - glShaderSource(vertex_shader, 1, vertex_shader_source, NULL); + glShaderSource(vertex_shader, 1, &vss, NULL); glCompileShader(vertex_shader); int32_t shader_compiled = GL_FALSE; @@ -51,9 +55,10 @@ void Shader::compile() { glAttachShader(program, vertex_shader); - const char **fragment_shader_source = get_fragment_shader_source(); + CharString fragment_shader_source = _fragment_shader_source.utf8(); + const char *fss = fragment_shader_source.get_data(); - glShaderSource(fragment_shader, 1, fragment_shader_source, NULL); + glShaderSource(fragment_shader, 1, &fss, NULL); glCompileShader(fragment_shader); shader_compiled = GL_FALSE; @@ -85,49 +90,44 @@ void Shader::destroy() { glDeleteProgram(program); } -const char **Shader::get_vertex_shader_source() { - return vertex_shader_source; +String Shader::get_vertex_shader_source() { + return _vertex_shader_source; } -void Shader::set_vertex_shader_source(const char **source) { - vertex_shader_source = source; +void Shader::set_vertex_shader_source(const String &source) { + _vertex_shader_source = source; } -const char **Shader::get_fragment_shader_source() { - return fragment_shader_source; +String Shader::get_fragment_shader_source() { + return _fragment_shader_source; } -void Shader::set_fragment_shader_source(const char **source) { - fragment_shader_source = source; +void Shader::set_fragment_shader_source(const String &source) { + _fragment_shader_source = source; } -void Shader::print_shader_errors(const uint32_t p_program, const char *name) { +void Shader::print_shader_errors(const uint32_t p_program, const String &name) { int max_length = 5000; - Vector error_log; - error_log.resize(max_length); + char error_log[5000]; - glGetShaderInfoLog(p_program, max_length, &max_length, error_log.ptrw()); + glGetShaderInfoLog(p_program, max_length, &max_length, error_log); - printf("Error %s!\n", name); - printf("%s!\n", error_log.ptr()); + ERR_PRINT("Error " + name + "\n" + String::utf8(error_log, max_length)); } void Shader::print_program_errors(const uint32_t p_program) { if (glIsProgram(program)) { int info_length = 0; int max_length = 5000; + char info_log[5000]; glGetProgramiv(p_program, GL_INFO_LOG_LENGTH, &info_length); - char *info_log = new char[max_length]; - glGetProgramInfoLog(p_program, max_length, &info_length, info_log); if (info_length > 0) { - printf("%s\n", info_log); + ERR_PRINT(String::utf8(info_log, max_length)); } - - delete[] info_log; } else { - printf("print_program_errors: Not a program!\n"); + ERR_PRINT("print_program_errors: Not a program!\n"); } } @@ -135,9 +135,6 @@ Shader::Shader() { vertex_shader = 0; fragment_shader = 0; program = 0; - - vertex_shader_source = NULL; - fragment_shader_source = NULL; } Shader::~Shader() { destroy(); diff --git a/sfw/render_core/shader.h b/sfw/render_core/shader.h index 72f3d65..d4a981b 100644 --- a/sfw/render_core/shader.h +++ b/sfw/render_core/shader.h @@ -4,8 +4,8 @@ //--STRIP //--STRIP -#include "render_core/3rd_glad.h" #include "core/hash_map.h" +#include "render_core/3rd_glad.h" //--STRIP class Shader { @@ -23,13 +23,13 @@ public: void compile(); void destroy(); - const char **get_vertex_shader_source(); - void set_vertex_shader_source(const char **source); + String get_vertex_shader_source(); + void set_vertex_shader_source(const String &source); - const char **get_fragment_shader_source(); - void set_fragment_shader_source(const char **source); + String get_fragment_shader_source(); + void set_fragment_shader_source(const String &source); - void print_shader_errors(const uint32_t p_program, const char *name); + void print_shader_errors(const uint32_t p_program, const String &name); void print_program_errors(const uint32_t p_program); Shader(); @@ -42,8 +42,8 @@ public: static Shader *current_shader; protected: - const char **vertex_shader_source; - const char **fragment_shader_source; + String _vertex_shader_source; + String _fragment_shader_source; }; class ShaderCache { diff --git a/sfw/render_core/texture_material.cpp b/sfw/render_core/texture_material.cpp index 47e324f..2e6a84b 100644 --- a/sfw/render_core/texture_material.cpp +++ b/sfw/render_core/texture_material.cpp @@ -31,7 +31,7 @@ void TextureMaterial::setup_state() { glEnable(GL_TEXTURE_2D); } -const char **TextureMaterial::get_vertex_shader_source() { +String TextureMaterial::get_vertex_shader_source() { static const char *vertex_shader_source[] = { "uniform mat4 u_proj_matrix;\n" "uniform mat4 u_camera_matrix;\n" @@ -48,10 +48,10 @@ const char **TextureMaterial::get_vertex_shader_source() { "}" }; - return vertex_shader_source; + return String(*vertex_shader_source); } -const char **TextureMaterial::get_fragment_shader_source() { +String TextureMaterial::get_fragment_shader_source() { static const char *fragment_shader_source[] = { #ifndef __APPLE__ "precision mediump float;\n" @@ -66,7 +66,7 @@ const char **TextureMaterial::get_fragment_shader_source() { "}" }; - return fragment_shader_source; + return String(*fragment_shader_source); } TextureMaterial::TextureMaterial() { diff --git a/sfw/render_core/texture_material.h b/sfw/render_core/texture_material.h index 19d3304..d637092 100644 --- a/sfw/render_core/texture_material.h +++ b/sfw/render_core/texture_material.h @@ -23,8 +23,8 @@ public: void unbind(); void setup_state(); - const char **get_vertex_shader_source(); - const char **get_fragment_shader_source(); + String get_vertex_shader_source(); + String get_fragment_shader_source(); TextureMaterial(); diff --git a/sfw/render_core/texture_material_2d.cpp b/sfw/render_core/texture_material_2d.cpp index 5c80acf..30bd371 100644 --- a/sfw/render_core/texture_material_2d.cpp +++ b/sfw/render_core/texture_material_2d.cpp @@ -28,7 +28,7 @@ void TextureMaterial2D::setup_state() { glEnable(GL_TEXTURE_2D); } -const char **TextureMaterial2D::get_vertex_shader_source() { +String TextureMaterial2D::get_vertex_shader_source() { static const char *vertex_shader_source[] = { "uniform mat4 u_proj_matrix;\n" "uniform mat4 u_model_view_matrix;\n" @@ -44,10 +44,10 @@ const char **TextureMaterial2D::get_vertex_shader_source() { "}" }; - return vertex_shader_source; + return String(*vertex_shader_source); } -const char **TextureMaterial2D::get_fragment_shader_source() { +String TextureMaterial2D::get_fragment_shader_source() { static const char *fragment_shader_source[] = { #ifndef __APPLE__ "precision mediump float;\n" @@ -69,7 +69,7 @@ const char **TextureMaterial2D::get_fragment_shader_source() { "}" }; - return fragment_shader_source; + return String(*fragment_shader_source); } TextureMaterial2D::TextureMaterial2D() { diff --git a/sfw/render_core/texture_material_2d.h b/sfw/render_core/texture_material_2d.h index a46611b..af1b00c 100644 --- a/sfw/render_core/texture_material_2d.h +++ b/sfw/render_core/texture_material_2d.h @@ -23,8 +23,8 @@ public: void unbind(); void setup_state(); - const char **get_vertex_shader_source(); - const char **get_fragment_shader_source(); + String get_vertex_shader_source(); + String get_fragment_shader_source(); TextureMaterial2D(); diff --git a/sfw/render_core/transparent_texture_material.cpp b/sfw/render_core/transparent_texture_material.cpp index 1e0b1f6..017bb4b 100644 --- a/sfw/render_core/transparent_texture_material.cpp +++ b/sfw/render_core/transparent_texture_material.cpp @@ -28,7 +28,7 @@ void TransparentTextureMaterial::setup_state() { glEnable(GL_TEXTURE_2D); } -const char **TransparentTextureMaterial::get_vertex_shader_source() { +String TransparentTextureMaterial::get_vertex_shader_source() { static const char *vertex_shader_source[] = { "uniform mat4 u_proj_matrix;\n" "uniform mat4 u_model_view_matrix;\n" @@ -44,10 +44,10 @@ const char **TransparentTextureMaterial::get_vertex_shader_source() { "}" }; - return vertex_shader_source; + return String(*vertex_shader_source); } -const char **TransparentTextureMaterial::get_fragment_shader_source() { +String TransparentTextureMaterial::get_fragment_shader_source() { static const char *fragment_shader_source[] = { #ifndef __APPLE__ "precision mediump float;\n" @@ -68,7 +68,7 @@ const char **TransparentTextureMaterial::get_fragment_shader_source() { "}" }; - return fragment_shader_source; + return String(*fragment_shader_source); } TransparentTextureMaterial::TransparentTextureMaterial() { diff --git a/sfw/render_core/transparent_texture_material.h b/sfw/render_core/transparent_texture_material.h index 374e88f..8d4af3b 100644 --- a/sfw/render_core/transparent_texture_material.h +++ b/sfw/render_core/transparent_texture_material.h @@ -23,8 +23,8 @@ public: void unbind(); void setup_state(); - const char **get_vertex_shader_source(); - const char **get_fragment_shader_source(); + String get_vertex_shader_source(); + String get_fragment_shader_source(); TransparentTextureMaterial();