@@ -936,17 +936,19 @@ bool IsGLExtensionsSupported(HDC hdc, std::string extension) noexcept
936
936
return std::binary_search (extensions.begin (), extensions.end (), extension);
937
937
}
938
938
939
- void LoadOpenGLExtensions () noexcept
939
+ bool LoadOpenGLExtensions () noexcept
940
940
{
941
- static std::once_flag token = {};
942
- std::call_once (token, [&] {
941
+ if (!glGenBuffers)
942
+ {
943
943
glGenBuffers = reinterpret_cast <decltype (glGenBuffers)>(wglGetProcAddress (" glGenBuffers" ));
944
944
glDeleteBuffers = reinterpret_cast <decltype (glDeleteBuffers)>(wglGetProcAddress (" glDeleteBuffers" ));
945
945
glBindBuffer = reinterpret_cast <decltype (glBindBuffer)>(wglGetProcAddress (" glBindBuffer" ));
946
946
glBufferData = reinterpret_cast <decltype (glBufferData)>(wglGetProcAddress (" glBufferData" ));
947
947
glMapBuffer = reinterpret_cast <decltype (glMapBuffer)>(wglGetProcAddress (" glMapBuffer" ));
948
948
glUnmapBuffer = reinterpret_cast <decltype (glUnmapBuffer)>(wglGetProcAddress (" glUnmapBuffer" ));
949
- });
949
+ }
950
+
951
+ return glGenBuffers && glDeleteBuffers && glBindBuffer && glBufferData && glMapBuffer && glUnmapBuffer;
950
952
}
951
953
952
954
void GeneratePixelBuffers (void * ctx, GLuint (&pbo)[2], GLint width, GLint height, GLint stride) noexcept
@@ -975,11 +977,11 @@ void GeneratePixelBuffers(void* ctx, GLuint (&pbo)[2], GLint width, GLint height
975
977
// Generate buffers
976
978
glGenBuffers (2 , pbo);
977
979
glBindBuffer (GL_PIXEL_PACK_BUFFER, pbo[0 ]);
978
- glBufferData (GL_PIXEL_PACK_BUFFER, width * height * stride, 0 , GL_STREAM_READ);
980
+ glBufferData (GL_PIXEL_PACK_BUFFER, width * height * stride, nullptr , GL_STREAM_READ);
979
981
glBindBuffer (GL_PIXEL_PACK_BUFFER, 0 );
980
982
981
983
glBindBuffer (GL_PIXEL_PACK_BUFFER, pbo[1 ]);
982
- glBufferData (GL_PIXEL_PACK_BUFFER, width * height * stride, 0 , GL_STREAM_READ);
984
+ glBufferData (GL_PIXEL_PACK_BUFFER, width * height * stride, nullptr , GL_STREAM_READ);
983
985
glBindBuffer (GL_PIXEL_PACK_BUFFER, 0 );
984
986
}
985
987
}
@@ -1009,7 +1011,7 @@ void ReadPixelBuffers(void* ctx, GLubyte* dest, GLuint (&pbo)[2], GLint width, G
1009
1011
index = (index + 1 ) % 2 ;
1010
1012
nextIndex = (index + 1 ) % 2 ;
1011
1013
1012
- // read back-buffer.
1014
+ // Read back-buffer.
1013
1015
glPixelStorei (GL_UNPACK_ROW_LENGTH, width);
1014
1016
glBindBuffer (GL_PIXEL_PACK_BUFFER, pbo[index ]);
1015
1017
glReadPixels (0 , 0 , width, height, gl_format, GL_UNSIGNED_BYTE, nullptr );
@@ -1028,7 +1030,9 @@ void ReadPixelBuffers(void* ctx, GLubyte* dest, GLuint (&pbo)[2], GLint width, G
1028
1030
glReadPixels (0 , 0 , width, height, gl_format, GL_UNSIGNED_BYTE, dest);
1029
1031
}
1030
1032
1033
+ // Restore state
1031
1034
glBindBuffer (GL_PIXEL_PACK_BUFFER, 0 );
1035
+ glPixelStorei (GL_UNPACK_ROW_LENGTH, 0 );
1032
1036
}
1033
1037
1034
1038
void PushGLContext (HDC hdc, GLint width, GLint height) noexcept
@@ -1132,7 +1136,11 @@ BOOL __stdcall mSwapBuffers(HDC hdc) noexcept
1132
1136
1133
1137
// Check if extensions are supported
1134
1138
// This check is needed for renderers that do not support pixel buffer objects or vertex buffer objects
1135
- bool hasGLExtension = IsGLExtensionsSupported (hdc, " GL_ARB_vertex_buffer_object" ) || IsGLExtensionsSupported (hdc, " GL_ARB_pixel_buffer_object" );
1139
+ // static bool hasGLExtension = IsGLExtensionsSupported(hdc, "GL_ARB_vertex_buffer_object") || IsGLExtensionsSupported(hdc, "GL_ARB_pixel_buffer_object");
1140
+
1141
+ // The above extension check is unreliable!
1142
+ // It's best to attempt to load the extensions and see if they exist.
1143
+ static bool hasGLExtension = LoadOpenGLExtensions ();
1136
1144
1137
1145
// Render to Shared Memory
1138
1146
std::uint8_t * dest = control_center->get_image ();
@@ -1142,7 +1150,6 @@ BOOL __stdcall mSwapBuffers(HDC hdc) noexcept
1142
1150
if (hasGLExtension)
1143
1151
{
1144
1152
// Performance Boost! :D
1145
- LoadOpenGLExtensions ();
1146
1153
GeneratePixelBuffers (hdc, pbo, width, height, 4 );
1147
1154
ReadPixelBuffers (hdc, dest, pbo, width, height, 4 , format);
1148
1155
}
0 commit comments