コード例 #1
0
ファイル: TextureBlitter.cs プロジェクト: ishkang/Gorgon
        /// <summary>
        /// Function to update the projection data.
        /// </summary>
        private void UpdateProjection()
        {
            if ((!_needsWvpUpdate) ||
                (_graphics.RenderTargets[0] == null))
            {
                return;
            }

            GorgonRenderTargetView target = _graphics.RenderTargets[0];

            DX.Matrix.OrthoOffCenterLH(0,
                                       target.Width,
                                       target.Height,
                                       0,
                                       0,
                                       1.0f,
                                       out DX.Matrix projectionMatrix);

            _wvpBuffer.Buffer.SetData(ref projectionMatrix);

            _targetBounds   = new DX.Rectangle(0, 0, target.Width, target.Height);
            _needsWvpUpdate = false;
        }
コード例 #2
0
        /// <summary>
        /// Function to render the sprite to a thumbnail image.
        /// </summary>
        /// <param name="sprite">The sprite to render.</param>
        /// <returns>The image containing the rendered sprite.</returns>
        private IGorgonImage RenderThumbnail(GorgonSprite sprite)
        {
            GorgonRenderTargetView prevRtv = null;

            try
            {
                sprite.Anchor = new DX.Vector2(0.5f);

                if (sprite.Size.Width > sprite.Size.Height)
                {
                    sprite.Scale = new DX.Vector2(256.0f / (sprite.Size.Width.Max(1)));
                }
                else
                {
                    sprite.Scale = new DX.Vector2(256.0f / (sprite.Size.Height.Max(1)));
                }

                sprite.Position = new DX.Vector2(128, 128);

                prevRtv = GraphicsContext.Graphics.RenderTargets[0];
                GraphicsContext.Graphics.SetRenderTarget(_rtv);
                GraphicsContext.Renderer2D.Begin();
                GraphicsContext.Renderer2D.DrawFilledRectangle(new DX.RectangleF(0, 0, 256, 256), GorgonColor.White, _bgPattern, new DX.RectangleF(0, 0, 1, 1));
                GraphicsContext.Renderer2D.DrawSprite(sprite);
                GraphicsContext.Renderer2D.End();

                return(_rtv.Texture.ToImage());
            }
            finally
            {
                if (prevRtv != null)
                {
                    GraphicsContext.Graphics.SetRenderTarget(prevRtv);
                }
            }
        }
コード例 #3
0
ファイル: VolumeRenderer.cs プロジェクト: ishkang/Gorgon
        /// <summary>
        /// Function to perform the actual rendering.
        /// </summary>
        public void Render()
        {
            UpdateCubeTransform();

            // Draw the window with our volume texture mapped to a cube.
            GorgonRenderTargetView currentRtv = _graphics.RenderTargets[0];

            DX.ViewportF oldViewport = _graphics.Viewports[0];

            // Draw the volume sections.
            _graphics.SetRenderTarget(_volumeRtSections[0]);
            _volumeRtSections[0].Clear(GorgonColor.BlackTransparent);
            _graphics.Submit(_cubePosDrawCull);
            _graphics.SetRenderTarget(_volumeRtSections[1]);
            _volumeRtSections[1].Clear(GorgonColor.BlackTransparent);
            _graphics.Submit(_cubePosDrawFrontCull);
            _graphics.SetRenderTarget(_volumeRtSections[2]);

            // Draw the actual cube.
            _graphics.SetRenderTarget(currentRtv);
            _graphics.SetViewport(ref _cubeView);
            _graphics.Submit(_cubeDirDrawCall);
            _graphics.SetViewport(ref oldViewport);
        }
