neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
fragment_shader.hpp
Go to the documentation of this file.
1// fragment_shader.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#include <neogfx/gfx/shader.hpp>
32#include "standard.frag.hpp"
33
34namespace neogfx
35{
36 template <typename Base = i_fragment_shader>
37 class fragment_shader : public shader<Base>
38 {
39 typedef shader<Base> base_type;
40 public:
41 fragment_shader(std::string const& aName) :
43 {
44 }
45 };
46
47 template <typename Base = i_fragment_shader>
49 {
51 public:
52 using base_type::add_in_variable;
53 using base_type::add_out_variable;
54 public:
55 standard_fragment_shader(std::string const& aName = "standard_fragment_shader") :
56 fragment_shader<Base>{ aName }
57 {
58 add_in_variable<vec3f>("Coord"_s, 0u);
59 auto& fragColor = add_in_variable<vec4f>("Color"_s, 1u);
60 add_in_variable<vec2f>("TexCoord"_s, 2u);
61 auto& fragFunction0 = add_in_variable<vec4f>("Function0"_s, 3u, true);
62 auto& fragFunction1 = add_in_variable<vec4f>("Function1"_s, 4u, true);
63 auto& fragFunction2 = add_in_variable<vec4f>("Function2"_s, 5u, true);
64 auto& fragFunction3 = add_in_variable<vec4f>("Function3"_s, 6u, true);
65 auto& fragFunction4 = add_in_variable<vec4f>("Function4"_s, 7u, true);
66 add_out_variable<vec4f>("FragColor"_s, 0u).link(fragColor);
67 add_out_variable<vec4f>("FragFunction0"_s, 1u).link(fragFunction0);
68 add_out_variable<vec4f>("FragFunction1"_s, 2u).link(fragFunction1);
69 add_out_variable<vec4f>("FragFunction2"_s, 3u).link(fragFunction2);
70 add_out_variable<vec4f>("FragFunction3"_s, 4u).link(fragFunction3);
71 add_out_variable<vec4f>("FragFunction4"_s, 5u).link(fragFunction4);
72 }
73 public:
85 void generate_code(const i_shader_program& aProgram, shader_language aLanguage, i_string& aOutput) const override
86 {
87 fragment_shader<Base>::generate_code(aProgram, aLanguage, aOutput);
88 if (aProgram.is_first_in_stage(*this))
89 {
90 if (aLanguage == shader_language::Glsl)
91 aOutput += string{ glsl::StandardFragmentShader };
92 else
93 throw unsupported_shader_language();
94 }
95 }
96 };
97
98 class standard_gradient_shader : public standard_fragment_shader<i_gradient_shader>
99 {
100 public:
101 standard_gradient_shader(std::string const& aName = "standard_gradient_shader");
102 public:
103 void generate_code(const i_shader_program& aProgram, shader_language aLanguage, i_string& aOutput) const override;
104 public:
105 void clear_gradient() final;
106 void set_gradient(i_rendering_context& aContext, const gradient& aGradient) final;
107 void set_gradient(i_rendering_context& aContext, const game::gradient& aGradient) final;
108 private:
109 cache_uniform(uGradientGuiCoordinates)
110 cache_uniform(uGradientDirection)
111 cache_uniform(uGradientAngle)
112 cache_uniform(uGradientStartFrom)
113 cache_uniform(uGradientSize)
114 cache_uniform(uGradientShape)
115 cache_uniform(uGradientExponents)
116 cache_uniform(uGradientCenter)
117 cache_uniform(uGradientTile)
118 cache_uniform(uGradientTileParams)
119 cache_uniform(uGradientColorCount)
120 cache_uniform(uGradientColorRow)
121 cache_uniform(uGradientColors)
122 cache_uniform(uGradientFilterSize)
123 cache_uniform(uGradientFilter)
124 cache_uniform(uGradientEnabled)
125 };
126
128 {
129 public:
130 standard_texture_shader(std::string const& aName = "standard_texture_shader");
131 public:
132 bool supports(vertex_buffer_type aBufferType) const override;
133 public:
134 void generate_code(const i_shader_program& aProgram, shader_language aLanguage, i_string& aOutput) const override;
135 public:
136 void clear_texture() final;
137 void set_texture(const i_texture& aTexture) final;
138 void set_effect(shader_effect aEffect) final;
139 private:
140 cache_uniform(uTextureEnabled)
141 cache_uniform(uTextureDataFormat)
142 cache_uniform(uTextureMultisample)
143 cache_uniform(uTextureExtents)
144 cache_uniform(uTextureEffect)
145 };
146
148 {
149 public:
150 standard_filter_shader(std::string const& aName = "standard_filter_shader");
151 public:
152 bool supports(vertex_buffer_type aBufferType) const override;
153 public:
154 void generate_code(const i_shader_program& aProgram, shader_language aLanguage, i_string& aOutput) const override;
155 public:
156 void clear_filter() final;
157 void set_filter(shader_filter aFilter, scalar aArgument1 = 0.0, scalar aArgument2 = 0.0, scalar aArgument3 = 0.0, scalar aArgument4 = 0.0) final;
158 private:
159 cache_uniform(uFilterEnabled)
160 cache_uniform(uFilterType)
161 cache_uniform(uFilterArguments)
162 cache_uniform(uFilterKernelSize)
163 cache_uniform(uFilterKernel)
164 std::map<std::pair<shader_filter, vec4>, std::optional<shader_array<float>>> iFilterKernel;
165 };
166
168 {
169 public:
170 standard_glyph_shader(std::string const& aName = "standard_glyph_shader");
171 public:
172 void generate_code(const i_shader_program& aProgram, shader_language aLanguage, i_string& aOutput) const override;
173 public:
174 void clear_glyph() final;
175 void set_first_glyph(const i_rendering_context& aContext, const glyph_text& aText, const glyph_char& aGlyphChar) final;
176 private:
177 cache_uniform(uGlyphRenderTargetExtents)
178 cache_uniform(uGlyphGuiCoordinates)
179 cache_uniform(uGlyphRenderOutput)
180 cache_uniform(uGlyphSubpixel)
181 cache_uniform(uGlyphSubpixelFormat)
182 cache_uniform(uGlyphEnabled)
183 };
184
186 {
187 public:
188 standard_stipple_shader(std::string const& aName = "standard_stipple_shader");
189 public:
190 void generate_code(const i_shader_program& aProgram, shader_language aLanguage, i_string& aOutput) const override;
191 public:
192 bool stipple_active() const final;
193 void clear_stipple() final;
194 void set_stipple(scalar aFactor, uint16_t aPattern, scalar aPosition = 0.0) final;
195 void start(const i_rendering_context& aContext, const vec3& aFrom) final;
196 void next(const i_rendering_context& aContext, const vec3& aFrom, scalar aPositionOffset) final;
197 private:
198 scalar iPosition;
199 private:
200 cache_uniform(uStippleFactor)
201 cache_uniform(uStipplePattern)
202 cache_uniform(uStipplePosition)
203 cache_uniform(uStippleVertex)
204 cache_uniform(uStippleEnabled)
205 };
206
208 {
209 public:
210 standard_shape_shader(std::string const& aName = "standard_shape_shader");
211 public:
212 void generate_code(const i_shader_program& aProgram, shader_language aLanguage, i_string& aOutput) const override;
213 public:
214 bool shape_active() const final;
215 void clear_shape() final;
216 void set_shape(shader_shape aShape) final;
217 private:
218 cache_shared_uniform(uShapeEnabled)
220 };
221}
fragment_shader(std::string const &aName)
virtual bool is_first_in_stage(const i_shader &aShader) const =0
void generate_code(const i_shader_program &aProgram, shader_language aLanguage, i_string &aOutput) const override
Definition shader.hpp:267
void generate_code(const i_shader_program &aProgram, shader_language aLanguage, i_string &aOutput) const override
standard_filter_shader(std::string const &aName="standard_filter_shader")
bool supports(vertex_buffer_type aBufferType) const override
void generate_code(const i_shader_program &aProgram, shader_language aLanguage, i_string &aOutput) const override
standard_fragment_shader(std::string const &aName="standard_fragment_shader")
bool supports(vertex_buffer_type aBufferType) const override
standard_glyph_shader(std::string const &aName="standard_glyph_shader")
void generate_code(const i_shader_program &aProgram, shader_language aLanguage, i_string &aOutput) const override
standard_gradient_shader(std::string const &aName="standard_gradient_shader")
void generate_code(const i_shader_program &aProgram, shader_language aLanguage, i_string &aOutput) const override
void set_gradient(i_rendering_context &aContext, const gradient &aGradient) final
void generate_code(const i_shader_program &aProgram, shader_language aLanguage, i_string &aOutput) const override
standard_shape_shader(std::string const &aName="standard_shape_shader")
bool shape_active() const final
void generate_code(const i_shader_program &aProgram, shader_language aLanguage, i_string &aOutput) const override
bool stipple_active() const final
standard_stipple_shader(std::string const &aName="standard_stipple_shader")
standard_texture_shader(std::string const &aName="standard_texture_shader")
void generate_code(const i_shader_program &aProgram, shader_language aLanguage, i_string &aOutput) const override
bool supports(vertex_buffer_type aBufferType) const override
shader_language
Definition i_shader.hpp:64
double scalar
Definition numerical.hpp:63
Definition plf_hive.h:79
#define cache_shared_uniform(uniformName)
Definition shader.hpp:33
#define cache_uniform(uniformName)
Definition shader.hpp:32