public class HighlightPlusRenderPassFeature : ScriptableRendererFeature { class…
- User
public class HighlightPlusRenderPassFeature : ScriptableRendererFeature { class HighlightPass : ScriptableRenderPass {
// far objects render first class DistanceComparer : IComparer<HighlightEffect> { public Vector3 camPos; public int Compare(HighlightEffect e1, HighlightEffect e2) { Vector3 e1Pos = e1 == null ? Vector3.zero : e1.transform.position; float dx1 = e1Pos.x - camPos.x; float dy1 = e1Pos.y - camPos.y; float dz1 = e1Pos.z - camPos.z; float distE1 = dx1 * dx1 + dy1 * dy1 + dz1 * dz1; Vector3 e2Pos = e2 == null ? Vector3.zero : e2.transform.position; float dx2 = e2Pos.x - camPos.x; float dy2 = e2Pos.y - camPos.y; float dz2 = e2Pos.z - camPos.z; float distE2 = dx2 * dx2 + dy2 * dy2 + dz2 * dz2; if (distE1 > distE2) return -1; if (distE1 < distE2) return 1; return 0; } } public bool usesCameraOverlay = false; ScriptableRenderer renderer; RenderTextureDescriptor cameraTextureDescriptor; DistanceComparer effectDistanceComparer; static bool isVREnabled; bool clearStencil; FullScreenBlitMethod fullScreenBlitMethod = FullScreenBlit; public void Setup(HighlightPlusRenderPassFeature passFeature, ScriptableRenderer renderer) { this.renderPassEvent = passFeature.renderPassEvent; this.clearStencil = passFeature.clearStencil; this.renderer = renderer; if (effectDistanceComparer == null) { effectDistanceComparer = new DistanceComparer(); } isVREnabled = UnityEngine.XR.XRSettings.enabled && Application.isPlaying; } public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor) { this.cameraTextureDescriptor = cameraTextureDescriptor;#if UNITY_2021_2_OR_NEWER ConfigureInput(ScriptableRenderPassInput.Depth); #endif }
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData) { Camera cam = renderingData.cameraData.camera; int camLayer = 1 << cam.gameObject.layer; RenderTargetIdentifier cameraColorTarget = renderer.cameraColorTarget; RenderTargetIdentifier cameraDepthTarget = renderer.cameraDepthTarget;#if !UNITY_2021_2_OR_NEWER // In Unity 2021.2, when MSAA > 1, cameraDepthTarget is no longer cameraColorTarget if (!usesCameraOverlay && (cameraTextureDescriptor.msaaSamples > 1 || cam.cameraType == CameraType.SceneView)) { cameraDepthTarget = cameraColorTarget; } #endif if (!HighlightEffect.customSorting && (Time.frameCount % 10 == 0 || !Application.isPlaying)) { effectDistanceComparer.camPos = cam.transform.position; HighlightEffect.effects.Sort(effectDistanceComparer); }
int count = HighlightEffect.effects.Count; bool clearStencil = this.clearStencil; for (int k = 0; k < count; k++) { HighlightEffect effect = HighlightEffect.effects[k]; if (effect == null) { continue; } if (effect.isActiveAndEnabled) { if ((effect.camerasLayerMask & camLayer) == 0) continue; CommandBuffer cb = effect.GetCommandBuffer(cam, cameraColorTarget, cameraDepthTarget, fullScreenBlitMethod, clearStencil); if (cb != null) { context.ExecuteCommandBuffer(cb); clearStencil = false; } } } } static Matrix4x4 matrix4x4identity = Matrix4x4.identity; static void FullScreenBlit(CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier destination, Material material, int passIndex) { destination = new RenderTargetIdentifier(destination, 0, CubemapFace.Unknown, -1); cmd.SetRenderTarget(destination); cmd.SetGlobalTexture(ShaderParams.MainTex, source); cmd.SetGlobalFloat(ShaderParams.AspectRatio, isVREnabled ? 0.5f : 1); cmd.DrawMesh(RenderingUtils.fullscreenMesh, matrix4x4identity, material, 0, passIndex); } public override void FrameCleanup(CommandBuffer cmd) { } } HighlightPass renderPass; public RenderPassEvent renderPassEvent = RenderPassEvent.AfterRenderingTransparents; [Tooltip("Clears stencil buffer before rendering highlight effects. This option can solve compatibility issues with shaders that also use stencil buffers.")] public bool clearStencil; public static bool installed; void OnDisable() { installed = false; } public override void Create() { renderPass = new HighlightPass(); } // This method is called when setting up the renderer once per-camera. public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData) {#if UNITY_2019_4_OR_NEWER if (renderingData.cameraData.renderType == UnityEngine.Rendering.Universal.CameraRenderType.Base) { Camera cam = renderingData.cameraData.camera; renderPass.usesCameraOverlay = cam.GetUniversalAdditionalCameraData().cameraStack.Count > 0; } #endif renderPass.Setup(this, renderer); renderer.EnqueuePass(renderPass); installed = true; } } 这段代码中,RenderTexture的使用上 是否有潜在的内存破坏风险
- Assistant
The provided code is a script for a Unity development environment, specifically using the Universal Render Pipeline (URP) to implement a custom rendering feature for highlighting effects. When examining it for potential memory corruption risks related to
RenderTexture, we should consider several aspects:-
RenderTargetIdentifier Usage:
- The code uses
RenderTargetIdentifierfor managing render targets, likecameraColorTargetandcameraDepthTarget. These targets are managed by Unity, and usingRenderTargetIdentifiergenerally doesn't pose a memory risk.
- The code uses
-
RenderTextureDescriptor Management:
- The
RenderTextureDescriptoris just a descriptor and does not hold actual render texture data. It's primarily used to configure how a render texture should be allocated by Unity. There's no memory corruption risk merely by configuring this descriptor.
- The
-
Command Buffer Operations:
- The
ExecuteCommandBufferis used for executing graphics commands. Command buffers need to be used carefully, but unless there is an error in command recording, these won't lead to memory corruption. It's crucial to ensure that command buffers are properly constructed and not used after they've been disposed of.
- The
-
Potential Leaks or Mismanagement:
- It's important to manage the lifecycle of any additional resources manually created within the render pass, such as custom RenderTextures if any were allocated and managed manually. The code doesn't seem to create or destroy such resources directly within this script, reducing manual mismanagement risk.
-
VR Handling:
- The
isVREnabledlogic and use in blitting considers VR scenarios. It adjusts texture handling to manage VR's specific characteristics (e.g., aspect ratio), but this should be memory safe under typical Unity handling unless incorrectly indexed or sliced.
- The
Overall, the provided code mainly leverages Unity's high-level API for render passes, which usually abstracts away direct memory management. This reduces the likelihood of memory corruption at the level of abstraction shown in the code.
However, always ensure you're on the latest Unity version to benefit from any bug fixes or improvements in the rendering pipeline and manage additional resources explicitly created in scripts externally. To further minimize risks, ensure proper testing, particularly when dealing with any custom shaders or additional complex resources.
-