mirror of
https://github.com/DarthAffe/ScreenCapture.NET.git
synced 2025-12-13 05:48:39 +00:00
Fixed and simplified textures; Updated Vortice
This commit is contained in:
parent
6237578059
commit
6a9ddf6462
@ -10,7 +10,6 @@ using Vortice.DXGI;
|
|||||||
using Vortice.Mathematics;
|
using Vortice.Mathematics;
|
||||||
using MapFlags = Vortice.Direct3D11.MapFlags;
|
using MapFlags = Vortice.Direct3D11.MapFlags;
|
||||||
using ResultCode = Vortice.DXGI.ResultCode;
|
using ResultCode = Vortice.DXGI.ResultCode;
|
||||||
using Usage = Vortice.Direct3D11.Usage;
|
|
||||||
|
|
||||||
namespace ScreenCapture.NET
|
namespace ScreenCapture.NET
|
||||||
{
|
{
|
||||||
@ -31,6 +30,8 @@ namespace ScreenCapture.NET
|
|||||||
FeatureLevel.Level_10_0
|
FeatureLevel.Level_10_0
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private const int BPP = 4;
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Properties & Fields
|
#region Properties & Fields
|
||||||
@ -179,7 +180,13 @@ namespace ScreenCapture.NET
|
|||||||
MappedSubresource mapSource = _context.Map(stagingTexture, 0, MapMode.Read, MapFlags.None);
|
MappedSubresource mapSource = _context.Map(stagingTexture, 0, MapMode.Read, MapFlags.None);
|
||||||
IntPtr sourcePtr = mapSource.DataPointer;
|
IntPtr sourcePtr = mapSource.DataPointer;
|
||||||
lock (captureZone.Buffer)
|
lock (captureZone.Buffer)
|
||||||
Marshal.Copy(sourcePtr, captureZone.Buffer, 0, captureZone.Buffer.Length);
|
{
|
||||||
|
for (int y = 0; y < captureZone.Height; y++)
|
||||||
|
{
|
||||||
|
Marshal.Copy(sourcePtr, captureZone.Buffer, y * captureZone.Stride, captureZone.Stride);
|
||||||
|
sourcePtr += mapSource.RowPitch;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_context.Unmap(stagingTexture, 0);
|
_context.Unmap(stagingTexture, 0);
|
||||||
captureZone.SetUpdated();
|
captureZone.SetUpdated();
|
||||||
@ -199,9 +206,6 @@ namespace ScreenCapture.NET
|
|||||||
if ((x + width) > Display.Width) throw new ArgumentException("x + width > Display width");
|
if ((x + width) > Display.Width) throw new ArgumentException("x + width > Display width");
|
||||||
if ((y + height) > Display.Height) throw new ArgumentException("y + height > Display height");
|
if ((y + height) > Display.Height) throw new ArgumentException("y + height > Display height");
|
||||||
|
|
||||||
int textureWidth = (int)Math.Ceiling(width / 32.0) * 32;
|
|
||||||
int textureHeight = (int)Math.Ceiling(height / 32.0) * 32;
|
|
||||||
|
|
||||||
int unscaledWidth = width;
|
int unscaledWidth = width;
|
||||||
int unscaledHeight = height;
|
int unscaledHeight = height;
|
||||||
if (downscaleLevel > 0)
|
if (downscaleLevel > 0)
|
||||||
@ -214,12 +218,9 @@ namespace ScreenCapture.NET
|
|||||||
if (width < 1) width = 1;
|
if (width < 1) width = 1;
|
||||||
if (height < 1) height = 1;
|
if (height < 1) height = 1;
|
||||||
|
|
||||||
int bufferWidth = (int)Math.Ceiling(width / 32.0) * 32;
|
byte[] buffer = new byte[width * height * 4];
|
||||||
int bufferHeight = (int)Math.Ceiling(height / 32.0) * 32;
|
|
||||||
|
|
||||||
byte[] buffer = new byte[bufferWidth * bufferHeight * 4];
|
CaptureZone captureZone = new(_indexCounter++, x, y, width, height, BPP, downscaleLevel, unscaledWidth, unscaledHeight, buffer);
|
||||||
|
|
||||||
CaptureZone captureZone = new(_indexCounter++, x, y, width, height, downscaleLevel, unscaledWidth, unscaledHeight, textureWidth, textureHeight, bufferWidth, bufferHeight, buffer);
|
|
||||||
lock (_captureZones)
|
lock (_captureZones)
|
||||||
InitializeCaptureZone(captureZone);
|
InitializeCaptureZone(captureZone);
|
||||||
|
|
||||||
@ -252,13 +253,13 @@ namespace ScreenCapture.NET
|
|||||||
CpuAccessFlags = CpuAccessFlags.Read,
|
CpuAccessFlags = CpuAccessFlags.Read,
|
||||||
BindFlags = BindFlags.None,
|
BindFlags = BindFlags.None,
|
||||||
Format = Format.B8G8R8A8_UNorm,
|
Format = Format.B8G8R8A8_UNorm,
|
||||||
Width = captureZone.BufferWidth,
|
Width = captureZone.Width,
|
||||||
Height = captureZone.BufferHeight,
|
Height = captureZone.Height,
|
||||||
OptionFlags = ResourceOptionFlags.None,
|
OptionFlags = ResourceOptionFlags.None,
|
||||||
MipLevels = 1,
|
MipLevels = 1,
|
||||||
ArraySize = 1,
|
ArraySize = 1,
|
||||||
SampleDescription = { Count = 1, Quality = 0 },
|
SampleDescription = { Count = 1, Quality = 0 },
|
||||||
Usage = Usage.Staging
|
Usage = ResourceUsage.Staging
|
||||||
};
|
};
|
||||||
ID3D11Texture2D stagingTexture = _device!.CreateTexture2D(stagingTextureDesc);
|
ID3D11Texture2D stagingTexture = _device!.CreateTexture2D(stagingTextureDesc);
|
||||||
|
|
||||||
@ -271,13 +272,13 @@ namespace ScreenCapture.NET
|
|||||||
CpuAccessFlags = CpuAccessFlags.None,
|
CpuAccessFlags = CpuAccessFlags.None,
|
||||||
BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,
|
BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,
|
||||||
Format = Format.B8G8R8A8_UNorm,
|
Format = Format.B8G8R8A8_UNorm,
|
||||||
Width = captureZone.CaptureWidth,
|
Width = captureZone.UnscaledWidth,
|
||||||
Height = captureZone.CaptureHeight,
|
Height = captureZone.UnscaledHeight,
|
||||||
OptionFlags = ResourceOptionFlags.GenerateMips,
|
OptionFlags = ResourceOptionFlags.GenerateMips,
|
||||||
MipLevels = captureZone.DownscaleLevel + 1,
|
MipLevels = captureZone.DownscaleLevel + 1,
|
||||||
ArraySize = 1,
|
ArraySize = 1,
|
||||||
SampleDescription = { Count = 1, Quality = 0 },
|
SampleDescription = { Count = 1, Quality = 0 },
|
||||||
Usage = Usage.Default
|
Usage = ResourceUsage.Default
|
||||||
};
|
};
|
||||||
scalingTexture = _device!.CreateTexture2D(scalingTextureDesc);
|
scalingTexture = _device!.CreateTexture2D(scalingTextureDesc);
|
||||||
scalingTextureView = _device.CreateShaderResourceView(scalingTexture);
|
scalingTextureView = _device.CreateShaderResourceView(scalingTexture);
|
||||||
@ -302,7 +303,7 @@ namespace ScreenCapture.NET
|
|||||||
_context = _device.ImmediateContext;
|
_context = _device.ImmediateContext;
|
||||||
|
|
||||||
_output = adapter.GetOutput(Display.Index);
|
_output = adapter.GetOutput(Display.Index);
|
||||||
using IDXGIOutput5 output1 = _output.QueryInterface<IDXGIOutput5>();
|
using IDXGIOutput5 output = _output.QueryInterface<IDXGIOutput5>();
|
||||||
|
|
||||||
Texture2DDescription captureTextureDesc = new()
|
Texture2DDescription captureTextureDesc = new()
|
||||||
{
|
{
|
||||||
@ -315,7 +316,7 @@ namespace ScreenCapture.NET
|
|||||||
MipLevels = 1,
|
MipLevels = 1,
|
||||||
ArraySize = 1,
|
ArraySize = 1,
|
||||||
SampleDescription = { Count = 1, Quality = 0 },
|
SampleDescription = { Count = 1, Quality = 0 },
|
||||||
Usage = Usage.Default
|
Usage = ResourceUsage.Default
|
||||||
};
|
};
|
||||||
_captureTexture = _device.CreateTexture2D(captureTextureDesc);
|
_captureTexture = _device.CreateTexture2D(captureTextureDesc);
|
||||||
|
|
||||||
@ -325,7 +326,7 @@ namespace ScreenCapture.NET
|
|||||||
InitializeCaptureZone(captureZone);
|
InitializeCaptureZone(captureZone);
|
||||||
}
|
}
|
||||||
|
|
||||||
_duplicatedOutput = output1.DuplicateOutput1(_device, Format.B8G8R8A8_UNorm); // DarthAffe 27.02.2021: This prepares for the use of 10bit color depth
|
_duplicatedOutput = output.DuplicateOutput1(_device, Format.B8G8R8A8_UNorm); // DarthAffe 27.02.2021: This prepares for the use of 10bit color depth
|
||||||
}
|
}
|
||||||
catch { Dispose(false); }
|
catch { Dispose(false); }
|
||||||
}
|
}
|
||||||
|
|||||||
@ -78,11 +78,10 @@ namespace ScreenCapture.NET
|
|||||||
private int CalculateTop()
|
private int CalculateTop()
|
||||||
{
|
{
|
||||||
int threshold = Threshold;
|
int threshold = Threshold;
|
||||||
int stride = _captureZone.BufferWidth * 4;
|
int stride = _captureZone.Stride;
|
||||||
int bytesPerRow = _captureZone.Width * 4;
|
|
||||||
for (int row = 0; row < _captureZone.Height; row++)
|
for (int row = 0; row < _captureZone.Height; row++)
|
||||||
{
|
{
|
||||||
Span<byte> data = new(_captureZone.Buffer, row * stride, bytesPerRow);
|
Span<byte> data = new(_captureZone.Buffer, row * stride, stride);
|
||||||
for (int i = 0; i < data.Length; i += 4)
|
for (int i = 0; i < data.Length; i += 4)
|
||||||
if ((data[i] > threshold) || (data[i + 1] > threshold) || (data[i + 2] > threshold))
|
if ((data[i] > threshold) || (data[i + 1] > threshold) || (data[i + 2] > threshold))
|
||||||
return row;
|
return row;
|
||||||
@ -94,11 +93,10 @@ namespace ScreenCapture.NET
|
|||||||
private int CalculateBottom()
|
private int CalculateBottom()
|
||||||
{
|
{
|
||||||
int threshold = Threshold;
|
int threshold = Threshold;
|
||||||
int stride = _captureZone.BufferWidth * 4;
|
int stride = _captureZone.Stride;
|
||||||
int bytesPerRow = _captureZone.Width * 4;
|
|
||||||
for (int row = _captureZone.Height; row >= 0; row--)
|
for (int row = _captureZone.Height; row >= 0; row--)
|
||||||
{
|
{
|
||||||
Span<byte> data = new(_captureZone.Buffer, row * stride, bytesPerRow);
|
Span<byte> data = new(_captureZone.Buffer, row * stride, stride);
|
||||||
for (int i = 0; i < data.Length; i += 4)
|
for (int i = 0; i < data.Length; i += 4)
|
||||||
if ((data[i] > threshold) || (data[i + 1] > threshold) || (data[i + 2] > threshold))
|
if ((data[i] > threshold) || (data[i + 1] > threshold) || (data[i + 2] > threshold))
|
||||||
return _captureZone.Height - row;
|
return _captureZone.Height - row;
|
||||||
@ -110,7 +108,7 @@ namespace ScreenCapture.NET
|
|||||||
private int CalculateLeft()
|
private int CalculateLeft()
|
||||||
{
|
{
|
||||||
int threshold = Threshold;
|
int threshold = Threshold;
|
||||||
int stride = _captureZone.BufferWidth * 4;
|
int stride = _captureZone.Stride;
|
||||||
byte[] buffer = _captureZone.Buffer;
|
byte[] buffer = _captureZone.Buffer;
|
||||||
for (int column = 0; column < _captureZone.Width; column++)
|
for (int column = 0; column < _captureZone.Width; column++)
|
||||||
for (int row = 0; row < _captureZone.Height; row++)
|
for (int row = 0; row < _captureZone.Height; row++)
|
||||||
@ -125,7 +123,7 @@ namespace ScreenCapture.NET
|
|||||||
private int CalculateRight()
|
private int CalculateRight()
|
||||||
{
|
{
|
||||||
int threshold = Threshold;
|
int threshold = Threshold;
|
||||||
int stride = _captureZone.BufferWidth * 4;
|
int stride = _captureZone.Stride;
|
||||||
byte[] buffer = _captureZone.Buffer;
|
byte[] buffer = _captureZone.Buffer;
|
||||||
for (int column = _captureZone.Width; column >= 0; column--)
|
for (int column = _captureZone.Width; column >= 0; column--)
|
||||||
for (int row = 0; row < _captureZone.Height; row++)
|
for (int row = 0; row < _captureZone.Height; row++)
|
||||||
|
|||||||
@ -52,26 +52,14 @@ namespace ScreenCapture.NET
|
|||||||
public int UnscaledHeight { get; }
|
public int UnscaledHeight { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the actually captured width of the region (this can be greated than <see cref="Width"/> due to size-constraints on the GPU).
|
/// Gets the amount of bytes per pixel in the image (most likely 3 [RGB] or 4 [ARGB]).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int CaptureWidth { get; }
|
public int BytesPerPixel { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the actually captured height of the region (this can be greated than <see cref="Height"/> due to size-constraints on the GPU).
|
/// Gets the size in bytes of a row in the region (<see cref="Width"/> * <see cref="BytesPerPixel"/>).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int CaptureHeight { get; }
|
public int Stride => Width * BytesPerPixel;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the width of the buffer the capture is saved to.
|
|
||||||
/// Equals <see cref="CaptureWidth"/> most of the time but can be bigger.
|
|
||||||
/// </summary>
|
|
||||||
public int BufferWidth { get; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the height of the buffer the capture is saved to.
|
|
||||||
/// Equals <see cref="CaptureHeight"/> most of the time but can be bigger.
|
|
||||||
/// </summary>
|
|
||||||
public int BufferHeight { get; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the buffer containing the image data. Format depends on the specific capture but is most likely BGRA32.
|
/// Gets the buffer containing the image data. Format depends on the specific capture but is most likely BGRA32.
|
||||||
@ -117,25 +105,18 @@ namespace ScreenCapture.NET
|
|||||||
/// <param name="downscaleLevel">The level of downscaling applied to the image of this region before copying to local memory.</param>
|
/// <param name="downscaleLevel">The level of downscaling applied to the image of this region before copying to local memory.</param>
|
||||||
/// <param name="unscaledWidth">The original width of the region.</param>
|
/// <param name="unscaledWidth">The original width of the region.</param>
|
||||||
/// <param name="unscaledHeight">The original height of the region</param>
|
/// <param name="unscaledHeight">The original height of the region</param>
|
||||||
/// <param name="captureWidth">The actually captured width of the region.</param>
|
|
||||||
/// <param name="captureHeight">The actually captured height of the region.</param>
|
|
||||||
/// <param name="bufferWidth">The width of the buffer the capture is saved to.</param>
|
|
||||||
/// <param name="bufferHeight">The height of the buffer the capture is saved to.</param>
|
|
||||||
/// <param name="buffer">The buffer containing the image data.</param>
|
/// <param name="buffer">The buffer containing the image data.</param>
|
||||||
public CaptureZone(int id, int x, int y, int width, int height, int downscaleLevel, int unscaledWidth, int unscaledHeight, int captureWidth, int captureHeight, int bufferWidth, int bufferHeight, byte[] buffer)
|
internal CaptureZone(int id, int x, int y, int width, int height, int bytesPerPixel, int downscaleLevel, int unscaledWidth, int unscaledHeight, byte[] buffer)
|
||||||
{
|
{
|
||||||
this.Id = id;
|
this.Id = id;
|
||||||
this.X = x;
|
this.X = x;
|
||||||
this.Y = y;
|
this.Y = y;
|
||||||
this.Width = width;
|
this.Width = width;
|
||||||
this.Height = height;
|
this.Height = height;
|
||||||
|
this.BytesPerPixel = bytesPerPixel;
|
||||||
this.UnscaledWidth = unscaledWidth;
|
this.UnscaledWidth = unscaledWidth;
|
||||||
this.UnscaledHeight = unscaledHeight;
|
this.UnscaledHeight = unscaledHeight;
|
||||||
this.DownscaleLevel = downscaleLevel;
|
this.DownscaleLevel = downscaleLevel;
|
||||||
this.CaptureWidth = captureWidth;
|
|
||||||
this.CaptureHeight = captureHeight;
|
|
||||||
this.BufferWidth = bufferWidth;
|
|
||||||
this.BufferHeight = bufferHeight;
|
|
||||||
this.Buffer = buffer;
|
this.Buffer = buffer;
|
||||||
|
|
||||||
BlackBars = new BlackBarDetection(this);
|
BlackBars = new BlackBarDetection(this);
|
||||||
|
|||||||
@ -60,7 +60,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Vortice.Direct3D11" Version="1.9.65" />
|
<PackageReference Include="Vortice.Direct3D11" Version="1.9.87" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user