Ejemplo n.º 1
0
        /// <summary>
        /// Load a user defined heightmap
        /// </summary>
        /// <param name="image"></param>
        public void UpdateTexture(Bitmap image)
        {
            if (this.currentTexture != null)
            {
                this.currentTexture.Dispose();
            }

            var storage = new TexImage2D(TexImage2D.Target.Texture2D, 0, GL.GL_RGBA, image.Width, image.Height, 0, GL.GL_BGRA, GL.GL_UNSIGNED_BYTE, new ImageDataProvider(image));
            var texture = new Texture(TextureTarget.Texture2D, storage,
                                      //new TexParameteri(TexParameter.PropertyName.TextureWrapR, (int)GL.GL_REPEAT),
                                      new TexParameteri(TexParameter.PropertyName.TextureWrapS, (int)GL.GL_REPEAT),
                                      new TexParameteri(TexParameter.PropertyName.TextureWrapT, (int)GL.GL_REPEAT),
                                      new TexParameteri(TexParameter.PropertyName.TextureMinFilter, (int)GL.GL_LINEAR),
                                      new TexParameteri(TexParameter.PropertyName.TextureMagFilter, (int)GL.GL_LINEAR)
                                      );

            texture.TextureUnitIndex = 0;
            texture.Initialize();

            RenderUnit    unit    = this.RenderUnits[(int)RenderMode.Textured];
            ShaderProgram program = unit.Program;

            program.SetUniform("textureMap", texture);

            this.currentTexture = texture;
        }
