Browse Source

Add SOA Struct Layout (experimental) to demo.odin

gingerBill 5 years ago
parent
commit
57853fe1b1
2 changed files with 72 additions and 0 deletions
  1. 71 0
      examples/demo/demo.odin
  2. 1 0
      src/check_expr.cpp

+ 71 - 0
examples/demo/demo.odin

@@ -1279,6 +1279,76 @@ range_statements_with_multiple_return_values :: proc() {
 	}
 }
 
+soa_struct_layout :: proc() {
+	// IMPORTANT NOTE(bill, 2019-11-03): This feature is subject to be changed/removed
+	fmt.println("\n#SOA Struct Layout");
+
+	{
+		Vector3 :: struct {x, y, z: f32};
+
+		N :: 2;
+		v_aos: [N]Vector3;
+		v_aos[0].x = 1;
+		v_aos[0].y = 4;
+		v_aos[0].z = 9;
+
+		fmt.println(len(v_aos));
+		fmt.println(v_aos[0]);
+		fmt.println(v_aos[0].x);
+		fmt.println(&v_aos[0].x);
+
+		v_aos[1] = {0, 3, 4};
+		v_aos[1].x = 2;
+		fmt.println(v_aos[1]);
+		fmt.println(v_aos);
+
+		v_soa: intrinsics.soa_struct(N, Vector3);
+
+		v_soa[0].x = 1;
+		v_soa[0].y = 4;
+		v_soa[0].z = 9;
+
+
+		// Same syntax as AOS and treat as if it was an array
+		fmt.println(len(v_soa));
+		fmt.println(v_soa[0]);
+		fmt.println(v_soa[0].x);
+		v_soa[1] = {0, 3, 4};
+		v_soa[1].x = 2;
+		fmt.println(v_soa[1]);
+
+		// Can use SOA syntax if necessary
+		v_soa.x[0] = 1;
+		v_soa.y[0] = 4;
+		v_soa.z[0] = 9;
+		fmt.println(v_soa.x[0]);
+
+		// Same pointer addresses with both syntaxes
+		assert(&v_soa[0].x == &v_soa.x[0]);
+
+
+		// Same fmt printing
+		fmt.println(v_aos);
+		fmt.println(v_soa);
+	}
+	{
+		// Works with arrays of length <= 4 which have the implicit fields xyzw/rgba
+		Vector3 :: distinct [3]f32;
+
+		N :: 2;
+		v_aos: [N]Vector3;
+		v_aos[0].x = 1;
+		v_aos[0].y = 4;
+		v_aos[0].z = 9;
+
+		v_soa: intrinsics.soa_struct(N, Vector3);
+
+		v_soa[0].x = 1;
+		v_soa[0].y = 4;
+		v_soa[0].z = 9;
+	}
+}
+
 main :: proc() {
 	when true {
 		extra_general_stuff();
@@ -1303,6 +1373,7 @@ main :: proc() {
 		where_clauses();
 		ranged_fields_for_array_compound_literals();
 		range_statements_with_multiple_return_values();
+		soa_struct_layout();
 	}
 }
 

+ 1 - 0
src/check_expr.cpp

@@ -4810,6 +4810,7 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32
 				Entity *new_field = alloc_entity_field(scope, token, array_type, false, cast(i32)i);
 				soa_struct->Struct.fields[i] = new_field;
 				add_entity(c->checker, scope, nullptr, new_field);
+				add_entity_use(c, nullptr, new_field);
 			}
 
 		} else {