예제 #1
0
        public GLESHardwareIndexBuffer(HardwareBufferManagerBase manager, IndexType type, int numIndices, BufferUsage usage, bool useShadowBuffer)
            : base(manager, type, numIndices, usage, false, useShadowBuffer)
        {
            if (type == IndexType.Size32)
            {
                throw new AxiomException("32 bit hardware buffers are not allowed in OpenGL ES.");
            }

            if (!useShadowBuffer)
            {
                throw new AxiomException("Only support with shadowBuffer");
            }

            OpenGL.GenBuffers(1, ref this._bufferId);
            GLESConfig.GlCheckError(this);
            if (this._bufferId == 0)
            {
                throw new AxiomException("Cannot create GL index buffer");
            }

            OpenGL.BindBuffer(All.ElementArrayBuffer, this._bufferId);
            GLESConfig.GlCheckError(this);
            OpenGL.BufferData(All.ElementArrayBuffer, new IntPtr(sizeInBytes), IntPtr.Zero, GLESHardwareBufferManager.GetGLUsage(usage));
            GLESConfig.GlCheckError(this);
        }
예제 #2
0
        /// <summary>
        ///   Internal method to create a warning texture (bound when a texture unit is blank)
        /// </summary>
        protected void CreateWarningTexture()
        {
            // Generate warning texture
            int width  = 8;
            int height = 8;

            // TODO convert to 5_6_5
            unsafe
            {
                int *data = stackalloc int[width * height];                   // 0xXXRRGGBB

                //yellow / black stripes
                for (int y = 0; y < height; ++y)
                {
                    for (int x = 0; x < width; ++x)
                    {
                        data[y * width + x] = (((x + y) % 8) < 4) ? 0x000000 : 0xFFFF00;
                    }
                }

                // Create GL resource
                OpenGL.GenTextures(1, ref this._warningTextureID);
                GLESConfig.GlCheckError(this);
                OpenGL.BindTexture(All.Texture2D, this._warningTextureID);
                GLESConfig.GlCheckError(this);
                OpenGL.TexImage2D(All.Texture2D, 0, (int)All.Rgb, width, height, 0, All.Rgb, All.UnsignedByte, (IntPtr)data);
                GLESConfig.GlCheckError(this);
            }
        }
예제 #3
0
 public static void Projection(Matrix projection)
 {
     GL11.MatrixMode(All11.Projection);
     GL11.LoadIdentity();
     GL11.LoadMatrix(Matrix.ToFloatArray(projection));
     //GL11.Ortho(0, _device.DisplayMode.Width, _device.DisplayMode.Height, 0, -1, 1);
 }
예제 #4
0
 public static void View(Matrix view)
 {
     GL11.MatrixMode(All11.Viewport);
     GL11.LoadIdentity();
     GL11.LoadMatrix(Matrix.ToFloatArray(view));
     //GL11.Ortho(0, _device.DisplayMode.Width, _device.DisplayMode.Height, 0, -1, 1);
 }
예제 #5
0
 public static void WorldView(Matrix world, Matrix view)
 {
     GL11.MatrixMode(All11.Modelview);
     GL11.LoadMatrix(ref view.M11);
     GL11.MultMatrix(ref world.M11);
     //GL11.Ortho(0, _device.DisplayMode.Width, _device.DisplayMode.Height, 0, -1, 1);
 }
예제 #6
0
 public void Unload()
 {
     int[] textureIds = new int[1] {
         TexHandle
     };
     GL.DeleteTextures(1, textureIds);
 }
예제 #7
0
        public static void Cull(CullMode cullMode)
        {
            if (_cull != cullMode)
            {
                switch (cullMode)
                {
                case CullMode.CullClockwiseFace:
                    GL11.FrontFace(All11.Ccw);
                    GL11.CullFace(All11.Back);
                    GL11.Enable(All11.CullFace);
                    break;

                case CullMode.CullCounterClockwiseFace:
                    GL11.FrontFace(All11.Cw);
                    GL11.CullFace(All11.Back);
                    GL11.Enable(All11.CullFace);
                    break;

                case CullMode.None:
                    GL11.Disable(All11.CullFace);
                    break;
                }
                _cull = cullMode;
            }
        }
