首页 > 解决方案 > DirectX11离屏渲染:输出图像是flipepd

问题描述

我正在制作我的图形引擎。我希望能够在 C++ 上编写它,但在 C# 上为编辑器创建 UI。因此,通过一些定义,我禁用渲染到窗口并尝试进行屏幕外渲染以将数据传递给 c# 但我有一个问题,我理解它为什么会发生(这就是 DirectX 创建纹理并存储它们的方式)但是不知道如何解决它。所以这里是结果。

图像渲染到窗口: 渲染到窗口的图像

渲染为 bmp 的图像(翻转): 渲染为 bmp 的图像(翻转)

在第一张图片上,一切看起来都很好,而在第二张图片上,你可以看到我翻转了 Y,也许还有 X(不确定)坐标。对于可能有用的信息,我将法线表示为颜色。

这是我的代码

顶点缓冲区

cbuffer Transformation : register(b0) {
    matrix transformation;
};

cbuffer ViewProjection : register(b1) {
    matrix  projection;
    matrix  view;
};

struct VS_OUT {
    float2 texcoord : TextureCoordinate;
    float3 normal : Normal;
    float4 position : SV_Position;
};

VS_OUT main(float3 position : Position, float3 normal : Normal, float2 texcoord : TextureCoordinate) {
    matrix tView = transpose(view);
    matrix tProjection = transpose(projection);
    matrix tTransformation = transpose(transformation);

    matrix MVP = mul(tTransformation, mul(tView, tProjection));
    VS_OUT result;
    result.position = mul(float4(position, 1.0f), MVP);
    result.texcoord = texcoord;
    result.normal = normal;
    
    return result;
}

像素缓冲区

float4 main(float2 texcoord : TextureCoordinate, float3 normal : Normal) : SV_Target
{
    float3 color = (normal + 1) * 0.5f;
    return float4(color.rgb, 1.0f);
}

用于离屏渲染初始化的 DirectX 代码

D3D_FEATURE_LEVEL FeatureLevels[] = {
            D3D_FEATURE_LEVEL_11_1,
            D3D_FEATURE_LEVEL_11_0,
            D3D_FEATURE_LEVEL_10_1,
            D3D_FEATURE_LEVEL_10_0,
            D3D_FEATURE_LEVEL_9_3,
            D3D_FEATURE_LEVEL_9_2,
            D3D_FEATURE_LEVEL_9_1
        };

        UINT deviceFlags = 0;

#if defined(DEBUG) || defined(_DEBUG)
        deviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif      

        DirectX11Call(D3D11CreateDevice(
            nullptr,
            D3D_DRIVER_TYPE_HARDWARE,
            nullptr,
            deviceFlags,
            FeatureLevels,
            ARRAYSIZE(FeatureLevels),
            D3D11_SDK_VERSION,
            &m_Device,
            &m_FeatureLevel,
            &m_DeviceContext
        ))

        D3D11_TEXTURE2D_DESC renderingDescription = {};
        renderingDescription.Width = width;
        renderingDescription.Height = height;
        renderingDescription.ArraySize = 1;
        renderingDescription.SampleDesc.Count = 1;
        renderingDescription.Usage = D3D11_USAGE_DEFAULT;
        renderingDescription.BindFlags = D3D11_BIND_RENDER_TARGET;
        renderingDescription.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
        m_Device->CreateTexture2D(&renderingDescription, nullptr, &m_Target);

        renderingDescription.BindFlags = 0;
        renderingDescription.Usage = D3D11_USAGE_STAGING;
        renderingDescription.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
        m_Device->CreateTexture2D(&renderingDescription, nullptr, &m_Output);

        DirectX11Call(m_Device->CreateRenderTargetView(m_Target.Get(), nullptr, &m_RenderTargetView))

        D3D11_DEPTH_STENCIL_DESC depthStencilStateDescription = {};
        depthStencilStateDescription.DepthEnable = TRUE;
        depthStencilStateDescription.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
        depthStencilStateDescription.DepthFunc = D3D11_COMPARISON_LESS;

        Microsoft::WRL::ComPtr<ID3D11DepthStencilState> depthStencilState;
        DirectX11Call(m_Device->CreateDepthStencilState(&depthStencilStateDescription, &depthStencilState))
        m_DeviceContext->OMSetDepthStencilState(depthStencilState.Get(), 0);

        D3D11_TEXTURE2D_DESC depthStencilDescription = {};
        depthStencilDescription.Width = width;
        depthStencilDescription.Height = height;
        depthStencilDescription.MipLevels = 1;
        depthStencilDescription.ArraySize = 1;
        depthStencilDescription.Format = DXGI_FORMAT_D32_FLOAT;
        depthStencilDescription.SampleDesc.Count = 1;
        depthStencilDescription.SampleDesc.Quality = 0;
        depthStencilDescription.Usage = D3D11_USAGE_DEFAULT;
        depthStencilDescription.BindFlags = D3D11_BIND_DEPTH_STENCIL;
        depthStencilDescription.CPUAccessFlags = 0;
        depthStencilDescription.MiscFlags = 0;

        DirectX11Call(m_Device->CreateTexture2D(&depthStencilDescription, nullptr, &m_DepthStencilBuffer))

        D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDescription = {};
        depthStencilViewDescription.Format = DXGI_FORMAT_D32_FLOAT;
        depthStencilViewDescription.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
        depthStencilViewDescription.Texture2D.MipSlice = 0;

        DirectX11Call(m_Device->CreateDepthStencilView(m_DepthStencilBuffer.Get(), &depthStencilViewDescription, &m_DepthStencilView))

        m_DeviceContext->OMSetRenderTargets(1, m_RenderTargetView.GetAddressOf(), m_DepthStencilView.Get());

        m_DeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

        D3D11_VIEWPORT viewPort;
        viewPort.TopLeftX = 0.0f;
        viewPort.TopLeftY = 0.0f;
        viewPort.Width = static_cast<float>(width);
        viewPort.Height = static_cast<float>(height);
        viewPort.MinDepth = 0.0f;
        viewPort.MaxDepth = 1.0f;
        m_DeviceContext->RSSetViewports(1, &viewPort);

        D3D11_RASTERIZER_DESC rasterizerDescription = {};
        rasterizerDescription.FillMode = D3D11_FILL_SOLID;
        rasterizerDescription.CullMode = D3D11_CULL_FRONT;

        Microsoft::WRL::ComPtr<ID3D11RasterizerState> rasterizerState;
        DirectX11Call(m_Device->CreateRasterizerState(&rasterizerDescription, &rasterizerState))
        m_DeviceContext->RSSetState(rasterizerState.Get());

绘制到纹理的代码

    m_DeviceContext->Flush();

    m_DeviceContext->CopyResource(m_Output.Get(), m_Target.Get());

    static const UINT resource_id = D3D11CalcSubresource(0, 0, 0);
    m_DeviceContext->Map(m_Output.Get(), resource_id, D3D11_MAP_READ, 0, &m_OutputResource);

渲染到窗口之间的区别在于我也在创建交换链。所以我的问题是我该如何修复它(翻转 CPU 错误的解决方案,它可能会导致着色器出现问题,例如在这个示例中,我有不同的球体颜色)

标签: c++winapidirectxgame-engine

解决方案


推荐阅读