protected override PixelBox LockImpl(BasicBox lockBox, BufferLocking options)
        {
            //Entering critical section
            LockDeviceAccess();

            // Check for misuse
            if (((int)usage & (int)TextureUsage.RenderTarget) != 0)
            {
                throw new AxiomException(
                          "DirectX does not allow locking of or directly writing to RenderTargets. Use BlitFromMemory if you need the contents.");
            }

            // Set locking flags according to options
            var flags = D3D9Helper.ConvertEnum(options, usage);

            if (this.mapDeviceToBufferResources.Count == 0)
            {
                throw new AxiomException("There are no resources attached to this pixel buffer !!");
            }

            lockedBox      = lockBox;
            this.lockFlags = flags;

            var bufferResources = this.mapDeviceToBufferResources.First().Value;

            // Lock the source buffer.
            var lockedBuf = LockBuffer(bufferResources, lockBox, flags);

            //Leaving critical section
            UnlockDeviceAccess();

            return(lockedBuf);
        }
        protected override IntPtr LockImpl(int offset, int length, BufferLocking locking)
        {
            //Debug.Assert( !handle.IsAllocated, "Internal error, data being pinned twice." );

            // return the offset into the array as a pointer
            return(GetDataPointer(offset));
        }
        /// <summary>
        ///		Used to lock a vertex buffer in hardware memory in order to make modifications.
        /// </summary>
        /// <param name="offset">Starting index in the buffer to lock.</param>
        /// <param name="length">Nunber of bytes to lock after the offset.</param>
        /// <param name="locking">Specifies how to lock the buffer.</param>
        /// <returns>An array of the <code>System.Type</code> associated with this VertexBuffer.</returns>
        public virtual IntPtr Lock(int offset, int length, BufferLocking locking)
        {
            Debug.Assert(!isLocked, "Cannot lock this buffer because it is already locked.");

            IntPtr data = IntPtr.Zero;

            if (useShadowBuffer)
            {
                if (locking != BufferLocking.ReadOnly)
                {
                    // we have to assume a read / write lock so we use the shadow buffer
                    // and tag for sync on Unlock()
                    shadowUpdated = true;
                }

                data = shadowBuffer.Lock(offset, length, locking);
            }
            else
            {
                // lock the real deal and flag it as locked
                data     = this.LockImpl(offset, length, locking);
                isLocked = true;
            }

            lockStart = offset;
            lockSize  = length;

            return(data);
        }
        protected override IntPtr LockImpl(int offset, int length, BufferLocking locking)
        {
            isLocked = true;

            // return the offset into the array as a pointer
            return(Marshal.UnsafeAddrOfPinnedArrayElement(data, offset));
        }
Beispiel #5
0
        public virtual BufferBase Lock(int offset, int length, BufferLocking locking)
        {
            Debug.Assert(!IsLocked, "Cannot lock this buffer because it is already locked.");
            Debug.Assert(offset >= 0 && (offset + length) <= this.sizeInBytes,
                         "The data area to be locked exceeds the buffer.");

            BufferBase ret;             // = IntPtr.Zero;

            if (this.useShadowBuffer)
            {
                if (locking != BufferLocking.ReadOnly)
                {
                    // we have to assume a read / write lock so we use the shadow buffer
                    // and tag for sync on Unlock()
                    this.shadowUpdated = true;
                }

                ret = this.shadowBuffer.Lock(offset, length, locking);
            }
            else
            {
                // lock the real deal and flag it as locked
                ret           = LockImpl(offset, length, locking);
                this.isLocked = true;
            }

            this.lockStart = offset;
            this.lockSize  = length;
            return(ret);
        }
 public static D3D.LockFlags ConvertEnum(BufferLocking locking, BufferUsage usage)
 {
     D3D.LockFlags d3dLockFlags = 0;
     if (locking == BufferLocking.Discard)
     {
         // D3D doesn't like discard or no_overwrite on non-dynamic buffers
         if ((usage & BufferUsage.Dynamic) != 0)
         {
             d3dLockFlags |= D3D.LockFlags.Discard;
         }
     }
     else if (locking == BufferLocking.ReadOnly)
     {
         // D3D debug runtime doesn't like you locking managed buffers readonly
         // when they were created with write-only (even though you CAN read
         // from the software backed version)
         if ((usage & BufferUsage.WriteOnly) == 0)
         {
             d3dLockFlags |= D3D.LockFlags.ReadOnly;
         }
     }
     else if (locking == BufferLocking.NoOverwrite)
     {
         // D3D doesn't like discard or no_overwrite on non-dynamic buffers
         if ((usage & BufferUsage.Dynamic) != 0)
         {
             d3dLockFlags |= D3D.LockFlags.NoOverwrite;
         }
     }
     return(d3dLockFlags);
 }
        ///<summary>
        ///    Internal implementation of <see cref="HardwareBuffer.Lock"/>.
        ///</summary>
        protected override PixelBox LockImpl(BasicBox lockBox, BufferLocking options)
        {
            _lockedBox = lockBox;
            // Set extents and format
            var rval        = new PixelBox(lockBox, Format);
            var sizeInBytes = PixelUtil.GetMemorySize(lockBox.Width, lockBox.Height, lockBox.Depth,
                                                      XnaHelper.Convert(surface.Format));

            if (_bufferBytes == null || _bufferBytes.Length != sizeInBytes)
            {
                _bufferBytes = new byte[sizeInBytes];
#if !SILVERLIGHT
                if (surface != null)
                {
                    surface.GetData(mipLevel, XnaHelper.ToRectangle(lockBox), _bufferBytes, 0, _bufferBytes.Length);
                }
                else if (cube != null)
                {
                    cube.GetData(face, mipLevel, XnaHelper.ToRectangle(lockBox), _bufferBytes, 0, _bufferBytes.Length);
                }
                else
                {
                    volume.GetData(mipLevel, lockBox.Left, lockBox.Top, lockBox.Right, lockBox.Bottom,
                                   lockBox.Front, lockBox.Back, _bufferBytes, 0, _bufferBytes.Length);
                }
#endif
            }

            rval.Data = BufferBase.Wrap(_bufferBytes);

            return(rval);
        }
 /// <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)
 {
     _offset  = offset;
     _length  = length;
     _locking = locking;
     return(BufferBase.Wrap(_bufferBytes).Offset(offset));
 }
        public override IntPtr Lock(int offset, int length, BufferLocking locking)
        {
            //Debug.Assert( !isLocked, "Cannot lock this buffer because it is already locked." );
            Debug.Assert(offset >= 0 && (offset + length) <= sizeInBytes, "The data area to be locked exceeds the buffer.");

            isLocked = true;

            return(LockImpl(offset, length, locking));
        }
        protected override IntPtr LockImpl(int offset, int length, BufferLocking locking)
        {
            isLocked = true;

            // return the offset into the array as a pointer
            // return Marshal.UnsafeAddrOfPinnedArrayElement(data, offset);
            handle = GCHandle.Alloc(data, GCHandleType.Pinned);
            return(handle.AddrOfPinnedObject());
        }
