Ejemplo n.º 1
0
 /// <summary>
 /// Release the CPU buffer of this Buffer.
 /// </summary>
 protected void DeleteCpuBuffer()
 {
     if (_CpuBuffer != null)
     {
         _CpuBuffer.Dispose();
         _CpuBuffer = null;
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting managed/unmanaged resources.
        /// </summary>
        /// <exception cref="InvalidOperationException">
        /// Exception thrown in the case <see cref="GraphicsResource.RefCount"/> is greater than zero. This means that the method is trying to dispose
        /// an object that is actually referenced by something else.
        /// </exception>
        protected override void Dispose(bool disposing)
        {
            // Base implementation
            base.Dispose(disposing);

            if (disposing)
            {
                GpuBuffer?.Dispose();
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Performs application-defined tasks associated with freeing, releasing, or resetting managed/unmanaged resources.
 /// </summary>
 /// <param name="disposing">
 /// </param>
 protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         if (_PixelBuffers != null)
         {
             // Release the buffer buffer
             _PixelBuffers.Dispose();
             _PixelBuffers = null;
         }
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Create the GPU buffer in case GL_ARB_vertex_buffer_object is not supported.
        /// </summary>
        /// <param name="ctx">
        /// A <see cref="GraphicsContext"/> used for allocating resources.
        /// </param>
        /// <param name="size">
        /// A <see cref="UInt32"/> that specifies the size of the GPU buffer, in bytes.
        /// </param>
        protected void CreateObject_Compatible(GraphicsContext ctx, uint size)
        {
            // Discard previous GPU buffer, if any
            if (_GpuBuffer != null)
            {
                _GpuBuffer.Dispose();
            }

            if (CpuBufferAddress != IntPtr.Zero)
            {
                // Swap CPU/GPU buffers
                _GpuBuffer = _CpuBuffer;
                _CpuBuffer = null;
            }
            else
            {
                // Allocate simulated GPU buffer
                _GpuBuffer = new AlignedMemoryBuffer(size, DefaultBufferAlignment);
            }

            // Store GPU buffer size
            _GpuBufferSize = _GpuBuffer.Size;
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting managed/unmanaged resources.
        /// </summary>
        /// <exception cref="InvalidOperationException">
        /// Exception thrown in the case <see cref="GraphicsResource.RefCount"/> is greater than zero. This means that the method is trying to dispose
        /// an object that is actually referenced by something else.
        /// </exception>
        protected override void Dispose(bool disposing)
        {
            // Base implementation
            base.Dispose(disposing);

            if (disposing)
            {
                // Simulated GPU buffer is disposed at disposition
                if (GpuBuffer != null)
                {
                    GpuBuffer.Dispose();
                    GpuBuffer = null;
                    // Reset GPU buffer size
                    _GpuBufferSize = 0;
                }

                // Release CPU buffer
                DeleteCpuBuffer();
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Actually create this BufferObject resources.
        /// </summary>
        /// <param name="ctx">
        /// A <see cref="GraphicsContext"/> used for allocating resources.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// Exception thrown if <paramref name="ctx"/> is null.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// Exception thrown if <paramref name="ctx"/> is not current on the calling thread.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// Exception thrown if this BufferObject has not client memory allocated and the hint is different from
        /// <see cref="BufferObjectHint.StaticCpuDraw"/> or <see cref="BufferObjectHint.DynamicCpuDraw"/>.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// Exception thrown if this BufferObject is currently mapped.
        /// </exception>
        protected override void CreateObject(GraphicsContext ctx)
        {
            CheckCurrentContext(ctx);

            if ((ClientBufferAddress == IntPtr.Zero) && ((Hint != BufferObjectHint.StaticCpuDraw) && (Hint != BufferObjectHint.DynamicCpuDraw)))
            {
                throw new InvalidOperationException("no client buffer");
            }

            // Determine the client buffer size
            uint clientBufferSize = _ClientBufferSize;

            if (ClientBufferAddress != IntPtr.Zero)
            {
                clientBufferSize = ClientBufferSize;
            }

            Debug.Assert(clientBufferSize > 0);

            // Buffer must be bound
            ctx.Bind(this, true);

            if (ctx.Extensions.VertexBufferObject_ARB)
            {
                if (IsMapped)
                {
                    throw new InvalidOperationException("mapped");
                }

                // Define buffer object (type, size and hints)
                AllocateGpuBuffer(clientBufferSize, null);

                // Define buffer object contents
                if (ClientBufferAddress != IntPtr.Zero)
                {
                    // Provide buffer contents
                    Gl.BufferSubData(BufferType, IntPtr.Zero, _GpuBufferSize, ClientBufferAddress);
                    // Release memory, if it is not required anymore
                    if (_MemoryBufferAutoDispose)
                    {
                        ReleaseClientBuffer();
                    }
                }
            }
            else
            {
                // Discard previous GPU buffer, if any
                if (_GpuBuffer != null)
                {
                    _GpuBuffer.Dispose();
                }

                if (ClientBufferAddress == IntPtr.Zero)
                {
                    // Note: GPU buffer size specified by _ClientBufferSize
                    Debug.Assert(_ClientBufferSize > 0);
                    // Allocate simulated GPU buffer
                    _GpuBuffer = new AlignedMemoryBuffer(_ClientBufferSize, MinimumBufferAlignment);
                }
                else
                {
                    // Let a virtual implementation decide how pass information from the client buffer and the "GPU" buffer
                    UploadClientBuffer();
                }

                // Store GPU buffer size
                _GpuBufferSize = _GpuBuffer.Size;
            }

            // Reset requested client buffer size
            _ClientBufferSize = 0;
        }