コード例 #4
0
ファイル: Gorgon2D.cs プロジェクト: tmp7701/Gorgon
        /// <summary>
        /// Initializes a new instance of the <see cref="Gorgon2D"/> class.
        /// </summary>
        /// <param name="target">The primary render target to use.</param>
        /// <param name="vertexCacheSize">The number of vertices that can be placed in vertex cache.</param>
        /// <param name="autoCreatedTarget">TRUE if Gorgon created the target, FALSE if the user created the target.</param>
        internal Gorgon2D(GorgonRenderTargetView target, int vertexCacheSize, bool autoCreatedTarget)
        {
            _systemCreatedTarget = autoCreatedTarget;

            _cache = new Gorgon2DVertexCache(this, vertexCacheSize.Max(1024));

            IsBlendingEnabled  = true;
            IsAlphaTestEnabled = true;

            TrackedObjects = new GorgonDisposableObjectCollection();
            Graphics       = target.Resource.Graphics;
            DefaultTarget  = target;

            _logoSprite = new GorgonSprite(this, "Gorgon2D.LogoSprite")
            {
                Anchor        = new Vector2(Graphics.Textures.GorgonLogo.Settings.Size),
                Texture       = Graphics.Textures.GorgonLogo,
                TextureRegion = new RectangleF(Vector2.Zero, new Vector2(1)),
                Color         = Color.White,
                Size          = Graphics.Textures.GorgonLogo.Settings.Size
            };
            DefaultCamera = new Gorgon2DOrthoCamera(this,
                                                    "Gorgon.Camera.Default",
                                                    new RectangleF(0, 0, _defaultTarget.Width, _defaultTarget.Height),
                                                    0,
                                                    1.0f)
            {
                AutoUpdate = true
            };

            Renderables = new GorgonRenderables(this, _cache);
            Drawing     = new GorgonDrawing(this);

            // Perform further initialization.
            Initialize();
        }
コード例 #5
0
ファイル: GorgonExample.cs プロジェクト: ishkang/Gorgon
        /// <summary>
        /// Function to draw the statistics and the logo for the example.
        /// </summary>
        /// <param name="renderer">The 2D renderer that we are using.</param>
        public static void DrawStatsAndLogo(IGorgon2DFluent renderer)
        {
            renderer.ValidateObject(nameof(renderer));

            GorgonRenderTargetView currentRtv = renderer.Graphics.RenderTargets[0];

            if ((currentRtv == null) || (_logo == null) || (_statsFont == null))
            {
                return;
            }

            // We won't include these in the draw call count.
            _statsText.Length = 0;
            _statsText.AppendFormat("Average FPS: {0:0.0}\nFrame Delta: {1:0.00#} seconds\nDraw Call Count: {2}", GorgonTiming.AverageFPS, GorgonTiming.Delta, renderer.Graphics.DrawCallCount);

            DX.Size2F measure     = _statsFont.MeasureText(_statsText.ToString(), true);
            var       statsRegion = new DX.RectangleF(0, 0, currentRtv.Width, measure.Height + 4);
            var       logoRegion  = new DX.RectangleF(currentRtv.Width - _logo.Width - 5, currentRtv.Height - _logo.Height - 2, _logo.Width, _logo.Height);

            renderer
            .Begin()
            .DrawIf(() => ShowStatistics, r =>
            {
                // Draw translucent window.
                r.DrawFilledRectangle(statsRegion, new GorgonColor(0, 0, 0, 0.5f));
                // Draw lines for separators.
                r.DrawLine(0, measure.Height + 3, currentRtv.Width, measure.Height + 3, GorgonColor.White);
                r.DrawLine(0, measure.Height + 4, currentRtv.Width, measure.Height + 4, GorgonColor.Black);

                // Draw FPS text.
                r.DrawString(_statsText.ToString(), DX.Vector2.One, _statsFont, GorgonColor.White);
            })
            .DrawFilledRectangle(logoRegion, GorgonColor.White, _logo, new DX.RectangleF(0, 0, 1, 1))
            .End()
            .Update(g => g.ResetDrawCallStatistics());
        }
