Skip to content

Commit 6a9ddf6

Browse files
committed
Fixed and simplified textures; Updated Vortice
1 parent 6237578 commit 6a9ddf6

File tree

4 files changed

+34
-54
lines changed

4 files changed

+34
-54
lines changed

ScreenCapture.NET/DirectX/DX11ScreenCapture.cs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using Vortice.Mathematics;
1111
using MapFlags = Vortice.Direct3D11.MapFlags;
1212
using ResultCode = Vortice.DXGI.ResultCode;
13-
using Usage = Vortice.Direct3D11.Usage;
1413

1514
namespace ScreenCapture.NET
1615
{
@@ -31,6 +30,8 @@ public sealed class DX11ScreenCapture : IScreenCapture
3130
FeatureLevel.Level_10_0
3231
};
3332

33+
private const int BPP = 4;
34+
3435
#endregion
3536

3637
#region Properties & Fields
@@ -179,7 +180,13 @@ private void UpdateZones()
179180
MappedSubresource mapSource = _context.Map(stagingTexture, 0, MapMode.Read, MapFlags.None);
180181
IntPtr sourcePtr = mapSource.DataPointer;
181182
lock (captureZone.Buffer)
182-
Marshal.Copy(sourcePtr, captureZone.Buffer, 0, captureZone.Buffer.Length);
183+
{
184+
for (int y = 0; y < captureZone.Height; y++)
185+
{
186+
Marshal.Copy(sourcePtr, captureZone.Buffer, y * captureZone.Stride, captureZone.Stride);
187+
sourcePtr += mapSource.RowPitch;
188+
}
189+
}
183190

184191
_context.Unmap(stagingTexture, 0);
185192
captureZone.SetUpdated();
@@ -199,9 +206,6 @@ public CaptureZone RegisterCaptureZone(int x, int y, int width, int height, int
199206
if ((x + width) > Display.Width) throw new ArgumentException("x + width > Display width");
200207
if ((y + height) > Display.Height) throw new ArgumentException("y + height > Display height");
201208

202-
int textureWidth = (int)Math.Ceiling(width / 32.0) * 32;
203-
int textureHeight = (int)Math.Ceiling(height / 32.0) * 32;
204-
205209
int unscaledWidth = width;
206210
int unscaledHeight = height;
207211
if (downscaleLevel > 0)
@@ -214,12 +218,9 @@ public CaptureZone RegisterCaptureZone(int x, int y, int width, int height, int
214218
if (width < 1) width = 1;
215219
if (height < 1) height = 1;
216220

217-
int bufferWidth = (int)Math.Ceiling(width / 32.0) * 32;
218-
int bufferHeight = (int)Math.Ceiling(height / 32.0) * 32;
221+
byte[] buffer = new byte[width * height * 4];
219222

220-
byte[] buffer = new byte[bufferWidth * bufferHeight * 4];
221-
222-
CaptureZone captureZone = new(_indexCounter++, x, y, width, height, downscaleLevel, unscaledWidth, unscaledHeight, textureWidth, textureHeight, bufferWidth, bufferHeight, buffer);
223+
CaptureZone captureZone = new(_indexCounter++, x, y, width, height, BPP, downscaleLevel, unscaledWidth, unscaledHeight, buffer);
223224
lock (_captureZones)
224225
InitializeCaptureZone(captureZone);
225226

@@ -252,13 +253,13 @@ private void InitializeCaptureZone(in CaptureZone captureZone)
252253
CpuAccessFlags = CpuAccessFlags.Read,
253254
BindFlags = BindFlags.None,
254255
Format = Format.B8G8R8A8_UNorm,
255-
Width = captureZone.BufferWidth,
256-
Height = captureZone.BufferHeight,
256+
Width = captureZone.Width,
257+
Height = captureZone.Height,
257258
OptionFlags = ResourceOptionFlags.None,
258259
MipLevels = 1,
259260
ArraySize = 1,
260261
SampleDescription = { Count = 1, Quality = 0 },
261-
Usage = Usage.Staging
262+
Usage = ResourceUsage.Staging
262263
};
263264
ID3D11Texture2D stagingTexture = _device!.CreateTexture2D(stagingTextureDesc);
264265

@@ -271,13 +272,13 @@ private void InitializeCaptureZone(in CaptureZone captureZone)
271272
CpuAccessFlags = CpuAccessFlags.None,
272273
BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,
273274
Format = Format.B8G8R8A8_UNorm,
274-
Width = captureZone.CaptureWidth,
275-
Height = captureZone.CaptureHeight,
275+
Width = captureZone.UnscaledWidth,
276+
Height = captureZone.UnscaledHeight,
276277
OptionFlags = ResourceOptionFlags.GenerateMips,
277278
MipLevels = captureZone.DownscaleLevel + 1,
278279
ArraySize = 1,
279280
SampleDescription = { Count = 1, Quality = 0 },
280-
Usage = Usage.Default
281+
Usage = ResourceUsage.Default
281282
};
282283
scalingTexture = _device!.CreateTexture2D(scalingTextureDesc);
283284
scalingTextureView = _device.CreateShaderResourceView(scalingTexture);
@@ -302,7 +303,7 @@ public void Restart()
302303
_context = _device.ImmediateContext;
303304

