[Improvement] (...): Optimize code.

This commit is contained in:
BZLZHH
2025-10-31 13:29:48 +08:00
parent 2652a12fdc
commit d982928a23
39 changed files with 284 additions and 775 deletions
@@ -1,4 +1,4 @@
#include "GLShaderLangConverter.h"
#include "ProgramEnumConverter.h"
namespace MobileGL {
namespace MG_Util {
@@ -0,0 +1,24 @@
#include "ProgramEnumConverter.h"
namespace MobileGL {
namespace MG_Util {
ShaderStage ConvertGLEnumToShaderStage(GLenum type) {
switch (type) {
case GL_VERTEX_SHADER:
return ShaderStage::Vertex;
case GL_TESS_CONTROL_SHADER:
return ShaderStage::TessControl;
case GL_TESS_EVALUATION_SHADER:
return ShaderStage::TessEval;
case GL_GEOMETRY_SHADER:
return ShaderStage::Geometry;
case GL_FRAGMENT_SHADER:
return ShaderStage::Fragment;
case GL_COMPUTE_SHADER:
return ShaderStage::Compute;
default:
return ShaderStage::Unknown;
}
}
} // namespace MG_Util
} // namespace MobileGL
@@ -0,0 +1,9 @@
#pragma once
#include <Includes.h>
#include <MG_State/GLState/ProgramState/ShaderObject.h>
namespace MobileGL {
namespace MG_Util {
ShaderStage ConvertGLEnumToShaderStage(GLenum type);
}
} // namespace MobileGL
@@ -0,0 +1,24 @@
#include "ProgramEnumConverter.h"
namespace MobileGL {
namespace MG_Util {
GLenum ConvertShaderStageToGLEnum(ShaderStage stage) {
switch (stage) {
case ShaderStage::Vertex:
return GL_VERTEX_SHADER;
case ShaderStage::TessControl:
return GL_TESS_CONTROL_SHADER;
case ShaderStage::TessEval:
return GL_TESS_EVALUATION_SHADER;
case ShaderStage::Geometry:
return GL_GEOMETRY_SHADER;
case ShaderStage::Fragment:
return GL_FRAGMENT_SHADER;
case ShaderStage::Compute:
return GL_COMPUTE_SHADER;
default:
return GL_UNKNOWN_MGL;
}
}
} // namespace MG_Util
} // namespace MobileGL
@@ -0,0 +1,9 @@
#pragma once
#include <Includes.h>
#include <MG_State/GLState/ProgramState/ShaderObject.h>
namespace MobileGL {
namespace MG_Util {
GLenum ConvertShaderStageToGLEnum(ShaderStage stage);
}
} // namespace MobileGL
@@ -1,10 +1,5 @@
//
// Created by Swung0x48 on 2025/8/14.
//
#include "SpvcTypeConverter.h"
#include "MG_Util/ShaderTranspiler/SpvcSession.h"
#include <MG_Util/ShaderTranspiler/SpvcSession.h>
namespace MobileGL {
namespace MG_Util {
@@ -1,7 +1,6 @@
#pragma once
#include <Includes.h>
#include "MG_Util/ShaderTranspiler/SpvcSession.h"
#include <MG_Util/ShaderTranspiler/SpvcSession.h>
namespace MobileGL {
namespace MG_Util {
@@ -1,7 +1,3 @@
//
// Created by Swung0x48 on 2025/8/22.
//
#include "BufferMetrics.h"
namespace MobileGL {
+1 -9
View File
@@ -1,10 +1,4 @@
//
// Created by Swung0x48 on 2025/8/22.
//
#ifndef BUFFERMETRICS_H
#define BUFFERMETRICS_H
#pragma once
#include <Includes.h>
namespace MobileGL {
@@ -12,5 +6,3 @@ namespace MobileGL {
SizeT GetGLTypeSize(GLenum type);
}
} // namespace MobileGL
#endif // BUFFERMETRICS_H
+102 -105
View File
@@ -10,7 +10,7 @@ namespace MobileGL {
case TextureInternalFormat::R8I:
case TextureInternalFormat::R8UI:
case TextureInternalFormat::R3G3B2:
return 1;
return 1;
case TextureInternalFormat::R16:
case TextureInternalFormat::R16Snorm:
@@ -85,124 +85,122 @@ namespace MobileGL {
SizeT GetBaseInternalFormatComponentCount(TextureInternalFormat format) {
switch (format) {
case TextureInternalFormat::DepthComponent:
case TextureInternalFormat::DepthStencil:
// Depth stencil is actually 2 components
// tho real formats always gives byte size in whole
// so we count this as one here
case TextureInternalFormat::Red:
case TextureInternalFormat::R8:
case TextureInternalFormat::R8Snorm:
case TextureInternalFormat::R8I:
case TextureInternalFormat::R8UI:
case TextureInternalFormat::R16:
case TextureInternalFormat::R16Snorm:
case TextureInternalFormat::R16I:
case TextureInternalFormat::R16UI:
case TextureInternalFormat::R16F:
return 1;
case TextureInternalFormat::RG:
case TextureInternalFormat::RG8:
case TextureInternalFormat::RG8Snorm:
case TextureInternalFormat::RG8I:
case TextureInternalFormat::RG8UI:
case TextureInternalFormat::RG16:
case TextureInternalFormat::RG16Snorm:
case TextureInternalFormat::RG16I:
case TextureInternalFormat::RG16UI:
case TextureInternalFormat::RG16F:
case TextureInternalFormat::RG32F:
case TextureInternalFormat::RG32I:
case TextureInternalFormat::RG32UI:
return 2;
case TextureInternalFormat::RGB:
case TextureInternalFormat::RGB4:
case TextureInternalFormat::RGB5:
case TextureInternalFormat::RGB8:
case TextureInternalFormat::RGB8Snorm:
case TextureInternalFormat::SRGB8:
case TextureInternalFormat::RGB8I:
case TextureInternalFormat::RGB8UI:
case TextureInternalFormat::RGB16F:
case TextureInternalFormat::RGB32F:
case TextureInternalFormat::RGB16I:
case TextureInternalFormat::RGB16UI:
case TextureInternalFormat::RGB32I:
case TextureInternalFormat::RGB32UI:
case TextureInternalFormat::R11FG11FB10F:
case TextureInternalFormat::RGB9E5:
return 3;
case TextureInternalFormat::RGBA:
case TextureInternalFormat::RGBA2:
case TextureInternalFormat::RGBA4:
case TextureInternalFormat::RGBA8:
case TextureInternalFormat::RGBA8Snorm:
case TextureInternalFormat::RGBA8I:
case TextureInternalFormat::RGBA8UI:
case TextureInternalFormat::RGBA12:
case TextureInternalFormat::RGBA16:
case TextureInternalFormat::RGBA16I:
case TextureInternalFormat::RGBA16UI:
case TextureInternalFormat::RGBA16F:
case TextureInternalFormat::RGBA32F:
case TextureInternalFormat::RGBA32I:
case TextureInternalFormat::RGBA32UI:
case TextureInternalFormat::RGB10A2:
case TextureInternalFormat::RGB10A2UI:
return 4;
default:
return 0;
case TextureInternalFormat::DepthComponent:
case TextureInternalFormat::DepthStencil:
// Depth stencil is actually 2 components
// tho real formats always gives byte size in whole
// so we count this as one here
case TextureInternalFormat::Red:
case TextureInternalFormat::R8:
case TextureInternalFormat::R8Snorm:
case TextureInternalFormat::R8I:
case TextureInternalFormat::R8UI:
case TextureInternalFormat::R16:
case TextureInternalFormat::R16Snorm:
case TextureInternalFormat::R16I:
case TextureInternalFormat::R16UI:
case TextureInternalFormat::R16F:
return 1;
case TextureInternalFormat::RG:
case TextureInternalFormat::RG8:
case TextureInternalFormat::RG8Snorm:
case TextureInternalFormat::RG8I:
case TextureInternalFormat::RG8UI:
case TextureInternalFormat::RG16:
case TextureInternalFormat::RG16Snorm:
case TextureInternalFormat::RG16I:
case TextureInternalFormat::RG16UI:
case TextureInternalFormat::RG16F:
case TextureInternalFormat::RG32F:
case TextureInternalFormat::RG32I:
case TextureInternalFormat::RG32UI:
return 2;
case TextureInternalFormat::RGB:
case TextureInternalFormat::RGB4:
case TextureInternalFormat::RGB5:
case TextureInternalFormat::RGB8:
case TextureInternalFormat::RGB8Snorm:
case TextureInternalFormat::SRGB8:
case TextureInternalFormat::RGB8I:
case TextureInternalFormat::RGB8UI:
case TextureInternalFormat::RGB16F:
case TextureInternalFormat::RGB32F:
case TextureInternalFormat::RGB16I:
case TextureInternalFormat::RGB16UI:
case TextureInternalFormat::RGB32I:
case TextureInternalFormat::RGB32UI:
case TextureInternalFormat::R11FG11FB10F:
case TextureInternalFormat::RGB9E5:
return 3;
case TextureInternalFormat::RGBA:
case TextureInternalFormat::RGBA2:
case TextureInternalFormat::RGBA4:
case TextureInternalFormat::RGBA8:
case TextureInternalFormat::RGBA8Snorm:
case TextureInternalFormat::RGBA8I:
case TextureInternalFormat::RGBA8UI:
case TextureInternalFormat::RGBA12:
case TextureInternalFormat::RGBA16:
case TextureInternalFormat::RGBA16I:
case TextureInternalFormat::RGBA16UI:
case TextureInternalFormat::RGBA16F:
case TextureInternalFormat::RGBA32F:
case TextureInternalFormat::RGBA32I:
case TextureInternalFormat::RGBA32UI:
case TextureInternalFormat::RGB10A2:
case TextureInternalFormat::RGB10A2UI:
return 4;
default:
return 0;
}
}
SizeT GetSizedTexturePixelDataTypeSize(TexturePixelDataType type) {
switch (type) {
case TexturePixelDataType::UnsignedByte332:
case TexturePixelDataType::UnsignedByte233Rev:
return 1;
case TexturePixelDataType::UnsignedShort565:
case TexturePixelDataType::UnsignedShort565Rev:
case TexturePixelDataType::UnsignedShort4444:
case TexturePixelDataType::UnsignedShort4444Rev:
case TexturePixelDataType::UnsignedShort5551:
case TexturePixelDataType::UnsignedShort1555Rev:
return 2;
case TexturePixelDataType::UnsignedInt8888:
case TexturePixelDataType::UnsignedInt8888Rev:
case TexturePixelDataType::UnsignedInt1010102:
case TexturePixelDataType::UnsignedInt2101010Rev:
case TexturePixelDataType::UnsignedInt101111Rev:
case TexturePixelDataType::UnsignedInt5999Rev:
return 4;
default:
return 0;
case TexturePixelDataType::UnsignedByte332:
case TexturePixelDataType::UnsignedByte233Rev:
return 1;
case TexturePixelDataType::UnsignedShort565:
case TexturePixelDataType::UnsignedShort565Rev:
case TexturePixelDataType::UnsignedShort4444:
case TexturePixelDataType::UnsignedShort4444Rev:
case TexturePixelDataType::UnsignedShort5551:
case TexturePixelDataType::UnsignedShort1555Rev:
return 2;
case TexturePixelDataType::UnsignedInt8888:
case TexturePixelDataType::UnsignedInt8888Rev:
case TexturePixelDataType::UnsignedInt1010102:
case TexturePixelDataType::UnsignedInt2101010Rev:
case TexturePixelDataType::UnsignedInt101111Rev:
case TexturePixelDataType::UnsignedInt5999Rev:
return 4;
default:
return 0;
}
}
SizeT GetBaseTexturePixelDataTypeSize(TexturePixelDataType type) {
switch (type) {
case TexturePixelDataType::UnsignedByte:
case TexturePixelDataType::Byte:
return 1;
case TexturePixelDataType::UnsignedShort:
case TexturePixelDataType::Short:
return 2;
case TexturePixelDataType::UnsignedInt:
case TexturePixelDataType::Int:
case TexturePixelDataType::Float:
return 4;
default:
return 0;
case TexturePixelDataType::UnsignedByte:
case TexturePixelDataType::Byte:
return 1;
case TexturePixelDataType::UnsignedShort:
case TexturePixelDataType::Short:
return 2;
case TexturePixelDataType::UnsignedInt:
case TexturePixelDataType::Int:
case TexturePixelDataType::Float:
return 4;
default:
return 0;
}
}
SizeT GetInternalBytesPerPixel(TextureInternalFormat internalformat, TexturePixelDataType type) {
SizeT sizedTextureFormatSize = GetSizedInternalFormatSizeInBytes(internalformat);
if (sizedTextureFormatSize > 0)
return sizedTextureFormatSize;
if (sizedTextureFormatSize > 0) return sizedTextureFormatSize;
SizeT sizedPixelFormatSize = GetSizedTexturePixelDataTypeSize(type);
if (sizedPixelFormatSize > 0)
return sizedPixelFormatSize;
if (sizedPixelFormatSize > 0) return sizedPixelFormatSize;
SizeT chCount = GetBaseInternalFormatComponentCount(internalformat);
SizeT bytesPerChannel = GetBaseTexturePixelDataTypeSize(type);
return chCount * bytesPerChannel;
@@ -210,8 +208,7 @@ namespace MobileGL {
SizeT GetInputBytesPerPixel(TextureInternalFormat internalformat, TexturePixelDataType type) {
SizeT sizedPixelFormatSize = GetSizedTexturePixelDataTypeSize(type);
if (sizedPixelFormatSize > 0)
return sizedPixelFormatSize;
if (sizedPixelFormatSize > 0) return sizedPixelFormatSize;
SizeT bytesPerChannel = GetBaseTexturePixelDataTypeSize(type);
SizeT chCount = GetBaseInternalFormatComponentCount(internalformat);
return chCount * bytesPerChannel;
@@ -1,6 +1,6 @@
#include "ShaderCompiler.h"
#include <MG_Util/Converters/GLToStr/GLEnumConverter.h>
#include <MG_Util/Converters/GLToGlslang/GLShaderLangConverter.h>
#include <MG_Util/Converters/GLToGlslang/ProgramEnumConverter.h>
namespace MobileGL {
namespace MG_Util {
@@ -117,7 +117,7 @@ namespace MobileGL {
auto shaderType = attrib.shaderType;
auto& sourceStr = attrib.sourceStr;
auto lang = GetEShLanguageByShaderType(shaderType);
auto lang = MG_Util::ConvertGLEnumToEShLanguage(shaderType);
if (lang == EShLanguage::EShLangCount) {
ResultInfo r;
r.log += "Error: [Preprocess] Unsupported shader type: " + ConvertGLEnumToString(shaderType);
@@ -79,8 +79,8 @@ namespace MobileGL {
// Should be called once, and only once, for every SPIR-V binary
spvc_result ParseMetaData();
private:
private:
spvc_context context = nullptr;
spvc_parsed_ir ir = nullptr;
spvc_compiler compiler = nullptr;
+2 -120
View File
@@ -20,7 +20,6 @@ namespace MobileGL {
};
struct ProgramAttrib {
// Vector<GLenum> shaderTypes;
Vector<SharedPtr<glslang::TShader>> shaders;
};
@@ -38,8 +37,8 @@ namespace MobileGL {
using Result = std::expected<T, ResultInfo>;
struct InterfaceVariable {
std::string name;
uint32_t location;
String name;
Uint32 location;
Bool operator<(const InterfaceVariable& other) const { return location < other.location; }
@@ -47,123 +46,6 @@ namespace MobileGL {
return location == other.location && name == other.name;
}
};
// class SpvcSession {
// public:
// SpvcSession() {}
//
// explicit SpvcSession(const Vector<unsigned int>& spirv) {
// const SpvId *p_spirv = spirv.data();
// size_t word_count = spirv.size();
//
// spvc_context_create(&context);
// spvc_context_parse_spirv(context, p_spirv, word_count, &ir);
// spvc_context_create_compiler(context, SPVC_BACKEND_GLSL, ir, SPVC_CAPTURE_MODE_TAKE_OWNERSHIP,
// &compiler); spvc_compiler_create_shader_resources(compiler, &resources);
// }
//
// 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);
// std::swap(this->resources, that.resources);
// }
//
// 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);
// std::swap(this->resources, that.resources);
// return *this;
// }
//
// spvc_result CreateOptions(spvc_compiler_options *options) {
// return spvc_compiler_create_compiler_options(compiler, options);
// }
//
// spvc_result SetOptions(spvc_compiler_options options) {
// compiler_options = options;
// return spvc_compiler_install_compiler_options(compiler, options);
// }
//
// Vector<InterfaceVariable> GetShaderInterface(spvc_resource_type resource_type) const {
// const spvc_reflected_resource *list = nullptr;
// size_t count = 0;
// spvc_resources_get_resource_list_for_type(resources, resource_type, &list, &count);
//
// Vector<InterfaceVariable> variables;
// for (size_t i = 0; i < count; ++i) {
// if (spvc_compiler_has_decoration(compiler, list[i].id, SpvDecorationBuiltIn)) {
// continue;
// }
//
// InterfaceVariable var;
// var.name = list[i].name;
// var.location = spvc_compiler_get_decoration(compiler, list[i].id, SpvDecorationLocation);
// variables.push_back(var);
//
// if (resource_type == SPVC_RESOURCE_TYPE_UNIFORM_BUFFER) {
// spvc_type type = spvc_compiler_get_type_handle(compiler, list[i].base_type_id);
// size_t num_members = spvc_type_get_num_member_types(type);
// printf("uniform %s {\n", var.name.c_str());
// for (size_t j = 0; j < num_members; ++j) {
// const char *memberName = spvc_compiler_get_member_name(compiler,
// list[i].base_type_id, j);
// // auto memberTypeId = spvc_type_get_member_type(type, j);
// // auto memberType = spvc_compiler_get_type_handle(compiler, memberTypeId);
//
// unsigned memberOffset = 0;
// spvc_compiler_type_struct_member_offset(compiler, type, j, &memberOffset);
// printf("\b%s; // %u\n", memberName, memberOffset);
// }
// printf("}\n");
// }
// }
// std::sort(variables.begin(), variables.end());
// return variables;
// }
//
// spvc_result Compile(const char** result) const {
// return spvc_compiler_compile(compiler, result);
// }
//
// const char* GetLastErrorString() const {
// return spvc_context_get_last_error_string(context);
// }
//
// ~SpvcSession() { spvc_context_destroy(context); }
// private:
// spvc_context context = nullptr;
// spvc_parsed_ir ir = nullptr;
// spvc_compiler compiler = nullptr;
// spvc_compiler_options compiler_options = nullptr;
// spvc_resources resources = nullptr;
// };
inline static EShLanguage GetEShLanguageByShaderType(GLenum shaderType) {
switch (shaderType) {
case GL_VERTEX_SHADER:
return EShLanguage::EShLangVertex;
case GL_FRAGMENT_SHADER:
return EShLanguage::EShLangFragment;
case GL_COMPUTE_SHADER:
return EShLanguage::EShLangCompute;
case GL_TESS_CONTROL_SHADER:
return EShLanguage::EShLangTessControl;
case GL_TESS_EVALUATION_SHADER:
return EShLanguage::EShLangTessEvaluation;
case GL_GEOMETRY_SHADER:
return EShLanguage::EShLangGeometry;
default:
return EShLanguage::EShLangCount;
}
}
} // namespace ShaderTranspiler
} // namespace MG_Util
} // namespace MobileGL
@@ -1,7 +1,3 @@
//
// Created by Swung 0x48 on 2025/7/20.
//
#include "UniformTraverser.h"
namespace MobileGL {
@@ -16,22 +12,7 @@ namespace MobileGL {
const auto& type = symbol->getType();
if (symbol->getQualifier().isUniform() && type.getBasicType() != glslang::EbtSampler) {
m_collectedSymbols.emplace_back(symbol);
// auto& name = symbol->getName();
// auto qualifier = symbol->getQualifier();
//
// printf("layout(location = %d) uniform %s\n", qualifier.layoutLocation, name.c_str());
// auto &uniform = uniforms.emplace_back();
// uniform.name = name;
// uniform.storageQualifier = qualifier.storage;
// uniform.layoutLocation = qualifier.layoutLocation;
// uniform.layoutBinding = qualifier.layoutBinding;
// uniform.layoutPacking = qualifier.layoutPacking;
}
// else if (type.getBasicType() == glslang::EbtSampler) {
// auto &uniform = samplers.emplace_back();
// uniform.name = symbol->getName();
// uniform.sampler = type.getSampler();
// }
}
} // namespace ShaderTranspiler
} // namespace MG_Util
@@ -1,10 +1,4 @@
//
// Created by Swung 0x48 on 2025/7/20.
//
#ifndef MOBILEGL_UNIFORMTRAVERSER_H
#define MOBILEGL_UNIFORMTRAVERSER_H
#pragma once
#include "Includes.h"
namespace MobileGL {
@@ -20,6 +14,4 @@ namespace MobileGL {
};
} // namespace ShaderTranspiler
} // namespace MG_Util
} // namespace MobileGL
#endif // MOBILEGL_UNIFORMTRAVERSER_H
} // namespace MobileGL