Cycles: Make Blender importer more forward compatible

Basically the idea is to make code robust against extending
enum options in the future by falling back to a known safe
default setting when RNA is set to something unknown.

While this approach solves the issues similar to T47377,
but it wouldn't really help when/if any of the RNA values
gets ever deprecated and removed. There'll be no simple
solution to that apart from defining explicit mapping from
RNA value to Cycles one.

Another part which isn't so great actually is that we now
have to have some enum guards and give some explicit values
to the enum items, but we can live with that perhaps.

Reviewers: dingto, juicyfruit, lukasstockner97, brecht

Reviewed By: brecht

Differential Revision: https://developer.blender.org/D1785
This commit is contained in:
Sergey Sharybin 2016-02-10 15:09:45 +01:00
parent ec9977855f
commit 28604c46a1
17 changed files with 200 additions and 98 deletions

@ -158,21 +158,10 @@ static void blender_camera_from_object(BlenderCamera *bcam,
break;
}
switch(get_enum(ccamera, "panorama_type")) {
case 1:
bcam->panorama_type = PANORAMA_FISHEYE_EQUIDISTANT;
break;
case 2:
bcam->panorama_type = PANORAMA_FISHEYE_EQUISOLID;
break;
case 3:
bcam->panorama_type = PANORAMA_MIRRORBALL;
break;
case 0:
default:
bcam->panorama_type = PANORAMA_EQUIRECTANGULAR;
break;
}
bcam->panorama_type = (PanoramaType)get_enum(ccamera,
"panorama_type",
PANORAMA_NUM_TYPES,
PANORAMA_EQUIRECTANGULAR);
bcam->fisheye_fov = RNA_float_get(&ccamera, "fisheye_fov");
bcam->fisheye_lens = RNA_float_get(&ccamera, "fisheye_lens");
@ -460,32 +449,16 @@ void BlenderSync::sync_camera(BL::RenderSettings& b_render,
curvemapping_to_array(b_shutter_curve, bcam.shutter_curve, RAMP_TABLE_SIZE);
PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
switch(get_enum(cscene, "motion_blur_position")) {
case 0:
bcam.motion_position = Camera::MOTION_POSITION_START;
break;
case 1:
bcam.motion_position = Camera::MOTION_POSITION_CENTER;
break;
case 2:
bcam.motion_position = Camera::MOTION_POSITION_END;
break;
default:
bcam.motion_position = Camera::MOTION_POSITION_CENTER;
break;
}
switch(get_enum(cscene, "rolling_shutter_type")) {
case 0:
bcam.rolling_shutter_type = Camera::ROLLING_SHUTTER_NONE;
break;
case 1:
bcam.rolling_shutter_type = Camera::ROLLING_SHUTTER_TOP;
break;
default:
bcam.rolling_shutter_type = Camera::ROLLING_SHUTTER_NONE;
break;
}
bcam.motion_position =
(Camera::MotionPosition)get_enum(cscene,
"motion_blur_position",
Camera::MOTION_NUM_POSITIONS,
Camera::MOTION_POSITION_CENTER);
bcam.rolling_shutter_type =
(Camera::RollingShutterType)get_enum(cscene,
"rolling_shutter_type",
Camera::ROLLING_SHUTTER_NUM_TYPES,
Camera::ROLLING_SHUTTER_NONE);
bcam.rolling_shutter_duration = RNA_float_get(&cscene, "rolling_shutter_duration");
/* border */

@ -795,8 +795,16 @@ void BlenderSync::sync_curve_settings()
curve_system_manager->minimum_width = get_float(csscene, "minimum_width");
curve_system_manager->maximum_width = get_float(csscene, "maximum_width");
curve_system_manager->primitive = get_enum(csscene, "primitive");
curve_system_manager->curve_shape = get_enum(csscene, "shape");
curve_system_manager->primitive =
(CurvePrimitiveType)get_enum(csscene,
"primitive",
CURVE_NUM_PRIMITIVE_TYPES,
CURVE_LINE_SEGMENTS);
curve_system_manager->curve_shape =
(CurveShapeType)get_enum(csscene,
"shape",
CURVE_NUM_SHAPE_TYPES,
CURVE_THICK);
curve_system_manager->resolution = get_int(csscene, "resolution");
curve_system_manager->subdivisions = get_int(csscene, "subdivisions");
curve_system_manager->use_backfacing = !get_boolean(csscene, "cull_backfacing");

@ -827,7 +827,10 @@ Mesh *BlenderSync::sync_mesh(BL::Object& b_ob,
/* displacement method */
if(cmesh.data) {
const int method = get_enum(cmesh, "displacement_method");
const int method = get_enum(cmesh,
"displacement_method",
Mesh::DISPLACE_NUM_METHODS,
Mesh::DISPLACE_BUMP);
if(method == 0 || !experimental)
mesh->displacement_method = Mesh::DISPLACE_BUMP;

@ -51,6 +51,50 @@ void BlenderSync::find_shader(BL::ID& id,
}
}
/* RNA translation utilities */
static VolumeSampling get_volume_sampling(PointerRNA& ptr)
{
return (VolumeSampling)get_enum(ptr,
"volume_sampling",
VOLUME_NUM_SAMPLING,
VOLUME_SAMPLING_DISTANCE);
}
static VolumeInterpolation get_volume_interpolation(PointerRNA& ptr)
{
return (VolumeInterpolation)get_enum(ptr,
"volume_interpolation",
VOLUME_NUM_INTERPOLATION,
VOLUME_INTERPOLATION_LINEAR);
}
static int validate_enum_value(int value, int num_values, int default_value)
{
if(value >= num_values) {
return default_value;
}
return value;
}
template<typename NodeType>
static InterpolationType get_image_interpolation(NodeType b_node)
{
int value = b_node.interpolation();
return (InterpolationType)validate_enum_value(value,
INTERPOLATION_NUM_TYPES,
INTERPOLATION_LINEAR);
}
template<typename NodeType>
static ExtensionType get_image_extension(NodeType b_node)
{
int value = b_node.extension();
return (ExtensionType)validate_enum_value(value,
EXTENSION_NUM_TYPES,
EXTENSION_REPEAT);
}
/* Graph */
static BL::NodeSocket get_node_output(BL::Node& b_node, const string& name)
@ -653,14 +697,14 @@ static ShaderNode *add_node(Scene *scene,
scene->image_manager->tag_reload_image(
image->filename,
image->builtin_data,
(InterpolationType)b_image_node.interpolation(),
(ExtensionType)b_image_node.extension());
get_image_interpolation(b_image_node),
get_image_extension(b_image_node));
}
}
image->color_space = ImageTextureNode::color_space_enum[(int)b_image_node.color_space()];
image->projection = ImageTextureNode::projection_enum[(int)b_image_node.projection()];
image->interpolation = (InterpolationType)b_image_node.interpolation();
image->extension = (ExtensionType)b_image_node.extension();
image->interpolation = get_image_interpolation(b_image_node);
image->extension = get_image_extension(b_image_node);
image->projection_blend = b_image_node.projection_blend();
BL::TexMapping b_texture_mapping(b_image_node.texture_mapping());
get_tex_mapping(&image->tex_mapping, b_texture_mapping);
@ -696,14 +740,15 @@ static ShaderNode *add_node(Scene *scene,
/* TODO(sergey): Does not work properly when we change builtin type. */
if(b_image.is_updated()) {
scene->image_manager->tag_reload_image(env->filename,
env->builtin_data,
(InterpolationType)b_env_node.interpolation(),
EXTENSION_REPEAT);
scene->image_manager->tag_reload_image(
env->filename,
env->builtin_data,
get_image_interpolation(b_env_node),
EXTENSION_REPEAT);
}
}
env->color_space = EnvironmentTextureNode::color_space_enum[(int)b_env_node.color_space()];
env->interpolation = (InterpolationType)b_env_node.interpolation();
env->interpolation = get_image_interpolation(b_env_node);
env->projection = EnvironmentTextureNode::projection_enum[(int)b_env_node.projection()];
BL::TexMapping b_texture_mapping(b_env_node.texture_mapping());
get_tex_mapping(&env->tex_mapping, b_texture_mapping);
@ -824,8 +869,7 @@ static ShaderNode *add_node(Scene *scene,
point_density->filename = b_point_density_node.name();
point_density->space =
PointDensityTextureNode::space_enum[(int)b_point_density_node.space()];
point_density->interpolation =
(InterpolationType)b_point_density_node.interpolation();
point_density->interpolation = get_image_interpolation(b_point_density_node);
point_density->builtin_data = b_point_density_node.ptr.data;
/* 1 - render settings, 0 - vewport settings. */
@ -1200,8 +1244,8 @@ void BlenderSync::sync_materials(bool update_all)
shader->use_mis = get_boolean(cmat, "sample_as_light");
shader->use_transparent_shadow = get_boolean(cmat, "use_transparent_shadow");
shader->heterogeneous_volume = !get_boolean(cmat, "homogeneous_volume");
shader->volume_sampling_method = (VolumeSampling)get_enum(cmat, "volume_sampling");
shader->volume_interpolation_method = (VolumeInterpolation)get_enum(cmat, "volume_interpolation");
shader->volume_sampling_method = get_volume_sampling(cmat);
shader->volume_interpolation_method = get_volume_interpolation(cmat);
shader->set_graph(graph);
shader->tag_update(scene);
@ -1231,8 +1275,8 @@ void BlenderSync::sync_world(bool update_all)
/* volume */
PointerRNA cworld = RNA_pointer_get(&b_world.ptr, "cycles");
shader->heterogeneous_volume = !get_boolean(cworld, "homogeneous_volume");
shader->volume_sampling_method = (VolumeSampling)get_enum(cworld, "volume_sampling");
shader->volume_interpolation_method = (VolumeInterpolation)get_enum(cworld, "volume_interpolation");
shader->volume_sampling_method = get_volume_sampling(cworld);
shader->volume_interpolation_method = get_volume_interpolation(cworld);
}
else if(b_world) {
ShaderNode *closure, *out;

@ -220,7 +220,11 @@ void BlenderSync::sync_integrator()
if(get_boolean(cscene, "use_animated_seed"))
integrator->seed = hash_int_2d(b_scene.frame_current(), get_int(cscene, "seed"));
integrator->sampling_pattern = (SamplingPattern)get_enum(cscene, "sampling_pattern");
integrator->sampling_pattern = (SamplingPattern)get_enum(
cscene,
"sampling_pattern",
SAMPLING_NUM_PATTERNS,
SAMPLING_PATTERN_SOBOL);
integrator->layer_flag = render_layer.layer;
@ -237,7 +241,10 @@ void BlenderSync::sync_integrator()
}
#endif
integrator->method = (Integrator::Method)get_enum(cscene, "progressive");
integrator->method = (Integrator::Method)get_enum(cscene,
"progressive",
Integrator::NUM_METHODS,
Integrator::PATH);
integrator->sample_all_lights_direct = get_boolean(cscene, "sample_all_lights_direct");
integrator->sample_all_lights_indirect = get_boolean(cscene, "sample_all_lights_indirect");
@ -287,7 +294,10 @@ void BlenderSync::sync_film()
film->use_sample_clamp = (integrator->sample_clamp_direct != 0.0f || integrator->sample_clamp_indirect != 0.0f);
film->exposure = get_float(cscene, "film_exposure");
film->filter_type = (FilterType)get_enum(cscene, "pixel_filter_type");
film->filter_type = (FilterType)get_enum(cscene,
"pixel_filter_type",
FILTER_NUM_TYPES,
FILTER_BLACKMAN_HARRIS);
film->filter_width = (film->filter_type == FILTER_BOX)? 1.0f: get_float(cscene, "filter_width");
if(b_scene.world()) {
@ -440,7 +450,11 @@ SceneParams BlenderSync::get_scene_params(BL::Scene& b_scene,
if(background)
params.bvh_type = SceneParams::BVH_STATIC;
else
params.bvh_type = (SceneParams::BVHType)get_enum(cscene, "debug_bvh_type");
params.bvh_type = (SceneParams::BVHType)get_enum(
cscene,
"debug_bvh_type",
SceneParams::BVH_NUM_TYPES,
SceneParams::BVH_STATIC);
params.use_bvh_spatial_split = RNA_boolean_get(&cscene, "debug_use_spatial_splits");

@ -357,9 +357,24 @@ static inline void set_int(PointerRNA& ptr, const char *name, int value)
RNA_int_set(&ptr, name, value);
}
static inline int get_enum(PointerRNA& ptr, const char *name)
/* Get a RNA enum value with sanity check: if the RNA value is above num_values
* the function will return a fallback default value.
*
* NOTE: This function assumes that RNA enum values are a continuous sequence
* from 0 to num_values-1. Be careful to use it with enums where some values are
* deprecated!
*/
static inline int get_enum(PointerRNA& ptr,
const char *name,
int num_values = -1,
int default_value = -1)
{
return RNA_enum_get(&ptr, name);
int value = RNA_enum_get(&ptr, name);
if(num_values != -1 && value >= num_values) {
assert(default_value != -1);
value = default_value;
}
return value;
}
static inline string get_enum_identifier(PointerRNA& ptr, const char *name)

@ -579,6 +579,8 @@ public:
cuda_assert(cuTexRefSetAddressMode(texref, 0, CU_TR_ADDRESS_MODE_BORDER));
cuda_assert(cuTexRefSetAddressMode(texref, 1, CU_TR_ADDRESS_MODE_BORDER));
break;
default:
assert(0);
}
cuda_assert(cuTexRefSetFormat(texref, format, mem.data_elements));

