Generate Float32Array serialization methods for vectors and matrices
Signed-off-by: John McCutchan <john@johnmccutchan.com>
diff --git a/gen/MatrixGen.dart b/gen/MatrixGen.dart
index cea809b..18ecf26 100644
--- a/gen/MatrixGen.dart
+++ b/gen/MatrixGen.dart
@@ -1367,6 +1367,48 @@
iPrint('}');
}
}
+
+ void generateBuffer() {
+ iPrint('\/\/\/ Copies [this] into [array] starting at [offset].');
+ iPrint('void copyIntoArray(Float32Array array, [int offset=0]) {');
+ iPush();
+ iPrint('int i = offset;');
+ for (int j = 0; j < cols; j++) {
+ for (int i = 0; i < rows; i++) {
+ iPrint('array[i] = ${Access(i,j)};');
+ iPrint('i++;');
+ }
+ }
+ iPop();
+ iPrint('}');
+ iPrint('\/\/\/ Returns a copy of [this] as a [Float32Array].');
+ iPrint('Float32Array copyAsArray() {');
+ iPush();
+ iPrint('Float32Array array = new Float32Array(${rows * cols});');
+ iPrint('int i = 0;');
+ for (int j = 0; j < cols; j++) {
+ for (int i = 0; i < rows; i++) {
+ iPrint('array[i] = ${Access(i,j)};');
+ iPrint('i++;');
+ }
+ }
+ iPrint('return array;');
+ iPop();
+ iPrint('}');
+ iPrint('\/\/\/ Copies elements from [array] into [this] starting at [offset].');
+ iPrint('void copyFromArray(Float32Array array, [int offset=0]) {');
+ iPush();
+ iPrint('int i = offset;');
+ for (int j = 0; j < cols; j++) {
+ for (int i = 0; i < rows; i++) {
+ iPrint('${Access(i,j)} = array[i];');
+ iPrint('i++;');
+ }
+ }
+ iPop();
+ iPrint('}');
+ }
+
void generate() {
writeLicense();
generatePrologue();
@@ -1402,6 +1444,7 @@
generateSelfTransposeMultiplyMatrix();
generateSelfMultiplyTransposeMatrix();
generateTransforms();
+ generateBuffer();
generateEpilogue();
}
}
diff --git a/gen/VectorMathGen.dart b/gen/VectorMathGen.dart
index 991fe0b..5bbdefe 100644
--- a/gen/VectorMathGen.dart
+++ b/gen/VectorMathGen.dart
@@ -271,6 +271,17 @@
}
iPop();
iPrint('}');
+
+ iPrint('\/\/\/ Constructs a new [$generatedName] that is initialized with values from [array] starting at [offset].');
+ iPrint('$generatedName.array(Float32Array array, [int offset=0]) {');
+ iPush();
+ iPrint('int i = offset;');
+ for (String e in vectorComponents) {
+ iPrint('$e = array[i];');
+ iPrint('i++;');
+ }
+ iPop();
+ iPrint('}');
}
void generateToString() {
@@ -671,6 +682,41 @@
iPrint('}');
}
+ void generateBuffer() {
+ iPrint('\/\/\/ Copies [this] into [array] starting at [offset].');
+ iPrint('void copyIntoArray(Float32Array array, [int offset=0]) {');
+ iPush();
+ iPrint('int i = offset;');
+ for (String c in vectorComponents) {
+ iPrint('array[i] = $c;');
+ iPrint('i++;');
+ }
+ iPop();
+ iPrint('}');
+ iPrint('\/\/\/ Returns a copy of [this] as a [Float32Array].');
+ iPrint('Float32Array copyAsArray() {');
+ iPush();
+ iPrint('Float32Array array = new Float32Array($vectorLen);');
+ iPrint('int i = 0;');
+ for (String c in vectorComponents) {
+ iPrint('array[i] = $c;');
+ iPrint('i++;');
+ }
+ iPrint('return array;');
+ iPop();
+ iPrint('}');
+ iPrint('\/\/\/ Copies elements from [array] into [this] starting at [offset].');
+ iPrint('void copyFromArray(Float32Array array, [int offset=0]) {');
+ iPush();
+ iPrint('int i = offset;');
+ for (String c in vectorComponents) {
+ iPrint('$c = array[i];');
+ iPrint('i++;');
+ }
+ iPop();
+ iPrint('}');
+ }
+
void generate() {
writeLicense();
generatePrologue();
@@ -719,6 +765,7 @@
generateSelfScalarOp('Scale', '*');
generateSelfNegate();
generateCopy();
+ generateBuffer();
generateEpilogue();
}
}
diff --git a/lib/VectorMath/VectorMath.dart b/lib/VectorMath/VectorMath.dart
index 0a1c029..c2484d2 100644
--- a/lib/VectorMath/VectorMath.dart
+++ b/lib/VectorMath/VectorMath.dart
@@ -23,6 +23,7 @@
*/
#library("VectorMath");
#import('dart:builtin');
+#import('dart:html'); // For Float32Array
#source("src/ScalarMath.dart");
#source("gen/vec2_gen.dart");
#source("gen/vec3_gen.dart");