Ejemplo n.º 2
0
        /*
         #?RADIANCE
         # Made with FreeImage 3.9.3
         # FORMAT=32-bit_rle_rgbe
         # GAMMA=1
         # EXPOSURE=0
         #
         # -Y 800 +X 1600
         */
        private Texture LoadHdrEnvironmentMap(string filename)
        {
            HdrFile hdrFile = HdrAssetImporter.ReadHdrFile(filename);

            Pixel[] pixels = hdrFile.Colors;
            byte[]  bytes  = new byte[pixels.Length * 4];
            for (int i = 0; i < pixels.Length; i++)
            {
                //bytes[i * 4 + 0] = pixels[i].r;
                //bytes[i * 4 + 1] = pixels[i].g;
                //bytes[i * 4 + 2] = pixels[i].b;
                //bytes[i * 4 + 3] = pixels[i].a;
            }
            var storage = new TexImage2D(TexImage2D.Target.Texture2D, GL.GL_RGB16F, hdrFile.Width, hdrFile.Height, GL.GL_RGBA, GL.GL_BYTE, new ArrayDataProvider <byte>(bytes));
            var texture = new Texture(storage,
                                      new TexParameteri(TexParameter.PropertyName.TextureWrapS, (int)GL.GL_CLAMP_TO_EDGE),
                                      new TexParameteri(TexParameter.PropertyName.TextureWrapT, (int)GL.GL_CLAMP_TO_EDGE),
                                      new TexParameteri(TexParameter.PropertyName.TextureMinFilter, (int)GL.GL_LINEAR),
                                      new TexParameteri(TexParameter.PropertyName.TextureMagFilter, (int)GL.GL_LINEAR));

            texture.Initialize();

            var file = new FileInfo(filename);

            texture.GetImage(hdrFile.Width, hdrFile.Height).Save(
                string.Format("{0}.GetImage.png", file.Name));

            return(texture);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Load a user defined heightmap
        /// </summary>
        /// <param name="image"></param>
        public void UpdateHeightmap(Bitmap image)
        {
            if (this.heightTexture != null)
            {
                this.heightTexture.Dispose();
            }

            var storage          = new TexImage2D(TexImage2D.Target.Texture2D, 0, GL.GL_RED, image.Width, image.Height, 0, GL.GL_BGRA, GL.GL_UNSIGNED_BYTE, new ImageDataProvider(image));
            var heightMapTexture = new Texture(TextureTarget.Texture2D, storage,
                                               //new TexParameteri(TexParameter.PropertyName.TextureWrapR, (int)GL.GL_CLAMP),
                                               new TexParameteri(TexParameter.PropertyName.TextureWrapS, (int)GL.GL_CLAMP),
                                               new TexParameteri(TexParameter.PropertyName.TextureWrapT, (int)GL.GL_CLAMP),
                                               new TexParameteri(TexParameter.PropertyName.TextureMinFilter, (int)GL.GL_NEAREST),
                                               new TexParameteri(TexParameter.PropertyName.TextureMagFilter, (int)GL.GL_NEAREST)
                                               );

            heightMapTexture.TextureUnitIndex = 0;
            heightMapTexture.Initialize();

            RenderUnit    unit    = this.RenderUnits[0];
            ShaderProgram program = unit.Program;

            program.SetUniform("heightMapTexture", heightMapTexture);

            this.heightTexture = heightMapTexture;
        }
Ejemplo n.º 4
0
        protected override void DoInitialize()
        {
            base.DoInitialize();
            {
                var bitmap  = new Bitmap(@"heightmap.png");
                var storage = new TexImage2D(TexImage2D.Target.Texture2D, 0, GL.GL_RGBA, bitmap.Width, bitmap.Height, 0, GL.GL_BGRA, GL.GL_UNSIGNED_BYTE, new ImageDataProvider(bitmap));
                var texture = new Texture(TextureTarget.Texture2D, storage);
                texture.BuiltInSampler.Add(new TexParameteri(TexParameter.PropertyName.TextureWrapS, (int)GL.GL_REPEAT));
                texture.BuiltInSampler.Add(new TexParameteri(TexParameter.PropertyName.TextureWrapT, (int)GL.GL_REPEAT));
                texture.BuiltInSampler.Add(new TexParameteri(TexParameter.PropertyName.TextureWrapR, (int)GL.GL_REPEAT));
                texture.BuiltInSampler.Add(new TexParameteri(TexParameter.PropertyName.TextureMinFilter, (int)GL.GL_LINEAR));
                texture.BuiltInSampler.Add(new TexParameteri(TexParameter.PropertyName.TextureMagFilter, (int)GL.GL_LINEAR));

                texture.Initialize();
                texture.TextureUnitIndex = 0;
                bitmap.Dispose();
                this.displacementMap = texture;
            }
            {
                var bitmap  = new Bitmap(@"diffuse.png");
                var storage = new TexImage2D(TexImage2D.Target.Texture2D, 0, GL.GL_RGBA, bitmap.Width, bitmap.Height, 0, GL.GL_BGRA, GL.GL_UNSIGNED_BYTE, new ImageDataProvider(bitmap));
                var texture = new Texture(TextureTarget.Texture2D, storage);
                texture.BuiltInSampler.Add(new TexParameteri(TexParameter.PropertyName.TextureWrapS, (int)GL.GL_REPEAT));
                texture.BuiltInSampler.Add(new TexParameteri(TexParameter.PropertyName.TextureWrapT, (int)GL.GL_REPEAT));
                texture.BuiltInSampler.Add(new TexParameteri(TexParameter.PropertyName.TextureWrapR, (int)GL.GL_REPEAT));
                texture.BuiltInSampler.Add(new TexParameteri(TexParameter.PropertyName.TextureMinFilter, (int)GL.GL_LINEAR));
                texture.BuiltInSampler.Add(new TexParameteri(TexParameter.PropertyName.TextureMagFilter, (int)GL.GL_LINEAR));

                texture.Initialize();
                texture.TextureUnitIndex = 1;
                bitmap.Dispose();
                this.colorMap = texture;
            }
            {
                var light = new DirectionalLight();
                light.Color            = new vec3(1, 1, 1);
                light.AmbientIntensity = 1.0f;
                light.DiffuseIntensity = 0.01f;
                light.direction        = new vec3(1, 1, 0);

                this.directionalLight = light;
            }
            {
                RenderMethod      method      = this.RenderUnit.Methods[0];
                VertexArrayObject vao         = method.VertexArrayObject;
                IndexBuffer       indexBuffer = vao.IndexBuffer;
                indexBuffer.Mode = DrawMode.Patches;

                var polygonModeState = new PolygonModeState(CSharpGL.PolygonMode.Fill);
                method.StateList.Add(polygonModeState);
                this.PolygonMode = polygonModeState;
            }
        }
Ejemplo n.º 5
0
        private Texture LoadBRDFTexture()
        {
            var storage        = new TexImage2D(TexImage2D.Target.Texture2D, GL.GL_RG16F, 512, 512, GL.GL_RG, GL.GL_FLOAT);
            var brdfLUTTexture = new Texture(storage,
                                             new TexParameteri(TexParameter.PropertyName.TextureWrapS, (int)GL.GL_CLAMP_TO_EDGE),
                                             new TexParameteri(TexParameter.PropertyName.TextureWrapT, (int)GL.GL_CLAMP_TO_EDGE),
                                             new TexParameteri(TexParameter.PropertyName.TextureMinFilter, (int)GL.GL_LINEAR),
                                             new TexParameteri(TexParameter.PropertyName.TextureMagFilter, (int)GL.GL_LINEAR));

            brdfLUTTexture.Initialize();
            return(brdfLUTTexture);
        }
Ejemplo n.º 6
0
        private Texture InitFace2DTexture(int width, int height)
        {
            var storage = new TexImage2D(TexImage2D.Target.Texture2D, GL.GL_RGBA16F, width, height, GL.GL_RGBA, GL.GL_FLOAT);
            var texture = new Texture(storage,
                                      new TexParameteri(TexParameter.PropertyName.TextureWrapR, (int)GL.GL_REPEAT),
                                      new TexParameteri(TexParameter.PropertyName.TextureWrapS, (int)GL.GL_REPEAT),
                                      new TexParameteri(TexParameter.PropertyName.TextureWrapT, (int)GL.GL_REPEAT),
                                      new TexParameteri(TexParameter.PropertyName.TextureMinFilter, (int)GL.GL_NEAREST),
                                      new TexParameteri(TexParameter.PropertyName.TextureMagFilter, (int)GL.GL_NEAREST));

            texture.Initialize();

            return(texture);
        }
Ejemplo n.º 7
0
        private unsafe Texture LoadHDRTexture(string filename)
        {
            int    width, height, nrComponents;
            float *data         = stb_Image.stbi_loadf(filename, &width, &height, &nrComponents, 0);
            var    dataProvider = new PointerDataProvider(new PointerData(new IntPtr(data)));
            var    storage      = new TexImage2D(TexImage2D.Target.Texture2D, GL.GL_RGB16F, width, height, GL.GL_RGB, GL.GL_FLOAT, dataProvider);
            var    texture      = new Texture(storage,
                                              new TexParameteri(TexParameter.PropertyName.TextureWrapS, (int)GL.GL_CLAMP_TO_EDGE),
                                              new TexParameteri(TexParameter.PropertyName.TextureWrapT, (int)GL.GL_CLAMP_TO_EDGE),
                                              new TexParameteri(TexParameter.PropertyName.TextureMinFilter, (int)GL.GL_LINEAR),
                                              new TexParameteri(TexParameter.PropertyName.TextureMagFilter, (int)GL.GL_LINEAR));

            texture.Initialize();

            texture.GetImage(width, height).Save(string.Format("HDR.Texture.png"));

            return(texture);
        }
Ejemplo n.º 8
0
        private unsafe Texture LoadHDRTexture(string filename)
        {
            // pbr: load the HDR environment map
            stb_Image.stbi_set_flip_vertically_on_load(true);
            int    width, height, nrComponents;
            float *data         = stb_Image.stbi_loadf(filename, &width, &height, &nrComponents, 0);
            var    dataProvider = new PointerDataProvider(new PointerData(new IntPtr(data)));
            // note how we specify the texture's data value to be float
            var storage = new TexImage2D(TexImage2D.Target.Texture2D, GL.GL_RGB16F, width, height, GL.GL_RGB, GL.GL_FLOAT, dataProvider);
            var texture = new Texture(storage,
                                      new TexParameteri(TexParameter.PropertyName.TextureWrapS, (int)GL.GL_CLAMP_TO_EDGE),
                                      new TexParameteri(TexParameter.PropertyName.TextureWrapT, (int)GL.GL_CLAMP_TO_EDGE),
                                      new TexParameteri(TexParameter.PropertyName.TextureMinFilter, (int)GL.GL_LINEAR),
                                      new TexParameteri(TexParameter.PropertyName.TextureMagFilter, (int)GL.GL_LINEAR));

            texture.Initialize();

            return(texture);
        }
Ejemplo n.º 9
0
        private void LoadGLEntryPoints()
        {
            /* Basic entry points. If you don't have these, you're screwed. */
            try
            {
                INTERNAL_glGetString = (GetString) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glGetString"),
                    typeof(GetString)
                );
                glGetIntegerv = (GetIntegerv) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glGetIntegerv"),
                    typeof(GetIntegerv)
                );
                glEnable = (Enable) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glEnable"),
                    typeof(Enable)
                );
                glDisable = (Disable) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glDisable"),
                    typeof(Disable)
                );
                glViewport = (G_Viewport) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glViewport"),
                    typeof(G_Viewport)
                );
                glDepthRange = (DepthRange) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glDepthRange"),
                    typeof(DepthRange)
                );
                glScissor = (Scissor) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glScissor"),
                    typeof(Scissor)
                );
                glBlendColor = (BlendColor) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glBlendColor"),
                    typeof(BlendColor)
                );
                glBlendFuncSeparate = (BlendFuncSeparate) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glBlendFuncSeparate"),
                    typeof(BlendFuncSeparate)
                );
                glBlendEquationSeparate = (BlendEquationSeparate) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glBlendEquationSeparate"),
                    typeof(BlendEquationSeparate)
                );
                glColorMask = (ColorMask) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glColorMask"),
                    typeof(ColorMask)
                );
                glDepthMask = (DepthMask) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glDepthMask"),
                    typeof(DepthMask)
                );
                glDepthFunc = (DepthFunc) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glDepthFunc"),
                    typeof(DepthFunc)
                );
                glStencilMask = (StencilMask) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glStencilMask"),
                    typeof(StencilMask)
                );
                glStencilFuncSeparate = (StencilFuncSeparate) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glStencilFuncSeparate"),
                    typeof(StencilFuncSeparate)
                );
                glStencilOpSeparate = (StencilOpSeparate) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glStencilOpSeparate"),
                    typeof(StencilOpSeparate)
                );
                glStencilFunc = (StencilFunc) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glStencilFunc"),
                    typeof(StencilFunc)
                );
                glStencilOp = (StencilOp) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glStencilOp"),
                    typeof(StencilOp)
                );
                glCullFace = (CullFace) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glCullFace"),
                    typeof(CullFace)
                );
                glFrontFace = (FrontFace) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glFrontFace"),
                    typeof(FrontFace)
                );
                glPolygonMode = (PolygonMode) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glPolygonMode"),
                    typeof(PolygonMode)
                );
                glPolygonOffset = (PolygonOffset) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glPolygonOffset"),
                    typeof(PolygonOffset)
                );
                glGenTextures = (GenTextures) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glGenTextures"),
                    typeof(GenTextures)
                );
                glDeleteTextures = (DeleteTextures) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glDeleteTextures"),
                    typeof(DeleteTextures)
                );
                glBindTexture = (G_BindTexture) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glBindTexture"),
                    typeof(G_BindTexture)
                );
                glTexImage2D = (TexImage2D) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glTexImage2D"),
                    typeof(TexImage2D)
                );
                glTexSubImage2D = (TexSubImage2D) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glTexSubImage2D"),
                    typeof(TexSubImage2D)
                );
                glCompressedTexImage2D = (CompressedTexImage2D) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glCompressedTexImage2D"),
                    typeof(CompressedTexImage2D)
                );
                glCompressedTexSubImage2D = (CompressedTexSubImage2D) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glCompressedTexSubImage2D"),
                    typeof(CompressedTexSubImage2D)
                );
                glTexImage3D = (TexImage3D) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glTexImage3D"),
                    typeof(TexImage3D)
                );
                glTexSubImage3D = (TexSubImage3D) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glTexSubImage3D"),
                    typeof(TexSubImage3D)
                );
                glGetTexImage = (GetTexImage) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glGetTexImage"),
                    typeof(GetTexImage)
                );
                glTexParameteri = (TexParameteri) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glTexParameteri"),
                    typeof(TexParameteri)
                );
                glTexParameterf = (TexParameterf) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glTexParameterf"),
                    typeof(TexParameterf)
                );
                glActiveTexture = (ActiveTexture) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glActiveTexture"),
                    typeof(ActiveTexture)
                );
                glPixelStorei = (PixelStorei) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glPixelStorei"),
                    typeof(PixelStorei)
                );
                glGenBuffers = (GenBuffers) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glGenBuffers"),
                    typeof(GenBuffers)
                );
                glDeleteBuffers = (DeleteBuffers) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glDeleteBuffers"),
                    typeof(DeleteBuffers)
                );
                glBindBuffer = (BindBuffer) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glBindBuffer"),
                    typeof(BindBuffer)
                );
                glBufferData = (BufferData) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glBufferData"),
                    typeof(BufferData)
                );
                glBufferSubData = (BufferSubData) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glBufferSubData"),
                    typeof(BufferSubData)
                );
                glMapBuffer = (MapBuffer) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glMapBuffer"),
                    typeof(MapBuffer)
                );
                glUnmapBuffer = (UnmapBuffer) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glUnmapBuffer"),
                    typeof(UnmapBuffer)
                );
                glClearColor = (ClearColor) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glClearColor"),
                    typeof(ClearColor)
                );
                glClearDepth = (ClearDepth) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glClearDepth"),
                    typeof(ClearDepth)
                );
                glClearStencil = (ClearStencil) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glClearStencil"),
                    typeof(ClearStencil)
                );
                glClear = (G_Clear) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glClear"),
                    typeof(G_Clear)
                );
                glDrawBuffers = (DrawBuffers) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glDrawBuffers"),
                    typeof(DrawBuffers)
                );
                glReadPixels = (ReadPixels) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glReadPixels"),
                    typeof(ReadPixels)
                );
                glVertexAttribPointer = (VertexAttribPointer) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glVertexAttribPointer"),
                    typeof(VertexAttribPointer)
                );
                glEnableVertexAttribArray = (EnableVertexAttribArray) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glEnableVertexAttribArray"),
                    typeof(EnableVertexAttribArray)
                );
                glDisableVertexAttribArray = (DisableVertexAttribArray) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glDisableVertexAttribArray"),
                    typeof(DisableVertexAttribArray)
                );
                glDrawRangeElements = (DrawRangeElements) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glDrawRangeElements"),
                    typeof(DrawRangeElements)
                );
                glDrawArrays = (DrawArrays) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glDrawArrays"),
                    typeof(DrawArrays)
                );
                glGenQueries = (GenQueries) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glGenQueries"),
                    typeof(GenQueries)
                );
                glDeleteQueries = (DeleteQueries) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glDeleteQueries"),
                    typeof(DeleteQueries)
                );
                glBeginQuery = (BeginQuery) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glBeginQuery"),
                    typeof(BeginQuery)
                );
                glEndQuery = (EndQuery) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glEndQuery"),
                    typeof(EndQuery)
                );
                glGetQueryObjectiv = (GetQueryObjectiv) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glGetQueryObjectiv"),
                    typeof(GetQueryObjectiv)
                );
            }
            catch
            {
                throw new NoSuitableGraphicsDeviceException("OpenGL 2.1 support is required!");
            }

            /* ARB_framebuffer_object. We're flexible, but not _that_ flexible. */
            try
            {
                glGenFramebuffers = (GenFramebuffers) Marshal.GetDelegateForFunctionPointer(
                    TryGetFramebufferEP("glGenFramebuffers"),
                    typeof(GenFramebuffers)
                );
                glDeleteFramebuffers = (DeleteFramebuffers) Marshal.GetDelegateForFunctionPointer(
                    TryGetFramebufferEP("glDeleteFramebuffers"),
                    typeof(DeleteFramebuffers)
                );
                glBindFramebuffer = (G_BindFramebuffer) Marshal.GetDelegateForFunctionPointer(
                    TryGetFramebufferEP("glBindFramebuffer"),
                    typeof(G_BindFramebuffer)
                );
                glFramebufferTexture2D = (FramebufferTexture2D) Marshal.GetDelegateForFunctionPointer(
                    TryGetFramebufferEP("glFramebufferTexture2D"),
                    typeof(FramebufferTexture2D)
                );
                glFramebufferRenderbuffer = (FramebufferRenderbuffer) Marshal.GetDelegateForFunctionPointer(
                    TryGetFramebufferEP("glFramebufferRenderbuffer"),
                    typeof(FramebufferRenderbuffer)
                );
                glGenerateMipmap = (GenerateMipmap) Marshal.GetDelegateForFunctionPointer(
                    TryGetFramebufferEP("glGenerateMipmap"),
                    typeof(GenerateMipmap)
                );
            #if !DISABLE_FAUXBACKBUFFER
                glBlitFramebuffer = (BlitFramebuffer) Marshal.GetDelegateForFunctionPointer(
                    TryGetFramebufferEP("glBlitFramebuffer"),
                    typeof(BlitFramebuffer)
                );
            #endif
                glGenRenderbuffers = (GenRenderbuffers) Marshal.GetDelegateForFunctionPointer(
                    TryGetFramebufferEP("glGenRenderbuffers"),
                    typeof(GenRenderbuffers)
                );
                glDeleteRenderbuffers = (DeleteRenderbuffers) Marshal.GetDelegateForFunctionPointer(
                    TryGetFramebufferEP("glDeleteRenderbuffers"),
                    typeof(DeleteRenderbuffers)
                );
                glBindRenderbuffer = (BindRenderbuffer) Marshal.GetDelegateForFunctionPointer(
                    TryGetFramebufferEP("glBindRenderbuffer"),
                    typeof(BindRenderbuffer)
                );
                glRenderbufferStorage = (RenderbufferStorage) Marshal.GetDelegateForFunctionPointer(
                    TryGetFramebufferEP("glRenderbufferStorage"),
                    typeof(RenderbufferStorage)
                );
            }
            catch
            {
                throw new NoSuitableGraphicsDeviceException("OpenGL framebuffer support is required!");
            }

            /* ARB_instanced_arrays/ARB_draw_instanced are almost optional. */
            SupportsHardwareInstancing = true;
            try
            {
                glVertexAttribDivisor = (VertexAttribDivisor) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glVertexAttribDivisor"),
                    typeof(VertexAttribDivisor)
                );
                glDrawElementsInstanced = (DrawElementsInstanced) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glDrawElementsInstanced"),
                    typeof(DrawElementsInstanced)
                );
            }
            catch
            {
                SupportsHardwareInstancing = false;
            }

            /* EXT_draw_buffers2 is probably used by nobody. */
            try
            {
                glColorMaskIndexedEXT = (ColorMaskIndexedEXT) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glColorMaskIndexedEXT"),
                    typeof(ColorMaskIndexedEXT)
                );
            }
            catch
            {
                // FIXME: SupportsIndependentWriteMasks? -flibit
            }

            /* EXT_framebuffer_multisample/ARB_texture_multisample is glitter -flibit */
            supportsMultisampling = true;
            try
            {
                glRenderbufferStorageMultisample = (RenderbufferStorageMultisample) Marshal.GetDelegateForFunctionPointer(
                    TryGetFramebufferEP("glRenderbufferStorageMultisample"),
                    typeof(RenderbufferStorageMultisample)
                );
                glSampleMaski = (SampleMaski) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glSampleMaski"),
                    typeof(SampleMaski)
                );
            }
            catch
            {
                supportsMultisampling = false;
            }

            if (useCoreProfile)
            {
                try
                {
                    INTERNAL_glGetStringi = (GetStringi) Marshal.GetDelegateForFunctionPointer(
                        SDL.SDL_GL_GetProcAddress("glGetStringi"),
                        typeof(GetStringi)
                    );
                    glGenVertexArrays = (GenVertexArrays) Marshal.GetDelegateForFunctionPointer(
                        SDL.SDL_GL_GetProcAddress("glGenVertexArrays"),
                        typeof(GenVertexArrays)
                    );
                    glDeleteVertexArrays = (DeleteVertexArrays) Marshal.GetDelegateForFunctionPointer(
                        SDL.SDL_GL_GetProcAddress("glDeleteVertexArrays"),
                        typeof(DeleteVertexArrays)
                    );
                    glBindVertexArray = (BindVertexArray) Marshal.GetDelegateForFunctionPointer(
                        SDL.SDL_GL_GetProcAddress("glBindVertexArray"),
                        typeof(BindVertexArray)
                    );
                }
                catch
                {
                    throw new NoSuitableGraphicsDeviceException("OpenGL 3.2 support is required!");
                }
            }

            #if DEBUG
            /* ARB_debug_output, for debug contexts */
            IntPtr messageCallback = SDL.SDL_GL_GetProcAddress("glDebugMessageCallbackARB");
            IntPtr messageControl = SDL.SDL_GL_GetProcAddress("glDebugMessageControlARB");
            if (messageCallback == IntPtr.Zero || messageControl == IntPtr.Zero)
            {
                System.Console.WriteLine("ARB_debug_output not supported!");
            }
            else
            {
                glDebugMessageCallbackARB = (DebugMessageCallback) Marshal.GetDelegateForFunctionPointer(
                    messageCallback,
                    typeof(DebugMessageCallback)
                );
                glDebugMessageControlARB = (DebugMessageControl) Marshal.GetDelegateForFunctionPointer(
                    messageControl,
                    typeof(DebugMessageControl)
                );
                glDebugMessageControlARB(
                    GLenum.GL_DONT_CARE,
                    GLenum.GL_DONT_CARE,
                    GLenum.GL_DONT_CARE,
                    0,
                    IntPtr.Zero,
                    true
                );
                glDebugMessageControlARB(
                    GLenum.GL_DONT_CARE,
                    GLenum.GL_DEBUG_TYPE_OTHER_ARB,
                    GLenum.GL_DEBUG_SEVERITY_LOW_ARB,
                    0,
                    IntPtr.Zero,
                    false
                );
                glDebugMessageControlARB(
                    GLenum.GL_DONT_CARE,
                    GLenum.GL_DEBUG_TYPE_OTHER_ARB,
                    GLenum.GL_DEBUG_SEVERITY_NOTIFICATION_ARB,
                    0,
                    IntPtr.Zero,
                    false
                );
                glDebugMessageCallbackARB(DebugCall, IntPtr.Zero);
            }

            /* GREMEDY_string_marker, for apitrace */
            IntPtr stringMarkerCallback = SDL.SDL_GL_GetProcAddress("glStringMarkerGREMEDY");
            if (stringMarkerCallback == IntPtr.Zero)
            {
                System.Console.WriteLine("GREMEDY_string_marker not supported!");
            }
            else
            {
                glStringMarkerGREMEDY = (StringMarkerGREMEDY) Marshal.GetDelegateForFunctionPointer(
                    stringMarkerCallback,
                    typeof(StringMarkerGREMEDY)
                );
            }
            #endif
        }
