-
Notifications
You must be signed in to change notification settings - Fork 83
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
Update pixel shader translations to fix lighting issues #32
Conversation
- Minor updates in vertex and pixel shaders regex to merge two negative lookbacks into one - Added code to get the arithmetic count in use - Updated regex code that changes 'add' with a '-' modifier to 'sub' to make it more generic - Added code to generically update any modifier on a constant by using the extra arithmetic functions available, this fixes the lighting issues with Star Wars Republic Commando - Added code that changes 'mad' with a '-' modifier to 'sub', this fixes the lighting issues with Star Wars Republic Commando - Added code that changes '_bx2' source modifier for constant to use _x2 destination modifier, this fixes a minor lighting issue with Silent Hill 2
Let me explain this update in a bit more detail. The regex expressions are quite detailed and not easy to follow. CreateVertexShaderSourceCode = std::regex_replace(SourceCode, std::regex("mov (oFog|oPts)(.*), (-?)([crv][0-9]+(?![\\.0-9]))"), "mov $1$2, $3$4.x /* select single component */"); For this function I changed one line. Basically I merged these bracketed groups CreatePixelShaderThere were a few more updates I did here. Get Arithmetic Countint countArithmetic = 10; // Default to 10
const size_t ArithmeticPosition = SourceCode.find("arithmetic");
if (ArithmeticPosition > 2 && ArithmeticPosition < SourceCode.size())
{
countArithmetic = atoi(&SourceCode[ArithmeticPosition - 2]);
if (countArithmetic == 0)
{
countArithmetic = 10; // Default to 10
}
} This code simply finds the number of arithmetic operations done by looking at the decompiled Update 'sub' operation when using '-' constantSourceCode = std::regex_replace(SourceCode, std::regex("(add)([_satxd248]*) (r[0-9][\\.wxyz]*), ((1-|)[crtv][0-9][\\.wxyz_abdis2]*), (-)(c[0-9][\\.wxyz]*)(_bx2|_bias|_x2|_d[zbwa]|)(?![_\\.wxyz])"),
"sub$2 $3, $4, $7$8 /* changed 'add' to 'sub' removed modifier $6 */"); The next line will change the
Changing a This fixes part of the lighting issue in Star Wars Republic Commando. The next two updates are also required for the full fix. Each of these affect a different set of shaders where the lighting is way too bright. Replacing any modifier on a constant by using extra arithmetic operatorsfor (int x = 8 - countArithmetic; x > 0; x--)
{
const size_t beforeReplace = SourceCode.size();
// Only replace one match
SourceCode = std::regex_replace(SourceCode, std::regex("(...)(_[_satxd248]*|) (r[0-9][\\.wxyz]*), (1?-?[crtv][0-9][\\.wxyz_abdis2]*, )?(1?-?[crtv][0-9][\\.wxyz_abdis2]*, )?(1?-?[crtv][0-9][\\.wxyz_abdis2]*, )?((1?-)(c[0-9])([\\.wxyz]*)(_bx2|_bias|_x2|_d[zbwa]|)|(1?-?)(c[0-9])([\\.wxyz]*)(_bx2|_bias|_x2|_d[zbwa]))(?![_\\.wxyz])"),
"mov $3, $9$10$13$14 /* added line */\n $1$2 $3, $4$5$8$12$3$10$11$14$15 /* changed $9$13 to $3 */", std::regex_constants::format_first_only);
// Check if string was replaced
if (SourceCode.size() - beforeReplace == 0)
{
break;
}
} The next part of the code will take the modifier on the constant and
This whole section should also result in an exact match because rather than just removing the modifier we can use the destination register to replace the source constant. This fixes another part of the lighting issue in Star Wars Republic Commando. Update 'mad' operation when using '-' constantSourceCode = std::regex_replace(SourceCode, std::regex("(mad)([_satxd248]*) (r[0-9][\\.wxyz]*), (1?-?[crtv][0-9][\\.wxyz_abdis2]*), (1?-?[crtv][0-9][\\.wxyz_abdis2]*), (-)(c[0-9][\\.wxyz]*)(_bx2|_bias|_x2|_d[zbwa]|)(?![_\\.wxyz])"),
"sub$2 $3, $4, $7$8 /* changed 'mad' to 'sub' removed $5 removed modifier $6 */"); This is similar to the Update 'sub' operation above so I won't spell out all the regex groups here. But there are two main differences:
This is not an exact match, meaning it will not do exactly what the shader does in Direct3D8. However the code is already implemented to remove all modifiers on constants which means it already does not do exactly what Direct3D8 does. At least this change gets the behavior closer to what the Direct3D8 shader does. This fixes the final part of the lighting issue in Star Wars Republic Commando. Update '_bx2' modifier to use '_x2'SourceCode = std::regex_replace(SourceCode, std::regex("(...)(_sat|) (r[0-9][\\.wxyz]*), ([crtv][0-9][\\.wxyz]*)_bx2, (c[0-9][\\.wxyz]*)_bx2(?!,)"),
"$1_x2$2 $3, $4, $5 /* removed modifiers _bx2 added modifier _x2 */"); The regex on this is quite a bit simpler, so I won't detail it out unless needed. Basically if both source registers use the This issue was found in Silent Hill 2. Remove all trailing modifiers on constantsSourceCode = std::regex_replace(SourceCode, std::regex("(c[0-9][\\.wxyz]*)(_bx2|_bias|_x2|_d[zbwa])"), "$1 /* removed modifier $2 */"); This line has not changed much. However I did move it down some. It no longer needs to be at the top because all the regex expressions above it already ensure that no trailing modifier becomes dangling. I did change group $1 from Remove all leading modifiers on constantsSourceCode = std::regex_replace(SourceCode, std::regex("(1?-)(c[0-9][\\.wxyz]*(?![\\.wxyz]))"), "$2 /* removed modifier $1 */"); Again this line has not changed much either. I did change group $2 from TestingFor all of these changes I have tested them extensively with the regex debugger here, trying all sorts of combinations to ensure the regex would do what is expected in all cases with no side effects. I also tested this with the following Direct3D8 games to ensure that there was no ill side-effect:
|
Please let me know if you have any concerns with this code or any modifications you would like me to make before merging it. |
Awesome summary. I haven't merged it yet because I'm currently limited to my cell phone which makes viewing bigger changes rather complicated. As such I prefer to wait until I'm back at a desktop computer in a few days. |
This update completely resolves the lighting issues mentioned in #24 and #31.