Skip to content

Commit

Permalink
Merge pull request #62 from EthicalML/godot_example
Browse files Browse the repository at this point in the history
Adding godot example
  • Loading branch information
axsaucedo authored Sep 20, 2020
2 parents c1b9436 + e78e2e4 commit 5fc36e3
Show file tree
Hide file tree
Showing 52 changed files with 100,984 additions and 21 deletions.
27 changes: 7 additions & 20 deletions examples/array_multiplication/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,40 +1,27 @@
cmake_minimum_required(VERSION 3.17.0)
project(kompute_array_mult VERSION 0.1.0)
project(kompute_godot VERSION 0.1.0)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 14)

option(KOMPUTE_OPT_ENABLE_SPDLOG "Extra compile flags for Kompute, see docs for full list" 0)
set(KOMPUTE_EXTRA_CXX_FLAGS "" CACHE STRING "Extra compile flags for Kompute, see docs for full list")

if(KOMPUTE_OPT_ENABLE_SPDLOG)
set(KOMPUTE_EXTRA_CXX_FLAGS "${KOMPUTE_EXTRA_CXX_FLAGS} -DKOMPUTE_ENABLE_SPDLOG=1")
endif()

# It is necessary to pass the DEBUG or RELEASE flag accordingly to Kompute
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}")

find_package(kompute REQUIRED)
find_package(Vulkan REQUIRED)

if(KOMPUTE_OPT_ENABLE_SPDLOG)
find_package(spdlog REQUIRED)
find_package(fmt REQUIRED)
endif()

add_executable(kompute_array_mult
add_executable(kompute_godot
src/Main.cpp)

target_link_libraries(kompute_array_mult
target_link_libraries(kompute_godot
kompute::kompute
Vulkan::Vulkan
)

if(KOMPUTE_OPT_ENABLE_SPDLOG)
target_link_libraries(kompute_array_mult
kompute::kompute
fmt::fmt
spdlog::spdlog
)
endif()
target_link_libraries(kompute_godot
"lib/godot.windows.tools.64.lib"
)

2 changes: 2 additions & 0 deletions examples/godot_examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.import
godot_engine/godot
Empty file.
3 changes: 3 additions & 0 deletions examples/godot_examples/custom_module/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vulkan-kompute
lib
godot
66 changes: 66 additions & 0 deletions examples/godot_examples/custom_module/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Vulkan Kompute Godot Example

## 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:

```
git clone --branch v0.3.1 https://github.com/EthicalML/vulkan-kompute/
```

You will be able to use cmake to generate the build files for your platform.

```
cmake vulkan-kompute/. -Bvulkan-kompute/build
```

You need to make sure that the build is configured with the same flags required for godot, for example, in windows you will need:

* 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

### Clone godot repository

Now we can clone the godot repository - it must be on a separate repository, so you can use the parent directory if you are on the Kompute repo.

```
cd ../../godot_engine
git clone --branch 3.2.3-stable https://github.com/godotengine/godot
cd godot/
```

And now we can build against our module

```
wscons -j16 custom_modules=../../custom_module/ platform=windows target=release_debug
```

Once we have built it we can now run the generated godot engine in the `bin/` folder, and we will be able to access the custom module from anywhere in the project, as well as creating new nodes from the user interface.



Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* summator.cpp */

#include <vector>

#include "KomputeSummatorNode.h"

KomputeSummatorNode::KomputeSummatorNode() {
this->_init();
}

void KomputeSummatorNode::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 KomputeSummatorNode::reset() {
}

float KomputeSummatorNode::get_total() const {
return this->mPrimaryTensor->data()[0];
}

void KomputeSummatorNode::_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");

// We now record the steps in the sequence
if (std::shared_ptr<kp::Sequence> sq = this->mSequence.lock())
{

std::string shader(R"(
#version 450
layout (local_size_x = 1) in;
layout(set = 0, binding = 0) buffer a { float pa[]; };
layout(set = 0, binding = 1) buffer b { float pb[]; };
void main() {
uint index = gl_GlobalInvocationID.x;
pa[index] = pb[index] + pa[index];
}
)");

sq->begin();

// First we ensure secondary tensor loads to GPU
// No need to sync the primary tensor as it should not be changed
sq->record<kp::OpTensorSyncDevice>(
{ this->mSecondaryTensor });

// Then we run the operation with both tensors
sq->record<kp::OpAlgoBase<>>(
{ this->mPrimaryTensor, this->mSecondaryTensor },
std::vector<char>(shader.begin(), shader.end()));

// We map the result back to local
sq->record<kp::OpTensorSyncLocal>(
{ this->mPrimaryTensor });

sq->end();
}
else {
throw std::runtime_error("Sequence pointer no longer available");
}
}

void KomputeSummatorNode::_process(float delta) {

}

void KomputeSummatorNode::_bind_methods() {
ClassDB::bind_method(D_METHOD("_process", "delta"), &KomputeSummatorNode::_process);
ClassDB::bind_method(D_METHOD("_init"), &KomputeSummatorNode::_init);

ClassDB::bind_method(D_METHOD("add", "value"), &KomputeSummatorNode::add);
ClassDB::bind_method(D_METHOD("reset"), &KomputeSummatorNode::reset);
ClassDB::bind_method(D_METHOD("get_total"), &KomputeSummatorNode::get_total);
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include <memory>

#include "kompute/Kompute.hpp"

#include "scene/main/node.h"

class KomputeSummatorNode : public Node {
GDCLASS(KomputeSummatorNode, Node);

public:
KomputeSummatorNode();

void add(float value);
void reset();
float get_total() const;

void _process(float delta);
void _init();

protected:
static void _bind_methods();

private:
kp::Manager mManager;
std::weak_ptr<kp::Sequence> mSequence;
std::shared_ptr<kp::Tensor> mPrimaryTensor;
std::shared_ptr<kp::Tensor> mSecondaryTensor;
};

17 changes: 17 additions & 0 deletions examples/godot_examples/custom_module/kompute_summator/SCsub
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import os

Import('env')

dir_path = os.getcwd()

# Kompute & Vulkan header files
env.Append(CPPPATH = ['include/'])

env.add_source_files(env.modules_sources, "*.cpp")

# Kompute & Vulkan libraries
env.Append(LIBS=[
File(dir_path +'/lib/kompute.lib'),
File(dir_path +'/lib/vulkan-1.lib'),
])

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def can_build(env, platform):
return True

def configure(env):
pass
Loading

0 comments on commit 5fc36e3

Please sign in to comment.