You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GLSL双三次着色器编译报错:int型vec2无法转float型vec2

GLSL双三次着色器编译错误解决方法

编译片段着色器时出现以下错误:

ERROR: 0:44: '=' : cannot convert from 'highp 2-component vector of int' to 'highp 2-component vector of float'

问题出在代码的这一行:

vec2 texSize = textureSize(sampler, vec2(0.0, 0.0)).rg;

错误原因

GLSL中textureSize函数返回的是整数类型二维向量(ivec2),代码直接将其赋值给浮点类型的vec2变量,类型不匹配导致编译失败。另外,textureSize的第二个参数是纹理LOD级别,规范要求传入整数而非浮点向量,原代码的参数使用也不符合要求。

修复方案

将textureSize的返回值显式转换为浮点向量,同时修正LOD参数为整数:

修改后的关键代码行:

vec2 texSize = vec2(textureSize(sampler, 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 = vec2(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);
}

内容的提问来源于stack exchange,提问作者tinoliam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 07:45:14