forked from sencercoltu/steamvr-undistort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UndistortShader.cs
62 lines (53 loc) · 2.41 KB
/
UndistortShader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using SharpDX;
using SharpDX.D3DCompiler;
using SharpDX.Direct3D;
using SharpDX.Direct3D11;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Valve.VR;
using static Undistort.Program;
namespace Undistort
{
public static class UndistortShader
{
private static SharpDX.Direct3D11.Buffer vertexBuffer;
private static VertexBufferBinding vertexBufferBinding;
private static Shader shader;
public static void Load(Device device)
{
shader = new Shader(device, "Undistort_VS", "Undistort_PS", new InputElement[]
{
new InputElement("POSITION", 0, SharpDX.DXGI.Format.R32G32_Float, 0, 0),
new InputElement("TEXCOORD", 0, SharpDX.DXGI.Format.R32G32_Float, 8, 0)
});
var verticesList = new List<float>();
vertexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.VertexBuffer, new[]
{
// 3D coordinates UV Texture coordinates
-1.0f, -1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 1.0f, 0.0f,
-1.0f, 1.0f, 0.0f, 0.0f,
-1.0f, -1.0f, 0.0f, 1.0f,
1.0f, -1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 0.0f,
});
vertexBufferBinding = new VertexBufferBinding(vertexBuffer, sizeof(float) * 4, 0);
}
public static void Render(DeviceContext context, ref EyeData eye)
{
context.OutputMerger.SetDepthStencilState(Program.DepthStencilState);
context.OutputMerger.SetBlendState(null);
var textureView = Program.UndistortTextureView;
context.Rasterizer.SetViewport(0, 0, eye.FrameSize.Width, eye.FrameSize.Height);
context.ClearRenderTargetView(textureView, SharpDX.Color.Black);
context.ClearDepthStencilView(eye.DepthStencilView, DepthStencilClearFlags.Depth, 1.0f, 0);
context.OutputMerger.SetTargets(eye.DepthStencilView, textureView);
shader.Apply(context);
context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
context.InputAssembler.SetVertexBuffers(0, vertexBufferBinding);
context.PixelShader.SetShaderResource(0, eye.ShaderView);
context.Draw(6, 0);
}
}
}