2D scene rendering + image loading #15
@@ -13,18 +13,17 @@ class Scene {
|
||||
Scene();
|
||||
~Scene();
|
||||
|
||||
// Every entity gets a TagComponent (named `name`) and a TransformComponent.
|
||||
Entity createEntity(const std::string& name = {});
|
||||
void destroyEntity(Entity entity);
|
||||
|
||||
Entity createCamera(const std::string& name = "Camera");
|
||||
|
||||
/** Calls `fn(Entity)` for every live entity. Defined in entity.hpp. */
|
||||
template <typename Fn>
|
||||
void forEachEntity(Fn&& fn);
|
||||
|
||||
// The camera a render request uses when it doesn't name one. Null until
|
||||
// set.
|
||||
entt::entity getActiveCamera() const { return activeCamera; }
|
||||
void setActiveCamera(entt::entity camera) { activeCamera = camera; }
|
||||
Entity getActiveCamera();
|
||||
void setActiveCamera(Entity camera);
|
||||
|
||||
private:
|
||||
friend class Entity;
|
||||
|
||||
@@ -17,8 +17,9 @@ enum class ClearFlags : u8 {
|
||||
|
||||
struct SceneView {
|
||||
SceneHandle scene = INVALID_HANDLE;
|
||||
RenderTargetHandle target = INVALID_HANDLE;
|
||||
Entity camera;
|
||||
RenderTargetHandle target =
|
||||
INVALID_HANDLE; // null implies render to swapchain
|
||||
Entity camera; // null implies use scene's active camera
|
||||
|
||||
vec4 viewport{0.0f, 0.0f, 1.0f, 1.0f};
|
||||
|
||||
|
||||
@@ -91,12 +91,13 @@ void ImGuiRenderer::beginFrame() {
|
||||
}
|
||||
|
||||
ImTextureID ImGuiRenderer::getTextureId(RenderTargetHandle handle) {
|
||||
vk::ImageView view =
|
||||
resources.renderTargetPool.get(handle).getImage().getView();
|
||||
const RenderTarget& target = resources.renderTargetPool.get(handle);
|
||||
vk::ImageView view = target.getImage().getView();
|
||||
|
||||
auto it = textures.find(handle.index);
|
||||
if (it != textures.end()) {
|
||||
if (it->second.generation == handle.generation && it->second.view == view) {
|
||||
if (it->second.generation == handle.generation &&
|
||||
it->second.version == target.getVersion()) {
|
||||
return (ImTextureID)it->second.descriptorSet;
|
||||
}
|
||||
ImGui_ImplVulkan_RemoveTexture(it->second.descriptorSet);
|
||||
@@ -107,7 +108,8 @@ ImTextureID ImGuiRenderer::getTextureId(RenderTargetHandle handle) {
|
||||
// left in once the scene pass has finished.
|
||||
VkDescriptorSet descriptorSet = ImGui_ImplVulkan_AddTexture(
|
||||
static_cast<VkImageView>(view), VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
|
||||
textures[handle.index] = {handle.generation, view, descriptorSet};
|
||||
textures[handle.index] = {handle.generation, target.getVersion(),
|
||||
descriptorSet};
|
||||
return (ImTextureID)descriptorSet;
|
||||
}
|
||||
|
||||
@@ -120,7 +122,7 @@ void ImGuiRenderer::releaseStaleTextures() {
|
||||
|
||||
bool stale =
|
||||
!renderTargetPool.isAlive(handle) ||
|
||||
renderTargetPool.get(handle).getImage().getView() != it->second.view;
|
||||
renderTargetPool.get(handle).getVersion() != it->second.version;
|
||||
if (stale) {
|
||||
ImGui_ImplVulkan_RemoveTexture(it->second.descriptorSet);
|
||||
it = textures.erase(it);
|
||||
|
||||
@@ -37,7 +37,7 @@ class ImGuiRenderer {
|
||||
|
||||
struct Texture {
|
||||
u32 generation;
|
||||
vk::ImageView view;
|
||||
u32 version;
|
||||
VkDescriptorSet descriptorSet;
|
||||
};
|
||||
|
||||
|
||||
@@ -39,6 +39,7 @@ void RenderTarget::applyPendingResize() {
|
||||
}
|
||||
|
||||
void RenderTarget::create(u32 width, u32 height) {
|
||||
this->version++;
|
||||
this->image = Image::Builder(this->device)
|
||||
.setExtent(width, height)
|
||||
.setFormat(this->format)
|
||||
|
||||
@@ -27,6 +27,7 @@ class RenderTarget {
|
||||
Image& getImage() const { return *this->image; }
|
||||
|
||||
vk::Extent2D getExtent() const;
|
||||
u32 getVersion() const { return this->version; }
|
||||
|
||||
void requestResize(u32 width, u32 height);
|
||||
|
||||
@@ -46,6 +47,7 @@ class RenderTarget {
|
||||
std::unique_ptr<Image> image;
|
||||
|
||||
std::optional<vk::Extent2D> pendingExtent;
|
||||
u32 version = 0;
|
||||
};
|
||||
|
||||
} // namespace sophia
|
||||
|
||||
@@ -16,6 +16,24 @@ Entity Scene::createEntity(const std::string& name) {
|
||||
return entity;
|
||||
}
|
||||
|
||||
Entity Scene::createCamera(const std::string& name) {
|
||||
Entity camera = createEntity(name);
|
||||
camera.addComponent<CameraComponent>();
|
||||
if (this->activeCamera == entt::null) {
|
||||
this->activeCamera = camera;
|
||||
}
|
||||
return camera;
|
||||
}
|
||||
|
||||
Entity Scene::getActiveCamera() {
|
||||
if (this->activeCamera == entt::null) {
|
||||
return {};
|
||||
}
|
||||
return {this->activeCamera, this};
|
||||
}
|
||||
|
||||
void Scene::setActiveCamera(Entity camera) { this->activeCamera = camera; }
|
||||
|
||||
void Scene::destroyEntity(Entity entity) {
|
||||
if (this->activeCamera == entity) {
|
||||
this->activeCamera = entt::null;
|
||||
|
||||
@@ -87,6 +87,8 @@ class Boids2D : public sophia::Application {
|
||||
[this](sophia::KeyReleasedEvent& e) {
|
||||
setScrollKeyHeld(e.getKeyCode(), false);
|
||||
});
|
||||
|
||||
debugUI().show();
|
||||
}
|
||||
|
||||
void onCreate() override {
|
||||
@@ -110,8 +112,7 @@ class Boids2D : public sophia::Application {
|
||||
sceneHandle = scenes().create();
|
||||
sophia::Scene& scene = scenes().get(sceneHandle);
|
||||
|
||||
cameraEntity = scene.createEntity("Camera");
|
||||
cameraEntity.addComponent<sophia::CameraComponent>();
|
||||
cameraEntity = scene.createCamera();
|
||||
|
||||
std::uniform_real_distribution<float> tint{0.0f, 1.0f};
|
||||
for (Boid& boid : boids) {
|
||||
@@ -127,7 +128,6 @@ class Boids2D : public sophia::Application {
|
||||
target = resources().create<sophia::RenderTarget>();
|
||||
views().create({.scene = sceneHandle,
|
||||
.target = target,
|
||||
.camera = cameraEntity,
|
||||
.clearColor = {0.01f, 0.01f, 0.02f, 1.0f}});
|
||||
|
||||
panels().emplace<ViewportPanel>(ui(), target);
|
||||
|
||||
@@ -40,8 +40,7 @@ class ImageViewer : public sophia::Application {
|
||||
sophia::SceneHandle sceneHandle = scenes().create();
|
||||
sophia::Scene& scene = scenes().get(sceneHandle);
|
||||
|
||||
camera = scene.createEntity("Camera");
|
||||
camera.addComponent<sophia::CameraComponent>();
|
||||
camera = scene.createCamera();
|
||||
|
||||
sophia::TextureHandle texture = resources().load<sophia::Texture>(path);
|
||||
imageSize = resources().getExtent(texture);
|
||||
@@ -54,7 +53,6 @@ class ImageViewer : public sophia::Application {
|
||||
target = resources().create<sophia::RenderTarget>();
|
||||
views().create({.scene = sceneHandle,
|
||||
.target = target,
|
||||
.camera = camera,
|
||||
.clearColor = {0.1f, 0.1f, 0.15f, 1.f}});
|
||||
|
||||
panels().emplace<ViewportPanel>(ui(), target);
|
||||
|
||||
Reference in New Issue
Block a user