Ejemplo n.º 10
0
		/* END STRING MARKER FUNCTIONS */
#endif

		private void LoadGLEntryPoints()
		{
			string baseErrorString;
			if (useES2)
			{
				baseErrorString = "OpenGL ES 2.0";
			}
			else
			{
				baseErrorString = "OpenGL 2.1";
			}
			baseErrorString += " support is required!";

			/* Basic entry points. If you don't have these, you're screwed. */
			try
			{
				INTERNAL_glGetString = (GetString) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glGetString"),
					typeof(GetString)
				);
				glGetIntegerv = (GetIntegerv) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glGetIntegerv"),
					typeof(GetIntegerv)
				);
				glEnable = (Enable) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glEnable"),
					typeof(Enable)
				);
				glDisable = (Disable) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glDisable"),
					typeof(Disable)
				);
				glViewport = (G_Viewport) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glViewport"),
					typeof(G_Viewport)
				);
				glScissor = (Scissor) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glScissor"),
					typeof(Scissor)
				);
				glBlendColor = (BlendColor) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glBlendColor"),
					typeof(BlendColor)
				);
				glBlendFuncSeparate = (BlendFuncSeparate) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glBlendFuncSeparate"),
					typeof(BlendFuncSeparate)
				);
				glBlendEquationSeparate = (BlendEquationSeparate) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glBlendEquationSeparate"),
					typeof(BlendEquationSeparate)
				);
				glColorMask = (ColorMask) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glColorMask"),
					typeof(ColorMask)
				);
				glDepthMask = (DepthMask) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glDepthMask"),
					typeof(DepthMask)
				);
				glDepthFunc = (DepthFunc) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glDepthFunc"),
					typeof(DepthFunc)
				);
				glStencilMask = (StencilMask) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glStencilMask"),
					typeof(StencilMask)
				);
				glStencilFuncSeparate = (StencilFuncSeparate) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glStencilFuncSeparate"),
					typeof(StencilFuncSeparate)
				);
				glStencilOpSeparate = (StencilOpSeparate) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glStencilOpSeparate"),
					typeof(StencilOpSeparate)
				);
				glStencilFunc = (StencilFunc) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glStencilFunc"),
					typeof(StencilFunc)
				);
				glStencilOp = (StencilOp) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glStencilOp"),
					typeof(StencilOp)
				);
				glFrontFace = (FrontFace) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glFrontFace"),
					typeof(FrontFace)
				);
				glPolygonOffset = (PolygonOffset) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glPolygonOffset"),
					typeof(PolygonOffset)
				);
				glGenTextures = (GenTextures) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glGenTextures"),
					typeof(GenTextures)
				);
				glDeleteTextures = (DeleteTextures) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glDeleteTextures"),
					typeof(DeleteTextures)
				);
				glBindTexture = (G_BindTexture) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glBindTexture"),
					typeof(G_BindTexture)
				);
				glTexImage2D = (TexImage2D) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glTexImage2D"),
					typeof(TexImage2D)
				);
				glTexSubImage2D = (TexSubImage2D) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glTexSubImage2D"),
					typeof(TexSubImage2D)
				);
				glCompressedTexImage2D = (CompressedTexImage2D) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glCompressedTexImage2D"),
					typeof(CompressedTexImage2D)
				);
				glCompressedTexSubImage2D = (CompressedTexSubImage2D) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glCompressedTexSubImage2D"),
					typeof(CompressedTexSubImage2D)
				);
				glTexParameteri = (TexParameteri) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glTexParameteri"),
					typeof(TexParameteri)
				);
				glTexParameterf = (TexParameterf) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glTexParameterf"),
					typeof(TexParameterf)
				);
				glActiveTexture = (ActiveTexture) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glActiveTexture"),
					typeof(ActiveTexture)
				);
				glPixelStorei = (PixelStorei) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glPixelStorei"),
					typeof(PixelStorei)
				);
				glGenBuffers = (GenBuffers) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glGenBuffers"),
					typeof(GenBuffers)
				);
				glDeleteBuffers = (DeleteBuffers) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glDeleteBuffers"),
					typeof(DeleteBuffers)
				);
				glBindBuffer = (BindBuffer) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glBindBuffer"),
					typeof(BindBuffer)
				);
				glBufferData = (BufferData) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glBufferData"),
					typeof(BufferData)
				);
				glBufferSubData = (BufferSubData) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glBufferSubData"),
					typeof(BufferSubData)
				);
				glClearColor = (ClearColor) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glClearColor"),
					typeof(ClearColor)
				);
				glClearStencil = (ClearStencil) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glClearStencil"),
					typeof(ClearStencil)
				);
				glClear = (G_Clear) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glClear"),
					typeof(G_Clear)
				);
				glDrawBuffers = (DrawBuffers) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glDrawBuffers"),
					typeof(DrawBuffers)
				);
				glReadPixels = (ReadPixels) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glReadPixels"),
					typeof(ReadPixels)
				);
				glVertexAttribPointer = (VertexAttribPointer) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glVertexAttribPointer"),
					typeof(VertexAttribPointer)
				);
				glEnableVertexAttribArray = (EnableVertexAttribArray) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glEnableVertexAttribArray"),
					typeof(EnableVertexAttribArray)
				);
				glDisableVertexAttribArray = (DisableVertexAttribArray) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glDisableVertexAttribArray"),
					typeof(DisableVertexAttribArray)
				);
				glDrawArrays = (DrawArrays) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glDrawArrays"),
					typeof(DrawArrays)
				);
			}
			catch
			{
				throw new NoSuitableGraphicsDeviceException(baseErrorString);
			}

			/* ARB_draw_elements_base_vertex is ideal! */
			IntPtr ep = SDL.SDL_GL_GetProcAddress("glDrawRangeElementsBaseVertex");
			supportsBaseVertex = ep != IntPtr.Zero;
			if (supportsBaseVertex)
			{
				glDrawRangeElementsBaseVertex = (DrawRangeElementsBaseVertex) Marshal.GetDelegateForFunctionPointer(
					ep,
					typeof(DrawRangeElementsBaseVertex)
				);
				glDrawRangeElements = (DrawRangeElements) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glDrawRangeElements"),
					typeof(DrawRangeElements)
				);
			}
			else
			{
				/* DrawRangeElements is better, but some ES2 targets don't have it. */
				ep = SDL.SDL_GL_GetProcAddress("glDrawRangeElements");
				if (ep != IntPtr.Zero)
				{
					glDrawRangeElements = (DrawRangeElements) Marshal.GetDelegateForFunctionPointer(
						ep,
						typeof(DrawRangeElements)
					);
					glDrawRangeElementsBaseVertex = DrawRangeElementsNoBase;
				}
				else
				{
					ep = SDL.SDL_GL_GetProcAddress("glDrawElements");
					if (ep == IntPtr.Zero)
					{
						throw new NoSuitableGraphicsDeviceException(baseErrorString);
					}
					glDrawElements = (DrawElements) Marshal.GetDelegateForFunctionPointer(
						ep,
						typeof(DrawElements)
					);
					glDrawRangeElements = DrawRangeElementsUnchecked;
					glDrawRangeElementsBaseVertex = DrawRangeElementsNoBaseUnchecked;
				}
			}

			/* These functions are NOT supported in ES.
			 * NVIDIA or desktop ES might, but real scenarios where you need ES
			 * will certainly not have these.
			 * -flibit
			 */
			if (useES2)
			{
				ep = SDL.SDL_GL_GetProcAddress("glPolygonMode");
				if (ep != IntPtr.Zero)
				{
					glPolygonMode = (PolygonMode) Marshal.GetDelegateForFunctionPointer(
						ep,
						typeof(PolygonMode)
					);
				}
				else
				{
					glPolygonMode = PolygonModeESError;
				}
				ep = SDL.SDL_GL_GetProcAddress("glGetTexImage");
				if (ep != IntPtr.Zero)
				{
					glGetTexImage = (GetTexImage) Marshal.GetDelegateForFunctionPointer(
						ep,
						typeof(GetTexImage)
					);
				}
				else
				{
					glGetTexImage = GetTexImageESError;
				}
				ep = SDL.SDL_GL_GetProcAddress("glGetBufferSubData");
				if (ep != IntPtr.Zero)
				{
					glGetBufferSubData = (GetBufferSubData) Marshal.GetDelegateForFunctionPointer(
						ep,
						typeof(GetBufferSubData)
					);
				}
				else
				{
					glGetBufferSubData = GetBufferSubDataESError;
				}
			}
			else
			{
				try
				{
					glPolygonMode = (PolygonMode) Marshal.GetDelegateForFunctionPointer(
						SDL.SDL_GL_GetProcAddress("glPolygonMode"),
						typeof(PolygonMode)
					);
					glGetTexImage = (GetTexImage) Marshal.GetDelegateForFunctionPointer(
						SDL.SDL_GL_GetProcAddress("glGetTexImage"),
						typeof(GetTexImage)
					);
					glGetBufferSubData = (GetBufferSubData) Marshal.GetDelegateForFunctionPointer(
						SDL.SDL_GL_GetProcAddress("glGetBufferSubData"),
						typeof(GetBufferSubData)
					);
				}
				catch
				{
					throw new NoSuitableGraphicsDeviceException(baseErrorString);
				}
			}

			/* We need _some_ form of depth range, ES... */
			IntPtr drPtr = SDL.SDL_GL_GetProcAddress("glDepthRange");
			if (drPtr != IntPtr.Zero)
			{
				glDepthRange = (DepthRange) Marshal.GetDelegateForFunctionPointer(
					drPtr,
					typeof(DepthRange)
				);
			}
			else
			{
				drPtr = SDL.SDL_GL_GetProcAddress("glDepthRangef");
				if (drPtr == IntPtr.Zero)
				{
					throw new NoSuitableGraphicsDeviceException(baseErrorString);
				}
				glDepthRangef = (DepthRangef) Marshal.GetDelegateForFunctionPointer(
					drPtr,
					typeof(DepthRangef)
				);
				glDepthRange = DepthRangeFloat;
			}
			drPtr = SDL.SDL_GL_GetProcAddress("glClearDepth");
			if (drPtr != IntPtr.Zero)
			{
				glClearDepth = (ClearDepth) Marshal.GetDelegateForFunctionPointer(
					drPtr,
					typeof(ClearDepth)
				);
			}
			else
			{
				drPtr = SDL.SDL_GL_GetProcAddress("glClearDepthf");
				if (drPtr == IntPtr.Zero)
				{
					throw new NoSuitableGraphicsDeviceException(baseErrorString);
				}
				glClearDepthf = (ClearDepthf) Marshal.GetDelegateForFunctionPointer(
					drPtr,
					typeof(ClearDepthf)
				);
				glClearDepth = ClearDepthFloat;
			}

			/* Silently fail if using GLES. You didn't need these, right...? >_> */
			try
			{
				glTexImage3D = (TexImage3D) Marshal.GetDelegateForFunctionPointer(
					TryGetEPEXT("glTexImage3D", "OES"),
					typeof(TexImage3D)
				);
				glTexSubImage3D = (TexSubImage3D) Marshal.GetDelegateForFunctionPointer(
					TryGetEPEXT("glTexSubImage3D", "OES"),
					typeof(TexSubImage3D)
				);
				glGenQueries = (GenQueries) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glGenQueries"),
					typeof(GenQueries)
				);
				glDeleteQueries = (DeleteQueries) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glDeleteQueries"),
					typeof(DeleteQueries)
				);
				glBeginQuery = (BeginQuery) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glBeginQuery"),
					typeof(BeginQuery)
				);
				glEndQuery = (EndQuery) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glEndQuery"),
					typeof(EndQuery)
				);
				glGetQueryObjectuiv = (GetQueryObjectuiv) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glGetQueryObjectuiv"),
					typeof(GetQueryObjectuiv)
				);
			}
			catch
			{
				if (useES2)
				{
					FNAPlatform.Log("Some non-ES functions failed to load. Beware...");
				}
				else
				{
					throw new NoSuitableGraphicsDeviceException(baseErrorString);
				}
			}

			/* ARB_framebuffer_object. We're flexible, but not _that_ flexible. */
			try
			{
				glGenFramebuffers = (GenFramebuffers) Marshal.GetDelegateForFunctionPointer(
					TryGetEPEXT("glGenFramebuffers"),
					typeof(GenFramebuffers)
				);
				glDeleteFramebuffers = (DeleteFramebuffers) Marshal.GetDelegateForFunctionPointer(
					TryGetEPEXT("glDeleteFramebuffers"),
					typeof(DeleteFramebuffers)
				);
				glBindFramebuffer = (G_BindFramebuffer) Marshal.GetDelegateForFunctionPointer(
					TryGetEPEXT("glBindFramebuffer"),
					typeof(G_BindFramebuffer)
				);
				glFramebufferTexture2D = (FramebufferTexture2D) Marshal.GetDelegateForFunctionPointer(
					TryGetEPEXT("glFramebufferTexture2D"),
					typeof(FramebufferTexture2D)
				);
				glFramebufferRenderbuffer = (FramebufferRenderbuffer) Marshal.GetDelegateForFunctionPointer(
					TryGetEPEXT("glFramebufferRenderbuffer"),
					typeof(FramebufferRenderbuffer)
				);
				glGenerateMipmap = (GenerateMipmap) Marshal.GetDelegateForFunctionPointer(
					TryGetEPEXT("glGenerateMipmap"),
					typeof(GenerateMipmap)
				);
				glGenRenderbuffers = (GenRenderbuffers) Marshal.GetDelegateForFunctionPointer(
					TryGetEPEXT("glGenRenderbuffers"),
					typeof(GenRenderbuffers)
				);
				glDeleteRenderbuffers = (DeleteRenderbuffers) Marshal.GetDelegateForFunctionPointer(
					TryGetEPEXT("glDeleteRenderbuffers"),
					typeof(DeleteRenderbuffers)
				);
				glBindRenderbuffer = (BindRenderbuffer) Marshal.GetDelegateForFunctionPointer(
					TryGetEPEXT("glBindRenderbuffer"),
					typeof(BindRenderbuffer)
				);
				glRenderbufferStorage = (RenderbufferStorage) Marshal.GetDelegateForFunctionPointer(
					TryGetEPEXT("glRenderbufferStorage"),
					typeof(RenderbufferStorage)
				);
			}
			catch
			{
				throw new NoSuitableGraphicsDeviceException("OpenGL framebuffer support is required!");
			}

			/* EXT_framebuffer_blit (or ARB_framebuffer_object) is needed by the faux-backbuffer. */
			supportsFauxBackbuffer = true;
			try
			{
				glBlitFramebuffer = (BlitFramebuffer) Marshal.GetDelegateForFunctionPointer(
					TryGetEPEXT("glBlitFramebuffer"),
					typeof(BlitFramebuffer)
				);
			}
			catch
			{
				supportsFauxBackbuffer = false;
			}

			/* ARB_instanced_arrays/ARB_draw_instanced are almost optional. */
			SupportsHardwareInstancing = true;
			try
			{
				glVertexAttribDivisor = (VertexAttribDivisor) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glVertexAttribDivisor"),
					typeof(VertexAttribDivisor)
				);
				/* The likelihood of someone having BaseVertex but not Instanced is 0...? */
				if (supportsBaseVertex)
				{
					glDrawElementsInstancedBaseVertex = (DrawElementsInstancedBaseVertex) Marshal.GetDelegateForFunctionPointer(
						SDL.SDL_GL_GetProcAddress("glDrawElementsInstancedBaseVertex"),
						typeof(DrawElementsInstancedBaseVertex)
					);
				}
				else
				{
					glDrawElementsInstanced = (DrawElementsInstanced) Marshal.GetDelegateForFunctionPointer(
						SDL.SDL_GL_GetProcAddress("glDrawElementsInstanced"),
						typeof(DrawElementsInstanced)
					);
					glDrawElementsInstancedBaseVertex = DrawElementsInstancedNoBase;
				}
			}
			catch
			{
				SupportsHardwareInstancing = false;
			}

			/* EXT_draw_buffers2 is probably used by nobody. */
			try
			{
				glColorMaskIndexedEXT = (ColorMaskIndexedEXT) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glColorMaskIndexedEXT"),
					typeof(ColorMaskIndexedEXT)
				);
			}
			catch
			{
				// FIXME: SupportsIndependentWriteMasks? -flibit
			}

			/* EXT_framebuffer_multisample/ARB_texture_multisample is glitter -flibit */
			supportsMultisampling = true;
			try
			{
				glRenderbufferStorageMultisample = (RenderbufferStorageMultisample) Marshal.GetDelegateForFunctionPointer(
					TryGetEPEXT("glRenderbufferStorageMultisample"),
					typeof(RenderbufferStorageMultisample)
				);
				glSampleMaski = (SampleMaski) Marshal.GetDelegateForFunctionPointer(
					SDL.SDL_GL_GetProcAddress("glSampleMaski"),
					typeof(SampleMaski)
				);
			}
			catch
			{
				supportsMultisampling = false;
			}

			if (useCoreProfile)
			{
				try
				{
					INTERNAL_glGetStringi = (GetStringi) Marshal.GetDelegateForFunctionPointer(
						SDL.SDL_GL_GetProcAddress("glGetStringi"),
						typeof(GetStringi)
					);
					glGenVertexArrays = (GenVertexArrays) Marshal.GetDelegateForFunctionPointer(
						SDL.SDL_GL_GetProcAddress("glGenVertexArrays"),
						typeof(GenVertexArrays)
					);
					glDeleteVertexArrays = (DeleteVertexArrays) Marshal.GetDelegateForFunctionPointer(
						SDL.SDL_GL_GetProcAddress("glDeleteVertexArrays"),
						typeof(DeleteVertexArrays)
					);
					glBindVertexArray = (BindVertexArray) Marshal.GetDelegateForFunctionPointer(
						SDL.SDL_GL_GetProcAddress("glBindVertexArray"),
						typeof(BindVertexArray)
					);
				}
				catch
				{
					throw new NoSuitableGraphicsDeviceException("OpenGL 3.2 support is required!");
				}
			}

