Example #1
0
        public void FillRectangle(Color color, Vector2 location, Vector2 size)
        {
            if (!Initialized)
            {
                return;
            }

            var col = (RawColor4)color;

            GeometryBuffer.AppendVertices(
                new Vertex(location.X, location.Y, col),
                new Vertex(location.X + size.X, location.Y, col),
                new Vertex(location.X, location.Y + size.Y, col),
                new Vertex(location.X + size.X, location.Y + size.Y, col)
                );

            GeometryBuffer.AppendIndices(

                1,
                2,
                3,

                0,
                1,
                2

                );

            GeometryBuffer.SetupTexture(WhiteView);
            GeometryBuffer.SetPrimitiveType(PrimitiveTopology.TriangleStrip);
            GeometryBuffer.Trim();
        }
Example #2
0
        public void Present()
        {
            if (!Initialized)
            {
                return;
            }

            GeometryBuffer.Draw();
            GeometryBuffer.Reset();

            swapChain.Present(1, PresentFlags.None);
        }
Example #3
0
        public void Debug(GeometryBuffer buf)
        {
            var color = new RawColor4(1f, 1f, 1f, 1f);

            buf.AppendVertices(
                new Vertex(0f, 0f, color, 0f, 0f),
                new Vertex(1024, 0f, color, 1f, 0f),
                new Vertex(0f, 512, color, 0f, 1f),
                new Vertex(1024, 512, color, 1f, 1f));
            buf.AppendIndices(0, 1, 2, 1, 2, 3);
            buf.SetupTexture(atlas.Resource);
            buf.Trim();
        }
Example #4
0
        public void DrawLines(Color color, Vector2[] points)
        {
            if (!Initialized)
            {
                return;
            }

            var col = (RawColor4)color;

            points.ToList().ForEach(el => { GeometryBuffer.AppendVertex(new Vertex {
                    Origin = el, Color = col
                }); });

            GeometryBuffer.SetupTexture(WhiteView);
            GeometryBuffer.SetPrimitiveType(PrimitiveTopology.LineList);
            GeometryBuffer.Trim();
        }
Example #5
0
        public void DrawRectangle(Color color, Vector2 location, Vector2 size)
        {
            if (!Initialized)
            {
                return;
            }

            var col = (RawColor4)color;

            GeometryBuffer.AppendVertices(
                new Vertex(location.X, location.Y, col),

                new Vertex(location.X + size.X, location.Y, col),
                new Vertex(location.X, location.Y + size.Y, col),

                new Vertex(location.X + size.X, location.Y + size.Y, col)
                );

            GeometryBuffer.AppendIndices(
                // top left -> top right
                0,
                1,

                // top right -> bottom right
                1,
                3,

                // bottom right -> bottom left
                3,
                2,

                // bottom left -> top left
                2,
                0
                );

            GeometryBuffer.SetupTexture(WhiteView);
            GeometryBuffer.SetPrimitiveType(PrimitiveTopology.LineList);
            GeometryBuffer.Trim();
        }
Example #6
0
        public void DrawLine(Color color, Vector2 from, Vector2 to)
        {
            if (!Initialized)
            {
                return;
            }

            var col = (RawColor4)color;

            GeometryBuffer.AppendVertices(
                new Vertex(from.X, from.Y, col),
                new Vertex(to.X, to.Y, col)
                );

            //GeometryBuffer.AppendIndices(new short[]
            //{
            //    0,
            //    1
            //});
            GeometryBuffer.SetPrimitiveType(PrimitiveTopology.LineList);

            GeometryBuffer.Trim();
        }
Example #7
0
        public void DrawString(GeometryBuffer geometry_buffer, Vector2 location, RawColor4 color, string text,
                               TextAlignment halign = TextAlignment.Near, TextAlignment valign = TextAlignment.Near)
        {
            if (IsDisposed)
            {
                return;
            }

            List <float> line_widths;
            float        height;

            MeasureString(text, out line_widths, out height);

            if (line_widths.Count == 0 && text.Length == 0)
            {
                return;
            }

            float wide         = 0f;
            float space        = Height * 0.35f;
            float highest_char = 0f;

            short vertex_offset = 0;

            switch (halign)
            {
            case TextAlignment.Center:
                wide = location.X - line_widths[0] / 2;
                break;

            case TextAlignment.Far:
                wide = location.X - line_widths[0];
                break;

            default:
                wide = location.X;
                break;
            }

            switch (valign)
            {
            case TextAlignment.Center:
                break;
                location.Y -= height / 2;

            case TextAlignment.Far:
                location.Y -= height;
                break;
            }

            foreach (var c in text)
            {
                if (c == ' ')
                {
                    wide += space;
                    continue;
                }

                if (c == '\n')
                {
                    height += highest_char == 0f ? Height : highest_char;

                    switch (halign)
                    {
                    case TextAlignment.Center:
                        wide = location.X - line_widths[0] / 2;
                        break;

                    case TextAlignment.Far:
                        wide = location.X - line_widths[0];
                        break;

                    default:
                        wide = location.X;
                        break;
                    }
                }

                Glyph glyph;
                if (atlas.Glyphs.ContainsKey(c))
                {
                    glyph = atlas.Glyphs[c];
                }
                else
                {
                    glyph = atlas.Glyphs['?'];
                }

                geometry_buffer.AppendVertices(
                    new Vertex(wide, location.Y, color, glyph.UV[0].X, glyph.UV[0].Y),

                    new Vertex(wide + glyph.Size.Width, location.Y, color, glyph.UV[1].X, glyph.UV[0].Y),

                    new Vertex(wide, location.Y + glyph.Size.Height, color, glyph.UV[0].X, glyph.UV[1].Y),

                    new Vertex(wide + glyph.Size.Width, location.Y + glyph.Size.Height, color, glyph.UV[1].X,
                               glyph.UV[1].Y)
                    );

                geometry_buffer.AppendIndices(
                    vertex_offset, (short)(vertex_offset + 1), (short)(vertex_offset + 2),
                    (short)(vertex_offset + 1), (short)(vertex_offset + 2), (short)(vertex_offset + 3)
                    );

                vertex_offset += 4;

                wide += glyph.Size.Width;

                if (glyph.Size.Height > highest_char)
                {
                    highest_char = glyph.Size.Height;
                }
            }

            geometry_buffer.SetPrimitiveType(PrimitiveTopology.TriangleStrip);
            geometry_buffer.SetupTexture(atlas.Resource);
            geometry_buffer.Trim();
        }
