Gamp v0.0.8
Gamp: Graphics, Audio, Multimedia and Processing
Loading...
Searching...
No Matches
GLCapabilities.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#ifndef GAMP_RENDER_GL_GLCAPABILITIES_HPP_
12#define GAMP_RENDER_GL_GLCAPABILITIES_HPP_
13
14#include <jau/string_util.hpp>
15
16#include <gamp/GampTypes.hpp>
18
19namespace gamp::render::gl {
20 /** \addtogroup Gamp_GL
21 *
22 * @{
23 */
24
25 using namespace gamp::wt;
26
27 /**
28 * Specifies a set of OpenGL capabilities.
29 *
30 * At creation time of a {@link GLDrawable} using {@link GLDrawableFactory},
31 * an instance of this class is passed,
32 * describing the desired capabilities that a rendering context
33 * must support, such as the OpenGL profile, color depth and whether stereo is enabled.
34 *
35 * The actual capabilites of created {@link GLDrawable}s are then reflected by their own
36 * GLCapabilites instance, which can be queried with {@link GLDrawable#getChosenGLCapabilities()}.
37 *
38 * It currently contains the minimal number of routines which allow
39 * configuration on all supported window systems.
40 *
41 * Default (bit-sizes):
42 * - bit sizes
43 * - red 8
44 * - green 8
45 * - blue 8
46 * - alpha 8
47 * - transparent off (zero)
48 * - depth 16
49 * - stencil 0
50 * - depth 16
51 * - stencil 0
52 * - accum 0 (rgba)
53 * - multi-samples-count 0
54 * - flags
55 * - onscreen true
56 * - bitmap false
57 * - fbo false
58 * - double-buffered true
59 * - stereo false
60 * - hardware-accelerated true
61 */
63 private:
64 bool m_isFBO = false;
65 bool m_doubleBuffered = true;
66 bool m_stereo = false;
67 bool m_hardwareAccelerated = true;
68 int m_depthBits = 16;
69 int m_stencilBits = 0;
70 int m_accumRedBits = 0;
71 int m_accumGreenBits = 0;
72 int m_accumBlueBits = 0;
73 int m_accumAlphaBits = 0;
74
75 // Using full-scene antialiasing (FSAA) if value > 1,
76 // TODO: std::string sampleExtension = DEFAULT_SAMPLE_EXTENSION;
77 int m_multiSamplesCount = 0;
78
79 public:
80 constexpr GLCapabilities() noexcept = default;
81 constexpr GLCapabilities(const GLCapabilities&) noexcept = default;
82 constexpr GLCapabilities(GLCapabilities&&) noexcept = default;
83 GLCapabilities& operator=(const GLCapabilities&) noexcept = default;
84
85 static const jau::type_info& GLSignature() noexcept { return jau::static_ctti<GLCapabilities>(); }
86 const jau::type_info& signature() const noexcept override { return GLSignature(); }
87
88 CapabilitiesPtr clone() const noexcept override { return std::make_unique<GLCapabilities>(*this); }
89
90 std::size_t hash_code() const noexcept override {
91 // 31 * x == (x << 5) - x
92 size_t hash = Capabilities::hash_code();
93 hash = ((hash << 5) - hash) + (m_hardwareAccelerated ? 1 : 0);
94 hash = ((hash << 5) - hash) + (m_stereo ? 1 : 0);
95 hash = ((hash << 5) - hash) + (m_isFBO ? 1 : 0);
96 // hash = ((hash << 5) - hash) + (isPBuffer ? 1 : 0);
97 hash = ((hash << 5) - hash) + multiSamplesCount();
98 // hash = ((hash << 5) - hash) + sampleExtension.hashCode(); // FIXME
99 hash = ((hash << 5) - hash) + m_depthBits;
100 hash = ((hash << 5) - hash) + m_stencilBits;
101 hash = ((hash << 5) - hash) + m_accumRedBits;
102 hash = ((hash << 5) - hash) + m_accumGreenBits;
103 hash = ((hash << 5) - hash) + m_accumBlueBits;
104 hash = ((hash << 5) - hash) + m_accumAlphaBits;
105 return hash;
106 }
107
108 bool operator==(const Capabilities& rhs0) const noexcept override {
109 if( signature() != rhs0.signature() ) {
110 return false;
111 }
112 const GLCapabilities& rhs = static_cast<const GLCapabilities&>(rhs0);
113 if( this == &rhs ) {
114 return true;
115 }
116 bool res = Capabilities::operator==(rhs) &&
117 // rhs.isPBuffer() == isPBuffer &&
118 rhs.isFBO() == m_isFBO &&
119 rhs.doubleBuffered() == m_doubleBuffered &&
120 rhs.stereo() == m_stereo &&
121 rhs.hardwareAccelerated() == m_hardwareAccelerated &&
122 rhs.depthBits() == m_depthBits &&
123 rhs.stencilBits() == m_stencilBits &&
124 rhs.accumRedBits() == m_accumRedBits &&
125 rhs.accumGreenBits() == m_accumGreenBits &&
126 rhs.accumBlueBits() == m_accumBlueBits &&
127 rhs.accumAlphaBits() == m_accumAlphaBits &&
129 // && rhs.getSampleExtension().equals(sampleExtension)
130 return res;
131 }
132
133 /** comparing hw/sw, stereo, multisample, stencil, RGBA and depth only */
134 int compare(const Capabilities& rhs0) const noexcept override {
135 if( signature() != rhs0.signature() ) {
136 return 1;
137 }
138 const GLCapabilities& rhs = static_cast<const GLCapabilities&>(rhs0);
139 if( this == &rhs ) {
140 return 0;
141 }
142 if( m_hardwareAccelerated && !rhs.hardwareAccelerated() ) {
143 return 1;
144 } else if( !m_hardwareAccelerated && rhs.hardwareAccelerated() ) {
145 return -1;
146 }
147
148 if( m_stereo && !rhs.stereo() ) {
149 return 1;
150 } else if( !m_stereo && rhs.stereo() ) {
151 return -1;
152 }
153
154 if( m_doubleBuffered && !rhs.doubleBuffered() ) {
155 return 1;
156 } else if( !m_doubleBuffered && rhs.doubleBuffered() ) {
157 return -1;
158 }
159
160 const int ms = multiSamplesCount();
161 const int xms = rhs.multiSamplesCount();
162
163 if( ms > xms ) {
164 return 1;
165 } else if( ms < xms ) {
166 return -1;
167 }
168 // ignore the sample extension
169
170 if( m_stencilBits > rhs.stencilBits() ) {
171 return 1;
172 } else if( m_stencilBits < rhs.stencilBits() ) {
173 return -1;
174 }
175
176 const int sc = Capabilities::compare(rhs0); // RGBA
177 if( 0 != sc ) {
178 return sc;
179 }
180
181 if( m_depthBits > rhs.depthBits() ) {
182 return 1;
183 } else if( m_depthBits < rhs.depthBits() ) {
184 return -1;
185 }
186
187 return 0; // they are equal: hw/sw, stereo, multisample, stencil, RGBA and depth
188 }
189
190 /**
191 * Returns whether FBO offscreen mode is requested, available or chosen.
192 * <p>
193 * Default is false.
194 * </p>
195 * <p>
196 * For chosen capabilities, only the selected offscreen surface is set to <code>true</code>.
197 * </p>
198 */
199 constexpr bool isFBO() const noexcept { return m_isFBO; }
200
201 /**
202 * Requesting offscreen FBO mode.
203 * <p>
204 * If enabled this method also invokes {@link #setOnscreen(boolean) setOnscreen(false)}.
205 * </p>
206 * <p>
207 * Defaults to false.
208 * </p>
209 * <p>
210 * Requesting offscreen FBO mode disables the offscreen auto selection.
211 * </p>
212 */
213 constexpr GLCapabilities& setFBO(bool enable) noexcept {
214 if( enable ) {
215 setOnscreen(false);
216 }
217 m_isFBO = enable;
218 return *this;
219 }
220
221 /**
222 * Returns whether double-buffering is requested, available or chosen.
223 * <p>
224 * Default is true.
225 * </p>
226 */
227 constexpr bool doubleBuffered() const noexcept { return m_doubleBuffered; }
228
229 /** Enables or disables double buffering. */
230 constexpr GLCapabilities& setDoubleBuffered(bool enable) noexcept { m_doubleBuffered = enable; return *this; }
231
232 /**
233 * Returns whether stereo is requested, available or chosen.
234 * <p>
235 * Default is false.
236 * </p>
237 */
238 constexpr bool stereo() const noexcept { return m_stereo; }
239
240 /** Enables or disables stereo viewing. */
241 constexpr GLCapabilities& setStereo(bool enable) noexcept { m_stereo = enable; return *this; }
242
243 /**
244 * Returns whether hardware acceleration is requested, available or chosen.
245 * <p>
246 * Default is true.
247 * </p>
248 */
249 constexpr bool hardwareAccelerated() const noexcept { return m_hardwareAccelerated; }
250
251 /** Enables or disables hardware acceleration. */
252 constexpr GLCapabilities& setHardwareAccelerated(bool enable) noexcept { m_hardwareAccelerated = enable; return *this; }
253
254 /**
255 * Returns the number of depth buffer bits.
256 */
257 constexpr int depthBits() const noexcept { return m_depthBits; }
258
259 /** Sets the number of bits requested for the depth buffer. */
260 constexpr int& depthBits() noexcept { return m_depthBits; }
261
262 /**
263 * Returns the number of stencil buffer bits.
264 * <p>
265 * Default is 0.
266 * </p>
267 */
268 constexpr int stencilBits() const noexcept { return m_stencilBits; }
269
270 /** Sets the number of bits requested for the stencil buffer. */
271 constexpr int& stencilBits() noexcept { return m_stencilBits; }
272
273 /**
274 * Returns the number of bits for the accumulation
275 * buffer's red component. On some systems only the accumulation
276 * buffer depth, which is the sum of the red, green, and blue bits,
277 * is considered.
278 */
279 constexpr int accumRedBits() const noexcept { return m_accumRedBits; }
280
281 /** Sets the number of bits requested for the accumulation buffer's
282 red component. On some systems only the accumulation buffer
283 depth, which is the sum of the red, green, and blue bits, is
284 considered. */
285 constexpr int& accumRedBits() noexcept { return m_accumRedBits; }
286
287 /**
288 * Returns the number of bits for the accumulation
289 * buffer's green component. On some systems only the accumulation
290 * buffer depth, which is the sum of the red, green, and blue bits,
291 * is considered.
292 */
293 constexpr int accumGreenBits() const noexcept { return m_accumGreenBits; }
294
295 /** Sets the number of bits requested for the accumulation buffer's
296 green component. On some systems only the accumulation buffer
297 depth, which is the sum of the red, green, and blue bits, is
298 considered. */
299 constexpr int& accumGreenBits() noexcept { return m_accumGreenBits; }
300
301 /**
302 * Returns the number of bits for the accumulation
303 * buffer's blue component. On some systems only the accumulation
304 * buffer depth, which is the sum of the red, green, and blue bits,
305 * is considered.
306 */
307 constexpr int accumBlueBits() const noexcept { return m_accumBlueBits; }
308
309 /** Sets the number of bits requested for the accumulation buffer's
310 blue component. On some systems only the accumulation buffer
311 depth, which is the sum of the red, green, and blue bits, is
312 considered. */
313 constexpr int& accumBlueBits() noexcept { return m_accumBlueBits; }
314
315 /**
316 * Returns the number of bits for the accumulation
317 * buffer's alpha component. On some systems only the accumulation
318 * buffer depth, which is the sum of the red, green, and blue bits,
319 * is considered.
320 */
321 constexpr int accumAlphaBits() const noexcept { return m_accumAlphaBits; }
322
323 /** Sets number of bits requested for accumulation buffer's alpha
324 component. On some systems only the accumulation buffer depth,
325 which is the sum of the red, green, and blue bits, is
326 considered. */
327 constexpr int& accumAlphaBits() noexcept { return m_accumAlphaBits; }
328
329 /**
330 * Sets the desired extension for full-scene antialiasing
331 * (FSAA), default is {@link #DEFAULT_SAMPLE_EXTENSION}.
332 */
333 // constexpr void setSampleExtension(final String se) { sampleExtension = se; }
334 /**
335 * Returns the extension for full-scene antialiasing
336 * (FSAA).
337 * <p>
338 * Default is {@link #DEFAULT_SAMPLE_EXTENSION}.
339 * </p>
340 */
341 // const std::string& getSampleExtension() { return sampleExtension; }
342
343 /**
344 * Returns whether sample buffers for full-scene antialiasing (FSAA)
345 * is/shall-be used for this drawable.
346 *
347 * Default is false.
348 */
349 constexpr bool hasMultiSamples() const noexcept { return m_multiSamplesCount > 1; }
350
351 /**
352 * Defaults to 0, i.e. no full-scene antialiasing (FSAA).
353 *
354 * Indicates whether sample buffers for full-scene antialiasing
355 * (FSAA) should is/shall-be used for this drawable.
356 *
357 * If FSAA is enabled but alphaBits()==0, alphaBits() will be set to 1.
358 */
359 constexpr GLCapabilities& setMultiSamplesCount(int v) noexcept {
360 m_multiSamplesCount = v;
361 if( v > 1 && alphaBits() == 0 ) {
362 alphaBits() = 1;
363 }
364 return *this;
365 }
366
367 /**
368 * Returns the number of sample buffers to be allocated if sample
369 * buffers are enabled, otherwise returns 0.
370 * <p>
371 * Default is 0 due to disable sample buffers per default.
372 * </p>
373 */
374 constexpr int multiSamplesCount() const noexcept { return hasMultiSamples() ? m_multiSamplesCount : 0; }
375
376 /** Returns a textual representation of this GLCapabilities
377 object. */
378 std::string toString() const override {
379 std::string msg("GLCaps[");
380 toString(msg);
381 msg.append("]");
382 return msg;
383 }
384
385 protected:
386 std::string toString(std::string& sink) const {
387 Capabilities::toString(sink, false);
388
389 sink.append(", accum-rgba ").append(std::to_string(m_accumRedBits)).append(ESEP).append(std::to_string(m_accumGreenBits)).append(ESEP)
390 .append(std::to_string(m_accumBlueBits)).append(ESEP).append(std::to_string(m_accumAlphaBits));
391 sink.append(", dp/st/ms ").append(std::to_string(m_depthBits)).append(ESEP)
392 .append(std::to_string(m_stencilBits)).append(ESEP).append(std::to_string(multiSamplesCount()));
393 // if(samples>0) { sink.append(", sample-ext ").append(m_sampleExtension); }
394 if( m_doubleBuffered ) {
395 sink.append(", dbl");
396 } else {
397 sink.append(", one");
398 }
399 if( m_stereo ) {
400 sink.append(", stereo");
401 } else {
402 sink.append(", mono ");
403 }
404 if( m_hardwareAccelerated ) {
405 sink.append(", hw, ");
406 } else {
407 sink.append(", sw");
408 }
409 // sink.append(", ").append(glProfile);
410 if( isOnscreen() ) {
411 sink.append(", on-scr[");
412 } else {
413 sink.append(", offscr[");
414 }
415 bool ns = false;
416 if( isFBO() ) {
417 sink.append("fbo");
418 ns = true;
419 }
420 if( isBitmap() ) {
421 if( ns ) { sink.append(CSEP); }
422 sink.append("bitmap");
423 ns = true;
424 }
425 if( !ns ) { // !FBO !Bitmap
426 if( isOnscreen() ) {
427 sink.append("."); // no additional off-screen modes besides on-screen
428 } else {
429 sink.append("auto-cfg"); // auto-config off-screen mode
430 }
431 }
432 sink.append("]");
433
434 return sink;
435 }
436 };
437 typedef std::unique_ptr<GLCapabilities> GLCapabilitiesPtr;
438
439 /**@}*/
440
441} // namespace gamp::render::gl
442
443#endif /* GAMP_RENDER_GL_GLCAPABILITIES_HPP_ */
constexpr int stencilBits() const noexcept
Returns the number of stencil buffer bits.
constexpr int accumGreenBits() const noexcept
Returns the number of bits for the accumulation buffer's green component.
static const jau::type_info & GLSignature() noexcept
constexpr int & accumBlueBits() noexcept
Sets the number of bits requested for the accumulation buffer's blue component.
constexpr int accumBlueBits() const noexcept
Returns the number of bits for the accumulation buffer's blue component.
constexpr GLCapabilities & setMultiSamplesCount(int v) noexcept
Defaults to 0, i.e.
constexpr GLCapabilities & setFBO(bool enable) noexcept
Requesting offscreen FBO mode.
constexpr int depthBits() const noexcept
Returns the number of depth buffer bits.
constexpr int multiSamplesCount() const noexcept
Returns the number of sample buffers to be allocated if sample buffers are enabled,...
constexpr int accumAlphaBits() const noexcept
Returns the number of bits for the accumulation buffer's alpha component.
constexpr int & stencilBits() noexcept
Sets the number of bits requested for the stencil buffer.
constexpr GLCapabilities() noexcept=default
bool operator==(const Capabilities &rhs0) const noexcept override
constexpr bool stereo() const noexcept
Returns whether stereo is requested, available or chosen.
std::string toString(std::string &sink) const
constexpr GLCapabilities & setDoubleBuffered(bool enable) noexcept
Enables or disables double buffering.
constexpr int & accumGreenBits() noexcept
Sets the number of bits requested for the accumulation buffer's green component.
std::size_t hash_code() const noexcept override
constexpr GLCapabilities & setHardwareAccelerated(bool enable) noexcept
Enables or disables hardware acceleration.
int compare(const Capabilities &rhs0) const noexcept override
comparing hw/sw, stereo, multisample, stencil, RGBA and depth only
constexpr int & accumAlphaBits() noexcept
Sets number of bits requested for accumulation buffer's alpha component.
constexpr bool isFBO() const noexcept
Returns whether FBO offscreen mode is requested, available or chosen.
constexpr int accumRedBits() const noexcept
Returns the number of bits for the accumulation buffer's red component.
constexpr bool doubleBuffered() const noexcept
Returns whether double-buffering is requested, available or chosen.
std::string toString() const override
Returns a textual representation of this GLCapabilities object.
constexpr int & depthBits() noexcept
Sets the number of bits requested for the depth buffer.
constexpr GLCapabilities & setStereo(bool enable) noexcept
Enables or disables stereo viewing.
constexpr int & accumRedBits() noexcept
Sets the number of bits requested for the accumulation buffer's red component.
const jau::type_info & signature() const noexcept override
CapabilitiesPtr clone() const noexcept override
constexpr bool hasMultiSamples() const noexcept
Sets the desired extension for full-scene antialiasing (FSAA), default is DEFAULT_SAMPLE_EXTENSION.
constexpr bool hardwareAccelerated() const noexcept
Returns whether hardware acceleration is requested, available or chosen.
static constexpr std::string_view ESEP
Element separator.
constexpr bool isOnscreen() const noexcept
Returns whether an on- or offscreen surface is requested, available or chosen.
constexpr bool isBitmap() const noexcept
Returns whether bitmap offscreen mode is requested, available or chosen.
virtual int compare(const Capabilities &rhs) const noexcept
Comparing RGBA values only.
static constexpr std::string_view CSEP
Component separator.
virtual std::size_t hash_code() const noexcept
constexpr void setOnscreen(bool v) noexcept
Sets whether the surface shall be on- or offscreen.
virtual std::string toString() const
Returns a textual representation of this Capabilities object.
constexpr int alphaBits() const noexcept
Returns the number of bits for the color buffer's alpha component.
virtual bool operator==(const Capabilities &rhs) const noexcept
constexpr Capabilities() noexcept=default
Creates a Capabilities object.
Generic type information using either Runtime type information (RTTI) or Compile time type informatio...
const jau::type_info & static_ctti() noexcept
Returns a static global reference of make_ctti<T>(true) w/ identity instance.
std::unique_ptr< GLCapabilities > GLCapabilitiesPtr
std::unique_ptr< Capabilities > CapabilitiesPtr
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
Definition backtrace.hpp:32