#if DEBUG
			/* ARB_debug_output, for debug contexts */
			IntPtr messageCallback = SDL.SDL_GL_GetProcAddress("glDebugMessageCallbackARB");
			IntPtr messageControl = SDL.SDL_GL_GetProcAddress("glDebugMessageControlARB");
			if (messageCallback == IntPtr.Zero || messageControl == IntPtr.Zero)
			{
				FNAPlatform.Log("ARB_debug_output not supported!");
			}
			else
			{
				glDebugMessageCallbackARB = (DebugMessageCallback) Marshal.GetDelegateForFunctionPointer(
					messageCallback,
					typeof(DebugMessageCallback)
				);
				glDebugMessageControlARB = (DebugMessageControl) Marshal.GetDelegateForFunctionPointer(
					messageControl,
					typeof(DebugMessageControl)
				);
				glDebugMessageControlARB(
					GLenum.GL_DONT_CARE,
					GLenum.GL_DONT_CARE,
					GLenum.GL_DONT_CARE,
					0,
					IntPtr.Zero,
					true
				);
				glDebugMessageControlARB(
					GLenum.GL_DONT_CARE,
					GLenum.GL_DEBUG_TYPE_OTHER_ARB,
					GLenum.GL_DEBUG_SEVERITY_LOW_ARB,
					0,
					IntPtr.Zero,
					false
				);
				glDebugMessageControlARB(
					GLenum.GL_DONT_CARE,
					GLenum.GL_DEBUG_TYPE_OTHER_ARB,
					GLenum.GL_DEBUG_SEVERITY_NOTIFICATION_ARB,
					0,
					IntPtr.Zero,
					false
				);
				glDebugMessageCallbackARB(DebugCall, IntPtr.Zero);
			}

			/* GREMEDY_string_marker, for apitrace */
			IntPtr stringMarkerCallback = SDL.SDL_GL_GetProcAddress("glStringMarkerGREMEDY");
			if (stringMarkerCallback == IntPtr.Zero)
			{
				FNAPlatform.Log("GREMEDY_string_marker not supported!");
			}
			else
			{
				glStringMarkerGREMEDY = (StringMarkerGREMEDY) Marshal.GetDelegateForFunctionPointer(
					stringMarkerCallback,
					typeof(StringMarkerGREMEDY)
				);
			}
