123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- /*
- * Modifications Copyright (c) Contributors to the Open 3D Engine Project.
- * For complete copyright and license terms please see the LICENSE at the root of this distribution.
- *
- * SPDX-License-Identifier: Apache-2.0 OR MIT
- *
- */
- //---------------------------------------------------------------------------------------
- // Shader code utilities for TressFX
- //-------------------------------------------------------------------------------------
- //
- // Copyright (c) 2019 Advanced Micro Devices, Inc. All rights reserved.
- //
- // 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.
- //
- // 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.
- //
- // Cutoff to not render hair
- #pragma once
- #define SHORTCUT_MIN_ALPHA 0.02
- #define TRESSFX_FLOAT_EPSILON 1e-7
- // Needed for DirectionalLightShadow.azsli - o_opacity_mode exists in most shaders but not this one.
- option enum class OpacityMode
- {
- Opaque,
- Cutout,
- Blended,
- TintedTransparent
-
- } o_opacity_mode = OpacityMode::Blended;
- //--------------------------------------------------------------------------------------
- //
- // Controls whether you do mul(M,v) or mul(v,M)
- // i.e., row major vs column major
- //
- //--------------------------------------------------------------------------------------
- float4 MatrixMult(float4x4 m, float4 v)
- {
- return mul(m, v);
- }
- // Pack a float4 into an uint
- uint PackFloat4IntoUint(float4 vValue)
- {
- return (((uint)(vValue.x * 255)) << 24) | (((uint)(vValue.y * 255)) << 16) | (((uint)(vValue.z * 255)) << 8) | (uint)(vValue.w * 255);
- }
- // Unpack a uint into a float4 value
- float4 UnpackUintIntoFloat4(uint uValue)
- {
- return float4(((uValue & 0xFF000000) >> 24) / 255.0, ((uValue & 0x00FF0000) >> 16) / 255.0, ((uValue & 0x0000FF00) >> 8) / 255.0, ((uValue & 0x000000FF)) / 255.0);
- }
- // Pack a float3 and a uint8 into an uint
- uint PackFloat3ByteIntoUint(float3 vValue, uint uByteValue)
- {
- return (((uint)(vValue.x * 255)) << 24) | (((uint)(vValue.y * 255)) << 16) | (((uint)(vValue.z * 255)) << 8) | uByteValue;
- }
- // Unpack a uint into a float3 and a uint8 value
- float3 UnpackUintIntoFloat3Byte(uint uValue, out uint uByteValue)
- {
- uByteValue = uValue & 0x000000FF;
- return float3(((uValue & 0xFF000000) >> 24) / 255.0, ((uValue & 0x00FF0000) >> 16) / 255.0, ((uValue & 0x0000FF00) >> 8) / 255.0);
- }
- //--------------------------------------------------------------------------------------
- //
- // Safe_normalize-float2
- //
- //--------------------------------------------------------------------------------------
- float2 Safe_normalize(float2 vec)
- {
- float len = length(vec);
- return len >= TRESSFX_FLOAT_EPSILON ? (vec * rcp(len)) : float2(0, 0);
- }
- //--------------------------------------------------------------------------------------
- //
- // Safe_normalize-float3
- //
- //--------------------------------------------------------------------------------------
- float3 Safe_normalize(float3 vec)
- {
- float len = length(vec);
- return len >= TRESSFX_FLOAT_EPSILON ? (vec * rcp(len)) : float3(0, 0, 0);
- }
- //--------------------------------------------------------------------------------------
- // ComputeCoverage
- //
- // Calculate the pixel coverage of a hair strand by computing the hair width
- //--------------------------------------------------------------------------------------
- float ComputeCoverage(float2 p0, float2 p1, float2 pixelLoc, float2 winSize)
- {
- // p0, p1, pixelLoc are in d3d clip space (-1 to 1)x(-1 to 1)
- // Scale positions so 1.f = half pixel width
- p0 *= winSize;
- p1 *= winSize;
- pixelLoc *= winSize;
- float p0dist = length(p0 - pixelLoc);
- float p1dist = length(p1 - pixelLoc);
- float hairWidth = length(p0 - p1);
- // will be 1.f if pixel outside hair, 0.f if pixel inside hair
- float outside = any(float2(step(hairWidth, p0dist), step(hairWidth, p1dist)));
- // if outside, set sign to -1, else set sign to 1
- float sign = outside > 0.f ? -1.f : 1.f;
- // signed distance (positive if inside hair, negative if outside hair)
- float relDist = sign * saturate(min(p0dist, p1dist));
- // returns coverage based on the relative distance
- // 0, if completely outside hair edge
- // 1, if completely inside hair edge
- return (relDist + 1.f) * 0.5f;
- }
|