Working screen capture, but not with hidden windows. Need to make that a priority.

This commit is contained in:
jrhodnik 2019-05-23 23:24:40 -04:00
parent d07a2d5adc
commit 0c220316e8
1 changed files with 38 additions and 95 deletions

View File

@ -65,35 +65,14 @@ namespace VDash.Overlay
var adapter = factory.GetAdapter1(0); var adapter = factory.GetAdapter1(0);
//Get device from adapter //Get device from adapter
var device = new SharpDX.Direct3D11.Device(adapter); var device = new SharpDX.Direct3D11.Device(adapter);
//Get front buffer of the adapter
var output = adapter.GetOutput(0);
var output1 = output.QueryInterface<Output1>();
// Width/Height of desktop to capture
int width = output.Description.DesktopBounds.Right;
int height = output.Description.DesktopBounds.Bottom;
// Create Staging texture CPU-accessible
var textureDesc = new Texture2DDescription
{
CpuAccessFlags = CpuAccessFlags.Read,
BindFlags = BindFlags.None,
Format = Format.B8G8R8A8_UNorm,
Width = width,
Height = height,
OptionFlags = ResourceOptionFlags.None,
MipLevels = 1,
ArraySize = 1,
SampleDescription = { Count = 1, Quality = 0 },
Usage = ResourceUsage.Staging
};
var screenTexture = new Texture2D(device, textureDesc);
#endregion #endregion
int frames = 0; int frames = 0;
DateTime lastFps = DateTime.MinValue; DateTime lastFps = DateTime.MinValue;
while (m_Running) while (m_Running)
{ {
#region VR Events
VREvent_t vrEvent = new VREvent_t(); VREvent_t vrEvent = new VREvent_t();
while (OpenVR.Overlay.PollNextOverlayEvent(m_Handle, ref vrEvent, (uint)Marshal.SizeOf(vrEvent))) while (OpenVR.Overlay.PollNextOverlayEvent(m_Handle, ref vrEvent, (uint)Marshal.SizeOf(vrEvent)))
@ -126,49 +105,43 @@ namespace VDash.Overlay
} }
} }
#endregion
#region Acutally Render #region Acutally Render
using (var duplicatedOutput = output1.DuplicateOutput(device)) var proc = Process.GetProcessesByName("discord")[1];
var rect = new User32.Rect();
User32.GetWindowRect(proc.MainWindowHandle, ref rect);
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
using (var bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb))
{ {
SharpDX.DXGI.Resource screenResource = null; using (Graphics graphics = Graphics.FromImage(bmp))
try
{ {
OutputDuplicateFrameInformation duplicateFrameInformation; graphics.CopyFromScreen(rect.left, rect.top, 0, 0, new Size(width, height), CopyPixelOperation.SourceCopy);
// Try to get duplicated frame within given time is ms var data = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
// Create Staging texture CPU-accessible
duplicatedOutput.AcquireNextFrame(10000, out duplicateFrameInformation, out screenResource); var textureDesc = new Texture2DDescription
{
CpuAccessFlags = CpuAccessFlags.Read,
BindFlags = BindFlags.None,
#region junk Format = Format.B8G8R8A8_UNorm,
// copy resource into memory that can be accessed by the CPU Width = width,
//using (var screenTexture2D = screenResource.QueryInterface<Texture2D>()) Height = height,
//{ OptionFlags = ResourceOptionFlags.None,
// device.ImmediateContext.CopyResource(screenTexture2D, screenTexture); MipLevels = 1,
//} ArraySize = 1,
SampleDescription = { Count = 1, Quality = 0 },
// Get the desktop capture texture Usage = ResourceUsage.Staging
var mapSource = device.ImmediateContext.MapSubresource(screenTexture, 0, MapMode.Read, SharpDX.Direct3D11.MapFlags.None); };
// Create Drawing.Bitmap using (var screenTexture = new Texture2D(device, textureDesc, new SharpDX.DataRectangle(data.Scan0, data.Stride)))
//using (var bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb)) //using (var screenTexture = new Texture2D(device, textureDesc))
//{
// var boundsRect = new Rectangle(0, 0, width, height);
// // Copy pixels from screen capture Texture to GDI bitmap
// var mapDest = bitmap.LockBits(boundsRect, ImageLockMode.WriteOnly, bitmap.PixelFormat);
// var sourcePtr = mapSource.DataPointer;
// var destPtr = mapDest.Scan0;
if (duplicateFrameInformation.AccumulatedFrames > 0)
{ {
//Console.WriteLine($"Frames: {duplicateFrameInformation.AccumulatedFrames}");
var texture = new Texture_t(); var texture = new Texture_t();
texture.handle = screenTexture.NativePointer; texture.handle = screenTexture.NativePointer;
texture.eType = ETextureType.DirectX; texture.eType = ETextureType.DirectX;
@ -178,51 +151,21 @@ namespace VDash.Overlay
{ {
Console.WriteLine($"Error rendering: {result}"); Console.WriteLine($"Error rendering: {result}");
} }
frames++; frames++;
bmp.UnlockBits(data);
} }
//Dispose not freeing screenTexture: https://gamedev.stackexchange.com/questions/93311/freeing-texture-memory-in-sharpdx-what-am-i-missing
//for (int y = 0; y < height; y++) device.ImmediateContext.ClearState();
//{ device.ImmediateContext.Flush();
// Copy a single line
// Utilities.CopyMemory(destPtr, sourcePtr, width * 4);
// Advance pointers
// sourcePtr = IntPtr.Add(sourcePtr, mapSource.RowPitch);
// destPtr = IntPtr.Add(destPtr, mapDest.Stride);
//}
//// // Release source and dest locks
//bitmap.UnlockBits(mapDest);
device.ImmediateContext.UnmapSubresource(screenTexture, 0);
//using (var ms = new FileStream(@"C:\Users\jrhodnik\Desktop\ss.bmp", FileMode.Create))
//{
// bitmap.Save(ms, ImageFormat.Bmp);
//}
//}
#endregion
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
if (screenResource != null)
{
screenResource.Dispose();
}
duplicatedOutput.ReleaseFrame();
} }
} }
#endregion #endregion
//Thread.Sleep(20); Thread.Sleep(10);
if(DateTime.Now.Subtract(lastFps).TotalMilliseconds > 1000) if (DateTime.Now.Subtract(lastFps).TotalMilliseconds > 1000)
{ {
Console.WriteLine($"{frames} fps."); Console.WriteLine($"{frames} fps.");
frames = 0; frames = 0;