Skip to content

Commit

Permalink
Audit usage of reference counting, making it thread safe in a (pretty…
Browse files Browse the repository at this point in the history
… rare) edge case.

Also uses a more consistent naming convention when reference counting is involved.

Fixes #332
Potentially fixes #329 (I can't reproduce it anymore)
Discovers #338
  • Loading branch information
IntegratedQuantum committed Apr 30, 2024
1 parent 1a6454a commit 3e0c666
Show file tree
Hide file tree
Showing 11 changed files with 235 additions and 165 deletions.
2 changes: 1 addition & 1 deletion src/audio.zig
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fn findMusic(musicId: []const u8) ?[]f32 {
{
taskMutex.lock();
defer taskMutex.unlock();
if(musicCache.find(AudioData{.musicId = musicId})) |musicData| {
if(musicCache.find(AudioData{.musicId = musicId}, null)) |musicData| {
return musicData.data;
}
for(activeTasks.items) |taskFileName| {
Expand Down
2 changes: 1 addition & 1 deletion src/itemdrop.zig
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ pub const ItemDropRenderer = struct {

fn getModelIndex(item: items.Item) u31 {
const compareObject = ItemVoxelModel{.item = item};
return voxelModels.findOrCreate(compareObject, ItemVoxelModel.init).index;
return voxelModels.findOrCreate(compareObject, ItemVoxelModel.init, null).index;
}

pub fn renderItemDrops(projMatrix: Mat4f, ambientLight: Vec3f, playerPos: Vec3d, time: u32) void {
Expand Down
10 changes: 5 additions & 5 deletions src/renderer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ pub fn renderWorld(world: *World, ambientLight: Vec3f, skyColor: Vec3f, playerPo

worldFrameBuffer.bindTexture(c.GL_TEXTURE3);

const playerBlock = mesh_storage.getBlockFromAnyLodFromRenderThread(@intFromFloat(@floor(playerPos[0])), @intFromFloat(@floor(playerPos[1])), @intFromFloat(@floor(playerPos[2])));
const playerBlock = mesh_storage.getBlockFromAnyLod(@intFromFloat(@floor(playerPos[0])), @intFromFloat(@floor(playerPos[1])), @intFromFloat(@floor(playerPos[2])));

if(settings.bloom) {
Bloom.render(lastWidth, lastHeight, playerBlock);
Expand Down Expand Up @@ -695,7 +695,7 @@ pub const MeshSelection = struct {
selectedBlockPos = null;

while(total_tMax < closestDistance) {
const block = mesh_storage.getBlockFromRenderThread(voxelPos[0], voxelPos[1], voxelPos[2]) orelse break;
const block = mesh_storage.getBlock(voxelPos[0], voxelPos[1], voxelPos[2]) orelse break;
if(block.typ != 0) {
if(block.blockClass() != .fluid) { // TODO: Buckets could select fluids
const relativePlayerPos: Vec3f = @floatCast(pos - @as(Vec3d, @floatFromInt(voxelPos)));
Expand Down Expand Up @@ -737,7 +737,7 @@ pub const MeshSelection = struct {

pub fn placeBlock(inventoryStack: *main.items.ItemStack) void {
if(selectedBlockPos) |selectedPos| {
var block = mesh_storage.getBlockFromRenderThread(selectedPos[0], selectedPos[1], selectedPos[2]) orelse return;
var block = mesh_storage.getBlock(selectedPos[0], selectedPos[1], selectedPos[2]) orelse return;
if(inventoryStack.item) |item| {
switch(item) {
.baseItem => |baseItem| {
Expand All @@ -758,7 +758,7 @@ pub const MeshSelection = struct {
const neighborPos = posBeforeBlock;
neighborDir = selectedPos - posBeforeBlock;
const relPos: Vec3f = @floatCast(lastPos - @as(Vec3d, @floatFromInt(neighborPos)));
block = mesh_storage.getBlockFromRenderThread(neighborPos[0], neighborPos[1], neighborPos[2]) orelse return;
block = mesh_storage.getBlock(neighborPos[0], neighborPos[1], neighborPos[2]) orelse return;
if(block.typ == itemBlock) {
if(rotationMode.generateData(main.game.world.?, neighborPos, relPos, lastDir, neighborDir, &block, false)) {
// TODO: world.updateBlock(bi.x, bi.y, bi.z, block.data); (→ Sending it over the network)
Expand Down Expand Up @@ -790,7 +790,7 @@ pub const MeshSelection = struct {

pub fn breakBlock(inventoryStack: *main.items.ItemStack) void {
if(selectedBlockPos) |selectedPos| {
var block = mesh_storage.getBlockFromRenderThread(selectedPos[0], selectedPos[1], selectedPos[2]) orelse return;
var block = mesh_storage.getBlock(selectedPos[0], selectedPos[1], selectedPos[2]) orelse return;
// TODO: Breaking animation and tools.
if(inventoryStack.item) |item| {
switch(item) {
Expand Down
Loading

0 comments on commit 3e0c666

Please sign in to comment.