예제 #8
0
 /// <summary>
 /// </summary>
 /// <param name="p"> </param>
 internal void CopyFromFramebuffer(int p)
 {
     OpenGL.BindBuffer(All.Texture2D, this._textureId);
     GLESConfig.GlCheckError(this);
     OpenGL.CopyTexSubImage2D(All.Texture2D, this._level, 0, 0, 0, 0, Width, Height);
     GLESConfig.GlCheckError(this);
 }
예제 #9
0
 public static void World(Matrix world)
 {
     GL11.MatrixMode(All11.Modelview);
     GL11.LoadIdentity();
     GL11.LoadMatrix(Matrix.ToFloatArray(world));
     //GL11.Ortho(0, _device.DisplayMode.Width, _device.DisplayMode.Height, 0, -1, 1);
 }
예제 #10
0
 public static void Cull(All11 cullMode)
 {
     if (_cull != cullMode)
     {
         _cull = cullMode;
         GL11.Enable(_cull);
     }
 }
예제 #11
0
        protected static void BuildMipmaps(PixelBox data)
        {
            int      width  = 0;
            int      height = 0;
            int      logW   = 0;
            int      logH   = 0;
            int      level  = 0;
            PixelBox scaled = data;

            scaled.Data   = data.Data;
            scaled.Left   = data.Left;
            scaled.Right  = data.Right;
            scaled.Top    = data.Top;
            scaled.Bottom = data.Bottom;
            scaled.Front  = data.Front;
            scaled.Back   = data.Back;

            All format   = GLESPixelUtil.GetGLOriginFormat(data.Format);
            All dataType = GLESPixelUtil.GetGLOriginDataType(data.Format);

            width  = data.Width;
            height = data.Height;

            logW  = ComputeLog(width);
            logH  = ComputeLog(height);
            level = (logW > logH ? logW : logH);

            for (int mip = 0; mip <= level; mip++)
            {
                format   = GLESPixelUtil.GetGLOriginFormat(scaled.Format);
                dataType = GLESPixelUtil.GetGLOriginDataType(scaled.Format);

                OpenGL.TexImage2D(All.Texture2D, mip, (int)format, width, height, 0, format, dataType, scaled.Data);

                GLESConfig.GlCheckError(null);

                if (mip != 0)
                {
                    scaled.Data = IntPtr.Zero;
                }

                if (width > 1)
                {
                    width = width / 2;
                }
                if (height > 1)
                {
                    height = height / 2;
                }

                int sizeInBytes = PixelUtil.GetMemorySize(width, height, 1, data.Format);
                scaled = new PixelBox(width, height, 1, data.Format);
                var dataarr = new byte[sizeInBytes];
                scaled.Data = Memory.PinObject(dataarr);
                Image.Scale(data, scaled, ImageFilter.Linear);
            }
        }
예제 #12
0
        /// <summary>
        /// </summary>
        /// <param name="disposeManagedResources"> </param>
        protected override void dispose(bool disposeManagedResources)
        {
            // Unregister with group manager
            ResourceGroupManager.Instance.UnregisterResourceManager(base.ResourceType);
            // Delete warning texture
            OpenGL.DeleteTextures(1, ref this._warningTextureID);
            GLESConfig.GlCheckError(this);

            base.dispose(disposeManagedResources);
        }
예제 #13
0
        public static void BlendFunc(All11 source, All11 dest)
        {
            if (source != _blendFuncSource && dest != _blendFuncDest)
            {
                source = _blendFuncSource;
                dest   = _blendFuncDest;

                GL11.BlendFunc(source, dest);
            }
        }
예제 #14
0
        private static All11 _cull = All11.Ccw; // default

        public static void TextureCoordArray(bool enable)
        {
            if (enable && (_textureCoordArray != GLStateEnabled.True))
            {
                GL11.EnableClientState(All11.TextureCoordArray);
            }
            else
            {
                GL11.DisableClientState(All11.TextureCoordArray);
            }
        }
