This commit is contained in:
Aitor
2023-03-15 17:25:01 +01:00
parent 6f8e8da466
commit 7d8e985877
16 changed files with 190 additions and 50 deletions

View File

@@ -1,2 +1,3 @@
all:
clang -target wasm32 -Wl,--no-entry -Wl,--export-all -nostdlib -O3 add.c -o add.wasm
mkdir -p build
em++ -std=c++20 -O3 -sINCOMING_MODULE_JS_API=['print'] -sENVIRONMENT=web,node,worker -sFILESYSTEM=0 src/main.cpp -o build/main.js

View File

@@ -1,5 +1,7 @@
# Notas
## TO DO
- [ ] Mover todo esto a algún otro sitio mejor que no sea `resources/public`.
- [ ] Implementar tanto `clang-format` como `clang-tidy` para formatear el código.
- [ ] Implementar algún sistema de testing (Catch2, Google Test, CppUnit, etc).
@@ -11,6 +13,52 @@ Para compilar el código en C++ se puede usar la siguiente línea:
g++ -std=c++20 src/main.cpp -o main
```
## Emscripten
### Instalación
1. Clonar repositorio:
```sh
git clone https://github.com/emscripten-core/emsdk.git
cd emsdk
```
2. Actualizar e instalar dependencias:
```sh
git pull
./emsdk install latest
./emsdk activate latest
source ./emsdk_env.sh
```
3. Probar:
:bulb: Ahora deberíamos tener disponibles herramientas como `emcc` (equivalente a
`gcc` o `clang`), `em++` (equivalente a `g++` o `clang++`), `emmake`
(equivalente a `make`) o `emcmake` (equivalente a `cmake`).
Puedes compilar el proyecto con:
```sh
emmake make
```
## WebAssembly
### Memoria
La memoria de WebAssembly se crea cuando se instancia el módulo de WebAssembly, aunque esta memoria puede crecer. Si no se pasa un `WebAssembly.Memory` al módulo en la instanciación, crea una memoria por defecto con el número de páginas que indique el módulo.
:bulb: Para averiguar cuál es este valor por defecto podemos usar `wasm-objdump -x <module.wasm> | grep 'pages:'`.
La memoria de WebAssembly se reserva usando páginas (una página equivale a 64KB).
El máximo de memoria que puede reservar un módulo de WebAssembly ahora mismo son 4GB (65536 páginas).
:bulb: Ahora mismo existen dos _proposals_ para ampliar estos límites: [Memory64](https://github.com/WebAssembly/memory64) y [Multi-Memory](https://github.com/WebAssembly/multi-memory/blob/master/proposals/multi-memory/Overview.md)
## Documentos
- [C++ Core Guidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#S-introduction)
@@ -18,3 +66,7 @@ g++ -std=c++20 src/main.cpp -o main
## Recursos
- [Compiling C to WebAssembly without Emscripten](https://surma.dev/things/c-to-webassembly/)
- [Emscripten: C/C++ compiler toolchain](https://emscripten.org/)
- [Emscripten: settings.js](https://github.com/emscripten-core/emscripten/blob/main/src/settings.js)
- [WABT: WebAssembly Binary Toolkit](https://github.com/WebAssembly/wabt)
- [Binaryen: Compiler Toolchain](https://github.com/WebAssembly/binaryen)

View File

@@ -1,19 +0,0 @@
#include "int.h"
#define MAX_OPERATIONS 2048
typedef struct _operations {
int32_t a, b, r;
} operations_t;
operations_t operations[MAX_OPERATIONS];
int32_t add(int32_t a, int32_t b) {
return a + b;
}
void compute() {
for (int32_t i = 0; i < MAX_OPERATIONS; i++) {
operations[i].r = add(operations[i].a, operations[i].b);
}
}

View File

@@ -1,5 +0,0 @@
function add(a, b) {
return a + b
}
add(5, 5)

File diff suppressed because one or more lines are too long

Binary file not shown.

View File

@@ -1,10 +0,0 @@
#pragma once
typedef __INT8_TYPE__ int8_t;
typedef __UINT8_TYPE__ uint8_t;
typedef __INT16_TYPE__ int16_t;
typedef __UINT16_TYPE__ uint16_t;
typedef __INT32_TYPE__ int32_t;
typedef __UINT32_TYPE__ uint32_t;

View File

@@ -0,0 +1,3 @@
all:
em++ --no-entry -sEXPORT_KEEPALIVE=1 -sENVIRONMENT=web -sFILESYSTEM=0 vector.cpp -o vector.wasm

View File

@@ -0,0 +1,9 @@
import Vector from './vector.mjs'
console.log(Vector)
async function main()
{
const vector = await Vector()
}
main()

View File

@@ -0,0 +1,25 @@
#include <emscripten.h>
#include <iostream>
#include <vector>
#include <memory>
enum ShapeType
{
FRAME = 0,
RECT,
ELLIPSE,
};
struct Shape {
ShapeType type;
Shape() : type(FRAME) {};
};
static std::shared_ptr<Shape> root;
void EMSCRIPTEN_KEEPALIVE resize(const std::shared_ptr<Shape> ptr)
{
std::cout << "resize" << std::endl;
}

Binary file not shown.

View File

@@ -4,20 +4,20 @@
#include <iostream>
template <typename T>
struct Matrix23 {
struct Matrix2D {
// a c tx
// b d ty
T a, b, c, d, tx, ty;
Matrix23() : a(1), b(0), c(0), d(1), tx(0), ty(0) {}
Matrix23(T a, T b, T c, T d, T tx, T ty) : a(a), b(b), c(c), d(d), tx(tx), ty(ty) {}
Matrix23(const Matrix23<T>& other) : a(other.a), b(other.b), c(other.c), d(other.d), tx(other.tx), ty(other.ty) {}
Matrix2D() : a(1), b(0), c(0), d(1), tx(0), ty(0) {}
Matrix2D(T a, T b, T c, T d, T tx, T ty) : a(a), b(b), c(c), d(d), tx(tx), ty(ty) {}
Matrix2D(const Matrix2D<T>& other) : a(other.a), b(other.b), c(other.c), d(other.d), tx(other.tx), ty(other.ty) {}
auto determinant() const {
return a * d - b * c;
}
Matrix23<T>& identity()
Matrix2D<T>& identity()
{
a = 1;
b = 0;
@@ -28,14 +28,14 @@ struct Matrix23 {
return *this;
}
Matrix23<T>& translate(T x, T y)
Matrix2D<T>& translate(T x, T y)
{
tx += x;
ty += y;
return *this;
}
Matrix23<T>& scale(T x, T y)
Matrix2D<T>& scale(T x, T y)
{
a *= x;
b *= y;
@@ -44,7 +44,7 @@ struct Matrix23 {
return *this;
}
Matrix23<T>& rotate(auto angle)
Matrix2D<T>& rotate(auto angle)
{
auto cos = std::cos(angle);
auto sin = std::sin(angle);
@@ -62,7 +62,7 @@ struct Matrix23 {
return *this;
}
Matrix23<T> invert()
Matrix2D<T> invert()
{
auto det = determinant();
if (det == 0)
@@ -80,7 +80,7 @@ struct Matrix23 {
};
}
Matrix23<T> operator*(const Matrix23<T>& other)
Matrix2D<T> operator*(const Matrix2D<T>& other)
{
// M N
// a c x a c x
@@ -95,11 +95,28 @@ struct Matrix23 {
tx * other.b + ty * other.d + other.ty
};
}
static Matrix2D<T> create_translation(const T x, const T y)
{
return { 1, 0, 0, 1, x, y };
}
static Matrix2D<T> create_scale(const T x, const T y)
{
return { x, 0, 0, y, 0, 0 };
}
static Matrix2D<T> create_rotation(auto angle)
{
auto c = std::cos(angle);
auto s = std::sin(angle);
return { c, s, -s, c, 0, 0 };
}
};
template <typename T>
std::ostream &operator<<(std::ostream &os, const Matrix23<T> &matrix)
std::ostream &operator<<(std::ostream &os, const Matrix2D<T> &matrix)
{
os << "Matrix23(" << matrix.a << ", " << matrix.b << ", " << matrix.c << ", " << matrix.d << ", " << matrix.tx << ", " << matrix.ty << ")";
os << "Matrix2D(" << matrix.a << ", " << matrix.b << ", " << matrix.c << ", " << matrix.d << ", " << matrix.tx << ", " << matrix.ty << ")";
return os;
}

View File

@@ -0,0 +1,65 @@
#pragma once
#include <vector>
#include <memory>
#include "Vector2.h"
#include "Matrix2D.h"
#include "Color.h"
enum ShapeType
{
FRAME = 0,
RECT,
ELLIPSE,
PATH,
TEXT
};
enum PaintType
{
COLOR = 0,
IMAGE,
PATTERN,
LINEAR_GRADIENT,
RADIAL_GRADIENT
};
struct MatrixTransform
{
Matrix2D<float> concatenatedMatrix;
Matrix2D<float> matrix;
};
struct Transform
{
Vector2<float> position;
Vector2<float> scale;
float rotation;
};
struct Paint
{
PaintType type;
};
struct Stroke
{
float width;
Paint paint;
};
struct Fill
{
Paint paint;
};
struct Shape
{
ShapeType type;
Transform transform;
std::shared_ptr<Shape> parent;
std::vector<std::shared_ptr<Shape>> children;
// std::vector<Stroke> strokes;
// std::vector<Fill> fills;
};

View File

@@ -4,6 +4,7 @@
#include <iostream>
#include "Interpolation.h"
#include "Matrix2D.h"
template <typename T>
struct Vector2 {
@@ -108,7 +109,7 @@ struct Vector2 {
);
}
Vector2<T> operator*(const Matrix23<T> m)
Vector2<T> operator*(const Matrix2D<T> m)
{
return {
x * m.a + y * m.c + m.tx,

View File

@@ -2,7 +2,7 @@
#include <iostream>
#include "Interpolation.h"
#include "Matrix23.h"
#include "Matrix2D.h"
#include "Vector2.h"
#include "Box2.h"
@@ -16,7 +16,7 @@ int main(int argc, char** argv)
Vector2<float> a(1, 0);
Vector2<float> b(0, 1);
Matrix23<float> m;
Matrix2D<float> m;
std::cout << m << std::endl;