Now Shaders and materials use Strings.

This commit is contained in:
Relintai 2024-01-20 20:44:31 +01:00
parent d2c3151d73
commit 6837cb1042
18 changed files with 81 additions and 85 deletions

View File

@ -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() {

View File

@ -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();

View File

@ -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() {

View File

@ -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();

View File

@ -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() {

View File

@ -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();

View File

@ -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() {

View File

@ -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();

View File

@ -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;

View File

@ -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();

View File

@ -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<char> 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();

View File

@ -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 {

View File

@ -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() {

View File

@ -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();

View File

@ -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() {

View File

@ -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();

View File

@ -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() {

View File

@ -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();