bool init() { ConsoleExtensions.tracei("enter init, call eglGetCurrentContext"); mEglContext = egl.eglGetCurrentContext(); mProgram = createProgram(VERTEX_SHADER, FRAGMENT_SHADER); if (mProgram == 0) return false; ConsoleExtensions.tracei("init mProgram: ", (int)mProgram); gl3.glGenBuffers((int)VB.VB_COUNT, mVB); gl3.glBindBuffer(gl3.GL_ARRAY_BUFFER, mVB[(int)VB.VB_INSTANCE]); //gl3.glBufferData(gl3.GL_ARRAY_BUFFER, sizeof(QUAD), &QUAD[0], gl3.GL_STATIC_DRAW); gl3.glBufferData(gl3.GL_ARRAY_BUFFER, sizeof(Vertex) * 4, QUAD.__value, gl3.GL_STATIC_DRAW); gl3.glBindBuffer(gl3.GL_ARRAY_BUFFER, mVB[(int)VB.VB_SCALEROT]); gl3.glBufferData(gl3.GL_ARRAY_BUFFER, MAX_INSTANCES * 4 * sizeof(float), null, gl3.GL_DYNAMIC_DRAW); gl3.glBindBuffer(gl3.GL_ARRAY_BUFFER, mVB[(int)VB.VB_OFFSET]); gl3.glBufferData(gl3.GL_ARRAY_BUFFER, MAX_INSTANCES * 2 * sizeof(float), null, gl3.GL_STATIC_DRAW); gl3.glGenVertexArrays(1, ref mVBState); gl3.glBindVertexArray(mVBState); gl3.glBindBuffer(gl3.GL_ARRAY_BUFFER, mVB[(int)VB.VB_INSTANCE]); //gl3.glVertexAttribPointer(POS_ATTRIB, 4, gl3.GL_FLOAT, gl3.GL_FALSE, sizeof(Vertex), (const GLvoid*)offsetof(Vertex, pos)); var offset_pos = (void*)(0); gl3.glVertexAttribPointer(POS_ATTRIB, 4, gl3.GL_FLOAT, false, sizeof(Vertex), offset_pos); var offset_rgba = (void*)(2 * sizeof(float)); //gl3.glVertexAttribPointer(COLOR_ATTRIB, 4, gl3.GL_UNSIGNED_BYTE, gl3.GL_TRUE, sizeof(Vertex), (const GLvoid*)offsetof(Vertex, rgba)); gl3.glVertexAttribPointer(COLOR_ATTRIB, 4, gl3.GL_UNSIGNED_BYTE, true, sizeof(Vertex), offset_rgba); gl3.glEnableVertexAttribArray(POS_ATTRIB); gl3.glEnableVertexAttribArray(COLOR_ATTRIB); gl3.glBindBuffer(gl3.GL_ARRAY_BUFFER, mVB[(int)VB.VB_SCALEROT]); gl3.glVertexAttribPointer(SCALEROT_ATTRIB, 4, gl3.GL_FLOAT, false, 4 * sizeof(float), default(void*)); gl3.glEnableVertexAttribArray(SCALEROT_ATTRIB); gl3.glVertexAttribDivisor(SCALEROT_ATTRIB, 1); gl3.glBindBuffer(gl3.GL_ARRAY_BUFFER, mVB[(int)VB.VB_OFFSET]); gl3.glVertexAttribPointer(OFFSET_ATTRIB, 2, gl3.GL_FLOAT, false, 2 * sizeof(float), null); gl3.glEnableVertexAttribArray(OFFSET_ATTRIB); gl3.glVertexAttribDivisor(OFFSET_ATTRIB, 1); //ALOGV("Using OpenGL ES 3.0 renderer"); return true; }
public static bool eglDestroyContext(EGLDisplay dpy, EGLContext ctx) { throw null; }
public static bool eglMakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx) { throw null; }
public static extern bool QueryContext(EGLDisplay dpy, EGLContext ctx, int attribute, out int value);
public static EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config, EGLContext share_context, int[] attrib_list) { throw null; }
private static extern IntPtr eglCreateContext(EGLDisplay dpy, EGLConfig config, EGLContext share_context, int[] attrib_list);
public static extern bool MakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx);
static extern IntPtr eglCreateContext(EGLDisplay dpy, EGLConfig config, EGLContext share_context, int[] attrib_list);
public static EGLContext CreateContext(EGLDisplay dpy, EGLConfig config, EGLContext share_context, int[] attrib_list) { IntPtr ptr = eglCreateContext(dpy, config, share_context, attrib_list); if (ptr == IntPtr.Zero) throw new GraphicsContextException(String.Format("Failed to create EGL context, error: {0}.", Egl.GetError())); return ptr; }
public void destroyContext(IEGL10 egl, EGLDisplay display, EGLContext context) { egl.EglDestroyContext(display, context); }
// called by AppThreadFunction public void ovrEgl_CreateContext(EGLContext shareEgl_Context) { // 152 ConsoleExtensions.trace("enter ovrEgl_CreateContext, eglGetDisplay"); if (this.Display != egl.EGL_NO_DISPLAY) { return; } this.Display = egl.eglGetDisplay(default(native_window.ANativeWindow)); egl.eglInitialize(this.Display, out this.MajorVersion, out this.MinorVersion); // Do NOT use eglChooseConfig, because the Android EGL code pushes in multisample // flags in eglChooseConfig if the user has selected the "force 4x MSAA" option in // settings, and that is completely wasted for our warp target. int numConfigs = 0; if (egl.eglGetConfigs(this.Display, configs, MAX_CONFIGS, out numConfigs) == false) { //ALOGE( " eglGetConfigs() failed: %s", EglErrorString( eglGetError() ) ); return; } ConsoleExtensions.tracei("ovrEgl_CreateContext numConfigs: ", numConfigs); // \VrCubeWorld.Egl.cs:95 ovrEgl_CreateContext numConfigs: 22 const int egl_EGL_OPENGL_ES3_BIT_KHR = 0x0040; this.Config = default(EGLConfig); #region numConfigs for (int i = 0; i < numConfigs; i++) { int value = 0; egl.eglGetConfigAttrib(this.Display, configs[i], egl.EGL_RENDERABLE_TYPE, out value); if ((value & egl_EGL_OPENGL_ES3_BIT_KHR) != egl_EGL_OPENGL_ES3_BIT_KHR) { continue; } // The pbuffer config also needs to be compatible with normal window rendering // so it can share textures with the window context. egl.eglGetConfigAttrib(this.Display, configs[i], egl.EGL_SURFACE_TYPE, out value); if ((value & (egl.EGL_WINDOW_BIT | egl.EGL_PBUFFER_BIT)) != (egl.EGL_WINDOW_BIT | egl.EGL_PBUFFER_BIT)) { continue; } int j = 0; for (; configAttribs[j] != egl.EGL_NONE; j += 2) { egl.eglGetConfigAttrib(this.Display, configs[i], configAttribs[j], out value); if (value != configAttribs[j + 1]) { break; } } if (configAttribs[j] == egl.EGL_NONE) { ConsoleExtensions.tracei("ovrEgl_CreateContext config found i: ", i); this.Config = configs[i]; break; } } #endregion if (this.Config == default(EGLConfig)) { ConsoleExtensions.tracei("ovrEgl_CreateContext eglChooseConfig failed"); //ALOGE( " eglChooseConfig() failed: %s", EglErrorString( eglGetError() ) ); return; } //ALOGV( " Context = eglCreateContext( Display, Config, EGL_NO_CONTEXT, contextAttribs )" ); //this.Context = egl.eglCreateContext(this.Display, this.Config, (shareEgl != NULL) ? shareEgl->Context : egl.EGL_NO_CONTEXT, contextAttribs); this.Context = egl.eglCreateContext(this.Display, this.Config, shareEgl_Context, contextAttribs); if (this.Context == egl.EGL_NO_CONTEXT) { //ALOGE( " eglCreateContext() failed: %s", EglErrorString( eglGetError() ) ); return; } //ALOGV( " TinySurface = eglCreatePbufferSurface( Display, Config, surfaceAttribs )" ); this.TinySurface = egl.eglCreatePbufferSurface(this.Display, this.Config, surfaceAttribs); if (this.TinySurface == egl.EGL_NO_SURFACE) { //ALOGE( " eglCreatePbufferSurface() failed: %s", EglErrorString( eglGetError() ) ); egl.eglDestroyContext(this.Display, this.Context); this.Context = egl.EGL_NO_CONTEXT; return; } //ALOGV( " eglMakeCurrent( Display, TinySurface, TinySurface, Context )" ); if (egl.eglMakeCurrent(this.Display, this.TinySurface, this.TinySurface, this.Context) == false) { //ALOGE( " eglMakeCurrent() failed: %s", EglErrorString( eglGetError() ) ); egl.eglDestroySurface(this.Display, this.TinySurface); egl.eglDestroyContext(this.Display, this.Context); this.Context = egl.EGL_NO_CONTEXT; return; } ConsoleExtensions.trace("exit ovrEgl_CreateContext"); }
public static extern bool QueryContext(EGLDisplay dpy, EGLContext ctx, int attribute, out int value);
public static extern bool MakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx);
public static extern bool DestroyContext(EGLDisplay dpy, EGLContext ctx);
// called by AppThreadFunction public void ovrEgl_DestroyContext() { // 259 if (this.Display != egl.EGL_NO_DISPLAY) { //ALOGE(" eglMakeCurrent( Display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT )"); if (egl.eglMakeCurrent(this.Display, egl.EGL_NO_SURFACE, egl.EGL_NO_SURFACE, egl.EGL_NO_CONTEXT) == false) { //ALOGE(" eglMakeCurrent() failed: %s", EglErrorString(eglGetError())); } } if (this.Context != egl.EGL_NO_CONTEXT) { //ALOGE(" eglDestroyContext( Display, Context )"); if (egl.eglDestroyContext(this.Display, this.Context) == false) { //ALOGE(" eglDestroyContext() failed: %s", EglErrorString(eglGetError())); } this.Context = egl.EGL_NO_CONTEXT; } if (this.TinySurface != egl.EGL_NO_SURFACE) { //ALOGE(" eglDestroySurface( Display, TinySurface )"); if (egl.eglDestroySurface(this.Display, this.TinySurface) == false) { //ALOGE(" eglDestroySurface() failed: %s", EglErrorString(eglGetError())); } this.TinySurface = egl.EGL_NO_SURFACE; } if (this.Display != egl.EGL_NO_DISPLAY) { //ALOGE(" eglTerminate( Display )"); if (egl.eglTerminate(this.Display) == false) { //ALOGE(" eglTerminate() failed: %s", EglErrorString(eglGetError())); } this.Display = egl.EGL_NO_DISPLAY; } }
public static extern bool DestroyContext(EGLDisplay dpy, EGLContext ctx);
// called by AppThreadFunction public void ovrEgl_CreateContext(EGLContext shareEgl_Context) { // 152 ConsoleExtensions.trace("enter ovrEgl_CreateContext, eglGetDisplay"); if (this.Display != egl.EGL_NO_DISPLAY) { return; } this.Display = egl.eglGetDisplay(default(native_window.ANativeWindow)); egl.eglInitialize(this.Display, out this.MajorVersion, out this.MinorVersion); // Do NOT use eglChooseConfig, because the Android EGL code pushes in multisample // flags in eglChooseConfig if the user has selected the "force 4x MSAA" option in // settings, and that is completely wasted for our warp target. int numConfigs = 0; if (egl.eglGetConfigs(this.Display, configs, MAX_CONFIGS, out numConfigs) == false) { //ALOGE( " eglGetConfigs() failed: %s", EglErrorString( eglGetError() ) ); return; } ConsoleExtensions.tracei("ovrEgl_CreateContext numConfigs: ", numConfigs); // \VrCubeWorld.Egl.cs:95 ovrEgl_CreateContext numConfigs: 22 const int egl_EGL_OPENGL_ES3_BIT_KHR = 0x0040; this.Config = default(EGLConfig); #region numConfigs for (int i = 0; i < numConfigs; i++) { int value = 0; egl.eglGetConfigAttrib(this.Display, configs[i], egl.EGL_RENDERABLE_TYPE, out value); if ((value & egl_EGL_OPENGL_ES3_BIT_KHR) != egl_EGL_OPENGL_ES3_BIT_KHR) { continue; } // The pbuffer config also needs to be compatible with normal window rendering // so it can share textures with the window context. egl.eglGetConfigAttrib(this.Display, configs[i], egl.EGL_SURFACE_TYPE, out value); if ((value & (egl.EGL_WINDOW_BIT | egl.EGL_PBUFFER_BIT)) != (egl.EGL_WINDOW_BIT | egl.EGL_PBUFFER_BIT)) { continue; } int j = 0; for (; configAttribs[j] != egl.EGL_NONE; j += 2) { egl.eglGetConfigAttrib(this.Display, configs[i], configAttribs[j], out value); if (value != configAttribs[j + 1]) { break; } } if (configAttribs[j] == egl.EGL_NONE) { ConsoleExtensions.tracei("ovrEgl_CreateContext config found i: ", i); this.Config = configs[i]; break; } } #endregion if (this.Config == default(EGLConfig)) { ConsoleExtensions.tracei("ovrEgl_CreateContext eglChooseConfig failed"); //ALOGE( " eglChooseConfig() failed: %s", EglErrorString( eglGetError() ) ); return; } //ALOGV( " Context = eglCreateContext( Display, Config, EGL_NO_CONTEXT, contextAttribs )" ); //this.Context = egl.eglCreateContext(this.Display, this.Config, (shareEgl != NULL) ? shareEgl->Context : egl.EGL_NO_CONTEXT, contextAttribs); this.Context = egl.eglCreateContext(this.Display, this.Config, shareEgl_Context, contextAttribs); if (this.Context == egl.EGL_NO_CONTEXT) { //ALOGE( " eglCreateContext() failed: %s", EglErrorString( eglGetError() ) ); return; } //ALOGV( " TinySurface = eglCreatePbufferSurface( Display, Config, surfaceAttribs )" ); this.TinySurface = egl.eglCreatePbufferSurface(this.Display, this.Config, surfaceAttribs); if (this.TinySurface == egl.EGL_NO_SURFACE) { //ALOGE( " eglCreatePbufferSurface() failed: %s", EglErrorString( eglGetError() ) ); egl.eglDestroyContext(this.Display, this.Context); this.Context = egl.EGL_NO_CONTEXT; return; } //ALOGV( " eglMakeCurrent( Display, TinySurface, TinySurface, Context )" ); if (egl.eglMakeCurrent(this.Display, this.TinySurface, this.TinySurface, this.Context) == false) { //ALOGE( " eglMakeCurrent() failed: %s", EglErrorString( eglGetError() ) ); egl.eglDestroySurface(this.Display, this.TinySurface); egl.eglDestroyContext(this.Display, this.Context); this.Context = egl.EGL_NO_CONTEXT; return; } ConsoleExtensions.trace("exit ovrEgl_CreateContext"); }
/** * Prepares EGL. We want a GLES 2.0 context and a surface that supports recording. */ private void EglSetup() { _EGLDisplay = EGL14.EglGetDisplay(EGL14.EglDefaultDisplay); if (_EGLDisplay == EGL14.EglNoDisplay) { throw new RuntimeException("unable to get EGL14 display"); } int[] version = new int[2]; if (!EGL14.EglInitialize(_EGLDisplay, version, 0, version, 1)) { _EGLDisplay = null; throw new RuntimeException("unable to initialize EGL14"); } // Configure EGL for pbuffer and OpenGL ES 2.0. We want enough RGB bits // to be able to tell if the frame is reasonable. int[] attribList = { EGL14.EglRedSize, 8, EGL14.EglGreenSize, 8, EGL14.EglBlueSize, 8, EGL14.EglRenderableType, EGL_OPENGL_ES2_BIT, EGL_RECORDABLE_ANDROID, 1, EGL14.EglNone }; var configs = new EGLConfig[1]; var numConfigs = new int[1]; if (!EGL14.EglChooseConfig(_EGLDisplay, attribList, 0, configs, 0, configs.Length, numConfigs, 0)) { throw new RuntimeException("unable to find RGB888+recordable ES2 EGL config"); } // Configure context for OpenGL ES 2.0. int[] attrib_list = { EGL14.EglContextClientVersion, 2, EGL14.EglNone }; _EGLContext = EGL14.EglCreateContext(_EGLDisplay, configs[0], EGL14.EglNoContext, attrib_list, 0); CheckEglError("eglCreateContext"); if (_EGLContext == null) { throw new RuntimeException("null context"); } // Create a window surface, and attach it to the Surface we received. int[] surfaceAttribs = { EGL14.EglNone }; _EGLSurface = EGL14.EglCreateWindowSurface(_EGLDisplay, configs[0], _surface, surfaceAttribs, 0); CheckEglError("eglCreateWindowSurface"); if (_EGLSurface == null) { throw new RuntimeException("surface was null"); } }