예제 #15
0
 public static void DepthTest(bool enable)
 {
     if (enable && (_depthTest != GLStateEnabled.True))
     {
         GL11.Enable(All11.DepthTest);
     }
     else
     {
         GL11.Disable(All11.DepthTest);
     }
 }
예제 #16
0
 public static void NormalArray(bool enable)
 {
     if (enable && (_normalArray != GLStateEnabled.True))
     {
         GL11.EnableClientState(All11.NormalArray);
     }
     else
     {
         GL11.DisableClientState(All11.NormalArray);
     }
 }
예제 #17
0
 public static void Textures2D(bool enable)
 {
     if (enable && (_textures2D != GLStateEnabled.True))
     {
         GL11.Enable(All11.Texture2D);
     }
     else
     {
         GL11.Disable(All11.Texture2D);
     }
 }
예제 #18
0
 public static void ColorArray(bool enable)
 {
     if (enable && (_colorArray != GLStateEnabled.True))
     {
         GL11.EnableClientState(All11.ColorArray);
     }
     else
     {
         GL11.DisableClientState(All11.ColorArray);
     }
 }
예제 #19
0
 public static void VertexArray(bool enable)
 {
     if (enable && (_vertextArray != GLStateEnabled.True))
     {
         GL11.EnableClientState(All11.VertexArray);
     }
     else
     {
         GL11.DisableClientState(All11.VertexArray);
     }
 }
예제 #20
0
        /// <summary>
        ///   Try a certain FBO format, and return the status. Also sets mDepthRB and mStencilRB.
        /// </summary>
        /// <param name="depthFormat"> </param>
        /// <param name="stencilFormat"> </param>
        /// <returns> true if this combo is supported, false if not </returns>
        private bool TryFormat(All depthFormat, All stencilFormat)
        {
            int status = 0, depthRB = 0, stencilRB = 0;

            if (depthFormat != 0)
            {
                /// Generate depth renderbuffer
                OpenGLOES.GenRenderbuffers(1, ref depthRB);

                /// Bind it to FBO;
                OpenGLOES.RenderbufferStorage(All.RenderbufferOes, depthFormat, ProbeSize, ProbeSize);

                /// Attach depth
                OpenGLOES.FramebufferRenderbuffer(All.FramebufferOes, All.DepthAttachmentOes, All.RenderbufferOes, depthRB);
            }
            // Stencil buffers aren't available on iPhone
            if (stencilFormat != 0)
            {
                /// Generate stencil renderbuffer
                OpenGLOES.GenRenderbuffers(1, ref stencilRB);

                //bind it to FBO
                OpenGLOES.BindRenderbuffer(All.RenderbufferOes, stencilRB);

                /// Allocate storage for stencil buffer
                OpenGLOES.RenderbufferStorage(All.RenderbufferOes, stencilFormat, ProbeSize, ProbeSize);

                /// Attach stencil
                OpenGLOES.FramebufferRenderbuffer(All.FramebufferOes, All.StencilAttachmentOes, All.RenderbufferOes, stencilRB);
            }

            status = (int)OpenGLOES.CheckFramebufferStatus(All.FramebufferOes);

            OpenGLOES.FramebufferRenderbuffer(All.FramebufferOes, All.DepthAttachmentOes, All.RenderbufferOes, depthRB);
            OpenGLOES.FramebufferRenderbuffer(All.FramebufferOes, All.StencilAttachmentOes, All.RenderbufferOes, stencilRB);

            if (depthRB != 0)
            {
                OpenGLOES.DeleteRenderbuffers(1, ref depthRB);
            }

            if (stencilRB != 0)
            {
                OpenGLOES.DeleteRenderbuffers(1, ref stencilRB);
            }

            //Clear OpenGL Errors create because of the evaluation
            while (OpenGL.GetError() != All.NoError)
            {
                ;
            }

            return(status == (int)All.FramebufferCompleteOes);
        }
