将HLSL像素着色器应用于Win32屏幕截图

Applying HLSL Pixel Shaders to Win32 Screen Capture(将HLSL像素着色器应用于Win32屏幕截图)

本文介绍了将HLSL像素着色器应用于Win32屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个小背景:我正在尝试制作一个Windows(10)应用程序,使屏幕看起来像旧的CRT监视器、扫描线、模糊等等。我使用微软的这个官方screen capture demo作为起点:在这个阶段,我可以捕获一个窗口,并将其显示在一个新的鼠标直通窗口中,就像它是原始窗口一样。 我正在尝试使用CRT-Royale CRT shaders,它们通常被认为是最好的CRT着色器;它们以.cg格式提供。我用cgc将它们转换成hlsl,然后用fxc将hlsl文件编译成编译的着色器字节码。我能够成功加载编译的着色器并创建像素着色器。然后,我在d3d上下文中设置了像素着色器。然后,我尝试将捕获曲面帧复制到像素着色器资源,并设置创建的着色器资源。所有这些都构建并运行,但我在输出映像中看不到任何差异,也不确定如何继续。以下是相关代码。我不是一个c++开发人员,我把这个作为一个个人项目,一旦我有了一个原始的工作版本,我就计划开源。如有任何建议,我们将不胜感激,谢谢。

SimpleCapture::SimpleCapture(
IDirect3DDevice const& device,
GraphicsCaptureItem const& item)
{
m_item = item;
m_device = device;

// Set up 
auto d3dDevice = GetDXGIInterfaceFromObject<ID3D11Device>(m_device);
d3dDevice->GetImmediateContext(m_d3dContext.put());
auto size = m_item.Size();

m_swapChain = CreateDXGISwapChain(
    d3dDevice, 
    static_cast<uint32_t>(size.Width),
    static_cast<uint32_t>(size.Height),
    static_cast<DXGI_FORMAT>(DirectXPixelFormat::B8G8R8A8UIntNormalized),
    2);

// ADDED THIS
HRESULT hr1 = D3DReadFileToBlob(L"crt-royale-first-pass-ps_4_0.fxc", &ps_1_buffer);
HRESULT hr = d3dDevice->CreatePixelShader(
    ps_1_buffer->GetBufferPointer(),
    ps_1_buffer->GetBufferSize(),
    nullptr,
    &ps_1
);
m_d3dContext->PSSetShader(
    ps_1,
    nullptr,
    0
);
// END OF ADDED CHANGES

// Create framepool, define pixel format (DXGI_FORMAT_B8G8R8A8_UNORM), and frame size. 
m_framePool = Direct3D11CaptureFramePool::Create(
    m_device,
    DirectXPixelFormat::B8G8R8A8UIntNormalized,
    2,
    size);
m_session = m_framePool.CreateCaptureSession(m_item);
m_lastSize = size;
m_frameArrived = m_framePool.FrameArrived(auto_revoke, { this, &SimpleCapture::OnFrameArrived });
}

void SimpleCapture::OnFrameArrived(
Direct3D11CaptureFramePool const& sender,
winrt::Windows::Foundation::IInspectable const&)
{
auto newSize = false;

{
    auto frame = sender.TryGetNextFrame();
    auto frameContentSize = frame.ContentSize();

    if (frameContentSize.Width != m_lastSize.Width ||
        frameContentSize.Height != m_lastSize.Height)
    {
        // The thing we have been capturing has changed size.
        // We need to resize our swap chain first, then blit the pixels.
        // After we do that, retire the frame and then recreate our frame pool.
        newSize = true;
        m_lastSize = frameContentSize;
        m_swapChain->ResizeBuffers(
            2, 
            static_cast<uint32_t>(m_lastSize.Width),
            static_cast<uint32_t>(m_lastSize.Height),
            static_cast<DXGI_FORMAT>(DirectXPixelFormat::B8G8R8A8UIntNormalized), 
            0);
    }

    {
        auto frameSurface = GetDXGIInterfaceFromObject<ID3D11Texture2D>(frame.Surface());

        com_ptr<ID3D11Texture2D> backBuffer;
        check_hresult(m_swapChain->GetBuffer(0, guid_of<ID3D11Texture2D>(), backBuffer.put_void()));

        // ADDED THIS
        D3D11_TEXTURE2D_DESC txtDesc = {};
        txtDesc.MipLevels = txtDesc.ArraySize = 1;
        txtDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
        txtDesc.SampleDesc.Count = 1;
        txtDesc.Usage = D3D11_USAGE_IMMUTABLE;
        txtDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;

        auto d3dDevice = GetDXGIInterfaceFromObject<ID3D11Device>(m_device);

        ID3D11Texture2D *tex;
        d3dDevice->CreateTexture2D(&txtDesc, NULL,
            &tex);
        frameSurface.copy_to(&tex);

        d3dDevice->CreateShaderResourceView(
            tex,
            nullptr,
            srv_1
        );

        auto texture = srv_1;
        m_d3dContext->PSSetShaderResources(0, 1, texture);
        // END OF ADDED CHANGES

        m_d3dContext->CopyResource(backBuffer.get(), frameSurface.get());
    }
}

DXGI_PRESENT_PARAMETERS presentParameters = { 0 };
m_swapChain->Present1(1, 0, &presentParameters);
... // Truncated

推荐答案

着色器定义绘制对象的方式。但是,您不绘制任何内容-只是复制,这就是着色器不执行任何操作的原因。

您应该做的是移除CopyResource调用,转而在后台缓冲区上绘制一个全屏四边形(这需要您创建一个可以绑定的顶点缓冲区,然后将后台缓冲区设置为渲染目标,最后调用Draw/DrawIndexed来实际渲染一些内容,然后它将调用着色器)。

另外-因为我不确定您是否已经这样做了,并且只是从显示的代码中删除了它-像CreatePixelShader这样的函数返回HRESULT不是为了好玩-您应该检查实际返回的是什么,因为DirectX会静默返回大多数错误,并希望您处理它们,而不是使程序崩溃。

这篇关于将HLSL像素着色器应用于Win32屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:将HLSL像素着色器应用于Win32屏幕截图

基础教程推荐