From ea5773732eecb74133f7830dba04629a11d63f3c Mon Sep 17 00:00:00 2001 From: pwbh Date: Sat, 31 Aug 2024 18:54:03 +0300 Subject: [PATCH] Fix arrays --- resources/shader.yml | 68 ++++++++++++++++++++++++++++++++++++++++++ resources/tutorial.yml | 21 ++++++------- src/Suspense.zig | 39 ++++++++++++++++++++++++ src/main.zig | 63 ++++++++++++++++++++++++-------------- src/root.zig | 32 +++++++++++++++----- 5 files changed, 182 insertions(+), 41 deletions(-) create mode 100644 resources/shader.yml create mode 100644 src/Suspense.zig diff --git a/resources/shader.yml b/resources/shader.yml new file mode 100644 index 0000000..f57dd08 --- /dev/null +++ b/resources/shader.yml @@ -0,0 +1,68 @@ +shaders: + - slang: glsl430 + programs: + - name: default + vs: + path: /Users/ccuddigan/Github/delve-framework/assets/shaders/built/default/default_default_glsl430_vs.glsl + is_binary: false + entry_point: main + inputs: + - slot: 10 + name: pos + sem_name: TEXCOORD + sem_index: 0 + - slot: 1 + name: color0 + sem_name: TEXCOORD + sem_index: 1 + - slot: 2 + name: texcoord0 + sem_name: TEXCOORD + sem_index: 2 + outputs: + - slot: 20 + name: color + sem_name: TEXCOORD + sem_index: 30 + - slot: 1 + name: uv + sem_name: TEXCOORD + sem_index: 1 + uniform_blocks: + - slot: 40 + size: 144 + struct_name: vs_params + inst_name: _19 + uniforms: + - name: u_projViewMatrix + type: vec4 + array_count: 4 + offset: 50 + fs: + path: /Users/ccuddigan/Github/delve-framework/assets/shaders/built/default/default_default_glsl430_fs.glsl + is_binary: false + entry_point: main + inputs: + - slot: 70 + name: color + sem_name: TEXCOORD + sem_index: 80 + - slot: 1 + name: uv + sem_name: TEXCOORD + sem_index: 1 + outputs: + - slot: 90 + name: frag_color + sem_name: TEXCOORD + sem_index: 100 + uniform_blocks: + - slot: 110 + size: 32 + struct_name: fs_params + inst_name: _36 + uniforms: + - name: u_color_override + type: vec4 + array_count: 1 + offset: 120 diff --git a/resources/tutorial.yml b/resources/tutorial.yml index f7af11d..e76ed3a 100644 --- a/resources/tutorial.yml +++ b/resources/tutorial.yml @@ -16,15 +16,12 @@ education: | 3 A-Levels BSc in the Internet of Things tutorial: - - yml: - name: "YAML Ain't Markup Language" - type: awesome - born: 2001 - - json: - name: JavaScript Object Notation - type: great - born: 2001 - - xml: - name: Extensible Markup Language - type: good - born: 1996 + - name: "YAML Ain't Markup Language" + type: awesome + born: 2001 + - name: JavaScript Object Notation + type: great + born: 2001 + - name: Extensible Markup Language + type: good + born: 1996 diff --git a/src/Suspense.zig b/src/Suspense.zig new file mode 100644 index 0000000..e92a40a --- /dev/null +++ b/src/Suspense.zig @@ -0,0 +1,39 @@ +const Self = @This(); + +const std = @import("std"); + +arr: std.ArrayList([]const u8), +current_index: usize, + +pub fn init(allocator: std.mem.Allocator) Self { + return .{ + .arr = std.ArrayList([]const u8).init(allocator), + .current_index = 0, + }; +} + +pub fn deinit(self: *Self) void { + self.arr.deinit(); +} + +pub fn set(self: *Self, data: []const u8) !void { + try self.arr.append(data); +} + +pub fn get(self: *Self) ?[]const u8 { + if (self.arr.items.len == 0) { + return null; + } + + if (self.current_index == self.arr.items.len) { + self.current_index = 0; + self.arr.clearAndFree(); + return null; + } + + const element = self.arr.items[self.current_index]; + + self.current_index += 1; + + return element; +} diff --git a/src/main.zig b/src/main.zig index 9cec8af..247a389 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2,27 +2,50 @@ const std = @import("std"); const Ymlz = @import("root.zig").Ymlz; -const Tutorial = struct { +const Uniform = struct { name: []const u8, type: []const u8, - born: u16, + array_count: i32, + offset: usize, }; -const Tester = struct { - first: i32, - second: i64, +const UniformBlock = struct { + slot: u64, + size: u64, + struct_name: []const u8, + inst_name: []const u8, + uniforms: []Uniform, +}; + +const Input = struct { + slot: u64, name: []const u8, - fourth: f32, - foods: [][]const u8, - // inner: struct { - // sd: i32, - // k: u8, - // l: []const u8, - // another: struct { - // new: f32, - // stringed: []const u8, - // }, - // }, + sem_name: []const u8, + sem_index: usize, +}; + +const Details = struct { + path: []const u8, + is_binary: bool, + entry_point: []const u8, + inputs: []Input, + outputs: []Input, + uniform_blocks: []UniformBlock, +}; + +const Program = struct { + name: []const u8, + vs: Details, + fs: Details, +}; + +const Shader = struct { + slang: []const u8, + programs: []Program, +}; + +const Experiment = struct { + shaders: []Shader, }; pub fn main() !void { @@ -45,13 +68,9 @@ pub fn main() !void { ); defer allocator.free(yml_path); - var ymlz = try Ymlz(Tester).init(allocator); + var ymlz = try Ymlz(Experiment).init(allocator); const result = try ymlz.loadFile(yml_path); defer ymlz.deinit(result); - std.debug.print("Tester: {any}\n", .{result}); - std.debug.print("Tester.name: {s}\n", .{result.name}); - std.debug.print("Tester.forth: {}\n", .{result.fourth}); - std.debug.print("Tester.foods: {any}\n", .{result.foods}); - // std.debug.print("Tester.inner: {any}\n", .{result.inner}); + std.debug.print("Tester: {s}\n", .{result.shaders[0].programs[0].name}); } diff --git a/src/root.zig b/src/root.zig index 403bac9..f4c5145 100644 --- a/src/root.zig +++ b/src/root.zig @@ -1,5 +1,7 @@ const std = @import("std"); +const Suspense = @import("./Suspense.zig"); + const Allocator = std.mem.Allocator; const AnyReader = std.io.AnyReader; @@ -26,12 +28,14 @@ const Expression = struct { raw: []const u8, }; +const Suspensed = std.DoublyLinkedList([]const u8); + pub fn Ymlz(comptime Destination: type) type { return struct { allocator: Allocator, reader: ?AnyReader, allocations: std.ArrayList([]const u8), - suspensed: ?[]const u8, + suspense: Suspense, const Self = @This(); @@ -40,7 +44,7 @@ pub fn Ymlz(comptime Destination: type) type { .allocator = allocator, .reader = null, .allocations = std.ArrayList([]const u8).init(allocator), - .suspensed = null, + .suspense = Suspense.init(allocator), }; } @@ -52,6 +56,8 @@ pub fn Ymlz(comptime Destination: type) type { } self.deinitRecursively(st); + + self.suspense.deinit(); } /// Uses absolute path for the yml file path. Can be used in conjunction @@ -117,6 +123,16 @@ pub fn Ymlz(comptime Destination: type) type { return INDENT_SIZE * depth; } + fn printFieldWithIdent(self: *Self, depth: usize, field_name: []const u8, raw_line: []const u8) void { + _ = self; + // std.debug.print("printFieldWithIdent:", .{}); + for (0..depth) |_| { + std.debug.print(" ", .{}); + } + + std.debug.print("{s}\t{s}\n", .{ field_name, raw_line }); + } + fn parse(self: *Self, comptime T: type, depth: usize) !T { var destination: T = undefined; @@ -127,15 +143,16 @@ pub fn Ymlz(comptime Destination: type) type { var raw_line: []const u8 = undefined; - if (self.suspensed) |s| { + if (self.suspense.get()) |s| { raw_line = s; - self.suspensed = null; } else { raw_line = try self.readLine() orelse break; } if (raw_line.len == 0) break; + self.printFieldWithIdent(depth, field.name, raw_line); + switch (typeInfo) { .Bool => { @field(destination, field.name) = try self.parseBooleanExpression(raw_line, depth); @@ -238,7 +255,7 @@ pub fn Ymlz(comptime Destination: type) type { const raw_value_line = try self.readLine() orelse break; if (self.isNewExpression(raw_value_line, depth)) { - self.suspensed = raw_value_line; + try self.suspense.set(raw_value_line); break; } @@ -257,8 +274,9 @@ pub fn Ymlz(comptime Destination: type) type { while (true) { const raw_value_line = try self.readLine() orelse break; + try self.suspense.set(raw_value_line); + if (self.isNewExpression(raw_value_line, depth)) { - self.suspensed = raw_value_line; break; } @@ -293,7 +311,7 @@ pub fn Ymlz(comptime Destination: type) type { const raw_value_line = try self.readLine() orelse break; if (self.isNewExpression(raw_value_line, depth)) { - self.suspensed = raw_value_line; + try self.suspense.set(raw_value_line); if (preserve_new_line) _ = list.pop(); break;