diff options
author | Brecht Van Lommel <brechtvanlommel@gmail.com> | 2018-03-08 07:33:55 +0300 |
---|---|---|
committer | Brecht Van Lommel <brechtvanlommel@gmail.com> | 2018-03-10 06:54:04 +0300 |
commit | 516e82a90012da3911518103829158215d94215f (patch) | |
tree | d91fac1a4fb558f15870900a5415e61235cc983e /intern/cycles/util | |
parent | cd15d87bfcb4aafb0d4f13dcc902a135f472c9df (diff) |
Code refactor: add ProjectionTransform separate from regular Transform.
This is in preparation of making Transform affine only.
Diffstat (limited to 'intern/cycles/util')
-rw-r--r-- | intern/cycles/util/CMakeLists.txt | 1 | ||||
-rw-r--r-- | intern/cycles/util/util_projection.h | 176 | ||||
-rw-r--r-- | intern/cycles/util/util_transform.h | 35 |
3 files changed, 177 insertions, 35 deletions
diff --git a/intern/cycles/util/CMakeLists.txt b/intern/cycles/util/CMakeLists.txt index 66c4f22a7e2..24043e2231b 100644 --- a/intern/cycles/util/CMakeLists.txt +++ b/intern/cycles/util/CMakeLists.txt @@ -67,6 +67,7 @@ set(SRC_HEADERS util_param.h util_path.h util_progress.h + util_projection.h util_queue.h util_rect.h util_set.h diff --git a/intern/cycles/util/util_projection.h b/intern/cycles/util/util_projection.h new file mode 100644 index 00000000000..0b8ff032120 --- /dev/null +++ b/intern/cycles/util/util_projection.h @@ -0,0 +1,176 @@ +/* + * Copyright 2011-2018 Blender Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __UTIL_PROJECTION_H__ +#define __UTIL_PROJECTION_H__ + +#include "util/util_transform.h" + +CCL_NAMESPACE_BEGIN + +/* Data Types */ + +typedef struct ProjectionTransform { + float4 x, y, z, w; /* rows */ + +#ifndef __KERNEL_GPU__ + ProjectionTransform() + { + } + + explicit ProjectionTransform(const Transform& tfm) + : x(tfm.x), + y(tfm.y), + z(tfm.z), + w(tfm.w) + { + } +#endif +} ProjectionTransform; + +typedef struct PerspectiveMotionTransform { + ProjectionTransform pre; + ProjectionTransform post; +} PerspectiveMotionTransform; + +/* Functions */ + +ccl_device_inline float3 transform_perspective(const ProjectionTransform *t, const float3 a) +{ + float4 b = make_float4(a.x, a.y, a.z, 1.0f); + float3 c = make_float3(dot(t->x, b), dot(t->y, b), dot(t->z, b)); + float w = dot(t->w, b); + + return (w != 0.0f)? c/w: make_float3(0.0f, 0.0f, 0.0f); +} + +ccl_device_inline float3 transform_perspective_direction(const ProjectionTransform *t, const float3 a) +{ + float3 c = make_float3( + a.x*t->x.x + a.y*t->x.y + a.z*t->x.z, + a.x*t->y.x + a.y*t->y.y + a.z*t->y.z, + a.x*t->z.x + a.y*t->z.y + a.z*t->z.z); + + return c; +} + +#ifndef __KERNEL_GPU__ + +ccl_device_inline ProjectionTransform projection_transpose(const ProjectionTransform& a) +{ + ProjectionTransform t; + + t.x.x = a.x.x; t.x.y = a.y.x; t.x.z = a.z.x; t.x.w = a.w.x; + t.y.x = a.x.y; t.y.y = a.y.y; t.y.z = a.z.y; t.y.w = a.w.y; + t.z.x = a.x.z; t.z.y = a.y.z; t.z.z = a.z.z; t.z.w = a.w.z; + t.w.x = a.x.w; t.w.y = a.y.w; t.w.z = a.z.w; t.w.w = a.w.w; + + return t; +} + +ccl_device_inline ProjectionTransform projection_inverse(const ProjectionTransform& a) +{ + Transform t = {a.x, a.y, a.z, a.w}; + t = transform_inverse(t); + return ProjectionTransform(t); +} + +ccl_device_inline ProjectionTransform make_projection( + float a, float b, float c, float d, + float e, float f, float g, float h, + float i, float j, float k, float l, + float m, float n, float o, float p) +{ + ProjectionTransform t; + + t.x.x = a; t.x.y = b; t.x.z = c; t.x.w = d; + t.y.x = e; t.y.y = f; t.y.z = g; t.y.w = h; + t.z.x = i; t.z.y = j; t.z.z = k; t.z.w = l; + t.w.x = m; t.w.y = n; t.w.z = o; t.w.w = p; + + return t; +} +ccl_device_inline ProjectionTransform projection_identity() +{ + return make_projection( + 1.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 1.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 1.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 1.0f); +} + +ccl_device_inline ProjectionTransform operator*(const ProjectionTransform& a, const ProjectionTransform& b) +{ + ProjectionTransform c = projection_transpose(b); + ProjectionTransform t; + + t.x = make_float4(dot(a.x, c.x), dot(a.x, c.y), dot(a.x, c.z), dot(a.x, c.w)); + t.y = make_float4(dot(a.y, c.x), dot(a.y, c.y), dot(a.y, c.z), dot(a.y, c.w)); + t.z = make_float4(dot(a.z, c.x), dot(a.z, c.y), dot(a.z, c.z), dot(a.z, c.w)); + t.w = make_float4(dot(a.w, c.x), dot(a.w, c.y), dot(a.w, c.z), dot(a.w, c.w)); + + return t; +} + +ccl_device_inline ProjectionTransform operator*(const ProjectionTransform& a, const Transform& b) +{ + return a * ProjectionTransform(b); +} + +ccl_device_inline ProjectionTransform operator*(const Transform& a, const ProjectionTransform& b) +{ + return ProjectionTransform(a) * b; +} + +ccl_device_inline void print_projection(const char *label, const ProjectionTransform& t) +{ + print_float4(label, t.x); + print_float4(label, t.y); + print_float4(label, t.z); + print_float4(label, t.w); + printf("\n"); +} + +ccl_device_inline ProjectionTransform projection_perspective(float fov, float n, float f) +{ + ProjectionTransform persp = make_projection( + 1, 0, 0, 0, + 0, 1, 0, 0, + 0, 0, f / (f - n), -f*n / (f - n), + 0, 0, 1, 0); + + float inv_angle = 1.0f/tanf(0.5f*fov); + + Transform scale = transform_scale(inv_angle, inv_angle, 1); + + return scale * persp; +} + +ccl_device_inline ProjectionTransform projection_orthographic(float znear, float zfar) +{ + Transform t = + transform_scale(1.0f, 1.0f, 1.0f / (zfar-znear)) * + transform_translate(0.0f, 0.0f, -znear); + + return ProjectionTransform(t); +} + +#endif /* __KERNEL_GPU__ */ + +CCL_NAMESPACE_END + +#endif /* __UTIL_PROJECTION_H__ */ + diff --git a/intern/cycles/util/util_transform.h b/intern/cycles/util/util_transform.h index 1efe001f6a8..52022292d0b 100644 --- a/intern/cycles/util/util_transform.h +++ b/intern/cycles/util/util_transform.h @@ -47,22 +47,8 @@ typedef struct ccl_may_alias MotionTransform { Transform post; } MotionTransform; -typedef struct PerspectiveMotionTransform { - Transform pre; - Transform post; -} PerspectiveMotionTransform; - /* Functions */ -ccl_device_inline float3 transform_perspective(const Transform *t, const float3 a) -{ - float4 b = make_float4(a.x, a.y, a.z, 1.0f); - float3 c = make_float3(dot(t->x, b), dot(t->y, b), dot(t->z, b)); - float w = dot(t->w, b); - - return (w != 0.0f)? c/w: make_float3(0.0f, 0.0f, 0.0f); -} - ccl_device_inline float3 transform_point(const Transform *t, const float3 a) { /* TODO(sergey): Disabled for now, causes crashes in certain cases. */ @@ -221,21 +207,6 @@ ccl_device_inline Transform transform_scale(float x, float y, float z) return transform_scale(make_float3(x, y, z)); } -ccl_device_inline Transform transform_perspective(float fov, float n, float f) -{ - Transform persp = make_transform( - 1, 0, 0, 0, - 0, 1, 0, 0, - 0, 0, f / (f - n), -f*n / (f - n), - 0, 0, 1, 0); - - float inv_angle = 1.0f/tanf(0.5f*fov); - - Transform scale = transform_scale(inv_angle, inv_angle, 1); - - return scale * persp; -} - ccl_device_inline Transform transform_rotate(float angle, float3 axis) { float s = sinf(angle); @@ -272,12 +243,6 @@ ccl_device_inline Transform transform_euler(float3 euler) transform_rotate(euler.x, make_float3(1.0f, 0.0f, 0.0f)); } -ccl_device_inline Transform transform_orthographic(float znear, float zfar) -{ - return transform_scale(1.0f, 1.0f, 1.0f / (zfar-znear)) * - transform_translate(0.0f, 0.0f, -znear); -} - ccl_device_inline Transform transform_identity() { return transform_scale(1.0f, 1.0f, 1.0f); |