示例#1
0
        public bool Initialize()
        {
            Debug.Assert(!_initialized);

            #region Shaders

            var SpriteFX = @"Texture2D SpriteTex;
            SamplerState samLinear {
            Filter = MIN_MAG_MIP_LINEAR;
            AddressU = WRAP;
            AddressV = WRAP;
            };
            struct VertexIn {
            float3 PosNdc : POSITION;
            float2 Tex    : TEXCOORD;
            float4 Color  : COLOR;
            };
            struct VertexOut {
            float4 PosNdc : SV_POSITION;
            float2 Tex    : TEXCOORD;
            float4 Color  : COLOR;
            };
            VertexOut VS(VertexIn vin) {
            VertexOut vout;
            vout.PosNdc = float4(vin.PosNdc, 1.0f);
            vout.Tex    = vin.Tex;
            vout.Color  = vin.Color;
            return vout;
            };
            float4 PS(VertexOut pin) : SV_Target {
            return pin.Color*SpriteTex.Sample(samLinear, pin.Tex);
            };
            technique11 SpriteTech {
            pass P0 {
            SetVertexShader( CompileShader( vs_5_0, VS() ) );
            SetHullShader( NULL );
            SetDomainShader( NULL );
            SetGeometryShader( NULL );
            SetPixelShader( CompileShader( ps_5_0, PS() ) );
            }
            };";

            #endregion

            _compiledFX = ToDispose(ShaderBytecode.Compile(SpriteFX, "SpriteTech", "fx_5_0"));
            {
                if (_compiledFX.HasErrors)
                    return false;

                _effect = ToDispose(new Effect(_device, _compiledFX));
                {
                    _spriteTech = ToDispose(_effect.GetTechniqueByName("SpriteTech"));
                    _spriteMap = ToDispose(_effect.GetVariableByName("SpriteTex").AsShaderResource());

                    using (var pass = _spriteTech.GetPassByIndex(0))
                    {
                        InputElement[] layoutDesc =
                        {
                            new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0,
                                InputClassification.PerVertexData, 0),
                            new InputElement("TEXCOORD", 0, Format.R32G32_Float, 12, 0,
                                InputClassification.PerVertexData, 0),
                            new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 20, 0,
                                InputClassification.PerVertexData, 0)
                        };

                        _inputLayout = ToDispose(new InputLayout(_device, pass.Description.Signature, layoutDesc));
                    }
                    // Create Vertex Buffer
                    var vbd = new BufferDescription
                    {
                        SizeInBytes = 2048*Marshal.SizeOf(typeof (SpriteVertex)),
                        Usage = ResourceUsage.Dynamic,
                        BindFlags = BindFlags.VertexBuffer,
                        CpuAccessFlags = CpuAccessFlags.Write,
                        OptionFlags = ResourceOptionFlags.None,
                        StructureByteStride = 0
                    };

                    _VB = ToDispose(new Buffer(_device, vbd));

                    // Create and initialise Index Buffer

                    var indices = new short[3072];

                    for (ushort i = 0; i < 512; ++i)
                    {
                        indices[i*6] = (short) (i*4);
                        indices[i*6 + 1] = (short) (i*4 + 1);
                        indices[i*6 + 2] = (short) (i*4 + 2);
                        indices[i*6 + 3] = (short) (i*4);
                        indices[i*6 + 4] = (short) (i*4 + 2);
                        indices[i*6 + 5] = (short) (i*4 + 3);
                    }

                    _indexBuffer = ToDispose(new SafeHGlobal(indices.Length*Marshal.SizeOf(indices[0])));
                    Marshal.Copy(indices, 0, _indexBuffer.DangerousGetHandle(), indices.Length);

                    var ibd = new BufferDescription
                    {
                        SizeInBytes = 3072*Marshal.SizeOf(typeof (short)),
                        Usage = ResourceUsage.Immutable,
                        BindFlags = BindFlags.IndexBuffer,
                        CpuAccessFlags = CpuAccessFlags.None,
                        OptionFlags = ResourceOptionFlags.None,
                        StructureByteStride = 0
                    };

                    _IB = ToDispose(new Buffer(_device, _indexBuffer.DangerousGetHandle(), ibd));

                    var transparentDesc = new BlendStateDescription
                    {
                        AlphaToCoverageEnable = false,
                        IndependentBlendEnable = false
                    };
                    transparentDesc.RenderTarget[0].IsBlendEnabled = true;
                    transparentDesc.RenderTarget[0].SourceBlend = BlendOption.SourceAlpha;
                    transparentDesc.RenderTarget[0].DestinationBlend = BlendOption.InverseSourceAlpha;
                    transparentDesc.RenderTarget[0].BlendOperation = BlendOperation.Add;
                    transparentDesc.RenderTarget[0].SourceAlphaBlend = BlendOption.One;
                    transparentDesc.RenderTarget[0].DestinationAlphaBlend = BlendOption.Zero;
                    transparentDesc.RenderTarget[0].AlphaBlendOperation = BlendOperation.Add;
                    transparentDesc.RenderTarget[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;

                    _transparentBS = ToDispose(new BlendState(_device, transparentDesc));
                }
            }

            _initialized = true;

            return true;
        }
