Example #1
0
 public void Dispose()
 {
     VertexBuffer.Dispose();
     IndexBuffer.Dispose();
     VertexBuffer = null;
     IndexBuffer  = null;
 }
 public RenderableLightPrimitive(LightShader shader, Buffer vertexbuffer, int vertexCount, Buffer indexbuffer, int indexCount)
 {
     this.Shader = shader;
     this.VertexBuffer = vertexbuffer;
     this.IndexBuffer = indexbuffer;
     this.VertexCount = vertexCount;
     this.IndexCount = indexCount;
 }
Example #3
0
 internal void Dispose()
 {
     if(Vertices == null)
         return;
     if (!Vertices.Disposed)
         Vertices.Dispose();
     Vertices = null;
     VertexCount = 0;
 }
Example #4
0
 internal void Dispose()
 {
     if (Vertices == null)
     {
         return;
     }
     if (!Vertices.Disposed)
     {
         Vertices.Dispose();
     }
     Vertices    = null;
     VertexCount = 0;
 }
Example #5
0
        internal IndexBuffer(SlimDX.Direct3D11.Buffer indexBuffer, int indexCount)
        {
            #if ASSERT
            if (indexBuffer == null)
            {
                throw new ArgumentNullException("indexBuffer");
            }

            if (indexCount <= 0)
            {
                throw new ArgumentNullException("indexCount");
            }
            #endif

            this.indexBuffer = indexBuffer;
            this.indexCount = indexCount;
            resourceOwner = false;
        }
Example #6
0
        internal IndexBuffer(SlimDX.Direct3D11.Device device, IEnumerable<int> indices)
        {
            #if ASSERT
            if (device == null)
            {
                throw new ArgumentNullException("device");
            }

            if (indices == null)
            {
                throw new ArgumentNullException("indices");
            }

            if (indices.Count() == 0)
            {
                throw new ArgumentOutOfRangeException("indices");
            }
            #endif

            indexCount = indices.Count();
            resourceOwner = true;
            var indexSize = Marshal.SizeOf(typeof(UInt32));
            var bufferSize = indexCount * indexSize;

            using (var dataStream = new SlimDX.DataStream(bufferSize, true, true))
            {
                foreach (var index in indices)
                {
                    dataStream.Write(Convert.ToUInt32(index));
                }

                dataStream.Position = 0;

                indexBuffer = new SlimDX.Direct3D11.Buffer(device, dataStream, bufferSize, SlimDX.Direct3D11.ResourceUsage.Default, SlimDX.Direct3D11.BindFlags.IndexBuffer, SlimDX.Direct3D11.CpuAccessFlags.None, SlimDX.Direct3D11.ResourceOptionFlags.None, 0);
            }
        }
Example #7
0
        internal VertexBuffer(SlimDX.Direct3D11.Device device, SlimDX.DataStream dataStream, int bytesPerVertex, int vertexCount, SlimDX.Direct3D11.PrimitiveTopology primitiveTopology)
        {
            #if ASSERT
            if (device == null)
            {
                throw new ArgumentNullException("device");
            }

            if (dataStream == null)
            {
                throw new ArgumentNullException("dataStream");
            }

            if (bytesPerVertex <= 0)
            {
                throw new ArgumentOutOfRangeException("bytesPerVertex");
            }

            if (vertexCount <= 0)
            {
                throw new ArgumentOutOfRangeException("vertexCount");
            }
            #endif

            this.bytesPerVertex = bytesPerVertex;
            this.primitiveTopology = primitiveTopology;
            this.resourceOwner = true;
            this.vertexCount = vertexCount;

            // Seek to the start of the vertices
            dataStream.Position = 0;

            vertexBuffer = new SlimDX.Direct3D11.Buffer(device, dataStream, vertexCount * bytesPerVertex, SlimDX.Direct3D11.ResourceUsage.Default, SlimDX.Direct3D11.BindFlags.VertexBuffer,
                    SlimDX.Direct3D11.CpuAccessFlags.None, SlimDX.Direct3D11.ResourceOptionFlags.None, 0);
            vertexBufferBinding = new SlimDX.Direct3D11.VertexBufferBinding(vertexBuffer, bytesPerVertex, 0);
        }
