|
19 | 19 |
|
20 | 20 | #include "Factory.hpp" |
21 | 21 | #include "LineGeometry.hpp" |
22 | | -@@ -80,6 +88,111 @@ |
| 22 | +@@ -80,6 +88,118 @@ |
23 | 23 | m_finalComposite.CompileCompositeShader(m_state); |
24 | 24 | } |
25 | 25 |
|
|
31 | 31 | +{ |
32 | 32 | + int W{0}, H{0}, CW{0}, CH{0}, S{0}; |
33 | 33 | + GLuint lTex[2]{}, lFbo[2]{}, dTex{}, dFbo{}, hcTex{}, hcFbo{}, vao{}; |
34 | | -+ GLuint progDown{}, progCombine{}, progInject{}, progReanchor{}; |
| 34 | ++ GLuint progDown{}, progCombine{}, progInject{}; |
35 | 35 | + std::shared_ptr<libprojectM::Renderer::Texture> lWrap[2]; |
36 | 36 | + libprojectM::Renderer::CopyTexture flip; |
37 | 37 | + bool valid{false}; |
|
75 | 75 | +)"; |
76 | 76 | +const char* CombineFragment = R"(#version 330 |
77 | 77 | +uniform sampler2D Lw; uniform sampler2D Hw; uniform sampler2D D; uniform float alpha; uniform vec2 native; out vec4 o; |
78 | | -+void main() { vec2 uv = gl_FragCoord.xy / native; o = texture(Lw, uv); |
79 | | -+ if (alpha != 0.0) o += alpha * (texelFetch(Hw, ivec2(gl_FragCoord.xy), 0) - texture(D, uv)); } |
80 | | -+)"; |
81 | | -+// Correct the mean after RGBA8 clipping without creating negative values that |
82 | | -+// would clip again: contract toward black when too bright, toward white when |
83 | | -+// too dark. Use a constant affine transform per canvas cell, per channel. |
84 | | -+const char* ReanchorFragment = R"(#version 330 |
85 | | -+uniform sampler2D Lw; uniform sampler2D Hc; uniform sampler2D D; uniform int S; out vec4 o; |
| 78 | ++vec4 Base(ivec2 p) { return texture(Lw, (vec2(p) + vec2(0.5)) / native); } |
| 79 | ++vec4 Band(ivec2 p) { return texelFetch(Hw, p, 0) - texture(D, (vec2(p) + vec2(0.5)) / native); } |
86 | 80 | +void main() { |
87 | | -+ ivec2 p = ivec2(gl_FragCoord.xy); ivec2 c = p / S; |
88 | | -+ vec3 target = texelFetch(Lw, c, 0).rgb; |
89 | | -+ vec3 mean = texelFetch(D, c, 0).rgb; |
90 | | -+ vec3 h = texelFetch(Hc, p, 0).rgb; |
91 | | -+ vec3 darker = h * target / max(mean, vec3(1.0 / 255.0)); |
92 | | -+ vec3 lighter = vec3(1.0) - (vec3(1.0) - h) * (vec3(1.0) - target) |
93 | | -+ / max(vec3(1.0) - mean, vec3(1.0 / 255.0)); |
94 | | -+ o = vec4(mix(darker, lighter, step(mean, target)), 1.0); |
| 81 | ++ ivec2 p = ivec2(gl_FragCoord.xy); o = Base(p); |
| 82 | ++ if (alpha != 0.0) { |
| 83 | ++ int S = int(native.x) / textureSize(Lw, 0).x; ivec2 cell = (p / S) * S; |
| 84 | ++ // bilinear_up(box(Hw)) is not a projection: remove the residual's |
| 85 | ++ // actual block mean before limiting it. This also removes D's rounding. |
| 86 | ++ vec4 mean = vec4(0.0); |
| 87 | ++ for (int j = 0; j < S; ++j) for (int i = 0; i < S; ++i) |
| 88 | ++ mean += Band(cell + ivec2(i, j)); |
| 89 | ++ mean /= float(S * S); |
| 90 | ++ // One gain per block and channel preserves the centered band's zero |
| 91 | ++ // mean. Choose the largest gain <= alpha that fits every pixel's |
| 92 | ++ // headroom; independent per-pixel clamps would introduce another bias. |
| 93 | ++ vec4 gain = vec4(alpha); |
| 94 | ++ for (int j = 0; j < S; ++j) for (int i = 0; i < S; ++i) { |
| 95 | ++ ivec2 q = cell + ivec2(i, j); vec4 b = Base(q); vec4 d = Band(q) - mean; |
| 96 | ++ vec4 positive = (vec4(1.0) - b) / max(d, vec4(1e-6)); |
| 97 | ++ vec4 negative = b / max(-d, vec4(1e-6)); |
| 98 | ++ gain = min(gain, mix(positive, negative, lessThan(d, vec4(0.0)))); |
| 99 | ++ } |
| 100 | ++ o += gain * (Band(p) - mean); |
| 101 | ++ } |
95 | 102 | +} |
96 | 103 | +)"; |
97 | 104 | +// Geometry enters the authored state as MilkDrop rasterizes it at canvas size: the native pixel at each |
|
131 | 138 | void MilkdropPreset::RenderFrame(const libprojectM::Audio::FrameAudioData& audioData, const Renderer::RenderContext& renderContext) |
132 | 139 | { |
133 | 140 | m_state.audioData = audioData; |
134 | | -@@ -103,7 +216,7 @@ |
| 141 | +@@ -103,7 +223,7 @@ |
135 | 142 |
|
136 | 143 | // Above the line reference size, the warp reads the previous frame through a small blur that gives |
137 | 144 | // back the per-frame smoothing feedback presets get at the reference size (see FeedbackDiffusion.hpp). |
|
140 | 147 | renderContext.lineReferenceWidth, renderContext.lineReferenceHeight) : 0.0f)) |
141 | 148 | { |
142 | 149 | m_diffusionHoldsPreviousFrame = false; |
143 | | -@@ -149,6 +262,45 @@ |
| 150 | +@@ -149,6 +269,45 @@ |
144 | 151 | // here on, so this can happen before the warp: the warp and everything drawn on top of it |
145 | 152 | // then go to the current framebuffer in one render pass. Not if the warp shader reads the |
146 | 153 | // blur textures: it gets last frame's, as in projectM, so they are updated after the warp. |
|
166 | 173 | + detail->dTex = MakeTexture(canvas.width, canvas.height, GL_LINEAR); detail->dFbo = MakeFbo(detail->dTex); |
167 | 174 | + detail->hcTex = MakeTexture(detail->W, detail->H, GL_NEAREST); detail->hcFbo = MakeFbo(detail->hcTex); |
168 | 175 | + glGenVertexArrays(1, &detail->vao); |
169 | | -+ detail->progDown = MakeProgram(DownFragment); detail->progCombine = MakeProgram(CombineFragment); detail->progInject = MakeProgram(InjectFragment); detail->progReanchor = MakeProgram(ReanchorFragment); |
| 176 | ++ detail->progDown = MakeProgram(DownFragment); detail->progCombine = MakeProgram(CombineFragment); detail->progInject = MakeProgram(InjectFragment); |
170 | 177 | + detail->valid = false; |
171 | 178 | + } |
172 | 179 | + if (!detail->valid || m_isFirstFrame) |
|
186 | 193 | auto const updateBlur = [this]() { |
187 | 194 | const auto warpedImage = m_framebuffer.GetColorAttachmentTexture(m_previousFrameBuffer, 0); |
188 | 195 | assert(warpedImage.get()); |
189 | | -@@ -156,7 +308,13 @@ |
| 196 | +@@ -156,7 +315,13 @@ |
190 | 197 | LineScale(m_state.renderContext.viewportSizeX, m_state.renderContext.viewportSizeY, |
191 | 198 | m_state.renderContext.lineReferenceWidth, m_state.renderContext.lineReferenceHeight)); |
192 | 199 | }; |
|
201 | 208 | { |
202 | 209 | updateBlur(); |
203 | 210 | } |
204 | | -@@ -178,7 +336,16 @@ |
| 211 | +@@ -178,7 +343,16 @@ |
205 | 212 | } |
206 | 213 |
|
207 | 214 | // Draw previous frame image warped via per-pixel mesh and warp shader |
|
219 | 226 |
|
220 | 227 | if (writeMotionUV) |
221 | 228 | { |
222 | | -@@ -188,22 +355,119 @@ |
| 229 | +@@ -188,22 +362,97 @@ |
223 | 230 | glDrawBuffers(2, buffers); |
224 | 231 | } |
225 | 232 |
|
|
273 | 280 | + glUniform1f(glGetUniformLocation(detail->progCombine, "alpha"), detailAlpha); |
274 | 281 | + glUniform2f(glGetUniformLocation(detail->progCombine, "native"), static_cast<float>(detail->W), static_cast<float>(detail->H)); |
275 | 282 | + FullScreen(*detail, detail->progCombine, detail->hcFbo, detail->W, detail->H); |
276 | | -+ // Re-anchor the stored (already clamped) detail result. Standard keeps |
277 | | -+ // its original path, including bilinear reconstruction between cells. |
278 | | -+ if (detailAlpha > 0.0f) |
279 | | -+ { |
280 | | -+ glUseProgram(detail->progDown); BindTex(detail->progDown, "src", 0, detail->hcTex); |
281 | | -+ glUniform1i(glGetUniformLocation(detail->progDown, "S"), detail->S); |
282 | | -+ FullScreen(*detail, detail->progDown, detail->dFbo, detail->CW, detail->CH); |
283 | | -+ glUseProgram(detail->progReanchor); |
284 | | -+ BindTex(detail->progReanchor, "Lw", 0, detail->lTex[1]); |
285 | | -+ BindTex(detail->progReanchor, "Hc", 1, detail->hcTex); |
286 | | -+ BindTex(detail->progReanchor, "D", 2, detail->dTex); |
287 | | -+ glUniform1i(glGetUniformLocation(detail->progReanchor, "S"), detail->S); |
288 | | -+ m_framebuffer.Bind(m_currentFrameBuffer); |
289 | | -+ GLint targetFbo = 0; glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &targetFbo); |
290 | | -+ FullScreen(*detail, detail->progReanchor, static_cast<GLuint>(targetFbo), detail->W, detail->H); |
291 | | -+ // Injection must subtract the corrected pre-geometry picture. |
292 | | -+ m_framebuffer.BindRead(m_currentFrameBuffer); |
293 | | -+ glBindFramebuffer(GL_DRAW_FRAMEBUFFER, detail->hcFbo); |
294 | | -+ } |
295 | | -+ else |
296 | | -+ { |
297 | | -+ glBindFramebuffer(GL_READ_FRAMEBUFFER, detail->hcFbo); |
298 | | -+ m_framebuffer.BindDraw(m_currentFrameBuffer); |
299 | | -+ } |
| 283 | ++ // Put H_c into the current frame, then draw this frame's geometry on it as usual. |
| 284 | ++ glBindFramebuffer(GL_READ_FRAMEBUFFER, detail->hcFbo); |
| 285 | ++ m_framebuffer.BindDraw(m_currentFrameBuffer); |
300 | 286 | + glBlitFramebuffer(0, 0, detail->W, detail->H, 0, 0, detail->W, detail->H, GL_COLOR_BUFFER_BIT, GL_NEAREST); |
301 | | -+ m_framebuffer.Bind(m_currentFrameBuffer); |
302 | 287 | + if (std::getenv("PM_DETAIL_DEBUG")) |
303 | 288 | + { |
304 | 289 | + fprintf(stderr, "blit: Hw-before? current-after %.5f hc %.5f err 0x%x\n", |
|
340 | 325 |
|
341 | 326 | // Done in DrawSprites() in Milkdrop |
342 | 327 | if (*m_perFrameContext.darken_center > 0) |
343 | | -@@ -218,6 +482,34 @@ |
| 328 | +@@ -218,6 +467,34 @@ |
344 | 329 | m_framebuffer.RemoveColorAttachment(m_currentFrameBuffer, 1); |
345 | 330 | } |
346 | 331 |
|
|
375 | 360 | // Todo: Song title anim would go here |
376 | 361 |
|
377 | 362 | // y-flip the image for final compositing again. With diffusion, the same pass also filters |
378 | | ---- a/src/libprojectM/MilkdropPreset/MilkdropShader.cpp |
379 | | -+++ b/src/libprojectM/MilkdropPreset/MilkdropShader.cpp |
| 363 | +diff --color -ruN engine-main/src/libprojectM/MilkdropPreset/MilkdropShader.cpp engine-mdetail-a/src/libprojectM/MilkdropPreset/MilkdropShader.cpp |
| 364 | +--- engine-main/src/libprojectM/MilkdropPreset/MilkdropShader.cpp 2026-10-05 02:21:33 |
| 365 | ++++ engine-mdetail-a/src/libprojectM/MilkdropPreset/MilkdropShader.cpp 2026-10-05 02:21:33 |
380 | 366 | @@ -16,10 +16,13 @@ |
381 | 367 |
|
382 | 368 | #include <algorithm> |
|
425 | 411 | tempMatrices[i] = randomTranslation * rotationX; |
426 | 412 | tempMatrices[i] = rotationZ * tempMatrices[i]; |
427 | 413 | tempMatrices[i] = rotationY * tempMatrices[i]; |
428 | | -- } |
429 | 414 | + cached.matrices[i - 20] = tempMatrices[i]; |
430 | | -+ } |
| 415 | + } |
431 | 416 | + cached.frame = presetState.renderContext.frame; |
432 | 417 |
|
433 | 418 | m_shader.SetUniformMat3x4("rot_s1", tempMatrices[0]); |
434 | 419 | m_shader.SetUniformMat3x4("rot_s2", tempMatrices[1]); |
435 | | ---- a/src/libprojectM/MilkdropPreset/PerPixelMesh.cpp |
436 | | -+++ b/src/libprojectM/MilkdropPreset/PerPixelMesh.cpp |
437 | | -@@ -139,6 +139,17 @@ |
438 | | - |
439 | | - // Render the resulting mesh. |
| 420 | +diff --color -ruN engine-main/src/libprojectM/MilkdropPreset/PerPixelMesh.cpp engine-mdetail-a/src/libprojectM/MilkdropPreset/PerPixelMesh.cpp |
| 421 | +--- engine-main/src/libprojectM/MilkdropPreset/PerPixelMesh.cpp 2026-10-05 02:15:46 |
| 422 | ++++ engine-mdetail-a/src/libprojectM/MilkdropPreset/PerPixelMesh.cpp 2026-10-05 02:35:04 |
| 423 | +@@ -141,6 +141,17 @@ |
440 | 424 | WarpedBlit(presetState, perFrameContext); |
441 | | -+} |
442 | | -+ |
| 425 | + } |
| 426 | + |
443 | 427 | +void PerPixelMesh::Prepare(const PresetState& presetState, const PerFrameContext& perFrameContext, PerPixelContext& perPixelContext) |
444 | 428 | +{ |
445 | 429 | + if (presetState.renderContext.viewportSizeX == 0 || presetState.renderContext.viewportSizeY == 0 || |
|
449 | 433 | + } |
450 | 434 | + InitializeMesh(presetState); |
451 | 435 | + CalculateMesh(presetState, perFrameContext, perPixelContext); |
452 | | - } |
453 | | - |
| 436 | ++} |
| 437 | ++ |
454 | 438 | void PerPixelMesh::InitializeMesh(const PresetState& presetState) |
455 | | ---- a/src/libprojectM/MilkdropPreset/PerPixelMesh.hpp |
456 | | -+++ b/src/libprojectM/MilkdropPreset/PerPixelMesh.hpp |
457 | | -@@ -47,6 +47,11 @@ |
458 | | - * @param presetState The preset state to retrieve the configuration values from. |
| 439 | + { |
| 440 | + if (m_gridSizeX != presetState.renderContext.perPixelMeshX || |
| 441 | +diff --color -ruN engine-main/src/libprojectM/MilkdropPreset/PerPixelMesh.hpp engine-mdetail-a/src/libprojectM/MilkdropPreset/PerPixelMesh.hpp |
| 442 | +--- engine-main/src/libprojectM/MilkdropPreset/PerPixelMesh.hpp 2026-10-05 02:15:46 |
| 443 | ++++ engine-mdetail-a/src/libprojectM/MilkdropPreset/PerPixelMesh.hpp 2026-10-05 02:35:04 |
| 444 | +@@ -48,6 +48,11 @@ |
459 | 445 | */ |
460 | 446 | void CompileWarpShader(PresetState& presetState); |
461 | | -+ |
| 447 | + |
462 | 448 | + /** PROTOTYPE detail layer: draw this frame's already calculated mesh again (other target/main texture). */ |
463 | 449 | + void DrawAgain(const PresetState& presetState, const PerFrameContext& perFrameContext) { WarpedBlit(presetState, perFrameContext); } |
464 | 450 | + // Detail layer: evaluate the per-vertex equations without drawing (DrawAgain() draws the result). |
465 | 451 | + void Prepare(const PresetState& presetState, const PerFrameContext& perFrameContext, PerPixelContext& perPixelContext); |
466 | | - |
| 452 | ++ |
467 | 453 | /** |
468 | 454 | * @brief True if the compiled warp shader reads the main texture through a point sampler. |
| 455 | + */ |
0 commit comments