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<typename T> 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<typename T> int sdna_struct_id_get();

int sdna_struct_id_get_max();
int sdna_struct_id_get_max() { return 951; }

}
struct IDPropertyUIData;
template<> int blender:🧬:sdna_struct_id_get<IDPropertyUIData>() { return 1; }
struct IDPropertyUIDataEnumItem;
template<> int blender:🧬:sdna_struct_id_get<IDPropertyUIDataEnumItem>() { 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
This commit is contained in:
Jacques Lucke 2025-05-12 11:16:26 +02:00
parent a96ecd2834
commit 9fd7a093c9
19 changed files with 155 additions and 95 deletions

View File

@ -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<LineStyleColorModifier_AlongStroke>();
break;
case LS_MODIFIER_DISTANCE_FROM_CAMERA:
struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleColorModifier_DistanceFromCamera);
struct_nr = sdna_struct_id_get<LineStyleColorModifier_DistanceFromCamera>();
break;
case LS_MODIFIER_DISTANCE_FROM_OBJECT:
struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleColorModifier_DistanceFromObject);
struct_nr = sdna_struct_id_get<LineStyleColorModifier_DistanceFromObject>();
break;
case LS_MODIFIER_MATERIAL:
struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleColorModifier_Material);
struct_nr = sdna_struct_id_get<LineStyleColorModifier_Material>();
break;
case LS_MODIFIER_TANGENT:
struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleColorModifier_Tangent);
struct_nr = sdna_struct_id_get<LineStyleColorModifier_Tangent>();
break;
case LS_MODIFIER_NOISE:
struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleColorModifier_Noise);
struct_nr = sdna_struct_id_get<LineStyleColorModifier_Noise>();
break;
case LS_MODIFIER_CREASE_ANGLE:
struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleColorModifier_CreaseAngle);
struct_nr = sdna_struct_id_get<LineStyleColorModifier_CreaseAngle>();
break;
case LS_MODIFIER_CURVATURE_3D:
struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleColorModifier_Curvature_3D);
struct_nr = sdna_struct_id_get<LineStyleColorModifier_Curvature_3D>();
break;
default:
struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleModifier); /* this should not happen */
struct_nr = sdna_struct_id_get<LineStyleModifier>(); /* 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<LineStyleAlphaModifier_AlongStroke>();
break;
case LS_MODIFIER_DISTANCE_FROM_CAMERA:
struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleAlphaModifier_DistanceFromCamera);
struct_nr = sdna_struct_id_get<LineStyleAlphaModifier_DistanceFromCamera>();
break;
case LS_MODIFIER_DISTANCE_FROM_OBJECT:
struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleAlphaModifier_DistanceFromObject);
struct_nr = sdna_struct_id_get<LineStyleAlphaModifier_DistanceFromObject>();
break;
case LS_MODIFIER_MATERIAL:
struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleAlphaModifier_Material);
struct_nr = sdna_struct_id_get<LineStyleAlphaModifier_Material>();
break;
case LS_MODIFIER_TANGENT:
struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleAlphaModifier_Tangent);
struct_nr = sdna_struct_id_get<LineStyleAlphaModifier_Tangent>();
break;
case LS_MODIFIER_NOISE:
struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleAlphaModifier_Noise);
struct_nr = sdna_struct_id_get<LineStyleAlphaModifier_Noise>();
break;
case LS_MODIFIER_CREASE_ANGLE:
struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleAlphaModifier_CreaseAngle);
struct_nr = sdna_struct_id_get<LineStyleAlphaModifier_CreaseAngle>();
break;
case LS_MODIFIER_CURVATURE_3D:
struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleAlphaModifier_Curvature_3D);
struct_nr = sdna_struct_id_get<LineStyleAlphaModifier_Curvature_3D>();
break;
default:
struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleModifier); /* this should not happen */
struct_nr = sdna_struct_id_get<LineStyleModifier>(); /* 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<LineStyleThicknessModifier_AlongStroke>();
break;
case LS_MODIFIER_DISTANCE_FROM_CAMERA:
struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleThicknessModifier_DistanceFromCamera);
struct_nr = sdna_struct_id_get<LineStyleThicknessModifier_DistanceFromCamera>();
break;
case LS_MODIFIER_DISTANCE_FROM_OBJECT:
struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleThicknessModifier_DistanceFromObject);
struct_nr = sdna_struct_id_get<LineStyleThicknessModifier_DistanceFromObject>();
break;
case LS_MODIFIER_MATERIAL:
struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleThicknessModifier_Material);
struct_nr = sdna_struct_id_get<LineStyleThicknessModifier_Material>();
break;
case LS_MODIFIER_CALLIGRAPHY:
struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleThicknessModifier_Calligraphy);
struct_nr = sdna_struct_id_get<LineStyleThicknessModifier_Calligraphy>();
break;
case LS_MODIFIER_TANGENT:
struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleThicknessModifier_Tangent);
struct_nr = sdna_struct_id_get<LineStyleThicknessModifier_Tangent>();
break;
case LS_MODIFIER_NOISE:
struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleThicknessModifier_Noise);
struct_nr = sdna_struct_id_get<LineStyleThicknessModifier_Noise>();
break;
case LS_MODIFIER_CREASE_ANGLE:
struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleThicknessModifier_CreaseAngle);
struct_nr = sdna_struct_id_get<LineStyleThicknessModifier_CreaseAngle>();
break;
case LS_MODIFIER_CURVATURE_3D:
struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleThicknessModifier_Curvature_3D);
struct_nr = sdna_struct_id_get<LineStyleThicknessModifier_Curvature_3D>();
break;
default:
struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleModifier); /* this should not happen */
struct_nr = sdna_struct_id_get<LineStyleModifier>(); /* 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<LineStyleGeometryModifier_Sampling>();
break;
case LS_MODIFIER_BEZIER_CURVE:
struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleGeometryModifier_BezierCurve);
struct_nr = sdna_struct_id_get<LineStyleGeometryModifier_BezierCurve>();
break;
case LS_MODIFIER_SINUS_DISPLACEMENT:
struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleGeometryModifier_SinusDisplacement);
struct_nr = sdna_struct_id_get<LineStyleGeometryModifier_SinusDisplacement>();
break;
case LS_MODIFIER_SPATIAL_NOISE:
struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleGeometryModifier_SpatialNoise);
struct_nr = sdna_struct_id_get<LineStyleGeometryModifier_SpatialNoise>();
break;
case LS_MODIFIER_PERLIN_NOISE_1D:
struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleGeometryModifier_PerlinNoise1D);
struct_nr = sdna_struct_id_get<LineStyleGeometryModifier_PerlinNoise1D>();
break;
case LS_MODIFIER_PERLIN_NOISE_2D:
struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleGeometryModifier_PerlinNoise2D);
struct_nr = sdna_struct_id_get<LineStyleGeometryModifier_PerlinNoise2D>();
break;
case LS_MODIFIER_BACKBONE_STRETCHER:
struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleGeometryModifier_BackboneStretcher);
struct_nr = sdna_struct_id_get<LineStyleGeometryModifier_BackboneStretcher>();
break;
case LS_MODIFIER_TIP_REMOVER:
struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleGeometryModifier_TipRemover);
struct_nr = sdna_struct_id_get<LineStyleGeometryModifier_TipRemover>();
break;
case LS_MODIFIER_POLYGONIZATION:
struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleGeometryModifier_Polygonalization);
struct_nr = sdna_struct_id_get<LineStyleGeometryModifier_Polygonalization>();
break;
case LS_MODIFIER_GUIDING_LINES:
struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleGeometryModifier_GuidingLines);
struct_nr = sdna_struct_id_get<LineStyleGeometryModifier_GuidingLines>();
break;
case LS_MODIFIER_BLUEPRINT:
struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleGeometryModifier_Blueprint);
struct_nr = sdna_struct_id_get<LineStyleGeometryModifier_Blueprint>();
break;
case LS_MODIFIER_2D_OFFSET:
struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleGeometryModifier_2DOffset);
struct_nr = sdna_struct_id_get<LineStyleGeometryModifier_2DOffset>();
break;
case LS_MODIFIER_2D_TRANSFORM:
struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleGeometryModifier_2DTransform);
struct_nr = sdna_struct_id_get<LineStyleGeometryModifier_2DTransform>();
break;
case LS_MODIFIER_SIMPLIFICATION:
struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleGeometryModifier_Simplification);
struct_nr = sdna_struct_id_get<LineStyleGeometryModifier_Simplification>();
break;
default:
struct_nr = SDNA_TYPE_FROM_STRUCT(LineStyleModifier); /* this should not happen */
struct_nr = sdna_struct_id_get<LineStyleModifier>(); /* this should not happen */
}
BLO_write_struct_by_id(writer, struct_nr, m);
}

