Skip to content
This repository was archived by the owner on Dec 4, 2025. It is now read-only.
Merged
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
1 change: 1 addition & 0 deletions include/dz/AssetPack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
#pragma once
#include "FileHandle.hpp"
#include "size_ptr.hpp"

namespace dz
{
Expand Down
3 changes: 2 additions & 1 deletion include/dz/DrawList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace dz
/**
* @brief A CameraTuple is the information required to draw from a cameras perspective
*/
using CameraTuple = std::tuple<int, Framebuffer*, std::function<void()>>;
using CameraTuple = std::tuple<int, Framebuffer*, std::function<void()>, bool>;

/**
* @brief Information for a single Camera draw
Expand All @@ -65,6 +65,7 @@ namespace dz
Framebuffer* framebuffer;
std::function<void()> pre_render_fn;
ShaderDrawList shaderDrawList;
bool inactive = false;
};

/**
Expand Down
23 changes: 16 additions & 7 deletions include/dz/DrawListManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ namespace dz
DrawInformation drawInformation;
bool draw_list_dirty = true;
public:
bool enable_global_camera_if_cameras_empty;

/**
* @brief Constructs a DrawListManager with a draw key and logic function.
*
Expand All @@ -57,12 +59,14 @@ namespace dz
DrawListManager(
const std::string& draw_key, const Determine_DrawT_DrawTuples_Function& fn_determine_DrawT_DrawTuples,
const std::string& camera_key = "", const Determine_CameraTuple_Function& fn_determine_CameraTuple = {},
const Determine_VisibleDraws_Function& fn_get_visible_draws = {}
const Determine_VisibleDraws_Function& fn_get_visible_draws = {},
bool enable_global_camera_if_cameras_empty = true
):
fn_determine_DrawT_DrawTuples(fn_determine_DrawT_DrawTuples),
fn_determine_CameraTuple(fn_determine_CameraTuple),
draw_key(draw_key),
camera_key(camera_key)
camera_key(camera_key),
enable_global_camera_if_cameras_empty(enable_global_camera_if_cameras_empty)
{
if (fn_get_visible_draws) {
this->fn_get_visible_draws = fn_get_visible_draws;
Expand Down Expand Up @@ -99,19 +103,22 @@ namespace dz
{
const size_t camera_elements = buffer_group_get_buffer_element_count(buffer_group, camera_key);

drawInformation.cameraDrawInfos.resize(camera_elements);
auto cameraDrawInfos_data = drawInformation.cameraDrawInfos.data();

for (size_t i = 0; i < camera_elements; ++i)
{
auto [camera_index, framebuffer, camera_pre_render_fn] = fn_determine_CameraTuple(buffer_group, i);
CameraDrawInformation cameraDrawInfo{
auto [camera_index, framebuffer, camera_pre_render_fn, inactive] = fn_determine_CameraTuple(buffer_group, i);
cameraDrawInfos_data[i] = CameraDrawInformation{
.camera_index = camera_index,
.framebuffer = framebuffer,
.pre_render_fn = camera_pre_render_fn
.pre_render_fn = camera_pre_render_fn,
.inactive = inactive
};
drawInformation.cameraDrawInfos.push_back(cameraDrawInfo);
}
}

if (drawInformation.cameraDrawInfos.empty())
if (enable_global_camera_if_cameras_empty && drawInformation.cameraDrawInfos.empty())
{
CameraDrawInformation cameraDrawInfo{
.camera_index = -1,
Expand All @@ -134,6 +141,8 @@ namespace dz
};

for (auto& cameraDrawInfo : drawInformation.cameraDrawInfos) {
if (cameraDrawInfo.inactive)
continue;
auto camera_index = cameraDrawInfo.camera_index;
auto visible_entity_indices = fn_get_visible_draws(buffer_group, camera_index);
auto visible_entity_indices_data = visible_entity_indices.data();
Expand Down
15 changes: 12 additions & 3 deletions include/dz/ECS.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,10 @@ namespace dz {
auto GenerateCamerasDrawFunction() {
return [&](auto buffer_group, auto camera_index) -> CameraTuple {
auto& camera_group = GetGroupByIndex<CameraProviderT, typename CameraProviderT::ReflectableGroup>(camera_index);
auto& camera = GetCamera(camera_group.id);
return {camera_index, camera_group.framebuffer, [&, camera_index]() {
shader_update_push_constant(main_shader, 0, (void*)&camera_index, sizeof(uint32_t));
}};
}, !camera.is_active};
};
}

Expand Down Expand Up @@ -216,7 +217,8 @@ namespace dz {
draw_mg(
buffer_name, GenerateEntitysDrawFunction(),
Cameras_Str, GenerateCamerasDrawFunction(),
GenerateCameraVisibilityFunction()
GenerateCameraVisibilityFunction(),
false
)
{
RegisterProviders();
Expand All @@ -236,7 +238,8 @@ namespace dz {
draw_mg(
buffer_name, GenerateEntitysDrawFunction(),
Cameras_Str, GenerateCamerasDrawFunction(),
GenerateCameraVisibilityFunction()
GenerateCameraVisibilityFunction(),
false
)
{
RegisterProviders();
Expand Down Expand Up @@ -376,6 +379,9 @@ namespace dz {
auto cam_ptr = dynamic_cast<typename CameraProviderT::ReflectableGroup*>(ptr);
assert(cam_ptr);
cam_ptr->InitFramebuffer(main_shader, width, height);
cam_ptr->update_draw_list_fn = [&]() {
draw_mg.MarkDirty();
};
}
}
}
Expand Down Expand Up @@ -838,6 +844,9 @@ namespace dz {

auto& camera = GetCamera(camera_id);
auto& camera_group = GetGroupByID<TCamera, typename TCamera::ReflectableGroup>(camera_id);
camera_group.update_draw_list_fn = [&]() {
draw_mg.MarkDirty();
};

auto& width = *window_get_width_ref(window_ptr);
auto& height = *window_get_height_ref(window_ptr);
Expand Down
40 changes: 29 additions & 11 deletions include/dz/ECS/Camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ namespace dz::ecs {
float orthoHeight;
int parent_index = -1;
int parent_cid = 0;
int padding1 = 0;
int padding2 = 0;
int transform_dirty = 1;
int is_active = 1;
inline static constexpr bool IsCameraProvider = true;
inline static constexpr size_t PID = 5;
inline static float Priority = 0.5f;
Expand All @@ -49,8 +49,8 @@ struct Camera {
float orthoHeight;
int parent_index;
int parent_cid;
int padding1;
int padding2;
int transform_dirty;
int is_active;
};
)";

Expand All @@ -72,6 +72,13 @@ struct Camera {
void GetCameraModel(int camera_index, out mat4 out_model, out int parent_index, out int parent_cid) {
Camera camera = GetCameraData(camera_index);

if (camera.transform_dirty == 0) {
out_model = inverse(camera.view);
parent_index = camera.parent_index;
parent_cid = camera.parent_cid;
return;
}

vec3 eye = camera.position;
vec3 center = camera.center;
vec3 up = camera.up;
Expand Down Expand Up @@ -112,33 +119,39 @@ void GetCameraModel(int camera_index, out mat4 out_model, out int parent_index,

parent_index = camera.parent_index;
parent_cid = camera.parent_cid;

camera.transform_dirty = 0;
}
)"}
};


struct CameraTypeReflectable : Reflectable {
struct CameraMetaReflectable : Reflectable {

private:
std::function<Camera*()> get_camera_function;
std::function<void()> reset_reflectables_function;
int uid;
std::string name;
inline static std::unordered_map<std::string, std::pair<int, int>> prop_name_indexes = {
{"type", {0, 0}}
{"type", {0, 0}},
{"is_active", {1, 0}}
};
inline static std::unordered_map<int, std::string> prop_index_names = {
{0, "type"}
{0, "type"},
{1, "is_active"}
};
inline static std::vector<std::string> prop_names = {
"type"
"type",
"is_active"
};
inline static const std::vector<const std::type_info*> typeinfos = {
&typeid(Camera::ProjectionType)
&typeid(Camera::ProjectionType),
&typeid(bool)
};

public:
CameraTypeReflectable(
CameraMetaReflectable(
const std::function<Camera*()>& get_camera_function,
const std::function<void()>& reset_reflectables_function
);
Expand Down Expand Up @@ -281,6 +294,8 @@ void GetCameraModel(int camera_index, out mat4 out_model, out int parent_index,
VkDescriptorSet frame_image_ds = VK_NULL_HANDLE;
bool open_in_editor = true;

std::function<void()> update_draw_list_fn;

CameraReflectableGroup(BufferGroup* buffer_group):
buffer_group(buffer_group),
name("Camera")
Expand Down Expand Up @@ -316,14 +331,16 @@ void GetCameraModel(int camera_index, out mat4 out_model, out int parent_index,
delete reflectable;
reflectables.clear();
}

void UpdateChildren() override {
if (reflectables.size() == 0) {
reflectables.push_back(new CameraTypeReflectable([&]() mutable {
reflectables.push_back(new CameraMetaReflectable([&]() mutable {
auto camera_buff = buffer_group_get_buffer_data_ptr(buffer_group, CamerasBufferName);
auto cameras_ptr = (struct Camera*)(camera_buff.get());
return &cameras_ptr[index];
}, [&]() mutable {
UpdateChildren();
update_draw_list_fn();
}));
reflectables.push_back(new CameraViewReflectable([&]() mutable {
auto camera_buff = buffer_group_get_buffer_data_ptr(buffer_group, CamerasBufferName);
Expand Down Expand Up @@ -362,6 +379,7 @@ void GetCameraModel(int camera_index, out mat4 out_model, out int parent_index,
default: break;
}
}

void InitFramebuffer(Shader* shader, float width, float height) {
// Initialize camera framebuffer
fb_color_image = image_create({
Expand Down
13 changes: 11 additions & 2 deletions include/dz/ECS/Entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace dz::ecs {
int parent_index = -1;
int parent_cid = 0;
int enabled_components = 0;
int padding = 0;
int transform_dirty = 1;
vec<float, 4> position = vec<float, 4>(0.0f, 0.0f, 0.0f, 1.0f);
vec<float, 4> rotation = vec<float, 4>(0.0f, 0.0f, 0.0f, 1.0f);;
vec<float, 4> scale = vec<float, 4>(1.0f, 1.0f, 1.0f, 1.0f);;
Expand All @@ -24,7 +24,7 @@ struct Entity {
int parent_index;
int parent_cid;
int enabled_components;
int padding;
int transform_dirty;
vec4 position;
vec4 rotation;
vec4 scale;
Expand All @@ -39,6 +39,13 @@ struct Entity {
void GetEntityModel(int entity_index, out mat4 out_model, out int parent_index, out int parent_cid) {
Entity entity = GetEntityData(entity_index);

if (entity.transform_dirty == 0) {
out_model = entity.model;
parent_index = entity.parent_index;
parent_cid = entity.parent_cid;
return;
}

mat4 model = mat4(1.0);
model[3] = vec4(entity.position.xyz, 1.0);

Expand Down Expand Up @@ -71,6 +78,8 @@ void GetEntityModel(int entity_index, out mat4 out_model, out int parent_index,

parent_index = entity.parent_index;
parent_cid = entity.parent_cid;

entity.transform_dirty = 0;
}
)"}
};
Expand Down
5 changes: 5 additions & 0 deletions include/dz/ECS/Material.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
#include "../Image.hpp"

namespace dz::ecs {

struct MaterialIndexReflectable {
int material_index = 0;
};

struct Material : Provider<Material> {
vec<float, 4> atlas_pack = {-1.0f, -1.0f, -1.0f, -1.0f};
vec<float, 4> albedo = {1.0f, 1.0f, 1.0f, 1.0f};
Expand Down
Loading
Loading