コード例 #6
0
ファイル: Gorgon2DEffect.cs プロジェクト: ishkang/Gorgon
        /// <summary>
        /// Function called prior to rendering a pass.
        /// </summary>
        /// <param name="passIndex">The index of the pass to render.</param>
        /// <param name="output">The final render target that will receive the rendering from the effect.</param>
        /// <param name="camera">The currently active camera.</param>
        /// <returns>A <see cref="PassContinuationState"/> to instruct the effect on how to proceed.</returns>
        /// <remarks>
        /// <para>
        /// Applications can use this to set up per-pass states and other configuration settings prior to executing a single render pass.
        /// </para>
        /// </remarks>
        /// <seealso cref="PassContinuationState"/>

        protected virtual PassContinuationState OnBeforeRenderPass(int passIndex, GorgonRenderTargetView output, IGorgon2DCamera camera) => PassContinuationState.Continue;
コード例 #7
0
ファイル: Gorgon2DEffect.cs プロジェクト: ishkang/Gorgon
 /// <summary>
 /// Function called after rendering is complete.
 /// </summary>
 /// <param name="output">The final render target that will receive the rendering from the effect.</param>
 /// <remarks>
 /// <para>
 /// Applications can use this to clean up and/or restore any states when rendering is finished. This is an ideal method to copy any rendering imagery to the final output render target.
 /// </para>
 /// </remarks>
 protected virtual void OnAfterRender(GorgonRenderTargetView output)
 {
 }
コード例 #8
0
ファイル: Gorgon2DEffect.cs プロジェクト: ishkang/Gorgon
 /// <summary>
 /// Function called prior to rendering.
 /// </summary>
 /// <param name="output">The final render target that will receive the rendering from the effect.</param>
 /// <param name="camera">The currently active camera.</param>
 /// <param name="sizeChanged"><b>true</b> if the output size changed since the last render, or <b>false</b> if it's the same.</param>
 /// <remarks>
 /// <para>
 /// Applications can use this to set up common states and other configuration settings prior to executing the render passes. This is an ideal method to initialize and resize your internal render
 /// targets (if applicable).
 /// </para>
 /// </remarks>
 protected virtual void OnBeforeRender(GorgonRenderTargetView output, IGorgon2DCamera camera, bool sizeChanged)
 {
 }
コード例 #9
0
ファイル: Gorgon2DEffect.cs プロジェクト: ishkang/Gorgon
        /// <summary>
        /// Function to set up state prior to rendering.
        /// </summary>
        /// <param name="blendStateOverride">An override for the current blending state.</param>
        /// <param name="depthStencilStateOverride">An override for the current depth/stencil state.</param>
        /// <param name="rasterStateOverride">An override for the current raster state.</param>
        /// <param name="output">The target used as the output.</param>
        /// <param name="camera">The active camera.</param>
        /// <returns><b>true</b> if state was overridden, <b>false</b> if not or <b>null</b> if rendering is canceled.</returns>
        private bool SetupStates(GorgonBlendState blendStateOverride, GorgonDepthStencilState depthStencilStateOverride, GorgonRasterState rasterStateOverride, GorgonRenderTargetView output, IGorgon2DCamera camera)
        {
            if (!_isInitialized)
            {
                OnInitialize();
                _isInitialized = true;
            }

            bool outputSizeChanged = false;

            if ((_prevOutputSize.Width != output.Width) ||
                (_prevOutputSize.Height != output.Height))
            {
                _prevOutputSize   = new DX.Size2(output.Width, output.Height);
                outputSizeChanged = true;
            }

            OnBeforeRender(output, camera, outputSizeChanged);

            if ((blendStateOverride == BlendStateOverride) &&
                (depthStencilStateOverride == DepthStencilStateOverride) &&
                (rasterStateOverride == RasterStateOverride))
            {
                return(false);
            }

            BatchStateBuilder.BlendState(blendStateOverride ?? GorgonBlendState.Default)
            .DepthStencilState(depthStencilStateOverride ?? GorgonDepthStencilState.Default)
            .RasterState(rasterStateOverride ?? GorgonRasterState.Default);

            BlendStateOverride        = blendStateOverride;
            DepthStencilStateOverride = depthStencilStateOverride;
            RasterStateOverride       = rasterStateOverride;

            return(true);
        }
