一直在尝试将我编写的渲染器从 SlimDX 更改为 SharpDX,但遇到了问题。我想渲染到多个渲染目标(在本例中为颜色和对象 ID 以供选择)
这是渲染目标的初始化(都具有相同的维度和多重采样设置)
//Swapchain, Device, Primary Rendertarget
var description = new SwapChainDescription()
{
BufferCount = 1,
Usage = Usage.RenderTargetOutput,
OutputHandle = Form.Handle,
IsWindowed = true,
ModeDescription = new ModeDescription(0, 0, new Rational(60, 1), Format.R8G8B8A8_UNorm),
SampleDescription = new SampleDescription(1, 0),
Flags = SwapChainFlags.AllowModeSwitch,
SwapEffect = SwapEffect.Discard
};
this.Device = new Device(adapter);
this.SwapChain = new SwapChain(factory, Device, description);
this.backBuffer = SharpDX.Direct3D11.Texture2D.FromSwapChain<SharpDX.Direct3D11.Texture2D>(SwapChain, 0);
this.RenderTargetView = new RenderTargetView(Device, backBuffer);
//Depthbuffer
Texture2DDescription descDepth = new Texture2DDescription();
descDepth.Width = (int)Viewport.Width;
descDepth.Height = (int)Viewport.Height;
descDepth.MipLevels = 1;
descDepth.ArraySize = 1;
descDepth.Format = Format.D32_Float;
descDepth.Usage = ResourceUsage.Default;
descDepth.SampleDescription = new SampleDescription(1, 0);
descDepth.BindFlags = BindFlags.DepthStencil;
descDepth.CpuAccessFlags = 0;
descDepth.OptionFlags = 0;
using (Texture2D depthStencil = new Texture2D(Device, descDepth))
{
depthView = new DepthStencilView(Device, depthStencil);
}
//Rendertargetview for the ID
Texture2DDescription IdMapDesc = new Texture2DDescription();
IdMapDesc.Width = (int)Viewport.Width;
IdMapDesc.Height = (int)Viewport.Height;
IdMapDesc.ArraySize = 1;
IdMapDesc.MipLevels = 1;
IdMapDesc.Format = Format.R16_UInt;
IdMapDesc.Usage = ResourceUsage.Default;
IdMapDesc.SampleDescription = new SampleDescription(1, 0);
IdMapDesc.BindFlags = BindFlags.ShaderResource | BindFlags.RenderTarget;
IdMapDesc.CpuAccessFlags = 0;
IdMapDesc.OptionFlags = 0;
using (Texture2D idMap = new Texture2D(Device, IdMapDesc))
{
idView = new RenderTargetView(Device, idMap);
}
这就是我做渲染的方式
public override void Render()
{
Context.ClearDepthStencilView(depthView, DepthStencilClearFlags.Depth, 1f, 0);
Context.OutputMerger.SetTargets(depthView, RenderTargetView);
staticMeshRenderer.UpdateCameraConstants();
foreach(TerrainSegment segment in terrain.SegmentMap)
{
terrainRenderer.Draw(segment, null);
}
objectManager.DrawContent(Device);
}
生成此输出(无法发布图像,这是一个具有工作深度模板的场景)
但是当像这样使用多个渲染目标时
Context.OutputMerger.SetTargets(depthView, RenderTargetView, idView);
Depthstencil 停止工作。用于两次尝试的 HLSL 代码:
struct PS_Output
{
float4 Color : SV_TARGET0;
uint ID : SV_TARGET1;
};
PS_Output PShader(VS_OutputStatic input)
{
PS_Output output;
output.ID = 3; //test
output.Color = Diffuse.Sample(StateLinear, input.TexCoords).rgba;
return output;
}
我在这里做错了什么?提前致谢!
我是一名优秀的程序员,十分优秀!