Skip to content

Commit

Permalink
feat: 内联常量改为使用全局只读变量实现
Browse files Browse the repository at this point in the history
这可以避免宏定义引起的名字冲突,如 #678
  • Loading branch information
Blinue committed Jan 2, 2025
1 parent c102cd5 commit ca68838
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 37 deletions.
4 changes: 1 addition & 3 deletions src/Magpie.Core/EffectCacheManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ static constexpr uint32_t MAX_CACHE_COUNT = 127;

// 缓存版本
// 当缓存文件结构有更改时更新它,使旧缓存失效
static constexpr uint32_t EFFECT_CACHE_VERSION = 13;
static constexpr uint32_t EFFECT_CACHE_VERSION = 14;


static std::wstring GetLinearEffectName(std::wstring_view effectName) {
Expand Down Expand Up @@ -295,5 +295,3 @@ std::wstring EffectCacheManager::GetHash(std::string& source, const phmap::flat_
}

}


73 changes: 39 additions & 34 deletions src/Magpie.Core/EffectCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1140,39 +1140,6 @@ static uint32_t GeneratePassSource(
}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// 内联常量
//
////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isInlineParams && inlineParams) {
phmap::flat_hash_set<std::wstring> paramNames;
for (const auto& d : desc.params) {
const std::wstring& name = *paramNames.emplace(StrHelper::UTF8ToUTF16(d.name)).first;

auto it = inlineParams->find(name);
if (it == inlineParams->end()) {
if (d.constant.index() == 0) {
macros.emplace_back(d.name, std::to_string(std::get<0>(d.constant).defaultValue));
} else {
macros.emplace_back(d.name, std::to_string(std::get<1>(d.constant).defaultValue));
}
} else {
if (d.constant.index() == 0) {
macros.emplace_back(d.name, std::to_string(it->second));
} else {
macros.emplace_back(d.name, std::to_string((int)std::lroundf(it->second)));
}
}
}

for (const auto& pair : *inlineParams) {
if (!paramNames.contains(pair.first)) {
return 1;
}
}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// 内置函数
Expand Down Expand Up @@ -1363,7 +1330,45 @@ static uint32_t CompilePasses(
}
}

cbHlsl.append("};\n");
cbHlsl.append("};\n\n");

if (desc.flags & EffectFlags::InlineParams) {
phmap::flat_hash_set<std::wstring> paramNames;
for (const auto& d : desc.params) {
cbHlsl.append("static const ")
.append(d.constant.index() == 0 ? "float " : "int ")
.append(d.name)
.append(" = ");

const std::wstring& name = *paramNames.emplace(StrHelper::UTF8ToUTF16(d.name)).first;

auto it = inlineParams->find(name);
if (it == inlineParams->end()) {
if (d.constant.index() == 0) {
cbHlsl.append(std::to_string(std::get<0>(d.constant).defaultValue)).append("f");
} else {
cbHlsl.append(std::to_string(std::get<1>(d.constant).defaultValue));
}
} else {
if (d.constant.index() == 0) {
cbHlsl.append(std::to_string(it->second)).append("f");
} else {
cbHlsl.append(std::to_string((int)std::lroundf(it->second)));
}
}

cbHlsl.append(";\n");
}

// 检查 inlineParams 是否存在非法参数
for (const auto& pair : *inlineParams) {
if (!paramNames.contains(pair.first)) {
return 1;
}
}

cbHlsl.append("\n");
}

if (desc.flags & EffectFlags::UseDynamic) {
cbHlsl.append("cbuffer __CB2 : register(b1) { uint __frameCount; };\n\n");
Expand Down
3 changes: 3 additions & 0 deletions src/Magpie/AppSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,8 @@ bool AppSettings::_Save(const _AppSettingsData& data) noexcept {
writer.Bool(data._isDeveloperMode);
writer.Key("debugMode");
writer.Bool(data._isDebugMode);
writer.Key("perfTestMode");
writer.Bool(data._isPerfTestMode);
writer.Key("disableEffectCache");
writer.Bool(data._isEffectCacheDisabled);
writer.Key("disableFontCache");
Expand Down Expand Up @@ -648,6 +650,7 @@ void AppSettings::_LoadSettings(const rapidjson::GenericObject<true, rapidjson::
}
JsonHelper::ReadBool(root, "developerMode", _isDeveloperMode);
JsonHelper::ReadBool(root, "debugMode", _isDebugMode);
JsonHelper::ReadBool(root, "perfTestMode", _isPerfTestMode);
JsonHelper::ReadBool(root, "disableEffectCache", _isEffectCacheDisabled);
JsonHelper::ReadBool(root, "disableFontCache", _isFontCacheDisabled);
JsonHelper::ReadBool(root, "saveEffectSources", _isSaveEffectSources);
Expand Down

0 comments on commit ca68838

Please sign in to comment.