Skip to content

Commit

Permalink
Initial GLES3 (bis)
Browse files Browse the repository at this point in the history
merged and improved from haxenme#496 .
Added GL3.hx for GLES3 functions.
It is only activated for windows and android because needs testing on
other targets (ios, linux, mac, emscripten). Windows angle needs
native-toolkit/libsdl#11 , otherwise gets a GLES2
context.
Tested with
https://github.com/madrazo/nme-opengl-tutorials/tree/master/tutorial02_red_triangle
  • Loading branch information
madrazo committed Oct 20, 2017
1 parent ad9680c commit d8031a1
Show file tree
Hide file tree
Showing 8 changed files with 286 additions and 8 deletions.
1 change: 1 addition & 0 deletions project/include/Hardware.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ class HardwareRenderer : public HardwareContext
virtual void DestroyShader(unsigned int inShader)=0;
virtual void DestroyFramebuffer(unsigned int inBuffer)=0;
virtual void DestroyRenderbuffer(unsigned int inBuffer)=0;
virtual void DestroyVertexarray(unsigned int inBuffer)=0;

#ifdef NME_S3D
virtual void EndS3DRender()=0;
Expand Down
14 changes: 14 additions & 0 deletions project/src/opengl/OGL.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@
#define NME_GLES
#define GL_GLEXT_PROTOTYPES

#ifndef NME_NO_GLES3COMPAT
#include <GLES3/gl3.h>
#endif
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>

#elif defined(BLACKBERRY) || defined(ANDROID) || defined(WEBOS) || defined(GPH) || defined(RASPBERRYPI) || defined(EMSCRIPTEN)

#define NME_GLES

#ifndef NME_NO_GLES3COMPAT
#include <GLES3/gl3.h>
#define __gl2_h_ //Not needed for Android Platform >= 21
#endif
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>

Expand All @@ -24,13 +31,20 @@

#include <gl2.h>
#include <gl2ext.h>
#ifndef NME_NO_GLES3COMPAT
#include <gl3.h>
#endif

#elif defined(IPHONE)

#include <OpenGLES/ES1/gl.h>
#include <OpenGLES/ES1/glext.h>
#include <OpenGLES/ES2/gl.h>
#include <OpenGLES/ES2/glext.h>
#ifndef NME_NO_GLES3COMPAT
#include <OpenGLES/ES3/gl.h>
#include <OpenGLES/ES3/glext.h>
#endif

//typedef CAEAGLLayer *WinDC;
//typedef EAGLContext *GLCtx;
Expand Down
46 changes: 41 additions & 5 deletions project/src/opengl/OGLExport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ enum ResoType
resoProgram, //4
resoFramebuffer, //5
resoRenderbuffer, //6
resoVertexarray, //7 (GLES3)
};

const char *getTypeString(int inType)
Expand All @@ -59,6 +60,7 @@ const char *getTypeString(int inType)
case resoProgram: return "Program";
case resoFramebuffer: return "Framebuffer";
case resoRenderbuffer: return "Renderbuffer";
case resoVertexarray: return "Vertexarray";
}
return "Unknown";
}
Expand Down Expand Up @@ -306,6 +308,9 @@ class NmeResource : public nme::Object
case resoRenderbuffer:
ctx->DestroyRenderbuffer(id);
break;
case resoVertexarray:
ctx->DestroyVertexarray(id);
break;
}
}
type = resoNone;
Expand Down Expand Up @@ -638,7 +643,6 @@ value nme_gl_get_parameter(value pname_val)
case GL_SAMPLE_BUFFERS:
case GL_SAMPLES:
case GL_SCISSOR_TEST:
case GL_SHADING_LANGUAGE_VERSION:
case GL_STENCIL_BACK_FAIL:
case GL_STENCIL_BACK_FUNC:
case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
Expand All @@ -663,6 +667,7 @@ value nme_gl_get_parameter(value pname_val)

case GL_VENDOR:
case GL_VERSION:
case GL_SHADING_LANGUAGE_VERSION:
case GL_RENDERER:
strings = 1;
break;
Expand Down Expand Up @@ -794,6 +799,17 @@ GL_GEN_RESO(buffer,glGenBuffers,resoBuffer)
GL_GEN_RESO(framebuffer,glGenFramebuffers,resoFramebuffer)
GL_GEN_RESO(render_buffer,glGenRenderbuffers,resoRenderbuffer)

//GLES3
#ifdef NME_NO_GLES3COMPAT
value nme_gl_create_vertexarray(value inType)
{
DBGFUNC("createShader");
return val_int(-1);
}
DEFINE_PRIM(nme_gl_create_vertexarray,0);
#else
GL_GEN_RESO(vertexarray,glGenVertexArrays,resoVertexarray)
#endif

// --- Stencil -------------------------------------------

Expand Down Expand Up @@ -1376,9 +1392,12 @@ value nme_gl_shader_source(value inId,value inSource)
const char *lines = source.c_str();
#ifdef NME_GLES
// TODO - do something better here
std::string buffer;
buffer = std::string("precision mediump float;\n") + hxToStdString(source);
lines = buffer.c_str();
if (lines[0]!='#' && lines[1]!='v')
{
std::string buffer;
buffer = std::string("precision mediump float;\n") + hxToStdString(source);
lines = buffer.c_str();
}
#endif

glShaderSource(id,1,&lines,0);
Expand Down Expand Up @@ -1756,9 +1775,26 @@ value nme_gl_get_render_buffer_parameter(value target, value pname)
}
DEFINE_PRIM(nme_gl_get_render_buffer_parameter,2);

// --- Drawing -------------------------------



// --- GLES3: VertexArray