예제 #21
0
        internal void GenerateBuffer <T>() where T : struct, IVertexType
        {
            var vd = VertexDeclaration.FromType(_type);

            _size = vd.VertexStride * ((T[])_buffer).Length;

            All11 bufferUsage = (_bufferUsage == BufferUsage.WriteOnly) ? All11.StaticDraw : All11.DynamicDraw;

            GL11.GenBuffers(1, out _bufferStore);
            GL11.BindBuffer(All11.ArrayBuffer, _bufferStore);
            GL11.BufferData <T>(All11.ArrayBuffer, (IntPtr)_size, (T[])_buffer, bufferUsage);
        }
예제 #22
0
        /// <summary>
        /// </summary>
        /// <param name="name"> </param>
        /// <param name="target"> </param>
        /// <param name="id"> </param>
        /// <param name="width"> </param>
        /// <param name="height"> </param>
        /// <param name="format"> </param>
        /// <param name="face"> </param>
        /// <param name="level"> </param>
        /// <param name="usage"> </param>
        /// <param name="crappyCard"> </param>
        /// <param name="writeGamma"> </param>
        /// <param name="fsaa"> </param>
        public GLESTextureBuffer(string basename, All targetfmt, int id, int width, int height, int format, int face, int level, BufferUsage usage, bool crappyCard, bool writeGamma, int fsaa)
            : base(0, 0, 0, Media.PixelFormat.Unknown, usage)
        {
            this._target         = targetfmt;
            this._textureId      = id;
            this._face           = face;
            this._level          = level;
            this._softwareMipmap = crappyCard;

            GLESConfig.GlCheckError(this);
            OpenGL.BindTexture(All.Texture2D, this._textureId);
            GLESConfig.GlCheckError(this);

            // Get face identifier
            this._faceTarget = this._target;

            // TODO verify who get this
            Width  = width;
            Height = height;
            Depth  = 1;

            _glInternalFormat = (All)format;
            Format            = GLESPixelUtil.GetClosestAxiomFormat(_glInternalFormat);

            RowPitch    = Width;
            SlicePitch  = Height * Width;
            sizeInBytes = PixelUtil.GetMemorySize(Width, Height, Depth, Format);

            // Set up a pixel box
            _buffer = new PixelBox(Width, Height, Depth, Format);
            if (Width == 0 || Height == 0 || Depth == 0)
            {
                /// We are invalid, do not allocate a buffer
                return;
            }

            // Is this a render target?
            if (((int)Usage & (int)TextureUsage.RenderTarget) != 0)
            {
                // Create render target for each slice
                for (int zoffset = 0; zoffset < Depth; zoffset++)
                {
                    string name = string.Empty;
                    name = "rtt/" + GetHashCode() + "/" + basename;
                    var target = new GLESSurfaceDescription();
                    target.Buffer  = this;
                    target.ZOffset = zoffset;
                    RenderTexture trt = GLESRTTManager.Instance.CreateRenderTexture(name, target, writeGamma, fsaa);
                    this._sliceTRT.Add(trt);
                    Root.Instance.RenderSystem.AttachRenderTarget(this._sliceTRT[zoffset]);
                }
            }
        }
예제 #23
0
        /// <summary>
        /// </summary>
        /// <param name="disposeManagedResources"> </param>
        protected override void dispose(bool disposeManagedResources)
        {
            if (!IsDisposed)
            {
                if (disposeManagedResources)
                {
                    OpenGL.DeleteBuffers(1, ref this._bufferId);
                    GLESConfig.GlCheckError(this);
                }
            }

            // If it is available, make the call to the
            // base class's Dispose(Boolean) method
            base.dispose(disposeManagedResources);
        }
