Added base working example

This commit is contained in:
Alejandro Saucedo 2020-09-20 12:40:35 +01:00
parent 9483739d3c
commit b76cc00b46
11 changed files with 189 additions and 84 deletions

View file

@ -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
)

View file

@ -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
```

View file

@ -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<godot::Summator>();
}

View file

@ -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;
}

View file

@ -0,0 +1,42 @@
#pragma once
#include <Godot.hpp>
#include <KinematicBody2D.hpp>
#include <Input.hpp>
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:
};
}

View file

@ -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<Summator>();
}
void unregister_summator_types() {
// Nothing to do here in this example.
}

View file

@ -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 */

View file

@ -0,0 +1 @@
#include "Root.h"

View file

@ -0,0 +1,5 @@
#pragma once
class Root
{
};

View file

@ -1,10 +1,38 @@
/* summator.cpp */
#include <vector>
#include <iostream>
#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<kp::Sequence> 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<kp::Sequence> 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);
//}
}

View file

@ -1,16 +1,16 @@
/* summator.h */
#pragma once
#ifndef SUMMATOR_H
#define SUMMATOR_H
#include <Godot.hpp>
#include <Node2D.hpp>
#include <memory>
#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<kp::Tensor> mSecondaryTensor;
};
#endif // SUMMATOR_H
}