Skip to content

Commit 65ac4dc

Browse files
committed
zebra stripes in gles
1 parent cf5e1df commit 65ac4dc

File tree

4 files changed

+34
-19
lines changed

4 files changed

+34
-19
lines changed

assets/shaders/image-shader_frag.glsl

+22-11
Original file line numberDiff line numberDiff line change
@@ -166,23 +166,25 @@ float sample_channel(sampler2D sampler, vec2 uv, bool within_image)
166166

167167
void main()
168168
{
169+
vec2 pixel = vec2(gl_FragCoord.x, -gl_FragCoord.y);
169170
vec4 background = vec4(bg_color.rgb, 1.0);
170171
if (bg_mode == BG_BLACK)
171172
background.rgb = vec3(0.0);
172173
else if (bg_mode == BG_WHITE)
173174
background.rgb = vec3(1.0);
174175
else if (bg_mode == BG_DARK_CHECKER || bg_mode == BG_LIGHT_CHECKER)
175176
{
176-
float dark_gray = (bg_mode == BG_DARK_CHECKER) ? 0.1 : 0.5;
177-
float light_gray = (bg_mode == BG_DARK_CHECKER) ? 0.2 : 0.55;
178-
float checkerboard =
179-
mod(floor(gl_FragCoord.x / 8.0) + floor(gl_FragCoord.y / 8.0), 2.0) == 0.0 ? dark_gray : light_gray;
180-
background.rgb = sRGBToLinear(vec3(checkerboard));
177+
float dark_gray = (bg_mode == BG_DARK_CHECKER) ? 0.1 : 0.5;
178+
float light_gray = (bg_mode == BG_DARK_CHECKER) ? 0.2 : 0.55;
179+
float checkerboard = mod(floor(pixel.x / 8.0) + floor(pixel.y / 8.0), 2.0) == 0.0 ? dark_gray : light_gray;
180+
background.rgb = sRGBToLinear(vec3(checkerboard));
181181
}
182182

183+
float zebra1 = (mod(float(int(floor((pixel.x + pixel.y - 30.0 * time) / 8.0))), 2.0) == 0.0) ? 0.0 : 1.0;
184+
float zebra2 = (mod(float(int(floor((pixel.x - pixel.y - 30.0 * time) / 8.0))), 2.0) == 0.0) ? 0.0 : 1.0;
185+
183186
bool in_img = primary_uv.x < 1.0 && primary_uv.y < 1.0 && primary_uv.x > 0.0 && primary_uv.y > 0.0;
184-
bool in_ref =
185-
secondary_uv.x < 1.0 && secondary_uv.y < 1.0 && secondary_uv.x > 0.0 && secondary_uv.y > 0.0 && has_reference;
187+
bool in_ref = secondary_uv.x < 1.0 && secondary_uv.y < 1.0 && secondary_uv.x > 0.0 && secondary_uv.y > 0.0;
186188

187189
if (!in_img && !(in_ref && has_reference))
188190
{
@@ -214,8 +216,17 @@ void main()
214216
value = blend(value, reference_val);
215217
}
216218

217-
vec4 foreground = choose_channel(value) * vec4(vec3(gain), 1.0);
218-
vec4 blended = dither(linearToSRGB(tonemap(foreground) + background * (1.0 - foreground.a)));
219-
blended = clamp(blended, clamp_to_LDR ? 0.0 : -64.0, clamp_to_LDR ? 1.0 : 64.0);
220-
frag_color = vec4(blended.rgb, 1.0);
219+
vec4 foreground = choose_channel(value) * vec4(vec3(gain), 1.0);
220+
vec4 tonemapped = tonemap(foreground) + background * (1.0 - foreground.a);
221+
bvec3 clipped = greaterThan(foreground.rgb, clip_range.yyy);
222+
bvec3 crushed = lessThan(foreground.rgb, clip_range.xxx);
223+
vec4 blended = linearToSRGB(tonemapped);
224+
vec4 dithered = dither(blended);
225+
dithered = clamp(dithered, clamp_to_LDR ? 0.0 : -64.0, clamp_to_LDR ? 1.0 : 64.0);
226+
if (draw_clip_warnings)
227+
{
228+
dithered.rgb = mix(dithered.rgb, vec3(zebra1), clipped);
229+
dithered.rgb = mix(dithered.rgb, vec3(zebra2), crushed);
230+
}
231+
frag_color = vec4(dithered.rgb, 1.0);
221232
}