Beispiel #11
0
        protected override BufferBase LockImpl(int offset, int length, BufferLocking options)
        {
            Debug.Assert(!IsLocked, "Cannot lock this buffer, it is already locked!");
            Debug.Assert(offset == 0 && length == sizeInBytes, "Cannot lock memory region, must lock box or entire buffer");

            var myBox = new BasicBox(0, 0, 0, Width, Height, Depth);
            var rv    = Lock(myBox, options);

            return(rv.Data);
        }
Beispiel #12
0
 /// <summary>
 /// </summary>
 /// <param name="lockBox"> </param>
 /// <param name="options"> </param>
 /// <returns> </returns>
 protected override PixelBox LockImpl(BasicBox lockBox, BufferLocking options)
 {
     AllocateBuffer();
     if (options != BufferLocking.Discard && (Usage & BufferUsage.WriteOnly) == 0)
     {
         // Download the old contents of the texture
         Download(this._buffer);
     }
     this._currentLocking = options;
     return(this._buffer.GetSubVolume(lockBox));
 }
Beispiel #13
0
 protected override Media.PixelBox LockImpl(Media.BasicBox lockBox, BufferLocking options)
 {
     this.AllocateBuffer();
     if (options != BufferLocking.Discard && (usage & BufferUsage.WriteOnly) == 0)
     {
         //Downoad the old contents of the texture
         this.Download(this.Buffer);
     }
     this.CurrentLockOptions = options;
     lockedBox = lockBox;
     return(this.Buffer.GetSubVolume(lockBox));
 }
        /// <summary>
        ///
        /// </summary>
        /// <param name="offset"></param>
        /// <param name="length"></param>
        /// <param name="locking"></param>
        /// <returns></returns>
        protected override IntPtr LockImpl(int offset, int length, BufferLocking locking)
        {
            int access = 0;

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

            // bind this buffer
            Gl.glBindBufferARB(Gl.GL_ELEMENT_ARRAY_BUFFER_ARB, bufferID);

            if (locking == BufferLocking.Discard)
            {
                // commented out to fix ATI issues

                /*Gl.glBufferDataARB(Gl.GL_ELEMENT_ARRAY_BUFFER_ARB,
                 *   sizeInBytes,
                 *   IntPtr.Zero,
                 *   GLHelper.ConvertEnum(usage));
                 */

                // find out how we shall access this buffer
                access = (usage == BufferUsage.Dynamic) ?
                         Gl.GL_READ_WRITE_ARB : Gl.GL_WRITE_ONLY_ARB;
            }
            else if (locking == BufferLocking.ReadOnly)
            {
                if (usage == BufferUsage.WriteOnly)
                {
                    LogManager.Instance.Write("Invalid attempt to lock a write-only vertex buffer as read-only.");
                }

                access = Gl.GL_READ_ONLY_ARB;
            }
            else if (locking == BufferLocking.Normal || locking == BufferLocking.NoOverwrite)
            {
                access = (usage == BufferUsage.Dynamic) ?
                         Gl.GL_READ_WRITE_ARB : Gl.GL_WRITE_ONLY_ARB;
            }

            IntPtr ptr = Gl.glMapBufferARB(Gl.GL_ELEMENT_ARRAY_BUFFER_ARB, access);

            if (ptr == IntPtr.Zero)
            {
                throw new Exception("GL Vertex Buffer: Out of memory");
            }

            isLocked = true;

            return(new IntPtr(ptr.ToInt32() + offset));
        }
        public static D3D.LockFlags ConvertEnum(BufferLocking locking, BufferUsage usage)
        {
            D3D.LockFlags ret = 0;
            if (locking == BufferLocking.Discard)
            {
#if !NO_OGRE_D3D_MANAGE_BUFFERS
                // Only add the discard flag for dynamic usgae and default pool
                if ((usage & BufferUsage.Dynamic) != 0 &&
                    (usage & BufferUsage.Discardable) != 0)
                {
                    ret |= D3D.LockFlags.Discard;
                }
#else
                // D3D doesn't like discard or no_overwrite on non-dynamic buffers
                if ((usage & BufferUsage.Dynamic) != 0)
                {
                    ret |= D3D.LockFlags.Discard;
                }
#endif
            }
            if (locking == BufferLocking.ReadOnly)
            {
                // D3D debug runtime doesn't like you locking managed buffers readonly
                // when they were created with write-only (even though you CAN read
                // from the software backed version)
                if ((usage & BufferUsage.WriteOnly) == 0)
                {
                    ret |= D3D.LockFlags.ReadOnly;
                }
            }
            if (locking == BufferLocking.NoOverwrite)
            {
#if !NO_OGRE_D3D_MANAGE_BUFFERS
                // Only add the nooverwrite flag for dynamic usgae and default pool
                if ((usage & BufferUsage.Dynamic) != 0 &&
                    (usage & BufferUsage.Discardable) != 0)
                {
                    ret |= D3D.LockFlags.NoOverwrite;
                }
#else
                // D3D doesn't like discard or no_overwrite on non-dynamic buffers
                if ((usage & BufferUsage.Dynamic) != 0)
                {
                    ret |= D3D.LockFlags.NoOverwrite;
                }
#endif
            }

            return(ret);
        }
        protected override BufferBase LockImpl(int offset, int length, BufferLocking options)
        {
            //Entering critical section
            this.LockDeviceAccess();

            if (options != BufferLocking.ReadOnly)
            {
                foreach (var it in this._mapDeviceToBufferResources)
                {
                    var bufferResources = it.Value;
                    bufferResources.IsOutOfDate = true;

                    if (bufferResources.LockLength > 0)
                    {
                        var highPoint = Math.Utility.Max(offset + length, bufferResources.LockOffset + bufferResources.LockLength);
                        bufferResources.LockOffset = Math.Utility.Min(bufferResources.LockOffset, offset);
                        bufferResources.LockLength = highPoint - bufferResources.LockOffset;
                    }
                    else
                    {
                        if (offset < bufferResources.LockOffset)
                        {
                            bufferResources.LockOffset = offset;
                        }

                        if (length > bufferResources.LockLength)
                        {
                            bufferResources.LockLength = length;
                        }
                    }

                    if (bufferResources.LockOptions != BufferLocking.Discard)
                    {
                        bufferResources.LockOptions = options;
                    }
                }
            }

            //Leaving critical section
            this.UnlockDeviceAccess();

            return(this._systemMemoryBuffer + offset);
        }
        public static D3D.LockFlags ConvertEnum(BufferLocking locking)
        {
            D3D.LockFlags d3dLockFlags = 0;

            if (locking == BufferLocking.Discard)
            {
                d3dLockFlags |= D3D.LockFlags.Discard;
            }
            if (locking == BufferLocking.ReadOnly)
            {
                d3dLockFlags |= D3D.LockFlags.ReadOnly;
            }
            if (locking == BufferLocking.NoOverwrite)
            {
                d3dLockFlags |= D3D.LockFlags.NoOverwrite;
            }

            return(d3dLockFlags);
        }
