Improve wxGLCanvas to be more type safe and better support modern OpenGL

Add wxGLAttribsBase, wxGLAttributes and wxGLContextAttrs replacing the old
untyped "int attributes[]".

Don't use global object for creating OpenGL > 3.0 contexts.

Closes #16909.
This commit is contained in:
Manuel Martin
2016-02-23 00:30:49 +01:00
committed by Vadim Zeitlin
parent fb5ff50eda
commit bdc95f5766
17 changed files with 2964 additions and 1089 deletions

View File

@@ -49,13 +49,120 @@ enum
WX_GL_MIN_ACCUM_ALPHA, // use alpha buffer with most bits (> MIN_ACCUM_ALPHA bits)
WX_GL_SAMPLE_BUFFERS, // 1 for multisampling support (antialiasing)
WX_GL_SAMPLES, // 4 for 2x2 antialiasing supersampling on most graphics cards
WX_GL_FRAMEBUFFER_SRGB,// capability for sRGB framebuffer
// Context attributes
WX_GL_CORE_PROFILE, // use an OpenGL core profile
WX_GL_MAJOR_VERSION, // major OpenGL version of the core profile
WX_GL_MINOR_VERSION // minor OpenGL version of the core profile
WX_GL_MINOR_VERSION, // minor OpenGL version of the core profile
wx_GL_COMPAT_PROFILE, // use compatible profile (use all versions features)
WX_GL_FORWARD_COMPAT, // forward compatible context. OpenGL >= 3.0
WX_GL_ES2, // ES or ES2 context.
WX_GL_DEBUG, // create a debug context
WX_GL_ROBUST_ACCESS, // robustness.
WX_GL_NO_RESET_NOTIFY, // never deliver notification of reset events
WX_GL_LOSE_ON_RESET, // if graphics reset, all context state is lost
WX_GL_RESET_ISOLATION, // protect other apps or share contexts from reset side-effects
WX_GL_RELEASE_FLUSH, // on context release, flush pending commands
WX_GL_RELEASE_NONE // on context release, pending commands are not flushed
};
#define wxGLCanvasName wxT("GLCanvas")
// ----------------------------------------------------------------------------
// wxGLAttribsBase: OpenGL rendering attributes
// ----------------------------------------------------------------------------
class WXDLLIMPEXP_GL wxGLAttribsBase
{
public:
wxGLAttribsBase() { Reset(); }
// Setters
void AddAttribute(int attribute) { m_GLValues.push_back(attribute); }
// Search for searchVal and combine the next value with combineVal
void AddAttribBits(int searchVal, int combineVal);
// ARB functions necessity
void SetNeedsARB(bool needsARB = true) { m_needsARB = needsARB; }
// Delete contents
void Reset()
{
m_GLValues.clear();
m_needsARB = false;
}
// Accessors
const int* GetGLAttrs() const
{
return (m_GLValues.empty() || !m_GLValues[0]) ? NULL : &*m_GLValues.begin();
}
int GetSize() const { return (int)(m_GLValues.size()); }
// ARB function (e.g. wglCreateContextAttribsARB) is needed
bool NeedsARB() const { return m_needsARB; }
private:
wxVector<int> m_GLValues;
bool m_needsARB;
};
// ----------------------------------------------------------------------------
// wxGLContextAttrs: OpenGL rendering context attributes
// ----------------------------------------------------------------------------
class WXDLLIMPEXP_GL wxGLContextAttrs : public wxGLAttribsBase
{
public:
// Setters, allowing chained calls
wxGLContextAttrs& CoreProfile();
wxGLContextAttrs& MajorVersion(int val);
wxGLContextAttrs& MinorVersion(int val);
wxGLContextAttrs& OGLVersion(int vmayor, int vminor)
{ return MajorVersion(vmayor).MinorVersion(vminor); }
wxGLContextAttrs& CompatibilityProfile();
wxGLContextAttrs& ForwardCompatible();
wxGLContextAttrs& ES2();
wxGLContextAttrs& DebugCtx();
wxGLContextAttrs& Robust();
wxGLContextAttrs& NoResetNotify();
wxGLContextAttrs& LoseOnReset();
wxGLContextAttrs& ResetIsolation();
wxGLContextAttrs& ReleaseFlush(int val = 1); //'int' allows future values
wxGLContextAttrs& PlatformDefaults();
void EndList(); // No more values can be chained
// Currently only used for X11 context creation
bool x11Direct; // X11 direct render
bool renderTypeRGBA;
};
// ----------------------------------------------------------------------------
// wxGLAttributes: canvas configuration
// ----------------------------------------------------------------------------
class WXDLLIMPEXP_GL wxGLAttributes : public wxGLAttribsBase
{
public:
// Setters, allowing chained calls
wxGLAttributes& RGBA();
wxGLAttributes& BufferSize(int val);
wxGLAttributes& Level(int val);
wxGLAttributes& DoubleBuffer();
wxGLAttributes& Stereo();
wxGLAttributes& AuxBuffers(int val);
wxGLAttributes& MinRGBA(int mRed, int mGreen, int mBlue, int mAlpha);
wxGLAttributes& Depth(int val);
wxGLAttributes& Stencil(int val);
wxGLAttributes& MinAcumRGBA(int mRed, int mGreen, int mBlue, int mAlpha);
wxGLAttributes& PlatformDefaults();
wxGLAttributes& Defaults();
wxGLAttributes& SampleBuffers(int val);
wxGLAttributes& Samplers(int val);
wxGLAttributes& FrameBuffersRGB();
void EndList(); // No more values can be chained
};
// ----------------------------------------------------------------------------
// wxGLContextBase: OpenGL rendering context
// ----------------------------------------------------------------------------
@@ -63,14 +170,20 @@ enum
class WXDLLIMPEXP_GL wxGLContextBase : public wxObject
{
public:
/*
The derived class should provide a ctor with this signature:
wxGLContext(wxGLCanvas *win, const wxGLContext *other = NULL);
*/
// The derived class should provide a ctor with this signature:
//
// wxGLContext(wxGLCanvas *win,
// const wxGLContext *other = NULL,
// const wxGLContextAttrs *ctxAttrs = NULL);
// set this context as the current one
virtual bool SetCurrent(const wxGLCanvas& win) const = 0;
bool IsOK() { return m_isOk; }
protected:
bool m_isOk;
};
// ----------------------------------------------------------------------------
@@ -90,8 +203,8 @@ public:
The derived class should provide a ctor with this signature:
wxGLCanvas(wxWindow *parent,
const wxGLAttributes& dispAttrs,
wxWindowID id = wxID_ANY,
int* attribList = 0,
const wxPoint& pos = wxDefaultPosition,
const wxSize& size = wxDefaultSize,
long style = 0,
@@ -113,6 +226,7 @@ public:
// ---------
// check if the given attributes are supported without creating a canvas
static bool IsDisplaySupported(const wxGLAttributes& dispAttrs);
static bool IsDisplaySupported(const int *attribList);
#if wxUSE_PALETTE
@@ -134,6 +248,11 @@ public:
// compilation
static bool IsExtensionSupported(const char *extension);
// Get the wxGLContextAttrs object filled with the context-related values
// of the list of attributes passed at ctor when no wxGLAttributes is used
// as a parameter
wxGLContextAttrs& GetGLCTXAttrs() { return m_GLCTXAttrs; }
// deprecated methods using the implicit wxGLContext
#if WXWIN_COMPATIBILITY_2_8
wxDEPRECATED( wxGLContext* GetContext() const );
@@ -158,6 +277,15 @@ protected:
// by glXQueryExtensionsString() or glGetString(GL_EXTENSIONS)
static bool IsExtensionInList(const char *list, const char *extension);
// For the case of "int* attribList" at ctor is != 0
wxGLContextAttrs m_GLCTXAttrs;
// Extract pixel format and context attributes.
// Return false if an unknown attribute is found.
static bool ParseAttribList(const int* attribList,
wxGLAttributes& dispAttrs,
wxGLContextAttrs* ctxAttrs = NULL);
#if wxUSE_PALETTE
// create default palette if we're not using RGBA mode
// (not supported in most ports)