This commit is contained in:
2026-01-06 21:10:07 +01:00
parent bc2131f1e4
commit 4a83333116
7 changed files with 56 additions and 52 deletions

View File

@@ -303,12 +303,17 @@ pub fn raycast(self: *const Chunks, origin_sv: vm.Vector3Int, ray_sv: vm.Vector3
c.subvoxelsToVoxelSubvoxels(end_sv.z),
});
const zero = vm.epi32(0);
const one = vm.epi32(1);
const ray_positive: vm.Vector3Int = .{ .vector = @select(i32, ray_sv.vector > zero, one, zero) };
const ray_negative: vm.Vector3Int = .{ .vector = @select(i32, ray_sv.vector < zero, one, zero) };
const ray_sign: vm.Vector3Int = .sub(ray_positive, ray_negative);
const ray_positive = vm.Vector3Int.init(
@intFromBool(ray_sv.x > 0),
@intFromBool(ray_sv.y > 0),
@intFromBool(ray_sv.z > 0),
);
const ray_negative = vm.Vector3Int.init(
@intFromBool(ray_sv.x < 0),
@intFromBool(ray_sv.y < 0),
@intFromBool(ray_sv.z < 0),
);
const ray_sign = ray_positive.sub(ray_negative);
const start_vx = origin_sv.sub(ray_positive).divScalar(c.sv_per_vx).add(ray_sign);
const end_vx = end_sv.sub(ray_positive).divScalar(c.sv_per_vx);
@@ -346,33 +351,33 @@ pub fn raycast(self: *const Chunks, origin_sv: vm.Vector3Int, ray_sv: vm.Vector3
return null;
}
const oi = origin_sv.vector[i_index];
const oj = origin_sv.vector[j_index];
const ok = origin_sv.vector[k_index];
const oi = origin_sv.asArray()[i_index];
const oj = origin_sv.asArray()[j_index];
const ok = origin_sv.asArray()[k_index];
const di = ray_sv.vector[i_index];
const dj = ray_sv.vector[j_index];
const dk = ray_sv.vector[k_index];
const di = ray_sv.asArray()[i_index];
const dj = ray_sv.asArray()[j_index];
const dk = ray_sv.asArray()[k_index];
var i_vx = start_vx.vector[i_index];
var i_vx = start_vx.asArray()[i_index];
var enter_i_sv = oi;
var enter_di_sv = enter_i_sv - oi;
var enter_jdi_sv = oj * di + enter_di_sv * dj;
var enter_kdi_sv = ok * di + enter_di_sv * dk;
var enter_i_vx = @divFloor(enter_i_sv - ray_positive.vector[i_index], c.sv_per_vx);
var enter_j_vx = @divFloor(enter_jdi_sv - ray_positive.vector[j_index], c.sv_per_vx * di);
var enter_k_vx = @divFloor(enter_kdi_sv - ray_positive.vector[k_index], c.sv_per_vx * di);
var enter_i_vx = @divFloor(enter_i_sv - ray_positive.asArray()[i_index], c.sv_per_vx);
var enter_j_vx = @divFloor(enter_jdi_sv - ray_positive.asArray()[j_index], c.sv_per_vx * di);
var enter_k_vx = @divFloor(enter_kdi_sv - ray_positive.asArray()[k_index], c.sv_per_vx * di);
var exit_i_sv = c.voxelsToSubvoxels(i_vx + ray_negative.vector[i_index]);
var exit_i_sv = c.voxelsToSubvoxels(i_vx + ray_negative.asArray()[i_index]);
var exit_di_sv = exit_i_sv - oi;
var exit_jdi_sv = oj * di + exit_di_sv * dj;
var exit_kdi_sv = ok * di + exit_di_sv * dk;
var exit_i_vx = @divFloor(exit_i_sv - ray_positive.vector[i_index], c.sv_per_vx);
var exit_j_vx = @divFloor(exit_jdi_sv - ray_positive.vector[j_index], c.sv_per_vx * di);
var exit_k_vx = @divFloor(exit_kdi_sv - ray_positive.vector[k_index], c.sv_per_vx * di);
var exit_i_vx = @divFloor(exit_i_sv - ray_positive.asArray()[i_index], c.sv_per_vx);
var exit_j_vx = @divFloor(exit_jdi_sv - ray_positive.asArray()[j_index], c.sv_per_vx * di);
var exit_k_vx = @divFloor(exit_kdi_sv - ray_positive.asArray()[k_index], c.sv_per_vx * di);
std.debug.print("({d}:{X}, {d:.3}, {d:.3}) → ({d}:{X}, {d:.3}, {d:.3}) | ({d}, {d}, {d}) → ({d}, {d}, {d})\n", .{
c.subvoxelsToVoxels(.border_up, enter_i_sv),
@@ -391,7 +396,7 @@ pub fn raycast(self: *const Chunks, origin_sv: vm.Vector3Int, ray_sv: vm.Vector3
exit_k_vx,
});
while (i_vx != end_vx.vector[i_index]) : (i_vx += ray_sign.vector[i_index]) {
while (i_vx != end_vx.asArray()[i_index]) : (i_vx += ray_sign.asArray()[i_index]) {
enter_i_sv = exit_i_sv;
enter_di_sv = exit_di_sv;
enter_jdi_sv = exit_jdi_sv;
@@ -401,14 +406,14 @@ pub fn raycast(self: *const Chunks, origin_sv: vm.Vector3Int, ray_sv: vm.Vector3
enter_j_vx = exit_j_vx;
enter_k_vx = exit_k_vx;
exit_i_sv = c.voxelsToSubvoxels(i_vx + ray_positive.vector[i_index]);
exit_i_sv = c.voxelsToSubvoxels(i_vx + ray_positive.asArray()[i_index]);
exit_di_sv = exit_i_sv - oi;
exit_jdi_sv = oj * di + exit_di_sv * dj;
exit_kdi_sv = ok * di + exit_di_sv * dk;
exit_i_vx = @divFloor(exit_i_sv - ray_positive.vector[i_index], c.sv_per_vx);
exit_j_vx = @divFloor(exit_jdi_sv - ray_positive.vector[j_index], c.sv_per_vx * di);
exit_k_vx = @divFloor(exit_kdi_sv - ray_positive.vector[k_index], c.sv_per_vx * di);
exit_i_vx = @divFloor(exit_i_sv - ray_positive.asArray()[i_index], c.sv_per_vx);
exit_j_vx = @divFloor(exit_jdi_sv - ray_positive.asArray()[j_index], c.sv_per_vx * di);
exit_k_vx = @divFloor(exit_kdi_sv - ray_positive.asArray()[k_index], c.sv_per_vx * di);
std.debug.print("({d}:{X}, {d:.3}, {d:.3}) → ({d}:{X}, {d:.3}, {d:.3}) | ({d}, {d}, {d}) → ({d}, {d}, {d})\n", .{
c.subvoxelsToVoxels(.border_up, enter_i_sv),
@@ -442,9 +447,9 @@ pub fn raycast(self: *const Chunks, origin_sv: vm.Vector3Int, ray_sv: vm.Vector3
exit_jdi_sv = oj * di + exit_di_sv * dj;
exit_kdi_sv = ok * di + exit_di_sv * dk;
exit_i_vx = @divFloor(exit_i_sv - ray_positive.vector[i_index], c.sv_per_vx);
exit_j_vx = @divFloor(exit_jdi_sv - ray_positive.vector[j_index], c.sv_per_vx * di);
exit_k_vx = @divFloor(exit_kdi_sv - ray_positive.vector[k_index], c.sv_per_vx * di);
exit_i_vx = @divFloor(exit_i_sv - ray_positive.asArray()[i_index], c.sv_per_vx);
exit_j_vx = @divFloor(exit_jdi_sv - ray_positive.asArray()[j_index], c.sv_per_vx * di);
exit_k_vx = @divFloor(exit_kdi_sv - ray_positive.asArray()[k_index], c.sv_per_vx * di);
std.debug.print("({d}:{X}, {d:.3}, {d:.3}) → ({d}:{X}, {d:.3}, {d:.3}) | ({d}, {d}, {d}) → ({d}, {d}, {d})\n", .{
c.subvoxelsToVoxels(.border_up, enter_i_sv),

View File

@@ -765,11 +765,8 @@ fn render(self: *Game) !void {
@floatFromInt(extent.height),
);
const camera_position = vm.Vector3.init(
@floatFromInt(self.player.position_sv.x),
@floatFromInt(self.player.position_sv.y),
@floatFromInt(self.player.position_sv.z),
)
const camera_position = self.player.position_sv
.asFloat()
.divScalar(c.sv_per_vx)
.add(.init(0, 0, Player.camera_height_vx));
const camera_rotation = vm.Quaternion.mulQuaternion(

View File

@@ -255,7 +255,7 @@ fn loadBlock(
defer file.close();
var file_reader = file.reader(&buffer);
var json_reader: std.json.Reader = .init(temp_allocator, &file_reader.interface);
var json_reader = std.json.Reader.init(temp_allocator, &file_reader.interface);
defer json_reader.deinit();
const parsed: std.json.Parsed(DefinitionJson) = try std.json.parseFromTokenSource(DefinitionJson, temp_allocator, &json_reader, .{

View File

@@ -279,7 +279,7 @@ fn loadMaterial(self: *Materials, engine: *Engine, textures: *Textures, filename
defer file.close();
var file_reader = file.reader(&buffer);
var json_reader: std.json.Reader = .init(temp_allocator, &file_reader.interface);
var json_reader = std.json.Reader.init(temp_allocator, &file_reader.interface);
defer json_reader.deinit();
const parsed: std.json.Parsed(MaterialJson) = try std.json.parseFromTokenSource(MaterialJson, temp_allocator, &json_reader, .{

View File

@@ -23,11 +23,9 @@ pub fn init(engine: *Engine, queue_type: QueueType) !CommandBuffer {
.queue_type = queue_type,
.level = .primary,
});
const proxy: vk.CommandBufferProxy = .init(handle, engine.device.wrapper);
const allocator = engine.vk_allocator.allocator;
return .{
.proxy = proxy,
.allocator = allocator,
.proxy = .init(handle, engine.device.wrapper),
.allocator = engine.vk_allocator.allocator,
.queue_type = queue_type,
};
}

View File

@@ -232,7 +232,7 @@ pub fn init(allocator: std.mem.Allocator, maybe_window: ?*glfw.Window) !Engine {
var queue_create_info: std.ArrayList(vk.DeviceQueueCreateInfo) = .initBuffer(&queue_create_info_buffer);
const queue_allocations: QueueAllocations = blk: {
var queue_allocator: QueueAllocator = .init(instance, physical_device);
var queue_allocator = QueueAllocator.init(instance, physical_device);
const queue_families_properties = queue_allocator.queueFamiliesProperties();
const graphics_queue_family = findGraphicsQueueFamily(queue_families_properties) orelse return error.NoGraphicsQueue;
@@ -347,21 +347,21 @@ pub fn init(allocator: std.mem.Allocator, maybe_window: ?*glfw.Window) !Engine {
}, &vk_allocator.interface);
errdefer device.destroyCommandPool(transfer_command_pool, &vk_allocator.interface);
const graphics_queue: Queue = .init(
const graphics_queue = Queue.init(
device.getDeviceQueue(
queue_allocations.graphics_queue.family,
queue_allocations.graphics_queue.index,
),
queue_allocations.graphics_queue,
);
const compute_queue: Queue = .init(
const compute_queue = Queue.init(
device.getDeviceQueue(
queue_allocations.compute_queue.family,
queue_allocations.compute_queue.index,
),
queue_allocations.compute_queue,
);
const transfer_queue: Queue = .init(
const transfer_queue = Queue.init(
device.getDeviceQueue(
queue_allocations.transfer_queue.family,
queue_allocations.transfer_queue.index,
@@ -692,6 +692,10 @@ fn findPresentationQueueFamily(queue_families_properties: []const vk.QueueFamily
return null;
}
fn makeArena(self: *const Engine) std.heap.ArenaAllocator {
return .init(self.vk_allocator.allocator);
}
fn resolveCommandPool(self: *const Engine, queue_type: QueueType) vk.CommandPool {
return switch (queue_type) {
.graphics => self.graphics_command_pool,
@@ -975,7 +979,7 @@ pub fn bindImageMemory(self: *Engine, image: vk.Image, memory: vk.DeviceMemory,
}
pub fn createBuffer(self: *Engine, create_info: BufferCreateInfo) !vk.Buffer {
var arena: std.heap.ArenaAllocator = .init(self.vk_allocator.allocator);
var arena = self.makeArena();
defer arena.deinit();
const allocator = arena.allocator();
@@ -998,7 +1002,7 @@ pub fn createBuffer(self: *Engine, create_info: BufferCreateInfo) !vk.Buffer {
}
pub fn createDescriptorSetLayout(self: *Engine, create_info: DescriptorSetLayoutCreateInfo) !vk.DescriptorSetLayout {
var arena: std.heap.ArenaAllocator = .init(self.vk_allocator.allocator);
var arena = self.makeArena();
defer arena.deinit();
const allocator = arena.allocator();
@@ -1074,7 +1078,7 @@ pub fn createFramebuffer(self: *Engine, create_info: FramebufferCreateInfo) !vk.
}
pub fn createGraphicsPipeline(self: *Engine, create_info: GraphicsPipelineCreateInfo) !vk.Pipeline {
var arena: std.heap.ArenaAllocator = .init(self.vk_allocator.allocator);
var arena = self.makeArena();
defer arena.deinit();
const allocator = arena.allocator();
@@ -1153,7 +1157,7 @@ pub fn createGraphicsPipeline(self: *Engine, create_info: GraphicsPipelineCreate
}
pub fn createImage(self: *Engine, create_info: ImageCreateInfo) !vk.Image {
var arena: std.heap.ArenaAllocator = .init(self.vk_allocator.allocator);
var arena = self.makeArena();
defer arena.deinit();
const allocator = arena.allocator();
@@ -1206,7 +1210,7 @@ pub fn createPipelineLayout(self: *Engine, create_info: PipelineLayoutCreateInfo
}
pub fn createRenderPass(self: *Engine, create_info: RenderPassCreateInfo) !vk.RenderPass {
var arena: std.heap.ArenaAllocator = .init(self.vk_allocator.allocator);
var arena = self.makeArena();
defer arena.deinit();
const allocator = arena.allocator();
@@ -1258,7 +1262,7 @@ pub fn createShaderModule(self: *Engine, create_info: ShaderModuleCreateInfo) !v
}
pub fn createSwapchain(self: *Engine, create_info: SwapchainCreateInfo) !vk.SwapchainKHR {
var arena: std.heap.ArenaAllocator = .init(self.vk_allocator.allocator);
var arena = self.makeArena();
defer arena.deinit();
const allocator = arena.allocator();
@@ -1402,7 +1406,7 @@ pub fn unmapMemory(self: *Engine, memory: vk.DeviceMemory) void {
}
pub fn updateDescriptorSets(self: *Engine, update_info: DescriptorSetsUpdateInfo) !void {
var arena: std.heap.ArenaAllocator = .init(self.vk_allocator.allocator);
var arena = self.makeArena();
defer arena.deinit();
const allocator = arena.allocator();

View File

@@ -66,7 +66,7 @@ fn allocationFunction(
) callconv(vk.vulkan_call_conv) ?*anyopaque {
const self: *VkAllocator = @ptrCast(@alignCast(p_user_data.?));
const desired_alignment: std.mem.Alignment = .fromByteUnits(alignment);
const desired_alignment = std.mem.Alignment.fromByteUnits(alignment);
std.debug.assert(std.mem.Alignment.compare(actual_alignment, .gte, desired_alignment));
self.mutex.lock();
@@ -91,7 +91,7 @@ fn reallocationFunction(
) callconv(vk.vulkan_call_conv) ?*anyopaque {
const self: *VkAllocator = @ptrCast(@alignCast(p_user_data.?));
const desired_alignment: std.mem.Alignment = .fromByteUnits(alignment);
const desired_alignment = std.mem.Alignment.fromByteUnits(alignment);
std.debug.assert(std.mem.Alignment.compare(actual_alignment, .gte, desired_alignment));
self.mutex.lock();