refactor: 重构项目目录结构并更新头文件引用路径

将核心模块从 core 目录迁移到 base 目录,并调整相关头文件引用路径
更新事件系统、渲染器、场景图等模块的文件位置
优化代码组织结构,提升项目可维护性
This commit is contained in:
ChestnutYueyue 2026-02-25 05:27:36 +08:00
parent 5fddc4a209
commit 89f69a66eb
90 changed files with 2252 additions and 154 deletions

View File

@ -1,6 +1,6 @@
#pragma once
#include <extra2d/core/types.h>
#include <extra2d/base/Types.h>
#include <extra2d/platform/Window.h>
#include <string>
#include <vector>

View File

@ -1,6 +1,6 @@
#pragma once
#include <extra2d/core/Object.h>
#include <extra2d/base/Object.h>
#include <string>
namespace extra2d {

View File

@ -1,8 +1,8 @@
#pragma once
#include <extra2d/core/assets/Asset.h>
#include <extra2d/core/assets/Texture2D.h>
#include <extra2d/core/assets/ImageAsset.h>
#include <extra2d/assets/Asset.h>
#include <extra2d/assets/Texture2D.h>
#include <extra2d/assets/ImageAsset.h>
#include <unordered_map>
#include <string>
#include <functional>

View File

@ -1,6 +1,6 @@
#pragma once
#include <extra2d/core/assets/Asset.h>
#include <extra2d/assets/Asset.h>
#include <extra2d/gfx/GFXDef.h>
#include <vector>

View File

@ -1,7 +1,7 @@
#pragma once
#include <extra2d/core/assets/Asset.h>
#include <extra2d/core/math_types.h>
#include <extra2d/assets/Asset.h>
#include <extra2d/math/Math.h>
#include <extra2d/gfx/GFXDef.h>
#include <unordered_map>
#include <variant>

View File

@ -1,7 +1,7 @@
#pragma once
#include <extra2d/core/assets/Asset.h>
#include <extra2d/core/assets/ImageAsset.h>
#include <extra2d/assets/Asset.h>
#include <extra2d/assets/ImageAsset.h>
#include <extra2d/gfx/GFXTexture.h>
#include <vector>

View File

@ -1,6 +1,6 @@
#pragma once
#include <extra2d/core/types.h>
#include <extra2d/base/Types.h>
#include <vector>
namespace extra2d {

View File

@ -1,6 +1,6 @@
#pragma once
#include <extra2d/core/types.h>
#include <extra2d/base/Types.h>
#include <atomic>
namespace extra2d {

View File

@ -1,6 +1,6 @@
#pragma once
#include <extra2d/core/types.h>
#include <extra2d/base/Types.h>
#include <functional>
#include <unordered_map>
#include <vector>

View File

@ -1,6 +1,6 @@
#pragma once
#include <extra2d/core/types.h>
#include <extra2d/base/Types.h>
#include <extra2d/base/Scheduler.h>
#include <chrono>

View File

@ -1,7 +1,7 @@
#pragma once
#include <extra2d/core/types.h>
#include <extra2d/core/math_types.h>
#include <extra2d/base/Types.h>
#include <extra2d/math/Math.h>
#include <functional>
#include <unordered_map>
#include <vector>

View File

@ -1,7 +1,7 @@
#pragma once
#include <extra2d/core/types.h>
#include <extra2d/core/event/EventBus.h>
#include <extra2d/base/Types.h>
#include <extra2d/event/EventBus.h>
#include <functional>
#include <unordered_map>
#include <vector>

View File

@ -8,29 +8,29 @@
#include <extra2d/base/Scheduler.h>
// Core
#include <extra2d/core/color.h>
#include <extra2d/core/math_types.h>
#include <extra2d/core/types.h>
#include <extra2d/core/Object.h>
#include <extra2d/math/Color.h>
#include <extra2d/math/Math.h>
#include <extra2d/base/Types.h>
#include <extra2d/base/Object.h>
// Core - Event
#include <extra2d/core/event/EventBus.h>
#include <extra2d/core/event/EventTarget.h>
#include <extra2d/event/EventBus.h>
#include <extra2d/event/EventTarget.h>
// Core - Scene Graph
#include <extra2d/core/scene-graph/Node.h>
#include <extra2d/core/scene-graph/Scene.h>
#include <extra2d/core/scene-graph/Component.h>
#include <extra2d/core/scene-graph/Camera.h>
#include <extra2d/core/scene-graph/Model.h>
#include <extra2d/core/scene-graph/RenderScene.h>
#include <extra2d/scene/Node.h>
#include <extra2d/scene/Scene.h>
#include <extra2d/scene/Component.h>
#include <extra2d/scene/Camera.h>
#include <extra2d/scene/Model.h>
#include <extra2d/scene/RenderScene.h>
// Core - Assets
#include <extra2d/core/assets/Asset.h>
#include <extra2d/core/assets/ImageAsset.h>
#include <extra2d/core/assets/Texture2D.h>
#include <extra2d/core/assets/AssetManager.h>
#include <extra2d/core/assets/Material.h>
#include <extra2d/assets/Asset.h>
#include <extra2d/assets/ImageAsset.h>
#include <extra2d/assets/Texture2D.h>
#include <extra2d/assets/AssetManager.h>
#include <extra2d/assets/Material.h>
// Platform
#include <extra2d/platform/SDLHelper.h>
@ -52,6 +52,13 @@
#include <extra2d/gfx/GFXPipeline.h>
#include <extra2d/gfx/GFXInputAssembler.h>
#include <extra2d/gfx/GFXDevice.h>
#include <extra2d/gfx/GFXCommandBuffer.h>
#include <extra2d/gfx/GFXQueue.h>
#include <extra2d/gfx/GFXRenderPass.h>
#include <extra2d/gfx/GFXFramebuffer.h>
#include <extra2d/gfx/GFXSwapchain.h>
#include <extra2d/gfx/GFXDescriptorSet.h>
#include <extra2d/gfx/GFXSampler.h>
// GFX - OpenGL Backend
#include <extra2d/gfx/opengl/GLDevice.h>
@ -60,17 +67,24 @@
#include <extra2d/gfx/opengl/GLShader.h>
#include <extra2d/gfx/opengl/GLPipelineState.h>
#include <extra2d/gfx/opengl/GLInputAssembler.h>
#include <extra2d/gfx/opengl/GLCommandBuffer.h>
#include <extra2d/gfx/opengl/GLQueue.h>
#include <extra2d/gfx/opengl/GLRenderPass.h>
#include <extra2d/gfx/opengl/GLFramebuffer.h>
#include <extra2d/gfx/opengl/GLSwapchain.h>
#include <extra2d/gfx/opengl/GLDescriptorSet.h>
#include <extra2d/gfx/opengl/GLSampler.h>
// 2D Renderer
#include <extra2d/2d/renderer/RenderEntity.h>
#include <extra2d/2d/renderer/RenderDrawInfo.h>
#include <extra2d/2d/renderer/UIMeshBuffer.h>
#include <extra2d/2d/renderer/Batcher2d.h>
#include <extra2d/2d/renderer/StencilManager.h>
#include <extra2d/renderer/2d/RenderEntity.h>
#include <extra2d/renderer/2d/RenderDrawInfo.h>
#include <extra2d/renderer/2d/UIMeshBuffer.h>
#include <extra2d/renderer/2d/Batcher2d.h>
#include <extra2d/renderer/2d/StencilManager.h>
// 2D Components
#include <extra2d/2d/components/SpriteFrame.h>
#include <extra2d/2d/components/Sprite.h>
#include <extra2d/renderer/2d/components/SpriteFrame.h>
#include <extra2d/renderer/2d/components/Sprite.h>
// Utils
#include <extra2d/utils/random.h>

View File

@ -38,18 +38,6 @@ struct Rect {
float height{0.0f};
};
/**
* @brief
*/
inline ClearFlagBit operator|(ClearFlagBit a, ClearFlagBit b) {
return static_cast<ClearFlagBit>(static_cast<u32>(a) | static_cast<u32>(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;
// ==================== 绑定命令 ====================
/**

View File

@ -1,6 +1,6 @@
#pragma once
#include <extra2d/core/types.h>
#include <extra2d/base/Types.h>
#include <cstdint>
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<u32>(flags) & static_cast<u32>(flag)) != 0;
}
/**
* @brief
*/
inline ClearFlagBit operator|(ClearFlagBit a, ClearFlagBit b) {
return static_cast<ClearFlagBit>(static_cast<u32>(a) | static_cast<u32>(b));
}
inline ClearFlagBit operator&(ClearFlagBit a, ClearFlagBit b) {
return static_cast<ClearFlagBit>(static_cast<u32>(a) & static_cast<u32>(b));
}
inline bool hasFlag(ClearFlagBit flags, ClearFlagBit flag) {
return (static_cast<u32>(flags) & static_cast<u32>(flag)) != 0;
}
} // namespace gfx
} // namespace extra2d

View File

@ -0,0 +1,184 @@
#pragma once
#include <extra2d/gfx/GFXObject.h>
#include <extra2d/gfx/GFXDef.h>
#include <vector>
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<DescriptorSetLayoutBinding> 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<u32>(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<DescriptorSetLayoutBinding> 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

View File

@ -0,0 +1,111 @@
#pragma once
#include <extra2d/gfx/GFXObject.h>
#include <extra2d/gfx/GFXDef.h>
#include <vector>
namespace extra2d {
namespace gfx {
class RenderPass;
class Texture;
/**
* @brief
*/
struct FramebufferInfo {
RenderPass* renderPass{nullptr};
std::vector<Texture*> 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<u32>(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<Texture*> colorTextures_;
Texture* depthStencilTexture_{nullptr};
u32 width_{0};
u32 height_{0};
};
} // namespace gfx
} // namespace extra2d

View File

@ -0,0 +1,97 @@
#pragma once
#include <extra2d/gfx/GFXObject.h>
#include <extra2d/gfx/GFXDef.h>
#include <vector>
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<CommandBuffer*>& cmdBuffers) {
submit(cmdBuffers.data(), static_cast<u32>(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

View File

@ -0,0 +1,161 @@
#pragma once
#include <extra2d/gfx/GFXObject.h>
#include <extra2d/gfx/GFXDef.h>
#include <vector>
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<u32> inputs;
std::vector<u32> colors;
std::vector<u32> resolves;
std::vector<u32> preserves;
u32 depthStencil{0xFFFFFFFF};
u32 depthStencilResolve{0xFFFFFFFF};
};
/**
* @brief
*/
struct SubpassDependency {
u32 srcSubpass{0};
u32 dstSubpass{0};
};
/**
* @brief
*/
struct RenderPassInfo {
std::vector<ColorAttachment> colorAttachments;
DepthStencilAttachment depthStencilAttachment;
std::vector<SubpassInfo> subpasses;
std::vector<SubpassDependency> 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<u32>(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<ColorAttachment> colorAttachments_;
DepthStencilAttachment depthStencilAttachment_;
std::vector<SubpassInfo> subpasses_;
std::vector<SubpassDependency> dependencies_;
};
} // namespace gfx
} // namespace extra2d

View File

@ -0,0 +1,118 @@
#pragma once
#include <extra2d/gfx/GFXObject.h>
#include <extra2d/gfx/GFXDef.h>
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

View File

@ -0,0 +1,134 @@
#pragma once
#include <extra2d/gfx/GFXObject.h>
#include <extra2d/gfx/GFXDef.h>
#include <vector>
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

View File

@ -0,0 +1,118 @@
#pragma once
#include <extra2d/gfx/GFXDescriptorSet.h>
#include <unordered_map>
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<u32, DescriptorBinding> bindings_;
};
} // namespace gfx
} // namespace extra2d

View File

@ -0,0 +1,58 @@
#pragma once
#include <extra2d/gfx/GFXFramebuffer.h>
#ifdef _WIN32
#include <glad/glad.h>
#else
#include <GLES3/gl3.h>
#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

View File

@ -0,0 +1,80 @@
#pragma once
#include <extra2d/gfx/GFXQueue.h>
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

View File

@ -0,0 +1,45 @@
#pragma once
#include <extra2d/gfx/GFXRenderPass.h>
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

View File

@ -0,0 +1,56 @@
#pragma once
#include <extra2d/gfx/GFXSampler.h>
#ifdef _WIN32
#include <glad/glad.h>
#else
#include <GLES3/gl3.h>
#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

View File

@ -0,0 +1,81 @@
#pragma once
#include <extra2d/gfx/GFXSwapchain.h>
#include <extra2d/gfx/opengl/GLTexture.h>
#ifdef _WIN32
#include <glad/glad.h>
#else
#include <GLES3/gl3.h>
#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<GLTexture> colorTexture_;
IntrusivePtr<GLTexture> depthStencilTexture_;
};
} // namespace gfx
} // namespace extra2d

