void Clear()
        {
            TaaUtilities.EnsureArray(ref this._reprojection_index, 2);
            this._reprojection_index[0] = -1;
            this._reprojection_index[1] = -1;

            this._frustumJitter?.Clear(ref this._camera);
            this._velocityBuffer?.Clear();
        }
        void OnApplicationQuit()
        {
            this._velocityBuffer.OnApplicationQuit();

            if (this._reprojection_buffer != null)
            {
                TaaUtilities.ReleaseRenderTarget(ref this._reprojection_buffer[0, 0]);
                TaaUtilities.ReleaseRenderTarget(ref this._reprojection_buffer[0, 1]);
                TaaUtilities.ReleaseRenderTarget(ref this._reprojection_buffer[1, 0]);
                TaaUtilities.ReleaseRenderTarget(ref this._reprojection_buffer[1, 1]);
            }
        }
Ejemplo n.º 3
0
        void Awake()
        {
            if (!this._material)
            {
                this._material = TaaUtilities.CreateMaterial("PDTAAFork/AdaptiveSharpness");
            }

            if (!this._camera)
            {
                this._camera = this.GetComponent <Camera>();
            }

            this._camera.depthTextureMode |= DepthTextureMode.Depth;
            this._camera.depthTextureMode |= DepthTextureMode.MotionVectors;
        }
        void Resolve(RenderTexture source, RenderTexture destination)
        {
            TaaUtilities.EnsureArray(ref this._reprojection_buffer, 2, 2);
            TaaUtilities.EnsureArray(ref this._reprojection_index, 2, initial_value: -1);

            TaaUtilities.EnsureMaterial(ref this._reprojection_material, this._reprojection_shader);
            if (this._reprojection_material == null)
            {
                Graphics.Blit(source, destination);
                return;
            }

      #if SUPPORT_STEREO
            var eye_index = this._camera.stereoActiveEye == Camera.MonoOrStereoscopicEye.Right ? 1 : 0;
      #else
            int eyeIndex = 0;
      #endif
            var buffer_w = source.width;
            var buffer_h = source.height;

            if (TaaUtilities.EnsureRenderTarget(ref this._reprojection_buffer[eye_index, 0],
                                                buffer_w,
                                                buffer_h,
                                                this.render_texture_format,
                                                this.filtering,
                                                anti_aliasing: source.antiAliasing))
            {
                this.Clear();
            }

            if (TaaUtilities.EnsureRenderTarget(ref this._reprojection_buffer[eye_index, 1],
                                                buffer_w,
                                                buffer_h,
                                                this.render_texture_format,
                                                this.filtering,
                                                anti_aliasing: source.antiAliasing))
            {
                this.Clear();
            }

      #if SUPPORT_STEREO
            var stereo_enabled = this._camera.stereoEnabled;
      #else
            bool stereo_enabled = false;
      #endif
      #if UNITY_EDITOR
            var allow_motion_blur = !stereo_enabled && Application.isPlaying;
      #else
            bool allow_motion_blur = !stereo_enabled;
      #endif

            TaaUtilities.EnsureKeyword(this._reprojection_material,
                                       "CAMERA_PERSPECTIVE",
                                       !this._camera.orthographic);
            TaaUtilities.EnsureKeyword(this._reprojection_material,
                                       "CAMERA_ORTHOGRAPHIC",
                                       this._camera.orthographic);

            TaaUtilities.EnsureKeyword(this._reprojection_material,
                                       "MINMAX_3X3",
                                       this.neighborhood == Neighborhood.Min_max3_x3_);
            TaaUtilities.EnsureKeyword(this._reprojection_material,
                                       "MINMAX_3X3_ROUNDED",
                                       this.neighborhood == Neighborhood.Min_max3_x3_rounded_);
            TaaUtilities.EnsureKeyword(this._reprojection_material,
                                       "MINMAX_3X3_WEIGHTED",
                                       this.neighborhood == Neighborhood.Min_max3_x3_weighted_);
            TaaUtilities.EnsureKeyword(this._reprojection_material,
                                       "MINMAX_4TAP_VARYING",
                                       this.neighborhood == Neighborhood.Min_max4_tap_varying_);
            TaaUtilities.EnsureKeyword(this._reprojection_material,
                                       "INTRPL_NONE",
                                       this._history_interpolation == HistoryInterpolation.Interpolation_none_);
            TaaUtilities.EnsureKeyword(this._reprojection_material,
                                       "INTRPL_CATMULL_ROM",
                                       this._history_interpolation
                                       == HistoryInterpolation.Interpolation_cat_mull_rom_);
            TaaUtilities.EnsureKeyword(this._reprojection_material,
                                       "INTRPL_CATMULL_ROM_CUBIC",
                                       this._history_interpolation
                                       == HistoryInterpolation.Interpolation_cat_mull_rom_cubic_);
            TaaUtilities.EnsureKeyword(this._reprojection_material,
                                       "UNJITTER_COLORSAMPLES",
                                       this.unjitterColorSamples);
            TaaUtilities.EnsureKeyword(this._reprojection_material,
                                       "UNJITTER_NEIGHBORHOOD",
                                       this.unjitterNeighborhood);
            TaaUtilities.EnsureKeyword(this._reprojection_material,
                                       "UNJITTER_REPROJECTION",
                                       this.unjitterReprojection);

            TaaUtilities.EnsureKeyword(this._reprojection_material,
                                       "CLIPPING_NONE",
                                       this._clipping == Clipping.Clipping_none_);
            TaaUtilities.EnsureKeyword(this._reprojection_material,
                                       "CLIPPING_DEFAULT",
                                       this._clipping == Clipping.Clipping_default_);
            TaaUtilities.EnsureKeyword(this._reprojection_material,
                                       "CLIPPING_PHASIC",
                                       this._clipping == Clipping.Clipping_phasic_);
            TaaUtilities.EnsureKeyword(this._reprojection_material,
                                       "CLIPPING_PHASIC_DIRECTIONAL",
                                       this._clipping == Clipping.Clipping_phasic_directional_);
            TaaUtilities.EnsureKeyword(this._reprojection_material,
                                       "CLIPPING_PHASIC_VARIANCE",
                                       this._clipping == Clipping.Clipping_phasic_variance_);

            TaaUtilities.EnsureKeyword(this._reprojection_material, "CLIP_TOWARDS_CENTER", this.clipTowardsCenter);
            TaaUtilities.EnsureKeyword(this._reprojection_material, "VARIANCE_CLIPPING", this.varianceClipping);
            TaaUtilities.EnsureKeyword(this._reprojection_material, "ADAPTIVE_CLIPPING", this.adaptiveClipping);

            TaaUtilities.EnsureKeyword(this._reprojection_material, "USE_YCOCG", this.useYCoCg);
            TaaUtilities.EnsureKeyword(this._reprojection_material,
                                       "YCOCG_SHRINK_CHROMA_MIN_MAX",
                                       this.shrinkChrome);
            TaaUtilities.EnsureKeyword(this._reprojection_material,
                                       "YCOCG_ORIENT_IN_LUMINANCE_AXIS",
                                       this.luminanceOrientBox);

            TaaUtilities.EnsureKeyword(this._reprojection_material, "USE_DILATION", this.useDilation);
            TaaUtilities.EnsureKeyword(this._reprojection_material,
                                       "USE_MOTION_BLUR",
                                       this.useMotionBlur && allow_motion_blur);
            if (this._velocityBuffer != null)
            {
                TaaUtilities.EnsureKeyword(this._reprojection_material,
                                           "USE_MAX_NEIGHBOR_VELOCITY",
                                           this._velocityBuffer.ActiveVelocityNeighborMax != null);

                TaaUtilities.EnsureKeyword(this._reprojection_material, "VELOCITY_DEBUG", this.velocity_debug);
            }

            if (this._reprojection_index[eye_index] == -1) // bootstrap
            {
                this._reprojection_index[eye_index] = 0;
                this._reprojection_buffer[eye_index, this._reprojection_index[eye_index]].DiscardContents();
                Graphics.Blit(source, this._reprojection_buffer[eye_index, this._reprojection_index[eye_index]]);
            }

            var index_read  = this._reprojection_index[eye_index];
            var index_write = (this._reprojection_index[eye_index] + 1) % 2;

            var jitter_uv = this._frustumJitter._ActiveSample;
            jitter_uv.x /= source.width;
            jitter_uv.y /= source.height;
            jitter_uv.z /= source.width;
            jitter_uv.w /= source.height;

            this._reprojection_material.SetVector(_jitter_uv, jitter_uv);
            this._reprojection_material.SetTexture(_velocity_buffer, this._velocityBuffer.ActiveVelocityBuffer);
            this._reprojection_material.SetTexture(_velocity_neighbor_max,
                                                   this._velocityBuffer.ActiveVelocityNeighborMax);
            this._reprojection_material.SetTexture(_main_tex, source);
            this._reprojection_material.SetTexture(_prev_tex, this._reprojection_buffer[eye_index, index_read]);
            this._reprojection_material.SetFloat(_feedback_min, this.feedbackMin);
            this._reprojection_material.SetFloat(_feedback_max, this.feedbackMax);
            this._reprojection_material.SetFloat(_adaptive_clipping_gamma, this.varianceClippingGamma);
            this._reprojection_material.SetFloat(_adaptive_clipping_gamma_min, this.varianceClippingGammaMin);
            this._reprojection_material.SetFloat(_y_co_cg_chroma_shrinkage_factor, this.chromaShrinkageFactor);
            this._reprojection_material.SetFloat(_velocity_weight, this.velocityWeight);
            this._reprojection_material.SetFloat(_motion_scale,
                                                 this.motionBlurStrength
                                                 * (this.motionBlurIgnoreFF
                                                  ? Mathf.Min(1.0f, 1.0f / this._velocityBuffer.TimeScale)
                                                  : 1.0f));
            this._reprojection_material.SetFloat(_clipping_phase_in_factor, this.clippingPhaseInFactor);

            // reproject frame n-1 into output + history buffer
            _mrt[0] = this._reprojection_buffer[eye_index, index_write].colorBuffer;
            _mrt[1] = destination.colorBuffer;

            Graphics.SetRenderTarget(_mrt, source.depthBuffer);
            this._reprojection_material.SetPass(0);
            this._reprojection_buffer[eye_index, index_write].DiscardContents();

            TaaUtilities.DrawFullscreenQuad();

            this._reprojection_index[eye_index] = index_write;
        }
