Visual Computing Library
Loading...
Searching...
No Matches
context.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_BGFX_CONTEXT_H
24#define VCL_BGFX_CONTEXT_H
25
26#include "context/callback.h"
27#include "context/font_manager.h"
28#include "context/program_manager.h"
29
30#include <bgfx/bgfx.h>
31#include <bgfx/platform.h>
32
33#include <mutex>
34#include <stack>
35
36#define BGFX_INVALID_VIEW 65535
37
38namespace vcl {
39
41{
42 // when initialization is done with a window handle
43 // this will become the main window handle (withe the *special* framebuffer)
44 // otherwise (null window handle) means that the context is headless (no UI)
45 void* mWindowHandle = nullptr;
46 void* mDisplayHandle = nullptr;
47
48 std::stack<bgfx::ViewId> mViewStack;
49
50 Callback mCallBack;
51 FontManager* mFontManager = nullptr;
52 ProgramManager* mProgramManager = nullptr;
53
54 inline static bgfx::RendererType::Enum sRenderType =
55 bgfx::RendererType::Count;
56
57 inline static uint sResetFlags = BGFX_RESET_VSYNC;
58
59 // singleton
60 inline static Context* sInstancePtr = nullptr;
61 inline static std::mutex sMutex;
62
63public:
64 // default values, used for optional parameters
65 static constexpr uint32_t DEFAULT_CLEAR_COLOR = 0x000000ff;
66 static constexpr float DEFAULT_CLEAR_DEPTH = 1.0f;
67 static constexpr uint8_t DEFAULT_CLEAR_STENCIL = 0;
68 static constexpr bgfx::TextureFormat::Enum DEFAULT_COLOR_FORMAT =
69 bgfx::TextureFormat::RGBA8;
70 static constexpr bgfx::TextureFormat::Enum DEFAULT_DEPTH_FORMAT =
71 bgfx::TextureFormat::D24S8;
72
81 static Context& instance(
82 void* windowHandle = nullptr,
83 void* displayHandle = nullptr);
84
85 static void init(
86 void* windowHandle = nullptr,
87 void* displayHandle = nullptr);
88
89 static void initHeadless();
90
91 static bool isInitialized();
92
93 static void shutdown();
94
95 static bgfx::RendererType::Enum renderType();
96
97 static void setRenderType(bgfx::RendererType::Enum renderType);
98
99 static void setResetFlags(uint flags);
100
101 static void setDebugVerbosity(bool verbose);
102
103 bool isHeadless() const;
104
105 const bgfx::Caps& capabilites() const;
106
107 bool supportsReadback() const;
108
115 bool isDefaultWindow(void* windowHandle) const;
116
117 bool isValidViewId(bgfx::ViewId viewId) const;
118
119 bgfx::ViewId requestViewId();
120
121 void releaseViewId(bgfx::ViewId viewId);
122
133 bgfx::FrameBufferHandle createOffscreenFramebuffer(
134 uint16_t width,
135 uint16_t height,
136 bgfx::TextureFormat::Enum colorFormat = DEFAULT_COLOR_FORMAT,
137 bgfx::TextureFormat::Enum depthFormat = DEFAULT_DEPTH_FORMAT);
138
139 void resetDefaultFramebuffer(
140 uint16_t width,
141 uint16_t height,
142 bgfx::TextureFormat::Enum colorFormat = DEFAULT_COLOR_FORMAT);
143
144 bgfx::FrameBufferHandle createFramebufferAndInitView(
145 void* winId,
146 bgfx::ViewId view,
147 uint16_t width,
148 uint16_t height,
149 bool clear = false,
150 uint32_t clearColor = DEFAULT_CLEAR_COLOR,
151 float clearDepth = DEFAULT_CLEAR_DEPTH,
152 uint8_t clearStencil = DEFAULT_CLEAR_STENCIL,
153 bgfx::TextureFormat::Enum colorFormat = DEFAULT_COLOR_FORMAT,
154 bgfx::TextureFormat::Enum depthFormat = DEFAULT_DEPTH_FORMAT);
155
156 bgfx::FrameBufferHandle createOffscreenFramebufferAndInitView(
157 bgfx::ViewId view,
158 uint16_t width,
159 uint16_t height,
160 bool clear = false,
161 uint32_t clearColor = DEFAULT_CLEAR_COLOR,
162 float clearDepth = DEFAULT_CLEAR_DEPTH,
163 uint8_t clearStencil = DEFAULT_CLEAR_STENCIL,
164 bgfx::TextureFormat::Enum colorFormat = DEFAULT_COLOR_FORMAT,
165 bgfx::TextureFormat::Enum depthFormat = DEFAULT_DEPTH_FORMAT);
166
167 FontManager& fontManager();
168
169 ProgramManager& programManager();
170
171private:
172 Context(void* windowHandle, void* displayHandle);
173
174 ~Context();
175
176public:
177 Context(const Context&) = delete;
178 Context& operator=(const Context&) = delete;
179 Context(Context&&) = delete;
180 Context& operator=(Context&&) = delete;
181};
182
183} // namespace vcl
184
185#endif // VCL_BGFX_CONTEXT_H
Definition callback.h:31
Definition context.h:41
bool isDefaultWindow(void *windowHandle) const
Checks whether the context is initialized with the provided window handle.
Definition context.cpp:152
static void setResetFlags(uint flags)
Set the reset flags used by the default frame buffer.
Definition context.cpp:108
static void setRenderType(bgfx::RendererType::Enum renderType)
Set the backend renderer type used by bgfx.
Definition context.cpp:89
const bgfx::Caps & capabilites() const
Return the capabilities of the backend renderer.
Definition context.cpp:126
static Context & instance(void *windowHandle=nullptr, void *displayHandle=nullptr)
Return the context instance.
Definition context.cpp:365
static bgfx::RendererType::Enum renderType()
Return the backend renderer type used by bgfx.
Definition context.cpp:67
bgfx::FrameBufferHandle createOffscreenFramebuffer(uint16_t width, uint16_t height, bgfx::TextureFormat::Enum colorFormat=DEFAULT_COLOR_FORMAT, bgfx::TextureFormat::Enum depthFormat=DEFAULT_DEPTH_FORMAT)
Create a framebuffer with with 2 attachments (color and depth)
Definition context.cpp:166
Definition font_manager.h:36
Definition program_manager.h:36
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43