コード例 #1
0
        protected void CreateDeviceCompatibleTexture(int width, int height, IDisposable texture10, out IDisposable texture11, out IDisposable srv11)
        {
            var texture  = (Texture2D)texture10;
            var device11 = Sprite.Device;

            lock (device11) {
                var dxgiResource = new Resource(texture);

                SlimDX.Direct3D11.Texture2D tex11;
                if (PixCompatible)
                {
                    tex11 = new SlimDX.Direct3D11.Texture2D(device11, new SlimDX.Direct3D11.Texture2DDescription {
                        ArraySize         = 1,
                        BindFlags         = SlimDX.Direct3D11.BindFlags.ShaderResource | SlimDX.Direct3D11.BindFlags.RenderTarget,
                        CpuAccessFlags    = SlimDX.Direct3D11.CpuAccessFlags.None,
                        Format            = Format.R8G8B8A8_UNorm,
                        Height            = height,
                        Width             = width,
                        MipLevels         = 1,
                        OptionFlags       = SlimDX.Direct3D11.ResourceOptionFlags.Shared,
                        SampleDescription = new SampleDescription(1, 0),
                        Usage             = SlimDX.Direct3D11.ResourceUsage.Default
                    });
                }
                else
                {
                    tex11 = device11.OpenSharedResource <SlimDX.Direct3D11.Texture2D>(dxgiResource.SharedHandle);
                }
                srv11 = new ShaderResourceView(device11, tex11);
                // device11.ImmediateContext.GenerateMips((global::SlimDX.Direct3D11.ShaderResourceView)srv11);
                texture11 = tex11;
                dxgiResource.Dispose();
            }
        }
コード例 #2
0
ファイル: Texture2d.cs プロジェクト: HaKDMoDz/WorldRender
        internal Texture2d(SlimDX.Direct3D11.Device device, string fileName)
        {
            #if ASSERT
            if (device == null)
            {
                throw new ArgumentNullException("device");
            }
            #endif

            texture2d = SlimDX.Direct3D11.Texture2D.FromFile(device, fileName);
            shaderResourceView = new SlimDX.Direct3D11.ShaderResourceView(device, texture2d);
            uniqueId = new UniqueId<Texture2d>();
        }
コード例 #3
0
ファイル: Texture2d.cs プロジェクト: HaKDMoDz/WorldRender
        public void Dispose()
        {
            if (shaderResourceView != null)
            {
                shaderResourceView.Dispose();
                shaderResourceView = null;
            }

            if (texture2d != null)
            {
                texture2d.Dispose();
                texture2d = null;
            }
        }
