From 1dd9e18e326a8cc1993e7bb9b32ff602a62f3501 Mon Sep 17 00:00:00 2001 From: Cameron Murphy Reikes Date: Mon, 10 Jul 2023 19:59:32 -0700 Subject: [PATCH] ACES color correction --- main.c | 31 +++++++++++++++++++++++++++++-- threedee.glsl | 43 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 71 insertions(+), 3 deletions(-) diff --git a/main.c b/main.c index 1772ca7..39527c7 100644 --- a/main.c +++ b/main.c @@ -2650,6 +2650,7 @@ static struct sg_image threedee_pass_depth_image; sg_pipeline twodee_outline_pip; + sg_pipeline twodee_colorcorrect_pip; Shadow_State shadows; } state; @@ -3299,9 +3300,34 @@ void init(void) .dst_factor_alpha = SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, .op_alpha = SG_BLENDOP_ADD, }, - .label = "quad-pipeline", + .label = "twodee-outline", }); + state.twodee_colorcorrect_pip = sg_make_pipeline(&(sg_pipeline_desc) + { + .shader = sg_make_shader(threedee_twodee_colorcorrect_shader_desc(sg_query_backend())), + .depth = { + .compare = SG_COMPAREFUNC_LESS_EQUAL, + .write_enabled = true + }, + .layout = { + .attrs = + { + [ATTR_threedee_vs_twodee_position].format = SG_VERTEXFORMAT_FLOAT3, + [ATTR_threedee_vs_twodee_texcoord0].format = SG_VERTEXFORMAT_FLOAT2, + } + }, + .colors[0].blend = (sg_blend_state) { // allow transparency + .enabled = true, + .src_factor_rgb = SG_BLENDFACTOR_SRC_ALPHA, + .dst_factor_rgb = SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, + .op_rgb = SG_BLENDOP_ADD, + .src_factor_alpha = SG_BLENDFACTOR_ONE, + .dst_factor_alpha = SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, + .op_alpha = SG_BLENDOP_ADD, + }, + .label = "twodee-color-correct", + }); desc = threedee_mesh_shader_desc(sg_query_backend()); assert(desc); @@ -5665,7 +5691,7 @@ void frame(void) flush_all_drawn_things(light_dir, cam_pos, facing, right); // draw the 3d render - draw_quad((DrawParams){quad_at(V2(0.0, screen_size().y), screen_size()), IMG(state.threedee_pass_image), WHITE, .layer = LAYER_WORLD }); + draw_quad((DrawParams){quad_at(V2(0.0, screen_size().y), screen_size()), IMG(state.threedee_pass_image), WHITE, .layer = LAYER_WORLD, .custom_pipeline = state.twodee_colorcorrect_pip }); // draw the freaking outline. Play ball! draw_quad((DrawParams){quad_at(V2(0.0, screen_size().y), screen_size()), IMG(state.outline_pass_image), WHITE, .layer = LAYER_UI_FG, .custom_pipeline = state.twodee_outline_pip, .layer = LAYER_UI}); @@ -6764,6 +6790,7 @@ void frame(void) quad_centered(bubble_center, size), IMG(image_dialog_bubble), blendalpha(WHITE, bubble_factor), + .layer = LAYER_UI_FG, }); AABB placing_text_in = aabb_centered(AddV2(bubble_center, V2(0,10.0f)), V2(size.x*0.8f, size.y*0.15f)); diff --git a/threedee.glsl b/threedee.glsl index 1446fe8..6169cf8 100644 --- a/threedee.glsl +++ b/threedee.glsl @@ -361,6 +361,47 @@ void main() { } @end +@fs fs_twodee_color_correction +uniform sampler2D twodee_tex; +uniform twodee_fs_params { + vec4 tint; + + // both in clip space + vec2 clip_ul; + vec2 clip_lr; + + float alpha_clip_threshold; + + vec2 tex_size; +}; + +in vec2 uv; +in vec2 pos; +out vec4 frag_color; + +// Black Box From https://github.com/armory3d/armory/blob/master/Shaders/std/tonemap.glsl +vec3 acesFilm(const vec3 x) { + const float a = 2.51; + const float b = 0.03; + const float c = 2.43; + const float d = 0.59; + const float e = 0.14; + return clamp((x * (a * x + b)) / (x * (c * x + d ) + e), 0.0, 1.0); +} + +void main() { + // clip space is from [-1,1] [left, right]of screen on X, and [-1,1] [bottom, top] of screen on Y + if(pos.x < clip_ul.x || pos.x > clip_lr.x || pos.y < clip_lr.y || pos.y > clip_ul.y) discard; + + vec4 col = texture(twodee_tex, uv); + + col.rgb = acesFilm(col.rgb); + + frag_color = col; +} +@end + + @fs fs_outline uniform sampler2D tex; @@ -395,4 +436,4 @@ void main() { @program twodee vs_twodee fs_twodee @program twodee_outline vs_twodee fs_twodee_outline - +@program twodee_colorcorrect vs_twodee fs_twodee_color_correction