Skip to content

Commit

Permalink
Move DirectX Texture2D initialization to after OpenVR Init
Browse files Browse the repository at this point in the history
This allows us to create the texture on the correct GPU.
  • Loading branch information
BenjaminZehowlt authored and Natsumi-sama committed Nov 13, 2023
1 parent e436492 commit 928422e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 37 deletions.
4 changes: 4 additions & 0 deletions Dotnet/Overlay/OffScreenBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public OffScreenBrowser(string address, int width, int height)

public void RenderToTexture(Texture2D texture)
{
// Safeguard against uninitialized texture
if (texture == null)
return;

_paintBufferLock.EnterReadLock();
try
{
Expand Down
45 changes: 22 additions & 23 deletions Dotnet/Overlay/VRCXVR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using System.Threading;
using CefSharp;
using SharpDX;
using SharpDX.Direct3D;
using SharpDX.Direct3D11;
using SharpDX.DXGI;
using Valve.VR;
Expand Down Expand Up @@ -82,30 +81,13 @@ public void Restart()
MainForm.Instance.Browser.ExecuteScriptAsync("console.log('VRCXVR Restarted');");
}

private void ThreadLoop()
private void SetupTextures()
{
var active = false;
var e = new VREvent_t();
var nextInit = DateTime.MinValue;
var nextDeviceUpdate = DateTime.MinValue;
var nextOverlay = DateTime.MinValue;
var overlayIndex = OpenVR.k_unTrackedDeviceIndexInvalid;
var overlayVisible1 = false;
var overlayVisible2 = false;
var dashboardHandle = 0UL;
var overlayHandle1 = 0UL;
var overlayHandle2 = 0UL;

// REMOVE THIS
// nextOverlay = DateTime.MaxValue;
// https://stackoverflow.com/questions/38312597/how-to-choose-a-specific-graphics-device-in-sharpdx-directx-11/38596725#38596725
Factory f = new Factory1();
var a = f.GetAdapter(1);

var flags = DeviceCreationFlags.BgraSupport;

_device = Program.GPUFix ? new Device(a, flags) : new Device(DriverType.Hardware, DeviceCreationFlags.SingleThreaded | DeviceCreationFlags.BgraSupport);
_device = new Device(f.GetAdapter(OpenVR.System.GetD3D9AdapterIndex()),
DeviceCreationFlags.SingleThreaded | DeviceCreationFlags.BgraSupport);

_texture1?.Dispose();
_texture1 = new Texture2D(
_device,
new Texture2DDescription
Expand All @@ -121,7 +103,8 @@ private void ThreadLoop()
CpuAccessFlags = CpuAccessFlags.Write
}
);


_texture2?.Dispose();
_texture2 = new Texture2D(
_device,
new Texture2DDescription
Expand All @@ -137,7 +120,22 @@ private void ThreadLoop()
CpuAccessFlags = CpuAccessFlags.Write
}
);
}

private void ThreadLoop()
{
var active = false;
var e = new VREvent_t();
var nextInit = DateTime.MinValue;
var nextDeviceUpdate = DateTime.MinValue;
var nextOverlay = DateTime.MinValue;
var overlayIndex = OpenVR.k_unTrackedDeviceIndexInvalid;
var overlayVisible1 = false;
var overlayVisible2 = false;
var dashboardHandle = 0UL;
var overlayHandle1 = 0UL;
var overlayHandle2 = 0UL;

_browser1 = new OffScreenBrowser(
"file://vrcx/vr.html?1",
512,
Expand Down Expand Up @@ -183,6 +181,7 @@ private void ThreadLoop()
}

active = true;
SetupTextures();
}

while (system.PollNextEvent(ref e, (uint)Marshal.SizeOf(e)))
Expand Down
11 changes: 0 additions & 11 deletions Dotnet/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public static class Program
public static string ConfigLocation;
public static string Version { get; private set; }
public static bool LaunchDebug;
public static bool GPUFix;
private static readonly NLog.Logger logger = NLog.LogManager.GetLogger("VRCX");
static Program()
{
Expand Down Expand Up @@ -143,7 +142,6 @@ private static void Run()
ProcessMonitor.Instance.Init();
SQLiteLegacy.Instance.Init();
VRCXStorage.Load();
LoadFromConfig();
CpuMonitor.Instance.Init();
Discord.Instance.Init();
WebApi.Instance.Init();
Expand All @@ -170,14 +168,5 @@ private static void Run()
SQLiteLegacy.Instance.Exit();
ProcessMonitor.Instance.Exit();
}

/// <summary>
/// Sets GPUFix to true if it is not already set and the VRCX_GPUFix key in the database is true.
/// </summary>
private static void LoadFromConfig()
{
if (!GPUFix)
GPUFix = VRCXStorage.Instance.Get("VRCX_GPUFix") == "true";
}
}
}
3 changes: 0 additions & 3 deletions Dotnet/StartupArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ public static void ArgsCheck()

foreach (var arg in args)
{
if (arg.Contains("--gpufix"))
Program.GPUFix = true;

if (arg.Length > 12 && arg.Substring(0, 12) == "/uri=vrcx://")
LaunchCommand = arg.Substring(12);

Expand Down

0 comments on commit 928422e

Please sign in to comment.