@ -152,6 +152,8 @@ template<typename T> struct texture_image {
ix = wrap_clamp(ix, width);
iy = wrap_clamp(iy, height);
break;
default:
kernel_assert(0);
}
return read(data[ix + iy*width]);
}
@ -179,6 +181,8 @@ template<typename T> struct texture_image {
ix = wrap_clamp(ix, width);
iy = wrap_clamp(iy, height);
break;
default:
kernel_assert(0);
}
float4 r = (1.0f - ty)*(1.0f - tx)*read(data[ix + iy*width]);
@ -225,6 +229,8 @@ template<typename T> struct texture_image {
ix = wrap_clamp(ix, width);
iy = wrap_clamp(iy, height);
break;
default:
kernel_assert(0);
}
const int xc[4] = {pix, ix, nix, nnix};
@ -290,6 +296,8 @@ template<typename T> struct texture_image {
iy = wrap_clamp(iy, height);
iz = wrap_clamp(iz, depth);
break;
default:
kernel_assert(0);
}
return read(data[ix + iy*width + iz*width*height]);
@ -325,6 +333,8 @@ template<typename T> struct texture_image {
iy = wrap_clamp(iy, height);
iz = wrap_clamp(iz, depth);
break;
default:
kernel_assert(0);
}
float4 r;
@ -390,6 +400,8 @@ template<typename T> struct texture_image {
iy = wrap_clamp(iy, height);
iz = wrap_clamp(iz, depth);
break;
default:
kernel_assert(0);
}
const int xc[4] = {pix, ix, nix, nnix};

