Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/damaged_helmet/color_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,4 @@ void ColorPass::draw(RenderContext &ctx, const glm::ivec2 &g_size,
// End rendering to FBO
ctx.endRendering();
}
}
}
2 changes: 1 addition & 1 deletion example/damaged_helmet/color_pass.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,4 @@ class ColorPass {
Buffer m_material_ubo;
};

}
}
2 changes: 1 addition & 1 deletion example/damaged_helmet/final_pass.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ class FinalPass {
std::unique_ptr<GraphicsPipeline> m_pipeline;
};

} // namespace paimon
} // namespace paimon
7 changes: 3 additions & 4 deletions example/damaged_helmet/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,8 @@ int main() {
// Create render context
RenderContext ctx;

// Create screen quad (it will load shaders internally from singleton)
ColorPass color_pass(ctx);
FinalPass final_pass(ctx);
// Create Renderer (it will own per-pass resources)
Renderer renderer(ctx, texturePtrMap);


LOG_INFO("Setup complete, entering render loop");
Expand Down Expand Up @@ -156,4 +155,4 @@ int main() {
LOG_INFO("Shutting down");

return 0;
}
}
41 changes: 41 additions & 0 deletions example/damaged_helmet/renderer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "renderer.h"

using namespace paimon;

Renderer::Renderer(RenderContext &renderContext,
const std::map<std::shared_ptr<sg::Texture>, Texture *> &texturePtrMap)
: m_renderContext(renderContext),
m_colorPass(renderContext),
m_finalPass(renderContext),
m_transformUbo(),
m_materialUbo(),
m_lightingUbo(),
m_texturePtrMap(texturePtrMap) {
m_transformUbo.set_storage(sizeof(TransformUBO), nullptr, GL_DYNAMIC_STORAGE_BIT);
m_materialUbo.set_storage(sizeof(MaterialUBO), nullptr, GL_DYNAMIC_STORAGE_BIT);
m_lightingUbo.set_storage(sizeof(LightingUBO), nullptr, GL_DYNAMIC_STORAGE_BIT);
}

void Renderer::draw(const glm::ivec2 &resolution, const Camera &camera,
const LightingUBO &lighting,
const std::vector<MeshData> &meshes) {
// Prepare transform UBO
TransformUBO transformData;
transformData.model = glm::mat4(1.0f);
transformData.view = glm::lookAt(camera.position, glm::vec3(0.0f, 0.0f, 0.0f), camera.up);
transformData.projection = glm::perspective(glm::radians(camera.fov),
static_cast<float>(resolution.x) / resolution.y,
0.1f, 100.0f);
m_transformUbo.set_sub_data(0, sizeof(TransformUBO), &transformData);

// Update lighting UBO (copy viewPos from camera)
LightingUBO lightingData = lighting;
lightingData.viewPos = camera.position;
m_lightingUbo.set_sub_data(0, sizeof(LightingUBO), &lightingData);

// First pass: render scene to color texture
m_colorPass.draw(m_renderContext, resolution, meshes, m_texturePtrMap, m_transformUbo, m_materialUbo, m_lightingUbo);

// Second pass: blit color texture to default framebuffer
m_finalPass.draw(m_renderContext, *m_colorPass.getColorTexture(), resolution);
}
51 changes: 44 additions & 7 deletions example/damaged_helmet/renderer.h
Original file line number Diff line number Diff line change
@@ -1,21 +1,58 @@
#pragma once

#include <vector>
#include <map>
#include <memory>

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>

#include "paimon/rendering/render_context.h"
#include "paimon/core/fg/transient_resources.h"
#include "paimon/opengl/buffer.h"
#include "paimon/opengl/texture.h"

#include "color_pass.h"
#include "final_pass.h"
#include "mesh_data.h"

namespace paimon {

// Camera struct used by Renderer (matches previous main.cpp fields)
struct Camera {
glm::vec3 position = glm::vec3(0.0f, 0.0f, 3.0f);
glm::vec3 front = glm::vec3(0.0f, 0.0f, -1.0f);
glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f);
float yaw = -90.0f;
float pitch = 0.0f;
float fov = 45.0f;
};

class Renderer {
public:
Renderer(RenderContext &renderContext);
Renderer(RenderContext &renderContext,
const std::map<std::shared_ptr<sg::Texture>, Texture *> &texturePtrMap);

// resolution: framebuffer size in pixels
// camera: camera state (position, fov...)
// lighting: LightingUBO structure (lightPos, viewPos)
// meshes: list of MeshData to render
void draw(const glm::ivec2 &resolution, const Camera &camera,
const LightingUBO &lighting,
const std::vector<MeshData> &meshes);

void render();
// expose color texture for tests or external use if needed
const Texture *getColorTexture() const { return m_colorPass.getColorTexture(); }

private:
RenderContext &m_renderContext;
TransientResources m_transientResources;
ColorPass m_colorPass;
FinalPass m_finalPass;


float m_rotation = 0.0f;
Buffer m_transformUbo;
Buffer m_materialUbo;
Buffer m_lightingUbo;

const std::map<std::shared_ptr<sg::Texture>, Texture *> &m_texturePtrMap;
};
}

} // namespace paimon
12 changes: 8 additions & 4 deletions example/frame_graph/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <glm/gtc/type_ptr.hpp>

#include "paimon/app/window.h"
#include "paimon/core/log_system.h"
#include "paimon/core/fg/frame_graph.h"
#include "paimon/core/fg/frame_graph_texture.h"
#include "paimon/core/fg/transient_resources.h"
Expand Down Expand Up @@ -388,6 +389,8 @@ void renderScene(RenderData &rd, Program &program, bool isShadowPass) {
}

