Skip to content

Commit 536c92b

Browse files
authored
Merge pull request #10 from akon47/develop
release: 1.0.6
2 parents e7a1f53 + 99120ba commit 536c92b

File tree

5 files changed

+489
-336
lines changed

5 files changed

+489
-336
lines changed

ScreenRecorder/DirectX/DuplicatorCapture.cs

+27-19
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ static public MonitorInfo[] GetActiveMonitorInfos()
109109
private SharpDX.Direct3D11.Buffer verticesBuffer;
110110
private VertexBufferBinding vertextBufferBinding;
111111
private ColorShader colorShader;
112+
private CursorShader cursorShader;
112113
private IntPtr dataPointer;
113114

114115
private bool drawCursor;
@@ -227,9 +228,16 @@ public DuplicatorCapture(string deviceName = null, bool drawCursor = true)
227228

228229
context.Rasterizer.SetViewport(new Viewport(0, 0, screenWidth, screenHeight, 0.0f, 1.0f));
229230

231+
230232
colorShader = new ColorShader();
231233
colorShader.Initialize(device);
232234

235+
if (drawCursor)
236+
{
237+
cursorShader = new CursorShader();
238+
cursorShader.Initialize(device);
239+
}
240+
233241
verticesBuffer = new SharpDX.Direct3D11.Buffer(device, (Vector3.SizeInBytes + Vector2.SizeInBytes) * 6,
234242
ResourceUsage.Dynamic, BindFlags.VertexBuffer, CpuAccessFlags.Write, ResourceOptionFlags.None, 0);
235243
vertextBufferBinding = new VertexBufferBinding(verticesBuffer, Vector3.SizeInBytes + Vector2.SizeInBytes, 0);
@@ -419,34 +427,28 @@ public bool AcquireNextFrame(out IntPtr dataPointer, out int width, out int heig
419427
}
420428
}
421429
}
430+
422431
cursorTexture.Render(context, pointerInfo.Position.X, pointerInfo.Position.Y, (int)(pointerInfo.ShapeInfo.Width * destScaleFactor.Width), (int)(pointerInfo.ShapeInfo.Height * destScaleFactor.Height));
423-
colorShader.Render(context, cursorTexture.GetTexture());
432+
cursorShader.Render(context, cursorTexture.GetTexture(), (OutputDuplicatePointerShapeType)pointerInfo.ShapeInfo.Type);
424433
}
425434

426435
nv12Converter.Convert(renderTargetTexture, nv12Texture);
427-
context.CopyResource(nv12Texture, readableNv12Texture);
428-
DataBox mapSource = context.MapSubresource(readableNv12Texture, 0, MapMode.Read, SharpDX.Direct3D11.MapFlags.None, out DataStream stream);
429-
width = readableNv12Texture.Description.Width;
430-
height = readableNv12Texture.Description.Height;
431-
stride = mapSource.RowPitch;
432-
dataPointer = this.dataPointer;
433-
pixelFormat = MediaEncoder.PixelFormat.NV12;
434-
stream.Read(this.dataPointer, 0, mapSource.SlicePitch);
435-
context.UnmapSubresource(readableNv12Texture, 0);
436436
}
437437
else
438438
{
439439
nv12Converter.Convert(displayTexture2D, nv12Texture);
440-
context.CopyResource(nv12Texture, readableNv12Texture);
441-
DataBox mapSource = context.MapSubresource(readableNv12Texture, 0, MapMode.Read, SharpDX.Direct3D11.MapFlags.None, out DataStream stream);
442-
width = readableNv12Texture.Description.Width;
443-
height = readableNv12Texture.Description.Height;
444-
stride = mapSource.RowPitch;
445-
dataPointer = this.dataPointer;
446-
pixelFormat = MediaEncoder.PixelFormat.NV12;
447-
stream.Read(this.dataPointer, 0, mapSource.SlicePitch);
448-
context.UnmapSubresource(readableNv12Texture, 0);
449440
}
441+
442+
context.CopyResource(nv12Texture, readableNv12Texture);
443+
DataBox mapSource = context.MapSubresource(readableNv12Texture, 0, MapMode.Read, SharpDX.Direct3D11.MapFlags.None, out DataStream stream);
444+
width = readableNv12Texture.Description.Width;
445+
height = readableNv12Texture.Description.Height;
446+
stride = mapSource.RowPitch;
447+
dataPointer = this.dataPointer;
448+
pixelFormat = MediaEncoder.PixelFormat.NV12;
449+
stream.Read(this.dataPointer, 0, mapSource.SlicePitch);
450+
context.UnmapSubresource(readableNv12Texture, 0);
451+
450452
return true;
451453
}
452454
}
@@ -495,9 +497,15 @@ public void Dispose()
495497
readableRenderTargetTexture = null;
496498
}
497499

