Skip to content

Commit

Permalink
Added new samples, HelloScreen and HelloTriangle (#133)
Browse files Browse the repository at this point in the history
feat: added HelloScreen and HelloTriangle example

These two samples showcase basic initialization as well as basic rendering output for the library. Additionally various smaller fixes were done in this commit.

By default the examples are disabled, but can be enabled using the `PE_EXAMPLES` cmake arg.

fix:
    - path transformer would crash when a directory of 0 sized was used as arg
    - library had competing default constructors
    - when using an optional library path it would fail due to tests that could only run with a valid library
    - psl::meta::library did not handle relative paths well

misc:
    - added a readme for the examples
    - silenced a warning related to nothrow
    - ability to specify sub-directories for cmake targets
    - added warning when the gpu_camera system runs without a camera
  • Loading branch information
JessyDL committed Jun 12, 2024
1 parent 4c705e5 commit ab99b18
Show file tree
Hide file tree
Showing 44 changed files with 1,105 additions and 59 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ OPTION(PE_TESTS "make the tests" ON)
OPTION(PE_PCH "enable usage of the precompiled header" ON)
OPTION(PE_CCACHE "enable ccache" ON)
OPTION(PE_CORE "generate the paradigm core, turn this off if you only want the PSL to generate" ON)
OPTION(PE_EXAMPLES "generate the examples" OFF)

include(Utils)
include(FindXCB)
Expand Down Expand Up @@ -399,6 +400,10 @@ if(PE_TESTS)
add_subdirectory(tests)
endif()

if(PE_EXAMPLES)
add_subdirectory(examples)
endif()


if(EXISTS "${PROJECT_SOURCE_DIR}/modules.txt")
include(${PROJECT_SOURCE_DIR}/modules.txt)
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ set_property(TARGET benchmarks PROPERTY FOLDER "benchmarks")
target_link_libraries(benchmarks PUBLIC ${SHLWAPI} paradigm::core benchmark::benchmark)
set_target_properties(benchmarks PROPERTIES LINKER_LANGUAGE CXX)

set_target_output_directory(benchmarks)
set_target_output_directory(TARGET benchmarks)
target_include_directories(benchmarks
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/inc
Expand Down
50 changes: 36 additions & 14 deletions cmake/Utils.cmake
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@

macro(set_target_output_directory target)
foreach(OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES})
string(TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG)
string(TOLOWER ${OUTPUTCONFIG} OUTPUTCONFIG_FOLDERNAME)
set_target_properties(${target}
PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG} "${PE_BUILD_DIR}/${OUTPUTCONFIG_FOLDERNAME}/${ARCHI}/lib"
LIBRARY_OUTPUT_DIRECTORY_${OUTPUTCONFIG} "${PE_BUILD_DIR}/${OUTPUTCONFIG_FOLDERNAME}/${ARCHI}/lib"
RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} "${PE_BUILD_DIR}/${OUTPUTCONFIG_FOLDERNAME}/${ARCHI}/bin")
macro(set_target_output_directory)
set(oneValueArgs DIRECTORY)
set(multiValueArgs TARGET)
cmake_parse_arguments(SET_TARGET_OUTPUT_DIRECTORY "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
if(NOT DEFINED ${SET_TARGET_OUTPUT_DIRECTORY_DIRECTORY})
set(${SET_TARGET_OUTPUT_DIRECTORY_DIRECTORY} "")
endif()
foreach(targ ${SET_TARGET_OUTPUT_DIRECTORY_TARGET})
get_target_property(target_type ${targ} TYPE)
foreach(OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES})
string(TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG)
string(TOLOWER ${OUTPUTCONFIG} OUTPUTCONFIG_FOLDERNAME)
# mostly done so the directories don't get created in the output for binaries when unneeded.
if (target_type STREQUAL "EXECUTABLE")
set_target_properties(${targ}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} "${PE_BUILD_DIR}/${OUTPUTCONFIG_FOLDERNAME}/${ARCHI}/bin/${SET_TARGET_OUTPUT_DIRECTORY_DIRECTORY}")
else()
set_target_properties(${targ}
PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG} "${PE_BUILD_DIR}/${OUTPUTCONFIG_FOLDERNAME}/${ARCHI}/lib/${SET_TARGET_OUTPUT_DIRECTORY_DIRECTORY}"
LIBRARY_OUTPUT_DIRECTORY_${OUTPUTCONFIG} "${PE_BUILD_DIR}/${OUTPUTCONFIG_FOLDERNAME}/${ARCHI}/lib/${SET_TARGET_OUTPUT_DIRECTORY_DIRECTORY}"
RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} "${PE_BUILD_DIR}/${OUTPUTCONFIG_FOLDERNAME}/${ARCHI}/bin/${SET_TARGET_OUTPUT_DIRECTORY_DIRECTORY}")
endif()
endforeach()
if (target_type STREQUAL "EXECUTABLE")
set_target_properties(${targ}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${PE_BUILD_DIR}/default/bin/${SET_TARGET_OUTPUT_DIRECTORY_DIRECTORY}")
else ()
set_target_properties(${targ}
PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY "${PE_BUILD_DIR}/default/lib/${SET_TARGET_OUTPUT_DIRECTORY_DIRECTORY}"
LIBRARY_OUTPUT_DIRECTORY "${PE_BUILD_DIR}/default/lib/${SET_TARGET_OUTPUT_DIRECTORY_DIRECTORY}"
RUNTIME_OUTPUT_DIRECTORY "${PE_BUILD_DIR}/default/bin/${SET_TARGET_OUTPUT_DIRECTORY_DIRECTORY}")
endif()
endforeach()
set_target_properties(${target}
PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY "${PE_BUILD_DIR}/default/lib"
LIBRARY_OUTPUT_DIRECTORY "${PE_BUILD_DIR}/default/lib"
RUNTIME_OUTPUT_DIRECTORY "${PE_BUILD_DIR}/default/bin")
endmacro()
2 changes: 1 addition & 1 deletion core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ if(VK_STATIC AND PE_VULKAN)
endif()

target_link_libraries(core PUBLIC paradigm::psl ${CMAKE_DL_LIBS} ${PE_DL_LIBS} ${TEST_LIBS})
set_target_output_directory(core)
set_target_output_directory(TARGET core)
target_include_directories(core
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/inc
Expand Down
10 changes: 5 additions & 5 deletions core/inc/core/gfx/drawgroup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ class drawgroup {
drawgroup& operator=(drawgroup&&) = default;

const drawlayer& layer(const psl::string& layer, uint32_t priority, uint32_t extent) noexcept;
bool contains(const psl::string& layer) const noexcept;
std::optional<std::reference_wrapper<const drawlayer>> get(const psl::string& layer) const noexcept;
[[nodiscard]] bool contains(const psl::string& layer) const noexcept;
[[nodiscard]] std::optional<std::reference_wrapper<const drawlayer>> get(const psl::string& layer) const noexcept;
bool priority(drawlayer& layer, uint32_t priority) noexcept;

drawcall& add(const drawlayer& layer, core::resource::handle<core::gfx::bundle> bundle) noexcept;
std::optional<std::reference_wrapper<drawcall>> get(const drawlayer& layer,
core::resource::handle<core::gfx::bundle> bundle) noexcept;
[[nodiscard]] drawcall& add(const drawlayer& layer, core::resource::handle<core::gfx::bundle> bundle) noexcept;
[[nodiscard]] std::optional<std::reference_wrapper<drawcall>>
get(const drawlayer& layer, core::resource::handle<core::gfx::bundle> bundle) noexcept;

// bool remove(const drawlayer& layer);
// bool remove(const drawcall& call);
Expand Down
3 changes: 3 additions & 0 deletions core/inc/core/gfx/swapchain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ class swapchain {
swapchain& operator=(const swapchain& other) = delete;
swapchain& operator=(swapchain&& other) noexcept = delete;

void clear_color(const psl::vec4& color) noexcept;
psl::vec4 clear_color() const noexcept;

template <core::gfx::graphics_backend backend>
core::resource::handle<backend_type_t<swapchain, backend>> resource() const noexcept {
#ifdef PE_VULKAN
Expand Down
4 changes: 2 additions & 2 deletions core/inc/core/gles/swapchain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class swapchain {

bool present();
void clear();
/*/// returns the amount of images in the swapchain
/// returns the amount of images in the swapchain
uint32_t size() const noexcept;

/// \returns the width of the swapchain image
Expand Down Expand Up @@ -53,7 +53,7 @@ class swapchain {
void clear_color(psl::vec4 color) noexcept;

/// \returns false in case the window might be resizing.
bool is_ready() const noexcept;*/
bool is_ready() const noexcept;

private:
psl::vec4 m_ClearColor {0.25f, 0.4f, 0.95f, 1.0f};
Expand Down
6 changes: 4 additions & 2 deletions core/inc/core/meta/shader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class shader final : public psl::meta::file {
};

shader() = default;
shader(const psl::UID& key) : psl::meta::file(key) {};
shader(const psl::UID& key);

~shader() = default;

Expand Down Expand Up @@ -188,6 +188,8 @@ class shader final : public psl::meta::file {
psl::array_view<descriptor> descriptors() const noexcept { return {m_Descriptors.value}; }
void descriptors(psl::array<descriptor> value) noexcept { m_Descriptors.value = std::move(value); }

static void register_serializer();

private:
/// \brief method that will be invoked by the serialization system.
/// \tparam S the type of the serializer/deserializer
Expand Down Expand Up @@ -234,6 +236,6 @@ class shader final : public psl::meta::file {
/// \brief returns the polymorphic ID at runtime, to resolve what type this is.
virtual const uint64_t polymorphic_id() override { return polymorphic_identity; }
/// \brief the associated unique ID (per type, not instance) for the polymorphic system.
static const uint64_t polymorphic_identity;
static uint64_t polymorphic_identity;
};
} // namespace core::meta
2 changes: 1 addition & 1 deletion core/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ set_target_properties(main PROPERTIES LINKER_LANGUAGE CXX)

set_property(TARGET main PROPERTY FOLDER "paradigm-engine")

set_target_output_directory(main)
set_target_output_directory(TARGET main)
2 changes: 1 addition & 1 deletion core/main/android_native_app_glue/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
add_library(android_native_glue STATIC ${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c)
set_target_properties(android_native_glue PROPERTIES LINKER_LANGUAGE C)
add_library(paradigm::android_native_glue ALIAS android_native_glue)
set_target_output_directory(android_native_glue)
set_target_output_directory(TARGET android_native_glue)
6 changes: 5 additions & 1 deletion core/src/ecs/systems/gpu_camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ void gpu_camera::tick(
psl::ecs::info_t& info,
psl::ecs::pack_direct_full_t<const core::ecs::components::camera, const core::ecs::components::transform> cameras) {
size_t i {0};
if(cameras.size() >= m_Max)
if(cameras.empty()) {
core::log->warn("no cameras found in the scene");
}
if(cameras.size() >= m_Max) {
throw std::runtime_error(
fmt::format("cannot allocate more than {}, but {} was requested for this frame", m_Max, cameras.size()));
}

auto camera_components = cameras.get<const core::ecs::components::camera>();
auto transf_components = cameras.get<const core::ecs::components::transform>();
Expand Down
33 changes: 33 additions & 0 deletions core/src/gfx/swapchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,36 @@ swapchain::swapchain(core::resource::cache_t& cache,
throw std::runtime_error("a backend that was not enabled somehow was used to create a context.");
}
}

void swapchain::clear_color(const psl::vec4& color) noexcept {
switch(m_Backend) {
case graphics_backend::vulkan: {
#if defined(PE_VULKAN)
vk::ClearColorValue vk_color;
vk_color.float32[0] = color.x;
vk_color.float32[1] = color.y;
vk_color.float32[2] = color.z;
vk_color.float32[3] = color.w;
m_VKHandle->clear_color(vk_color);
#else
psl::fatal("a backend that was not enabled somehow was used to create a context.");
#endif
} break;
case graphics_backend::gles:
#ifdef PE_GLES
m_GLESHandle->clear_color(color);
#else
psl::fatal("a backend that was not enabled somehow was used to create a context.");
#endif
break;
case graphics_backend::webgpu:
#if defined(PE_WEBGPU)
psl::not_implemented();
#else
psl::fatal("a backend that was not enabled somehow was used to create a context.");
#endif
break;
default:
psl::fatal("unknown backend used");
}
}
36 changes: 36 additions & 0 deletions core/src/gles/swapchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,39 @@ void swapchain::clear() {
glClearColor(m_ClearColor[0], m_ClearColor[1], m_ClearColor[2], m_ClearColor[3]);
}
}

uint32_t swapchain::size() const noexcept {
return 1;
}

uint32_t swapchain::width() const noexcept {
return m_Surface->data().width();
}

uint32_t swapchain::height() const noexcept {
return m_Surface->data().height();
}

const psl::vec4 swapchain::clear_color() const noexcept {
return m_ClearColor;
}

const float swapchain::clear_depth() const noexcept {
return m_ClearDepth;
}

const uint32_t swapchain::clear_stencil() const noexcept {
return m_ClearStencil;
}

bool swapchain::has_depth() const noexcept {
return m_UseDepth;
}

void swapchain::clear_color(psl::vec4 color) noexcept {
m_ClearColor = color;
}

bool swapchain::is_ready() const noexcept {
return true;
}
15 changes: 14 additions & 1 deletion core/src/meta/shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,20 @@

using namespace core::meta;
using namespace psl::serialization;
const uint64_t shader::polymorphic_identity {register_polymorphic<shader>()};
uint64_t shader::polymorphic_identity {register_polymorphic<shader>()};


shader::shader(const psl::UID& key) : psl::meta::file(key) {};


// todo: for some reason the polymorphic_identity is not being self-registered
// on msvc. This is a workaround for that. See issue https://github.com/JessyDL/paradigm/issues/134
void shader::register_serializer() {
static auto reg = []() {
polymorphic_identity = register_polymorphic<shader>();
return true;
}();
}
//
//// vertex attribute
// shader::vertex::attribute::attribute() {}
Expand Down
4 changes: 3 additions & 1 deletion core/src/vk/pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,10 @@ bool pipeline::update(core::resource::cache_t& cache, const core::data::material
buffer_handle->buffer->resource<gfx::graphics_backend::vulkan>()->gpu_buffer();
bufferInfo->offset = 0;
// todo this lookup can be improved
bufferInfo->range = get_range(
auto const range = get_range(
m_Context.value(), *shader_handle.meta(), binding, buffer_handle->buffer->data().size());
bufferInfo->range = range;
;
} else {
core::ivk::log->warn("Tried to use the unloaded {} in a pipeline in shader {}",
vk::to_string(conversion::to_vk(binding.descriptor())),
Expand Down
2 changes: 2 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_subdirectory(HelloScreen)
add_subdirectory(HelloTriangle)
10 changes: 10 additions & 0 deletions examples/HelloScreen/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
add_executable(ex_hello_screen main.cpp)
add_executable(paradigm::examples::hello_screen ALIAS ex_hello_screen)
target_link_libraries(ex_hello_screen PUBLIC paradigm::psl ${PE_DL_LIBS} paradigm::core)

target_compile_features(ex_hello_screen PUBLIC ${PE_COMPILER_FEATURES})
target_compile_options(ex_hello_screen PRIVATE ${PE_COMPILE_OPTIONS} ${PE_COMPILE_OPTIONS_EXE})
set_target_properties(ex_hello_screen PROPERTIES LINKER_LANGUAGE CXX)

set_property(TARGET ex_hello_screen PROPERTY FOLDER "paradigm-engine/examples")
set_target_output_directory(TARGET ex_hello_screen DIRECTORY "examples/HelloScreen")
Loading

0 comments on commit ab99b18

Please sign in to comment.