Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wrong color space #14

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions src/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ impl<A: AllocatorTrait> Integration<A> {
.array_layers(1)
.extent(extent)
.flags(vk::ImageCreateFlags::empty())
.format(vk::Format::R8G8B8A8_UNORM)
.format(vk::Format::R8G8B8A8_SRGB)
.image_type(vk::ImageType::TYPE_2D)
.initial_layout(vk::ImageLayout::UNDEFINED)
.mip_levels(1)
Expand Down Expand Up @@ -920,7 +920,7 @@ impl<A: AllocatorTrait> Integration<A> {
let create_info = vk::ImageViewCreateInfo::builder()
.components(vk::ComponentMapping::default())
.flags(vk::ImageViewCreateFlags::empty())
.format(vk::Format::R8G8B8A8_UNORM)
.format(vk::Format::R8G8B8A8_SRGB)
.image(texture_image)
.subresource_range(
vk::ImageSubresourceRange::builder()
Expand Down
Binary file modified src/shaders/spv/frag.spv
Binary file not shown.
Binary file modified src/shaders/spv/vert.spv
Binary file not shown.
15 changes: 14 additions & 1 deletion src/shaders/src/frag.frag
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,17 @@ layout(location = 0) out vec4 outColor;

layout(binding = 0, set = 0) uniform sampler2D font_texture;

void main() { outColor = inColor * texture(font_texture, inUV); }
// 0-1 sRGB gamma from 0-1 linear
vec3 srgb_gamma_from_linear(vec3 rgb) {
bvec3 cutoff = lessThan(rgb, vec3(0.0031308));
vec3 lower = rgb * vec3(12.92);
vec3 higher = vec3(1.055) * pow(rgb, vec3(1.0 / 2.4)) - vec3(0.055);
return mix(higher, lower, vec3(cutoff));
}

// 0-1 sRGBA gamma from 0-1 linear
vec4 srgba_gamma_from_linear(vec4 rgba) {
return vec4(srgb_gamma_from_linear(rgba.rgb), rgba.a);
}

void main() { outColor = inColor * srgba_gamma_from_linear(texture(font_texture, inUV)); }
8 changes: 1 addition & 7 deletions src/shaders/src/vert.vert
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,11 @@ layout(location = 1) out vec2 outUV;
layout(push_constant) uniform PushConstants { vec2 screen_size; }
pushConstants;

vec3 srgb_to_linear(vec3 srgb) {
bvec3 cutoff = lessThan(srgb, vec3(0.04045));
vec3 lower = srgb / vec3(12.92);
vec3 higher = pow((srgb + vec3(0.055)) / vec3(1.055), vec3(2.4));
return mix(higher, lower, cutoff);
}

void main() {
gl_Position =
vec4(2.0 * inPos.x / pushConstants.screen_size.x - 1.0,
2.0 * inPos.y / pushConstants.screen_size.y - 1.0, 0.0, 1.0);
outColor = vec4(srgb_to_linear(inColor.rgb), inColor.a);
outColor = inColor;
outUV = inUV;
}