///////////////////////////////////////////////////////////////////////////////////////////// // Commandbuffer helpers ///////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Set a specific keyword for the pixelshader /// </summary> /// <param name="keyword"></param> /// <param name="enable"></param> private void SetKeyword(MaterialKeywords keyword, bool enable) { //For now disable check if da keyword is already set //to make sure the cmd is always correctly setuped //if(_shaderKeywords[(int)keyword].enabled != enable) { PipelineExtensions.SetKeyword(_shaderKeywords[(int)keyword].name, enable); _shaderKeywords[(int)keyword].enabled = enable; } }
/// <summary> /// This cleans up the final render step /// </summary> internal void AfterCompositeCleanup() { _bloomUpsampleBuffer.ClearTemporary(_commandBuffer, 0, _renderPipeline); DisableDebugKeywords(); DisableRenderKeywords(); if (_renderPipeline == RenderPipeline.Legacy) { PipelineExtensions.SetKeyword(_shaderKeywords[(int)MaterialKeywords.LegacyBlit].name, false); } }
/// <summary> /// Draw into a destination framebuffer based on shadertype /// Always prepare for drawing using the PrepareDraw command /// </summary> /// <param name="forcePixelShader"></param> private void Draw() { if (_renderPipeline == RenderPipeline.SRP) { _commandBuffer.Draw(_renderTargetsBundle, _renderMaterialNoGeometry, _currentRenderIndex); } else { PipelineExtensions.Draw(_renderTargetsBundle, _renderMaterialNoGeometry, _currentRenderIndex); } _renderTargetsBundle.Clear(); }
/// <summary> /// Create a specific level of the the buffer /// </summary> /// <param name="renderContext"></param> /// <param name="level"></param> /// <param name="cmd"></param> /// <param name="format"></param> internal void CreateTemporary(RenderContext[] renderContext, int level, CommandBuffer cmd, RenderTextureFormat format, RenderPipeline renderPipeline) { if (renderPipeline == RenderPipeline.SRP) { #if UNITY_2017_1_OR_NEWER cmd.GetTemporaryRT(renderTargets[level].identifier, renderContext[level].descriptor, FilterMode.Bilinear); #else cmd.GetTemporaryRT(renderTargets[level].identifier, renderContext[level].width, renderContext[level].height, 0, FilterMode.Bilinear, format, RenderTextureReadWrite.Default, 1); #endif } else { renderTargets[level].renderTexture = PipelineExtensions.GetTemporary(renderContext[level], format); } }
///////////////////////////////////////////////////////////////////////////////////////////// // Commandbuffer helpers ///////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Set a specific keyword for the pixelshader /// </summary> /// <param name="keyword"></param> /// <param name="enable"></param> private void SetKeyword(MaterialKeywords keyword, bool enable) { //For now disable check if a keyword is already set //to make sure the cmd is always correctly setuped //if(_shaderKeywords[(int)keyword].enabled != enable) { if (_renderPipeline == RenderPipeline.SRP) { _commandBuffer.SetKeyword(_shaderKeywords[(int)keyword].name, enable); } else { PipelineExtensions.SetKeyword(_shaderKeywords[(int)keyword].name, enable); } _shaderKeywords[(int)keyword].enabled = enable; } }
/// <summary> /// Renders the effect from source into destination buffer /// </summary> /// <param name="source"></param> /// <param name="destination"></param> internal void Build(RenderTargetIdentifier source, RenderTargetIdentifier destination, Settings settings, CommandBuffer cmd, Camera renderingCamera) { _commandBuffer = cmd; _settings = settings; _renderingCamera = renderingCamera; _commandBuffer.BeginSample(PipelineProperties.CommandBufferProperties.samplePrepare); CheckFeatureSupport(); _sourceFrameBuffer = source; #if UNITY_2018_2_OR_NEWER _destinationFrameBuffer = new RenderTargetIdentifier(destination, 0, CubemapFace.Unknown, -1); #else _destinationFrameBuffer = destination; #endif UpdateRenderBuffers(); _commandBuffer.EndSample(PipelineProperties.CommandBufferProperties.samplePrepare); //Prepare for selective glow if (_settings.workflow == Workflow.Selective) { _commandBuffer.BeginSample(PipelineProperties.CommandBufferProperties.sampleReplacement); _selectiveRenderContext.UpdateRenderContext(_renderingCamera, _renderTextureFormat, 16, _sourceContext[0].renderDimension); _selectiveRenderTexture = PipelineExtensions.GetTemporary(_selectiveRenderContext, _renderTextureFormat); SetupSelectiveGlowCamera(); selectiveGlowCamera.RenderWithShader(_resources.selectiveRenderShader, _selectiveReplacementTag); _commandBuffer.EndSample(PipelineProperties.CommandBufferProperties.sampleReplacement); } _commandBuffer.BeginSample(PipelineProperties.CommandBufferProperties.sampleSetup); UpdateConstantBuffers(); _commandBuffer.EndSample(PipelineProperties.CommandBufferProperties.sampleSetup); PreSample(); Downsample(); Upsample(); Composite(); }
/// <summary> /// Draw into a destination framebuffer based on shadertype /// Always prepare for drawing using the PrepareDraw command /// </summary> /// <param name="forcePixelShader"></param> private void Draw(bool forcePixelShader = false) { PipelineExtensions.Draw(_renderTargetsBundle, _renderMaterialNoGeometry, _currentRenderIndex); _renderTargetsBundle.Clear(); }
/// <summary> /// Create a specific level of the the buffer /// </summary> /// <param name="renderContext"></param> /// <param name="level"></param> /// <param name="format"></param> internal void CreateTemporary(RenderContext[] renderContext, int level, RenderTextureFormat format) { renderTargets[level] = PipelineExtensions.GetTemporary(renderContext[level], format); }
/// <summary> /// Precomposite of the glow map /// </summary> private void Composite() { BeginProfileSample(PipelineProperties.CommandBufferProperties.sampleComposite); int renderpass; switch (_settings.debugView) { case DebugView.RawBloom: _renderKeywordsBundle.Add(MaterialKeywords.DebugRawBloom); renderpass = (int)ShaderRenderPass.Debug; break; case DebugView.Bloom: _renderKeywordsBundle.Add(MaterialKeywords.DebugBloom); renderpass = (int)ShaderRenderPass.Debug; break; case DebugView.Composite: if (_useLensSurface) { _renderKeywordsBundle.Add(MaterialKeywords.LensSurface); } _renderKeywordsBundle.Add(MaterialKeywords.DebugComposite); renderpass = (int)ShaderRenderPass.Debug; break; default: if (_useLensSurface) { _renderKeywordsBundle.Add(MaterialKeywords.LensSurface); } renderpass = (int)ShaderRenderPass.Composite; break; } PrepareDraw ( renderpass, _sourceContext[0].renderDimension ); if (_settings.workflow == Workflow.Selective && (_settings.debugView == DebugView.RawBloom)) { SetTexture(PipelineProperties.ShaderProperties.sourceTex, sourceFrameBuffer.renderTexture, true); } else { SetTexture(PipelineProperties.ShaderProperties.sourceTex, sourceFrameBuffer, true); SetTexture(PipelineProperties.ShaderProperties.bloomTex, _bloomUpsampleBuffer.renderTargets[0], true); } if (_useLensSurface) { SetTexture(PipelineProperties.ShaderProperties.lensSurfaceDirtTex, _settings.lensSurfaceDirtTexture ? _settings.lensSurfaceDirtTexture : _resources.lensSurfaceDirtTextureDefault, true); SetTexture(PipelineProperties.ShaderProperties.lensSurfaceDiffractionTex, _settings.lensSurfaceDiffractionTexture ? _settings.lensSurfaceDiffractionTexture : _resources.lensSurfaceDiffractionTextureDefault, true); } //Dont draw when using legacy render pipeline if (_renderPipeline == RenderPipeline.SRP) { _renderTargetsBundle.Add(_destinationFrameBuffer); Draw(); AfterCompositeCleanup(); } else { PipelineExtensions.SetKeyword(_shaderKeywords[(int)MaterialKeywords.LegacyBlit].name, true); _renderTargetsBundle.Clear(); } EndProfileSample(PipelineProperties.CommandBufferProperties.sampleComposite); }