コード例 #10
0
 /// <summary>Function called to render a single effect pass.</summary>
 /// <param name="passIndex">The index of the pass being rendered.</param>
 /// <param name="renderMethod">The method used to render a scene for the effect.</param>
 /// <param name="output">The render target that will receive the final render data.</param>
 /// <remarks>Applications must implement this in order to see any results from the effect.</remarks>
 protected override void OnRenderPass(int passIndex, Action <int, int, DX.Size2> renderMethod, GorgonRenderTargetView output) => renderMethod(passIndex, PassCount, new DX.Size2(output.Width, output.Height));
コード例 #11
0
        /// <summary>
        /// Function to render the scene for the compositor effects.
        /// </summary>
        /// <param name="output">The final output render target for the compositor.</param>
        /// <param name="renderMethod">The method used to render the initial scene.</param>
        /// <returns>The fluent interface for the effects processor.</returns>
        public Gorgon2DCompositor Render(GorgonRenderTargetView output,
                                         Action renderMethod)
        {
            output.ValidateObject(nameof(output));
            renderMethod.ValidateObject(nameof(renderMethod));

            // Create or update our resources
            if ((_final != output) || (NeedsResourceUpdate(output)))
            {
                FreeResources();
                CreateResources(output);
            }

            // Create the batch state that we'll use for our initial scene.
            if (_hasBatchStateChanged)
            {
                _batchState           = _batchStateBuilder.Build();
                _hasBatchStateChanged = false;
            }

            if (_hasFinalBatchStateChanged)
            {
                _finalBatchState           = _finalBatchStateBuilder.Build();
                _hasFinalBatchStateChanged = false;
            }

            RenderInitalScene(renderMethod);

            // If we have no effects, then, just output the scene to the render target as-is.
            if (_effects.Count == 0)
            {
                CopyToFinal(_originalTexture);
                return(this);
            }

            (GorgonRenderTargetView target, GorgonTexture2DView texture)current = (_pingTarget, _originalTexture);

            // Iterate through our items.
            for (int i = 0; i < _effectList.Count; ++i)
            {
                Gorgon2DCompositionPass pass = _effectList[i];

                if (!pass.Enabled)
                {
                    continue;
                }

                GorgonRenderTargetView   currentTarget  = current.target;
                GorgonTexture2DView      currentTexture = current.texture;
                GorgonTexture2DView      nextTexture    = ((currentTexture == _originalTexture) || (currentTexture == _pongTexture)) ? _pingTexture : _pongTexture;
                GorgonRenderTarget2DView nextTarget     = current.target == _pingTarget ? _pongTarget : _pingTarget;

                if (pass.ClearColor != null)
                {
                    currentTarget.Clear(pass.ClearColor.Value);
                }

                if (pass.Effect != null)
                {
                    RenderEffectPass(pass, currentTarget, currentTexture);
                }
                else
                {
                    RenderNoEffectPass(pass, currentTarget, currentTexture);
                }

                current = (nextTarget, nextTexture);
            }

            CopyToFinal(current.texture);

            return(this);
        }
コード例 #12
0
 /// <summary>
 /// Function to determine if the resources need updating or not.
 /// </summary>
 /// <param name="outputTarget">The final render output target.</param>
 /// <returns><b>true</b> if the resources need updating, or <b>false</b> if not.</returns>
 private bool NeedsResourceUpdate(GorgonRenderTargetView outputTarget) => (_final == null) ||
 (_originalTarget == null) ||
 (_originalTarget.Width != outputTarget.Width) ||
 (_originalTarget.Height != outputTarget.Height) ||
 (_originalTarget.Format != outputTarget.Format);
