Skip to content

Commit

Permalink
Added support for precision=double.
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriySalnikov committed Nov 3, 2024
1 parent 1346f79 commit 65b4e62
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 65 deletions.
61 changes: 31 additions & 30 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
{
"configurations": [
{
"name": "DefaultCPP",
"includePath": [
"${workspaceFolder}/src/**",
"${workspaceFolder}/godot-cpp/gdextension/**",
"${workspaceFolder}/godot-cpp/include",
"${workspaceFolder}/godot-cpp/gen/include"
],
"defines": [
"DEBUG_ENABLED",
"DEV_ENABLED",
"UNICODE",
"_UNICODE",
"GDEXTENSION_LIBRARY",
"NOMINMAX",
"TYPED_METHOD_BIND",
"HOT_RELOAD_ENABLED",
"TOOLS_ENABLED",
"TELEMETRY_ENABLED",
"TELEMETRY_PROJECT_DD3D"
],
"windowsSdkVersion": "10.0.22000.0",
"compilerPath": "cl.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-msvc-x64"
}
],
"version": 4
"configurations": [
{
"name": "DefaultCPP",
"includePath": [
"${workspaceFolder}/src/**",
"${workspaceFolder}/godot-cpp/gdextension/**",
"${workspaceFolder}/godot-cpp/include",
"${workspaceFolder}/godot-cpp/gen/include"
],
"defines": [
"DEBUG_ENABLED",
"DEV_ENABLED",
"UNICODE",
"_UNICODE",
"GDEXTENSION_LIBRARY",
"NOMINMAX",
"TYPED_METHOD_BIND",
"HOT_RELOAD_ENABLED",
"TOOLS_ENABLED",
"TELEMETRY_ENABLED",
"TELEMETRY_PROJECT_DD3D"
//"REAL_T_IS_DOUBLE"
],
"windowsSdkVersion": "10.0.22000.0",
"compilerPath": "cl.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-msvc-x64"
}
],
"version": 4
}
26 changes: 26 additions & 0 deletions src/3d/debug_draw_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ GODOT_WARNING_DISABLE()
#include <godot_cpp/classes/camera3d.hpp>
#include <godot_cpp/classes/os.hpp>
#include <godot_cpp/classes/world3d.hpp>

// save meshes
#if !defined(DISABLE_DEBUG_RENDERING) && defined(DEV_ENABLED)
#include <godot_cpp/classes/dir_access.hpp>
#include <godot_cpp/classes/resource_saver.hpp>
#endif
GODOT_WARNING_RESTORE()

