From 6a32f8c055d8cc3e655e2290a2bc88b56c2a81e4 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Thu, 16 Jul 2020 10:30:39 -0700 Subject: [PATCH 1/2] Stop clamping radial gradient t values to [0.0, 1.0]. Closes #393. --- resources/shaders/gl3/d3d9/tile.fs.glsl | 2 +- resources/shaders/gl4/d3d11/tile.cs.glsl | 2 +- resources/shaders/gl4/d3d9/tile.fs.glsl | 2 +- resources/shaders/metal/d3d11/tile.cs.metal | 72 ++++++++++----------- resources/shaders/metal/d3d9/tile.fs.metal | 62 +++++++++--------- shaders/tile_fragment.inc.glsl | 2 +- 6 files changed, 71 insertions(+), 71 deletions(-) diff --git a/resources/shaders/gl3/d3d9/tile.fs.glsl b/resources/shaders/gl3/d3d9/tile.fs.glsl index ea281c03..631fd631 100644 --- a/resources/shaders/gl3/d3d9/tile.fs.glsl +++ b/resources/shaders/gl3/d3d9/tile.fs.glsl @@ -319,7 +319,7 @@ vec4 filterRadialGradient(vec2 colorTexCoord, if(ts . x > ts . y) ts = ts . yx; float t = ts . x >= 0.0 ? ts . x : ts . y; - color = texture(colorTexture, uvOrigin + vec2(clamp(t, 0.0, 1.0), 0.0)); + color = texture(colorTexture, uvOrigin + vec2(t, 0.0)); } return color; diff --git a/resources/shaders/gl4/d3d11/tile.cs.glsl b/resources/shaders/gl4/d3d11/tile.cs.glsl index 0364fb14..4bc2169f 100644 --- a/resources/shaders/gl4/d3d11/tile.cs.glsl +++ b/resources/shaders/gl4/d3d11/tile.cs.glsl @@ -321,7 +321,7 @@ vec4 filterRadialGradient(vec2 colorTexCoord, if(ts . x > ts . y) ts = ts . yx; float t = ts . x >= 0.0 ? ts . x : ts . y; - color = texture(colorTexture, uvOrigin + vec2(clamp(t, 0.0, 1.0), 0.0)); + color = texture(colorTexture, uvOrigin + vec2(t, 0.0)); } return color; diff --git a/resources/shaders/gl4/d3d9/tile.fs.glsl b/resources/shaders/gl4/d3d9/tile.fs.glsl index ea281c03..631fd631 100644 --- a/resources/shaders/gl4/d3d9/tile.fs.glsl +++ b/resources/shaders/gl4/d3d9/tile.fs.glsl @@ -319,7 +319,7 @@ vec4 filterRadialGradient(vec2 colorTexCoord, if(ts . x > ts . y) ts = ts . yx; float t = ts . x >= 0.0 ? ts . x : ts . y; - color = texture(colorTexture, uvOrigin + vec2(clamp(t, 0.0, 1.0), 0.0)); + color = texture(colorTexture, uvOrigin + vec2(t, 0.0)); } return color; diff --git a/resources/shaders/metal/d3d11/tile.cs.metal b/resources/shaders/metal/d3d11/tile.cs.metal index 0467fba4..d355e756 100644 --- a/resources/shaders/metal/d3d11/tile.cs.metal +++ b/resources/shaders/metal/d3d11/tile.cs.metal @@ -18,7 +18,7 @@ struct bTiles constant uint3 gl_WorkGroupSize [[maybe_unused]] = uint3(16u, 4u, 1u); -constant float3 _1082 = {}; +constant float3 _1081 = {}; // Implementation of the GLSL mod() function, which is slightly different than Metal fmod() template @@ -133,7 +133,7 @@ float4 filterRadialGradient(thread const float2& colorTexCoord, thread const tex _595 = ts.y; } float t = _595; - color = colorTexture.sample(colorTextureSmplr, (uvOrigin + float2(fast::clamp(t, 0.0, 1.0), 0.0)), level(0.0)); + color = colorTexture.sample(colorTextureSmplr, (uvOrigin + float2(t, 0.0)), level(0.0)); } return color; } @@ -146,19 +146,19 @@ float4 filterBlur(thread const float2& colorTexCoord, thread const texture2d uDestImage [[texture(0)]], texture2d uTextureMetadata [[texture(1)]], texture2d uColorTexture0 [[texture(2)]], texture2d uMaskTexture0 [[texture(3)]], texture2d uGammaLUT [[texture(4)]], sampler uTextureMetadataSmplr [[sampler(0)]], sampler uColorTexture0Smplr [[sampler(1)]], sampler uMaskTexture0Smplr [[sampler(2)]], sampler uGammaLUTSmplr [[sampler(3)]], uint3 gl_WorkGroupID [[threadgroup_position_in_grid]], uint3 gl_LocalInvocationID [[thread_position_in_threadgroup]]) +kernel void main0(constant int2& uFramebufferTileSize [[buffer(3)]], constant int& uLoadAction [[buffer(4)]], constant int2& uTextureMetadataSize [[buffer(7)]], constant float2& uFramebufferSize [[buffer(0)]], constant float2& uTileSize [[buffer(1)]], constant float4& uClearColor [[buffer(5)]], constant float2& uColorTextureSize0 [[buffer(8)]], constant float2& uMaskTextureSize0 [[buffer(9)]], const device bFirstTileMap& _1509 [[buffer(2)]], const device bTiles& _1602 [[buffer(6)]], texture2d uDestImage [[texture(0)]], texture2d uTextureMetadata [[texture(1)]], texture2d uColorTexture0 [[texture(2)]], texture2d uMaskTexture0 [[texture(3)]], texture2d uGammaLUT [[texture(4)]], sampler uTextureMetadataSmplr [[sampler(0)]], sampler uColorTexture0Smplr [[sampler(1)]], sampler uMaskTexture0Smplr [[sampler(2)]], sampler uGammaLUTSmplr [[sampler(3)]], uint3 gl_WorkGroupID [[threadgroup_position_in_grid]], uint3 gl_LocalInvocationID [[thread_position_in_threadgroup]]) { int2 tileCoord = int2(gl_WorkGroupID.xy); int2 firstTileSubCoord = int2(gl_LocalInvocationID.xy) * int2(1, 4); int2 firstFragCoord = (tileCoord * int2(uTileSize)) + firstTileSubCoord; - int tileIndex = _1510.iFirstTileMap[tileCoord.x + (uFramebufferTileSize.x * tileCoord.y)]; + int tileIndex = _1509.iFirstTileMap[tileCoord.x + (uFramebufferTileSize.x * tileCoord.y)]; if ((tileIndex < 0) && (uLoadAction != 0)) { return; @@ -685,8 +685,8 @@ kernel void main0(constant int2& uFramebufferTileSize [[buffer(3)]], constant in { int2 tileSubCoord = firstTileSubCoord + int2(0, subY_1); float2 fragCoord = float2(firstFragCoord + int2(0, subY_1)) + float2(0.5); - int alphaTileIndex = int(_1603.iTiles[(tileIndex * 4) + 2] << uint(8)) >> 8; - uint tileControlWord = _1603.iTiles[(tileIndex * 4) + 3]; + int alphaTileIndex = int(_1602.iTiles[(tileIndex * 4) + 2] << uint(8)) >> 8; + uint tileControlWord = _1602.iTiles[(tileIndex * 4) + 3]; uint colorEntry = tileControlWord & 65535u; int tileCtrl = int((tileControlWord >> uint(16)) & 255u); if (alphaTileIndex >= 0) @@ -726,7 +726,7 @@ kernel void main0(constant int2& uFramebufferTileSize [[buffer(3)]], constant in float4 srcColor = calculateColor(param_10, uColorTexture0, uColorTexture0Smplr, uMaskTexture0, uMaskTexture0Smplr, uColorTexture0, uColorTexture0Smplr, uGammaLUT, uGammaLUTSmplr, param_11, param_12, param_13, param_14, param_15, param_16, param_17, param_18, param_19, param_20, param_21); destColors[subY_1] = (destColors[subY_1] * (1.0 - srcColor.w)) + srcColor; } - tileIndex = int(_1603.iTiles[(tileIndex * 4) + 0]); + tileIndex = int(_1602.iTiles[(tileIndex * 4) + 0]); } for (int subY_2 = 0; subY_2 < 4; subY_2++) { diff --git a/resources/shaders/metal/d3d9/tile.fs.metal b/resources/shaders/metal/d3d9/tile.fs.metal index 90346ffc..016c4194 100644 --- a/resources/shaders/metal/d3d9/tile.fs.metal +++ b/resources/shaders/metal/d3d9/tile.fs.metal @@ -6,7 +6,7 @@ using namespace metal; -constant float3 _1056 = {}; +constant float3 _1055 = {}; struct main0_out { @@ -85,7 +85,7 @@ float4 filterRadialGradient(thread const float2& colorTexCoord, thread const tex _569 = ts.y; } float t = _569; - color = colorTexture.sample(colorTextureSmplr, (uvOrigin + float2(fast::clamp(t, 0.0, 1.0), 0.0))); + color = colorTexture.sample(colorTextureSmplr, (uvOrigin + float2(t, 0.0))); } return color; } @@ -98,19 +98,19 @@ float4 filterBlur(thread const float2& colorTexCoord, thread const texture2d ts.y) ts = ts.yx; float t = ts.x >= 0.0 ? ts.x : ts.y; - color = texture(colorTexture, uvOrigin + vec2(clamp(t, 0.0, 1.0), 0.0)); + color = texture(colorTexture, uvOrigin + vec2(t, 0.0)); } return color; From 1570c38217055d3a2cbff88e7e3fc727aba78ff7 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Thu, 16 Jul 2020 10:37:31 -0700 Subject: [PATCH 2/2] Clamp to prevent overflow when interpolating between two gradient stops. Closes #397. --- content/src/gradient.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/content/src/gradient.rs b/content/src/gradient.rs index e475a16c..828f7b86 100644 --- a/content/src/gradient.rs +++ b/content/src/gradient.rs @@ -166,10 +166,8 @@ impl Gradient { return lower_stop.color; } - lower_stop.color - .to_f32() - .lerp(upper_stop.color.to_f32(), (t - lower_stop.offset) / denom) - .to_u8() + let ratio = ((t - lower_stop.offset) / denom).min(1.0); + lower_stop.color.to_f32().lerp(upper_stop.color.to_f32(), ratio).to_u8() } #[inline]