View File

@ -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<struct_name>(), 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<struct_name>(), 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<struct_name>(), 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<struct_name>(), 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<struct_name>(), 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<struct_name>(), 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<struct_name>(), id_address, id)
/**
* Specific code to prepare IDs to be written.

View File

@ -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<struct_id>(), 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<struct_id>(), nr, adr)
/** \} */

View File

@ -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. */

View File

@ -7,8 +7,7 @@
#include <cstdint>
#include <iosfwd>
/* 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<struct_name>(), data_ptr)

View File

@ -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<typename T> 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

View File

@ -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}
"$<TARGET_FILE:makesdna>"
${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
)

View File

@ -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<typename T> 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;

View File

@ -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<NodeGeometryAttributeCaptureItem>();
void CaptureAttributeItemsAccessor::blend_write_item(BlendWriter *writer, const ItemT &item)
{

View File

@ -912,7 +912,7 @@ std::unique_ptr<LazyFunction> 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<NodeGeometryBakeItem>();
void BakeItemsAccessor::blend_write_item(BlendWriter *writer, const ItemT &item)
{

View File

@ -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<NodeGeometryClosureInputItem>();
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<NodeGeometryClosureOutputItem>();
void ClosureOutputItemsAccessor::blend_write_item(BlendWriter *writer, const ItemT &item)
{

View File

@ -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<NodeGeometryCombineBundleItem>();
void CombineBundleItemsAccessor::blend_write_item(BlendWriter *writer, const ItemT &item)
{

View File

@ -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<NodeGeometryEvaluateClosureInputItem>();
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<NodeGeometryEvaluateClosureOutputItem>();
void EvaluateClosureOutputItemsAccessor::blend_write_item(BlendWriter *writer, const ItemT &item)
{

View File

@ -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<NodeForeachGeometryElementInputItem>();
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<NodeForeachGeometryElementMainItem>();
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<NodeForeachGeometryElementGenerationItem>();
void ForeachGeometryElementGenerationItemsAccessor::blend_write_item(BlendWriter *writer,
const ItemT &item)

View File

@ -410,7 +410,7 @@ std::unique_ptr<LazyFunction> 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<IndexSwitchItem>();
void IndexSwitchItemsAccessor::blend_write_item(BlendWriter * /*writer*/, const ItemT & /*item*/)
{

View File

@ -453,7 +453,7 @@ std::unique_ptr<LazyFunction> 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<NodeEnumItem>();
void MenuSwitchItemsAccessor::blend_write_item(BlendWriter *writer, const ItemT &item)
{

View File

@ -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<NodeRepeatItem>();
void RepeatItemsAccessor::blend_write_item(BlendWriter *writer, const ItemT &item)
{

View File

@ -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<NodeGeometrySeparateBundleItem>();
void SeparateBundleItemsAccessor::blend_write_item(BlendWriter *writer, const ItemT &item)
{

View File

@ -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<NodeSimulationItem>();
void SimulationItemsAccessor::blend_write_item(BlendWriter *writer, const ItemT &item)
{