Beispiel #18
0
        public virtual PixelBox Lock(BasicBox lockBox, BufferLocking options)
        {
            if (useShadowBuffer)
            {
                if (options != BufferLocking.ReadOnly)
                {
                    // we have to assume a read / write lock so we use the shadow buffer
                    // and tag for sync on unlock()
                    shadowUpdated = true;
                }
                this.currentLock = ((HardwarePixelBuffer)shadowBuffer).Lock(lockBox, options);
            }
            else
            {
                // Lock the real buffer if there is no shadow buffer
                this.currentLock = LockImpl(lockBox, options);
                isLocked         = true;
            }

            return(this.currentLock);
        }
Beispiel #19
0
        ///<summary>
        ///    Copies a box from another PixelBuffer to a region of the
        ///    this PixelBuffer.
        ///</summary>
        ///<param name="src">Source/dest pixel buffer</param>
        ///<param name="srcBox">Image.BasicBox describing the source region in this buffer</param>
        ///<param name="dstBox">Image.BasicBox describing the destination region in this buffer</param>
        ///<remarks>
        ///    The source and destination regions dimensions don't have to match, in which
        ///    case scaling is done. This scaling is generally done using a bilinear filter in hardware,
        ///    but it is faster to pass the source image in the right dimensions.
        ///    Only call this function when both  buffers are unlocked.
        ///</remarks>
        public virtual void Blit(HardwarePixelBuffer src, BasicBox srcBox, BasicBox dstBox)
        {
            if (IsLocked || src.IsLocked)
            {
                throw new Exception("Source and destination buffer may not be locked!  In HardwarePixelBuffer.Blit");
            }
            if (src == this)
            {
                throw new Exception("Source must not be the same object, in HardwarePixelBuffer.Blit");
            }

            PixelBox srclock = src.Lock(srcBox, BufferLocking.ReadOnly);

            BufferLocking method = BufferLocking.Normal;

            if (dstBox.Left == 0 && dstBox.Top == 0 && dstBox.Front == 0 &&
                dstBox.Right == width && dstBox.Bottom == height &&
                dstBox.Back == depth)
            {
                // Entire buffer -- we can discard the previous contents
                method = BufferLocking.Discard;
            }

            PixelBox dstlock = Lock(dstBox, method);

            if (dstlock.Width != srclock.Width || dstlock.Height != srclock.Height || dstlock.Depth != srclock.Depth)
            {
                // Scaling desired
                throw new Exception("Image scaling not yet implemented; in HardwarePixelBuffer.Blit");
            }
            // Image.Scale(srclock, dstlock);
            else
            {
                // No scaling needed
                PixelUtil.BulkPixelConversion(srclock, dstlock);
            }

            Unlock();
            src.Unlock();
        }
        /// <summary>
        ///     Updates the real buffer from the shadow buffer, if required.
        /// </summary>
        protected void UpdateFromShadow()
        {
            if (useShadowBuffer && shadowUpdated && !suppressHardwareUpdate)
            {
                // do this manually to avoid locking problems
                IntPtr src = shadowBuffer.LockImpl(lockStart, lockSize, BufferLocking.ReadOnly);

                // Lock with discard if the whole buffer was locked, otherwise normal
                BufferLocking locking =
                    (lockStart == 0 && lockSize == sizeInBytes) ? BufferLocking.Discard : BufferLocking.Normal;

                IntPtr dest = this.LockImpl(lockStart, lockSize, locking);

                // copy the data in directly
                Memory.Copy(src, dest, lockSize);

                // unlock both buffers to commit the write
                this.UnlockImpl();
                shadowBuffer.UnlockImpl();

                shadowUpdated = false;
            }
        }
