-
-
Notifications
You must be signed in to change notification settings - Fork 454
Expand file tree
/
Copy pathMilkdropPreset.cpp
More file actions
executable file
·324 lines (260 loc) · 10.8 KB
/
MilkdropPreset.cpp
File metadata and controls
executable file
·324 lines (260 loc) · 10.8 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/**
* projectM -- Milkdrop-esque visualisation SDK
* Copyright (C)2003-2004 projectM Team
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* See 'LICENSE.txt' included within this release
*
*/
#include "MilkdropPreset.hpp"
#include "Factory.hpp"
#include "MilkdropPresetExceptions.hpp"
#include "PresetFileParser.hpp"
#include <Renderer/Backend/OpenGL/OpenGLCopyTexture.hpp>
#include <Logging.hpp>
namespace libprojectM {
namespace MilkdropPreset {
MilkdropPreset::MilkdropPreset(const std::string& absoluteFilePath)
: m_absoluteFilePath(absoluteFilePath)
, m_perFrameContext(m_state.globalMemory, &m_state.globalRegisters)
, m_perPixelContext(m_state.globalMemory, &m_state.globalRegisters)
, m_motionVectors(m_state)
, m_waveform(m_state)
, m_darkenCenter(m_state)
, m_border(m_state)
, m_flipTexture(std::make_unique<libprojectM::Renderer::Backend::OpenGL::OpenGLCopyTexture>())
{
// Ensure OpenGLCopyTexture is visible to this translation unit
// (include the header if not already included at the top of this file)
Load(absoluteFilePath);
}
MilkdropPreset::MilkdropPreset(std::istream& presetData)
: m_perFrameContext(m_state.globalMemory, &m_state.globalRegisters)
, m_perPixelContext(m_state.globalMemory, &m_state.globalRegisters)
, m_motionVectors(m_state)
, m_waveform(m_state)
, m_darkenCenter(m_state)
, m_border(m_state)
, m_flipTexture(std::make_unique<libprojectM::Renderer::Backend::OpenGL::OpenGLCopyTexture>())
{
// Ensure OpenGLCopyTexture is visible to this translation unit
// (include the header if not already included at the top of this file)
Load(presetData);
}
void MilkdropPreset::Initialize(const Renderer::RenderContext& renderContext)
{
assert(renderContext.textureManager);
m_state.renderContext = renderContext;
m_state.blurTexture.Initialize(renderContext);
m_state.LoadShaders();
// Initialize variables and code now we have a proper render state.
CompileCodeAndRunInitExpressions();
// Update framebuffer and texture sizes if needed
m_framebuffer.SetSize(renderContext.viewportSizeX, renderContext.viewportSizeY);
m_motionVectorUVMap->SetSize(renderContext.viewportSizeX, renderContext.viewportSizeY);
if (m_state.mainTexture.expired())
{
m_state.mainTexture = m_framebuffer.GetColorAttachmentTexture(1, 0);
}
m_perPixelMesh.CompileWarpShader(m_state);
m_finalComposite.CompileCompositeShader(m_state);
}
void MilkdropPreset::RenderFrame(const libprojectM::Audio::FrameAudioData& audioData, const Renderer::RenderContext& renderContext)
{
m_state.audioData = audioData;
m_state.renderContext = renderContext;
// Update framebuffer and u/v texture size if needed
if (m_framebuffer.SetSize(renderContext.viewportSizeX, renderContext.viewportSizeY))
{
m_motionVectorUVMap->SetSize(renderContext.viewportSizeX, renderContext.viewportSizeY);
m_isFirstFrame = true;
}
m_state.mainTexture = m_framebuffer.GetColorAttachmentTexture(m_previousFrameBuffer, 0);
// First evaluate per-frame code
PerFrameUpdate();
glViewport(0, 0, renderContext.viewportSizeX, renderContext.viewportSizeY);
m_framebuffer.Bind(m_previousFrameBuffer);
// Motion vector field. Drawn to the previous frame texture before warping it.
// Only do it after drawing one frame after init or resize.
if (!m_isFirstFrame)
{
m_motionVectors.Draw(m_perFrameContext, m_motionVectorUVMap->Texture());
}
// y-flip the previous frame and assign the flipped texture as "main"
m_flipTexture->Draw(m_framebuffer.GetColorAttachmentTexture(m_previousFrameBuffer, 0), nullptr, true, false);
m_state.mainTexture = m_flipTexture->Texture();
// We now draw to the current framebuffer.
m_framebuffer.Bind(m_currentFrameBuffer);
// Add motion vector u/v texture for the warp mesh draw and clean both buffers.
m_framebuffer.SetAttachment(m_currentFrameBuffer, 1, m_motionVectorUVMap);
// Draw previous frame image warped via per-pixel mesh and warp shader
m_perPixelMesh.Draw(m_state, m_perFrameContext, m_perPixelContext);
// Remove the u/v texture from the framebuffer.
m_framebuffer.RemoveColorAttachment(m_currentFrameBuffer, 1);
// Update blur textures
{
const auto warpedImage = m_framebuffer.GetColorAttachmentTexture(m_previousFrameBuffer, 0);
assert(warpedImage.get());
m_state.blurTexture.Update(*warpedImage, m_perFrameContext);
}
// Draw audio-data-related stuff
for (auto& shape : m_customShapes)
{
shape->Draw();
}
for (auto& wave : m_customWaveforms)
{
wave->Draw(m_perFrameContext);
}
m_waveform.Draw(m_perFrameContext);
// Done in DrawSprites() in Milkdrop
if (*m_perFrameContext.darken_center > 0)
{
m_darkenCenter.Draw();
}
m_border.Draw(m_perFrameContext);
// y-flip the image for final compositing again
m_flipTexture->Draw(m_framebuffer.GetColorAttachmentTexture(m_currentFrameBuffer, 0), nullptr, true, false);
m_state.mainTexture = m_flipTexture->Texture();
// We no longer need the previous frame image, use it to render the final composite.
m_framebuffer.BindRead(m_currentFrameBuffer);
m_framebuffer.BindDraw(m_previousFrameBuffer);
m_finalComposite.Draw(m_state, m_perFrameContext);
if (!m_finalComposite.HasCompositeShader())
{
// Flip texture again in "previous" framebuffer as old-school effects are still upside down.
m_flipTexture->Draw(m_framebuffer.GetColorAttachmentTexture(m_previousFrameBuffer, 0), m_framebuffer, m_previousFrameBuffer, true, false);
}
// Swap framebuffer IDs for the next frame.
std::swap(m_currentFrameBuffer, m_previousFrameBuffer);
m_isFirstFrame = false;
}
auto MilkdropPreset::OutputTexture() const -> std::shared_ptr<Renderer::Texture>
{
// the composited image is always stored in the "current" framebuffer after a frame is rendered.
return m_flipTexture->Texture();
}
void MilkdropPreset::DrawInitialImage(const std::shared_ptr<Renderer::Texture>& image, const Renderer::RenderContext& renderContext)
{
m_framebuffer.SetSize(renderContext.viewportSizeX, renderContext.viewportSizeY);
// Render to previous framebuffer, as this is the image used to draw the next frame on.
m_flipTexture->Draw(image, m_framebuffer, m_previousFrameBuffer);
}
void MilkdropPreset::BindFramebuffer()
{
if (m_framebuffer.Width() > 0 && m_framebuffer.Height() > 0)
{
m_framebuffer.BindDraw(m_previousFrameBuffer);
}
}
void MilkdropPreset::PerFrameUpdate()
{
m_perFrameContext.LoadStateVariables(m_state);
m_perPixelContext.LoadStateReadOnlyVariables(m_state, m_perFrameContext);
m_perFrameContext.ExecutePerFrameCode();
m_perPixelContext.LoadPerFrameQVariables(m_state, m_perFrameContext);
// Clamp gamma and echo zoom values
*m_perFrameContext.gamma = std::max(0.0, std::min(8.0, *m_perFrameContext.gamma));
*m_perFrameContext.echo_zoom = std::max(0.001, std::min(1000.0, *m_perFrameContext.echo_zoom));
}
void MilkdropPreset::Load(const std::string& pathname)
{
LOG_DEBUG("[MilkdropPreset] Loading preset from file \"" + pathname + "\".")
SetFilename(ParseFilename(pathname));
PresetFileParser parser;
if (!parser.Read(pathname))
{
const std::string error = "[MilkdropPreset] Could not parse preset file \"" + pathname + "\".";
LOG_ERROR(error)
throw MilkdropPresetLoadException(error);
}
InitializePreset(parser);
}
void MilkdropPreset::Load(std::istream& stream)
{
LOG_DEBUG("[MilkdropPreset] Loading preset from stream.");
PresetFileParser parser;
if (!parser.Read(stream))
{
const std::string error = "[MilkdropPreset] Could not parse preset data.";
LOG_ERROR(error)
throw MilkdropPresetLoadException(error);
}
InitializePreset(parser);
}
void MilkdropPreset::InitializePreset(PresetFileParser& parsedFile)
{
// Create the offscreen rendering surfaces.
m_motionVectorUVMap = std::make_shared<Renderer::TextureAttachment>(GL_RG16F, GL_RG, GL_FLOAT, 0, 0);
m_framebuffer.CreateColorAttachment(0, 0); // Main image 1
m_framebuffer.CreateColorAttachment(1, 0); // Main image 2
Renderer::Framebuffer::Unbind();
// Load global init variables into the state
m_state.Initialize(parsedFile);
// Register code context variables
m_perFrameContext.RegisterBuiltinVariables();
m_perPixelContext.RegisterBuiltinVariables();
// Custom waveforms:
for (int i = 0; i < CustomWaveformCount; i++)
{
auto wave = std::make_unique<CustomWaveform>(m_state);
wave->Initialize(parsedFile, i);
m_customWaveforms[i] = std::move(wave);
}
// Custom shapes:
for (int i = 0; i < CustomShapeCount; i++)
{
auto shape = std::make_unique<CustomShape>(m_state);
shape->Initialize(parsedFile, i);
m_customShapes[i] = std::move(shape);
}
// Preload shaders
LoadShaderCode();
}
void MilkdropPreset::CompileCodeAndRunInitExpressions()
{
// Per-frame init and code
m_perFrameContext.LoadStateVariables(m_state);
m_perFrameContext.EvaluateInitCode(m_state);
m_perFrameContext.CompilePerFrameCode(m_state.perFrameCode);
// Per-vertex code
m_perPixelContext.CompilePerPixelCode(m_state.perPixelCode);
for (int i = 0; i < CustomWaveformCount; i++)
{
auto& wave = m_customWaveforms[i];
wave->CompileCodeAndRunInitExpressions(m_perFrameContext);
}
for (int i = 0; i < CustomShapeCount; i++)
{
auto& shape = m_customShapes[i];
shape->CompileCodeAndRunInitExpressions();
}
}
void MilkdropPreset::LoadShaderCode()
{
m_perPixelMesh.LoadWarpShader(m_state);
m_finalComposite.LoadCompositeShader(m_state);
}
auto MilkdropPreset::ParseFilename(const std::string& filename) -> std::string
{
const std::size_t start = filename.find_last_of('/');
if (start == std::string::npos || start >= (filename.length() - 1))
{
return "";
}
return filename.substr(start + 1, filename.length());
}
} // namespace MilkdropPreset
} // namespace libprojectM