Example #8
0
        protected override DX11VertexGeometry GetGeom(DX11RenderContext device, int slice)
        {
            if (d2dFactory == null)
            {
                d2dFactory = new D2DFactory();
                dwFactory  = new DWriteFactory(SharpDX.DirectWrite.FactoryType.Shared);
            }

            TextFormat fmt = new TextFormat(dwFactory, this.FFontInput[slice].Name, FFontSize[slice]);
            TextLayout tl  = new TextLayout(dwFactory, FText[slice], fmt, 0.0f, 32.0f);

            tl.WordWrapping       = WordWrapping.NoWrap;
            tl.TextAlignment      = FHAlignment[slice];
            tl.ParagraphAlignment = FVAlignment[slice];

            OutlineRenderer renderer = new OutlineRenderer(d2dFactory);
            Extruder        ex       = new Extruder(d2dFactory);


            tl.Draw(renderer, 0.0f, 0.0f);

            var outlinedGeometry = renderer.GetGeometry();

            ex.GetVertices(outlinedGeometry, vertexList, this.FExtrude[slice]);
            outlinedGeometry.Dispose();

            Vector3 min = new Vector3(float.MaxValue);
            Vector3 max = new Vector3(float.MinValue);

            for (int i = 0; i < vertexList.Count; i++)
            {
                Pos3Norm3VertexSDX pn = vertexList[i];

                min.X = pn.Position.X < min.X ? pn.Position.X : min.X;
                min.Y = pn.Position.Y < min.Y ? pn.Position.Y : min.Y;
                min.Z = pn.Position.Z < min.Z ? pn.Position.Z : min.Z;

                max.X = pn.Position.X > max.X ? pn.Position.X : max.X;
                max.Y = pn.Position.Y > max.Y ? pn.Position.Y : max.Y;
                max.Z = pn.Position.Z > max.Z ? pn.Position.Z : max.Z;
            }

            SlimDX.DataStream ds = new SlimDX.DataStream(vertexList.Count * Pos3Norm3VertexSDX.VertexSize, true, true);
            ds.Position = 0;

            for (int i = 0; i < vertexList.Count; i++)
            {
                ds.Write(vertexList[i]);
            }

            ds.Position = 0;

            var vbuffer = new SlimDX.Direct3D11.Buffer(device.Device, ds, new SlimDX.Direct3D11.BufferDescription()
            {
                BindFlags      = SlimDX.Direct3D11.BindFlags.VertexBuffer,
                CpuAccessFlags = SlimDX.Direct3D11.CpuAccessFlags.None,
                OptionFlags    = SlimDX.Direct3D11.ResourceOptionFlags.None,
                SizeInBytes    = (int)ds.Length,
                Usage          = SlimDX.Direct3D11.ResourceUsage.Default
            });

            ds.Dispose();

            DX11VertexGeometry vg = new DX11VertexGeometry(device);

            vg.InputLayout    = Pos3Norm3VertexSDX.Layout;
            vg.Topology       = SlimDX.Direct3D11.PrimitiveTopology.TriangleList;
            vg.VertexBuffer   = vbuffer;
            vg.VertexSize     = Pos3Norm3VertexSDX.VertexSize;
            vg.VerticesCount  = vertexList.Count;
            vg.HasBoundingBox = true;
            vg.BoundingBox    = new SlimDX.BoundingBox(new SlimDX.Vector3(min.X, min.Y, min.Z), new SlimDX.Vector3(max.X, max.Y, max.Z));

            renderer.Dispose();
            fmt.Dispose();
            tl.Dispose();

            return(vg);
        }
Example #9
0
        public void Dispose()
        {
            if (resourceOwner)
            {
                if (indexBuffer != null)
                {
                    indexBuffer.Dispose();
                }
            }

            indexBuffer = null;
        }
