From f7c0b4a177312ee665c93430d41c25099d104512 Mon Sep 17 00:00:00 2001 From: rexy712 Date: Sun, 20 Feb 2022 18:47:15 -0800 Subject: [PATCH] Remove need to know the shader type from pause_state. Moves rendering api dependance elsewhere --- include/gfx/ogl/shader.hpp | 6 ++- include/wip/base_shader.hpp | 45 ++--------------- include/wip/ogl_base_shader.hpp | 87 +++++++++++++++++++++++++++++++++ include/wip/shader_source.hpp | 34 +++++++++++++ src/gfx/ogl/shader.cpp | 2 + src/ttt/pause_state.cpp | 4 +- 6 files changed, 132 insertions(+), 46 deletions(-) create mode 100644 include/wip/ogl_base_shader.hpp create mode 100644 include/wip/shader_source.hpp diff --git a/include/gfx/ogl/shader.hpp b/include/gfx/ogl/shader.hpp index 261cf1b..f19a455 100644 --- a/include/gfx/ogl/shader.hpp +++ b/include/gfx/ogl/shader.hpp @@ -20,6 +20,7 @@ #define OUR_DICK_GRAPHICS_OGL_SHADER_HPP #include "gl_include.hpp" +#include "wip/shader_source.hpp" #include namespace gfx::ogl{ @@ -29,7 +30,7 @@ namespace gfx::ogl{ { public: //Strongly typed enum of possible shader types - enum class type : GLuint{ + enum class type : unsigned int{ FRAGMENT = GL_FRAGMENT_SHADER, VERTEX = GL_VERTEX_SHADER, GEOMETRY = GL_GEOMETRY_SHADER, @@ -43,8 +44,9 @@ namespace gfx::ogl{ public: //initialize this shader with the type 't' and add no source text explicit shader(type t); - //initialize this shader with the type 't't and load in 'data' as source text + //initialize this shader with the type 't' and load in 'data' as source text shader(const char* data, type t); + shader(const wip::gfx::shader_source&); shader(const shader&); shader(shader&&); ~shader(void); diff --git a/include/wip/base_shader.hpp b/include/wip/base_shader.hpp index 50a77c4..1571fff 100644 --- a/include/wip/base_shader.hpp +++ b/include/wip/base_shader.hpp @@ -19,50 +19,11 @@ #ifndef OUR_DICK_GRAPHICS_WIP_BASE_SHADER_HPP #define OUR_DICK_GRAPHICS_WIP_BASE_SHADER_HPP -namespace wip::gfx::base_shader{ +#include "ogl_base_shader.hpp" - static constexpr char vertex_shader_text[] = - R"glsl( - #version 430 core - - layout (location = 0) in vec3 position; - layout (location = 1) in vec2 tex_coords; - - - layout (std140) uniform mvp_matrix{ - mat4 vp_mat; - mat4 model_mat; - }; - - out VS_OUT{ - vec2 tex_coords; - }vs_out; - - const vec4 vertices[6] = vec4[](vec4(0,1,0,1), vec4(0,0,0,1), vec4(1,0,0,1), vec4(1,0,0,1), vec4(1,1,0,1), vec4(0,1,0,1)); - - void main(){ - gl_Position = vp_mat * model_mat * vec4(position, 1); - vs_out.tex_coords = tex_coords; - } - )glsl"; - - static constexpr char fragment_shader_text[] = - R"glsl( - #version 430 core - - in VS_OUT{ - vec2 tex_coords; - }fs_in; - - out vec4 frag_color; - - uniform sampler2D diffuse_texture; - - void main(){ - frag_color = texture(diffuse_texture, fs_in.tex_coords);; - } - )glsl"; +namespace wip::gfx{ + using base_shader = ogl::base_shader; } #endif diff --git a/include/wip/ogl_base_shader.hpp b/include/wip/ogl_base_shader.hpp new file mode 100644 index 0000000..d269904 --- /dev/null +++ b/include/wip/ogl_base_shader.hpp @@ -0,0 +1,87 @@ +/** + This file is a part of our_dick + Copyright (C) 2022 rexy712 + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +#ifndef OUR_DICK_GRAPHICS_WIP_OGL_BASE_SHADER_HPP +#define OUR_DICK_GRAPHICS_WIP_OGL_BASE_SHADER_HPP + +#include "shader_source.hpp" +#include "gfx/ogl/shader.hpp" + +namespace wip::gfx::ogl{ + + class base_shader + { + public: + static inline constexpr shader_source sources[] = { + { + .text = + R"glsl( + #version 430 core + + layout (location = 0) in vec3 position; + layout (location = 1) in vec2 tex_coords; + + + layout (std140) uniform mvp_matrix{ + mat4 vp_mat; + mat4 model_mat; + }; + + out VS_OUT{ + vec2 tex_coords; + }vs_out; + + const vec4 vertices[6] = vec4[](vec4(0,1,0,1), vec4(0,0,0,1), vec4(1,0,0,1), vec4(1,0,0,1), vec4(1,1,0,1), vec4(0,1,0,1)); + + void main(){ + gl_Position = vp_mat * model_mat * vec4(position, 1); + vs_out.tex_coords = tex_coords; + } + )glsl", + .len = 0, + .type = static_cast(::gfx::ogl::shader::type::VERTEX) + }, + { + .text = + R"glsl( + #version 430 core + + in VS_OUT{ + vec2 tex_coords; + }fs_in; + + out vec4 frag_color; + + uniform sampler2D diffuse_texture; + + void main(){ + frag_color = texture(diffuse_texture, fs_in.tex_coords);; + } + )glsl", + .len = 0, + .type = static_cast(::gfx::ogl::shader::type::FRAGMENT) + } + }; + static inline constexpr const char* vertex_shader_text = sources[0].text; + static inline constexpr const char* fragment_shader_text = sources[1].text; + }; + +} + + +#endif diff --git a/include/wip/shader_source.hpp b/include/wip/shader_source.hpp new file mode 100644 index 0000000..c2bbf97 --- /dev/null +++ b/include/wip/shader_source.hpp @@ -0,0 +1,34 @@ +/** + This file is a part of our_dick + Copyright (C) 2022 rexy712 + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +#ifndef OUR_DICK_GRAPHICS_WIP_SHADER_SOURCE_HPP +#define OUR_DICK_GRAPHICS_WIP_SHADER_SOURCE_HPP + +#include //size_t + +namespace wip::gfx{ + + struct shader_source{ + const char* text; + size_t len; + unsigned int type; + }; + +} + +#endif diff --git a/src/gfx/ogl/shader.cpp b/src/gfx/ogl/shader.cpp index 8f452d3..05aea7b 100644 --- a/src/gfx/ogl/shader.cpp +++ b/src/gfx/ogl/shader.cpp @@ -31,6 +31,8 @@ namespace gfx::ogl{ { load(data); } + shader::shader(const wip::gfx::shader_source& s): + shader(s.text, static_cast(s.type)){} shader::shader(const shader& s): shader(s.get_type()) { diff --git a/src/ttt/pause_state.cpp b/src/ttt/pause_state.cpp index 8ae9e07..a6760d6 100644 --- a/src/ttt/pause_state.cpp +++ b/src/ttt/pause_state.cpp @@ -43,8 +43,8 @@ pause_state::pause_state(egn::game& owner, egn::game_state_iface* under): m_elapsed_time(0) { //TODO hide GL implementation details somewhere else - auto vert = renderer().shaders().emplace_value("base_vert", wip::gfx::base_shader::vertex_shader_text, gfx::ogl::shader::type::VERTEX).first; - auto frag = renderer().shaders().emplace_value("base_frag", wip::gfx::base_shader::fragment_shader_text, gfx::ogl::shader::type::FRAGMENT).first; + auto vert = renderer().shaders().emplace_value("base_vert", wip::gfx::base_shader::sources[0]).first; + auto frag = renderer().shaders().emplace_value("base_frag", wip::gfx::base_shader::sources[1]).first; auto shader = renderer().shader_programs().emplace_value("base_shader", *vert, *frag).first; auto tex = renderer().textures().emplace_value("test_texture", util::make_deferred("assets/images/x.jpg", true)).first; auto material = renderer().materials().emplace_value("test_material", shader).first;