Gamp v0.0.8
Gamp: Graphics, Audio, Multimedia and Processing
Loading...
Searching...
No Matches
RenderContext.hpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright Gothel Software e.K.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 * This Source Code Form is subject to the terms of the MIT License
8 * If a copy of the MIT was not distributed with this file,
9 * you can obtain one at https://opensource.org/license/mit/.
10 */
11
12#ifndef GAMP_RENDER_RENDERCONTEXT_HPP_
13#define GAMP_RENDER_RENDERCONTEXT_HPP_
14
15#include <jau/basic_types.hpp>
16#include <jau/int_math_ct.hpp>
17#include <jau/int_types.hpp>
18#include <jau/float_types.hpp>
19#include <jau/string_util.hpp>
20#include <jau/enum_util.hpp>
22
23#include <gamp/GampTypes.hpp>
24
25namespace gamp::wt {
26 class Surface;
27 typedef std::shared_ptr<Surface> SurfaceSRef;
28}
29namespace gamp::render {
30
31 /** @defgroup Gamp_Render Gamp Rendering
32 * Managed rendering support, data handling and functionality.
33 *
34 * @{
35 */
36 using namespace jau::enums;
37
38 /** OpenGL context flags. */
39 enum class RenderContextFlags : uint32_t {
40 none = 0,
41 /** Compatible context. */
42 compatible = 1U << 0,
43 /** Debug context. */
44 debug = 1U << 1,
45 /** Robust context. */
46 robust = 1U << 2,
47 /** Software rasterizer context. */
48 software = 1U << 3,
49 /** Verbose operations (debugging). */
50 verbose = 1U << 31
51 };
53
54 class RenderContext;
55 typedef std::shared_ptr<RenderContext> RenderContextSRef;
56
57 /**
58 * Specifies the render profile.
59 */
61 public:
62 /** The default profile, used for the device default profile map */
63 constexpr static std::string_view RP_UNDEF = "undef";
64
65 private:
66 std::string_view m_profile;
68
69 protected:
71
72 virtual void clear() noexcept {
73 m_profile = "";
74 m_version = jau::util::VersionNumber();
75 }
76 static const RenderProfile& getUndef() noexcept {
77 static RenderProfile a;
78 return a;
79 }
80
81 public:
82 /** Create an undefined instance.*/
83 constexpr RenderProfile() noexcept : m_profile(RP_UNDEF), m_version() {}
84
85 /** Create an instance w/ unique name.*/
86 constexpr RenderProfile(const std::string_view profile, const jau::util::VersionNumber& version) noexcept
87 : m_profile(profile), m_version(version)
88 {}
89
90 virtual ~RenderProfile() noexcept = default;
91
92 virtual const jau::type_info& signature() const noexcept { return jau::static_ctti<RenderProfile>(); }
93
94 constexpr const jau::util::VersionNumber& version() const noexcept { return m_version; }
95 constexpr const std::string_view name() const noexcept { return m_profile; }
96
97 constexpr bool operator==(const RenderProfile& rhs) const noexcept {
98 return signature() == rhs.signature() && m_profile == rhs.m_profile && m_version == rhs.version();
99 }
100
101 virtual std::string toString() const {
102 return std::string("RenderProfile[").append(signature().name()).append(", ")
103 .append(name()).append(" ").append(m_version.toString()).append("]");
104 }
105 };
106 inline std::ostream& operator<<(std::ostream& out, const RenderProfile& v) {
107 return out << v.toString();
108 }
109
110 /** Rendering Context */
112 private:
113 gamp::handle_t m_context;
114 jau::util::VersionNumber m_version;
115 RenderContextFlags m_flags;
116 StringViewAttachables m_attachables;
117
118 protected:
119 struct Private { explicit Private() = default; };
121
122 public:
123 /** Private: Create an invalid instance.*/
125 : m_context(0), m_version(), m_flags() { }
126
127 /** Private: Create an instance. Given profile tag must be a valid implementation profile. */
132
133 virtual ~RenderContext() noexcept = default;
134
135 virtual const jau::type_info& signature() const noexcept { return jau::static_ctti<RenderContext>(); }
136
137 virtual const RenderProfile& renderProfile() const noexcept { return RenderProfile::getUndef(); }
138
139 constexpr bool isValid() const noexcept { return 0 != m_context; }
140 constexpr gamp::handle_t context() const noexcept { return m_context; }
141 constexpr const jau::util::VersionNumber& version() const { return m_version; }
142
143 constexpr RenderContextFlags contextFlags() const noexcept { return m_flags; }
144
145 virtual void dispose() noexcept {}
146
147 virtual void disposedNotify() {
148 m_surface = nullptr;
149 m_context = 0;
150 m_version = jau::util::VersionNumberString();
152 }
153
154 /// Make this context current (used for OpenGL, but a NOP on Vulkan)
155 virtual bool makeCurrent(const gamp::wt::SurfaceSRef& s) noexcept { m_surface=s; return true; }
156 /// Release this context (used for OpenGL, but a NOP on Vulkan)
157 virtual void releaseContext() noexcept { m_surface = nullptr; }
158 const gamp::wt::SurfaceSRef& boundSurface() const noexcept { return m_surface; }
159
160 /** Returns the attached user object for the given name. */
161 AttachableSRef getAttachedObject(std::string_view key) const { return m_attachables.get(key); }
162
163 /** Clears the attachment map. */
164 void clearAttachedObjects() { m_attachables.clear(); }
165
166 /**
167 * Attaches user object for the given name, overwrites old mapping if exists.
168 * @param key persistent std::string_view key, must be valid through the lifecycle of this instance
169 * @return previously set object or nullptr.
170 */
171 AttachableSRef attachObject(std::string_view key, const AttachableSRef& obj) { return m_attachables.put3(key, obj); }
172
173 /** Removes attached object if exists and returns it, otherwise returns nullptr. */
174 AttachableSRef detachObject(std::string_view key) { return m_attachables.remove2(key); }
175
176 virtual std::string toString() const;
177 };
178 typedef std::unique_ptr<RenderContext> RenderContextPtr;
179
180 inline std::ostream& operator<<(std::ostream& out, const RenderContext& v) {
181 return out << v.toString();
182 }
183 /**@}*/
184
185} // namespace gamp::render
186
187
188#endif /* GAMP_RENDER_RENDERCONTEXT_HPP_ */
const gamp::wt::SurfaceSRef & boundSurface() const noexcept
virtual void dispose() noexcept
RenderContext(Private) noexcept
Private: Create an invalid instance.
AttachableSRef attachObject(std::string_view key, const AttachableSRef &obj)
Attaches user object for the given name, overwrites old mapping if exists.
virtual ~RenderContext() noexcept=default
virtual bool makeCurrent(const gamp::wt::SurfaceSRef &s) noexcept
Make this context current (used for OpenGL, but a NOP on Vulkan)
gamp::wt::SurfaceSRef m_surface
void clearAttachedObjects()
Clears the attachment map.
AttachableSRef detachObject(std::string_view key)
Removes attached object if exists and returns it, otherwise returns nullptr.
virtual std::string toString() const
Definition gamp.cpp:62
RenderContext(Private, gamp::handle_t context, RenderContextFlags contextFlags, const jau::util::VersionNumber &version) noexcept
Private: Create an instance.
virtual const jau::type_info & signature() const noexcept
AttachableSRef getAttachedObject(std::string_view key) const
Returns the attached user object for the given name.
constexpr gamp::handle_t context() const noexcept
virtual void releaseContext() noexcept
Release this context (used for OpenGL, but a NOP on Vulkan)
constexpr RenderContextFlags contextFlags() const noexcept
constexpr const jau::util::VersionNumber & version() const
virtual const RenderProfile & renderProfile() const noexcept
constexpr bool isValid() const noexcept
Specifies the render profile.
constexpr const std::string_view name() const noexcept
virtual std::string toString() const
constexpr const jau::util::VersionNumber & version() const noexcept
constexpr RenderProfile(const std::string_view profile, const jau::util::VersionNumber &version) noexcept
Create an instance w/ unique name.
virtual ~RenderProfile() noexcept=default
virtual void clear() noexcept
virtual const jau::type_info & signature() const noexcept
constexpr bool operator==(const RenderProfile &rhs) const noexcept
constexpr RenderProfile() noexcept
Create an undefined instance.
static const RenderProfile & getUndef() noexcept
static constexpr std::string_view RP_UNDEF
The default profile, used for the device default profile map.
Generic type information using either Runtime type information (RTTI) or Compile time type informatio...
Simple version number class containing a version number either being defined explicit or derived from...
Simple version number class containing a version number either being defined explicit or derived from...
#define JAU_MAKE_BITFIELD_ENUM_STRING(type,...)
std::ostream & operator<<(std::ostream &os, const T v)
const jau::type_info & static_ctti() noexcept
Returns a static global reference of make_ctti<T>(true) w/ identity instance.
std::unique_ptr< RenderContext > RenderContextPtr
RenderContextFlags
OpenGL context flags.
std::shared_ptr< RenderContext > RenderContextSRef
@ verbose
Verbose operations (debugging).
@ software
Software rasterizer context.
std::shared_ptr< Surface > SurfaceSRef
uintptr_t handle_t
A native handle type, big enough to store a pointer.
Definition GampTypes.hpp:52
std::shared_ptr< Attachable > AttachableSRef
Definition GampTypes.hpp:88
jau::StringViewHashMapWrap< AttachableSRef, std::nullptr_t, nullptr > StringViewAttachables
Definition GampTypes.hpp:91
Author: Sven Gothel sgothel@jausoft.com Copyright Gothel Software e.K.
Definition enum_util.hpp:65
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
Definition backtrace.hpp:32