@ -264,7 +264,9 @@ enum PathTraceDimension {
enum SamplingPattern {
SAMPLING_PATTERN_SOBOL = 0,
SAMPLING_PATTERN_CMJ = 1
SAMPLING_PATTERN_CMJ = 1,
SAMPLING_NUM_PATTERNS,
};
/* these flags values correspond to raytypes in osl.cpp, so keep them in sync!
@ -482,10 +484,12 @@ enum CameraType {
/* Panorama Type */
enum PanoramaType {
PANORAMA_EQUIRECTANGULAR,
PANORAMA_MIRRORBALL,
PANORAMA_FISHEYE_EQUIDISTANT,
PANORAMA_FISHEYE_EQUISOLID
PANORAMA_EQUIRECTANGULAR = 0,
PANORAMA_FISHEYE_EQUIDISTANT = 1,
PANORAMA_FISHEYE_EQUISOLID = 2,
PANORAMA_MIRRORBALL = 3,
PANORAMA_NUM_TYPES,
};
/* Differential */

@ -40,11 +40,13 @@ public:
/* Specifies an offset for the shutter's time interval. */
enum MotionPosition {
/* Shutter opens at the current frame. */
MOTION_POSITION_START,
MOTION_POSITION_START = 0,
/* Shutter is fully open at the current frame. */
MOTION_POSITION_CENTER,
MOTION_POSITION_CENTER = 1,
/* Shutter closes at the current frame. */
MOTION_POSITION_END,
MOTION_POSITION_END = 2,
MOTION_NUM_POSITIONS,
};
/* Specifies rolling shutter effect. */
@ -52,7 +54,9 @@ public:
/* No rolling shutter effect. */
ROLLING_SHUTTER_NONE = 0,
/* Sensor is being scanned vertically from top to bottom. */
ROLLING_SHUTTER_TOP,
ROLLING_SHUTTER_TOP = 1,
ROLLING_SHUTTER_NUM_TYPES,
};
/* motion blur */

