elishacloud / dxwrapper

Fixes compatibility issues with older games running on Windows 10/11 by wrapping DirectX dlls. Also allows loading custom libraries with the file extension .asi into game processes.
zlib License
1.16k stars 83 forks source link

Can't wrap custom d3d8.dll with d3d8to9 #134

Closed BC46 closed 1 year ago

BC46 commented 2 years ago

I'm trying to run a game with a d3d8.dll from an older Windows 10 version that fixes some lighting-related issues the game has with the default Windows 10 d3d8.dll. Additionally, I'd like to wrap the older d3d8.dll with d3d8to9 so I can enable AF and AA.

This is what I have in my game folder: dxwrapper.dll dxwrapper.ini with the following config:

[Compatibility]
D3d8to9                    = 1

[d3d9]
AnisotropicFiltering       = 16
AntiAliasing               = 1

This file includes all the other values in the default dxwrapper.ini file, but those have not been changed from their defaults. d3d8.dll from the Stub folder d3d8.ini with the following config:

;; Config file for Stub to dxwrapper
[General]
RealDllPath       = Custom\d3d8.dll
WrapperMode       = d3d8.dll
StubOnly          = 0
LoadFromMemory    = 0

Custom\d3d8.dll. This is the custom d3d8.dll that I want to load instead of the default Windows 10 one.

With all these files and configs, d3d8to9 seems to load since the AF and AA are both working. However, it also seems the default Windows 10 d3d8.dll has loaded instead of the custom one, since the lighting bug is present in the game.

When I change D3d8to9 in dxwrapper.ini from 1 to 0, the exact opposite happens; the custom d3d8.dll loads, which has resulted in the lighting bug being gone, but obviously now d3d8to9 doesn't load as there is no AF nor AA.

It seems like I can only get one or the other to work simultaneously, but I'd like d3d8to9 to wrap the custom d3d8.dll. Am I missing something or is it impossible to get the result I'm trying to achieve here?

I'm using DxWrapper v1.0.6542.21. (latest release)

BC46 commented 1 year ago

Can you send me a Microsoft PIX output file of that frame?

Do I have to load the scene with the 16x AF and 8x AA enabled from d3d8?

elishacloud commented 1 year ago

You could enable AF and AA from d3d8 if you like. Then you can use d3d8to9 to covert that to d3d9. I do plan to look at the code and see if I can see any issues with dxwrapper's AF that could cause this issue.

BC46 commented 1 year ago

Sorry for the delay but I'm currently very busy studying for an upcoming exam. I'll provide the PIX files as soon as I can.

BC46 commented 1 year ago

I just tried making a GPU capture with PIX while Freelancer is running. Unfortunately it didn't work because the game is x86 and PIX only supports x64 apps from what I could find. Is there anything else I can try?

elishacloud commented 1 year ago

Yeah, you need the old version of PIX. The new one doesn't work on DirectX9 or 32-bit applications.

You need to download the old June 2010 SDK. Then you should be able to find the old version of PIX there. See this site.

Once I installed the June 2010 SDK I found PIX here:

C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Utilities\bin\x86\PIXWin.exe

BC46 commented 1 year ago

Here they are: PIX.zip. Both captures were made with AF and AA enabled from DxWrapper. The RX5700-DxWrapper.PIXRun file was taken on my main rig with the AMD RX 5700 (detail and emission textures were visible). The GTX1050-DxWrapper.PIXRun file I took on my old laptop with the GTX 1050 GPU (detail and emission textures were not visible).

elishacloud commented 1 year ago

Basically Freelancer uses so-called "detail textures" which are grayscale textures that are mapped on top of regular textures to make those appear more detailed. Also notice how in the second image the ship's lights on the top right appear to be turned off. This is because the emission map for it failed to load.

In both of these cases the game is using texture stage 1 to draw these items. As far as I can tell there are only a few things that are drawn by this game using stage 1.

The weird thing is that in the GTX capture these function calls were completely missing. I am not sure what could cause these calls to be completely missing. It is almost like something caused the game to decide not to do any stage 1 function calls.

Because recently as a test I force-enabled 16x AF and 8x AA in Freelancer using the d3d8 wrapper, and that worked like a charm without any repercussions.

What wrapper did you use to force 16x AF? How exactly did you use the wrapper to force this?

elishacloud commented 1 year ago

You could try this update, I'm not sure it will help, but it is worth a try; dxwrapper.zip

BC46 commented 1 year ago

What wrapper did you use to force 16x AF? How exactly did you use the wrapper to force this?

I used the d3d8 wrapper from your DirectX Wrappers project. As for the 16x AF, I simply updated the m_IDirect3DDevice8::SetTextureStageState method so it looks like this:

