Skip to content

Commit 4a3d99e

Browse files
committed
Fix crash in 64-bit. Fix slow rendering in 64-bit (OpenGL)
Fix OpenGL rendering without Extension Support
1 parent 766df49 commit 4a3d99e

8 files changed

+30
-19
lines changed

RemoteInput/Platform/NativeHooks_Darwin.cxx

+3-1
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ void ReadPixelBuffers(void* ctx, GLubyte* dest, GLuint (&pbo)[2], GLint width, G
442442
}
443443
}(format);
444444

445-
//read back-buffer.
445+
//Read back-buffer.
446446
glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
447447
glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo[index]);
448448
glReadPixels(0, 0, width, height, gl_format, GL_UNSIGNED_BYTE, nullptr);
@@ -461,7 +461,9 @@ void ReadPixelBuffers(void* ctx, GLubyte* dest, GLuint (&pbo)[2], GLint width, G
461461
glReadPixels(0, 0, width, height, gl_format, GL_UNSIGNED_BYTE, dest);
462462
}
463463

464+
//Restore state
464465
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
466+
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
465467
}
466468

467469
#if defined(USE_DETOURS)

RemoteInput/Platform/NativeHooks_Linux.cxx

+3-1
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ void ReadPixelBuffers(GLXDrawable ctx, GLubyte* dest, GLuint (&pbo)[2], GLint wi
492492
index = (index + 1) % 2;
493493
nextIndex = (index + 1) % 2;
494494

495-
//read back-buffer.
495+
//Read back-buffer.
496496
glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
497497
glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo[index]);
498498
glReadPixels(0, 0, width, height, gl_format, GL_UNSIGNED_BYTE, nullptr);
@@ -511,7 +511,9 @@ void ReadPixelBuffers(GLXDrawable ctx, GLubyte* dest, GLuint (&pbo)[2], GLint wi
511511
glReadPixels(0, 0, width, height, gl_format, GL_UNSIGNED_BYTE, dest);
512512
}
513513

514+
//Restore state
514515
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
516+
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
515517
}
516518

517519
#if defined(USE_DETOURS)

RemoteInput/Platform/NativeHooks_Windows.cxx

+16-9
Original file line numberDiff line numberDiff line change
@@ -936,17 +936,19 @@ bool IsGLExtensionsSupported(HDC hdc, std::string extension) noexcept
936936
return std::binary_search(extensions.begin(), extensions.end(), extension);
937937
}
938938

939-
void LoadOpenGLExtensions() noexcept
939+
bool LoadOpenGLExtensions() noexcept
940940
{
941-
static std::once_flag token = {};
942-
std::call_once(token, [&]{
941+
if (!glGenBuffers)
942+
{
943943
glGenBuffers = reinterpret_cast<decltype(glGenBuffers)>(wglGetProcAddress("glGenBuffers"));
944944
glDeleteBuffers = reinterpret_cast<decltype(glDeleteBuffers)>(wglGetProcAddress("glDeleteBuffers"));
945945
glBindBuffer = reinterpret_cast<decltype(glBindBuffer)>(wglGetProcAddress("glBindBuffer"));
946946
glBufferData = reinterpret_cast<decltype(glBufferData)>(wglGetProcAddress("glBufferData"));
947947
glMapBuffer = reinterpret_cast<decltype(glMapBuffer)>(wglGetProcAddress("glMapBuffer"));
948948
glUnmapBuffer = reinterpret_cast<decltype(glUnmapBuffer)>(wglGetProcAddress("glUnmapBuffer"));
949-
});
949+
}
950+
951+
return glGenBuffers && glDeleteBuffers && glBindBuffer && glBufferData && glMapBuffer && glUnmapBuffer;
950952
}
951953

952954
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
975977
//Generate buffers
976978
glGenBuffers(2, pbo);
977979
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);
979981
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
980982

981983
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);
983985
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
984986
}
985987
}
@@ -1009,7 +1011,7 @@ void ReadPixelBuffers(void* ctx, GLubyte* dest, GLuint (&pbo)[2], GLint width, G
10091011
index = (index + 1) % 2;
10101012
nextIndex = (index + 1) % 2;
10111013

1012-
//read back-buffer.
1014+
//Read back-buffer.
10131015
glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
10141016
glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo[index]);
10151017
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
10281030
glReadPixels(0, 0, width, height, gl_format, GL_UNSIGNED_BYTE, dest);
10291031
}
10301032

1033+
//Restore state
10311034
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
1035+
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
10321036
}
10331037

10341038
void PushGLContext(HDC hdc, GLint width, GLint height) noexcept
@@ -1132,7 +1136,11 @@ BOOL __stdcall mSwapBuffers(HDC hdc) noexcept
11321136

11331137
//Check if extensions are supported
11341138
//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();
11361144

