Example #1
0
        internal VertexShader(SlimDX.Direct3D11.InputLayout inputLayout, SlimDX.Direct3D11.VertexShader vertexShader)
        {
            #if ASSERT
            if (inputLayout == null)
            {
                throw new ArgumentNullException("inputLayout");
            }

            if (vertexShader == null)
            {
                throw new ArgumentNullException("vertexShader");
            }
            #endif

            this.inputLayout = inputLayout;
            resourceOwner = false;
            this.vertexShader = vertexShader;
            uniqueId = new UniqueId<VertexShader>();
        }
Example #2
0
        internal VertexShader(SlimDX.Direct3D11.Device device, IEnumerable<SlimDX.Direct3D11.InputElement> inputElements, SlimDX.D3DCompiler.ShaderBytecode vertexShaderCode)
        {
            #if ASSERT
            if (device == null)
            {
                throw new ArgumentNullException("device");
            }

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

            if (vertexShaderCode == null)
            {
                throw new ArgumentNullException("vertexShaderCode");
            }
            #endif

            resourceOwner = true;
            vertexShader = new SlimDX.Direct3D11.VertexShader(device, vertexShaderCode);
            inputLayout = new SlimDX.Direct3D11.InputLayout(device, vertexShaderCode, inputElements.ToArray());
            uniqueId = new UniqueId<VertexShader>();
        }
Example #3
0
        internal VertexShader(SlimDX.Direct3D11.Device device, IEnumerable<SlimDX.Direct3D11.InputElement> inputElements, string shaderCode, string entryPoint, SlimDX.D3DCompiler.ShaderFlags shaderFlags)
        {
            #if ASSERT
            if (device == null)
            {
                throw new ArgumentNullException("device");
            }

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

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

            if (shaderCode.Length == 0)
            {
                throw new ArgumentOutOfRangeException("shaderCode");
            }

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

            if (entryPoint.Length == 0)
            {
                throw new ArgumentOutOfRangeException("entryPoint");
            }
            #endif

            resourceOwner = true;
            uniqueId = new UniqueId<VertexShader>();

            using (var vertexShaderCode = SlimDX.D3DCompiler.ShaderBytecode.Compile(shaderCode, entryPoint, "vs_5_0", shaderFlags, SlimDX.D3DCompiler.EffectFlags.None))
            {
                vertexShader = new SlimDX.Direct3D11.VertexShader(device, vertexShaderCode);
                inputLayout = new SlimDX.Direct3D11.InputLayout(device, vertexShaderCode, inputElements.ToArray());
            }
        }
Example #4
0
        public void Dispose()
        {
            if (resourceOwner)
            {
                if (inputLayout != null)
                {
                    inputLayout.Dispose();
                }

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

            inputLayout = null;
            vertexShader = null;
        }
Example #5
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();

                }
            }
        }