Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Zylann committed Jun 9, 2024
1 parent c94f91b commit 452f538
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 44 deletions.
30 changes: 21 additions & 9 deletions edition/funcs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,20 @@ void copy_from_chunked_storage( //
dst_buffer.set_channel_depth(channel, src_buffer->get_channel_depth(channel));
// Note: copy_from takes care of clamping the area if it's on an edge
dst_buffer.copy_channel_from(
*src_buffer, min_pos - src_block_origin, src_buffer->get_size(), Vector3i(), channel);
*src_buffer, min_pos - src_block_origin, src_buffer->get_size(), Vector3i(), channel
);
}

} else {
for (const uint8_t channel : channels) {
// For now, inexistent blocks default to hardcoded defaults, corresponding to "empty space".
// If we want to change this, we may have to add an API for that.
dst_buffer.fill_area(VoxelBuffer::get_default_value_static(channel), src_block_origin - min_pos,
src_block_origin - min_pos + block_size_v, channel);
dst_buffer.fill_area(
VoxelBuffer::get_default_value_static(channel),
src_block_origin - min_pos,
src_block_origin - min_pos + block_size_v,
channel
);
}
}
}
Expand Down Expand Up @@ -89,7 +94,8 @@ void paste_to_chunked_storage( //

if (use_mask) {
paste_src_masked(
to_span(channels), src_buffer, mask_channel, mask_value, *dst_buffer, dst_base_pos, true);
to_span(channels), src_buffer, mask_channel, mask_value, *dst_buffer, dst_base_pos, true
);

} else {
paste(to_span(channels), src_buffer, *dst_buffer, dst_base_pos, true);
Expand Down Expand Up @@ -118,8 +124,11 @@ bool indices_to_bitarray_u16(Span<const int32_t> indices, DynamicBitset &bitarra
const int32_t max_supported_value = 65535;
// Validate
for (const int32_t i : indices) {
ZN_ASSERT_RETURN_V_MSG(i >= 0 && i <= max_supported_value, false,
format("Index {} is out of supported range 0..{}", i, max_supported_value));
ZN_ASSERT_RETURN_V_MSG(
i >= 0 && i <= max_supported_value,
false,
format("Index {} is out of supported range 0..{}", i, max_supported_value)
);
return false;
}
#endif
Expand All @@ -146,12 +155,14 @@ Box3i get_round_cone_int_bounds(Vector3 p0, Vector3 p1, float r0, float r1) {
const Vector3 minp( //
math::min(p0.x - r0, p1.x - r1), //
math::min(p0.y - r0, p1.y - r1), //
math::min(p0.z - r0, p1.z - r1));
math::min(p0.z - r0, p1.z - r1)
);

const Vector3 maxp( //
math::max(p0.x + r0, p1.x + r1), //
math::max(p0.y + r0, p1.y + r1), //
math::max(p0.z + r0, p1.z + r1));
math::max(p0.z + r0, p1.z + r1)
);

return Box3i::from_min_max(to_vec3i(math::floor(minp)), to_vec3i(math::ceil(maxp)));
}
Expand Down Expand Up @@ -282,7 +293,8 @@ void box_blur(const VoxelBuffer &src, VoxelBuffer &dst, int radius, Vector3f sph
for (dst_pos.y = 1; dst_pos.y < tmp_size.y; ++dst_pos.y) {
// Look 2*radius ahead because we sample from a buffer that's also bigger than tmp in Y
const float sd = src.get_voxel_f(
Vector3i(dst_pos.x, dst_pos.y + radius * 2, dst_pos.z), VoxelBuffer::CHANNEL_SDF);
Vector3i(dst_pos.x, dst_pos.y + radius * 2, dst_pos.z), VoxelBuffer::CHANNEL_SDF
);
// Remove sample exiting the window
sd_sum -= ring_buffer[rbr];
// Add sample entering the window
Expand Down
84 changes: 61 additions & 23 deletions edition/funcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ inline float interpolate_trilinear(Span<const float> grid, const Vector3i res, c
const unsigned int i111 = i110 + n001;

return math::interpolate_trilinear(
grid[i000], grid[i100], grid[i101], grid[i001], grid[i010], grid[i110], grid[i111], grid[i011], pf);
grid[i000], grid[i100], grid[i101], grid[i001], grid[i010], grid[i110], grid[i111], grid[i011], pf
);
}

