More changes

This commit is contained in:
2026-09-26 02:03:11 -07:00
parent 8a82df59ca
commit 0b09aeb5d9
6 changed files with 64 additions and 45 deletions
+22 -9
View File
@@ -6,6 +6,7 @@
#include <cstddef>
#include <glm/glm.hpp>
#include <map>
#include <set>
#include <sophia/application.hpp>
#include <sophia/util/logger.hpp>
#include <string>
@@ -87,11 +88,12 @@ class Application::Impl {
PanelRegistry& panelRegistry = this->application.panelRegistry;
SceneRegistry& sceneRegistry = this->application.sceneRegistry;
SceneViewRegistry& sceneViewRegistry = this->application.sceneViewRegistry;
const SceneViewRegistry& sceneViewRegistryConst =
this->application.sceneViewRegistry;
imguiRenderer.setDefaultLayout(panelRegistry.getDefaultLayout());
// Views already warned about having no camera, so each warns once.
std::set<SceneViewHandle> viewsWithoutCamera;
auto startTime = std::chrono::high_resolution_clock::now();
auto currentTime = startTime;
u32 frameIndex = 0;
@@ -148,8 +150,8 @@ class Application::Impl {
graph.importImage(renderContext.getCurrentImage(), Access::Present);
std::map<RenderTargetHandle, ImageRef> viewTargets;
for (SceneViewHandle handle : sceneViewRegistryConst.GetRenderQueue()) {
const SceneView& view = sceneViewRegistryConst.get(handle);
for (SceneViewHandle handle : sceneViewRegistry.GetRenderQueue()) {
const SceneView& view = sceneViewRegistry.peek(handle);
if (!sceneRegistry.isAlive(view.scene) ||
!renderTargetPool.isAlive(view.target)) {
continue;
@@ -172,12 +174,23 @@ class Application::Impl {
: vk::AttachmentLoadOp::eLoad,
{color.r, color.g, color.b, color.a});
},
[&sceneRenderer, &sceneViewRegistryConst,
handle](vk::CommandBuffer commandBuffer) {
const SceneView& view = sceneViewRegistryConst.get(handle);
if (view.camera) {
sceneRenderer.render(commandBuffer, view);
[&sceneRenderer, &sceneRegistry, &sceneViewRegistry,
&viewsWithoutCamera, handle](vk::CommandBuffer commandBuffer) {
const SceneView& view = sceneViewRegistry.peek(handle);
Entity camera =
view.camera ? view.camera
: sceneRegistry.get(view.scene).getActiveCamera();
// No camera to look through: the target is just cleared.
if (!camera) {
if (viewsWithoutCamera.insert(handle).second) {
log::warning(
"scene view has no camera and its scene has no active "
"camera; it will only be cleared (see "
"Scene::createCamera)");
}
return;
}
sceneRenderer.render(commandBuffer, view, camera);
});
}
+14 -32
View File
@@ -14,19 +14,13 @@ SceneRenderer::SceneRenderer(const SceneRegistry& sceneRegistry,
resources{resources},
spriteRenderSystem{device, colorFormat, resources} {}
SceneRenderer::~SceneRenderer() {}
void SceneRenderer::beginFrame(u32 frameIndex) {
this->spriteRenderSystem.beginFrame(frameIndex);
}
void SceneRenderer::render(vk::CommandBuffer commandBuffer,
const SceneView& sceneView) {
const Scene& scene = this->sceneRegistry.get(sceneView.scene);
const entt::registry& registry = scene.registry;
entt::entity camera = sceneView.camera;
SOPHIA_ASSERT(registry.all_of<CameraComponent>(camera),
const SceneView& sceneView, Entity camera) {
SOPHIA_ASSERT(camera.hasComponent<CameraComponent>(),
"camera does not have camera component!");
vk::Extent2D extent =
@@ -34,17 +28,12 @@ void SceneRenderer::render(vk::CommandBuffer commandBuffer,
float aspect =
static_cast<float>(extent.width) / static_cast<float>(extent.height);
// The view matrix ignores the camera's scale; zoom is the projection's job.
glm::mat4 cameraWorld{1.f};
if (const auto* transform = registry.try_get<TransformComponent>(camera)) {
cameraWorld = glm::translate(glm::mat4{1.f}, transform->position) *
glm::mat4_cast(transform->rotation);
}
glm::mat4 viewProjection =
registry.get<CameraComponent>(camera).getProjection(aspect) *
glm::inverse(cameraWorld);
camera.getComponent<CameraComponent>().getViewProjection(
camera.getComponent<TransformComponent>(), aspect);
renderSprites(commandBuffer, scene, viewProjection);
renderSprites(commandBuffer, this->sceneRegistry.get(sceneView.scene),
viewProjection);
// TODO: meshes. Group MeshComponent entities by mesh and draw each group
// (instanced), as sprites are grouped by texture.
@@ -53,8 +42,7 @@ void SceneRenderer::render(vk::CommandBuffer commandBuffer,
void SceneRenderer::renderSprites(vk::CommandBuffer commandBuffer,
const Scene& scene,
const glm::mat4& viewProjection) {
std::vector<SpriteDrawItem>& items = this->spriteDrawItems;
items.clear();
this->spriteDrawItems.clear();
auto view =
scene.registry.view<const TransformComponent, const SpriteComponent>();
@@ -63,28 +51,22 @@ void SceneRenderer::renderSprites(vk::CommandBuffer commandBuffer,
? sprite.texture
: this->resources.whiteTexture;
// Unit quad -> sprite rectangle around its pivot -> world.
glm::mat4 model =
transform.mat4() *
glm::translate(glm::mat4{1.f},
glm::vec3{-sprite.pivot * sprite.size, 0.f}) *
glm::scale(glm::mat4{1.f}, glm::vec3{sprite.size, 1.f});
items.push_back({sprite.sortOrder,
transform.position.z,
texture,
{model, sprite.color, sprite.uvRect}});
this->spriteDrawItems.push_back(
{sprite.sortOrder,
transform.position.z,
texture,
{sprite.getModel(transform), sprite.color, sprite.uvRect}});
}
// Texture is the last key so that sprites whose order doesn't matter
// relative to each other still end up in as few batches as possible.
std::ranges::sort(items, {}, [](const SpriteDrawItem& item) {
std::ranges::sort(this->spriteDrawItems, {}, [](const SpriteDrawItem& item) {
return std::tuple{item.sortOrder, item.depth, item.texture};
});
this->spriteInstances.clear();
this->spriteBatches.clear();
for (const SpriteDrawItem& item : items) {
for (const SpriteDrawItem& item : this->spriteDrawItems) {
auto index = static_cast<u32>(this->spriteInstances.size());
this->spriteInstances.push_back(item.instance);
+5 -4
View File
@@ -21,14 +21,15 @@ class SceneRenderer {
SceneRenderer(const SceneRegistry& sceneRegistry, ResourceRegistry& resources,
Device& device, vk::Format colorFormat);
~SceneRenderer();
// Call once per frame, before any render().
void beginFrame(u32 frameIndex);
// Records `sceneView` into `commandBuffer`. Rendering into the view's
// target must already have begun.
void render(vk::CommandBuffer commandBuffer, const SceneView& sceneView);
// Records `sceneView` as seen through `camera` (an entity of the view's
// scene with a CameraComponent) into `commandBuffer`. Rendering into the
// view's target must already have begun.
void render(vk::CommandBuffer commandBuffer, const SceneView& sceneView,
Entity camera);
private:
struct SpriteDrawItem {