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))
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)
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
363#ifndef SMAA_THRESHOLD
364# define SMAA_THRESHOLD 0.1f
372#ifndef SMAA_DEPTH_THRESHOLD
373# define SMAA_DEPTH_THRESHOLD (0.1f * SMAA_THRESHOLD)
386#ifndef SMAA_MAX_SEARCH_STEPS
387# define SMAA_MAX_SEARCH_STEPS 16
402#ifndef SMAA_MAX_SEARCH_STEPS_DIAG
403# define SMAA_MAX_SEARCH_STEPS_DIAG 8
413#ifndef SMAA_CORNER_ROUNDING
414# define SMAA_CORNER_ROUNDING 25
425#ifndef SMAA_LOCAL_CONTRAST_ADAPTATION_FACTOR
426# define SMAA_LOCAL_CONTRAST_ADAPTATION_FACTOR 2.0f
443#ifndef SMAA_PREDICATION
444# define SMAA_PREDICATION 0
453#ifndef SMAA_PREDICATION_THRESHOLD
454# define SMAA_PREDICATION_THRESHOLD 0.01f
463#ifndef SMAA_PREDICATION_SCALE
464# define SMAA_PREDICATION_SCALE 2.0f
472#ifndef SMAA_PREDICATION_STRENGTH
473# define SMAA_PREDICATION_STRENGTH 0.4f
487#ifndef SMAA_REPROJECTION
488# define SMAA_REPROJECTION 0
502#ifndef SMAA_REPROJECTION_WEIGHT_SCALE
503# define SMAA_REPROJECTION_WEIGHT_SCALE 30.0f
510#ifndef SMAA_INCLUDE_VS
511# define SMAA_INCLUDE_VS 1
513#ifndef SMAA_INCLUDE_PS
514# define SMAA_INCLUDE_PS 1
520#ifndef SMAA_AREATEX_SELECT
521# if defined(SMAA_HLSL_3)
522# define SMAA_AREATEX_SELECT(sample) sample.ra
524# define SMAA_AREATEX_SELECT(sample) sample.rg
528#ifndef SMAA_SEARCHTEX_SELECT
529# define SMAA_SEARCHTEX_SELECT(sample) sample.r
532#ifndef SMAA_DECODE_VELOCITY
533# define SMAA_DECODE_VELOCITY(sample) sample.rg
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)
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))
556# define SMAASampleLevelZeroOffset(tex, coord, offset) tex2Dlod(tex, float4(coord + offset * SMAA_RT_METRICS.xy, 0.0, 0.0))
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]
564#if defined(SMAA_HLSL_4) || defined(SMAA_HLSL_4_1)
565SamplerState LinearSampler
567 Filter = MIN_MAG_LINEAR_MIP_POINT;
571SamplerState PointSampler
573 Filter = MIN_MAG_MIP_POINT;
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)
582# define SMAASampleLevelZeroOffset(tex, coord, offset) tex.SampleLevel(LinearSampler, coord, 0, offset)
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)
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)
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)
611# if defined(SMAA_GLSL_4)
612# define mad(a, b, c) fma(a, b, c)
613# elif defined(GPU_VULKAN)
619vec3
mad(vec3 a, vec3
b, vec3 c)
623vec2
mad(vec2 a, vec2
b, vec2 c)
627float mad(
float a,
float b,
float c)
632# define mad(a, b, c) (a * b + c)
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
681 offset[0] =
float4(texcoord.
xy(), texcoord.
xy()) +
683 offset[1] =
float4(texcoord.
xy(), texcoord.
xy()) +
685 offset[2] =
float4(texcoord.
xy(), texcoord.
xy()) +
700 offset[0] =
float4(texcoord.
xy(), texcoord.
xy()) +
702 offset[1] =
float4(texcoord.
xy(), texcoord.
xy()) +
706 offset[2] =
float4(offset[0].
x, offset[0].
z, offset[1].
y, offset[1].
w) +
732 float edge_threshold,
733 float3 luminance_coefficients,
734 float local_contrast_adaptation_factor)
737 float2 threshold = SMAACalculatePredicatedThreshold(
741 float2 threshold =
float2(edge_threshold, edge_threshold);
755 delta.x = delta_left_top.x;
756 delta.y = delta_left_top.y;
768 delta.z = delta_right_bottom.x;
769 delta.w = delta_right_bottom.y;
778 delta.z = delta_left_left_top_top.x;
779 delta.w = delta_left_left_top_top.y;
783 float finalDelta =
math::max(maxDelta.x, maxDelta.y);
786 edges *=
math::step(finalDelta, local_contrast_adaptation_factor * delta.
xy());
794#if !defined(SMAA_DISABLE_DIAG_DETECTION)
836 coord.x = increment.x;
837 coord.y = increment.y;
838 coord.z = increment.z;
849 coord.x += 0.25f /
size.x;
853 coord.x = increment.x;
854 coord.y = increment.y;
855 coord.z = increment.z;
911 d.x = negative_diagonal.x;
912 d.z = negative_diagonal.y;
913 d.x +=
float(end.y > 0.9f);
921 d.y = positive_diagonal.x;
922 d.w = positive_diagonal.y;
925 if (d.x + d.y > 2.0f) {
937 c.y = decoded_access.x;
938 c.x = decoded_access.y;
939 c.w = decoded_access.z;
940 c.z = decoded_access.w;
963 d.x = negative_diagonal.x;
964 d.z = negative_diagonal.y;
968 d.y = positive_diagonal.x;
969 d.w = positive_diagonal.y;
970 d.y +=
float(end.y > 0.9f);
978 if (d.x + d.y > 2.0f) {
1019 scale +=
float2(-1.0f, 1.0f);
1020 bias +=
float2(0.5f, -0.5f);
1045 while (texcoord.x > end &&
e.y > 0.8281f &&
1054 return texcoord.x + offset /
size.x;
1074 while (texcoord.x < end &&
e.y > 0.8281f &&
1082 return texcoord.x - offset /
size.x;
1089 while (texcoord.y > end &&
e.x > 0.8281f &&
1096 float offset =
mad(-(255.0f / 127.0f),
1099 return texcoord.y + offset /
size.y;
1106 while (texcoord.y < end &&
e.x > 0.8281f &&
1113 float offset =
mad(-(255.0f / 127.0f),
1116 return texcoord.y - offset /
size.y;
1148 int corner_rounding)
1150#if !defined(SMAA_DISABLE_CORNER_DETECTION)
1152 float2 rounding = (1.0f - corner_rounding / 100.0f) * leftRight;
1154 rounding /= leftRight.x + leftRight.y;
1171 int corner_rounding)
1173#if !defined(SMAA_DISABLE_CORNER_DETECTION)
1175 float2 rounding = (1.0f - corner_rounding / 100.0f) * leftRight;
1177 rounding /= leftRight.x + leftRight.y;
1200 int corner_rounding)
1209#if !defined(SMAA_DISABLE_DIAG_DETECTION)
1219 weights.x = diagonal_weights.x;
1220 weights.y = diagonal_weights.y;
1225 if (weights.x == -weights.y) {
1271 coords.y = texcoord.y;
1273 float2 corner_weight = weights.
xy();
1276 float4(coords.
xy(), coords.z, coords.y),
1280 weights.x = corner_weight.x;
1281 weights.y = corner_weight.y;
1283#if !defined(SMAA_DISABLE_DIAG_DETECTION)
1302 coords.x = offset[0].x;
1331 coords.x = texcoord.x;
1333 float2 corner_weight = weights.
zw();
1336 float4(coords.
xy(), coords.x, coords.z),
1340 weights.z = corner_weight.x;
1341 weights.w = corner_weight.y;
1371#if SMAA_REPROJECTION
1397#if SMAA_REPROJECTION
1399 float2 velocity = blendingWeight.x *
1401 velocity += blendingWeight.y *
1415 float3 luminance_coefficients;
1417 return luminance_coefficients;
1420 return float3(1.0f, 0.0f, 0.0f);
1431 const float threshold,
1432 const float local_contrast_adaptation_factor)
1434 gpu::Shader *shader = context.get_shader(
"compositor_smaa_edge_detection");
1441 shader,
"smaa_local_contrast_adaptation_factor", local_contrast_adaptation_factor);
1444 input.bind_as_texture(shader,
"input_tx");
1453 input.unbind_as_texture();
1461 const float threshold,
1462 const float local_contrast_adaptation_factor)
1480 luminance_coefficients,
1481 local_contrast_adaptation_factor);
1490 const float threshold,
1491 const float local_contrast_adaptation_factor)
1493 if (context.use_gpu()) {
1502 const int corner_rounding)
1504 gpu::Shader *shader = context.get_shader(
"compositor_smaa_blending_weight_calculation");
1513 context.cache_manager().smaa_precomputed_textures.get(context);
1534 const int corner_rounding)
1537 context.cache_manager().smaa_precomputed_textures.get(context);
1547 float2 pixel_coordinates;
1563 return weights_result;
1568 const int corner_rounding)
1570 if (context.use_gpu()) {
1581 return "compositor_smaa_neighborhood_blending_float4";
1583 return "compositor_smaa_neighborhood_blending_float";
1601 input.bind_as_texture(shader,
"input_tx");
1607 output.bind_as_image(shader,
"output_img");
1612 input.unbind_as_texture();
1614 output.unbind_as_image();
1638 if (context.use_gpu()) {
1649 const float threshold,
1650 const float local_contrast_adaptation_factor,
1651 const int corner_rounding)
1653 if (
input.is_single_value()) {
#define BLI_assert_unreachable()
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_texture_filter_mode(blender::gpu::Texture *texture, bool use_filter)
static double Clamp(const double x, const double min, const double max)
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)
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
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
void bind_as_texture(gpu::Shader *shader, const char *texture_name) const
const Domain & domain() 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(float_to_float2(value.x), vec2(0.0f))") DEFINE_VALUE("CONVERT_EXPRESSION(value)"
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)
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 float4 SMAANeighborhoodBlendingPS(float2 texcoord, float4 offset, SMAATexture2D(colorTex), SMAATexture2D(blendTex), int2 size)
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 float2 SMAALumaEdgeDetectionPS(float2 texcoord, float4 offset[3], SMAATexture2D(colorTex), float edge_threshold, float3 luminance_coefficients, float local_contrast_adaptation_factor)
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 void blend_neighborhood_gpu(Context &context, const Result &input, const Result &weights, Result &output)
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)
VecBase< float, 4 > float4
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
VecBase< float, 3 > float3
#define SMAATexturePass2D(tex)
#define SMAASampleLevelZero(tex, coord)
#define SMAA_AREATEX_MAX_DISTANCE
#define SMAATexture2D(tex)
#define SMAA_AREATEX_MAX_DISTANCE_DIAG
#define SMAASamplePoint(tex, coord)
#define SMAASamplePointOffset(tex, coord, offset, size)
#define SMAA_DECODE_VELOCITY(sample)
#define SMAA_SEARCHTEX_PACKED_SIZE
#define SMAASample(tex, coord)
#define SMAA_SEARCHTEX_SELECT(sample)
#define SMAA_REPROJECTION
#define SMAASampleLevelZeroOffset(tex, coord, offset, size)
#define SMAA_MAX_SEARCH_STEPS
#define SMAA_AREATEX_PIXEL_SIZE
#define SMAA_AREATEX_SELECT(sample)
#define SMAA_MAX_SEARCH_STEPS_DIAG
#define SMAA_SEARCHTEX_SIZE
#define SMAA_AREATEX_SUBTEX_SIZE
VecBase< T, 2 > zw() const
VecBase< T, 2 > xy() const
VecBase< T, 3 > xyz() const