#endif
		}
Ejemplo n.º 11
0
        private void UpdateResources(int width, int height)
        {
            {
                if (this.headTexture != null)
                {
                    this.headTexture.Dispose();
                }

                int  level = 0, border = 0;
                uint internalformat = GL.GL_R32UI, format = GL.GL_RED_INTEGER, type = GL.GL_UNSIGNED_BYTE;
                var  storage = new TexImage2D(TexImage2D.Target.Texture2D, level, internalformat, width, height, border, format, type);
                var  texture = new Texture(TextureTarget.Texture2D, storage,
                                           TexParameter.Create(TexParameter.PropertyName.TextureWrapT, (int)GL.GL_REPEAT),
                                           TexParameter.Create(TexParameter.PropertyName.TextureWrapS, (int)GL.GL_REPEAT),
                                           TexParameter.Create(TexParameter.PropertyName.TextureWrapR, (int)GL.GL_REPEAT),
                                           TexParameter.Create(TexParameter.PropertyName.TextureMinFilter, (int)GL.GL_NEAREST),
                                           TexParameter.Create(TexParameter.PropertyName.TextureMagFilter, (int)GL.GL_NEAREST)
                                           );
                texture.Initialize();

                this.headTexture = texture;
            }
            // Create buffer for clearing the head pointer texture
            {
                if (this.headClearBuffer != null)
                {
                    this.headClearBuffer.Dispose();
                }

                int length = width * height;
                // NOTE: not all initial values are zero in this unmanged array.
                PixelUnpackBuffer buffer = PixelUnpackBuffer.Create(typeof(uint), length, BufferUsage.StaticDraw);
                // initialize buffer's value to 0.
                //unsafe
                //{
                //    IntPtr pointer = ptr.MapBuffer(MapBufferAccess.WriteOnly);
                //    var array = (uint*)pointer.ToPointer();
                //    for (int i = 0; i < MAX_FRAMEBUFFER_WIDTH * MAX_FRAMEBUFFER_HEIGHT; i++)
                //    {
                //        array[i] = 0;
                //    }
                //}
                // another way to initialize buffer's value to 0.
                //using (var data = new UnmanagedArray<uint>(1))
                //{
                //    data[0] = 0;
                //    ptr.ClearBufferData(OpenGL.GL_UNSIGNED_INT, OpenGL.GL_RED, OpenGL.GL_UNSIGNED_INT, data);
                //}

                this.headClearBuffer = buffer;
            }
            // Bind it to a texture (for use as a TBO)
            {
                if (this.linkedListTexture != null)
                {
                    this.linkedListTexture.Dispose();
                }

                int           length         = width * height * 3;
                TextureBuffer buffer         = TextureBuffer.Create(typeof(vec4), length, BufferUsage.DynamicCopy);
                uint          internalFormat = GL.GL_RGBA32UI;
                Texture       texture        = buffer.DumpBufferTexture(internalFormat, autoDispose: true);
                texture.Initialize();
                buffer.Dispose();// dispose it ASAP.

                this.linkedListTexture = texture;
            }
        }
Ejemplo n.º 12
0
        public PeelingResource(int width, int height)
        {
            this.width  = width;
            this.height = height;

            for (int i = 0; i < 2; i++)
            {
                var depthStorage = new TexImage2D(TexImage2D.Target.TextureRectangle, 0, GL.GL_DEPTH_COMPONENT32, width, height, 0, GL.GL_DEPTH_COMPONENT, GL.GL_FLOAT);
                var depthTexture = new Texture(TextureTarget.TextureRectangle, depthStorage,
                                               new TexParameteri(TexParameter.PropertyName.TextureWrapS, (int)GL.GL_CLAMP),
                                               new TexParameteri(TexParameter.PropertyName.TextureWrapT, (int)GL.GL_CLAMP),
                                               new TexParameteri(TexParameter.PropertyName.TextureMagFilter, (int)GL.GL_NEAREST),
                                               new TexParameteri(TexParameter.PropertyName.TextureMinFilter, (int)GL.GL_NEAREST)
                                               );
                depthTexture.Initialize();

                var colorStorage = new TexImage2D(TexImage2D.Target.TextureRectangle, 0, GL.GL_RGBA, width, height, 0, GL.GL_RGBA, GL.GL_FLOAT);
                var colorTexture = new Texture(TextureTarget.TextureRectangle, colorStorage,
                                               new TexParameteri(TexParameter.PropertyName.TextureWrapS, (int)GL.GL_CLAMP),
                                               new TexParameteri(TexParameter.PropertyName.TextureWrapT, (int)GL.GL_CLAMP),
                                               new TexParameteri(TexParameter.PropertyName.TextureMagFilter, (int)GL.GL_NEAREST),
                                               new TexParameteri(TexParameter.PropertyName.TextureMinFilter, (int)GL.GL_NEAREST)
                                               );
                colorTexture.Initialize();

                var framebuffer = new Framebuffer(width, height);
                framebuffer.Bind();

                framebuffer.Attach(depthTexture, true);
                framebuffer.Attach(colorTexture, false);

                framebuffer.CheckCompleteness();
                framebuffer.Unbind();

                this.framebuffers[i]     = framebuffer;
                this.colorAttachments[i] = colorTexture;
                this.depthAttachments[i] = depthTexture;
            }

            {
                var colorStorage = new TexImage2D(TexImage2D.Target.TextureRectangle, 0, GL.GL_RGBA, width, height, 0, GL.GL_RGBA, GL.GL_FLOAT);
                var colorTexture = new Texture(TextureTarget.TextureRectangle, colorStorage,
                                               new TexParameteri(TexParameter.PropertyName.TextureWrapS, (int)GL.GL_CLAMP),
                                               new TexParameteri(TexParameter.PropertyName.TextureWrapT, (int)GL.GL_CLAMP),
                                               new TexParameteri(TexParameter.PropertyName.TextureMagFilter, (int)GL.GL_NEAREST),
                                               new TexParameteri(TexParameter.PropertyName.TextureMinFilter, (int)GL.GL_NEAREST)
                                               );
                colorTexture.Initialize();

                var framebuffer = new Framebuffer(width, height);
                framebuffer.Bind();

                framebuffer.Attach(this.depthAttachments[0], true);
                framebuffer.Attach(colorTexture, false);

                framebuffer.CheckCompleteness();
                framebuffer.Unbind();

                this.colorBlenderFramebuffer     = framebuffer;
                this.colorBlenderColorAttachment = colorTexture;
            }
        }
Ejemplo n.º 13
0
        /* END STRING MARKER FUNCTIONS */
