From fa82c9b964ec850c08d745fb54cbc96f6a536ef7 Mon Sep 17 00:00:00 2001 From: Swung0x48 Date: Mon, 21 Jul 2025 22:44:02 +0800 Subject: [PATCH] [Chore] (MG_Util/Program): SpvcSession rule-of-three? --- MobileGL/MG_Test/Program/ProgramTest.cpp | 7 ++++- .../ShaderTranspiler/ShaderCompiler.cpp | 3 +-- .../MG_Util/ShaderTranspiler/ShaderCompiler.h | 2 +- MobileGL/MG_Util/ShaderTranspiler/Types.h | 26 +++++++++++++++++-- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/MobileGL/MG_Test/Program/ProgramTest.cpp b/MobileGL/MG_Test/Program/ProgramTest.cpp index 728a5ceb..f35bbd69 100644 --- a/MobileGL/MG_Test/Program/ProgramTest.cpp +++ b/MobileGL/MG_Test/Program/ProgramTest.cpp @@ -218,9 +218,14 @@ TEST_F(ProgramTest, DecompProgram) { auto spirvs = program_res.value(); + Vector sessions(spirvs.size()); + for (SizeT i = 0; i < spirvs.size(); ++i) { + sessions[i] = SpvcSession(spirvs[i]); + } + for (SizeT i = 0; i < spirvs.size(); ++i) { std::cout << "Decompiling " << MG_Util::ConvertGLEnumToString(programAttrib.shaderTypes[i]) << std::endl; - auto src = ShaderCompiler::DecompileShader(spirvs[i]); + auto src = ShaderCompiler::DecompileShader(sessions[i]); if (!src) { ASSERT_NE(src.error().errc, 0); FAIL() << "errc: " << src.error().errc << "\nlog: " << src.error().log; diff --git a/MobileGL/MG_Util/ShaderTranspiler/ShaderCompiler.cpp b/MobileGL/MG_Util/ShaderTranspiler/ShaderCompiler.cpp index 28d603f6..2e17227b 100644 --- a/MobileGL/MG_Util/ShaderTranspiler/ShaderCompiler.cpp +++ b/MobileGL/MG_Util/ShaderTranspiler/ShaderCompiler.cpp @@ -197,8 +197,7 @@ namespace MobileGL { return allSpirv; } - Result ShaderCompiler::DecompileShader(Vector spirv) { - SpvcSession session(spirv); + Result ShaderCompiler::DecompileShader(SpvcSession& session) { spvc_compiler_options options; session.CreateOptions(&options); diff --git a/MobileGL/MG_Util/ShaderTranspiler/ShaderCompiler.h b/MobileGL/MG_Util/ShaderTranspiler/ShaderCompiler.h index c9c91f5c..9023c67b 100644 --- a/MobileGL/MG_Util/ShaderTranspiler/ShaderCompiler.h +++ b/MobileGL/MG_Util/ShaderTranspiler/ShaderCompiler.h @@ -12,7 +12,7 @@ namespace ShaderTranspiler { public: static Result> CompileShader(const ShaderAttrib& attrib); static Result>> LinkProgram(const ProgramAttrib& attrib); - static Result DecompileShader(Vector spirv); + static Result DecompileShader(SpvcSession& session); }; } } diff --git a/MobileGL/MG_Util/ShaderTranspiler/Types.h b/MobileGL/MG_Util/ShaderTranspiler/Types.h index 6a35e93f..3fb8da13 100644 --- a/MobileGL/MG_Util/ShaderTranspiler/Types.h +++ b/MobileGL/MG_Util/ShaderTranspiler/Types.h @@ -28,8 +28,11 @@ namespace MobileGL { template using Result = std::expected; - struct SpvcSession { - SpvcSession(Vector spirv) { + class SpvcSession { + public: + SpvcSession() {} + + explicit SpvcSession(Vector spirv) { const SpvId *p_spirv = spirv.data(); size_t word_count = spirv.size(); @@ -38,6 +41,25 @@ namespace MobileGL { spvc_context_create_compiler(context, SPVC_BACKEND_GLSL, ir, SPVC_CAPTURE_MODE_TAKE_OWNERSHIP, &compiler); } + SpvcSession(SpvcSession&) = delete; + + SpvcSession(SpvcSession&& that) { + std::swap(this->context, that.context); + std::swap(this->compiler, that.compiler); + std::swap(this->ir, that.ir); + std::swap(this->compiler_options, that.compiler_options); + } + + SpvcSession& operator=(SpvcSession& session) = delete; + + SpvcSession& operator=(SpvcSession&& that) { + std::swap(this->context, that.context); + std::swap(this->compiler, that.compiler); + std::swap(this->ir, that.ir); + std::swap(this->compiler_options, that.compiler_options); + return *this; + } + spvc_result CreateOptions(spvc_compiler_options *options) { return spvc_compiler_create_compiler_options(compiler, options); }