Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using SharpDX;
- using SharpDX.Direct3D11;
- using SharpDX.DXGI;
- namespace VoidwalkerEngine.Framework.DirectX.Rendering
- {
- public class DepthStencil
- {
- public bool IsValid { get; private set; }
- public string Identifier { get; set; }
- public int Width { get; private set; }
- public int Height { get; private set; }
- private Texture2D _texture;
- private DepthStencilView _depthStencilView;
- private Material _material;
- private SharpDX.Direct3D11.Device _device;
- public DepthStencil(SharpDX.Direct3D11.Device device)
- {
- this._device = device;
- }
- public DepthStencil(SharpDX.Direct3D11.Device device, int width, int height)
- {
- this._device = device;
- Resize(width, height);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="context"></param>
- /// <param name="flag"></param>
- /// <param name="depth"></param>
- /// <param name="stencil"></param>
- public void Clear(DeviceContext context,
- DepthStencilClearFlags flag = DepthStencilClearFlags.Depth,
- float depth = 1.0f, byte stencil = 0)
- {
- context.ClearDepthStencilView(_depthStencilView, flag, depth, stencil);
- }
- /// <summary>
- ///
- /// </summary>
- /// <returns></returns>
- public DepthStencilView GetView()
- {
- return this._depthStencilView;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="width"></param>
- /// <param name="height"></param>
- /// <param name="rebuild"></param>
- public void Resize(int width, int height, bool rebuild = true)
- {
- this.Width = width;
- this.Height = height;
- if (rebuild)
- {
- Rebuild();
- }
- }
- /// <summary>
- ///
- /// </summary>
- public void Rebuild()
- {
- if (this.Width <= 0 || this.Height <= 0)
- {
- throw new System.Exception("FrameBuffer Width and Height must be greater than 0!");
- }
- this.Dispose();
- /**
- * Create Depth Stencil
- */
- this._texture = new Texture2D(_device, new Texture2DDescription()
- {
- Format = Format.R24G8_Typeless,
- ArraySize = 1,
- MipLevels = 1,
- Width = this.Width,
- Height = this.Height,
- SampleDescription = new SampleDescription(1, 0),
- Usage = ResourceUsage.Default,
- BindFlags = BindFlags.DepthStencil | BindFlags.ShaderResource,
- CpuAccessFlags = CpuAccessFlags.None,
- OptionFlags = ResourceOptionFlags.None
- });
- this._depthStencilView = new DepthStencilView(_device, _texture, new DepthStencilViewDescription()
- {
- Flags = 0,
- Format = Format.D24_UNorm_S8_UInt,
- Dimension = DepthStencilViewDimension.Texture2D
- });
- SamplerState sampler = new SamplerState(_device, new SamplerStateDescription
- {
- Filter = Filter.MinMagPointMipLinear,
- AddressU = TextureAddressMode.Wrap,
- AddressV = TextureAddressMode.Wrap,
- AddressW = TextureAddressMode.Wrap,
- });
- ShaderResourceViewDescription depthResource = new ShaderResourceViewDescription()
- {
- Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2D,
- Format = Format.R24_UNorm_X8_Typeless,
- };
- depthResource.Texture2D.MipLevels = 1;
- _material = new Material()
- {
- Texture = _texture,
- Device = _device,
- SamplerState = sampler,
- ResourceView = new ShaderResourceView(_device, _texture, depthResource)
- };
- }
- /// <summary>
- ///
- /// </summary>
- public void Dispose()
- {
- if (_texture != null && _depthStencilView != null)
- {
- SharpDX.Utilities.Dispose(ref _texture);
- SharpDX.Utilities.Dispose(ref _depthStencilView);
- _material.Dispose();
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <returns></returns>
- public Material GetMaterial()
- {
- return _material;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement