• HDRP图形入门:RTHandle未知问题


          正好电脑看奥本海默,全程尿点十足,就一边看一边把之前整合HDRP遇到的问题说一下。
          那就是RTHandle的未知问题,这是官方对RTHandle的说明:
          unity RTHandle
          源代码如下:

    using System.Collections.Generic;
    using UnityEngine.Rendering;
    
    namespace UnityEngine.Rendering
    {
        /// 
        /// A RTHandle is a RenderTexture that scales automatically with the camera size.
        /// This allows proper reutilization of RenderTexture memory when different cameras with various sizes are used during rendering.
        /// 
        /// 
        public class RTHandle
        {
            internal RTHandleSystem m_Owner;
            internal RenderTexture m_RT;
            internal Texture m_ExternalTexture;
            internal RenderTargetIdentifier m_NameID;
            internal bool m_EnableMSAA = false;
            internal bool m_EnableRandomWrite = false;
            internal bool m_EnableHWDynamicScale = false;
            internal string m_Name;
    
            internal bool m_UseCustomHandleScales = false;
            internal RTHandleProperties m_CustomHandleProperties;
    
            /// 
            /// By default, rtHandleProperties gets the global state of scalers against the global reference mode.
            /// This method lets the current RTHandle use a local custom RTHandleProperties. This function is being used
            /// by scalers such as TAAU and DLSS, which require to have a different resolution for color (independent of the RTHandleSystem).
            /// 
            /// Properties to set.
            public void SetCustomHandleProperties(in RTHandleProperties properties)
            {
                m_UseCustomHandleScales = true;
                m_CustomHandleProperties = properties;
            }
    
            /// 
            /// Method that clears any custom handle property being set.
            /// 
            public void ClearCustomHandleProperties()
            {
                m_UseCustomHandleScales = false;
            }
    
            /// 
            /// Scale factor applied to the RTHandle reference size.
            /// 
            public Vector2 scaleFactor { get; internal set; }
            internal ScaleFunc scaleFunc;
    
            /// 
            /// Returns true if the RTHandle uses automatic scaling.
            /// 
            public bool useScaling { get; internal set; }
            /// 
            /// Reference size of the RTHandle System associated with the RTHandle
            /// 
            public Vector2Int referenceSize { get; internal set; }
            /// 
            /// Current properties of the RTHandle System. If a custom property has been set through SetCustomHandleProperties method, it will be used that one instead.
            /// 
            public RTHandleProperties rtHandleProperties { get { return m_UseCustomHandleScales ? m_CustomHandleProperties : m_Owner.rtHandleProperties; } }
            /// 
            /// RenderTexture associated with the RTHandle
            /// 
            public RenderTexture rt { get { return m_RT; } }
            /// 
            /// RenderTargetIdentifier associated with the RTHandle
            /// 
            public RenderTargetIdentifier nameID { get { return m_NameID; } }
            /// 
            /// Name of the RTHandle
            /// 
            public string name { get { return m_Name; } }
    
            /// 
            /// Returns true is MSAA is enabled, false otherwise.
            /// 
            public bool isMSAAEnabled { get { return m_EnableMSAA; } }
    
            // Keep constructor private
            internal RTHandle(RTHandleSystem owner)
            {
                m_Owner = owner;
            }
    
            /// 
            /// Implicit conversion operator to RenderTargetIdentifier
            /// 
            /// Input RTHandle
            /// RenderTargetIdentifier representation of the RTHandle.
            public static implicit operator RenderTargetIdentifier(RTHandle handle)
            {
                return handle != null ? handle.nameID : default(RenderTargetIdentifier);
            }
    
            /// 
            /// Implicit conversion operator to Texture
            /// 
            /// Input RTHandle
            /// Texture representation of the RTHandle.
            public static implicit operator Texture(RTHandle handle)
            {
                // If RTHandle is null then conversion should give a null Texture
                if (handle == null)
                    return null;
    
                Debug.Assert(handle.m_ExternalTexture != null || handle.rt != null);
                return (handle.rt != null) ? handle.rt : handle.m_ExternalTexture;
            }
    
            /// 
            /// Implicit conversion operator to RenderTexture
            /// 
            /// Input RTHandle
            /// RenderTexture representation of the RTHandle.
            public static implicit operator RenderTexture(RTHandle handle)
            {
                // If RTHandle is null then conversion should give a null RenderTexture
                if (handle == null)
                    return null;
    
                Debug.Assert(handle.rt != null, "RTHandle was created using a regular Texture and is used as a RenderTexture");
                return handle.rt;
            }
    
            internal void SetRenderTexture(RenderTexture rt)
            {
                m_RT = rt;
                m_ExternalTexture = null;
                m_NameID = new RenderTargetIdentifier(rt);
            }
    
            internal void SetTexture(Texture tex)
            {
                m_RT = null;
                m_ExternalTexture = tex;
                m_NameID = new RenderTargetIdentifier(tex);
            }
    
            internal void SetTexture(RenderTargetIdentifier tex)
            {
                m_RT = null;
                m_ExternalTexture = null;
                m_NameID = tex;
            }
    
            /// 
            /// Get the Instance ID of the RTHandle.
            /// 
            /// The RTHandle Instance ID.
            public int GetInstanceID()
            {
                if (m_RT != null)
                    return m_RT.GetInstanceID();
                else if (m_ExternalTexture != null)
                    return m_ExternalTexture.GetInstanceID();
                else
                    return m_NameID.GetHashCode(); // No instance ID so we return the hash code.
            }
    
            /// 
            /// Release the RTHandle
            /// 
            public void Release()
            {
                m_Owner.Remove(this);
                CoreUtils.Destroy(m_RT);
                m_NameID = BuiltinRenderTextureType.None;
                m_RT = null;
                m_ExternalTexture = null;
            }
    
            /// 
            /// Return the input size, scaled by the RTHandle scale factor.
            /// 
            /// Input size
            /// Input size scaled by the RTHandle scale factor.
            public Vector2Int GetScaledSize(Vector2Int refSize)
            {
                if (!useScaling)
                    return refSize;
    
                if (scaleFunc != null)
                {
                    return scaleFunc(refSize);
                }
                else
                {
                    return new Vector2Int(
                        x: Mathf.RoundToInt(scaleFactor.x * refSize.x),
                        y: Mathf.RoundToInt(scaleFactor.y * refSize.y)
                    );
                }
            }
    
            /// 
            /// Return the scaled size of the RTHandle.
            /// 
            /// The scaled size of the RTHandle.
            public Vector2Int GetScaledSize()
            {
                if (scaleFunc != null)
                {
                    return scaleFunc(referenceSize);
                }
                else
                {
                    return new Vector2Int(
                        x: Mathf.RoundToInt(scaleFactor.x * referenceSize.x),
                        y: Mathf.RoundToInt(scaleFactor.y * referenceSize.y)
                    );
                }
            }
    
    #if UNITY_2020_2_OR_NEWER
            /// 
            /// Switch the render target to fast memory on platform that have it.
            /// 
            /// Command buffer used for rendering.
            /// How much of the render target is to be switched into fast memory (between 0 and 1).
            /// Flag to determine what parts of the render target is spilled if not fully resident in fast memory.
            /// Whether the content of render target are copied or not when switching to fast memory.
    
            public void SwitchToFastMemory(CommandBuffer cmd,
                float residencyFraction = 1.0f,
                FastMemoryFlags flags = FastMemoryFlags.SpillTop,
                bool copyContents = false
            )
            {
                residencyFraction = Mathf.Clamp01(residencyFraction);
                cmd.SwitchIntoFastMemory(m_RT, flags, residencyFraction, copyContents);
            }
    
            /// 
            /// Switch the render target to fast memory on platform that have it and copies the content.
            /// 
            /// Command buffer used for rendering.
            /// How much of the render target is to be switched into fast memory (between 0 and 1).
            /// Flag to determine what parts of the render target is spilled if not fully resident in fast memory.
            public void CopyToFastMemory(CommandBuffer cmd,
                float residencyFraction = 1.0f,
                FastMemoryFlags flags = FastMemoryFlags.SpillTop
            )
            {
                SwitchToFastMemory(cmd, residencyFraction, flags, copyContents: true);
            }
    
            /// 
            /// Switch out the render target from fast memory back to main memory on platforms that have fast memory.
            /// 
            /// Command buffer used for rendering.
            /// Whether the content of render target are copied or not when switching out fast memory.
            public void SwitchOutFastMemory(CommandBuffer cmd, bool copyContents = true)
            {
                cmd.SwitchOutOfFastMemory(m_RT, copyContents);
            }
    
    #endif
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262

          我自己把它理解为一个动态的RT,和RT一样的使用,当然写代码用起来很容易。却碰到一个问题,以多迭代高斯滤波为例,使用RTHandle会造成纹理“发黄”,如下:
    在这里插入图片描述      当时没感觉有问题,后面同事说这效果好奇怪,好黄?!我就感觉确实。起初我以为我shader颜色计算出问题了,查了半天不是。我百度google查了仅有不多有关RTHandle的帖子,并没找到相关问题,不过有个博主说复制一份RTHandle使用,我尝试做了一下,果然ok了。

    //复制一份原始纹理
    private RTHandle copySource;
    
    public override void Setup()
    {
        if (Shader.Find(shaderName) != null)
        {
            mat = new Material(Shader.Find(shaderName));
    
            copySource = RTHandles.Alloc(Vector2.one, filterMode: FilterMode.Bilinear, dimension: TextureDimension.Tex2DArray);
        }
        else
            Debug.LogError($"Unable to find shader '{shaderName}'. Post Process Volume GuassBlurPostProcessVolume is unable to load.");
    }
    
    public override void Render(CommandBuffer cmd, HDCamera camera, RTHandle source, RTHandle destination)
    {
        if (mat == null || !isBlur.value)
        {
            return;
        }
    
        mat.SetFloat("_Brightness", brightness.value);
    
        //复制一份纹理
        //注意:由原始的纹理数组传递到单纹理,原始的纹理数组封装了多种平台下纹理集合
        HDUtils.BlitCameraTexture(cmd, source, copySource);
    
        //使用复制RTH还是原始RTH
        RenderTexture rtinput = isCopyRT.value ? copySource : source;
    
        int rtwid = rtinput.width / downSample.value;
        int rthei = rtinput.height / downSample.value;
    
        mat.SetTexture("_InputTex", rtinput);
    
        //对pass0进行迭代,增加高斯滤波次数和效果
        for (float it = 0; it < iterations.value; it += 0.2f)
        {
            mat.SetFloat("_BlurSpread", 1.0f + it * blurSpread.value);
    
            //滤波采样一次
            RenderTexture rt1 = RenderTexture.GetTemporary(rtwid, rthei, 0);
    
            cmd.Blit(rtinput, rt1, mat, 0);
            //叠加滤波
            cmd.Blit(rt1, rtinput);
    
            RenderTexture.ReleaseTemporary(rt1);
        }
    
        //采样迭代后的纹理
        mat.SetTexture("_IterTex", rtinput);
    
        cmd.Blit(rtinput, destination, mat, 1);
    }
    
    public override void Cleanup()
    {
        CoreUtils.Destroy(mat);
        RTHandles.Release(copySource);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62

          虽然不明白为什么会这样,但确实解决了问题,如下:
    在这里插入图片描述      如果大家写渲染功能,碰到类似的问题,不妨Copy一份RTHandle。

  • 相关阅读:
    7.堆叠注入
    解读提示工程(Prompt Engineering)
    公积金提取一次需要间隔多长时间
    WebRTC系列 -- iOS 音频采集之setParameter参数处理流程
    mysql操作 sql语句中的完整性约束有哪些,主键约束、外键约束、引用完整性约束,主键外键、唯一性
    (53)linux
    QTableWidget 用法
    母亲节祝福html源码示例
    给rust的cargo环境或gnome构建器安装rust-analyzer
    Educational Codeforces Round 138 (Rated for Div. 2) A-E
  • 原文地址:https://blog.csdn.net/yinhun2012/article/details/134449432