HRESULT m_IDirect3DDevice8::SetTextureStageState(DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD Value)
{
    if (Type == D3DTSS_MAGFILTER)
        Value = D3DTEXF_ANISOTROPIC;
    if (Type == D3DTSS_MINFILTER)
        Value = D3DTEXF_ANISOTROPIC;
    if (Type == D3DTSS_MIPFILTER)
        Value = D3DTEXF_ANISOTROPIC;
    if (Type == D3DTSS_MAXANISOTROPY)
        Value = 16;

    return ProxyInterface->SetTextureStageState(Stage, Type, Value);
}

I know you're not supposed to force-enable AF this way, but I just did it as a test.

You could try this update, I'm not sure it will help, but it is worth a try; dxwrapper.zip

With this build the textures do seem to load on the GTX PC. Well done! However, I just noticed that the AF doesn't seem to be working (this is also the case with the previous DxWrapper build): Untitled

On the AMD PC it works without issues: image

elishacloud commented 1 year ago

With this build the textures do seem to load on the GTX PC. Well done! However, I just noticed that the AF doesn't seem to be working (this is also the case with the previous DxWrapper build):

Ok, this means that the GTX you have does not support multi-staged AF textures. I made a simple change to turn off AF if multi-staged AF was not supported. I'm not sure the best way to turn off AF only when actually drawing multi-staged textures.

elishacloud commented 1 year ago

Ok, try this one. I have it disable AF only when a multi-stage texture is actually set. dxwrapper.zip

BC46 commented 1 year ago

Same result as with the last build. Could have something to do with the GPU.

elishacloud commented 1 year ago

Ok, can you try with one more update? dxwrapper.zip

Also, please upload your log files for both systems.

BC46 commented 1 year ago

Unfortunately still no AF on the GTX 1050 laptop as far as I could tell. Here are the logs: dxwrapper-freelancer-1050.log dxwrapper-freelancer-5700.log

elishacloud commented 1 year ago

Ok, I think this is the best we can get: dxwrapper.zip

Can you try this one and let me know how it works on both systems? Does AF work? Are there any missing textures? Etc.

Thanks for your help here!

BC46 commented 1 year ago

Alright, so on the RX 5700 PC all the textures loaded just fine, AA and AF both worked (same result as with the previous builds). On the GTX 1050 laptop however, the detail and emission textures appeared to be invisible again, as opposed to the last three builds. AA seemed to work fine though, but not AF.

elishacloud commented 1 year ago

Ok, that is not what I expected. I don't think there is anyway I can get the GTX 1050 to work with AF and support those textures on d3d9.

Try one more build. I hope this is the last: dxwrapper.zip

BC46 commented 1 year ago

With this build the emission and detail textures didn't work on the GTX 1050, neither did AF. Additionally, this time the AF option stopped working on the RX 5700 as well.

I don't think there is anyway I can get the GTX 1050 to work with AF and support those textures on d3d9.

Oh well, at least the detail and emission textures can be loaded, so that's definitely an improvement!

elishacloud commented 1 year ago

Too bad! I was hoping I would not need to make this more complicated. Ok, thanks for trying all these. I hope this one works correctly. It is similar to the second one I gave you.

Can you try this one: dxwrapper.zip

Note: the issue here is that I need to account for cube and volume textures, which makes it more complex. The first testing ones I gave you did not account for that.

BC46 commented 1 year ago

Alright, this is an interesting one. With the RX 5700, everything works fine. On the GTX 1050 however, the detail and emission textures seem to load at first, but after Alt-Tabbing and reloading the scene, they're no longer there. I didn't notice this behavior with the second build.

Edit: It's possible AF has been working on the GTX 1050 all along, just at a very low sample rate.

elishacloud commented 1 year ago

after Alt-Tabbing and reloading the scene, they're no longer there. I didn't notice this behavior with the second build.

Ok, I was just thinking I might need to reset it. Try this one to see if this is fixed: dxwrapper.zip

It's possible AF has been working on the GTX 1050 all along, just at a very low sample rate.

From the logs it looks like the GTX supports multi-stage AF for MinFiltering but not for MagFiltering. This could be why it looks like it is very low sample rate.

BC46 commented 1 year ago

Great, the issue has been fixed with the newest build. Tested it on both the RX 5700 and GTX 1050; couldn't find any new issues.

elishacloud commented 1 year ago

Ok, here is the final build. I integrated all the fixes in. If you don't mind testing one last time I would be grateful: dxwrapper.zip

BC46 commented 1 year ago

Of course! Seems to work ok on both systems.

elishacloud commented 1 year ago

Great! Thanks for all your help here!