Skip to content

Commit

Permalink
Changed: Always change pools and usages in d3d9ex mode.
Browse files Browse the repository at this point in the history
Fixes ReShade, maybe other things.
  • Loading branch information
Sewer56 committed Jul 2, 2021
1 parent 253f770 commit 59ddda0
Showing 1 changed file with 7 additions and 23 deletions.
30 changes: 7 additions & 23 deletions Riders.Tweakbox/Controllers/Direct3DController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public Direct3DController(TweakboxConfig config, IReloadedHooks hooks)
_config = config;
var dx9Hook = Sewer56.SonicRiders.API.Misc.DX9Hook.Value;
_createDeviceHook = dx9Hook.Direct3D9VTable.CreateFunctionHook<DX9Hook.CreateDevice>((int)IDirect3D9.CreateDevice, CreateDeviceHook).Activate();

_createTextureHook = dx9Hook.DeviceVTable.CreateFunctionHook<CreateTexture>((int)IDirect3DDevice9.CreateTexture, CreateTextureHook).Activate();
_createVertexBufferHook = dx9Hook.DeviceVTable.CreateFunctionHook<CreateVertexBuffer>((int)IDirect3DDevice9.CreateVertexBuffer, CreateVertexBufferHook).Activate();
_createIndexBufferHook = dx9Hook.DeviceVTable.CreateFunctionHook<CreateIndexBuffer>((int)IDirect3DDevice9.CreateIndexBuffer, CreateIndexBufferHook).Activate();
Expand Down Expand Up @@ -100,7 +99,10 @@ private IntPtr CreateDeviceHook(IntPtr direct3dpointer, uint adapter, DeviceType
{
// Do not edit if does not belong to Riders.
if (D3dEx == null || direct3dpointer != D3dEx.NativePointer)
{
Log.WriteLine($"[{nameof(CreateDeviceHook)}] Not Riders Device, Ignoring");
return _createDeviceHook.OriginalFunction(direct3dpointer, adapter, deviceType, hFocusWindow, behaviorFlags, ref presentParameters, ppReturnedDeviceInterface);
}

if (_config.Data.D3DDeviceFlags)
{
Expand Down Expand Up @@ -152,7 +154,10 @@ private IntPtr CreateDeviceHook(IntPtr direct3dpointer, uint adapter, DeviceType
private IntPtr ResetHook(IntPtr device, ref PresentParameters presentparameters)
{
if (D3dDeviceEx == null || D3dDeviceEx.NativePointer != device)
{
Log.WriteLine($"[{nameof(ResetHook)}] Not Riders Device, Ignoring");
return _resetHook.OriginalFunction(device, ref presentparameters);
}

SetPresentParameters(ref presentparameters);
LastPresentParameters = presentparameters;
Expand Down Expand Up @@ -187,64 +192,44 @@ private void SetPresentParameters(ref PresentParameters presentParameters)
#region Hooks for D3D9Ex Support
private IntPtr CreateTextureHook(IntPtr devicePointer, int width, int height, int miplevels, Usage usage, Format format, Pool pool, void** pptexture, void* sharedhandle)
{
if (D3dDeviceEx == null || D3dDeviceEx.NativePointer != devicePointer)
return _createTextureHook.OriginalFunction(devicePointer, width, height, miplevels, usage, format, pool, pptexture, sharedhandle);

usage |= (pool == Pool.Managed ? Usage.Dynamic : 0);
pool = (pool == Pool.Managed) ? Pool.Default : pool;
return _createTextureHook.OriginalFunction(devicePointer, width, height, miplevels, usage, format, pool, pptexture, sharedhandle);
}

private IntPtr CreateVertexBufferHook(IntPtr devicePointer, uint length, Usage usage, VertexFormat format, Pool pool, void** ppvertexbuffer, void* psharedhandle)
{
if (D3dDeviceEx == null || D3dDeviceEx.NativePointer != devicePointer)
return _createVertexBufferHook.OriginalFunction(devicePointer, length, usage, format, pool, ppvertexbuffer, psharedhandle);

usage |= (pool == Pool.Managed ? Usage.Dynamic : 0);
pool = (pool == Pool.Managed) ? Pool.Default : pool;
return _createVertexBufferHook.OriginalFunction(devicePointer, length, usage, format, pool, ppvertexbuffer, psharedhandle);
}

private IntPtr CreateIndexBufferHook(IntPtr devicePointer, uint length, Usage usage, Format format, Pool pool, void** ppindexbuffer, void* psharedhandle)
{
if (D3dDeviceEx == null || D3dDeviceEx.NativePointer != devicePointer)
return _createIndexBufferHook.OriginalFunction(devicePointer, length, usage, format, pool, ppindexbuffer, psharedhandle);

usage |= (pool == Pool.Managed ? Usage.Dynamic : 0);
pool = (pool == Pool.Managed) ? Pool.Default : pool;

return _createIndexBufferHook.OriginalFunction(devicePointer, length, usage, format, pool, ppindexbuffer, psharedhandle);
}


#endregion

#region D3D9Ex: These APIs aren't used but just in case!!
private IntPtr CreateVolumeTextureHook(IntPtr devicepointer, int width, int height, int depth, int miplevels, Usage usage, Format format, Pool pool, void** pptexture, void* sharedhandle)
{
if (D3dDeviceEx == null || D3dDeviceEx.NativePointer != devicepointer)
return _createVolumeTextureHook.OriginalFunction(devicepointer, width, height, depth, miplevels, usage, format, pool, pptexture, sharedhandle);

usage |= (pool == Pool.Managed ? Usage.Dynamic : 0);
pool = (pool == Pool.Managed) ? Pool.Default : pool;
return _createVolumeTextureHook.OriginalFunction(devicepointer, width, height, depth, miplevels, usage, format, pool, pptexture, sharedhandle);
}

private IntPtr CreateCubeTextureHook(IntPtr devicepointer, int edgelength, int levels, Usage usage, Format format, Pool pool, void** pptexture, void* sharedhandle)
{
if (D3dDeviceEx == null || D3dDeviceEx.NativePointer != devicepointer)
return _createCubeTextureHook.OriginalFunction(devicepointer, edgelength, levels, usage, format, pool, pptexture, sharedhandle);

{
usage |= (pool == Pool.Managed ? Usage.Dynamic : 0);
pool = (pool == Pool.Managed) ? Pool.Default : pool;
return _createCubeTextureHook.OriginalFunction(devicepointer, edgelength, levels, usage, format, pool, pptexture, sharedhandle);
}

private IntPtr CreateOffscreenPlainSurfaceHook(IntPtr devicepointer, int width, int height, Format format, Pool pool, void** ppsurface, void* sharedhandle)
{
if (D3dDeviceEx == null || D3dDeviceEx.NativePointer != devicepointer)
return _createOffscreenPlainSurfaceHook.OriginalFunction(devicepointer, width, height, format, pool, ppsurface, sharedhandle);

pool = (pool == Pool.Managed) ? Pool.Default : pool;
return _createOffscreenPlainSurfaceHook.OriginalFunction(devicepointer, width, height, format, pool, ppsurface, sharedhandle);
}
Expand All @@ -270,6 +255,5 @@ private IntPtr CreateOffscreenPlainSurfaceHook(IntPtr devicepointer, int width,

[Function(CallingConventions.Stdcall)]
public unsafe delegate IntPtr CreateOffscreenPlainSurface(IntPtr devicePointer, int width, int height, Format format, Pool pool, void** ppSurface, void* sharedHandle);

}
}

0 comments on commit 59ddda0

Please sign in to comment.