diff options
author | Brecht Van Lommel <brecht@blender.org> | 2020-10-26 20:13:53 +0300 |
---|---|---|
committer | Brecht Van Lommel <brecht@blender.org> | 2020-10-28 14:43:42 +0300 |
commit | ee6b989f8e2ae99c28441ab8663a99bfd16b6c65 (patch) | |
tree | eecd1fbfed088c5552d96f200cfdee1f807a6ba0 /intern/cycles/kernel/geom | |
parent | fb88d4eda8a807e79ecb100e82ac930250d871db (diff) |
Cycles: refactor to split surface and volume attribute lookup more
This avoids OpenCL inlining heavy volume interpolation code once for every
data type, which could cause a performance regression when we add a float4
data type in the next commit.
Ref D2057
Diffstat (limited to 'intern/cycles/kernel/geom')
-rw-r--r-- | intern/cycles/kernel/geom/geom_primitive.h | 172 | ||||
-rw-r--r-- | intern/cycles/kernel/geom/geom_volume.h | 45 |
2 files changed, 71 insertions, 146 deletions
diff --git a/intern/cycles/kernel/geom/geom_primitive.h b/intern/cycles/kernel/geom/geom_primitive.h index 997abf438d0..2c31e5cee03 100644 --- a/intern/cycles/kernel/geom/geom_primitive.h +++ b/intern/cycles/kernel/geom/geom_primitive.h @@ -21,38 +21,11 @@ CCL_NAMESPACE_BEGIN -/* Generic primitive attribute reading functions */ -ccl_device_inline float primitive_attribute_float( - KernelGlobals *kg, const ShaderData *sd, const AttributeDescriptor desc, float *dx, float *dy) -{ - if (sd->type & PRIMITIVE_ALL_TRIANGLE) { - if (subd_triangle_patch(kg, sd) == ~0) - return triangle_attribute_float(kg, sd, desc, dx, dy); - else - return subd_triangle_attribute_float(kg, sd, desc, dx, dy); - } -#ifdef __HAIR__ - else if (sd->type & PRIMITIVE_ALL_CURVE) { - return curve_attribute_float(kg, sd, desc, dx, dy); - } -#endif -#ifdef __VOLUME__ - else if (sd->object != OBJECT_NONE && desc.element == ATTR_ELEMENT_VOXEL) { - if (dx) - *dx = 0.0f; - if (dy) - *dy = 0.0f; - return volume_attribute_float(kg, sd, desc); - } -#endif - else { - if (dx) - *dx = 0.0f; - if (dy) - *dy = 0.0f; - return 0.0f; - } -} +/* Surface Attributes + * + * Read geometry attributes for surface shading. This is distinct from volume + * attributes for performance, mainly for GPU performance to avoid bringing in + * heavy volume interpolation code. */ ccl_device_inline float primitive_surface_attribute_float( KernelGlobals *kg, const ShaderData *sd, const AttributeDescriptor desc, float *dx, float *dy) @@ -77,25 +50,11 @@ ccl_device_inline float primitive_surface_attribute_float( } } -#ifdef __VOLUME__ -ccl_device_inline float primitive_volume_attribute_float(KernelGlobals *kg, - const ShaderData *sd, - const AttributeDescriptor desc) -{ - if (sd->object != OBJECT_NONE && desc.element == ATTR_ELEMENT_VOXEL) { - return volume_attribute_float(kg, sd, desc); - } - else { - return 0.0f; - } -} -#endif - -ccl_device_inline float2 primitive_attribute_float2(KernelGlobals *kg, - const ShaderData *sd, - const AttributeDescriptor desc, - float2 *dx, - float2 *dy) +ccl_device_inline float2 primitive_surface_attribute_float2(KernelGlobals *kg, + const ShaderData *sd, + const AttributeDescriptor desc, + float2 *dx, + float2 *dy) { if (sd->type & PRIMITIVE_ALL_TRIANGLE) { if (subd_triangle_patch(kg, sd) == ~0) @@ -108,16 +67,6 @@ ccl_device_inline float2 primitive_attribute_float2(KernelGlobals *kg, return curve_attribute_float2(kg, sd, desc, dx, dy); } #endif -#ifdef __VOLUME__ - else if (sd->object != OBJECT_NONE && desc.element == ATTR_ELEMENT_VOXEL) { - kernel_assert(0); - if (dx) - *dx = make_float2(0.0f, 0.0f); - if (dy) - *dy = make_float2(0.0f, 0.0f); - return make_float2(0.0f, 0.0f); - } -#endif else { if (dx) *dx = make_float2(0.0f, 0.0f); @@ -127,11 +76,11 @@ ccl_device_inline float2 primitive_attribute_float2(KernelGlobals *kg, } } -ccl_device_inline float3 primitive_attribute_float3(KernelGlobals *kg, - const ShaderData *sd, - const AttributeDescriptor desc, - float3 *dx, - float3 *dy) +ccl_device_inline float3 primitive_surface_attribute_float3(KernelGlobals *kg, + const ShaderData *sd, + const AttributeDescriptor desc, + float3 *dx, + float3 *dy) { if (sd->type & PRIMITIVE_ALL_TRIANGLE) { if (subd_triangle_patch(kg, sd) == ~0) @@ -144,15 +93,6 @@ ccl_device_inline float3 primitive_attribute_float3(KernelGlobals *kg, return curve_attribute_float3(kg, sd, desc, dx, dy); } #endif -#ifdef __VOLUME__ - else if (sd->object != OBJECT_NONE && desc.element == ATTR_ELEMENT_VOXEL) { - if (dx) - *dx = make_float3(0.0f, 0.0f, 0.0f); - if (dy) - *dy = make_float3(0.0f, 0.0f, 0.0f); - return volume_attribute_float3(kg, sd, desc); - } -#endif else { if (dx) *dx = make_float3(0.0f, 0.0f, 0.0f); @@ -162,11 +102,11 @@ ccl_device_inline float3 primitive_attribute_float3(KernelGlobals *kg, } } -ccl_device_inline float4 primitive_attribute_float4(KernelGlobals *kg, - const ShaderData *sd, - const AttributeDescriptor desc, - float4 *dx, - float4 *dy) +ccl_device_inline float4 primitive_surface_attribute_float4(KernelGlobals *kg, + const ShaderData *sd, + const AttributeDescriptor desc, + float4 *dx, + float4 *dy) { if (sd->type & PRIMITIVE_ALL_TRIANGLE) { if (subd_triangle_patch(kg, sd) == ~0) @@ -188,68 +128,52 @@ ccl_device_inline float4 primitive_attribute_float4(KernelGlobals *kg, } } -ccl_device_inline float2 primitive_surface_attribute_float2(KernelGlobals *kg, - const ShaderData *sd, - const AttributeDescriptor desc, - float2 *dx, - float2 *dy) +#ifdef __VOLUME__ +/* Volume Attributes + * + * Read geometry attributes for volume shading. This is distinct from surface + * attributes for performance, mainly for GPU performance to avoid bringing in + * heavy volume interpolation code. */ + +ccl_device_inline bool primitive_is_volume_attribute(const ShaderData *sd, + const AttributeDescriptor desc) { - if (sd->type & PRIMITIVE_ALL_TRIANGLE) { - if (subd_triangle_patch(kg, sd) == ~0) - return triangle_attribute_float2(kg, sd, desc, dx, dy); - else - return subd_triangle_attribute_float2(kg, sd, desc, dx, dy); - } -#ifdef __HAIR__ - else if (sd->type & PRIMITIVE_ALL_CURVE) { - return curve_attribute_float2(kg, sd, desc, dx, dy); + return (sd->object != OBJECT_NONE && desc.element == ATTR_ELEMENT_VOXEL); +} + +ccl_device_inline float primitive_volume_attribute_float(KernelGlobals *kg, + const ShaderData *sd, + const AttributeDescriptor desc) +{ + if (primitive_is_volume_attribute(sd, desc)) { + return volume_attribute_value_to_float(volume_attribute_float4(kg, sd, desc)); } -#endif else { - if (dx) - *dx = make_float2(0.0f, 0.0f); - if (dy) - *dy = make_float2(0.0f, 0.0f); - return make_float2(0.0f, 0.0f); + return 0.0f; } } -ccl_device_inline float3 primitive_surface_attribute_float3(KernelGlobals *kg, - const ShaderData *sd, - const AttributeDescriptor desc, - float3 *dx, - float3 *dy) +ccl_device_inline float3 primitive_volume_attribute_float3(KernelGlobals *kg, + const ShaderData *sd, + const AttributeDescriptor desc) { - if (sd->type & PRIMITIVE_ALL_TRIANGLE) { - if (subd_triangle_patch(kg, sd) == ~0) - return triangle_attribute_float3(kg, sd, desc, dx, dy); - else - return subd_triangle_attribute_float3(kg, sd, desc, dx, dy); - } -#ifdef __HAIR__ - else if (sd->type & PRIMITIVE_ALL_CURVE) { - return curve_attribute_float3(kg, sd, desc, dx, dy); + if (primitive_is_volume_attribute(sd, desc)) { + return volume_attribute_value_to_float3(volume_attribute_float4(kg, sd, desc)); } -#endif else { - if (dx) - *dx = make_float3(0.0f, 0.0f, 0.0f); - if (dy) - *dy = make_float3(0.0f, 0.0f, 0.0f); return make_float3(0.0f, 0.0f, 0.0f); } } -#ifdef __VOLUME__ -ccl_device_inline float3 primitive_volume_attribute_float3(KernelGlobals *kg, +ccl_device_inline float4 primitive_volume_attribute_float4(KernelGlobals *kg, const ShaderData *sd, const AttributeDescriptor desc) { - if (sd->object != OBJECT_NONE && desc.element == ATTR_ELEMENT_VOXEL) { - return volume_attribute_float3(kg, sd, desc); + if (primitive_is_volume_attribute(sd, desc)) { + return volume_attribute_float4(kg, sd, desc); } else { - return make_float3(0.0f, 0.0f, 0.0f); + return make_float4(0.0f, 0.0f, 0.0f, 0.0f); } } #endif diff --git a/intern/cycles/kernel/geom/geom_volume.h b/intern/cycles/kernel/geom/geom_volume.h index f43a7841b46..13b027a5f6c 100644 --- a/intern/cycles/kernel/geom/geom_volume.h +++ b/intern/cycles/kernel/geom/geom_volume.h @@ -47,38 +47,39 @@ ccl_device_inline float3 volume_normalized_position(KernelGlobals *kg, return P; } -ccl_device float volume_attribute_float(KernelGlobals *kg, - const ShaderData *sd, - const AttributeDescriptor desc) +ccl_device float volume_attribute_value_to_float(const float4 value) { - /* todo: optimize this so we don't have to transform both here and in - * kernel_tex_image_interp_3d when possible. Also could optimize for the - * common case where transform is translation/scale only. */ - float3 P = sd->P; - object_inverse_position_transform(kg, sd, &P); - InterpolationType interp = (sd->flag & SD_VOLUME_CUBIC) ? INTERPOLATION_CUBIC : - INTERPOLATION_NONE; - float4 r = kernel_tex_image_interp_3d(kg, desc.offset, P, interp); - return average(float4_to_float3(r)); + return average(float4_to_float3(value)); } -ccl_device float3 volume_attribute_float3(KernelGlobals *kg, +ccl_device float volume_attribute_value_to_alpha(const float4 value) +{ + return value.w; +} + +ccl_device float3 volume_attribute_value_to_float3(const float4 value) +{ + if (value.w > 1e-6f && value.w != 1.0f) { + /* For RGBA colors, unpremultiply after interpolation. */ + return float4_to_float3(value) / value.w; + } + else { + return float4_to_float3(value); + } +} + +ccl_device float4 volume_attribute_float4(KernelGlobals *kg, const ShaderData *sd, const AttributeDescriptor desc) { + /* todo: optimize this so we don't have to transform both here and in + * kernel_tex_image_interp_3d when possible. Also could optimize for the + * common case where transform is translation/scale only. */ float3 P = sd->P; object_inverse_position_transform(kg, sd, &P); InterpolationType interp = (sd->flag & SD_VOLUME_CUBIC) ? INTERPOLATION_CUBIC : INTERPOLATION_NONE; - float4 r = kernel_tex_image_interp_3d(kg, desc.offset, P, interp); - - if (r.w > 1e-6f && r.w != 1.0f) { - /* For RGBA colors, unpremultiply after interpolation. */ - return float4_to_float3(r) / r.w; - } - else { - return float4_to_float3(r); - } + return kernel_tex_image_interp_3d(kg, desc.offset, P, interp); } #endif |