24#include <SDL2/SDL_video.h>
36 return 0 == SDL_GL_GetAttribute(attr, &
value);
39 return 0 == SDL_GL_SetAttribute(attr,
value);
42bool Surface::setSwapIntervalImpl(
int v)
noexcept {
43 if( 0 == SDL_GL_SetSwapInterval( v ) ) {
47 if( -1 == v && 0 == SDL_GL_SetSwapInterval( 1 ) ) {
56 if (!surface->isValid()) {
130 if( !surface->isValid() ) {
131 printf(
"SDL: Error creating GL context: Invalid surface: %s\n", surface->toString().c_str());
136 printf(
"Surface::createContext: shareWith %p, profile %s, ctx %s, surface %s\n",
137 (
void*)shareWith, profile.toString().c_str(),
to_string(contextFlags).c_str(), surface->toString().c_str());
139 SDL_Window* sdl_win =
reinterpret_cast<SDL_Window*
>(surface->surfaceHandle());
141 surface->setSwapIntervalImpl( surface->swapInterval() );
145 ctxFlags |= SDL_GL_CONTEXT_DEBUG_FLAG;
148 ctxFlags |= SDL_GL_CONTEXT_ROBUST_ACCESS_FLAG;
150 SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, ctxFlags);
154 SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
157 printf(
"Surface::createContext.1: surface %s\n", surface->toString().c_str());
161 bool use_glp_core =
false;
166 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
168 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
170 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
174 if( !use_glp_core ) {
175 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
176 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
177 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
179 SDL_GLContext sdl_glc = SDL_GL_CreateContext(sdl_win);
180 if (
nullptr == sdl_glc) {
182 printf(
"SDL: Error creating %s context: %s\n", glp_in.
toString().c_str(), SDL_GetError());
185 printf(
"SDL: Error creating GL ES 3 context: %s\n", SDL_GetError());
186 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
187 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
188 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
189 sdl_glc = SDL_GL_CreateContext(sdl_win);
190 if (
nullptr == sdl_glc) {
191 printf(
"SDL: Error creating GL ES 2 context: %s\n", SDL_GetError());
195 if (0 != SDL_GL_MakeCurrent(sdl_win, sdl_glc)) {
196 printf(
"SDL: Error making GL context current: %s\n", SDL_GetError());
197 SDL_GL_DeleteContext(sdl_glc);
200 const char* gl_version_cstr =
reinterpret_cast<const char*
>( glGetString(GL_VERSION) );
201 if (
nullptr == gl_version_cstr) {
202 printf(
"SDL: Error retrieving GL version: %s\n", SDL_GetError());
203 SDL_GL_DeleteContext(sdl_glc);
207 int major, minor, nContextFlags, nProfileMask;
212 ok = ok &&
GetGLAttribute(SDL_GL_CONTEXT_PROFILE_MASK, nProfileMask);
214 printf(
"SDL: Error retrieving GL context version information: %s\n", SDL_GetError());
215 SDL_GL_DeleteContext(sdl_glc);
219 if( 0 != ( nProfileMask & SDL_GL_CONTEXT_PROFILE_CORE ) ) {
221 }
else if( 0 != ( nProfileMask & SDL_GL_CONTEXT_PROFILE_COMPATIBILITY ) ) {
223 }
else if( 0 != ( nProfileMask & SDL_GL_CONTEXT_PROFILE_ES ) ) {
227 if( 0 != ( nContextFlags & SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG ) ) {
230 if( 0 != ( nContextFlags & SDL_GL_CONTEXT_DEBUG_FLAG ) ) {
233 if( 0 != ( nContextFlags & SDL_GL_CONTEXT_ROBUST_ACCESS_FLAG ) ) {
240 printf(
"Surface::createContext.2: GLProfile (query) %s\n", glp_out0.
toString().c_str());
241 printf(
"Surface::createContext.2: GLProfile (version) %s\n", glp_out.
toString().c_str());
249 SDL_GL_SwapWindow(
reinterpret_cast<SDL_Window*
>(
windowHandle()));
262 SDL_GLContext sdl_glc =
reinterpret_cast<SDL_GLContext
>(
context());
267 SDL_GL_DeleteContext(sdl_glc);
273 if( !surface || !surface->isValid() || !context) {
274 printf(
"SDL: Error GLContext::makeCurrent: Invalid surface/context: surface %s, context %p\n",
275 surface ? surface->toString().c_str() :
"nil", (
void*)context);
278 SDL_Window* sdl_win =
reinterpret_cast<SDL_Window*
>(surface->surfaceHandle());
279 SDL_GLContext sdl_glc =
reinterpret_cast<SDL_GLContext
>(context);
280 if (0 != SDL_GL_MakeCurrent(sdl_win, sdl_glc)) {
287 if( !surface || !surface->isValid() ) {
288 printf(
"SDL: Error GLContext::release: Invalid surface: surface %s\n",
289 surface ? surface->toString().c_str() :
"nil");
292 SDL_Window* sdl_win =
reinterpret_cast<SDL_Window*
>(surface->surfaceHandle());
293 SDL_GL_MakeCurrent(sdl_win,
nullptr);
constexpr gamp::handle_t context() const noexcept
Specifies the render profile.
constexpr const jau::util::VersionNumber & version() const noexcept
Specifies a set of OpenGL capabilities.
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() 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 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 bool stereo() const noexcept
Returns whether stereo is requested, available or chosen.
constexpr GLCapabilities & setDoubleBuffered(bool enable) noexcept
Enables or disables double buffering.
constexpr GLCapabilities & setHardwareAccelerated(bool enable) noexcept
Enables or disables hardware acceleration.
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 GLCapabilities & setStereo(bool enable) noexcept
Enables or disables stereo viewing.
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.
OpenGL Rendering Context.
static RenderContextPtr create(gamp::handle_t context, GLProfile &&profile, RenderContextFlags contextFlags, const char *gl_version_cstr) noexcept
Create a new instance of a non-current context.
bool isCurrent() const noexcept
void dispose() noexcept override
void disposedNotify() override
void releaseContext() noexcept override
Release this context (used for OpenGL, but a NOP on Vulkan)
Specifies the OpenGL profile.
std::string toString() const override
constexpr bool isGLES() const noexcept
Indicates whether this profile is capable of GLES.
static const GLProfile & downcast(const RenderProfile &rp)
Downcast dereferenced given const RenderProfile& to const GLProfile&, throws exception if signature d...
static const jau::type_info & GLSignature() noexcept
A class for storing and comparing OpenGL version numbers.
static GLVersionNumber create(const std::string &versionString) noexcept
Specifies a set of capabilities that a window's rendering context must support, such as color depth p...
constexpr int redBits() const noexcept
Returns the number of bits for the color buffer's red component.
constexpr int greenBits() const noexcept
Returns the number of bits for the color buffer's green component.
constexpr int blueBits() const noexcept
Returns the number of bits for the color buffer's blue component.
constexpr int alphaBits() const noexcept
Returns the number of bits for the color buffer's alpha component.
static void setCaps(handle_t surface_handle, const Capabilities &requested) noexcept
bool createContext(const gamp::render::RenderProfile &profile, const gamp::render::RenderContextFlags &contextFlags)
constexpr handle_t windowHandle() const noexcept
Returns the handle to the surface for this NativeSurface.
bool isValid() const noexcept override
bool surfaceSwap() noexcept override
Provide a mechanism to utilize custom (pre-) swap surface code.
Simple version number class containing a version number either being defined explicit or derived from...
constexpr int major() const noexcept
static bool SetGLAttribute(SDL_GLattr attr, int value) noexcept
static bool GetGLAttribute(SDL_GLattr attr, int &value) noexcept
constexpr bool value(const Bool rhs) noexcept
constexpr bool is_set(const E mask, const E bits) noexcept
GLProfileMask
OpenGL profile-mask bits.
std::unique_ptr< GLCapabilities > GLCapabilitiesPtr
@ compat
Desktop compatibility profile.
@ core
Desktop core profile.
std::unique_ptr< RenderContext > RenderContextPtr
RenderContextFlags
OpenGL context flags.
@ verbose
Verbose operations (debugging).
@ compatible
Compatible context.
std::shared_ptr< Surface > SurfaceSRef
std::unique_ptr< Capabilities > CapabilitiesPtr
uintptr_t handle_t
A native handle type, big enough to store a pointer.
std::string_view to_string(const math_error_t v) noexcept
Returns std::string_view representation of math_error_t.
Gamp: Graphics, Audio, Multimedia and Processing Framework (Native C++, WebAssembly,...
Author: Sven Gothel sgothel@jausoft.com Copyright Gothel Software e.K.
int printf(const char *format,...)
Operating Systems predefined macros.