From f55b54f732eaa87d1311eae23a4e27c40386fd60 Mon Sep 17 00:00:00 2001 From: jonahwilliams Date: Tue, 28 Mar 2023 16:25:56 -0700 Subject: [PATCH 1/3] [Impeller] migrate texture fill shaders to half precision --- .../compiler/shader_lib/impeller/texture.glsl | 21 +++++++++++++++++++ impeller/entity/BUILD.gn | 2 ++ impeller/entity/shaders/texture_fill.frag | 10 ++++----- impeller/entity/shaders/texture_fill.vert | 6 +++--- .../entity/shaders/tiled_texture_fill.frag | 12 +++++------ .../entity/shaders/tiled_texture_fill.vert | 6 +++--- 6 files changed, 40 insertions(+), 17 deletions(-) diff --git a/impeller/compiler/shader_lib/impeller/texture.glsl b/impeller/compiler/shader_lib/impeller/texture.glsl index 7979e897ea922..6a4d5127dbb45 100644 --- a/impeller/compiler/shader_lib/impeller/texture.glsl +++ b/impeller/compiler/shader_lib/impeller/texture.glsl @@ -6,6 +6,7 @@ #define TEXTURE_GLSL_ #include +#include /// Sample from a texture. /// @@ -92,6 +93,26 @@ vec4 IPSampleWithTileMode(sampler2D tex, return texture(tex, coords); } +const float16_t kTileModeDecalHf = 3.0hf; + +/// Sample a texture, emulating a specific tile mode. +/// +/// This is useful for Impeller graphics backend that don't have native support +/// for Decal. +f16vec4 IPSampleWithTileMode(f16sampler2D tex, + f16vec2 coords, + float16_t x_tile_mode, + float16_t y_tile_mode) { + if (x_tile_mode == kTileModeDecalHf && + (coords.x < 0.0hf || coords.x >= 1.0hf) || + y_tile_mode == kTileModeDecalHf && + (coords.y < 0.0hf || coords.y >= 1.0hf)) { + return f16vec4(0.0hf); + } + + return texture(tex, coords); +} + /// Sample a texture, emulating a specific tile mode. /// /// This is useful for Impeller graphics backend that don't have native support diff --git a/impeller/entity/BUILD.gn b/impeller/entity/BUILD.gn index 9689ac979e85d..844adbe867577 100644 --- a/impeller/entity/BUILD.gn +++ b/impeller/entity/BUILD.gn @@ -11,6 +11,8 @@ impeller_shaders("entity_shaders") { vulkan_language_version = 130 } + use_half_textures = true + shaders = [ "shaders/blending/advanced_blend.vert", "shaders/blending/advanced_blend_color.frag", diff --git a/impeller/entity/shaders/texture_fill.frag b/impeller/entity/shaders/texture_fill.frag index 78cb9d90b297e..85aae42445bc9 100644 --- a/impeller/entity/shaders/texture_fill.frag +++ b/impeller/entity/shaders/texture_fill.frag @@ -4,18 +4,18 @@ #include -uniform sampler2D texture_sampler; +uniform f16sampler2D texture_sampler; uniform FragInfo { - float alpha; + float16_t alpha; } frag_info; -in vec2 v_texture_coords; +in f16vec2 v_texture_coords; -out vec4 frag_color; +out f16vec4 frag_color; void main() { - vec4 sampled = texture(texture_sampler, v_texture_coords); + f16vec4 sampled = texture(texture_sampler, v_texture_coords); frag_color = sampled * frag_info.alpha; } diff --git a/impeller/entity/shaders/texture_fill.vert b/impeller/entity/shaders/texture_fill.vert index e0f2c7882936a..b6af412649d47 100644 --- a/impeller/entity/shaders/texture_fill.vert +++ b/impeller/entity/shaders/texture_fill.vert @@ -14,10 +14,10 @@ frame_info; in vec2 position; in vec2 texture_coords; -out vec2 v_texture_coords; +out f16vec2 v_texture_coords; void main() { gl_Position = frame_info.mvp * vec4(position, 0.0, 1.0); - v_texture_coords = - IPRemapCoords(texture_coords, frame_info.texture_sampler_y_coord_scale); + v_texture_coords = f16vec2( + IPRemapCoords(texture_coords, frame_info.texture_sampler_y_coord_scale)); } diff --git a/impeller/entity/shaders/tiled_texture_fill.frag b/impeller/entity/shaders/tiled_texture_fill.frag index 030a94ba09aba..7b864b88fa1e0 100644 --- a/impeller/entity/shaders/tiled_texture_fill.frag +++ b/impeller/entity/shaders/tiled_texture_fill.frag @@ -5,18 +5,18 @@ #include #include -uniform sampler2D texture_sampler; +uniform f16sampler2D texture_sampler; uniform FragInfo { - float x_tile_mode; - float y_tile_mode; - float alpha; + float16_t x_tile_mode; + float16_t y_tile_mode; + float16_t alpha; } frag_info; -in vec2 v_texture_coords; +in f16vec2 v_texture_coords; -out vec4 frag_color; +out f16vec4 frag_color; void main() { frag_color = IPSampleWithTileMode(texture_sampler, // sampler diff --git a/impeller/entity/shaders/tiled_texture_fill.vert b/impeller/entity/shaders/tiled_texture_fill.vert index e0f2c7882936a..b6af412649d47 100644 --- a/impeller/entity/shaders/tiled_texture_fill.vert +++ b/impeller/entity/shaders/tiled_texture_fill.vert @@ -14,10 +14,10 @@ frame_info; in vec2 position; in vec2 texture_coords; -out vec2 v_texture_coords; +out f16vec2 v_texture_coords; void main() { gl_Position = frame_info.mvp * vec4(position, 0.0, 1.0); - v_texture_coords = - IPRemapCoords(texture_coords, frame_info.texture_sampler_y_coord_scale); + v_texture_coords = f16vec2( + IPRemapCoords(texture_coords, frame_info.texture_sampler_y_coord_scale)); } From 6a5366c6817938198314e2e86aa842a2ef58e496 Mon Sep 17 00:00:00 2001 From: jonahwilliams Date: Tue, 28 Mar 2023 16:41:04 -0700 Subject: [PATCH 2/3] dont use overloading --- impeller/compiler/shader_lib/impeller/texture.glsl | 8 ++++---- impeller/entity/shaders/tiled_texture_fill.frag | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/impeller/compiler/shader_lib/impeller/texture.glsl b/impeller/compiler/shader_lib/impeller/texture.glsl index 6a4d5127dbb45..4b657f36e95ab 100644 --- a/impeller/compiler/shader_lib/impeller/texture.glsl +++ b/impeller/compiler/shader_lib/impeller/texture.glsl @@ -99,10 +99,10 @@ const float16_t kTileModeDecalHf = 3.0hf; /// /// This is useful for Impeller graphics backend that don't have native support /// for Decal. -f16vec4 IPSampleWithTileMode(f16sampler2D tex, - f16vec2 coords, - float16_t x_tile_mode, - float16_t y_tile_mode) { +f16vec4 IPSampleWithTileModeHf(f16sampler2D tex, + f16vec2 coords, + float16_t x_tile_mode, + float16_t y_tile_mode) { if (x_tile_mode == kTileModeDecalHf && (coords.x < 0.0hf || coords.x >= 1.0hf) || y_tile_mode == kTileModeDecalHf && diff --git a/impeller/entity/shaders/tiled_texture_fill.frag b/impeller/entity/shaders/tiled_texture_fill.frag index 7b864b88fa1e0..26ffae30be6a4 100644 --- a/impeller/entity/shaders/tiled_texture_fill.frag +++ b/impeller/entity/shaders/tiled_texture_fill.frag @@ -19,10 +19,10 @@ in f16vec2 v_texture_coords; out f16vec4 frag_color; void main() { - frag_color = IPSampleWithTileMode(texture_sampler, // sampler - v_texture_coords, // texture coordinates - frag_info.x_tile_mode, // x tile mode - frag_info.y_tile_mode // y tile mode - ) * + frag_color = IPSampleWithTileModeHf(texture_sampler, // sampler + v_texture_coords, // texture coordinates + frag_info.x_tile_mode, // x tile mode + frag_info.y_tile_mode // y tile mode + ) * frag_info.alpha; } From 0a67998c4c35d4f7e6965296770af833eb3a7810 Mon Sep 17 00:00:00 2001 From: jonahwilliams Date: Tue, 28 Mar 2023 17:28:24 -0700 Subject: [PATCH 3/3] rename --- impeller/compiler/shader_lib/impeller/texture.glsl | 8 ++++---- impeller/entity/shaders/tiled_texture_fill.frag | 13 +++++++------ 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/impeller/compiler/shader_lib/impeller/texture.glsl b/impeller/compiler/shader_lib/impeller/texture.glsl index 4b657f36e95ab..3bbb580fb275f 100644 --- a/impeller/compiler/shader_lib/impeller/texture.glsl +++ b/impeller/compiler/shader_lib/impeller/texture.glsl @@ -99,10 +99,10 @@ const float16_t kTileModeDecalHf = 3.0hf; /// /// This is useful for Impeller graphics backend that don't have native support /// for Decal. -f16vec4 IPSampleWithTileModeHf(f16sampler2D tex, - f16vec2 coords, - float16_t x_tile_mode, - float16_t y_tile_mode) { +f16vec4 IPHalfSampleWithTileMode(f16sampler2D tex, + f16vec2 coords, + float16_t x_tile_mode, + float16_t y_tile_mode) { if (x_tile_mode == kTileModeDecalHf && (coords.x < 0.0hf || coords.x >= 1.0hf) || y_tile_mode == kTileModeDecalHf && diff --git a/impeller/entity/shaders/tiled_texture_fill.frag b/impeller/entity/shaders/tiled_texture_fill.frag index 26ffae30be6a4..9d4a916a88fbf 100644 --- a/impeller/entity/shaders/tiled_texture_fill.frag +++ b/impeller/entity/shaders/tiled_texture_fill.frag @@ -19,10 +19,11 @@ in f16vec2 v_texture_coords; out f16vec4 frag_color; void main() { - frag_color = IPSampleWithTileModeHf(texture_sampler, // sampler - v_texture_coords, // texture coordinates - frag_info.x_tile_mode, // x tile mode - frag_info.y_tile_mode // y tile mode - ) * - frag_info.alpha; + frag_color = + IPHalfSampleWithTileMode(texture_sampler, // sampler + v_texture_coords, // texture coordinates + frag_info.x_tile_mode, // x tile mode + frag_info.y_tile_mode // y tile mode + ) * + frag_info.alpha; }