Files
Seele/src/Engine/Graphics/RenderPass/UIPass.h
T
2026-11-09 20:07:40 +01:00

84 lines
3.6 KiB
C++

#pragma once
#include "Graphics/Shader.h"
#include "RenderPass.h"
#include "UI/System.h"
namespace Seele {
DECLARE_NAME_REF(Gfx, Texture2D)
DECLARE_NAME_REF(Gfx, RenderTargetAttachment)
class UIPass : public RenderPass {
public:
UIPass(Gfx::PGraphics graphics, UI::PSystem system);
UIPass(UIPass&&) = default;
UIPass& operator=(UIPass&&) = default;
virtual ~UIPass();
virtual void beginFrame(const Component::Camera& cam, const Component::Transform& transform) override;
virtual void render() override;
virtual void endFrame() override;
virtual void publishOutputs() override;
virtual void createRenderPass() override;
// input: the owning view forwards its window callbacks here (window-relative coords)
void mouseMove(Vector2 position) { system->mouseMove(position); }
void mouseButton(MouseButton button, InputAction action, KeyModifierFlags modifier, Vector2 position) {
system->mouseButton(button, action, (KeyModifier)modifier, position);
}
void scroll(Vector2 delta) { system->scroll(delta); }
void key(KeyCode code, InputAction action, KeyModifierFlags modifier, char text = 0) { system->key(code, action, (KeyModifier)modifier, text); }
private:
struct RenderElementStyle {
float x;
float y;
float w;
float h;
// Layout is locked 1:1 to the Slang std430 struct (UIPass.slang::RenderElementStyle).
// Offsets (both sides): x/y/w/h @0/4/8/12, color @16 (12B, 16B-aligned in std430),
// opacity @28, z @32, textureIndex @36, uvOffset @40, uvSize @48, pad @56, sizeof=64.
//
// The trailing `pad` (Vector2) is LOAD-BEARING: the Slang std430 struct is 16B-aligned
// (because of the float3), so its stride is sizeof=64. On platforms where the C++ side
// aligns to 4 (alignof(glm::vec3)==4), the natural sizeof would be 56 and the CPU would
// pack elements at 56B — desyncing element 1's data against the GPU's 64B stride and
// corrupting every field from uvSize onward. `pad` forces sizeof==64 on ALL platforms.
//
// Do NOT reorder, remove, or shrink `pad`, and do NOT insert fields between existing ones
// without updating BOTH this struct AND the Slang struct AND re-verifying the offsets
// (slangc + spirv-dis, and a static_assert on the C++ side if available).
Vector color; // glm::vec3 (12B; std430 aligns it to 16B, C++ aligns per ABI)
float opacity = 1;
float z = 0;
uint32 textureIndex = std::numeric_limits<uint32>::max();
// UV rect for textured quads (glyphs: their rect in the font atlas); unused for untextured quads
Vector2 uvOffset = Vector2(0);
Vector2 uvSize = Vector2(1);
Vector2 pad; // do not remove — see layout comment above
};
Gfx::RenderTargetAttachment colorAttachment;
Gfx::RenderTargetAttachment depthAttachment;
Gfx::OTexture2D depthBuffer;
Gfx::ODescriptorLayout uiDescriptorLayout;
Gfx::ODescriptorSet uiDescriptorSet;
Gfx::ODescriptorSet viewParamsSet;
Gfx::OVertexShader uiVertexShader;
Gfx::OFragmentShader uiFragmentShader;
Gfx::OPipelineLayout uiPipelineLayout;
Gfx::PGraphicsPipeline uiPipeline;
UI::PSystem system;
Array<UI::UIRender> renderElements;
Array<RenderElementStyle> elements;
Gfx::OShaderBuffer elementBuffer;
constexpr static const char* ELEMENT_NAME = "elements";
Gfx::OSampler uiSampler;
constexpr static const char* GLYPHSAMPLER_NAME = "glyphSampler";
Array<Gfx::PTexture2D> usedTextures;
constexpr static const char* TEXTURES_NAME = "textures";
};
DEFINE_REF(UIPass);
} // namespace Seele