Example #8
0
        public void DrawString(GeometryBuffer geometry_buffer, Vector2 location, RawColor4 color, string text)
        {
            if (IsDisposed)
            {
                return;
            }

            float wide_pos     = location.X;
            float high_pos     = location.Y;
            float highest_char = 0f;

            short vertex_offset = 0;

            var space = Height * 0.35f;

            foreach (var c in text)
            {
                if (c == ' ')
                {
                    wide_pos += space;
                    continue;
                }

                if (c == '\n')
                {
                    wide_pos     = location.X;
                    high_pos    += highest_char == 0f ? Height : highest_char;
                    highest_char = 0f;
                    continue;
                }

                Glyph glyph;
                if (!atlas.Glyphs.ContainsKey(c))
                {
                    glyph = atlas.Glyphs['?'];
                }
                else
                {
                    glyph = atlas.Glyphs[c];
                }

                if (glyph.Size.Height > highest_char)
                {
                    highest_char = glyph.Size.Height;
                }

                geometry_buffer.AppendVertices(
                    new Vertex(wide_pos, high_pos, color, glyph.UV[0].X, glyph.UV[0].Y),

                    new Vertex(wide_pos + glyph.Size.Width, high_pos, color, glyph.UV[1].X, glyph.UV[0].Y),

                    new Vertex(wide_pos, high_pos + glyph.Size.Height, color, glyph.UV[0].X, glyph.UV[1].Y),

                    new Vertex(wide_pos + glyph.Size.Width, high_pos + glyph.Size.Height, color, glyph.UV[1].X,
                               glyph.UV[1].Y)
                    );

                geometry_buffer.AppendIndices(
                    vertex_offset, (short)(vertex_offset + 1), (short)(vertex_offset + 2),
                    (short)(vertex_offset + 1), (short)(vertex_offset + 2), (short)(vertex_offset + 3)
                    );

                vertex_offset += 4;

                wide_pos += glyph.Size.Width;
            }

            geometry_buffer.SetPrimitiveType(PrimitiveTopology.TriangleStrip);
            geometry_buffer.SetupTexture(atlas.Resource);
            geometry_buffer.Trim();
        }