示例#2
0
        public bool Initialize()
        {
            Debug.Assert(!_initialized);

            #region Shaders
            string SpriteFX = @"Texture2D SpriteTex;
SamplerState samLinear {
    Filter = MIN_MAG_MIP_LINEAR;
    AddressU = WRAP;
    AddressV = WRAP;
};
struct VertexIn {
    float3 PosNdc : POSITION;
    float2 Tex    : TEXCOORD;
    float4 Color  : COLOR;
};
struct VertexOut {
    float4 PosNdc : SV_POSITION;
    float2 Tex    : TEXCOORD;
    float4 Color  : COLOR;
};
VertexOut VS(VertexIn vin) {
    VertexOut vout;
    vout.PosNdc = float4(vin.PosNdc, 1.0f);
    vout.Tex    = vin.Tex;
    vout.Color  = vin.Color;
    return vout;
};
float4 PS(VertexOut pin) : SV_Target {
    return pin.Color*SpriteTex.Sample(samLinear, pin.Tex);
};
technique11 SpriteTech {
    pass P0 {
        SetVertexShader( CompileShader( vs_5_0, VS() ) );
        SetHullShader( NULL );
        SetDomainShader( NULL );
        SetGeometryShader( NULL );
        SetPixelShader( CompileShader( ps_5_0, PS() ) );
    }
};";
            #endregion

            _compiledFX = ToDispose(ShaderBytecode.Compile(SpriteFX, "SpriteTech", "fx_5_0"));
            {
                if (_compiledFX.HasErrors)
                {
                    return(false);
                }

                _effect = ToDispose(new Effect(_device, _compiledFX));
                {
                    _spriteTech = ToDispose(_effect.GetTechniqueByName("SpriteTech"));
                    _spriteMap  = ToDispose(_effect.GetVariableByName("SpriteTex").AsShaderResource());

                    using (var pass = _spriteTech.GetPassByIndex(0))
                    {
                        InputElement[] layoutDesc =
                        {
                            new InputElement("POSITION", 0, global::SharpDX.DXGI.Format.R32G32B32_Float,     0, 0, InputClassification.PerVertexData, 0),
                            new InputElement("TEXCOORD", 0, global::SharpDX.DXGI.Format.R32G32_Float,       12, 0, InputClassification.PerVertexData, 0),
                            new InputElement("COLOR",    0, global::SharpDX.DXGI.Format.R32G32B32A32_Float, 20, 0, InputClassification.PerVertexData, 0)
                        };

                        _inputLayout = ToDispose(new InputLayout(_device, pass.Description.Signature, layoutDesc));
                    }
                    // Create Vertex Buffer
                    BufferDescription vbd = new BufferDescription
                    {
                        SizeInBytes         = 2048 * Marshal.SizeOf(typeof(SpriteVertex)),
                        Usage               = ResourceUsage.Dynamic,
                        BindFlags           = BindFlags.VertexBuffer,
                        CpuAccessFlags      = CpuAccessFlags.Write,
                        OptionFlags         = ResourceOptionFlags.None,
                        StructureByteStride = 0
                    };

                    _VB = ToDispose(new global::SharpDX.Direct3D11.Buffer(_device, vbd));

                    // Create and initialise Index Buffer

                    short[] indices = new short[3072];

                    for (ushort i = 0; i < 512; ++i)
                    {
                        indices[i * 6]     = (short)(i * 4);
                        indices[i * 6 + 1] = (short)(i * 4 + 1);
                        indices[i * 6 + 2] = (short)(i * 4 + 2);
                        indices[i * 6 + 3] = (short)(i * 4);
                        indices[i * 6 + 4] = (short)(i * 4 + 2);
                        indices[i * 6 + 5] = (short)(i * 4 + 3);
                    }

                    _indexBuffer = ToDispose(new SafeHGlobal(indices.Length * Marshal.SizeOf(indices[0])));
                    Marshal.Copy(indices, 0, _indexBuffer.DangerousGetHandle(), indices.Length);

                    BufferDescription ibd = new BufferDescription
                    {
                        SizeInBytes         = 3072 * Marshal.SizeOf(typeof(short)),
                        Usage               = ResourceUsage.Immutable,
                        BindFlags           = BindFlags.IndexBuffer,
                        CpuAccessFlags      = CpuAccessFlags.None,
                        OptionFlags         = ResourceOptionFlags.None,
                        StructureByteStride = 0
                    };

                    _IB = ToDispose(new global::SharpDX.Direct3D11.Buffer(_device, _indexBuffer.DangerousGetHandle(), ibd));

                    BlendStateDescription transparentDesc = new BlendStateDescription()
                    {
                        AlphaToCoverageEnable  = false,
                        IndependentBlendEnable = false,
                    };
                    transparentDesc.RenderTarget[0].IsBlendEnabled        = true;
                    transparentDesc.RenderTarget[0].SourceBlend           = BlendOption.SourceAlpha;
                    transparentDesc.RenderTarget[0].DestinationBlend      = BlendOption.InverseSourceAlpha;
                    transparentDesc.RenderTarget[0].BlendOperation        = BlendOperation.Add;
                    transparentDesc.RenderTarget[0].SourceAlphaBlend      = BlendOption.One;
                    transparentDesc.RenderTarget[0].DestinationAlphaBlend = BlendOption.Zero;
                    transparentDesc.RenderTarget[0].AlphaBlendOperation   = BlendOperation.Add;
                    transparentDesc.RenderTarget[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;

                    _transparentBS = ToDispose(new BlendState(_device, transparentDesc));
                }
            }

            _initialized = true;

            return(true);
        }
示例#3
0
        public Boolean Initialize()
        {
            Debug.Assert(!this.Initialized, "Ensure not initialized");

            String spriteFX = @"Texture2D SpriteTex;
SamplerState samLinear {
    Filter = MIN_MAG_MIP_LINEAR;
    AddressU = WRAP;
    AddressV = WRAP;
};
struct VertexIn {
    float3 PosNdc : POSITION;
    float2 Tex    : TEXCOORD;
    float4 Color  : COLOR;
};
struct VertexOut {
    float4 PosNdc : SV_POSITION;
    float2 Tex    : TEXCOORD;
    float4 Color  : COLOR;
};
VertexOut VS(VertexIn vin) {
    VertexOut vout;
    vout.PosNdc = float4(vin.PosNdc, 1.0f);
    vout.Tex    = vin.Tex;
    vout.Color  = vin.Color;
    return vout;
};
float4 PS(VertexOut pin) : SV_Target {
    return pin.Color*SpriteTex.Sample(samLinear, pin.Tex);
};
technique11 SpriteTech {
    pass P0 {
        SetVertexShader( CompileShader( vs_5_0, VS() ) );
        SetHullShader( NULL );
        SetDomainShader( NULL );
        SetGeometryShader( NULL );
        SetPixelShader( CompileShader( ps_5_0, PS() ) );
    }
};";

            this.CompiledFX = ShaderBytecode.Compile(spriteFX, "SpriteTech", "fx_5_0");
            {
                if (this.CompiledFX.HasErrors)
                {
                    return(false);
                }

                this.Effect = new Effect(this.Device, this.CompiledFX);
                {
                    this.SpriteTech = Effect.GetTechniqueByName("SpriteTech");
                    this.SpriteMap  = Effect.GetVariableByName("SpriteTex").AsShaderResource();

                    using (EffectPass effectPas = this.SpriteTech.GetPassByIndex(0))
                    {
                        InputElement[] layoutDesc =
                        {
                            new InputElement("POSITION", 0, SharpDX.DXGI.Format.R32G32B32_Float,     0, 0, InputClassification.PerVertexData, 0),
                            new InputElement("TEXCOORD", 0, SharpDX.DXGI.Format.R32G32_Float,       12, 0, InputClassification.PerVertexData, 0),
                            new InputElement("COLOR",    0, SharpDX.DXGI.Format.R32G32B32A32_Float, 20, 0, InputClassification.PerVertexData, 0)
                        };

                        this.InputLayout = new InputLayout(this.Device, effectPas.Description.Signature, layoutDesc);
                    }

                    // Create Vertex Buffer
                    BufferDescription vertexBufferDescription = new BufferDescription
                    {
                        SizeInBytes         = 2048 * Marshal.SizeOf(typeof(SpriteVertex)),
                        Usage               = ResourceUsage.Dynamic,
                        BindFlags           = BindFlags.VertexBuffer,
                        CpuAccessFlags      = CpuAccessFlags.Write,
                        OptionFlags         = ResourceOptionFlags.None,
                        StructureByteStride = 0
                    };

                    this.VertexBuffer = new SharpDX.Direct3D11.Buffer(Device, vertexBufferDescription); // ToDispose()

                    // Create and initialise Index Buffer
                    Int16[] indicies = new Int16[3072];

                    for (UInt16 index = 0; index < 512; ++index)
                    {
                        indicies[index * 6]       = (Int16)(index * 4);
                        indicies[(index * 6) + 1] = (Int16)((index * 4) + 1);
                        indicies[(index * 6) + 2] = (Int16)((index * 4) + 2);
                        indicies[(index * 6) + 3] = (Int16)(index * 4);
                        indicies[(index * 6) + 4] = (Int16)((index * 4) + 2);
                        indicies[(index * 6) + 5] = (Int16)((index * 4) + 3);
                    }

                    this.IndexBuffer = new SafeHGlobal(indicies.Length * Marshal.SizeOf(indicies[0]));
                    Marshal.Copy(indicies, 0, this.IndexBuffer.DangerousGetHandle(), indicies.Length);

                    BufferDescription bufferDescription = new BufferDescription
                    {
                        SizeInBytes         = 3072 * Marshal.SizeOf(typeof(Int16)),
                        Usage               = ResourceUsage.Immutable,
                        BindFlags           = BindFlags.IndexBuffer,
                        CpuAccessFlags      = CpuAccessFlags.None,
                        OptionFlags         = ResourceOptionFlags.None,
                        StructureByteStride = 0
                    };

                    this.Buffer = new SharpDX.Direct3D11.Buffer(this.Device, this.IndexBuffer.DangerousGetHandle(), bufferDescription);

                    BlendStateDescription transparentDescription = new BlendStateDescription()
                    {
                        AlphaToCoverageEnable  = false,
                        IndependentBlendEnable = false,
                    };

                    transparentDescription.RenderTarget[0].IsBlendEnabled        = true;
                    transparentDescription.RenderTarget[0].SourceBlend           = BlendOption.SourceAlpha;
                    transparentDescription.RenderTarget[0].DestinationBlend      = BlendOption.InverseSourceAlpha;
                    transparentDescription.RenderTarget[0].BlendOperation        = BlendOperation.Add;
                    transparentDescription.RenderTarget[0].SourceAlphaBlend      = BlendOption.One;
                    transparentDescription.RenderTarget[0].DestinationAlphaBlend = BlendOption.Zero;
                    transparentDescription.RenderTarget[0].AlphaBlendOperation   = BlendOperation.Add;
                    transparentDescription.RenderTarget[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;

                    this.TransparentBlendState = new BlendState(Device, transparentDescription); // ToDispose()
                }
            }

            this.Initialized = true;

            return(true);
        }