diff --git a/examples/godot_examples/gdnative_shared/CMakeLists.txt b/examples/godot_examples/gdnative_shared/CMakeLists.txt index 236655f39..520a0613d 100644 --- a/examples/godot_examples/gdnative_shared/CMakeLists.txt +++ b/examples/godot_examples/gdnative_shared/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.17.0) -project(kompute_linear_reg VERSION 0.1.0) +project(kompute_godot VERSION 0.1.0) set(CMAKE_CXX_STANDARD 14) @@ -9,18 +9,32 @@ set(KOMPUTE_EXTRA_CXX_FLAGS "" CACHE STRING "Extra compile flags for Kompute, se set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUG=1 ${KOMPUTE_EXTRA_CXX_FLAGS}") set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -DRELEASE=1 ${KOMPUTE_EXTRA_CXX_FLAGS}") +set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CC_FLAGS} /W3 /MDd /Zi /EHsc /Ox /DNDEBUG /FS") + find_package(kompute REQUIRED) find_package(Vulkan REQUIRED) -add_executable(kompute_linear_reg - src/*.cpp) +add_library(kompute_godot + SHARED + src/summator.cpp + src/gdlibrary.cpp) -target_link_libraries(kompute_linear_reg +target_include_directories( + kompute_godot PUBLIC + src/ + godot-cpp/include + godot-cpp/include/core + godot-cpp/include/gen + godot-cpp/godot_headers + +) + +target_link_libraries(kompute_godot kompute::kompute Vulkan::Vulkan ) -target_link_libraries(kompute_linear_reg - "lib/godot.windows.tools.64.lib" +target_link_libraries(kompute_godot + ${CMAKE_CURRENT_SOURCE_DIR}/godot-cpp/bin/libgodot-cpp.windows.release.default.lib ) diff --git a/examples/godot_examples/gdnative_shared/README.md b/examples/godot_examples/gdnative_shared/README.md index d8e88ee00..d7aa37520 100644 --- a/examples/godot_examples/gdnative_shared/README.md +++ b/examples/godot_examples/gdnative_shared/README.md @@ -2,44 +2,24 @@ ## Set Up Dependencies -### Vulkan - -You will need the Vulkan SDK, in this case we use version `1.2.148.1`, which you can get at the official site https://vulkan.lunarg.com/sdk/home#windows - -This will have the following contents that will be required later on: - -* The VulkanSDK static library `vulkan-1` - -### Kompute - -We will be using v0.3.1 of Kompute, and similar to above we will need the built static library, but in this case we will build it. - -We can start by cloning the repository on the v0.3.1 branch: +We can get all the required dependencies from godot by running ``` -git clone --branch v0.3.1 https://github.com/EthicalML/vulkan-kompute/ +git clone --branch 3.2 https://github.com/godotengine/godot-cpp + +cd godot-cpp ``` -You will be able to use cmake to generate the build files for your platform. +Then we can get all the subomdules ``` -cmake vulkan-kompute/. -Bvulkan-kompute/build +git submodule sync ``` -You need to make sure that the build is configured with the same flags required for godot, for example, in windows you will need: +and we build the bindings -* Release build -* Configuration type: static library -* Runtime lib: Multi-threaded / multi-threaded debug - -Now you should see the library built under `build/src/Release` - -## Building Godot - -Now to build godot you will need to set up a couple of things for the Scons file to work - namely setting up the following: - -* Copy the `vulkan-1` library from your vulkan sdk folder to `lib/vulkan-1.lib` -* Copy the `kompute.lib` library from the Kompute build to `lib/kompute.lib` -* Make sure the versions above match as we provide the headers in the `include` folder - if you used different versions make sure these match as well +``` +scons -j16 platform=linuxbsd target=debug +``` diff --git a/examples/godot_examples/gdnative_shared/src/gdlibrary.cpp b/examples/godot_examples/gdnative_shared/src/gdlibrary.cpp new file mode 100644 index 000000000..599bba3ac --- /dev/null +++ b/examples/godot_examples/gdnative_shared/src/gdlibrary.cpp @@ -0,0 +1,15 @@ +#include "summator.h" + +extern "C" void GDN_EXPORT godot_gdnative_init(godot_gdnative_init_options *o) { + godot::Godot::gdnative_init(o); +} + +extern "C" void GDN_EXPORT godot_gdnative_terminate(godot_gdnative_terminate_options *o) { + godot::Godot::gdnative_terminate(o); +} + +extern "C" void GDN_EXPORT godot_nativescript_init(void *handle) { + godot::Godot::nativescript_init(handle); + + godot::register_class(); +} diff --git a/examples/godot_examples/gdnative_shared/src/player.cpp b/examples/godot_examples/gdnative_shared/src/player.cpp new file mode 100644 index 000000000..79c8c79ae --- /dev/null +++ b/examples/godot_examples/gdnative_shared/src/player.cpp @@ -0,0 +1,39 @@ +#include "Player.h" + +using namespace godot; + +void Player::_register_methods() { + register_method((char*)"_process", &Player::_process); +} + +void Player::_init() {} + +Player::Player() { + motion = Vector2(0, 0); +} + +Player::~Player() {} + +void Player::_process(float delta) +{ + UpdateMotionFromInput(); + move_and_slide(motion); +} + +void Player::UpdateMotionFromInput() +{ + motion = Vector2(0, 0); + + + Input* i = Input::get_singleton(); + + if (i->is_action_pressed("ui_up")) + motion.y -= speed; + if (i->is_action_pressed("ui_down")) + motion.y += speed; + if (i->is_action_pressed("ui_left")) + motion.x -= speed; + if (i->is_action_pressed("ui_right")) + motion.x += speed; + +} diff --git a/examples/godot_examples/gdnative_shared/src/player.h b/examples/godot_examples/gdnative_shared/src/player.h new file mode 100644 index 000000000..9d836af13 --- /dev/null +++ b/examples/godot_examples/gdnative_shared/src/player.h @@ -0,0 +1,42 @@ +#pragma once + +#include +#include +#include + +namespace godot { + class Player : public KinematicBody2D + { + + // Godot structure + private: + GODOT_CLASS(Player, KinematicBody2D) + public: + static void _register_methods(); + void _init(); + void _process(float delta); + + Player(); + ~Player(); + + + // Gameplay variables + public: + const int speed = 300; + + private: + Vector2 motion; + + + + // Gameplay methods + public: + void UpdateMotionFromInput(); + + private: + + + + + }; +} diff --git a/examples/godot_examples/gdnative_shared/src/register_types.cpp b/examples/godot_examples/gdnative_shared/src/register_types.cpp deleted file mode 100644 index 3cb5b536f..000000000 --- a/examples/godot_examples/gdnative_shared/src/register_types.cpp +++ /dev/null @@ -1,14 +0,0 @@ -/* register_types.cpp */ - -#include "register_types.h" - -#include "core/class_db.h" -#include "summator.h" - -void register_summator_types() { - ClassDB::register_class(); -} - -void unregister_summator_types() { - // Nothing to do here in this example. -} diff --git a/examples/godot_examples/gdnative_shared/src/register_types.h b/examples/godot_examples/gdnative_shared/src/register_types.h deleted file mode 100644 index fa64f1c7b..000000000 --- a/examples/godot_examples/gdnative_shared/src/register_types.h +++ /dev/null @@ -1,6 +0,0 @@ -/* register_types.h */ -#pragma once - -void register_summator_types(); -void unregister_summator_types(); -/* yes, the word in the middle must be the same as the module folder name */ diff --git a/examples/godot_examples/gdnative_shared/src/root.cpp b/examples/godot_examples/gdnative_shared/src/root.cpp new file mode 100644 index 000000000..384c905c1 --- /dev/null +++ b/examples/godot_examples/gdnative_shared/src/root.cpp @@ -0,0 +1 @@ +#include "Root.h" diff --git a/examples/godot_examples/gdnative_shared/src/root.h b/examples/godot_examples/gdnative_shared/src/root.h new file mode 100644 index 000000000..fba0e2470 --- /dev/null +++ b/examples/godot_examples/gdnative_shared/src/root.h @@ -0,0 +1,5 @@ +#pragma once +class Root +{ +}; + diff --git a/examples/godot_examples/gdnative_shared/src/summator.cpp b/examples/godot_examples/gdnative_shared/src/summator.cpp index ab87d1376..89aed9c27 100644 --- a/examples/godot_examples/gdnative_shared/src/summator.cpp +++ b/examples/godot_examples/gdnative_shared/src/summator.cpp @@ -1,10 +1,38 @@ /* summator.cpp */ #include +#include #include "summator.h" +namespace godot { + Summator::Summator() { + std::cout << "CALLING CONSTRUCTOR" << std::endl; + this->_init(); +} + +void Summator::add(float value) { + // Set the new data in the local device + this->mSecondaryTensor->setData({value}); + // Execute recorded sequence + if (std::shared_ptr sq = this->mSequence.lock()) { + sq->eval(); + } + else { + throw std::runtime_error("Sequence pointer no longer available"); + } +} + +void Summator::reset() { +} + +float Summator::get_total() const { + return this->mPrimaryTensor->data()[0]; +} + +void Summator::_init() { + std::cout << "CALLING INIT" << std::endl; this->mPrimaryTensor = this->mManager.buildTensor({ 0.0 }); this->mSecondaryTensor = this->mManager.buildTensor({ 0.0 }); this->mSequence = this->mManager.getOrCreateManagedSequence("AdditionSeq"); @@ -50,28 +78,24 @@ Summator::Summator() { } } -void Summator::add(float value) { - // Set the new data in the local device - this->mSecondaryTensor->setData({value}); - // Execute recorded sequence - if (std::shared_ptr sq = this->mSequence.lock()) { - sq->eval(); - } - else { - throw std::runtime_error("Sequence pointer no longer available"); - } +void Summator::_process(float delta) { + std::cout << "CALLING PROCESS" << std::endl; + } -void Summator::reset() { +void Summator::_register_methods() { + register_method((char *)"_process", &Summator::_process); + register_method((char *)"_init", &Summator::_init); + register_method((char *)"add", &Summator::add); + register_method((char *)"reset", &Summator::reset); + register_method((char *)"get_total", &Summator::get_total); } -float Summator::get_total() const { - return this->mPrimaryTensor->data()[0]; -} - -void Summator::_bind_methods() { - ClassDB::bind_method(D_METHOD("add", "value"), &Summator::add); - ClassDB::bind_method(D_METHOD("reset"), &Summator::reset); - ClassDB::bind_method(D_METHOD("get_total"), &Summator::get_total); +//void Summator::_bind_methods() { +// ClassDB::bind_method(D_METHOD("add", "value"), &Summator::add); +// ClassDB::bind_method(D_METHOD("reset"), &Summator::reset); +// ClassDB::bind_method(D_METHOD("get_total"), &Summator::get_total); +//} + } diff --git a/examples/godot_examples/gdnative_shared/src/summator.h b/examples/godot_examples/gdnative_shared/src/summator.h index 9c13c5565..fdd8b8970 100644 --- a/examples/godot_examples/gdnative_shared/src/summator.h +++ b/examples/godot_examples/gdnative_shared/src/summator.h @@ -1,16 +1,16 @@ -/* summator.h */ +#pragma once -#ifndef SUMMATOR_H -#define SUMMATOR_H +#include +#include #include #include "kompute/Kompute.hpp" -#include "scene/main/node.h" - -class Summator : public Node { - GDCLASS(Summator, Node); +namespace godot { +class Summator : public Node2D { +private: + GODOT_CLASS(Summator, Node2D); public: Summator(); @@ -19,8 +19,13 @@ public: void reset(); float get_total() const; + void _process(float delta); + void _init(); + + static void _register_methods(); + protected: - static void _bind_methods(); + //static void _bind_methods(); private: kp::Manager mManager; @@ -29,4 +34,4 @@ private: std::shared_ptr mSecondaryTensor; }; -#endif // SUMMATOR_H +}