コード例 #13
0
        /// <summary>
        /// Function called to render a single effect pass.
        /// </summary>
        /// <param name="passIndex">The index of the pass being rendered.</param>
        /// <param name="renderMethod">The method used to render a scene for the effect.</param>
        /// <param name="output">The render target that will receive the final render data.</param>
        /// <remarks>
        /// <para>
        /// Applications must implement this in order to see any results from the effect.
        /// </para>
        /// </remarks>
        protected override void OnRenderPass(int passIndex, Action <int, int, DX.Size2> renderMethod, GorgonRenderTargetView output)
        {
            if (passIndex == 0)
            {
                renderMethod(passIndex, PassCount, new DX.Size2(output.Width, output.Height));
                return;
            }

            // We'll do our own drawing so we can ensure we have screen space.
            IGorgon2DCamera currentCamera = Renderer.CurrentCamera;

            Renderer.End();

            var   destRect = new DX.RectangleF(0, 0, output.Width, output.Height);
            float minZ     = 0;

            // Calculate our full screen blit area in camera projection if we're using another camera.
            if (currentCamera != null)
            {
                minZ     = currentCamera.MinimumDepth;
                destRect = currentCamera.ViewableRegion;
            }

            for (int i = 0; i < Lights.Count; ++i)
            {
                Gorgon2DLight light = Lights[i];

                if (light == null)
                {
                    continue;
                }

                UpdateLight(light);

                Renderer.Begin(_lightingState, currentCamera);
                Renderer.DrawFilledRectangle(destRect,
                                             GorgonColor.White,
                                             _gbufferTexture,
                                             new DX.RectangleF(0, 0, 1, 1),
                                             textureSampler: GorgonSamplerState.Default,
                                             depth: minZ);
                Renderer.End();
            }
        }
コード例 #14
0
 /// <summary>Function called after rendering is complete.</summary>
 /// <param name="output">The final render target that will receive the rendering from the effect.</param>
 /// <remarks>Applications can use this to clean up and/or restore any states when rendering is finished. This is an ideal method to copy any rendering imagery to the final output render target.</remarks>
 protected override void OnAfterRender(GorgonRenderTargetView output)
 {
     Graphics.TemporaryTargets.Return(_gbufferTargets[0]);
     Graphics.TemporaryTargets.Return(_gbufferTargets[2]);
 }
コード例 #15
0
ファイル: Gorgon2DEffect.cs プロジェクト: ishkang/Gorgon
 /// <summary>
 /// Function called after a pass is finished rendering.
 /// </summary>
 /// <param name="passIndex">The index of the pass that was rendered.</param>
 /// <param name="output">The final render target that will receive the rendering from the effect.</param>
 /// <remarks>
 /// <para>
 /// Applications can use this to clean up and/or restore any states after the pass completes.
 /// </para>
 /// </remarks>
 protected virtual void OnAfterRenderPass(int passIndex, GorgonRenderTargetView output)
 {
 }
コード例 #16
0
ファイル: Gorgon2DEffect.cs プロジェクト: ishkang/Gorgon
 /// <summary>
 /// Function called to render a single effect pass.
 /// </summary>
 /// <param name="passIndex">The index of the pass being rendered.</param>
 /// <param name="renderMethod">The method used to render a scene for the effect.</param>
 /// <param name="output">The render target that will receive the final render data.</param>
 /// <remarks>
 /// <para>
 /// Applications must implement this in order to see any results from the effect.
 /// </para>
 /// </remarks>
 protected virtual void OnRenderPass(int passIndex, Action <int, int, DX.Size2> renderMethod, GorgonRenderTargetView output)
 {
 }
