void DrawCamera(ScriptableRenderContext context, CullingResults cullingResults, Camera camera) { bool enableDynamicBatching = false; bool enableInstancing = false; context.SetupCameraProperties(camera); PerObjectData perObjectData = PerObjectData.None; FilteringSettings opaqueFilteringSettings = new FilteringSettings(RenderQueueRange.opaque); FilteringSettings transparentFilteringSettings = new FilteringSettings(RenderQueueRange.transparent); SortingSettings opaqueSortingSettings = new SortingSettings(camera); opaqueSortingSettings.criteria = SortingCriteria.CommonOpaque; // ShaderTagId must match the "LightMode" tag inside the shader pass. // If not "LightMode" tag is found the object won't render. DrawingSettings opaqueDrawingSettings = new DrawingSettings(ShaderPassTag.Forward, opaqueSortingSettings); opaqueDrawingSettings.enableDynamicBatching = enableDynamicBatching; opaqueDrawingSettings.enableInstancing = enableInstancing; opaqueDrawingSettings.perObjectData = perObjectData; // Sets active render target and clear based on camera background color. var cmd = CommandBufferPool.Get(); cmd.SetRenderTarget(BuiltinRenderTextureType.CameraTarget); cmd.ClearRenderTarget(true, true, camera.backgroundColor); context.ExecuteCommandBuffer(cmd); CommandBufferPool.Release(cmd); context.DrawRenderers(cullingResults, ref opaqueDrawingSettings, ref opaqueFilteringSettings); // Renders skybox if required if (camera.clearFlags == CameraClearFlags.Skybox && RenderSettings.skybox != null) { context.DrawSkybox(camera); } // Render Opaque objects given the filtering and settings computed above. // This functions will sort and batch objects. m_LightCullingPass.Execute(context, cullingResults, camera); DeferredLightPass(context, cullingResults, camera); //context.DrawSkybox(camera); cmd = CommandBufferPool.Get("FinalBlit"); cmd.Blit(m_ColorBufferRTID, BuiltinRenderTextureType.CameraTarget); context.ExecuteCommandBuffer(cmd); CommandBufferPool.Release(cmd); // if (m_DebugTileCount) { DebugPass(context, cullingResults, camera); } // Submit commands to GPU. Up to this point all commands have been enqueued in the context. // Several submits can be done in a frame to better controls CPU/GPU workload. context.Submit(); }
private void DrawVisibleObjects(bool useDynamicBatching, bool useGPUInstancing, bool useLightsPerObject) { PerObjectData lightsPerObjectFlag = useLightsPerObject ? PerObjectData.LightData | PerObjectData.LightIndices : PerObjectData.None; SortingSettings sortingSettings = new SortingSettings(camera) { criteria = SortingCriteria.CommonOpaque }; // Lightmaps enable LIGHTMAP_ON, ShadowMask enable shadow mask texture, OcclusionProbe enable unity_ProbesOcclusion DrawingSettings drawingSettings = new DrawingSettings(unlitShaderTagID, sortingSettings) { enableDynamicBatching = useDynamicBatching, enableInstancing = useGPUInstancing, perObjectData = PerObjectData.Lightmaps | PerObjectData.ShadowMask | PerObjectData.OcclusionProbe | PerObjectData.OcclusionProbeProxyVolume | PerObjectData.LightProbe | PerObjectData.LightProbeProxyVolume | PerObjectData.ReflectionProbes | lightsPerObjectFlag }; drawingSettings.SetShaderPassName(1, litShaderTagID); // drawingSettings.SetShaderPassName(2, alwaysTagID); FilteringSettings filteringSettings = new FilteringSettings(RenderQueueRange.opaque); renderContext.DrawRenderers(cullingResults, ref drawingSettings, ref filteringSettings); renderContext.DrawSkybox(camera); sortingSettings.criteria = SortingCriteria.CommonTransparent; drawingSettings.sortingSettings = sortingSettings; filteringSettings.renderQueueRange = RenderQueueRange.transparent; renderContext.DrawRenderers(cullingResults, ref drawingSettings, ref filteringSettings); }
public VXGIRenderPipeline(VXGIRenderPipelineAsset asset) { _renderer = new VXGIRenderer(this); _command = new CommandBuffer() { name = "VXGI.RenderPipeline" }; _filterSettings = new FilteringSettings() { renderQueueRange = RenderQueueRange.opaque }; _perObjectData = PerObjectData.None; if (asset.environmentLighting) { _perObjectData |= PerObjectData.LightProbe; } if (asset.environmentReflections) { _perObjectData |= PerObjectData.ReflectionProbes; } Shader.globalRenderPipeline = "VXGI"; GraphicsSettings.lightsUseLinearIntensity = true; GraphicsSettings.useScriptableRenderPipelineBatching = asset.SRPBatching; }
void DrawForward(Camera camera, CullingResults cullingResults, ScriptableRenderContext context) { bool enableDynamicBatching = false; bool enableInstancing = false; PerObjectData perObjectData = PerObjectData.None; FilteringSettings opaqueFilteringSettings = new FilteringSettings(RenderQueueRange.opaque); FilteringSettings transparentFilteringSettings = new FilteringSettings(RenderQueueRange.transparent); SortingSettings opaqueSortingSettings = new SortingSettings(camera); opaqueSortingSettings.criteria = SortingCriteria.CommonOpaque; // ShaderTagId must match the "LightMode" tag inside the shader pass. // If not "LightMode" tag is found the object won't render. DrawingSettings opaqueDrawingSettings = new DrawingSettings(ShaderPassTag.forwardLit, opaqueSortingSettings); opaqueDrawingSettings.enableDynamicBatching = enableDynamicBatching; opaqueDrawingSettings.enableInstancing = enableInstancing; opaqueDrawingSettings.perObjectData = perObjectData; // Render Opaque objects given the filtering and settings computed above. // This functions will sort and batch objects. context.DrawRenderers(cullingResults, ref opaqueDrawingSettings, ref opaqueFilteringSettings); // Renders skybox if required if (camera.clearFlags == CameraClearFlags.Skybox && RenderSettings.skybox != null) { context.DrawSkybox(camera); } }
/// <summary> /// 画可见的几何体 /// </summary> void DrawVisibleGeometry(bool useDynamicBathching, bool useGPUInstancing, bool useLightsPerObject, int renderingLayerMask) { PerObjectData lightsPerObjectFlags = useLightsPerObject ? PerObjectData.LightData | PerObjectData.LightIndices : PerObjectData.None; //先画不透明物体 顺序是从前往后画 不透明 在半透明物体 var sortingSettings = new SortingSettings(camera) { criteria = SortingCriteria.CommonOpaque }; //开启动态合批 关闭GPU Instancing Draw unlitShader 和 litShaderTagId LightMap LightProbe LightProbeVolume Reflection Probe var drawingSettings = new DrawingSettings(unlitShaderTagId, sortingSettings) { enableDynamicBatching = useDynamicBathching, enableInstancing = useGPUInstancing, perObjectData = PerObjectData.ReflectionProbes | PerObjectData.Lightmaps | PerObjectData.ShadowMask | PerObjectData.LightProbe | PerObjectData.OcclusionProbe | PerObjectData.LightProbeProxyVolume | PerObjectData.OcclusionProbeProxyVolume | lightsPerObjectFlags }; //CPU 发送数据给GPU 动态物体ShadowMask drawingSettings.SetShaderPassName(1, litShaderTagId); var filteringSettings = new FilteringSettings(RenderQueueRange.opaque, renderingLayerMask: (uint)renderingLayerMask); //Draw Opaque Visible Renderer context.DrawRenderers(cullingResults, ref drawingSettings, ref filteringSettings); //还没提交到GPU Queque队列里面 CPU维护了一个Command List队列 context.DrawSkybox(camera); if (useColorTexture || useDepthTexture) { //拷贝深度图 CopyAttachments(); } //画半透明物体 半透明物体不写入深度 sortingSettings.criteria = SortingCriteria.CommonTransparent; drawingSettings.sortingSettings = sortingSettings; filteringSettings.renderQueueRange = RenderQueueRange.transparent; context.DrawRenderers(cullingResults, ref drawingSettings, ref filteringSettings); }
int m_UseSrpBatcher; // only needed to match native struct #pragma warning restore 414 public DrawingSettings(ShaderTagId shaderPassName, SortingSettings sortingSettings) { m_SortingSettings = sortingSettings; m_PerObjectData = PerObjectData.None; m_Flags = DrawRendererFlags.EnableInstancing; m_OverrideMaterialInstanceId = 0; m_OverrideMaterialPassIndex = 0; m_fallbackMaterialInstanceId = 0; m_MainLightIndex = -1; fixed(int *p = shaderPassNames) { p[0] = shaderPassName.id; for (int i = 1; i < maxShaderPasses; i++) { p[i] = -1; } } m_PerObjectData = PerObjectData.None; m_Flags = DrawRendererFlags.EnableInstancing; m_UseSrpBatcher = 0; }
private void DrawVisibleGeometry(bool useDynamicBatching, bool useGPUInstancing, bool useLightsPerObject) { PerObjectData lightsPerObjectFlags = useLightsPerObject ? PerObjectData.LightData | PerObjectData.LightIndices : PerObjectData.None; SortingSettings sortingSettings = new SortingSettings(camera) { criteria = SortingCriteria.CommonOpaque }; DrawingSettings drawingSettings = new DrawingSettings(unlitShaderTagId, sortingSettings) { enableDynamicBatching = useDynamicBatching, enableInstancing = useGPUInstancing, perObjectData = PerObjectData.ReflectionProbes | PerObjectData.Lightmaps | PerObjectData.ShadowMask | PerObjectData.LightProbe | PerObjectData.OcclusionProbe | PerObjectData.LightProbeProxyVolume | PerObjectData.OcclusionProbeProxyVolume | lightsPerObjectFlags }; drawingSettings.SetShaderPassName(1, litShaderTagId); FilteringSettings filteringSettings = new FilteringSettings(RenderQueueRange.opaque); context.DrawRenderers(cullingResults, ref drawingSettings, ref filteringSettings); context.DrawSkybox(camera); sortingSettings.criteria = SortingCriteria.CommonTransparent; drawingSettings.sortingSettings = sortingSettings; filteringSettings.renderQueueRange = RenderQueueRange.transparent; context.DrawRenderers(cullingResults, ref drawingSettings, ref filteringSettings); }
protected override void Render(ScriptableRenderContext context, Camera[] cameras) { FilteringSettings opaqueFilteringSettings = new FilteringSettings(RenderQueueRange.opaque); FilteringSettings transparentFilteringSettings = new FilteringSettings(RenderQueueRange.transparent); bool enableDynamicBatching = false; bool enableInstancing = false; PerObjectData perObjectData = PerObjectData.None; foreach (Camera camera in cameras) { #if UNITY_EDITOR if (camera.cameraType == CameraType.SceneView) { ScriptableRenderContext.EmitWorldGeometryForSceneView(camera); } #endif // Culling. Adjust culling parameters for your needs. One could enable/disable // per-object lighting or control shadow caster distance. camera.TryGetCullingParameters(m_IsStereoSupported, out var cullingParameters); var cullingResults = context.Cull(ref cullingParameters); SortingSettings opaqueSortingSettings = new SortingSettings(camera); opaqueSortingSettings.criteria = SortingCriteria.CommonOpaque; // ShaderTagId must match the "LightMode" tag inside the shader pass. // If not "LightMode" tag is found the object won't render. DrawingSettings opaqueDrawingSettings = new DrawingSettings(m_ShaderTagId, opaqueSortingSettings); opaqueDrawingSettings.enableDynamicBatching = enableDynamicBatching; opaqueDrawingSettings.enableInstancing = enableInstancing; opaqueDrawingSettings.perObjectData = perObjectData; // Helper method to setup some per-camera shader constants and camera matrices. context.SetupCameraProperties(camera, m_IsStereoSupported); // Sets active render target and clear based on camera backgroud color. var cmd = CommandBufferPool.Get(); cmd.SetRenderTarget(BuiltinRenderTextureType.CameraTarget); cmd.ClearRenderTarget(true, true, camera.backgroundColor); context.ExecuteCommandBuffer(cmd); CommandBufferPool.Release(cmd); // Render Opaque objects given the filtering and settings computed above. // This functions will sort and batch objects. context.DrawRenderers(cullingResults, ref opaqueDrawingSettings, ref opaqueFilteringSettings); // Renders skybox if required if (camera.clearFlags == CameraClearFlags.Skybox && RenderSettings.skybox != null) { context.DrawSkybox(camera); } // Submit commands to GPU. Up to this point all commands have been enqueued in the context. // Several submits can be done in a frame to better controls CPU/GPU workload. context.Submit(); } }
/// <summary> /// Configure the pass before execution /// </summary> /// <param name="baseDescriptor">Current target descriptor</param> /// <param name="colorAttachmentHandle">Color attachment to render into</param> /// <param name="depthAttachmentHandle">Depth attachment to render into</param> /// <param name="configuration">Specific render configuration</param> public void Setup( RenderTextureDescriptor baseDescriptor, RenderTargetHandle colorAttachmentHandle, RenderTargetHandle depthAttachmentHandle, PerObjectData configuration) { this.colorAttachmentHandle = colorAttachmentHandle; this.depthAttachmentHandle = depthAttachmentHandle; descriptor = baseDescriptor; rendererConfiguration = configuration; }
void DrawVisibleGeometry( bool useDynamicBatching, bool useGPUInstancing, bool useLightsPerObject, int renderingLayerMask ) { PerObjectData lightsPerObjectFlags = useLightsPerObject ? PerObjectData.LightData | PerObjectData.LightIndices : PerObjectData.None; var sortingSettings = new SortingSettings(camera) { criteria = SortingCriteria.CommonOpaque }; var drawingSettings = new DrawingSettings( unlitShaderTagId, sortingSettings ) { enableDynamicBatching = useDynamicBatching, enableInstancing = useGPUInstancing, perObjectData = PerObjectData.ReflectionProbes | PerObjectData.Lightmaps | PerObjectData.ShadowMask | PerObjectData.LightProbe | PerObjectData.LightProbeProxyVolume | PerObjectData.OcclusionProbe | PerObjectData.OcclusionProbeProxyVolume | lightsPerObjectFlags }; drawingSettings.SetShaderPassName(1, litShaderTagId); var filteringSettings = new FilteringSettings( RenderQueueRange.opaque, renderingLayerMask: (uint)renderingLayerMask); context.DrawRenderers( cullingResults, ref drawingSettings, ref filteringSettings ); context.DrawSkybox(camera); if (useColorTexture || useDepthTexture) { CopyAttachments(); } sortingSettings.criteria = SortingCriteria.CommonTransparent; drawingSettings.sortingSettings = sortingSettings; filteringSettings.renderQueueRange = RenderQueueRange.transparent; context.DrawRenderers( cullingResults, ref drawingSettings, ref filteringSettings ); }
void DrawVisibleGeometry(bool useDynameicBatching, bool useGPUInstancing, bool useLightPerObject, int renderingLayerMask) { //per Object light data stuff PerObjectData lightPerObjectFlags = useLightPerObject ? PerObjectData.LightData | PerObjectData.LightIndices : PerObjectData.None; //draw opaque var sortingSettings = new SortingSettings(camera) { criteria = SortingCriteria.CommonOpaque}; //drawing setting what kind of shader should be draw var drawingSettings = new DrawingSettings(unlitShaderTagId, sortingSettings) { enableDynamicBatching = useDynameicBatching, enableInstancing = useGPUInstancing, perObjectData = PerObjectData.Lightmaps |//lightmap UV PerObjectData.LightProbe |//lighting Probe coefficient PerObjectData.LightProbeProxyVolume |// LPPV data PerObjectData.ShadowMask |//shadowmask texture PerObjectData.OcclusionProbe|//for using lightmap on dynamic assets PerObjectData.OcclusionProbeProxyVolume |//same above for LPPV PerObjectData.ReflectionProbes |//send reflection probes to GPU lightPerObjectFlags }; drawingSettings.SetShaderPassName(1, LitShaderTadId); //filter object queue as well as RenderingLayerMask var filteringSettings = new FilteringSettings(RenderQueueRange.opaque, renderingLayerMask:(uint)renderingLayerMask); context.DrawRenderers( cullingResults, ref drawingSettings, ref filteringSettings); //we are drawing in order like opaque->skybox->tranparent context.DrawSkybox(camera); //copy the depth and color of all opaque and sky //so if the opaque object tries to sample the _CameraDepthTexture or _CameraColorTexture, //result will be invalid if (useColorTexture || useDepthTexture) { CopyAttachments(); } //draw transparent sortingSettings.criteria = SortingCriteria.CommonTransparent; drawingSettings.sortingSettings = sortingSettings; filteringSettings.renderQueueRange = RenderQueueRange.transparent; context.DrawRenderers( cullingResults, ref drawingSettings, ref filteringSettings); }
public VXGIRenderPipeline(VXGIRenderPipelineAsset asset) { _renderer = new VXGIRenderer(this); _command = new CommandBuffer() { name = "VXGI.RenderPipeline" }; _filteringSettings = FilteringSettings.defaultValue; PerObjectData = asset.perObjectData; Shader.globalRenderPipeline = "VXGI"; GraphicsSettings.lightsUseLinearIntensity = true; GraphicsSettings.useScriptableRenderPipelineBatching = asset.SRPBatching; }
/// <summary> /// Configure the pass before execution /// </summary> /// <param name="baseDescriptor">Current target descriptor</param> /// <param name="colorAttachmentHandle">Color attachment to render into</param> /// <param name="depthAttachmentHandle">Depth attachment to render into</param> /// <param name="clearFlag">Camera clear flag</param> /// <param name="clearColor">Camera clear color</param> /// <param name="configuration">Specific render configuration</param> public void Setup( RenderTextureDescriptor baseDescriptor, RenderTargetHandle colorAttachmentHandle, RenderTargetHandle depthAttachmentHandle, ClearFlag clearFlag, Color clearColor, PerObjectData configuration) { this.colorAttachmentHandle = colorAttachmentHandle; this.depthAttachmentHandle = depthAttachmentHandle; this.clearColor = CoreUtils.ConvertSRGBToActiveColorSpace(clearColor); this.clearFlag = clearFlag; descriptor = baseDescriptor; this.rendererConfiguration = configuration; }
/// <summary> /// Execute the DrawRenderers with parameters setup from the editor /// </summary> /// <param name="ctx">The context of the custom pass. Contains command buffer, render context, buffer, etc.</param> protected override void Execute(CustomPassContext ctx) { var shaderPasses = GetShaderTagIds(); if (overrideMaterial != null) { shaderPasses[shaderPasses.Length - 1] = new ShaderTagId(overrideMaterialPassName); overrideMaterial.SetFloat(fadeValueId, fadeValue); } if (shaderPasses.Length == 0) { Debug.LogWarning("Attempt to call DrawRenderers with an empty shader passes. Skipping the call to avoid errors"); return; } var mask = overrideDepthState ? RenderStateMask.Depth : 0; mask |= overrideDepthState && !depthWrite ? RenderStateMask.Stencil : 0; var stateBlock = new RenderStateBlock(mask) { depthState = new DepthState(depthWrite, depthCompareFunction), // We disable the stencil when the depth is overwritten but we don't write to it, to prevent writing to the stencil. stencilState = new StencilState(false), }; PerObjectData renderConfig = ctx.hdCamera.frameSettings.IsEnabled(FrameSettingsField.Shadowmask) ? HDUtils.GetBakedLightingWithShadowMaskRenderConfig() : HDUtils.GetBakedLightingRenderConfig(); var result = new RendererUtils.RendererListDesc(shaderPasses, ctx.cullingResults, ctx.hdCamera.camera) { rendererConfiguration = renderConfig, renderQueueRange = GetRenderQueueRange(renderQueueType), sortingCriteria = sortingCriteria, excludeObjectMotionVectors = false, overrideMaterial = overrideMaterial, overrideMaterialPassIndex = (overrideMaterial != null) ? overrideMaterial.FindPass(overrideMaterialPassName) : 0, stateBlock = stateBlock, layerMask = layerMask, }; var renderCtx = ctx.renderContext; CoreUtils.DrawRendererList(ctx.renderContext, ctx.cmd, renderCtx.CreateRendererList(result)); }
protected override void Execute(ScriptableRenderContext renderContext, CommandBuffer cmd, HDCamera hdCamera, CullingResults cullingResult) { PerObjectData renderConfig = PerObjectData.LightProbe | PerObjectData.Lightmaps | PerObjectData.LightProbeProxyVolume; if (hdCamera.frameSettings.IsEnabled(FrameSettingsField.Shadowmask)) { renderConfig |= PerObjectData.OcclusionProbe | PerObjectData.OcclusionProbeProxyVolume | PerObjectData.ShadowMask; } // We use a different HDRP shader passes for depth prepass (where we write depth + normal) and forward (were we only write color) bool isDepthNormal = injectionPoint == CustomPassInjectionPoint.AfterOpaqueDepthAndNormal; var ids = isDepthNormal ? depthPrepassIds : forwardIds; var result = new RendererListDesc(ids, cullingResult, hdCamera.camera) { rendererConfiguration = renderConfig, renderQueueRange = GetRenderQueueRange(RenderQueueType.AllOpaque), sortingCriteria = SortingCriteria.CommonOpaque, excludeObjectMotionVectors = false, layerMask = layerMask, }; if (isDepthNormal) { // Bind normal + depth buffer var normalBuffer = GetNormalBuffer(); GetCameraBuffers(out _, out var depth); CoreUtils.SetRenderTarget(cmd, normalBuffer, depth, ClearFlag.None); // Enable keyword to write normal in the depth pre-pass CoreUtils.SetKeyword(cmd, "WRITE_NORMAL_BUFFER", true); } // Render all the opaque objects in the layer HDUtils.DrawRendererList(renderContext, cmd, RendererList.Create(result)); if (isDepthNormal) { // Reset the keyword to it's default value CoreUtils.SetKeyword(cmd, "WRITE_NORMAL_BUFFER", hdCamera.frameSettings.litShaderMode == LitShaderMode.Forward); } }
void RenderGbufferSubPass(ScriptableRenderContext context, CullingResults cullingResults, Camera camera) { bool enableDynamicBatching = false; bool enableInstancing = false; SortingSettings opaqueSortingSettings = new SortingSettings(camera); PerObjectData perObjectData = PerObjectData.None; // DrawGBuffers DrawingSettings gBufferDrawingSettings = new DrawingSettings(ShaderPassTag.GBuffer, opaqueSortingSettings); FilteringSettings opaqueFilteringSettings = new FilteringSettings(RenderQueueRange.opaque); gBufferDrawingSettings.enableDynamicBatching = enableDynamicBatching; gBufferDrawingSettings.enableInstancing = enableInstancing; gBufferDrawingSettings.perObjectData = perObjectData; var cmd = CommandBufferPool.Get("GbufferSubpass"); cmd.SetRenderTarget(m_GBufferRTIDs, m_DepthBufferRTID); cmd.ClearRenderTarget(true, true, camera.backgroundColor); //CoreUtils.SetRenderTarget(cmd, m_GBufferRTIDs, m_DepthBufferRTID, ClearFlag.All); context.ExecuteCommandBuffer(cmd); CommandBufferPool.Release(cmd); context.DrawRenderers(cullingResults, ref gBufferDrawingSettings, ref opaqueFilteringSettings); }
void RenderGBuffer(Camera camera, CullingResults cullingResults, ScriptableRenderContext context) { bool enableDynamicBatching = false; bool enableInstancing = false; PerObjectData perObjectData = PerObjectData.None; FilteringSettings opaqueFilteringSettings = new FilteringSettings(RenderQueueRange.opaque); FilteringSettings transparentFilteringSettings = new FilteringSettings(RenderQueueRange.transparent); SortingSettings opaqueSortingSettings = new SortingSettings(camera); opaqueSortingSettings.criteria = SortingCriteria.CommonOpaque; // ShaderTagId must match the "LightMode" tag inside the shader pass. // If not "LightMode" tag is found the object won't render. DrawingSettings opaqueDrawingSettings = new DrawingSettings(ShaderPassTag.deferredGBuffer, opaqueSortingSettings); opaqueDrawingSettings.enableDynamicBatching = enableDynamicBatching; opaqueDrawingSettings.enableInstancing = enableInstancing; opaqueDrawingSettings.perObjectData = perObjectData; context.DrawRenderers(cullingResults, ref opaqueDrawingSettings, ref opaqueFilteringSettings); }
/// <summary> /// Execute the DrawRenderers with parameters setup from the editor /// </summary> /// <param name="renderContext"></param> /// <param name="cmd"></param> /// <param name="camera"></param> /// <param name="cullingResult"></param> protected override void Execute(ScriptableRenderContext renderContext, CommandBuffer cmd, HDCamera hdCamera, CullingResults cullingResult) { var shaderPasses = GetShaderTagIds(); if (overrideMaterial != null) { shaderPasses[forwardShaderTags.Length - 1] = new ShaderTagId(overrideMaterialPassName); overrideMaterial.SetFloat(fadeValueId, fadeValue); } if (shaderPasses.Length == 0) { Debug.LogWarning("Attempt to call DrawRenderers with an empty shader passes. Skipping the call to avoid errors"); return; } var stateBlock = new RenderStateBlock(overrideDepthState ? RenderStateMask.Depth : 0) { depthState = new DepthState(depthWrite, depthCompareFunction), }; PerObjectData renderConfig = hdCamera.frameSettings.IsEnabled(FrameSettingsField.Shadowmask) ? HDUtils.k_RendererConfigurationBakedLightingWithShadowMask : HDUtils.k_RendererConfigurationBakedLighting; var result = new RendererListDesc(shaderPasses, cullingResult, hdCamera.camera) { rendererConfiguration = renderConfig, renderQueueRange = GetRenderQueueRange(renderQueueType), sortingCriteria = sortingCriteria, excludeObjectMotionVectors = false, overrideMaterial = overrideMaterial, overrideMaterialPassIndex = (overrideMaterial != null) ? overrideMaterial.FindPass(overrideMaterialPassName) : 0, stateBlock = stateBlock, layerMask = layerMask, }; HDUtils.DrawRendererList(renderContext, cmd, RendererList.Create(result)); }
protected DrawingSettings CreateDrawingSettings(Camera camera, SortingCriteria sortingCriteria, PerObjectData perObjectData, bool supportsDynamicBatching, int mainLightIndex = -1) { SortingSettings sortingSettings = new SortingSettings(camera) { criteria = sortingCriteria }; DrawingSettings settings = new DrawingSettings(m_ShaderTagIDs[0], sortingSettings) { perObjectData = perObjectData, enableInstancing = true, mainLightIndex = mainLightIndex, enableDynamicBatching = supportsDynamicBatching }; for (int i = 1; i < m_ShaderTagIDs.Count; ++i) { settings.SetShaderPassName(i, m_ShaderTagIDs[i]); } return(settings); }
void DeferredLightPass(ScriptableRenderContext context, CullingResults cullingResults, Camera camera) { var cmd = CommandBufferPool.Get("SetupGlobalLights"); SortingSettings opaqueSortingSettings = new SortingSettings(camera); opaqueSortingSettings.criteria = SortingCriteria.CommonOpaque; FilteringSettings opaqueFilteringSettings = new FilteringSettings(RenderQueueRange.opaque); if (cullingResults.visibleLights.Length > 0) { VisibleLight lightData = cullingResults.visibleLights[0]; Vector4 dir = -lightData.localToWorldMatrix.GetColumn(2); Vector4 lightPos = new Vector4(dir.x, dir.y, dir.z, 0.0f); cmd.SetGlobalVector("_MainLightPosition", lightPos); cmd.SetGlobalVector("_MainLightColor", lightData.finalColor); } if (cullingResults.visibleReflectionProbes.Length > 0) { cmd.SetGlobalTexture("unity_SpecCube0", cullingResults.visibleReflectionProbes[0].texture); } context.ExecuteCommandBuffer(cmd); CommandBufferPool.Release(cmd); bool useRenderPass = false; if (!useRenderPass) { bool enableDynamicBatching = false; bool enableInstancing = false; PerObjectData perObjectData = PerObjectData.None; // DrawGBuffers DrawingSettings gBufferDrawingSettings = new DrawingSettings(ShaderPassTag.GBuffer, opaqueSortingSettings); gBufferDrawingSettings.enableDynamicBatching = enableDynamicBatching; gBufferDrawingSettings.enableInstancing = enableInstancing; gBufferDrawingSettings.perObjectData = perObjectData; cmd = CommandBufferPool.Get("Gbuffer"); cmd.SetRenderTarget(m_GBufferRTIDs, m_DepthBufferRTID); cmd.ClearRenderTarget(true, true, camera.backgroundColor); //CoreUtils.SetRenderTarget(cmd, m_GBufferRTIDs, m_DepthBufferRTID, ClearFlag.All); context.ExecuteCommandBuffer(cmd); CommandBufferPool.Release(cmd); context.DrawRenderers(cullingResults, ref gBufferDrawingSettings, ref opaqueFilteringSettings); cmd = CommandBufferPool.Get("DeferredLightingPass"); cmd.SetGlobalVector("unity_SpecCube0_HDR", new Vector4(1, 1, 0, 0)); // Bind buffers cmd.SetGlobalTexture("_GBufferAlbedo", m_GBufferRTIDs[0]); cmd.SetGlobalTexture("_GBufferNormal", m_GBufferRTIDs[1]); cmd.SetGlobalTexture("_GBufferMetallicOcclusionSmoothness", m_GBufferRTIDs[2]); cmd.SetGlobalTexture("_GBufferDepth", m_DepthBufferRTID); cmd.SetGlobalInt("_TileCountX", (camera.scaledPixelWidth + 64 - 1) / 64); cmd.SetGlobalInt("_TileCountY", (camera.scaledPixelHeight + 64 - 1) / 64); cmd.SetGlobalVector("unity_LightData", new Vector4(6, 0, 1, 0)); //Set RenderTarget cmd.SetRenderTarget(m_ColorBuffer, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store); cmd.ClearRenderTarget(true, true, Color.black, 0.0f); //cmd.SetViewProjectionMatrices(Matrix4x4.identity, Matrix4x4.identity); //cmd.SetViewport(camera.pixelRect); cmd.DrawMesh(CustomRenderPipeline.fullscreenMesh, Matrix4x4.identity, m_DeferredLightingMat, 0, 0); context.ExecuteCommandBuffer(cmd); CommandBufferPool.Release(cmd); //cmd.SetViewport(new Rect(0,0,camera.scaledPixelWidth, camera.scaledPixelHeight)); } else { // var colorBuffer = new AttachmentDescriptor(RenderTextureFormat.ARGBHalf); // 64 bit // var depthBuffer = new AttachmentDescriptor(RenderTextureFormat.Depth); // 32 bit // var albedoGBuffer = new AttachmentDescriptor(RenderTextureFormat.ARGB32); // 32 bit // var normalGBuffer = new AttachmentDescriptor(RenderTextureFormat.ARGBHalf); // 64 bit // var pbrGBuffer = new AttachmentDescriptor(RenderTextureFormat.ARGB32); // 32 // var depthGBuffer = new AttachmentDescriptor(RenderTextureFormat.RFloat); // 32 bit // // colorBuffer.ConfigureClear(new Color(0.0f, 0.0f, 0.0f, 0.0f), 1.0f, 0); // depthBuffer.ConfigureClear(new Color(), 1.0f, 0); // colorBuffer.ConfigureTarget(BuiltinRenderTextureType.CameraTarget, true, true); // // var attachments = new NativeArray<AttachmentDescriptor>(6, Allocator.Temp); // const int colorBufferId = 0, depthBufferId = 1, albedoGBufferId = 2, normalGBufferId = 3, pbrGbufferId = 4, depthGBufferId = 5; // //const int colorBufferId = 0, albedoGBufferId = 2, normalGBufferId = 3, pbrGbufferId = 4, depthGBufferId = 5; // attachments[colorBufferId] = colorBuffer; // attachments[depthBufferId] = depthBuffer; // attachments[albedoGBufferId] = albedoGBuffer; // attachments[normalGBufferId] = normalGBuffer; // attachments[pbrGbufferId] = pbrGBuffer; // attachments[depthGBufferId] = depthGBuffer; // // using (context.BeginScopedRenderPass(camera.scaledPixelWidth, camera.scaledPixelHeight, 1, attachments, depthBufferId)) // { // attachments.Dispose(); // var gBuffers = new NativeArray<int>(4, Allocator.Temp); // // //gBuffers[0] = colorBufferId; // gBuffers[0] = albedoGBufferId; // gBuffers[1] = normalGBufferId; // gBuffers[2] = pbrGbufferId; // gBuffers[3] = depthGBufferId; // using (context.BeginScopedSubPass(gBuffers)) // { // RenderGbufferSubPass(context, cullingResults, camera); // } // var lightingColors = new NativeArray<int>(1, Allocator.Temp); // lightingColors[0] = colorBufferId; // var lightingInputs = new NativeArray<int>(4, Allocator.Temp); // lightingInputs[0] = albedoGBufferId; // //lightingInputs[1] = albedoGBufferId; // lightingInputs[1] = normalGBufferId; // lightingInputs[2] = pbrGbufferId; // lightingInputs[3] = depthGBufferId; // using (context.BeginScopedSubPass(lightingColors, lightingInputs, true)) // { // lightingColors.Dispose(); // lightingInputs.Dispose(); // // // RenderLighting(camera, cullResults, context); // RenderDeferredLightingSubPass(context, cullingResults, camera); // } // // // } var albedo = new AttachmentDescriptor(RenderTextureFormat.ARGB32); var specRough = new AttachmentDescriptor(RenderTextureFormat.ARGB32); var normal = new AttachmentDescriptor(RenderTextureFormat.ARGB2101010); var emission = new AttachmentDescriptor(RenderTextureFormat.ARGBHalf); var depth = new AttachmentDescriptor(RenderTextureFormat.Depth); emission.ConfigureClear(new Color(0.0f, 0.0f, 0.0f, 0.0f), 1.0f, 0); depth.ConfigureClear(new Color(), 1.0f, 0); albedo.ConfigureTarget(BuiltinRenderTextureType.CameraTarget, false, true); var attachments = new NativeArray <AttachmentDescriptor>(5, Allocator.Temp); const int depthIndex = 0, albedoIndex = 1, specRoughIndex = 2, normalIndex = 3, emissionIndex = 4; attachments[depthIndex] = depth; attachments[albedoIndex] = albedo; attachments[specRoughIndex] = specRough; attachments[normalIndex] = normal; attachments[emissionIndex] = emission; using (context.BeginScopedRenderPass(camera.pixelWidth, camera.pixelHeight, 1, attachments, depthIndex)) { attachments.Dispose(); // Start the first subpass, GBuffer creation: render to albedo, specRough, normal and emission, no need to read any input attachments var gbufferColors = new NativeArray <int>(4, Allocator.Temp); gbufferColors[0] = albedoIndex; gbufferColors[1] = specRoughIndex; gbufferColors[2] = normalIndex; gbufferColors[3] = emissionIndex; using (context.BeginScopedSubPass(gbufferColors)) { gbufferColors.Dispose(); // Render the deferred G-Buffer RenderGbufferSubPass(context, cullingResults, camera); } // Second subpass, lighting: Render to the emission buffer, read from albedo, specRough, normal and depth. // The last parameter indicates whether the depth buffer can be bound as read-only. // Note that some renderers (notably iOS Metal) won't allow reading from the depth buffer while it's bound as Z-buffer, // so those renderers should write the Z into an additional FP32 render target manually in the pixel shader and read from it instead var lightingColors = new NativeArray <int>(1, Allocator.Temp); lightingColors[0] = emissionIndex; var lightingInputs = new NativeArray <int>(4, Allocator.Temp); lightingInputs[0] = albedoIndex; lightingInputs[1] = specRoughIndex; lightingInputs[2] = normalIndex; lightingInputs[3] = depthIndex; using (context.BeginScopedSubPass(lightingColors, lightingInputs, true)) { lightingColors.Dispose(); lightingInputs.Dispose(); // PushGlobalShadowParams(context); RenderDeferredLightingSubPass(context, cullingResults, camera); } // Third subpass, tonemapping: Render to albedo (which is bound to the camera target), read from emission. // var tonemappingColors = new NativeArray<int>(1, Allocator.Temp); // tonemappingColors[0] = albedoIndex; // var tonemappingInputs = new NativeArray<int>(1, Allocator.Temp); // tonemappingInputs[0] = emissionIndex; // using (context.BeginScopedSubPass(tonemappingColors, tonemappingInputs, true)) // { // tonemappingColors.Dispose(); // tonemappingInputs.Dispose(); // // // present frame buffer. // // FinalPass(context); // } } } }
///////////Mesh Batch public static RendererListDesc CreateRendererListDesc(CullingResults CullingData, Camera RenderCamera, ShaderTagId PassName, PerObjectData rendererConfiguration = 0, RenderQueueRange?renderQueueRange = null, RenderStateBlock?stateBlock = null, Material overrideMaterial = null, bool excludeObjectMotionVectors = false) { RendererListDesc result = new RendererListDesc(PassName, CullingData, RenderCamera) { rendererConfiguration = rendererConfiguration, renderQueueRange = RenderQueueRange.opaque, sortingCriteria = SortingCriteria.CommonOpaque, stateBlock = stateBlock, overrideMaterial = overrideMaterial, excludeObjectMotionVectors = excludeObjectMotionVectors }; return(result); }
internal static void SetupCullingResultsForRendering(ref RenderingData renderingData, ref CullingResults cullingResults, PerObjectData perObjectData) { if (0 < renderingData.lightData.additionalLightsCount && (perObjectData & PerObjectData.LightIndices) != 0) { cullingResults.visibleLights.CopyFrom(renderingData.cullResults.visibleLights); var lightIndexMap = renderingData.cullResults.GetLightIndexMap(Unity.Collections.Allocator.Temp); cullingResults.SetLightIndexMap(lightIndexMap); lightIndexMap.Dispose(); } if ((perObjectData & PerObjectData.ReflectionProbes) != 0) { cullingResults.visibleReflectionProbes.CopyFrom(renderingData.cullResults.visibleReflectionProbes); var indexMap = renderingData.cullResults.GetReflectionProbeIndexMap(Unity.Collections.Allocator.Temp); cullingResults.SetReflectionProbeIndexMap(indexMap); indexMap.Dispose(); } }
void DrawCameraRecursive(ScriptableRenderContext context, Camera camera, Camera portalCamera, int depth) { BeginCameraRendering(context, camera); CullingResults cullingResults = Cull(context, camera); InitializeLightData(ref cullingResults, out LightData lightData); m_lights.Setup(context, ref lightData); bool enableDynamicBatching = false; bool enableInstancing = false; PerObjectData perObjectData = PerObjectData.LightData | PerObjectData.LightIndices; FilteringSettings opaqueFilteringSettings = new FilteringSettings(RenderQueueRange.opaque); FilteringSettings transparentFilteringSettings = new FilteringSettings(RenderQueueRange.transparent); SortingSettings opaqueSortingSettings = new SortingSettings(camera); opaqueSortingSettings.criteria = SortingCriteria.CommonOpaque; SortingSettings transparentSortingSettings = new SortingSettings(camera); transparentSortingSettings.criteria = SortingCriteria.CommonTransparent; // ShaderTagId must match the "LightMode" tag inside the shader pass. // If not "LightMode" tag is found the object won't render. DrawingSettings opaqueDrawingSettings = new DrawingSettings(ShaderPassTag.forwardLit, opaqueSortingSettings); opaqueDrawingSettings.enableDynamicBatching = enableDynamicBatching; opaqueDrawingSettings.enableInstancing = enableInstancing; opaqueDrawingSettings.perObjectData = perObjectData; DrawingSettings transparentDrawingSettings = new DrawingSettings(ShaderPassTag.forwardLit, transparentSortingSettings); transparentDrawingSettings.enableDynamicBatching = enableDynamicBatching; transparentDrawingSettings.enableInstancing = enableInstancing; transparentDrawingSettings.perObjectData = perObjectData; ProfilingSampler cameraSampler = new ProfilingSampler(camera.name); CommandBuffer cameraCmd = CommandBufferPool.Get(cameraSampler.name); using (new ProfilingScope(cameraCmd, cameraSampler)) { context.ExecuteCommandBuffer(cameraCmd); cameraCmd.Clear(); context.SetupCameraProperties(camera); DrawRenderersProfiled(context, cullingResults, "Opaque", ref opaqueDrawingSettings, ref opaqueFilteringSettings); if (portalCamera != null) { // TODO set portal camera position DrawCameraRecursive(context, portalCamera, null, depth + 1); // TODO reset portal camera position (optional) } context.SetupCameraProperties(camera); DrawRenderersProfiled(context, cullingResults, "Transparent", ref transparentDrawingSettings, ref transparentFilteringSettings); } context.ExecuteCommandBuffer(cameraCmd); CommandBufferPool.Release(cameraCmd); EndCameraRendering(context, camera); }