Skip to content

Commit

Permalink
Load angle dynamically when using NME_LOCAL_TOOLKIT
Browse files Browse the repository at this point in the history
  • Loading branch information
hughsando committed Sep 16, 2024
1 parent fb19106 commit 067a6ab
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 64 deletions.
7 changes: 7 additions & 0 deletions project/ToolkitBuild.xml
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,13 @@
<lib name="comdlg32.lib" />
<lib name="setupapi.lib" />
<lib name="d3d9.lib" if="NME_ANGLE"/>
<section if="NME_SDL3">
<lib name="d3d9.lib" />
<!--
<lib name="d3d11.lib" />
<lib name="d3d12.lib" />
-->
</section>
</section>

<section if="winrt">
Expand Down
23 changes: 15 additions & 8 deletions project/src/common/ExternalInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -780,26 +780,33 @@ int nme_get_bits()
DEFINE_PRIME0(nme_get_bits);


value nme_log(value inMessage)
void nme_log(HxString message)
{
HxString message = valToHxString(inMessage);
#ifdef IPHONE
nmeLog(message.c_str());
#else
printf("%s\n",message.c_str());
#endif

return alloc_null();
}
DEFINE_PRIM(nme_log,1);
DEFINE_PRIME1v(nme_log);

int *sNmeCrashPtr = (int *)nullptr;
value nme_crash()
void nme_crash()
{
*sNmeCrashPtr = 0xdeadbeef;
return alloc_null();
}
DEFINE_PRIM(nme_crash,0);
DEFINE_PRIME0v(nme_crash);

void nme_set_renderer(HxString inRenderer)
{
//printf("nme_set_renderer %s\n", inRenderer.c_str() );
#ifdef NME_DYNAMIC_ANGLE
extern bool nmeEglMode;
if (std::string(inRenderer.c_str())=="opengl")
nmeEglMode = false;
#endif
}
DEFINE_PRIME1v(nme_set_renderer);



Expand Down
1 change: 1 addition & 0 deletions project/src/opengl/OGL.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ class GPUProg
};

void InitOGL2Extensions();
bool InitDynamicGLES();

#define NME_GL_STATS_DRAW_ARRAYS 0x0
#define NME_GL_STATS_DRAW_ELEMENTS 0x2
Expand Down
11 changes: 10 additions & 1 deletion project/src/opengl/OGLShaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ GLuint OGLProg::createShader(GLuint inType, const char *inShader)
{
const char *source = inShader;
GLuint shader = glCreateShader(inType);
if (shader==0)
{
ELOG("Error in glCreateShader : %d\n", glGetError() );
*(int *)0=0;
return 0;
}


glShaderSource(shader,1,&source,0);
glCompileShader(shader);
Expand All @@ -50,7 +57,7 @@ GLuint OGLProg::createShader(GLuint inType, const char *inShader)
if (compiled)
return shader;

GLint blen = 0;
GLint blen = 0;
GLsizei slen = 0;

glGetShaderiv(shader, GL_INFO_LOG_LENGTH , &blen);
Expand All @@ -64,6 +71,8 @@ GLuint OGLProg::createShader(GLuint inType, const char *inShader)
}
else
{
printf("Shader %d\n", shader);
ELOG("%s\n", source);
ELOG("Unknown error compiling shader : %d\n", glGetError() );
}
glDeleteShader(shader);
Expand Down
98 changes: 59 additions & 39 deletions project/src/opengl/OpenGLContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#endif

#ifdef NME_DYNAMIC_ANGLE
bool nmeEglMode = false;
bool nmeEglMode = true;
#endif