#endif

        private void LoadGLEntryPoints()
        {
            /* Basic entry points. If you don't have these, you're screwed. */
            try
            {
                INTERNAL_glGetString = (GetString)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glGetString"),
                    typeof(GetString)
                    );
                glGetIntegerv = (GetIntegerv)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glGetIntegerv"),
                    typeof(GetIntegerv)
                    );
                glEnable = (Enable)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glEnable"),
                    typeof(Enable)
                    );
                glDisable = (Disable)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glDisable"),
                    typeof(Disable)
                    );
                glViewport = (G_Viewport)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glViewport"),
                    typeof(G_Viewport)
                    );
                glDepthRange = (DepthRange)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glDepthRange"),
                    typeof(DepthRange)
                    );
                glScissor = (Scissor)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glScissor"),
                    typeof(Scissor)
                    );
                glBlendColor = (BlendColor)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glBlendColor"),
                    typeof(BlendColor)
                    );
                glBlendFuncSeparate = (BlendFuncSeparate)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glBlendFuncSeparate"),
                    typeof(BlendFuncSeparate)
                    );
                glBlendEquationSeparate = (BlendEquationSeparate)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glBlendEquationSeparate"),
                    typeof(BlendEquationSeparate)
                    );
                glColorMask = (ColorMask)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glColorMask"),
                    typeof(ColorMask)
                    );
                glDepthMask = (DepthMask)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glDepthMask"),
                    typeof(DepthMask)
                    );
                glDepthFunc = (DepthFunc)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glDepthFunc"),
                    typeof(DepthFunc)
                    );
                glStencilMask = (StencilMask)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glStencilMask"),
                    typeof(StencilMask)
                    );
                glStencilFuncSeparate = (StencilFuncSeparate)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glStencilFuncSeparate"),
                    typeof(StencilFuncSeparate)
                    );
                glStencilOpSeparate = (StencilOpSeparate)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glStencilOpSeparate"),
                    typeof(StencilOpSeparate)
                    );
                glStencilFunc = (StencilFunc)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glStencilFunc"),
                    typeof(StencilFunc)
                    );
                glStencilOp = (StencilOp)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glStencilOp"),
                    typeof(StencilOp)
                    );
                glCullFace = (CullFace)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glCullFace"),
                    typeof(CullFace)
                    );
                glFrontFace = (FrontFace)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glFrontFace"),
                    typeof(FrontFace)
                    );
                glPolygonMode = (PolygonMode)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glPolygonMode"),
                    typeof(PolygonMode)
                    );
                glPolygonOffset = (PolygonOffset)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glPolygonOffset"),
                    typeof(PolygonOffset)
                    );
                glGenTextures = (GenTextures)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glGenTextures"),
                    typeof(GenTextures)
                    );
                glDeleteTextures = (DeleteTextures)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glDeleteTextures"),
                    typeof(DeleteTextures)
                    );
                glBindTexture = (G_BindTexture)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glBindTexture"),
                    typeof(G_BindTexture)
                    );
                glTexImage2D = (TexImage2D)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glTexImage2D"),
                    typeof(TexImage2D)
                    );
                glTexSubImage2D = (TexSubImage2D)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glTexSubImage2D"),
                    typeof(TexSubImage2D)
                    );
                glCompressedTexImage2D = (CompressedTexImage2D)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glCompressedTexImage2D"),
                    typeof(CompressedTexImage2D)
                    );
                glCompressedTexSubImage2D = (CompressedTexSubImage2D)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glCompressedTexSubImage2D"),
                    typeof(CompressedTexSubImage2D)
                    );
                glTexImage3D = (TexImage3D)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glTexImage3D"),
                    typeof(TexImage3D)
                    );
                glTexSubImage3D = (TexSubImage3D)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glTexSubImage3D"),
                    typeof(TexSubImage3D)
                    );
                glGetTexImage = (GetTexImage)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glGetTexImage"),
                    typeof(GetTexImage)
                    );
                glTexParameteri = (TexParameteri)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glTexParameteri"),
                    typeof(TexParameteri)
                    );
                glTexParameterf = (TexParameterf)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glTexParameterf"),
                    typeof(TexParameterf)
                    );
                glActiveTexture = (ActiveTexture)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glActiveTexture"),
                    typeof(ActiveTexture)
                    );
                glPixelStorei = (PixelStorei)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glPixelStorei"),
                    typeof(PixelStorei)
                    );
                glGenBuffers = (GenBuffers)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glGenBuffers"),
                    typeof(GenBuffers)
                    );
                glDeleteBuffers = (DeleteBuffers)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glDeleteBuffers"),
                    typeof(DeleteBuffers)
                    );
                glBindBuffer = (BindBuffer)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glBindBuffer"),
                    typeof(BindBuffer)
                    );
                glBufferData = (BufferData)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glBufferData"),
                    typeof(BufferData)
                    );
                glBufferSubData = (BufferSubData)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glBufferSubData"),
                    typeof(BufferSubData)
                    );
                glMapBuffer = (MapBuffer)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glMapBuffer"),
                    typeof(MapBuffer)
                    );
                glUnmapBuffer = (UnmapBuffer)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glUnmapBuffer"),
                    typeof(UnmapBuffer)
                    );
                glClearColor = (ClearColor)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glClearColor"),
                    typeof(ClearColor)
                    );
                glClearDepth = (ClearDepth)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glClearDepth"),
                    typeof(ClearDepth)
                    );
                glClearStencil = (ClearStencil)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glClearStencil"),
                    typeof(ClearStencil)
                    );
                glClear = (G_Clear)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glClear"),
                    typeof(G_Clear)
                    );
                glDrawBuffers = (DrawBuffers)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glDrawBuffers"),
                    typeof(DrawBuffers)
                    );
                glReadPixels = (ReadPixels)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glReadPixels"),
                    typeof(ReadPixels)
                    );
                glDrawRangeElements = (DrawRangeElements)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glDrawRangeElements"),
                    typeof(DrawRangeElements)
                    );
                glDrawArrays = (DrawArrays)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glDrawArrays"),
                    typeof(DrawArrays)
                    );
                glGenQueries = (GenQueries)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glGenQueries"),
                    typeof(GenQueries)
                    );
                glDeleteQueries = (DeleteQueries)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glDeleteQueries"),
                    typeof(DeleteQueries)
                    );
                glBeginQuery = (BeginQuery)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glBeginQuery"),
                    typeof(BeginQuery)
                    );
                glEndQuery = (EndQuery)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glEndQuery"),
                    typeof(EndQuery)
                    );
                glGetQueryObjectiv = (GetQueryObjectiv)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glGetQueryObjectiv"),
                    typeof(GetQueryObjectiv)
                    );
            }
            catch
            {
                throw new NoSuitableGraphicsDeviceException("OpenGL 2.1 support is required!");
            }

            /* ARB_framebuffer_object. We're flexible, but not _that_ flexible. */
            try
            {
                glGenFramebuffers = (GenFramebuffers)Marshal.GetDelegateForFunctionPointer(
                    TryGetFramebufferEP("glGenFramebuffers"),
                    typeof(GenFramebuffers)
                    );
                glDeleteFramebuffers = (DeleteFramebuffers)Marshal.GetDelegateForFunctionPointer(
                    TryGetFramebufferEP("glDeleteFramebuffers"),
                    typeof(DeleteFramebuffers)
                    );
                glBindFramebuffer = (G_BindFramebuffer)Marshal.GetDelegateForFunctionPointer(
                    TryGetFramebufferEP("glBindFramebuffer"),
                    typeof(G_BindFramebuffer)
                    );
                glFramebufferTexture2D = (FramebufferTexture2D)Marshal.GetDelegateForFunctionPointer(
                    TryGetFramebufferEP("glFramebufferTexture2D"),
                    typeof(FramebufferTexture2D)
                    );
                glFramebufferRenderbuffer = (FramebufferRenderbuffer)Marshal.GetDelegateForFunctionPointer(
                    TryGetFramebufferEP("glFramebufferRenderbuffer"),
                    typeof(FramebufferRenderbuffer)
                    );
                glGenerateMipmap = (GenerateMipmap)Marshal.GetDelegateForFunctionPointer(
                    TryGetFramebufferEP("glGenerateMipmap"),
                    typeof(GenerateMipmap)
                    );
#if !DISABLE_FAUXBACKBUFFER
                glBlitFramebuffer = (BlitFramebuffer)Marshal.GetDelegateForFunctionPointer(
                    TryGetFramebufferEP("glBlitFramebuffer"),
                    typeof(BlitFramebuffer)
                    );
#endif
                glGenRenderbuffers = (GenRenderbuffers)Marshal.GetDelegateForFunctionPointer(
                    TryGetFramebufferEP("glGenRenderbuffers"),
                    typeof(GenRenderbuffers)
                    );
                glDeleteRenderbuffers = (DeleteRenderbuffers)Marshal.GetDelegateForFunctionPointer(
                    TryGetFramebufferEP("glDeleteRenderbuffers"),
                    typeof(DeleteRenderbuffers)
                    );
                glBindRenderbuffer = (BindRenderbuffer)Marshal.GetDelegateForFunctionPointer(
                    TryGetFramebufferEP("glBindRenderbuffer"),
                    typeof(BindRenderbuffer)
                    );
                glRenderbufferStorage = (RenderbufferStorage)Marshal.GetDelegateForFunctionPointer(
                    TryGetFramebufferEP("glRenderbufferStorage"),
                    typeof(RenderbufferStorage)
                    );
            }
            catch
            {
                throw new NoSuitableGraphicsDeviceException("OpenGL framebuffer support is required!");
            }

            /* ARB_instanced_arrays/ARB_draw_instanced are almost optional.
             * While we do not directly call glVertexAttribDivisor ourselves,
             * we still need to check for ARB_instanced_arrays support.
             * -flibit
             */
            SupportsHardwareInstancing = SDL.SDL_GL_GetProcAddress("glVertexAttribDivisor") != IntPtr.Zero;
            try
            {
                glDrawElementsInstanced = (DrawElementsInstanced)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glDrawElementsInstanced"),
                    typeof(DrawElementsInstanced)
                    );
            }
            catch
            {
                SupportsHardwareInstancing = false;
            }

            /* EXT_draw_buffers2 is probably used by nobody. */
            try
            {
                glColorMaskIndexedEXT = (ColorMaskIndexedEXT)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glColorMaskIndexedEXT"),
                    typeof(ColorMaskIndexedEXT)
                    );
            }
            catch
            {
                // FIXME: SupportsIndependentWriteMasks? -flibit
            }

            /* EXT_framebuffer_multisample/ARB_texture_multisample is glitter -flibit */
            supportsMultisampling = true;
            try
            {
                glRenderbufferStorageMultisample = (RenderbufferStorageMultisample)Marshal.GetDelegateForFunctionPointer(
                    TryGetFramebufferEP("glRenderbufferStorageMultisample"),
                    typeof(RenderbufferStorageMultisample)
                    );
                glSampleMaski = (SampleMaski)Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glSampleMaski"),
                    typeof(SampleMaski)
                    );
            }
            catch
            {
                supportsMultisampling = false;
            }

