2016-09-14 18:01:13 +00:00
//
//---------------------------------------------------------------------------
//
// Copyright(C) 2005-2016 Christoph Oelckers
// All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see http://www.gnu.org/licenses/
//
//--------------------------------------------------------------------------
//
2013-06-23 07:49:34 +00:00
/*
* * r_opengl . cpp
* *
* * OpenGL system interface
* *
*/
2016-09-14 18:01:13 +00:00
2018-05-16 21:34:52 +00:00
# include "gl_load/gl_system.h"
2013-06-23 07:49:34 +00:00
# include "tarray.h"
# include "doomtype.h"
# include "m_argv.h"
# include "version.h"
2018-04-26 22:22:00 +00:00
# include "v_video.h"
2018-05-16 21:21:21 +00:00
# include "gl_load/gl_interface.h"
2018-04-25 18:33:55 +00:00
# include "hwrenderer/utility/hw_cvars.h"
2013-06-23 07:49:34 +00:00
static TArray < FString > m_Extensions ;
2013-08-18 13:41:52 +00:00
RenderContext gl ;
2018-02-13 22:06:59 +00:00
static double realglversion ; // this is public so the statistics code can access it.
2013-06-23 07:49:34 +00:00
//==========================================================================
//
//
//
//==========================================================================
static void CollectExtensions ( )
{
2014-07-15 00:26:23 +00:00
const char * extension ;
2013-06-23 07:49:34 +00:00
2014-07-15 00:26:23 +00:00
int max = 0 ;
glGetIntegerv ( GL_NUM_EXTENSIONS , & max ) ;
2013-06-23 07:49:34 +00:00
2018-11-30 16:16:21 +00:00
// Use modern method to collect extensions
for ( int i = 0 ; i < max ; i + + )
2013-06-23 07:49:34 +00:00
{
2018-11-30 16:16:21 +00:00
extension = ( const char * ) glGetStringi ( GL_EXTENSIONS , i ) ;
m_Extensions . Push ( FString ( extension ) ) ;
2013-06-23 07:49:34 +00:00
}
}
//==========================================================================
//
//
//
//==========================================================================
static bool CheckExtension ( const char * ext )
{
for ( unsigned int i = 0 ; i < m_Extensions . Size ( ) ; + + i )
{
if ( m_Extensions [ i ] . CompareNoCase ( ext ) = = 0 ) return true ;
}
return false ;
}
2013-08-18 13:41:52 +00:00
2013-06-23 07:49:34 +00:00
//==========================================================================
//
//
//
//==========================================================================
2013-08-18 13:41:52 +00:00
static void InitContext ( )
2013-06-23 07:49:34 +00:00
{
2013-08-18 13:41:52 +00:00
gl . flags = 0 ;
}
//==========================================================================
//
//
//
//==========================================================================
2016-04-26 11:50:05 +00:00
# define FUDGE_FUNC(name, ext) if (_ptrc_##name == NULL) _ptrc_##name = _ptrc_##name##ext;
2013-08-18 13:41:52 +00:00
void gl_LoadExtensions ( )
{
InitContext ( ) ;
2013-06-23 07:49:34 +00:00
CollectExtensions ( ) ;
2016-04-26 11:50:05 +00:00
const char * glversion = ( const char * ) glGetString ( GL_VERSION ) ;
2017-01-25 06:18:26 +00:00
const char * version = Args - > CheckValue ( " -glversion " ) ;
2018-02-13 22:06:59 +00:00
realglversion = strtod ( glversion , NULL ) ;
2016-04-26 11:50:05 +00:00
if ( version = = NULL )
{
version = glversion ;
}
else
{
double v1 = strtod ( version , NULL ) ;
2018-06-08 17:12:06 +00:00
if ( v1 > = 3.0 & & v1 < 3.3 )
{
v1 = 3.3 ; // promote '3' to 3.3 to avoid falling back to the legacy path.
version = " 3.3 " ;
}
2018-02-13 22:06:59 +00:00
if ( realglversion < v1 ) version = glversion ;
2016-04-26 11:50:05 +00:00
else Printf ( " Emulating OpenGL v %s \n " , version ) ;
}
2016-09-01 09:52:52 +00:00
float gl_version = ( float ) strtod ( version , NULL ) + 0.01f ;
2014-06-13 23:24:28 +00:00
2018-06-08 17:12:06 +00:00
// Don't even start if it's lower than 2.0 or no framebuffers are available (The framebuffer extension is needed for glGenerateMipmapsEXT!)
if ( gl_version < 3.3f )
2013-06-23 07:49:34 +00:00
{
2018-10-31 09:50:45 +00:00
I_FatalError ( " Unsupported OpenGL version. \n At least OpenGL 3.3 is required to run " GAMENAME " . \n For older versions of OpenGL please download the vintage build of " GAMENAME " . \n " ) ;
2013-06-23 07:49:34 +00:00
}
2014-07-15 00:26:23 +00:00
2013-06-23 07:49:34 +00:00
2018-06-08 17:12:06 +00:00
// add 0.01 to account for roundoff errors making the number a tad smaller than the actual version
gl . glslversion = strtod ( ( char * ) glGetString ( GL_SHADING_LANGUAGE_VERSION ) , NULL ) + 0.01f ;
2013-06-23 07:49:34 +00:00
2018-06-08 17:12:06 +00:00
gl . vendorstring = ( char * ) glGetString ( GL_VENDOR ) ;
2018-07-21 17:14:11 +00:00
gl . modelstring = ( char * ) glGetString ( GL_RENDERER ) ;
2016-09-01 09:52:52 +00:00
2018-06-08 17:12:06 +00:00
// first test for optional features
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 ( gl_version < 4.f )
{
# ifdef _WIN32
if ( strstr ( gl . vendorstring , " ATI Tech " ) )
2016-08-29 08:43:03 +00:00
{
2018-06-08 17:12:06 +00:00
gl . flags | = RFL_NO_CLIP_PLANES ; // gl_ClipDistance is horribly broken on ATI GL3 drivers for Windows. (TBD: Relegate to vintage build? Maybe after the next survey.)
2016-09-01 09:52:52 +00:00
}
2017-01-25 06:18:26 +00:00
# endif
2018-07-28 10:43:35 +00:00
gl . glslversion = 3.31f ; // Force GLSL down to 3.3.
2018-06-08 17:12:06 +00:00
}
else if ( gl_version < 4.5f )
{
// don't use GL 4.x features when running a GL 3.x context.
if ( CheckExtension ( " GL_ARB_buffer_storage " ) )
{
// work around a problem with older AMD drivers: Their implementation of shader storage buffer objects is piss-poor and does not match uniform buffers even closely.
// Recent drivers, GL 4.4 don't have this problem, these can easily be recognized by also supporting the GL_ARB_buffer_storage extension.
if ( CheckExtension ( " GL_ARB_shader_storage_buffer_object " ) )
2014-09-17 07:01:16 +00:00
{
2018-07-28 10:43:35 +00:00
gl . flags | = RFL_SHADER_STORAGE_BUFFER ;
2017-01-25 06:18:26 +00:00
}
2018-06-08 17:12:06 +00:00
gl . flags | = RFL_BUFFER_STORAGE ;
}
}
else
{
// Assume that everything works without problems on GL 4.5 drivers where these things are core features.
gl . flags | = RFL_SHADER_STORAGE_BUFFER | RFL_BUFFER_STORAGE ;
}
2016-04-26 11:50:05 +00:00
2018-07-28 10:43:35 +00:00
// Mesa implements shader storage only for fragment shaders.
// Just disable the feature there. The light buffer may just use a uniform buffer without any adverse effects.
int v ;
glGetIntegerv ( GL_MAX_VERTEX_SHADER_STORAGE_BLOCKS , & v ) ;
if ( v = = 0 )
gl . flags & = ~ RFL_SHADER_STORAGE_BUFFER ;
2018-06-08 17:12:06 +00:00
if ( gl_version > = 4.3f | | CheckExtension ( " GL_ARB_invalidate_subdata " ) ) gl . flags | = RFL_INVALIDATE_BUFFER ;
if ( gl_version > = 4.3f | | CheckExtension ( " GL_KHR_debug " ) ) gl . flags | = RFL_DEBUG ;
2016-08-17 15:37:13 +00:00
2018-07-29 19:31:16 +00:00
glGetIntegerv ( GL_MAX_FRAGMENT_UNIFORM_COMPONENTS , & v ) ;
gl . maxuniforms = v ;
glGetIntegerv ( GL_MAX_UNIFORM_BLOCK_SIZE , & v ) ;
gl . maxuniformblock = v ;
glGetIntegerv ( GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT , & v ) ;
gl . uniformblockalignment = v ;
2016-04-26 11:50:05 +00:00
glGetIntegerv ( GL_MAX_TEXTURE_SIZE , & gl . max_texturesize ) ;
2013-06-23 07:49:34 +00:00
glPixelStorei ( GL_UNPACK_ALIGNMENT , 1 ) ;
}
//==========================================================================
//
//
//
//==========================================================================
2013-08-18 13:41:52 +00:00
void gl_PrintStartupLog ( )
2013-06-23 07:49:34 +00:00
{
2015-04-27 19:25:16 +00:00
int v = 0 ;
2018-06-08 17:12:06 +00:00
glGetIntegerv ( GL_CONTEXT_PROFILE_MASK , & v ) ;
2014-08-23 23:09:44 +00:00
2013-06-23 07:49:34 +00:00
Printf ( " GL_VENDOR: %s \n " , glGetString ( GL_VENDOR ) ) ;
Printf ( " GL_RENDERER: %s \n " , glGetString ( GL_RENDERER ) ) ;
2014-08-23 23:09:44 +00:00
Printf ( " GL_VERSION: %s (%s profile) \n " , glGetString ( GL_VERSION ) , ( v & GL_CONTEXT_CORE_PROFILE_BIT ) ? " Core " : " Compatibility " ) ;
2013-06-23 07:49:34 +00:00
Printf ( " GL_SHADING_LANGUAGE_VERSION: %s \n " , glGetString ( GL_SHADING_LANGUAGE_VERSION ) ) ;
2017-01-05 11:01:00 +00:00
Printf ( PRINT_LOG , " GL_EXTENSIONS: " ) ;
2014-07-15 00:26:23 +00:00
for ( unsigned i = 0 ; i < m_Extensions . Size ( ) ; i + + )
{
2017-01-05 11:01:00 +00:00
Printf ( PRINT_LOG , " %s " , m_Extensions [ i ] . GetChars ( ) ) ;
2014-07-15 00:26:23 +00:00
}
2013-06-23 07:49:34 +00:00
2014-04-06 12:35:44 +00:00
glGetIntegerv ( GL_MAX_TEXTURE_SIZE , & v ) ;
2014-07-15 00:26:23 +00:00
Printf ( " \n Max. texture size: %d \n " , v ) ;
2013-06-23 07:49:34 +00:00
glGetIntegerv ( GL_MAX_TEXTURE_IMAGE_UNITS , & v ) ;
Printf ( " Max. texture units: %d \n " , v ) ;
glGetIntegerv ( GL_MAX_VARYING_FLOATS , & v ) ;
Printf ( " Max. varying: %d \n " , v ) ;
2016-04-30 14:31:09 +00:00
if ( gl . flags & RFL_SHADER_STORAGE_BUFFER )
{
glGetIntegerv ( GL_MAX_COMBINED_SHADER_STORAGE_BLOCKS , & v ) ;
Printf ( " Max. combined shader storage blocks: %d \n " , v ) ;
glGetIntegerv ( GL_MAX_VERTEX_SHADER_STORAGE_BLOCKS , & v ) ;
Printf ( " Max. vertex shader storage blocks: %d \n " , v ) ;
}
2018-07-28 10:43:35 +00:00
else
{
glGetIntegerv ( GL_MAX_UNIFORM_BLOCK_SIZE , & v ) ;
Printf ( " Max. uniform block size: %d \n " , v ) ;
glGetIntegerv ( GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT , & v ) ;
Printf ( " Uniform block alignment: %d \n " , v ) ;
}
2013-06-23 07:49:34 +00:00
}
2018-02-13 22:06:59 +00:00
std : : pair < double , bool > gl_getInfo ( )
{
// gl_ARB_bindless_texture is the closest we can get to determine Vulkan support from OpenGL.
// This isn't foolproof because Intel doesn't support it but for NVidia and AMD support of this extension means Vulkan support.
return std : : make_pair ( realglversion , CheckExtension ( " GL_ARB_bindless_texture " ) ) ;
2018-11-30 16:16:21 +00:00
}