diff options
-rw-r--r-- | Jamroot.jam | 23 | ||||
-rw-r--r-- | gfx/gl/shader-source.h | 20 | ||||
-rw-r--r-- | gfx/gl/shader.cpp | 34 | ||||
-rw-r--r-- | gfx/gl/shader.h | 6 | ||||
-rw-r--r-- | utility/embed-glsl.cpp.m4 | 10 | ||||
-rw-r--r-- | utility/embed-glsl.h.m4 | 4 |
6 files changed, 65 insertions, 32 deletions
diff --git a/Jamroot.jam b/Jamroot.jam index 51097f7..953092a 100644 --- a/Jamroot.jam +++ b/Jamroot.jam @@ -2,6 +2,7 @@ using gcc : 11.0.0 ; using pkg-config ; import pkg-config ; import type : register ; +import type : type ; import generators : register-standard ; import testing ; import lex ; @@ -17,8 +18,6 @@ project : requirements <variant>debug:<warnings>extra <variant>debug:<warnings-as-errors>on <variant>release:<lto>on - <toolset>tidy:<exclude>gfx/gl/shaders/fs-basicShader.h - <toolset>tidy:<exclude>gfx/gl/shaders/vs-basicShader.h <toolset>tidy:<exclude>gfx/models/obj.cpp <toolset>tidy:<checkxx>boost-* <toolset>tidy:<checkxx>bugprone-* @@ -37,17 +36,23 @@ project : requirements <toolset>tidy:<define>TIDY ; -type.register VERTEXSHADER : vs ; -type.register FRAGMENTSHADER : fs ; +type.register GL_VERTEX_SHADER : vs ; +type.register GL_FRAGMENT_SHADER : fs ; -generators.register-standard xxd.i : VERTEXSHADER : C(vs-%) H(vs-%) ; -generators.register-standard xxd.i : FRAGMENTSHADER : C(fs-%) H(fs-%) ; +generators.register-standard embed.glsl : GL_VERTEX_SHADER : CPP(vs-%) H(vs-%) ; +generators.register-standard embed.glsl : GL_FRAGMENT_SHADER : CPP(fs-%) H(fs-%) ; -actions xxd.i +actions embed.glsl { - ( cd $(2:D) ; xxd -i $(2:B)$(2:S) ) | tee $(1[1]) | cproto -veo $(1[2]) + m4 -DNAME=$(2:B) -DTYPE=$(2:S) > $(1[2]) utility/embed-glsl.h.m4 + m4 -DSOURCE=$(2) -DNAME=$(2:B) -DTYPE=$(2:S) -DGLTYPE=$(OPTIONS) > $(1[1]) utility/embed-glsl.cpp.m4 } -IMPORT $(__name__) : xxd.i : : xxd.i ; +rule embed.glsl ( targets * : sources * : properties * ) +{ + DEPENDS $(targets) : utility/embed-glsl.h.m4 utility/embed-glsl.cpp.m4 ; + OPTIONS on $(targets) = [ type.type $(sources) ] ; +} +IMPORT $(__name__) : embed.glsl : : embed.glsl ; exe iliketrains : application/main.cpp diff --git a/gfx/gl/shader-source.h b/gfx/gl/shader-source.h new file mode 100644 index 0000000..4fbbe5b --- /dev/null +++ b/gfx/gl/shader-source.h @@ -0,0 +1,20 @@ +#ifndef SHADER_SOURCE_H +#define SHADER_SOURCE_H + +#include <GL/glew.h> + +struct GLsource { + const GLchar * text; + GLint len; + GLuint type; +}; + +constexpr auto +constexpr_strlen(const GLchar * const s) +{ + auto e {s}; + while (*++e) { } + return e - s; +} + +#endif diff --git a/gfx/gl/shader.cpp b/gfx/gl/shader.cpp index f80ab22..badaaef 100644 --- a/gfx/gl/shader.cpp +++ b/gfx/gl/shader.cpp @@ -10,11 +10,10 @@ #include <stdexcept>
#include <string>
-Shader::ProgramHandle::ProgramHandle(std::initializer_list<GLuint> srcs) : viewProjection_uniform {}, model_uniform {}
+Shader::ProgramHandle::ProgramHandle(GLuint vs, GLuint fs) : viewProjection_uniform {}, model_uniform {}
{
- for (const auto & srcId : srcs) {
- glAttachShader(m_program, srcId);
- }
+ glAttachShader(m_program, vs);
+ glAttachShader(m_program, fs);
glBindAttribLocation(m_program, 0, "position");
glBindAttribLocation(m_program, 1, "texCoord");
@@ -31,17 +30,17 @@ Shader::ProgramHandle::ProgramHandle(std::initializer_list<GLuint> srcs) : viewP }
Shader::Shader() :
- programs {{ProgramHandle {
- Source {{basicShader_vs, basicShader_vs_len}, GL_VERTEX_SHADER}.id,
- Source {{basicShader_fs, basicShader_fs_len}, GL_FRAGMENT_SHADER}.id,
+ programs {{{
+ Source {basicShader_vs}.id,
+ Source {basicShader_fs}.id,
},
- ProgramHandle {
- Source {{waterShader_vs, waterShader_vs_len}, GL_VERTEX_SHADER}.id,
- Source {{waterShader_fs, waterShader_fs_len}, GL_FRAGMENT_SHADER}.id,
+ {
+ Source {waterShader_vs}.id,
+ Source {waterShader_fs}.id,
},
- ProgramHandle {
- Source {{landmassShader_vs, landmassShader_vs_len}, GL_VERTEX_SHADER}.id,
- Source {{landmassShader_fs, landmassShader_fs_len}, GL_FRAGMENT_SHADER}.id,
+ {
+ Source {landmassShader_vs}.id,
+ Source {landmassShader_fs}.id,
}}}
{
}
@@ -99,14 +98,9 @@ Shader::CheckShaderError(GLuint shader, GLuint flag, bool isProgram, std::string }
}
-Shader::Source::Source(const std::basic_string_view<unsigned char> text, GLuint type) :
- Source {(const GLchar *)(text.data()), (GLint)(text.length()), type}
-{
-}
-
-Shader::Source::Source(const GLchar * text, GLint len, unsigned int type) : id {type}
+Shader::Source::Source(const GLsource src) : id {src.type}
{
- glShaderSource(id, 1, &text, &len);
+ glShaderSource(id, 1, &src.text, &src.len);
glCompileShader(id);
CheckShaderError(id, GL_COMPILE_STATUS, false, "Error compiling shader!");
diff --git a/gfx/gl/shader.h b/gfx/gl/shader.h index 47a2b7d..293ce9b 100644 --- a/gfx/gl/shader.h +++ b/gfx/gl/shader.h @@ -1,6 +1,7 @@ #ifndef SHADER_INCLUDED_H
#define SHADER_INCLUDED_H
+#include "shader-source.h"
#include <GL/glew.h>
#include <array>
#include <glRef.hpp>
@@ -22,8 +23,7 @@ private: public:
using ShaderRef = glRef<GLuint, __glewCreateShader, __glewDeleteShader>;
- Source(const std::basic_string_view<unsigned char> text, GLuint type);
- Source(const GLchar * text, GLint len, GLuint type);
+ explicit Source(const GLsource src);
ShaderRef id;
};
@@ -32,7 +32,7 @@ private: class ProgramHandle {
public:
- explicit ProgramHandle(std::initializer_list<GLuint>);
+ ProgramHandle(GLuint, GLuint);
using ProgramRef = glRef<GLuint, __glewCreateProgram, __glewDeleteProgram>;
ProgramRef m_program;
diff --git a/utility/embed-glsl.cpp.m4 b/utility/embed-glsl.cpp.m4 new file mode 100644 index 0000000..febba68 --- /dev/null +++ b/utility/embed-glsl.cpp.m4 @@ -0,0 +1,10 @@ +changecom()dnl +// NAME +#include "substr(TYPE,1)-NAME.h" +#include <cstring> + +constexpr const GLchar * src { R"GLSL-EMBED(dnl +include(SOURCE))GLSL-EMBED" }; +constexpr auto len { constexpr_strlen (src) }; + +const GLsource NAME`_'substr(TYPE,1) { src, len, GLTYPE }; diff --git a/utility/embed-glsl.h.m4 b/utility/embed-glsl.h.m4 new file mode 100644 index 0000000..918007b --- /dev/null +++ b/utility/embed-glsl.h.m4 @@ -0,0 +1,4 @@ +// NAME +#include <gfx/gl/shader-source.h> + +extern const GLsource NAME`_'substr(TYPE,1); |