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.15k stars 82 forks source link

Reverse D3DDECLUSAGE_POSITIONT to normal D3DDECLUSAGE_POSITION #184

Closed dkollmann closed 1 month ago

dkollmann commented 1 year ago

Hey Elisha, I created a new issue since this is a really specific problem and I need your insights.

The issue is that RTX Remix does not support D3DDECLUSAGE_POSITIONT, due to that is needs the actual 3D scene to do all the raytracing. I now try to transform the viewspace coordinates back into world coordinates.

Could you take a look at my commit and let me know what you think? It does currently not work yet and is probably incomplete, but I think this is kind of the direction to go.

https://github.com/dkollmann/dxwrapper/commit/34d98e3f21565aaff3f661196c7d2557f63826bb

Thank you, Daniel

PatrickvL commented 1 year ago

@dkollmann perhaps it's wise to use a feature toggle for this?

elishacloud commented 1 year ago

Since the code is already inside the if (Config.Dd7to9) statement it will only run if Dd7to9 is enabled. And we pretty much always want to run it when Dd7to9 is enabled since any game that uses D3DDECLUSAGE_POSITION will need it converted in this case.

dkollmann commented 1 year ago

Thank you guys for your input. I will get to that once the code is actually working.

The current state is as follows:

I disable the SetTransform function. Everything renders and looks fine.

Then I change the FVF type from D3DFVF_XYZRHW to D3DFVF_XYZW, and make sure the vertex position W is set to 1.0. The overall range of Z positions is between -1.0 and 1.0.

However I do not see anything rendered. Do you know if there is more that I must change to switch from D3DFVF_XYZRHW to D3DFVF_XYZW, when there are no transforms applied?

Thank you

elishacloud commented 1 year ago

There are three things that need to be done when changing the FVF type from D3DFVF_XYZRHW to D3DFVF_XYZW.

  1. You need to convert the Vertex buffer array data from D3DFVF_XYZRHW to D3DFVF_XYZW. That will probably be the pVertexStreamZeroData sent to DrawIndexedPrimitiveUP().
  2. You need to call SetFVF() with the new FVF type.
  3. You need to call the Draw (usually the DrawIndexedPrimitiveUP()) function with the new data. Note, make sure that you also update the data size VertexStreamZeroStride parameter.

You can see all three of these items happening here.

PatrickvL commented 1 year ago

@dkollmann I added a comment to https://github.com/dkollmann/dxwrapper/commit/34d98e3f21565aaff3f661196c7d2557f63826bb

dkollmann commented 1 year ago

Thank you guys, instead of manipulating the original vertex buffer, I created one using POSITION_XYZ and copied the data, but the result is the same.

dkollmann commented 1 year ago

Here is the updated code. Could guys take a quick look, if there are any obvious issues in there? Thank you

Both REVERSE_POSITIONT_SIZE = 3 and 4 give the same behavior.

https://github.com/dkollmann/dxwrapper/commit/d9f7146ff6148972576ac1a5530bf5cf28a5e254?diff=split

I added the missing "vertex += stride;" to the loop for REVERSE_POSITIONT_SIZE == 4.

dkollmann commented 1 year ago

The good news is, when I export/capture the scene, it looks correct. So this is probably indeed a camera / view issue right now.

image

PatrickvL commented 1 year ago

The REVERSE_POSITIONT_SIZE == 3 case has a bug. That's probably why you went for SIZE 4? (Which still overwrites the client buffer.) So I commented a suggestion which should fix the SIZE 3 case.

Edit: Actually, the SIZE 4 case also has a bug, so whatever you've tested is not working as you'd want...

PatrickvL commented 1 year ago

With the other drawing API's that receive a User Pointer buffer and a given stride, there's also a potential case where the stride could be much much larger than the number of components actually used in the active vertex declaration. I know that's not the use case here, but at least be aware if you're going to cover those API's as well.

dkollmann commented 1 year ago

I am finally making some progress :)

image

elishacloud commented 1 year ago

Nice!

dkollmann commented 1 year ago

Okay, I got the game running in XYZW and XYZ mode.

Now my idea was to simply let the game set its projection matrix, and when I inverse transform all vertices with it, they should be transformed back to where they are now. The difference would be that when the scene is exported, it should look like the actual world we want to render. However everything goes back to black.

https://github.com/dkollmann/dxwrapper/commit/33e1cec9ec7469d54634f0d8c188ded420ddf537

elishacloud commented 1 year ago

I'm not quite sure what is happening. Have you tried using PIX to see what is happening?

dkollmann commented 1 year ago

It seems that neither PIX nor NVIDIA Nsight support 32-bit :(

An older Nsight version is available https://developer.nvidia.com/gameworksdownload#?dn=nvidia-nsight-graphics-2021-5

But it does not support D3D9 :(

elishacloud commented 1 year ago

If you install the DirectX9 SDK it has PIX built-in that works with DirectX9. It is located in the C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Utilities\bin\x86 folder. There is also a 64bit PIX located here: C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Utilities\bin\x64

dkollmann commented 1 year ago

Okay, I am finally at a point where I start to see things again.

However, the matrix is still not correct.

Could you guys take a look this commit and let me know if this seems correct to you?

https://github.com/dkollmann/dxwrapper/blob/dfc0857a33a55654c78a9651ea04acf92e520741/ddraw/IDirect3DDeviceX.cpp#L329

The idea is, that for RTX Remix to correctly reconstruct the camera for later raytracing, it has to be correctly set up. So the view matrix is the position and orientation of the camera in the world and the projection matrix are the properties of the camera itself, like fov and so on.

The world matrix is always identity here.

dkollmann commented 1 year ago

I am finally getting somewhere with the world reconstruction :)

image

elishacloud commented 1 month ago

I think this issue is resolved now. Reopen it if there is still an issue.