Example #10
0
        public TextRenderer(Device device)
        {
            using (var factory = new SlimDX.DXGI.Factory1())
            {
                using (var adapter = factory.GetAdapter1(0))
                {
                    this.device = new SlimDX.Direct3D10_1.Device1(
                        adapter,
                        SlimDX.Direct3D10.DriverType.Hardware,
                        SlimDX.Direct3D10.DeviceCreationFlags.BgraSupport,
                        SlimDX.Direct3D10_1.FeatureLevel.Level_10_0
                    );

                    // Create the DirectX11 texture2D. This texture will be shared with the DirectX10
                    // device. The DirectX10 device will be used to render text onto this texture. DirectX11
                    // will then draw this texture (blended) onto the screen.
                    // The KeyedMutex flag is required in order to share this resource.
                    textureD3D11 = new SlimDX.Direct3D11.Texture2D(device.Handle, new SlimDX.Direct3D11.Texture2DDescription
                    {
                        Width = device.Form.Width,
                        Height = device.Form.Height,
                        MipLevels = 1,
                        ArraySize = 1,
                        Format = SlimDX.DXGI.Format.B8G8R8A8_UNorm,
                        SampleDescription = new SlimDX.DXGI.SampleDescription(1, 0),
                        Usage = SlimDX.Direct3D11.ResourceUsage.Default,
                        BindFlags = SlimDX.Direct3D11.BindFlags.RenderTarget | SlimDX.Direct3D11.BindFlags.ShaderResource,
                        CpuAccessFlags = SlimDX.Direct3D11.CpuAccessFlags.None,
                        OptionFlags = SlimDX.Direct3D11.ResourceOptionFlags.KeyedMutex
                    });

                    // A DirectX10 Texture2D sharing the DirectX11 Texture2D
                    var sharedResource = new SlimDX.DXGI.Resource(textureD3D11);
                    var textureD3D10 = this.device.OpenSharedResource<SlimDX.Direct3D10.Texture2D>(sharedResource.SharedHandle);

                    // The KeyedMutex is used just prior to writing to textureD3D11 or textureD3D10.
                    // This is how DirectX knows which DirectX (10 or 11) is supposed to be writing
                    // to the shared texture.  The keyedMutex is just defined here, they will be used
                    // a bit later.
                    mutex10 = new SlimDX.DXGI.KeyedMutex(textureD3D10);
                    mutex11 = new SlimDX.DXGI.KeyedMutex(textureD3D11);

                    // Direct2D Factory
                    SlimDX.Direct2D.Factory d2Factory = new SlimDX.Direct2D.Factory(
                        SlimDX.Direct2D.FactoryType.SingleThreaded,
                        SlimDX.Direct2D.DebugLevel.Information
                    );

                    // Direct Write factory
                    SlimDX.DirectWrite.Factory dwFactory = new SlimDX.DirectWrite.Factory(
                        SlimDX.DirectWrite.FactoryType.Isolated
                    );

                    // The textFormat we will use to draw text with
                    textFormat = new SlimDX.DirectWrite.TextFormat(
                        dwFactory,
                        "Arial",
                        SlimDX.DirectWrite.FontWeight.Normal,
                        SlimDX.DirectWrite.FontStyle.Normal,
                        SlimDX.DirectWrite.FontStretch.Normal,
                        24,
                        "en-US"
                    );
                    textFormat.TextAlignment = SlimDX.DirectWrite.TextAlignment.Center;
                    textFormat.ParagraphAlignment = SlimDX.DirectWrite.ParagraphAlignment.Center;

                    // Query for a IDXGISurface.
                    // DirectWrite and DirectX10 can interoperate thru DXGI.
                    var surface = textureD3D10.AsSurface();
                    var rtp = new SlimDX.Direct2D.RenderTargetProperties();
                    rtp.MinimumFeatureLevel = SlimDX.Direct2D.FeatureLevel.Direct3D10;
                    rtp.Type = SlimDX.Direct2D.RenderTargetType.Hardware;
                    rtp.Usage = SlimDX.Direct2D.RenderTargetUsage.None;
                    rtp.PixelFormat = new SlimDX.Direct2D.PixelFormat(SlimDX.DXGI.Format.Unknown, SlimDX.Direct2D.AlphaMode.Premultiplied);
                    dwRenderTarget = SlimDX.Direct2D.RenderTarget.FromDXGI(d2Factory, surface, rtp);

                    // Brush used to DrawText
                    brushSolidWhite = new SlimDX.Direct2D.SolidColorBrush(
                        dwRenderTarget,
                        new SlimDX.Color4(1, 1, 1, 1)
                    );

                    // Think of the shared textureD3D10 as an overlay.
                    // The overlay needs to show the text but let the underlying triangle (or whatever)
                    // show thru, which is accomplished by blending.
                    var bsd = new SlimDX.Direct3D11.BlendStateDescription();
                    bsd.RenderTargets[0].BlendEnable = true;
                    bsd.RenderTargets[0].SourceBlend = SlimDX.Direct3D11.BlendOption.SourceAlpha;
                    bsd.RenderTargets[0].DestinationBlend = SlimDX.Direct3D11.BlendOption.InverseSourceAlpha;
                    bsd.RenderTargets[0].BlendOperation = SlimDX.Direct3D11.BlendOperation.Add;
                    bsd.RenderTargets[0].SourceBlendAlpha = SlimDX.Direct3D11.BlendOption.One;
                    bsd.RenderTargets[0].DestinationBlendAlpha = SlimDX.Direct3D11.BlendOption.Zero;
                    bsd.RenderTargets[0].BlendOperationAlpha = SlimDX.Direct3D11.BlendOperation.Add;
                    bsd.RenderTargets[0].RenderTargetWriteMask = SlimDX.Direct3D11.ColorWriteMaskFlags.All;
                    BlendState_Transparent = SlimDX.Direct3D11.BlendState.FromDescription(device.Handle, bsd);

                    // Load Effect. This includes both the vertex and pixel shaders.
                    // Also can include more than one technique.
                    var shaderByteCode = SlimDX.D3DCompiler.ShaderBytecode.CompileFromFile(
                        "texteffect.fx",
                        "fx_5_0",
                        SlimDX.D3DCompiler.ShaderFlags.EnableStrictness,
                        SlimDX.D3DCompiler.EffectFlags.None);

                    effect = new SlimDX.Direct3D11.Effect(device.Handle, shaderByteCode);

                    // create triangle vertex data, making sure to rewind the stream afterward
                    var verticesTriangle = new SlimDX.DataStream(30 * 3, true, true);
                    verticesTriangle.Write(new SlimDX.Vector3(0.0f, 0.5f, 0.5f));
                    verticesTriangle.Write(new SlimDX.Color4(1.0f, 0.0f, 0.0f, 1.0f));
                    verticesTriangle.Write(new SlimDX.Vector3(0.5f, -0.5f, 0.5f));
                    verticesTriangle.Write(new SlimDX.Color4(0.0f, 1.0f, 0.0f, 1.0f));
                    verticesTriangle.Write(new SlimDX.Vector3(-0.5f, -0.5f, 0.5f));
                    verticesTriangle.Write(new SlimDX.Color4(0.0f, 0.0f, 1.0f, 1.0f));
                    verticesTriangle.Position = 0;

                    // create the triangle vertex layout and buffer
                    var inputElements = new SlimDX.Direct3D11.InputElement[] {
                new SlimDX.Direct3D11.InputElement("POSITION", 0, SlimDX.DXGI.Format.R32G32B32A32_Float, 0, 0),
                new SlimDX.Direct3D11.InputElement("COLOR",0,SlimDX.DXGI.Format.R32G32B32A32_Float,16,0)
            };
                    var layoutColor = new SlimDX.Direct3D11.InputLayout(device.Handle, effect.GetTechniqueByName("Color").GetPassByIndex(0).Description.Signature, inputElements);
                    var vertexBufferColor = new SlimDX.Direct3D11.Buffer(device.Handle, verticesTriangle, (int)verticesTriangle.Length, SlimDX.Direct3D11.ResourceUsage.Default, SlimDX.Direct3D11.BindFlags.VertexBuffer, SlimDX.Direct3D11.CpuAccessFlags.None, SlimDX.Direct3D11.ResourceOptionFlags.None, 0);
                    verticesTriangle.Close();

                    // create text vertex data, making sure to rewind the stream afterward
                    // Top Left of screen is -1, +1
                    // Bottom Right of screen is +1, -1
                    var verticesText = new SlimDX.DataStream(30 * 4, true, true);
                    verticesText.Write(new SlimDX.Vector3(-1, 1, 0));
                    verticesText.Write(new SlimDX.Vector2(0, 0f));
                    verticesText.Write(new SlimDX.Vector3(1, 1, 0));
                    verticesText.Write(new SlimDX.Vector2(1, 0));
                    verticesText.Write(new SlimDX.Vector3(-1, -1, 0));
                    verticesText.Write(new SlimDX.Vector2(0, 1));
                    verticesText.Write(new SlimDX.Vector3(1, -1, 0));
                    verticesText.Write(new SlimDX.Vector2(1, 1));
                    verticesText.Position = 0;

                    // create the text vertex layout and buffer
                    layoutText = new SlimDX.Direct3D11.InputLayout(device.Handle, effect.GetTechniqueByName("Text").GetPassByIndex(0).Description.Signature, inputElements);
                    vertexBufferText = new SlimDX.Direct3D11.Buffer(device.Handle, verticesText, (int)verticesText.Length, SlimDX.Direct3D11.ResourceUsage.Default, SlimDX.Direct3D11.BindFlags.VertexBuffer, SlimDX.Direct3D11.CpuAccessFlags.None, SlimDX.Direct3D11.ResourceOptionFlags.None, 0);
                    verticesText.Close();

                }
            }
        }
