Blender V5.3
smaa.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2013 Jorge Jimenez <jorge@iryoku.com>
2 * SPDX-FileCopyrightText: 2013 Jose I. Echevarria <joseignacioechevarria@gmail.com>
3 * SPDX-FileCopyrightText: 2013 Belen Masia <bmasia@unizar.es>
4 * SPDX-FileCopyrightText: 2013 Fernando Navarro <fernandn@microsoft.com>
5 * SPDX-FileCopyrightText: 2013 Diego Gutierrez <diegog@unizar.es>
6 * SPDX-FileCopyrightText: 2019-2023 Blender Authors
7 *
8 * SPDX-License-Identifier: MIT AND GPL-2.0-or-later */
9
10#include "BLI_assert.hh"
11#include "BLI_math_vector.hh"
12
14
15#include "GPU_shader.hh"
16#include "GPU_texture.hh"
17
18#include "COM_context.hh"
19#include "COM_result.hh"
20#include "COM_utilities.hh"
21
22#include "COM_algorithm_smaa.hh"
23
25
26namespace blender::compositor {
27
293
294/* ----------------------------------------------------------------------------
295 * Blender's Defines */
296
297#define SMAA_CUSTOM_SL
298#define SMAA_AREATEX_SELECT(sample) sample
299#define SMAA_SEARCHTEX_SELECT(sample) sample
300#define SMAATexture2D(tex) const Result &tex
301#define SMAATexturePass2D(tex) tex
302#define SMAASampleLevelZero(T, tex, coord) tex.sample_bilinear_extended<T>(coord)
303#define SMAASampleLevelZeroPoint(T, tex, coord) tex.sample_bilinear_extended<T>(coord)
304#define SMAASampleLevelZeroOffset(T, tex, coord, offset, size) \
305 tex.sample_bilinear_extended<T>(coord + float2(offset) / float2(size))
306#define SMAASample(T, tex, coord) tex.sample_bilinear_extended<T>(coord)
307#define SMAASamplePoint(T, tex, coord) \
308 tex.sample<T>(coord, Interpolation::Nearest, Extension::Extend, Extension::Extend)
309#define SMAASamplePointOffset(T, tex, coord, offset, size) \
310 tex.sample<T>(coord + float2(offset) / float2(size), \
311 Interpolation::Nearest, \
312 Extension::Extend, \
313 Extension::Extend)
314#define SMAASampleOffset(T, tex, coord, offset, size) \
315 tex.sample_bilinear_extended<T>(coord + float2(offset) / float2(size))
316#define SMAA_FLATTEN
317#define SMAA_BRANCH
318#define lerp(a, b, t) math::interpolate(a, b, t)
319#define saturate(a) math::clamp(a, 0.0f, 1.0f)
320#define mad(a, b, c) (a * b + c)
321
322/* ----------------------------------------------------------------------------
323 * SMAA Presets */
324
329
330#if defined(SMAA_PRESET_LOW)
331# define SMAA_THRESHOLD 0.15f
332# define SMAA_MAX_SEARCH_STEPS 4
333# define SMAA_DISABLE_DIAG_DETECTION
334# define SMAA_DISABLE_CORNER_DETECTION
335#elif defined(SMAA_PRESET_MEDIUM)
336# define SMAA_THRESHOLD 0.1f
337# define SMAA_MAX_SEARCH_STEPS 8
338# define SMAA_DISABLE_DIAG_DETECTION
339# define SMAA_DISABLE_CORNER_DETECTION
340#elif defined(SMAA_PRESET_HIGH)
341# define SMAA_THRESHOLD 0.1f
342# define SMAA_MAX_SEARCH_STEPS 16
343# define SMAA_MAX_SEARCH_STEPS_DIAG 8
344# define SMAA_CORNER_ROUNDING 25
345#elif defined(SMAA_PRESET_ULTRA)
346# define SMAA_THRESHOLD 0.05f
347# define SMAA_MAX_SEARCH_STEPS 32
348# define SMAA_MAX_SEARCH_STEPS_DIAG 16
349# define SMAA_CORNER_ROUNDING 25
350#endif
351
352/* ----------------------------------------------------------------------------
353 * Configurable Defines */
354
367#ifndef SMAA_THRESHOLD
368# define SMAA_THRESHOLD 0.1f
369#endif
370
376#ifndef SMAA_DEPTH_THRESHOLD
377# define SMAA_DEPTH_THRESHOLD (0.1f * SMAA_THRESHOLD)
378#endif
379
390#ifndef SMAA_MAX_SEARCH_STEPS
391# define SMAA_MAX_SEARCH_STEPS 16
392#endif
393
406#ifndef SMAA_MAX_SEARCH_STEPS_DIAG
407# define SMAA_MAX_SEARCH_STEPS_DIAG 8
408#endif
409
417#ifndef SMAA_CORNER_ROUNDING
418# define SMAA_CORNER_ROUNDING 25
419#endif
420
429#ifndef SMAA_LOCAL_CONTRAST_ADAPTATION_FACTOR
430# define SMAA_LOCAL_CONTRAST_ADAPTATION_FACTOR 2.0f
431#endif
432
447#ifndef SMAA_PREDICATION
448# define SMAA_PREDICATION 0
449#endif
450
457#ifndef SMAA_PREDICATION_THRESHOLD
458# define SMAA_PREDICATION_THRESHOLD 0.01f
459#endif
460
467#ifndef SMAA_PREDICATION_SCALE
468# define SMAA_PREDICATION_SCALE 2.0f
469#endif
470
476#ifndef SMAA_PREDICATION_STRENGTH
477# define SMAA_PREDICATION_STRENGTH 0.4f
478#endif
479
491#ifndef SMAA_REPROJECTION
492# define SMAA_REPROJECTION 0
493#endif
494
506#ifndef SMAA_REPROJECTION_WEIGHT_SCALE
507# define SMAA_REPROJECTION_WEIGHT_SCALE 30.0f
508#endif
509
514#ifndef SMAA_INCLUDE_VS
515# define SMAA_INCLUDE_VS 1
516#endif
517#ifndef SMAA_INCLUDE_PS
518# define SMAA_INCLUDE_PS 1
519#endif
520
521/* ----------------------------------------------------------------------------
522 * Texture Access Defines */
523
524#ifndef SMAA_AREATEX_SELECT
525# if defined(SMAA_HLSL_3)
526# define SMAA_AREATEX_SELECT(sample) sample.ra
527# else
528# define SMAA_AREATEX_SELECT(sample) sample.rg
529# endif
530#endif
531
532#ifndef SMAA_SEARCHTEX_SELECT
533# define SMAA_SEARCHTEX_SELECT(sample) sample.r
534#endif
535
536#ifndef SMAA_DECODE_VELOCITY
537# define SMAA_DECODE_VELOCITY(sample) sample.rg
538#endif
539
540/* ----------------------------------------------------------------------------
541 * Non-Configurable Defines */
542
543#define SMAA_AREATEX_MAX_DISTANCE 16
544#define SMAA_AREATEX_MAX_DISTANCE_DIAG 20
545#define SMAA_AREATEX_PIXEL_SIZE (1.0f / float2(160.0f, 560.0f))
546#define SMAA_AREATEX_SUBTEX_SIZE (1.0f / 7.0f)
547#define SMAA_SEARCHTEX_SIZE float2(66.0f, 33.0f)
548#define SMAA_SEARCHTEX_PACKED_SIZE float2(64.0f, 16.0f)
549#define SMAA_CORNER_ROUNDING_NORM (float(SMAA_CORNER_ROUNDING) / 100.0f)
550
551/* ----------------------------------------------------------------------------
552 * Porting Functions */
553
554#if defined(SMAA_HLSL_3)
555# define SMAATexture2D(tex) sampler2D tex
556# define SMAATexturePass2D(tex) tex
557# define SMAASampleLevelZero(tex, coord) tex2Dlod(tex, float4(coord, 0.0, 0.0))
558# define SMAASampleLevelZeroPoint(tex, coord) tex2Dlod(tex, float4(coord, 0.0, 0.0))
559/* clang-format off */
560# define SMAASampleLevelZeroOffset(tex, coord, offset) tex2Dlod(tex, float4(coord + offset * SMAA_RT_METRICS.xy, 0.0, 0.0))
561/* clang-format on */
562# define SMAASample(tex, coord) tex2D(tex, coord)
563# define SMAASamplePoint(tex, coord) tex2D(tex, coord)
564# define SMAASampleOffset(tex, coord, offset) tex2D(tex, coord + offset * SMAA_RT_METRICS.xy)
565# define SMAA_FLATTEN [flatten]
566# define SMAA_BRANCH [branch]
567#endif
568#if defined(SMAA_HLSL_4) || defined(SMAA_HLSL_4_1)
569SamplerState LinearSampler
570{
571 Filter = MIN_MAG_LINEAR_MIP_POINT;
572 AddressU = Clamp;
573 AddressV = Clamp;
574};
575SamplerState PointSampler
576{
577 Filter = MIN_MAG_MIP_POINT;
578 AddressU = Clamp;
579 AddressV = Clamp;
580};
581# define SMAATexture2D(tex) Texture2D tex
582# define SMAATexturePass2D(tex) tex
583# define SMAASampleLevelZero(tex, coord) tex.SampleLevel(LinearSampler, coord, 0)
584# define SMAASampleLevelZeroPoint(tex, coord) tex.SampleLevel(PointSampler, coord, 0)
585/* clang-format off */
586# define SMAASampleLevelZeroOffset(tex, coord, offset) tex.SampleLevel(LinearSampler, coord, 0, offset)
587/* clang-format on */
588# define SMAASample(tex, coord) tex.Sample(LinearSampler, coord)
589# define SMAASamplePoint(tex, coord) tex.Sample(PointSampler, coord)
590# define SMAASampleOffset(tex, coord, offset) tex.Sample(LinearSampler, coord, offset)
591# define SMAA_FLATTEN [flatten]
592# define SMAA_BRANCH [branch]
593# define SMAATexture2DMS2(tex) Texture2DMS<float4, 2> tex
594# define SMAALoad(tex, pos, sample) tex.Load(pos, sample)
595# if defined(SMAA_HLSL_4_1)
596# define SMAAGather(tex, coord) tex.Gather(LinearSampler, coord, 0)
597# endif
598#endif
599#if defined(SMAA_GLSL_3) || defined(SMAA_GLSL_4) || defined(GPU_METAL) || defined(GPU_VULKAN)
600# define SMAATexture2D(tex) sampler2D tex
601# define SMAATexturePass2D(tex) tex
602# define SMAASampleLevelZero(tex, coord) textureLod(tex, coord, 0.0)
603# define SMAASampleLevelZeroPoint(tex, coord) textureLod(tex, coord, 0.0)
604# define SMAASampleLevelZeroOffset(tex, coord, offset) textureLodOffset(tex, coord, 0.0, offset)
605# define SMAASample(tex, coord) texture(tex, coord)
606# define SMAASamplePoint(tex, coord) texture(tex, coord)
607# define SMAASampleOffset(tex, coord, offset) texture(tex, coord, offset)
608# define SMAA_FLATTEN
609# define SMAA_BRANCH
610# define lerp(a, b, t) mix(a, b, t)
611# define saturate(a) clamp(a, 0.0, 1.0)
612# if defined(SMAA_GLSL_4)
613# define SMAAGather(tex, coord) textureGather(tex, coord)
614# endif
615# if defined(SMAA_GLSL_4)
616# define mad(a, b, c) fma(a, b, c)
617# elif defined(GPU_VULKAN)
618/* NOTE(Vulkan) mad macro doesn't work, define each override as work-around. */
619vec4 mad(vec4 a, vec4 b, vec4 c)
620{
621 return fma(a, b, c);
622}
623vec3 mad(vec3 a, vec3 b, vec3 c)
624{
625 return fma(a, b, c);
626}
627vec2 mad(vec2 a, vec2 b, vec2 c)
628{
629 return fma(a, b, c);
630}
631float mad(float a, float b, float c)
632{
633 return fma(a, b, c);
634}
635# else
636# define mad(a, b, c) (a * b + c)
637# endif
638/* NOTE(Metal): Types already natively declared in MSL. */
639# ifndef GPU_METAL
640# define float2 vec2
641# define float3 vec3
642# define float4 vec4
643# define int2 ivec2
644# define int3 ivec3
645# define int4 ivec4
646# define bool2 bvec2
647# define bool3 bvec3
648# define bool4 bvec4
649# endif
650#endif
651
652/* clang-format off */
653#if !defined(SMAA_HLSL_3) && !defined(SMAA_HLSL_4) && !defined(SMAA_HLSL_4_1) && !defined(SMAA_GLSL_3) && !defined(SMAA_GLSL_4) && !defined(SMAA_CUSTOM_SL)
654# error you must define the shading language: SMAA_HLSL_*, SMAA_GLSL_* or SMAA_CUSTOM_SL
655#endif
656/* clang-format on */
657
658/* ----------------------------------------------------------------------------
659 * Misc functions */
660
664static void SMAAMovc(float2 cond, float2 &variable, float2 value)
665{
666 /* Use select function (select(genType A, genType B, genBType cond)). */
667 variable = math::interpolate(variable, value, cond);
668}
669
670static void SMAAMovc(float4 cond, float4 &variable, float4 value)
671{
672 /* Use select function (select(genType A, genType B, genBType cond)). */
673 variable = math::interpolate(variable, value, cond);
674}
675
676#if SMAA_INCLUDE_VS
677/* ----------------------------------------------------------------------------
678 * Vertex Shaders */
679
683static void SMAAEdgeDetectionVS(float2 texcoord, int2 size, float4 offset[3])
684{
685 offset[0] = float4(texcoord.xy(), texcoord.xy()) +
686 float4(-1.0f, 0.0f, 0.0f, -1.0f) / float4(size, size);
687 offset[1] = float4(texcoord.xy(), texcoord.xy()) +
688 float4(1.0f, 0.0f, 0.0f, 1.0f) / float4(size, size);
689 offset[2] = float4(texcoord.xy(), texcoord.xy()) +
690 float4(-2.0f, 0.0f, 0.0f, -2.0f) / float4(size, size);
691}
692
697 int2 size,
698 float2 &pixcoord,
699 float4 offset[3])
700{
701 pixcoord = texcoord * float2(size);
702
703 /* We will use these offsets for the searches later on (see @PSEUDO_GATHER4): */
704 offset[0] = float4(texcoord.xy(), texcoord.xy()) +
705 float4(-0.25f, -0.125f, 1.25f, -0.125f) / float4(size, size);
706 offset[1] = float4(texcoord.xy(), texcoord.xy()) +
707 float4(-0.125f, -0.25f, -0.125f, 1.25f) / float4(size, size);
708
709 /* And these for the searches, they indicate the ends of the loops: */
710 offset[2] = float4(offset[0].x, offset[0].z, offset[1].y, offset[1].w) +
711 (float4(-2.0f, 2.0f, -2.0f, 2.0f) * float(SMAA_MAX_SEARCH_STEPS)) /
712 float4(float2(size.x), float2(size.y));
713}
714
718static void SMAANeighborhoodBlendingVS(float2 texcoord, int2 size, float4 &offset)
719{
720 offset = float4(texcoord, texcoord) + float4(1.0f, 0.0f, 0.0f, 1.0f) / float4(size, size);
721}
722#endif /* SMAA_INCLUDE_VS */
723
730template<typename T>
732 float4 offset[3],
733 SMAATexture2D(colorTex),
735 SMAATexture2D(predicationTex),
736#endif
737 float edge_threshold,
738 float3 luminance_coefficients,
739 float local_contrast_adaptation_factor)
740{
741#if SMAA_PREDICATION
742 float2 threshold = SMAACalculatePredicatedThreshold(
743 texcoord, offset, SMAATexturePass2D(predicationTex));
744#else
745 /* Calculate the threshold: */
746 float2 threshold = float2(edge_threshold, edge_threshold);
747#endif
748
749 /* Calculate lumas: */
750 // float4 weights = float4(0.2126, 0.7152, 0.0722, 0.0);
751 float4 weights = float4(luminance_coefficients, 0.0f);
752 float L = math::dot(float4(SMAASamplePoint(T, colorTex, texcoord)), weights);
753
754 float Lleft = math::dot(float4(SMAASamplePoint(T, colorTex, offset[0].xy())), weights);
755 float Ltop = math::dot(float4(SMAASamplePoint(T, colorTex, offset[0].zw())), weights);
756
757 /* We do the usual threshold: */
758 float4 delta;
759 float2 delta_left_top = math::abs(L - float2(Lleft, Ltop));
760 delta.x = delta_left_top.x;
761 delta.y = delta_left_top.y;
762 float2 edges = math::step(threshold, delta.xy());
763
764 /* Then return early if there is no edge: */
765 if (math::dot(edges, float2(1.0f, 1.0f)) == 0.0f) {
766 return float2(0.0f);
767 }
768
769 /* Calculate right and bottom deltas: */
770 float Lright = math::dot(float4(SMAASamplePoint(T, colorTex, offset[1].xy())), weights);
771 float Lbottom = math::dot(float4(SMAASamplePoint(T, colorTex, offset[1].zw())), weights);
772 float2 delta_right_bottom = math::abs(L - float2(Lright, Lbottom));
773 delta.z = delta_right_bottom.x;
774 delta.w = delta_right_bottom.y;
775
776 /* Calculate the maximum delta in the direct neighborhood: */
777 float2 maxDelta = math::max(delta.xy(), delta.zw());
778
779 /* Calculate left-left and top-top deltas: */
780 float Lleftleft = math::dot(float4(SMAASamplePoint(T, colorTex, offset[2].xy())), weights);
781 float Ltoptop = math::dot(float4(SMAASamplePoint(T, colorTex, offset[2].zw())), weights);
782 float2 delta_left_left_top_top = math::abs(float2(Lleft, Ltop) - float2(Lleftleft, Ltoptop));
783 delta.z = delta_left_left_top_top.x;
784 delta.w = delta_left_left_top_top.y;
785
786 /* Calculate the final maximum delta: */
787 maxDelta = math::max(maxDelta.xy(), delta.zw());
788 float finalDelta = math::max(maxDelta.x, maxDelta.y);
789
790 /* Local contrast adaptation: */
791 edges *= math::step(finalDelta, local_contrast_adaptation_factor * delta.xy());
792
793 return edges;
794}
795
796/* ----------------------------------------------------------------------------
797 * Diagonal Search Functions */
798
799#if !defined(SMAA_DISABLE_DIAG_DETECTION)
800
805{
806 /* Bilinear access for fetching 'e' have a 0.25 offset, and we are
807 * interested in the R and G edges:
808 *
809 * +---G---+-------+
810 * | x o R x |
811 * +-------+-------+
812 *
813 * Then, if one of these edge is enabled:
814 * Red: `(0.75 * X + 0.25 * 1) => 0.25 or 1.0`
815 * Green: `(0.75 * 1 + 0.25 * X) => 0.75 or 1.0`
816 *
817 * This function will unpack the values `(mad + mul + round)`:
818 * wolframalpha.com: `round(x * abs(5 * x - 5 * 0.75))` plot 0 to 1
819 */
820 e.x = e.x * math::abs(5.0f * e.x - 5.0f * 0.75f);
821 return math::round(e);
822}
823
825{
826 e.x = e.x * math::abs(5.0f * e.x - 5.0f * 0.75f);
827 e.z = e.z * math::abs(5.0f * e.z - 5.0f * 0.75f);
828 return math::round(e);
829}
830
835 SMAATexture2D(edgesTex), float2 texcoord, float2 dir, int2 size, float2 &e)
836{
837 float4 coord = float4(texcoord, -1.0f, 1.0f);
838 float3 t = float3(1.0f / float2(size), 1.0f);
839 while (coord.z < float(SMAA_MAX_SEARCH_STEPS_DIAG - 1) && coord.w > 0.9f) {
840 float3 increment = mad(t, float3(dir, 1.0f), coord.xyz());
841 coord.x = increment.x;
842 coord.y = increment.y;
843 coord.z = increment.z;
844 e = SMAASamplePoint(float2, edgesTex, coord.xy());
845 coord.w = math::dot(e, float2(0.5f, 0.5f));
846 }
847 return coord.zw();
848}
849
851 SMAATexture2D(edgesTex), float2 texcoord, float2 dir, int2 size, float2 &e)
852{
853 float4 coord = float4(texcoord, -1.0f, 1.0f);
854 coord.x += 0.25f / size.x; /* See @SearchDiag2Optimization */
855 float3 t = float3(1.0f / float2(size), 1.0f);
856 while (coord.z < float(SMAA_MAX_SEARCH_STEPS_DIAG - 1) && coord.w > 0.9f) {
857 float3 increment = mad(t, float3(dir, 1.0f), coord.xyz());
858 coord.x = increment.x;
859 coord.y = increment.y;
860 coord.z = increment.z;
861
862 /* @SearchDiag2Optimization */
863 /* Fetch both edges at once using bilinear filtering: */
864 e = SMAASampleLevelZero(float2, edgesTex, coord.xy()).xy();
866
867 /* Non-optimized version: */
868 // e.g = SMAASampleLevelZero(float2, edgesTex, coord.xy).g;
869 // e.r = SMAASampleLevelZeroOffset(float2, edgesTex, coord.xy, int2(1, 0), size).r;
870
871 coord.w = math::dot(e, float2(0.5f, 0.5f));
872 }
873 return coord.zw();
874}
875
880static float2 SMAAAreaDiag(SMAATexture2D(areaTex), float2 dist, float2 e, float offset)
881{
882 float2 texcoord = mad(
884
885 /* We do a scale and bias for mapping to texel space: */
886 texcoord = mad(SMAA_AREATEX_PIXEL_SIZE, texcoord, 0.5f * SMAA_AREATEX_PIXEL_SIZE);
887
888 /* Diagonal areas are on the second half of the texture: */
889 texcoord.x += 0.5f;
890
891 /* Move to proper place, according to the sub-pixel offset: */
892 texcoord.y += SMAA_AREATEX_SUBTEX_SIZE * offset;
893
894 /* Do it! */
895 return SMAA_AREATEX_SELECT(SMAASampleLevelZero(float2, areaTex, texcoord));
896}
897
902 SMAATexture2D(areaTex),
903 float2 texcoord,
904 float2 e,
905 float4 subsampleIndices,
906 int2 size)
907{
908 float2 weights = float2(0.0f, 0.0f);
909
910 /* Search for the line ends: */
911 float4 d;
912 float2 end;
913 if (e.x > 0.0f) {
914 float2 negative_diagonal = SMAASearchDiag1(
915 SMAATexturePass2D(edgesTex), texcoord, float2(-1.0f, 1.0f), size, end);
916 d.x = negative_diagonal.x;
917 d.z = negative_diagonal.y;
918 d.x += float(end.y > 0.9f);
919 }
920 else {
921 d.x = 0.0f;
922 d.z = 0.0f;
923 }
924 float2 positive_diagonal = SMAASearchDiag1(
925 SMAATexturePass2D(edgesTex), texcoord, float2(1.0, -1.0), size, end);
926 d.y = positive_diagonal.x;
927 d.w = positive_diagonal.y;
928
930 if (d.x + d.y > 2.0f) { /* `d.x + d.y + 1 > 3`. */
931 /* Fetch the crossing edges: */
932 float4 coords = float4(texcoord, texcoord) +
933 float4(-d.x + 0.25f, d.x, d.y, -d.y - 0.25f) / float4(size, size);
934 float4 c;
935 float2 left_edge =
936 SMAASampleLevelZeroOffset(float2, edgesTex, coords.xy(), int2(-1, 0), size).xy();
937 float2 right_edge =
938 SMAASampleLevelZeroOffset(float2, edgesTex, coords.zw(), int2(1, 0), size).xy();
939 c.x = left_edge.x;
940 c.y = left_edge.y;
941 c.z = right_edge.x;
942 c.w = right_edge.y;
943 float4 decoded_access = SMAADecodeDiagBilinearAccess(c);
944 c.y = decoded_access.x;
945 c.x = decoded_access.y;
946 c.w = decoded_access.z;
947 c.z = decoded_access.w;
948
949 /* Non-optimized version: */
950 // float4 coords = mad(float4(-d.x, d.x, d.y, -d.y), SMAA_RT_METRICS.xyxy, texcoord.xyxy);
951 // float4 c;
952 // c.x = SMAASampleLevelZeroOffset(float2, edgesTex, coords.xy, int2(-1, 0), size).g;
953 // c.y = SMAASampleLevelZeroOffset(float2, edgesTex, coords.xy, int2( 0, 0), size).r;
954 // c.z = SMAASampleLevelZeroOffset(float2, edgesTex, coords.zw, int2( 1, 0), size).g;
955 // c.w = SMAASampleLevelZeroOffset(float2, edgesTex, coords.zw, int2( 1, -1), size).r;
956
957 /* Merge crossing edges at each side into a single value: */
958 float2 cc = mad(float2(2.0f, 2.0f), float2(c.x, c.z), float2(c.y, c.w));
959
960 /* Remove the crossing edge if we didn't found the end of the line: */
961 SMAAMovc(math::step(0.9f, d.zw()), cc, float2(0.0f, 0.0f));
962
963 /* Fetch the areas for this line: */
964 weights += SMAAAreaDiag(SMAATexturePass2D(areaTex), d.xy(), cc, subsampleIndices.z);
965 }
966
967 /* Search for the line ends: */
968 float2 negative_diagonal = SMAASearchDiag2(
969 SMAATexturePass2D(edgesTex), texcoord, float2(-1.0f, -1.0f), size, end);
970 d.x = negative_diagonal.x;
971 d.z = negative_diagonal.y;
972 if (SMAASamplePointOffset(float2, edgesTex, texcoord, int2(1, 0), size).x > 0.0f) {
973 float2 positive_diagonal = SMAASearchDiag2(
974 SMAATexturePass2D(edgesTex), texcoord, float2(1.0f, 1.0f), size, end);
975 d.y = positive_diagonal.x;
976 d.w = positive_diagonal.y;
977 d.y += float(end.y > 0.9f);
978 }
979 else {
980 d.y = 0.0f;
981 d.w = 0.0f;
982 }
983
985 if (d.x + d.y > 2.0f) { /* `d.x + d.y + 1 > 3` */
986 /* Fetch the crossing edges: */
987 float4 coords = float4(texcoord, texcoord) + float4(-d.x, -d.x, d.y, d.y) / float4(size, size);
988 float4 c;
989 c.x = SMAASampleLevelZeroOffset(float2, edgesTex, coords.xy(), int2(-1, 0), size).y;
990 c.y = SMAASampleLevelZeroOffset(float2, edgesTex, coords.xy(), int2(0, -1), size).x;
991 float2 left_edge =
992 SMAASampleLevelZeroOffset(float2, edgesTex, coords.zw(), int2(1, 0), size).xy();
993 c.z = left_edge.y;
994 c.w = left_edge.x;
995 float2 cc = mad(float2(2.0f, 2.0f), float2(c.x, c.z), float2(c.y, c.w));
996
997 /* Remove the crossing edge if we didn't found the end of the line: */
998 SMAAMovc(math::step(0.9f, d.zw()), cc, float2(0.0f, 0.0f));
999
1000 /* Fetch the areas for this line: */
1001 float2 area = SMAAAreaDiag(SMAATexturePass2D(areaTex), d.xy(), cc, subsampleIndices.w).xy();
1002 weights.x += area.y;
1003 weights.y += area.x;
1004 }
1005
1006 return weights;
1007}
1008#endif
1009
1010/* ----------------------------------------------------------------------------
1011 * Horizontal/Vertical Search Functions */
1012
1019static float SMAASearchLength(SMAATexture2D(searchTex), float2 e, float offset)
1020{
1021 /* The texture is flipped vertically, with left and right cases taking half
1022 * of the space horizontally: */
1023 float2 scale = SMAA_SEARCHTEX_SIZE * float2(0.5f, -1.0f);
1024 float2 bias = SMAA_SEARCHTEX_SIZE * float2(offset, 1.0f);
1025
1026 /* Scale and bias to access texel centers: */
1027 scale += float2(-1.0f, 1.0f);
1028 bias += float2(0.5f, -0.5f);
1029
1030 /* Convert from pixel coordinates to texcoords:
1031 * (We use SMAA_SEARCHTEX_PACKED_SIZE because the texture is cropped). */
1033 bias *= 1.0f / SMAA_SEARCHTEX_PACKED_SIZE;
1034
1035 /* Lookup the search texture: */
1036 return SMAA_SEARCHTEX_SELECT(SMAASampleLevelZero(float, searchTex, mad(scale, e, bias)));
1037}
1038
1042static float SMAASearchXLeft(
1043 SMAATexture2D(edgesTex), SMAATexture2D(searchTex), float2 texcoord, float end, int2 size)
1044{
1052 float2 e = float2(0.0f, 1.0f);
1053 while (texcoord.x > end && e.y > 0.8281f && /* Is there some edge not activated? */
1054 e.x == 0.0f) /* Or is there a crossing edge that breaks the line? */
1055 {
1056 e = SMAASampleLevelZero(float2, edgesTex, texcoord).xy();
1057 texcoord = texcoord - float2(2.0f, 0.0f) / float2(size);
1058 }
1059
1060 float offset = mad(
1061 -(255.0f / 127.0f), SMAASearchLength(SMAATexturePass2D(searchTex), e, 0.0f), 3.25f);
1062 return texcoord.x + offset / size.x;
1063
1064 /* Non-optimized version:
1065 * We correct the previous (-0.25, -0.125) offset we applied: */
1066 // texcoord.x += 0.25 * SMAA_RT_METRICS.x;
1067
1068 /* The searches are bias by 1, so adjust the coords accordingly: */
1069 // texcoord.x += SMAA_RT_METRICS.x;
1070
1071 /* Disambiguate the length added by the last step: */
1072 // texcoord.x += 2.0 * SMAA_RT_METRICS.x; /* Undo last step. */
1073 // texcoord.x -= SMAA_RT_METRICS.x * (255.0 / 127.0) *
1074 // SMAASearchLength(SMAATexturePass2D(searchTex), e, 0.0);
1075 // return mad(SMAA_RT_METRICS.x, offset, texcoord.x);
1076}
1077
1078static float SMAASearchXRight(
1079 SMAATexture2D(edgesTex), SMAATexture2D(searchTex), float2 texcoord, float end, int2 size)
1080{
1081 float2 e = float2(0.0f, 1.0f);
1082 while (texcoord.x < end && e.y > 0.8281f && /* Is there some edge not activated? */
1083 e.x == 0.0f) /* Or is there a crossing edge that breaks the line? */
1084 {
1085 e = SMAASampleLevelZero(float2, edgesTex, texcoord).xy();
1086 texcoord = texcoord + float2(2.0f, 0.0f) / float2(size);
1087 }
1088 float offset = mad(
1089 -(255.0f / 127.0f), SMAASearchLength(SMAATexturePass2D(searchTex), e, 0.5f), 3.25f);
1090 return texcoord.x - offset / size.x;
1091}
1092
1093static float SMAASearchYUp(
1094 SMAATexture2D(edgesTex), SMAATexture2D(searchTex), float2 texcoord, float end, int2 size)
1095{
1096 float2 e = float2(1.0f, 0.0f);
1097 while (texcoord.y > end && e.x > 0.8281f && /* Is there some edge not activated? */
1098 e.y == 0.0f) /* Or is there a crossing edge that breaks the line? */
1099 {
1100 e = SMAASampleLevelZero(float2, edgesTex, texcoord).xy();
1101 texcoord = texcoord - float2(0.0f, 2.0f) / float2(size);
1102 }
1103 float2 flipped_edge = float2(e.y, e.x);
1104 float offset = mad(-(255.0f / 127.0f),
1105 SMAASearchLength(SMAATexturePass2D(searchTex), flipped_edge, 0.0f),
1106 3.25f);
1107 return texcoord.y + offset / size.y;
1108}
1109
1110static float SMAASearchYDown(
1111 SMAATexture2D(edgesTex), SMAATexture2D(searchTex), float2 texcoord, float end, int2 size)
1112{
1113 float2 e = float2(1.0f, 0.0f);
1114 while (texcoord.y < end && e.x > 0.8281f && /* Is there some edge not activated? */
1115 e.y == 0.0f) /* Or is there a crossing edge that breaks the line? */
1116 {
1117 e = SMAASampleLevelZero(float2, edgesTex, texcoord).xy();
1118 texcoord = texcoord + float2(0.0f, 2.0f) / float2(size);
1119 }
1120 float2 flipped_edge = float2(e.y, e.x);
1121 float offset = mad(-(255.0f / 127.0f),
1122 SMAASearchLength(SMAATexturePass2D(searchTex), flipped_edge, 0.5f),
1123 3.25f);
1124 return texcoord.y - offset / size.y;
1125}
1126
1131static float2 SMAAArea(SMAATexture2D(areaTex), float2 dist, float e1, float e2, float offset)
1132{
1133 /* Rounding prevents precision errors of bilinear filtering: */
1135 math::round(4.0f * float2(e1, e2)),
1136 dist);
1137
1138 /* We do a scale and bias for mapping to texel space: */
1139 texcoord = mad(SMAA_AREATEX_PIXEL_SIZE, texcoord, 0.5f * SMAA_AREATEX_PIXEL_SIZE);
1140
1141 /* Move to proper place, according to the sub-pixel offset: */
1142 texcoord.y = mad(SMAA_AREATEX_SUBTEX_SIZE, offset, texcoord.y);
1143
1144 /* Do it! */
1145 return SMAA_AREATEX_SELECT(SMAASampleLevelZero(float2, areaTex, texcoord));
1146}
1147
1148/* ----------------------------------------------------------------------------
1149 * Corner Detection Functions */
1150
1152 float2 &weights,
1153 float4 texcoord,
1154 float2 d,
1155 int2 size,
1156 int corner_rounding)
1157{
1158#if !defined(SMAA_DISABLE_CORNER_DETECTION)
1159 float2 leftRight = math::step(d, float2(d.y, d.x));
1160 float2 rounding = (1.0f - corner_rounding / 100.0f) * leftRight;
1161
1162 rounding /= leftRight.x + leftRight.y; /* Reduce blending for pixels in the center of a line. */
1163
1164 float2 factor = float2(1.0f, 1.0f);
1165 factor.x -= rounding.x *
1166 SMAASampleLevelZeroOffset(float2, edgesTex, texcoord.xy(), int2(0, 1), size).x;
1167 factor.x -= rounding.y *
1168 SMAASampleLevelZeroOffset(float2, edgesTex, texcoord.zw(), int2(1, 1), size).x;
1169 factor.y -= rounding.x *
1170 SMAASampleLevelZeroOffset(float2, edgesTex, texcoord.xy(), int2(0, -2), size).x;
1171 factor.y -= rounding.y *
1172 SMAASampleLevelZeroOffset(float2, edgesTex, texcoord.zw(), int2(1, -2), size).x;
1173
1174 weights *= saturate(factor);
1175#endif
1176}
1177
1179 float2 &weights,
1180 float4 texcoord,
1181 float2 d,
1182 int2 size,
1183 int corner_rounding)
1184{
1185#if !defined(SMAA_DISABLE_CORNER_DETECTION)
1186 float2 leftRight = math::step(d, float2(d.y, d.x));
1187 float2 rounding = (1.0f - corner_rounding / 100.0f) * leftRight;
1188
1189 rounding /= leftRight.x + leftRight.y;
1190
1191 float2 factor = float2(1.0f, 1.0f);
1192 factor.x -= rounding.x *
1193 SMAASampleLevelZeroOffset(float2, edgesTex, texcoord.xy(), int2(1, 0), size).y;
1194 factor.x -= rounding.y *
1195 SMAASampleLevelZeroOffset(float2, edgesTex, texcoord.zw(), int2(1, 1), size).y;
1196 factor.y -= rounding.x *
1197 SMAASampleLevelZeroOffset(float2, edgesTex, texcoord.xy(), int2(-2, 0), size).y;
1198 factor.y -= rounding.y *
1199 SMAASampleLevelZeroOffset(float2, edgesTex, texcoord.zw(), int2(-2, 1), size).y;
1200
1201 weights *= saturate(factor);
1202#endif
1203}
1204
1205/* ----------------------------------------------------------------------------
1206 * Blending Weight Calculation Pixel Shader (Second Pass) */
1207
1209 float2 pixcoord,
1210 float4 offset[3],
1211 SMAATexture2D(edgesTex),
1212 SMAATexture2D(areaTex),
1213 SMAATexture2D(searchTex),
1214 float4 subsampleIndices,
1215 int2 size,
1216 int corner_rounding)
1217{
1218 /* Just pass zero for SMAA 1x, see @SUBSAMPLE_INDICES. */
1219 float4 weights = float4(0.0f, 0.0f, 0.0f, 0.0f);
1220
1221 float2 e = SMAASamplePoint(float2, edgesTex, texcoord);
1222
1224 if (e.y > 0.0f) { /* Edge at north. */
1225#if !defined(SMAA_DISABLE_DIAG_DETECTION)
1226 /* Diagonals have both north and west edges, so searching for them in
1227 * one of the boundaries is enough. */
1228 float2 diagonal_weights = SMAACalculateDiagWeights(SMAATexturePass2D(edgesTex),
1229 SMAATexturePass2D(areaTex),
1230 texcoord,
1231 e,
1232 subsampleIndices,
1233 size);
1234
1235 weights.x = diagonal_weights.x;
1236 weights.y = diagonal_weights.y;
1237
1238 /* We give priority to diagonals, so if we find a diagonal we skip
1239 * horizontal/vertical processing. */
1241 if (weights.x == -weights.y) { /* `weights.x + weights.y == 0.0` */
1242#endif
1243
1244 float2 d;
1245
1246 /* Find the distance to the left: */
1247 float3 coords;
1248 coords.x = SMAASearchXLeft(SMAATexturePass2D(edgesTex),
1249 SMAATexturePass2D(searchTex),
1250 offset[0].xy(),
1251 offset[2].x,
1252 size);
1253 coords.y =
1254 offset[1].y; // offset[1].y = texcoord.y - 0.25 * SMAA_RT_METRICS.y (@CROSSING_OFFSET)
1255 d.x = coords.x;
1256
1257 /* Now fetch the left crossing edges, two at a time using bilinear
1258 * filtering. Sampling at -0.25 (see @CROSSING_OFFSET) enables to
1259 * discern what value each edge has: */
1260 float e1 = SMAASampleLevelZero(float2, edgesTex, coords.xy()).x;
1261
1262 /* Find the distance to the right: */
1263 coords.z = SMAASearchXRight(SMAATexturePass2D(edgesTex),
1264 SMAATexturePass2D(searchTex),
1265 offset[0].zw(),
1266 offset[2].y,
1267 size);
1268 d.y = coords.z;
1269
1270 /* We want the distances to be in pixel units (doing this here allows
1271 * better interleaving of arithmetic and memory accesses): */
1272 d = math::abs(math::round(mad(float2(size.x), d, -float2(pixcoord.x))));
1273
1274 /* SMAAArea below needs a sqrt, as the areas texture is compressed quadratically: */
1275 float2 sqrt_d = math::sqrt(d);
1276
1277 /* Fetch the right crossing edges: */
1278 float e2 = SMAASampleLevelZeroOffset(
1279 float2, edgesTex, float2(coords.z, coords.y), int2(1, 0), size)
1280 .x;
1281
1282 /* Ok, we know how this pattern looks like, now it is time for getting the actual area: */
1283 float2 area = SMAAArea(SMAATexturePass2D(areaTex), sqrt_d, e1, e2, subsampleIndices.y);
1284 weights.x = area.x;
1285 weights.y = area.y;
1286
1287 /* Fix corners: */
1288 coords.y = texcoord.y;
1289
1290 float2 corner_weight = weights.xy();
1292 corner_weight,
1293 float4(coords.xy(), coords.z, coords.y),
1294 d,
1295 size,
1296 corner_rounding);
1297 weights.x = corner_weight.x;
1298 weights.y = corner_weight.y;
1299
1300#if !defined(SMAA_DISABLE_DIAG_DETECTION)
1301 }
1302 else {
1303 e.x = 0.0f; /* Skip vertical processing. */
1304 }
1305#endif
1306 }
1307
1309 if (e.x > 0.0f) { /* Edge at west. */
1310 float2 d;
1311
1312 /* Find the distance to the top: */
1313 float3 coords;
1314 coords.y = SMAASearchYUp(SMAATexturePass2D(edgesTex),
1315 SMAATexturePass2D(searchTex),
1316 offset[1].xy(),
1317 offset[2].z,
1318 size);
1319 coords.x = offset[0].x; // offset[1].x = texcoord.x - 0.25 * SMAA_RT_METRICS.x;
1320 d.x = coords.y;
1321
1322 /* Fetch the top crossing edges: */
1323 float e1 = SMAASampleLevelZero(float2, edgesTex, coords.xy()).y;
1324
1325 /* Find the distance to the bottom: */
1326 coords.z = SMAASearchYDown(SMAATexturePass2D(edgesTex),
1327 SMAATexturePass2D(searchTex),
1328 offset[1].zw(),
1329 offset[2].w,
1330 size);
1331 d.y = coords.z;
1332
1333 /* We want the distances to be in pixel units: */
1334 d = math::abs(math::round(mad(float2(size.y), d, -float2(pixcoord.y))));
1335
1336 /* SMAAArea below needs a sqrt, as the areas texture is compressed quadratically: */
1337 float2 sqrt_d = math::sqrt(d);
1338
1339 /* Fetch the bottom crossing edges: */
1340 float e2 = SMAASampleLevelZeroOffset(
1341 float2, edgesTex, float2(coords.x, coords.z), int2(0, 1), size)
1342 .y;
1343
1344 /* Get the area for this direction: */
1345 float2 area = SMAAArea(SMAATexturePass2D(areaTex), sqrt_d, e1, e2, subsampleIndices.x);
1346 weights.z = area.x;
1347 weights.w = area.y;
1348
1349 /* Fix corners: */
1350 coords.x = texcoord.x;
1351
1352 float2 corner_weight = weights.zw();
1354 corner_weight,
1355 float4(coords.xy(), coords.x, coords.z),
1356 d,
1357 size,
1358 corner_rounding);
1359 weights.z = corner_weight.x;
1360 weights.w = corner_weight.y;
1361 }
1362
1363 return weights;
1364}
1365
1366/* ----------------------------------------------------------------------------
1367 * Neighborhood Blending Pixel Shader (Third Pass) */
1368
1369template<typename T>
1371 float4 offset,
1372 SMAATexture2D(colorTex),
1373 SMAATexture2D(blendTex),
1375 SMAATexture2D(velocityTex),
1376#endif
1377 int2 size)
1378{
1379 /* Fetch the blending weights for current pixel: */
1380 float4 a;
1381 a.x = SMAASample(float4, blendTex, offset.xy()).w; // Right
1382 a.y = SMAASample(float4, blendTex, offset.zw()).y; // Top
1383 a.z = SMAASample(float4, blendTex, texcoord).z; // Left
1384 a.w = SMAASample(float4, blendTex, texcoord).x; // Bottom
1385
1386 /* Is there any blending weight with a value greater than 0.0? */
1388 if (math::dot(a, float4(1.0f, 1.0f, 1.0f, 1.0f)) < 1e-5f) {
1389 float4 color = float4(SMAASampleLevelZero(T, colorTex, texcoord));
1390
1391#if SMAA_REPROJECTION
1392 float2 velocity = SMAA_DECODE_VELOCITY(SMAASampleLevelZero(velocityTex, texcoord));
1393
1394 /* Pack velocity into the alpha channel: */
1395 color.a = math::sqrt(5.0f * math::length(velocity));
1396#endif
1397
1398 return color;
1399 }
1400
1401 bool h = math::max(a.x, a.z) > math::max(a.y, a.w); /* `max(horizontal) > max(vertical)`. */
1402
1403 /* Calculate the blending offsets: */
1404 float4 blendingOffset = float4(0.0f, a.y, 0.0f, a.w);
1405 float2 blendingWeight = float2(a.y, a.w);
1406 SMAAMovc(float4(h), blendingOffset, float4(a.x, 0.0f, a.z, 0.0f));
1407 SMAAMovc(float2(h), blendingWeight, float2(a.x, a.z));
1408 blendingWeight /= math::dot(blendingWeight, float2(1.0f, 1.0f));
1409
1410 /* Calculate the texture coordinates: */
1411 float4 blendingCoord = float4(texcoord, texcoord) + blendingOffset / float4(size, -size);
1412
1413 /* We exploit bilinear filtering to mix current pixel with the chosen neighbor: */
1414 float4 color = blendingWeight.x * float4(SMAASampleLevelZero(T, colorTex, blendingCoord.xy()));
1415 color += blendingWeight.y * float4(SMAASampleLevelZero(T, colorTex, blendingCoord.zw()));
1416
1417#if SMAA_REPROJECTION
1418 /* Anti-alias velocity for proper reprojection in a later stage: */
1419 float2 velocity = blendingWeight.x *
1420 SMAA_DECODE_VELOCITY(SMAASampleLevelZero(velocityTex, blendingCoord.xy()));
1421 velocity += blendingWeight.y *
1422 SMAA_DECODE_VELOCITY(SMAASampleLevelZero(velocityTex, blendingCoord.zw()));
1423
1424 /* Pack velocity into the alpha channel: */
1425 color.a = math::sqrt(5.0f * math::length(velocity));
1426#endif
1427
1428 return color;
1429}
1430
1432{
1433 switch (type) {
1434 case ResultType::Color: {
1435 float3 luminance_coefficients;
1436 IMB_colormanagement_get_luminance_coefficients(luminance_coefficients);
1437 return luminance_coefficients;
1438 }
1439 case ResultType::Float:
1440 return float3(1.0f, 0.0f, 0.0f);
1441 default:
1442 break;
1443 }
1444
1446 return float3(0.0f);
1447}
1448
1450 const Result &input,
1451 const float threshold,
1452 const float local_contrast_adaptation_factor)
1453{
1454 gpu::Shader *shader = context.get_shader("compositor_smaa_edge_detection");
1456
1457 const float3 luminance_coefficients = get_luminance_coefficients(input.type());
1458 GPU_shader_uniform_3fv(shader, "luminance_coefficients", luminance_coefficients);
1459 GPU_shader_uniform_1f(shader, "smaa_threshold", threshold);
1461 shader, "smaa_local_contrast_adaptation_factor", local_contrast_adaptation_factor);
1462
1463 GPU_texture_filter_mode(input, true);
1464 input.bind_as_texture(shader, "input_tx");
1465
1466 Result edges = context.create_result(ResultType::Color);
1467 edges.allocate_texture(input.domain());
1468 edges.bind_as_image(shader, "edges_img");
1469
1471
1473 input.unbind_as_texture();
1474 edges.unbind_as_image();
1475
1476 return edges;
1477}
1478
1480 const Result &input,
1481 const float threshold,
1482 const float local_contrast_adaptation_factor)
1483{
1484 const float3 luminance_coefficients = get_luminance_coefficients(input.type());
1485
1486 Result edges = context.create_result(ResultType::Float2);
1487 edges.allocate_texture(input.domain());
1488
1489 const int2 size = input.domain().data_size;
1490 if (input.type() == ResultType::Color) {
1491 parallel_for(size, [&](const int2 texel) {
1492 const float2 coordinates = (float2(texel) + float2(0.5f)) / float2(size);
1493
1494 float4 offset[3];
1495 SMAAEdgeDetectionVS(coordinates, size, offset);
1496
1497 const float2 edge = SMAALumaEdgeDetectionPS<Color>(coordinates,
1498 offset,
1499 input,
1500 threshold,
1501 luminance_coefficients,
1502 local_contrast_adaptation_factor);
1503 edges.store_pixel(texel, edge);
1504 });
1505 }
1506 else {
1507 parallel_for(size, [&](const int2 texel) {
1508 const float2 coordinates = (float2(texel) + float2(0.5f)) / float2(size);
1509
1510 float4 offset[3];
1511 SMAAEdgeDetectionVS(coordinates, size, offset);
1512
1513 const float2 edge = SMAALumaEdgeDetectionPS<float>(coordinates,
1514 offset,
1515 input,
1516 threshold,
1517 luminance_coefficients,
1518 local_contrast_adaptation_factor);
1519 edges.store_pixel(texel, edge);
1520 });
1521 }
1522
1523 return edges;
1524}
1525
1527 const Result &input,
1528 const float threshold,
1529 const float local_contrast_adaptation_factor)
1530{
1531 if (context.use_gpu()) {
1532 return detect_edges_gpu(context, input, threshold, local_contrast_adaptation_factor);
1533 }
1534
1535 return detect_edges_cpu(context, input, threshold, local_contrast_adaptation_factor);
1536}
1537
1539 const Result &edges,
1540 const int corner_rounding)
1541{
1542 gpu::Shader *shader = context.get_shader("compositor_smaa_blending_weight_calculation");
1544
1545 GPU_shader_uniform_1i(shader, "smaa_corner_rounding", corner_rounding);
1546
1547 GPU_texture_filter_mode(edges, true);
1548 edges.bind_as_texture(shader, "edges_tx");
1549
1550 const SMAAPrecomputedTextures &smaa_precomputed_textures =
1551 context.cache_manager().smaa_precomputed_textures.get(context);
1552 smaa_precomputed_textures.bind_area_texture(shader, "area_tx");
1553 smaa_precomputed_textures.bind_search_texture(shader, "search_tx");
1554
1555 Result weights = context.create_result(ResultType::Float4);
1556 weights.allocate_texture(edges.domain());
1557 weights.bind_as_image(shader, "weights_img");
1558
1560
1562 edges.unbind_as_texture();
1563 smaa_precomputed_textures.unbind_area_texture();
1564 smaa_precomputed_textures.unbind_search_texture();
1565 weights.unbind_as_image();
1566
1567 return weights;
1568}
1569
1571 const Result &edges,
1572 const int corner_rounding)
1573{
1574 const SMAAPrecomputedTextures &smaa_precomputed_textures =
1575 context.cache_manager().smaa_precomputed_textures.get(context);
1576
1577 Result weights_result = context.create_result(ResultType::Float4);
1578 weights_result.allocate_texture(edges.domain());
1579
1580 const int2 size = edges.domain().data_size;
1581 parallel_for(size, [&](const int2 texel) {
1582 const float2 coordinates = (float2(texel) + float2(0.5f)) / float2(size);
1583
1584 float4 offset[3];
1585 float2 pixel_coordinates;
1586 SMAABlendingWeightCalculationVS(coordinates, size, pixel_coordinates, offset);
1587
1589 coordinates,
1590 pixel_coordinates,
1591 offset,
1592 edges,
1593 smaa_precomputed_textures.area_texture,
1594 smaa_precomputed_textures.search_texture,
1595 float4(0.0f),
1596 size,
1597 corner_rounding);
1598 weights_result.store_pixel(texel, weights);
1599 });
1600
1601 return weights_result;
1602}
1603
1605 const Result &edges,
1606 const int corner_rounding)
1607{
1608 if (context.use_gpu()) {
1609 return calculate_blending_weights_gpu(context, edges, corner_rounding);
1610 }
1611
1612 return calculate_blending_weights_cpu(context, edges, corner_rounding);
1613}
1614
1615static const char *get_blend_shader_name(ResultType type)
1616{
1617 switch (type) {
1618 case ResultType::Color:
1619 return "compositor_smaa_neighborhood_blending_float4";
1620 case ResultType::Float:
1621 return "compositor_smaa_neighborhood_blending_float";
1622 default:
1623 break;
1624 }
1625
1627 return "";
1628}
1629
1630static void blend_neighborhood_gpu(Context &context,
1631 const Result &input,
1632 const Result &weights,
1633 Result &output)
1634{
1635 gpu::Shader *shader = context.get_shader(get_blend_shader_name(input.type()));
1637
1638 GPU_texture_filter_mode(input, true);
1639 input.bind_as_texture(shader, "input_tx");
1640
1641 GPU_texture_filter_mode(weights, true);
1642 weights.bind_as_texture(shader, "weights_tx");
1643
1644 output.allocate_texture(input.domain());
1645 output.bind_as_image(shader, "output_img");
1646
1648
1650 input.unbind_as_texture();
1651 weights.unbind_as_texture();
1652 output.unbind_as_image();
1653}
1654
1655static void blend_neighborhood_cpu(const Result &input, const Result &weights, Result &output)
1656{
1657 output.allocate_texture(input.domain());
1658
1659 const int2 size = input.domain().data_size;
1660
1661 output.get_cpp_type().to_static_type<float, Color>([&]<typename T>() {
1662 parallel_for(size, [&](const int2 texel) {
1663 const float2 coordinates = (float2(texel) + float2(0.5f)) / float2(size);
1664
1665 float4 offset;
1666 SMAANeighborhoodBlendingVS(coordinates, size, offset);
1667
1669 coordinates, offset, input, weights, size);
1670 if constexpr (std::is_same_v<T, float>) {
1671 output.store_pixel(texel, result.x);
1672 }
1673 else {
1674 output.store_pixel(texel, Color(result));
1675 }
1676 });
1677 });
1678}
1679
1680static void blend_neighborhood(Context &context,
1681 const Result &input,
1682 const Result &weights,
1683 Result &output)
1684{
1685 if (context.use_gpu()) {
1686 blend_neighborhood_gpu(context, input, weights, output);
1687 }
1688 else {
1689 blend_neighborhood_cpu(input, weights, output);
1690 }
1691}
1692
1693void smaa(Context &context,
1694 const Result &input,
1695 Result &output,
1696 const float threshold,
1697 const float local_contrast_adaptation_factor,
1698 const int corner_rounding)
1699{
1700 if (input.is_single_value()) {
1701 output.share_data(input);
1702 return;
1703 }
1704
1705 Result edges = detect_edges(context, input, threshold, local_contrast_adaptation_factor);
1706 Result weights = calculate_blending_weights(context, edges, corner_rounding);
1707 edges.release();
1708 blend_neighborhood(context, input, weights, output);
1709 weights.release();
1710}
1711
1712} // namespace blender::compositor
#define BLI_assert_unreachable()
#define output
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
static btVector3 Clamp(const btVector3 &v, btScalar maxlength)
void store_pixel(const int2 &texel, const T &pixel_value)
void allocate_texture(const Domain domain, const bool from_pool=true, const std::optional< ResultStorageType > storage_type=std::nullopt)
Definition result.cc:554
void unbind_as_texture() const
Definition result.cc:964
static ResultType type(gpu::TextureFormat format)
Definition result.cc:337
void bind_as_texture(gpu::Shader *shader, const char *texture_name) const
Definition result.cc:854
void unbind_as_image() const
Definition result.cc:990
void bind_as_image(gpu::Shader *shader, const char *image_name, bool read=false) const
Definition result.cc:951
void bind_area_texture(gpu::Shader *shader, const char *sampler_name) const
void bind_search_texture(gpu::Shader *shader, const char *sampler_name) const
vec4(1.0f/vec2(textureSize(edges_tx, 0)), vec2(textureSize(edges_tx, 0)))") DEFINE_VALUE("SMAA_RT_METRICS"
#define fma
float3x3 scale(float3x3 mat, float2 scale)
#define T
#define L
static float SMAASearchYUp(SMAATexture2D(edgesTex), SMAATexture2D(searchTex), float2 texcoord, float end, int2 size)
Definition smaa.cc:1093
static void blend_neighborhood_cpu(const Result &input, const Result &weights, Result &output)
Definition smaa.cc:1655
static void SMAANeighborhoodBlendingVS(float2 texcoord, int2 size, float4 &offset)
Definition smaa.cc:718
static float2 SMAASearchDiag2(SMAATexture2D(edgesTex), float2 texcoord, float2 dir, int2 size, float2 &e)
Definition smaa.cc:850
static float2 SMAAAreaDiag(SMAATexture2D(areaTex), float2 dist, float2 e, float offset)
Definition smaa.cc:880
static void SMAADetectHorizontalCornerPattern(SMAATexture2D(edgesTex), float2 &weights, float4 texcoord, float2 d, int2 size, int corner_rounding)
Definition smaa.cc:1151
static float2 SMAAArea(SMAATexture2D(areaTex), float2 dist, float e1, float e2, float offset)
Definition smaa.cc:1131
static const char * get_blend_shader_name(ResultType type)
Definition smaa.cc:1615
static Result calculate_blending_weights(Context &context, const Result &edges, const int corner_rounding)
Definition smaa.cc:1604
void compute_dispatch_threads_at_least(gpu::Shader *shader, int2 threads_range, int2 local_size=int2(16))
Definition utilities.cc:235
static void SMAABlendingWeightCalculationVS(float2 texcoord, int2 size, float2 &pixcoord, float4 offset[3])
Definition smaa.cc:696
static Result detect_edges_gpu(Context &context, const Result &input, const float threshold, const float local_contrast_adaptation_factor)
Definition smaa.cc:1449
static float SMAASearchXRight(SMAATexture2D(edgesTex), SMAATexture2D(searchTex), float2 texcoord, float end, int2 size)
Definition smaa.cc:1078
static float2 SMAASearchDiag1(SMAATexture2D(edgesTex), float2 texcoord, float2 dir, int2 size, float2 &e)
Definition smaa.cc:834
static void blend_neighborhood(Context &context, const Result &input, const Result &weights, Result &output)
Definition smaa.cc:1680
ColorSceneLinear4f< eAlpha::Premultiplied > Color
Definition COM_result.hh:96
static Result calculate_blending_weights_gpu(Context &context, const Result &edges, const int corner_rounding)
Definition smaa.cc:1538
static float2 SMAADecodeDiagBilinearAccess(float2 e)
Definition smaa.cc:804
static float SMAASearchYDown(SMAATexture2D(edgesTex), SMAATexture2D(searchTex), float2 texcoord, float end, int2 size)
Definition smaa.cc:1110
static Result detect_edges(Context &context, const Result &input, const float threshold, const float local_contrast_adaptation_factor)
Definition smaa.cc:1526
static void SMAAMovc(float2 cond, float2 &variable, float2 value)
Definition smaa.cc:664
static Result calculate_blending_weights_cpu(Context &context, const Result &edges, const int corner_rounding)
Definition smaa.cc:1570
static float SMAASearchLength(SMAATexture2D(searchTex), float2 e, float offset)
Definition smaa.cc:1019
static float2 SMAACalculateDiagWeights(SMAATexture2D(edgesTex), SMAATexture2D(areaTex), float2 texcoord, float2 e, float4 subsampleIndices, int2 size)
Definition smaa.cc:901
static float3 get_luminance_coefficients(ResultType type)
Definition smaa.cc:1431
static void SMAAEdgeDetectionVS(float2 texcoord, int2 size, float4 offset[3])
Definition smaa.cc:683
void smaa(Context &context, const Result &input, Result &output, const float threshold=0.1f, const float local_contrast_adaptation_factor=2.0f, const int corner_rounding=25)
Definition smaa.cc:1693
static float SMAASearchXLeft(SMAATexture2D(edgesTex), SMAATexture2D(searchTex), float2 texcoord, float end, int2 size)
Definition smaa.cc:1042
static void SMAADetectVerticalCornerPattern(SMAATexture2D(edgesTex), float2 &weights, float4 texcoord, float2 d, int2 size, int corner_rounding)
Definition smaa.cc:1178
static Result detect_edges_cpu(Context &context, const Result &input, const float threshold, const float local_contrast_adaptation_factor)
Definition smaa.cc:1479
void parallel_for(const int2 range, const Function &function)
static float4 SMAANeighborhoodBlendingPS(float2 texcoord, float4 offset, SMAATexture2D(colorTex), SMAATexture2D(blendTex), int2 size)
Definition smaa.cc:1370
static void blend_neighborhood_gpu(Context &context, const Result &input, const Result &weights, Result &output)
Definition smaa.cc:1630
static float2 SMAALumaEdgeDetectionPS(float2 texcoord, float4 offset[3], SMAATexture2D(colorTex), float edge_threshold, float3 luminance_coefficients, float local_contrast_adaptation_factor)
Definition smaa.cc:731
static float4 SMAABlendingWeightCalculationPS(float2 texcoord, float2 pixcoord, float4 offset[3], SMAATexture2D(edgesTex), SMAATexture2D(areaTex), SMAATexture2D(searchTex), float4 subsampleIndices, int2 size, int corner_rounding)
Definition smaa.cc:1208
T sqrt(const T &a)
T length(const VecBase< T, Size > &a)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
T step(const T &edge, const T &value)
T interpolate(const T &a, const T &b, const FactorT &t)
T max(const T &a, const T &b)
T abs(const T &a)
T round(const T &a)
BLI_INLINE void IMB_colormanagement_get_luminance_coefficients(float r_rgb[3])
void GPU_shader_unbind()
void GPU_shader_uniform_3fv(gpu::Shader *sh, const char *name, const float data[3])
void GPU_texture_filter_mode(gpu::Texture *texture, bool use_filter)
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_shader_uniform_1i(gpu::Shader *sh, const char *name, int value)
void GPU_shader_uniform_1f(gpu::Shader *sh, const char *name, float value)
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define SMAATexturePass2D(tex)
Definition smaa.cc:301
#define SMAA_BRANCH
Definition smaa.cc:317
#define SMAA_AREATEX_MAX_DISTANCE
Definition smaa.cc:543
#define SMAASampleLevelZero(T, tex, coord)
Definition smaa.cc:302
#define SMAASamplePointOffset(T, tex, coord, offset, size)
Definition smaa.cc:309
#define SMAATexture2D(tex)
Definition smaa.cc:300
#define SMAA_AREATEX_MAX_DISTANCE_DIAG
Definition smaa.cc:544
#define SMAA_DECODE_VELOCITY(sample)
Definition smaa.cc:537
#define SMAA_SEARCHTEX_PACKED_SIZE
Definition smaa.cc:548
#define SMAA_SEARCHTEX_SELECT(sample)
Definition smaa.cc:299
#define SMAASamplePoint(T, tex, coord)
Definition smaa.cc:307
#define SMAA_REPROJECTION
Definition smaa.cc:492
#define SMAASample(T, tex, coord)
Definition smaa.cc:306
#define SMAA_MAX_SEARCH_STEPS
Definition smaa.cc:391
#define SMAA_AREATEX_PIXEL_SIZE
Definition smaa.cc:545
#define SMAASampleLevelZeroOffset(T, tex, coord, offset, size)
Definition smaa.cc:304
#define SMAA_AREATEX_SELECT(sample)
Definition smaa.cc:298
#define SMAA_MAX_SEARCH_STEPS_DIAG
Definition smaa.cc:407
#define mad(a, b, c)
Definition smaa.cc:320
#define SMAA_SEARCHTEX_SIZE
Definition smaa.cc:547
#define SMAA_PREDICATION
Definition smaa.cc:448
#define SMAA_AREATEX_SUBTEX_SIZE
Definition smaa.cc:546
#define saturate(a)
Definition smaa.cc:319