value nme_gl_bind_vertexarray(value inId )
{
int id = getResourceId(inId,resoVertexarray);
#ifndef NME_NO_GLES3COMPAT
glBindVertexArray(id);
#endif
return alloc_null();
}
DEFINE_PRIM(nme_gl_bind_vertexarray,1);




// --- Drawing -------------------------------

value nme_gl_draw_arrays(value inMode, value inFirst, value inCount)
{
DBGFUNC("drawArrays");
Expand Down
23 changes: 23 additions & 0 deletions project/src/opengl/OpenGLContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,18 @@ class OGLContext : public HardwareRenderer
else
glDeleteRenderbuffers(1,&inBuffer);
}
void DestroyVertexarray(unsigned int inBuffer)
{
if ( !IsMainThread() )
{
mHasZombie = true;
mZombieVertexarrays.push_back(inBuffer);
}
#ifndef NME_NO_GLES3COMPAT
else
glDeleteVertexArrays(1,&inBuffer);
#endif
}


void OnContextLost()
Expand All @@ -164,6 +176,7 @@ class OGLContext : public HardwareRenderer
mZombieShaders.resize(0);
mZombieFramebuffers.resize(0);
mZombieRenderbuffers.resize(0);
mZombieVertexarrays.resize(0);
mHasZombie = false;
}

Expand Down Expand Up @@ -277,6 +290,14 @@ class OGLContext : public HardwareRenderer
glDeleteRenderbuffers(mZombieRenderbuffers.size(),&mZombieRenderbuffers[0]);
mZombieRenderbuffers.resize(0);
}

if (mZombieVertexarrays.size())
{
#ifndef NME_NO_GLES3COMPAT
glDeleteVertexArrays(mZombieVertexarrays.size(),&mZombieVertexarrays[0]);
#endif
mZombieVertexarrays.resize(0);
}
}


Expand Down Expand Up @@ -316,6 +337,7 @@ class OGLContext : public HardwareRenderer
mZombieShaders.resize(0);
mZombieFramebuffers.resize(0);
mZombieRenderbuffers.resize(0);
mZombieVertexarrays.resize(0);

ReloadExtentions();
}
Expand Down Expand Up @@ -771,6 +793,7 @@ class OGLContext : public HardwareRenderer
QuickVec<GLuint> mZombieShaders;
QuickVec<GLuint> mZombieFramebuffers;
QuickVec<GLuint> mZombieRenderbuffers;
QuickVec<GLuint> mZombieVertexarrays;

GPUProg *mProg[PROG_COUNT];

Expand Down
17 changes: 15 additions & 2 deletions project/src/sdl2/SDL2Stage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ static int sgDesktopWidth = 0;
static int sgDesktopHeight = 0;
static Rect sgWindowRect = Rect(0, 0, 0, 0);
static bool sgInitCalled = false;
//static bool sgJoystickEnabled = false;
static bool sgGameControllerEnabled = false;
static bool sgIsOGL2 = false;
const int sgJoystickDeadZone = 1000;
Expand Down Expand Up @@ -1806,9 +1805,13 @@ void CreateMainFrame(FrameCreationCallback inOnFrame, int inWidth, int inHeight,
if (fullscreen) requestWindowFlags |= FullscreenMode; //SDL_WINDOW_FULLSCREEN_DESKTOP;

#ifdef NME_ANGLE
int major = 3;
#ifdef NME_NO_GLES3COMPAT
major = 2;
#endif
SDL_GL_SetAttribute(SDL_GL_CONTEXT_EGL, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, major);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
#endif

Expand Down Expand Up @@ -1938,6 +1941,16 @@ void CreateMainFrame(FrameCreationCallback inOnFrame, int inWidth, int inHeight,
sgIsOGL2 = false;
}

#ifdef NME_ANGLE
if (!renderer && opengl && major>2)
{
fprintf(stderr, "GLES3 is not available. Retrying with GLES2. (%s)\n", SDL_GetError());
major = 2;
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, major);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
}
else
#endif
if (!renderer && (inFlags & wfHW_AA_HIRES || inFlags & wfHW_AA)) {
// if no window was created and AA was enabled, disable AA and try again
fprintf(stderr, "Multisampling is not available. Retrying without. (%s)\n", SDL_GetError());
Expand Down
41 changes: 41 additions & 0 deletions src/nme/gl/GL3.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package nme.gl;

import nme.display.BitmapData;
import nme.utils.ArrayBuffer;
import nme.utils.ByteArray;
import nme.utils.IMemoryRange;
import nme.utils.ArrayBufferView;
import nme.geom.Matrix3D;
import nme.Lib;
import nme.Loader;

#if (neko||cpp)
import nme.utils.Float32Array;
import nme.utils.Int32Array;
#end


@:nativeProperty
class GL3
{

#if (neko||cpp)

public static inline function bindVertexArray(vertexarray:GLVertexArray):Void
{
nme_gl_bind_vertexarray(vertexarray);
}

public static inline function createVertexArray():GLVertexArray
{
return new GLVertexArray(GL.version, nme_gl_create_vertexarray());
}


// Native Methods
private static var nme_gl_create_vertexarray = GL.load("nme_gl_create_vertexarray", 0);
private static var nme_gl_bind_vertexarray = GL.load("nme_gl_bind_vertexarray", 1);

#end
}

18 changes: 18 additions & 0 deletions src/nme/gl/GLVertexArray.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package nme.gl;
#if (!flash)

@:nativeProperty
class GLVertexArray extends GLObject
{
public function new(inVersion:Int, inId:Dynamic)
{
super(inVersion, inId);
}

override function getType():String
{
return "VertexArray";
}
}

#end
Loading

0 comments on commit d8031a1

Please sign in to comment.