예제 #24
0
        public void LoadFromImage(Bitmap bmp)
        {
            _bmp = bmp;

            GL.BindTexture(All.Texture2D, TexHandle);

            //GL.TexEnv(All.TextureEnv, All.TextureEnvMode, (float)All.Modulate);

            // setup texture parameters
            GL.TexParameterx(All.Texture2D, All.TextureMagFilter, (int)All.Linear);
            GL.TexParameterx(All.Texture2D, All.TextureMinFilter, (int)All.Linear);
            GL.TexParameterx(All.Texture2D, All.TextureWrapS, (int)All.ClampToEdge);
            GL.TexParameterx(All.Texture2D, All.TextureWrapT, (int)All.ClampToEdge);

            Android.Opengl.GLUtils.TexImage2D((int)All.Texture2D, 0, bmp, 0);
        }
예제 #25
0
        public override void Render()
        {
            base.BeforeRender();

            GL.Disable(All.Texture2D);
            GL.Color4(FillColor.R, FillColor.G, FillColor.B, FillColor.A);
            GL.Enable(All.Points);

            GL.EnableClientState(All.VertexArray);

            var vArray = new float[Stars.Count * 3];

            for (var i = 0; i < Stars.Count; i++)
            {
                vArray[i * 3 + 0] = (float)(Stars[i].X + Position.X);
                vArray[i * 3 + 1] = (float)(Stars[i].Y + Position.Y);
                vArray[i * 3 + 2] = (float)(Stars[i].Z + Position.Z);
            }

            // pin the data, so that GC doesn't move them, while used
            // by native code
            unsafe
            {
                fixed(float *pv = vArray)
                {
                    GL.VertexPointer(3, All.Float, 0, new IntPtr(pv));
                    GL.DrawArrays(All.Points, 0, 4);
                    GL.Finish();
                }
            }

            GL.Enable(All.Texture2D);

            GL.DisableClientState(All.VertexArray);

            base.AfterRender();
        }
예제 #26
0
        /// <summary>
        /// </summary>
        protected override void UpdateFromShadow()
        {
            if (useShadowBuffer && shadowUpdated && !suppressHardwareUpdate)
            {
                var srcData = shadowBuffer.Lock(lockStart, lockSize, BufferLocking.ReadOnly);
                OpenGL.BindBuffer(All.ElementArrayBuffer, this._bufferId);
                GLESConfig.GlCheckError(this);

                var srcPtr = new IntPtr(srcData.Ptr);
                // Update whole buffer if possible, otherwise normal
                if (lockStart == 0 && lockSize == sizeInBytes)
                {
                    OpenGL.BufferData(All.ElementArrayBuffer, new IntPtr(sizeInBytes), srcPtr, GLESHardwareBufferManager.GetGLUsage(usage));
                    GLESConfig.GlCheckError(this);
                }
                else
                {
                    OpenGL.BufferSubData(All.ElementArrayBuffer, new IntPtr(lockStart), new IntPtr(lockSize), srcPtr);
                    GLESConfig.GlCheckError(this);
                }
                shadowBuffer.Unlock();
                shadowUpdated = false;
            }
        }
예제 #27
0
        /// <summary>
        /// </summary>
        /// <param name="offset"> </param>
        /// <param name="length"> </param>
        /// <param name="src"> </param>
        /// <param name="discardWholeBuffer"> </param>
        public override void WriteData(int offset, int length, BufferBase src, bool discardWholeBuffer)
        {
            OpenGL.BindBuffer(All.ElementArrayBuffer, this._bufferId);
            GLESConfig.GlCheckError(this);
            // Update the shadow buffer
            if (useShadowBuffer)
            {
                var destData = shadowBuffer.Lock(offset, length, discardWholeBuffer ? BufferLocking.Discard : BufferLocking.Normal);
                Memory.Copy(src, destData, length);
                shadowBuffer.Unlock();
            }

            var srcPtr = src.Ptr;

            if (offset == 0 && length == sizeInBytes)
            {
                OpenGL.BufferData(All.ElementArrayBuffer, new IntPtr(sizeInBytes), ref srcPtr, GLESHardwareBufferManager.GetGLUsage(usage));
                GLESConfig.GlCheckError(this);
            }
            else
            {
                if (discardWholeBuffer)
                {
                    OpenGL.BufferData(All.ElementArrayBuffer, new IntPtr(sizeInBytes), IntPtr.Zero, GLESHardwareBufferManager.GetGLUsage(usage));
                    GLESConfig.GlCheckError(this);
                }
                // Now update the real buffer
                OpenGL.BufferSubData(All.ElementArrayBuffer, new IntPtr(offset), new IntPtr(length), ref srcPtr);
                GLESConfig.GlCheckError(this);
            }

            if (src.Ptr != srcPtr)
            {
                LogManager.Instance.Write("[GLES2] HardwareIndexBuffer.WriteData - buffer pointer modified by GL.BufferData.");
            }
        }
