Ejemplo n.º 1
0
        /// <summary>
        /// Function to initialize the 2D renderer.
        /// </summary>
        private void Initialize()
        {
            // Add shader includes.
            if (!Graphics.Shaders.IncludeFiles.Contains("Gorgon2DShaders"))
            {
                Graphics.ImmediateContext.Shaders.IncludeFiles.Add("Gorgon2DShaders", Encoding.UTF8.GetString(Resources.BasicSprite));
            }

            // Create shader states.
            PixelShader = new Gorgon2DPixelShaderState(this);

            VertexShader = new Gorgon2DVertexShaderState(this);

            // Create layout information so we can bind our vertices to the shader.
            DefaultLayout = Graphics.ImmediateContext.Input.CreateInputLayout("Gorgon2D Input Layout", typeof(Gorgon2DVertex), VertexShader.DefaultVertexShader);

            // Create pre-defined effects objects.
            Effects = new Gorgon2DEffects(this);

            int spriteVertexBufferSize = Gorgon2DVertex.SizeInBytes * _cache.CacheSize;
            int spriteIndexBufferSize  = sizeof(int) * _cache.CacheSize * 6;

            // Set up our index buffer.
            using (var ibData = new GorgonDataStream(spriteIndexBufferSize))
            {
                unsafe
                {
                    ushort index  = 0;
                    var    buffer = (int *)ibData.UnsafePointer;
                    for (int i = 0; i < _cache.CacheSize; i++)
                    {
                        *(buffer++) = index;
                        *(buffer++) = (index + 1);
                        *(buffer++) = (index + 2);
                        *(buffer++) = (index + 1);
                        *(buffer++) = (index + 3);
                        *(buffer++) = (index + 2);

                        index += 4;
                    }
                }

                DefaultIndexBuffer = Graphics.ImmediateContext.Buffers.CreateIndexBuffer("Gorgon2D Default Index Buffer", new GorgonIndexBufferSettings
                {
                    IsOutput        = false,
                    SizeInBytes     = (int)ibData.Length,
                    Usage           = BufferUsage.Immutable,
                    Use32BitIndices = true
                }, ibData);
            }

            // Create our empty vertex buffer.
            _defaultVertexBuffer =
                new GorgonVertexBufferBinding(
                    Graphics.ImmediateContext.Buffers.CreateVertexBuffer("Gorgon 2D Default Vertex Buffer", new GorgonBufferSettings
            {
                SizeInBytes = spriteVertexBufferSize,
                Usage       = BufferUsage.Dynamic
            }), Gorgon2DVertex.SizeInBytes);

            // Set up the default render states.
            SetDefaultStates();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Releases unmanaged and - optionally - managed resources
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        private void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            if (disposing)
            {
                // Dump any pending rendering.
                _cache.Reset();

                if (_initialState != null)
                {
                    _initialState.Restore(true);
                    _initialState = null;
                }

                TrackedObjects.ReleaseAll();

                if (Effects != null)
                {
                    Effects.FreeEffects();
                    Effects = null;
                }

                if (_currentTarget.SwapChain != null)
                {
                    _currentTarget.SwapChain.AfterSwapChainResized -= target_Resized;
                }

                if (DefaultLayout != null)
                {
                    DefaultLayout.Dispose();
                }

                if (VertexShader != null)
                {
                    VertexShader.CleanUp();
                }

                if (PixelShader != null)
                {
                    PixelShader.CleanUp();
                }

                VertexShader = null;
                PixelShader  = null;

                DefaultVertexBufferBinding.VertexBuffer.Dispose();

                if (DefaultIndexBuffer != null)
                {
                    DefaultIndexBuffer.Dispose();
                }

                if ((_systemCreatedTarget) && (_defaultTarget.Target != null))
                {
                    _defaultTarget.Target.Resource.Dispose();
                    _defaultTarget = default(Gorgon2DTarget);
                }

                Graphics.RemoveTrackedObject(this);
            }

            _disposed = true;
        }