gpt4 book ai didi

glsl - GLSL 中的高效双三次过滤代码?

转载 作者:行者123 更新时间:2023-12-03 10:10:42 31 4
gpt4 key购买 nike

我想知道是否有人拥有完整、有效且高效的代码来在 glsl 中进行双三次纹理过滤。有这个:

http://www.codeproject.com/Articles/236394/Bi-Cubic-and-Bi-Linear-Interpolation-with-GLSL
或者
https://github.com/visionworkbench/visionworkbench/blob/master/src/vw/GPU/Shaders/Interp/interpolation-bicubic.glsl

但两者都进行 16 次纹理读取,其中只需要 4 次:

https://groups.google.com/forum/#!topic/comp.graphics.api.opengl/kqrujgJfTxo

然而,上面的方法使用了一个缺失的“cubic()”函数,我不知道它应该做什么,并且还需要一个无法解释的“texscale”参数。

还有NVidia版本:

https://developer.nvidia.com/gpugems/gpugems2/part-iii-high-quality-rendering/chapter-20-fast-third-order-texture-filtering

但我相信这使用了 CUDA,它特定于 NVidia 的卡。我需要glsl。

我可能可以将 nvidia 版本移植到 glsl,但我想我会先询问是否有人已经拥有完整的、有效的 glsl 双三次着色器。

最佳答案

我发现这个实现可以用作 texture() 的替代品(来自 http://www.java-gaming.org/index.php?topic=35123.0(修正了一个错字)):

// from http://www.java-gaming.org/index.php?topic=35123.0
vec4 cubic(float v){
vec4 n = vec4(1.0, 2.0, 3.0, 4.0) - v;
vec4 s = n * n * n;
float x = s.x;
float y = s.y - 4.0 * s.x;
float z = s.z - 4.0 * s.y + 6.0 * s.x;
float w = 6.0 - x - y - z;
return vec4(x, y, z, w) * (1.0/6.0);
}

vec4 textureBicubic(sampler2D sampler, vec2 texCoords){

vec2 texSize = textureSize(sampler, 0);
vec2 invTexSize = 1.0 / texSize;

texCoords = texCoords * texSize - 0.5;


vec2 fxy = fract(texCoords);
texCoords -= fxy;

vec4 xcubic = cubic(fxy.x);
vec4 ycubic = cubic(fxy.y);

vec4 c = texCoords.xxyy + vec2 (-0.5, +1.5).xyxy;

vec4 s = vec4(xcubic.xz + xcubic.yw, ycubic.xz + ycubic.yw);
vec4 offset = c + vec4 (xcubic.yw, ycubic.yw) / s;

offset *= invTexSize.xxyy;

vec4 sample0 = texture(sampler, offset.xz);
vec4 sample1 = texture(sampler, offset.yz);
vec4 sample2 = texture(sampler, offset.xw);
vec4 sample3 = texture(sampler, offset.yw);

float sx = s.x / (s.x + s.y);
float sy = s.z / (s.z + s.w);

return mix(
mix(sample3, sample2, sx), mix(sample1, sample0, sx)
, sy);
}

示例:最近的、双线性的、双三次的:

enter image description here

该图像的 ImageData 是
{{{0.698039, 0.996078, 0.262745}, {0., 0.266667, 1.}, {0.00392157, 
0.25098, 0.996078}, {1., 0.65098, 0.}}, {{0.996078, 0.823529,
0.}, {0.498039, 0., 0.00392157}, {0.831373, 0.00392157,
0.00392157}, {0.956863, 0.972549, 0.00784314}}, {{0.909804,
0.00784314, 0.}, {0.87451, 0.996078, 0.0862745}, {0.196078,
0.992157, 0.760784}, {0.00392157, 0.00392157, 0.498039}}, {{1.,
0.878431, 0.}, {0.588235, 0.00392157, 0.00392157}, {0.00392157,
0.0666667, 0.996078}, {0.996078, 0.517647, 0.}}}

我试图重现这个(许多其他插值技术)

enter image description here

但他们有夹紧填充,而我有重复(包装)边界。因此,它并不完全相同。

看来这个双三次业务不合适 插值 ,即它在定义数据的点不采用原始值。

关于glsl - GLSL 中的高效双三次过滤代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13501081/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com