/* =========================================================================== Copyright (C) 2023-2024 Gian 'myT' Schellenbaum This file is part of Challenge Quake 3 (CNQ3). Challenge Quake 3 is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. Challenge Quake 3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Challenge Quake 3. If not, see . =========================================================================== */ // shared utilities #pragma once #include "../common/state_bits.h.hlsli" #include "../common/blend.hlsli" #define PI 3.1415926535897932384626433832795 #define PI_D2 (PI / 2.0) #define PI_D4 (PI / 4.0) #define PI_M2 (PI * 2.0) #define INT8_MIN 0x80 #define INT16_MIN 0x8000 #define INT32_MIN 0x80000000 #define INT64_MIN 0x8000000000000000 #define INT8_MAX 0x7F #define INT16_MAX 0x7FFF #define INT32_MAX 0x7FFFFFFF #define INT64_MAX 0x7FFFFFFFFFFFFFFF #define UINT8_MAX 0xFF #define UINT16_MAX 0xFFFF #define UINT32_MAX 0xFFFFFFFF #define UINT64_MAX 0xFFFFFFFFFFFFFFFF typedef RaytracingAccelerationStructure RTAS; float DegToRad(float deg) { return PI * (deg / 180.0); } float RadToDeg(float rad) { return 180.0 * (rad / PI); } float Brightness(float3 color) { float brightness = dot(color, float3(0.299, 0.587, 0.114)); return brightness; } float4 MakeGreyscale(float4 input, float amount) { float grey = dot(input.rgb, float3(0.299, 0.587, 0.114)); float4 result = lerp(input, float4(grey, grey, grey, input.a), amount); return result; } float LinearDepth(float zwDepth, float zNear, float zFar) { float n = zNear; float f = zFar; float zw = zwDepth; float zl = (f * n) / (n + zw * (f - n)); return zl; } float LinearDepth(float zwDepth, float3 constants) { return constants.x / (constants.y + zwDepth * constants.z); } float PostProjectionDepth(float viewDepth, float zNear, float zFar) { float n = zNear; float f = zFar; float zv = viewDepth; float zw = (n * (f - zv)) / ((f - n) * zv); return zw; } float4 FSTrianglePosFromVertexId(uint id) { return float4( (float)(id / 2) * 4.0 - 1.0, (float)(id % 2) * 4.0 - 1.0, 0.0, 1.0); } float2 FSTriangleTCFromVertexId(uint id) { return float2( (float)(id / 2) * 2.0, 1.0 - (float)(id % 2) * 2.0); } uint PackColor(float4 c) { uint4 u = uint4(saturate(c) * 255.0); uint r = u.r | (u.g << 8) | (u.b << 16) | (u.a << 24); return r; } float4 UnpackColor(uint c) { uint4 u = uint4(c & 0xFFu, (c >> 8) & 0xFFu, (c >> 16) & 0xFFu, (c >> 24) & 0xFFu); float4 r = float4(u) / 255.0; return r; } float EaseInCubic(float x) { return x * x * x; } float EaseOutCubic(float x) { float y = 1.0 - x; return 1.0 - y * y * y; } float EaseInOutCubic(float x) { if(x < 0.5) { return 4 * x * x * x; } float y = -2 * x + 2; return 1 - 0.5 * y * y * y; } float EaseInQuad(float x) { return x * x; } float EaseInExp(float x) { return x == 0.0 ? 0.0 : pow(2.0, 10.0 * x - 10.0); } float EaseOutExp(float x) { return x == 1.0 ? 1.0 : 1.0 - pow(2.0, -10.0 * x); } float smoothstep01(float x) { return smoothstep(0.0, 1.0, x); } // Oct*: octahedron normal vector encoding // original code from "A Survey of Efficient Representations for Independent Unit Vectors" // further improved by Krzysztof Narkowicz and Rune Stubbe float2 OctWrap(float2 v) { return (1.0 - abs(v.yx)) * select(v.xy >= 0.0, 1.0, -1.0); } float2 OctEncode(float3 n) { n /= (abs(n.x) + abs(n.y) + abs(n.z)); n.xy = n.z >= 0.0 ? n.xy : OctWrap(n.xy); n.xy = n.xy * 0.5 + 0.5; return n.xy; } float3 OctDecode(float2 f) { f = f * 2.0 - 1.0; float3 n = float3(f.x, f.y, 1.0 - abs(f.x) - abs(f.y)); float t = saturate(-n.z); n.xy += select(n.xy >= 0.0, -t, t); return normalize(n); } float3 GetPositionFromDepth(float2 tc01, float depthZW, matrix invMatrix) { float x = tc01.x * 2.0 - 1.0; float y = (1.0 - tc01.y) * 2.0 - 1.0; float4 position = mul(float4(x, y, depthZW, 1.0), invMatrix); float3 result = position.xyz / position.w; return result; } float3 TransformNormal(float3 normal, matrix transform) { return mul(transform, float4(normal, 0)).xyz; } float3 TransformPoint(float3 position, matrix transform) { float4 result = mul(transform, float4(position, 1)); return result.xyz / result.w; } float3 RandomColorFromUInt(uint id) { float r = frac(0.420 + 1.337 * id); float g = frac(0.69 + 1.666 * id); float b = frac(0.13 + 1.777 * id); return float3(r, g, b); } float3 BiasPosition(float3 position, float3 normal) { float3 result = position + sign(normal) * abs(position * 0.0000002); return result; } // from Mauricio Vives, https://gist.github.com/pixnblox/5e64b0724c186313bc7b6ce096b08820 // Projects the specified position (point) onto the plane with the specified origin and normal. float3 ProjectPointOnPlane(float3 position, float3 planeOrigin, float3 planeNormal) { return position - dot(position - planeOrigin, planeNormal) * planeNormal; } // from Mauricio Vives, https://gist.github.com/pixnblox/5e64b0724c186313bc7b6ce096b08820 // Computes the shading position of the specified geometric position and vertex positions and // normals. For a triangle with normals describing a convex surface, this point will be slightly // above the surface. For a concave surface, the geometry position is used directly. // NOTE: The difference between the shading position and geometry position is significant when // casting shadow rays. If the geometric position is used, a triangle may fully shadow itself when // it should be partly lit based on the shading normals; this is the "shadow terminator" problem. float3 GetShadingPosition( float3 geomPosition, float3 shadingNormal, float3 positions[3], float3 normals[3], float3 barycentrics) { // Project the geometric position (inside the triangle) to the planes defined by the vertex // positions and normals. float3 p0 = ProjectPointOnPlane(geomPosition, positions[0], normals[0]); float3 p1 = ProjectPointOnPlane(geomPosition, positions[1], normals[1]); float3 p2 = ProjectPointOnPlane(geomPosition, positions[2], normals[2]); // Interpolate the projected positions using the barycentric coordinates, which gives the // shading position. float3 shadingPosition = p0 * barycentrics.x + p1 * barycentrics.y + p2 * barycentrics.z; // Return the shading position for a convex triangle, where the shading point is above the // triangle based on the shading normal. Otherwise use the geometric position. bool convex = dot(shadingPosition - geomPosition, shadingNormal) > 0.0; float3 result = convex ? shadingPosition : BiasPosition(geomPosition, shadingNormal); return result; } // based on "Hacking the Shadow Terminator" by Johannes Hanika in "Ray Tracing Gems II" float3 GetShadingPositionV2(float3 geomPosition, float3 positions[3], float3 normals[3], float3 barycentrics) { float3 tmpu = geomPosition - positions[0]; float3 tmpv = geomPosition - positions[1]; float3 tmpw = geomPosition - positions[2]; float dotu = min(0.0, dot(tmpu, normals[0])); float dotv = min(0.0, dot(tmpv, normals[1])); float dotw = min(0.0, dot(tmpw, normals[2])); tmpu -= dotu * normals[0]; tmpv -= dotv * normals[1]; tmpw -= dotw * normals[2]; float3 shadingPosition = geomPosition + 1.0 * (barycentrics.x * tmpu + barycentrics.y * tmpv + barycentrics.z * tmpw); return shadingPosition; } template T trilerp(T v0, T v1, T v2, float3 barycentrics) { return barycentrics.x * v0 + barycentrics.y * v1 + barycentrics.z * v2; } template<> float trilerp(float v0, float v1, float v2, float3 barycentrics) { return dot(float3(v0, v1, v2), barycentrics); } // Interleaved Gradient Noise by Jorge Jimenez // from "Next Generation Post Processing in Call of Duty: Advanced Warfare" float InterleavedGradientNoise(float2 uv) { float3 magic = float3(0.06711056, 0.00583715, 52.9829189); return frac(magic.z * frac(dot(uv, magic.xy))); } template bool IsInRange(T p, T min, T max) { return all(p >= min) && all(p <= max); } bool Is01(float2 p) { return IsInRange(p, float2(0, 0), float2(1, 1)); } bool Is01(float3 p) { return IsInRange(p, float3(0, 0, 0), float3(1, 1, 1)); } bool Is01(float4 p) { return IsInRange(p, float4(0, 0, 0, 0), float4(1, 1, 1, 1)); } bool IsInTexture(int2 tc, int2 textureSize) { return all(tc >= int2(0, 0)) && all(tc < textureSize); } bool IsInTexture(int3 tc, int3 textureSize) { return all(tc >= int3(0, 0, 0)) && all(tc < textureSize); } template uint2 GetTextureSize(Texture2D texture0) { uint2 size; texture0.GetDimensions(size.x, size.y); return size; } template uint2 GetTextureSize(RWTexture2D texture0) { uint2 size; texture0.GetDimensions(size.x, size.y); return size; } template uint3 GetTextureSize(Texture3D texture0) { uint3 size; texture0.GetDimensions(size.x, size.y, size.z); return size; } template uint3 GetTextureSize(RWTexture3D texture0) { uint3 size; texture0.GetDimensions(size.x, size.y, size.z); return size; } // by Sakib Saikia, https://sakibsaikia.github.io/graphics/2022/01/04/Nan-Checks-In-HLSL.html bool IsNan(float x) { return (asuint(x) & 0x7FFFFFFFu) > 0x7F800000u; } bool isnan(float x) { return IsNan(x); } // from "Using Blue Noise For Raytraced Soft Shadows" by Alan Wolfe in "Ray Tracing Gems II" // this turns the blue noise into a low discrepancy additive recurrence float AnimateBlueNoise(float blueNoise, uint frameIndex) { return frac(blueNoise + float(frameIndex % 32) * 0.61803399); } float2 NDCToTC(float2 ndc) { float2 tc = ndc * float2(0.5, -0.5) + float2(0.5, 0.5); return tc; } float3 NDCToTC(float3 ndc) { float3 tc = ndc * float3(0.5, -0.5, 0.5) + float3(0.5, 0.5, 0.5); return tc; } float2 TCToNDC(float2 tc) { float2 ndc = (2.0 * tc - 1.0) * float2(1, -1); return ndc; } float3 TCToNDC(float3 tc) { float3 ndc = (2.0 * tc - 1.0) * float3(1, -1, 1); return ndc; } // returns the longest vector float2 vmax(float2 a, float2 b) { float2 result = dot(a, a) > dot(b, b) ? a : b; return result; } uint PackHalf2(float2 input) { uint2 d = f32tof16(input); uint result = d.x | (d.y << 16u); return result; } float2 UnpackHalf2(uint input) { uint2 d = uint2(input & 0xFFFFu, input >> 16u); float2 result = f16tof32(d); return result; } float2 CartesianToPolar(float2 cartesian) { float radius = length(cartesian); float angle = atan2(cartesian.y, cartesian.x); float2 polar = float2(radius, angle); return polar; } float2 PolarToCartesian(float2 polar) { float sinAngle, cosAngle; sincos(polar.y, sinAngle, cosAngle); float2 cartesian = polar.x * float2(cosAngle, sinAngle); return cartesian; } // Beer-Lambert law float Transmittance(float distance, float extinction) { float transmittance = exp(-distance * extinction); return transmittance; } // phase function for Mie scattering // g is in the range [-1;1] // -1: backward scattering // 0: isotropic // 1: forward scattering float HenyeyGreenstein(float cosTheta, float g) { float g2 = g * g; float num = 1.0 - g2; float denom = 4.0 * PI * pow(1.0 + g2 - 2.0 * g * cosTheta, 1.5); float result = num / denom; return result; } uint FlattenIndex(uint3 tileIndex, uint3 tileResolution) { return tileIndex.x + tileIndex.y * tileResolution.x + tileIndex.z * tileResolution.x * tileResolution.y; } int FlattenIndex(int3 tileIndex, int3 tileResolution) { return tileIndex.x + tileIndex.y * tileResolution.x + tileIndex.z * tileResolution.x * tileResolution.y; } uint3 UnflattenIndex(uint flatIndex, uint3 tileResolution) { uint h = tileResolution.y; uint wh = tileResolution.x * h; uint z = flatIndex / wh; flatIndex -= z * wh; uint y = flatIndex / h; uint x = flatIndex - y * h; uint3 result = uint3(x, y, z); return result; } int3 UnflattenIndex(int flatIndex, int3 tileResolution) { int h = tileResolution.y; int wh = tileResolution.x * h; int z = flatIndex / wh; flatIndex -= z * wh; int y = flatIndex / h; int x = flatIndex - y * h; int3 result = int3(x, y, z); return result; } void ClearBoundingBox(out int3 boxMin, out int3 boxMax) { boxMin = int3(INT32_MAX, INT32_MAX, INT32_MAX); boxMax = int3(INT32_MIN, INT32_MIN, INT32_MIN); } template void ExpandBoundingBox(inout T boxMin, inout T boxMax, T newPoint) { boxMin = min(boxMin, newPoint); boxMax = max(boxMax, newPoint); } // Credit: Riku Salminen // dispatch the draw call with 36 indices float3 CubeFromVertexID(uint vertexId) { int tri = int(vertexId) / 3; int idx = int(vertexId) % 3; int face = tri / 2; int top = tri % 2; int dir = face % 3; int pos = face / 3; int nz = dir >> 1; int ny = dir & 1; int nx = 1 ^ (ny | nz); float3 d = float3(nx, ny, nz); float flip = 1 - 2 * pos; float3 n = flip * d; float3 u = -d.yzx; float3 v = flip * d.zxy; float mirror = -1 + 2 * top; float3 xyz = n + mirror * (1 - 2 * (idx & 1)) * u + mirror * (1 - 2 * (idx >> 1)) * v; return xyz; } // dispatch the draw call with 6 indices float2 QuadFromVertexID(uint vertexId) { float2 position; position.x = (vertexId >= 1 && vertexId <= 3) ? 1.0 : -1.0; position.y = (vertexId >= 2 && vertexId <= 4) ? -1.0 : 1.0; return position; } float3 AABoxIndexToWorldSpace(int3 index, float3 centerPosition, float3 textureSize, float3 worldScale) { float3 position = centerPosition + worldScale * (float3(index)+float3(0.5, 0.5, 0.5) - 0.5 * textureSize); return position; } float3 AABoxTCToWorldSpace(float3 tc, float3 centerPosition, float3 textureSize, float3 worldScale) { float3 position = centerPosition + worldScale * textureSize * (tc - float3(0.5, 0.5, 0.5)); return position; } float3 AABoxWorldSpaceToTC(float3 position, float3 centerPosition, float3 textureSize, float3 worldScale) { float3 boxSize = worldScale * textureSize; float3 boxMin = centerPosition - 0.5 * boxSize; float3 tc = (position - boxMin) / boxSize; return tc; } int3 AABoxWorldSpaceToIndex(float3 position, float3 centerPosition, float3 textureSize, float3 worldScale) { float3 boxSize = worldScale * textureSize; float3 boxMin = centerPosition - 0.5 * boxSize; float3 indexF = (position - boxMin) / worldScale; int3 index = int3(indexF); return index; } float3 AABoxIndexToWorldSpace(int3 index, float3 centerPosition, float3 textureSize, float worldScale) { return AABoxIndexToWorldSpace(index, centerPosition, textureSize, worldScale.xxx); } float3 AABoxTCToWorldSpace(float3 tc, float3 centerPosition, float3 textureSize, float worldScale) { return AABoxTCToWorldSpace(tc, centerPosition, textureSize, worldScale.xxx); } float3 AABoxWorldSpaceToTC(float3 position, float3 centerPosition, float3 textureSize, float worldScale) { return AABoxWorldSpaceToTC(position, centerPosition, textureSize, worldScale.xxx); } int3 AABoxWorldSpaceToIndex(float3 position, float3 centerPosition, float3 textureSize, float worldScale) { return AABoxWorldSpaceToIndex(position, centerPosition, textureSize, worldScale.xxx); } template T min3(T v0, T v1, T v2) { return min(v0, min(v1, v2)); } template T max3(T v0, T v1, T v2) { return max(v0, max(v1, v2)); } template T min4(T v0, T v1, T v2, T v3) { return min(min(v0, v1), min(v2, v3)); } template T max4(T v0, T v1, T v2, T v3) { return max(max(v0, v1), max(v2, v3)); } // credit: Inigo Quilez // returns t == -1 when nothing was hit float RaytraceSphere(float3 rayOrigin, float3 rayDir, float3 spherePos, float sphereRadius) { float3 oc = rayOrigin - spherePos; float b = dot(oc, rayDir); float c = dot(oc, oc) - sphereRadius * sphereRadius; float h = b * b - c; if(h < 0.0) { return -1.0; } h = sqrt(h); float t = -b - h; return t; } float3 DirectionFromLongLat(float longitude01, float latitude01) { float lon = longitude01 * PI_M2; float lat = latitude01 * PI_M2; float sinLat, cosLat; sincos(lat, sinLat, cosLat); float sinLon, cosLon; sincos(lon, sinLon, cosLon); float3 direction = float3(cosLat * sinLon, sinLat * sinLon, cosLon); return direction; } float3 AmbientColor(float4 payloadA, float4 payloadB, float3 normal, float3 fallbackColor) { float3 ambColor = payloadA.rgb; float3 localColor = float3(payloadA.a, payloadB.rg); float3 localDir = DirectionFromLongLat(payloadB.b, payloadB.a); float localScale = dot(localDir, normal) * 0.5 + 0.5; // wraps around float3 interpColor = ambColor + localColor * localScale; float brightNew = Brightness(interpColor); float brightFall = Brightness(fallbackColor); float t = saturate(brightNew / max(brightFall, 0.001)); float3 color = lerp(fallbackColor, interpColor, t); return color; }