Ejemplo n.º 5
0
        void Resolve(RenderTexture source, RenderTexture destination)
        {
            TaaUtilities.EnsureArray(ref this._reprojectionBuffer, 2, 2);
            TaaUtilities.EnsureArray(ref this._reprojectionIndex, 2, initial_value: -1);

            TaaUtilities.EnsureMaterial(ref this.reprojectionMaterial, this._reprojectionShader);
            if (this.reprojectionMaterial == null)
            {
                Graphics.Blit(source, destination);
                return;
            }

#if SUPPORT_STEREO
            var eyeIndex = this._camera.stereoActiveEye == Camera.MonoOrStereoscopicEye.Right ? 1 : 0;
#else
            int eyeIndex = 0;
#endif
            var bufferW = source.width;
            var bufferH = source.height;

            if (TaaUtilities.EnsureRenderTarget(ref this._reprojectionBuffer[eyeIndex, 0],
                                                bufferW,
                                                bufferH,
                                                this.renderTextureFormat,
                                                this.filtering,
                                                anti_aliasing: source.antiAliasing))
            {
                this.Clear();
            }

            if (TaaUtilities.EnsureRenderTarget(ref this._reprojectionBuffer[eyeIndex, 1],
                                                bufferW,
                                                bufferH,
                                                this.renderTextureFormat,
                                                this.filtering,
                                                anti_aliasing: source.antiAliasing))
            {
                this.Clear();
            }

#if SUPPORT_STEREO
            var stereoEnabled = this._camera.stereoEnabled;
#else
            bool stereoEnabled = false;
#endif
#if UNITY_EDITOR
            var allowMotionBlur = !stereoEnabled && Application.isPlaying;
#else
            bool allowMotionBlur = !stereoEnabled;
#endif

            TaaUtilities.EnsureKeyword(this.reprojectionMaterial,
                                       "CAMERA_PERSPECTIVE",
                                       !this._camera.orthographic);
            TaaUtilities.EnsureKeyword(this.reprojectionMaterial,
                                       "CAMERA_ORTHOGRAPHIC",
                                       this._camera.orthographic);

            TaaUtilities.EnsureKeyword(this.reprojectionMaterial,
                                       "MINMAX_3X3",
                                       this.neighborhood == Neighborhood.MinMax3X3);
            TaaUtilities.EnsureKeyword(this.reprojectionMaterial,
                                       "MINMAX_3X3_ROUNDED",
                                       this.neighborhood == Neighborhood.MinMax3X3Rounded);
            TaaUtilities.EnsureKeyword(this.reprojectionMaterial,
                                       "MINMAX_3X3_WEIGHTED",
                                       this.neighborhood == Neighborhood.MinMax3X3Weighted);
            TaaUtilities.EnsureKeyword(this.reprojectionMaterial,
                                       "MINMAX_4TAP_VARYING",
                                       this.neighborhood == Neighborhood.MinMax4TapVarying);
            TaaUtilities.EnsureKeyword(this.reprojectionMaterial,
                                       "INTRPL_NONE",
                                       this.historyInterpolation == HistoryInterpolation.None);
            TaaUtilities.EnsureKeyword(this.reprojectionMaterial,
                                       "INTRPL_CATMULL_ROM",
                                       this.historyInterpolation
                                       == HistoryInterpolation.CatMullRom);
            TaaUtilities.EnsureKeyword(this.reprojectionMaterial,
                                       "INTRPL_CATMULL_ROM_CUBIC",
                                       this.historyInterpolation
                                       == HistoryInterpolation.CatMullRomCubic);
            TaaUtilities.EnsureKeyword(this.reprojectionMaterial,
                                       "UNJITTER_COLORSAMPLES",
                                       this.unjitterColorSamples);
            TaaUtilities.EnsureKeyword(this.reprojectionMaterial,
                                       "UNJITTER_NEIGHBORHOOD",
                                       this.unjitterNeighborhood);
            TaaUtilities.EnsureKeyword(this.reprojectionMaterial,
                                       "UNJITTER_REPROJECTION",
                                       this.unjitterReprojection);

            TaaUtilities.EnsureKeyword(this.reprojectionMaterial,
                                       "CONSTRAINT_NONE",
                                       this._constraint == Constraint.None_);
            TaaUtilities.EnsureKeyword(this.reprojectionMaterial,
                                       "CONSTRAINT_CLAMP",
                                       this._constraint == Constraint.Clamp);
            TaaUtilities.EnsureKeyword(this.reprojectionMaterial,
                                       "CONSTRAINT_CLIP",
                                       this._constraint == Constraint.Clip);
            TaaUtilities.EnsureKeyword(this.reprojectionMaterial,
                                       "PHASIC_CONSTRAINT",
                                       phasicAf);
            TaaUtilities.EnsureKeyword(this.reprojectionMaterial,
                                       "LIMIT_FEEDBACK",
                                       feedbackLimiting);
            TaaUtilities.EnsureKeyword(this.reprojectionMaterial,
                                       "JIMANEZ_ANTI_FLICKER",
                                       jimanezAf);
            TaaUtilities.EnsureKeyword(this.reprojectionMaterial,
                                       "STACHOWIAC_ANTI_FLICKER",
                                       stachowiacAf);

            TaaUtilities.EnsureKeyword(this.reprojectionMaterial, "CLIP_TOWARDS_CENTER", this.clipTowardsCenter);
            TaaUtilities.EnsureKeyword(this.reprojectionMaterial, "VARIANCE_CLIPPING", this.gaussianNeighborhood);
            TaaUtilities.EnsureKeyword(this.reprojectionMaterial, "ADAPTIVE_CLIPPING", this.adaptiveConstraint);

            TaaUtilities.EnsureKeyword(this.reprojectionMaterial, "COLORSPACE_YCBCR", this._constraint_in == ConstraintColorSpace.YCBCR);
            TaaUtilities.EnsureKeyword(this.reprojectionMaterial, "COLORSPACE_YCOCG", this._constraint_in == ConstraintColorSpace.YCOCG);
            TaaUtilities.EnsureKeyword(this.reprojectionMaterial, "COLORSPACE_RGB", this._constraint_in == ConstraintColorSpace.RGB);


            TaaUtilities.EnsureKeyword(this.reprojectionMaterial, "ENCODE_LINEAR", this._color_encoding == ColorEncoding.LINEAR);
            TaaUtilities.EnsureKeyword(this.reprojectionMaterial, "ENCODE_EXP", this._color_encoding == ColorEncoding.EXP);
            TaaUtilities.EnsureKeyword(this.reprojectionMaterial, "ENCODE_SUPER_EXP", this._color_encoding == ColorEncoding.SUPER_EXP);

            TaaUtilities.EnsureKeyword(this.reprojectionMaterial, "RESOLVE_HISTORY", this.resolveHistory);
            TaaUtilities.EnsureKeyword(this.reprojectionMaterial,
                                       "SCALE_AABB_BOX",
                                       this.scaleAabbBox);
            TaaUtilities.EnsureKeyword(this.reprojectionMaterial, "USE_DILATION", this.useDilation);
            TaaUtilities.EnsureKeyword(this.reprojectionMaterial,
                                       "USE_MOTION_BLUR",
                                       this.useMotionBlur && allowMotionBlur);
            if (this.velocityBuffer != null)
            {
                TaaUtilities.EnsureKeyword(this.reprojectionMaterial,
                                           "USE_MAX_NEIGHBOR_VELOCITY",
                                           this.velocityBuffer.ActiveVelocityNeighborMax != null);

                TaaUtilities.EnsureKeyword(this.reprojectionMaterial, "VELOCITY_DEBUG", this.velocityDebug);
            }

            if (this._reprojectionIndex[eyeIndex] == -1)
            {
                // bootstrap
                this._reprojectionIndex[eyeIndex] = 0;
                this._reprojectionBuffer[eyeIndex, this._reprojectionIndex[eyeIndex]].DiscardContents();
                Graphics.Blit(source, this._reprojectionBuffer[eyeIndex, this._reprojectionIndex[eyeIndex]]);
            }

            var indexRead  = this._reprojectionIndex[eyeIndex];
            var indexWrite = (this._reprojectionIndex[eyeIndex] + 1) % 2;

            var jitterUv = this.frustumJitter._ActiveSample;
            jitterUv.x /= source.width;
            jitterUv.y /= source.height;
            jitterUv.z /= source.width;
            jitterUv.w /= source.height;

            this.reprojectionMaterial.SetVector(JitterUv, jitterUv);
            this.reprojectionMaterial.SetTexture(VelocityBuffer, this.velocityBuffer.ActiveVelocityBuffer);
            this.reprojectionMaterial.SetTexture(VelocityNeighborMax,
                                                 this.velocityBuffer.ActiveVelocityNeighborMax);
            this.reprojectionMaterial.SetTexture(MainTex, source);
            this.reprojectionMaterial.SetTexture(PrevTex, this._reprojectionBuffer[eyeIndex, indexRead]);
            this.reprojectionMaterial.SetFloat(FeedbackMin, this.feedbackMin);
            this.reprojectionMaterial.SetFloat(FeedbackMax, this.feedbackMax);
            this.reprojectionMaterial.SetFloat(AdaptiveBoxMax, this.adaptiveBoxMax);
            this.reprojectionMaterial.SetFloat(AdaptiveBoxMin, this.adaptiveBoxMin);
            this.reprojectionMaterial.SetFloat(XBoxScalingFactor, this
                                               .xBoxScale);
            this.reprojectionMaterial.SetFloat(YBoxScalingFactor, this.yBoxScale);
            this.reprojectionMaterial.SetFloat(ZBoxScalingFactor, this.zBoxScale);
            this.reprojectionMaterial.SetFloat(ConstraintVelocityWeight, this.constraintVelocityWeight);
            this.reprojectionMaterial.SetFloat(PhasicVelocityWeight, this.phasicVelocityTerm);
            this.reprojectionMaterial.SetFloat(MotionScale,
                                               this.motionBlurStrength
                                               * (this.motionBlurIgnoreFf
                    ? Mathf.Min(1.0f, 1.0f / this.velocityBuffer.TimeScale)
                    : 1.0f));
            this.reprojectionMaterial.SetFloat(ClippingPhaseInFactor, this.clippingPhaseInFactor);
            this.reprojectionMaterial.SetFloat(ClippingPhaseOutFactor, this.clippingPhaseOutFactor);
            this.reprojectionMaterial.SetFloat(JimanezAfContrastMin, this.jimanezAfContrastMin);
            this.reprojectionMaterial.SetFloat(JimanezAfContrastMax, this.jimanezAfContrastMax);
            this.reprojectionMaterial.SetFloat(StachowiacAfContrastMin, this.stachowiacAfContrastMin);
            this.reprojectionMaterial.SetFloat(StachowiacAfContrastMax, this.stachowiacAfContrastMax);

            // reproject frame n-1 into output + history buffer
            _mrt[0] = this._reprojectionBuffer[eyeIndex, indexWrite].colorBuffer;
            _mrt[1] = destination.colorBuffer;

            Graphics.SetRenderTarget(_mrt, source.depthBuffer);
            this.reprojectionMaterial.SetPass(0);
            this._reprojectionBuffer[eyeIndex, indexWrite].DiscardContents();

            TaaUtilities.DrawFullscreenQuad();

            this._reprojectionIndex[eyeIndex] = indexWrite;
        }