diff --git a/src/gl/data/gl_vertexbuffer.cpp b/src/gl/data/gl_vertexbuffer.cpp index 854750ecbf..0e90655575 100644 --- a/src/gl/data/gl_vertexbuffer.cpp +++ b/src/gl/data/gl_vertexbuffer.cpp @@ -90,26 +90,60 @@ void FVertexBuffer::BindVBO() FFlatVertexBuffer::FFlatVertexBuffer() : FVertexBuffer() { - unsigned int bytesize = BUFFER_SIZE * sizeof(FFlatVertex); - glBindBuffer(GL_ARRAY_BUFFER, vbo_id); - glBufferStorage(GL_ARRAY_BUFFER, bytesize, NULL, GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT); - map = (FFlatVertex*)glMapBufferRange(GL_ARRAY_BUFFER, 0, bytesize, GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT); - mNumReserved = mIndex = mCurIndex = 0; + if (gl.flags & RFL_BUFFER_STORAGE) + { + unsigned int bytesize = BUFFER_SIZE * sizeof(FFlatVertex); + glBindBuffer(GL_ARRAY_BUFFER, vbo_id); + glBufferStorage(GL_ARRAY_BUFFER, bytesize, NULL, GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT); + map = (FFlatVertex*)glMapBufferRange(GL_ARRAY_BUFFER, 0, bytesize, GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT); - glBindVertexArray(vao_id); - glBindBuffer(GL_ARRAY_BUFFER, vbo_id); - glVertexAttribPointer(VATTR_VERTEX, 3,GL_FLOAT, false, sizeof(FFlatVertex), &VTO->x); - glVertexAttribPointer(VATTR_TEXCOORD, 2,GL_FLOAT, false, sizeof(FFlatVertex), &VTO->u); - glEnableVertexAttribArray(VATTR_VERTEX); - glEnableVertexAttribArray(VATTR_TEXCOORD); - glBindVertexArray(0); + glBindVertexArray(vao_id); + glBindBuffer(GL_ARRAY_BUFFER, vbo_id); + glVertexAttribPointer(VATTR_VERTEX, 3,GL_FLOAT, false, sizeof(FFlatVertex), &VTO->x); + glVertexAttribPointer(VATTR_TEXCOORD, 2,GL_FLOAT, false, sizeof(FFlatVertex), &VTO->u); + glEnableVertexAttribArray(VATTR_VERTEX); + glEnableVertexAttribArray(VATTR_TEXCOORD); + glBindVertexArray(0); + } + else + { + vbo_shadowdata.Reserve(BUFFER_SIZE); + map = &vbo_shadowdata[0]; + } + mNumReserved = mIndex = mCurIndex = 0; } FFlatVertexBuffer::~FFlatVertexBuffer() { - glBindBuffer(GL_ARRAY_BUFFER, vbo_id); - glUnmapBuffer(GL_ARRAY_BUFFER); - glBindBuffer(GL_ARRAY_BUFFER, 0); + if (gl.flags & RFL_BUFFER_STORAGE) + { + glBindBuffer(GL_ARRAY_BUFFER, vbo_id); + glUnmapBuffer(GL_ARRAY_BUFFER); + glBindBuffer(GL_ARRAY_BUFFER, 0); + } +} + +//========================================================================== +// +// immediate mode fallback for drivers without GL_ARB_buffer_storage +// +// No single core method is performant enough to handle this adequately +// so we have to resort to immediate mode instead... +// +//========================================================================== + +void FFlatVertexBuffer::ImmRenderBuffer(unsigned int primtype, unsigned int offset, unsigned int count) +{ + // this will only get called if we can't acquire a persistently mapped buffer. +#ifndef CORE_PROFILE + glBegin(primtype); + for (unsigned int i = 0; i < count; i++) + { + glVertexAttrib2fv(VATTR_TEXCOORD, &map[offset + i].u); + glVertexAttrib3fv(VATTR_VERTEX, &map[offset + i].x); + } + glEnd(); +#endif } //========================================================================== @@ -289,10 +323,24 @@ void FFlatVertexBuffer::UpdatePlaneVertices(sector_t *sec, int plane) void FFlatVertexBuffer::CreateVBO() { - vbo_shadowdata.Resize(mNumReserved); - CreateFlatVBO(); - mCurIndex = mIndex = vbo_shadowdata.Size(); - memcpy(map, &vbo_shadowdata[0], vbo_shadowdata.Size() * sizeof(FFlatVertex)); + if (gl.flags & RFL_BUFFER_STORAGE) + { + vbo_shadowdata.Resize(mNumReserved); + CreateFlatVBO(); + mCurIndex = mIndex = vbo_shadowdata.Size(); + memcpy(map, &vbo_shadowdata[0], vbo_shadowdata.Size() * sizeof(FFlatVertex)); + } + else if (sectors) + { + // set all VBO info to invalid values so that we can save some checks in the rendering code + for(int i=0;iGetHeightSec(); - if (hs != NULL) CheckPlanes(hs); - for(unsigned i = 0; i < sector->e->XFloor.ffloors.Size(); i++) - CheckPlanes(sector->e->XFloor.ffloors[i]->model); + if (gl.flags & RFL_BUFFER_STORAGE) + { + CheckPlanes(sector); + sector_t *hs = sector->GetHeightSec(); + if (hs != NULL) CheckPlanes(hs); + for (unsigned i = 0; i < sector->e->XFloor.ffloors.Size(); i++) + CheckPlanes(sector->e->XFloor.ffloors[i]->model); + } } \ No newline at end of file diff --git a/src/gl/data/gl_vertexbuffer.h b/src/gl/data/gl_vertexbuffer.h index de61fb7cb4..b6d61f2da1 100644 --- a/src/gl/data/gl_vertexbuffer.h +++ b/src/gl/data/gl_vertexbuffer.h @@ -83,7 +83,14 @@ public: void RenderArray(unsigned int primtype, unsigned int offset, unsigned int count) { drawcalls.Clock(); - glDrawArrays(primtype, offset, count); + if (gl.flags & RFL_BUFFER_STORAGE) + { + glDrawArrays(primtype, offset, count); + } + else + { + ImmRenderBuffer(primtype, offset, count); + } drawcalls.Unclock(); } diff --git a/src/gl/system/gl_interface.cpp b/src/gl/system/gl_interface.cpp index 4da1d9a42a..7a745110b2 100644 --- a/src/gl/system/gl_interface.cpp +++ b/src/gl/system/gl_interface.cpp @@ -117,9 +117,9 @@ void gl_LoadExtensions() else Printf("Emulating OpenGL v %s\n", version); // Don't even start if it's lower than 3.0 - if (strcmp(version, "3.3") < 0 || !CheckExtension("GL_ARB_buffer_storage")) + if (strcmp(version, "3.3") < 0) { - I_FatalError("Unsupported OpenGL version.\nAt least OpenGL 3.3 and the »GL_ARB_buffer_storage« extension is required to run " GAMENAME ".\n"); + I_FatalError("Unsupported OpenGL version.\nAt least OpenGL 3.3 is required to run " GAMENAME ".\n"); } // add 0.01 to account for roundoff errors making the number a tad smaller than the actual version @@ -130,7 +130,12 @@ void gl_LoadExtensions() if (CheckExtension("GL_ARB_texture_compression")) gl.flags|=RFL_TEXTURE_COMPRESSION; if (CheckExtension("GL_EXT_texture_compression_s3tc")) gl.flags|=RFL_TEXTURE_COMPRESSION_S3TC; - if (CheckExtension("GL_ARB_shader_storage_buffer_object")) gl.flags |= RFL_SHADER_STORAGE_BUFFER; + if (!Args->CheckParm("-gl3")) + { + // don't use GL 4.x features when running in GL 3 emulation mode. + if (CheckExtension("GL_ARB_shader_storage_buffer_object")) gl.flags |= RFL_SHADER_STORAGE_BUFFER; + if (CheckExtension("GL_ARB_buffer_storage")) gl.flags |= RFL_BUFFER_STORAGE; + } glGetIntegerv(GL_MAX_TEXTURE_SIZE,&gl.max_texturesize); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); diff --git a/src/gl/system/gl_interface.h b/src/gl/system/gl_interface.h index 5d59e5560f..af4c4f7c2d 100644 --- a/src/gl/system/gl_interface.h +++ b/src/gl/system/gl_interface.h @@ -9,7 +9,8 @@ enum RenderFlags RFL_TEXTURE_COMPRESSION=1, RFL_TEXTURE_COMPRESSION_S3TC=2, - RFL_SHADER_STORAGE_BUFFER = 4, // to be used later for a parameter buffer + RFL_SHADER_STORAGE_BUFFER = 4, + RFL_BUFFER_STORAGE = 8 }; enum TexMode diff --git a/src/win32/win32gliface.cpp b/src/win32/win32gliface.cpp index ab6d152545..a9911460fb 100644 --- a/src/win32/win32gliface.cpp +++ b/src/win32/win32gliface.cpp @@ -726,6 +726,32 @@ bool Win32GLVideo::SetupPixelFormat(int multisample) // //========================================================================== +bool Win32GLVideo::checkCoreUsability() +{ + // if we explicitly want to disable 4.x features this must fail. + if (Args->CheckParm("-gl3")) return false; + + // GL 4.4 implies GL_ARB_buffer_storage + if (strcmp((char*)glGetString(GL_VERSION), "4.4") >= 0) return true; + + // at this point GLEW has not been initialized so we have to retrieve glGetStringi ourselves. + PFNGLGETSTRINGIPROC myglGetStringi = (PFNGLGETSTRINGIPROC)wglGetProcAddress("glGetStringi"); + if (!myglGetStringi) return false; // this should not happen. + + const char *extension; + + int max = 0; + glGetIntegerv(GL_NUM_EXTENSIONS, &max); + + // step through all reported extensions and see if we got what we need... + for (int i = 0; i < max; i++) + { + extension = (const char*)myglGetStringi(GL_EXTENSIONS, i); + if (!strcmp(extension, "GL_ARB_buffer_storage")) return true; + } + return false; +} + bool Win32GLVideo::InitHardware (HWND Window, int multisample) { m_Window=Window; @@ -737,36 +763,50 @@ bool Win32GLVideo::InitHardware (HWND Window, int multisample) return false; } - m_hRC = NULL; - if (myWglCreateContextAttribsARB != NULL) + for (int prof = WGL_CONTEXT_CORE_PROFILE_BIT_ARB; prof <= WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB; prof++) { - // let's try to get the best version possible. Some drivers only give us the version we request - // which breaks all version checks for feature support. The highest used features we use are from version 4.4, and 3.0 is a requirement. - static int versions[] = { 44, 43, 42, 41, 40, 33, -1 }; - - for (int i = 0; versions[i] > 0; i++) + m_hRC = NULL; + if (myWglCreateContextAttribsARB != NULL) { - int ctxAttribs[] = { - WGL_CONTEXT_MAJOR_VERSION_ARB, versions[i] / 10, - WGL_CONTEXT_MINOR_VERSION_ARB, versions[i] % 10, - WGL_CONTEXT_FLAGS_ARB, gl_debug ? WGL_CONTEXT_DEBUG_BIT_ARB : 0, - WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB, - 0 - }; + // let's try to get the best version possible. Some drivers only give us the version we request + // which breaks all version checks for feature support. The highest used features we use are from version 4.4, and 3.3 is a requirement. + static int versions[] = { 45, 44, 43, 42, 41, 40, 33, -1 }; - m_hRC = myWglCreateContextAttribsARB(m_hDC, 0, ctxAttribs); - if (m_hRC != NULL) break; + for (int i = 0; versions[i] > 0; i++) + { + int ctxAttribs[] = { + WGL_CONTEXT_MAJOR_VERSION_ARB, versions[i] / 10, + WGL_CONTEXT_MINOR_VERSION_ARB, versions[i] % 10, + WGL_CONTEXT_FLAGS_ARB, gl_debug ? WGL_CONTEXT_DEBUG_BIT_ARB : 0, + WGL_CONTEXT_PROFILE_MASK_ARB, prof, + 0 + }; + + m_hRC = myWglCreateContextAttribsARB(m_hDC, 0, ctxAttribs); + if (m_hRC != NULL) break; + } + } + + if (m_hRC == NULL && prof == WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB) + { + Printf("R_OPENGL: Couldn't create render context. Reverting to software mode...\n"); + return false; + } + + wglMakeCurrent(m_hDC, m_hRC); + + // we can only use core profile contexts if GL_ARB_buffer_storage is supported or GL version is >= 4.4 + if (prof == WGL_CONTEXT_CORE_PROFILE_BIT_ARB && !checkCoreUsability()) + { + wglMakeCurrent(0, 0); + wglDeleteContext(m_hRC); + } + else + { + return true; } } - - if (m_hRC == NULL) - { - Printf ("R_OPENGL: Couldn't create render context. Reverting to software mode...\n"); - return false; - } - - wglMakeCurrent(m_hDC, m_hRC); - return true; + return false; } //========================================================================== diff --git a/src/win32/win32gliface.h b/src/win32/win32gliface.h index f61c318570..f425757f1d 100644 --- a/src/win32/win32gliface.h +++ b/src/win32/win32gliface.h @@ -87,6 +87,7 @@ protected: void MakeModesList(); void AddMode(int x, int y, int bits, int baseHeight, int refreshHz); void FreeModes(); + bool checkCoreUsability(); public: int GetTrueHeight() { return m_trueHeight; }