int main() {
LogSystem::init();

auto window = Window::create(WindowConfig{
.title = "Frame Graph Shadow Mapping Example",
.format =
Expand All @@ -404,6 +407,9 @@ int main() {
.vsync = true,
});

RenderContext rc;
TransientResources allocator(rc);

RenderData renderData;

// Setup geometry and shaders
Expand Down Expand Up @@ -463,7 +469,7 @@ int main() {
});
data.shadow_map = builder.write(data.shadow_map);
},
[&renderData](FrameGraphResources &resources, void *context) {
[&renderData](const ShadowPassData &data, FrameGraphResources &resources, void *context) {
std::cout << "Executing Shadow Pass\n";

// Bind shadow framebuffer
Expand Down Expand Up @@ -493,7 +499,7 @@ int main() {
[&](FrameGraph::Builder &builder, ScenePassData &data) {
data.shadow_input = builder.read(shadow_pass.shadow_map);
},
[&renderData](FrameGraphResources &resources, void *context) {
[&renderData](const ScenePassData &data, FrameGraphResources &resources, void *context) {
std::cout << "Executing Scene Pass\n";

// Render to default framebuffer
Expand Down Expand Up @@ -548,8 +554,6 @@ int main() {
while (!window->shouldClose()) {
window->pollEvents();

RenderContext rc;
TransientResources allocator(rc);
fg.execute(&rc, &allocator);

window->swapBuffers();
Expand Down
2 changes: 1 addition & 1 deletion source/paimon/core/ecs/entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,4 @@ class Entity {
};

} // namespace ecs
} // namespace paimon
} // namespace paimon
2 changes: 1 addition & 1 deletion source/paimon/core/fg/frame_graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class FrameGraph {

template <class TData, class TSetup, class TExecutor>
requires std::invocable<TSetup, Builder &, TData &> &&
std::invocable<TExecutor, FrameGraphResources &, void *>
std::invocable<TExecutor, const TData&, FrameGraphResources &, void *>
const TData &create_pass(std::string name, TSetup &&setup,
TExecutor &&executor) {
auto id = m_pass_nodes.size();
Expand Down
1 change: 0 additions & 1 deletion source/paimon/core/fg/frame_graph_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ void FrameGraphTexture::create(void *allocator, const Descriptor &desc) {
void FrameGraphTexture::destroy(void *allocator, const Descriptor &desc) {
auto *transientResources = static_cast<TransientResources *>(allocator);
transientResources->releaseTexture(desc, m_texture);
m_texture = nullptr;
}

void FrameGraphTexture::preRead(void *context, const Descriptor &desc,
Expand Down
3 changes: 3 additions & 0 deletions source/paimon/core/fg/frame_graph_texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class FrameGraphTexture {

void preWrite(void *context, const Descriptor &desc, uint32_t flags = 0);

Texture *getTexture() { return m_texture; }
const Texture *getTexture() const { return m_texture; }

private:
Texture *m_texture = nullptr;
};
Expand Down
2 changes: 1 addition & 1 deletion source/paimon/core/fg/pass.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Pass : public PassConcept {
~Pass() override = default;

void execute(FrameGraphResources &resources, void *context) const override {
std::invoke(m_executor, resources, context);
std::invoke(m_executor, m_data, resources, context);
}

TData &get_data() { return m_data; }
Expand Down
6 changes: 4 additions & 2 deletions source/paimon/core/fg/resource_entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ class ResourceEntry {

template <class TResource>
TResource &get() {
return dynamic_cast<Resource<TResource>>(*m_concept).get();
auto &resource = static_cast<Resource<TResource>&>(*m_concept);
return resource.get();
}

template <class TResource>
typename TResource::Descriptor &get_desc() {
return dynamic_cast<Resource<TResource>>(*m_concept).get_desc();
auto &resource = static_cast<Resource<TResource>&>(*m_concept);
return resource.get_desc();
}

private:
Expand Down
4 changes: 2 additions & 2 deletions source/paimon/rendering/render_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void RenderContext::beginRendering(const RenderingInfo& info) {
if (info.depthAttachment.has_value()) {
const auto& attachment = info.depthAttachment.value();
if (attachment.loadOp == AttachmentLoadOp::Clear) {
float depth = attachment.clearValue.depthStencil.depth;
float depth = attachment.clearValue.depth;
m_currentFbo->clear(GL_DEPTH, 0, &depth);
}
}
Expand All @@ -53,7 +53,7 @@ void RenderContext::beginRendering(const RenderingInfo& info) {
if (info.stencilAttachment.has_value()) {
const auto& attachment = info.stencilAttachment.value();
if (attachment.loadOp == AttachmentLoadOp::Clear) {
GLint stencilValue = static_cast<GLint>(attachment.clearValue.depthStencil.stencil);
GLint stencilValue = static_cast<GLint>(attachment.clearValue.stencil);
m_currentFbo->clear(GL_STENCIL, 0, &stencilValue);
}
}
Expand Down
14 changes: 14 additions & 0 deletions source/paimon/rendering/rendering_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ struct ClearValue {
struct {
float r, g, b, a;
} color;
float depth;
uint32_t stencil;
struct {
float depth;
uint32_t stencil;
Expand All @@ -46,6 +48,18 @@ struct ClearValue {
return value;
}

static ClearValue Depth(float depth) {
ClearValue value;
value.depth = depth;
return value;
}

static ClearValue Stencil(uint32_t stencil) {
ClearValue value;
value.stencil = stencil;
return value;
}

static ClearValue DepthStencil(float depth, uint32_t stencil = 0) {
ClearValue value;
value.depthStencil.depth = depth;
Expand Down
Loading