Beispiel #21
0
 protected abstract PixelBox LockImpl(BasicBox lockBox, BufferLocking options);
 /// <summary>
 /// 
 /// </summary>
 /// <param name="offset"></param>
 /// <param name="length"></param>
 /// <param name="locking"></param>
 /// <returns></returns>
 protected override IntPtr LockImpl(int offset, int length, BufferLocking locking)
 {
     D3D.LockFlags d3dLocking = D3DHelper.ConvertEnum(locking, usage);
     Microsoft.DirectX.GraphicsStream s = d3dBuffer.Lock(offset, length, d3dLocking);
     return s.InternalData;
 }
		/// <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 )
		{
			return this._dataPtr + offset;
		}
		public override IntPtr Lock( int offset, int length, BufferLocking locking )
		{
		    Debug.Assert( !isLocked );
			isLocked = true;
			var ret = Memory.PinObject( _mpData );
		    unsafe
		    {
		        var v = (byte*)ret.ToPointer();
		        v += offset;
		        ret = new IntPtr(v);
		    }
			return ret;
		}
        protected override IntPtr LockImpl(int offset, int length, BufferLocking locking)
        {
            isLocked = true;

            // return the offset into the array as a pointer
            return Marshal.UnsafeAddrOfPinnedArrayElement(data, offset);
        }
        ///<summary>
        ///    Internal implementation of <see cref="HardwareBuffer.Lock"/>.
        ///</summary>
        protected override PixelBox LockImpl( BasicBox lockBox, BufferLocking options )
        {
            _lockedBox = lockBox;
            // Set extents and format
            var rval = new PixelBox( lockBox, Format );
            var sizeInBytes = PixelUtil.GetMemorySize( lockBox.Width, lockBox.Height, lockBox.Depth,
                                                       XnaHelper.Convert( surface.Format ) );
            if ( _bufferBytes == null || _bufferBytes.Length != sizeInBytes )
            {
                _bufferBytes = new byte[sizeInBytes];
#if !SILVERLIGHT
                if ( surface != null )
                    surface.GetData( mipLevel, XnaHelper.ToRectangle( lockBox ), _bufferBytes, 0, _bufferBytes.Length );
                else if ( cube != null )
                    cube.GetData( face, mipLevel, XnaHelper.ToRectangle( lockBox ), _bufferBytes, 0, _bufferBytes.Length );
                else
                    volume.GetData( mipLevel, lockBox.Left, lockBox.Top, lockBox.Right, lockBox.Bottom,
                                    lockBox.Front, lockBox.Back, _bufferBytes, 0, _bufferBytes.Length );
#endif
            }

            rval.Data = BufferBase.Wrap( _bufferBytes );

            return rval;
        }
		protected override BufferBase LockImpl( int offset, int length, BufferLocking options )
		{
			Debug.Assert( !IsLocked, "Cannot lock this buffer, it is already locked!" );
			Debug.Assert( offset == 0 && length == sizeInBytes, "Cannot lock memory region, must lock box or entire buffer" );

			var myBox = new BasicBox( 0, 0, 0, Width, Height, Depth );
			var rv = Lock( myBox, options );
			return rv.Data;
		}
 /// <summary>
 ///     Internal implementation of Lock, which will be overridden by subclasses to provide
 ///     the core locking functionality.
 /// </summary>
 /// <param name="offset">Offset into the buffer (in bytes) to lock.</param>
 /// <param name="length">Length of the portion of the buffer (int bytes) to lock.</param>
 /// <param name="locking">Locking type.</param>
 /// <returns>IntPtr to the beginning of the locked portion of the buffer.</returns>
 protected abstract IntPtr LockImpl(int offset, int length, BufferLocking locking);
		protected override BufferBase LockImpl( int offset, int length, BufferLocking locking )
		{
			if ( IsLocked )
			{
				throw new AxiomException( "Invalid attempt to lock an index buffer that has already been locked." );
			}
			BufferBase retPtr;
			var glBufManager = ( HardwareBufferManager.Instance as GLESHardwareBufferManager );
			
			//Try to use scratch buffers for smaller buffers
			if ( length < glBufManager.MapBufferThreshold )
			{
				retPtr = glBufManager.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 )
					{
						//have to read back the data before returning the pointer
						this.ReadData( offset, length, retPtr );
					}
				}
			}
			else
			{
				throw new AxiomException( "Invalid Buffer lockSize" );
			}
			
			if ( retPtr == null )
			{
				GLenum access = GLenum.Zero;
				( Root.Instance.RenderSystem as GLESRenderSystem ).BindGLBuffer( GLenum.ArrayBuffer, this._bufferID );
				
				if ( locking == BufferLocking.Discard )
				{
					//Discard the buffer
					GL.BufferData( GLenum.ArrayBuffer, new IntPtr( sizeInBytes ), IntPtr.Zero, GLESHardwareBufferManager.GetGLUsage( usage ) );
					GLESConfig.GlCheckError( this );
				}
				if ( ( usage & BufferUsage.WriteOnly ) == BufferUsage.WriteOnly )
				{
					access = GLenum.WriteOnlyOes;
				}
				
				var pbuffer = GL.Oes.MapBuffer( GLenum.ArrayBuffer, access );
				GLESConfig.GlCheckError( this );
				
				if ( pbuffer == IntPtr.Zero )
				{
					throw new AxiomException( "Vertex Buffer: Out of memory" );
				}
				
				//return offsetted
				retPtr = BufferBase.Wrap( pbuffer, sizeInBytes ) + offset;
				this._lockedToScratch = false;
			}
			isLocked = true;
			return retPtr;
		}
        /// <summary>
        ///		Used to lock a vertex buffer in hardware memory in order to make modifications.
        /// </summary>
        /// <param name="offset">Starting index in the buffer to lock.</param>
        /// <param name="length">Nunber of bytes to lock after the offset.</param>
        /// <param name="locking">Specifies how to lock the buffer.</param>
        /// <returns>An array of the <code>System.Type</code> associated with this VertexBuffer.</returns>
        public virtual IntPtr Lock(int offset, int length, BufferLocking locking)
        {
            Debug.Assert(!isLocked, "Cannot lock this buffer because it is already locked.");

            IntPtr data = IntPtr.Zero;

            if(useShadowBuffer) {
                if(locking != BufferLocking.ReadOnly) {
                    // we have to assume a read / write lock so we use the shadow buffer
                    // and tag for sync on Unlock()
                    shadowUpdated = true;
                }

                data = shadowBuffer.Lock(offset, length, locking);
            }
            else {
                // lock the real deal and flag it as locked
                data = this.LockImpl(offset, length, locking);
                isLocked = true;
            }

            lockStart = offset;
            lockSize = length;

            return data;
        }
 public BufferStream LockStream(BufferLocking locking)
 {
     return new BufferStream(this, Lock(locking));
 }
 /// <summary>
 ///		Convenient overload to allow locking the entire buffer with only having
 ///		to supply the locking type.
 /// </summary>
 /// <param name="locking">Locking options.</param>
 /// <returns>IntPtr to the beginning of the locked region of buffer memory.</returns>
 public IntPtr Lock(BufferLocking locking)
 {
     return Lock(0, sizeInBytes, locking);
 }
 protected override BufferBase LockImpl(int offset, int length, BufferLocking locking)
 {
     Debug.Assert(!isLocked);
     base.isLocked = true;
     return(Memory.PinObject(_mpData).Offset(offset));
 }
		/// <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;
		}
 protected override IntPtr LockImpl(int offset, int length, BufferLocking locking)
 {
     return(GetDataPointer(offset));
 }
		///<summary>
		///    Internal implementation of <see cref="HardwareBuffer.Lock"/>.
		///</summary>
		unsafe protected override PixelBox LockImpl( BasicBox lockBox, BufferLocking options )
		{
			_lockedBox = lockBox;
			// Set extents and format
			PixelBox rval = new PixelBox( lockBox, Format );
			int sizeInBytes = PixelUtil.GetMemorySize( lockBox.Width, lockBox.Height, lockBox.Depth, XnaHelper.Convert( surface.Format ) );
			_bufferBytes = new byte[ sizeInBytes ];

			surface.GetData( mipLevel, XnaHelper.ToRectangle( lockBox  ), _bufferBytes, 0, _bufferBytes.Length );

			fixed ( byte* bytes = &_bufferBytes[ 0 ] )
			{
				rval.Data = new IntPtr( bytes );
			}

			return rval;
		}
		protected override BufferBase LockImpl( int offset, int length, BufferLocking options )
		{
			//Entering critical section
			this.LockDeviceAccess();

			if ( options != BufferLocking.ReadOnly )
			{
				foreach ( var it in this._mapDeviceToBufferResources )
				{
					var bufferResources = it.Value;
					bufferResources.IsOutOfDate = true;

					if ( bufferResources.LockLength > 0 )
					{
						var highPoint = Math.Utility.Max( offset + length, bufferResources.LockOffset + bufferResources.LockLength );
						bufferResources.LockOffset = Math.Utility.Min( bufferResources.LockOffset, offset );
						bufferResources.LockLength = highPoint - bufferResources.LockOffset;
					}
					else
					{
						if ( offset < bufferResources.LockOffset )
						{
							bufferResources.LockOffset = offset;
						}

						if ( length > bufferResources.LockLength )
						{
							bufferResources.LockLength = length;
						}
					}

					if ( bufferResources.LockOptions != BufferLocking.Discard )
					{
						bufferResources.LockOptions = options;
					}
				}
			}

			//Leaving critical section
			this.UnlockDeviceAccess();

			return this._systemMemoryBuffer + offset;
		}
