I have written some code to draw a texture (_textureResource) into a form using SharpDX. Now I would like to adjust the code so it renders into another texture first (outputTexture) and then renders outputTexture into the form. When I try to do this, the form shows the clear color of outputTexture instead of _textureResource. Any thoughts on what I am doing wrong?
Thanks.
The code below shows how the renderloop changes between the using the outputTexture and not using it.
RenderLoop.Run(_form, () =>
{
if (isFormClosed)
{
return;
}
bool useTexture = true;
if (useTexture)
{
_context.ClearDepthStencilView(_depthStencilView, DepthStencilClearFlags.Depth, 1, 0);
_context.OutputMerger.SetTargets(_depthStencilView, outputTextureRenderTargetView);
_context.ClearRenderTargetView(outputTextureRenderTargetView, new Color4(1, 1, 0, 1));
_context.PixelShader.SetShaderResource(0, _textureResource);
_context.DrawIndexed(_indexCount, 0, 0);
_context.Flush();
}
_context.ClearDepthStencilView(_depthStencilView, DepthStencilClearFlags.Depth, 1, 0);
_context.OutputMerger.SetTargets(_depthStencilView, _renderTargetView);
if (useTexture)
{
_context.PixelShader.SetShaderResource(0, outputTextureShaderResourceView);
}
else
{
_context.PixelShader.SetShaderResource(0, _textureResource);
}
_context.DrawIndexed(_indexCount, 0, 0);
_swapChain.Present(0, PresentFlags.None);
});
Probably these are the most interesting parts of the initialization code:
int width = 800;
int height = 600;
...
var ProjectionMatrix = Matrix.PerspectiveLH(width, height, 10, -10);
var WorldMatrix = Matrix.Identity;
Vector3 position = new Vector3(width / 2, height / 2, -10);
Vector3 lookAt = new Vector3(width / 2, height / 2, -9);
Vector3 up = new Vector3(0, 1, 0);
var ViewMatrix = Matrix.LookAtLH(position, lookAt, up);
...
var vertices = new[]
{
// Bottom left.
new Vertex()
{
position = new Vector3(0, 0, 0),
texture = new Vector2(0, 1)
},
// Top left.
new Vertex()
{
position = new Vector3(0, height, 0),
texture = new Vector2(0, 0)
},
// Bottom right.
new Vertex()
{
position = new Vector3(width, 0, 0),
texture = new Vector2(1, 1)
},
// Top right.
new Vertex()
{
position = new Vector3(width, height, 0),
texture = new Vector2(1, 0)
}
};
Texture2DDescription outputTextureDescription = new Texture2DDescription()
{
ArraySize = 1,
BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,
CpuAccessFlags = CpuAccessFlags.None,
Format = Format.R32G32B32A32_Float,
Height = width,
Width = height,
MipLevels = 1,
OptionFlags = ResourceOptionFlags.None,
SampleDescription = new SampleDescription(1, 0),
Usage = ResourceUsage.Default
};
Texture2D outputTexture = new Texture2D(_device, outputTextureDescription);
var renderTargetViewDesc = new RenderTargetViewDescription()
{
Format = outputTextureDescription.Format,
Dimension = RenderTargetViewDimension.Texture2D
};
renderTargetViewDesc.Texture2D.MipSlice = 0;
var outputTextureRenderTargetView = new RenderTargetView(_device, outputTexture, renderTargetViewDesc);
var shaderResourceViewDesc = new ShaderResourceViewDescription()
{
Format = outputTextureDescription.Format,
Dimension = ShaderResourceViewDimension.Texture2D,
};
shaderResourceViewDesc.Texture2D.MipLevels = 1;
shaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
// Create the render target view.
var outputTextureShaderResourceView = new ShaderResourceView(_device, outputTexture, shaderResourceViewDesc);
Enabling debug output of directx showed me that the stencil view and render target resolution did not match. The code posted in the question doesn't contain an error.