Example #9
0
        public void Init(Form form)
        {
            ViewportSize  = new Size2F(form.Width, form.Height);
            hViewportSize = new Size2F(form.Width / 2f, form.Height / 2f);

            ModeDescription      backBufferDesc = new ModeDescription(form.Width, form.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm);
            SwapChainDescription swapChainDesc  = new SwapChainDescription()
            {
                ModeDescription   = backBufferDesc,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = Usage.RenderTargetOutput,
                BufferCount       = 1,
                OutputHandle      = form.Handle,
                IsWindowed        = true,
            };

            D3D11.Device.CreateWithSwapChain(DriverType.Hardware, D3D11.DeviceCreationFlags.None, swapChainDesc, out d3dDevice, out swapChain);
            d3dDeviceContext = d3dDevice.ImmediateContext;

            d3dDeviceContext.Rasterizer.SetViewport(0, 0, form.Width, form.Height);
            using (D3D11.Texture2D backBuffer = swapChain.GetBackBuffer <D3D11.Texture2D>(0))
            {
                renderTargetView = new D3D11.RenderTargetView(d3dDevice, backBuffer);
            }

            D3D11.BlendStateDescription blendStateDesc = D3D11.BlendStateDescription.Default();
            blendStateDesc.AlphaToCoverageEnable                 = false;
            blendStateDesc.RenderTarget[0].IsBlendEnabled        = true;
            blendStateDesc.RenderTarget[0].SourceBlend           = D3D11.BlendOption.SourceAlpha;
            blendStateDesc.RenderTarget[0].DestinationBlend      = D3D11.BlendOption.One;         //
            blendStateDesc.RenderTarget[0].BlendOperation        = D3D11.BlendOperation.Maximum;
            blendStateDesc.RenderTarget[0].SourceAlphaBlend      = D3D11.BlendOption.SourceAlpha; //Zero
            blendStateDesc.RenderTarget[0].DestinationAlphaBlend = D3D11.BlendOption.DestinationAlpha;
            blendStateDesc.RenderTarget[0].AlphaBlendOperation   = D3D11.BlendOperation.Maximum;
            blendStateDesc.RenderTarget[0].RenderTargetWriteMask = D3D11.ColorWriteMaskFlags.All;
            blendState = new D3D11.BlendState(d3dDevice, blendStateDesc);

            GeometryBuffer = new GeometryBuffer(this);

            Fonts = new FontCache(this);

            var layout = new D3D11.InputElement[]
            {
                new D3D11.InputElement("POSITION", 0, Format.R32G32_Float, 0, 0),
                new D3D11.InputElement("COLOR", 0, Format.R32G32B32A32_Float, 8, 0),
                new D3D11.InputElement("TEXCOORDS", 0, Format.R32G32_Float, 24, 0),
            };

            var vertexShaderOutput = ShaderBytecode.Compile(vertexShaderCode, "main", "vs_4_0", ShaderFlags.Debug);
            var pixelShaderOutput  = ShaderBytecode.Compile(pixelShaderCode, "main", "ps_4_0", ShaderFlags.Debug);

            vertexShader = new D3D11.VertexShader(Device, vertexShaderOutput);
            pixelShader  = new D3D11.PixelShader(Device, pixelShaderOutput);

            var shaderSignature = ShaderSignature.GetInputSignature(vertexShaderOutput);

            inputLayout = new D3D11.InputLayout(Device, shaderSignature, layout);

            IntPtr data  = System.Runtime.InteropServices.Marshal.AllocHGlobal(4 * 4 * 4);
            var    white = BitConverter.GetBytes(1f);

            for (int i = 0; i < 4 * 4; i++)
            {
                for (int j = 0; j < white.Length; j++)
                {
                    System.Runtime.InteropServices.Marshal.WriteByte(data, i * sizeof(float) + j, white[j]);
                }
            }

            White = new D3D11.Texture2D(Device, new D3D11.Texture2DDescription
            {
                Width             = 4,
                Height            = 4,
                ArraySize         = 1,
                BindFlags         = D3D11.BindFlags.ShaderResource,
                Usage             = D3D11.ResourceUsage.Dynamic,
                CpuAccessFlags    = D3D11.CpuAccessFlags.Write,
                Format            = Format.R32G32B32A32_Float,
                MipLevels         = 1,
                OptionFlags       = D3D11.ResourceOptionFlags.None,
                SampleDescription = new DXGI.SampleDescription(1, 0),
            }, new DataBox[] { new DataBox(data, 4 * 2, 4) });

            System.Runtime.InteropServices.Marshal.FreeHGlobal(data);

            WhiteView = new D3D11.ShaderResourceView(Device, White);

            samplerState = new D3D11.SamplerState(Device, new D3D11.SamplerStateDescription()
            {
                Filter             = D3D11.Filter.MinMagMipLinear,
                AddressU           = D3D11.TextureAddressMode.Clamp,
                AddressV           = D3D11.TextureAddressMode.Clamp,
                AddressW           = D3D11.TextureAddressMode.Clamp,
                BorderColor        = new RawColor4(1f, 0f, 1f, 1f),
                ComparisonFunction = D3D11.Comparison.Never,
                MaximumAnisotropy  = 16,
                MipLodBias         = 0,
                MinimumLod         = 0,
                MaximumLod         = 16
            });

            transfBuffer = new SharpDX.Direct3D11.Buffer(Device,
                                                         new SharpDX.Direct3D11.BufferDescription(sizeof(float) * 4, SharpDX.Direct3D11.ResourceUsage.Dynamic, SharpDX.Direct3D11.BindFlags.ConstantBuffer,
                                                                                                  SharpDX.Direct3D11.CpuAccessFlags.Write, SharpDX.Direct3D11.ResourceOptionFlags.None, sizeof(float)));

            DataStream stream;

            DeviceContext.MapSubresource(transfBuffer, D3D11.MapMode.WriteDiscard, D3D11.MapFlags.None, out stream);

            stream.Write(hViewportSize.Width);
            stream.Write(hViewportSize.Height);

            DeviceContext.UnmapSubresource(transfBuffer, 0);

            DeviceContext.VertexShader.SetShader(vertexShader, null, 0);
            DeviceContext.VertexShader.SetConstantBuffer(0, transfBuffer);
            DeviceContext.PixelShader.SetShader(pixelShader, null, 0);
            DeviceContext.PixelShader.SetSampler(0, samplerState);
            DeviceContext.InputAssembler.InputLayout = inputLayout;
            DeviceContext.OutputMerger.BlendState    = blendState;
        }