MetalByTutorials/21-metal-performance-shaders/projects/starter/Raytracing/Raytracing Shared/RayTracing.metal

130 lines
5.1 KiB
Metal

//
/**
* Copyright (c) 2018 Razeware LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* Notwithstanding the foregoing, you may not use, copy, modify, merge, publish,
* distribute, sublicense, create a derivative work, and/or sell copies of the
* Software in any work that is designed, intended, or marketed for pedagogical or
* instructional purposes related to programming, coding, application development,
* or information technology. Permission for such use, copying, modification,
* merger, publication, distribution, sublicensing, creation of derivative works,
* or sale is expressly withheld.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <metal_stdlib>
#include <simd/simd.h>
#import "ShaderTypes.h"
using namespace metal;
// Add structs here
/*
// Interpolates vertex attribute of an arbitrary type across the surface of a triangle
// given the barycentric coordinates and triangle index in an intersection struct
template<typename T>
inline T interpolateVertexAttribute(device T *attributes, Intersection intersection) {
float3 uvw;
uvw.xy = intersection.coordinates;
uvw.z = 1.0 - uvw.x - uvw.y;
unsigned int triangleIndex = intersection.primitiveIndex;
T T0 = attributes[triangleIndex * 3 + 0];
T T1 = attributes[triangleIndex * 3 + 1];
T T2 = attributes[triangleIndex * 3 + 2];
return uvw.x * T0 + uvw.y * T1 + uvw.z * T2;
}
*/
// Uses the inversion method to map two uniformly random numbers to a three dimensional
// unit hemisphere where the probability of a given sample is proportional to the cosine
// of the angle between the sample direction and the "up" direction (0, 1, 0)
inline float3 sampleCosineWeightedHemisphere(float2 u) {
float phi = 2.0f * M_PI_F * u.x;
float cos_phi;
float sin_phi = sincos(phi, cos_phi);
float cos_theta = sqrt(u.y);
float sin_theta = sqrt(1.0f - cos_theta * cos_theta);
return float3(sin_theta * cos_phi, cos_theta, sin_theta * sin_phi);
}
// Maps two uniformly random numbers to the surface of a two-dimensional area light
// source and returns the direction to this point, the amount of light which travels
// between the intersection point and the sample point on the light source, as well
// as the distance between these two points.
inline void sampleAreaLight(constant AreaLight & light,
float2 u,
float3 position,
thread float3 & lightDirection,
thread float3 & lightColor,
thread float & lightDistance)
{
// Map to -1..1
u = u * 2.0f - 1.0f;
// Transform into light's coordinate system
float3 samplePosition = light.position +
light.right * u.x +
light.up * u.y;
// Compute vector from sample point on light source to intersection point
lightDirection = samplePosition - position;
lightDistance = length(lightDirection);
float inverseLightDistance = 1.0f / max(lightDistance, 1e-3f);
// Normalize the light direction
lightDirection *= inverseLightDistance;
// Start with the light's color
lightColor = light.color;
// Light falls off with the inverse square of the distance to the intersection point
lightColor *= (inverseLightDistance * inverseLightDistance);
// Light also falls off with the cosine of angle between the intersection point and
// the light source
lightColor *= saturate(dot(-lightDirection, light.forward));
}
// Aligns a direction on the unit hemisphere such that the hemisphere's "up" direction
// (0, 1, 0) maps to the given surface normal direction
inline float3 alignHemisphereWithNormal(float3 sample, float3 normal) {
// Set the "up" vector to the normal
float3 up = normal;
// Find an arbitrary direction perpendicular to the normal. This will become the
// "right" vector.
float3 right = normalize(cross(normal, float3(0.0072f, 1.0f, 0.0034f)));
// Find a third vector perpendicular to the previous two. This will be the
// "forward" vector.
float3 forward = cross(right, up);
// Map the direction on the unit hemisphere to the coordinate system aligned
// with the normal.
return sample.x * right + sample.y * up + sample.z * forward;
}