304305
_output = adapter.GetOutput(Display.Index);
305-
using IDXGIOutput5 output1 = _output.QueryInterface<IDXGIOutput5>();
306+
using IDXGIOutput5 output = _output.QueryInterface<IDXGIOutput5>();
306307

307308
Texture2DDescription captureTextureDesc = new()
308309
{
@@ -315,7 +316,7 @@ public void Restart()
315316
MipLevels = 1,
316317
ArraySize = 1,
317318
SampleDescription = { Count = 1, Quality = 0 },
318-
Usage = Usage.Default
319+
Usage = ResourceUsage.Default
319320
};
320321
_captureTexture = _device.CreateTexture2D(captureTextureDesc);
321322

@@ -324,8 +325,8 @@ public void Restart()
324325
foreach (CaptureZone captureZone in captureZones)
325326
InitializeCaptureZone(captureZone);
326327
}
327-
328-
_duplicatedOutput = output1.DuplicateOutput1(_device, Format.B8G8R8A8_UNorm); // DarthAffe 27.02.2021: This prepares for the use of 10bit color depth
328+
329+
_duplicatedOutput = output.DuplicateOutput1(_device, Format.B8G8R8A8_UNorm); // DarthAffe 27.02.2021: This prepares for the use of 10bit color depth
329330
}
330331
catch { Dispose(false); }
331332
}

