More refactors around assets. Trust me, we need them
This commit is contained in:
143
src/Game.zig
143
src/Game.zig
@@ -1,8 +1,9 @@
|
||||
const Game = @This();
|
||||
const std = @import("std");
|
||||
|
||||
const math = @import("math.zig");
|
||||
const glfw = @import("zglfw");
|
||||
const math = @import("math.zig");
|
||||
const shaders = @import("shaders.zig");
|
||||
const vk = @import("vulkan");
|
||||
const worldgen = @import("worldgen.zig");
|
||||
|
||||
@@ -10,7 +11,6 @@ const Blocks = @import("assets/Blocks.zig");
|
||||
const Chunk = @import("assets/Chunk.zig");
|
||||
const CommandBuffer = @import("engine/CommandBuffer.zig");
|
||||
const Engine = @import("engine/Engine.zig");
|
||||
const GenericBuffer = @import("engine/GenericBuffer.zig").GenericBuffer;
|
||||
const Iterator2 = math.Iterator2;
|
||||
const Materials = @import("assets/Materials.zig");
|
||||
const Matrix4x4 = math.Matrix4x4;
|
||||
@@ -22,87 +22,6 @@ const Textures = @import("assets/Textures.zig");
|
||||
const Vector2 = math.Vector2;
|
||||
const Vector2x8 = math.Vector2x8;
|
||||
const Vector3 = math.Vector3;
|
||||
const Vector4 = math.Vector4;
|
||||
|
||||
const PointLight = extern struct {
|
||||
positionWS: [3]f32,
|
||||
color: [3]f32,
|
||||
|
||||
pub fn init(position_ws: Vector3, color: Vector3) PointLight {
|
||||
return .{
|
||||
.positionWS = position_ws.asArray(),
|
||||
.color = color.asArray(),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
const DirectionalLight = extern struct {
|
||||
directionWS: [3]f32,
|
||||
color: [3]f32,
|
||||
|
||||
pub fn init(direction_ws: Vector3, color: Vector3) DirectionalLight {
|
||||
return .{
|
||||
.directionWS = direction_ws.asArray(),
|
||||
.color = color.asArray(),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
const GlobalUniforms = extern struct {
|
||||
matrixWStoVS: [16]f32,
|
||||
matrixVStoCS: [16]f32,
|
||||
ambientLight: [3]f32,
|
||||
|
||||
pub fn init(matrix_ws_to_vs: Matrix4x4, matrix_vs_to_cs: Matrix4x4, ambient_light: Vector3) GlobalUniforms {
|
||||
return .{
|
||||
.matrixWStoVS = matrix_ws_to_vs.asArray(),
|
||||
.matrixVStoCS = matrix_vs_to_cs.asArray(),
|
||||
.ambientLight = ambient_light.asArray(),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
pub const ObjectUniforms = extern struct {
|
||||
matrixOStoWS: [16]f32,
|
||||
matrixOStoWSNormal: [16]f32,
|
||||
|
||||
material: Materials.Id,
|
||||
|
||||
pub fn init(matrix_os_to_ws: Matrix4x4, matrix_ow_to_ws_normal: Matrix4x4, material: Materials.Id) ObjectUniforms {
|
||||
return .{
|
||||
.matrixOStoWS = matrix_os_to_ws.asArray(),
|
||||
.matrixOStoWSNormal = matrix_ow_to_ws_normal.asArray(),
|
||||
.material = material,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
const Vertex = extern struct {
|
||||
positionOS: [3]f32,
|
||||
texCoord: [2]u16,
|
||||
normalOS: [3]i8,
|
||||
tangentOS: [4]i8,
|
||||
|
||||
pub fn init(position_os: Vector3, tex_coord: Vector2, normal_os: Vector3, tangent_os: Vector4) Vertex {
|
||||
return .{
|
||||
.positionOS = position_os.asArray(),
|
||||
.texCoord = tex_coord.asArrayNorm(u16),
|
||||
.normalOS = normal_os.asArrayNorm(i8),
|
||||
.tangentOS = tangent_os.asArrayNorm(i8),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
const GlobalUniformsBuffer = GenericBuffer(GlobalUniforms, void);
|
||||
const PointLightBuffer = GenericBuffer(u32, PointLight);
|
||||
const DirectionalLightBuffer = GenericBuffer(u32, DirectionalLight);
|
||||
const ObjectUniformsBuffer = GenericBuffer(void, ObjectUniforms);
|
||||
|
||||
const VertexBuffer = GenericBuffer(void, Vertex);
|
||||
const IndexBuffer = GenericBuffer(void, u16);
|
||||
|
||||
const main_vert_spv align(4) = @embedFile("shaders/main_vert.spv").*;
|
||||
const main_frag_spv align(4) = @embedFile("shaders/main_frag.spv").*;
|
||||
|
||||
allocator: std.mem.Allocator,
|
||||
engine: *Engine,
|
||||
@@ -114,15 +33,15 @@ global_descriptor_set: vk.DescriptorSet,
|
||||
pipeline_layout: vk.PipelineLayout,
|
||||
pipeline: vk.Pipeline,
|
||||
|
||||
vertex_buffer: VertexBuffer,
|
||||
index_buffer: IndexBuffer,
|
||||
vertex_buffer: shaders.VertexBuffer,
|
||||
index_buffer: shaders.IndexBuffer,
|
||||
|
||||
global_uniforms: GlobalUniformsBuffer,
|
||||
global_uniforms: shaders.GlobalUniformsBuffer,
|
||||
global_uniforms_staging_buffer: StagingBuffer,
|
||||
global_uniforms_transfer_command_buffer: CommandBuffer,
|
||||
global_uniforms_transfer_semaphores: []vk.Semaphore,
|
||||
point_lights: PointLightBuffer,
|
||||
directional_lights: DirectionalLightBuffer,
|
||||
point_lights: shaders.PointLightBuffer,
|
||||
directional_lights: shaders.DirectionalLightBuffer,
|
||||
sampler: vk.Sampler,
|
||||
|
||||
blocks: Blocks,
|
||||
@@ -235,15 +154,15 @@ pub fn init(allocator: std.mem.Allocator, engine: *Engine, swapchain: *Swapchain
|
||||
errdefer engine.destroyPipelineLayout(pipeline_layout);
|
||||
engine.setObjectName(pipeline_layout, "PL", .{});
|
||||
|
||||
const vertex_shader = try engine.createShaderModule(.{ .code = &main_vert_spv });
|
||||
const vertex_shader = try engine.createShaderModule(.{ .code = &shaders.main_vert_spv });
|
||||
defer engine.destroyShaderModule(vertex_shader);
|
||||
engine.setObjectName(vertex_shader, "SM main_vert", .{});
|
||||
|
||||
const fragment_shader = try engine.createShaderModule(.{ .code = &main_frag_spv });
|
||||
const fragment_shader = try engine.createShaderModule(.{ .code = &shaders.main_frag_spv });
|
||||
defer engine.destroyShaderModule(fragment_shader);
|
||||
engine.setObjectName(fragment_shader, "SM main_frag", .{});
|
||||
|
||||
var vertex_buffer = try VertexBuffer.init(engine, .{
|
||||
var vertex_buffer = try shaders.VertexBuffer.init(engine, .{
|
||||
.usage = .vertex,
|
||||
.target_queue = .graphics,
|
||||
.array_capacity = 4,
|
||||
@@ -279,7 +198,7 @@ pub fn init(allocator: std.mem.Allocator, engine: *Engine, swapchain: *Swapchain
|
||||
},
|
||||
});
|
||||
|
||||
var index_buffer = try IndexBuffer.init(engine, .{
|
||||
var index_buffer = try shaders.IndexBuffer.init(engine, .{
|
||||
.usage = .index,
|
||||
.target_queue = .graphics,
|
||||
.array_capacity = 6,
|
||||
@@ -290,7 +209,7 @@ pub fn init(allocator: std.mem.Allocator, engine: *Engine, swapchain: *Swapchain
|
||||
.elements = &.{ 0, 1, 2, 2, 1, 3 },
|
||||
});
|
||||
|
||||
var global_uniforms = try GlobalUniformsBuffer.init(engine, .{
|
||||
var global_uniforms = try shaders.GlobalUniformsBuffer.init(engine, .{
|
||||
.usage = .uniform,
|
||||
.target_queue = .graphics,
|
||||
.name = "GlobalUniforms",
|
||||
@@ -298,7 +217,7 @@ pub fn init(allocator: std.mem.Allocator, engine: *Engine, swapchain: *Swapchain
|
||||
errdefer global_uniforms.deinit(engine);
|
||||
|
||||
var global_uniforms_staging_buffer = try StagingBuffer.init(engine, .{
|
||||
.capacity = @sizeOf(GlobalUniforms),
|
||||
.capacity = @sizeOf(shaders.GlobalUniforms),
|
||||
.target_queue = .graphics,
|
||||
});
|
||||
errdefer global_uniforms_staging_buffer.deinit(engine);
|
||||
@@ -314,7 +233,7 @@ pub fn init(allocator: std.mem.Allocator, engine: *Engine, swapchain: *Swapchain
|
||||
.{
|
||||
.src_offset = 0,
|
||||
.dst_offset = 0,
|
||||
.size = @sizeOf(GlobalUniforms),
|
||||
.size = @sizeOf(shaders.GlobalUniforms),
|
||||
},
|
||||
},
|
||||
);
|
||||
@@ -341,7 +260,7 @@ pub fn init(allocator: std.mem.Allocator, engine: *Engine, swapchain: *Swapchain
|
||||
allocator.free(global_uniforms_transfer_semaphores);
|
||||
}
|
||||
|
||||
var point_lights = try PointLightBuffer.init(engine, .{
|
||||
var point_lights = try shaders.PointLightBuffer.init(engine, .{
|
||||
.usage = .storage,
|
||||
.target_queue = .graphics,
|
||||
.array_capacity = max_point_lights,
|
||||
@@ -349,7 +268,7 @@ pub fn init(allocator: std.mem.Allocator, engine: *Engine, swapchain: *Swapchain
|
||||
});
|
||||
errdefer point_lights.deinit(engine);
|
||||
|
||||
var directional_lights = try DirectionalLightBuffer.init(engine, .{
|
||||
var directional_lights = try shaders.DirectionalLightBuffer.init(engine, .{
|
||||
.usage = .storage,
|
||||
.target_queue = .graphics,
|
||||
.array_capacity = max_directional_lights,
|
||||
@@ -374,7 +293,7 @@ pub fn init(allocator: std.mem.Allocator, engine: *Engine, swapchain: *Swapchain
|
||||
.vertex_binding_descriptions = &.{
|
||||
.{
|
||||
.binding = 0,
|
||||
.stride = @sizeOf(Vertex),
|
||||
.stride = @sizeOf(shaders.Vertex),
|
||||
.input_rate = .vertex,
|
||||
},
|
||||
},
|
||||
@@ -383,25 +302,25 @@ pub fn init(allocator: std.mem.Allocator, engine: *Engine, swapchain: *Swapchain
|
||||
.location = 0,
|
||||
.binding = 0,
|
||||
.format = .r32g32b32_sfloat,
|
||||
.offset = @offsetOf(Vertex, "positionOS"),
|
||||
.offset = @offsetOf(shaders.Vertex, "positionOS"),
|
||||
},
|
||||
.{
|
||||
.location = 1,
|
||||
.binding = 0,
|
||||
.format = .r16g16_unorm,
|
||||
.offset = @offsetOf(Vertex, "texCoord"),
|
||||
.offset = @offsetOf(shaders.Vertex, "texCoord"),
|
||||
},
|
||||
.{
|
||||
.location = 2,
|
||||
.binding = 0,
|
||||
.format = .r8g8b8_snorm,
|
||||
.offset = @offsetOf(Vertex, "normalOS"),
|
||||
.offset = @offsetOf(shaders.Vertex, "normalOS"),
|
||||
},
|
||||
.{
|
||||
.location = 3,
|
||||
.binding = 0,
|
||||
.format = .r8g8b8a8_snorm,
|
||||
.offset = @offsetOf(Vertex, "tangentOS"),
|
||||
.offset = @offsetOf(shaders.Vertex, "tangentOS"),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -502,19 +421,19 @@ pub fn init(allocator: std.mem.Allocator, engine: *Engine, swapchain: *Swapchain
|
||||
const global_descriptor_set = try engine.allocateDescriptorSet(.{
|
||||
.descriptor_pool = descriptor_pool,
|
||||
.set_layout = global_descriptor_set_layout,
|
||||
.variable_descriptor_count = @intCast(textures.textures.items.len),
|
||||
.variable_descriptor_count = @intCast(textures.array.items.len),
|
||||
});
|
||||
engine.setObjectName(global_descriptor_set, "DS Global", .{});
|
||||
|
||||
const block_grass = try blocks.getOrLoadFilename(engine, &materials, &textures, "Grass.json", allocator);
|
||||
const block_dirt = try blocks.getOrLoadFilename(engine, &materials, &textures, "Dirt.json", allocator);
|
||||
const block_stone = try blocks.getOrLoadFilename(engine, &materials, &textures, "Stone.json", allocator);
|
||||
const block_bedrock = try blocks.getOrLoadFilename(engine, &materials, &textures, "Bedrock.json", allocator);
|
||||
const block_grass = try blocks.getOrLoad(engine, &materials, &textures, "Grass.json", allocator);
|
||||
const block_dirt = try blocks.getOrLoad(engine, &materials, &textures, "Dirt.json", allocator);
|
||||
const block_stone = try blocks.getOrLoad(engine, &materials, &textures, "Stone.json", allocator);
|
||||
const block_bedrock = try blocks.getOrLoad(engine, &materials, &textures, "Bedrock.json", allocator);
|
||||
|
||||
// VOLATILE Load all assets before this point
|
||||
|
||||
const descriptor_images = try allocator.alloc(vk.DescriptorImageInfo, textures.textures.items.len);
|
||||
for (textures.textures.items, descriptor_images) |texture, *info| {
|
||||
const descriptor_images = try allocator.alloc(vk.DescriptorImageInfo, textures.array.items.len);
|
||||
for (textures.array.items, descriptor_images) |texture, *info| {
|
||||
info.* = .{
|
||||
.sampler = .null_handle,
|
||||
.image_view = texture.image_view,
|
||||
@@ -680,13 +599,13 @@ pub fn init(allocator: std.mem.Allocator, engine: *Engine, swapchain: *Swapchain
|
||||
}, allocator);
|
||||
}
|
||||
|
||||
const point_lights_data: []const PointLight = &.{};
|
||||
const point_lights_data: []const shaders.PointLight = &.{};
|
||||
try point_lights.write(engine, .{
|
||||
.header = @intCast(point_lights_data.len),
|
||||
.elements = point_lights_data,
|
||||
});
|
||||
|
||||
const directional_lights_data: []const DirectionalLight = &.{
|
||||
const directional_lights_data: []const shaders.DirectionalLight = &.{
|
||||
.{
|
||||
.directionWS = .{ 0, 0, -1 },
|
||||
.color = .{ 0.3, 0.3, 0.3 },
|
||||
@@ -797,7 +716,7 @@ pub fn update(self: *Game, dt: f32) void {
|
||||
|
||||
const ambient_light = Vector3.init(0.01, 0.01, 0.01);
|
||||
|
||||
const global_uniforms_data: GlobalUniforms = .{
|
||||
const global_uniforms_data: shaders.GlobalUniforms = .{
|
||||
.matrixWStoVS = matrix_ws_to_vs.asArray(),
|
||||
.matrixVStoCS = matrix_vs_to_cs.asArray(),
|
||||
.ambientLight = ambient_light.asArray(),
|
||||
|
||||
Reference in New Issue
Block a user