|
| 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 | +} |
0 commit comments