-
Notifications
You must be signed in to change notification settings - Fork 3
/
ShaderRecWindow.cs
168 lines (132 loc) · 5.61 KB
/
ShaderRecWindow.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using FFmpeg.Wrapper;
using GL2O;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.Desktop;
using System.Diagnostics;
//Recording OpenGL framebuffer using hardware encoder.
//This uses multiple Pixel Buffer Objects to enable asynchronous framebuffer downloads and reduce delay.
public unsafe class ShaderRecWindow : GameWindow
{
private MediaMuxer _muxer;
private MediaStream _stream;
private VideoEncoder _encoder;
private VideoFrame _rgbFrame, _frame = new();
private SwScaler _scaler;
private List<ShaderProgram> _shaders = new();
private VertexFormat _format = null!;
private BufferObject _emptyVbo = null!;
private BufferObject[] _pbos = new BufferObject[4];
private int _frameNo;
const int _width = 1280, _height = 720;
public ShaderRecWindow(string outVideoPath)
: base(
new GameWindowSettings(),
new NativeWindowSettings() {
Size = new(_width, _height),
Flags = ContextFlags.Debug | ContextFlags.ForwardCompatible,
APIVersion = new Version(4, 5),
})
{
UpdateFrequency = 5; //don't waste CPU on update logic
var format = new PictureFormat(_width, _height, PixelFormats.NV12);
using var device = VideoEncoder.CreateCompatibleHardwareDevice(CodecIds.HEVC, format, out var hwConfig)
?? throw new InvalidOperationException("No compatible hardware encoder for the given settings");
_encoder = new VideoEncoder(hwConfig, format, frameRate: 60, device);
if (_encoder.Codec.Name == "h265_qsv") {
_encoder.SetOption("preset", "veryslow");
}
_encoder.SetGlobalOption("global_quality", "28");
_muxer = new MediaMuxer(outVideoPath);
_stream = _muxer.AddStream(_encoder);
_muxer.Open();
_rgbFrame = new VideoFrame(format.Width, format.Height, PixelFormats.RGBA);
_frame = new VideoFrame(format);
_scaler = new SwScaler(_rgbFrame.Format, format);
}
protected override void OnLoad()
{
base.OnLoad();
string shaderBasePath = AppContext.BaseDirectory + "Shaders/";
foreach (string file in Directory.GetFiles(shaderBasePath + "shadertoy/", "*.frag")) {
Console.WriteLine("Loading shader " + Path.GetFileName(file));
var shader = new ShaderProgram();
shader.AttachFile(ShaderType.VertexShader, shaderBasePath + "full_screen_quad.vert");
shader.AttachFile(ShaderType.FragmentShader, file);
shader.Link();
_shaders.Add(shader);
}
_format = VertexFormat.CreateEmpty();
_emptyVbo = new BufferObject(16, BufferStorageFlags.None);
for (int i = 0; i < _pbos.Length; i++) {
_pbos[i] = new BufferObject(_width * _height * 4, BufferStorageFlags.DynamicStorageBit | BufferStorageFlags.MapReadBit);
}
}
protected override void OnRenderFrame(FrameEventArgs e)
{
base.OnRenderFrame(e);
GL.Viewport(0, 0, _width, _height);
double time = _frameNo / (double)_encoder.FrameRate;
int demoDuration = 5;
//Encode some previously rendered frame
if (_frameNo >= _pbos.Length) {
EncodeFrame();
}
if (time > _shaders.Count * demoDuration) {
Close();
}
//Draw demo shader
var currShader = _shaders[(int)time / demoDuration % _shaders.Count];
currShader.SetUniform("iTime", (float)time);
currShader.SetUniform("iResolution", new Vector3(ClientSize.X, ClientSize.Y, 0));
currShader.DrawArrays(PrimitiveType.Triangles, _format, _emptyVbo, 0, 3);
//Begin the download of the current frame to one of our PBOs
BeginBackbufferDownload();
SwapBuffers();
_frameNo++;
}
private void BeginBackbufferDownload()
{
GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, 0);
GL.BindBuffer(BufferTarget.PixelPackBuffer, _pbos[_frameNo % _pbos.Length].Id);
GL.ReadPixels(0, 0, _width, _height, PixelFormat.Rgba, PixelType.UnsignedByte, 0);
GL.BindBuffer(BufferTarget.PixelPackBuffer, 0);
}
private void EncodeFrame()
{
long start = Stopwatch.GetTimestamp();
var pbo = _pbos[(_frameNo - _pbos.Length + 1) % _pbos.Length];
var mapping = pbo.Map<byte>(0, (int)pbo.Size, BufferAccessMask.MapReadBit);
//OpenGL uses bottom-left as the pixel origin, so we'll flip the Y coords here.
//
//Note that giving the mapped memory directly to swscaler is _super_ slow.
//It's probably doing lots of accesses for interpolation and planar chroma
//packing or something like that.
for (int y = 0; y < _height; y++) {
var row = _rgbFrame.GetRowSpan<byte>(_height - 1 - y);
mapping.Slice(y * row.Length, row.Length).CopyTo(row);
}
pbo.Unmap();
_scaler.Convert(_rgbFrame, _frame);
_frame.PresentationTimestamp = _encoder.GetFramePts(_frameNo);
_muxer.EncodeAndWrite(_stream, _encoder, _frame);
Title = $"EncodeFrame: {Stopwatch.GetElapsedTime(start).TotalMilliseconds:0.0}ms";
}
protected override void OnUnload()
{
base.OnUnload();
//Flush delayed frames
_muxer.EncodeAndWrite(_stream, _encoder, null);
_muxer.Dispose();
_encoder.Dispose();
_frame.Dispose();
_scaler.Dispose();
foreach (var shader in _shaders) {
shader.Dispose();
}
foreach (var pbo in _pbos) {
pbo.Dispose();
}
_emptyVbo.Dispose();
_format.Dispose();
}
}