Skip to content

Commit

Permalink
Fix dev assert
Browse files Browse the repository at this point in the history
  • Loading branch information
Zylann committed Dec 29, 2024
1 parent 87f6975 commit f9b9a83
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
7 changes: 4 additions & 3 deletions storage/voxel_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -747,9 +747,10 @@ bool VoxelBuffer::get_channel_as_bytes_read_only(unsigned int channel_index, Spa
void VoxelBuffer::set_channel_from_bytes(const unsigned int channel_index, Span<const uint8_t> src) {
const Channel &channel = _channels[channel_index];
if (channel.compression == COMPRESSION_UNIFORM) {
#ifdef DEV_ENABLED
ZN_ASSERT(channel.data == nullptr);
#endif
// We don't init channel data to nullptr in the constructor so can't do that check
// #ifdef DEV_ENABLED
// ZN_ASSERT(channel.data == nullptr);
// #endif
ZN_ASSERT_RETURN(create_channel_noinit(channel_index, _size));
}
ZN_ASSERT_RETURN(channel.data != nullptr);
Expand Down
1 change: 1 addition & 0 deletions tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ void run_voxel_tests() {
VOXEL_TEST(test_voxel_stream_sqlite_coordinate_format);
VOXEL_TEST(test_sdf_hemisphere);
VOXEL_TEST(test_fnl_range);
VOXEL_TEST(test_voxel_buffer_set_channel_bytes);

print_line("------------ Voxel tests end -------------");
}
Expand Down
46 changes: 46 additions & 0 deletions tests/voxel/test_voxel_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,4 +384,50 @@ void test_voxel_buffer_paste_masked() {
ZN_TEST_ASSERT(dst.equals(expected));
}

void test_voxel_buffer_set_channel_bytes() {
// Set 8-bit non-empty data
{
Ref<godot::VoxelBuffer> vb;
vb.instantiate();
vb->create(3, 4, 5);
const godot::VoxelBuffer::ChannelId channel = godot::VoxelBuffer::CHANNEL_TYPE;
vb->set_channel_depth(channel, godot::VoxelBuffer::DEPTH_8_BIT);

const uint64_t volume = Vector3iUtil::get_volume_u64(vb->get_size());

PackedByteArray bytes;
bytes.resize(volume);
Span<uint8_t> bytes_s(bytes.ptrw(), bytes.size());
for (unsigned int i = 0; i < bytes_s.size(); ++i) {
bytes_s[0] = i;
}

vb->set_channel_from_byte_array(channel, bytes);

{
Vector3i pos;
unsigned int i = 0;
for (pos.z = 0; pos.z < vb->get_size().z; ++pos.z) {
for (pos.x = 0; pos.x < vb->get_size().x; ++pos.x) {
for (pos.y = 0; pos.y < vb->get_size().y; ++pos.y) {
const int v = vb->get_voxel(pos.x, pos.y, pos.z, channel);
const int expected_v = bytes[i];
ZN_TEST_ASSERT(v == expected_v);
++i;
}
}
}
}
}
// Set empty
// Might error, but should not crash
{
Ref<godot::VoxelBuffer> vb;
vb.instantiate();

const godot::VoxelBuffer::ChannelId channel = godot::VoxelBuffer::CHANNEL_TYPE;
vb->set_channel_from_byte_array(channel, PackedByteArray());
}
}

} // namespace zylann::voxel::tests
1 change: 1 addition & 0 deletions tests/voxel/test_voxel_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ void test_voxel_buffer_create();
void test_voxel_buffer_metadata();
void test_voxel_buffer_metadata_gd();
void test_voxel_buffer_paste_masked();
void test_voxel_buffer_set_channel_bytes();

} // namespace zylann::voxel::tests

Expand Down

0 comments on commit f9b9a83

Please sign in to comment.