Add srgb converion in shaders

This commit is contained in:
Imbris 2019-08-04 15:54:08 -04:00
parent d71cc31e29
commit 3988daed39
18 changed files with 114 additions and 80 deletions

View File

@ -23,12 +23,13 @@ uniform u_bones {
#include <sky.glsl> #include <sky.glsl>
#include <light.glsl> #include <light.glsl>
#include <srgb.glsl>
out vec4 tgt_color; out vec4 tgt_color;
void main() { void main() {
vec3 light = get_sun_diffuse(f_norm, time_of_day.x) + light_at(f_pos, f_norm); vec3 light = get_sun_diffuse(f_norm, time_of_day.x) + light_at(f_pos, f_norm);
vec3 surf_color = model_col.rgb * f_col * 2.0 * light; vec3 surf_color = srgb_to_linear(model_col.rgb * f_col) * 2.0 * light;
float fog_level = fog(f_pos.xy, focus_pos.xy); float fog_level = fog(f_pos.xy, focus_pos.xy);
vec3 fog_color = get_sky_color(normalize(f_pos - cam_pos.xyz), time_of_day.x); vec3 fog_color = get_sky_color(normalize(f_pos - cam_pos.xyz), time_of_day.x);

View File

@ -8,6 +8,8 @@ uniform u_lights {
Light lights[32]; Light lights[32];
}; };
#include <srgb.glsl>
float attenuation_strength(vec3 rpos) { float attenuation_strength(vec3 rpos) {
return 1.0 / (rpos.x * rpos.x + rpos.y * rpos.y + rpos.z * rpos.z); return 1.0 / (rpos.x * rpos.x + rpos.y * rpos.y + rpos.z * rpos.z);
} }
@ -30,7 +32,7 @@ vec3 light_at(vec3 wpos, vec3 wnorm) {
float strength = attenuation_strength(difference); float strength = attenuation_strength(difference);
// Multiply the vec3 only once // Multiply the vec3 only once
vec3 color = L.light_col.rgb * (strength * L.light_col.a); vec3 color = srgb_to_linear(L.light_col.rgb) * (strength * L.light_col.a);
// This is commented out to avoid conditional branching. See here: https://community.khronos.org/t/glsl-float-multiply-by-zero/104391 // This is commented out to avoid conditional branching. See here: https://community.khronos.org/t/glsl-float-multiply-by-zero/104391
// if (max(max(color.r, color.g), color.b) < 0.002) { // if (max(max(color.r, color.g), color.b) < 0.002) {

View File

@ -0,0 +1,8 @@
//https://gamedev.stackexchange.com/questions/92015/optimized-linear-to-srgb-glsl
vec3 srgb_to_linear(vec3 srgb) {
bvec3 cutoff = lessThan(srgb, vec3(0.04045));
vec3 higher = pow((srgb + vec3(0.055))/vec3(1.055), vec3(2.4));
vec3 lower = srgb/vec3(12.92);
return mix(higher, lower, cutoff);
}

View File

@ -170,7 +170,8 @@ void main() {
hsva_color.y *= 1.45; hsva_color.y *= 1.45;
hsva_color.z *= 0.85; hsva_color.z *= 0.85;
//hsva_color.z = 1.0 - 1.0 / (1.0 * hsva_color.z + 1.0); //hsva_color.z = 1.0 - 1.0 / (1.0 * hsva_color.z + 1.0);
vec4 final_color = vec4(hsv2rgb(hsva_color.rgb), hsva_color.a); vec4 final_color = fxaa_color;
//vec4 final_color = vec4(hsv2rgb(hsva_color.rgb), hsva_color.a);
tgt_color = vec4(final_color.rgb, 1); tgt_color = vec4(final_color.rgb, 1);
} }

View File

@ -16,10 +16,11 @@ out vec4 tgt_color;
#include <sky.glsl> #include <sky.glsl>
#include <light.glsl> #include <light.glsl>
#include <srgb.glsl>
void main() { void main() {
vec3 light = get_sun_diffuse(f_norm, time_of_day.x) * f_light + light_at(f_pos, f_norm); vec3 light = get_sun_diffuse(f_norm, time_of_day.x) * f_light + light_at(f_pos, f_norm);
vec3 surf_color = f_col * light; vec3 surf_color = srgb_to_linear(f_col) * light;
float fog_level = fog(f_pos.xy, focus_pos.xy); float fog_level = fog(f_pos.xy, focus_pos.xy);
vec3 fog_color = get_sky_color(normalize(f_pos - cam_pos.xyz), time_of_day.x); vec3 fog_color = get_sky_color(normalize(f_pos - cam_pos.xyz), time_of_day.x);

View File

@ -38,7 +38,7 @@ void main() {
float((v_col_light >> 8) & 0xFFu), float((v_col_light >> 8) & 0xFFu),
float((v_col_light >> 16) & 0xFFu), float((v_col_light >> 16) & 0xFFu),
float((v_col_light >> 24) & 0xFFu) float((v_col_light >> 24) & 0xFFu)
) / 200.0; ) / 255.0;
f_light = float(v_col_light & 0xFFu) / 255.0; f_light = float(v_col_light & 0xFFu) / 255.0;

View File

@ -1,7 +1,7 @@
[package] [package]
name = "veloren-common" name = "veloren-common"
version = "0.3.0" version = "0.3.0"
authors = ["Joshua Barretto <joshua.s.barretto@gmail.com>", "Maciej Ćwięka <mckol363@gmail.com>, Imbris <imbrisf@gmail.com>"] authors = ["Joshua Barretto <joshua.s.barretto@gmail.com>", "Maciej Ćwięka <mckol363@gmail.com>", "Imbris <imbrisf@gmail.com>"]
edition = "2018" edition = "2018"
[dependencies] [dependencies]

View File

@ -121,7 +121,7 @@ pub fn load_watched<A: Asset + 'static>(
} }
let owned_specifier = specifier.to_string(); let owned_specifier = specifier.to_string();
indicator.add(specifier, move || { indicator.add(path, move || {
if let Err(err) = reload::<A>(&owned_specifier) { if let Err(err) = reload::<A>(&owned_specifier) {
error!("Error reloading {}: {:#?}", &owned_specifier, err); error!("Error reloading {}: {:#?}", &owned_specifier, err);
} }

View File

@ -149,9 +149,6 @@ impl ReloadIndicator {
self.paths.push(path.clone()); self.paths.push(path.clone());
}; };
let mut path = super::ASSETS_PATH.clone();
path.push(specifier);
if WATCHER_TX if WATCHER_TX
.lock() .lock()
.unwrap() .unwrap()

View File

@ -1 +1,35 @@
pub const GIT_HASH: &str = include_str!(concat!(env!("OUT_DIR"), "/githash")); pub const GIT_HASH: &str = include_str!(concat!(env!("OUT_DIR"), "/githash"));
use vek::{Rgb, Rgba};
#[inline(always)]
pub fn srgb_to_linear(col: Rgb<f32>) -> Rgb<f32> {
#[inline(always)]
fn to_linear(x: f32) -> f32 {
if x <= 0.04045 {
x / 12.92
} else {
((x + 0.055) / 1.055).powf(2.4)
}
}
col.map(to_linear)
}
#[inline(always)]
pub fn linear_to_srgb(col: Rgb<f32>) -> Rgb<f32> {
#[inline(always)]
fn to_srgb(x: f32) -> f32 {
if x <= 0.0031308 {
x * 12.92
} else {
x.powf(1.0 / 2.4) * 1.055 - 0.055
}
}
col.map(to_srgb)
}
#[inline(always)]
pub fn srgba_to_linear(col: Rgba<f32>) -> Rgba<f32> {
Rgba::from_translucent(srgb_to_linear(Rgb::from(col)), col.a)
}
#[inline(always)]
pub fn linear_to_srgba(col: Rgba<f32>) -> Rgba<f32> {
Rgba::from_translucent(linear_to_srgb(Rgb::from(col)), col.a)
}

View File

@ -1,7 +1,7 @@
[package] [package]
name = "veloren-voxygen" name = "veloren-voxygen"
version = "0.3.0" version = "0.3.0"
authors = ["Joshua Barretto <joshua.s.barretto@gmail.com>"] authors = ["Joshua Barretto <joshua.s.barretto@gmail.com>", "Imbris <imbrisf@gmail.com>"]
edition = "2018" edition = "2018"
default-run = "veloren-voxygen" default-run = "veloren-voxygen"

View File

@ -4,6 +4,7 @@ use crate::{
}; };
use common::{ use common::{
figure::Segment, figure::Segment,
util::{linear_to_srgb, srgb_to_linear},
vol::{ReadVol, SizedVol}, vol::{ReadVol, SizedVol},
}; };
use vek::*; use vek::*;
@ -28,7 +29,12 @@ impl Meshable for Segment {
offs + pos.map(|e| e as f32), offs + pos.map(|e| e as f32),
col, col,
|origin, norm, col, ao, light| { |origin, norm, col, ao, light| {
FigureVertex::new(origin, norm, col * ao * light, 0) FigureVertex::new(
origin,
norm,
linear_to_srgb(srgb_to_linear(col) * ao * light),
0,
)
}, },
true, true,
&[[[1.0; 3]; 3]; 3], &[[[1.0; 3]; 3]; 3],

View File

@ -54,9 +54,9 @@ impl Vertex {
| ((pos.z.max(0.0).min((1 << 13) as f32) as u32) & 0x1FFF) << 16 | ((pos.z.max(0.0).min((1 << 13) as f32) as u32) & 0x1FFF) << 16
| ((norm_bits as u32) & 0x7) << 29, | ((norm_bits as u32) & 0x7) << 29,
col_light: 0 col_light: 0
| ((col.r.mul(200.0) as u32) & 0xFF) << 8 | ((col.r.mul(255.0) as u32) & 0xFF) << 8
| ((col.g.mul(200.0) as u32) & 0xFF) << 16 | ((col.g.mul(255.0) as u32) & 0xFF) << 16
| ((col.b.mul(200.0) as u32) & 0xFF) << 24 | ((col.b.mul(255.0) as u32) & 0xFF) << 24
| ((light.mul(255.0) as u32) & 0xFF) << 0, | ((light.mul(255.0) as u32) & 0xFF) << 0,
} }
} }

View File

@ -18,7 +18,7 @@ use log::error;
use vek::*; use vek::*;
/// Represents the format of the pre-processed color target. /// Represents the format of the pre-processed color target.
pub type TgtColorFmt = gfx::format::Rgba16F; pub type TgtColorFmt = gfx::format::Srgba8;
/// Represents the format of the pre-processed depth target. /// Represents the format of the pre-processed depth target.
pub type TgtDepthFmt = gfx::format::Depth; pub type TgtDepthFmt = gfx::format::Depth;
@ -476,11 +476,15 @@ fn create_pipelines(
let light = let light =
assets::load_watched::<String>("voxygen.shaders.include.light", shader_reload_indicator) assets::load_watched::<String>("voxygen.shaders.include.light", shader_reload_indicator)
.unwrap(); .unwrap();
let srgb =
assets::load_watched::<String>("voxygen.shaders.include.srgb", shader_reload_indicator)
.unwrap();
let mut include_ctx = IncludeContext::new(); let mut include_ctx = IncludeContext::new();
include_ctx.include("globals.glsl", &globals); include_ctx.include("globals.glsl", &globals);
include_ctx.include("sky.glsl", &sky); include_ctx.include("sky.glsl", &sky);
include_ctx.include("light.glsl", &light); include_ctx.include("light.glsl", &light);
include_ctx.include("srgb.glsl", &srgb);
// Construct a pipeline for rendering skyboxes // Construct a pipeline for rendering skyboxes
let skybox_pipeline = create_pipeline( let skybox_pipeline = create_pipeline(

View File

@ -1,6 +1,6 @@
use super::super::{linear_to_srgb, srgb_to_linear};
use common::{ use common::{
figure::Segment, figure::Segment,
util::{linear_to_srgba, srgba_to_linear},
vol::{ReadVol, SizedVol, Vox}, vol::{ReadVol, SizedVol, Vox},
}; };
use euc::{buffer::Buffer2d, rasterizer, Pipeline}; use euc::{buffer::Buffer2d, rasterizer, Pipeline};
@ -46,13 +46,13 @@ impl<'a> Pipeline for Voxel {
}: &Self::Vertex, }: &Self::Vertex,
) -> ([f32; 3], Self::VsOut) { ) -> ([f32; 3], Self::VsOut) {
let light = Rgba::from_opaque(Rgb::from(*ao_level as f32 / 4.0 + 0.25)); let light = Rgba::from_opaque(Rgb::from(*ao_level as f32 / 4.0 + 0.25));
let color = light * srgb_to_linear(Rgba::from_opaque(*col)); let color = light * srgba_to_linear(Rgba::from_opaque(*col));
let position = Vec3::from(self.mvp * Vec4::from_point(*pos)).into_array(); let position = Vec3::from(self.mvp * Vec4::from_point(*pos)).into_array();
(position, color) (position, color)
} }
#[inline(always)] #[inline(always)]
fn frag(&self, color: &Self::VsOut) -> Self::Pixel { fn frag(&self, color: &Self::VsOut) -> Self::Pixel {
linear_to_srgb(*color) linear_to_srgba(*color)
.map(|e| (e * 255.0) as u8) .map(|e| (e * 255.0) as u8)
.into_array() .into_array()
} }

