我正在使用DirectX11和SharpDx& C#中的WPF D3DImage将图像渲染为纹理。
之前我正在更新我的渲染目标(效果很好),而不是使用像素着色器在我的四边形中显示更新的纹理。
现在我已经意识到我的错误,我决定使用像素着色器,以便我可以在视口的帮助下实现信箱技术。
在这样做时,我有点无法弄清楚为什么我的渲染纹理是图像的子区域,而这个区域又显示为拉伸。
使用的代码:
a)初始化代码
var device = this.Device;
var context = device.ImmediateContext;
byte[] fileBytes = GetShaderEffectsFileBytes();
var vertexShaderByteCode = ShaderBytecode.Compile(fileBytes, "VSMain", "vs_5_0", ShaderFlags.None, EffectFlags.None);
var vertexShader = new VertexShader(device, vertexShaderByteCode);
var pixelShaderByteCode = ShaderBytecode.Compile(fileBytes, "PSMain", "ps_5_0", ShaderFlags.None, EffectFlags.None);
var pixelShader = new PixelShader(device, pixelShaderByteCode);
layout = new InputLayout(device, vertexShaderByteCode, new[] {
new InputElement("SV_Position", 0, Format.R32G32B32A32_Float, 0, 0),
new InputElement("TEXCOORD", 0, Format.R32G32_Float, 16, 0),
});
// Write vertex data to a datastream
var stream = new DataStream(Utilities.SizeOf<VertexPositionTexture>() * 6, true, true);
stream.WriteRange(new[]
{
new VertexPositionTexture(
new Vector4(1, 1, 0.5f, 1.0f), // position top-left
new Vector2(1.0f, 0.0f)
),
new VertexPositionTexture(
new Vector4(1, -1, 0.5f, 1.0f), // position top-right
new Vector2(1.0f, 1.0f)
),
new VertexPositionTexture(
new Vector4(-1, 1, 0.5f, 1.0f), // position bottom-left
new Vector2(0.0f, 0.0f)
),
new VertexPositionTexture(
new Vector4(-1, -1, 0.5f, 1.0f), // position bottom-right
new Vector2(0.0f, 1.0f)
),
});
stream.Position = 0;
vertices = new SharpDX.Direct3D11.Buffer(device, stream, new BufferDescription()
{
BindFlags = BindFlags.VertexBuffer,
CpuAccessFlags = CpuAccessFlags.None,
OptionFlags = ResourceOptionFlags.None,
SizeInBytes = Utilities.SizeOf<VertexPositionTexture>() * 6,
Usage = ResourceUsage.Default,
StructureByteStride = 0
});
stream.Dispose();
context.InputAssembler.InputLayout = (layout);
context.InputAssembler.PrimitiveTopology = (PrimitiveTopology.TriangleStrip);
context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertices, Utilities.SizeOf<VertexPositionTexture>(), 0));
context.VertexShader.Set(vertexShader);
context.GeometryShader.Set(null);
context.PixelShader.Set(pixelShader);
Device.ImmediateContext.OutputMerger.SetTargets(m_depthStencilView, m_RenderTargetView);
this.ImgSource.SetRenderTargetDX11(this.RenderTarget);
Texture2D flower = Texture2D.FromFile<Texture2D>(this.Device, "3.jpg");
var srv = new ShaderResourceView(this.Device, flower);
Device.ImmediateContext.PixelShader.SetShaderResource(0, srv);
srv.Dispose();
b)VertexPositionTexture
public struct VertexPositionTexture
{
public VertexPositionTexture(Vector4 position, Vector2 textureUV)
{
Position = position;
TextureUV = textureUV;
}
public Vector4 Position;
public Vector2 TextureUV;
}
c)渲染代码
Device.ImmediateContext.ClearRenderTargetView(this.m_RenderTargetView, new Color4(Color.Blue.R, Color.Blue.G, Color.Blue.B, Color.Blue.A));
Device.ImmediateContext.ClearDepthStencilView(m_depthStencilView, DepthStencilClearFlags.Depth, 1.0f, 0);
Device.ImmediateContext.Draw(4, 0);
Device.ImmediateContext.Flush();
this.ImgSource.InvalidateD3DImage();
d)着色器效果文件:
Texture2D ShaderTexture : register(t0);
SamplerState Sampler : register (s0);
struct VertexShaderInput
{
float4 Position : SV_Position;
float2 TextureUV : TEXCOORD0;
};
struct VertexShaderOutput
{
float4 Position : SV_Position;
float2 TextureUV : TEXCOORD0;
};
VertexShaderOutput VSMain(VertexShaderInput input)
{
VertexShaderOutput output = (VertexShaderOutput)0;
output.Position = input.Position;
output.TextureUV = input.TextureUV;
return output;
}
float4 PSMain(VertexShaderOutput input) : SV_Target
{
return ShaderTexture.Sample(Sampler, input.TextureUV);
}
同样在下面我添加了我一直有的问题的截图和应该渲染的实际图像。
任何建议或帮助都会非常有用,因为我研究过网络和各种论坛,但没有运气。
感谢。