From 74cbfc9c04b26c40b3a83e823021d42dfde3f1f9 Mon Sep 17 00:00:00 2001 From: bkaradzic Date: Sun, 17 Feb 2013 18:04:40 -0800 Subject: [PATCH] GL MSAA WIP. --- src/glcontext_wgl.cpp | 114 ++++++++++++++++++++++++++++++++++++++++++ src/renderer_gl.cpp | 10 ++-- 2 files changed, 120 insertions(+), 4 deletions(-) diff --git a/src/glcontext_wgl.cpp b/src/glcontext_wgl.cpp index b1c01cbe..3d3cfaed 100644 --- a/src/glcontext_wgl.cpp +++ b/src/glcontext_wgl.cpp @@ -16,6 +16,9 @@ namespace bgfx PFNWGLMAKECURRENTPROC wglMakeCurrent; PFNWGLCREATECONTEXTPROC wglCreateContext; PFNWGLDELETECONTEXTPROC wglDeleteContext; + PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB; + PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB; + PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB; # define GL_IMPORT(_optional, _proto, _func) _proto _func # include "glimports.h" @@ -80,6 +83,117 @@ namespace bgfx result = wglMakeCurrent(m_hdc, m_context); BGFX_FATAL(0 != result, Fatal::UnableToInitialize, "wglMakeCurrent failed!"); + wglGetExtensionsStringARB = (PFNWGLGETEXTENSIONSSTRINGARBPROC)wglGetProcAddress("wglGetExtensionsStringARB"); + wglChoosePixelFormatARB = (PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB"); + wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB"); + + if (NULL != wglGetExtensionsStringARB) + { + BX_TRACE("WGL extensions:"); + const char* extensions = (const char*)wglGetExtensionsStringARB(m_hdc); + if (NULL != extensions) + { + char name[1024]; + const char* pos = extensions; + const char* end = extensions + strlen(extensions); + while (pos < end) + { + uint32_t len; + const char* space = strchr(pos, ' '); + if (NULL != space) + { + len = uint32_min(sizeof(name), (uint32_t)(space - pos) ); + } + else + { + len = uint32_min(sizeof(name), (uint32_t)strlen(pos) ); + } + + strncpy(name, pos, len); + name[len] = '\0'; + + BX_TRACE("\t%s", name); + + pos += len+1; + } + } + } + +#if 0 + // An application can only set the pixel format of a window one time. + // Once a window's pixel format is set, it cannot be changed. + // MSDN: http://msdn.microsoft.com/en-us/library/windows/desktop/dd369049%28v=vs.85%29.aspx + if (NULL != wglChoosePixelFormatARB + && NULL != wglCreateContextAttribsARB) + { + int32_t attrs[] = + { + WGL_SAMPLE_BUFFERS_ARB, 0, + WGL_SAMPLES_ARB, 0, + WGL_SUPPORT_OPENGL_ARB, true, + WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB, + WGL_DRAW_TO_WINDOW_ARB, true, + WGL_DOUBLE_BUFFER_ARB, true, + WGL_RED_BITS_ARB, 8, + WGL_BLUE_BITS_ARB, 8, + WGL_GREEN_BITS_ARB, 8, + WGL_ALPHA_BITS_ARB, 8, + WGL_DEPTH_BITS_ARB, 24, + WGL_STENCIL_BITS_ARB, 8, + 0 + }; + + uint32_t numFormats = 0; + do + { + result = wglChoosePixelFormatARB(m_hdc, attrs, NULL, 1, &pixelFormat, &numFormats); + if (0 == result + || 0 == numFormats) + { + attrs[3] >>= 1; + attrs[1] = attrs[3] == 0 ? 0 : 1; + } + + } while (0 == numFormats); + + DescribePixelFormat(m_hdc, pixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd); + + BX_TRACE("Pixel format:\n" + "\tiPixelType %d\n" + "\tcColorBits %d\n" + "\tcAlphaBits %d\n" + "\tcDepthBits %d\n" + "\tcStencilBits %d\n" + , pfd.iPixelType + , pfd.cColorBits + , pfd.cAlphaBits + , pfd.cDepthBits + , pfd.cStencilBits + ); + + result = SetPixelFormat(m_hdc, pixelFormat, &pfd); + BGFX_FATAL(0 != result, Fatal::UnableToInitialize, "SetPixelFormat failed (last err: 0x%08x)!", GetLastError() ); + + wglMakeCurrent(m_hdc, NULL); + wglDeleteContext(m_context); + + const int32_t contextAttrs[] = + { + WGL_CONTEXT_MAJOR_VERSION_ARB, 2, + WGL_CONTEXT_MINOR_VERSION_ARB, 1, +#if BGFX_CONFIG_DEBUG + WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_DEBUG_BIT_ARB, +#endif // BGFX_CONFIG_DEBUG + 0 + }; + + m_context = wglCreateContextAttribsARB(m_hdc, 0, contextAttrs); + + result = wglMakeCurrent(m_hdc, m_context); + BGFX_FATAL(0 != result, Fatal::UnableToInitialize, "wglMakeCurrent failed!"); + } +#endif // 0 + import(); } diff --git a/src/renderer_gl.cpp b/src/renderer_gl.cpp index 8585ab65..218ed9c0 100644 --- a/src/renderer_gl.cpp +++ b/src/renderer_gl.cpp @@ -178,23 +178,25 @@ namespace bgfx m_textVideoMem.clear(); m_resolution = _resolution; - setRenderContextSize(_resolution.m_width, _resolution.m_height); + + uint32_t msaa = 1<<( (m_resolution.m_flags&BGFX_RESET_MSAA_MASK)>>BGFX_RESET_MSAA_SHIFT); + setRenderContextSize(_resolution.m_width, _resolution.m_height, msaa); updateCapture(); } } - void setRenderContextSize(uint32_t _width, uint32_t _height) + void setRenderContextSize(uint32_t _width, uint32_t _height, uint32_t _msaa) { if (_width != 0 || _height != 0) { if (!m_glctx.isValid() ) { - m_glctx.create(_width, _height); + m_glctx.create(_width, _height/*, _msaa*/); } else { - m_glctx.resize(_width, _height); + m_glctx.resize(_width, _height/*, _msaa*/); } }