Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

[Impeller] migrate texture fill shaders to half precision. #40735

Merged
merged 4 commits into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions impeller/compiler/shader_lib/impeller/texture.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define TEXTURE_GLSL_

#include <impeller/branching.glsl>
#include <impeller/types.glsl>

/// Sample from a texture.
///
Expand Down Expand Up @@ -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 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 &&
(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
Expand Down
2 changes: 2 additions & 0 deletions impeller/entity/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 5 additions & 5 deletions impeller/entity/shaders/texture_fill.frag
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@

#include <impeller/types.glsl>

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;
}
6 changes: 3 additions & 3 deletions impeller/entity/shaders/texture_fill.vert
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
25 changes: 13 additions & 12 deletions impeller/entity/shaders/tiled_texture_fill.frag
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,25 @@
#include <impeller/texture.glsl>
#include <impeller/types.glsl>

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
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;
}
6 changes: 3 additions & 3 deletions impeller/entity/shaders/tiled_texture_fill.vert
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}