Beispiel #38
0
 ///<summary>
 ///    Internal implementation of lock(), must be overridden in subclasses
 ///</summary>
 public abstract PixelBox LockImpl(BasicBox lockBox, BufferLocking options);
		protected abstract PixelBox LockImpl( BasicBox lockBox, BufferLocking options );
Beispiel #40
0
		public static D3D.LockFlags ConvertEnum( BufferLocking locking, BufferUsage usage )
		{
			D3D.LockFlags ret = 0;
			if ( locking == BufferLocking.Discard )
            {
#if !NO_AXIOM_D3D_MANAGE_BUFFERS
                // Only add the discard flag for dynamic usgae and default pool
				if ( ( usage & BufferUsage.Dynamic ) != 0 &&
					( usage & BufferUsage.Discardable ) != 0 )
					ret |= D3D.LockFlags.Discard;
#else
				// D3D doesn't like discard or no_overwrite on non-dynamic buffers
				if ((usage & BufferUsage.Dynamic) != 0)
					ret |= D3D.LockFlags.Discard;
#endif
			}
			if ( locking == BufferLocking.ReadOnly )
			{
				// D3D debug runtime doesn't like you locking managed buffers readonly
				// when they were created with write-only (even though you CAN read
				// from the software backed version)
				if ( ( usage & BufferUsage.WriteOnly ) == 0 )
					ret |= D3D.LockFlags.ReadOnly;

			}
			if ( locking == BufferLocking.NoOverwrite )
            {
#if !NO_AXIOM_D3D_MANAGE_BUFFERS
                // Only add the nooverwrite flag for dynamic usgae and default pool
				if ( ( usage & BufferUsage.Dynamic ) != 0 &&
					( usage & BufferUsage.Discardable ) != 0 )
					ret |= D3D.LockFlags.NoOverwrite;
#else
				// D3D doesn't like discard or no_overwrite on non-dynamic buffers
				if ((usage & BufferUsage.Dynamic) != 0)
					ret |= D3D.LockFlags.NoOverwrite;
#endif
			}

			return ret;
		}
		public virtual PixelBox Lock( BasicBox lockBox, BufferLocking options )
		{
			if ( useShadowBuffer )
			{
				if ( options != BufferLocking.ReadOnly )
				{
					// we have to assume a read / write lock so we use the shadow buffer
					// and tag for sync on unlock()
					shadowUpdated = true;
				}
				this.currentLock = ( (HardwarePixelBuffer)shadowBuffer ).Lock( lockBox, options );
			}
			else
			{
				// Lock the real buffer if there is no shadow buffer 
				this.currentLock = LockImpl( lockBox, options );
				isLocked = true;
			}

			return this.currentLock;
		}
		/// <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 )
		{
			// Only for use internally, no 'locking' as such
			return this._dataPtr + offset;
		}
 protected override IntPtr LockImpl( int offset, int length, BufferLocking locking )
 {
     throw new NotImplementedException();
 }
