Exemplo n.º 1
0
        public Postprocess(Game game, string shaderFile, RenderTexture rt = null, string name = null)
        {
            this.game = game;
            this.renderTexture = rt;
            if(name == null)
            {
                this.Name = System.IO.Path.GetFileNameWithoutExtension(shaderFile);
                this.debugName = "Postprocess " + this.Name;
            }
            else
            {
                this.debugName = this.Name = name;
            }
            this.shaderFile = shaderFile;

            var desc = SamplerStateDescription.Default();
            desc.Filter = Filter.MinMagMipPoint;
            defaultSamplerStateDescription = desc;

            LoadShader();
            BuildVertexBuffer();
            ppBuffer = Material.CreateBuffer<LiliumPostprocessData>();

            game.AddObject(this);
        }
Exemplo n.º 2
0
        public TextureShader(Device device)
        {
            var vertexShaderByteCode = ShaderBytecode.CompileFromFile(ShaderFileName, "TextureVertexShader", "vs_4_0", ShaderFlags);
            var pixelShaderByteCode = ShaderBytecode.CompileFromFile(ShaderFileName, "TexturePixelShader", "ps_4_0", ShaderFlags);

            VertexShader = new VertexShader(device, vertexShaderByteCode);
            PixelShader = new PixelShader(device, pixelShaderByteCode);

            Layout = VertexDefinition.PositionTexture.GetInputLayout(device, vertexShaderByteCode);

            vertexShaderByteCode.Dispose();
            pixelShaderByteCode.Dispose();

            ConstantMatrixBuffer = new Buffer(device, MatrixBufferDesription);

            // Create a texture sampler state description.
            var samplerDesc = new SamplerStateDescription
            {
                Filter = Filter.Anisotropic,
                AddressU = TextureAddressMode.Mirror,
                AddressV = TextureAddressMode.Mirror,
                AddressW = TextureAddressMode.Mirror,
                MipLodBias = 0,
                MaximumAnisotropy = 16,
                ComparisonFunction = Comparison.Always,
                BorderColor = new Color4(1, 1, 1, 1),
                MinimumLod = 0,
                MaximumLod = 0
            };

            // Create the texture sampler state.
            SamplerState = new SamplerState(device, samplerDesc);
        }
Exemplo n.º 3
0
        public WorldTerrainShader(Device device)
        {
            var vertexShaderByteCode = ShaderBytecode.CompileFromFile(ShaderFileName, "TerrainVertexShader", "vs_4_0", ShaderFlags);
            var pixelShaderByteCode = ShaderBytecode.CompileFromFile(ShaderFileName, "TerrainPixelShader", "ps_4_0", ShaderFlags);

            VertexShader = new VertexShader(device, vertexShaderByteCode);
            PixelShader = new PixelShader(device, pixelShaderByteCode);

            Layout = VertexDefinition.PositionTexture.GetInputLayout(device, vertexShaderByteCode);

            vertexShaderByteCode.Dispose();
            pixelShaderByteCode.Dispose();

            ConstantMatrixBuffer = new Buffer(device, MatrixBufferDesription);

            var samplerDescMap = new SamplerStateDescription
            {
                Filter = Filter.MinMagMipPoint,
                AddressU = TextureAddressMode.Clamp,
                AddressV = TextureAddressMode.Clamp,
                AddressW = TextureAddressMode.Clamp,
                MipLodBias = 0,
                MaximumAnisotropy = 1,
                ComparisonFunction = Comparison.Always,
                BorderColor = Color.Transparent,
                MinimumLod = 0,
                MaximumLod = float.MaxValue
            };
            SamplerStateMap = new SamplerState(device, samplerDescMap);
        }