Expand Down Expand Up @@ -849,7 +849,8 @@ class OGLContext : public HardwareRenderer
static HMODULE gEGLLibraryHandle = 0;
static HMODULE gOGLLibraryHandle = 0;
typedef PROC (*wglGetProcAddressFunc)(const char * unnamedParam1);
wglGetProcAddressFunc dynamicGetProcAddress = nullptr;
wglGetProcAddressFunc dynamicWglGetProcAddress = nullptr;
wglGetProcAddressFunc dynamicEglGetProcAddress = nullptr;
#else
//static void * gEGLLibraryHandle = 0;
static void * gOGLLibraryHandle = 0;
Expand All @@ -863,10 +864,20 @@ void *GetGlFunction(const char *functionName)
void *result = nullptr;
#if defined(NME_DYNAMIC_ANGLE)
#ifdef HX_WINDOWS
if (dynamicGetProcAddress)
result = dynamicGetProcAddress(functionName);
if (!result && dynamicEglGetProcAddress)
{
result = dynamicEglGetProcAddress(functionName);
}
if (!result && dynamicWglGetProcAddress)
{
result = dynamicWglGetProcAddress(functionName);
}
if (!result && gOGLLibraryHandle)
{
result = (void *)GetProcAddress(gOGLLibraryHandle,functionName);
}
//if (!result)
// printf("Could not get %p/%p %s\n", dynamicWglGetProcAddress, dynamicEglGetProcAddress ,functionName);
#endif
#elif defined(NME_ANGLE)
//result = (void *)eglGetProcAddress(functionName);
Expand All @@ -884,61 +895,65 @@ void *GetGlFunction(const char *functionName)
return result;
}

bool use_angle = false;

bool InitOGLFunctions()
// Loads eglGetProcAddress, if wanted by nmeEglMode.
// Otherwise sets up to load opengl32
bool InitDynamicGLES()
{
static bool result = true;
if (!extentions_init)
static bool isinit = false;
if (!isinit)
{
extentions_init = true;
isinit = true;

#ifdef NME_DYNAMIC_ANGLE
if (use_angle)
if (nmeEglMode)
{
#ifdef HX_WINDOWS
gEGLLibraryHandle = LoadLibraryA("libEGL.dll");
gOGLLibraryHandle = LoadLibraryA("libGLESv2.dll");
if (!gEGLLibraryHandle)
{
//printf("Could not open libEGL\n");
use_angle = false;
fprintf(stderr,"ERROR: Could not open libEGL\n");
nmeEglMode = false;
}
else
{
dynamicGetProcAddress = (wglGetProcAddressFunc)GetProcAddress(gEGLLibraryHandle, "eglGetProcAddress");
//printf("eglGetProcAddress -> %p\n", dynamicGetProcAddress);
dynamicEglGetProcAddress = (wglGetProcAddressFunc)GetProcAddress(gEGLLibraryHandle, "eglGetProcAddress");
}
#endif

if (dynamicEglGetProcAddress)
return true;
}
#endif

if (!use_angle)
#ifdef HX_WINDOWS
gOGLLibraryHandle = LoadLibraryA("opengl32.dll");
if (!gOGLLibraryHandle)
{
#ifdef HX_WINDOWS
gOGLLibraryHandle = LoadLibraryA("opengl32.dll");
if (!gOGLLibraryHandle)
{
printf("Error - could not load hardware driver\n");
result = false;
return result;
}

dynamicGetProcAddress = (wglGetProcAddressFunc)GetProcAddress(gOGLLibraryHandle, "wglGetProcAddress");
if (!dynamicGetProcAddress)
{
printf("Error - could not load hardware interface\n");
result = false;
return result;
}
#endif
printf("Error - could not load hardware driver\n");
return false;
}

nmeEglMode = use_angle;
#else
#ifdef HX_WINDOWS
gOGLLibraryHandle = LoadLibraryA("opengl32.dll");
#endif
dynamicWglGetProcAddress = (wglGetProcAddressFunc)GetProcAddress(gOGLLibraryHandle, "wglGetProcAddress");
if (!dynamicWglGetProcAddress)
{
printf("Error - could not load hardware interface\n");
return false;
}
#endif
}

return dynamicEglGetProcAddress;
}