#define NEED_LEAVE (!_is_enabled_override())
Expand Down Expand Up @@ -72,6 +78,10 @@ void DebugDraw3D::_bind_methods() {

#pragma endregion

#if !defined(DISABLE_DEBUG_RENDERING) && defined(DEV_ENABLED)
ClassDB::bind_method(D_METHOD(NAMEOF(_save_generated_meshes)), &DebugDraw3D::_save_generated_meshes);
#endif

#pragma region Draw Functions
ClassDB::bind_method(D_METHOD(NAMEOF(regenerate_geometry_meshes)), &DebugDraw3D::regenerate_geometry_meshes);
ClassDB::bind_method(D_METHOD(NAMEOF(clear_all)), &DebugDraw3D::clear_all);
Expand Down Expand Up @@ -692,6 +702,22 @@ void DebugDraw3D::clear_all() {

#ifndef DISABLE_DEBUG_RENDERING

#ifdef DEV_ENABLED
void DebugDraw3D::_save_generated_meshes() {
if (!shared_generated_meshes.size())
return;

for (int i = 0; i < 2; i++) {
for (int type = 0; type < (int)InstanceType::MAX; type++) {
Ref<ArrayMesh> mesh = shared_generated_meshes[type][i];
String dir_path = FMT_STR("res://debug_meshes/{0}", i == 0 ? "normal" : "no_depth");
DirAccess::make_dir_recursive_absolute(dir_path);
ResourceSaver::get_singleton()->save(mesh, FMT_STR("{0}/{1}.mesh", dir_path, type), ResourceSaver::SaverFlags::FLAG_BUNDLE_RESOURCES | ResourceSaver::SaverFlags::FLAG_REPLACE_SUBRESOURCE_PATHS);
}
}
}
#endif

Vector3 DebugDraw3D::get_up_vector(const Vector3 &p_dir) {
if (Math::is_equal_approx(p_dir.x, 0)) {
if (Math::is_equal_approx(p_dir.z, 0))
Expand Down
4 changes: 4 additions & 0 deletions src/3d/debug_draw_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ class DebugDraw3D : public Object, public IScopeStorage<DebugDraw3DScopeConfig,

void create_arrow(const Vector3 &p_a, const Vector3 &p_b, const Color &p_color, const real_t &p_arrow_size, const bool &p_is_absolute_size, const real_t &p_duration = 0);

#ifdef DEV_ENABLED
void _save_generated_meshes();
#endif

#endif

void init(DebugDrawManager *p_root);
Expand Down
12 changes: 6 additions & 6 deletions src/3d/geometry_generators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,11 @@ Ref<ArrayMesh> GeometryGenerator::RotatedMesh(const Ref<ArrayMesh> mesh, const V
};

auto rotate_f32 = [&axis, &angle](PackedFloat32Array &arr) {
for (int i = 0; i < arr.size(); i += 3) {
for (int64_t i = 0; i < arr.size(); i += 3) {
Vector3 v = Vector3(arr[i + 0], arr[i + 1], arr[i + 2]).rotated(axis, angle);
arr[i + 0] = v.x;
arr[i + 1] = v.y;
arr[i + 2] = v.z;
arr[i + 0] = (float)v.x;
arr[i + 1] = (float)v.y;
arr[i + 2] = (float)v.z;
}
};

Expand Down Expand Up @@ -270,7 +270,7 @@ Ref<ArrayMesh> GeometryGenerator::ConvertWireframeToVolumetric(Ref<ArrayMesh> me
PackedColorArray(),
PackedVector3Array(),
res_uv,
Utils::convert_packed_array_to_diffrent_types<PackedFloat32Array>(res_custom0),
Utils::convert_packed_vector3_to_packed_float(res_custom0),
ArrayMesh::ARRAY_CUSTOM_RGB_FLOAT << Mesh::ARRAY_FORMAT_CUSTOM0_SHIFT);
}

Expand Down Expand Up @@ -543,7 +543,7 @@ Ref<ArrayMesh> GeometryGenerator::CreateVolumetricArrowHead(const real_t &radius
PackedColorArray(),
PackedVector3Array(),
uv,
Utils::convert_packed_array_to_diffrent_types<PackedFloat32Array>(custom0),
Utils::convert_packed_vector3_to_packed_float(custom0),
ArrayMesh::ARRAY_CUSTOM_RGB_FLOAT << Mesh::ARRAY_FORMAT_CUSTOM0_SHIFT);
}

Expand Down
4 changes: 3 additions & 1 deletion src/3d/render_instances.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ void GeometryPool::fill_mesh_data(const std::vector<Ref<MultiMesh> *> &p_meshes,
void GeometryPool::fill_instance_data(const std::vector<Ref<MultiMesh> *> &p_meshes, std::unordered_map<Viewport *, std::shared_ptr<GeometryPoolCullingData> > &p_culling_data) {
ZoneScoped;

constexpr size_t INSTANCE_DATA_FLOAT_COUNT = ((sizeof(float) * 3 /*3 components*/ * 4 /*4 vectors3*/ + sizeof(godot::Color) /*Instance Color*/ + sizeof(godot::Color) /*Custom Data*/) / sizeof(float));

// reset timers
time_spent_to_cull_instances = 0;
time_spent_to_fill_buffers_of_instances = 0;
Expand Down Expand Up @@ -146,7 +148,7 @@ void GeometryPool::fill_instance_data(const std::vector<Ref<MultiMesh> *> &p_mes
auto w = buffer.ptrw();

for (auto &inst : visible_buffer) {
memcpy(w + last_added++ * INSTANCE_DATA_FLOAT_COUNT, reinterpret_cast<const real_t *>(&inst->data), INSTANCE_DATA_FLOAT_COUNT * sizeof(real_t));
memcpy(w + last_added++ * INSTANCE_DATA_FLOAT_COUNT, reinterpret_cast<const float *>(&inst->data), INSTANCE_DATA_FLOAT_COUNT * sizeof(float));
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/3d/render_instances.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@ class GeometryPoolCullingData {
};

struct GeometryPoolData3DInstance {
Vector3 basis_x;
Vector3Float basis_x;
float origin_x;
Vector3 basis_y;
Vector3Float basis_y;
float origin_y;
Vector3 basis_z;
Vector3Float basis_z;
float origin_z;
Color color;
Color custom;

GeometryPoolData3DInstance() :
basis_x(Vector3()),
basis_x(),
origin_x(0),
basis_y(Vector3()),
basis_y(),
origin_y(0),
basis_z(Vector3()),
basis_z(),
origin_z(0),
color(Color()),
custom(Color()) {}
Expand Down
35 changes: 35 additions & 0 deletions src/resources/debug/test_extended.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@tool
extends Node3D

@export_range(0, 100) var instances = 1
@export_range(0, 1) var thickness = 0.1
@export_range(0, 1) var brightness = 0.8

func _ready() -> void:
await get_tree().process_frame

var f32_to_v3 = func (arr):
var v3: PackedVector3Array
for i in range(0, arr.size(), 3):
v3.push_back(Vector3(arr[i], arr[i+1], arr[i+2]))
return v3

var m: ArrayMesh = $MMesh.multimesh.mesh
var arrs: Array = m.surface_get_arrays(0)
print("Vertexes %d\nIndexes %d\nCustom0 %d\nVertexes %s\nIndexes %s\nCustom0 %s" % [
arrs[ArrayMesh.ARRAY_VERTEX].size(),
arrs[ArrayMesh.ARRAY_INDEX].size(),
arrs[ArrayMesh.ARRAY_CUSTOM0].size(),
arrs[ArrayMesh.ARRAY_VERTEX],
arrs[ArrayMesh.ARRAY_INDEX],
f32_to_v3.call(arrs[ArrayMesh.ARRAY_CUSTOM0])
])

func _process(delta: float) -> void:
var mm:MultiMesh = $MMesh.multimesh
mm.instance_count = instances
# Color(p_cfg->thickness, p_cfg->center_brightness, 0, 0)
for i in mm.instance_count:
mm.set_instance_transform(i, Transform3D($MMesh.basis, Vector3(i * 2, 0, 0)))
mm.set_instance_color(i, Color.CORNFLOWER_BLUE)
mm.set_instance_custom_data(i, Color(thickness, brightness, 0, 0))
23 changes: 23 additions & 0 deletions src/resources/debug/test_extended.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[gd_scene load_steps=4 format=3 uid="uid://c6fkx0e7sri1d"]

[ext_resource type="Script" path="res://resources/debug/test_extended.gd" id="1_qh7ib"]
[ext_resource type="ArrayMesh" uid="uid://83qs2t5wxctw" path="res://debug_meshes/normal/10.mesh" id="2_3hlxm"]

[sub_resource type="MultiMesh" id="MultiMesh_e532d"]
transform_format = 1
use_colors = true
use_custom_data = true
instance_count = 1
mesh = ExtResource("2_3hlxm")
buffer = PackedFloat32Array(0.702432, 0, -0.136374, 0, 0, 0.923358, 0, 0, 0.248153, 0, 1.27818, 0, 0.392157, 0.584314, 0.929412, 1, 0.159, 0.245, 0, 0)

[node name="Root" type="Node3D"]
transform = Transform3D(0.802547, 0, -0.596589, 0, 1, 0, 0.596589, 0, 0.802547, 0, 0, 0)
script = ExtResource("1_qh7ib")
thickness = 0.159
brightness = 0.245

[node name="MMesh" type="MultiMeshInstance3D" parent="."]
transform = Transform3D(0.702432, 0, -0.136374, 0, 0.923358, 0, 0.248153, 0, 1.27818, 0, 0, 0)
rotation_edit_mode = 2
multimesh = SubResource("MultiMesh_e532d")
30 changes: 30 additions & 0 deletions src/utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ void Utils::get_godot_version(int *major, int *minor, int *patch, int *version_s
}

bool Utils::is_current_godot_version_in_range(uint8_t min_major, uint8_t min_minor, uint8_t min_patch, uint8_t max_major, uint8_t max_minor, uint8_t max_patch) {
ZoneScoped;
int godot_version;
get_godot_version(nullptr, nullptr, nullptr, &godot_version);

Expand All @@ -162,3 +163,32 @@ bool Utils::is_current_godot_version_in_range(uint8_t min_major, uint8_t min_min
}
return false;
}

godot::PackedFloat32Array Utils::convert_packed_vector3_to_packed_float(godot::PackedVector3Array &arr) {
ZoneScoped;

godot::PackedFloat32Array p;
long s = sizeof(float);

if (!arr.is_empty()) {
#if REAL_T_IS_DOUBLE
if (arr.size()) {
p.resize(arr.size() * 3);
auto *w = p.ptrw();
for (int64_t i = 0; i < arr.size(); i++) {
const godot::Vector3 &v = arr[i];
w[i * 3 + 0] = (float)v.x;
w[i * 3 + 1] = (float)v.y;
w[i * 3 + 2] = (float)v.z;
}
}
#else
auto *data = arr.ptr();
if (data) {
p.resize(arr.size() * sizeof(arr[0]) / s);
memcpy(p.ptrw(), data, sizeof(arr[0]) * arr.size());
}
#endif
}
return p;
}
39 changes: 17 additions & 22 deletions src/utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ static String get_file_name_in_repository(const String &name) {

#pragma endregion !BINDING REGISTRATION

constexpr size_t INSTANCE_DATA_FLOAT_COUNT = ((sizeof(godot::Transform3D) + sizeof(godot::Color) /*Instance Color*/ + sizeof(godot::Color) /*Custom Data*/) / sizeof(real_t));

#define IS_EDITOR_HINT() Engine::get_singleton()->is_editor_hint()
#define SCENE_TREE() Object::cast_to<SceneTree>(Engine::get_singleton()->get_main_loop())
#define SCENE_ROOT() (SCENE_TREE()->get_root())
Expand Down Expand Up @@ -193,6 +191,20 @@ const extern godot::Vector3 Vector3_FORWARD;

const extern godot::Quaternion Quaternion_IDENTITY;

#if REAL_T_IS_DOUBLE
struct Vector3Float {
float x, y, z;
Vector3Float() :
x{}, y{}, z{} {}
Vector3Float(float x, float y, float z) :
x(x), y(y), z(z) {}
Vector3Float(godot::Vector3 v) :
x((float)v.x), y((float)v.y), z((float)v.z) {}
};
#else
typedef godot::Vector3 Vector3Float;
#endif

class Utils {
#if DEBUG_ENABLED
struct LogData {
Expand Down Expand Up @@ -254,7 +266,7 @@ class Utils {
TPool p;
if (!arr.empty()) {
auto *data = arr.data();
if (data) {
if (arr.size()) {
p.resize(arr.size());
memcpy(p.ptrw(), data, sizeof(arr[0]) * arr.size());
}
Expand All @@ -273,32 +285,15 @@ class Utils {

if (!arr.empty()) {
auto *data = arr.data();
if (data) {
if (arr.size()) {
p.resize(arr.size() * sizeof(arr[0]) / s);
memcpy(p.ptrw(), data, sizeof(arr[0]) * arr.size());
}
}
return p;
}

template <class TPool, class TContainer>
static TPool convert_packed_array_to_diffrent_types(TContainer &arr) {
ZoneScoped;

TPool p;
p.resize(1);
long s = sizeof(p[0]);
p.resize(0);

if (!arr.is_empty()) {
auto *data = arr.ptr();
if (data) {
p.resize(arr.size() * sizeof(arr[0]) / s);
memcpy(p.ptrw(), data, sizeof(arr[0]) * arr.size());
}
}
return p;
}
static godot::PackedFloat32Array convert_packed_vector3_to_packed_float(godot::PackedVector3Array &arr);

// TODO need to use make from API when it becomes possible
#pragma region HACK_FOR_DICTIONARIES
Expand Down

0 comments on commit 65b4e62

Please sign in to comment.