From d4ae68b6ba5211c7bc11a52951c466c5050ae377 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 21 Jan 2024 01:34:10 +0000 Subject: String view/constexpr Shader instances Gonna need more constexpr stuff and strstr/strlen aren't that --- gfx/gl/shader.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'gfx/gl/shader.cpp') diff --git a/gfx/gl/shader.cpp b/gfx/gl/shader.cpp index 5f83b83..285c2c3 100644 --- a/gfx/gl/shader.cpp +++ b/gfx/gl/shader.cpp @@ -7,7 +7,10 @@ Shader::ShaderRef Shader::compile() const { ShaderRef shader {type}; - glShaderSource(shader, 1, &text, &len); + auto source = [&shader](auto text, GLint len) { + glShaderSource(shader, 1, &text, &len); + }; + source(text.data(), static_cast(text.length())); glCompileShader(shader); CheckShaderError(shader, GL_COMPILE_STATUS, false, "Error compiling shader!"); -- cgit v1.2.3 From 979c226bdcd7904db5bd23dfbf526de4c040f436 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 21 Jan 2024 02:39:30 +0000 Subject: Look for and replace GL_XXX with fixed string --- gfx/gl/shader.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'gfx/gl/shader.cpp') diff --git a/gfx/gl/shader.cpp b/gfx/gl/shader.cpp index 285c2c3..b57dcd4 100644 --- a/gfx/gl/shader.cpp +++ b/gfx/gl/shader.cpp @@ -10,7 +10,16 @@ Shader::compile() const auto source = [&shader](auto text, GLint len) { glShaderSource(shader, 1, &text, &len); }; - source(text.data(), static_cast(text.length())); + if (lookups) { + std::basic_string textMod {text}; + for (const auto & match : ctre::range(textMod)) { + textMod.replace(match.begin(), match.end(), "255"); + } + source(textMod.c_str(), static_cast(textMod.length())); + } + else { + source(text.data(), static_cast(text.length())); + } glCompileShader(shader); CheckShaderError(shader, GL_COMPILE_STATUS, false, "Error compiling shader!"); -- cgit v1.2.3 From 2368cde54675f39e9b5a99880124503b3ae391e1 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 21 Jan 2024 03:01:44 +0000 Subject: Replace tokens found with values from getIntegerv --- gfx/gl/shader.cpp | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'gfx/gl/shader.cpp') diff --git a/gfx/gl/shader.cpp b/gfx/gl/shader.cpp index b57dcd4..0f75817 100644 --- a/gfx/gl/shader.cpp +++ b/gfx/gl/shader.cpp @@ -1,8 +1,24 @@ #include "shader.h" +#include #include #include #include +namespace { + auto + getInt(GLenum e) + { + GLint i {}; + glGetIntegerv(e, &i); + return std::to_string(i); + } + + using LookUpFunction = std::string (*)(GLenum); + constexpr std::array, 1> LOOKUPS {{ + {"GL_MAX_GEOMETRY_OUTPUT_VERTICES", GL_MAX_GEOMETRY_OUTPUT_VERTICES, getInt}, + }}; +} + Shader::ShaderRef Shader::compile() const { @@ -13,7 +29,14 @@ Shader::compile() const if (lookups) { std::basic_string textMod {text}; for (const auto & match : ctre::range(textMod)) { - textMod.replace(match.begin(), match.end(), "255"); + if (const auto lookup = std::find_if(LOOKUPS.begin(), LOOKUPS.end(), + [&match](const auto & lookup) { + return std::get(lookup) == match; + }); + lookup != LOOKUPS.end()) { + const auto & [name, pname, getFunction] = *lookup; + textMod.replace(match.begin(), match.end(), getFunction(pname)); + } } source(textMod.c_str(), static_cast(textMod.length())); } -- cgit v1.2.3