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