View File

@ -1,7 +1,7 @@
#pragma once
#include <algorithm>
#include <extra2d/core/types.h>
#include <extra2d/base/Types.h>
#include <glm/vec4.hpp>
namespace extra2d {

View File

@ -2,7 +2,7 @@
#include <algorithm>
#include <cmath>
#include <extra2d/core/types.h>
#include <extra2d/base/Types.h>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/mat4x4.hpp>
#include <glm/vec2.hpp>

View File

@ -1,7 +1,7 @@
#pragma once
#include <extra2d/core/types.h>
#include <extra2d/core/math_types.h>
#include <extra2d/base/Types.h>
#include <extra2d/math/Math.h>
#include <vector>
#include <array>

View File

@ -1,8 +1,8 @@
#pragma once
#include <extra2d/core/types.h>
#include <extra2d/core/math_types.h>
#include <extra2d/core/event/EventBus.h>
#include <extra2d/base/Types.h>
#include <extra2d/math/Math.h>
#include <extra2d/event/EventBus.h>
struct SDL_Window;
struct SDL_Event;

View File

@ -1,7 +1,7 @@
#pragma once
#include <extra2d/core/types.h>
#include <extra2d/core/math_types.h>
#include <extra2d/base/Types.h>
#include <extra2d/math/Math.h>
#include <string>
struct SDL_Window;

View File

@ -1,10 +1,10 @@
#pragma once
#include <extra2d/core/types.h>
#include <extra2d/core/math_types.h>
#include <extra2d/2d/renderer/RenderEntity.h>
#include <extra2d/2d/renderer/RenderDrawInfo.h>
#include <extra2d/2d/renderer/UIMeshBuffer.h>
#include <extra2d/base/Types.h>
#include <extra2d/math/Math.h>
#include <extra2d/renderer/2d/RenderEntity.h>
#include <extra2d/renderer/2d/RenderDrawInfo.h>
#include <extra2d/renderer/2d/UIMeshBuffer.h>
#include <extra2d/gfx/GFXDef.h>
#include <vector>
#include <unordered_map>

View File

@ -1,7 +1,7 @@
#pragma once
#include <extra2d/core/types.h>
#include <extra2d/core/math_types.h>
#include <extra2d/base/Types.h>
#include <extra2d/math/Math.h>
#include <extra2d/gfx/GFXDef.h>
namespace extra2d {

View File

@ -1,7 +1,7 @@
#pragma once
#include <extra2d/core/types.h>
#include <extra2d/core/math_types.h>
#include <extra2d/base/Types.h>
#include <extra2d/math/Math.h>
#include <vector>
#include <array>

View File

@ -1,6 +1,6 @@
#pragma once
#include <extra2d/core/types.h>
#include <extra2d/base/Types.h>
#include <extra2d/gfx/GFXDef.h>
#include <unordered_map>

View File

@ -1,7 +1,7 @@
#pragma once
#include <extra2d/core/types.h>
#include <extra2d/core/math_types.h>
#include <extra2d/base/Types.h>
#include <extra2d/math/Math.h>
#include <extra2d/gfx/GFXDef.h>
#include <vector>

View File

@ -1,10 +1,10 @@
#pragma once
#include <extra2d/core/scene-graph/Component.h>
#include <extra2d/core/math_types.h>
#include <extra2d/2d/components/SpriteFrame.h>
#include <extra2d/2d/renderer/RenderEntity.h>
#include <extra2d/2d/renderer/RenderDrawInfo.h>
#include <extra2d/scene/Component.h>
#include <extra2d/math/Math.h>
#include <extra2d/renderer/2d/components/SpriteFrame.h>
#include <extra2d/renderer/2d/RenderEntity.h>
#include <extra2d/renderer/2d/RenderDrawInfo.h>
namespace extra2d {

View File

@ -1,8 +1,8 @@
#pragma once
#include <extra2d/core/assets/Asset.h>
#include <extra2d/core/assets/Texture2D.h>
#include <extra2d/core/math_types.h>
#include <extra2d/assets/Asset.h>
#include <extra2d/assets/Texture2D.h>
#include <extra2d/math/Math.h>
namespace extra2d {

View File

@ -1,7 +1,7 @@
#pragma once
#include <extra2d/core/Object.h>
#include <extra2d/core/math_types.h>
#include <extra2d/base/Object.h>
#include <extra2d/math/Math.h>
#include <extra2d/gfx/GFXDef.h>
#include <glm/mat4x4.hpp>
#include <string>

View File

@ -1,7 +1,7 @@
#pragma once
#include <extra2d/core/Object.h>
#include <extra2d/core/event/EventTarget.h>
#include <extra2d/base/Object.h>
#include <extra2d/event/EventTarget.h>
namespace extra2d {

View File

@ -1,6 +1,6 @@
#pragma once
#include <extra2d/core/types.h>
#include <extra2d/base/Types.h>
#include <string>
#include <unordered_map>
#include <vector>

View File

@ -1,8 +1,8 @@
#pragma once
#include <extra2d/core/Object.h>
#include <extra2d/core/math_types.h>
#include <extra2d/core/assets/Material.h>
#include <extra2d/base/Object.h>
#include <extra2d/math/Math.h>
#include <extra2d/assets/Material.h>
#include <extra2d/gfx/GFXDef.h>
#include <vector>

View File

@ -1,8 +1,8 @@
#pragma once
#include <extra2d/core/Object.h>
#include <extra2d/core/event/EventTarget.h>
#include <extra2d/core/math_types.h>
#include <extra2d/base/Object.h>
#include <extra2d/event/EventTarget.h>
#include <extra2d/math/Math.h>
#include <functional>
#include <vector>

View File

@ -1,7 +1,7 @@
#pragma once
#include <extra2d/core/Object.h>
#include <extra2d/core/types.h>
#include <extra2d/base/Object.h>
#include <extra2d/base/Types.h>
#include <string>
#include <vector>

View File

@ -1,10 +1,10 @@
#pragma once
#include <extra2d/core/Object.h>
#include <extra2d/core/event/EventTarget.h>
#include <extra2d/core/scene-graph/RenderScene.h>
#include <extra2d/core/scene-graph/Camera.h>
#include <extra2d/core/scene-graph/Model.h>
#include <extra2d/base/Object.h>
#include <extra2d/event/EventTarget.h>
#include <extra2d/scene/RenderScene.h>
#include <extra2d/scene/Camera.h>
#include <extra2d/scene/Model.h>
#include <extra2d/gfx/GFXDef.h>
#include <vector>
#include <memory>

View File

@ -1,6 +1,6 @@
#pragma once
#include <extra2d/core/scene-graph/Node.h>
#include <extra2d/scene/Node.h>
#include <vector>
#include <string>

View File

@ -1,6 +1,6 @@
#pragma once
#include <extra2d/core/types.h>
#include <extra2d/base/Types.h>
#include <random>
namespace extra2d {

View File

@ -1,7 +1,7 @@
#pragma once
#include <chrono>
#include <extra2d/core/types.h>
#include <extra2d/base/Types.h>
#include <map>
#include <memory>
#include <vector>

View File

@ -3,7 +3,7 @@
#include <extra2d/platform/Window.h>
#include <extra2d/platform/Input.h>
#include <extra2d/platform/SDLHelper.h>
#include <extra2d/core/event/EventBus.h>
#include <extra2d/event/EventBus.h>
#include <chrono>
#include <thread>

View File

@ -1,4 +1,4 @@
#include <extra2d/core/assets/Asset.h>
#include <extra2d/assets/Asset.h>
namespace extra2d {

View File

@ -1,5 +1,5 @@
#include <extra2d/core/assets/AssetManager.h>
#include <extra2d/core/assets/Texture2D.h>
#include <extra2d/assets/AssetManager.h>
#include <extra2d/assets/Texture2D.h>
#include <extra2d/gfx/GFXDevice.h>
#include <unordered_map>
#include <string>

View File

@ -1,4 +1,4 @@
#include <extra2d/core/assets/ImageAsset.h>
#include <extra2d/assets/ImageAsset.h>
#include <cstring>
namespace extra2d {

View File

@ -1,4 +1,4 @@
#include <extra2d/core/assets/Material.h>
#include <extra2d/assets/Material.h>
#include <extra2d/gfx/GFXShader.h>
#include <extra2d/gfx/GFXPipeline.h>
#include <functional>

View File

@ -1,4 +1,4 @@
#include <extra2d/core/assets/Texture2D.h>
#include <extra2d/assets/Texture2D.h>
#include <extra2d/gfx/GFXDevice.h>
namespace extra2d {

View File

@ -1,4 +1,4 @@
#include <extra2d/core/Object.h>
#include <extra2d/base/Object.h>
namespace extra2d {

View File

@ -1,5 +1,5 @@
#include <extra2d/engine/Engine.h>
#include <extra2d/core/event/EventBus.h>
#include <extra2d/event/EventBus.h>
#include <extra2d/base/AutoreleasePool.h>
namespace extra2d {

View File

@ -1,4 +1,4 @@
#include <extra2d/core/event/EventBus.h>
#include <extra2d/event/EventBus.h>
namespace extra2d {

View File

@ -1,4 +1,4 @@
#include <extra2d/core/event/EventTarget.h>
#include <extra2d/event/EventTarget.h>
namespace extra2d {

View File

@ -0,0 +1,39 @@
#include <extra2d/gfx/GFXDescriptorSet.h>
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<DescriptorSetLayout*>(info.layout);
doInit(info);
}
void DescriptorSet::destroy() {
doDestroy();
layout_ = nullptr;
}
} // namespace gfx
} // namespace extra2d

View File

@ -0,0 +1,40 @@
#include <extra2d/gfx/GFXFramebuffer.h>
#include <extra2d/gfx/GFXRenderPass.h>
#include <extra2d/gfx/GFXTexture.h>
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

View File

@ -0,0 +1,20 @@
#include <extra2d/gfx/GFXQueue.h>
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

View File

@ -0,0 +1,33 @@
#include <extra2d/gfx/GFXRenderPass.h>
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

View File

@ -0,0 +1,31 @@
#include <extra2d/gfx/GFXSampler.h>
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

View File

@ -0,0 +1,39 @@
#include <extra2d/gfx/GFXSwapchain.h>
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

View File

@ -161,7 +161,8 @@ void GLCommandBuffer::updateBuffer(Buffer* buffer, u32 offset, const void* data,
if (!buffer || !data) return;
auto* glBuffer = static_cast<GLBuffer*>(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<GLBuffer*>(src);
auto* glDst = static_cast<GLBuffer*>(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<GLTexture*>(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
);
}

View File

@ -0,0 +1,136 @@
#include <extra2d/gfx/opengl/GLDescriptorSet.h>
#include <extra2d/gfx/opengl/GLBuffer.h>
#include <extra2d/gfx/opengl/GLTexture.h>
#ifdef _WIN32
#include <glad/glad.h>
#else
#include <GLES3/gl3.h>
#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<GLBuffer*>(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<GLTexture*>(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<GLTexture*>(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

View File

@ -0,0 +1,69 @@
#include <extra2d/gfx/opengl/GLFramebuffer.h>
#include <extra2d/gfx/opengl/GLTexture.h>
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<GLTexture*>(colorTextures_[i]);
glFramebufferTexture2D(
GL_FRAMEBUFFER,
static_cast<GLenum>(GL_COLOR_ATTACHMENT0 + i),
GL_TEXTURE_2D,
glTexture->getGLHandle(),
0
);
}
}
// 绑定深度模板附件
if (depthStencilTexture_) {
auto* glTexture = static_cast<GLTexture*>(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

View File

@ -0,0 +1,45 @@
#include <extra2d/gfx/opengl/GLQueue.h>
#include <extra2d/gfx/GFXCommandBuffer.h>
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 <glad/glad.h>
#else
#include <GLES3/gl3.h>
#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

View File

@ -0,0 +1,22 @@
#include <extra2d/gfx/opengl/GLRenderPass.h>
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

View File

@ -0,0 +1,131 @@
#include <extra2d/gfx/opengl/GLSampler.h>
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

View File

@ -0,0 +1,68 @@
#include <extra2d/gfx/opengl/GLSwapchain.h>
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<GLTexture>::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<GLTexture>::create();
depthStencilTexture_->initialize(depthInfo);
}
}
} // namespace gfx
} // namespace extra2d

View File

@ -1,5 +1,5 @@
#include <extra2d/platform/Input.h>
#include <extra2d/core/event/EventBus.h>
#include <extra2d/event/EventBus.h>
#include <SDL2/SDL.h>
#include <cmath>

View File

@ -1,5 +1,5 @@
#include <extra2d/2d/renderer/Batcher2d.h>
#include <extra2d/core/scene-graph/Node.h>
#include <extra2d/renderer/2d/Batcher2d.h>
#include <extra2d/scene/Node.h>
#include <extra2d/gfx/GFXDevice.h>
#include <extra2d/gfx/GFXShader.h>
#include <extra2d/gfx/GFXPipeline.h>

View File

@ -1,5 +1,5 @@
#include <extra2d/2d/renderer/RenderDrawInfo.h>
#include <extra2d/2d/renderer/UIMeshBuffer.h>
#include <extra2d/renderer/2d/RenderDrawInfo.h>
#include <extra2d/renderer/2d/UIMeshBuffer.h>
#include <extra2d/gfx/GFXDevice.h>
namespace extra2d {

View File

@ -1,5 +1,5 @@
#include <extra2d/2d/renderer/RenderEntity.h>
#include <extra2d/2d/renderer/RenderDrawInfo.h>
#include <extra2d/renderer/2d/RenderEntity.h>
#include <extra2d/renderer/2d/RenderDrawInfo.h>
namespace extra2d {

View File

@ -1,5 +1,5 @@
#include <extra2d/2d/renderer/StencilManager.h>
#include <extra2d/2d/renderer/RenderEntity.h>
#include <extra2d/renderer/2d/StencilManager.h>
#include <extra2d/renderer/2d/RenderEntity.h>
namespace extra2d {

View File

@ -1,4 +1,4 @@
#include <extra2d/2d/renderer/UIMeshBuffer.h>
#include <extra2d/renderer/2d/UIMeshBuffer.h>
#include <extra2d/gfx/GFXDevice.h>
#include <cstring>

View File

@ -1,6 +1,6 @@
#include <extra2d/2d/components/Sprite.h>
#include <extra2d/core/scene-graph/Node.h>
#include <extra2d/2d/renderer/Batcher2d.h>
#include <extra2d/renderer/2d/components/Sprite.h>
#include <extra2d/scene/Node.h>
#include <extra2d/renderer/2d/Batcher2d.h>
namespace extra2d {

View File

@ -1,6 +1,6 @@
#include <extra2d/2d/components/SpriteFrame.h>
#include <extra2d/2d/components/Sprite.h>
#include <extra2d/core/scene-graph/Node.h>
#include <extra2d/renderer/2d/components/SpriteFrame.h>
#include <extra2d/renderer/2d/components/Sprite.h>
#include <extra2d/scene/Node.h>
namespace extra2d {

View File

@ -1,6 +1,6 @@
#include <extra2d/core/scene-graph/Camera.h>
#include <extra2d/core/scene-graph/Node.h>
#include <extra2d/core/scene-graph/Scene.h>
#include <extra2d/scene/Camera.h>
#include <extra2d/scene/Node.h>
#include <extra2d/scene/Scene.h>
#include <extra2d/gfx/GFXDevice.h>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>

View File

@ -1,5 +1,5 @@
#include <extra2d/core/scene-graph/Component.h>
#include <extra2d/core/scene-graph/Node.h>
#include <extra2d/scene/Component.h>
#include <extra2d/scene/Node.h>
namespace extra2d {

View File

@ -1,4 +1,4 @@
#include <extra2d/core/scene-graph/Layers.h>
#include <extra2d/scene/Layers.h>
namespace extra2d {

View File

@ -1,6 +1,6 @@
#include <extra2d/core/scene-graph/Model.h>
#include <extra2d/core/scene-graph/Node.h>
#include <extra2d/core/scene-graph/RenderScene.h>
#include <extra2d/scene/Model.h>
#include <extra2d/scene/Node.h>
#include <extra2d/scene/RenderScene.h>
#include <algorithm>
namespace extra2d {

View File

@ -1,6 +1,6 @@
#include <extra2d/core/scene-graph/Node.h>
#include <extra2d/core/scene-graph/Component.h>
#include <extra2d/core/scene-graph/Scene.h>
#include <extra2d/scene/Node.h>
#include <extra2d/scene/Component.h>
#include <extra2d/scene/Scene.h>
#include <algorithm>
namespace extra2d {

View File

@ -1,7 +1,7 @@
#include <extra2d/core/scene-graph/RenderScene.h>
#include <extra2d/core/scene-graph/Camera.h>
#include <extra2d/core/scene-graph/Node.h>
#include <extra2d/2d/renderer/Batcher2d.h>
#include <extra2d/scene/RenderScene.h>
#include <extra2d/scene/Camera.h>
#include <extra2d/scene/Node.h>
#include <extra2d/renderer/2d/Batcher2d.h>
#include <algorithm>
namespace extra2d {

View File

@ -1,4 +1,4 @@
#include <extra2d/core/scene-graph/Scene.h>
#include <extra2d/scene/Scene.h>
namespace extra2d {

117
refactor.py Normal file
View File

@ -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'<extra2d/core/Object\.h>': r'<extra2d/base/Object.h>',
r'<extra2d/core/types\.h>': r'<extra2d/base/Types.h>',
r'<extra2d/core/color\.h>': r'<extra2d/math/Color.h>',
r'<extra2d/core/math_types\.h>': r'<extra2d/math/Math.h>',
r'<extra2d/core/event/EventBus\.h>': r'<extra2d/event/EventBus.h>',
r'<extra2d/core/event/EventTarget\.h>': r'<extra2d/event/EventTarget.h>',
r'<extra2d/core/scene-graph/Node\.h>': r'<extra2d/scene/Node.h>',
r'<extra2d/core/scene-graph/Scene\.h>': r'<extra2d/scene/Scene.h>',
r'<extra2d/core/scene-graph/Component\.h>': r'<extra2d/scene/Component.h>',
r'<extra2d/core/scene-graph/Camera\.h>': r'<extra2d/scene/Camera.h>',
r'<extra2d/core/scene-graph/Model\.h>': r'<extra2d/scene/Model.h>',
r'<extra2d/core/scene-graph/RenderScene\.h>': r'<extra2d/scene/RenderScene.h>',
r'<extra2d/core/scene-graph/Layers\.h>': r'<extra2d/scene/Layers.h>',
r'<extra2d/core/Root\.h>': r'<extra2d/scene/Root.h>',
r'<extra2d/core/assets/Asset\.h>': r'<extra2d/assets/Asset.h>',
r'<extra2d/core/assets/AssetManager\.h>': r'<extra2d/assets/AssetManager.h>',
r'<extra2d/core/assets/ImageAsset\.h>': r'<extra2d/assets/ImageAsset.h>',
r'<extra2d/core/assets/Texture2D\.h>': r'<extra2d/assets/Texture2D.h>',
r'<extra2d/core/assets/Material\.h>': r'<extra2d/assets/Material.h>',
r'<extra2d/2d/components/Sprite\.h>': r'<extra2d/renderer/2d/components/Sprite.h>',
r'<extra2d/2d/components/SpriteFrame\.h>': r'<extra2d/renderer/2d/components/SpriteFrame.h>',
r'<extra2d/2d/renderer/Batcher2d\.h>': r'<extra2d/renderer/2d/Batcher2d.h>',
r'<extra2d/2d/renderer/RenderEntity\.h>': r'<extra2d/renderer/2d/RenderEntity.h>',
r'<extra2d/2d/renderer/RenderDrawInfo\.h>': r'<extra2d/renderer/2d/RenderDrawInfo.h>',
r'<extra2d/2d/renderer/UIMeshBuffer\.h>': r'<extra2d/renderer/2d/UIMeshBuffer.h>',
r'<extra2d/2d/renderer/StencilManager\.h>': r'<extra2d/renderer/2d/StencilManager.h>',
}
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完成!")