bool InitOGLFunctions()
{
static bool result = true;
if (!extentions_init)
{
extentions_init = true;

InitDynamicGLES();


#ifdef HX_LINUX
if (!gOGLLibraryHandle)
Expand Down Expand Up @@ -988,3 +1003,8 @@ HardwareRenderer *HardwareRenderer::CreateOpenGL(void *inWindow, void *inGLCtx,
}

} // end namespace nme

extern "C" {
void nmeInitOGLFunctions() { nme::InitOGLFunctions(); }
}

45 changes: 37 additions & 8 deletions project/src/sdl2/SDL2Stage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,11 @@ int InitSDL()
#endif

#ifdef NME_DYNAMIC_ANGLE
InitOGLFunctions();
SDL_SetHint(SDL_HINT_RENDER_DRIVER, nmeEglMode ? "opengles2" : "opengl");
bool forceEgl = InitDynamicGLES();
SDL_SetHint(SDL_HINT_VIDEO_FORCE_EGL, forceEgl ? "1" : "0");
#endif


int err = SDL_Init(SDL_INIT_VIDEO | audioFlag | SDL_INIT_TIMER);

if (err == 0 && SDL_InitSubSystem (SDL_INIT_GAMECONTROLLER) == 0)
Expand Down Expand Up @@ -192,10 +193,16 @@ class HardwareRenderSurface : public HardwareSurface
lastWindow = window;
int width = 0;
int height = 0;

#ifdef SDL3
SDL_GetWindowSizeInPixels(window, &width, &height);
#else
SDL_GL_GetDrawableSize(window, &width, &height);
//SDL_GetWindowSize(window, &width, &height);
mHardware->SetWindowSize(width,height);
#endif
SDL_GL_MakeCurrent(window, context);
mHardware->SetWindowSize(width,height);

}
return HardwareSurface::BeginRender(inRect, inForHitTest);
}
Expand Down Expand Up @@ -2246,6 +2253,7 @@ void CreateMainFrame(FrameCreationCallback inOnFrame, int inWidth, int inHeight,
MacBoot();
#endif


bool fullscreen = (inFlags & wfFullScreen) != 0;
#if defined(NME_OGL) && defined(NME_METAL)
nmeOpenglRenderer = !(inFlags & wfHardwareMetal);
Expand Down Expand Up @@ -2509,11 +2517,32 @@ void CreateMainFrame(FrameCreationCallback inOnFrame, int inWidth, int inHeight,

#ifdef NME_SDL3
{
//int n = SDL_GetNumRenderDrivers();
//for(int i=0;i<n;i++)
// printf(" %d] %s\n",i, SDL_GetRenderDriver(i));
int n = SDL_GetNumRenderDrivers();
#if !defined(NME_DYNAMIC_ANGLE)
bool useEgl = nmeEglMode;
#else
bool useEgl = false;
#endif

for(int i=0;i<n;i++)
{
std::string rname = SDL_GetRenderDriver(i);
#if defined(NME_DYNAMIC_ANGLE)
if (rname=="opengles2" && nmeEglMode)
useEgl = true;
#endif
//printf(" %d] %s\n",i, rname.c_str());
}

renderer = SDL_CreateRenderer(window, metal ? "metal" : useEgl ? "opengles2" : "opengl" );
std::string rname = SDL_GetRendererName(renderer);
#ifdef NME_DYNAMIC_ANGLE
if (rname=="opengles2")
nmeEglMode = true;
#endif
//printf("Using driver: %s, nmeEglMode=%d\n", rname.c_str(), nmeEglMode);

renderer = SDL_CreateRenderer(window, metal ? "metal" : nullptr);
InitOGLFunctions();
}
#else
int renderFlags = 0;
Expand All @@ -2528,7 +2557,7 @@ void CreateMainFrame(FrameCreationCallback inOnFrame, int inWidth, int inHeight,
}
else
{
sgIsOGL2 = false;
sgIsOGL2 = nmeEglMode;
}

if (!renderer && (inFlags & wfHW_AA_HIRES || inFlags & wfHW_AA))
Expand Down
Loading

0 comments on commit 067a6ab

Please sign in to comment.