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

Add color clamping to metal shader (cherry pick #397) #398

Merged
merged 1 commit into from
Dec 14, 2023
Merged
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
14 changes: 14 additions & 0 deletions src/graphic/Fast3D/gfx_metal_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,13 @@ MTL::VertexDescriptor* gfx_metal_build_shader(char buf[8192], size_t& num_floats
append_line(buf, &len, "}");
// end vertex shader

append_line(buf, &len, "float3 mod(float3 a, float3 b) {");
append_line(
buf, &len,
" return float3(a.x - b.x * floor(a.x / b.x), a.y - b.y * floor(a.y / b.y), a.z - b.z * floor(a.z / b.z));");
append_line(buf, &len, "}");
append_line(buf, &len, "#define WRAP(x, low, high) mod((x)-(low), (high)-(low)) + (low)");

// fragment shader
if (three_point_filtering) {
append_line(
Expand Down Expand Up @@ -387,8 +394,15 @@ MTL::VertexDescriptor* gfx_metal_build_shader(char buf[8192], size_t& num_floats
cc_features.do_mix[c][0], cc_features.opt_alpha, false, cc_features.opt_alpha);
}
append_line(buf, &len, ";");

if (c == 0) {
append_str(buf, &len, "texel.xyz = WRAP(texel.xyz, -1.01, 1.01);");
}
}

append_str(buf, &len, "texel.xyz = WRAP(texel.xyz, -0.51, 1.51);");
append_str(buf, &len, "texel.xyz = clamp(texel.xyz, 0.0, 1.0);");

if (cc_features.opt_fog) {
if (cc_features.opt_alpha) {
append_line(buf, &len, " texel = float4(mix(texel.xyz, in.fog.xyz, in.fog.w), texel.w);");
Expand Down
Loading