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