Advertisement
Krythic

DepthStencil

Aug 26th, 2020
1,063
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.69 KB | None | 0 0
  1. using SharpDX;
  2. using SharpDX.Direct3D11;
  3. using SharpDX.DXGI;
  4.  
  5. namespace VoidwalkerEngine.Framework.DirectX.Rendering
  6. {
  7.     public class DepthStencil
  8.     {
  9.         public bool IsValid { get; private set; }
  10.         public string Identifier { get; set; }
  11.         public int Width { get; private set; }
  12.         public int Height { get; private set; }
  13.         private Texture2D _texture;
  14.         private DepthStencilView _depthStencilView;
  15.         private Material _material;
  16.         private SharpDX.Direct3D11.Device _device;
  17.  
  18.  
  19.         public DepthStencil(SharpDX.Direct3D11.Device device)
  20.         {
  21.             this._device = device;
  22.         }
  23.  
  24.         public DepthStencil(SharpDX.Direct3D11.Device device, int width, int height)
  25.         {
  26.             this._device = device;
  27.             Resize(width, height);
  28.         }
  29.  
  30.         /// <summary>
  31.         ///
  32.         /// </summary>
  33.         /// <param name="context"></param>
  34.         /// <param name="flag"></param>
  35.         /// <param name="depth"></param>
  36.         /// <param name="stencil"></param>
  37.         public void Clear(DeviceContext context,
  38.             DepthStencilClearFlags flag = DepthStencilClearFlags.Depth,
  39.             float depth = 1.0f, byte stencil = 0)
  40.         {
  41.             context.ClearDepthStencilView(_depthStencilView, flag, depth, stencil);
  42.         }
  43.  
  44.         /// <summary>
  45.         ///
  46.         /// </summary>
  47.         /// <returns></returns>
  48.         public DepthStencilView GetView()
  49.         {
  50.             return this._depthStencilView;
  51.         }
  52.  
  53.         /// <summary>
  54.         ///
  55.         /// </summary>
  56.         /// <param name="width"></param>
  57.         /// <param name="height"></param>
  58.         /// <param name="rebuild"></param>
  59.         public void Resize(int width, int height, bool rebuild = true)
  60.         {
  61.             this.Width = width;
  62.             this.Height = height;
  63.             if (rebuild)
  64.             {
  65.                 Rebuild();
  66.             }
  67.         }
  68.  
  69.         /// <summary>
  70.         ///
  71.         /// </summary>
  72.         public void Rebuild()
  73.         {
  74.             if (this.Width <= 0 || this.Height <= 0)
  75.             {
  76.                 throw new System.Exception("FrameBuffer Width and Height must be greater than 0!");
  77.             }
  78.             this.Dispose();
  79.             /**
  80.              * Create Depth Stencil
  81.              */
  82.             this._texture = new Texture2D(_device, new Texture2DDescription()
  83.             {
  84.                 Format = Format.R24G8_Typeless,
  85.                 ArraySize = 1,
  86.                 MipLevels = 1,
  87.                 Width = this.Width,
  88.                 Height = this.Height,
  89.                 SampleDescription = new SampleDescription(1, 0),
  90.                 Usage = ResourceUsage.Default,
  91.                 BindFlags = BindFlags.DepthStencil | BindFlags.ShaderResource,
  92.                 CpuAccessFlags = CpuAccessFlags.None,
  93.                 OptionFlags = ResourceOptionFlags.None
  94.             });
  95.             this._depthStencilView = new DepthStencilView(_device, _texture, new DepthStencilViewDescription()
  96.             {
  97.                 Flags = 0,
  98.                 Format = Format.D24_UNorm_S8_UInt,
  99.                 Dimension = DepthStencilViewDimension.Texture2D
  100.             });
  101.             SamplerState sampler = new SamplerState(_device, new SamplerStateDescription
  102.             {
  103.                 Filter = Filter.MinMagPointMipLinear,
  104.                 AddressU = TextureAddressMode.Wrap,
  105.                 AddressV = TextureAddressMode.Wrap,
  106.                 AddressW = TextureAddressMode.Wrap,
  107.             });
  108.             ShaderResourceViewDescription depthResource = new ShaderResourceViewDescription()
  109.             {
  110.                 Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2D,
  111.                 Format = Format.R24_UNorm_X8_Typeless,
  112.             };
  113.             depthResource.Texture2D.MipLevels = 1;
  114.             _material = new Material()
  115.             {
  116.                 Texture = _texture,
  117.                 Device = _device,
  118.                 SamplerState = sampler,
  119.                 ResourceView = new ShaderResourceView(_device, _texture, depthResource)
  120.             };
  121.         }
  122.  
  123.  
  124.         /// <summary>
  125.         ///
  126.         /// </summary>
  127.         public void Dispose()
  128.         {
  129.             if (_texture != null && _depthStencilView != null)
  130.             {
  131.                 SharpDX.Utilities.Dispose(ref _texture);
  132.                 SharpDX.Utilities.Dispose(ref _depthStencilView);
  133.                 _material.Dispose();
  134.             }
  135.         }
  136.  
  137.         /// <summary>
  138.         ///
  139.         /// </summary>
  140.         /// <returns></returns>
  141.         public Material GetMaterial()
  142.         {
  143.             return _material;
  144.         }
  145.     }
  146. }
  147.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement