neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
gradient_manager.hpp
Go to the documentation of this file.
1// gradient_manager.hpp
2/*
3 neogfx C++ App/Game Engine
4 Copyright (c) 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>
23#include <unordered_set>
27
28namespace neogfx
29{
30 class gradient_sampler : public i_gradient_sampler
31 {
32 friend class gradient_manager;
33 public:
34 gradient_sampler(i_shader_array<avec4u8> const& aSampler, uint32_t aRow) :
35 iSampler{ &aSampler },
36 iRow{ aRow }
37 {
38 }
39 public:
40 i_shader_array<avec4u8> const& sampler() const override
41 {
42 return *iSampler;
43 }
44 uint32_t sampler_row() const override
45 {
46 return iRow;
47 }
48 public:
49 bool used_by(gradient_id aGradient) const override
50 {
51 return iReferences.find(aGradient) != iReferences.end();
52 }
53 void add_ref(gradient_id aGradient) const override
54 {
55 iReferences.insert(aGradient);
56 }
57 void release(gradient_id aGradient) const override
58 {
59 auto existing = iReferences.find(aGradient);
60 if (existing != iReferences.end())
61 iReferences.erase(existing);
62 }
63 void release_all() const override
64 {
65 iReferences.clear();
66 }
67 private:
68 i_shader_array<avec4u8> const* iSampler;
69 uint32_t iRow;
70 mutable std::unordered_set<gradient_id> iReferences;
71 };
72
73 constexpr uint32_t GRADIENT_FILTER_SIZE = 15;
74
75 class gradient_filter : public i_gradient_filter
76 {
77 friend class gradient_manager;
78 public:
80 iSampler{ std::make_shared<shader_array<float>>(size_u32{ GRADIENT_FILTER_SIZE, GRADIENT_FILTER_SIZE }) }
81 {
82 }
83 public:
84 i_shader_array<float> const& sampler() const override
85 {
86 return *iSampler;
87 }
88 private:
90 {
91 return *iSampler;
92 }
93 private:
94 std::shared_ptr<shader_array<float>> iSampler;
95 };
96
98 {
99 friend class gradient_object;
100 // types
101 protected:
105 typedef std::pair<gradient::color_stop_list, gradient::alpha_stop_list> sampler_key_t;
106 typedef std::map<sampler_key_t, gradient_sampler> sampler_map_t;
107 typedef std::map<scalar, gradient_filter> filter_map_t;
108 // constants
109 public:
110 static constexpr uint32_t MaxSamplers = 1024;
111 static constexpr uint32_t MaxFilters = 16;
112 // construction
113 public:
115 // operations
116 public:
117 void clear_gradients() override;
118 i_gradient_sampler const& sampler(i_gradient const& aGradient) override;
119 i_gradient_filter const& filter(i_gradient const& aGradient) override;
120 // implementation
121 protected:
123 void add_ref(gradient_id aId) override;
124 void release(gradient_id aId) override;
125 long use_count(gradient_id aId) const override;
126 protected:
128 const gradient_list& gradients() const;
131 private:
132 void do_find_gradient(gradient_id aId, neolib::i_ref_ptr<i_gradient>& aResult) const override;
133 void do_create_gradient(neolib::i_ref_ptr<i_gradient>& aResult) override;
134 void do_create_gradient(i_gradient const& aOther, neolib::i_ref_ptr<i_gradient>& aResult) override;
135 void do_create_gradient(i_string const& aCssDeclaration, neolib::i_ref_ptr<i_gradient>& aResult) override;
136 void do_create_gradient(sRGB_color const& aColor, neolib::i_ref_ptr<i_gradient>& aResult) override;
137 void do_create_gradient(sRGB_color const& aColor, gradient_direction aDirection, neolib::i_ref_ptr<i_gradient>& aResult) override;
138 void do_create_gradient(sRGB_color const& aColor1, sRGB_color const& aColor2, gradient_direction aDirection, neolib::i_ref_ptr<i_gradient>& aResult) override;
139 void do_create_gradient(i_gradient::color_stop_list const& aColorStops, gradient_direction aDirection, neolib::i_ref_ptr<i_gradient>& aResult) override;
140 void do_create_gradient(i_gradient::color_stop_list const& aColorStops, i_gradient::alpha_stop_list const& aAlphaStops, gradient_direction aDirection, neolib::i_ref_ptr<i_gradient>& aResult) override;
141 void do_create_gradient(i_gradient const& aOther, i_gradient::color_stop_list const& aColorStops, neolib::i_ref_ptr<i_gradient>& aResult) override;
142 void do_create_gradient(i_gradient const& aOther, i_gradient::color_stop_list const& aColorStops, i_gradient::alpha_stop_list const& aAlphaStops, neolib::i_ref_ptr<i_gradient>& aResult) override;
143 void do_create_gradient(neolib::i_vector<sRGB_color::abstract_type> const& aColors, gradient_direction aDirection, neolib::i_ref_ptr<i_gradient>& aResult) override;
144 private:
145 shader_array<avec4u8>& samplers();
146 std::vector<gradient_sampler>& free_samplers();
147 std::vector<gradient_filter>& free_filters();
148 void cleanup();
149 private:
150 gradient_list iGradients;
151 std::optional<shader_array<avec4u8>> iSamplers;
152 sampler_map_t iAllocatedSamplers;
153 std::optional<std::vector<gradient_sampler>> iFreeSamplers;
154 std::deque<sampler_map_t::const_iterator> iSamplerQueue;
155 filter_map_t iAllocatedFilters;
156 std::optional<std::vector<gradient_filter>> iFreeFilters;
157 std::deque<filter_map_t::const_iterator> iFilterQueue;
158 };
159}
i_shader_array< float > const & sampler() const override
std::map< scalar, gradient_filter > filter_map_t
void release(gradient_id aId) override
i_gradient_sampler const & sampler(i_gradient const &aGradient) override
neolib::ref_ptr< i_gradient > add_gradient(neolib::ref_ptr< i_gradient > aGradient)
neolib::pair< gradient_pointer, uint32_t > gradient_list_entry
void add_ref(gradient_id aId) override
const gradient_list & gradients() const
std::pair< gradient::color_stop_list, gradient::alpha_stop_list > sampler_key_t
friend neolib::cookie item_cookie(gradient_list_entry const &)
gradient_id allocate_gradient_id()
static constexpr uint32_t MaxSamplers
void clear_gradients() override
std::map< sampler_key_t, gradient_sampler > sampler_map_t
static constexpr uint32_t MaxFilters
neolib::jar< gradient_list_entry > gradient_list
i_gradient_filter const & filter(i_gradient const &aGradient) override
gradient_list & gradients()
ref_ptr< i_gradient > gradient_pointer
long use_count(gradient_id aId) const override
void release_all() const override
bool used_by(gradient_id aGradient) const override
void release(gradient_id aGradient) const override
i_shader_array< avec4u8 > const & sampler() const override
uint32_t sampler_row() const override
gradient_sampler(i_shader_array< avec4u8 > const &aSampler, uint32_t aRow)
void add_ref(gradient_id aGradient) const override
constexpr uint32_t GRADIENT_FILTER_SIZE
game::id_t gradient_id
basic_size< uint32_t > size_u32
gradient_direction
uint32_t cookie
Definition i_jar.hpp:44
basic_jar< T, vector< T >, cookie, MutexType > jar
Definition jar.hpp:736
Definition plf_hive.h:79