template <typename Volume_F>
Expand Down Expand Up @@ -194,7 +195,11 @@ void paste_to_chunked_storage_masked_writable_list( //

if (dst_writable_values.size() == 1) {
const uint64_t dst_mask_value = dst_writable_values[0];
paste_to_chunked_storage_tp(src, min_pos, block_size_po2, channels_mask, //
paste_to_chunked_storage_tp(
src,
min_pos,
block_size_po2,
channels_mask, //
get_block_func, //
SrcMasked_DstWritableValue{ src_mask_channel, dst_mask_channel, src_mask_value, dst_mask_value } //
);
Expand All @@ -203,7 +208,11 @@ void paste_to_chunked_storage_masked_writable_list( //
// TODO Candidate for TempAllocator
DynamicBitset bitarray;
indices_to_bitarray_u16(dst_writable_values, bitarray);
paste_to_chunked_storage_tp(src, min_pos, block_size_po2, channels_mask, //
paste_to_chunked_storage_tp(
src,
min_pos,
block_size_po2,
channels_mask, //
get_block_func, //
SrcMasked_DstWritableBitArray{ src_mask_channel, dst_mask_channel, src_mask_value, bitarray } //
);
Expand All @@ -227,8 +236,10 @@ struct SdfOperation16bit {
Op op;
Shape shape;
inline int16_t operator()(Vector3i pos, int16_t sdf) const {
return snorm_to_s16(op(s16_to_snorm(sdf) * constants::QUANTIZED_SDF_16_BITS_SCALE_INV, shape(Vector3(pos))) *
constants::QUANTIZED_SDF_16_BITS_SCALE);
return snorm_to_s16(
op(s16_to_snorm(sdf) * constants::QUANTIZED_SDF_16_BITS_SCALE_INV, shape(Vector3(pos))) *
constants::QUANTIZED_SDF_16_BITS_SCALE
);
}
};

Expand Down Expand Up @@ -264,8 +275,9 @@ struct BlockySetOperation {

inline Box3i get_sdf_sphere_box(Vector3 center, real_t radius) {
return Box3i::from_min_max( //
math::floor_to_int(center - Vector3(radius, radius, radius)),
math::ceil_to_int(center + Vector3(radius, radius, radius)))
math::floor_to_int(center - Vector3(radius, radius, radius)),
math::ceil_to_int(center + Vector3(radius, radius, radius))
)
// That padding is for SDF to have some margin
// TODO Don't add padding from here, it must be done at higher level, where we know the type of operation
.padded(2);
Expand Down Expand Up @@ -310,7 +322,9 @@ struct SdfHemisphere {
return sdf_scale *
math::sdf_smooth_subtract( //
math::sdf_sphere(pos, center, radius), //
math::sdf_plane(pos, flat_direction, plane_d), smoothness);
math::sdf_plane(pos, flat_direction, plane_d),
smoothness
);
}

inline bool is_inside(Vector3 pos) const {
Expand All @@ -337,7 +351,7 @@ struct SdfBufferShape {
// Transform terrain-space position to buffer-space
const Vector3f lpos = to_vec3f(world_to_buffer.xform(wpos));
if (lpos.x < 0 || lpos.y < 0 || lpos.z < 0 || lpos.x >= buffer_size.x || lpos.y >= buffer_size.y ||
lpos.z >= buffer_size.z) {
lpos.z >= buffer_size.z) {
// Outside the buffer
return constants::SDF_FAR_OUTSIDE;
}
Expand Down Expand Up @@ -366,7 +380,9 @@ struct SdfAxisAlignedBox {

inline Box3i get_box() {
return Box3i::from_min_max( //
math::floor_to_int(center - half_size), math::ceil_to_int(center + half_size))
math::floor_to_int(center - half_size),
math::ceil_to_int(center + half_size)
)
// That padding is for SDF to have some margin
// TODO Don't add padding from here, it must be done at higher level, where we know the type of
// operation
Expand Down Expand Up @@ -512,8 +528,12 @@ struct DoSphere {
} break;

case MODE_TEXTURE_PAINT: {
blocks.write_box_2(box, VoxelBuffer::CHANNEL_INDICES, VoxelBuffer::CHANNEL_WEIGHTS,
TextureBlendSphereOp(shape.center, shape.radius, texture_params));
blocks.write_box_2(
box,
VoxelBuffer::CHANNEL_INDICES,
VoxelBuffer::CHANNEL_WEIGHTS,
TextureBlendSphereOp(shape.center, shape.radius, texture_params)
);
} break;

default:
Expand All @@ -530,12 +550,14 @@ struct DoSphere {
};

template <typename TBlockAccess, typename FBlockAction>
void process_chunked_storage(Box3i voxel_box,
void process_chunked_storage(
Box3i voxel_box,
// VoxelBuffer *get_block(Vector3i bpos)
// unsigned int get_block_size_po2()
TBlockAccess block_access,
// void(VoxelBuffer &vb, Box3i local_box, Vector3i origin)
FBlockAction op_func) {
FBlockAction op_func
) {
//
const Vector3i max_pos = voxel_box.position + voxel_box.size;

Expand Down Expand Up @@ -565,24 +587,38 @@ void process_chunked_storage(Box3i voxel_box,
template <typename TBlockAccess, typename FOp>
inline void write_box_in_chunked_storage_1_channel(
// D process(D src, Vector3i position)
const FOp &op, TBlockAccess &block_access, Box3i box, VoxelBuffer::ChannelId channel_id) {
const FOp &op,
TBlockAccess &block_access,
Box3i box,
VoxelBuffer::ChannelId channel_id
) {
//
process_chunked_storage(
box, block_access, [&op, channel_id](VoxelBuffer &vb, const Box3i local_box, Vector3i origin) {
box,
block_access,
[&op, channel_id](VoxelBuffer &vb, const Box3i local_box, Vector3i origin) {
vb.write_box(local_box, channel_id, op, origin);
});
}
);
}

template <typename TBlockAccess, typename FOp>
inline void write_box_in_chunked_storage_2_channels(
// D process(D src, Vector3i position)
const FOp &op, TBlockAccess &block_access, Box3i box, VoxelBuffer::ChannelId channel0_id,
VoxelBuffer::ChannelId channel1_id) {
const FOp &op,
TBlockAccess &block_access,
Box3i box,
VoxelBuffer::ChannelId channel0_id,
VoxelBuffer::ChannelId channel1_id
) {
//
process_chunked_storage(box, block_access,
process_chunked_storage(
box,
block_access,
[&op, channel0_id, channel1_id](VoxelBuffer &vb, const Box3i local_box, Vector3i origin) {
vb.write_box_2_template<FOp, uint16_t, uint16_t>(local_box, channel0_id, channel1_id, op, origin);
});
}
);
}

struct VoxelDataGridAccess {
Expand Down Expand Up @@ -641,7 +677,8 @@ struct DoShapeChunked {
op.shape = shape;
op.texture_params = texture_params;
write_box_in_chunked_storage_2_channels(
op, block_access, box, VoxelBuffer::CHANNEL_INDICES, VoxelBuffer::CHANNEL_WEIGHTS);
op, block_access, box, VoxelBuffer::CHANNEL_INDICES, VoxelBuffer::CHANNEL_WEIGHTS
);
} break;

default:
Expand Down Expand Up @@ -704,7 +741,8 @@ struct DoShapeSingleBuffer {
op.shape = shape;
op.texture_params = texture_params;
buffer->write_box_2_template<TextureBlendOp<TShape>, uint16_t, uint16_t>(
box, VoxelBuffer::CHANNEL_INDICES, VoxelBuffer::CHANNEL_WEIGHTS, op, Vector3i());
box, VoxelBuffer::CHANNEL_INDICES, VoxelBuffer::CHANNEL_WEIGHTS, op, Vector3i()
);
} break;

default:
Expand Down
28 changes: 20 additions & 8 deletions edition/voxel_tool_terrain.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,21 @@ class VoxelToolTerrain : public VoxelTool {
VoxelToolTerrain(VoxelTerrain *terrain);

bool is_area_editable(const Box3i &box) const override;
Ref<VoxelRaycastResult> raycast(
Vector3 p_pos, Vector3 p_dir, float p_max_distance, uint32_t p_collision_mask) override;
Ref<VoxelRaycastResult> raycast(Vector3 p_pos, Vector3 p_dir, float p_max_distance, uint32_t p_collision_mask)
override;

void set_voxel_metadata(Vector3i pos, Variant meta) override;
Variant get_voxel_metadata(Vector3i pos) const override;

void copy(Vector3i pos, VoxelBuffer &dst, uint8_t channels_mask) const override;
void paste(Vector3i pos, const VoxelBuffer &src, uint8_t channels_mask) override;
void paste_masked(Vector3i pos, Ref<godot::VoxelBuffer> p_voxels, uint8_t channels_mask, uint8_t mask_channel,
uint64_t mask_value) override;
void paste_masked(
Vector3i pos,
Ref<godot::VoxelBuffer> p_voxels,
uint8_t channels_mask,
uint8_t mask_channel,
uint64_t mask_value
) override;

void paste_masked_writable_list( //
Vector3i pos, //
Expand All @@ -36,7 +41,7 @@ class VoxelToolTerrain : public VoxelTool {
uint64_t src_mask_value, //
uint8_t dst_mask_channel, //
PackedInt32Array dst_writable_list //
) override;
) override;

void do_box(Vector3i begin, Vector3i end) override;
void do_sphere(Vector3 center, float radius) override;
Expand All @@ -50,9 +55,16 @@ class VoxelToolTerrain : public VoxelTool {

// For easier unit testing (the regular one needs a terrain setup etc, harder to test atm)
// The `_static` suffix is because it otherwise conflicts with the non-static method when registering the class
static void run_blocky_random_tick_static(VoxelData &data, Box3i voxel_box, const VoxelBlockyLibraryBase &lib,
RandomPCG &random, int voxel_count, int batch_count, void *callback_data,
bool (*callback)(void *, Vector3i, int64_t));
static void run_blocky_random_tick_static(
VoxelData &data,
Box3i voxel_box,
const VoxelBlockyLibraryBase &lib,
RandomPCG &random,
int voxel_count,
int batch_count,
void *callback_data,
bool (*callback)(void *, Vector3i, int64_t)
);

void for_each_voxel_metadata_in_area(AABB voxel_area, const Callable &callback);

Expand Down
18 changes: 14 additions & 4 deletions tests/voxel/test_edition_funcs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,18 @@ void test_run_blocky_random_tick() {
RandomPCG random;
random.seed(131183);
VoxelToolTerrain::run_blocky_random_tick_static(
data, voxel_box, **library, random, 1000, 4, &cb, [](void *self, Vector3i pos, int64_t val) {
data,
voxel_box,
**library,
random,
1000,
4,
&cb,
[](void *self, Vector3i pos, int64_t val) {
Callback *cb = (Callback *)self;
return cb->exec(pos, val);
});
}
);

ZN_TEST_ASSERT(cb.ok);

Expand Down Expand Up @@ -210,9 +218,11 @@ void test_discord_soakil_copypaste() {
graph->add_connection(n_box, 0, n_out_sdf, 0);

pg::CompilationResult compilation_result = generator->compile(false);
ZN_TEST_ASSERT_MSG(compilation_result.success,
ZN_TEST_ASSERT_MSG(
compilation_result.success,
String("Failed to compile graph: {0}: {1}")
.format(varray(compilation_result.node_id, compilation_result.message)));
.format(varray(compilation_result.node_id, compilation_result.message))
);
}

VoxelData voxel_data;
Expand Down

0 comments on commit 452f538

Please sign in to comment.