500+
colorShader?.Dispose();
501+
colorShader = null;
502+
498503
cursorTexture?.Dispose();
499504
cursorTexture = null;
500505

506+
cursorShader?.Dispose();
507+
cursorShader = null;
508+
501509
if (!renderTargetView?.IsDisposed ?? false)
502510
{
503511
renderTargetView.Dispose();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
using System;
2+
using SharpDX.D3DCompiler;
3+
using SharpDX.Direct3D11;
4+
using SharpDX.DXGI;
5+
6+
namespace ScreenRecorder.DirectX.Shader
7+
{
8+
public class CursorShader : IDisposable
9+
{
10+
private readonly string shaderCode =
11+
@"
12+
Texture2D Texture : register(t0);
13+
SamplerState TextureSampler;
14+
15+
cbuffer ShaderArgs
16+
{
17+
int outputDuplicatePointerShapeType;
18+
};
19+
20+
struct VSInput
21+
{
22+
float4 position : POSITION;
23+
float2 uv : TEXCOORD0;
24+
};
25+
26+
struct PSInput
27+
{
28+
float4 position : SV_POSITION;
29+
float2 uv : TEXCOORD0;
30+
};
31+
32+
PSInput VShader(VSInput input)
33+
{
34+
PSInput output;
35+
output.position = input.position;
36+
output.uv = input.uv;
37+
return output;
38+
}
39+
40+
float4 PShader(PSInput input) : SV_Target
41+
{
42+
float4 color = Texture.Sample(TextureSampler, input.uv);
43+
if(outputDuplicatePointerShapeType == 4)
44+
{
45+
color.a = color.a == 0.0 ? 1.0 : 0.0;
46+
}
47+
return color;
48+
}
49+
";
50+
private InputLayout inputLayout;
51+
private ShaderSignature inputSignature;
52+
private VertexShader vertexShader;
53+
private PixelShader pixelShader;
54+
private SamplerState samplerState;
55+
private SharpDX.Direct3D11.Buffer argsBuffer;
56+
57+
public void Initialize(SharpDX.Direct3D11.Device device)
58+
{
59+
InitializeShader(device);
60+
}
61+
62+
private void InitializeShader(SharpDX.Direct3D11.Device device)
63+
{
64+
using (var bytecode = ShaderBytecode.Compile(shaderCode, "VShader", "vs_4_0", ShaderFlags.None, EffectFlags.None))
65+
{
66+
inputSignature = ShaderSignature.GetInputSignature(bytecode);
67+
vertexShader = new VertexShader(device, bytecode);
68+
}
69+
70+
using (var bytecode = ShaderBytecode.Compile(shaderCode, "PShader", "ps_4_0", ShaderFlags.None, EffectFlags.None))
71+
{
72+
pixelShader = new PixelShader(device, bytecode);
73+
}
74+
75+
var elements = new[]
76+
{
77+
new InputElement("POSITION", 0, SharpDX.DXGI.Format.R32G32B32_Float, 0, 0, InputClassification.PerVertexData, 0),
78+
new InputElement("TEXCOORD", 0, SharpDX.DXGI.Format.R32G32_Float, InputElement.AppendAligned, 0, InputClassification.PerVertexData, 0)
79+
};
80+
81+
inputLayout = new InputLayout(device, inputSignature, elements);
82+
83+
argsBuffer = new SharpDX.Direct3D11.Buffer(device, 16, ResourceUsage.Dynamic, BindFlags.ConstantBuffer, CpuAccessFlags.Write, ResourceOptionFlags.None, 0);
84+
85+
samplerState = new SamplerState(device, new SamplerStateDescription()
86+
{
87+
Filter = SharpDX.Direct3D11.Filter.MinMagMipLinear,
88+
AddressU = TextureAddressMode.Wrap,
89+
AddressV = TextureAddressMode.Wrap,
90+
AddressW = TextureAddressMode.Wrap,
91+
MipLodBias = 0.0f,
92+
MaximumAnisotropy = 1,
93+
ComparisonFunction = Comparison.Always,
94+
BorderColor = new SharpDX.Mathematics.Interop.RawColor4(0, 0, 0, 0),
95+
MinimumLod = 0,
96+
MaximumLod = float.MaxValue
97+
});
98+
}
99+
100+
public void Render(DeviceContext deviceContext, ShaderResourceView shaderResourceView, OutputDuplicatePointerShapeType outputDuplicatePointerShapeType)
101+
{
102+
SetShaderParameters(deviceContext, shaderResourceView, outputDuplicatePointerShapeType);
103+
RenderShader(deviceContext);
104+
105+
if (shaderResourceView != null)
106+
deviceContext.PixelShader.SetShaderResource(0, null);
107+
}
108+
109+
private int oldOutputDuplicatePointerShapeType = -1;
110+
private void SetShaderParameters(DeviceContext deviceContext, ShaderResourceView shaderResourceView, OutputDuplicatePointerShapeType outputDuplicatePointerShapeType)
111+
{
112+
if (oldOutputDuplicatePointerShapeType != (int)outputDuplicatePointerShapeType)
113+
{
114+
oldOutputDuplicatePointerShapeType = (int)outputDuplicatePointerShapeType;
115+
116+
deviceContext.MapSubresource(argsBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out SharpDX.DataStream dataStream);
117+
dataStream.Write<int>(oldOutputDuplicatePointerShapeType);
118+
deviceContext.UnmapSubresource(argsBuffer, 0);
119+
}
120+
121+
deviceContext.PixelShader.SetConstantBuffer(0, argsBuffer);
122+
deviceContext.PixelShader.SetShaderResource(0, shaderResourceView);
123+
}
124+
125+
private void RenderShader(DeviceContext deviceContext)
126+
{
127+
deviceContext.InputAssembler.InputLayout = inputLayout;
128+
deviceContext.VertexShader.Set(vertexShader);
129+
deviceContext.PixelShader.Set(pixelShader);
130+
deviceContext.PixelShader.SetSampler(0, samplerState);
131+
deviceContext.Draw(6, 0);
132+
}
133+
134+
public void Dispose()
135+
{
136+
inputLayout?.Dispose();
137+
inputSignature?.Dispose();
138+
vertexShader?.Dispose();
139+
pixelShader?.Dispose();
140+
samplerState?.Dispose();
141+
argsBuffer?.Dispose();
142+
}
143+
}
144+
}

ScreenRecorder/Properties/AssemblyInfo.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,5 @@
4949
// 모든 값을 지정하거나 아래와 같이 '*'를 사용하여 빌드 번호 및 수정 번호를
5050
// 기본값으로 할 수 있습니다.
5151
// [assembly: AssemblyVersion("1.0.*")]
52-
[assembly: AssemblyVersion("1.0.5.0")]
53-
[assembly: AssemblyFileVersion("1.0.5.0")]
52+
[assembly: AssemblyVersion("1.0.6.0")]
53+
[assembly: AssemblyFileVersion("1.0.6.0")]

ScreenRecorder/ScreenRecorder.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
<Compile Include="DirectX\DuplicatorCapture.cs" />
125125
<Compile Include="DirectX\NV12Converter.cs" />
126126
<Compile Include="DirectX\Shader\ColorShader.cs" />
127+
<Compile Include="DirectX\Shader\CursorShader.cs" />
127128
<Compile Include="DirectX\Texture\BitmapTexture.cs" />
128129
<Compile Include="Encoder\CircularBuffer.cs" />
129130
<Compile Include="Encoder\EncoderCodec.cs" />

0 commit comments

Comments
 (0)