예제 #28
0
 /// <summary>
 /// </summary>
 protected override void UnlockImpl()
 {
     if (this._lockedToScratch)
     {
         if (this._scratchUploadOnUnlock)
         {
             // have to write the data back to vertex buffer
             this.WriteData(this._scratchOffset, this._scratchSize, this._scratchPtr, this._scratchOffset == 0 && this._scratchSize == sizeInBytes);
         }
         // deallocate from scratch buffer
         ((GLESHardwareBufferManager)HardwareBufferManager.Instance).DeallocateScratch(this._scratchPtr);
         this._lockedToScratch = false;
     }
     else
     {
         OpenGL.BindBuffer(All.ElementArrayBuffer, this._bufferId);
         GLESConfig.GlCheckError(this);
         if (!OpenGLOES.UnmapBuffer(All.ElementArrayBuffer))
         {
             throw new AxiomException("Buffer data corrupted, please reload");
         }
     }
     isLocked = false;
 }
예제 #29
0
        /// <summary>
        /// </summary>
        /// <param name="offset"> </param>
        /// <param name="length"> </param>
        /// <param name="locking"> </param>
        /// <returns> </returns>
        protected override BufferBase LockImpl(int offset, int length, BufferLocking locking)
        {
            All access = 0;

            if (isLocked)
            {
                throw new AxiomException("Invalid attempt to lock an index buffer that has already been locked");
            }

            BufferBase retPtr = null;

            if (length < MapBufferThreshold)
            {
                retPtr = ((GLESHardwareBufferManager)HardwareBufferManager.Instance).AllocateScratch(length);
                if (retPtr != null)
                {
                    this._lockedToScratch       = true;
                    this._scratchOffset         = offset;
                    this._scratchSize           = length;
                    this._scratchPtr            = retPtr;
                    this._scratchUploadOnUnlock = (locking != BufferLocking.ReadOnly);

                    if (locking != BufferLocking.Discard)
                    {
                        this.ReadData(offset, length, retPtr);
                    }
                }
            }
            else
            {
                throw new AxiomException("Invalid Buffer lockSize");
            }

            if (retPtr == null)
            {
                OpenGL.BindBuffer(All.ElementArrayBuffer, this._bufferId);
                GLESConfig.GlCheckError(this);
                // Use glMapBuffer
                if (locking == BufferLocking.Discard)
                {
                    OpenGL.BufferData(All.ElementArrayBuffer, new IntPtr(sizeInBytes), IntPtr.Zero, GLESHardwareBufferManager.GetGLUsage(usage));
                    GLESConfig.GlCheckError(this);
                }
                if ((usage & BufferUsage.WriteOnly) != 0)
                {
                    access = All.WriteOnlyOes;
                }

                IntPtr pBuffer = OpenGLOES.MapBuffer(All.ElementArrayBuffer, access);
                GLESConfig.GlCheckError(this);
                if (pBuffer == IntPtr.Zero)
                {
                    throw new AxiomException("Index Buffer: Out of memory");
                }
                unsafe
                {
                    // return offset
                    retPtr = BufferBase.Wrap(pBuffer, sizeInBytes);
                }

                this._lockedToScratch = false;
            }
            isLocked = true;

            return(retPtr);
        }
예제 #30
0
 public void Dispose()
 {
     GL11.GenBuffers(0, out _bufferStore);
 }