Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
feat: introduce wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
neko-para committed Aug 22, 2023
1 parent 6c6d499 commit 86d36b7
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ jobs:
submodules: true # Fetch Hugo themes (true OR recursive)
fetch-depth: 0 # Fetch all history for .GitInfo and .Lastmod

- name: Setup emsdk
uses: mymindstorm/setup-emsdk@v11

- name: Install cmake
run: |
sudo apt-get update
sudo apt-get install cmake
- name: Build wasm module
run: |
cd render
mkdir build
cd build
emcmake cmake ..
cmake --build .
- name: Use Node.js 18
uses: actions/setup-node@v3
with:
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ coverage

running
*.zip

build
CMakeUserPresets.json
1 change: 1 addition & 0 deletions packages/client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
<script src="./src/native/render.js"></script>
</body>
</html>
1 change: 1 addition & 0 deletions packages/client/public/render.wasm
20 changes: 20 additions & 0 deletions packages/client/src/data/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,26 @@ function wrapNext(v?: string | string[]) {
return v ? (typeof v === 'string' ? [v] : v) : []
}

export const taskForwardIndex = computed(() => {
const res: Record<string, string[]> = {}
for (const name in taskIndex.value) {
const st = new Set<string>()
const keys = ['next', 'timeout_next', 'runout_next'] as const
const task = getTask(taskIndex.value[name])
if (!task) {
continue
}
for (const key of keys) {
let arr = wrapNext(task[key])
for (const to of arr) {
st.add(to)
}
}
res[name] = [...st]
}
return res
})

export const taskBackwardIndex = computed(() => {
const res: Record<string, string[]> = {}
for (const name in taskIndex.value) {
Expand Down
1 change: 1 addition & 0 deletions packages/client/src/native
5 changes: 5 additions & 0 deletions packages/client/src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createRouter, createWebHistory } from 'vue-router'
import EditView from '@/views/EditView.vue'
import EvalView from '@/views/EvalView.vue'
import RoiView from '@/views/RoiView.vue'
import VisualView from '@/views/VisualView.vue'

const router = createRouter({
history: createWebHistory(),
Expand All @@ -22,6 +23,10 @@ const router = createRouter({
{
path: '/roi',
component: RoiView
},
{
path: '/visual',
component: VisualView
}
]
})
Expand Down
8 changes: 8 additions & 0 deletions packages/client/src/views/EditView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
FileDownloadOutlined,
FileUploadOutlined,
HealthAndSafetyOutlined,
MapOutlined,
NavigateBeforeOutlined,
NavigateNextOutlined,
RedoOutlined,
Expand Down Expand Up @@ -97,6 +98,13 @@ function doValidate() {
</NIcon>
</template>
</NButton>
<NButton @click="router.push('/visual')">
<template #icon>
<NIcon>
<MapOutlined></MapOutlined>
</NIcon>
</template>
</NButton>
<NButton @click="doValidate">
<template #icon>
<NIcon>
Expand Down
23 changes: 23 additions & 0 deletions packages/client/src/views/VisualView.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<script setup lang="ts">
import { NButton } from 'naive-ui'
import { taskForwardIndex } from '@/data'
function render() {
const vertIndex = Object.keys(taskForwardIndex.value)
const edges: number[] = []
for (const [idx, name] of vertIndex.entries()) {
for (const to of taskForwardIndex.value[name]) {
edges.push(idx)
edges.push(vertIndex.indexOf(to))
}
}
Module.layoutGraph(vertIndex.length, edges)
}
</script>

<template>
<div>
<NButton @click="render">计算</NButton>
</div>
</template>
3 changes: 3 additions & 0 deletions packages/client/src/wasm.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare const Module: {
layoutGraph: (vertCount: number, edges: number[]) => void
}
9 changes: 9 additions & 0 deletions render/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
cmake_minimum_required(VERSION 3.22)

project(MaaJsonViewer)

set(CMAKE_CXX_STANDARD 20)

add_executable(render main.cpp)

target_link_libraries(render embind)
40 changes: 40 additions & 0 deletions render/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <emscripten.h>
#include <emscripten/bind.h>
#include <stdio.h>

struct Edge {
int from, to;
};

int main() {}

template <typename T>
std::vector<T> convertToVector(const emscripten::val &arr) {
std::vector<T> vec;
for (size_t i = 0; i < arr["length"].as<size_t>(); ++i) {
vec.push_back(arr[i].as<T>());
}
return vec;
}

void layoutGraph(int n, const emscripten::val &v_edges) {
auto edgeVerts = convertToVector<int>(v_edges);
std::vector<Edge> edges;

for (int idx = 0; idx + 1 < edgeVerts.size(); idx += 2) {
int from = edgeVerts[idx];
int to = edgeVerts[idx + 1];
if (from < 0 || from >= n || to < 0 || to >= n) {
continue;
}
edges.emplace_back(Edge{from, to});
}

for (auto [f, t] : edges) {
printf("%d -> %d\n", f, t);
}
}

EMSCRIPTEN_BINDINGS(my_module) {
emscripten::function("layoutGraph", layoutGraph);
}

0 comments on commit 86d36b7

Please sign in to comment.