コード例 #4
0
        private void CreateCharTable(byte bytePrefix)
        {
            var TableDesc = new CharTableDescription();

            //Get appropriate texture size
            int sizeX = (int)(Font.FontSize * 12);

            sizeX = (int)Math.Pow(2, Math.Ceiling(Math.Log(sizeX, 2)));
            //Try how many lines are needed:
            var tl = new TextLayout[256];
            int line = 0, xPos = 0, yPos = 0;

            for (int i = 0; i < 256; ++i)
            {
                tl[i] = new TextLayout(ModelEx.FontManager.Instance.WriteFactory, Convert.ToChar(i + (bytePrefix << 8)).ToString(), Font);
                int charWidth  = 2 + (int)Math.Ceiling(tl[i].Metrics.LayoutWidth + tl[i].OverhangMetrics.Left + tl[i].OverhangMetrics.Right);
                int charHeight = 2 + (int)Math.Ceiling(tl[i].Metrics.LayoutHeight + tl[i].OverhangMetrics.Top + tl[i].OverhangMetrics.Bottom);
                line = Math.Max(line, charHeight);
                if (xPos + charWidth >= sizeX)
                {
                    xPos  = 0;
                    yPos += line;
                    line  = 0;
                }
                xPos += charWidth;
            }
            int sizeY = (int)(line + yPos);

            sizeY = (int)Math.Pow(2, Math.Ceiling(Math.Log(sizeY, 2)));

            //Create Texture
            var TexDesc = new Texture2DDescription()
            {
                ArraySize         = 1,
                BindFlags         = BindFlags.ShaderResource | BindFlags.RenderTarget,
                CpuAccessFlags    = CpuAccessFlags.None,
                Format            = Format.R8G8B8A8_UNorm,
                Height            = sizeY,
                Width             = sizeX,
                MipLevels         = 1,
                OptionFlags       = ResourceOptionFlags.Shared,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = ResourceUsage.Default
            };
            var texture = new Texture2D(ModelEx.FontManager.Instance.D3DDevice10, TexDesc);

            var rtv = new RenderTargetView(ModelEx.FontManager.Instance.D3DDevice10, texture);

            ModelEx.FontManager.Instance.D3DDevice10.ClearRenderTargetView(rtv, new SlimDX.Color4(0, 1, 1, 1));
            //D3DDevice10.ClearRenderTargetView(rtv, new SlimDX.Color4(1, 0, 0, 0));
            Surface surface = texture.AsSurface();
            var     target  = RenderTarget.FromDXGI(ModelEx.FontManager.Instance.D2DFactory, surface, rtp);
            var     color   = new SolidColorBrush(target, new SlimDX.Color4(1, 1, 1, 1));

            target.BeginDraw();
            line = 0; xPos = 0; yPos = 0;
            //for (int i = 0; i < 256; ++i)
            for (int i = 0; i < 256; ++i)
            {
                //1 additional pixel on each side
                int charWidth  = 2 + (int)Math.Ceiling(tl[i].Metrics.LayoutWidth + tl[i].OverhangMetrics.Left + tl[i].OverhangMetrics.Right);
                int charHeight = 2 + (int)Math.Ceiling(tl[i].Metrics.LayoutHeight + tl[i].OverhangMetrics.Top + tl[i].OverhangMetrics.Bottom);
                line = Math.Max(line, charHeight);
                if (xPos + charWidth >= sizeX)
                {
                    xPos  = 0;
                    yPos += line;
                    line  = 0;
                }
                var charDesc = new CharDescription();

                charDesc.CharSize     = new Vector2(tl[i].Metrics.WidthIncludingTrailingWhitespace, tl[i].Metrics.Height);
                charDesc.OverhangLeft = tl[i].OverhangMetrics.Left + 1;
                charDesc.OverhangTop  = tl[i].OverhangMetrics.Top + 1;
                //Make XPos + CD.Overhang.Left an integer number in order to draw at integer positions
                charDesc.OverhangLeft += (float)Math.Ceiling(xPos + charDesc.OverhangLeft) - (xPos + charDesc.OverhangLeft);
                //Make YPos + CD.Overhang.Top an integer number in order to draw at integer positions
                charDesc.OverhangTop += (float)Math.Ceiling(yPos + charDesc.OverhangTop) - (yPos + charDesc.OverhangTop);

                charDesc.OverhangRight  = charWidth - charDesc.CharSize.X - charDesc.OverhangLeft;
                charDesc.OverhangBottom = charHeight - charDesc.CharSize.Y - charDesc.OverhangTop;

                charDesc.TexCoordsStart = new Vector2(((float)xPos / sizeX), ((float)yPos / sizeY));
                charDesc.TexCoordsSize  = new Vector2((float)charWidth / sizeX, (float)charHeight / sizeY);

                charDesc.TableDescription = TableDesc;

                TableDesc.Chars[i] = charDesc;

                target.DrawTextLayout(new PointF(xPos + charDesc.OverhangLeft, yPos + charDesc.OverhangTop), tl[i], color);
                xPos += charWidth;
                tl[i].Dispose();
            }
            target.EndDraw();

            color.Dispose();

            //This is a workaround for Windows 8.1 machines.
            //If these lines would not be present, the shared resource would be empty.
            //TODO: find a nicer solution
            using (var ms = new MemoryStream())
                Texture2D.ToStream(texture, ImageFileFormat.Bmp, ms);

            System.Threading.Monitor.Enter(D3DDevice11);
            var dxgiResource = new SlimDX.DXGI.Resource(texture);

            SlimDX.Direct3D11.Texture2D Texture11;
            if (PixCompatible)
            {
                Texture11 = new SlimDX.Direct3D11.Texture2D(D3DDevice11, new SlimDX.Direct3D11.Texture2DDescription()
                {
                    ArraySize         = 1,
                    BindFlags         = SlimDX.Direct3D11.BindFlags.ShaderResource | SlimDX.Direct3D11.BindFlags.RenderTarget,
                    CpuAccessFlags    = SlimDX.Direct3D11.CpuAccessFlags.None,
                    Format            = Format.R8G8B8A8_UNorm,
                    Height            = sizeY,
                    Width             = sizeX,
                    MipLevels         = 1,
                    OptionFlags       = SlimDX.Direct3D11.ResourceOptionFlags.Shared,
                    SampleDescription = new SampleDescription(1, 0),
                    Usage             = SlimDX.Direct3D11.ResourceUsage.Default
                });
            }
            else
            {
                Texture11 = D3DDevice11.OpenSharedResource <SlimDX.Direct3D11.Texture2D>(dxgiResource.SharedHandle);
            }
            var srv = new SlimDX.Direct3D11.ShaderResourceView(D3DDevice11, Texture11);

            TableDesc.Texture = Texture11;
            TableDesc.SRV     = srv;
            rtv.Dispose();
            System.Threading.Monitor.Exit(D3DDevice11);

            System.Diagnostics.Debug.WriteLine("Created Char Table " + bytePrefix + " in " + sizeX + " x " + sizeY);

            //System.Threading.Monitor.Enter(D3DDevice11);
            //SlimDX.Direct3D11.Texture2D.SaveTextureToFile(Sprite.Device.ImmediateContext, Texture11, SlimDX.Direct3D11.ImageFileFormat.Png, Font.FontFamilyName + "Table" + BytePrefix + ".png");
            //System.Threading.Monitor.Exit(D3DDevice11);

            CharTables.Add(bytePrefix, TableDesc);

            dxgiResource.Dispose();
            target.Dispose();
            surface.Dispose();
            texture.Dispose();
        }