11371145
//Render to Shared Memory
11381146
std::uint8_t* dest = control_center->get_image();
@@ -1142,7 +1150,6 @@ BOOL __stdcall mSwapBuffers(HDC hdc) noexcept
11421150
if (hasGLExtension)
11431151
{
11441152
//Performance Boost! :D
1145-
LoadOpenGLExtensions();
11461153
GeneratePixelBuffers(hdc, pbo, width, height, 4);
11471154
ReadPixelBuffers(hdc, dest, pbo, width, height, 4, format);
11481155
}

RemoteInput/Platform/Platform_Darwin.mm

+2-2
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ - (jobject)awtComponent:(JNIEnv*)env;
290290
if (vm.AttachCurrentThread(&env) == JNI_OK)
291291
{
292292
std::unique_ptr<Reflection> reflection;
293-
auto hasReflection = TimeOut(20, [&]{
293+
bool hasReflection = TimeOut(20, [&]{
294294
jclass cls = env->FindClass("java/awt/Frame");
295295
if (!cls)
296296
{
@@ -335,7 +335,7 @@ - (jobject)awtComponent:(JNIEnv*)env;
335335
return false;
336336
});
337337

338-
auto hasReflection2 = TimeOut(20, [&]{
338+
bool hasReflection2 = !hasReflection && TimeOut(20, [&]{
339339
for (auto&& view : GetWindowViews())
340340
{
341341
//TODO: Check if we can call "awt_GetComponent" from the JDK like on Linux

RemoteInput/Platform/Platform_Linux.cxx

+2-2
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ std::unique_ptr<Reflection> GetNativeReflector() noexcept
550550
if (vm.AttachCurrentThread(&env) == JNI_OK)
551551
{
552552
std::unique_ptr<Reflection> reflection;
553-
auto hasReflection = TimeOut(20, [&]{
553+
bool hasReflection = TimeOut(20, [&]{
554554
jclass cls = env->FindClass("java/awt/Frame");
555555
if (!cls)
556556
{
@@ -594,7 +594,7 @@ std::unique_ptr<Reflection> GetNativeReflector() noexcept
594594
return false;
595595
});
596596

597-
auto hasReflection2 = TimeOut(20, [&]{
597+
bool hasReflection2 = !hasReflection && TimeOut(20, [&]{
598598
if (!ModuleLoaded("libawt_xawt.so"))
599599
{
600600
return false;

RemoteInput/Platform/Platform_Windows.cxx

+2-2
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ std::unique_ptr<Reflection> GetNativeReflector() noexcept
388388
if (vm.AttachCurrentThread(&env) == JNI_OK)
389389
{
390390
std::unique_ptr<Reflection> reflection;
391-
auto hasReflection = TimeOut(20, [&]{
391+
bool hasReflection = TimeOut(20, [&]{
392392
jclass cls = env->FindClass("java/awt/Frame");
393393
if (!cls)
394394
{
@@ -432,7 +432,7 @@ std::unique_ptr<Reflection> GetNativeReflector() noexcept
432432
return false;
433433
});
434434

435-
auto hasReflection2 = TimeOut(20, [&]{
435+
bool hasReflection2 = !hasReflection && TimeOut(20, [&]{
436436
auto DSGetComponent = reinterpret_cast<jobject (__stdcall *)(JNIEnv*, void*)>(GetProcAddress(awt, "_DSGetComponent@8"));
437437
if (!DSGetComponent)
438438
{

RemoteInput/Plugin/Graphics.cxx

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ void RGBA(std::uint32_t colour, std::uint8_t& r, std::uint8_t& g, std::uint8_t&
162162
a = (colour & 0xFF);
163163
}
164164

165-
void FlipImageBytes(void* In, void* &Out, std::int32_t width, std::int32_t height, std::uint32_t Bpp) noexcept
165+
void FlipImageBytes(void* In, void* Out, std::int32_t width, std::int32_t height, std::uint32_t Bpp) noexcept
166166
{
167167
std::size_t Chunk = (Bpp > 24 ? width * 4 : width * 3 + width % 4);
168168
std::uint8_t* Destination = static_cast<std::uint8_t*>(Out);

RemoteInput/Plugin/Graphics.hxx

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ void alpha_blend_pixels(S source, D dest, std::int32_t width, std::int32_t heigh
110110
}
111111
}
112112

113-
void FlipImageBytes(void* In, void* &Out, std::int32_t width, std::int32_t height, std::uint32_t Bpp) noexcept;
113+
void FlipImageBytes(void* In, void* Out, std::int32_t width, std::int32_t height, std::uint32_t Bpp) noexcept;
114114
void FlipImageVertically(std::int32_t width, std::int32_t height, std::uint8_t* data) noexcept;
115115
void FlipImageVertically2(std::int32_t width, std::int32_t height, std::uint8_t* data) noexcept;
116116

0 commit comments

Comments
 (0)