@ -29,28 +29,32 @@ class Scene;
void curvebounds(float *lower, float *upper, float3 *p, int dim);
typedef enum curve_primitives {
CURVE_TRIANGLES,
CURVE_LINE_SEGMENTS,
CURVE_SEGMENTS,
CURVE_RIBBONS
} curve_primitives;
typedef enum CurvePrimitiveType {
CURVE_TRIANGLES = 0,
CURVE_LINE_SEGMENTS = 1,
CURVE_SEGMENTS = 2,
CURVE_RIBBONS = 3,
typedef enum curve_shape {
CURVE_RIBBON,
CURVE_THICK
} curve_shape;
CURVE_NUM_PRIMITIVE_TYPES,
} CurvePrimitiveType;
typedef enum curve_triangles {
typedef enum CurveShapeType {
CURVE_RIBBON = 0,
CURVE_THICK = 1,
CURVE_NUM_SHAPE_TYPES,
} CurveShapeType;
typedef enum CurveTriangleMethod {
CURVE_CAMERA_TRIANGLES,
CURVE_TESSELATED_TRIANGLES
} curve_triangles;
} CurveTriangleMethod;
typedef enum curve_lines {
typedef enum CurveLineMethod {
CURVE_ACCURATE,
CURVE_CORRECTED,
CURVE_UNCORRECTED
} curve_lines;
} CurveLineMethod;
class ParticleCurveData {
@ -83,10 +87,10 @@ public:
class CurveSystemManager {
public:
int primitive;
int curve_shape;
int line_method;
int triangle_method;
CurvePrimitiveType primitive;
CurveShapeType curve_shape;
CurveLineMethod line_method;
CurveTriangleMethod triangle_method;
int resolution;
int subdivisions;

@ -31,7 +31,9 @@ class Scene;
typedef enum FilterType {
FILTER_BOX,
FILTER_GAUSSIAN,
FILTER_BLACKMAN_HARRIS
FILTER_BLACKMAN_HARRIS,
FILTER_NUM_TYPES,
} FilterType;
class Pass {

@ -66,7 +66,9 @@ public:
enum Method {
BRANCHED_PATH = 0,
PATH = 1
PATH = 1,
NUM_METHODS,
};
Method method;

@ -63,9 +63,11 @@ public:
/* Displacement */
enum DisplacementMethod {
DISPLACE_BUMP,
DISPLACE_TRUE,
DISPLACE_BOTH
DISPLACE_BUMP = 0,
DISPLACE_TRUE = 1,
DISPLACE_BOTH = 2,
DISPLACE_NUM_METHODS,
};
ustring name;

@ -125,7 +125,12 @@ public:
class SceneParams {
public:
ShadingSystem shadingsystem;
enum BVHType { BVH_DYNAMIC, BVH_STATIC } bvh_type;
enum BVHType {
BVH_DYNAMIC = 0,
BVH_STATIC = 1,
BVH_NUM_TYPES,
} bvh_type;
bool use_bvh_spatial_split;
bool use_qbvh;
bool persistent_data;

@ -53,11 +53,15 @@ enum VolumeSampling {
VOLUME_SAMPLING_DISTANCE = 0,
VOLUME_SAMPLING_EQUIANGULAR = 1,
VOLUME_SAMPLING_MULTIPLE_IMPORTANCE = 2,
VOLUME_NUM_SAMPLING,
};
enum VolumeInterpolation {
VOLUME_INTERPOLATION_LINEAR = 0,
VOLUME_INTERPOLATION_CUBIC = 1,
VOLUME_NUM_INTERPOLATION,
};
/* Shader describing the appearance of a Mesh, Light or Background.

@ -468,6 +468,8 @@ enum InterpolationType {
INTERPOLATION_CLOSEST = 1,
INTERPOLATION_CUBIC = 2,
INTERPOLATION_SMART = 3,
INTERPOLATION_NUM_TYPES,
};
/* Extension types for textures.
@ -481,6 +483,8 @@ enum ExtensionType {
EXTENSION_EXTEND = 1,
/* Clip to image size and set exterior pixels as transparent. */
EXTENSION_CLIP = 2,
EXTENSION_NUM_TYPES,
};
/* macros */