コード例 #5
0
ファイル: TextRenderer.cs プロジェクト: HaKDMoDz/WorldRender
        public void Render(Device device)
        {
            // Draw Text on the shared Texture2D
                // Need to Acquire the shared texture for use with DirectX10
                mutex10.Acquire(0, 100);
                dwRenderTarget.BeginDraw();
                dwRenderTarget.Clear(new SlimDX.Color4(0, 0, 0, 0));
                const string text = "Hello world!";
                dwRenderTarget.DrawText(text, textFormat, new System.Drawing.Rectangle(0, 0, device.Form.Width, device.Form.Height), brushSolidWhite);
                dwRenderTarget.EndDraw();
                mutex10.Release(0);

                // Draw the shared texture2D onto the screen
                // Need to Aquire the shared texture for use with DirectX11
                mutex11.Acquire(0, 100);
                var srv = new SlimDX.Direct3D11.ShaderResourceView(device.Handle, textureD3D11);
                effect.GetVariableByName("g_textOverlay").AsResource().SetResource(srv);
                device.Context.InputAssembler.InputLayout = layoutText;
                device.Context.InputAssembler.PrimitiveTopology = SlimDX.Direct3D11.PrimitiveTopology.TriangleStrip;
                device.Context.InputAssembler.SetVertexBuffers(0, new SlimDX.Direct3D11.VertexBufferBinding(vertexBufferText, 30 * 4, 0));
                device.Context.OutputMerger.BlendState = BlendState_Transparent;
                var currentTechnique = effect.GetTechniqueByName("Text");
                for (int pass = 0; pass < currentTechnique.Description.PassCount; ++pass)
                {
                    var Pass = currentTechnique.GetPassByIndex(pass);
                    System.Diagnostics.Debug.Assert(Pass.IsValid, "Invalid EffectPass");
                    Pass.Apply(device.Context);
                    device.Context.Draw(4, 0);
                }
                srv.Dispose();
                mutex11.Release(0);
        }