neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
i_shader_program.hpp
Go to the documentation of this file.
1// i_shader_program.hpp
2/*
3 neogfx C++ App/Game Engine
4 Copyright (c) 2019, 2020 Leigh Johnston. All Rights Reserved.
5
6 This program is free software: you can redistribute it and / or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#pragma once
21
22#include <neogfx/neogfx.hpp>
30
31namespace neogfx
32{
33 struct shader_stage_not_found : std::logic_error { shader_stage_not_found() : std::logic_error{ "neogfx::shader_stage_not_found" } {} };
34 struct shader_not_found : std::logic_error { shader_not_found() : std::logic_error{ "neogfx::shader_not_found" } {} };
35 struct shader_name_exists : std::logic_error { shader_name_exists() : std::logic_error{ "neogfx::shader_name_exists" } {} };
36 struct no_vertex_shader : std::logic_error { no_vertex_shader() : std::logic_error{ "neogfx::no_vertex_shader" } {} };
37 struct no_fragment_shader : std::logic_error { no_fragment_shader() : std::logic_error{ "neogfx::no_fragment_shader" } {} };
38 struct shader_last_in_stage : std::logic_error { shader_last_in_stage() : std::logic_error{ "neogfx::shader_last_in_stage" } {} };
39 struct shader_program_dirty : std::logic_error { shader_program_dirty() : std::logic_error("neogfx::shader_program_dirty") {} };
40 struct failed_to_create_shader : std::runtime_error { failed_to_create_shader() : std::runtime_error("neogfx::failed_to_create_shader") {} };
41 struct failed_to_create_shader_program : std::runtime_error { failed_to_create_shader_program(std::string const& aReason) : std::runtime_error("neogfx::failed_to_create_shader_program: " + aReason) {} };
42 struct shader_program_error : std::runtime_error { shader_program_error(std::string const& aError) : std::runtime_error("neogfx::shader_program_error: " + aError) {} };
43
44 enum class shader_program_type : uint32_t
45 {
47 User
48 };
49
51 {
52 // types
53 public:
55 public:
58 // construction
59 public:
60 virtual ~i_shader_stage() = default;
61 // operations
62 public:
63 virtual shader_type type() const = 0;
64 virtual i_shaders_t const& shaders() const = 0;
65 virtual i_shaders_t& shaders() = 0;
66 };
67
69 {
70 // types
71 public:
73 public:
76 // construction
77 public:
78 virtual ~i_shader_program() = default;
79 // operations
80 public:
81 virtual shader_program_type type() const = 0;
82 virtual const i_string& name() const = 0;
83 virtual bool supports(vertex_buffer_type aBufferType) const = 0;
84 virtual bool created() const = 0;
85 virtual void* handle() const = 0;
86 virtual const i_stages_t& stages() const = 0;
87 virtual i_stages_t& stages() = 0;
88 virtual const i_stage_t& stage(shader_type aStage) const = 0;
89 virtual i_stage_t& stage(shader_type aStage) = 0;
90 virtual const i_shader& shader(const neolib::i_string& aName) const = 0;
91 virtual i_shader& shader(const neolib::i_string& aName) = 0;
92 virtual const i_vertex_shader& vertex_shader() const = 0;
94 virtual const i_fragment_shader& fragment_shader() const = 0;
96 virtual bool is_first_in_stage(const i_shader& aShader) const = 0;
97 virtual bool is_last_in_stage(const i_shader& aShader) const = 0;
98 virtual const i_shader& first_in_stage(shader_type aStage) const = 0;
99 virtual const i_shader& next_in_stage(const i_shader& aPreviousShader) const = 0;
100 virtual const i_shader& last_in_stage(shader_type aStage) const = 0;
101 virtual i_shader& add_shader(const neolib::i_ref_ptr<i_shader>& aShader) = 0;
102 virtual bool dirty() const = 0;
103 virtual void set_clean() = 0;
104 virtual void prepare_uniforms(const i_rendering_context& aContext) = 0;
105 virtual void make() = 0;
106 virtual void compile() = 0;
107 virtual void link() = 0;
108 virtual void use() = 0;
109 virtual void update_uniform_storage() = 0;
110 virtual void update_uniform_locations() = 0;
111 virtual bool uniforms_changed() const = 0;
112 virtual void update_uniforms(const i_rendering_context& aContext) = 0;
113 virtual bool active() const = 0;
114 virtual void activate(const i_rendering_context& aContext) = 0;
115 virtual void deactivate() = 0;
116 virtual void instantiate(const i_rendering_context& aContext) = 0;
117 public:
118 template <typename T>
119 const T& as() const
120 {
121 return static_cast<const T&>(*this);
122 }
123 template <typename T>
124 T& as()
125 {
126 return static_cast<T&>(*this);
127 }
128 bool have_stage(shader_type aStage) const
129 {
130 return !stages().at(static_cast<std::size_t>(aStage))->shaders().empty();
131 }
132 bool stage_clean(shader_type aStage) const
133 {
134 if (!have_stage(aStage))
135 return true;
136 const i_shader* shader = &first_in_stage(aStage);
137 for(;;)
138 {
139 if (shader->dirty())
140 return false;
142 return true;
144 }
145 }
146 bool stage_dirty(shader_type aStage) const
147 {
148 return !stage_clean(aStage);
149 }
150 };
151}
virtual const i_shader & shader(const neolib::i_string &aName) const =0
virtual bool dirty() const =0
virtual bool active() const =0
virtual const i_vertex_shader & vertex_shader() const =0
bool stage_dirty(shader_type aStage) const
virtual void update_uniforms(const i_rendering_context &aContext)=0
virtual void instantiate(const i_rendering_context &aContext)=0
neolib::i_ref_ptr< i_shader_stage > i_stage_t
virtual bool supports(vertex_buffer_type aBufferType) const =0
virtual shader_program_type type() const =0
virtual void prepare_uniforms(const i_rendering_context &aContext)=0
virtual i_vertex_shader & vertex_shader()=0
virtual i_shader & shader(const neolib::i_string &aName)=0
virtual i_fragment_shader & fragment_shader()=0
virtual bool is_last_in_stage(const i_shader &aShader) const =0
virtual ~i_shader_program()=default
virtual void deactivate()=0
virtual const i_shader & first_in_stage(shader_type aStage) const =0
virtual const i_shader & next_in_stage(const i_shader &aPreviousShader) const =0
virtual bool created() const =0
neolib::i_vector< i_stage_t > i_stages_t
virtual void make()=0
virtual void compile()=0
virtual i_stage_t & stage(shader_type aStage)=0
virtual const i_fragment_shader & fragment_shader() const =0
virtual bool is_first_in_stage(const i_shader &aShader) const =0
virtual i_shader & add_shader(const neolib::i_ref_ptr< i_shader > &aShader)=0
virtual void use()=0
virtual void update_uniform_storage()=0
virtual i_stages_t & stages()=0
virtual void activate(const i_rendering_context &aContext)=0
virtual void * handle() const =0
virtual void update_uniform_locations()=0
virtual void set_clean()=0
bool stage_clean(shader_type aStage) const
bool have_stage(shader_type aStage) const
virtual const i_string & name() const =0
virtual const i_stage_t & stage(shader_type aStage) const =0
virtual void link()=0
virtual const i_shader & last_in_stage(shader_type aStage) const =0
virtual bool uniforms_changed() const =0
virtual const i_stages_t & stages() const =0
neolib::i_ref_ptr< i_shader > i_shader_t
virtual shader_type type() const =0
neolib::i_vector< i_shader_t > i_shaders_t
virtual ~i_shader_stage()=default
virtual i_shaders_t & shaders()=0
virtual i_shaders_t const & shaders() const =0
bool dirty() const final
Definition shader.hpp:144
virtual const value_type & at(size_type aIndex) const =0
failed_to_create_shader_program(std::string const &aReason)
shader_program_error(std::string const &aError)