assets/shaders/image-shader_frag.metal

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ fragment float4 fragment_main(VertexOut vert [[stage_in]],
194194
float zebra2 = (fmod(float(int(floor((vert.position.x - vert.position.y - 30.0*time) / 8.0))), 2.0) == 0.0) ? 0.0 : 1.0;
195195

196196
bool in_img = all(vert.primary_uv < 1.0) and all(vert.primary_uv > 0.0);
197-
bool in_ref = all(vert.secondary_uv < 1.0) and all(vert.secondary_uv > 0.0);// and has_reference;
197+
bool in_ref = all(vert.secondary_uv < 1.0) and all(vert.secondary_uv > 0.0);
198198

199199
if (!in_img and !(in_ref and has_reference))
200200
return linearToSRGB(background);

src/app.cpp

+8-4
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ HDRViewApp::HDRViewApp(std::optional<float> force_exposure, std::optional<float>
275275
HelloImGui::DockableWindow advanced_settings_window;
276276
advanced_settings_window.label = "Advanced settings";
277277
advanced_settings_window.dockSpaceName = "RightSpace";
278-
advanced_settings_window.isVisible = true;
278+
advanced_settings_window.isVisible = false;
279279
advanced_settings_window.rememberIsVisible = true;
280280
advanced_settings_window.GuiFunction = [this]
281281
{
@@ -285,8 +285,8 @@ HDRViewApp::HDRViewApp(std::optional<float> force_exposure, std::optional<float>
285285
ImGui::SameLine();
286286
ImGui::PushItemWidth(-5 * HelloImGui::EmSize());
287287
ImGui::BeginDisabled(!m_draw_clip_warnings);
288-
ImGui::DragFloatRange2("Clip warning", &m_clip_range.min.x, &m_clip_range.max.x, 0.01f, 0.f, 0.f,
289-
"min: %.1f", "max: %.1f");
288+
ImGui::DragFloatRange2("Clip warning", &m_clip_range.x, &m_clip_range.y, 0.01f, 0.f, 0.f, "min: %.1f",
289+
"max: %.1f");
290290
ImGui::EndDisabled();
291291
ImGui::PopItemWidth();
292292
// ImGui::TreePop();
@@ -1073,6 +1073,8 @@ void HDRViewApp::load_settings()
10731073
m_dither = j.value<bool>("dither", m_dither);
10741074
g_file_list_mode = j.value<int>("file list mode", g_file_list_mode);
10751075
g_short_names = j.value<bool>("short names", g_short_names);
1076+
m_draw_clip_warnings = j.value<bool>("draw clip warnings", m_draw_clip_warnings);
1077+
m_clip_range = j.value<float2>("clip range", m_clip_range);
10761078
}
10771079
catch (json::exception &e)
10781080
{
@@ -1102,6 +1104,8 @@ void HDRViewApp::save_settings()
11021104
j["verbosity"] = spdlog::get_level();
11031105
j["file list mode"] = g_file_list_mode;
11041106
j["short names"] = g_short_names;
1107+
j["draw clip warnings"] = m_draw_clip_warnings;
1108+
j["clip range"] = m_clip_range;
11051109
HelloImGui::SaveUserPref("UserSettings", j.dump(4));
11061110
}
11071111

@@ -2622,7 +2626,7 @@ void HDRViewApp::draw_image() const
26222626

26232627
m_shader->set_uniform("time", (float)ImGui::GetTime());
26242628
m_shader->set_uniform("draw_clip_warnings", m_draw_clip_warnings);
2625-
m_shader->set_uniform("clip_range", float2{m_clip_range.min.x, m_clip_range.max.x});
2629+
m_shader->set_uniform("clip_range", m_clip_range);
26262630
m_shader->set_uniform("randomness", randomness);
26272631
m_shader->set_uniform("gain", powf(2.0f, m_exposure_live));
26282632
m_shader->set_uniform("gamma", m_gamma_live);

src/app.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,9 @@ class HDRViewApp
222222
AxisScale_ m_x_scale = AxisScale_Asinh, m_y_scale = AxisScale_Linear;
223223
bool m_clamp_to_LDR = false, m_dither = true, m_draw_grid = true, m_draw_pixel_info = true,
224224
m_draw_watched_pixels = true, m_draw_data_window = true, m_draw_display_window = true,
225-
m_draw_clip_warnings = true;
226-
Box1f m_clip_range{0.f, 1.f};
227-
Box2i m_roi = Box2i{int2{0}}, m_roi_live = Box2i{int2{0}};
225+
m_draw_clip_warnings = false;
226+
float2 m_clip_range{0.f, 1.f}; ///< Values outside this range will have zebra stripes if m_draw_clip_warnings = true
227+
Box2i m_roi{int2{0}}, m_roi_live{int2{0}};
228228

229229
// Image display parameters.
230230
float m_zoom_sensitivity = 1.0717734625f;

0 commit comments

Comments
 (0)