Replies: 1 comment
-
I changed the code to the following, and still only got 0 DXGI dxgi = new DXGI(new DefaultNativeContext("dxgi"));
if (dxgi.CreateDXGIFactory1<IDXGIFactory1>(out var factory)!=0)
{
throw new Exception("Failed to create DXGI factory");
}
IDXGIAdapter1* adapter1 = null;
if (factory.EnumAdapters1(0, ref adapter1)!=0)
{
throw new Exception("Failed to create DXGI adapter");
}
uint i = 1;
IDXGIOutput* output = null;
if (adapter1->EnumOutputs(i, ref output)!=0)
{
throw new Exception("Failed to create DXGI output");
}
OutputDesc desc=new OutputDesc(null);
if (output->GetDesc(ref desc)!=0)
{
throw new Exception("Failed to get output description");
}
if (output->QueryInterface<IDXGIOutput5>(out var output5)!=0)
{
throw new Exception("Failed to get IDXGIOutput5");
}
ID3D11Device* device=null;
ID3D11DeviceContext* context=null;
D3DFeatureLevel featureLevel=D3DFeatureLevel.Level101;
D3DFeatureLevel[] featureLevels =
[
D3DFeatureLevel.Level111, D3DFeatureLevel.Level110, D3DFeatureLevel.Level101, D3DFeatureLevel.Level100
];
fixed (D3DFeatureLevel* pFeatureLevels = &featureLevels[0])
{
D3D11 d3D11 = new D3D11(new DefaultNativeContext("d3d11"));
if (d3D11.CreateDevice((IDXGIAdapter*)adapter1, D3DDriverType.Unknown, IntPtr.Zero,
(uint)CreateDeviceFlag.None,pFeatureLevels,(uint)featureLevels.Length, D3D11.SdkVersion, ref device,
&featureLevel, ref context)!=0)
{
throw new Exception("Failed to create D3D11 device");
}
}
ID3D11DeviceContext* immediateContext = null;
device->GetImmediateContext(ref immediateContext);
IDXGIOutputDuplication *outputDuplication = null;
if (output5.DuplicateOutput((IUnknown*)device, ref outputDuplication)!=0)
{
throw new Exception("Failed to get output duplication");
}
OutduplFrameInfo outduplFrameInfo = new OutduplFrameInfo();
IDXGIResource* desktopResource = null;
if (outputDuplication->AcquireNextFrame(1000, &outduplFrameInfo, &desktopResource)!=0)
{
throw new Exception("Failed to acquire next frame");
}
if (desktopResource->QueryInterface<ID3D11Texture2D>(out var desktopTexture)!=0)
{
throw new Exception("Failed to get desktop texture");
}
Texture2DDesc stagingTextureDesc = new()
{
CPUAccessFlags = (uint)CpuAccessFlag.Read,
BindFlags = (uint)(BindFlag.None),
Format = Format.FormatB8G8R8A8Unorm,
Width = (uint)desc.DesktopCoordinates.Size.X,
Height = (uint)desc.DesktopCoordinates.Size.Y,
MiscFlags = (uint)ResourceMiscFlag.None,
MipLevels = 1,
ArraySize = 1,
SampleDesc = { Count = 1, Quality = 0 },
Usage = Usage.Staging
};
ID3D11Texture2D* stagingTexture = null;
if (device->CreateTexture2D(&stagingTextureDesc,null,ref stagingTexture )!=0)
{
throw new Exception("Failed to create staging texture");
}
Texture2DDesc captureTextureDesc = new()
{
CPUAccessFlags = (uint)CpuAccessFlag.None,
BindFlags = (uint)(BindFlag.RenderTarget|BindFlag.ShaderResource),
Format = Format.FormatB8G8R8A8Unorm,
Width = (uint)desc.DesktopCoordinates.Size.X,
Height = (uint)desc.DesktopCoordinates.Size.Y,
MiscFlags = (uint)ResourceMiscFlag.None,
MipLevels = 1,
ArraySize = 1,
SampleDesc = { Count = 1, Quality = 0 },
Usage = Usage.Default
};
ID3D11Texture2D* _captureTexture = null;
if (device->CreateTexture2D(&captureTextureDesc,null,ref _captureTexture )!=0)
{
throw new Exception("Failed to create staging texture");
}
immediateContext->CopyResource((ID3D11Resource*)_captureTexture, (ID3D11Resource*)desktopTexture.Handle);
desktopResource->Release();
outputDuplication->ReleaseFrame();
immediateContext->CopySubresourceRegion((ID3D11Resource*)stagingTexture, 0, 0, 0, 0,
(ID3D11Resource*)_captureTexture, 0, null);
MappedSubresource mappedSubresource=new MappedSubresource();
if (immediateContext->Map((ID3D11Resource*)stagingTexture, 0, Map.Read, 0, &mappedSubresource)!=0)
{
throw new Exception("Failed to map staging texture");
}
var span = new ReadOnlySpan<byte>(mappedSubresource.PData,
(int)mappedSubresource.DepthPitch);
var image=Image.Load<Bgra32>(span);
image.SaveAsJpeg("1.jpg"); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I tried this with the following code, but I ended up with a Span that only had 0 in it, anyone know why?
Beta Was this translation helpful? Give feedback.
All reactions