From 89f69a66ebdb78b1c491640255acad2ab20aac43 Mon Sep 17 00:00:00 2001 From: ChestnutYueyue <952134128@qq.com> Date: Wed, 25 Feb 2026 05:27:36 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE=E7=9B=AE=E5=BD=95=E7=BB=93=E6=9E=84=E5=B9=B6=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E5=A4=B4=E6=96=87=E4=BB=B6=E5=BC=95=E7=94=A8=E8=B7=AF?= =?UTF-8?q?=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将核心模块从 core 目录迁移到 base 目录,并调整相关头文件引用路径 更新事件系统、渲染器、场景图等模块的文件位置 优化代码组织结构,提升项目可维护性 --- .../include/extra2d/application/Application.h | 2 +- .../include/extra2d/{core => }/assets/Asset.h | 2 +- .../extra2d/{core => }/assets/AssetManager.h | 6 +- .../extra2d/{core => }/assets/ImageAsset.h | 2 +- .../extra2d/{core => }/assets/Material.h | 4 +- .../extra2d/{core => }/assets/Texture2D.h | 4 +- .../include/extra2d/base/AutoreleasePool.h | 2 +- .../include/extra2d/{core => base}/Object.h | 0 Extra2D/include/extra2d/base/RefCounted.h | 2 +- Extra2D/include/extra2d/base/Scheduler.h | 2 +- .../extra2d/{core/types.h => base/Types.h} | 0 Extra2D/include/extra2d/engine/Engine.h | 2 +- .../extra2d/{core => }/event/EventBus.h | 4 +- .../extra2d/{core => }/event/EventTarget.h | 4 +- Extra2D/include/extra2d/extra2d.h | 62 +++--- .../include/extra2d/gfx/GFXCommandBuffer.h | 47 +++-- Extra2D/include/extra2d/gfx/GFXDef.h | 29 ++- .../include/extra2d/gfx/GFXDescriptorSet.h | 184 ++++++++++++++++++ Extra2D/include/extra2d/gfx/GFXFramebuffer.h | 111 +++++++++++ Extra2D/include/extra2d/gfx/GFXQueue.h | 97 +++++++++ Extra2D/include/extra2d/gfx/GFXRenderPass.h | 161 +++++++++++++++ Extra2D/include/extra2d/gfx/GFXSampler.h | 118 +++++++++++ Extra2D/include/extra2d/gfx/GFXSwapchain.h | 134 +++++++++++++ .../extra2d/gfx/opengl/GLDescriptorSet.h | 118 +++++++++++ .../extra2d/gfx/opengl/GLFramebuffer.h | 58 ++++++ Extra2D/include/extra2d/gfx/opengl/GLQueue.h | 80 ++++++++ .../include/extra2d/gfx/opengl/GLRenderPass.h | 45 +++++ .../include/extra2d/gfx/opengl/GLSampler.h | 56 ++++++ .../include/extra2d/gfx/opengl/GLSwapchain.h | 81 ++++++++ .../extra2d/{core/color.h => math/Color.h} | 2 +- .../{core/math_types.h => math/Math.h} | 2 +- Extra2D/include/extra2d/platform/Input.h | 4 +- Extra2D/include/extra2d/platform/SDLHelper.h | 6 +- Extra2D/include/extra2d/platform/Window.h | 4 +- .../{2d/renderer => renderer/2d}/Batcher2d.h | 10 +- .../renderer => renderer/2d}/RenderDrawInfo.h | 4 +- .../renderer => renderer/2d}/RenderEntity.h | 4 +- .../renderer => renderer/2d}/StencilManager.h | 2 +- .../renderer => renderer/2d}/UIMeshBuffer.h | 4 +- .../{ => renderer}/2d/components/Sprite.h | 10 +- .../2d/components/SpriteFrame.h | 6 +- .../{core/scene-graph => scene}/Camera.h | 4 +- .../{core/scene-graph => scene}/Component.h | 4 +- .../{core/scene-graph => scene}/Layers.h | 2 +- .../{core/scene-graph => scene}/Model.h | 6 +- .../{core/scene-graph => scene}/Node.h | 6 +- .../{core/scene-graph => scene}/RenderScene.h | 4 +- .../include/extra2d/{core => scene}/Root.h | 10 +- .../{core/scene-graph => scene}/Scene.h | 2 +- Extra2D/include/extra2d/utils/random.h | 2 +- Extra2D/include/extra2d/utils/timer.h | 2 +- Extra2D/src/application/Application.cpp | 2 +- Extra2D/src/{core => }/assets/Asset.cpp | 2 +- .../src/{core => }/assets/AssetManager.cpp | 4 +- Extra2D/src/{core => }/assets/ImageAsset.cpp | 2 +- Extra2D/src/{core => }/assets/Material.cpp | 2 +- Extra2D/src/{core => }/assets/Texture2D.cpp | 2 +- Extra2D/src/{core => base}/Object.cpp | 2 +- Extra2D/src/engine/Engine.cpp | 2 +- Extra2D/src/{core => }/event/EventBus.cpp | 2 +- Extra2D/src/{core => }/event/EventTarget.cpp | 2 +- Extra2D/src/gfx/GFXDescriptorSet.cpp | 39 ++++ Extra2D/src/gfx/GFXFramebuffer.cpp | 40 ++++ Extra2D/src/gfx/GFXQueue.cpp | 20 ++ Extra2D/src/gfx/GFXRenderPass.cpp | 33 ++++ Extra2D/src/gfx/GFXSampler.cpp | 31 +++ Extra2D/src/gfx/GFXSwapchain.cpp | 39 ++++ Extra2D/src/gfx/opengl/GLCommandBuffer.cpp | 11 +- Extra2D/src/gfx/opengl/GLDescriptorSet.cpp | 136 +++++++++++++ Extra2D/src/gfx/opengl/GLFramebuffer.cpp | 69 +++++++ Extra2D/src/gfx/opengl/GLQueue.cpp | 45 +++++ Extra2D/src/gfx/opengl/GLRenderPass.cpp | 22 +++ Extra2D/src/gfx/opengl/GLSampler.cpp | 131 +++++++++++++ Extra2D/src/gfx/opengl/GLSwapchain.cpp | 68 +++++++ Extra2D/src/platform/Input.cpp | 2 +- .../renderer => renderer/2d}/Batcher2d.cpp | 4 +- .../2d}/RenderDrawInfo.cpp | 4 +- .../renderer => renderer/2d}/RenderEntity.cpp | 4 +- .../2d}/StencilManager.cpp | 4 +- .../renderer => renderer/2d}/UIMeshBuffer.cpp | 2 +- .../{ => renderer}/2d/components/Sprite.cpp | 6 +- .../2d/components/SpriteFrame.cpp | 6 +- .../{core/scene-graph => scene}/Camera.cpp | 6 +- .../{core/scene-graph => scene}/Component.cpp | 4 +- .../{core/scene-graph => scene}/Layers.cpp | 2 +- .../src/{core/scene-graph => scene}/Model.cpp | 6 +- .../src/{core/scene-graph => scene}/Node.cpp | 6 +- .../scene-graph => scene}/RenderScene.cpp | 8 +- .../src/{core/scene-graph => scene}/Scene.cpp | 2 +- refactor.py | 117 +++++++++++ 90 files changed, 2252 insertions(+), 154 deletions(-) rename Extra2D/include/extra2d/{core => }/assets/Asset.h (98%) rename Extra2D/include/extra2d/{core => }/assets/AssetManager.h (97%) rename Extra2D/include/extra2d/{core => }/assets/ImageAsset.h (98%) rename Extra2D/include/extra2d/{core => }/assets/Material.h (99%) rename Extra2D/include/extra2d/{core => }/assets/Texture2D.h (98%) rename Extra2D/include/extra2d/{core => base}/Object.h (100%) rename Extra2D/include/extra2d/{core/types.h => base/Types.h} (100%) rename Extra2D/include/extra2d/{core => }/event/EventBus.h (99%) rename Extra2D/include/extra2d/{core => }/event/EventTarget.h (97%) create mode 100644 Extra2D/include/extra2d/gfx/GFXDescriptorSet.h create mode 100644 Extra2D/include/extra2d/gfx/GFXFramebuffer.h create mode 100644 Extra2D/include/extra2d/gfx/GFXQueue.h create mode 100644 Extra2D/include/extra2d/gfx/GFXRenderPass.h create mode 100644 Extra2D/include/extra2d/gfx/GFXSampler.h create mode 100644 Extra2D/include/extra2d/gfx/GFXSwapchain.h create mode 100644 Extra2D/include/extra2d/gfx/opengl/GLDescriptorSet.h create mode 100644 Extra2D/include/extra2d/gfx/opengl/GLFramebuffer.h create mode 100644 Extra2D/include/extra2d/gfx/opengl/GLQueue.h create mode 100644 Extra2D/include/extra2d/gfx/opengl/GLRenderPass.h create mode 100644 Extra2D/include/extra2d/gfx/opengl/GLSampler.h create mode 100644 Extra2D/include/extra2d/gfx/opengl/GLSwapchain.h rename Extra2D/include/extra2d/{core/color.h => math/Color.h} (99%) rename Extra2D/include/extra2d/{core/math_types.h => math/Math.h} (99%) rename Extra2D/include/extra2d/{2d/renderer => renderer/2d}/Batcher2d.h (94%) rename Extra2D/include/extra2d/{2d/renderer => renderer/2d}/RenderDrawInfo.h (98%) rename Extra2D/include/extra2d/{2d/renderer => renderer/2d}/RenderEntity.h (98%) rename Extra2D/include/extra2d/{2d/renderer => renderer/2d}/StencilManager.h (99%) rename Extra2D/include/extra2d/{2d/renderer => renderer/2d}/UIMeshBuffer.h (96%) rename Extra2D/include/extra2d/{ => renderer}/2d/components/Sprite.h (92%) rename Extra2D/include/extra2d/{ => renderer}/2d/components/SpriteFrame.h (95%) rename Extra2D/include/extra2d/{core/scene-graph => scene}/Camera.h (99%) rename Extra2D/include/extra2d/{core/scene-graph => scene}/Component.h (96%) rename Extra2D/include/extra2d/{core/scene-graph => scene}/Layers.h (99%) rename Extra2D/include/extra2d/{core/scene-graph => scene}/Model.h (97%) rename Extra2D/include/extra2d/{core/scene-graph => scene}/Node.h (99%) rename Extra2D/include/extra2d/{core/scene-graph => scene}/RenderScene.h (98%) rename Extra2D/include/extra2d/{core => scene}/Root.h (95%) rename Extra2D/include/extra2d/{core/scene-graph => scene}/Scene.h (98%) rename Extra2D/src/{core => }/assets/Asset.cpp (97%) rename Extra2D/src/{core => }/assets/AssetManager.cpp (96%) rename Extra2D/src/{core => }/assets/ImageAsset.cpp (98%) rename Extra2D/src/{core => }/assets/Material.cpp (99%) rename Extra2D/src/{core => }/assets/Texture2D.cpp (98%) rename Extra2D/src/{core => base}/Object.cpp (97%) rename Extra2D/src/{core => }/event/EventBus.cpp (91%) rename Extra2D/src/{core => }/event/EventTarget.cpp (80%) create mode 100644 Extra2D/src/gfx/GFXDescriptorSet.cpp create mode 100644 Extra2D/src/gfx/GFXFramebuffer.cpp create mode 100644 Extra2D/src/gfx/GFXQueue.cpp create mode 100644 Extra2D/src/gfx/GFXRenderPass.cpp create mode 100644 Extra2D/src/gfx/GFXSampler.cpp create mode 100644 Extra2D/src/gfx/GFXSwapchain.cpp create mode 100644 Extra2D/src/gfx/opengl/GLDescriptorSet.cpp create mode 100644 Extra2D/src/gfx/opengl/GLFramebuffer.cpp create mode 100644 Extra2D/src/gfx/opengl/GLQueue.cpp create mode 100644 Extra2D/src/gfx/opengl/GLRenderPass.cpp create mode 100644 Extra2D/src/gfx/opengl/GLSampler.cpp create mode 100644 Extra2D/src/gfx/opengl/GLSwapchain.cpp rename Extra2D/src/{2d/renderer => renderer/2d}/Batcher2d.cpp (98%) rename Extra2D/src/{2d/renderer => renderer/2d}/RenderDrawInfo.cpp (96%) rename Extra2D/src/{2d/renderer => renderer/2d}/RenderEntity.cpp (97%) rename Extra2D/src/{2d/renderer => renderer/2d}/StencilManager.cpp (98%) rename Extra2D/src/{2d/renderer => renderer/2d}/UIMeshBuffer.cpp (99%) rename Extra2D/src/{ => renderer}/2d/components/Sprite.cpp (96%) rename Extra2D/src/{ => renderer}/2d/components/SpriteFrame.cpp (94%) rename Extra2D/src/{core/scene-graph => scene}/Camera.cpp (97%) rename Extra2D/src/{core/scene-graph => scene}/Component.cpp (92%) rename Extra2D/src/{core/scene-graph => scene}/Layers.cpp (97%) rename Extra2D/src/{core/scene-graph => scene}/Model.cpp (94%) rename Extra2D/src/{core/scene-graph => scene}/Node.cpp (98%) rename Extra2D/src/{core/scene-graph => scene}/RenderScene.cpp (95%) rename Extra2D/src/{core/scene-graph => scene}/Scene.cpp (97%) create mode 100644 refactor.py diff --git a/Extra2D/include/extra2d/application/Application.h b/Extra2D/include/extra2d/application/Application.h index 4580d80..bdf0b92 100644 --- a/Extra2D/include/extra2d/application/Application.h +++ b/Extra2D/include/extra2d/application/Application.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include #include diff --git a/Extra2D/include/extra2d/core/assets/Asset.h b/Extra2D/include/extra2d/assets/Asset.h similarity index 98% rename from Extra2D/include/extra2d/core/assets/Asset.h rename to Extra2D/include/extra2d/assets/Asset.h index 03cd16d..ee1b771 100644 --- a/Extra2D/include/extra2d/core/assets/Asset.h +++ b/Extra2D/include/extra2d/assets/Asset.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #include namespace extra2d { diff --git a/Extra2D/include/extra2d/core/assets/AssetManager.h b/Extra2D/include/extra2d/assets/AssetManager.h similarity index 97% rename from Extra2D/include/extra2d/core/assets/AssetManager.h rename to Extra2D/include/extra2d/assets/AssetManager.h index b653e9f..dcc0fdd 100644 --- a/Extra2D/include/extra2d/core/assets/AssetManager.h +++ b/Extra2D/include/extra2d/assets/AssetManager.h @@ -1,8 +1,8 @@ #pragma once -#include -#include -#include +#include +#include +#include #include #include #include diff --git a/Extra2D/include/extra2d/core/assets/ImageAsset.h b/Extra2D/include/extra2d/assets/ImageAsset.h similarity index 98% rename from Extra2D/include/extra2d/core/assets/ImageAsset.h rename to Extra2D/include/extra2d/assets/ImageAsset.h index 596e58f..57ecf98 100644 --- a/Extra2D/include/extra2d/core/assets/ImageAsset.h +++ b/Extra2D/include/extra2d/assets/ImageAsset.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include diff --git a/Extra2D/include/extra2d/core/assets/Material.h b/Extra2D/include/extra2d/assets/Material.h similarity index 99% rename from Extra2D/include/extra2d/core/assets/Material.h rename to Extra2D/include/extra2d/assets/Material.h index 16ce610..feaf334 100644 --- a/Extra2D/include/extra2d/core/assets/Material.h +++ b/Extra2D/include/extra2d/assets/Material.h @@ -1,7 +1,7 @@ #pragma once -#include -#include +#include +#include #include #include #include diff --git a/Extra2D/include/extra2d/core/assets/Texture2D.h b/Extra2D/include/extra2d/assets/Texture2D.h similarity index 98% rename from Extra2D/include/extra2d/core/assets/Texture2D.h rename to Extra2D/include/extra2d/assets/Texture2D.h index 099a576..a6e2b7b 100644 --- a/Extra2D/include/extra2d/core/assets/Texture2D.h +++ b/Extra2D/include/extra2d/assets/Texture2D.h @@ -1,7 +1,7 @@ #pragma once -#include -#include +#include +#include #include #include diff --git a/Extra2D/include/extra2d/base/AutoreleasePool.h b/Extra2D/include/extra2d/base/AutoreleasePool.h index 9377a7f..3005db4 100644 --- a/Extra2D/include/extra2d/base/AutoreleasePool.h +++ b/Extra2D/include/extra2d/base/AutoreleasePool.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #include namespace extra2d { diff --git a/Extra2D/include/extra2d/core/Object.h b/Extra2D/include/extra2d/base/Object.h similarity index 100% rename from Extra2D/include/extra2d/core/Object.h rename to Extra2D/include/extra2d/base/Object.h diff --git a/Extra2D/include/extra2d/base/RefCounted.h b/Extra2D/include/extra2d/base/RefCounted.h index 75bfe2a..68e3bab 100644 --- a/Extra2D/include/extra2d/base/RefCounted.h +++ b/Extra2D/include/extra2d/base/RefCounted.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #include namespace extra2d { diff --git a/Extra2D/include/extra2d/base/Scheduler.h b/Extra2D/include/extra2d/base/Scheduler.h index 1c787d5..81fdc2f 100644 --- a/Extra2D/include/extra2d/base/Scheduler.h +++ b/Extra2D/include/extra2d/base/Scheduler.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include #include diff --git a/Extra2D/include/extra2d/core/types.h b/Extra2D/include/extra2d/base/Types.h similarity index 100% rename from Extra2D/include/extra2d/core/types.h rename to Extra2D/include/extra2d/base/Types.h diff --git a/Extra2D/include/extra2d/engine/Engine.h b/Extra2D/include/extra2d/engine/Engine.h index c50ab58..13888d0 100644 --- a/Extra2D/include/extra2d/engine/Engine.h +++ b/Extra2D/include/extra2d/engine/Engine.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include diff --git a/Extra2D/include/extra2d/core/event/EventBus.h b/Extra2D/include/extra2d/event/EventBus.h similarity index 99% rename from Extra2D/include/extra2d/core/event/EventBus.h rename to Extra2D/include/extra2d/event/EventBus.h index 1d548ad..b3a5f8d 100644 --- a/Extra2D/include/extra2d/core/event/EventBus.h +++ b/Extra2D/include/extra2d/event/EventBus.h @@ -1,7 +1,7 @@ #pragma once -#include -#include +#include +#include #include #include #include diff --git a/Extra2D/include/extra2d/core/event/EventTarget.h b/Extra2D/include/extra2d/event/EventTarget.h similarity index 97% rename from Extra2D/include/extra2d/core/event/EventTarget.h rename to Extra2D/include/extra2d/event/EventTarget.h index 7e11054..86c70b9 100644 --- a/Extra2D/include/extra2d/core/event/EventTarget.h +++ b/Extra2D/include/extra2d/event/EventTarget.h @@ -1,7 +1,7 @@ #pragma once -#include -#include +#include +#include #include #include #include diff --git a/Extra2D/include/extra2d/extra2d.h b/Extra2D/include/extra2d/extra2d.h index b5f6d89..5f19e2d 100644 --- a/Extra2D/include/extra2d/extra2d.h +++ b/Extra2D/include/extra2d/extra2d.h @@ -8,29 +8,29 @@ #include // Core -#include -#include -#include -#include +#include +#include +#include +#include // Core - Event -#include -#include +#include +#include // Core - Scene Graph -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include // Core - Assets -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include // Platform #include @@ -52,6 +52,13 @@ #include #include #include +#include +#include +#include +#include +#include +#include +#include // GFX - OpenGL Backend #include @@ -60,17 +67,24 @@ #include #include #include +#include +#include +#include +#include +#include +#include +#include // 2D Renderer -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include // 2D Components -#include -#include +#include +#include // Utils #include diff --git a/Extra2D/include/extra2d/gfx/GFXCommandBuffer.h b/Extra2D/include/extra2d/gfx/GFXCommandBuffer.h index bd93225..0b71601 100644 --- a/Extra2D/include/extra2d/gfx/GFXCommandBuffer.h +++ b/Extra2D/include/extra2d/gfx/GFXCommandBuffer.h @@ -38,18 +38,6 @@ struct Rect { float height{0.0f}; }; -/** - * @brief 清除标志位运算 - */ -inline ClearFlagBit operator|(ClearFlagBit a, ClearFlagBit b) { - return static_cast(static_cast(a) | static_cast(b)); -} - -inline ClearFlagBit& operator|=(ClearFlagBit& a, ClearFlagBit b) { - a = a | b; - return a; -} - /** * @brief 命令缓冲区类 * @@ -63,6 +51,41 @@ public: */ ~CommandBuffer() override; + // ==================== 生命周期 ==================== + + /** + * @brief 开始录制命令 + */ + virtual void begin() = 0; + + /** + * @brief 结束录制命令 + */ + virtual void end() = 0; + + // ==================== 渲染通道 ==================== + + /** + * @brief 开始渲染通道 + * @param renderPass 渲染通道 + * @param framebuffer 帧缓冲 + * @param viewport 视口 + * @param clearFlags 清除标志 + * @param clearColor 清除颜色 + * @param clearDepth 清除深度 + * @param clearStencil 清除模板 + */ + virtual void beginRenderPass(RenderPass* renderPass, Framebuffer* framebuffer, + const Viewport& viewport, + ClearFlagBit clearFlags, + const Color& clearColor, + float clearDepth, u32 clearStencil) = 0; + + /** + * @brief 结束渲染通道 + */ + virtual void endRenderPass() = 0; + // ==================== 绑定命令 ==================== /** diff --git a/Extra2D/include/extra2d/gfx/GFXDef.h b/Extra2D/include/extra2d/gfx/GFXDef.h index 28f1b54..301be77 100644 --- a/Extra2D/include/extra2d/gfx/GFXDef.h +++ b/Extra2D/include/extra2d/gfx/GFXDef.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #include namespace extra2d { @@ -326,6 +326,18 @@ enum class DynamicStateFlag : u32 { StencilReference = 1 << 8, }; +/** + * @brief 清除标志位 + */ +enum class ClearFlagBit : u32 { + None = 0, + Color = 0x1, + Depth = 0x2, + Stencil = 0x4, + DepthStencil = Depth | Stencil, + All = Color | Depth | Stencil, +}; + /** * @brief 过滤模式 */ @@ -446,5 +458,20 @@ inline bool hasDynamicState(DynamicStateFlag flags, DynamicStateFlag flag) { return (static_cast(flags) & static_cast(flag)) != 0; } +/** + * @brief 清除标志位运算 + */ +inline ClearFlagBit operator|(ClearFlagBit a, ClearFlagBit b) { + return static_cast(static_cast(a) | static_cast(b)); +} + +inline ClearFlagBit operator&(ClearFlagBit a, ClearFlagBit b) { + return static_cast(static_cast(a) & static_cast(b)); +} + +inline bool hasFlag(ClearFlagBit flags, ClearFlagBit flag) { + return (static_cast(flags) & static_cast(flag)) != 0; +} + } // namespace gfx } // namespace extra2d diff --git a/Extra2D/include/extra2d/gfx/GFXDescriptorSet.h b/Extra2D/include/extra2d/gfx/GFXDescriptorSet.h new file mode 100644 index 0000000..7faadbc --- /dev/null +++ b/Extra2D/include/extra2d/gfx/GFXDescriptorSet.h @@ -0,0 +1,184 @@ +#pragma once + +#include +#include +#include + +namespace extra2d { +namespace gfx { + +class Buffer; +class Texture; +class Sampler; +class DescriptorSetLayout; + +/** + * @brief 描述符类型 + */ +enum class DescriptorType : u32 { + Unknown, + UniformBuffer, + DynamicUniformBuffer, + StorageBuffer, + DynamicStorageBuffer, + Sampler, + Texture, + SamplerTexture, + StorageImage, + InputAttachment, +}; + +/** + * @brief 描述符集布局绑定 + */ +struct DescriptorSetLayoutBinding { + u32 binding{0xFFFFFFFF}; + DescriptorType descriptorType{DescriptorType::Unknown}; + u32 count{0}; + ShaderStage stageFlags{ShaderStage::None}; +}; + +/** + * @brief 描述符集布局信息 + */ +struct DescriptorSetLayoutInfo { + std::vector bindings; +}; + +/** + * @brief 描述符集信息 + */ +struct DescriptorSetInfo { + const DescriptorSetLayout* layout{nullptr}; +}; + +/** + * @brief 描述符集布局抽象类 + */ +class DescriptorSetLayout : public Object { +public: + /** + * @brief 析构函数 + */ + ~DescriptorSetLayout() override; + + /** + * @brief 获取类型名称 + * @return 类型名称 + */ + const char* getTypeName() const override { return "DescriptorSetLayout"; } + + /** + * @brief 初始化布局 + * @param info 布局信息 + */ + void initialize(const DescriptorSetLayoutInfo& info); + + /** + * @brief 销毁布局 + */ + void destroy(); + + /** + * @brief 获取绑定数量 + * @return 绑定数量 + */ + u32 getBindingCount() const { return static_cast(bindings_.size()); } + + /** + * @brief 获取绑定 + * @param index 索引 + * @return 绑定 + */ + const DescriptorSetLayoutBinding& getBinding(u32 index) const { return bindings_[index]; } + +protected: + DescriptorSetLayout() = default; + + virtual void doInit(const DescriptorSetLayoutInfo& info) = 0; + virtual void doDestroy() = 0; + + std::vector bindings_; +}; + +/** + * @brief 描述符集抽象类 + * + * 管理着色器资源的绑定。 + * 参考 Cocos Creator 的 DescriptorSet 设计。 + */ +class DescriptorSet : public Object { +public: + /** + * @brief 析构函数 + */ + ~DescriptorSet() override; + + /** + * @brief 获取类型名称 + * @return 类型名称 + */ + const char* getTypeName() const override { return "DescriptorSet"; } + + /** + * @brief 初始化描述符集 + * @param info 描述符集信息 + */ + void initialize(const DescriptorSetInfo& info); + + /** + * @brief 销毁描述符集 + */ + void destroy(); + + /** + * @brief 绑定缓冲区 + * @param binding 绑定点 + * @param buffer 缓冲区 + */ + virtual void bindBuffer(u32 binding, Buffer* buffer) = 0; + + /** + * @brief 绑定纹理 + * @param binding 绑定点 + * @param texture 纹理 + */ + virtual void bindTexture(u32 binding, Texture* texture) = 0; + + /** + * @brief 绑定采样器 + * @param binding 绑定点 + * @param sampler 采样器 + */ + virtual void bindSampler(u32 binding, Sampler* sampler) = 0; + + /** + * @brief 绑定采样器纹理 + * @param binding 绑定点 + * @param texture 纹理 + * @param sampler 采样器 + */ + virtual void bindSamplerTexture(u32 binding, Texture* texture, Sampler* sampler) = 0; + + /** + * @brief 更新描述符 + */ + virtual void update() = 0; + + /** + * @brief 获取布局 + * @return 布局 + */ + DescriptorSetLayout* getLayout() const { return layout_; } + +protected: + DescriptorSet() = default; + + virtual void doInit(const DescriptorSetInfo& info) = 0; + virtual void doDestroy() = 0; + + DescriptorSetLayout* layout_{nullptr}; +}; + +} // namespace gfx +} // namespace extra2d diff --git a/Extra2D/include/extra2d/gfx/GFXFramebuffer.h b/Extra2D/include/extra2d/gfx/GFXFramebuffer.h new file mode 100644 index 0000000..8f19545 --- /dev/null +++ b/Extra2D/include/extra2d/gfx/GFXFramebuffer.h @@ -0,0 +1,111 @@ +#pragma once + +#include +#include +#include + +namespace extra2d { +namespace gfx { + +class RenderPass; +class Texture; + +/** + * @brief 帧缓冲信息 + */ +struct FramebufferInfo { + RenderPass* renderPass{nullptr}; + std::vector colorTextures; + Texture* depthStencilTexture{nullptr}; +}; + +/** + * @brief 帧缓冲抽象类 + * + * 管理渲染目标的集合。 + * 参考 Cocos Creator 的 Framebuffer 设计。 + */ +class Framebuffer : public Object { +public: + /** + * @brief 析构函数 + */ + ~Framebuffer() override; + + /** + * @brief 获取类型名称 + * @return 类型名称 + */ + const char* getTypeName() const override { return "Framebuffer"; } + + /** + * @brief 初始化帧缓冲 + * @param info 帧缓冲信息 + */ + void initialize(const FramebufferInfo& info); + + /** + * @brief 销毁帧缓冲 + */ + void destroy(); + + /** + * @brief 获取渲染通道 + * @return 渲染通道 + */ + RenderPass* getRenderPass() const { return renderPass_; } + + /** + * @brief 获取颜色纹理数量 + * @return 颜色纹理数量 + */ + u32 getColorTextureCount() const { return static_cast(colorTextures_.size()); } + + /** + * @brief 获取颜色纹理 + * @param index 索引 + * @return 颜色纹理 + */ + Texture* getColorTexture(u32 index) const { return colorTextures_[index]; } + + /** + * @brief 获取深度模板纹理 + * @return 深度模板纹理 + */ + Texture* getDepthStencilTexture() const { return depthStencilTexture_; } + + /** + * @brief 获取宽度 + * @return 宽度 + */ + u32 getWidth() const { return width_; } + + /** + * @brief 获取高度 + * @return 高度 + */ + u32 getHeight() const { return height_; } + +protected: + Framebuffer() = default; + + /** + * @brief 实际初始化实现 + * @param info 帧缓冲信息 + */ + virtual void doInit(const FramebufferInfo& info) = 0; + + /** + * @brief 实际销毁实现 + */ + virtual void doDestroy() = 0; + + RenderPass* renderPass_{nullptr}; + std::vector colorTextures_; + Texture* depthStencilTexture_{nullptr}; + u32 width_{0}; + u32 height_{0}; +}; + +} // namespace gfx +} // namespace extra2d diff --git a/Extra2D/include/extra2d/gfx/GFXQueue.h b/Extra2D/include/extra2d/gfx/GFXQueue.h new file mode 100644 index 0000000..ff57804 --- /dev/null +++ b/Extra2D/include/extra2d/gfx/GFXQueue.h @@ -0,0 +1,97 @@ +#pragma once + +#include +#include +#include + +namespace extra2d { +namespace gfx { + +class CommandBuffer; + +/** + * @brief 命令队列类型 + */ +enum class QueueType : u32 { + Graphics, + Compute, + Transfer, +}; + +/** + * @brief 命令队列信息 + */ +struct QueueInfo { + QueueType type{QueueType::Graphics}; +}; + +/** + * @brief 命令队列抽象类 + * + * 用于提交命令缓冲区执行。 + * 参考 Cocos Creator 的 Queue 设计。 + */ +class Queue : public Object { +public: + /** + * @brief 析构函数 + */ + ~Queue() override; + + /** + * @brief 获取类型名称 + * @return 类型名称 + */ + const char* getTypeName() const override { return "Queue"; } + + /** + * @brief 初始化队列 + * @param info 队列信息 + */ + void initialize(const QueueInfo& info); + + /** + * @brief 销毁队列 + */ + void destroy(); + + /** + * @brief 提交命令缓冲区 + * @param cmdBuffers 命令缓冲区数组 + * @param count 命令缓冲区数量 + */ + virtual void submit(CommandBuffer* const* cmdBuffers, u32 count) = 0; + + /** + * @brief 提交命令缓冲区(便捷方法) + * @param cmdBuffers 命令缓冲区列表 + */ + void submit(const std::vector& cmdBuffers) { + submit(cmdBuffers.data(), static_cast(cmdBuffers.size())); + } + + /** + * @brief 获取队列类型 + * @return 队列类型 + */ + QueueType getType() const { return type_; } + +protected: + Queue() = default; + + /** + * @brief 实际初始化实现 + * @param info 队列信息 + */ + virtual void doInit(const QueueInfo& info) = 0; + + /** + * @brief 实际销毁实现 + */ + virtual void doDestroy() = 0; + + QueueType type_{QueueType::Graphics}; +}; + +} // namespace gfx +} // namespace extra2d diff --git a/Extra2D/include/extra2d/gfx/GFXRenderPass.h b/Extra2D/include/extra2d/gfx/GFXRenderPass.h new file mode 100644 index 0000000..aaa7c42 --- /dev/null +++ b/Extra2D/include/extra2d/gfx/GFXRenderPass.h @@ -0,0 +1,161 @@ +#pragma once + +#include +#include +#include + +namespace extra2d { +namespace gfx { + +/** + * @brief 加载操作 + */ +enum class LoadOp : u32 { + Clear, + Load, + DontCare, +}; + +/** + * @brief 存储操作 + */ +enum class StoreOp : u32 { + Store, + DontCare, +}; + +/** + * @brief 颜色附件描述 + */ +struct ColorAttachment { + Format format{Format::Unknown}; + SampleCount sampleCount{SampleCount::X1}; + LoadOp loadOp{LoadOp::Clear}; + StoreOp storeOp{StoreOp::Store}; +}; + +/** + * @brief 深度模板附件描述 + */ +struct DepthStencilAttachment { + Format format{Format::Unknown}; + SampleCount sampleCount{SampleCount::X1}; + LoadOp depthLoadOp{LoadOp::Clear}; + StoreOp depthStoreOp{StoreOp::Store}; + LoadOp stencilLoadOp{LoadOp::Clear}; + StoreOp stencilStoreOp{StoreOp::Store}; +}; + +/** + * @brief 子通道信息 + */ +struct SubpassInfo { + std::vector inputs; + std::vector colors; + std::vector resolves; + std::vector preserves; + u32 depthStencil{0xFFFFFFFF}; + u32 depthStencilResolve{0xFFFFFFFF}; +}; + +/** + * @brief 子通道依赖 + */ +struct SubpassDependency { + u32 srcSubpass{0}; + u32 dstSubpass{0}; +}; + +/** + * @brief 渲染通道信息 + */ +struct RenderPassInfo { + std::vector colorAttachments; + DepthStencilAttachment depthStencilAttachment; + std::vector subpasses; + std::vector dependencies; +}; + +/** + * @brief 渲染通道抽象类 + * + * 定义渲染通道的附件格式和操作。 + * 参考 Cocos Creator 的 RenderPass 设计。 + */ +class RenderPass : public Object { +public: + /** + * @brief 析构函数 + */ + ~RenderPass() override; + + /** + * @brief 获取类型名称 + * @return 类型名称 + */ + const char* getTypeName() const override { return "RenderPass"; } + + /** + * @brief 初始化渲染通道 + * @param info 渲染通道信息 + */ + void initialize(const RenderPassInfo& info); + + /** + * @brief 销毁渲染通道 + */ + void destroy(); + + /** + * @brief 获取颜色附件数量 + * @return 颜色附件数量 + */ + u32 getColorAttachmentCount() const { return static_cast(colorAttachments_.size()); } + + /** + * @brief 获取颜色附件 + * @param index 索引 + * @return 颜色附件 + */ + const ColorAttachment& getColorAttachment(u32 index) const { return colorAttachments_[index]; } + + /** + * @brief 获取深度模板附件 + * @return 深度模板附件 + */ + const DepthStencilAttachment& getDepthStencilAttachment() const { return depthStencilAttachment_; } + + /** + * @brief 是否有深度附件 + * @return 是否有深度附件 + */ + bool hasDepth() const { return depthStencilAttachment_.format != Format::Unknown; } + + /** + * @brief 是否有模板附件 + * @return 是否有模板附件 + */ + bool hasStencil() const; + +protected: + RenderPass() = default; + + /** + * @brief 实际初始化实现 + * @param info 渲染通道信息 + */ + virtual void doInit(const RenderPassInfo& info) = 0; + + /** + * @brief 实际销毁实现 + */ + virtual void doDestroy() = 0; + + std::vector colorAttachments_; + DepthStencilAttachment depthStencilAttachment_; + std::vector subpasses_; + std::vector dependencies_; +}; + +} // namespace gfx +} // namespace extra2d diff --git a/Extra2D/include/extra2d/gfx/GFXSampler.h b/Extra2D/include/extra2d/gfx/GFXSampler.h new file mode 100644 index 0000000..eefb4f8 --- /dev/null +++ b/Extra2D/include/extra2d/gfx/GFXSampler.h @@ -0,0 +1,118 @@ +#pragma once + +#include +#include + +namespace extra2d { +namespace gfx { + +/** + * @brief 采样器信息 + */ +struct SamplerInfo { + Filter minFilter{Filter::Linear}; + Filter magFilter{Filter::Linear}; + Filter mipFilter{Filter::None}; + Address addressU{Address::Clamp}; + Address addressV{Address::Clamp}; + Address addressW{Address::Clamp}; + float maxAnisotropy{16.0f}; + ComparisonFunc comparisonFunc{ComparisonFunc::Never}; + float minLOD{0.0f}; + float maxLOD{1000.0f}; + float mipLODBias{0.0f}; +}; + +/** + * @brief 采样器抽象类 + * + * 定义纹理采样参数。 + * 参考 Cocos Creator 的 Sampler 设计。 + */ +class Sampler : public Object { +public: + /** + * @brief 析构函数 + */ + ~Sampler() override; + + /** + * @brief 获取类型名称 + * @return 类型名称 + */ + const char* getTypeName() const override { return "Sampler"; } + + /** + * @brief 初始化采样器 + * @param info 采样器信息 + */ + void initialize(const SamplerInfo& info); + + /** + * @brief 销毁采样器 + */ + void destroy(); + + /** + * @brief 获取缩小过滤 + * @return 缩小过滤 + */ + Filter getMinFilter() const { return minFilter_; } + + /** + * @brief 获取放大过滤 + * @return 放大过滤 + */ + Filter getMagFilter() const { return magFilter_; } + + /** + * @brief 获取 MIP 过滤 + * @return MIP 过滤 + */ + Filter getMipFilter() const { return mipFilter_; } + + /** + * @brief 获取 U 方向寻址模式 + * @return U 方向寻址模式 + */ + Address getAddressU() const { return addressU_; } + + /** + * @brief 获取 V 方向寻址模式 + * @return V 方向寻址模式 + */ + Address getAddressV() const { return addressV_; } + + /** + * @brief 获取 W 方向寻址模式 + * @return W 方向寻址模式 + */ + Address getAddressW() const { return addressW_; } + + /** + * @brief 获取最大各向异性 + * @return 最大各向异性 + */ + float getMaxAnisotropy() const { return maxAnisotropy_; } + +protected: + Sampler() = default; + + virtual void doInit(const SamplerInfo& info) = 0; + virtual void doDestroy() = 0; + + Filter minFilter_{Filter::Linear}; + Filter magFilter_{Filter::Linear}; + Filter mipFilter_{Filter::None}; + Address addressU_{Address::Clamp}; + Address addressV_{Address::Clamp}; + Address addressW_{Address::Clamp}; + float maxAnisotropy_{16.0f}; + ComparisonFunc comparisonFunc_{ComparisonFunc::Never}; + float minLOD_{0.0f}; + float maxLOD_{1000.0f}; + float mipLODBias_{0.0f}; +}; + +} // namespace gfx +} // namespace extra2d diff --git a/Extra2D/include/extra2d/gfx/GFXSwapchain.h b/Extra2D/include/extra2d/gfx/GFXSwapchain.h new file mode 100644 index 0000000..e5cf9b8 --- /dev/null +++ b/Extra2D/include/extra2d/gfx/GFXSwapchain.h @@ -0,0 +1,134 @@ +#pragma once + +#include +#include +#include + +namespace extra2d { +namespace gfx { + +class Texture; + +/** + * @brief 交换链信息 + */ +struct SwapchainInfo { + u32 width{0}; + u32 height{0}; + Format format{Format::RGBA8}; + u32 imageCount{2}; + bool vsync{true}; +}; + +/** + * @brief 交换链纹理信息 + */ +struct SwapchainTextureInfo { + Texture* texture{nullptr}; + Format format{Format::Unknown}; + u32 width{0}; + u32 height{0}; +}; + +/** + * @brief 交换链抽象类 + * + * 管理显示用的交换链。 + * 参考 Cocos Creator 的 Swapchain 设计。 + */ +class Swapchain : public Object { +public: + /** + * @brief 析构函数 + */ + ~Swapchain() override; + + /** + * @brief 获取类型名称 + * @return 类型名称 + */ + const char* getTypeName() const override { return "Swapchain"; } + + /** + * @brief 初始化交换链 + * @param info 交换链信息 + */ + void initialize(const SwapchainInfo& info); + + /** + * @brief 销毁交换链 + */ + void destroy(); + + /** + * @brief 调整大小 + * @param width 宽度 + * @param height 高度 + */ + void resize(u32 width, u32 height); + + /** + * @brief 获取宽度 + * @return 宽度 + */ + u32 getWidth() const { return width_; } + + /** + * @brief 获取高度 + * @return 高度 + */ + u32 getHeight() const { return height_; } + + /** + * @brief 获取颜色格式 + * @return 颜色格式 + */ + Format getFormat() const { return format_; } + + /** + * @brief 获取当前纹理 + * @return 当前纹理 + */ + virtual Texture* getColorTexture() = 0; + + /** + * @brief 获取深度模板纹理 + * @return 深度模板纹理 + */ + virtual Texture* getDepthStencilTexture() = 0; + + /** + * @brief 呈现 + */ + virtual void present() = 0; + +protected: + Swapchain() = default; + + /** + * @brief 实际初始化实现 + * @param info 交换链信息 + */ + virtual void doInit(const SwapchainInfo& info) = 0; + + /** + * @brief 实际销毁实现 + */ + virtual void doDestroy() = 0; + + /** + * @brief 实际调整大小实现 + * @param width 宽度 + * @param height 高度 + */ + virtual void doResize(u32 width, u32 height) = 0; + + u32 width_{0}; + u32 height_{0}; + Format format_{Format::RGBA8}; + u32 imageCount_{2}; + bool vsync_{true}; +}; + +} // namespace gfx +} // namespace extra2d diff --git a/Extra2D/include/extra2d/gfx/opengl/GLDescriptorSet.h b/Extra2D/include/extra2d/gfx/opengl/GLDescriptorSet.h new file mode 100644 index 0000000..5914af3 --- /dev/null +++ b/Extra2D/include/extra2d/gfx/opengl/GLDescriptorSet.h @@ -0,0 +1,118 @@ +#pragma once + +#include +#include + +namespace extra2d { +namespace gfx { + +class GLBuffer; +class GLTexture; + +/** + * @brief OpenGL 描述符集布局实现 + */ +class GLDescriptorSetLayout : public DescriptorSetLayout { +public: + /** + * @brief 构造函数 + */ + GLDescriptorSetLayout(); + + /** + * @brief 析构函数 + */ + ~GLDescriptorSetLayout() override; + + /** + * @brief 获取类型名称 + * @return 类型名称 + */ + const char* getTypeName() const override { return "GLDescriptorSetLayout"; } + +protected: + void doInit(const DescriptorSetLayoutInfo& info) override; + void doDestroy() override; +}; + +/** + * @brief 描述符绑定信息 + */ +struct DescriptorBinding { + DescriptorType type{DescriptorType::Unknown}; + GLBuffer* buffer{nullptr}; + GLTexture* texture{nullptr}; + u32 samplerHandle{0}; +}; + +/** + * @brief OpenGL 描述符集实现 + */ +class GLDescriptorSet : public DescriptorSet { +public: + /** + * @brief 构造函数 + */ + GLDescriptorSet(); + + /** + * @brief 析构函数 + */ + ~GLDescriptorSet() override; + + /** + * @brief 获取类型名称 + * @return 类型名称 + */ + const char* getTypeName() const override { return "GLDescriptorSet"; } + + /** + * @brief 绑定缓冲区 + * @param binding 绑定点 + * @param buffer 缓冲区 + */ + void bindBuffer(u32 binding, Buffer* buffer) override; + + /** + * @brief 绑定纹理 + * @param binding 绑定点 + * @param texture 纹理 + */ + void bindTexture(u32 binding, Texture* texture) override; + + /** + * @brief 绑定采样器 + * @param binding 绑定点 + * @param sampler 采样器 + */ + void bindSampler(u32 binding, Sampler* sampler) override; + + /** + * @brief 绑定采样器纹理 + * @param binding 绑定点 + * @param texture 纹理 + * @param sampler 采样器 + */ + void bindSamplerTexture(u32 binding, Texture* texture, Sampler* sampler) override; + + /** + * @brief 更新描述符 + */ + void update() override; + + /** + * @brief 绑定到 OpenGL 状态 + * @param program 着色器程序 + */ + void bindGL(u32 program); + +protected: + void doInit(const DescriptorSetInfo& info) override; + void doDestroy() override; + +private: + std::unordered_map bindings_; +}; + +} // namespace gfx +} // namespace extra2d diff --git a/Extra2D/include/extra2d/gfx/opengl/GLFramebuffer.h b/Extra2D/include/extra2d/gfx/opengl/GLFramebuffer.h new file mode 100644 index 0000000..1c6a348 --- /dev/null +++ b/Extra2D/include/extra2d/gfx/opengl/GLFramebuffer.h @@ -0,0 +1,58 @@ +#pragma once + +#include + +#ifdef _WIN32 +#include +#else +#include +#endif + +namespace extra2d { +namespace gfx { + +/** + * @brief OpenGL 帧缓冲实现 + */ +class GLFramebuffer : public Framebuffer { +public: + /** + * @brief 构造函数 + */ + GLFramebuffer(); + + /** + * @brief 析构函数 + */ + ~GLFramebuffer() override; + + /** + * @brief 获取类型名称 + * @return 类型名称 + */ + const char* getTypeName() const override { return "GLFramebuffer"; } + + /** + * @brief 获取 OpenGL 帧缓冲句柄 + * @return 帧缓冲句柄 + */ + GLuint getHandle() const { return framebuffer_; } + +protected: + /** + * @brief 实际初始化实现 + * @param info 帧缓冲信息 + */ + void doInit(const FramebufferInfo& info) override; + + /** + * @brief 实际销毁实现 + */ + void doDestroy() override; + +private: + GLuint framebuffer_{0}; +}; + +} // namespace gfx +} // namespace extra2d diff --git a/Extra2D/include/extra2d/gfx/opengl/GLQueue.h b/Extra2D/include/extra2d/gfx/opengl/GLQueue.h new file mode 100644 index 0000000..d673009 --- /dev/null +++ b/Extra2D/include/extra2d/gfx/opengl/GLQueue.h @@ -0,0 +1,80 @@ +#pragma once + +#include + +namespace extra2d { +namespace gfx { + +/** + * @brief OpenGL 命令队列实现 + * + * OpenGL 没有显式的队列概念,此实现主要用于统计和同步。 + */ +class GLQueue : public Queue { +public: + /** + * @brief 构造函数 + */ + GLQueue(); + + /** + * @brief 析构函数 + */ + ~GLQueue() override; + + /** + * @brief 获取类型名称 + * @return 类型名称 + */ + const char* getTypeName() const override { return "GLQueue"; } + + /** + * @brief 提交命令缓冲区 + * @param cmdBuffers 命令缓冲区数组 + * @param count 命令缓冲区数量 + */ + void submit(CommandBuffer* const* cmdBuffers, u32 count) override; + + /** + * @brief 获取绘制调用次数 + * @return 绘制调用次数 + */ + u32 getNumDrawCalls() const { return numDrawCalls_; } + + /** + * @brief 获取实例数量 + * @return 实例数量 + */ + u32 getNumInstances() const { return numInstances_; } + + /** + * @brief 获取三角形数量 + * @return 三角形数量 + */ + u32 getNumTriangles() const { return numTriangles_; } + + /** + * @brief 重置统计信息 + */ + void resetStats(); + +protected: + /** + * @brief 实际初始化实现 + * @param info 队列信息 + */ + void doInit(const QueueInfo& info) override; + + /** + * @brief 实际销毁实现 + */ + void doDestroy() override; + +private: + u32 numDrawCalls_{0}; + u32 numInstances_{0}; + u32 numTriangles_{0}; +}; + +} // namespace gfx +} // namespace extra2d diff --git a/Extra2D/include/extra2d/gfx/opengl/GLRenderPass.h b/Extra2D/include/extra2d/gfx/opengl/GLRenderPass.h new file mode 100644 index 0000000..4d79671 --- /dev/null +++ b/Extra2D/include/extra2d/gfx/opengl/GLRenderPass.h @@ -0,0 +1,45 @@ +#pragma once + +#include + +namespace extra2d { +namespace gfx { + +/** + * @brief OpenGL 渲染通道实现 + * + * OpenGL 没有显式的 RenderPass 概念,此实现主要用于存储附件信息。 + */ +class GLRenderPass : public RenderPass { +public: + /** + * @brief 构造函数 + */ + GLRenderPass(); + + /** + * @brief 析构函数 + */ + ~GLRenderPass() override; + + /** + * @brief 获取类型名称 + * @return 类型名称 + */ + const char* getTypeName() const override { return "GLRenderPass"; } + +protected: + /** + * @brief 实际初始化实现 + * @param info 渲染通道信息 + */ + void doInit(const RenderPassInfo& info) override; + + /** + * @brief 实际销毁实现 + */ + void doDestroy() override; +}; + +} // namespace gfx +} // namespace extra2d diff --git a/Extra2D/include/extra2d/gfx/opengl/GLSampler.h b/Extra2D/include/extra2d/gfx/opengl/GLSampler.h new file mode 100644 index 0000000..58fce66 --- /dev/null +++ b/Extra2D/include/extra2d/gfx/opengl/GLSampler.h @@ -0,0 +1,56 @@ +#pragma once + +#include + +#ifdef _WIN32 +#include +#else +#include +#endif + +namespace extra2d { +namespace gfx { + +/** + * @brief OpenGL 采样器实现 + */ +class GLSampler : public Sampler { +public: + /** + * @brief 构造函数 + */ + GLSampler(); + + /** + * @brief 析构函数 + */ + ~GLSampler() override; + + /** + * @brief 获取类型名称 + * @return 类型名称 + */ + const char* getTypeName() const override { return "GLSampler"; } + + /** + * @brief 获取 OpenGL 采样器句柄 + * @return 采样器句柄 + */ + GLuint getHandle() const { return sampler_; } + + /** + * @brief 绑定到纹理单元 + * @param unit 纹理单元 + */ + void bind(u32 unit); + +protected: + void doInit(const SamplerInfo& info) override; + void doDestroy() override; + +private: + GLuint sampler_{0}; +}; + +} // namespace gfx +} // namespace extra2d diff --git a/Extra2D/include/extra2d/gfx/opengl/GLSwapchain.h b/Extra2D/include/extra2d/gfx/opengl/GLSwapchain.h new file mode 100644 index 0000000..ccb4cd7 --- /dev/null +++ b/Extra2D/include/extra2d/gfx/opengl/GLSwapchain.h @@ -0,0 +1,81 @@ +#pragma once + +#include +#include + +#ifdef _WIN32 +#include +#else +#include +#endif + +namespace extra2d { +namespace gfx { + +/** + * @brief OpenGL 交换链实现 + * + * OpenGL 没有显式的交换链概念,由窗口系统管理。 + * 此实现提供默认帧缓冲的颜色和深度模板纹理。 + */ +class GLSwapchain : public Swapchain { +public: + /** + * @brief 构造函数 + */ + GLSwapchain(); + + /** + * @brief 析构函数 + */ + ~GLSwapchain() override; + + /** + * @brief 获取类型名称 + * @return 类型名称 + */ + const char* getTypeName() const override { return "GLSwapchain"; } + + /** + * @brief 获取当前颜色纹理 + * @return 颜色纹理(返回 nullptr 表示默认帧缓冲) + */ + Texture* getColorTexture() override; + + /** + * @brief 获取深度模板纹理 + * @return 深度模板纹理 + */ + Texture* getDepthStencilTexture() override; + + /** + * @brief 呈现 + */ + void present() override; + +protected: + /** + * @brief 实际初始化实现 + * @param info 交换链信息 + */ + void doInit(const SwapchainInfo& info) override; + + /** + * @brief 实际销毁实现 + */ + void doDestroy() override; + + /** + * @brief 实际调整大小实现 + * @param width 宽度 + * @param height 高度 + */ + void doResize(u32 width, u32 height) override; + +private: + IntrusivePtr colorTexture_; + IntrusivePtr depthStencilTexture_; +}; + +} // namespace gfx +} // namespace extra2d diff --git a/Extra2D/include/extra2d/core/color.h b/Extra2D/include/extra2d/math/Color.h similarity index 99% rename from Extra2D/include/extra2d/core/color.h rename to Extra2D/include/extra2d/math/Color.h index 8729586..137934e 100644 --- a/Extra2D/include/extra2d/core/color.h +++ b/Extra2D/include/extra2d/math/Color.h @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include namespace extra2d { diff --git a/Extra2D/include/extra2d/core/math_types.h b/Extra2D/include/extra2d/math/Math.h similarity index 99% rename from Extra2D/include/extra2d/core/math_types.h rename to Extra2D/include/extra2d/math/Math.h index ef63602..ce3b91a 100644 --- a/Extra2D/include/extra2d/core/math_types.h +++ b/Extra2D/include/extra2d/math/Math.h @@ -2,7 +2,7 @@ #include #include -#include +#include #include #include #include diff --git a/Extra2D/include/extra2d/platform/Input.h b/Extra2D/include/extra2d/platform/Input.h index 65c9c41..aaf5547 100644 --- a/Extra2D/include/extra2d/platform/Input.h +++ b/Extra2D/include/extra2d/platform/Input.h @@ -1,7 +1,7 @@ #pragma once -#include -#include +#include +#include #include #include diff --git a/Extra2D/include/extra2d/platform/SDLHelper.h b/Extra2D/include/extra2d/platform/SDLHelper.h index 203d5ea..a14ec6f 100644 --- a/Extra2D/include/extra2d/platform/SDLHelper.h +++ b/Extra2D/include/extra2d/platform/SDLHelper.h @@ -1,8 +1,8 @@ #pragma once -#include -#include -#include +#include +#include +#include struct SDL_Window; struct SDL_Event; diff --git a/Extra2D/include/extra2d/platform/Window.h b/Extra2D/include/extra2d/platform/Window.h index f4e9ec7..d9e20e9 100644 --- a/Extra2D/include/extra2d/platform/Window.h +++ b/Extra2D/include/extra2d/platform/Window.h @@ -1,7 +1,7 @@ #pragma once -#include -#include +#include +#include #include struct SDL_Window; diff --git a/Extra2D/include/extra2d/2d/renderer/Batcher2d.h b/Extra2D/include/extra2d/renderer/2d/Batcher2d.h similarity index 94% rename from Extra2D/include/extra2d/2d/renderer/Batcher2d.h rename to Extra2D/include/extra2d/renderer/2d/Batcher2d.h index 5c24f24..efb8219 100644 --- a/Extra2D/include/extra2d/2d/renderer/Batcher2d.h +++ b/Extra2D/include/extra2d/renderer/2d/Batcher2d.h @@ -1,10 +1,10 @@ #pragma once -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include #include #include #include diff --git a/Extra2D/include/extra2d/2d/renderer/RenderDrawInfo.h b/Extra2D/include/extra2d/renderer/2d/RenderDrawInfo.h similarity index 98% rename from Extra2D/include/extra2d/2d/renderer/RenderDrawInfo.h rename to Extra2D/include/extra2d/renderer/2d/RenderDrawInfo.h index 1ccd464..339d9bc 100644 --- a/Extra2D/include/extra2d/2d/renderer/RenderDrawInfo.h +++ b/Extra2D/include/extra2d/renderer/2d/RenderDrawInfo.h @@ -1,7 +1,7 @@ #pragma once -#include -#include +#include +#include #include namespace extra2d { diff --git a/Extra2D/include/extra2d/2d/renderer/RenderEntity.h b/Extra2D/include/extra2d/renderer/2d/RenderEntity.h similarity index 98% rename from Extra2D/include/extra2d/2d/renderer/RenderEntity.h rename to Extra2D/include/extra2d/renderer/2d/RenderEntity.h index 5312e41..65afc40 100644 --- a/Extra2D/include/extra2d/2d/renderer/RenderEntity.h +++ b/Extra2D/include/extra2d/renderer/2d/RenderEntity.h @@ -1,7 +1,7 @@ #pragma once -#include -#include +#include +#include #include #include diff --git a/Extra2D/include/extra2d/2d/renderer/StencilManager.h b/Extra2D/include/extra2d/renderer/2d/StencilManager.h similarity index 99% rename from Extra2D/include/extra2d/2d/renderer/StencilManager.h rename to Extra2D/include/extra2d/renderer/2d/StencilManager.h index 87cdac3..7aa7086 100644 --- a/Extra2D/include/extra2d/2d/renderer/StencilManager.h +++ b/Extra2D/include/extra2d/renderer/2d/StencilManager.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include diff --git a/Extra2D/include/extra2d/2d/renderer/UIMeshBuffer.h b/Extra2D/include/extra2d/renderer/2d/UIMeshBuffer.h similarity index 96% rename from Extra2D/include/extra2d/2d/renderer/UIMeshBuffer.h rename to Extra2D/include/extra2d/renderer/2d/UIMeshBuffer.h index e565048..d70d50d 100644 --- a/Extra2D/include/extra2d/2d/renderer/UIMeshBuffer.h +++ b/Extra2D/include/extra2d/renderer/2d/UIMeshBuffer.h @@ -1,7 +1,7 @@ #pragma once -#include -#include +#include +#include #include #include diff --git a/Extra2D/include/extra2d/2d/components/Sprite.h b/Extra2D/include/extra2d/renderer/2d/components/Sprite.h similarity index 92% rename from Extra2D/include/extra2d/2d/components/Sprite.h rename to Extra2D/include/extra2d/renderer/2d/components/Sprite.h index 56e5605..138c10c 100644 --- a/Extra2D/include/extra2d/2d/components/Sprite.h +++ b/Extra2D/include/extra2d/renderer/2d/components/Sprite.h @@ -1,10 +1,10 @@ #pragma once -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include namespace extra2d { diff --git a/Extra2D/include/extra2d/2d/components/SpriteFrame.h b/Extra2D/include/extra2d/renderer/2d/components/SpriteFrame.h similarity index 95% rename from Extra2D/include/extra2d/2d/components/SpriteFrame.h rename to Extra2D/include/extra2d/renderer/2d/components/SpriteFrame.h index 25baf68..c9ae936 100644 --- a/Extra2D/include/extra2d/2d/components/SpriteFrame.h +++ b/Extra2D/include/extra2d/renderer/2d/components/SpriteFrame.h @@ -1,8 +1,8 @@ #pragma once -#include -#include -#include +#include +#include +#include namespace extra2d { diff --git a/Extra2D/include/extra2d/core/scene-graph/Camera.h b/Extra2D/include/extra2d/scene/Camera.h similarity index 99% rename from Extra2D/include/extra2d/core/scene-graph/Camera.h rename to Extra2D/include/extra2d/scene/Camera.h index aacfff5..adadfbb 100644 --- a/Extra2D/include/extra2d/core/scene-graph/Camera.h +++ b/Extra2D/include/extra2d/scene/Camera.h @@ -1,7 +1,7 @@ #pragma once -#include -#include +#include +#include #include #include #include diff --git a/Extra2D/include/extra2d/core/scene-graph/Component.h b/Extra2D/include/extra2d/scene/Component.h similarity index 96% rename from Extra2D/include/extra2d/core/scene-graph/Component.h rename to Extra2D/include/extra2d/scene/Component.h index 26ea5b2..a259e18 100644 --- a/Extra2D/include/extra2d/core/scene-graph/Component.h +++ b/Extra2D/include/extra2d/scene/Component.h @@ -1,7 +1,7 @@ #pragma once -#include -#include +#include +#include namespace extra2d { diff --git a/Extra2D/include/extra2d/core/scene-graph/Layers.h b/Extra2D/include/extra2d/scene/Layers.h similarity index 99% rename from Extra2D/include/extra2d/core/scene-graph/Layers.h rename to Extra2D/include/extra2d/scene/Layers.h index 276cf67..0157f53 100644 --- a/Extra2D/include/extra2d/core/scene-graph/Layers.h +++ b/Extra2D/include/extra2d/scene/Layers.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include #include diff --git a/Extra2D/include/extra2d/core/scene-graph/Model.h b/Extra2D/include/extra2d/scene/Model.h similarity index 97% rename from Extra2D/include/extra2d/core/scene-graph/Model.h rename to Extra2D/include/extra2d/scene/Model.h index e4fb2a0..2b2d708 100644 --- a/Extra2D/include/extra2d/core/scene-graph/Model.h +++ b/Extra2D/include/extra2d/scene/Model.h @@ -1,8 +1,8 @@ #pragma once -#include -#include -#include +#include +#include +#include #include #include diff --git a/Extra2D/include/extra2d/core/scene-graph/Node.h b/Extra2D/include/extra2d/scene/Node.h similarity index 99% rename from Extra2D/include/extra2d/core/scene-graph/Node.h rename to Extra2D/include/extra2d/scene/Node.h index 2ab78ca..59f5074 100644 --- a/Extra2D/include/extra2d/core/scene-graph/Node.h +++ b/Extra2D/include/extra2d/scene/Node.h @@ -1,8 +1,8 @@ #pragma once -#include -#include -#include +#include +#include +#include #include #include diff --git a/Extra2D/include/extra2d/core/scene-graph/RenderScene.h b/Extra2D/include/extra2d/scene/RenderScene.h similarity index 98% rename from Extra2D/include/extra2d/core/scene-graph/RenderScene.h rename to Extra2D/include/extra2d/scene/RenderScene.h index dac6a46..254493f 100644 --- a/Extra2D/include/extra2d/core/scene-graph/RenderScene.h +++ b/Extra2D/include/extra2d/scene/RenderScene.h @@ -1,7 +1,7 @@ #pragma once -#include -#include +#include +#include #include #include diff --git a/Extra2D/include/extra2d/core/Root.h b/Extra2D/include/extra2d/scene/Root.h similarity index 95% rename from Extra2D/include/extra2d/core/Root.h rename to Extra2D/include/extra2d/scene/Root.h index 3f038ad..eba5a42 100644 --- a/Extra2D/include/extra2d/core/Root.h +++ b/Extra2D/include/extra2d/scene/Root.h @@ -1,10 +1,10 @@ #pragma once -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include #include #include #include diff --git a/Extra2D/include/extra2d/core/scene-graph/Scene.h b/Extra2D/include/extra2d/scene/Scene.h similarity index 98% rename from Extra2D/include/extra2d/core/scene-graph/Scene.h rename to Extra2D/include/extra2d/scene/Scene.h index 2d347c1..5e8a5b1 100644 --- a/Extra2D/include/extra2d/core/scene-graph/Scene.h +++ b/Extra2D/include/extra2d/scene/Scene.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include diff --git a/Extra2D/include/extra2d/utils/random.h b/Extra2D/include/extra2d/utils/random.h index 3c5e768..cefb074 100644 --- a/Extra2D/include/extra2d/utils/random.h +++ b/Extra2D/include/extra2d/utils/random.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #include namespace extra2d { diff --git a/Extra2D/include/extra2d/utils/timer.h b/Extra2D/include/extra2d/utils/timer.h index 2bb0514..cc41219 100644 --- a/Extra2D/include/extra2d/utils/timer.h +++ b/Extra2D/include/extra2d/utils/timer.h @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include #include #include diff --git a/Extra2D/src/application/Application.cpp b/Extra2D/src/application/Application.cpp index 67df24e..6b3b3fe 100644 --- a/Extra2D/src/application/Application.cpp +++ b/Extra2D/src/application/Application.cpp @@ -3,7 +3,7 @@ #include #include #include -#include +#include #include #include diff --git a/Extra2D/src/core/assets/Asset.cpp b/Extra2D/src/assets/Asset.cpp similarity index 97% rename from Extra2D/src/core/assets/Asset.cpp rename to Extra2D/src/assets/Asset.cpp index 3356cc7..58e2056 100644 --- a/Extra2D/src/core/assets/Asset.cpp +++ b/Extra2D/src/assets/Asset.cpp @@ -1,4 +1,4 @@ -#include +#include namespace extra2d { diff --git a/Extra2D/src/core/assets/AssetManager.cpp b/Extra2D/src/assets/AssetManager.cpp similarity index 96% rename from Extra2D/src/core/assets/AssetManager.cpp rename to Extra2D/src/assets/AssetManager.cpp index 221dcb5..ce270cf 100644 --- a/Extra2D/src/core/assets/AssetManager.cpp +++ b/Extra2D/src/assets/AssetManager.cpp @@ -1,5 +1,5 @@ -#include -#include +#include +#include #include #include #include diff --git a/Extra2D/src/core/assets/ImageAsset.cpp b/Extra2D/src/assets/ImageAsset.cpp similarity index 98% rename from Extra2D/src/core/assets/ImageAsset.cpp rename to Extra2D/src/assets/ImageAsset.cpp index 02abe37..d904ce2 100644 --- a/Extra2D/src/core/assets/ImageAsset.cpp +++ b/Extra2D/src/assets/ImageAsset.cpp @@ -1,4 +1,4 @@ -#include +#include #include namespace extra2d { diff --git a/Extra2D/src/core/assets/Material.cpp b/Extra2D/src/assets/Material.cpp similarity index 99% rename from Extra2D/src/core/assets/Material.cpp rename to Extra2D/src/assets/Material.cpp index bf8174a..4525b40 100644 --- a/Extra2D/src/core/assets/Material.cpp +++ b/Extra2D/src/assets/Material.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include diff --git a/Extra2D/src/core/assets/Texture2D.cpp b/Extra2D/src/assets/Texture2D.cpp similarity index 98% rename from Extra2D/src/core/assets/Texture2D.cpp rename to Extra2D/src/assets/Texture2D.cpp index c711fc2..3ba3bad 100644 --- a/Extra2D/src/core/assets/Texture2D.cpp +++ b/Extra2D/src/assets/Texture2D.cpp @@ -1,4 +1,4 @@ -#include +#include #include namespace extra2d { diff --git a/Extra2D/src/core/Object.cpp b/Extra2D/src/base/Object.cpp similarity index 97% rename from Extra2D/src/core/Object.cpp rename to Extra2D/src/base/Object.cpp index 186a752..ba0a5ae 100644 --- a/Extra2D/src/core/Object.cpp +++ b/Extra2D/src/base/Object.cpp @@ -1,4 +1,4 @@ -#include +#include namespace extra2d { diff --git a/Extra2D/src/engine/Engine.cpp b/Extra2D/src/engine/Engine.cpp index 6a9817d..4a2ce20 100644 --- a/Extra2D/src/engine/Engine.cpp +++ b/Extra2D/src/engine/Engine.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include namespace extra2d { diff --git a/Extra2D/src/core/event/EventBus.cpp b/Extra2D/src/event/EventBus.cpp similarity index 91% rename from Extra2D/src/core/event/EventBus.cpp rename to Extra2D/src/event/EventBus.cpp index f67e183..7bc1a94 100644 --- a/Extra2D/src/core/event/EventBus.cpp +++ b/Extra2D/src/event/EventBus.cpp @@ -1,4 +1,4 @@ -#include +#include namespace extra2d { diff --git a/Extra2D/src/core/event/EventTarget.cpp b/Extra2D/src/event/EventTarget.cpp similarity index 80% rename from Extra2D/src/core/event/EventTarget.cpp rename to Extra2D/src/event/EventTarget.cpp index be1b176..74e230f 100644 --- a/Extra2D/src/core/event/EventTarget.cpp +++ b/Extra2D/src/event/EventTarget.cpp @@ -1,4 +1,4 @@ -#include +#include namespace extra2d { diff --git a/Extra2D/src/gfx/GFXDescriptorSet.cpp b/Extra2D/src/gfx/GFXDescriptorSet.cpp new file mode 100644 index 0000000..8430fd3 --- /dev/null +++ b/Extra2D/src/gfx/GFXDescriptorSet.cpp @@ -0,0 +1,39 @@ +#include + +namespace extra2d { +namespace gfx { + +// ==================== DescriptorSetLayout ==================== + +DescriptorSetLayout::~DescriptorSetLayout() { + destroy(); +} + +void DescriptorSetLayout::initialize(const DescriptorSetLayoutInfo& info) { + bindings_ = info.bindings; + doInit(info); +} + +void DescriptorSetLayout::destroy() { + doDestroy(); + bindings_.clear(); +} + +// ==================== DescriptorSet ==================== + +DescriptorSet::~DescriptorSet() { + destroy(); +} + +void DescriptorSet::initialize(const DescriptorSetInfo& info) { + layout_ = const_cast(info.layout); + doInit(info); +} + +void DescriptorSet::destroy() { + doDestroy(); + layout_ = nullptr; +} + +} // namespace gfx +} // namespace extra2d diff --git a/Extra2D/src/gfx/GFXFramebuffer.cpp b/Extra2D/src/gfx/GFXFramebuffer.cpp new file mode 100644 index 0000000..a455d4d --- /dev/null +++ b/Extra2D/src/gfx/GFXFramebuffer.cpp @@ -0,0 +1,40 @@ +#include +#include +#include + +namespace extra2d { +namespace gfx { + +Framebuffer::~Framebuffer() { + destroy(); +} + +void Framebuffer::initialize(const FramebufferInfo& info) { + renderPass_ = info.renderPass; + colorTextures_ = info.colorTextures; + depthStencilTexture_ = info.depthStencilTexture; + + // 从第一个颜色纹理获取尺寸 + if (!colorTextures_.empty() && colorTextures_[0]) { + width_ = colorTextures_[0]->getWidth(); + height_ = colorTextures_[0]->getHeight(); + } else if (depthStencilTexture_) { + width_ = depthStencilTexture_->getWidth(); + height_ = depthStencilTexture_->getHeight(); + } + + doInit(info); +} + +void Framebuffer::destroy() { + doDestroy(); + + renderPass_ = nullptr; + colorTextures_.clear(); + depthStencilTexture_ = nullptr; + width_ = 0; + height_ = 0; +} + +} // namespace gfx +} // namespace extra2d diff --git a/Extra2D/src/gfx/GFXQueue.cpp b/Extra2D/src/gfx/GFXQueue.cpp new file mode 100644 index 0000000..803b42f --- /dev/null +++ b/Extra2D/src/gfx/GFXQueue.cpp @@ -0,0 +1,20 @@ +#include + +namespace extra2d { +namespace gfx { + +Queue::~Queue() { + destroy(); +} + +void Queue::initialize(const QueueInfo& info) { + type_ = info.type; + doInit(info); +} + +void Queue::destroy() { + doDestroy(); +} + +} // namespace gfx +} // namespace extra2d diff --git a/Extra2D/src/gfx/GFXRenderPass.cpp b/Extra2D/src/gfx/GFXRenderPass.cpp new file mode 100644 index 0000000..8e9dae4 --- /dev/null +++ b/Extra2D/src/gfx/GFXRenderPass.cpp @@ -0,0 +1,33 @@ +#include + +namespace extra2d { +namespace gfx { + +RenderPass::~RenderPass() { + destroy(); +} + +void RenderPass::initialize(const RenderPassInfo& info) { + colorAttachments_ = info.colorAttachments; + depthStencilAttachment_ = info.depthStencilAttachment; + subpasses_ = info.subpasses; + dependencies_ = info.dependencies; + + doInit(info); +} + +void RenderPass::destroy() { + doDestroy(); + + colorAttachments_.clear(); + subpasses_.clear(); + dependencies_.clear(); +} + +bool RenderPass::hasStencil() const { + return depthStencilAttachment_.format == Format::DepthStencil || + depthStencilAttachment_.format == Format::Depth24Stencil8; +} + +} // namespace gfx +} // namespace extra2d diff --git a/Extra2D/src/gfx/GFXSampler.cpp b/Extra2D/src/gfx/GFXSampler.cpp new file mode 100644 index 0000000..68a8dad --- /dev/null +++ b/Extra2D/src/gfx/GFXSampler.cpp @@ -0,0 +1,31 @@ +#include + +namespace extra2d { +namespace gfx { + +Sampler::~Sampler() { + destroy(); +} + +void Sampler::initialize(const SamplerInfo& info) { + minFilter_ = info.minFilter; + magFilter_ = info.magFilter; + mipFilter_ = info.mipFilter; + addressU_ = info.addressU; + addressV_ = info.addressV; + addressW_ = info.addressW; + maxAnisotropy_ = info.maxAnisotropy; + comparisonFunc_ = info.comparisonFunc; + minLOD_ = info.minLOD; + maxLOD_ = info.maxLOD; + mipLODBias_ = info.mipLODBias; + + doInit(info); +} + +void Sampler::destroy() { + doDestroy(); +} + +} // namespace gfx +} // namespace extra2d diff --git a/Extra2D/src/gfx/GFXSwapchain.cpp b/Extra2D/src/gfx/GFXSwapchain.cpp new file mode 100644 index 0000000..6608919 --- /dev/null +++ b/Extra2D/src/gfx/GFXSwapchain.cpp @@ -0,0 +1,39 @@ +#include + +namespace extra2d { +namespace gfx { + +Swapchain::~Swapchain() { + destroy(); +} + +void Swapchain::initialize(const SwapchainInfo& info) { + width_ = info.width; + height_ = info.height; + format_ = info.format; + imageCount_ = info.imageCount; + vsync_ = info.vsync; + + doInit(info); +} + +void Swapchain::destroy() { + doDestroy(); + + width_ = 0; + height_ = 0; +} + +void Swapchain::resize(u32 width, u32 height) { + if (width_ == width && height_ == height) { + return; + } + + width_ = width; + height_ = height; + + doResize(width, height); +} + +} // namespace gfx +} // namespace extra2d diff --git a/Extra2D/src/gfx/opengl/GLCommandBuffer.cpp b/Extra2D/src/gfx/opengl/GLCommandBuffer.cpp index a7cfbd1..aef2244 100644 --- a/Extra2D/src/gfx/opengl/GLCommandBuffer.cpp +++ b/Extra2D/src/gfx/opengl/GLCommandBuffer.cpp @@ -161,7 +161,8 @@ void GLCommandBuffer::updateBuffer(Buffer* buffer, u32 offset, const void* data, if (!buffer || !data) return; auto* glBuffer = static_cast(buffer); - glBuffer->update(offset, data, size); + // TODO: 需要扩展 GLBuffer 支持带偏移的更新 + glBuffer->update(data, size); } void GLCommandBuffer::copyBuffer(Buffer* src, u32 srcOffset, Buffer* dst, u32 dstOffset, u32 size) { @@ -170,8 +171,8 @@ void GLCommandBuffer::copyBuffer(Buffer* src, u32 srcOffset, Buffer* dst, u32 ds auto* glSrc = static_cast(src); auto* glDst = static_cast(dst); - glBindBuffer(GL_COPY_READ_BUFFER, glSrc->getHandle()); - glBindBuffer(GL_COPY_WRITE_BUFFER, glDst->getHandle()); + glBindBuffer(GL_COPY_READ_BUFFER, glSrc->getGLHandle()); + glBindBuffer(GL_COPY_WRITE_BUFFER, glDst->getGLHandle()); glCopyBufferSubData(srcOffset, dstOffset, size); glBindBuffer(GL_COPY_READ_BUFFER, 0); glBindBuffer(GL_COPY_WRITE_BUFFER, 0); @@ -184,8 +185,8 @@ void GLCommandBuffer::copyTexture(Texture* src, Texture* dst) { auto* glDst = static_cast(dst); glCopyImageSubData( - glSrc->getHandle(), GL_TEXTURE_2D, 0, 0, 0, 0, - glDst->getHandle(), GL_TEXTURE_2D, 0, 0, 0, 0, + glSrc->getGLHandle(), GL_TEXTURE_2D, 0, 0, 0, 0, + glDst->getGLHandle(), GL_TEXTURE_2D, 0, 0, 0, 0, src->getWidth(), src->getHeight(), 1 ); } diff --git a/Extra2D/src/gfx/opengl/GLDescriptorSet.cpp b/Extra2D/src/gfx/opengl/GLDescriptorSet.cpp new file mode 100644 index 0000000..4a7f412 --- /dev/null +++ b/Extra2D/src/gfx/opengl/GLDescriptorSet.cpp @@ -0,0 +1,136 @@ +#include +#include +#include + +#ifdef _WIN32 +#include +#else +#include +#endif + +namespace extra2d { +namespace gfx { + +// ==================== GLDescriptorSetLayout ==================== + +GLDescriptorSetLayout::GLDescriptorSetLayout() = default; + +GLDescriptorSetLayout::~GLDescriptorSetLayout() { + doDestroy(); +} + +void GLDescriptorSetLayout::doInit(const DescriptorSetLayoutInfo& info) { + // OpenGL 没有显式的描述符集布局对象 + // 绑定信息已存储在基类中 +} + +void GLDescriptorSetLayout::doDestroy() { + // OpenGL 没有需要销毁的资源 +} + +// ==================== GLDescriptorSet ==================== + +GLDescriptorSet::GLDescriptorSet() = default; + +GLDescriptorSet::~GLDescriptorSet() { + doDestroy(); +} + +void GLDescriptorSet::bindBuffer(u32 binding, Buffer* buffer) { + if (!buffer) return; + + auto* glBuffer = static_cast(buffer); + DescriptorBinding& db = bindings_[binding]; + db.type = DescriptorType::UniformBuffer; + db.buffer = glBuffer; +} + +void GLDescriptorSet::bindTexture(u32 binding, Texture* texture) { + if (!texture) return; + + auto* glTexture = static_cast(texture); + DescriptorBinding& db = bindings_[binding]; + db.type = DescriptorType::Texture; + db.texture = glTexture; +} + +void GLDescriptorSet::bindSampler(u32 binding, Sampler* sampler) { + // OpenGL 采样器绑定需要纹理单元 + // 这里只存储采样器信息 + DescriptorBinding& db = bindings_[binding]; + db.type = DescriptorType::Sampler; +} + +void GLDescriptorSet::bindSamplerTexture(u32 binding, Texture* texture, Sampler* sampler) { + if (!texture) return; + + auto* glTexture = static_cast(texture); + DescriptorBinding& db = bindings_[binding]; + db.type = DescriptorType::SamplerTexture; + db.texture = glTexture; +} + +void GLDescriptorSet::update() { + // OpenGL 的描述符更新在绑定时进行 + // 这里不需要额外操作 +} + +void GLDescriptorSet::bindGL(u32 program) { + u32 textureUnit = 0; + + for (auto& pair : bindings_) { + u32 binding = pair.first; + DescriptorBinding& db = pair.second; + + switch (db.type) { + case DescriptorType::UniformBuffer: + case DescriptorType::DynamicUniformBuffer: + if (db.buffer) { + GLuint blockIndex = glGetUniformBlockIndex(program, + ("UniformBlock_" + std::to_string(binding)).c_str()); + if (blockIndex != GL_INVALID_INDEX) { + glUniformBlockBinding(program, blockIndex, binding); + glBindBufferBase(GL_UNIFORM_BUFFER, binding, db.buffer->getGLHandle()); + } + } + break; + + case DescriptorType::Texture: + case DescriptorType::SamplerTexture: + if (db.texture) { + glActiveTexture(GL_TEXTURE0 + textureUnit); + glBindTexture(GL_TEXTURE_2D, db.texture->getGLHandle()); + + GLint location = glGetUniformLocation(program, + ("u_texture_" + std::to_string(binding)).c_str()); + if (location >= 0) { + glUniform1i(location, textureUnit); + } + + textureUnit++; + } + break; + + default: + break; + } + } +} + +void GLDescriptorSet::doInit(const DescriptorSetInfo& info) { + // 初始化绑定槽位 + if (layout_) { + for (u32 i = 0; i < layout_->getBindingCount(); ++i) { + const auto& binding = layout_->getBinding(i); + bindings_[binding.binding] = DescriptorBinding{}; + bindings_[binding.binding].type = binding.descriptorType; + } + } +} + +void GLDescriptorSet::doDestroy() { + bindings_.clear(); +} + +} // namespace gfx +} // namespace extra2d diff --git a/Extra2D/src/gfx/opengl/GLFramebuffer.cpp b/Extra2D/src/gfx/opengl/GLFramebuffer.cpp new file mode 100644 index 0000000..e8fbe51 --- /dev/null +++ b/Extra2D/src/gfx/opengl/GLFramebuffer.cpp @@ -0,0 +1,69 @@ +#include +#include + +namespace extra2d { +namespace gfx { + +GLFramebuffer::GLFramebuffer() = default; + +GLFramebuffer::~GLFramebuffer() { + doDestroy(); +} + +void GLFramebuffer::doInit(const FramebufferInfo& info) { + glGenFramebuffers(1, &framebuffer_); + glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_); + + // 绑定颜色附件 + for (size_t i = 0; i < colorTextures_.size(); ++i) { + if (colorTextures_[i]) { + auto* glTexture = static_cast(colorTextures_[i]); + glFramebufferTexture2D( + GL_FRAMEBUFFER, + static_cast(GL_COLOR_ATTACHMENT0 + i), + GL_TEXTURE_2D, + glTexture->getGLHandle(), + 0 + ); + } + } + + // 绑定深度模板附件 + if (depthStencilTexture_) { + auto* glTexture = static_cast(depthStencilTexture_); + GLenum attachment = GL_DEPTH_STENCIL_ATTACHMENT; + + Format format = depthStencilTexture_->getFormat(); + if (format == Format::Depth) { + attachment = GL_DEPTH_ATTACHMENT; + } else if (format == Format::DepthStencil || format == Format::Depth24Stencil8) { + attachment = GL_DEPTH_STENCIL_ATTACHMENT; + } + + glFramebufferTexture2D( + GL_FRAMEBUFFER, + attachment, + GL_TEXTURE_2D, + glTexture->getGLHandle(), + 0 + ); + } + + // 检查帧缓冲完整性 + GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); + if (status != GL_FRAMEBUFFER_COMPLETE) { + // 帧缓冲不完整,记录错误 + } + + glBindFramebuffer(GL_FRAMEBUFFER, 0); +} + +void GLFramebuffer::doDestroy() { + if (framebuffer_ != 0) { + glDeleteFramebuffers(1, &framebuffer_); + framebuffer_ = 0; + } +} + +} // namespace gfx +} // namespace extra2d diff --git a/Extra2D/src/gfx/opengl/GLQueue.cpp b/Extra2D/src/gfx/opengl/GLQueue.cpp new file mode 100644 index 0000000..4b1eec7 --- /dev/null +++ b/Extra2D/src/gfx/opengl/GLQueue.cpp @@ -0,0 +1,45 @@ +#include +#include + +namespace extra2d { +namespace gfx { + +GLQueue::GLQueue() = default; + +GLQueue::~GLQueue() { + doDestroy(); +} + +void GLQueue::submit(CommandBuffer* const* cmdBuffers, u32 count) { + for (u32 i = 0; i < count; ++i) { + if (cmdBuffers[i]) { + // OpenGL 命令立即执行,不需要显式提交 + // 这里可以添加同步逻辑 + } + } + +#ifdef _WIN32 +#include +#else +#include +#endif + + glFlush(); +} + +void GLQueue::resetStats() { + numDrawCalls_ = 0; + numInstances_ = 0; + numTriangles_ = 0; +} + +void GLQueue::doInit(const QueueInfo& info) { + resetStats(); +} + +void GLQueue::doDestroy() { + resetStats(); +} + +} // namespace gfx +} // namespace extra2d diff --git a/Extra2D/src/gfx/opengl/GLRenderPass.cpp b/Extra2D/src/gfx/opengl/GLRenderPass.cpp new file mode 100644 index 0000000..de50d69 --- /dev/null +++ b/Extra2D/src/gfx/opengl/GLRenderPass.cpp @@ -0,0 +1,22 @@ +#include + +namespace extra2d { +namespace gfx { + +GLRenderPass::GLRenderPass() = default; + +GLRenderPass::~GLRenderPass() { + doDestroy(); +} + +void GLRenderPass::doInit(const RenderPassInfo& info) { + // OpenGL 没有显式的 RenderPass 对象 + // 附件信息已存储在基类中 +} + +void GLRenderPass::doDestroy() { + // OpenGL 没有需要销毁的资源 +} + +} // namespace gfx +} // namespace extra2d diff --git a/Extra2D/src/gfx/opengl/GLSampler.cpp b/Extra2D/src/gfx/opengl/GLSampler.cpp new file mode 100644 index 0000000..c0f5337 --- /dev/null +++ b/Extra2D/src/gfx/opengl/GLSampler.cpp @@ -0,0 +1,131 @@ +#include + +namespace extra2d { +namespace gfx { + +GLSampler::GLSampler() = default; + +GLSampler::~GLSampler() { + doDestroy(); +} + +void GLSampler::bind(u32 unit) { + glBindSampler(unit, sampler_); +} + +void GLSampler::doInit(const SamplerInfo& info) { + glGenSamplers(1, &sampler_); + + // 设置过滤模式 + GLenum minFilter = GL_LINEAR; + GLenum magFilter = GL_LINEAR; + + switch (magFilter_) { + case Filter::Point: + magFilter = GL_NEAREST; + break; + case Filter::Linear: + default: + magFilter = GL_LINEAR; + break; + } + + switch (minFilter_) { + case Filter::Point: + if (mipFilter_ == Filter::Point) { + minFilter = GL_NEAREST_MIPMAP_NEAREST; + } else if (mipFilter_ == Filter::Linear) { + minFilter = GL_NEAREST_MIPMAP_LINEAR; + } else { + minFilter = GL_NEAREST; + } + break; + case Filter::Linear: + default: + if (mipFilter_ == Filter::Point) { + minFilter = GL_LINEAR_MIPMAP_NEAREST; + } else if (mipFilter_ == Filter::Linear) { + minFilter = GL_LINEAR_MIPMAP_LINEAR; + } else { + minFilter = GL_LINEAR; + } + break; + } + + glSamplerParameteri(sampler_, GL_TEXTURE_MIN_FILTER, minFilter); + glSamplerParameteri(sampler_, GL_TEXTURE_MAG_FILTER, magFilter); + + // 设置寻址模式 + auto toGLAddress = [](Address addr) -> GLenum { + switch (addr) { + case Address::Wrap: + return GL_REPEAT; + case Address::Mirror: + return GL_MIRRORED_REPEAT; + case Address::Clamp: + return GL_CLAMP_TO_EDGE; + case Address::Border: + return GL_CLAMP_TO_BORDER; + case Address::MirrorOnce: + return GL_MIRROR_CLAMP_TO_EDGE; + default: + return GL_CLAMP_TO_EDGE; + } + }; + + glSamplerParameteri(sampler_, GL_TEXTURE_WRAP_S, toGLAddress(addressU_)); + glSamplerParameteri(sampler_, GL_TEXTURE_WRAP_T, toGLAddress(addressV_)); + glSamplerParameteri(sampler_, GL_TEXTURE_WRAP_R, toGLAddress(addressW_)); + + // 设置各向异性过滤 + glSamplerParameterf(sampler_, GL_TEXTURE_MAX_ANISOTROPY, maxAnisotropy_); + + // 设置 LOD 范围 + glSamplerParameterf(sampler_, GL_TEXTURE_MIN_LOD, minLOD_); + glSamplerParameterf(sampler_, GL_TEXTURE_MAX_LOD, maxLOD_); + glSamplerParameterf(sampler_, GL_TEXTURE_LOD_BIAS, mipLODBias_); + + // 设置比较函数(用于阴影贴图) + if (comparisonFunc_ != ComparisonFunc::Never) { + glSamplerParameteri(sampler_, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE); + + GLenum cmpFunc = GL_NEVER; + switch (comparisonFunc_) { + case ComparisonFunc::Less: + cmpFunc = GL_LESS; + break; + case ComparisonFunc::Equal: + cmpFunc = GL_EQUAL; + break; + case ComparisonFunc::LessEqual: + cmpFunc = GL_LEQUAL; + break; + case ComparisonFunc::Greater: + cmpFunc = GL_GREATER; + break; + case ComparisonFunc::NotEqual: + cmpFunc = GL_NOTEQUAL; + break; + case ComparisonFunc::GreaterEqual: + cmpFunc = GL_GEQUAL; + break; + case ComparisonFunc::Always: + cmpFunc = GL_ALWAYS; + break; + default: + cmpFunc = GL_NEVER; + break; + } + glSamplerParameteri(sampler_, GL_TEXTURE_COMPARE_FUNC, cmpFunc); + } +} + +void GLSampler::doDestroy() { + if (sampler_ != 0) { + glDeleteSamplers(1, &sampler_); + sampler_ = 0; + } +} + +} // namespace gfx +} // namespace extra2d diff --git a/Extra2D/src/gfx/opengl/GLSwapchain.cpp b/Extra2D/src/gfx/opengl/GLSwapchain.cpp new file mode 100644 index 0000000..c777607 --- /dev/null +++ b/Extra2D/src/gfx/opengl/GLSwapchain.cpp @@ -0,0 +1,68 @@ +#include + +namespace extra2d { +namespace gfx { + +GLSwapchain::GLSwapchain() = default; + +GLSwapchain::~GLSwapchain() { + doDestroy(); +} + +Texture* GLSwapchain::getColorTexture() { + // OpenGL 默认帧缓冲没有显式的颜色纹理 + // 返回 nullptr 表示使用默认帧缓冲 + return colorTexture_.get(); +} + +Texture* GLSwapchain::getDepthStencilTexture() { + return depthStencilTexture_.get(); +} + +void GLSwapchain::present() { + // OpenGL 的呈现由窗口系统处理 + // 通常调用 SwapBuffers 或类似函数 + // 这里只需要确保所有命令已执行 + glFlush(); +} + +void GLSwapchain::doInit(const SwapchainInfo& info) { + // 创建深度模板纹理(可选) + // 对于 OpenGL,默认帧缓冲已经有深度模板缓冲 + // 但如果需要离屏渲染,可以创建额外的纹理 + + TextureInfo depthInfo{}; + depthInfo.type = TextureType::Tex2D; + depthInfo.format = Format::DepthStencil; + depthInfo.width = width_; + depthInfo.height = height_; + depthInfo.usage = TextureUsage::DepthStencilAttachment; + + depthStencilTexture_ = IntrusivePtr::create(); + depthStencilTexture_->initialize(depthInfo); +} + +void GLSwapchain::doDestroy() { + colorTexture_.reset(); + depthStencilTexture_.reset(); +} + +void GLSwapchain::doResize(u32 width, u32 height) { + // 重新创建深度模板纹理 + if (depthStencilTexture_) { + depthStencilTexture_.reset(); + + TextureInfo depthInfo{}; + depthInfo.type = TextureType::Tex2D; + depthInfo.format = Format::DepthStencil; + depthInfo.width = width; + depthInfo.height = height; + depthInfo.usage = TextureUsage::DepthStencilAttachment; + + depthStencilTexture_ = IntrusivePtr::create(); + depthStencilTexture_->initialize(depthInfo); + } +} + +} // namespace gfx +} // namespace extra2d diff --git a/Extra2D/src/platform/Input.cpp b/Extra2D/src/platform/Input.cpp index 162a0f3..9279e09 100644 --- a/Extra2D/src/platform/Input.cpp +++ b/Extra2D/src/platform/Input.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include diff --git a/Extra2D/src/2d/renderer/Batcher2d.cpp b/Extra2D/src/renderer/2d/Batcher2d.cpp similarity index 98% rename from Extra2D/src/2d/renderer/Batcher2d.cpp rename to Extra2D/src/renderer/2d/Batcher2d.cpp index bf82d0f..e91a3c4 100644 --- a/Extra2D/src/2d/renderer/Batcher2d.cpp +++ b/Extra2D/src/renderer/2d/Batcher2d.cpp @@ -1,5 +1,5 @@ -#include -#include +#include +#include #include #include #include diff --git a/Extra2D/src/2d/renderer/RenderDrawInfo.cpp b/Extra2D/src/renderer/2d/RenderDrawInfo.cpp similarity index 96% rename from Extra2D/src/2d/renderer/RenderDrawInfo.cpp rename to Extra2D/src/renderer/2d/RenderDrawInfo.cpp index bc9f868..fcd9b96 100644 --- a/Extra2D/src/2d/renderer/RenderDrawInfo.cpp +++ b/Extra2D/src/renderer/2d/RenderDrawInfo.cpp @@ -1,5 +1,5 @@ -#include -#include +#include +#include #include namespace extra2d { diff --git a/Extra2D/src/2d/renderer/RenderEntity.cpp b/Extra2D/src/renderer/2d/RenderEntity.cpp similarity index 97% rename from Extra2D/src/2d/renderer/RenderEntity.cpp rename to Extra2D/src/renderer/2d/RenderEntity.cpp index 1bfb809..37e0162 100644 --- a/Extra2D/src/2d/renderer/RenderEntity.cpp +++ b/Extra2D/src/renderer/2d/RenderEntity.cpp @@ -1,5 +1,5 @@ -#include -#include +#include +#include namespace extra2d { diff --git a/Extra2D/src/2d/renderer/StencilManager.cpp b/Extra2D/src/renderer/2d/StencilManager.cpp similarity index 98% rename from Extra2D/src/2d/renderer/StencilManager.cpp rename to Extra2D/src/renderer/2d/StencilManager.cpp index 5c9c3a9..95d1b9a 100644 --- a/Extra2D/src/2d/renderer/StencilManager.cpp +++ b/Extra2D/src/renderer/2d/StencilManager.cpp @@ -1,5 +1,5 @@ -#include -#include +#include +#include namespace extra2d { diff --git a/Extra2D/src/2d/renderer/UIMeshBuffer.cpp b/Extra2D/src/renderer/2d/UIMeshBuffer.cpp similarity index 99% rename from Extra2D/src/2d/renderer/UIMeshBuffer.cpp rename to Extra2D/src/renderer/2d/UIMeshBuffer.cpp index 2f3477b..e02eab3 100644 --- a/Extra2D/src/2d/renderer/UIMeshBuffer.cpp +++ b/Extra2D/src/renderer/2d/UIMeshBuffer.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include diff --git a/Extra2D/src/2d/components/Sprite.cpp b/Extra2D/src/renderer/2d/components/Sprite.cpp similarity index 96% rename from Extra2D/src/2d/components/Sprite.cpp rename to Extra2D/src/renderer/2d/components/Sprite.cpp index aa0f33a..fc332fa 100644 --- a/Extra2D/src/2d/components/Sprite.cpp +++ b/Extra2D/src/renderer/2d/components/Sprite.cpp @@ -1,6 +1,6 @@ -#include -#include -#include +#include +#include +#include namespace extra2d { diff --git a/Extra2D/src/2d/components/SpriteFrame.cpp b/Extra2D/src/renderer/2d/components/SpriteFrame.cpp similarity index 94% rename from Extra2D/src/2d/components/SpriteFrame.cpp rename to Extra2D/src/renderer/2d/components/SpriteFrame.cpp index 6786205..ffea38a 100644 --- a/Extra2D/src/2d/components/SpriteFrame.cpp +++ b/Extra2D/src/renderer/2d/components/SpriteFrame.cpp @@ -1,6 +1,6 @@ -#include -#include -#include +#include +#include +#include namespace extra2d { diff --git a/Extra2D/src/core/scene-graph/Camera.cpp b/Extra2D/src/scene/Camera.cpp similarity index 97% rename from Extra2D/src/core/scene-graph/Camera.cpp rename to Extra2D/src/scene/Camera.cpp index 98bf35d..f92d608 100644 --- a/Extra2D/src/core/scene-graph/Camera.cpp +++ b/Extra2D/src/scene/Camera.cpp @@ -1,6 +1,6 @@ -#include -#include -#include +#include +#include +#include #include #include #include diff --git a/Extra2D/src/core/scene-graph/Component.cpp b/Extra2D/src/scene/Component.cpp similarity index 92% rename from Extra2D/src/core/scene-graph/Component.cpp rename to Extra2D/src/scene/Component.cpp index d27920b..21e49ae 100644 --- a/Extra2D/src/core/scene-graph/Component.cpp +++ b/Extra2D/src/scene/Component.cpp @@ -1,5 +1,5 @@ -#include -#include +#include +#include namespace extra2d { diff --git a/Extra2D/src/core/scene-graph/Layers.cpp b/Extra2D/src/scene/Layers.cpp similarity index 97% rename from Extra2D/src/core/scene-graph/Layers.cpp rename to Extra2D/src/scene/Layers.cpp index b99eb2d..8538c3f 100644 --- a/Extra2D/src/core/scene-graph/Layers.cpp +++ b/Extra2D/src/scene/Layers.cpp @@ -1,4 +1,4 @@ -#include +#include namespace extra2d { diff --git a/Extra2D/src/core/scene-graph/Model.cpp b/Extra2D/src/scene/Model.cpp similarity index 94% rename from Extra2D/src/core/scene-graph/Model.cpp rename to Extra2D/src/scene/Model.cpp index 824c0de..65ad925 100644 --- a/Extra2D/src/core/scene-graph/Model.cpp +++ b/Extra2D/src/scene/Model.cpp @@ -1,6 +1,6 @@ -#include -#include -#include +#include +#include +#include #include namespace extra2d { diff --git a/Extra2D/src/core/scene-graph/Node.cpp b/Extra2D/src/scene/Node.cpp similarity index 98% rename from Extra2D/src/core/scene-graph/Node.cpp rename to Extra2D/src/scene/Node.cpp index 13ac340..71608b5 100644 --- a/Extra2D/src/core/scene-graph/Node.cpp +++ b/Extra2D/src/scene/Node.cpp @@ -1,6 +1,6 @@ -#include -#include -#include +#include +#include +#include #include namespace extra2d { diff --git a/Extra2D/src/core/scene-graph/RenderScene.cpp b/Extra2D/src/scene/RenderScene.cpp similarity index 95% rename from Extra2D/src/core/scene-graph/RenderScene.cpp rename to Extra2D/src/scene/RenderScene.cpp index 8688200..16d624e 100644 --- a/Extra2D/src/core/scene-graph/RenderScene.cpp +++ b/Extra2D/src/scene/RenderScene.cpp @@ -1,7 +1,7 @@ -#include -#include -#include -#include +#include +#include +#include +#include #include namespace extra2d { diff --git a/Extra2D/src/core/scene-graph/Scene.cpp b/Extra2D/src/scene/Scene.cpp similarity index 97% rename from Extra2D/src/core/scene-graph/Scene.cpp rename to Extra2D/src/scene/Scene.cpp index a195417..63f7c26 100644 --- a/Extra2D/src/core/scene-graph/Scene.cpp +++ b/Extra2D/src/scene/Scene.cpp @@ -1,4 +1,4 @@ -#include +#include namespace extra2d { diff --git a/refactor.py b/refactor.py new file mode 100644 index 0000000..861c6ad --- /dev/null +++ b/refactor.py @@ -0,0 +1,117 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Extra2D 目录结构重构脚本 +用于批量移动文件和更新 include 路径 +""" + +import os +import shutil +import re + +BASE_DIR = r"d:\Extra2D\Extra2D" + +def move_file(src, dst): + if os.path.exists(src): + os.makedirs(os.path.dirname(dst), exist_ok=True) + shutil.move(src, dst) + print(f"移动: {src} -> {dst}") + else: + print(f"文件不存在: {src}") + +def move_remaining_files(): + moves = [ + (r"include\extra2d\2d\renderer\UIMeshBuffer.h", r"include\extra2d\renderer\2d\UIMeshBuffer.h"), + (r"src\2d\components\Sprite.cpp", r"src\renderer\2d\components\Sprite.cpp"), + (r"src\2d\components\SpriteFrame.cpp", r"src\renderer\2d\components\SpriteFrame.cpp"), + (r"src\2d\renderer\Batcher2d.cpp", r"src\renderer\2d\Batcher2d.cpp"), + (r"src\2d\renderer\RenderDrawInfo.cpp", r"src\renderer\2d\RenderDrawInfo.cpp"), + (r"src\2d\renderer\RenderEntity.cpp", r"src\renderer\2d\RenderEntity.cpp"), + (r"src\2d\renderer\StencilManager.cpp", r"src\renderer\2d\StencilManager.cpp"), + (r"src\2d\renderer\UIMeshBuffer.cpp", r"src\renderer\2d\UIMeshBuffer.cpp"), + ] + + for src_rel, dst_rel in moves: + src = os.path.join(BASE_DIR, src_rel) + dst = os.path.join(BASE_DIR, dst_rel) + move_file(src, dst) + +def update_includes(): + path_mappings = { + r'': r'', + r'': r'', + r'': r'', + r'': r'', + r'': r'', + r'': r'', + r'': r'', + r'': r'', + r'': r'', + r'': r'', + r'': r'', + r'': r'', + r'': r'', + r'': r'', + r'': r'', + r'': r'', + r'': r'', + r'': r'', + r'': r'', + r'': r'', + r'': r'', + r'': r'', + r'': r'', + r'': r'', + r'': r'', + r'': r'', + } + + extensions = ['.h', '.hpp', '.cpp', '.c', '.inl'] + + for root, dirs, files in os.walk(BASE_DIR): + for file in files: + if any(file.endswith(ext) for ext in extensions): + filepath = os.path.join(root, file) + try: + with open(filepath, 'r', encoding='utf-8') as f: + content = f.read() + + original = content + for old_pattern, new_path in path_mappings.items(): + content = re.sub(old_pattern, new_path, content) + + if content != original: + with open(filepath, 'w', encoding='utf-8') as f: + f.write(content) + print(f"更新: {filepath}") + except Exception as e: + print(f"错误处理 {filepath}: {e}") + +def delete_empty_dirs(): + empty_dirs = [ + r"include\extra2d\core", + r"include\extra2d\2d", + r"src\core", + r"src\2d", + ] + + for dir_rel in empty_dirs: + dir_path = os.path.join(BASE_DIR, dir_rel) + if os.path.exists(dir_path): + try: + shutil.rmtree(dir_path) + print(f"删除目录: {dir_path}") + except Exception as e: + print(f"无法删除 {dir_path}: {e}") + +if __name__ == "__main__": + print("=== 移动剩余文件 ===") + move_remaining_files() + + print("\n=== 更新 include 路径 ===") + update_includes() + + print("\n=== 删除空目录 ===") + delete_empty_dirs() + + print("\n完成!")