From 327fad55e4b482217f6cba876a7d0700c2972b34 Mon Sep 17 00:00:00 2001 From: Kai Blaschke Date: Wed, 11 Dec 2024 18:16:41 +0100 Subject: [PATCH] Add both long and short sampler names for random textures. If a preset uses different long-form samplers for a single random slot, this will result in a failure, e.g. using both "sampler_rand00_something" and "sampler_rand00_else". As this also isn't supported by Milkdrop and would create a conflict situation, we don't care. --- .../MilkdropPreset/MilkdropShader.cpp | 27 +++++++++++++++++++ .../Renderer/TextureSamplerDescriptor.cpp | 9 +++++++ 2 files changed, 36 insertions(+) diff --git a/src/libprojectM/MilkdropPreset/MilkdropShader.cpp b/src/libprojectM/MilkdropPreset/MilkdropShader.cpp index 030729dc1..1dadec340 100644 --- a/src/libprojectM/MilkdropPreset/MilkdropShader.cpp +++ b/src/libprojectM/MilkdropPreset/MilkdropShader.cpp @@ -517,6 +517,33 @@ void MilkdropShader::GetReferencedSamplers(const std::string& program) found = program.find("texsize_", found); } + { + // Remove duplicate mentions or "randXX" names, keeping the long forms only (first one will determine the actual texture loaded). + auto samplerName = m_samplerNames.begin(); + std::locale loc; + while (samplerName != m_samplerNames.end()) + { + std::string lowerCaseName = Utils::ToLower(*samplerName); + if (lowerCaseName.length() == 6 && + lowerCaseName.substr(0, 4) == "rand" && std::isdigit(lowerCaseName.at(4), loc) && std::isdigit(lowerCaseName.at(5), loc)) + { + auto additionalName = samplerName; + additionalName++; + if (additionalName != m_samplerNames.end()) + { + std::string addLowerCaseName = Utils::ToLower(*additionalName); + if (addLowerCaseName.length() > 7 && + addLowerCaseName.substr(0, 6) == lowerCaseName && + addLowerCaseName[6] == '_') + { + samplerName = m_samplerNames.erase(samplerName); + } + } + } + samplerName++; + } + } + if (program.find("GetBlur3") != std::string::npos) { UpdateMaxBlurLevel(BlurTexture::BlurLevel::Blur3); diff --git a/src/libprojectM/Renderer/TextureSamplerDescriptor.cpp b/src/libprojectM/Renderer/TextureSamplerDescriptor.cpp index 75592c6fd..b9a7870f9 100644 --- a/src/libprojectM/Renderer/TextureSamplerDescriptor.cpp +++ b/src/libprojectM/Renderer/TextureSamplerDescriptor.cpp @@ -95,6 +95,15 @@ auto TextureSamplerDescriptor::SamplerDeclaration() const -> std::string declaration.append(m_samplerName); declaration.append(";\n"); + // Add short sampler name for prefixed random textures. + // E.g. "sampler_rand00" if a sampler "sampler_rand00_smalltiled" was declared + if (m_samplerName.substr(0, 4) == "rand" && m_samplerName.length() > 7 && m_samplerName.at(6) == '_') + { + declaration.append("uniform sampler2D sampler_"); + declaration.append(m_samplerName.substr(0, 6)); + declaration.append(";\n"); + } + return declaration; }