From bae66e75d928fb175e879428ddb1d093eea7634f Mon Sep 17 00:00:00 2001 From: Xu Date: Tue, 7 Jan 2025 14:39:13 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20FX=20=E6=94=AF=E6=8C=81=20USE=20?= =?UTF-8?q?=E6=8C=87=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Effects/ACNet.hlsl | 13 +- src/Effects/Anime4K/Anime4K_Restore_L.hlsl | 2 +- .../Anime4K/Anime4K_Restore_Soft_UL.hlsl | 2 +- src/Effects/Anime4K/Anime4K_Restore_UL.hlsl | 2 +- .../Anime4K/Anime4K_Upscale_Denoise_UL.hlsl | 2 +- src/Effects/Anime4K/Anime4K_Upscale_L.hlsl | 3 +- src/Effects/Anime4K/Anime4K_Upscale_VL.hlsl | 2 +- src/Effects/CRT/CRT_Geom.hlsl | 2 +- src/Effects/CuNNy/CuNNy-16x16C-NVL.hlsl | 2 +- src/Effects/CuNNy/CuNNy-6x8C-NVL.hlsl | 2 +- src/Effects/FSRCNNX/FSRCNNX.hlsl | 2 +- src/Magpie.Core/DirectXHelper.cpp | 2 +- src/Magpie.Core/EffectCacheManager.cpp | 4 +- src/Magpie.Core/EffectCompiler.cpp | 112 +++++++++++------- src/Magpie.Core/EffectDrawer.cpp | 4 +- src/Magpie.Core/Renderer.cpp | 50 ++++---- src/Magpie.Core/Renderer.h | 1 - src/Magpie.Core/include/EffectCompiler.h | 3 +- src/Magpie.Core/include/EffectDesc.h | 11 +- 19 files changed, 129 insertions(+), 92 deletions(-) diff --git a/src/Effects/ACNet.hlsl b/src/Effects/ACNet.hlsl index 80234d5e2..eb9ad79f2 100644 --- a/src/Effects/ACNet.hlsl +++ b/src/Effects/ACNet.hlsl @@ -1,10 +1,9 @@ // ACNet // 移植自 https://github.com/TianZerL/ACNetGLSL/blob/f20a6b6b7327f4caf588b06c6b21f18e40dae1ce/glsl/ACNet.glsl - //!MAGPIE EFFECT //!VERSION 4 -//!USE_FP16 +//!USE FP16 #include "StubDefs.hlsli" @@ -40,7 +39,6 @@ Texture2D tex3; //!FORMAT R16G16B16A16_FLOAT Texture2D tex4; - //!SAMPLER //!FILTER POINT SamplerState sam; @@ -49,9 +47,12 @@ SamplerState sam; //!FILTER LINEAR SamplerState sam1; - //!COMMON +#ifdef MP_DEBUG +#pragma warning(disable: 4714) // X4714: sum of temp registers and indexable temp registers times 256 threads exceeds the recommended total 16384. Performance may be reduced +#endif + #define RELU(x) max(x, 0) MF4 MulAdd(MF3 x, MF3x4 y, MF4 a) { @@ -2542,7 +2543,7 @@ void Pass9(uint2 blockStart, uint3 threadId) { uint2 destPos = gxy + uint2(i, j); uint index = j * 2 + i; - MF luma = clamp( + MF luma = saturate( target1.x * kernelsL10[0 + index] + target1.y * kernelsL10[4 + index] + target1.z * kernelsL10[8 + index] + @@ -2550,7 +2551,7 @@ void Pass9(uint2 blockStart, uint3 threadId) { target2.x * kernelsL10[16 + index] + target2.y * kernelsL10[20 + index] + target2.z * kernelsL10[24 + index] + - target2.w * kernelsL10[28 + index], 0.0, 1.0); + target2.w * kernelsL10[28 + index]); MF2 originUV = mul(rgb2uv, INPUT.SampleLevel(sam1, (destPos + 0.5f) * outputPt, 0).rgb); OUTPUT[destPos] = MF4(mul(yuv2rgb, MF3(luma, originUV)), 1); diff --git a/src/Effects/Anime4K/Anime4K_Restore_L.hlsl b/src/Effects/Anime4K/Anime4K_Restore_L.hlsl index c22b2a41f..957380f88 100644 --- a/src/Effects/Anime4K/Anime4K_Restore_L.hlsl +++ b/src/Effects/Anime4K/Anime4K_Restore_L.hlsl @@ -3,7 +3,7 @@ //!MAGPIE EFFECT //!VERSION 4 //!SORT_NAME Anime4K_Restore_2 -//!USE_FP16 +//!USE FP16 #include "..\StubDefs.hlsli" diff --git a/src/Effects/Anime4K/Anime4K_Restore_Soft_UL.hlsl b/src/Effects/Anime4K/Anime4K_Restore_Soft_UL.hlsl index 20e93d47d..435be6759 100644 --- a/src/Effects/Anime4K/Anime4K_Restore_Soft_UL.hlsl +++ b/src/Effects/Anime4K/Anime4K_Restore_Soft_UL.hlsl @@ -4,7 +4,7 @@ //!MAGPIE EFFECT //!VERSION 4 //!SORT_NAME Anime4K_Restore_Soft_4 -//!USE_FP16 +//!USE FP16 #include "..\StubDefs.hlsli" diff --git a/src/Effects/Anime4K/Anime4K_Restore_UL.hlsl b/src/Effects/Anime4K/Anime4K_Restore_UL.hlsl index 4863ae778..36f476966 100644 --- a/src/Effects/Anime4K/Anime4K_Restore_UL.hlsl +++ b/src/Effects/Anime4K/Anime4K_Restore_UL.hlsl @@ -4,7 +4,7 @@ //!MAGPIE EFFECT //!VERSION 4 //!SORT_NAME Anime4K_Restore_4 -//!USE_FP16 +//!USE FP16 #include "..\StubDefs.hlsli" diff --git a/src/Effects/Anime4K/Anime4K_Upscale_Denoise_UL.hlsl b/src/Effects/Anime4K/Anime4K_Upscale_Denoise_UL.hlsl index e1a523612..b32ee3af4 100644 --- a/src/Effects/Anime4K/Anime4K_Upscale_Denoise_UL.hlsl +++ b/src/Effects/Anime4K/Anime4K_Upscale_Denoise_UL.hlsl @@ -4,7 +4,7 @@ //!MAGPIE EFFECT //!VERSION 4 //!SORT_NAME Anime4K_Upscale_Denoise_3 -//!USE_FP16 +//!USE FP16 #include "..\StubDefs.hlsli" diff --git a/src/Effects/Anime4K/Anime4K_Upscale_L.hlsl b/src/Effects/Anime4K/Anime4K_Upscale_L.hlsl index 148e42caa..c3345818d 100644 --- a/src/Effects/Anime4K/Anime4K_Upscale_L.hlsl +++ b/src/Effects/Anime4K/Anime4K_Upscale_L.hlsl @@ -4,11 +4,10 @@ //!MAGPIE EFFECT //!VERSION 4 //!SORT_NAME Anime4K_Upscale_1 -//!USE_FP16 +//!USE FP16, MulAdd #include "..\StubDefs.hlsli" - //!TEXTURE Texture2D INPUT; diff --git a/src/Effects/Anime4K/Anime4K_Upscale_VL.hlsl b/src/Effects/Anime4K/Anime4K_Upscale_VL.hlsl index 9d7e0aeae..4218b3457 100644 --- a/src/Effects/Anime4K/Anime4K_Upscale_VL.hlsl +++ b/src/Effects/Anime4K/Anime4K_Upscale_VL.hlsl @@ -4,7 +4,7 @@ //!MAGPIE EFFECT //!VERSION 4 //!SORT_NAME Anime4K_Upscale_2 -//!USE_FP16 +//!USE FP16 #include "..\StubDefs.hlsli" diff --git a/src/Effects/CRT/CRT_Geom.hlsl b/src/Effects/CRT/CRT_Geom.hlsl index 1555cb504..ab224c445 100644 --- a/src/Effects/CRT/CRT_Geom.hlsl +++ b/src/Effects/CRT/CRT_Geom.hlsl @@ -25,7 +25,7 @@ //!MAGPIE EFFECT //!VERSION 4 -//!USE_DYNAMIC +//!USE Dynamic //!PARAMETER diff --git a/src/Effects/CuNNy/CuNNy-16x16C-NVL.hlsl b/src/Effects/CuNNy/CuNNy-16x16C-NVL.hlsl index 8247879b9..f224df2b0 100644 --- a/src/Effects/CuNNy/CuNNy-16x16C-NVL.hlsl +++ b/src/Effects/CuNNy/CuNNy-16x16C-NVL.hlsl @@ -17,7 +17,7 @@ //!MAGPIE EFFECT //!VERSION 4 //!SORT_NAME CuNNy-D16N16 -//!USE_FP16 +//!USE FP16 #include "..\StubDefs.hlsli" diff --git a/src/Effects/CuNNy/CuNNy-6x8C-NVL.hlsl b/src/Effects/CuNNy/CuNNy-6x8C-NVL.hlsl index 28b8dc182..79030ff0d 100644 --- a/src/Effects/CuNNy/CuNNy-6x8C-NVL.hlsl +++ b/src/Effects/CuNNy/CuNNy-6x8C-NVL.hlsl @@ -17,7 +17,7 @@ //!MAGPIE EFFECT //!VERSION 4 //!SORT_NAME CuNNy-D08N06 -//!USE_FP16 +//!USE FP16 //!TEXTURE diff --git a/src/Effects/FSRCNNX/FSRCNNX.hlsl b/src/Effects/FSRCNNX/FSRCNNX.hlsl index 6f3c4e10a..67364a843 100644 --- a/src/Effects/FSRCNNX/FSRCNNX.hlsl +++ b/src/Effects/FSRCNNX/FSRCNNX.hlsl @@ -4,7 +4,7 @@ //!MAGPIE EFFECT //!VERSION 4 -//!USE_FP16 +//!USE FP16 //!TEXTURE diff --git a/src/Magpie.Core/DirectXHelper.cpp b/src/Magpie.Core/DirectXHelper.cpp index 14351c3ee..0fcc8e97a 100644 --- a/src/Magpie.Core/DirectXHelper.cpp +++ b/src/Magpie.Core/DirectXHelper.cpp @@ -26,7 +26,7 @@ bool DirectXHelper::CompileComputeShader( flags |= D3DCOMPILE_SKIP_OPTIMIZATION | D3DCOMPILE_DEBUG; #else flags |= D3DCOMPILE_OPTIMIZATION_LEVEL3; -#endif // _DEBUG +#endif std::unique_ptr mc(new D3D_SHADER_MACRO[macros.size() + 1]); for (UINT i = 0; i < macros.size(); ++i) { diff --git a/src/Magpie.Core/EffectCacheManager.cpp b/src/Magpie.Core/EffectCacheManager.cpp index 70f0ee533..b16257bd2 100644 --- a/src/Magpie.Core/EffectCacheManager.cpp +++ b/src/Magpie.Core/EffectCacheManager.cpp @@ -64,7 +64,7 @@ void serialize(Archive& ar, EffectSamplerDesc& o) { template void serialize(Archive& ar, EffectPassDesc& o) { - ar& o.cso& o.inputs& o.outputs& o.numThreads[0] & o.numThreads[1] & o.numThreads[2] & o.blockSize& o.desc& o.isPSStyle; + ar& o.cso& o.inputs& o.outputs& o.numThreads[0] & o.numThreads[1] & o.numThreads[2] & o.blockSize& o.desc& o.flags; } template @@ -76,7 +76,7 @@ static constexpr uint32_t MAX_CACHE_COUNT = 127; // 缓存版本 // 当缓存文件结构有更改时更新它,使旧缓存失效 -static constexpr uint32_t EFFECT_CACHE_VERSION = 14; +static constexpr uint32_t EFFECT_CACHE_VERSION = 15; static std::wstring GetLinearEffectName(std::wstring_view effectName) { diff --git a/src/Magpie.Core/EffectCompiler.cpp b/src/Magpie.Core/EffectCompiler.cpp index 18e13f22e..ee10f52b5 100644 --- a/src/Magpie.Core/EffectCompiler.cpp +++ b/src/Magpie.Core/EffectCompiler.cpp @@ -266,11 +266,34 @@ static uint32_t GetNextExpr(std::string_view& source, std::string& expr) noexcep return 0; } -static uint32_t ResolveHeader(std::string_view block, EffectDesc& desc, bool noCompile, bool useFP16) noexcept { +static uint32_t ResolvePassFlags(std::string_view& block, uint32_t& passFlags) noexcept { + std::string_view features; + if (GetNextString(block, features)) { + return 1; + } + + for (std::string_view& feature : StrHelper::Split(features, ',')) { + StrHelper::Trim(feature); + + if (feature == "FP16") { + passFlags |= EffectPassFlags::UseFP16; + } else if (feature == "MulAdd") { + passFlags |= EffectPassFlags::UseMulAdd; + } else if (feature == "Dynamic") { + passFlags |= EffectPassFlags::UseDynamic; + } else { + return 1; + } + } + + return 0; +} + +static uint32_t ResolveHeader(std::string_view block, EffectDesc& desc, uint32_t& commonPassFlags, bool noCompile) noexcept { // 必需的选项: VERSION - // 可选的选项: USE_DYNAMIC, USE_FP16, SORT_NAME + // 可选的选项: SORT_NAME, USE - std::bitset<4> processed; + std::bitset<3> processed; std::string_view token; @@ -302,44 +325,29 @@ static uint32_t ResolveHeader(std::string_view block, EffectDesc& desc, bool noC if (GetNextToken(block, token) != 2) { return 1; } - } else if (t == "USE_DYNAMIC") { + } else if (t == "SORT_NAME") { if (processed[1]) { return 1; } processed[1] = true; - if (GetNextToken(block, token) != 2) { + std::string_view sortName; + if (GetNextString(block, sortName)) { return 1; } - desc.flags |= EffectFlags::UseDynamic; - } else if (t == "USE_FP16") { + if (noCompile) { + desc.sortName = sortName; + } + } else if (t == "USE") { if (processed[2]) { return 1; } processed[2] = true; - if (GetNextToken(block, token) != 2) { - return 1; - } - - if (useFP16) { - desc.flags |= EffectFlags::FP16; - } - } else if (t == "SORT_NAME") { - if (processed[3]) { - return 1; - } - processed[3] = true; - - std::string_view sortName; - if (GetNextString(block, sortName)) { + if (ResolvePassFlags(block, commonPassFlags)) { return 1; } - - if (noCompile) { - desc.sortName = sortName; - } } else { return 1; } @@ -788,10 +796,12 @@ static uint32_t ResolveCommon(std::string_view& block) noexcept { static uint32_t ResolvePasses( SmallVector& blocks, - EffectDesc& desc + EffectDesc& desc, + uint32_t commonPassFlags, + bool noFP16 ) noexcept { // 必选项: IN, OUT - // 可选项: BLOCK_SIZE, NUM_THREADS, STYLE + // 可选项: BLOCK_SIZE, NUM_THREADS, STYLE, USE // STYLE 为 PS 时不能有 BLOCK_SIZE 或 NUM_THREADS std::string_view token; @@ -849,6 +859,9 @@ static uint32_t ResolvePasses( std::string_view& block = blocks[i]; auto& passDesc = desc.passes[i]; + // 应用头中的标志 + passDesc.flags |= commonPassFlags; + // 用于检查输入和输出中重复的纹理 phmap::flat_hash_map texNames; texNames.reserve(desc.textures.size()); @@ -856,7 +869,7 @@ static uint32_t ResolvePasses( texNames.emplace(desc.textures[j].name, j); } - std::bitset<6> processed; + std::bitset<7> processed; while (true) { if (!CheckNextToken(block, META_INDICATOR)) { @@ -1016,7 +1029,7 @@ static uint32_t ResolvePasses( } if (val == "PS") { - passDesc.isPSStyle = true; + passDesc.flags |= EffectPassFlags::PSStyle; passDesc.blockSize.first = 16; passDesc.blockSize.second = 16; passDesc.numThreads = { 64,1,1 }; @@ -1036,6 +1049,15 @@ static uint32_t ResolvePasses( StrHelper::Trim(val); passDesc.desc = val; + } else if (t == "USE") { + if (processed[6]) { + return 1; + } + processed[6] = true; + + if (ResolvePassFlags(block, passDesc.flags)) { + return 1; + } } else { return 1; } @@ -1046,7 +1068,7 @@ static uint32_t ResolvePasses( return 1; } - if (passDesc.isPSStyle) { + if (passDesc.flags & EffectPassFlags::PSStyle) { if (processed[2] || processed[3]) { return 1; } @@ -1059,6 +1081,10 @@ static uint32_t ResolvePasses( if (passDesc.desc.empty()) { passDesc.desc = fmt::format("Pass {}", i + 1); } + + if (noFP16) { + passDesc.flags &= ~EffectPassFlags::UseFP16; + } } return 0; @@ -1090,6 +1116,10 @@ static uint32_t GeneratePassSource( // 常量缓冲区 result.append(cbHlsl); + if (passDesc.flags & EffectPassFlags::UseDynamic) { + result.append("cbuffer __CB2 : register(b1) { uint __frameCount; };\n\n"); + } + //////////////////////////////////////////////////////////////////////////////////////////////////////// // // SRV、UAV 和采样器 @@ -1130,7 +1160,7 @@ static uint32_t GeneratePassSource( macros.emplace_back("MP_NUM_THREADS_Y", std::to_string(passDesc.numThreads[1])); macros.emplace_back("MP_NUM_THREADS_Z", std::to_string(passDesc.numThreads[2])); - if (passDesc.isPSStyle) { + if (passDesc.flags & EffectPassFlags::PSStyle) { macros.emplace_back("MP_PS_STYLE", ""); } @@ -1144,7 +1174,7 @@ static uint32_t GeneratePassSource( // 用于在 FP32 和 FP16 间切换的宏 static const char* numbers[] = { "1","2","3","4" }; - if (desc.flags & EffectFlags::FP16) { + if (passDesc.flags & EffectPassFlags::UseFP16) { macros.emplace_back("MP_FP16", ""); macros.emplace_back("MF", "min16float"); @@ -1182,7 +1212,7 @@ float2 GetOutputPt() { return __outputPt; } float2 GetScale() { return __scale; } )"); - if (desc.flags & EffectFlags::UseDynamic) { + if (passDesc.flags & EffectPassFlags::UseDynamic) { result.append(R"(uint GetFrameCount() { return __frameCount; } )"); @@ -1208,7 +1238,7 @@ float2 GetScale() { return __scale; } // 着色器入口 // //////////////////////////////////////////////////////////////////////////////////////////////////////// - if (passDesc.isPSStyle) { + if (passDesc.flags & EffectPassFlags::PSStyle) { if (passDesc.outputs.size() <= 1) { std::string outputSize; std::string outputPt; @@ -1343,7 +1373,7 @@ static uint32_t CompilePasses( // PS 样式需要获知输出纹理的尺寸 // 最后一个通道不需要 for (uint32_t i = 0, end = (uint32_t)desc.passes.size() - 1; i < end; ++i) { - if (desc.passes[i].isPSStyle) { + if (desc.passes[i].flags & EffectPassFlags::PSStyle) { cbHlsl.append(fmt::format("\tuint2 __pass{0}OutputSize;\n\tfloat2 __pass{0}OutputPt;\n", i + 1)); } } @@ -1397,9 +1427,7 @@ static uint32_t CompilePasses( cbHlsl.append("\n"); } - if (desc.flags & EffectFlags::UseDynamic) { - cbHlsl.append("cbuffer __CB2 : register(b1) { uint __frameCount; };\n\n"); - } + std::wstring sourcesPathName = StrHelper::Concat(CommonSharedConstants::SOURCES_DIR, StrHelper::UTF8ToUTF16(desc.name)); std::wstring sourcesPath = sourcesPathName.substr(0, sourcesPathName.find_last_of(L'\\')); @@ -1614,7 +1642,9 @@ uint32_t EffectCompiler::Compile( return 1; } - if (ResolveHeader(headerBlock, desc, noCompile, flags & EffectCompilerFlags::FP16)) { + // 头中的标志将应用到所有通道 + uint32_t commonPassFlags = 0; + if (ResolveHeader(headerBlock, desc, commonPassFlags, noCompile)) { Logger::Get().Error("解析 Header 块失败"); return 1; } @@ -1695,7 +1725,7 @@ uint32_t EffectCompiler::Compile( } desc.passes.clear(); - if (ResolvePasses(passBlocks, desc)) { + if (ResolvePasses(passBlocks, desc, commonPassFlags, flags & EffectCompilerFlags::NoFP16)) { Logger::Get().Error("解析 Pass 块失败"); return 1; } diff --git a/src/Magpie.Core/EffectDrawer.cpp b/src/Magpie.Core/EffectDrawer.cpp index 17e2787db..90c07c488 100644 --- a/src/Magpie.Core/EffectDrawer.cpp +++ b/src/Magpie.Core/EffectDrawer.cpp @@ -289,7 +289,7 @@ bool EffectDrawer::_InitializeConstants( const size_t builtinConstantCount = 10; size_t psStylePassParams = 0; for (UINT i = 0, end = (UINT)desc.passes.size() - 1; i < end; ++i) { - if (desc.passes[i].isPSStyle) { + if (desc.passes[i].flags & EffectPassFlags::PSStyle) { psStylePassParams += 4; } } @@ -317,7 +317,7 @@ bool EffectDrawer::_InitializeConstants( EffectHelper::Constant32* pCurParam = _constants.data() + builtinConstantCount; if (psStylePassParams > 0) { for (UINT i = 0, end = (UINT)desc.passes.size() - 1; i < end; ++i) { - if (desc.passes[i].isPSStyle) { + if (desc.passes[i].flags & EffectPassFlags::PSStyle) { D3D11_TEXTURE2D_DESC outputDesc; _textures[desc.passes[i].outputs[0]]->GetDesc(&outputDesc); pCurParam->uintVal = outputDesc.Width; diff --git a/src/Magpie.Core/Renderer.cpp b/src/Magpie.Core/Renderer.cpp index 1b31c3ed5..b96c087c3 100644 --- a/src/Magpie.Core/Renderer.cpp +++ b/src/Magpie.Core/Renderer.cpp @@ -444,7 +444,7 @@ static int Measure(const Fn& func) noexcept { static std::optional CompileEffect( const EffectOption& effectOption, - bool useFP16, + bool noFP16, bool forceInlineParams = false ) noexcept { // 指定效果名 @@ -466,8 +466,8 @@ static std::optional CompileEffect( if (scalingOptions.IsInlineParams() || forceInlineParams) { compileFlag |= EffectCompilerFlags::InlineParams; } - if (useFP16) { - compileFlag |= EffectCompilerFlags::FP16; + if (noFP16) { + compileFlag |= EffectCompilerFlags::NoFP16; } bool success = true; @@ -488,7 +488,7 @@ static std::optional CompileEffect( ID3D11Texture2D* Renderer::_BuildEffects() noexcept { const ScalingOptions& options = ScalingWindow::Get().Options(); - const bool useFP16 = _backendResources.IsFP16Supported() && !options.IsFP16Disabled(); + const bool noFP16 = !_backendResources.IsFP16Supported() || options.IsFP16Disabled(); const std::vector& effects = options.effects; assert(!effects.empty()); @@ -501,7 +501,7 @@ ID3D11Texture2D* Renderer::_BuildEffects() noexcept { int duration = Measure([&]() { Win32Helper::RunParallel([&](uint32_t id) { - std::optional desc = CompileEffect(effects[id], useFP16); + std::optional desc = CompileEffect(effects[id], noFP16); if (desc) { effectDescs[id] = std::move(*desc); } else { @@ -563,7 +563,7 @@ ID3D11Texture2D* Renderer::_BuildEffects() noexcept { }; // 参数不会改变,因此可以内联 - std::optional bicubicDesc = CompileEffect(bicubicOption, useFP16, true); + std::optional bicubicDesc = CompileEffect(bicubicOption, noFP16, true); if (!bicubicDesc) { Logger::Get().Error("编译降采样效果失败"); return nullptr; @@ -592,24 +592,28 @@ ID3D11Texture2D* Renderer::_BuildEffects() noexcept { } // 初始化所有效果共用的动态常量缓冲区 - for (uint32_t i = 0; i < effectDescs.size(); ++i) { - if (effectDescs[i].flags & EffectFlags::UseDynamic) { - _firstDynamicEffectIdx = i; - break; + for (const EffectDesc& effectDesc : effectDescs) { + for (const EffectPassDesc& passDesc : effectDesc.passes) { + if (passDesc.flags & EffectPassFlags::UseDynamic) { + D3D11_BUFFER_DESC bd{ + .ByteWidth = 16, // 只用 4 个字节 + .Usage = D3D11_USAGE_DYNAMIC, + .BindFlags = D3D11_BIND_CONSTANT_BUFFER, + .CPUAccessFlags = D3D11_CPU_ACCESS_WRITE + }; + + HRESULT hr = _backendResources.GetD3DDevice()->CreateBuffer(&bd, nullptr, _dynamicCB.put()); + if (FAILED(hr)) { + Logger::Get().ComError("CreateBuffer 失败", hr); + return nullptr; + } + + break; + } } - } - - if (_firstDynamicEffectIdx != std::numeric_limits::max()) { - D3D11_BUFFER_DESC bd = { - .ByteWidth = 16, // 只用 4 个字节 - .Usage = D3D11_USAGE_DYNAMIC, - .BindFlags = D3D11_BIND_CONSTANT_BUFFER, - .CPUAccessFlags = D3D11_CPU_ACCESS_WRITE - }; - HRESULT hr = _backendResources.GetD3DDevice()->CreateBuffer(&bd, nullptr, _dynamicCB.put()); - if (FAILED(hr)) { - Logger::Get().ComError("CreateBuffer 失败", hr); - return nullptr; + + if (_dynamicCB) { + break; } } diff --git a/src/Magpie.Core/Renderer.h b/src/Magpie.Core/Renderer.h index b2f5a9f0f..ab265ecf0 100644 --- a/src/Magpie.Core/Renderer.h +++ b/src/Magpie.Core/Renderer.h @@ -114,7 +114,6 @@ class Renderer { winrt::com_ptr _backendSharedTextureMutex; winrt::com_ptr _dynamicCB; - uint32_t _firstDynamicEffectIdx = std::numeric_limits::max(); // 可由所有线程访问 std::atomic _sharedTextureMutexKey = 0; diff --git a/src/Magpie.Core/include/EffectCompiler.h b/src/Magpie.Core/include/EffectCompiler.h index 62fa50993..59510b2bc 100644 --- a/src/Magpie.Core/include/EffectCompiler.h +++ b/src/Magpie.Core/include/EffectCompiler.h @@ -6,14 +6,13 @@ namespace Magpie { struct EffectCompilerFlags { // 会影响编译出的字节码的标志放在低 16 位中,这样组织是为了便于缓存 static constexpr uint32_t InlineParams = 1; - static constexpr uint32_t FP16 = 1 << 1; + static constexpr uint32_t NoFP16 = 1 << 1; // 只解析输出尺寸和参数,供用户界面使用 static constexpr uint32_t NoCompile = 1 << 16; static constexpr uint32_t NoCache = 1 << 17; static constexpr uint32_t SaveSources = 1 << 18; static constexpr uint32_t WarningsAreErrors = 1 << 19; - }; struct EffectCompiler { diff --git a/src/Magpie.Core/include/EffectDesc.h b/src/Magpie.Core/include/EffectDesc.h index 4626e1fc6..50016ce8b 100644 --- a/src/Magpie.Core/include/EffectDesc.h +++ b/src/Magpie.Core/include/EffectDesc.h @@ -68,6 +68,13 @@ struct EffectParameterDesc { std::variant, EffectConstant> constant; }; +struct EffectPassFlags { + static constexpr uint32_t PSStyle = 1; + static constexpr uint32_t UseFP16 = 1 << 1; + static constexpr uint32_t UseMulAdd = 1 << 2; + static constexpr uint32_t UseDynamic = 1 << 3; +}; + struct EffectPassDesc { winrt::com_ptr cso; SmallVector inputs; @@ -75,13 +82,11 @@ struct EffectPassDesc { std::array numThreads{}; std::pair blockSize{}; std::string desc; - bool isPSStyle = false; + uint32_t flags = 0; // EffectPassFlags }; struct EffectFlags { static constexpr uint32_t InlineParams = 1; - static constexpr uint32_t FP16 = 1 << 1; - static constexpr uint32_t UseDynamic = 1 << 2; }; struct EffectDesc {