Skip to content

Commit

Permalink
Add color clamping to metal shader (#397) (#398)
Browse files Browse the repository at this point in the history
Co-authored-by: Garrett Cox <[email protected]>
  • Loading branch information
briaguya-ai and garrettjoecox authored Dec 14, 2023
1 parent a63baf4 commit 7a8d314
Showing 1 changed file with 14 additions and 0 deletions.
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

0 comments on commit 7a8d314

Please sign in to comment.