#if DEBUG
            /* ARB_debug_output, for debug contexts */
            IntPtr messageCallback = SDL.SDL_GL_GetProcAddress("glDebugMessageCallbackARB");
            IntPtr messageControl  = SDL.SDL_GL_GetProcAddress("glDebugMessageControlARB");
            if (messageCallback == IntPtr.Zero || messageControl == IntPtr.Zero)
            {
                System.Console.WriteLine("ARB_debug_output not supported!");
            }
            else
            {
                glDebugMessageCallbackARB = (DebugMessageCallback)Marshal.GetDelegateForFunctionPointer(
                    messageCallback,
                    typeof(DebugMessageCallback)
                    );
                glDebugMessageControlARB = (DebugMessageControl)Marshal.GetDelegateForFunctionPointer(
                    messageControl,
                    typeof(DebugMessageControl)
                    );
                glDebugMessageControlARB(
                    GLenum.GL_DONT_CARE,
                    GLenum.GL_DONT_CARE,
                    GLenum.GL_DONT_CARE,
                    0,
                    IntPtr.Zero,
                    true
                    );
                glDebugMessageControlARB(
                    GLenum.GL_DONT_CARE,
                    GLenum.GL_DEBUG_TYPE_OTHER_ARB,
                    GLenum.GL_DEBUG_SEVERITY_LOW_ARB,
                    0,
                    IntPtr.Zero,
                    false
                    );
                glDebugMessageControlARB(
                    GLenum.GL_DONT_CARE,
                    GLenum.GL_DEBUG_TYPE_OTHER_ARB,
                    GLenum.GL_DEBUG_SEVERITY_NOTIFICATION_ARB,
                    0,
                    IntPtr.Zero,
                    false
                    );
                glDebugMessageCallbackARB(DebugCall, IntPtr.Zero);
            }

            /* GREMEDY_string_marker, for apitrace */
            IntPtr stringMarkerCallback = SDL.SDL_GL_GetProcAddress("glStringMarkerGREMEDY");
            if (stringMarkerCallback == IntPtr.Zero)
            {
                System.Console.WriteLine("GREMEDY_string_marker not supported!");
            }
            else
            {
                glStringMarkerGREMEDY = (StringMarkerGREMEDY)Marshal.GetDelegateForFunctionPointer(
                    stringMarkerCallback,
                    typeof(StringMarkerGREMEDY)
                    );
            }
#endif
        }
