2018-02-14 11:23:30 +01:00
|
|
|
/*
|
2014-12-15 21:18:01 +05:00
|
|
|
* Copyright 2014, Blender Foundation.
|
2014-12-15 20:21:41 +05:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2015-04-01 21:08:12 +05:00
|
|
|
/* Triangle/Ray intersections.
|
2014-12-15 20:21:41 +05:00
|
|
|
*
|
2014-12-15 21:18:01 +05:00
|
|
|
* For BVH ray intersection we use a precomputed triangle storage to accelerate
|
|
|
|
* intersection at the cost of more memory usage.
|
|
|
|
*/
|
2014-12-15 20:21:41 +05:00
|
|
|
|
Cycles: merge of cycles-x branch, a major update to the renderer
This includes much improved GPU rendering performance, viewport interactivity,
new shadow catcher, revamped sampling settings, subsurface scattering anisotropy,
new GPU volume sampling, improved PMJ sampling pattern, and more.
Some features have also been removed or changed, breaking backwards compatibility.
Including the removal of the OpenCL backend, for which alternatives are under
development.
Release notes and code docs:
https://wiki.blender.org/wiki/Reference/Release_Notes/3.0/Cycles
https://wiki.blender.org/wiki/Source/Render/Cycles
Credits:
* Sergey Sharybin
* Brecht Van Lommel
* Patrick Mours (OptiX backend)
* Christophe Hery (subsurface scattering anisotropy)
* William Leeson (PMJ sampling pattern)
* Alaska (various fixes and tweaks)
* Thomas Dinges (various fixes)
For the full commit history, see the cycles-x branch. This squashes together
all the changes since intermediate changes would often fail building or tests.
Ref T87839, T87837, T87836
Fixes T90734, T89353, T80267, T80267, T77185, T69800
2021-09-20 17:59:20 +02:00
|
|
|
#pragma once
|
|
|
|
|
2021-10-24 14:19:19 +02:00
|
|
|
#include "kernel/sample/lcg.h"
|
Cycles: merge of cycles-x branch, a major update to the renderer
This includes much improved GPU rendering performance, viewport interactivity,
new shadow catcher, revamped sampling settings, subsurface scattering anisotropy,
new GPU volume sampling, improved PMJ sampling pattern, and more.
Some features have also been removed or changed, breaking backwards compatibility.
Including the removal of the OpenCL backend, for which alternatives are under
development.
Release notes and code docs:
https://wiki.blender.org/wiki/Reference/Release_Notes/3.0/Cycles
https://wiki.blender.org/wiki/Source/Render/Cycles
Credits:
* Sergey Sharybin
* Brecht Van Lommel
* Patrick Mours (OptiX backend)
* Christophe Hery (subsurface scattering anisotropy)
* William Leeson (PMJ sampling pattern)
* Alaska (various fixes and tweaks)
* Thomas Dinges (various fixes)
For the full commit history, see the cycles-x branch. This squashes together
all the changes since intermediate changes would often fail building or tests.
Ref T87839, T87837, T87836
Fixes T90734, T89353, T80267, T80267, T77185, T69800
2021-09-20 17:59:20 +02:00
|
|
|
|
2014-12-15 20:21:41 +05:00
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
2021-10-17 16:10:10 +02:00
|
|
|
ccl_device_inline bool triangle_intersect(KernelGlobals kg,
|
Cycles: Kernel address space changes for MSL
This is the first of a sequence of changes to support compiling Cycles kernels as MSL (Metal Shading Language) in preparation for a Metal GPU device implementation.
MSL requires that all pointer types be declared with explicit address space attributes (device, thread, etc...). There is already precedent for this with Cycles' address space macros (ccl_global, ccl_private, etc...), therefore the first step of MSL-enablement is to apply these consistently. Line-for-line this represents the largest change required to enable MSL. Applying this change first will simplify future patches as well as offering the emergent benefit of enhanced descriptiveness.
The vast majority of deltas in this patch fall into one of two cases:
- Ensuring ccl_private is specified for thread-local pointer types
- Ensuring ccl_global is specified for device-wide pointer types
Additionally, the ccl_addr_space qualifier can be removed. Prior to Cycles X, ccl_addr_space was used as a context-dependent address space qualifier, but now it is either redundant (e.g. in struct typedefs), or can be replaced by ccl_global in the case of pointer types. Associated function variants (e.g. lcg_step_float_addrspace) are also redundant.
In cases where address space qualifiers are chained with "const", this patch places the address space qualifier first. The rationale for this is that the choice of address space is likely to have the greater impact on runtime performance and overall architecture.
The final part of this patch is the addition of a metal/compat.h header. This is partially complete and will be extended in future patches, paving the way for the full Metal implementation.
Ref T92212
Reviewed By: brecht
Maniphest Tasks: T92212
Differential Revision: https://developer.blender.org/D12864
2021-10-14 13:53:40 +01:00
|
|
|
ccl_private Intersection *isect,
|
2014-12-15 21:18:01 +05:00
|
|
|
float3 P,
|
2017-03-27 17:06:37 +02:00
|
|
|
float3 dir,
|
Cycles: merge of cycles-x branch, a major update to the renderer
This includes much improved GPU rendering performance, viewport interactivity,
new shadow catcher, revamped sampling settings, subsurface scattering anisotropy,
new GPU volume sampling, improved PMJ sampling pattern, and more.
Some features have also been removed or changed, breaking backwards compatibility.
Including the removal of the OpenCL backend, for which alternatives are under
development.
Release notes and code docs:
https://wiki.blender.org/wiki/Reference/Release_Notes/3.0/Cycles
https://wiki.blender.org/wiki/Source/Render/Cycles
Credits:
* Sergey Sharybin
* Brecht Van Lommel
* Patrick Mours (OptiX backend)
* Christophe Hery (subsurface scattering anisotropy)
* William Leeson (PMJ sampling pattern)
* Alaska (various fixes and tweaks)
* Thomas Dinges (various fixes)
For the full commit history, see the cycles-x branch. This squashes together
all the changes since intermediate changes would often fail building or tests.
Ref T87839, T87837, T87836
Fixes T90734, T89353, T80267, T80267, T77185, T69800
2021-09-20 17:59:20 +02:00
|
|
|
float tmax,
|
2014-12-15 21:18:01 +05:00
|
|
|
uint visibility,
|
|
|
|
int object,
|
2022-01-13 17:15:30 +01:00
|
|
|
int prim,
|
2016-12-12 12:10:37 +01:00
|
|
|
int prim_addr)
|
2014-12-15 20:21:41 +05:00
|
|
|
{
|
2021-02-28 23:23:24 +01:00
|
|
|
const uint tri_vindex = kernel_tex_fetch(__tri_vindex, prim).w;
|
2021-11-16 14:03:59 +01:00
|
|
|
const float3 tri_a = kernel_tex_fetch(__tri_verts, tri_vindex + 0),
|
2021-02-28 23:23:24 +01:00
|
|
|
tri_b = kernel_tex_fetch(__tri_verts, tri_vindex + 1),
|
|
|
|
tri_c = kernel_tex_fetch(__tri_verts, tri_vindex + 2);
|
2017-03-23 13:30:18 +01:00
|
|
|
float t, u, v;
|
2021-11-22 20:41:19 +01:00
|
|
|
if (ray_triangle_intersect(P, dir, tmax, tri_a, tri_b, tri_c, &u, &v, &t)) {
|
2014-12-15 20:21:41 +05:00
|
|
|
#ifdef __VISIBILITY_FLAG__
|
2017-03-23 13:30:18 +01:00
|
|
|
/* Visibility flag test. we do it here under the assumption
|
|
|
|
* that most triangles are culled by node flags.
|
|
|
|
*/
|
|
|
|
if (kernel_tex_fetch(__prim_visibility, prim_addr) & visibility)
|
2014-12-15 20:21:41 +05:00
|
|
|
#endif
|
2017-03-23 13:30:18 +01:00
|
|
|
{
|
2022-01-13 17:15:30 +01:00
|
|
|
isect->object = object;
|
2021-02-28 23:23:24 +01:00
|
|
|
isect->prim = prim;
|
2017-03-23 13:30:18 +01:00
|
|
|
isect->type = PRIMITIVE_TRIANGLE;
|
|
|
|
isect->u = u;
|
|
|
|
isect->v = v;
|
|
|
|
isect->t = t;
|
|
|
|
return true;
|
2015-08-24 21:21:30 +02:00
|
|
|
}
|
2014-12-15 20:21:41 +05:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-14 11:23:30 +01:00
|
|
|
/* Special ray intersection routines for subsurface scattering. In that case we
|
2014-12-15 20:21:41 +05:00
|
|
|
* only want to intersect with primitives in the same object, and if case of
|
2014-12-15 21:18:01 +05:00
|
|
|
* multiple hits we pick a single random primitive as the intersection point.
|
2018-06-15 11:03:29 +02:00
|
|
|
* Returns whether traversal should be stopped.
|
2014-12-15 21:18:01 +05:00
|
|
|
*/
|
2014-12-15 20:21:41 +05:00
|
|
|
|
2017-10-30 20:25:08 +01:00
|
|
|
#ifdef __BVH_LOCAL__
|
2021-10-17 16:10:10 +02:00
|
|
|
ccl_device_inline bool triangle_intersect_local(KernelGlobals kg,
|
Cycles: Kernel address space changes for MSL
This is the first of a sequence of changes to support compiling Cycles kernels as MSL (Metal Shading Language) in preparation for a Metal GPU device implementation.
MSL requires that all pointer types be declared with explicit address space attributes (device, thread, etc...). There is already precedent for this with Cycles' address space macros (ccl_global, ccl_private, etc...), therefore the first step of MSL-enablement is to apply these consistently. Line-for-line this represents the largest change required to enable MSL. Applying this change first will simplify future patches as well as offering the emergent benefit of enhanced descriptiveness.
The vast majority of deltas in this patch fall into one of two cases:
- Ensuring ccl_private is specified for thread-local pointer types
- Ensuring ccl_global is specified for device-wide pointer types
Additionally, the ccl_addr_space qualifier can be removed. Prior to Cycles X, ccl_addr_space was used as a context-dependent address space qualifier, but now it is either redundant (e.g. in struct typedefs), or can be replaced by ccl_global in the case of pointer types. Associated function variants (e.g. lcg_step_float_addrspace) are also redundant.
In cases where address space qualifiers are chained with "const", this patch places the address space qualifier first. The rationale for this is that the choice of address space is likely to have the greater impact on runtime performance and overall architecture.
The final part of this patch is the addition of a metal/compat.h header. This is partially complete and will be extended in future patches, paving the way for the full Metal implementation.
Ref T92212
Reviewed By: brecht
Maniphest Tasks: T92212
Differential Revision: https://developer.blender.org/D12864
2021-10-14 13:53:40 +01:00
|
|
|
ccl_private LocalIntersection *local_isect,
|
2014-12-15 21:18:01 +05:00
|
|
|
float3 P,
|
2017-03-27 17:06:37 +02:00
|
|
|
float3 dir,
|
2014-12-15 21:18:01 +05:00
|
|
|
int object,
|
2022-01-13 17:15:30 +01:00
|
|
|
int prim,
|
2016-12-12 12:10:37 +01:00
|
|
|
int prim_addr,
|
2014-12-15 21:18:01 +05:00
|
|
|
float tmax,
|
Cycles: Kernel address space changes for MSL
This is the first of a sequence of changes to support compiling Cycles kernels as MSL (Metal Shading Language) in preparation for a Metal GPU device implementation.
MSL requires that all pointer types be declared with explicit address space attributes (device, thread, etc...). There is already precedent for this with Cycles' address space macros (ccl_global, ccl_private, etc...), therefore the first step of MSL-enablement is to apply these consistently. Line-for-line this represents the largest change required to enable MSL. Applying this change first will simplify future patches as well as offering the emergent benefit of enhanced descriptiveness.
The vast majority of deltas in this patch fall into one of two cases:
- Ensuring ccl_private is specified for thread-local pointer types
- Ensuring ccl_global is specified for device-wide pointer types
Additionally, the ccl_addr_space qualifier can be removed. Prior to Cycles X, ccl_addr_space was used as a context-dependent address space qualifier, but now it is either redundant (e.g. in struct typedefs), or can be replaced by ccl_global in the case of pointer types. Associated function variants (e.g. lcg_step_float_addrspace) are also redundant.
In cases where address space qualifiers are chained with "const", this patch places the address space qualifier first. The rationale for this is that the choice of address space is likely to have the greater impact on runtime performance and overall architecture.
The final part of this patch is the addition of a metal/compat.h header. This is partially complete and will be extended in future patches, paving the way for the full Metal implementation.
Ref T92212
Reviewed By: brecht
Maniphest Tasks: T92212
Differential Revision: https://developer.blender.org/D12864
2021-10-14 13:53:40 +01:00
|
|
|
ccl_private uint *lcg_state,
|
2014-12-15 21:18:01 +05:00
|
|
|
int max_hits)
|
2014-12-15 20:21:41 +05:00
|
|
|
{
|
2021-02-28 23:23:24 +01:00
|
|
|
const uint tri_vindex = kernel_tex_fetch(__tri_vindex, prim).w;
|
2021-11-16 14:03:59 +01:00
|
|
|
const float3 tri_a = kernel_tex_fetch(__tri_verts, tri_vindex + 0),
|
|
|
|
tri_b = kernel_tex_fetch(__tri_verts, tri_vindex + 1),
|
|
|
|
tri_c = kernel_tex_fetch(__tri_verts, tri_vindex + 2);
|
2017-03-23 15:17:26 +01:00
|
|
|
float t, u, v;
|
2021-11-22 20:41:19 +01:00
|
|
|
if (!ray_triangle_intersect(P, dir, tmax, tri_a, tri_b, tri_c, &u, &v, &t)) {
|
2018-06-15 11:03:29 +02:00
|
|
|
return false;
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-06-15 11:03:29 +02:00
|
|
|
/* If no actual hit information is requested, just return here. */
|
|
|
|
if (max_hits == 0) {
|
|
|
|
return true;
|
2014-12-15 21:18:01 +05:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-01-21 14:04:22 +01:00
|
|
|
int hit;
|
|
|
|
if (lcg_state) {
|
|
|
|
/* Record up to max_hits intersections. */
|
|
|
|
for (int i = min(max_hits, local_isect->num_hits) - 1; i >= 0; --i) {
|
|
|
|
if (local_isect->hits[i].t == t) {
|
2018-06-15 11:03:29 +02:00
|
|
|
return false;
|
2016-07-15 14:55:37 +02:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
2018-01-21 14:04:22 +01:00
|
|
|
local_isect->num_hits++;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-01-21 14:04:22 +01:00
|
|
|
if (local_isect->num_hits <= max_hits) {
|
|
|
|
hit = local_isect->num_hits - 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* reservoir sampling: if we are at the maximum number of
|
|
|
|
* hits, randomly replace element or skip it */
|
|
|
|
hit = lcg_step_uint(lcg_state) % local_isect->num_hits;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-01-21 14:04:22 +01:00
|
|
|
if (hit >= max_hits)
|
2018-06-15 11:03:29 +02:00
|
|
|
return false;
|
2014-12-15 20:21:41 +05:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2014-12-15 21:18:01 +05:00
|
|
|
else {
|
2018-01-21 14:04:22 +01:00
|
|
|
/* Record closest intersection only. */
|
|
|
|
if (local_isect->num_hits && t > local_isect->hits[0].t) {
|
2018-06-15 11:03:29 +02:00
|
|
|
return false;
|
2018-01-21 14:04:22 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-01-21 14:04:22 +01:00
|
|
|
hit = 0;
|
|
|
|
local_isect->num_hits = 1;
|
2014-12-15 21:18:01 +05:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-01-21 14:04:22 +01:00
|
|
|
/* Record intersection. */
|
Cycles: Kernel address space changes for MSL
This is the first of a sequence of changes to support compiling Cycles kernels as MSL (Metal Shading Language) in preparation for a Metal GPU device implementation.
MSL requires that all pointer types be declared with explicit address space attributes (device, thread, etc...). There is already precedent for this with Cycles' address space macros (ccl_global, ccl_private, etc...), therefore the first step of MSL-enablement is to apply these consistently. Line-for-line this represents the largest change required to enable MSL. Applying this change first will simplify future patches as well as offering the emergent benefit of enhanced descriptiveness.
The vast majority of deltas in this patch fall into one of two cases:
- Ensuring ccl_private is specified for thread-local pointer types
- Ensuring ccl_global is specified for device-wide pointer types
Additionally, the ccl_addr_space qualifier can be removed. Prior to Cycles X, ccl_addr_space was used as a context-dependent address space qualifier, but now it is either redundant (e.g. in struct typedefs), or can be replaced by ccl_global in the case of pointer types. Associated function variants (e.g. lcg_step_float_addrspace) are also redundant.
In cases where address space qualifiers are chained with "const", this patch places the address space qualifier first. The rationale for this is that the choice of address space is likely to have the greater impact on runtime performance and overall architecture.
The final part of this patch is the addition of a metal/compat.h header. This is partially complete and will be extended in future patches, paving the way for the full Metal implementation.
Ref T92212
Reviewed By: brecht
Maniphest Tasks: T92212
Differential Revision: https://developer.blender.org/D12864
2021-10-14 13:53:40 +01:00
|
|
|
ccl_private Intersection *isect = &local_isect->hits[hit];
|
2021-02-28 23:23:24 +01:00
|
|
|
isect->prim = prim;
|
2022-01-13 17:15:30 +01:00
|
|
|
isect->object = object;
|
2014-12-15 21:18:01 +05:00
|
|
|
isect->type = PRIMITIVE_TRIANGLE;
|
2017-03-23 15:17:26 +01:00
|
|
|
isect->u = u;
|
|
|
|
isect->v = v;
|
2016-07-15 14:55:37 +02:00
|
|
|
isect->t = t;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-11-22 15:00:29 +05:00
|
|
|
/* Record geometric normal. */
|
2017-10-30 20:25:08 +01:00
|
|
|
local_isect->Ng[hit] = normalize(cross(tri_b - tri_a, tri_c - tri_a));
|
2018-06-15 11:03:29 +02:00
|
|
|
|
|
|
|
return false;
|
2014-12-15 20:21:41 +05:00
|
|
|
}
|
2017-10-30 20:25:08 +01:00
|
|
|
#endif /* __BVH_LOCAL__ */
|
2014-12-15 20:21:41 +05:00
|
|
|
|
2022-01-13 17:12:03 +01:00
|
|
|
/**
|
|
|
|
* Use the barycentric coordinates to get the intersection location
|
2014-12-15 21:18:01 +05:00
|
|
|
*/
|
2022-01-13 17:12:03 +01:00
|
|
|
ccl_device_inline float3 triangle_point_from_uv(KernelGlobals kg,
|
|
|
|
ccl_private ShaderData *sd,
|
|
|
|
const int isect_object,
|
|
|
|
const int isect_prim,
|
|
|
|
const float u,
|
|
|
|
const float v)
|
2014-12-15 20:21:41 +05:00
|
|
|
{
|
2021-02-28 23:23:24 +01:00
|
|
|
const uint tri_vindex = kernel_tex_fetch(__tri_vindex, isect_prim).w;
|
2021-11-16 14:03:59 +01:00
|
|
|
const packed_float3 tri_a = kernel_tex_fetch(__tri_verts, tri_vindex + 0),
|
|
|
|
tri_b = kernel_tex_fetch(__tri_verts, tri_vindex + 1),
|
|
|
|
tri_c = kernel_tex_fetch(__tri_verts, tri_vindex + 2);
|
2022-01-13 17:12:03 +01:00
|
|
|
float w = 1.0f - u - v;
|
2014-12-15 20:21:41 +05:00
|
|
|
|
2022-01-13 17:12:03 +01:00
|
|
|
float3 P = u * tri_a + v * tri_b + w * tri_c;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-02-28 23:23:24 +01:00
|
|
|
if (!(sd->object_flag & SD_OBJECT_TRANSFORM_APPLIED)) {
|
Cycles: merge of cycles-x branch, a major update to the renderer
This includes much improved GPU rendering performance, viewport interactivity,
new shadow catcher, revamped sampling settings, subsurface scattering anisotropy,
new GPU volume sampling, improved PMJ sampling pattern, and more.
Some features have also been removed or changed, breaking backwards compatibility.
Including the removal of the OpenCL backend, for which alternatives are under
development.
Release notes and code docs:
https://wiki.blender.org/wiki/Reference/Release_Notes/3.0/Cycles
https://wiki.blender.org/wiki/Source/Render/Cycles
Credits:
* Sergey Sharybin
* Brecht Van Lommel
* Patrick Mours (OptiX backend)
* Christophe Hery (subsurface scattering anisotropy)
* William Leeson (PMJ sampling pattern)
* Alaska (various fixes and tweaks)
* Thomas Dinges (various fixes)
For the full commit history, see the cycles-x branch. This squashes together
all the changes since intermediate changes would often fail building or tests.
Ref T87839, T87837, T87836
Fixes T90734, T89353, T80267, T80267, T77185, T69800
2021-09-20 17:59:20 +02:00
|
|
|
const Transform tfm = object_get_transform(kg, sd);
|
2014-12-15 20:21:41 +05:00
|
|
|
P = transform_point(&tfm, P);
|
|
|
|
}
|
|
|
|
|
|
|
|
return P;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCL_NAMESPACE_END
|