コード例 #17
0
ファイル: TextureBlitter.cs プロジェクト: ishkang/Gorgon
        /// <summary>
        /// Function to blit the texture to the specified render target.
        /// </summary>
        /// <param name="texture">The texture that will be blitted to the render target.</param>
        /// <param name="destRect">The layout area to blit the texture into.</param>
        /// <param name="sourceOffset">The offset within the source texture to start blitting from.</param>
        /// <param name="color">The color used to tint the diffuse value of the texture.</param>
        /// <param name="clip"><b>true</b> to clip the contents of the texture if the destination is larger/small than the size of the texture.</param>
        /// <param name="blendState">The blending state to apply.</param>
        /// <param name="samplerState">The sampler state to apply.</param>
        /// <param name="pixelShader">The pixel shader used to override the default pixel shader.</param>
        /// <param name="pixelShaderConstants">The pixel shader constant buffers to use.</param>
        public void Blit(GorgonTexture2DView texture,
                         DX.Rectangle destRect,
                         DX.Point sourceOffset,
                         GorgonColor color,
                         bool clip,
                         GorgonBlendState blendState,
                         GorgonSamplerState samplerState,
                         GorgonPixelShader pixelShader,
                         GorgonConstantBuffers pixelShaderConstants)
        {
            if ((_graphics.RenderTargets[0] == null) ||
                (color.Alpha.EqualsEpsilon(0)))
            {
                return;
            }

            if (texture == null)
            {
                texture = _defaultTexture;
            }

            GorgonRenderTargetView currentView = _graphics.RenderTargets[0];

            // We need to update the projection/view if the size of the target changes.
            if ((_targetBounds == null) ||
                (currentView.Width != _targetBounds.Value.Width) ||
                (currentView.Height != _targetBounds.Value.Height))
            {
                _needsWvpUpdate = true;
            }

            UpdateProjection();

            // Set to default states if not provided.
            if (blendState == null)
            {
                blendState = GorgonBlendState.NoBlending;
            }

            if (pixelShader == null)
            {
                pixelShader = _pixelShader;
            }

            if (samplerState == null)
            {
                samplerState = GorgonSamplerState.Default;
            }

            if (pixelShaderConstants == null)
            {
                pixelShaderConstants = _emptyPsConstants;
            }

            GetDrawCall(texture, blendState, samplerState, pixelShader, pixelShaderConstants);

            // Calculate position on the texture.
            DX.Vector2 topLeft     = texture.Texture.ToTexel(sourceOffset);
            DX.Vector2 bottomRight = texture.Texture.ToTexel(clip ? new DX.Vector2(destRect.Width, destRect.Height) : new DX.Point(texture.Width, texture.Height));

            if (clip)
            {
                DX.Vector2.Add(ref bottomRight, ref topLeft, out bottomRight);
            }

            // Update the vertices.
            _vertices[0] = new BltVertex
            {
                Position = new DX.Vector4(destRect.X, destRect.Y, 0, 1.0f),
                Uv       = topLeft,
                Color    = color
            };
            _vertices[1] = new BltVertex
            {
                Position = new DX.Vector4(destRect.Right, destRect.Y, 0, 1.0f),
                Uv       = new DX.Vector2(bottomRight.X, topLeft.Y),
                Color    = color
            };
            _vertices[2] = new BltVertex
            {
                Position = new DX.Vector4(destRect.X, destRect.Bottom, 0, 1.0f),
                Uv       = new DX.Vector2(topLeft.X, bottomRight.Y),
                Color    = color
            };
            _vertices[3] = new BltVertex
            {
                Position = new DX.Vector4(destRect.Right, destRect.Bottom, 0, 1.0f),
                Uv       = new DX.Vector2(bottomRight.X, bottomRight.Y),
                Color    = color
            };

            // Copy to the vertex buffer.
            _vertexBufferBindings[0].VertexBuffer.SetData(_vertices);
            _graphics.Submit(_drawCall);
        }