Skip to content

Commit

Permalink
Use read-only method when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Zylann committed Jun 9, 2024
1 parent 734fb19 commit 2a684b7
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 42 deletions.
6 changes: 3 additions & 3 deletions edition/voxel_mesh_sdf_gd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ std::shared_ptr<ComputeShaderResource> VoxelMeshSDF::get_gpu_resource() {
const VoxelBuffer &buffer = _voxel_buffer->get_buffer();

Span<const float> sdf_grid;
ZN_ASSERT_RETURN_V(buffer.get_channel_data(VoxelBuffer::CHANNEL_SDF, sdf_grid), _gpu_resource);
ZN_ASSERT_RETURN_V(buffer.get_channel_data_read_only(VoxelBuffer::CHANNEL_SDF, sdf_grid), _gpu_resource);

std::shared_ptr<ComputeShaderResource> resource = make_shared_instance<ComputeShaderResource>();
resource->create_texture_3d_zxy(sdf_grid, buffer.get_size());
Expand All @@ -405,7 +405,7 @@ Array VoxelMeshSDF::debug_check_sdf(Ref<Mesh> mesh) {
ZN_ASSERT(_voxel_buffer.is_valid());
const VoxelBuffer &buffer = _voxel_buffer->get_buffer();
Span<const float> sdf_grid;
ZN_ASSERT_RETURN_V(buffer.get_channel_data(VoxelBuffer::CHANNEL_SDF, sdf_grid), result);
ZN_ASSERT_RETURN_V(buffer.get_channel_data_read_only(VoxelBuffer::CHANNEL_SDF, sdf_grid), result);

ZN_ASSERT_RETURN_V(mesh.is_valid(), result);
StdVector<mesh_sdf::Triangle> triangles;
Expand Down Expand Up @@ -449,7 +449,7 @@ Dictionary VoxelMeshSDF::_b_get_data() const {
PackedFloat32Array sdf_f32;
sdf_f32.resize(Vector3iUtil::get_volume(vb.get_size()));
Span<const float> channel;
ERR_FAIL_COND_V(!vb.get_channel_data(VoxelBuffer::CHANNEL_SDF, channel), Dictionary());
ERR_FAIL_COND_V(!vb.get_channel_data_read_only(VoxelBuffer::CHANNEL_SDF, channel), Dictionary());
memcpy(sdf_f32.ptrw(), channel.data(), channel.size() * sizeof(float));
d["sdf_f32"] = sdf_f32;

Expand Down
2 changes: 1 addition & 1 deletion edition/voxel_tool_lod_terrain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,7 @@ void VoxelToolLodTerrain::stamp_sdf(
op.shape.sdf_scale = sdf_scale;
// Note, the passed buffer must not be shared with another thread.
// buffer.decompress_channel(channel);
ZN_ASSERT_RETURN(buffer.get_channel_data(channel, op.shape.buffer));
ZN_ASSERT_RETURN(buffer.get_channel_data_read_only(channel, op.shape.buffer));

VoxelDataGrid grid;
data.get_blocks_grid(grid, voxel_box, 0);
Expand Down
24 changes: 12 additions & 12 deletions meshers/cubes/voxel_mesher_cubes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ inline uint8_t get_alpha_index(Color8 c) {
template <typename Voxel_T, typename Color_F>
void build_voxel_mesh_as_simple_cubes(
FixedArray<VoxelMesherCubes::Arrays, VoxelMesherCubes::MATERIAL_COUNT> &out_arrays_per_material,
const Span<Voxel_T> voxel_buffer,
const Span<const Voxel_T> voxel_buffer,
const Vector3i block_size,
Color_F color_func
) {
Expand Down Expand Up @@ -212,7 +212,7 @@ void build_voxel_mesh_as_simple_cubes(
template <typename Voxel_T, typename Color_F>
void build_voxel_mesh_as_greedy_cubes(
FixedArray<VoxelMesherCubes::Arrays, VoxelMesherCubes::MATERIAL_COUNT> &out_arrays_per_material,
const Span<Voxel_T> voxel_buffer,
const Span<const Voxel_T> voxel_buffer,
const Vector3i block_size,
StdVector<uint8_t> &mask_memory_pool,
Color_F color_func
Expand Down Expand Up @@ -775,8 +775,8 @@ void VoxelMesherCubes::build(VoxelMesher::Output &output, const VoxelMesher::Inp
return;
}

Span<uint8_t> raw_channel;
if (!voxels.get_channel_as_bytes(channel, raw_channel)) {
Span<const uint8_t> raw_channel;
if (!voxels.get_channel_as_bytes_read_only(channel, raw_channel)) {
// Case supposedly handled before...
ERR_PRINT("Something wrong happened");
return;
Expand Down Expand Up @@ -817,15 +817,15 @@ void VoxelMesherCubes::build(VoxelMesher::Output &output, const VoxelMesher::Inp
if (params.greedy_meshing) {
build_voxel_mesh_as_greedy_cubes(
cache.arrays_per_material,
raw_channel.reinterpret_cast_to<uint16_t>(),
raw_channel.reinterpret_cast_to<const uint16_t>(),
block_size,
cache.mask_memory_pool,
Color8::from_u16
);
} else {
build_voxel_mesh_as_simple_cubes(
cache.arrays_per_material,
raw_channel.reinterpret_cast_to<uint16_t>(),
raw_channel.reinterpret_cast_to<const uint16_t>(),
block_size,
Color8::from_u16
);
Expand All @@ -836,15 +836,15 @@ void VoxelMesherCubes::build(VoxelMesher::Output &output, const VoxelMesher::Inp
if (params.greedy_meshing) {
build_voxel_mesh_as_greedy_cubes(
cache.arrays_per_material,
raw_channel.reinterpret_cast_to<uint32_t>(),
raw_channel.reinterpret_cast_to<const uint32_t>(),
block_size,
cache.mask_memory_pool,
Color8::from_u32
);
} else {
build_voxel_mesh_as_simple_cubes(
cache.arrays_per_material,
raw_channel.reinterpret_cast_to<uint32_t>(),
raw_channel.reinterpret_cast_to<const uint32_t>(),
block_size,
Color8::from_u32
);
Expand Down Expand Up @@ -905,15 +905,15 @@ void VoxelMesherCubes::build(VoxelMesher::Output &output, const VoxelMesher::Inp
if (params.greedy_meshing) {
build_voxel_mesh_as_greedy_cubes(
cache.arrays_per_material,
raw_channel.reinterpret_cast_to<uint16_t>(),
raw_channel.reinterpret_cast_to<const uint16_t>(),
block_size,
cache.mask_memory_pool,
get_color_from_palette
);
} else {
build_voxel_mesh_as_simple_cubes(
cache.arrays_per_material,
raw_channel.reinterpret_cast_to<uint16_t>(),
raw_channel.reinterpret_cast_to<const uint16_t>(),
block_size,
get_color_from_palette
);
Expand Down Expand Up @@ -959,15 +959,15 @@ void VoxelMesherCubes::build(VoxelMesher::Output &output, const VoxelMesher::Inp
if (params.greedy_meshing) {
build_voxel_mesh_as_greedy_cubes(
cache.arrays_per_material,
raw_channel.reinterpret_cast_to<uint16_t>(),
raw_channel.reinterpret_cast_to<const uint16_t>(),
block_size,
cache.mask_memory_pool,
get_index_from_palette
);
} else {
build_voxel_mesh_as_simple_cubes(
cache.arrays_per_material,
raw_channel.reinterpret_cast_to<uint16_t>(),
raw_channel.reinterpret_cast_to<const uint16_t>(),
block_size,
get_index_from_palette
);
Expand Down
16 changes: 8 additions & 8 deletions meshers/transvoxel/transvoxel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1372,8 +1372,8 @@ Span<const T> get_or_decompress_channel(const VoxelBuffer &voxels, StdVector<T>
return to_span_const(backing_buffer);

} else {
Span<uint8_t> data_bytes;
ZN_ASSERT(voxels.get_channel_as_bytes(channel, data_bytes) == true);
Span<const uint8_t> data_bytes;
ZN_ASSERT(voxels.get_channel_as_bytes_read_only(channel, data_bytes) == true);
return data_bytes.reinterpret_cast_to<const T>();
}
}
Expand All @@ -1397,8 +1397,8 @@ TextureIndicesData get_texture_indices_data(
out_default_texture_indices_data.use = true;

} else {
Span<uint8_t> data_bytes;
ZN_ASSERT(voxels.get_channel_as_bytes(channel, data_bytes) == true);
Span<const uint8_t> data_bytes;
ZN_ASSERT(voxels.get_channel_as_bytes_read_only(channel, data_bytes) == true);
data.buffer = data_bytes.reinterpret_cast_to<const uint16_t>();

out_default_texture_indices_data.use = false;
Expand Down Expand Up @@ -1498,8 +1498,8 @@ DefaultTextureIndicesData build_regular_mesh(
ZN_PROFILE_SCOPE();
// From this point, we expect the buffer to contain allocated data in the relevant channels.

Span<uint8_t> sdf_data_raw;
ZN_ASSERT(voxels.get_channel_as_bytes(sdf_channel, sdf_data_raw) == true);
Span<const uint8_t> sdf_data_raw;
ZN_ASSERT(voxels.get_channel_as_bytes_read_only(sdf_channel, sdf_data_raw) == true);

const unsigned int voxels_count = Vector3iUtil::get_volume(voxels.get_size());

Expand Down Expand Up @@ -1618,8 +1618,8 @@ void build_transition_mesh(
ZN_PROFILE_SCOPE();
// From this point, we expect the buffer to contain allocated data in the relevant channels.

Span<uint8_t> sdf_data_raw;
ZN_ASSERT(voxels.get_channel_as_bytes(sdf_channel, sdf_data_raw) == true);
Span<const uint8_t> sdf_data_raw;
ZN_ASSERT(voxels.get_channel_as_bytes_read_only(sdf_channel, sdf_data_raw) == true);

const unsigned int voxels_count = Vector3iUtil::get_volume(voxels.get_size());

Expand Down
2 changes: 1 addition & 1 deletion modifiers/voxel_modifier_mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void VoxelModifierMesh::apply(VoxelModifierContext ctx) const {
const Transform3D buffer_to_world = model_to_world * buffer_to_model;

Span<const float> buffer_sdf;
ZN_ASSERT_RETURN(buffer.get_channel_data(VoxelBuffer::CHANNEL_SDF, buffer_sdf));
ZN_ASSERT_RETURN(buffer.get_channel_data_read_only(VoxelBuffer::CHANNEL_SDF, buffer_sdf));
const float smoothness = get_smoothness();

ops::SdfBufferShape shape;
Expand Down
33 changes: 20 additions & 13 deletions storage/voxel_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ void VoxelBuffer::move_to(VoxelBuffer &dst) {
}
}

bool VoxelBuffer::get_channel_as_bytes(unsigned int channel_index, Span<uint8_t> &slice) const {
bool VoxelBuffer::get_channel_as_bytes(unsigned int channel_index, Span<uint8_t> &slice) {
const Channel &channel = _channels[channel_index];
if (channel.compression != COMPRESSION_UNIFORM) {
#ifdef DEV_ENABLED
Expand All @@ -731,10 +731,17 @@ bool VoxelBuffer::get_channel_as_bytes(unsigned int channel_index, Span<uint8_t>
}

bool VoxelBuffer::get_channel_as_bytes_read_only(unsigned int channel_index, Span<const uint8_t> &slice) const {
Span<uint8_t> slice_w;
const bool success = get_channel_as_bytes(channel_index, slice_w);
slice = slice_w;
return success;
const Channel &channel = _channels[channel_index];
if (channel.compression != COMPRESSION_UNIFORM) {
#ifdef DEV_ENABLED
ZN_ASSERT(channel.data != nullptr);
#endif
slice = Span<const uint8_t>(channel.data, 0, channel.size_in_bytes);
return true;
}
// TODO Could we just return `Span<uint8_t>(&channel.defval, 1)` alongside the `false` return?
slice = Span<const uint8_t>();
return false;
}

bool VoxelBuffer::create_channel(int i, uint64_t defval) {
Expand Down Expand Up @@ -1095,30 +1102,30 @@ void get_unscaled_sdf(const VoxelBuffer &voxels, Span<float> sdf) {

switch (depth) {
case VoxelBuffer::DEPTH_8_BIT: {
Span<int8_t> raw;
ZN_ASSERT(voxels.get_channel_data(channel, raw));
Span<const int8_t> raw;
ZN_ASSERT(voxels.get_channel_data_read_only(channel, raw));
for (unsigned int i = 0; i < sdf.size(); ++i) {
sdf[i] = s8_to_snorm(raw[i]);
}
} break;

case VoxelBuffer::DEPTH_16_BIT: {
Span<int16_t> raw;
ZN_ASSERT(voxels.get_channel_data(channel, raw));
Span<const int16_t> raw;
ZN_ASSERT(voxels.get_channel_data_read_only(channel, raw));
for (unsigned int i = 0; i < sdf.size(); ++i) {
sdf[i] = s16_to_snorm(raw[i]);
}
} break;

case VoxelBuffer::DEPTH_32_BIT: {
Span<float> raw;
ZN_ASSERT(voxels.get_channel_data(channel, raw));
Span<const float> raw;
ZN_ASSERT(voxels.get_channel_data_read_only(channel, raw));
memcpy(sdf.data(), raw.data(), sizeof(float) * sdf.size());
} break;

case VoxelBuffer::DEPTH_64_BIT: {
Span<double> raw;
ZN_ASSERT(voxels.get_channel_data(channel, raw));
Span<const double> raw;
ZN_ASSERT(voxels.get_channel_data_read_only(channel, raw));
for (unsigned int i = 0; i < sdf.size(); ++i) {
sdf[i] = raw[i];
}
Expand Down
12 changes: 10 additions & 2 deletions storage/voxel_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -435,17 +435,25 @@ class VoxelBuffer {
return Vector3iUtil::get_volume(_size);
}

bool get_channel_as_bytes(unsigned int channel_index, Span<uint8_t> &slice) const;
bool get_channel_as_bytes(unsigned int channel_index, Span<uint8_t> &slice);
bool get_channel_as_bytes_read_only(unsigned int channel_index, Span<const uint8_t> &slice) const;

template <typename T>
bool get_channel_data(unsigned int channel_index, Span<T> &dst) const {
bool get_channel_data(unsigned int channel_index, Span<T> &dst) {
Span<uint8_t> dst8;
ZN_ASSERT_RETURN_V(get_channel_as_bytes(channel_index, dst8), false);
dst = dst8.reinterpret_cast_to<T>();
return true;
}

template <typename T>
bool get_channel_data_read_only(unsigned int channel_index, Span<const T> &dst) const {
Span<const uint8_t> dst8;
ZN_ASSERT_RETURN_V(get_channel_as_bytes_read_only(channel_index, dst8), false);
dst = dst8.reinterpret_cast_to<const T>();
return true;
}

void downscale_to(VoxelBuffer &dst, Vector3i src_min, Vector3i src_max, Vector3i dst_min) const;

bool equals(const VoxelBuffer &p_other) const;
Expand Down
5 changes: 3 additions & 2 deletions streams/voxel_block_serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,10 @@ SerializeResult serialize(const VoxelBuffer &voxel_buffer) {

switch (compression) {
case VoxelBuffer::COMPRESSION_NONE: {
Span<uint8_t> data;
Span<const uint8_t> data;
ERR_FAIL_COND_V(
!voxel_buffer.get_channel_as_bytes(channel_index, data), SerializeResult(dst_data, false)
!voxel_buffer.get_channel_as_bytes_read_only(channel_index, data),
SerializeResult(dst_data, false)
);
f.store_buffer(data);
} break;
Expand Down

0 comments on commit 2a684b7

Please sign in to comment.