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, \
314#define SMAASampleOffset(T, tex, coord, offset, size) \
315 tex.sample_bilinear_extended<T>(coord + float2(offset) / float2(size))
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)
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
367#ifndef SMAA_THRESHOLD
368# define SMAA_THRESHOLD 0.1f
376#ifndef SMAA_DEPTH_THRESHOLD
377# define SMAA_DEPTH_THRESHOLD (0.1f * SMAA_THRESHOLD)
390#ifndef SMAA_MAX_SEARCH_STEPS
391# define SMAA_MAX_SEARCH_STEPS 16
406#ifndef SMAA_MAX_SEARCH_STEPS_DIAG
407# define SMAA_MAX_SEARCH_STEPS_DIAG 8
417#ifndef SMAA_CORNER_ROUNDING
418# define SMAA_CORNER_ROUNDING 25
429#ifndef SMAA_LOCAL_CONTRAST_ADAPTATION_FACTOR
430# define SMAA_LOCAL_CONTRAST_ADAPTATION_FACTOR 2.0f
447#ifndef SMAA_PREDICATION
448# define SMAA_PREDICATION 0
457#ifndef SMAA_PREDICATION_THRESHOLD
458# define SMAA_PREDICATION_THRESHOLD 0.01f
467#ifndef SMAA_PREDICATION_SCALE
468# define SMAA_PREDICATION_SCALE 2.0f
476#ifndef SMAA_PREDICATION_STRENGTH
477# define SMAA_PREDICATION_STRENGTH 0.4f
491#ifndef SMAA_REPROJECTION
492# define SMAA_REPROJECTION 0
506#ifndef SMAA_REPROJECTION_WEIGHT_SCALE
507# define SMAA_REPROJECTION_WEIGHT_SCALE 30.0f
514#ifndef SMAA_INCLUDE_VS
515# define SMAA_INCLUDE_VS 1
517#ifndef SMAA_INCLUDE_PS
518# define SMAA_INCLUDE_PS 1
524#ifndef SMAA_AREATEX_SELECT
525# if defined(SMAA_HLSL_3)
526# define SMAA_AREATEX_SELECT(sample) sample.ra
528# define SMAA_AREATEX_SELECT(sample) sample.rg
532#ifndef SMAA_SEARCHTEX_SELECT
533# define SMAA_SEARCHTEX_SELECT(sample) sample.r
536#ifndef SMAA_DECODE_VELOCITY
537# define SMAA_DECODE_VELOCITY(sample) sample.rg
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)
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))
560# define SMAASampleLevelZeroOffset(tex, coord, offset) tex2Dlod(tex, float4(coord + offset * SMAA_RT_METRICS.xy, 0.0, 0.0))
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]
568#if defined(SMAA_HLSL_4) || defined(SMAA_HLSL_4_1)
569SamplerState LinearSampler
571 Filter = MIN_MAG_LINEAR_MIP_POINT;
575SamplerState PointSampler
577 Filter = MIN_MAG_MIP_POINT;
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)
586# define SMAASampleLevelZeroOffset(tex, coord, offset) tex.SampleLevel(LinearSampler, coord, 0, offset)
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)
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)
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)
615# if defined(SMAA_GLSL_4)
616# define mad(a, b, c) fma(a, b, c)
617# elif defined(GPU_VULKAN)
623vec3
mad(vec3 a, vec3
b, vec3 c)
627vec2
mad(vec2 a, vec2
b, vec2 c)
631float mad(
float a,
float b,
float c)
636# define mad(a, b, c) (a * b + c)
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
685 offset[0] =
float4(texcoord.xy(), texcoord.xy()) +
687 offset[1] =
float4(texcoord.xy(), texcoord.xy()) +
689 offset[2] =
float4(texcoord.xy(), texcoord.xy()) +
701 pixcoord = texcoord *
float2(size);
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);
710 offset[2] =
float4(offset[0].
x, offset[0].
z, offset[1].
y, offset[1].
w) +
720 offset =
float4(texcoord, texcoord) +
float4(1.0f, 0.0f, 0.0f, 1.0f) /
float4(size, size);
737 float edge_threshold,
738 float3 luminance_coefficients,
739 float local_contrast_adaptation_factor)
742 float2 threshold = SMAACalculatePredicatedThreshold(
746 float2 threshold =
float2(edge_threshold, edge_threshold);
760 delta.x = delta_left_top.x;
761 delta.y = delta_left_top.y;
773 delta.z = delta_right_bottom.x;
774 delta.w = delta_right_bottom.y;
783 delta.z = delta_left_left_top_top.x;
784 delta.w = delta_left_left_top_top.y;
787 maxDelta =
math::max(maxDelta.xy(), delta.zw());
788 float finalDelta =
math::max(maxDelta.x, maxDelta.y);
791 edges *=
math::step(finalDelta, local_contrast_adaptation_factor * delta.xy());
799#if !defined(SMAA_DISABLE_DIAG_DETECTION)
841 coord.x = increment.x;
842 coord.y = increment.y;
843 coord.z = increment.z;
854 coord.x += 0.25f / size.x;
858 coord.x = increment.x;
859 coord.y = increment.y;
860 coord.z = increment.z;
916 d.x = negative_diagonal.x;
917 d.z = negative_diagonal.y;
918 d.x +=
float(end.y > 0.9f);
926 d.y = positive_diagonal.x;
927 d.w = positive_diagonal.y;
930 if (d.x + d.y > 2.0f) {
933 float4(-d.x + 0.25f, d.x, d.y, -d.y - 0.25f) /
float4(size, size);
944 c.y = decoded_access.x;
945 c.x = decoded_access.y;
946 c.w = decoded_access.z;
947 c.z = decoded_access.w;
970 d.x = negative_diagonal.x;
971 d.z = negative_diagonal.y;
975 d.y = positive_diagonal.x;
976 d.w = positive_diagonal.y;
977 d.y +=
float(end.y > 0.9f);
985 if (d.x + d.y > 2.0f) {
1002 weights.x += area.y;
1003 weights.y += area.x;
1028 bias +=
float2(0.5f, -0.5f);
1053 while (texcoord.x > end &&
e.y > 0.8281f &&
1057 texcoord = texcoord -
float2(2.0f, 0.0f) /
float2(size);
1062 return texcoord.x + offset / size.x;
1082 while (texcoord.x < end &&
e.y > 0.8281f &&
1086 texcoord = texcoord +
float2(2.0f, 0.0f) /
float2(size);
1090 return texcoord.x - offset / size.x;
1097 while (texcoord.y > end &&
e.x > 0.8281f &&
1101 texcoord = texcoord -
float2(0.0f, 2.0f) /
float2(size);
1104 float offset =
mad(-(255.0f / 127.0f),
1107 return texcoord.y + offset / size.y;
1114 while (texcoord.y < end &&
e.x > 0.8281f &&
1118 texcoord = texcoord +
float2(0.0f, 2.0f) /
float2(size);
1121 float offset =
mad(-(255.0f / 127.0f),
1124 return texcoord.y - offset / size.y;
1156 int corner_rounding)
1158#if !defined(SMAA_DISABLE_CORNER_DETECTION)
1160 float2 rounding = (1.0f - corner_rounding / 100.0f) * leftRight;
1162 rounding /= leftRight.x + leftRight.y;
1165 factor.x -= rounding.x *
1167 factor.x -= rounding.y *
1169 factor.y -= rounding.x *
1171 factor.y -= rounding.y *
1183 int corner_rounding)
1185#if !defined(SMAA_DISABLE_CORNER_DETECTION)
1187 float2 rounding = (1.0f - corner_rounding / 100.0f) * leftRight;
1189 rounding /= leftRight.x + leftRight.y;
1192 factor.x -= rounding.x *
1194 factor.x -= rounding.y *
1196 factor.y -= rounding.x *
1198 factor.y -= rounding.y *
1216 int corner_rounding)
1225#if !defined(SMAA_DISABLE_DIAG_DETECTION)
1235 weights.x = diagonal_weights.x;
1236 weights.y = diagonal_weights.y;
1241 if (weights.x == -weights.y) {
1288 coords.y = texcoord.y;
1290 float2 corner_weight = weights.xy();
1293 float4(coords.xy(), coords.z, coords.y),
1297 weights.x = corner_weight.x;
1298 weights.y = corner_weight.y;
1300#if !defined(SMAA_DISABLE_DIAG_DETECTION)
1319 coords.x = offset[0].x;
1350 coords.x = texcoord.x;
1352 float2 corner_weight = weights.zw();
1355 float4(coords.xy(), coords.x, coords.z),
1359 weights.z = corner_weight.x;
1360 weights.w = corner_weight.y;
1391#if SMAA_REPROJECTION
1411 float4 blendingCoord =
float4(texcoord, texcoord) + blendingOffset /
float4(size, -size);
1417#if SMAA_REPROJECTION
1419 float2 velocity = blendingWeight.x *
1421 velocity += blendingWeight.y *
1435 float3 luminance_coefficients;
1437 return luminance_coefficients;
1440 return float3(1.0f, 0.0f, 0.0f);
1451 const float threshold,
1452 const float local_contrast_adaptation_factor)
1461 shader,
"smaa_local_contrast_adaptation_factor", local_contrast_adaptation_factor);
1481 const float threshold,
1482 const float local_contrast_adaptation_factor)
1501 luminance_coefficients,
1502 local_contrast_adaptation_factor);
1517 luminance_coefficients,
1518 local_contrast_adaptation_factor);
1528 const float threshold,
1529 const float local_contrast_adaptation_factor)
1531 if (context.use_gpu()) {
1532 return detect_edges_gpu(context, input, threshold, local_contrast_adaptation_factor);
1535 return detect_edges_cpu(context, input, threshold, local_contrast_adaptation_factor);
1540 const int corner_rounding)
1542 gpu::Shader *
shader = context.get_shader(
"compositor_smaa_blending_weight_calculation");
1551 context.cache_manager().smaa_precomputed_textures.get(context);
1572 const int corner_rounding)
1575 context.cache_manager().smaa_precomputed_textures.get(context);
1585 float2 pixel_coordinates;
1601 return weights_result;
1606 const int corner_rounding)
1608 if (context.use_gpu()) {
1619 return "compositor_smaa_neighborhood_blending_float4";
1621 return "compositor_smaa_neighborhood_blending_float";
1652 output.unbind_as_image();
1669 coordinates, offset, input, weights, size);
1670 if constexpr (std::is_same_v<T, float>) {
1685 if (context.use_gpu()) {
1696 const float threshold,
1697 const float local_contrast_adaptation_factor,
1698 const int corner_rounding)
1701 output.share_data(input);
1705 Result edges =
detect_edges(context, input, threshold, local_contrast_adaptation_factor);
#define BLI_assert_unreachable()
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
static btVector3 Clamp(const btVector3 &v, btScalar maxlength)
bool is_single_value() const
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)
void unbind_as_texture() const
static ResultType type(gpu::TextureFormat format)
void bind_as_texture(gpu::Shader *shader, const char *texture_name) const
void unbind_as_image() const
void bind_as_image(gpu::Shader *shader, const char *image_name, bool read=false) const
void bind_area_texture(gpu::Shader *shader, const char *sampler_name) const
void bind_search_texture(gpu::Shader *shader, const char *sampler_name) const
void unbind_area_texture() const
void unbind_search_texture() const
vec4(1.0f/vec2(textureSize(edges_tx, 0)), vec2(textureSize(edges_tx, 0)))") DEFINE_VALUE("SMAA_RT_METRICS"
float3x3 scale(float3x3 mat, float2 scale)
static float SMAASearchYUp(SMAATexture2D(edgesTex), SMAATexture2D(searchTex), float2 texcoord, float end, int2 size)
static void blend_neighborhood_cpu(const Result &input, const Result &weights, Result &output)
static void SMAANeighborhoodBlendingVS(float2 texcoord, int2 size, float4 &offset)
static float2 SMAASearchDiag2(SMAATexture2D(edgesTex), float2 texcoord, float2 dir, int2 size, float2 &e)
static float2 SMAAAreaDiag(SMAATexture2D(areaTex), float2 dist, float2 e, float offset)
static void SMAADetectHorizontalCornerPattern(SMAATexture2D(edgesTex), float2 &weights, float4 texcoord, float2 d, int2 size, int corner_rounding)
static float2 SMAAArea(SMAATexture2D(areaTex), float2 dist, float e1, float e2, float offset)
static const char * get_blend_shader_name(ResultType type)
static Result calculate_blending_weights(Context &context, const Result &edges, const int corner_rounding)
void compute_dispatch_threads_at_least(gpu::Shader *shader, int2 threads_range, int2 local_size=int2(16))
static void SMAABlendingWeightCalculationVS(float2 texcoord, int2 size, float2 &pixcoord, float4 offset[3])
static Result detect_edges_gpu(Context &context, const Result &input, const float threshold, const float local_contrast_adaptation_factor)
static float SMAASearchXRight(SMAATexture2D(edgesTex), SMAATexture2D(searchTex), float2 texcoord, float end, int2 size)
static float2 SMAASearchDiag1(SMAATexture2D(edgesTex), float2 texcoord, float2 dir, int2 size, float2 &e)
static void blend_neighborhood(Context &context, const Result &input, const Result &weights, Result &output)
ColorSceneLinear4f< eAlpha::Premultiplied > Color
static Result calculate_blending_weights_gpu(Context &context, const Result &edges, const int corner_rounding)
static float2 SMAADecodeDiagBilinearAccess(float2 e)
static float SMAASearchYDown(SMAATexture2D(edgesTex), SMAATexture2D(searchTex), float2 texcoord, float end, int2 size)
static Result detect_edges(Context &context, const Result &input, const float threshold, const float local_contrast_adaptation_factor)
static void SMAAMovc(float2 cond, float2 &variable, float2 value)
static Result calculate_blending_weights_cpu(Context &context, const Result &edges, const int corner_rounding)
static float SMAASearchLength(SMAATexture2D(searchTex), float2 e, float offset)
static float2 SMAACalculateDiagWeights(SMAATexture2D(edgesTex), SMAATexture2D(areaTex), float2 texcoord, float2 e, float4 subsampleIndices, int2 size)
static float3 get_luminance_coefficients(ResultType type)
static void SMAAEdgeDetectionVS(float2 texcoord, int2 size, float4 offset[3])
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)
static float SMAASearchXLeft(SMAATexture2D(edgesTex), SMAATexture2D(searchTex), float2 texcoord, float end, int2 size)
static void SMAADetectVerticalCornerPattern(SMAATexture2D(edgesTex), float2 &weights, float4 texcoord, float2 d, int2 size, int corner_rounding)
static Result detect_edges_cpu(Context &context, const Result &input, const float threshold, const float local_contrast_adaptation_factor)
void parallel_for(const int2 range, const Function &function)
static float4 SMAANeighborhoodBlendingPS(float2 texcoord, float4 offset, SMAATexture2D(colorTex), SMAATexture2D(blendTex), int2 size)
static void blend_neighborhood_gpu(Context &context, const Result &input, const Result &weights, Result &output)
static float2 SMAALumaEdgeDetectionPS(float2 texcoord, float4 offset[3], SMAATexture2D(colorTex), float edge_threshold, float3 luminance_coefficients, float local_contrast_adaptation_factor)
static float4 SMAABlendingWeightCalculationPS(float2 texcoord, float2 pixcoord, float4 offset[3], SMAATexture2D(edgesTex), SMAATexture2D(areaTex), SMAATexture2D(searchTex), float4 subsampleIndices, int2 size, int corner_rounding)
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)
BLI_INLINE void IMB_colormanagement_get_luminance_coefficients(float r_rgb[3])
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)
#define SMAA_AREATEX_MAX_DISTANCE
#define SMAASampleLevelZero(T, tex, coord)
#define SMAASamplePointOffset(T, tex, coord, offset, size)
#define SMAATexture2D(tex)
#define SMAA_AREATEX_MAX_DISTANCE_DIAG
#define SMAA_DECODE_VELOCITY(sample)
#define SMAA_SEARCHTEX_PACKED_SIZE
#define SMAA_SEARCHTEX_SELECT(sample)
#define SMAASamplePoint(T, tex, coord)
#define SMAA_REPROJECTION
#define SMAASample(T, tex, coord)
#define SMAA_MAX_SEARCH_STEPS
#define SMAA_AREATEX_PIXEL_SIZE
#define SMAASampleLevelZeroOffset(T, tex, coord, offset, size)
#define SMAA_AREATEX_SELECT(sample)
#define SMAA_MAX_SEARCH_STEPS_DIAG
#define SMAA_SEARCHTEX_SIZE
#define SMAA_AREATEX_SUBTEX_SIZE