It works!
This commit is contained in:
128
src/Game.zig
128
src/Game.zig
@@ -104,7 +104,7 @@ swapchain: *Swapchain,
|
||||
global_descriptor_set_layout: vk.DescriptorSetLayout,
|
||||
per_batch_descriptor_set_layout: vk.DescriptorSetLayout,
|
||||
descriptor_pool: vk.DescriptorPool,
|
||||
/// [0] GLOBAL, [1] PER OBJECT
|
||||
/// [0] GLOBAL, [1] PER BATCH
|
||||
descriptor_sets: [2]vk.DescriptorSet,
|
||||
pipeline_layout: vk.PipelineLayout,
|
||||
pipeline: vk.Pipeline,
|
||||
@@ -117,6 +117,7 @@ point_lights: PointLightBuffer,
|
||||
directional_lights: DirectionalLightBuffer,
|
||||
object_uniforms: ObjectUniformsBuffer,
|
||||
sampler: vk.Sampler,
|
||||
object_count: u32,
|
||||
|
||||
materials: Materials,
|
||||
textures: Textures,
|
||||
@@ -254,25 +255,25 @@ pub fn init(allocator: std.mem.Allocator, engine: *Engine, swapchain: *Swapchain
|
||||
.init(
|
||||
.init(-0.5, -0.5, 0),
|
||||
.init(0, 1),
|
||||
.init(1, 0, 0),
|
||||
.init(0, 0, 1),
|
||||
.init(1, 0, 0, -1),
|
||||
),
|
||||
.init(
|
||||
.init(0.5, -0.5, 0),
|
||||
.init(1, 1),
|
||||
.init(1, 0, 0),
|
||||
.init(0, 0, 1),
|
||||
.init(1, 0, 0, -1),
|
||||
),
|
||||
.init(
|
||||
.init(-0.5, 0.5, 0),
|
||||
.init(0, 0),
|
||||
.init(1, 0, 0),
|
||||
.init(0, 0, 1),
|
||||
.init(1, 0, 0, -1),
|
||||
),
|
||||
.init(
|
||||
.init(0.5, 0.5, 0),
|
||||
.init(1, 0),
|
||||
.init(1, 0, 0),
|
||||
.init(0, 0, 1),
|
||||
.init(1, 0, 0, -1),
|
||||
),
|
||||
},
|
||||
@@ -549,6 +550,26 @@ pub fn init(allocator: std.mem.Allocator, engine: *Engine, swapchain: *Swapchain
|
||||
},
|
||||
});
|
||||
|
||||
const object_count: u32 = blk: {
|
||||
var objects: std.ArrayList(ObjectUniforms) = try .initCapacity(allocator, 289);
|
||||
defer objects.deinit(allocator);
|
||||
|
||||
var it = Iterator3.init(.init(-8, -8, 0), .init(8, 8, 0), .one);
|
||||
while (it.next()) |pos| {
|
||||
const material: Materials.Id = @enumFromInt(engine.random.uintLessThan(u16, @intFromEnum(materials.next_id)));
|
||||
const matrix_os_to_ws = Matrix4x4.initTranslation(pos);
|
||||
const matrix_os_to_ws_normal = Matrix4x4.inverseTransposeAffine(matrix_os_to_ws);
|
||||
objects.appendAssumeCapacity(.{
|
||||
.matrixOStoWS = matrix_os_to_ws.asArray(),
|
||||
.matrixOStoWSNormal = matrix_os_to_ws_normal.asArray(),
|
||||
.material = material,
|
||||
});
|
||||
}
|
||||
try object_uniforms.write(engine, .{ .elements = objects.items });
|
||||
|
||||
break :blk @intCast(objects.items.len);
|
||||
};
|
||||
|
||||
return .{
|
||||
.allocator = allocator,
|
||||
.engine = engine,
|
||||
@@ -568,6 +589,7 @@ pub fn init(allocator: std.mem.Allocator, engine: *Engine, swapchain: *Swapchain
|
||||
.directional_lights = directional_lights,
|
||||
.object_uniforms = object_uniforms,
|
||||
.sampler = sampler,
|
||||
.object_count = object_count,
|
||||
|
||||
.materials = materials,
|
||||
.textures = textures,
|
||||
@@ -623,14 +645,14 @@ pub fn update(self: *Game, dt: f32) void {
|
||||
|
||||
// zig fmt: off
|
||||
const matrix_vs_to_cs = Matrix4x4.init(
|
||||
camera_xscale, 0, 0, 0,
|
||||
0, 0, camera_near_plane, 1,
|
||||
0, camera_yscale, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
camera_xscale, 0, 0, 0,
|
||||
0, 0, camera_near_plane, 1,
|
||||
0, -camera_yscale, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
);
|
||||
// zig fmt: on
|
||||
|
||||
const ambient_light = Vector3.init(0.3, 0.3, 0.3);
|
||||
const ambient_light = Vector3.init(0.1, 0.1, 0.1);
|
||||
|
||||
self.global_uniforms.write(self.engine, .{
|
||||
.header = .{
|
||||
@@ -649,19 +671,19 @@ pub fn update(self: *Game, dt: f32) void {
|
||||
.color = .{ 10, 10, 10 },
|
||||
},
|
||||
.{
|
||||
.positionWS = .{ -10, 10, 1 },
|
||||
.positionWS = .{ -7, 7, 1 },
|
||||
.color = .{ 5, 0, 0 },
|
||||
},
|
||||
.{
|
||||
.positionWS = .{ 10, 10, 1 },
|
||||
.positionWS = .{ 7, 7, 1 },
|
||||
.color = .{ 0, 0, 5 },
|
||||
},
|
||||
.{
|
||||
.positionWS = .{ -10, -10, 1 },
|
||||
.positionWS = .{ -7, -7, 1 },
|
||||
.color = .{ 0, 5, 0 },
|
||||
},
|
||||
.{
|
||||
.positionWS = .{ 10, -10, 1 },
|
||||
.positionWS = .{ 7, -7, 1 },
|
||||
.color = .{ 5, 5, 0 },
|
||||
},
|
||||
};
|
||||
@@ -747,51 +769,10 @@ fn render(self: *Game) !void {
|
||||
const engine = self.engine;
|
||||
const extent = self.swapchain.extent;
|
||||
|
||||
const object_count: u32 = blk: {
|
||||
var objects: std.ArrayList(ObjectUniforms) = try .initCapacity(self.engine.vk_allocator.allocator, 289);
|
||||
defer objects.deinit(self.engine.vk_allocator.allocator);
|
||||
|
||||
var it = Iterator3.init(.init(-8, -8, 0), .init(8, 8, 0), .one);
|
||||
var material: u16 = 0;
|
||||
while (it.next()) |pos| : (material = (material + 1) % @intFromEnum(self.materials.next_id)) {
|
||||
const matrix_os_to_ws = Matrix4x4.initTranslation(pos);
|
||||
const matrix_os_to_ws_normal = Matrix4x4.inverseTransposeAffine(matrix_os_to_ws);
|
||||
objects.appendAssumeCapacity(.{
|
||||
.matrixOStoWS = matrix_os_to_ws.asArray(),
|
||||
.matrixOStoWSNormal = matrix_os_to_ws_normal.asArray(),
|
||||
.material = @enumFromInt(material),
|
||||
});
|
||||
}
|
||||
try self.object_uniforms.write(self.engine, .{ .elements = objects.items });
|
||||
|
||||
break :blk @intCast(objects.items.len);
|
||||
};
|
||||
|
||||
const command_buffer = try engine.allocateGraphicsCommandBuffer();
|
||||
// NOTE Do not free command buffer yet
|
||||
|
||||
try command_buffer.beginCommandBuffer(&.{ .flags = .{ .one_time_submit_bit = true } });
|
||||
|
||||
const viewports = [_]vk.Viewport{
|
||||
.{
|
||||
.x = 0,
|
||||
.y = 0,
|
||||
.width = @floatFromInt(extent.width),
|
||||
.height = @floatFromInt(extent.height),
|
||||
.min_depth = 0,
|
||||
.max_depth = 1,
|
||||
},
|
||||
};
|
||||
command_buffer.setViewport(0, viewports.len, &viewports);
|
||||
|
||||
const scissors = [_]vk.Rect2D{
|
||||
.{
|
||||
.offset = .{ .x = 0, .y = 0 },
|
||||
.extent = extent,
|
||||
},
|
||||
};
|
||||
command_buffer.setScissor(0, scissors.len, &scissors);
|
||||
|
||||
{
|
||||
const clear_values = [_]vk.ClearValue{
|
||||
.{ .color = .{ .float_32 = .{ 0, 0, 0, 1 } } },
|
||||
@@ -799,7 +780,7 @@ fn render(self: *Game) !void {
|
||||
|
||||
command_buffer.beginRenderPass(&.{
|
||||
.render_pass = self.swapchain.render_pass,
|
||||
.framebuffer = self.swapchain.swapchain_images[0].framebuffer,
|
||||
.framebuffer = self.swapchain.swapchain_images[self.swapchain.image_index].framebuffer,
|
||||
.render_area = .{
|
||||
.offset = .{ .x = 0, .y = 0 },
|
||||
.extent = extent,
|
||||
@@ -809,25 +790,36 @@ fn render(self: *Game) !void {
|
||||
}, .@"inline");
|
||||
defer command_buffer.endRenderPass();
|
||||
|
||||
const viewports = [_]vk.Viewport{
|
||||
.{
|
||||
.x = 0,
|
||||
.y = 0,
|
||||
.width = @floatFromInt(extent.width),
|
||||
.height = @floatFromInt(extent.height),
|
||||
.min_depth = 0,
|
||||
.max_depth = 1,
|
||||
},
|
||||
};
|
||||
command_buffer.setViewport(0, viewports.len, &viewports);
|
||||
|
||||
const scissors = [_]vk.Rect2D{
|
||||
.{
|
||||
.offset = .{ .x = 0, .y = 0 },
|
||||
.extent = extent,
|
||||
},
|
||||
};
|
||||
command_buffer.setScissor(0, scissors.len, &scissors);
|
||||
|
||||
command_buffer.bindPipeline(.graphics, self.pipeline);
|
||||
|
||||
command_buffer.bindVertexBuffers(0, 1, @ptrCast(&self.vertex_buffer.buffer), &.{0});
|
||||
command_buffer.bindIndexBuffer(self.index_buffer.buffer, 0, .uint16);
|
||||
command_buffer.bindDescriptorSets(.graphics, self.pipeline_layout, 0, self.descriptor_sets.len, &self.descriptor_sets, 0, null);
|
||||
|
||||
command_buffer.bindDescriptorSets(
|
||||
.graphics,
|
||||
self.pipeline_layout,
|
||||
0,
|
||||
self.descriptor_sets.len,
|
||||
&self.descriptor_sets,
|
||||
0,
|
||||
null,
|
||||
);
|
||||
|
||||
command_buffer.drawIndexed(@intCast(self.index_buffer.array_capacity), object_count, 0, 0, 0);
|
||||
command_buffer.drawIndexed(@intCast(self.index_buffer.array_capacity), self.object_count, 0, 0, 0);
|
||||
}
|
||||
|
||||
try command_buffer.endCommandBuffer();
|
||||
|
||||
const res = try self.swapchain.present(self.engine, command_buffer.handle);
|
||||
|
||||
_ = res;
|
||||
|
||||
Reference in New Issue
Block a user