Blender V5.3
gpu_shader_bicubic_sampler.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
8
11{
12 float2 f2 = f * f;
13 float2 f3 = f2 * f;
14 /* Optimized formulae for cubic B-Spline coefficients. */
15 w3 = f3 / 6.0f;
16 w0 = -w3 + f2 * 0.5f - f * 0.5f + 1.0f / 6.0f;
17 w1 = f3 * 0.5f - f2 * 1.0f + 2.0f / 3.0f;
18 w2 = 1.0f - w0 - w1 - w3;
19}
20
21/* Samples the given 2D sampler at the given coordinates using Bicubic interpolation. This function
22 * uses an optimized algorithm which assumes a linearly filtered sampler, so the caller needs to
23 * take that into account when setting up the sampler. */
24float4 texture_bicubic(sampler2D sampler_2d, float2 coordinates)
25{
26 float2 texture_size = float2(textureSize(sampler_2d, 0).xy);
27 coordinates.xy *= texture_size;
28
29 float2 w0, w1, w2, w3;
30 float2 texel_center = floor(coordinates.xy - 0.5f) + 0.5f;
31 cubic_bspline_coefficients(coordinates.xy - texel_center, w0, w1, w2, w3);
32
33#if 1 /* Optimized version using 4 filtered taps. */
34 float2 s0 = w0 + w1;
35 float2 s1 = w2 + w3;
36
37 float2 f0 = w1 / (w0 + w1);
38 float2 f1 = w3 / (w2 + w3);
39
40 float4 sampling_coordinates;
41 sampling_coordinates.xy = texel_center - 1.0f + f0;
42 sampling_coordinates.zw = texel_center + 1.0f + f1;
43
44 sampling_coordinates /= texture_size.xyxy;
45
46 float4 sampled_color = textureLod(sampler_2d, sampling_coordinates.xy, 0.0f) * s0.x * s0.y;
47 sampled_color += textureLod(sampler_2d, sampling_coordinates.zy, 0.0f) * s1.x * s0.y;
48 sampled_color += textureLod(sampler_2d, sampling_coordinates.xw, 0.0f) * s0.x * s1.y;
49 sampled_color += textureLod(sampler_2d, sampling_coordinates.zw, 0.0f) * s1.x * s1.y;
50
51 return sampled_color;
52
53#else /* Reference brute-force 16 taps. */
54 float4 color = texelFetch(sampler_2d, int2(texel_center + float2(-1.0f, -1.0f)), 0) * w0.x *
55 w0.y;
56 color += texelFetch(sampler_2d, int2(texel_center + float2(0.0f, -1.0f)), 0) * w1.x * w0.y;
57 color += texelFetch(sampler_2d, int2(texel_center + float2(1.0f, -1.0f)), 0) * w2.x * w0.y;
58 color += texelFetch(sampler_2d, int2(texel_center + float2(2.0f, -1.0f)), 0) * w3.x * w0.y;
59
60 color += texelFetch(sampler_2d, int2(texel_center + float2(-1.0f, 0.0f)), 0) * w0.x * w1.y;
61 color += texelFetch(sampler_2d, int2(texel_center + float2(0.0f, 0.0f)), 0) * w1.x * w1.y;
62 color += texelFetch(sampler_2d, int2(texel_center + float2(1.0f, 0.0f)), 0) * w2.x * w1.y;
63 color += texelFetch(sampler_2d, int2(texel_center + float2(2.0f, 0.0f)), 0) * w3.x * w1.y;
64
65 color += texelFetch(sampler_2d, int2(texel_center + float2(-1.0f, 1.0f)), 0) * w0.x * w2.y;
66 color += texelFetch(sampler_2d, int2(texel_center + float2(0.0f, 1.0f)), 0) * w1.x * w2.y;
67 color += texelFetch(sampler_2d, int2(texel_center + float2(1.0f, 1.0f)), 0) * w2.x * w2.y;
68 color += texelFetch(sampler_2d, int2(texel_center + float2(2.0f, 1.0f)), 0) * w3.x * w2.y;
69
70 color += texelFetch(sampler_2d, int2(texel_center + float2(-1.0f, 2.0f)), 0) * w0.x * w3.y;
71 color += texelFetch(sampler_2d, int2(texel_center + float2(0.0f, 2.0f)), 0) * w1.x * w3.y;
72 color += texelFetch(sampler_2d, int2(texel_center + float2(1.0f, 2.0f)), 0) * w2.x * w3.y;
73 color += texelFetch(sampler_2d, int2(texel_center + float2(2.0f, 2.0f)), 0) * w3.x * w3.y;
74
75 return color;
76#endif
77}
void cubic_bspline_coefficients(float2 f, float2 &w0, float2 &w1, float2 &w2, float2 &w3)
float4 texture_bicubic(sampler2D sampler_2d, float2 coordinates)
#define floor
TEX_TEMPLATE DataVec texelFetch(T, IntCoord, int) RET
TEX_TEMPLATE DataVec textureLod(T, FltCoord, float) RET
SamplerBase< float, 2 > sampler2D
TEX_TEMPLATE SizeVec textureSize(T, int) RET
float x
float y