Beispiel #44
0
        protected override BufferBase LockImpl(int offset, int length, BufferLocking locking)
        {
            if (IsLocked)
            {
                throw new AxiomException("Invalid attempt to lock an index buffer that has already been locked.");
            }
            BufferBase retPtr;
            var        glBufManager = (HardwareBufferManager.Instance as GLES2HardwareBufferManager);

            //Try to use scratch buffers for smaller buffers
            if (length < glBufManager.MapBufferThreshold)
            {
                retPtr = glBufManager.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)
                    {
                        //have to read back the data before returning the pointer
                        this.ReadData(offset, length, retPtr);
                    }
                }
            }
            else
            {
                throw new AxiomException("Invalid Buffer lockSize");
            }

            if (retPtr == null)
            {
                GLenum access = GLenum.Zero;
                (Root.Instance.RenderSystem as GLES2RenderSystem).BindGLBuffer(GLenum.ArrayBuffer, this._bufferID);

                if (locking == BufferLocking.Discard)
                {
                    //Discard the buffer
                    GL.BufferData(GLenum.ArrayBuffer, new IntPtr(sizeInBytes), IntPtr.Zero, GLES2HardwareBufferManager.GetGLUsage(usage));
                    GLES2Config.GlCheckError(this);
                }
                if ((usage & BufferUsage.WriteOnly) == BufferUsage.WriteOnly)
                {
                    access = GLenum.WriteOnlyOes;
                }

                var pbuffer = GL.Oes.MapBuffer(GLenum.ArrayBuffer, access);
                GLES2Config.GlCheckError(this);

                if (pbuffer == IntPtr.Zero)
                {
                    throw new AxiomException("Vertex Buffer: Out of memory");
                }

                //return offsetted
                retPtr = BufferBase.Wrap(pbuffer, sizeInBytes) + offset;
                this._lockedToScratch = false;
            }
            isLocked = true;
            return(retPtr);
        }
        protected override IntPtr LockImpl(int offset, int length, BufferLocking locking)
        {
            isLocked = true;

            // return the offset into the array as a pointer
            // return Marshal.UnsafeAddrOfPinnedArrayElement(data, offset);
            handle = GCHandle.Alloc(data, GCHandleType.Pinned);
            return handle.AddrOfPinnedObject();
        }
		/// <summary>
		///
		/// </summary>
		/// <param name="offset"></param>
		/// <param name="length"></param>
		/// <param name="locking"></param>
		/// <returns></returns>
		protected override IntPtr LockImpl( int offset, int length, BufferLocking locking )
		{
			int access = 0;

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

			// bind this buffer
			Gl.glBindBufferARB( Gl.GL_ELEMENT_ARRAY_BUFFER_ARB, bufferID );

			if ( locking == BufferLocking.Discard )
			{
				// commented out to fix ATI issues
				/*Gl.glBufferDataARB(Gl.GL_ELEMENT_ARRAY_BUFFER_ARB,
					 sizeInBytes,
					 IntPtr.Zero,
					 GLHelper.ConvertEnum(usage));
				 */

				// find out how we shall access this buffer
				access = ( usage == BufferUsage.Dynamic ) ?
					Gl.GL_READ_WRITE_ARB : Gl.GL_WRITE_ONLY_ARB;
			}
			else if ( locking == BufferLocking.ReadOnly )
			{
				if ( usage == BufferUsage.WriteOnly )
				{
					LogManager.Instance.Write( "Invalid attempt to lock a write-only vertex buffer as read-only." );
				}

				access = Gl.GL_READ_ONLY_ARB;
			}
			else if ( locking == BufferLocking.Normal || locking == BufferLocking.NoOverwrite )
			{
				access = ( usage == BufferUsage.Dynamic ) ?
					Gl.GL_READ_WRITE_ARB : Gl.GL_WRITE_ONLY_ARB;
			}

			IntPtr ptr = Gl.glMapBufferARB( Gl.GL_ELEMENT_ARRAY_BUFFER_ARB, access );

			if ( ptr == IntPtr.Zero )
			{
				throw new Exception( "GL Vertex Buffer: Out of memory" );
			}

			isLocked = true;

			return new IntPtr( ptr.ToInt64() + offset );
		}
		/// <summary>
		///
		/// </summary>
		/// <param name="offset"></param>
		/// <param name="length"></param>
		/// <param name="locking"></param>
		/// <returns></returns>
		protected override IntPtr LockImpl( int offset, int length, BufferLocking locking )
		{
			_offset = offset;
			_length = length;
			fixed ( byte* bytes = &_bufferBytes[ offset ] )
			{
				return new IntPtr( bytes );
			}
		}
 /// <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 )
 {
     _offset = offset;
     _length = length;
     _locking = locking;
     return BufferBase.Wrap( _bufferBytes ).Offset( offset );
 }
		/// <summary>
		/// </summary>
		/// <param name="offset"> </param>
		/// <param name="length"> </param>
		/// <param name="locking"> </param>
		/// <returns> </returns>
		public override BufferBase Lock( int offset, int length, BufferLocking locking )
		{
			isLocked = true;
			return this._dataPtr + offset;
		}
 /// <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)
 {
     return(this._dataPtr + offset);
 }
 /// <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)
 {
     // Only for use internally, no 'locking' as such
     return(this._dataPtr + offset);
 }
