/// <summary> /// Makes bloom effect using specified input texture. /// </summary> /// <param name="texture">Texture to apply effect on.</param> /// <returns>Texture with applied effect.</returns> public ShaderResourceView MakeBloomEffect(RenderTarget texture) { Viewport viewport = graphicsDevice.ImmediateContext.Rasterizer.GetViewports()[0]; DataBox data = graphicsDevice.ImmediateContext.MapSubresource(constantBuffer, MapMode.WriteDiscard, MapFlags.None); data.Data.Write<Vector4>(new Vector4(viewport.Width, viewport.Height, 0, 0)); data.Data.Write<Vector4>(Settings.BloomSettings); graphicsDevice.ImmediateContext.UnmapSubresource(constantBuffer, 0); // DownSample4x to RT0 Dispatch("DownSample4x", 4, renderTargets[0], texture); // DownSample4x to RT1 Dispatch("DownSample4x", 16, renderTargets[1], renderTargets[0]); // BrightPass to RT2 Dispatch("BrightPass", 16, renderTargets[2], renderTargets[1]); // BlurV to RT1 Dispatch("BlurV", 16, renderTargets[1], renderTargets[2]); // BlurH to RT2 Dispatch("BlurH", 16, renderTargets[2], renderTargets[1]); // BlurV to RT1 Dispatch("BlurV", 16, renderTargets[1], renderTargets[2]); // BlurH to RT2 Dispatch("BlurH", 16, renderTargets[2], renderTargets[1]); // UpSample4x to RT0 Dispatch("UpSample4x", 4, renderTargets[0], renderTargets[2]); // UpSample4xCombine RT0 and texture to RT3 Dispatch("UpSample4xCombine", 1, renderTargets[3], texture, renderTargets[0]); return renderTargets[3].GetShaderResourceView(); }
/// <summary> /// Sets specified shader program and performs computations. /// </summary> /// <param name="shader">Name of shader program.</param> /// <param name="outputScale">Screen space scale factor.</param> /// <param name="output">Output render target.</param> /// <param name="input">Input render targets.</param> private void Dispatch(string shader, int outputScale, RenderTarget output, params RenderTarget[] input) { Viewport viewport = graphicsDevice.ImmediateContext.Rasterizer.GetViewports()[0]; graphicsDevice.ImmediateContext.ComputeShader.Set(effects[shader]); graphicsDevice.ImmediateContext.ComputeShader.SetConstantBuffer(constantBuffer, 0); for (int i = 0; i < input.Length; i++) graphicsDevice.ImmediateContext.ComputeShader.SetShaderResource(input[i].GetShaderResourceView(), i); graphicsDevice.ImmediateContext.ComputeShader.SetUnorderedAccessView(output.GetUnorderedAccessView(), 0); graphicsDevice.ImmediateContext.Dispatch((int)Math.Ceiling(viewport.Width / (32 * outputScale)), (int)Math.Ceiling(viewport.Height / (32 * outputScale)), 1); graphicsDevice.ImmediateContext.ComputeShader.SetUnorderedAccessView(null, 0); }