Changeset View
Changeset View
Standalone View
Standalone View
source/blender/blenkernel/intern/customdata.c
| Show First 20 Lines • Show All 53 Lines • ▼ Show 20 Lines | |||||
| #include "BKE_customdata_file.h" | #include "BKE_customdata_file.h" | ||||
| #include "BKE_main.h" | #include "BKE_main.h" | ||||
| #include "BKE_mesh_mapping.h" | #include "BKE_mesh_mapping.h" | ||||
| #include "BKE_mesh_remap.h" | #include "BKE_mesh_remap.h" | ||||
| #include "BKE_multires.h" | #include "BKE_multires.h" | ||||
| #include "bmesh.h" | #include "bmesh.h" | ||||
| #include "CLG_log.h" | |||||
| /* only for customdata_data_transfer_interp_normal_normals */ | /* only for customdata_data_transfer_interp_normal_normals */ | ||||
| #include "data_transfer_intern.h" | #include "data_transfer_intern.h" | ||||
| /* number of layers to add when growing a CustomData object */ | /* number of layers to add when growing a CustomData object */ | ||||
| #define CUSTOMDATA_GROW 5 | #define CUSTOMDATA_GROW 5 | ||||
| /* ensure typemap size is ok */ | /* ensure typemap size is ok */ | ||||
| BLI_STATIC_ASSERT(ARRAY_SIZE(((CustomData *)NULL)->typemap) == CD_NUMTYPES, "size mismatch"); | BLI_STATIC_ASSERT(ARRAY_SIZE(((CustomData *)NULL)->typemap) == CD_NUMTYPES, "size mismatch"); | ||||
| static CLG_LogRef LOG = {"bke.customdata"}; | |||||
| /********************* Layer type information **********************/ | /********************* Layer type information **********************/ | ||||
| typedef struct LayerTypeInfo { | typedef struct LayerTypeInfo { | ||||
| int size; /* the memory size of one element of this layer's data */ | int size; /* the memory size of one element of this layer's data */ | ||||
| /** name of the struct used, for file writing */ | /** name of the struct used, for file writing */ | ||||
| const char *structname; | const char *structname; | ||||
| /** number of structs per element, for file writing */ | /** number of structs per element, for file writing */ | ||||
| ▲ Show 20 Lines • Show All 536 Lines • ▼ Show 20 Lines | static int layerRead_mdisps(CDataFile *cdf, void *data, int count) | ||||
| MDisps *d = data; | MDisps *d = data; | ||||
| int i; | int i; | ||||
| for (i = 0; i < count; ++i) { | for (i = 0; i < count; ++i) { | ||||
| if (!d[i].disps) | if (!d[i].disps) | ||||
| d[i].disps = MEM_calloc_arrayN(d[i].totdisp, 3 * sizeof(float), "mdisps read"); | d[i].disps = MEM_calloc_arrayN(d[i].totdisp, 3 * sizeof(float), "mdisps read"); | ||||
| if (!cdf_read_data(cdf, d[i].totdisp * 3 * sizeof(float), d[i].disps)) { | if (!cdf_read_data(cdf, d[i].totdisp * 3 * sizeof(float), d[i].disps)) { | ||||
| printf("failed to read multires displacement %d/%d %d\n", i, count, d[i].totdisp); | CLOG_ERROR(&LOG, "failed to read multires displacement %d/%d %d", i, count, d[i].totdisp); | ||||
| return 0; | return 0; | ||||
| } | } | ||||
| } | } | ||||
| return 1; | return 1; | ||||
| } | } | ||||
| static int layerWrite_mdisps(CDataFile *cdf, const void *data, int count) | static int layerWrite_mdisps(CDataFile *cdf, const void *data, int count) | ||||
| { | { | ||||
| const MDisps *d = data; | const MDisps *d = data; | ||||
| int i; | int i; | ||||
| for (i = 0; i < count; ++i) { | for (i = 0; i < count; ++i) { | ||||
| if (!cdf_write_data(cdf, d[i].totdisp * 3 * sizeof(float), d[i].disps)) { | if (!cdf_write_data(cdf, d[i].totdisp * 3 * sizeof(float), d[i].disps)) { | ||||
| printf("failed to write multires displacement %d/%d %d\n", i, count, d[i].totdisp); | CLOG_ERROR(&LOG, "failed to write multires displacement %d/%d %d", i, count, d[i].totdisp); | ||||
| return 0; | return 0; | ||||
| } | } | ||||
| } | } | ||||
| return 1; | return 1; | ||||
| } | } | ||||
| static size_t layerFilesize_mdisps(CDataFile *UNUSED(cdf), const void *data, int count) | static size_t layerFilesize_mdisps(CDataFile *UNUSED(cdf), const void *data, int count) | ||||
| ▲ Show 20 Lines • Show All 1,589 Lines • ▼ Show 20 Lines | static void CustomData_copy_data_layer( | ||||
| typeInfo = layerType_getInfo(source->layers[src_i].type); | typeInfo = layerType_getInfo(source->layers[src_i].type); | ||||
| const size_t src_offset = (size_t)src_index * typeInfo->size; | const size_t src_offset = (size_t)src_index * typeInfo->size; | ||||
| const size_t dst_offset = (size_t)dst_index * typeInfo->size; | const size_t dst_offset = (size_t)dst_index * typeInfo->size; | ||||
| if (!count || !src_data || !dst_data) { | if (!count || !src_data || !dst_data) { | ||||
| if (count && !(src_data == NULL && dst_data == NULL)) { | if (count && !(src_data == NULL && dst_data == NULL)) { | ||||
| printf("%s: warning null data for %s type (%p --> %p), skipping\n", | CLOG_WARN(&LOG, "null data for %s type (%p --> %p), skipping", | ||||
| __func__, layerType_getName(source->layers[src_i].type), | layerType_getName(source->layers[src_i].type), | ||||
| (void *)src_data, (void *)dst_data); | (void *)src_data, (void *)dst_data); | ||||
| } | } | ||||
| return; | return; | ||||
| } | } | ||||
| if (typeInfo->copy) { | if (typeInfo->copy) { | ||||
| typeInfo->copy(POINTER_OFFSET(src_data, src_offset), | typeInfo->copy(POINTER_OFFSET(src_data, src_offset), | ||||
| POINTER_OFFSET(dst_data, dst_offset), | POINTER_OFFSET(dst_data, dst_offset), | ||||
| count); | count); | ||||
| ▲ Show 20 Lines • Show All 1,143 Lines • ▼ Show 20 Lines | void CustomData_file_write_prepare( | ||||
| const int totlayer = data->totlayer; | const int totlayer = data->totlayer; | ||||
| int i, j; | int i, j; | ||||
| for (i = 0, j = 0; i < totlayer; i++) { | for (i = 0, j = 0; i < totlayer; i++) { | ||||
| CustomDataLayer *layer = &data->layers[i]; | CustomDataLayer *layer = &data->layers[i]; | ||||
| if (layer->flag & CD_FLAG_NOCOPY) { /* Layers with this flag set are not written to file. */ | if (layer->flag & CD_FLAG_NOCOPY) { /* Layers with this flag set are not written to file. */ | ||||
| data->totlayer--; | data->totlayer--; | ||||
| /* printf("%s: skipping layer %p (%s)\n", __func__, layer, layer->name); */ | /* CLOG_WARN(&LOG, "skipping layer %p (%s)", layer, layer->name); */ | ||||
| } | } | ||||
| else { | else { | ||||
| if (UNLIKELY((size_t)j >= write_layers_size)) { | if (UNLIKELY((size_t)j >= write_layers_size)) { | ||||
| if (write_layers == write_layers_buff) { | if (write_layers == write_layers_buff) { | ||||
| write_layers = MEM_malloc_arrayN((write_layers_size + chunk_size), sizeof(*write_layers), __func__); | write_layers = MEM_malloc_arrayN((write_layers_size + chunk_size), sizeof(*write_layers), __func__); | ||||
| if (write_layers_buff) { | if (write_layers_buff) { | ||||
| memcpy(write_layers, write_layers_buff, sizeof(*write_layers) * write_layers_size); | memcpy(write_layers, write_layers_buff, sizeof(*write_layers) * write_layers_size); | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 236 Lines • ▼ Show 20 Lines | void CustomData_external_read(CustomData *data, ID *id, CustomDataMask mask, int totelem) | ||||
| if (!update) | if (!update) | ||||
| return; | return; | ||||
| customdata_external_filename(filename, id, external); | customdata_external_filename(filename, id, external); | ||||
| cdf = cdf_create(CDF_TYPE_MESH); | cdf = cdf_create(CDF_TYPE_MESH); | ||||
| if (!cdf_read_open(cdf, filename)) { | if (!cdf_read_open(cdf, filename)) { | ||||
| cdf_free(cdf); | cdf_free(cdf); | ||||
| fprintf(stderr, "Failed to read %s layer from %s.\n", layerType_getName(layer->type), filename); | CLOG_ERROR(&LOG, "Failed to read %s layer from %s.", layerType_getName(layer->type), filename); | ||||
| return; | return; | ||||
| } | } | ||||
| for (i = 0; i < data->totlayer; i++) { | for (i = 0; i < data->totlayer; i++) { | ||||
| layer = &data->layers[i]; | layer = &data->layers[i]; | ||||
| typeInfo = layerType_getInfo(layer->type); | typeInfo = layerType_getInfo(layer->type); | ||||
| if (!(mask & CD_TYPE_AS_MASK(layer->type))) { | if (!(mask & CD_TYPE_AS_MASK(layer->type))) { | ||||
| ▲ Show 20 Lines • Show All 72 Lines • ▼ Show 20 Lines | if ((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->filesize) { | ||||
| else { | else { | ||||
| cdf_free(cdf); | cdf_free(cdf); | ||||
| return; /* read failed for a layer! */ | return; /* read failed for a layer! */ | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| if (!cdf_write_open(cdf, filename)) { | if (!cdf_write_open(cdf, filename)) { | ||||
| fprintf(stderr, "Failed to open %s for writing.\n", filename); | CLOG_ERROR(&LOG, "Failed to open %s for writing.", filename); | ||||
| cdf_free(cdf); | cdf_free(cdf); | ||||
| return; | return; | ||||
| } | } | ||||
| for (i = 0; i < data->totlayer; i++) { | for (i = 0; i < data->totlayer; i++) { | ||||
| layer = &data->layers[i]; | layer = &data->layers[i]; | ||||
| typeInfo = layerType_getInfo(layer->type); | typeInfo = layerType_getInfo(layer->type); | ||||
| Show All 10 Lines | if ((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->write) { | ||||
| } | } | ||||
| else { | else { | ||||
| break; | break; | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| if (i != data->totlayer) { | if (i != data->totlayer) { | ||||
| fprintf(stderr, "Failed to write data to %s.\n", filename); | CLOG_ERROR(&LOG, "Failed to write data to %s.", filename); | ||||
| cdf_write_close(cdf); | cdf_write_close(cdf); | ||||
| cdf_free(cdf); | cdf_free(cdf); | ||||
| return; | return; | ||||
| } | } | ||||
| for (i = 0; i < data->totlayer; i++) { | for (i = 0; i < data->totlayer; i++) { | ||||
| layer = &data->layers[i]; | layer = &data->layers[i]; | ||||
| typeInfo = layerType_getInfo(layer->type); | typeInfo = layerType_getInfo(layer->type); | ||||
| ▲ Show 20 Lines • Show All 88 Lines • ▼ Show 20 Lines | case 2: | ||||
| break; | break; | ||||
| case 4: | case 4: | ||||
| COPY_BIT_FLAG(uint32_t, dst, src, flag); | COPY_BIT_FLAG(uint32_t, dst, src, flag); | ||||
| break; | break; | ||||
| case 8: | case 8: | ||||
| COPY_BIT_FLAG(uint64_t, dst, src, flag); | COPY_BIT_FLAG(uint64_t, dst, src, flag); | ||||
| break; | break; | ||||
| default: | default: | ||||
| //printf("ERROR %s: Unknown flags-container size (%zu)\n", __func__, datasize); | //CLOG_ERROR(&LOG, "Unknown flags-container size (%zu)", datasize); | ||||
| break; | break; | ||||
| } | } | ||||
| #undef COPY_BIT_FLAG | #undef COPY_BIT_FLAG | ||||
| } | } | ||||
| static bool check_bit_flag(const void *data, const size_t data_size, const uint64_t flag) | static bool check_bit_flag(const void *data, const size_t data_size, const uint64_t flag) | ||||
| { | { | ||||
| switch (data_size) { | switch (data_size) { | ||||
| case 1: | case 1: | ||||
| return ((*((uint8_t *)data) & ((uint8_t)flag)) != 0); | return ((*((uint8_t *)data) & ((uint8_t)flag)) != 0); | ||||
| case 2: | case 2: | ||||
| return ((*((uint16_t *)data) & ((uint16_t)flag)) != 0); | return ((*((uint16_t *)data) & ((uint16_t)flag)) != 0); | ||||
| case 4: | case 4: | ||||
| return ((*((uint32_t *)data) & ((uint32_t)flag)) != 0); | return ((*((uint32_t *)data) & ((uint32_t)flag)) != 0); | ||||
| case 8: | case 8: | ||||
| return ((*((uint64_t *)data) & ((uint64_t)flag)) != 0); | return ((*((uint64_t *)data) & ((uint64_t)flag)) != 0); | ||||
| default: | default: | ||||
| //printf("ERROR %s: Unknown flags-container size (%zu)\n", __func__, datasize); | //CLOG_ERROR(&LOG, "Unknown flags-container size (%zu)", datasize); | ||||
| return false; | return false; | ||||
| } | } | ||||
| } | } | ||||
| static void customdata_data_transfer_interp_generic( | static void customdata_data_transfer_interp_generic( | ||||
| const CustomDataTransferLayerMap *laymap, void *data_dst, | const CustomDataTransferLayerMap *laymap, void *data_dst, | ||||
| const void **sources, const float *weights, const int count, | const void **sources, const float *weights, const int count, | ||||
| const float mix_factor) | const float mix_factor) | ||||
| ▲ Show 20 Lines • Show All 211 Lines • Show Last 20 Lines | |||||