Esempio n. 1
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;
            }
        }
Esempio n. 2
0
 private void Shutdown()
 {
     if(pixelShader.SafeDispose()) pixelShader = null;
     if(vertexShader.SafeDispose()) vertexShader = null;
     if(texture.SafeDispose()) texture = null;
     if(matrixBuffer.SafeDispose()) matrixBuffer = null;
     if(layout.SafeDispose()) layout = null;
 }