Exemplo n.º 4
0
        public TerrainMinimapShader(Device device)
        {
            var vertexShaderByteCode = ShaderBytecode.CompileFromFile(ShaderFileName, "MinimapTerrainVertexShader", "vs_4_0", ShaderFlags);
            var pixelShaderByteCode = ShaderBytecode.CompileFromFile(ShaderFileName, "MinimapTerrainPixelShader", "ps_4_0", ShaderFlags);

            VertexShader = new VertexShader(device, vertexShaderByteCode);
            PixelShader = new PixelShader(device, pixelShaderByteCode);

            Layout = VertexDefinition.TerrainVertex.GetInputLayout(device, vertexShaderByteCode);

            vertexShaderByteCode.Dispose();
            pixelShaderByteCode.Dispose();

            ConstantMatrixBuffer = new Buffer(device, MatrixBufferDesription);
            ConstantSelectionBuffer = new Buffer(device, new BufferDescription
            {
                Usage = ResourceUsage.Dynamic,
                SizeInBytes = Utilities.SizeOf<SelectionBuffer>(),
                BindFlags = BindFlags.ConstantBuffer,
                CpuAccessFlags = CpuAccessFlags.Write,
                OptionFlags = ResourceOptionFlags.None,
                StructureByteStride = 0
            });

            var samplerDescBorder = new SamplerStateDescription
            {
                Filter = Filter.Anisotropic,
                AddressU = TextureAddressMode.MirrorOnce,
                AddressV = TextureAddressMode.Clamp,
                AddressW = TextureAddressMode.Clamp,
                MipLodBias = 0,
                MaximumAnisotropy = 16,
                ComparisonFunction = Comparison.Always,
                BorderColor = Color.Transparent,
                MinimumLod = 0,
                MaximumLod = float.MaxValue
            };

            // Create the texture sampler state.
            SamplerStateBorder = new SamplerState(device, samplerDescBorder);

            var samplerDescColor = new SamplerStateDescription
            {
                Filter = Filter.Anisotropic,
                AddressU = TextureAddressMode.Wrap,
                AddressV = TextureAddressMode.Wrap,
                AddressW = TextureAddressMode.Wrap,
                MipLodBias = 0,
                MaximumAnisotropy = 16,
                ComparisonFunction = Comparison.Always,
                BorderColor = Color.Transparent,
                MinimumLod = 0,
                MaximumLod = float.MaxValue
            };

            // Create the texture sampler state.
            SamplerStateColor = new SamplerState(device, samplerDescColor);
        }
        public ProjectiveTexturingShader(Device device)
        {
            var shaderByteCode = new ShaderBytecode(File.ReadAllBytes("Content/DepthAndProjectiveTextureVS.cso"));
            vertexShader = new VertexShader(device, shaderByteCode);
            geometryShader = new GeometryShader(device, new ShaderBytecode(File.ReadAllBytes("Content/DepthAndColorGS.cso")));
            pixelShader = new PixelShader(device, new ShaderBytecode(File.ReadAllBytes("Content/DepthAndColorPS.cso")));

            // depth stencil state
            var depthStencilStateDesc = new DepthStencilStateDescription()
            {
                IsDepthEnabled = true,
                DepthWriteMask = DepthWriteMask.All,
                DepthComparison = Comparison.LessEqual,
                IsStencilEnabled = false,
            };
            depthStencilState = new DepthStencilState(device, depthStencilStateDesc);

            // rasterizer states
            var rasterizerStateDesc = new RasterizerStateDescription()
            {
                CullMode = CullMode.None,
                FillMode = FillMode.Solid,
                IsDepthClipEnabled = true,
                IsFrontCounterClockwise = true,
                IsMultisampleEnabled = true,
            };
            rasterizerState = new RasterizerState(device, rasterizerStateDesc);

            // constant buffer
            var constantBufferDesc = new BufferDescription()
            {
                Usage = ResourceUsage.Dynamic,
                BindFlags = BindFlags.ConstantBuffer,
                SizeInBytes = Constants.size,
                CpuAccessFlags = CpuAccessFlags.Write,
                StructureByteStride = 0,
                OptionFlags = 0,
            };
            constantBuffer = new SharpDX.Direct3D11.Buffer(device, constantBufferDesc);

            // user view sampler state
            var colorSamplerStateDesc = new SamplerStateDescription()
            {
                Filter = Filter.MinMagMipLinear,
                AddressU = TextureAddressMode.Border,
                AddressV = TextureAddressMode.Border,
                AddressW = TextureAddressMode.Border,
                //BorderColor = new SharpDX.Color4(0.5f, 0.5f, 0.5f, 1.0f),
                BorderColor = new SharpDX.Color4(0, 0, 0, 1.0f),
            };
            colorSamplerState = new SamplerState(device, colorSamplerStateDesc);

            vertexInputLayout = new InputLayout(device, shaderByteCode.Data, new[]
            {
                new InputElement("SV_POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
            });

        }
Exemplo n.º 6
0
        public LightShader(Device device)
        {
            var vertexShaderByteCode = ShaderBytecode.CompileFromFile(ShaderFileName, "LightVertexShader", "vs_4_0", ShaderFlags);
            var pixelShaderByteCode = ShaderBytecode.CompileFromFile(ShaderFileName, "LightPixelShader", "ps_4_0", ShaderFlags);

            VertexShader = new VertexShader(device, vertexShaderByteCode);
            PixelShader = new PixelShader(device, pixelShaderByteCode);

            Layout = VertexDefinition.PositionTextureNormal.GetInputLayout(device, vertexShaderByteCode);

            vertexShaderByteCode.Dispose();
            pixelShaderByteCode.Dispose();

            ConstantMatrixBuffer = new Buffer(device, MatrixBufferDesription);

            // Setup the description of the dynamic matrix constant buffer that is in the vertex shader.
            var lightBufferDesc = new BufferDescription
            {
                Usage = ResourceUsage.Dynamic, // Updated each frame
                SizeInBytes = Utilities.SizeOf<LightBuffer>(), // Contains three matrices
                BindFlags = BindFlags.ConstantBuffer,
                CpuAccessFlags = CpuAccessFlags.Write,
                OptionFlags = ResourceOptionFlags.None,
                StructureByteStride = 0
            };
            ConstantLightBuffer = new Buffer(device, lightBufferDesc);

            // Setup the description of the dynamic matrix constant buffer that is in the vertex shader.
            var cameraBufferDesc = new BufferDescription
            {
                Usage = ResourceUsage.Dynamic, // Updated each frame
                SizeInBytes = Utilities.SizeOf<CameraBuffer>(), // Contains three matrices
                BindFlags = BindFlags.ConstantBuffer,
                CpuAccessFlags = CpuAccessFlags.Write,
                OptionFlags = ResourceOptionFlags.None,
                StructureByteStride = 0
            };
            ConstantCameraBuffer = new Buffer(device, cameraBufferDesc);

            // Create a texture sampler state description.
            var samplerDesc = new SamplerStateDescription
            {
                Filter = Filter.Anisotropic,
                AddressU = TextureAddressMode.Wrap,
                AddressV = TextureAddressMode.Wrap,
                AddressW = TextureAddressMode.Wrap,
                MipLodBias = 0,
                MaximumAnisotropy = 16,
                ComparisonFunction = Comparison.Always,
                BorderColor = new Color4(0, 0, 0, 0),
                MinimumLod = 0,
                MaximumLod = 10
            };

            // Create the texture sampler state.
            SamplerState = new SamplerState(device, samplerDesc);
        }
Exemplo n.º 7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SamplerState" /> class.
 /// </summary>
 /// <param name="device">The <see cref="GraphicsDevice"/>.</param>
 /// <param name="description">The description.</param>
 private SamplerState(GraphicsDevice device, SamplerStateDescription description) : base(device.MainDevice)
 {
     // For 9.1, anisotropy cannot be larger then 2
     if (device.Features.Level == FeatureLevel.Level_9_1)
     {
         description.MaximumAnisotropy = Math.Min(2, description.MaximumAnisotropy);
     }
     Description = description;
     Initialize(new SharpDX.Direct3D11.SamplerState(device, description));
 }
Exemplo n.º 8
0
 protected override void InitializeInternal()
 {
     var descr = new SamplerStateDescription
     {
         AddressU = GetTextureAddressMode(Settings.WrapU),
         AddressV = GetTextureAddressMode(Settings.WrapV),
         AddressW = GetTextureAddressMode(Settings.WrapW),
         Filter = GetFilterType(Settings.Filter)
     };
     SamplerState = new SamplerState(DeviceManager.Device, descr);
 }
Exemplo n.º 9
0
        public static D3D.SamplerState CreateSamplerState(this D3D.Device device, WrapMode wrapMode, InterpolationMode interpolationMode)
        {
            var desc = new D3D.SamplerStateDescription();

            if (wrapMode.HasFlag(WrapMode.Tile))
            {
                desc.AddressU = D3D.TextureAddressMode.Wrap;
                desc.AddressV = D3D.TextureAddressMode.Wrap;
            }
            else
            {
                desc.AddressU = D3D.TextureAddressMode.Clamp;
                desc.AddressV = D3D.TextureAddressMode.Clamp;
            }
            if (wrapMode.HasFlag(WrapMode.MirrorX))
            {
                desc.AddressU = D3D.TextureAddressMode.Mirror;
            }
            if (wrapMode.HasFlag(WrapMode.MirrorY))
            {
                desc.AddressV = D3D.TextureAddressMode.Mirror;
            }
            desc.AddressW = D3D.TextureAddressMode.Clamp;

            switch (interpolationMode)
            {
            case InterpolationMode.Linear:
                desc.Filter = D3D.Filter.MinMagMipLinear;
                break;

            case InterpolationMode.Anisotropic:
                desc.Filter = D3D.Filter.Anisotropic;
                break;

            default:
                desc.Filter = D3D.Filter.MinLinearMagMipPoint;
                break;
            }

            desc.ComparisonFunction = D3D.Comparison.Never;
            desc.MaximumAnisotropy  = 16;
            desc.MinimumLod         = 0;
            desc.MaximumLod         = float.MaxValue;

            var samplerState = new D3D.SamplerState(device, desc);

            return(samplerState);
        }
Exemplo n.º 10
0
        public TextureMap(int w, int h)
        {
            m_width = (ushort)w;
            m_height = (ushort)h;

            // Create a texture sampler default state description.
            m_samplerDesc = new SamplerStateDescription
            {
                Filter = Filter.MinMagMipPoint,
                AddressU = TextureAddressMode.Wrap,
                AddressV = TextureAddressMode.Wrap,
                AddressW = TextureAddressMode.Wrap,
                ComparisonFunction = Comparison.Never,
                MinimumLod = 0,
                MaximumLod = 0,
            };
        }
        internal static void InitOnce()
        {
            SamplerStateDescription description = new SamplerStateDescription();
            description.AddressU = TextureAddressMode.Clamp;
            description.AddressV = TextureAddressMode.Clamp;
            description.AddressW = TextureAddressMode.Clamp;
            description.Filter = Filter.MinMagMipLinear;
            description.MaximumLod = System.Single.MaxValue;
            m_default = MyPipelineStates.CreateSamplerState(description);

            description.AddressU = TextureAddressMode.Border;
            description.AddressV = TextureAddressMode.Border;
            description.AddressW = TextureAddressMode.Border;
            description.Filter = Filter.MinMagMipLinear;
            description.MaximumLod = System.Single.MaxValue;
            description.BorderColor = new Color4(0, 0, 0, 0);
            m_alphamask = MyPipelineStates.CreateSamplerState(description);

            description.AddressU = TextureAddressMode.Clamp;
            description.AddressV = TextureAddressMode.Clamp;
            description.AddressW = TextureAddressMode.Clamp;
            description.Filter = Filter.MinMagMipPoint;
            description.MaximumLod = System.Single.MaxValue;
            m_point = MyPipelineStates.CreateSamplerState(description);

            description.Filter = Filter.MinMagMipLinear;
            description.MaximumLod = System.Single.MaxValue;
            m_linear = MyPipelineStates.CreateSamplerState(description);

            description.AddressU = TextureAddressMode.Clamp;
            description.AddressV = TextureAddressMode.Clamp;
            description.AddressW = TextureAddressMode.Clamp;
            description.Filter = Filter.ComparisonMinMagMipLinear;
            description.MaximumLod = System.Single.MaxValue;
            description.ComparisonFunction = Comparison.LessEqual;
            m_shadowmap = MyPipelineStates.CreateSamplerState(description);

            m_texture = MyPipelineStates.CreateSamplerState(description);
            m_alphamaskArray = MyPipelineStates.CreateSamplerState(description);

            UpdateFiltering();

            Init();
        }
        private static void InitilizeSamplerStates()
        {
            SamplerStateDescription description = new SamplerStateDescription();
            description.AddressU = TextureAddressMode.Clamp;
            description.AddressV = TextureAddressMode.Clamp;
            description.AddressW = TextureAddressMode.Clamp;
            description.Filter = Filter.MinMagMipLinear;
            description.MaximumLod = System.Single.MaxValue;
            m_defaultSamplerState = MyPipelineStates.CreateSamplerState(description);

            description.AddressU = TextureAddressMode.Border;
            description.AddressV = TextureAddressMode.Border;
            description.AddressW = TextureAddressMode.Border;
            description.Filter = Filter.MinMagMipLinear;
            description.MaximumLod = System.Single.MaxValue;
            description.BorderColor = new Color4(0, 0, 0, 0);
            m_alphamaskSamplerState = MyPipelineStates.CreateSamplerState(description);

            description.AddressU = TextureAddressMode.Clamp;
            description.AddressV = TextureAddressMode.Clamp;
            description.AddressW = TextureAddressMode.Clamp;
            description.Filter = Filter.MinMagMipPoint;
            description.MaximumLod = System.Single.MaxValue;
            m_pointSamplerState = MyPipelineStates.CreateSamplerState(description);

            description.Filter = Filter.MinMagMipLinear;
            description.MaximumLod = System.Single.MaxValue;
            m_linearSamplerState = MyPipelineStates.CreateSamplerState(description);

            description.AddressU = TextureAddressMode.Clamp;
            description.AddressV = TextureAddressMode.Clamp;
            description.AddressW = TextureAddressMode.Clamp;
            description.Filter = Filter.ComparisonMinMagMipLinear;
            description.MaximumLod = System.Single.MaxValue;
            description.ComparisonFunction = Comparison.LessEqual;
            m_shadowmapSamplerState = MyPipelineStates.CreateSamplerState(description);

            m_textureSamplerState = MyPipelineStates.CreateSamplerState(description);
            m_alphamaskarraySamplerState = MyPipelineStates.CreateSamplerState(description);

            UpdateTextureSampler(m_textureSamplerState, TextureAddressMode.Wrap);
            UpdateTextureSampler(m_alphamaskarraySamplerState, TextureAddressMode.Clamp);
        }
Exemplo n.º 13
0
        public Sampler(GxContext context)
        {
            mContext = context;
            mDescription = new SamplerStateDescription
            {
                AddressU = TextureAddressMode.Wrap,
                AddressV = TextureAddressMode.Wrap,
                AddressW = TextureAddressMode.Wrap,
                BorderColor = SharpDX.Color4.Black,
                ComparisonFunction = Comparison.Always,
                Filter = Filter.MinMagMipLinear,
                MaximumAnisotropy = 0,
                MaximumLod = float.MaxValue,
                MinimumLod = float.MinValue,
                MipLodBias = 0.0f
            };

            mChanged = true;
        }
Exemplo n.º 14
0
        /// <summary>
        /// Function to build the D3D11 sampler state.
        /// </summary>
        /// <param name="device">The D3D11 device to use to create the sampler.</param>
        internal void BuildD3D11SamplerState(D3D11.Device5 device)
        {
            Debug.Assert(Native == null, "Already have a native sampler state.");

            var desc = new D3D11.SamplerStateDescription
            {
                BorderColor        = BorderColor.ToRawColor4(),
                ComparisonFunction = (D3D11.Comparison)ComparisonFunction,
                Filter             = (D3D11.Filter)Filter,
                AddressW           = (D3D11.TextureAddressMode)WrapW,
                AddressV           = (D3D11.TextureAddressMode)WrapV,
                AddressU           = (D3D11.TextureAddressMode)WrapU,
                MipLodBias         = MipLevelOfDetailBias,
                MinimumLod         = MinimumLevelOfDetail,
                MaximumLod         = MaximumLevelOfDetail,
                MaximumAnisotropy  = MaxAnisotropy
            };

            Native = new D3D11.SamplerState(device, desc);
        }
Exemplo n.º 15
0
        /// <summary>
        /// Binds the effect shader to the specified <see cref="Device"/>.
        /// </summary>
        /// <param name="device">The device to bind the shader to.</param>
        /// <returns>If the binding was successful.</returns>
        public bool Initialize(Device device)
        {
            try
            {
                matrixBuffer = new Buffer(device, Matrix.SizeInBytes * 3, ResourceUsage.Dynamic, BindFlags.ConstantBuffer, CpuAccessFlags.Write, ResourceOptionFlags.None, 0) {DebugName = "Matrix buffer"};
                using (var bytecode = ShaderBytecode.CompileFromFile("Shaders/Texture.vs", "TextureVertexShader", "vs_4_0"))
                {
                    layout = new InputLayout(device, ShaderSignature.GetInputSignature(bytecode), TextureDrawingVertex.VertexDeclaration) { DebugName = "Color vertex layout" };
                    vertexShader = new VertexShader(device, bytecode) { DebugName = "Texture vertex shader" };
                }

                using (var bytecode = ShaderBytecode.CompileFromFile("Shaders/Texture.ps", "TexturePixelShader", "ps_4_0"))
                {

                    pixelShader = new PixelShader(device, bytecode) { DebugName = "Texture pixel shader" };
                }

                var samplerDesc = new SamplerStateDescription
                {
                    AddressU = TextureAddressMode.Wrap,
                    AddressV = TextureAddressMode.Wrap,
                    AddressW = TextureAddressMode.Wrap,
                    Filter = Filter.ComparisonMinMagMipLinear,
                    MaximumAnisotropy = 1,
                    MipLodBias = 0f,
                    MinimumLod = 0,
                    MaximumLod = float.MaxValue,
                    BorderColor = Color.LimeGreen,
                    ComparisonFunction = Comparison.Always
                };
                pixelSampler = new SamplerState(device, samplerDesc);

                texture = new Texture();
                return texture.Initialize(device, "Textures/dirt.dds");
            }
            catch (Exception e)
            {
                MessageBox.Show("Shader error: " + e.Message);
                return false;
            }
        }
Exemplo n.º 16
0
        /* CONSTRUCTORS & DESTRUCTOR */
        /// <summary>
        /// Constructs an blank texture object that has no effect on rendered objects.
        /// </summary>
        /// <param name="window">A reference to the Direct3D-capable window in which this texture will be rendered.</param>
        public Texture(Window3D window)
            : base()
        {
            Window = window;
            Window.OnClose += Dispose;

            _TSSD = new SamplerStateDescription()
            {
                AddressU			= TextureAddressMode.Wrap,
                AddressV			= TextureAddressMode.Wrap,
                AddressW			= TextureAddressMode.Wrap,
                BorderColor			= Color4.Black,
                ComparisonFunction	= Comparison.Never,
                Filter				= Filter.Anisotropic,
                MaximumAnisotropy	= 16,
                MaximumLod			= float.MaxValue,
                MinimumLod			= 0f,
                MipLodBias			= 0f,
            };
            UpdateSettings = true;
        }
        internal static void UpdateTextureSampler(SamplerId samplerState, TextureAddressMode addressMode)
        {
            SamplerStateDescription description = new SamplerStateDescription();
            description.AddressU = addressMode;
            description.AddressV = addressMode;
            description.AddressW = addressMode;
            description.MaximumLod = System.Single.MaxValue;

            if(MyRender11.RenderSettings.AnisotropicFiltering == MyTextureAnisoFiltering.NONE)
            {
                description.Filter = Filter.MinMagMipLinear;
            }
            else
            {
                description.Filter = Filter.Anisotropic;

                switch(MyRender11.RenderSettings.AnisotropicFiltering)
                {
                    case MyTextureAnisoFiltering.ANISO_1:
                        description.MaximumAnisotropy = 1;
                        break;
                    case MyTextureAnisoFiltering.ANISO_4:
                        description.MaximumAnisotropy = 4;
                        break;
                    case MyTextureAnisoFiltering.ANISO_8:
                        description.MaximumAnisotropy = 8;
                        break;
                    case MyTextureAnisoFiltering.ANISO_16:
                        description.MaximumAnisotropy = 16;
                        break;
                    default:
                        description.MaximumAnisotropy = 1;
                        break;
                }
            }

            MyPipelineStates.ChangeSamplerState(samplerState, description);
        }
Exemplo n.º 18
0
        internal static SamplerId CreateSamplerState(SamplerStateDescription description)
        {
            var id = new SamplerId { Index = SamplerStates.Allocate() };
            MyArrayHelpers.Reserve(ref SamplerObjects, id.Index + 1);

            SamplerStates.Data[id.Index] = description;

            InitSamplerState(id);
            SamplerIndices.Add(id);

            return id;
        }
Exemplo n.º 19
0
        internal static void Init()
        {
            m_proxyVs = MyShaders.CreateVs("clouds.hlsl");
            m_cloudPs = MyShaders.CreatePs("clouds.hlsl");
            m_proxyIL = MyShaders.CreateIL(m_proxyVs.BytecodeId, MyVertexLayouts.GetLayout(
                    new MyVertexInputComponent(MyVertexInputComponentType.POSITION_PACKED, 0),
                    new MyVertexInputComponent(MyVertexInputComponentType.NORMAL, 1),
                    new MyVertexInputComponent(MyVertexInputComponentType.TANGENT_SIGN_OF_BITANGENT, 1),
                    new MyVertexInputComponent(MyVertexInputComponentType.TEXCOORD0_H, 1)));

            m_fogShader = MyShaders.CreateCs("clouds.hlsl", new [] {new ShaderMacro("NUMTHREADS", m_numFogThreads)});

            SamplerStateDescription description = new SamplerStateDescription
            {
                AddressU = TextureAddressMode.Wrap,
                AddressV = TextureAddressMode.Wrap,
                AddressW = TextureAddressMode.Wrap,
                Filter = Filter.MinMagMipLinear,
                MaximumLod = System.Single.MaxValue
            };
            m_textureSampler = MyPipelineStates.CreateSamplerState(description);
        }
Exemplo n.º 20
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="device"></param>
        internal D3DSamplerState Apply( GraphicsDevice device )
        {
            if ( state == null ) {

                var ssd = new SamplerStateDescription();

                ssd.ComparisonFunction	=	Converter.Convert( this.compareFunc );
                ssd.AddressU			=	Converter.Convert( this.addressU );
                ssd.AddressV			=	Converter.Convert( this.addressV );
                ssd.AddressW			=	Converter.Convert( this.addressW );
                ssd.BorderColor			=	SharpDXHelper.Convert( this.borderColor );
                ssd.Filter				=	Converter.Convert( this.filter );
                ssd.MaximumAnisotropy	=	this.maxAnisotropy;
                ssd.MaximumLod			=	this.maxMipLevel;
                ssd.MinimumLod			=	this.minMipLevel;
                ssd.MipLodBias			=	this.mipMapBias;

                state	=	new D3DSamplerState( device.Device, ssd );
            }

            return state;
        }
Exemplo n.º 21
0
        public static D3D.SamplerState CreateSamplerState(this D3D.Device device, WrapMode wrapMode, InterpolationMode interpolationMode)
        {
            var desc = new D3D.SamplerStateDescription();

            if (wrapMode.HasFlag(WrapMode.Tile))
            {
                desc.AddressU = D3D.TextureAddressMode.Wrap;
                desc.AddressV = D3D.TextureAddressMode.Wrap;
            }
            else
            {
                desc.AddressU = D3D.TextureAddressMode.Clamp;
                desc.AddressV = D3D.TextureAddressMode.Clamp;
            }
            if (wrapMode.HasFlag(WrapMode.MirrorX)) desc.AddressU = D3D.TextureAddressMode.Mirror;
            if (wrapMode.HasFlag(WrapMode.MirrorY)) desc.AddressV = D3D.TextureAddressMode.Mirror;
            desc.AddressW = D3D.TextureAddressMode.Clamp;

            switch (interpolationMode)
            {
                case InterpolationMode.Linear:
                    desc.Filter = D3D.Filter.MinMagMipLinear;
                    break;
                case InterpolationMode.Anisotropic:
                    desc.Filter = D3D.Filter.Anisotropic;
                    break;
                default:
                    desc.Filter = D3D.Filter.MinLinearMagMipPoint;
                    break;
            }

            desc.ComparisonFunction = D3D.Comparison.Never;
            desc.MaximumAnisotropy = 16;
            desc.MinimumLod = 0;
            desc.MaximumLod = float.MaxValue;

            var samplerState = new D3D.SamplerState(device, desc);
            return samplerState;
        }
Exemplo n.º 22
0
 /// <summary>	
 /// <p>Create a sampler-state object that encapsulates sampling information for a texture.</p>	
 /// </summary>	
 /// <param name="device">The <see cref="GraphicsDevice"/>.</param>
 /// <param name="description">A sampler state description</param>	
 /// <returns>A new <see cref="SamplerState"/> instance</returns>	
 /// <remarks>	
 /// <p>4096 unique sampler state objects can be created on a device at a time.</p><p>If an application attempts to create a sampler-state interface with the same state as an existing interface, the same interface will be returned and the total number of unique sampler state objects will stay the same.</p>	
 /// </remarks>	
 /// <msdn-id>ff476518</msdn-id>	
 /// <unmanaged>HRESULT ID3D11Device::CreateSamplerState([In] const D3D11_SAMPLER_DESC* pSamplerDesc,[Out, Fast] ID3D11SamplerState** ppSamplerState)</unmanaged>	
 /// <unmanaged-short>ID3D11Device::CreateSamplerState</unmanaged-short>	
 public static SamplerState New(GraphicsDevice device, SamplerStateDescription description)
 {
     return new SamplerState(device, description);
 }
Exemplo n.º 23
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SamplerState" /> class.
 /// </summary>
 /// <param name="device">The <see cref="GraphicsDevice"/>.</param>
 /// <param name="nativeState">State of the native.</param>
 private SamplerState(GraphicsDevice device, Direct3D11.SamplerState nativeState) : base(device.MainDevice)
 {
     Description = nativeState.Description;
     Initialize(nativeState);
 }
Exemplo n.º 24
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SamplerState" /> class.
 /// </summary>
 /// <param name="device">The <see cref="GraphicsDevice"/>.</param>
 /// <param name="description">The description.</param>
 private SamplerState(GraphicsDevice device, SamplerStateDescription description) : base(device.MainDevice)
 {
     Description = description;
     Initialize(new SharpDX.Direct3D11.SamplerState(device, description));
 }
Exemplo n.º 25
0
 /// <summary>	
 /// <p>Create a sampler-state object that encapsulates sampling information for a texture.</p>	
 /// </summary>	
 /// <param name="device">The <see cref="GraphicsDevice"/>.</param>
 /// <param name="name">Name of this sampler state.</param>
 /// <param name="description">A sampler state description</param>	
 /// <returns>A new <see cref="SamplerState"/> instance</returns>	
 /// <remarks>	
 /// <p>4096 unique sampler state objects can be created on a device at a time.</p><p>If an application attempts to create a sampler-state interface with the same state as an existing interface, the same interface will be returned and the total number of unique sampler state objects will stay the same.</p>	
 /// </remarks>	
 /// <msdn-id>ff476518</msdn-id>	
 /// <unmanaged>HRESULT ID3D11Device::CreateSamplerState([In] const D3D11_SAMPLER_DESC* pSamplerDesc,[Out, Fast] ID3D11SamplerState** ppSamplerState)</unmanaged>	
 /// <unmanaged-short>ID3D11Device::CreateSamplerState</unmanaged-short>	
 public static SamplerState New(GraphicsDevice device, string name, SamplerStateDescription description)
 {
     return new SamplerState(device, description) {Name = name};
 }
        public DepthAndColorShader(Device device)
        {
            shaderByteCode = new ShaderBytecode(File.ReadAllBytes("Content/DepthAndColorFloatVS.cso"));
            depthAndColorVS = new VertexShader(device, shaderByteCode);
            depthAndColorGS = new GeometryShader(device, new ShaderBytecode(File.ReadAllBytes("Content/DepthAndColorGS.cso")));
            depthAndColorPS = new PixelShader(device, new ShaderBytecode(File.ReadAllBytes("Content/DepthAndColorPS.cso")));

            // depth stencil state
            var depthStencilStateDesc = new DepthStencilStateDescription()
            {
                IsDepthEnabled = true,
                DepthWriteMask = DepthWriteMask.All,
                DepthComparison = Comparison.LessEqual,
                IsStencilEnabled = false,
            };
            depthStencilState = new DepthStencilState(device, depthStencilStateDesc);

            // rasterizer state
            var rasterizerStateDesc = new RasterizerStateDescription()
            {
                CullMode = CullMode.None,
                FillMode = FillMode.Solid,
                IsDepthClipEnabled = true,
                IsFrontCounterClockwise = true,
                IsMultisampleEnabled = true,
            };
            rasterizerState = new RasterizerState(device, rasterizerStateDesc);

            // color sampler state
            var colorSamplerStateDesc = new SamplerStateDescription()
            {
                Filter = Filter.MinMagMipLinear,
                AddressU = TextureAddressMode.Border,
                AddressV = TextureAddressMode.Border,
                AddressW = TextureAddressMode.Border,
                //BorderColor = new SharpDX.Color4(0.5f, 0.5f, 0.5f, 1.0f),
                BorderColor = new SharpDX.Color4(0, 0, 0, 1.0f),
            };
            colorSamplerState = new SamplerState(device, colorSamplerStateDesc);

            //// Kinect depth image
            //var depthImageTextureDesc = new Texture2DDescription()
            //{
            //    Width = depthImageWidth,
            //    Height = depthImageHeight,
            //    MipLevels = 1,
            //    ArraySize = 1,
            //    Format = SharpDX.DXGI.Format.R16_UInt, // R32_Float
            //    SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
            //    Usage = ResourceUsage.Dynamic,
            //    BindFlags = BindFlags.ShaderResource,
            //    CpuAccessFlags = CpuAccessFlags.Write,
            //};
            //depthImageTexture = new Texture2D(device, depthImageTextureDesc);
            //depthImageTextureRV = new ShaderResourceView(device, depthImageTexture);

            // filtered depth image
            var filteredDepthImageTextureDesc = new Texture2DDescription()
            {
                Width = Kinect2Calibration.depthImageWidth * 3,
                Height = Kinect2Calibration.depthImageHeight * 3,
                MipLevels = 1,
                ArraySize = 1,
                Format = SharpDX.DXGI.Format.R32G32_Float,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                Usage = ResourceUsage.Default,
                BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,
                CpuAccessFlags = CpuAccessFlags.None,
            };
            filteredDepthImageTexture = new Texture2D(device, filteredDepthImageTextureDesc);
            filteredRenderTargetView = new RenderTargetView(device, filteredDepthImageTexture);
            filteredDepthImageSRV = new ShaderResourceView(device, filteredDepthImageTexture);

            filteredDepthImageTexture2 = new Texture2D(device, filteredDepthImageTextureDesc);
            filteredRenderTargetView2 = new RenderTargetView(device, filteredDepthImageTexture2);
            filteredDepthImageSRV2 = new ShaderResourceView(device, filteredDepthImageTexture2);

            //// Kinect color image
            //var colorImageStagingTextureDesc = new Texture2DDescription()
            //{
            //    Width = colorImageWidth,
            //    Height = colorImageHeight,
            //    MipLevels = 1,
            //    ArraySize = 1,
            //    Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
            //    //Format = SharpDX.DXGI.Format.YUY2
            //    SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
            //    Usage = ResourceUsage.Dynamic,
            //    BindFlags = BindFlags.ShaderResource,
            //    CpuAccessFlags = CpuAccessFlags.Write
            //};
            //colorImageStagingTexture = new Texture2D(device, colorImageStagingTextureDesc);

            //var colorImageTextureDesc = new Texture2DDescription()
            //{
            //    Width = colorImageWidth,
            //    Height = colorImageHeight,
            //    MipLevels = 0,
            //    ArraySize = 1,
            //    Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
            //    SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
            //    Usage = ResourceUsage.Default,
            //    BindFlags = BindFlags.ShaderResource | BindFlags.RenderTarget,
            //    CpuAccessFlags = CpuAccessFlags.None,
            //    OptionFlags = ResourceOptionFlags.GenerateMipMaps
            //};
            //colorImageTexture = new Texture2D(device, colorImageTextureDesc);
            //colorImageTextureRV = new ShaderResourceView(device, colorImageTexture);

            // constant buffer
            var constantBufferDesc = new BufferDescription()
            {
                Usage = ResourceUsage.Dynamic,
                BindFlags = BindFlags.ConstantBuffer,
                SizeInBytes = ConstantBuffer.size,
                CpuAccessFlags = CpuAccessFlags.Write,
                StructureByteStride = 0,
                OptionFlags = 0,
            };
            constantBuffer = new SharpDX.Direct3D11.Buffer(device, constantBufferDesc);

            bilateralFilter = new BilateralFilter(device, Kinect2Calibration.depthImageWidth, Kinect2Calibration.depthImageHeight);

            vertexInputLayout = new InputLayout(device, shaderByteCode.Data, new[]
            {
                new InputElement("SV_POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
            });
        }
        internal Engine_Material(string _shaderFileName, string _imageFileName, bool _includeGeometryShader = false)
        {
            #region //Get Instances
            m_d3d = Engine_Renderer.Instance;
            #endregion

            #region //Create InputLayout
            var inputElements = new D3D11.InputElement[] {
                new D3D11.InputElement("POSITION", 0, DXGI.Format.R32G32B32_Float, 0, 0),
                new D3D11.InputElement("TEXCOORD", 0, DXGI.Format.R32G32_Float, D3D11.InputElement.AppendAligned, 0),
                new D3D11.InputElement("NORMAL", 0, DXGI.Format.R32G32B32_Float, D3D11.InputElement.AppendAligned, 0)
            };
            #endregion

            #region //Create VertexShader
            CompilationResult vsResult;
            using (vsResult = ShaderBytecode.CompileFromFile(_shaderFileName, "VS", "vs_4_0", ShaderFlags.None))
            {
                m_vertexShader = new D3D11.VertexShader(m_d3d.m_device, vsResult.Bytecode.Data);
                m_inputLayout  = new D3D11.InputLayout(m_d3d.m_device, vsResult.Bytecode, inputElements);
            }
            #endregion

            #region //Create PixelShader
            using (var psResult = ShaderBytecode.CompileFromFile(_shaderFileName, "PS", "ps_4_0", ShaderFlags.None))
                m_pixelShader = new D3D11.PixelShader(m_d3d.m_device, psResult.Bytecode.Data);
            #endregion

            #region //Create GeometryShader
            if (_includeGeometryShader)
            {
                using (var psResult = ShaderBytecode.CompileFromFile(_shaderFileName, "GS", "gs_4_0", ShaderFlags.None))
                    m_geometryShader = new D3D11.GeometryShader(m_d3d.m_device, psResult.Bytecode.Data);
            }
            #endregion

            #region //Create ConstantBuffers for Model
            SPerModelConstantBuffer cbModel = new SPerModelConstantBuffer();

            D3D11.BufferDescription bufferDescription = new D3D11.BufferDescription
            {
                BindFlags      = D3D11.BindFlags.ConstantBuffer,
                CpuAccessFlags = D3D11.CpuAccessFlags.Write,
                Usage          = D3D11.ResourceUsage.Dynamic,
            };

            m_model = D3D11.Buffer.Create(m_d3d.m_device, D3D11.BindFlags.ConstantBuffer, ref cbModel);
            #endregion

            #region //Create Texture and Sampler
            var texture = Engine_ImgLoader.CreateTexture2DFromBitmap(m_d3d.m_device, Engine_ImgLoader.LoadBitmap(new SharpDX.WIC.ImagingFactory2(), _imageFileName));
            m_resourceView = new D3D11.ShaderResourceView(m_d3d.m_device, texture);

            D3D11.SamplerStateDescription samplerStateDescription = new D3D11.SamplerStateDescription
            {
                Filter             = D3D11.Filter.Anisotropic,
                AddressU           = D3D11.TextureAddressMode.Clamp,
                AddressV           = D3D11.TextureAddressMode.Clamp,
                AddressW           = D3D11.TextureAddressMode.Clamp,
                ComparisonFunction = D3D11.Comparison.Always,
                MaximumAnisotropy  = 16,
                MinimumLod         = 0,
                MaximumLod         = float.MaxValue,
            };

            m_sampler = new D3D11.SamplerState(m_d3d.m_device, samplerStateDescription);
            #endregion
        }
Exemplo n.º 28
0
        public void Initialize()
        {
            LoadSky(0);

            Texture2DDescription descTex = new Texture2DDescription();
            descTex.ArraySize = 6;
            descTex.Width = Size;
            descTex.Height = Size;
            descTex.Usage = ResourceUsage.Default;
            descTex.CpuAccessFlags = CpuAccessFlags.None;
            descTex.Format = SharpDX.DXGI.Format.R8G8B8A8_UNorm;
            descTex.SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0);
            descTex.MipLevels = MipLevel;
            descTex.BindFlags = BindFlags.ShaderResource | BindFlags.RenderTarget;
            descTex.OptionFlags = ResourceOptionFlags.GenerateMipMaps | ResourceOptionFlags.TextureCube;

            //Create the texture and shader view
            CubeTexture = new Texture2D(ModelViewer.Program.device, descTex);
            CubeSRV = new ShaderResourceView(ModelViewer.Program.device, CubeTexture);

            RenderTargetViewDescription RTVD = new RenderTargetViewDescription();
            RTVD.Format = descTex.Format;
            RTVD.Dimension = RenderTargetViewDimension.Texture2DArray;
            RTVD.Texture2DArray.FirstArraySlice = 0;
            RTVD.Texture2DArray.ArraySize = 1;
            RTVD.Texture2DArray.MipSlice = 0;

            CubeRenderTarget = new RenderTargetView[6];

            for (int i = 0; i < 6; i++)
            {
                RTVD.Texture2DArray.FirstArraySlice = i;
                CubeRenderTarget[i] = new RenderTargetView(ModelViewer.Program.device, CubeTexture, RTVD);
            }

            SamplerStateDescription a = new SamplerStateDescription();
            a.AddressU = TextureAddressMode.Clamp;
            a.AddressV = TextureAddressMode.Clamp;
            a.AddressW = TextureAddressMode.Clamp;
            a.Filter = Filter.MinMagMipLinear;
            SSWrapMipLinear = new SamplerState(ModelViewer.Program.device, a);

            MContains = new MeshContainer();
            MContains.BytesPerVertex = 20;
            MContains.FaceCount = 12;
            MContains.VertexsCount = 24;

            Vector3 vExtents = new Vector3(500, 500, 500);

            var vertices = new DataStream(MContains.BytesPerVertex * MContains.VertexsCount, true, true);

            //Back
            vertices.Write(new Vector3(-vExtents.X, -vExtents.Y, vExtents.Z));
            vertices.Write(new Vector2(1, 1));
            vertices.Write(new Vector3(-vExtents.X, vExtents.Y, vExtents.Z));
            vertices.Write(new Vector2(1, 0));
            vertices.Write(new Vector3(vExtents.X, vExtents.Y, vExtents.Z));
            vertices.Write(new Vector2(0, 0));
            vertices.Write(new Vector3(vExtents.X, -vExtents.Y, vExtents.Z));
            vertices.Write(new Vector2(0, 1));
            //Front
            vertices.Write(new Vector3(vExtents.X, -vExtents.Y, -vExtents.Z));
            vertices.Write(new Vector2(1, 1));
            vertices.Write(new Vector3(vExtents.X, vExtents.Y, -vExtents.Z));
            vertices.Write(new Vector2(1, 0));
            vertices.Write(new Vector3(-vExtents.X, vExtents.Y, -vExtents.Z));
            vertices.Write(new Vector2(0, 0));
            vertices.Write(new Vector3(-vExtents.X, -vExtents.Y, -vExtents.Z));
            vertices.Write(new Vector2(0, 1));
            //Bottom
            vertices.Write(new Vector3(-vExtents.X, -vExtents.Y, -vExtents.Z));
            vertices.Write(new Vector2(0, 0));
            vertices.Write(new Vector3(-vExtents.X, -vExtents.Y, vExtents.Z));
            vertices.Write(new Vector2(0, 1));
            vertices.Write(new Vector3(vExtents.X, -vExtents.Y, vExtents.Z));
            vertices.Write(new Vector2(1, 1));
            vertices.Write(new Vector3(vExtents.X, -vExtents.Y, -vExtents.Z));
            vertices.Write(new Vector2(1, 0));
            //Top
            vertices.Write(new Vector3(vExtents.X, vExtents.Y, -vExtents.Z));
            vertices.Write(new Vector2(1, 1));
            vertices.Write(new Vector3(vExtents.X, vExtents.Y, vExtents.Z));
            vertices.Write(new Vector2(1, 0));
            vertices.Write(new Vector3(-vExtents.X, vExtents.Y, vExtents.Z));
            vertices.Write(new Vector2(0, 0));
            vertices.Write(new Vector3(-vExtents.X, vExtents.Y, -vExtents.Z));
            vertices.Write(new Vector2(0, 1));
            //Left
            vertices.Write(new Vector3(-vExtents.X, vExtents.Y, -vExtents.Z));
            vertices.Write(new Vector2(1, 0));
            vertices.Write(new Vector3(-vExtents.X, vExtents.Y, vExtents.Z));
            vertices.Write(new Vector2(0, 0));
            vertices.Write(new Vector3(-vExtents.X, -vExtents.Y, vExtents.Z));
            vertices.Write(new Vector2(0, 1));
            vertices.Write(new Vector3(-vExtents.X, -vExtents.Y, -vExtents.Z));
            vertices.Write(new Vector2(1, 1));
            //Right
            vertices.Write(new Vector3(vExtents.X, -vExtents.Y, -vExtents.Z));
            vertices.Write(new Vector2(0, 1));
            vertices.Write(new Vector3(vExtents.X, -vExtents.Y, vExtents.Z));
            vertices.Write(new Vector2(1, 1));
            vertices.Write(new Vector3(vExtents.X, vExtents.Y, vExtents.Z));
            vertices.Write(new Vector2(1, 0));
            vertices.Write(new Vector3(vExtents.X, vExtents.Y, -vExtents.Z));
            vertices.Write(new Vector2(0, 0));

            vertices.Position = 0;
            InputElement[] elements11 = new[] {
                new InputElement("POSITION", 0, SharpDX.DXGI.Format.R32G32B32_Float, 0, 0) ,
                new InputElement("TEXCOORD", 0, SharpDX.DXGI.Format.R32G32_Float, 12, 0)};

            MContains.Vertexs = new Buffer(ModelViewer.Program.device, vertices, MContains.BytesPerVertex * MContains.VertexsCount, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);

            var indices = new DataStream(4 * MContains.FaceCount * 3, true, true);
            for (int x = 0; x < 6; x++)
            {
                indices.Write((int)(x * 4 + 0));
                indices.Write((int)(x * 4 + 1));
                indices.Write((int)(x * 4 + 2));

                indices.Write((int)(x * 4 + 2));
                indices.Write((int)(x * 4 + 3));
                indices.Write((int)(x * 4 + 0));
            }
            indices.Position = 0;

            MContains.Indices = new Buffer(ModelViewer.Program.device, indices, 4 * MContains.FaceCount * 3, ResourceUsage.Default, BindFlags.IndexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);
            MContains.binding = new VertexBufferBinding(MContains.Vertexs, MContains.BytesPerVertex, 0);

            EEEM = ContentManager.LoadEffect("Content/Shaders/SkyBox", elements11);

            // Подготовка константного буффера
            BufferDescription bd = new BufferDescription();
            bd.SizeInBytes = Marshal.SizeOf(typeof(SkyShaderConstants));
            bd.Usage = ResourceUsage.Dynamic;
            bd.BindFlags = BindFlags.ConstantBuffer;
            bd.CpuAccessFlags = CpuAccessFlags.Write;
            bd.OptionFlags = ResourceOptionFlags.None;
            bd.StructureByteStride = 0;

            SkyConstantsBuffer = new Buffer(ModelViewer.Program.device, bd);
            SSC = new SkyShaderConstants();
        }
Exemplo n.º 29
0
 internal static void ChangeSamplerState(SamplerId id, SamplerStateDescription description)
 {
     SamplerStates.Data[id.Index] = description;
     if(SamplerObjects[id.Index] != null)
     {
         SamplerObjects[id.Index].Dispose();
     }
     SamplerObjects[id.Index] = new SamplerState(MyRender11.Device, description);
 }
Exemplo n.º 30
0
 /// <summary>
 ///   Constructs a new <see cref = "T:SharpDX.Direct3D11.SamplerState" /> based on the specified description.
 /// </summary>
 /// <param name = "device">The device with which to associate the state object.</param>
 /// <param name = "description">The state description.</param>
 /// <returns>The newly created object.</returns>
 /// <msdn-id>ff476518</msdn-id>	
 /// <unmanaged>HRESULT ID3D11Device::CreateSamplerState([In] const D3D11_SAMPLER_DESC* pSamplerDesc,[Out, Fast] ID3D11SamplerState** ppSamplerState)</unmanaged>	
 /// <unmanaged-short>ID3D11Device::CreateSamplerState</unmanaged-short>	
 public SamplerState(Device device, ref SamplerStateDescription description)
     : base(IntPtr.Zero)
 {
     device.CreateSamplerState(ref description, this);
 }
Exemplo n.º 31
0
 /// <summary>
 ///   Constructs a new <see cref = "T:SharpDX.Direct3D11.SamplerState" /> based on the specified description.
 /// </summary>
 /// <param name = "device">The device with which to associate the state object.</param>
 /// <param name = "description">The state description.</param>
 /// <returns>The newly created object.</returns>
 /// <msdn-id>ff476518</msdn-id>
 /// <unmanaged>HRESULT ID3D11Device::CreateSamplerState([In] const D3D11_SAMPLER_DESC* pSamplerDesc,[Out, Fast] ID3D11SamplerState** ppSamplerState)</unmanaged>
 /// <unmanaged-short>ID3D11Device::CreateSamplerState</unmanaged-short>
 public SamplerState(Device device, SamplerStateDescription description)
     : base(IntPtr.Zero)
 {
     device.CreateSamplerState(ref description, this);
 }
Exemplo n.º 32
0
        public WaterShader(Device device)
        {
            var vertexShaderByteCode = ShaderBytecode.CompileFromFile(VertexShaderFileName, "WaterVertexShader", "vs_4_0", ShaderFlags.Debug, EffectFlags.None);
            var pixelShaderByteCode = ShaderBytecode.CompileFromFile(PixelShaderFileName, "WaterPixelShader", "ps_4_0", ShaderFlags.Debug, EffectFlags.None);

            VertexShader = new VertexShader(device, vertexShaderByteCode);
            PixelShader = new PixelShader(device, pixelShaderByteCode);

            Layout = VertexDefinition.PositionTexture.GetInputLayout(device, vertexShaderByteCode);

            vertexShaderByteCode.Dispose();
            pixelShaderByteCode.Dispose();

            // Setup the description of the dynamic matrix constant buffer that is in the vertex shader.
            var matrixBufferDesc = new BufferDescription
            {
                Usage = ResourceUsage.Dynamic, // Updated each frame
                SizeInBytes = Utilities.SizeOf<MatrixBuffer>(), // Contains three matrices
                BindFlags = BindFlags.ConstantBuffer,
                CpuAccessFlags = CpuAccessFlags.Write,
                OptionFlags = ResourceOptionFlags.None,
                StructureByteStride = 0
            };
            ConstantMatrixBuffer = new Buffer(device, matrixBufferDesc);

            // Setup the description of the dynamic matrix constant buffer that is in the vertex shader.
            var translateBufferDesc = new BufferDescription
            {
                Usage = ResourceUsage.Dynamic, // Updated each frame
                SizeInBytes = Utilities.SizeOf<TranslationBuffer>(), // Contains three matrices
                BindFlags = BindFlags.ConstantBuffer,
                CpuAccessFlags = CpuAccessFlags.Write,
                OptionFlags = ResourceOptionFlags.None,
                StructureByteStride = 0
            };
            ConstantTranslationBuffer = new Buffer(device, translateBufferDesc);

            // Setup the description of the dynamic matrix constant buffer that is in the vertex shader.
            var cameraPositionBufferDesc = new BufferDescription
            {
                Usage = ResourceUsage.Dynamic, // Updated each frame
                SizeInBytes = Utilities.SizeOf<TranslationBuffer>(), // Contains three matrices
                BindFlags = BindFlags.ConstantBuffer,
                CpuAccessFlags = CpuAccessFlags.Write,
                OptionFlags = ResourceOptionFlags.None,
                StructureByteStride = 0
            };
            ConstantCameraPositionBuffer = new Buffer(device, cameraPositionBufferDesc);

            // Create a texture sampler state description.
            var samplerDesc = new SamplerStateDescription
            {
                Filter = Filter.ComparisonMinLinearMagPointMipLinear,
                AddressU = TextureAddressMode.Wrap,
                AddressV = TextureAddressMode.Wrap,
                AddressW = TextureAddressMode.Wrap,
                MipLodBias = 0,
                MaximumAnisotropy = 4,
                ComparisonFunction = Comparison.Always,
                BorderColor = new Color4(0, 0, 0, 0),
                MinimumLod = 0,
                MaximumLod = float.MaxValue
            };

            // Create the texture sampler state.
            SamplerState = new SamplerState(device, samplerDesc);
        }
Exemplo n.º 33
0
        private bool InitializeShader(Device device, IntPtr windowHandler, string vsFileName, string psFileName)
        {
            try
            {
                // Setup full pathes
                vsFileName = SystemConfiguration.ShadersFilePath + vsFileName;
                psFileName = SystemConfiguration.ShadersFilePath + psFileName;

                // Compile the vertex shader code.
                var vertexShaderByteCode = ShaderBytecode.CompileFromFile(vsFileName, "TextureVertexShader", "vs_4_0", ShaderFlags.None, EffectFlags.None);
                // Compile the pixel shader code.
                var pixelShaderByteCode = ShaderBytecode.CompileFromFile(psFileName, "TexturePixelShader", "ps_4_0", ShaderFlags.None, EffectFlags.None);

                // Create the vertex shader from the buffer.
                VertexShader = new VertexShader(device, vertexShaderByteCode);
                // Create the pixel shader from the buffer.
                PixelShader = new PixelShader(device, pixelShaderByteCode);

                // Now setup the layout of the data that goes into the shader.
                // This setup needs to match the VertexType structure in the Model and in the shader.
                var inputElements = new InputElement[]
                {
                    new InputElement()
                    {
                        SemanticName = "POSITION",
                        SemanticIndex = 0,
                        Format = Format.R32G32B32_Float,
                        Slot = 0,
                        AlignedByteOffset = 0,
                        Classification = InputClassification.PerVertexData,
                        InstanceDataStepRate = 0
                    },
                    new InputElement()
                    {
                        SemanticName = "TEXCOORD",
                        SemanticIndex = 0,
                        Format = Format.R32G32_Float,
                        Slot = 0,
                        AlignedByteOffset = Vertex.AppendAlignedElement,
                        Classification = InputClassification.PerVertexData,
                        InstanceDataStepRate = 0
                    }
                };

                // Create the vertex input the layout.
                Layout = new InputLayout(device, ShaderSignature.GetInputSignature(vertexShaderByteCode), inputElements);

                // Release the vertex and pixel shader buffers, since they are no longer needed.
                vertexShaderByteCode.Dispose();
                pixelShaderByteCode.Dispose();

                // Setup the description of the dynamic matrix constant buffer that is in the vertex shader.
                var matrixBufferDesc = new BufferDescription()
                {
                    Usage = ResourceUsage.Dynamic,
                    SizeInBytes = Utilities.SizeOf<MatrixBuffer>(),
                    BindFlags = BindFlags.ConstantBuffer,
                    CpuAccessFlags = CpuAccessFlags.Write,
                    OptionFlags = ResourceOptionFlags.None,
                    StructureByteStride = 0
                };

                // Create the constant buffer pointer so we can access the vertex shader constant buffer from within this class.
                ConstantMatrixBuffer = new Buffer(device, matrixBufferDesc);

                // Create a texture sampler state description.
                var samplerDesc = new SamplerStateDescription()
                {
                    Filter = Filter.MinMagMipLinear,
                    AddressU = TextureAddressMode.Wrap,
                    AddressV = TextureAddressMode.Wrap,
                    AddressW = TextureAddressMode.Wrap,
                    MipLodBias = 0,
                    MaximumAnisotropy = 1,
                    ComparisonFunction = Comparison.Always,
                    BorderColor = new Color4(0, 0, 0, 0),
                    MinimumLod = 0,
                    MaximumLod = 0
                };

                // Create the texture sampler state.
                SampleState = new SamplerState(device, samplerDesc);

                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error initializing shader. Error is " + ex.Message);
                return false;
            };
        }
Exemplo n.º 34
0
        public TextureShader(Device device)
        {
            var vertexShaderByteCode = ShaderBytecode.CompileFromFile(VertexShaderFileName, "TextureVertexShader", "vs_4_0", ShaderFlags.None, EffectFlags.None);
            var pixelShaderByteCode = ShaderBytecode.CompileFromFile(PixelShaderFileName, "TexturePixelShader", "ps_4_0", ShaderFlags.None, EffectFlags.None);

            VertexShader = new VertexShader(device, vertexShaderByteCode);
            PixelShader = new PixelShader(device, pixelShaderByteCode);

            var inputElements = new InputElement[]
            {
                new InputElement
                {
                    SemanticName = "POSITION",
                    SemanticIndex = 0,
                    Format = Format.R32G32B32_Float,
                    Slot = 0,
                    AlignedByteOffset = 0,
                    Classification = InputClassification.PerVertexData,
                    InstanceDataStepRate = 0
                },
                new InputElement
                {
                    SemanticName = "TEXCOORD",
                    SemanticIndex = 0,
                    Format = Format.R32G32_Float,
                    Slot = 0,
                    AlignedByteOffset = TextureShader.Vertex.AppendAlignedElement,
                    Classification = InputClassification.PerVertexData,
                    InstanceDataStepRate = 0
                }
            };
            Layout = new InputLayout(device, ShaderSignature.GetInputSignature(vertexShaderByteCode), inputElements);

            vertexShaderByteCode.Dispose();
            pixelShaderByteCode.Dispose();

            // Setup the description of the dynamic matrix constant buffer that is in the vertex shader.
            var matrixBufferDesc = new BufferDescription
            {
                Usage = ResourceUsage.Dynamic, // Updated each frame
                SizeInBytes = Utilities.SizeOf<TextureShader.MatrixBuffer>(), // Contains three matrices
                BindFlags = BindFlags.ConstantBuffer,
                CpuAccessFlags = CpuAccessFlags.Write,
                OptionFlags = ResourceOptionFlags.None,
                StructureByteStride = 0
            };
            ConstantMatrixBuffer = new SharpDX.Direct3D11.Buffer(device, matrixBufferDesc);

            // Create a texture sampler state description.
            var samplerDesc = new SamplerStateDescription
            {
                Filter = Filter.MinMagMipLinear,
                AddressU = TextureAddressMode.Wrap,
                AddressV = TextureAddressMode.Wrap,
                AddressW = TextureAddressMode.Wrap,
                MipLodBias = 0,
                MaximumAnisotropy = 1,
                ComparisonFunction = Comparison.Always,
                BorderColor = new Color4(0, 0, 0, 0),
                MinimumLod = 0,
                MaximumLod = 0
            };

            // Create the texture sampler state.
            SamplerState = new SamplerState(device, samplerDesc);
        }