public GpuSBuffer(Device device, T[] data) { StructCount = data.Length; StructSize = System.Runtime.InteropServices.Marshal.SizeOf <T>();// (sizeof(T)); BufferSize = StructCount * StructSize; //var ds = new DataStream(new DataPointer() Buffer = Buffer.Create <T>(device, BindFlags.ShaderResource, data, BufferSize, ResourceUsage.Default, CpuAccessFlags.None, ResourceOptionFlags.BufferStructured, StructSize); //Buffer = new Buffer(device, BufferSize, ResourceUsage.Default, BindFlags.ShaderResource, CpuAccessFlags.None, ResourceOptionFlags.BufferStructured, StructSize); SRV = DXUtility.CreateShaderResourceView(device, Buffer, SharpDX.DXGI.Format.Unknown, SharpDX.Direct3D.ShaderResourceViewDimension.Buffer, 0, 0, StructCount, 0); }
public GpuCBuffer(Device device, int count) { StructCount = count; StructSize = System.Runtime.InteropServices.Marshal.SizeOf <T>();// (sizeof(T)); BufferSize = StructCount * StructSize; //Buffer = Buffer.Create<T>(device, BindFlags.ShaderResource, null, BufferSize, ResourceUsage.Dynamic, CpuAccessFlags.Write, ResourceOptionFlags.BufferStructured, StructSize); Buffer = new Buffer(device, BufferSize, ResourceUsage.Dynamic, BindFlags.ShaderResource, CpuAccessFlags.Write, ResourceOptionFlags.BufferStructured, StructSize); SRV = DXUtility.CreateShaderResourceView(device, Buffer, SharpDX.DXGI.Format.Unknown, SharpDX.Direct3D.ShaderResourceViewDimension.Buffer, 0, 0, StructCount, 0); Data = new List <T>(count); DataArray = new T[count]; }
public GpuBuffer(Device device, int itemSize, int itemCount) { StructSize = System.Runtime.InteropServices.Marshal.SizeOf <T>();// sizeof(T)), StructCount = itemCount * itemSize; ItemTotalSize = itemSize * StructSize; ItemCount = itemCount; Size = StructSize * itemSize * itemCount; Buffer = DXUtility.CreateBuffer(device, Size, ResourceUsage.Default, BindFlags.ShaderResource | BindFlags.UnorderedAccess, 0, ResourceOptionFlags.BufferStructured, StructSize); SRV = DXUtility.CreateShaderResourceView(device, Buffer, Format.Unknown, ShaderResourceViewDimension.Buffer, 0, 0, StructCount, 0); UAV = DXUtility.CreateUnorderedAccessView(device, Buffer, Format.Unknown, UnorderedAccessViewDimension.Buffer, 0, StructCount, 0, 0); }
public void Init(Device device, int w, int h, Format f, int sc, int sq, bool depth, Format df) { VramUsage = 0; Multisampled = (sc > 1); UseDepth = depth; ResourceUsage u = ResourceUsage.Default; BindFlags b = BindFlags.RenderTarget | BindFlags.ShaderResource; RenderTargetViewDimension rtvd = RenderTargetViewDimension.Texture2D; ShaderResourceViewDimension srvd = ShaderResourceViewDimension.Texture2D;// D3D11_SRV_DIMENSION_TEXTURE2D; int fs = DXUtility.ElementSize(f); int wh = w * h; BindFlags db = BindFlags.DepthStencil;// | BindFlags.ShaderResource;// D3D11_BIND_DEPTH_STENCIL; DepthStencilViewDimension dsvd = DepthStencilViewDimension.Texture2D; Format dtexf = GetDepthTexFormat(df); Format dsrvf = GetDepthSrvFormat(df); Texture = DXUtility.CreateTexture2D(device, w, h, 1, 1, f, 1, 0, u, b, 0, 0); RTV = DXUtility.CreateRenderTargetView(device, Texture, f, rtvd, 0, 0, 0); SRV = DXUtility.CreateShaderResourceView(device, Texture, f, srvd, 1, 0, 0, 0); VramUsage += (wh * fs); if (Multisampled) { b = BindFlags.RenderTarget; rtvd = RenderTargetViewDimension.Texture2DMultisampled; dsvd = DepthStencilViewDimension.Texture2DMultisampled; //srvd = ShaderResourceViewDimension.Texture2DMultisampled; TextureMS = DXUtility.CreateTexture2D(device, w, h, 1, 1, f, sc, sq, u, b, 0, 0); MSRTV = DXUtility.CreateRenderTargetView(device, TextureMS, f, rtvd, 0, 0, 0); VramUsage += (wh * fs) * sc; if (depth) { DepthMS = DXUtility.CreateTexture2D(device, w, h, 1, 1, dtexf, sc, sq, u, db, 0, 0); MSDSV = DXUtility.CreateDepthStencilView(device, DepthMS, df, dsvd); //DepthSRV = DXUtility.CreateShaderResourceView(device, DepthMS, dsrvf, srvd, 1, 0, 0, 0); VramUsage += (wh * DXUtility.ElementSize(df)) * sc; } } else { if (depth) { Depth = DXUtility.CreateTexture2D(device, w, h, 1, 1, dtexf, sc, sq, u, db, 0, 0); DSV = DXUtility.CreateDepthStencilView(device, Depth, df, dsvd); //DepthSRV = DXUtility.CreateShaderResourceView(device, Depth, dsrvf, srvd, 1, 0, 0, 0); VramUsage += (wh * DXUtility.ElementSize(df)); } } }
public Shadowmap(Device device) { TextureSize = 1024; //todo: make this a setting... CascadeCount = Math.Min(Settings.Default.ShadowCascades, 8); // 6; //use setting PCFSize = 3; PCFOffset = 0.000125f; //0.002f BlurBetweenCascades = 0.05f; ShadowVars = new GpuVarsBuffer <ShadowmapVars>(device); DepthTexture = DXUtility.CreateTexture2D(device, TextureSize * CascadeCount, TextureSize, 1, 1, Format.R32_Typeless, 1, 0, ResourceUsage.Default, BindFlags.DepthStencil | BindFlags.ShaderResource, 0, 0); DepthTextureSS = DXUtility.CreateSamplerState(device, TextureAddressMode.Border, new Color4(0.0f), Comparison.Less, Filter.ComparisonMinMagLinearMipPoint, 0, 0.0f, 0.0f, 0.0f); DepthTextureSRV = DXUtility.CreateShaderResourceView(device, DepthTexture, Format.R32_Float, ShaderResourceViewDimension.Texture2D, 1, 0, 0, 0); DepthTextureDSV = DXUtility.CreateDepthStencilView(device, DepthTexture, Format.D32_Float, DepthStencilViewDimension.Texture2D); Cascades = new List <ShadowmapCascade>(CascadeCount); for (int i = 0; i < CascadeCount; i++) { ShadowmapCascade c = new ShadowmapCascade(); c.Owner = this; c.Index = i; c.ZNear = 0.0f; c.ZFar = 1.0f; c.IntervalNear = 0.0f; c.IntervalFar = 1.0f; c.DepthRenderVP = new ViewportF() { Height = (float)TextureSize, Width = (float)TextureSize, MaxDepth = 1.0f, MinDepth = 0.0f, X = (float)(TextureSize * i), Y = 0, }; Cascades.Add(c); } DepthRenderRS = DXUtility.CreateRasterizerState(device, FillMode.Solid, CullMode.None, true, false, true, 0, 0.0f, 1.0f); DepthRenderDS = DXUtility.CreateDepthStencilState(device, true, DepthWriteMask.All); DepthRenderVP = new ViewportF(); DepthRenderVP.Height = (float)TextureSize; DepthRenderVP.Width = (float)TextureSize; DepthRenderVP.MaxDepth = 1.0f; DepthRenderVP.MinDepth = 0.0f; DepthRenderVP.X = 0; DepthRenderVP.Y = 0; graphicsMemoryUsage = (long)(TextureSize * TextureSize * CascadeCount * 4); }
public PostProcessor(DXManager dxman) { var device = dxman.device; byte[] bReduceTo1DCS = File.ReadAllBytes("Shaders\\PPReduceTo1DCS.cso"); byte[] bReduceTo0DCS = File.ReadAllBytes("Shaders\\PPReduceTo0DCS.cso"); byte[] bLumBlendCS = File.ReadAllBytes("Shaders\\PPLumBlendCS.cso"); byte[] bBloomFilterBPHCS = File.ReadAllBytes("Shaders\\PPBloomFilterBPHCS.cso"); byte[] bBloomFilterVCS = File.ReadAllBytes("Shaders\\PPBloomFilterVCS.cso"); byte[] bCopyPixelsPS = File.ReadAllBytes("Shaders\\PPCopyPixelsPS.cso"); byte[] bFinalPassVS = File.ReadAllBytes("Shaders\\PPFinalPassVS.cso"); byte[] bFinalPassPS = File.ReadAllBytes("Shaders\\PPFinalPassPS.cso"); ReduceTo1DCS = new ComputeShader(device, bReduceTo1DCS); ReduceTo0DCS = new ComputeShader(device, bReduceTo0DCS); LumBlendCS = new ComputeShader(device, bLumBlendCS); BloomFilterBPHCS = new ComputeShader(device, bBloomFilterBPHCS); BloomFilterVCS = new ComputeShader(device, bBloomFilterVCS); CopyPixelsPS = new PixelShader(device, bCopyPixelsPS); FinalPassVS = new VertexShader(device, bFinalPassVS); FinalPassPS = new PixelShader(device, bFinalPassPS); FinalPassQuad = new UnitQuad(device, true); FinalPassLayout = new InputLayout(device, bFinalPassVS, new[] { new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0), new InputElement("TEXCOORD", 0, Format.R32G32_Float, 16, 0), }); ReduceCSVars = new GpuVarsBuffer <PostProcessorReduceCSVars>(device); LumBlendCSVars = new GpuVarsBuffer <PostProcessorLumBlendCSVars>(device); FilterBPHCSVars = new GpuVarsBuffer <PostProcessorFilterBPHCSVars>(device); FilterVCSVars = new GpuVarsBuffer <PostProcessorFilterVCSVars>(device); FinalPSVars = new GpuVarsBuffer <PostProcessorFinalPSVars>(device); TextureAddressMode a = TextureAddressMode.Clamp; Color4 b = new Color4(0.0f, 0.0f, 0.0f, 0.0f); Comparison c = Comparison.Always; SampleStatePoint = DXUtility.CreateSamplerState(device, a, b, c, Filter.MinMagMipPoint, 0, 1.0f, 1.0f, 0.0f); SampleStateLinear = DXUtility.CreateSamplerState(device, a, b, c, Filter.MinMagMipLinear, 0, 1.0f, 1.0f, 0.0f); BlendState = DXUtility.CreateBlendState(device, false, BlendOperation.Add, BlendOption.One, BlendOption.Zero, BlendOperation.Add, BlendOption.One, BlendOption.Zero, ColorWriteMaskFlags.All); GetSampleWeights(ref FilterVCSVars.Vars.avSampleWeights, 3.0f, 1.25f); //init sample weights FilterBPHCSVars.Vars.avSampleWeights = FilterVCSVars.Vars.avSampleWeights; }
public void Init(Device device, int w, int h, int count, Format f, bool depth, Format df, int multisamplecount) { Count = count; VramUsage = 0; UseDepth = depth; MultisampleCount = multisamplecount; Multisampled = (multisamplecount > 1); ResourceUsage u = ResourceUsage.Default; BindFlags b = BindFlags.RenderTarget | BindFlags.ShaderResource; int fs = DXUtility.ElementSize(f); int wh = w * h; BindFlags db = BindFlags.DepthStencil | BindFlags.ShaderResource; // D3D11_BIND_DEPTH_STENCIL; RenderTargetViewDimension rtvd = RenderTargetViewDimension.Texture2D; ShaderResourceViewDimension srvd = ShaderResourceViewDimension.Texture2D; // D3D11_SRV_DIMENSION_TEXTURE2D; DepthStencilViewDimension dsvd = DepthStencilViewDimension.Texture2D; if (Multisampled) { rtvd = RenderTargetViewDimension.Texture2DMultisampled; srvd = ShaderResourceViewDimension.Texture2DMultisampled; dsvd = DepthStencilViewDimension.Texture2DMultisampled; } Textures = new Texture2D[count]; RTVs = new RenderTargetView[count]; SRVs = new ShaderResourceView[count]; for (int i = 0; i < count; i++) { Textures[i] = DXUtility.CreateTexture2D(device, w, h, 1, 1, f, multisamplecount, 0, u, b, 0, 0); RTVs[i] = DXUtility.CreateRenderTargetView(device, Textures[i], f, rtvd, 0, 0, 0); SRVs[i] = DXUtility.CreateShaderResourceView(device, Textures[i], f, srvd, 1, 0, 0, 0); VramUsage += (wh * fs) * multisamplecount; } if (depth) { Format dtexf = GpuTexture.GetDepthTexFormat(df); Format dsrvf = GpuTexture.GetDepthSrvFormat(df); Depth = DXUtility.CreateTexture2D(device, w, h, 1, 1, dtexf, multisamplecount, 0, u, db, 0, 0); DSV = DXUtility.CreateDepthStencilView(device, Depth, df, dsvd); DepthSRV = DXUtility.CreateShaderResourceView(device, Depth, dsrvf, srvd, 1, 0, 0, 0); VramUsage += (wh * DXUtility.ElementSize(df)) * multisamplecount; } }
public DeferredScene(DXManager dxman) { var device = dxman.device; byte[] bDirLightVS = File.ReadAllBytes("Shaders\\DirLightVS.cso"); byte[] bDirLightPS = File.ReadAllBytes("Shaders\\DirLightPS.cso"); byte[] bDirLightMSPS = File.ReadAllBytes("Shaders\\DirLightPS_MS.cso"); byte[] bLodLightVS = File.ReadAllBytes("Shaders\\LodLightsVS.cso"); byte[] bLodLightPS = File.ReadAllBytes("Shaders\\LodLightsPS.cso"); byte[] bLodLightMSPS = File.ReadAllBytes("Shaders\\LodLightsPS_MS.cso"); byte[] bLightVS = File.ReadAllBytes("Shaders\\LightVS.cso"); byte[] bLightPS = File.ReadAllBytes("Shaders\\LightPS.cso"); byte[] bLightMSPS = File.ReadAllBytes("Shaders\\LightPS_MS.cso"); byte[] bFinalVS = File.ReadAllBytes("Shaders\\PPFinalPassVS.cso"); byte[] bSSAAPS = File.ReadAllBytes("Shaders\\PPSSAAPS.cso"); DirLightVS = new VertexShader(device, bDirLightVS); DirLightPS = new PixelShader(device, bDirLightPS); LodLightVS = new VertexShader(device, bLodLightVS); LodLightPS = new PixelShader(device, bLodLightPS); LightVS = new VertexShader(device, bLightVS); LightPS = new PixelShader(device, bLightPS); try { //error could happen here if the device isn't supporting feature level 10.1 DirLightMSPS = new PixelShader(device, bDirLightMSPS); LodLightMSPS = new PixelShader(device, bLodLightMSPS); LightMSPS = new PixelShader(device, bLightMSPS); } catch { MSAASampleCount = 1; //can't do MSAA without at least 10.1 support } LightCone = new UnitCone(device, bLodLightVS, 4, false); LightSphere = new UnitSphere(device, bLodLightVS, 4, true); LightCapsule = new UnitCapsule(device, bLodLightVS, 4, false); LightQuad = new UnitQuad(device, true); LightQuadLayout = new InputLayout(device, bDirLightVS, new[] { new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0), new InputElement("TEXCOORD", 0, Format.R32G32_Float, 16, 0), }); LightVSVars = new GpuVarsBuffer <DeferredLightVSVars>(device); LightPSVars = new GpuVarsBuffer <DeferredLightPSVars>(device); LightInstVars = new GpuVarsBuffer <DeferredLightInstVars>(device); FinalVS = new VertexShader(device, bFinalVS); SSAAPS = new PixelShader(device, bSSAAPS); SSAAPSVars = new GpuVarsBuffer <DeferredSSAAPSVars>(device); TextureAddressMode a = TextureAddressMode.Clamp; Color4 b = new Color4(0.0f, 0.0f, 0.0f, 0.0f); Comparison c = Comparison.Always; SampleStatePoint = DXUtility.CreateSamplerState(device, a, b, c, Filter.MinMagMipPoint, 0, 1.0f, 1.0f, 0.0f); SampleStateLinear = DXUtility.CreateSamplerState(device, a, b, c, Filter.MinMagMipLinear, 0, 1.0f, 1.0f, 0.0f); BlendState = DXUtility.CreateBlendState(device, false, BlendOperation.Add, BlendOption.One, BlendOption.Zero, BlendOperation.Add, BlendOption.One, BlendOption.Zero, ColorWriteMaskFlags.All); }
public void Init(Device device, int w, int h, int count, Format f, bool depth, Format df, int multisamplecount) { Count = count; VramUsage = 0; UseDepth = depth; MultisampleCount = multisamplecount; Multisampled = (multisamplecount > 1); ResourceUsage u = ResourceUsage.Default; BindFlags b = BindFlags.RenderTarget | BindFlags.ShaderResource; int fs = DXUtility.ElementSize(f); int wh = w * h; BindFlags db = BindFlags.DepthStencil | BindFlags.ShaderResource; // D3D11_BIND_DEPTH_STENCIL; RenderTargetViewDimension rtvd = RenderTargetViewDimension.Texture2D; ShaderResourceViewDimension srvd = ShaderResourceViewDimension.Texture2D; // D3D11_SRV_DIMENSION_TEXTURE2D; DepthStencilViewDimension dsvd = DepthStencilViewDimension.Texture2D; if (Multisampled) { rtvd = RenderTargetViewDimension.Texture2DMultisampled; srvd = ShaderResourceViewDimension.Texture2DMultisampled; dsvd = DepthStencilViewDimension.Texture2DMultisampled; } Textures = new Texture2D[count]; RTVs = new RenderTargetView[count]; SRVs = new ShaderResourceView[count]; for (int i = 0; i < count; i++) { Textures[i] = DXUtility.CreateTexture2D(device, w, h, 1, 1, f, multisamplecount, 0, u, b, 0, 0); RTVs[i] = DXUtility.CreateRenderTargetView(device, Textures[i], f, rtvd, 0, 0, 0); SRVs[i] = DXUtility.CreateShaderResourceView(device, Textures[i], f, srvd, 1, 0, 0, 0); VramUsage += (wh * fs) * multisamplecount; } if (depth) { Format dtexf = Format.R32_Typeless; Format dsrvf = Format.R32_Float; switch (df) { case Format.D16_UNorm: dtexf = Format.R16_Typeless; dsrvf = Format.R16_UNorm; break; case Format.D24_UNorm_S8_UInt: dtexf = Format.R24G8_Typeless; dsrvf = Format.R24_UNorm_X8_Typeless; break; case Format.D32_Float: dtexf = Format.R32_Typeless; dsrvf = Format.R32_Float; break; case Format.D32_Float_S8X24_UInt: dtexf = Format.R32G8X24_Typeless; //is this right? who uses this anyway?? dsrvf = Format.R32_Float_X8X24_Typeless; break; } Depth = DXUtility.CreateTexture2D(device, w, h, 1, 1, dtexf, multisamplecount, 0, u, db, 0, 0); DSV = DXUtility.CreateDepthStencilView(device, Depth, df, dsvd); DepthSRV = DXUtility.CreateShaderResourceView(device, Depth, dsrvf, srvd, 1, 0, 0, 0); VramUsage += (wh * DXUtility.ElementSize(df)) * multisamplecount; } }