Example #11
0
        protected override DX11VertexGeometry GetGeom(DX11RenderContext device, int slice)
        {
            if (d2dFactory == null)
            {
                d2dFactory = new D2DFactory();
                dwFactory = new DWriteFactory(SharpDX.DirectWrite.FactoryType.Shared);
            }

            TextFormat fmt = new TextFormat(dwFactory, this.FFontInput[slice].Name, FFontSize[slice]);

            TextLayout tl = new TextLayout(dwFactory, FText[slice], fmt, 0.0f, 32.0f);
            tl.WordWrapping = WordWrapping.NoWrap;
            tl.TextAlignment = FHAlignment[slice];
            tl.ParagraphAlignment = FVAlignment[slice];

            OutlineRenderer renderer = new OutlineRenderer(d2dFactory);
            Extruder ex = new Extruder(d2dFactory);

            tl.Draw(renderer, 0.0f, 0.0f);

            var result = ex.GetVertices(renderer.GetGeometry(), this.FExtrude[slice]);

            Vector3 min = new Vector3(float.MaxValue);
            Vector3 max = new Vector3(float.MinValue);

            result.ForEach(pn =>
            {
                min.X = pn.Position.X < min.X ? pn.Position.X : min.X;
                min.Y = pn.Position.Y < min.Y ? pn.Position.Y : min.Y;
                min.Z = pn.Position.Z < min.Z ? pn.Position.Z : min.Z;

                max.X = pn.Position.X > max.X ? pn.Position.X : max.X;
                max.Y = pn.Position.Y > max.Y ? pn.Position.Y : max.Y;
                max.Z = pn.Position.Z > max.Z ? pn.Position.Z : max.Z;
            });

            SlimDX.DataStream ds = new SlimDX.DataStream(result.Count * Pos3Norm3VertexSDX.VertexSize, true, true);
            ds.Position = 0;

            ds.WriteRange(result.ToArray());

            ds.Position = 0;

            var vbuffer = new SlimDX.Direct3D11.Buffer(device.Device, ds, new SlimDX.Direct3D11.BufferDescription()
            {
                BindFlags = SlimDX.Direct3D11.BindFlags.VertexBuffer,
                CpuAccessFlags = SlimDX.Direct3D11.CpuAccessFlags.None,
                OptionFlags = SlimDX.Direct3D11.ResourceOptionFlags.None,
                SizeInBytes = (int)ds.Length,
                Usage = SlimDX.Direct3D11.ResourceUsage.Default
            });

            ds.Dispose();

            DX11VertexGeometry vg = new DX11VertexGeometry(device);
            vg.InputLayout = Pos3Norm3VertexSDX.Layout;
            vg.Topology = SlimDX.Direct3D11.PrimitiveTopology.TriangleList;
            vg.VertexBuffer = vbuffer;
            vg.VertexSize = Pos3Norm3VertexSDX.VertexSize;
            vg.VerticesCount = result.Count;
            vg.HasBoundingBox = true;
            vg.BoundingBox = new SlimDX.BoundingBox(new SlimDX.Vector3(min.X, min.Y, min.Z), new SlimDX.Vector3(max.X, max.Y, max.Z));

            return vg;
        }
Example #12
0
        public void Dispose()
        {
            if (resourceOwner && vertexBuffer != null)
            {
                vertexBuffer.Dispose();
            }

            vertexBuffer = null;
        }