mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 15:48:51 +00:00
Add SOA Struct Layout (experimental) to demo.odin
This commit is contained in:
@@ -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() {
|
main :: proc() {
|
||||||
when true {
|
when true {
|
||||||
extra_general_stuff();
|
extra_general_stuff();
|
||||||
@@ -1303,6 +1373,7 @@ main :: proc() {
|
|||||||
where_clauses();
|
where_clauses();
|
||||||
ranged_fields_for_array_compound_literals();
|
ranged_fields_for_array_compound_literals();
|
||||||
range_statements_with_multiple_return_values();
|
range_statements_with_multiple_return_values();
|
||||||
|
soa_struct_layout();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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);
|
Entity *new_field = alloc_entity_field(scope, token, array_type, false, cast(i32)i);
|
||||||
soa_struct->Struct.fields[i] = new_field;
|
soa_struct->Struct.fields[i] = new_field;
|
||||||
add_entity(c->checker, scope, nullptr, new_field);
|
add_entity(c->checker, scope, nullptr, new_field);
|
||||||
|
add_entity_use(c, nullptr, new_field);
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user