Beispiel #52
0
        ///<summary>
        ///    @copydoc HardwareBuffer.Lock
        ///</summary>
        //public virtual IntPtr Lock(int offset, int length, BasicBox lockBox, BufferLocking options) {
        //    Debug.Assert(!IsLocked, "Cannot lock this buffer, it is already locked!");
        //    Debug.Assert(offset == 0 && length == sizeInBytes, "Cannot lock memory region, must lock box or entire buffer");

        //    BasicBox myBox = new BasicBox(0, 0, 0, width, height, depth);
        //    PixelBox rv = Lock(myBox, options);
        //    return rv.Data;
        //}

        ///<summary>
        ///    Internal implementation of lock(), do not override or call this
        ///    for HardwarePixelBuffer implementations, but override the previous method
        ///</summary>
        protected override IntPtr LockImpl(int offset, int length, BufferLocking options)
        {
            throw new NotImplementedException("HardwarePixelBuffer does not support this variant of LockImpl");
        }
		/// <summary>
		/// 
		/// </summary>
		/// <param name="lockBox"></param>
		/// <param name="options"></param>
		/// <returns></returns>
		protected override PixelBox LockImpl( BasicBox lockBox, BufferLocking options )
		{
			AllocateBuffer();
			if ( options != BufferLocking.Discard &&
				( Usage & BufferUsage.WriteOnly ) == 0 )
			{
				// Download the old contents of the texture
				Download( _buffer );
			}
			_currentLocking = options;
			return _buffer.GetSubVolume( lockBox );
		}
 /// <summary>
 /// </summary>
 /// <param name="offset"> </param>
 /// <param name="length"> </param>
 /// <param name="locking"> </param>
 /// <returns> </returns>
 public override BufferBase Lock(int offset, int length, BufferLocking locking)
 {
     isLocked = true;
     return(this._dataPtr + offset);
 }
		protected override PixelBox LockImpl( BasicBox lockBox, BufferLocking options )
		{
			//Entering critical section
			LockDeviceAccess();

			// Check for misuse
			if ( ( (int)usage & (int)TextureUsage.RenderTarget ) != 0 )
			{
				throw new AxiomException(
					"DirectX does not allow locking of or directly writing to RenderTargets. Use BlitFromMemory if you need the contents." );
			}

			// Set locking flags according to options
			var flags = D3D9Helper.ConvertEnum( options, usage );

			if ( this.mapDeviceToBufferResources.Count == 0 )
			{
				throw new AxiomException( "There are no resources attached to this pixel buffer !!" );
			}

			lockedBox = lockBox;
			this.lockFlags = flags;

			var bufferResources = this.mapDeviceToBufferResources.First().Value;

			// Lock the source buffer.
			var lockedBuf = LockBuffer( bufferResources, lockBox, flags );

			//Leaving critical section
			UnlockDeviceAccess();

			return lockedBuf;
		}
		///<summary>
		///    Lock a box
		///</summary>
		protected override PixelBox LockImpl( BasicBox lockBox, BufferLocking options )
		{
			// Check for misuse
			if ( ( (int)usage & (int)TextureUsage.RenderTarget ) != 0 )
				throw new Exception( "DirectX does not allow locking of or directly writing to RenderTargets. Use BlitFromMemory if you need the contents." );
			// Set extents and format
			PixelBox rval = new PixelBox( lockBox, Format );
			// Set locking flags according to options
			D3D.LockFlags flags = D3D.LockFlags.None;
			switch ( options )
			{
				case BufferLocking.Discard:
					// D3D only likes D3D.LockFlags.Discard if you created the texture with D3DUSAGE_DYNAMIC
					// debug runtime flags this up, could cause problems on some drivers
					if ( ( usage & BufferUsage.Dynamic ) != 0 )
						flags |= D3D.LockFlags.Discard;
					break;
				case BufferLocking.ReadOnly:
					flags |= D3D.LockFlags.ReadOnly;
					break;
				default:
					break;
			}

			if ( surface != null )
			{
				// Surface
				DX.DataRectangle data = null;
				try
				{
					if ( lockBox.Left == 0 && lockBox.Top == 0 &&
						 lockBox.Right == Width && lockBox.Bottom == Height )
					{
						// Lock whole surface
						data = surface.LockRectangle( flags );
					}
					else
					{
						System.Drawing.Rectangle prect = ToD3DRectangle( lockBox ); // specify range to lock
						data = surface.LockRectangle( prect, flags );
					}
				}
				catch ( Exception e )
				{
					throw new Exception( "Surface locking failed.", e );
				}

				FromD3DLock( rval, data );
			}
			else
			{
				// Volume
				D3D.Box pbox = ToD3DBox( lockBox ); // specify range to lock

				DX.DataBox data = volume.LockBox( pbox, flags );
				FromD3DLock( rval, data );
			}
			return rval;
		}
		protected override BufferBase LockImpl( int offset, int length, BufferLocking locking )
		{
			Debug.Assert( !isLocked );
			base.isLocked = true;
			return Memory.PinObject( _mpData ).Offset( offset );
		}
 ///<summary>
 ///    Internal implementation of lock(), must be overridden in subclasses
 ///</summary>
 public abstract PixelBox LockImpl(BasicBox lockBox,  BufferLocking options);
        /// <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);
        }
 ///<summary>
 ///    @copydoc HardwareBuffer.Lock
 ///</summary>
 //public virtual IntPtr Lock(int offset, int length, BasicBox lockBox, BufferLocking options) {
 //    Debug.Assert(!IsLocked, "Cannot lock this buffer, it is already locked!");
 //    Debug.Assert(offset == 0 && length == sizeInBytes, "Cannot lock memory region, must lock box or entire buffer");
 //    BasicBox myBox = new BasicBox(0, 0, 0, width, height, depth);
 //    PixelBox rv = Lock(myBox, options);
 //    return rv.Data;
 //}
 ///<summary>
 ///    Internal implementation of lock(), do not override or call this
 ///    for HardwarePixelBuffer implementations, but override the previous method
 ///</summary>
 protected override IntPtr LockImpl(int offset, int length, BufferLocking options)
 {
     throw new NotImplementedException("HardwarePixelBuffer does not support this variant of LockImpl");
 }