Skip to content

Commit 6148ecb

Browse files
authored
Update ShaderSourceProcessor_inline.h
1 parent 8504ad0 commit 6148ecb

1 file changed

Lines changed: 107 additions & 0 deletions

File tree

MobileGL/MG_Util/ShaderTranspiler/ShaderSourceProcessor_inline.h

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,113 @@ vec4 GI_TemporalFilter() {
11371137
}
11381138

11391139

1140+
// ==================== 3. 注入光照安全补丁 ====================
1141+
1142+
// 补丁1: 防止除零(SHADING_STRENGTH)
1143+
const char* str_rcp_shading = "rcp(SHADING_STRENGTH)";
1144+
if (source.find(str_rcp_shading) != String::npos) {
1145+
SizeT pos = source.find(str_rcp_shading);
1146+
while (pos != String::npos) {
1147+
MGLOG_D("Applying safety patch 1: Safe SHADING_STRENGTH division");
1148+
std::string replacement = "(SHADING_STRENGTH > 0.001 ? 1.0 / SHADING_STRENGTH : 1.0)";
1149+
source.replace(pos, strlen(str_rcp_shading), replacement);
1150+
pos = source.find(str_rcp_shading, pos + replacement.length());
1151+
}
1152+
}
1153+
1154+
// 补丁2: 防止pow函数接收负值
1155+
const char* str_pow8 = "pow8(blocklight)";
1156+
if (source.find(str_pow8) != String::npos) {
1157+
SizeT pos = source.find(str_pow8);
1158+
while (pos != String::npos) {
1159+
MGLOG_D("Applying safety patch 2: Safe pow8");
1160+
std::string replacement = "pow(max(blocklight, 0.0), 8.0)";
1161+
source.replace(pos, strlen(str_pow8), replacement);
1162+
pos = source.find(str_pow8, pos + replacement.length());
1163+
}
1164+
}
1165+
1166+
// 补丁3: 浮点相等性检查
1167+
const char* str_float_eq = "if (distance_weight_sum == 0.0)";
1168+
if (source.find(str_float_eq) != String::npos) {
1169+
SizeT pos = source.find(str_float_eq);
1170+
while (pos != String::npos) {
1171+
MGLOG_D("Applying safety patch 3: Float equality check");
1172+
std::string replacement = "if (distance_weight_sum <= 0.0001)";
1173+
source.replace(pos, strlen(str_float_eq), replacement);
1174+
pos = source.find(str_float_eq, pos + replacement.length());
1175+
}
1176+
}
1177+
1178+
// 补丁4: 防止光照方向y分量为负导致的异常
1179+
const char* str_lightdir_div = "clamp01(rcp(0.02) * light_dir.y)";
1180+
if (source.find(str_lightdir_div) != String::npos) {
1181+
SizeT pos = source.find(str_lightdir_div);
1182+
while (pos != String::npos) {
1183+
MGLOG_D("Applying safety patch 4: Safe light_dir.y division");
1184+
std::string replacement = "clamp01(max(light_dir.y, 0.0) * 50.0)";
1185+
source.replace(pos, strlen(str_lightdir_div), replacement);
1186+
pos = source.find(str_lightdir_div, pos + replacement.length());
1187+
}
1188+
}
1189+
1190+
// 补丁5: 防止NaN传播
1191+
const char* str_return_lighting = "return max0(lighting) * material.albedo * rcp_pi";
1192+
if (source.find(str_return_lighting) != String::npos) {
1193+
SizeT pos = source.find(str_return_lighting);
1194+
while (pos != String::npos) {
1195+
MGLOG_D("Applying safety patch 5: NaN protection");
1196+
1197+
// 构建替换代码
1198+
std::string replacement = R"( vec3 result = max0(lighting) * material.albedo * rcp_pi;
1199+
// 防止NaN和无穷大
1200+
result = clamp(result, vec3(0.0), vec3(100.0));
1201+
result = isnan(result.x) ? vec3(0.0) : result;
1202+
return result)";
1203+
1204+
source.replace(pos, strlen(str_return_lighting), replacement);
1205+
pos = source.find(str_return_lighting, pos + replacement.length());
1206+
}
1207+
}
1208+
1209+
// 补丁6: SSS系数安全计算
1210+
const char* str_sss_coeff = "vec3 coeff = albedo * inversesqrt(dot(albedo, luminance_weights) + eps);";
1211+
if (source.find(str_sss_coeff) != String::npos) {
1212+
SizeT pos = source.find(str_sss_coeff);
1213+
while (pos != String::npos) {
1214+
MGLOG_D("Applying safety patch 6: Safe SSS coefficient");
1215+
std::string replacement = "vec3 coeff = albedo * inversesqrt(max(dot(albedo, luminance_weights), 0.001) + eps);";
1216+
source.replace(pos, strlen(str_sss_coeff), replacement);
1217+
pos = source.find(str_sss_coeff, pos + replacement.length());
1218+
}
1219+
}
1220+
1221+
// 补丁7: 最小光照保护
1222+
const char* str_diffuse_shadows = "lighting += diffuse * shadows";
1223+
if (source.find(str_diffuse_shadows) != String::npos) {
1224+
SizeT pos = source.find(str_diffuse_shadows);
1225+
while (pos != String::npos) {
1226+
MGLOG_D("Applying safety patch 7: Minimum lighting");
1227+
std::string replacement = "lighting += max(diffuse, vec3(0.01)) * shadows";
1228+
source.replace(pos, strlen(str_diffuse_shadows), replacement);
1229+
pos = source.find(str_diffuse_shadows, pos + replacement.length());
1230+
}
1231+
}
1232+
1233+
// 补丁8: 安全的lift函数调用
1234+
const char* str_lift_nol = "lift(max0(NoL), 0.25 * rcp(SHADING_STRENGTH))";
1235+
if (source.find(str_lift_nol) != String::npos) {
1236+
SizeT pos = source.find(str_lift_nol);
1237+
while (pos != String::npos) {
1238+
MGLOG_D("Applying safety patch 8: Safe lift function");
1239+
std::string replacement = "max(lift(max0(NoL), 0.25 * (SHADING_STRENGTH > 0.001 ? 1.0 / SHADING_STRENGTH : 4.0)), 0.01)";
1240+
source.replace(pos, strlen(str_lift_nol), replacement);
1241+
pos = source.find(str_lift_nol, pos + replacement.length());
1242+
}
1243+
}
1244+
1245+
1246+
11401247
}
11411248

11421249

0 commit comments

Comments
 (0)