From 9fd7a093c9de4277ae68cea99df8620dd211b554 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Mon, 12 May 2025 11:16:26 +0200 Subject: [PATCH] DNA: support getting sdna id for static DNA type This adds a new `DNA_sdna_type_ids.hh` header: ```cpp namespace blender::dna { /** * Each DNA struct has an integer identifier which is unique within a specific * Blender build, but not necessarily across different builds. The identifier * can be used to index into `SDNA.structs`. */ template int sdna_struct_id_get(); /** * The maximum identifier that will be returned by #sdna_struct_id_get in this * Blender build. */ int sdna_struct_id_get_max(); } // namespace blender::dna ``` The `sdna_struct_id_get` function is used as replacement of `SDNA_TYPE_FROM_STRUCT` in all places except the DNA defaults system. The defaults system is C code and therefore can't use the template. There is ongoing work to replace the defaults system as well though: #134531. Using this templated function has some benefits over the old approach: * No need to rely on macros. * Can use type inferencing in functions like `BLO_write_struct` which avoids redundancy on the call site. E.g. `BLO_write_struct(writer, ActionStrip, strip);` can become `BLO_write_struct(writer, strip);` which could even become `writer.write_struct(strip);`. None of that is implemented as part of this patch though. * No need to include the generated `dna_type_offsets.h` file which contains a huge enum. Implementation wise, this is done using explicit template instantiations in a new file generated by `makesdna.cc`: `dna_struct_ids.cc`. The generated file looks like so: ```cpp namespace blender::dna { template int sdna_struct_id_get(); int sdna_struct_id_get_max(); int sdna_struct_id_get_max() { return 951; } } struct IDPropertyUIData; template<> int blender::dna::sdna_struct_id_get() { return 1; } struct IDPropertyUIDataEnumItem; template<> int blender::dna::sdna_struct_id_get() { return 2; } ``` I tried using static variables instead of separate functions, but I didn't manage to link it properly. Not quite sure yet if that's an actual limitation or if I was just missing something. Pull Request: https://projects.blender.org/blender/blender/pulls/138706 --- source/blender/blenkernel/intern/linestyle.cc | 89 ++++++++++--------- source/blender/blenloader/BLO_read_write.hh | 19 ++-- source/blender/blenloader/intern/writefile.cc | 7 +- .../blender/draw/engines/eevee/eevee_light.cc | 4 +- source/blender/makesdna/DNA_print.hh | 5 +- source/blender/makesdna/DNA_sdna_type_ids.hh | 25 ++++++ source/blender/makesdna/intern/CMakeLists.txt | 6 +- source/blender/makesdna/intern/makesdna.cc | 46 ++++++++-- .../nodes/node_geo_attribute_capture.cc | 4 +- .../nodes/geometry/nodes/node_geo_bake.cc | 2 +- .../nodes/geometry/nodes/node_geo_closure.cc | 7 +- .../geometry/nodes/node_geo_combine_bundle.cc | 4 +- .../nodes/node_geo_evaluate_closure.cc | 8 +- .../node_geo_foreach_geometry_element.cc | 12 +-- .../geometry/nodes/node_geo_index_switch.cc | 2 +- .../geometry/nodes/node_geo_menu_switch.cc | 2 +- .../nodes/geometry/nodes/node_geo_repeat.cc | 2 +- .../nodes/node_geo_separate_bundle.cc | 4 +- .../geometry/nodes/node_geo_simulation.cc | 2 +- 19 files changed, 155 insertions(+), 95 deletions(-) create mode 100644 source/blender/makesdna/DNA_sdna_type_ids.hh diff --git a/source/blender/blenkernel/intern/linestyle.cc b/source/blender/blenkernel/intern/linestyle.cc index c6e4887b786..c46cdad0207 100644 --- a/source/blender/blenkernel/intern/linestyle.cc +++ b/source/blender/blenkernel/intern/linestyle.cc @@ -18,6 +18,7 @@ #include "DNA_defaults.h" #include "DNA_material_types.h" /* for ramp blend */ #include "DNA_object_types.h" +#include "DNA_sdna_type_ids.hh" #include "DNA_texture_types.h" #include "BLI_listbase.h" @@ -43,6 +44,8 @@ #include "BLO_read_write.hh" +using blender::dna::sdna_struct_id_get; + static void linestyle_init_data(ID *id) { FreestyleLineStyle *linestyle = (FreestyleLineStyle *)id; @@ -180,31 +183,31 @@ static void write_linestyle_color_modifiers(BlendWriter *writer, ListBase *modif int struct_nr; switch (m->type) { case LS_MODIFIER_ALONG_STROKE: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleColorModifier_AlongStroke); + struct_nr = sdna_struct_id_get(); break; case LS_MODIFIER_DISTANCE_FROM_CAMERA: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleColorModifier_DistanceFromCamera); + struct_nr = sdna_struct_id_get(); break; case LS_MODIFIER_DISTANCE_FROM_OBJECT: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleColorModifier_DistanceFromObject); + struct_nr = sdna_struct_id_get(); break; case LS_MODIFIER_MATERIAL: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleColorModifier_Material); + struct_nr = sdna_struct_id_get(); break; case LS_MODIFIER_TANGENT: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleColorModifier_Tangent); + struct_nr = sdna_struct_id_get(); break; case LS_MODIFIER_NOISE: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleColorModifier_Noise); + struct_nr = sdna_struct_id_get(); break; case LS_MODIFIER_CREASE_ANGLE: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleColorModifier_CreaseAngle); + struct_nr = sdna_struct_id_get(); break; case LS_MODIFIER_CURVATURE_3D: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleColorModifier_Curvature_3D); + struct_nr = sdna_struct_id_get(); break; default: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleModifier); /* this should not happen */ + struct_nr = sdna_struct_id_get(); /* this should not happen */ } BLO_write_struct_by_id(writer, struct_nr, m); } @@ -247,31 +250,31 @@ static void write_linestyle_alpha_modifiers(BlendWriter *writer, ListBase *modif int struct_nr; switch (m->type) { case LS_MODIFIER_ALONG_STROKE: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleAlphaModifier_AlongStroke); + struct_nr = sdna_struct_id_get(); break; case LS_MODIFIER_DISTANCE_FROM_CAMERA: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleAlphaModifier_DistanceFromCamera); + struct_nr = sdna_struct_id_get(); break; case LS_MODIFIER_DISTANCE_FROM_OBJECT: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleAlphaModifier_DistanceFromObject); + struct_nr = sdna_struct_id_get(); break; case LS_MODIFIER_MATERIAL: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleAlphaModifier_Material); + struct_nr = sdna_struct_id_get(); break; case LS_MODIFIER_TANGENT: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleAlphaModifier_Tangent); + struct_nr = sdna_struct_id_get(); break; case LS_MODIFIER_NOISE: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleAlphaModifier_Noise); + struct_nr = sdna_struct_id_get(); break; case LS_MODIFIER_CREASE_ANGLE: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleAlphaModifier_CreaseAngle); + struct_nr = sdna_struct_id_get(); break; case LS_MODIFIER_CURVATURE_3D: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleAlphaModifier_Curvature_3D); + struct_nr = sdna_struct_id_get(); break; default: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleModifier); /* this should not happen */ + struct_nr = sdna_struct_id_get(); /* this should not happen */ } BLO_write_struct_by_id(writer, struct_nr, m); } @@ -313,34 +316,34 @@ static void write_linestyle_thickness_modifiers(BlendWriter *writer, ListBase *m int struct_nr; switch (m->type) { case LS_MODIFIER_ALONG_STROKE: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleThicknessModifier_AlongStroke); + struct_nr = sdna_struct_id_get(); break; case LS_MODIFIER_DISTANCE_FROM_CAMERA: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleThicknessModifier_DistanceFromCamera); + struct_nr = sdna_struct_id_get(); break; case LS_MODIFIER_DISTANCE_FROM_OBJECT: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleThicknessModifier_DistanceFromObject); + struct_nr = sdna_struct_id_get(); break; case LS_MODIFIER_MATERIAL: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleThicknessModifier_Material); + struct_nr = sdna_struct_id_get(); break; case LS_MODIFIER_CALLIGRAPHY: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleThicknessModifier_Calligraphy); + struct_nr = sdna_struct_id_get(); break; case LS_MODIFIER_TANGENT: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleThicknessModifier_Tangent); + struct_nr = sdna_struct_id_get(); break; case LS_MODIFIER_NOISE: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleThicknessModifier_Noise); + struct_nr = sdna_struct_id_get(); break; case LS_MODIFIER_CREASE_ANGLE: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleThicknessModifier_CreaseAngle); + struct_nr = sdna_struct_id_get(); break; case LS_MODIFIER_CURVATURE_3D: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleThicknessModifier_Curvature_3D); + struct_nr = sdna_struct_id_get(); break; default: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleModifier); /* this should not happen */ + struct_nr = sdna_struct_id_get(); /* this should not happen */ } BLO_write_struct_by_id(writer, struct_nr, m); } @@ -380,49 +383,49 @@ static void write_linestyle_geometry_modifiers(BlendWriter *writer, ListBase *mo int struct_nr; switch (m->type) { case LS_MODIFIER_SAMPLING: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleGeometryModifier_Sampling); + struct_nr = sdna_struct_id_get(); break; case LS_MODIFIER_BEZIER_CURVE: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleGeometryModifier_BezierCurve); + struct_nr = sdna_struct_id_get(); break; case LS_MODIFIER_SINUS_DISPLACEMENT: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleGeometryModifier_SinusDisplacement); + struct_nr = sdna_struct_id_get(); break; case LS_MODIFIER_SPATIAL_NOISE: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleGeometryModifier_SpatialNoise); + struct_nr = sdna_struct_id_get(); break; case LS_MODIFIER_PERLIN_NOISE_1D: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleGeometryModifier_PerlinNoise1D); + struct_nr = sdna_struct_id_get(); break; case LS_MODIFIER_PERLIN_NOISE_2D: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleGeometryModifier_PerlinNoise2D); + struct_nr = sdna_struct_id_get(); break; case LS_MODIFIER_BACKBONE_STRETCHER: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleGeometryModifier_BackboneStretcher); + struct_nr = sdna_struct_id_get(); break; case LS_MODIFIER_TIP_REMOVER: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleGeometryModifier_TipRemover); + struct_nr = sdna_struct_id_get(); break; case LS_MODIFIER_POLYGONIZATION: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleGeometryModifier_Polygonalization); + struct_nr = sdna_struct_id_get(); break; case LS_MODIFIER_GUIDING_LINES: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleGeometryModifier_GuidingLines); + struct_nr = sdna_struct_id_get(); break; case LS_MODIFIER_BLUEPRINT: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleGeometryModifier_Blueprint); + struct_nr = sdna_struct_id_get(); break; case LS_MODIFIER_2D_OFFSET: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleGeometryModifier_2DOffset); + struct_nr = sdna_struct_id_get(); break; case LS_MODIFIER_2D_TRANSFORM: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleGeometryModifier_2DTransform); + struct_nr = sdna_struct_id_get(); break; case LS_MODIFIER_SIMPLIFICATION: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleGeometryModifier_Simplification); + struct_nr = sdna_struct_id_get(); break; default: - struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleModifier); /* this should not happen */ + struct_nr = sdna_struct_id_get(); /* this should not happen */ } BLO_write_struct_by_id(writer, struct_nr, m); } diff --git a/source/blender/blenloader/BLO_read_write.hh b/source/blender/blenloader/BLO_read_write.hh index 67063f5388c..2dedba211dd 100644 --- a/source/blender/blenloader/BLO_read_write.hh +++ b/source/blender/blenloader/BLO_read_write.hh @@ -30,9 +30,7 @@ #pragma once -/* for SDNA_TYPE_FROM_STRUCT() macro */ -#include "dna_type_offsets.h" - +#include "DNA_sdna_type_ids.hh" #include "DNA_windowmanager_types.h" /* for eReportType */ #include "BLI_function_ref.hh" @@ -87,7 +85,6 @@ struct Main; * Mapping between names and ids. */ int BLO_get_struct_id_by_name(const BlendWriter *writer, const char *struct_name); -#define BLO_get_struct_id(writer, struct_name) SDNA_TYPE_FROM_STRUCT(struct_name) /** * Write single struct. @@ -95,7 +92,7 @@ int BLO_get_struct_id_by_name(const BlendWriter *writer, const char *struct_name void BLO_write_struct_by_name(BlendWriter *writer, const char *struct_name, const void *data_ptr); void BLO_write_struct_by_id(BlendWriter *writer, int struct_id, const void *data_ptr); #define BLO_write_struct(writer, struct_name, data_ptr) \ - BLO_write_struct_by_id(writer, BLO_get_struct_id(writer, struct_name), data_ptr) + BLO_write_struct_by_id(writer, blender::dna::sdna_struct_id_get(), data_ptr) /** * Write single struct at address. @@ -106,7 +103,7 @@ void BLO_write_struct_at_address_by_id(BlendWriter *writer, const void *data_ptr); #define BLO_write_struct_at_address(writer, struct_name, address, data_ptr) \ BLO_write_struct_at_address_by_id( \ - writer, BLO_get_struct_id(writer, struct_name), address, data_ptr) + writer, blender::dna::sdna_struct_id_get(), address, data_ptr) /** * Write single struct at address and specify a file-code. @@ -116,7 +113,7 @@ void BLO_write_struct_at_address_by_id_with_filecode( #define BLO_write_struct_at_address_with_filecode( \ writer, filecode, struct_name, address, data_ptr) \ BLO_write_struct_at_address_by_id_with_filecode( \ - writer, filecode, BLO_get_struct_id(writer, struct_name), address, data_ptr) + writer, filecode, blender::dna::sdna_struct_id_get(), address, data_ptr) /** * Write struct array. @@ -131,7 +128,7 @@ void BLO_write_struct_array_by_id(BlendWriter *writer, const void *data_ptr); #define BLO_write_struct_array(writer, struct_name, array_size, data_ptr) \ BLO_write_struct_array_by_id( \ - writer, BLO_get_struct_id(writer, struct_name), array_size, data_ptr) + writer, blender::dna::sdna_struct_id_get(), array_size, data_ptr) /** * Write struct array at address. @@ -143,7 +140,7 @@ void BLO_write_struct_array_at_address_by_id(BlendWriter *writer, const void *data_ptr); #define BLO_write_struct_array_at_address(writer, struct_name, array_size, address, data_ptr) \ BLO_write_struct_array_at_address_by_id( \ - writer, BLO_get_struct_id(writer, struct_name), array_size, address, data_ptr) + writer, blender::dna::sdna_struct_id_get(), array_size, address, data_ptr) /** * Write struct list. @@ -151,14 +148,14 @@ void BLO_write_struct_array_at_address_by_id(BlendWriter *writer, void BLO_write_struct_list_by_name(BlendWriter *writer, const char *struct_name, ListBase *list); void BLO_write_struct_list_by_id(BlendWriter *writer, int struct_id, const ListBase *list); #define BLO_write_struct_list(writer, struct_name, list_ptr) \ - BLO_write_struct_list_by_id(writer, BLO_get_struct_id(writer, struct_name), list_ptr) + BLO_write_struct_list_by_id(writer, blender::dna::sdna_struct_id_get(), list_ptr) /** * Write id struct. */ void blo_write_id_struct(BlendWriter *writer, int struct_id, const void *id_address, const ID *id); #define BLO_write_id_struct(writer, struct_name, id_address, id) \ - blo_write_id_struct(writer, BLO_get_struct_id(writer, struct_name), id_address, id) + blo_write_id_struct(writer, blender::dna::sdna_struct_id_get(), id_address, id) /** * Specific code to prepare IDs to be written. diff --git a/source/blender/blenloader/intern/writefile.cc b/source/blender/blenloader/intern/writefile.cc index bb3298542a9..0d45d326dcd 100644 --- a/source/blender/blenloader/intern/writefile.cc +++ b/source/blender/blenloader/intern/writefile.cc @@ -772,7 +772,7 @@ static void writestruct_at_address_nr(WriteData *wd, const void *adr, const void *data) { - BLI_assert(struct_nr > 0 && struct_nr < SDNA_TYPE_MAX); + BLI_assert(struct_nr > 0 && struct_nr <= blender::dna::sdna_struct_id_get_max()); if (adr == nullptr || data == nullptr || nr == 0) { return; @@ -917,10 +917,11 @@ static void writelist_id(WriteData *wd, const int filecode, const char *structna #endif #define writestruct_at_address(wd, filecode, struct_id, nr, adr, data) \ - writestruct_at_address_nr(wd, filecode, SDNA_TYPE_FROM_STRUCT(struct_id), nr, adr, data) + writestruct_at_address_nr( \ + wd, filecode, blender::dna::sdna_struct_id_get(), nr, adr, data) #define writestruct(wd, filecode, struct_id, nr, adr) \ - writestruct_nr(wd, filecode, SDNA_TYPE_FROM_STRUCT(struct_id), nr, adr) + writestruct_nr(wd, filecode, blender::dna::sdna_struct_id_get(), nr, adr) /** \} */ diff --git a/source/blender/draw/engines/eevee/eevee_light.cc b/source/blender/draw/engines/eevee/eevee_light.cc index ba85dd015f6..9bb2a603c91 100644 --- a/source/blender/draw/engines/eevee/eevee_light.cc +++ b/source/blender/draw/engines/eevee/eevee_light.cc @@ -15,7 +15,9 @@ #include "eevee_light.hh" #include "BLI_math_rotation.h" + #include "DNA_defaults.h" +#include "DNA_sdna_type_ids.hh" namespace blender::eevee { @@ -371,7 +373,7 @@ void LightModule::begin_sync() /* Create a placeholder light to be fed by the GPU after sunlight extraction. * Sunlight is disabled if power is zero. */ ::Light la = blender::dna::shallow_copy( - *(const ::Light *)DNA_default_table[SDNA_TYPE_FROM_STRUCT(Light)]); + *(const ::Light *)DNA_default_table[dna::sdna_struct_id_get<::Light>()]); la.type = LA_SUN; /* Set on the GPU. */ la.r = la.g = la.b = -1.0f; /* Tag as world sun light. */ diff --git a/source/blender/makesdna/DNA_print.hh b/source/blender/makesdna/DNA_print.hh index 238a12020a8..88140e1556e 100644 --- a/source/blender/makesdna/DNA_print.hh +++ b/source/blender/makesdna/DNA_print.hh @@ -7,8 +7,7 @@ #include #include -/* For #SDNA_TYPE_FROM_STRUCT macro. */ -#include "dna_type_offsets.h" +#include "DNA_sdna_type_ids.hh" struct SDNA; struct SDNA_Struct; @@ -47,4 +46,4 @@ void print_struct_by_id(int struct_id, const void *data); * DNA_print_struct(bNode, node); */ #define DNA_print_struct(struct_name, data_ptr) \ - blender::dna::print_struct_by_id(SDNA_TYPE_FROM_STRUCT(struct_name), data_ptr) + blender::dna::print_struct_by_id(blender::dna::sdna_struct_id_get(), data_ptr) diff --git a/source/blender/makesdna/DNA_sdna_type_ids.hh b/source/blender/makesdna/DNA_sdna_type_ids.hh new file mode 100644 index 00000000000..5abf5d67f8f --- /dev/null +++ b/source/blender/makesdna/DNA_sdna_type_ids.hh @@ -0,0 +1,25 @@ +/* SPDX-FileCopyrightText: 2025 Blender Authors + * + * SPDX-License-Identifier: GPL-2.0-or-later */ + +/** \file + * \ingroup DNA + */ + +#pragma once + +namespace blender::dna { + +/** + * Each DNA struct has an integer identifier which is unique within a specific Blender build, but + * not necessarily across different builds. The identifier can be used to index into + * `SDNA.structs`. + */ +template int sdna_struct_id_get(); + +/** + * The maximum identifier that will be returned by #sdna_struct_id_get in this Blender build. + */ +int sdna_struct_id_get_max(); + +} // namespace blender::dna diff --git a/source/blender/makesdna/intern/CMakeLists.txt b/source/blender/makesdna/intern/CMakeLists.txt index 399c406dabd..ba77fed35ed 100644 --- a/source/blender/makesdna/intern/CMakeLists.txt +++ b/source/blender/makesdna/intern/CMakeLists.txt @@ -104,12 +104,14 @@ add_custom_command( ${CMAKE_CURRENT_BINARY_DIR}/dna.cc ${CMAKE_CURRENT_BINARY_DIR}/dna_type_offsets.h ${CMAKE_CURRENT_BINARY_DIR}/dna_verify.cc - COMMAND + ${CMAKE_CURRENT_BINARY_DIR}/dna_struct_ids.cc + COMMAND ${CMAKE_COMMAND} -E env ${PLATFORM_ENV_BUILD} "$" ${CMAKE_CURRENT_BINARY_DIR}/dna.cc ${CMAKE_CURRENT_BINARY_DIR}/dna_type_offsets.h ${CMAKE_CURRENT_BINARY_DIR}/dna_verify.cc + ${CMAKE_CURRENT_BINARY_DIR}/dna_struct_ids.cc ${CMAKE_SOURCE_DIR}/source/blender/makesdna/ DEPENDS makesdna ) @@ -123,6 +125,7 @@ set(SRC dna_utils.cc ${CMAKE_CURRENT_BINARY_DIR}/dna.cc ${CMAKE_CURRENT_BINARY_DIR}/dna_verify.cc + ${CMAKE_CURRENT_BINARY_DIR}/dna_struct_ids.cc ${SRC_DNA_INC} ${CMAKE_CURRENT_BINARY_DIR}/dna_type_offsets.h @@ -134,6 +137,7 @@ set_source_files_properties( ${CMAKE_CURRENT_BINARY_DIR}/dna.cc ${CMAKE_CURRENT_BINARY_DIR}/dna_type_offsets.h ${CMAKE_CURRENT_BINARY_DIR}/dna_verify.cc + ${CMAKE_CURRENT_BINARY_DIR}/dna_struct_ids.cc PROPERTIES GENERATED TRUE ) diff --git a/source/blender/makesdna/intern/makesdna.cc b/source/blender/makesdna/intern/makesdna.cc index 72b54f33f81..4eff6939ad5 100644 --- a/source/blender/makesdna/intern/makesdna.cc +++ b/source/blender/makesdna/intern/makesdna.cc @@ -1262,10 +1262,8 @@ void print_struct_sizes() printf("*** End of list\n"); } -static int make_structDNA(const char *base_directory, - FILE *file, - FILE *file_offsets, - FILE *file_verify) +static int make_structDNA( + const char *base_directory, FILE *file, FILE *file_offsets, FILE *file_verify, FILE *file_ids) { if (debugSDNA > 0) { fflush(stdout); @@ -1483,6 +1481,26 @@ static int make_structDNA(const char *base_directory, fprintf(file_offsets, "};\n\n"); } + { + fprintf(file_ids, "\n\nnamespace blender::dna {\n\n"); + fprintf(file_ids, "template int sdna_struct_id_get();\n\n"); + fprintf(file_ids, "int sdna_struct_id_get_max();\n"); + fprintf(file_ids, "int sdna_struct_id_get_max() { return %d; }\n", structs_num - 1); + fprintf(file_ids, "\n}\n"); + + /* Starting at 1, because 0 is "raw data". */ + for (int i = 1; i < structs_num; i++) { + const short *structpoin = structs[i]; + const int struct_type_index = structpoin[0]; + const char *name = version_struct_alias_from_static(types[struct_type_index]); + fprintf(file_ids, "struct %s;\n", name); + fprintf(file_ids, + "template<> int blender::dna::sdna_struct_id_get<%s>() { return %d; }\n", + name, + i); + } + } + /* Check versioning errors which could cause duplicate names, * do last because names are stripped. */ { @@ -1557,14 +1575,15 @@ int main(int argc, char **argv) { int return_status = 0; - if (!ELEM(argc, 4, 5)) { - printf("Usage: %s dna.c dna_struct_offsets.h [base directory]\n", argv[0]); + if (!ELEM(argc, 5, 6)) { + printf("Usage: %s dna.c dna_struct_offsets.h dna_struct_ids.cc [base directory]\n", argv[0]); return_status = 1; } else { FILE *file_dna = fopen(argv[1], "w"); FILE *file_dna_offsets = fopen(argv[2], "w"); FILE *file_dna_verify = fopen(argv[3], "w"); + FILE *file_dna_ids = fopen(argv[4], "w"); if (!file_dna) { printf("Unable to open file: %s\n", argv[1]); return_status = 1; @@ -1577,11 +1596,15 @@ int main(int argc, char **argv) printf("Unable to open file: %s\n", argv[3]); return_status = 1; } + else if (!file_dna_ids) { + printf("Unable to open file: %s\n", argv[4]); + return_status = 1; + } else { const char *base_directory; - if (argc == 5) { - base_directory = argv[4]; + if (argc == 6) { + base_directory = argv[5]; } else { base_directory = BASE_HEADER; @@ -1599,7 +1622,9 @@ int main(int argc, char **argv) fprintf(file_dna, "const unsigned char" FORCE_ALIGN_4 "DNAstr[] = {\n"); #undef FORCE_ALIGN_4 - if (make_structDNA(base_directory, file_dna, file_dna_offsets, file_dna_verify)) { + if (make_structDNA( + base_directory, file_dna, file_dna_offsets, file_dna_verify, file_dna_ids)) + { /* error */ fclose(file_dna); file_dna = nullptr; @@ -1622,6 +1647,9 @@ int main(int argc, char **argv) if (file_dna_verify) { fclose(file_dna_verify); } + if (file_dna_ids) { + fclose(file_dna_ids); + } } return return_status; diff --git a/source/blender/nodes/geometry/nodes/node_geo_attribute_capture.cc b/source/blender/nodes/geometry/nodes/node_geo_attribute_capture.cc index f2bc0a20c43..eac2c120ceb 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_attribute_capture.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_attribute_capture.cc @@ -284,8 +284,8 @@ namespace blender::nodes { StructRNA *CaptureAttributeItemsAccessor::item_srna = &RNA_NodeGeometryCaptureAttributeItem; int CaptureAttributeItemsAccessor::node_type = GEO_NODE_CAPTURE_ATTRIBUTE; -int CaptureAttributeItemsAccessor::item_dna_type = SDNA_TYPE_FROM_STRUCT( - NodeGeometryAttributeCaptureItem); +int CaptureAttributeItemsAccessor::item_dna_type = + dna::sdna_struct_id_get(); void CaptureAttributeItemsAccessor::blend_write_item(BlendWriter *writer, const ItemT &item) { diff --git a/source/blender/nodes/geometry/nodes/node_geo_bake.cc b/source/blender/nodes/geometry/nodes/node_geo_bake.cc index fa64f2924db..2a4f72fcffd 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_bake.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_bake.cc @@ -912,7 +912,7 @@ std::unique_ptr get_bake_lazy_function( StructRNA *BakeItemsAccessor::item_srna = &RNA_NodeGeometryBakeItem; int BakeItemsAccessor::node_type = GEO_NODE_BAKE; -int BakeItemsAccessor::item_dna_type = SDNA_TYPE_FROM_STRUCT(NodeGeometryBakeItem); +int BakeItemsAccessor::item_dna_type = dna::sdna_struct_id_get(); void BakeItemsAccessor::blend_write_item(BlendWriter *writer, const ItemT &item) { diff --git a/source/blender/nodes/geometry/nodes/node_geo_closure.cc b/source/blender/nodes/geometry/nodes/node_geo_closure.cc index e63e0f28b04..e90103acf94 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_closure.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_closure.cc @@ -301,7 +301,8 @@ namespace blender::nodes { StructRNA *ClosureInputItemsAccessor::item_srna = &RNA_NodeGeometryClosureInputItem; int ClosureInputItemsAccessor::node_type = GEO_NODE_CLOSURE_OUTPUT; -int ClosureInputItemsAccessor::item_dna_type = SDNA_TYPE_FROM_STRUCT(NodeGeometryClosureInputItem); +int ClosureInputItemsAccessor::item_dna_type = + dna::sdna_struct_id_get(); void ClosureInputItemsAccessor::blend_write_item(BlendWriter *writer, const ItemT &item) { @@ -315,8 +316,8 @@ void ClosureInputItemsAccessor::blend_read_data_item(BlendDataReader *reader, It StructRNA *ClosureOutputItemsAccessor::item_srna = &RNA_NodeGeometryClosureOutputItem; int ClosureOutputItemsAccessor::node_type = GEO_NODE_CLOSURE_OUTPUT; -int ClosureOutputItemsAccessor::item_dna_type = SDNA_TYPE_FROM_STRUCT( - NodeGeometryClosureOutputItem); +int ClosureOutputItemsAccessor::item_dna_type = + dna::sdna_struct_id_get(); void ClosureOutputItemsAccessor::blend_write_item(BlendWriter *writer, const ItemT &item) { diff --git a/source/blender/nodes/geometry/nodes/node_geo_combine_bundle.cc b/source/blender/nodes/geometry/nodes/node_geo_combine_bundle.cc index a6b13f52d62..7f4267a3538 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_combine_bundle.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_combine_bundle.cc @@ -141,8 +141,8 @@ namespace blender::nodes { StructRNA *CombineBundleItemsAccessor::item_srna = &RNA_NodeGeometryCombineBundleItem; int CombineBundleItemsAccessor::node_type = GEO_NODE_COMBINE_BUNDLE; -int CombineBundleItemsAccessor::item_dna_type = SDNA_TYPE_FROM_STRUCT( - NodeGeometryCombineBundleItem); +int CombineBundleItemsAccessor::item_dna_type = + dna::sdna_struct_id_get(); void CombineBundleItemsAccessor::blend_write_item(BlendWriter *writer, const ItemT &item) { diff --git a/source/blender/nodes/geometry/nodes/node_geo_evaluate_closure.cc b/source/blender/nodes/geometry/nodes/node_geo_evaluate_closure.cc index 33f281330ab..395b11d6dbb 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_evaluate_closure.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_evaluate_closure.cc @@ -139,8 +139,8 @@ namespace blender::nodes { StructRNA *EvaluateClosureInputItemsAccessor::item_srna = &RNA_NodeGeometryEvaluateClosureInputItem; int EvaluateClosureInputItemsAccessor::node_type = GEO_NODE_EVALUATE_CLOSURE; -int EvaluateClosureInputItemsAccessor::item_dna_type = SDNA_TYPE_FROM_STRUCT( - NodeGeometryEvaluateClosureInputItem); +int EvaluateClosureInputItemsAccessor::item_dna_type = + dna::sdna_struct_id_get(); void EvaluateClosureInputItemsAccessor::blend_write_item(BlendWriter *writer, const ItemT &item) { @@ -155,8 +155,8 @@ void EvaluateClosureInputItemsAccessor::blend_read_data_item(BlendDataReader *re StructRNA *EvaluateClosureOutputItemsAccessor::item_srna = &RNA_NodeGeometryEvaluateClosureOutputItem; int EvaluateClosureOutputItemsAccessor::node_type = GEO_NODE_EVALUATE_CLOSURE; -int EvaluateClosureOutputItemsAccessor::item_dna_type = SDNA_TYPE_FROM_STRUCT( - NodeGeometryEvaluateClosureOutputItem); +int EvaluateClosureOutputItemsAccessor::item_dna_type = + dna::sdna_struct_id_get(); void EvaluateClosureOutputItemsAccessor::blend_write_item(BlendWriter *writer, const ItemT &item) { diff --git a/source/blender/nodes/geometry/nodes/node_geo_foreach_geometry_element.cc b/source/blender/nodes/geometry/nodes/node_geo_foreach_geometry_element.cc index 31ac093dccd..29ff45b9fae 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_foreach_geometry_element.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_foreach_geometry_element.cc @@ -453,8 +453,8 @@ namespace blender::nodes { StructRNA *ForeachGeometryElementInputItemsAccessor::item_srna = &RNA_ForeachGeometryElementInputItem; int ForeachGeometryElementInputItemsAccessor::node_type = GEO_NODE_FOREACH_GEOMETRY_ELEMENT_OUTPUT; -int ForeachGeometryElementInputItemsAccessor::item_dna_type = SDNA_TYPE_FROM_STRUCT( - NodeForeachGeometryElementInputItem); +int ForeachGeometryElementInputItemsAccessor::item_dna_type = + dna::sdna_struct_id_get(); void ForeachGeometryElementInputItemsAccessor::blend_write_item(BlendWriter *writer, const ItemT &item) @@ -471,8 +471,8 @@ void ForeachGeometryElementInputItemsAccessor::blend_read_data_item(BlendDataRea StructRNA *ForeachGeometryElementMainItemsAccessor::item_srna = &RNA_ForeachGeometryElementMainItem; int ForeachGeometryElementMainItemsAccessor::node_type = GEO_NODE_FOREACH_GEOMETRY_ELEMENT_OUTPUT; -int ForeachGeometryElementMainItemsAccessor::item_dna_type = SDNA_TYPE_FROM_STRUCT( - NodeForeachGeometryElementMainItem); +int ForeachGeometryElementMainItemsAccessor::item_dna_type = + dna::sdna_struct_id_get(); void ForeachGeometryElementMainItemsAccessor::blend_write_item(BlendWriter *writer, const ItemT &item) @@ -490,8 +490,8 @@ StructRNA *ForeachGeometryElementGenerationItemsAccessor::item_srna = &RNA_ForeachGeometryElementGenerationItem; int ForeachGeometryElementGenerationItemsAccessor::node_type = GEO_NODE_FOREACH_GEOMETRY_ELEMENT_OUTPUT; -int ForeachGeometryElementGenerationItemsAccessor::item_dna_type = SDNA_TYPE_FROM_STRUCT( - NodeForeachGeometryElementGenerationItem); +int ForeachGeometryElementGenerationItemsAccessor::item_dna_type = + dna::sdna_struct_id_get(); void ForeachGeometryElementGenerationItemsAccessor::blend_write_item(BlendWriter *writer, const ItemT &item) diff --git a/source/blender/nodes/geometry/nodes/node_geo_index_switch.cc b/source/blender/nodes/geometry/nodes/node_geo_index_switch.cc index 3cf5dd954c1..0738459c37f 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_index_switch.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_index_switch.cc @@ -410,7 +410,7 @@ std::unique_ptr get_index_switch_node_lazy_function( StructRNA *IndexSwitchItemsAccessor::item_srna = &RNA_IndexSwitchItem; int IndexSwitchItemsAccessor::node_type = GEO_NODE_INDEX_SWITCH; -int IndexSwitchItemsAccessor::item_dna_type = SDNA_TYPE_FROM_STRUCT(IndexSwitchItem); +int IndexSwitchItemsAccessor::item_dna_type = dna::sdna_struct_id_get(); void IndexSwitchItemsAccessor::blend_write_item(BlendWriter * /*writer*/, const ItemT & /*item*/) { diff --git a/source/blender/nodes/geometry/nodes/node_geo_menu_switch.cc b/source/blender/nodes/geometry/nodes/node_geo_menu_switch.cc index b841420e5ff..b989278d371 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_menu_switch.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_menu_switch.cc @@ -453,7 +453,7 @@ std::unique_ptr get_menu_switch_node_socket_usage_lazy_function(co StructRNA *MenuSwitchItemsAccessor::item_srna = &RNA_NodeEnumItem; int MenuSwitchItemsAccessor::node_type = GEO_NODE_MENU_SWITCH; -int MenuSwitchItemsAccessor::item_dna_type = SDNA_TYPE_FROM_STRUCT(NodeEnumItem); +int MenuSwitchItemsAccessor::item_dna_type = dna::sdna_struct_id_get(); void MenuSwitchItemsAccessor::blend_write_item(BlendWriter *writer, const ItemT &item) { diff --git a/source/blender/nodes/geometry/nodes/node_geo_repeat.cc b/source/blender/nodes/geometry/nodes/node_geo_repeat.cc index d419432a48e..fc2cbbd48c9 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_repeat.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_repeat.cc @@ -284,7 +284,7 @@ namespace blender::nodes { StructRNA *RepeatItemsAccessor::item_srna = &RNA_RepeatItem; int RepeatItemsAccessor::node_type = GEO_NODE_REPEAT_OUTPUT; -int RepeatItemsAccessor::item_dna_type = SDNA_TYPE_FROM_STRUCT(NodeRepeatItem); +int RepeatItemsAccessor::item_dna_type = dna::sdna_struct_id_get(); void RepeatItemsAccessor::blend_write_item(BlendWriter *writer, const ItemT &item) { diff --git a/source/blender/nodes/geometry/nodes/node_geo_separate_bundle.cc b/source/blender/nodes/geometry/nodes/node_geo_separate_bundle.cc index e56ec2d7fe2..16060e370d7 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_separate_bundle.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_separate_bundle.cc @@ -158,8 +158,8 @@ namespace blender::nodes { StructRNA *SeparateBundleItemsAccessor::item_srna = &RNA_NodeGeometrySeparateBundleItem; int SeparateBundleItemsAccessor::node_type = GEO_NODE_SEPARATE_BUNDLE; -int SeparateBundleItemsAccessor::item_dna_type = SDNA_TYPE_FROM_STRUCT( - NodeGeometrySeparateBundleItem); +int SeparateBundleItemsAccessor::item_dna_type = + dna::sdna_struct_id_get(); void SeparateBundleItemsAccessor::blend_write_item(BlendWriter *writer, const ItemT &item) { diff --git a/source/blender/nodes/geometry/nodes/node_geo_simulation.cc b/source/blender/nodes/geometry/nodes/node_geo_simulation.cc index fa89fbdecdc..a20f7e2ef9e 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_simulation.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_simulation.cc @@ -980,7 +980,7 @@ void mix_baked_data_item(const eNodeSocketDatatype socket_type, StructRNA *SimulationItemsAccessor::item_srna = &RNA_SimulationStateItem; int SimulationItemsAccessor::node_type = GEO_NODE_SIMULATION_OUTPUT; -int SimulationItemsAccessor::item_dna_type = SDNA_TYPE_FROM_STRUCT(NodeSimulationItem); +int SimulationItemsAccessor::item_dna_type = dna::sdna_struct_id_get(); void SimulationItemsAccessor::blend_write_item(BlendWriter *writer, const ItemT &item) {