View File

@ -2,7 +2,6 @@ mod cache;
mod event; mod event;
mod graphic; mod graphic;
mod scale; mod scale;
mod util;
mod widgets; mod widgets;
#[macro_use] #[macro_use]
pub mod img_ids; pub mod img_ids;
@ -28,7 +27,7 @@ use crate::{
Error, Error,
}; };
use cache::Cache; use cache::Cache;
use common::assets; use common::{assets, util::srgba_to_linear};
use conrod_core::{ use conrod_core::{
event::Input, event::Input,
graph::Graph, graph::Graph,
@ -48,7 +47,6 @@ use std::{
sync::Arc, sync::Arc,
time::Duration, time::Duration,
}; };
use util::{linear_to_srgb, srgb_to_linear};
use vek::*; use vek::*;
use widgets::tooltip::TooltipManager; use widgets::tooltip::TooltipManager;
@ -406,7 +404,7 @@ impl Ui {
} }
let color = let color =
srgb_to_linear(color.unwrap_or(conrod_core::color::WHITE).to_fsa().into()); srgba_to_linear(color.unwrap_or(conrod_core::color::WHITE).to_fsa().into());
let resolution = Vec2::new( let resolution = Vec2::new(
(rect.w() * p_scale_factor).round() as u16, (rect.w() * p_scale_factor).round() as u16,
@ -485,7 +483,7 @@ impl Ui {
}) })
.unwrap(); .unwrap();
let color = srgb_to_linear(color.to_fsa().into()); let color = srgba_to_linear(color.to_fsa().into());
for g in positioned_glyphs { for g in positioned_glyphs {
if let Ok(Some((uv_rect, screen_rect))) = if let Ok(Some((uv_rect, screen_rect))) =
@ -514,7 +512,7 @@ impl Ui {
} }
} }
PrimitiveKind::Rectangle { color } => { PrimitiveKind::Rectangle { color } => {
let color = srgb_to_linear(color.to_fsa().into()); let color = srgba_to_linear(color.to_fsa().into());
// Don't draw a transparent rectangle. // Don't draw a transparent rectangle.
if color[3] == 0.0 { if color[3] == 0.0 {
continue; continue;
@ -534,7 +532,7 @@ impl Ui {
} }
PrimitiveKind::TrianglesSingleColor { color, triangles } => { PrimitiveKind::TrianglesSingleColor { color, triangles } => {
// Don't draw transparent triangle or switch state if there are actually no triangles. // Don't draw transparent triangle or switch state if there are actually no triangles.
let color = srgb_to_linear(Rgba::from(Into::<[f32; 4]>::into(color))); let color = srgba_to_linear(Rgba::from(Into::<[f32; 4]>::into(color)));
if triangles.is_empty() || color[3] == 0.0 { if triangles.is_empty() || color[3] == 0.0 {
continue; continue;
} }

View File

@ -1,36 +0,0 @@
use vek::*;
#[inline(always)]
pub fn srgb_to_linear(c: Rgba<f32>) -> Rgba<f32> {
#[inline(always)]
fn to_linear(x: f32) -> f32 {
if x <= 0.04045 {
x / 12.92
} else {
((x + 0.055) / 1.055).powf(2.4)
}
}
Rgba {
r: to_linear(c.r),
g: to_linear(c.g),
b: to_linear(c.b),
a: c.a,
}
}
#[inline(always)]
pub fn linear_to_srgb(c: Rgba<f32>) -> Rgba<f32> {
#[inline(always)]
fn to_srgb(x: f32) -> f32 {
if x <= 0.0031308 {
x * 12.92
} else {
x.powf(1.0 / 2.4) * 1.055 - 0.055
}
}
Rgba {
r: to_srgb(c.r),
g: to_srgb(c.g),
b: to_srgb(c.b),
a: c.a,
}
}

View File

@ -7,6 +7,7 @@ use crate::{
}; };
use common::{ use common::{
terrain::{structure::StructureBlock, Block, Structure}, terrain::{structure::StructureBlock, Block, Structure},
util::linear_to_srgb,
vol::{ReadVol, Vox}, vol::{ReadVol, Vox},
}; };
use noise::NoiseFn; use noise::NoiseFn;
@ -208,7 +209,8 @@ impl<'a> BlockGen<'a> {
// Sample blocks // Sample blocks
let stone_col = Rgb::new(240, 230, 220); // let stone_col = Rgb::new(240, 230, 220);
let stone_col = Rgb::new(248, 243, 239);
// let dirt_col = Rgb::new(79, 67, 60); // let dirt_col = Rgb::new(79, 67, 60);
let air = Block::empty(); let air = Block::empty();
@ -217,12 +219,13 @@ impl<'a> BlockGen<'a> {
// let dirt = Block::new(1, dirt_col); // let dirt = Block::new(1, dirt_col);
// let sand = Block::new(1, Rgb::new(180, 150, 50)); // let sand = Block::new(1, Rgb::new(180, 150, 50));
// let warm_stone = Block::new(1, Rgb::new(165, 165, 130)); // let warm_stone = Block::new(1, Rgb::new(165, 165, 130));
let water = Block::new(1, Rgb::new(100, 150, 255)); // let water = Block::new(1, Rgb::new(100, 150, 255));
let water = Block::new(1, Rgb::new(168, 202, 255));
let grass_depth = 1.5 + 2.0 * chaos; let grass_depth = 1.5 + 2.0 * chaos;
let block = if (wposf.z as f32) < height - grass_depth { let block = if (wposf.z as f32) < height - grass_depth {
let col = Lerp::lerp( let col = Lerp::lerp(
sub_surface_color.map(|e| (e * 255.0) as u8), linear_to_srgb(sub_surface_color).map(|e| (e * 255.0) as u8),
stone_col, stone_col,
(height - grass_depth - wposf.z as f32) * 0.15, (height - grass_depth - wposf.z as f32) * 0.15,
); );
@ -242,7 +245,10 @@ impl<'a> BlockGen<'a> {
.powf(0.5), .powf(0.5),
); );
// Surface // Surface
Some(Block::new(1, col.map(|e| (e * 255.0) as u8))) Some(Block::new(
1,
linear_to_srgb(col).map(|e| (e * 255.0) as u8),
))
} else if (wposf.z as f32) < water_height { } else if (wposf.z as f32) < water_height {
// Ocean // Ocean
Some(water) Some(water)
@ -279,9 +285,9 @@ impl<'a> BlockGen<'a> {
1, 1,
stone_col stone_col
- Rgb::new( - Rgb::new(
field0.get(wpos) as u8 % 32, field0.get(wpos) as u8 % 16,
field1.get(wpos) as u8 % 32, field1.get(wpos) as u8 % 16,
field2.get(wpos) as u8 % 32, field2.get(wpos) as u8 % 16,
), ),
)) ))
} else { } else {
@ -407,7 +413,7 @@ impl StructureInfo {
.map(|e: i32| (e.abs() / 2) * 2) .map(|e: i32| (e.abs() / 2) * 2)
.reduce_max() .reduce_max()
{ {
Some(Block::new(2, Rgb::new(180, 140, 90))) Some(Block::new(2, Rgb::new(219, 196, 160)))
} else { } else {
None None
} }
@ -445,19 +451,27 @@ fn block_from_structure(
match sblock { match sblock {
StructureBlock::TemperateLeaves => Some(Block::new( StructureBlock::TemperateLeaves => Some(Block::new(
1, 1,
Lerp::lerp(Rgb::new(0.0, 70.0, 35.0), Rgb::new(100.0, 140.0, 0.0), lerp) Lerp::lerp(
.map(|e| e as u8), Rgb::new(0.00, 143.0, 103.6),
Rgb::new(168.1, 195.5, 0.0),
lerp,
)
.map(|e| e as u8),
)), )),
StructureBlock::PineLeaves => Some(Block::new( StructureBlock::PineLeaves => Some(Block::new(
1, 1,
Lerp::lerp(Rgb::new(0.0, 60.0, 50.0), Rgb::new(30.0, 100.0, 10.0), lerp) Lerp::lerp(
.map(|e| e as u8), Rgb::new(0.00, 133.2, 122.4),
Rgb::new(96.3, 168.1, 55.8),
lerp,
)
.map(|e| e as u8),
)), )),
StructureBlock::PalmLeaves => Some(Block::new( StructureBlock::PalmLeaves => Some(Block::new(
1, 1,
Lerp::lerp( Lerp::lerp(
Rgb::new(15.0, 100.0, 30.0), Rgb::new(68.6, 168.1, 96.3),
Rgb::new(55.0, 220.0, 0.0), Rgb::new(128.0, 239.0, 0.00),
lerp, lerp,
) )
.map(|e| e as u8), .map(|e| e as u8),
@ -465,16 +479,20 @@ fn block_from_structure(
StructureBlock::Acacia => Some(Block::new( StructureBlock::Acacia => Some(Block::new(
1, 1,
Lerp::lerp( Lerp::lerp(
Rgb::new(35.0, 100.0, 10.0), Rgb::new(103.6, 168.1, 55.8),
Rgb::new(70.0, 190.0, 25.0), Rgb::new(143.0, 224.0, 88.2),
lerp, lerp,
) )
.map(|e| e as u8), .map(|e| e as u8),
)), )),
StructureBlock::Fruit => Some(Block::new( StructureBlock::Fruit => Some(Block::new(
1, 1,
Lerp::lerp(Rgb::new(255.0, 0.0, 0.0), Rgb::new(200.0, 255.0, 6.0), lerp) Lerp::lerp(
.map(|e| e as u8), Rgb::new(255.0, 0.0, 0.0),
Rgb::new(229.1, 255.0, 42.4),
lerp,
)
.map(|e| e as u8),
)), )),
StructureBlock::Hollow => Some(Block::empty()), StructureBlock::Hollow => Some(Block::empty()),
StructureBlock::Block(block) => Some(block).filter(|block| !block.is_empty()), StructureBlock::Block(block) => Some(block).filter(|block| !block.is_empty()),