如何解决DirectX11 离屏渲染:输出图像翻转
我正在制作我的图形引擎。我希望能够在 C++ 上编写它,但在 C# 上为编辑器创建 UI。因此,通过一些定义,我禁用了对窗口的渲染并尝试进行屏幕外渲染以将数据传递给 c#,但是我遇到了问题,我理解为什么会发生这种情况(这是 DirectX 创建纹理并存储它们的方式)但是不知道如何修复它。结果如下。
渲染到窗口的图像:
图像渲染为 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,&m_Target);
renderingDescription.BindFlags = 0;
renderingDescription.Usage = D3D11_USAGE_STAGING;
renderingDescription.cpuAccessFlags = D3D11_cpu_ACCESS_READ;
m_Device->CreateTexture2D(&renderingDescription,&m_Output);
DirectX11Call(m_Device->CreaterendertargetView(m_Target.Get(),&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,&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);
m_DeviceContext->Map(m_Output.Get(),resource_id,D3D11_MAP_READ,&m_OutputResource);
渲染到窗口的区别在于我也在创建交换链。所以我的问题是如何修复它(翻转 cpu 错误的解决方案,它可能会导致着色器出现问题,例如在这个示例中,我的球体颜色不同)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。