Ejemplo n.º 14
0
        public void LoadGLEntryPoints()
        {
            /* Basic entry points. If you don't have these, you're screwed. */
            try
            {
                INTERNAL_glGetString = (GetString) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glGetString"),
                    typeof(GetString)
                );
                glGetIntegerv = (GetIntegerv) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glGetIntegerv"),
                    typeof(GetIntegerv)
                );
                glEnable = (Enable) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glEnable"),
                    typeof(Enable)
                );
                glDisable = (Disable) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glDisable"),
                    typeof(Disable)
                );
                glViewport = (G_Viewport) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glViewport"),
                    typeof(G_Viewport)
                );
                glDepthRange = (DepthRange) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glDepthRange"),
                    typeof(DepthRange)
                );
                glScissor = (Scissor) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glScissor"),
                    typeof(Scissor)
                );
                glBlendColor = (BlendColor) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glBlendColor"),
                    typeof(BlendColor)
                );
                glBlendFuncSeparate = (BlendFuncSeparate) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glBlendFuncSeparate"),
                    typeof(BlendFuncSeparate)
                );
                glBlendEquationSeparate = (BlendEquationSeparate) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glBlendEquationSeparate"),
                    typeof(BlendEquationSeparate)
                );
                glColorMask = (ColorMask) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glColorMask"),
                    typeof(ColorMask)
                );
                glDepthMask = (DepthMask) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glDepthMask"),
                    typeof(DepthMask)
                );
                glDepthFunc = (DepthFunc) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glDepthFunc"),
                    typeof(DepthFunc)
                );
                glStencilMask = (StencilMask) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glStencilMask"),
                    typeof(StencilMask)
                );
                glStencilFuncSeparate = (StencilFuncSeparate) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glStencilFuncSeparate"),
                    typeof(StencilFuncSeparate)
                );
                glStencilOpSeparate = (StencilOpSeparate) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glStencilOpSeparate"),
                    typeof(StencilOpSeparate)
                );
                glStencilFunc = (StencilFunc) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glStencilFunc"),
                    typeof(StencilFunc)
                );
                glStencilOp = (StencilOp) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glStencilOp"),
                    typeof(StencilOp)
                );
                glCullFace = (CullFace) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glCullFace"),
                    typeof(CullFace)
                );
                glFrontFace = (FrontFace) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glFrontFace"),
                    typeof(FrontFace)
                );
                glPolygonMode = (PolygonMode) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glPolygonMode"),
                    typeof(PolygonMode)
                );
                glPolygonOffset = (PolygonOffset) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glPolygonOffset"),
                    typeof(PolygonOffset)
                );
                glGenTextures = (GenTextures) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glGenTextures"),
                    typeof(GenTextures)
                );
                glDeleteTextures = (DeleteTextures) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glDeleteTextures"),
                    typeof(DeleteTextures)
                );
                glBindTexture = (G_BindTexture) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glBindTexture"),
                    typeof(G_BindTexture)
                );
                glTexImage2D = (TexImage2D) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glTexImage2D"),
                    typeof(TexImage2D)
                );
                glTexSubImage2D = (TexSubImage2D) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glTexSubImage2D"),
                    typeof(TexSubImage2D)
                );
                glCompressedTexImage2D = (CompressedTexImage2D) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glCompressedTexImage2D"),
                    typeof(CompressedTexImage2D)
                );
                glCompressedTexSubImage2D = (CompressedTexSubImage2D) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glCompressedTexSubImage2D"),
                    typeof(CompressedTexSubImage2D)
                );
                glTexImage3D = (TexImage3D) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glTexImage3D"),
                    typeof(TexImage3D)
                );
                glTexSubImage3D = (TexSubImage3D) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glTexSubImage3D"),
                    typeof(TexSubImage3D)
                );
                glGetTexImage = (GetTexImage) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glGetTexImage"),
                    typeof(GetTexImage)
                );
                glTexParameteri = (TexParameteri) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glTexParameteri"),
                    typeof(TexParameteri)
                );
                glTexParameterf = (TexParameterf) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glTexParameterf"),
                    typeof(TexParameterf)
                );
                glActiveTexture = (ActiveTexture) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glActiveTexture"),
                    typeof(ActiveTexture)
                );
                glGetTexLevelParameteriv = (GetTexLevelParameteriv) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glGetTexLevelParameteriv"),
                    typeof(GetTexLevelParameteriv)
                );
                glPixelStorei = (PixelStorei) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glPixelStorei"),
                    typeof(PixelStorei)
                );
                glGenBuffers = (GenBuffers) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glGenBuffers"),
                    typeof(GenBuffers)
                );
                glDeleteBuffers = (DeleteBuffers) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glDeleteBuffers"),
                    typeof(DeleteBuffers)
                );
                glBindBuffer = (BindBuffer) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glBindBuffer"),
                    typeof(BindBuffer)
                );
                glBufferData = (BufferData) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glBufferData"),
                    typeof(BufferData)
                );
                glBufferSubData = (BufferSubData) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glBufferSubData"),
                    typeof(BufferSubData)
                );
                glMapBuffer = (MapBuffer) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glMapBuffer"),
                    typeof(MapBuffer)
                );
                glUnmapBuffer = (UnmapBuffer) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glUnmapBuffer"),
                    typeof(UnmapBuffer)
                );
                glEnableVertexAttribArray = (EnableVertexAttribArray) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glEnableVertexAttribArray"),
                    typeof(EnableVertexAttribArray)
                );
                glDisableVertexAttribArray = (DisableVertexAttribArray) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glDisableVertexAttribArray"),
                    typeof(DisableVertexAttribArray)
                );
                glVertexAttribPointer = (G_VertexAttribPointer) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glVertexAttribPointer"),
                    typeof(G_VertexAttribPointer)
                );
                glClearColor = (ClearColor) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glClearColor"),
                    typeof(ClearColor)
                );
                glClearDepth = (ClearDepth) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glClearDepth"),
                    typeof(ClearDepth)
                );
                glClearStencil = (ClearStencil) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glClearStencil"),
                    typeof(ClearStencil)
                );
                glClear = (G_Clear) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glClear"),
                    typeof(G_Clear)
                );
                glDrawBuffers = (DrawBuffers) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glDrawBuffers"),
                    typeof(DrawBuffers)
                );
                glReadPixels = (ReadPixels) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glReadPixels"),
                    typeof(ReadPixels)
                );
                glDrawRangeElements = (DrawRangeElements) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glDrawRangeElements"),
                    typeof(DrawRangeElements)
                );
                glDrawArrays = (DrawArrays) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glDrawArrays"),
                    typeof(DrawArrays)
                );
                glGenQueries = (GenQueries) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glGenQueries"),
                    typeof(GenQueries)
                );
                glDeleteQueries = (DeleteQueries) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glDeleteQueries"),
                    typeof(DeleteQueries)
                );
                glBeginQuery = (BeginQuery) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glBeginQuery"),
                    typeof(BeginQuery)
                );
                glEndQuery = (EndQuery) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glEndQuery"),
                    typeof(EndQuery)
                );
                glGetQueryObjectiv = (GetQueryObjectiv) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glGetQueryObjectiv"),
                    typeof(GetQueryObjectiv)
                );
                glCreateShader = (CreateShader) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glCreateShader"),
                    typeof(CreateShader)
                );
                glDeleteShader = (DeleteShader) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glDeleteShader"),
                    typeof(DeleteShader)
                );
                glShaderSource = (ShaderSource) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glShaderSource"),
                    typeof(ShaderSource)
                );
                glCompileShader = (CompileShader) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glCompileShader"),
                    typeof(CompileShader)
                );
                glCreateProgram = (CreateProgram) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glCreateProgram"),
                    typeof(CreateProgram)
                );
                glDeleteProgram = (DeleteProgram) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glDeleteProgram"),
                    typeof(DeleteProgram)
                );
                glAttachShader = (AttachShader) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glAttachShader"),
                    typeof(AttachShader)
                );
                glDetachShader = (DetachShader) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glDetachShader"),
                    typeof(DetachShader)
                );
                glLinkProgram = (LinkProgram) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glLinkProgram"),
                    typeof(LinkProgram)
                );
                glUseProgram = (UseProgram) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glUseProgram"),
                    typeof(UseProgram)
                );
                glUniform1i = (Uniform1i) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glUniform1i"),
                    typeof(Uniform1i)
                );
                glUniform4fv = (Uniform4fv) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glUniform4fv"),
                    typeof(Uniform4fv)
                );
                glGetShaderiv = (GetShaderiv) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glGetShaderiv"),
                    typeof(GetShaderiv)
                );
                glGetProgramiv = (GetProgramiv) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glGetProgramiv"),
                    typeof(GetProgramiv)
                );
                glGetUniformLocation = (GetUniformLocation) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glGetUniformLocation"),
                    typeof(GetUniformLocation)
                );
                glGetAttribLocation = (GetAttribLocation) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glGetAttribLocation"),
                    typeof(GetAttribLocation)
                );
                glBindAttribLocation = (BindAttribLocation) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glBindAttribLocation"),
                    typeof(BindAttribLocation)
                );
                glIsShader = (IsShader) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glIsShader"),
                    typeof(IsShader)
                );
                glIsProgram = (IsProgram) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glIsProgram"),
                    typeof(IsProgram)
                );
                glGetShaderInfoLog = (GetShaderInfoLog) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glGetShaderInfoLog"),
                    typeof(GetShaderInfoLog)
                );
                glGetProgramInfoLog = (GetProgramInfoLog) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glGetProgramInfoLog"),
                    typeof(GetProgramInfoLog)
                );
                glFlush = (Flush) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glFlush"),
                    typeof(Flush)
                );
            }
            catch
            {
                throw new NoSuitableGraphicsDeviceException("OpenGL 2.1 support is required!");
            }

            /* ARB_framebuffer_object. We're flexible, but not _that_ flexible. */
            try
            {
                glGenFramebuffers = (GenFramebuffers) Marshal.GetDelegateForFunctionPointer(
                    TryGetFramebufferEP("glGenFramebuffers"),
                    typeof(GenFramebuffers)
                );
                glDeleteFramebuffers = (DeleteFramebuffers) Marshal.GetDelegateForFunctionPointer(
                    TryGetFramebufferEP("glDeleteFramebuffers"),
                    typeof(DeleteFramebuffers)
                );
                glBindFramebuffer = (G_BindFramebuffer) Marshal.GetDelegateForFunctionPointer(
                    TryGetFramebufferEP("glBindFramebuffer"),
                    typeof(G_BindFramebuffer)
                );
                glFramebufferTexture2D = (FramebufferTexture2D) Marshal.GetDelegateForFunctionPointer(
                    TryGetFramebufferEP("glFramebufferTexture2D"),
                    typeof(FramebufferTexture2D)
                );
                glFramebufferRenderbuffer = (FramebufferRenderbuffer) Marshal.GetDelegateForFunctionPointer(
                    TryGetFramebufferEP("glFramebufferRenderbuffer"),
                    typeof(FramebufferRenderbuffer)
                );
            #if !DISABLE_FAUXBACKBUFFER
                glBlitFramebuffer = (BlitFramebuffer) Marshal.GetDelegateForFunctionPointer(
                    TryGetFramebufferEP("glBlitFramebuffer"),
                    typeof(BlitFramebuffer)
                );
            #endif
                glGenRenderbuffers = (GenRenderbuffers) Marshal.GetDelegateForFunctionPointer(
                    TryGetFramebufferEP("glGenRenderbuffers"),
                    typeof(GenRenderbuffers)
                );
                glDeleteRenderbuffers = (DeleteRenderbuffers) Marshal.GetDelegateForFunctionPointer(
                    TryGetFramebufferEP("glDeleteRenderbuffers"),
                    typeof(DeleteRenderbuffers)
                );
                glBindRenderbuffer = (BindRenderbuffer) Marshal.GetDelegateForFunctionPointer(
                    TryGetFramebufferEP("glBindRenderbuffer"),
                    typeof(BindRenderbuffer)
                );
                glRenderbufferStorage = (RenderbufferStorage) Marshal.GetDelegateForFunctionPointer(
                    TryGetFramebufferEP("glRenderbufferStorage"),
                    typeof(RenderbufferStorage)
                );
            }
            catch
            {
                throw new NoSuitableGraphicsDeviceException("OpenGL framebuffer support is required!");
            }

            /* ARB_instanced_arrays/ARB_draw_instanced are almost optional. */
            SupportsHardwareInstancing = true;
            try
            {
                glVertexAttribDivisor = (VertexAttribDivisor) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glVertexAttribDivisor"),
                    typeof(VertexAttribDivisor)
                );
                glDrawElementsInstanced = (DrawElementsInstanced) Marshal.GetDelegateForFunctionPointer(
                    SDL.SDL_GL_GetProcAddress("glDrawElementsInstanced"),
                    typeof(DrawElementsInstanced)
                );
            }
            catch
            {
                SupportsHardwareInstancing = false;
            }

            #if DEBUG
            /* ARB_debug_output, for debug contexts */
            IntPtr messageCallback = SDL.SDL_GL_GetProcAddress("glDebugMessageCallbackARB");
            IntPtr messageControl = SDL.SDL_GL_GetProcAddress("glDebugMessageControlARB");
            if (messageCallback == IntPtr.Zero || messageControl == IntPtr.Zero)
            {
                System.Console.WriteLine("ARB_debug_output not supported!");
            }
            else
            {
                glDebugMessageCallbackARB = (DebugMessageCallback) Marshal.GetDelegateForFunctionPointer(
                    messageCallback,
                    typeof(DebugMessageCallback)
                );
                glDebugMessageControlARB = (DebugMessageControl) Marshal.GetDelegateForFunctionPointer(
                    messageControl,
                    typeof(DebugMessageControl)
                );
                glDebugMessageCallbackARB(DebugCall, IntPtr.Zero);
                glDebugMessageControlARB(
                    GLenum.GL_DONT_CARE,
                    GLenum.GL_DONT_CARE,
                    GLenum.GL_DONT_CARE,
                    0,
                    IntPtr.Zero,
                    true
                );
                glDebugMessageControlARB(
                    GLenum.GL_DONT_CARE,
                    GLenum.GL_DEBUG_TYPE_OTHER_ARB,
                    GLenum.GL_DEBUG_SEVERITY_LOW_ARB,
                    0,
                    IntPtr.Zero,
                    false
                );
            }

            /* GREMEDY_string_marker, for apitrace */
            IntPtr stringMarkerCallback = SDL.SDL_GL_GetProcAddress("glStringMarkerGREMEDY");
            if (stringMarkerCallback == IntPtr.Zero)
            {
                System.Console.WriteLine("GREMEDY_string_marker not supported!");
            }
            else
            {
                glStringMarkerGREMEDY = (StringMarkerGREMEDY) Marshal.GetDelegateForFunctionPointer(
                    stringMarkerCallback,
                    typeof(StringMarkerGREMEDY)
                );
            }
            #endif
        }
        public PeelingResource(int width, int height)
        {
            this.width  = width;
            this.height = height;

            uint nextUnit = 0;

            // backBlenderTexture.
            {
                var colorStorage = new TexImage2D(TexImage2D.Target.TextureRectangle, GL.GL_RGB, width, height, GL.GL_RGB, GL.GL_FLOAT);
                var colorTexture = new Texture(colorStorage,
                                               new TexParameteri(TexParameter.PropertyName.TextureWrapS, (int)GL.GL_CLAMP),
                                               new TexParameteri(TexParameter.PropertyName.TextureWrapT, (int)GL.GL_CLAMP),
                                               new TexParameteri(TexParameter.PropertyName.TextureMagFilter, (int)GL.GL_NEAREST),
                                               new TexParameteri(TexParameter.PropertyName.TextureMinFilter, (int)GL.GL_NEAREST)
                                               );
                colorTexture.Initialize();
                colorTexture.TextureUnitIndex = nextUnit++;
                this.backBlenderTexture       = colorTexture;
            }
            // backBlenderFBO.
            {
                var framebuffer = new Framebuffer(width, height);
                framebuffer.Bind();

                framebuffer.Attach(FramebufferTarget.Framebuffer, this.backBlenderTexture, 0u);
                //var depthBuffer = new Renderbuffer(width, height, GL.GL_DEPTH_COMPONENT24);
                //framebuffer.Attach(FramebufferTarget.Framebuffer, depthBuffer, AttachmentLocation.Depth);

                framebuffer.CheckCompleteness();
                framebuffer.Unbind();

                this.backBlenderFBO = framebuffer;
            }

            // depthTextures.
            for (int i = 0; i < 2; i++)
            {
                // TODO: is GL_FLOAT_RG32_NV same with GL_RG32F ?
                //var depthStorage = new TexImage2D(TexImage2D.Target.TextureRectangle, GL_FLOAT_RG32_NV, width, height, GL.GL_RGB, GL.GL_FLOAT);
                var depthStorage = new TexImage2D(TexImage2D.Target.TextureRectangle, GL.GL_RG32F, width, height, GL.GL_RGB, GL.GL_FLOAT);
                var depthTexture = new Texture(depthStorage,
                                               new TexParameteri(TexParameter.PropertyName.TextureWrapS, (int)GL.GL_CLAMP),
                                               new TexParameteri(TexParameter.PropertyName.TextureWrapT, (int)GL.GL_CLAMP),
                                               new TexParameteri(TexParameter.PropertyName.TextureMagFilter, (int)GL.GL_NEAREST),
                                               new TexParameteri(TexParameter.PropertyName.TextureMinFilter, (int)GL.GL_NEAREST)
                                               );
                depthTexture.Initialize();
                depthTexture.TextureUnitIndex = nextUnit++;
                this.depthTextures[i]         = depthTexture;
            }
            // frontBlenderTextures.
            for (int i = 0; i < 2; i++)
            {
                var colorStorage = new TexImage2D(TexImage2D.Target.TextureRectangle, GL.GL_RGBA, width, height, GL.GL_RGBA, GL.GL_FLOAT);
                var colorTexture = new Texture(colorStorage,
                                               new TexParameteri(TexParameter.PropertyName.TextureWrapS, (int)GL.GL_CLAMP),
                                               new TexParameteri(TexParameter.PropertyName.TextureWrapT, (int)GL.GL_CLAMP),
                                               new TexParameteri(TexParameter.PropertyName.TextureMagFilter, (int)GL.GL_NEAREST),
                                               new TexParameteri(TexParameter.PropertyName.TextureMinFilter, (int)GL.GL_NEAREST)
                                               );
                colorTexture.Initialize();
                colorTexture.TextureUnitIndex = nextUnit++;
                this.frontBlenderTextures[i]  = colorTexture;
            }
            // backTmpTextures.
            for (int i = 0; i < 2; i++)
            {
                var colorStorage = new TexImage2D(TexImage2D.Target.TextureRectangle, GL.GL_RGBA, width, height, GL.GL_RGBA, GL.GL_FLOAT);
                var colorTexture = new Texture(colorStorage,
                                               new TexParameteri(TexParameter.PropertyName.TextureWrapS, (int)GL.GL_CLAMP),
                                               new TexParameteri(TexParameter.PropertyName.TextureWrapT, (int)GL.GL_CLAMP),
                                               new TexParameteri(TexParameter.PropertyName.TextureMagFilter, (int)GL.GL_NEAREST),
                                               new TexParameteri(TexParameter.PropertyName.TextureMinFilter, (int)GL.GL_NEAREST)
                                               );
                colorTexture.Initialize();
                colorTexture.TextureUnitIndex = nextUnit++;
                this.backTmpTextures[i]       = colorTexture;
            }
            // peelingSingleFBO.
            {
                var framebuffer = new Framebuffer(width, height);
                framebuffer.Bind();

                framebuffer.Attach(FramebufferTarget.Framebuffer, this.depthTextures[0], 0u);
                framebuffer.Attach(FramebufferTarget.Framebuffer, this.frontBlenderTextures[0], 1u);
                framebuffer.Attach(FramebufferTarget.Framebuffer, this.backTmpTextures[0], 2u);
                framebuffer.Attach(FramebufferTarget.Framebuffer, this.depthTextures[1], 3u);
                framebuffer.Attach(FramebufferTarget.Framebuffer, this.frontBlenderTextures[1], 4u);
                framebuffer.Attach(FramebufferTarget.Framebuffer, this.backTmpTextures[1], 5u);
                framebuffer.Attach(FramebufferTarget.Framebuffer, this.backBlenderTexture, 6u);

                framebuffer.CheckCompleteness();
                framebuffer.Unbind();

                this.peelingSingleFBO = framebuffer;
            }
        }