ScreenCapture.NET/Model/BlackBarDetection.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,10 @@ public void InvalidateCache()
7878
private int CalculateTop()
7979
{
8080
int threshold = Threshold;
81-
int stride = _captureZone.BufferWidth * 4;
82-
int bytesPerRow = _captureZone.Width * 4;
81+
int stride = _captureZone.Stride;
8382
for (int row = 0; row < _captureZone.Height; row++)
8483
{
85-
Span<byte> data = new(_captureZone.Buffer, row * stride, bytesPerRow);
84+
Span<byte> data = new(_captureZone.Buffer, row * stride, stride);
8685
for (int i = 0; i < data.Length; i += 4)
8786
if ((data[i] > threshold) || (data[i + 1] > threshold) || (data[i + 2] > threshold))
8887
return row;
@@ -94,11 +93,10 @@ private int CalculateTop()
9493
private int CalculateBottom()
9594
{
9695
int threshold = Threshold;
97-
int stride = _captureZone.BufferWidth * 4;
98-
int bytesPerRow = _captureZone.Width * 4;
96+
int stride = _captureZone.Stride;
9997
for (int row = _captureZone.Height; row >= 0; row--)
10098
{
101-
Span<byte> data = new(_captureZone.Buffer, row * stride, bytesPerRow);
99+
Span<byte> data = new(_captureZone.Buffer, row * stride, stride);
102100
for (int i = 0; i < data.Length; i += 4)
103101
if ((data[i] > threshold) || (data[i + 1] > threshold) || (data[i + 2] > threshold))
104102
return _captureZone.Height - row;
@@ -110,7 +108,7 @@ private int CalculateBottom()
110108
private int CalculateLeft()
111109
{
112110
int threshold = Threshold;
113-
int stride = _captureZone.BufferWidth * 4;
111+
int stride = _captureZone.Stride;
114112
byte[] buffer = _captureZone.Buffer;
115113
for (int column = 0; column < _captureZone.Width; column++)
116114
for (int row = 0; row < _captureZone.Height; row++)
@@ -125,7 +123,7 @@ private int CalculateLeft()
125123
private int CalculateRight()
126124
{
127125
int threshold = Threshold;
128-
int stride = _captureZone.BufferWidth * 4;
126+
int stride = _captureZone.Stride;
129127
byte[] buffer = _captureZone.Buffer;
130128
for (int column = _captureZone.Width; column >= 0; column--)
131129
for (int row = 0; row < _captureZone.Height; row++)

ScreenCapture.NET/Model/CaptureZone.cs

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -52,26 +52,14 @@ public sealed class CaptureZone
5252
public int UnscaledHeight { get; }
5353

5454
/// <summary>
55-
/// Gets the actually captured width of the region (this can be greated than <see cref="Width"/> due to size-constraints on the GPU).
55+
/// Gets the amount of bytes per pixel in the image (most likely 3 [RGB] or 4 [ARGB]).
5656
/// </summary>
57-
public int CaptureWidth { get; }
57+
public int BytesPerPixel { get; }
5858

5959
/// <summary>
60-
/// Gets the actually captured height of the region (this can be greated than <see cref="Height"/> due to size-constraints on the GPU).
60+
/// Gets the size in bytes of a row in the region (<see cref="Width"/> * <see cref="BytesPerPixel"/>).
6161
/// </summary>
62-
public int CaptureHeight { get; }
63-
64-
/// <summary>
65-
/// Gets the width of the buffer the capture is saved to.
66-
/// Equals <see cref="CaptureWidth"/> most of the time but can be bigger.
67-
/// </summary>
68-
public int BufferWidth { get; }
69-
70-
/// <summary>
71-
/// Gets the height of the buffer the capture is saved to.
72-
/// Equals <see cref="CaptureHeight"/> most of the time but can be bigger.
73-
/// </summary>
74-
public int BufferHeight { get; }
62+
public int Stride => Width * BytesPerPixel;
7563

7664
/// <summary>
7765
/// Gets the buffer containing the image data. Format depends on the specific capture but is most likely BGRA32.
@@ -117,25 +105,18 @@ public sealed class CaptureZone
117105
/// <param name="downscaleLevel">The level of downscaling applied to the image of this region before copying to local memory.</param>
118106
/// <param name="unscaledWidth">The original width of the region.</param>
119107
/// <param name="unscaledHeight">The original height of the region</param>
120-
/// <param name="captureWidth">The actually captured width of the region.</param>
121-
/// <param name="captureHeight">The actually captured height of the region.</param>
122-
/// <param name="bufferWidth">The width of the buffer the capture is saved to.</param>
123-
/// <param name="bufferHeight">The height of the buffer the capture is saved to.</param>
124108
/// <param name="buffer">The buffer containing the image data.</param>
125-
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)
109+
internal CaptureZone(int id, int x, int y, int width, int height, int bytesPerPixel, int downscaleLevel, int unscaledWidth, int unscaledHeight, byte[] buffer)
126110
{
127111
this.Id = id;
128112
this.X = x;
129113
this.Y = y;
130114
this.Width = width;
131115
this.Height = height;
116+
this.BytesPerPixel = bytesPerPixel;
132117
this.UnscaledWidth = unscaledWidth;
133118
this.UnscaledHeight = unscaledHeight;
134119
this.DownscaleLevel = downscaleLevel;
135-
this.CaptureWidth = captureWidth;
136-
this.CaptureHeight = captureHeight;
137-
this.BufferWidth = bufferWidth;
138-
this.BufferHeight = bufferHeight;
139120
this.Buffer = buffer;
140121

141122
BlackBars = new BlackBarDetection(this);

ScreenCapture.NET/ScreenCapture.NET.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
</ItemGroup>
6161

6262
<ItemGroup>
63-
<PackageReference Include="Vortice.Direct3D11" Version="1.9.65" />
63+
<PackageReference Include="Vortice.Direct3D11" Version="1.9.87" />
6464
</ItemGroup>
6565

6666
</Project>

0 commit comments

Comments
 (0)