Files
UnrealEngineUWP/Engine/Shaders/Common.usf
Chris Bunner ab9d8e35b1 Copying //UE4/Dev-Rendering to //UE4/Dev-Main (Source: //UE4/Dev-Rendering @ 3388261)
#lockdown Nick.Penwarden
#rb None

==========================
MAJOR FEATURES + CHANGES
==========================

Change 3358140 on 2017/03/22 by Rolando.Caloca

	DR - Fix copy to cube face
	- Compile fix when using dump layer
	- Add new error enum

Change 3358301 on 2017/03/22 by Mitchell.Wilson

	Initial check in of LODs in InfiltratorForward. First pass on optimization in level. Adding a visibility track for SceneCapture2D in tunnel section.

Change 3358477 on 2017/03/22 by Mitchell.Wilson

	Updating Skeletal Mesh DPW_Robot_Export to resolve screen size being too low for LOD1. Cleaned up LOD1 which was showing some visible popping when transitioning.

Change 3358529 on 2017/03/22 by Mark.Satterthwaite

	Globally disable clang's "constant-logical-operand" warning when running under Distcc - it is much easier and less invasive than constantly fixing the code.

Change 3358745 on 2017/03/22 by Mark.Satterthwaite

	Disable another warning (parentheses-equality) under Distcc because again the separation of preprocessing from compilation means it turns up where it isn't expected.

Change 3358837 on 2017/03/22 by Joe.Graf

	Merge of pull request #3214 for the RenderDocPlugin

	#CodeReview: matt.kuhlenschmidt, marcus.wassmer
	#rb: marcus.wassmer

Change 3359112 on 2017/03/22 by Ben.Salem

	Update perf monitor to include frame time by default. Also, use only  game/PIE world timers when in editor, instead of all worlds combined.

	#tests Ran several Showdown test runs with plugin!

Change 3359363 on 2017/03/22 by Joe.Graf

	First pass at non-unity & no pch compilation

Change 3359449 on 2017/03/22 by Joe.Graf

	Added missing null check when exporting a EXR on Linux (UE-40268)

	#CodeReview: dmitry.rekman
	#rb: n/a

Change 3360349 on 2017/03/23 by Guillaume.Abadie

	Fixes TAA's AA_FORCE_ALPHA_CLAMP causing DOF layouts.

	#jira UE-42920

Change 3360405 on 2017/03/23 by Marcus.Wassmer

	Better method for detecting Kepler

Change 3360718 on 2017/03/23 by Daniel.Wright

	Planar reflections handle views smaller than the render target in a general way
	* Fixes planar reflections with adaptive pixel density (ViewFamily size larger than actual views combined)
	* Planar reflections are now supported in splitscreen

Change 3360758 on 2017/03/23 by Daniel.Wright

	[Copy] Added new light property bCastVolumetricShadow, which defaults to true for directional and sky lights, but false for point / spot lights as supporting volumetric fog shadowing has significant GPU overhead

Change 3360762 on 2017/03/23 by Daniel.Wright

	[Copy] Texture flags are now properly routed to RHICreateTexture3D from the render target pool

Change 3360768 on 2017/03/23 by Daniel.Wright

	[Copy] Disabled GPUProfiler histogram by default, controlled by r.ProfileGPU.ShowEventHistogram

Change 3360770 on 2017/03/23 by Daniel.Wright

	[Copy] Disabled fast clears on CustomDepth, saves .2ms on xbox

Change 3360771 on 2017/03/23 by Daniel.Wright

	[Copy] Particle lights no longer force tiled deferred lighting.  Tiled deferred lighting is only used if enough unshadowed lights + particle lights are on screen.  Saves 1.5ms Xbox with one particle light.

Change 3360774 on 2017/03/23 by Daniel.Wright

	[Copy] Distance field cvar comments

Change 3360782 on 2017/03/23 by Daniel.Wright

	[Copy] Disabled selection color on Volume materials

Change 3360795 on 2017/03/23 by Daniel.Wright

	[Copy] Volume materials now specify Albedo and Extinction, which is more intuitive than Scattering and Absorption.  Albedo is [0-1] reflectance, while Extinction is a world space density.

Change 3360799 on 2017/03/23 by Daniel.Wright

	[Copy] Cinematic scalability levels get 2x volumetric fog resolution in x and y

Change 3360806 on 2017/03/23 by Daniel.Wright

	[Copy] Fixed volumetric fog being offset when viewport min is not 0

Change 3360809 on 2017/03/23 by Daniel.Wright

	[Copy] Volumetric fog now adds a bias to the inverse squared light falloff denominator, prevents extreme aliasing from the hotspot.  Can be controlled with r.VolumetricFog.InverseSquaredLightDistanceBiasScale.

Change 3361651 on 2017/03/23 by Brian.Karis

	Higher quality sharp SSR at quality 4

Change 3361678 on 2017/03/23 by Brian.Karis

	Fresnel darkens diffuse for clearcoat.

Change 3361683 on 2017/03/23 by Brian.Karis

	Fixed SSR artifact

Change 3361691 on 2017/03/23 by Brian.Karis

	Chagned min roughness limit

Change 3361707 on 2017/03/23 by Brian.Karis

	Added inverse film tone map

Change 3361726 on 2017/03/23 by Brian.Karis

	Better precision inverse

Change 3361758 on 2017/03/23 by Brian.Karis

	Material flag normal curvature to roughness is no longer forward only.

Change 3361765 on 2017/03/23 by Brian.Karis

	Update ACES

Change 3361774 on 2017/03/23 by Brian.Karis

	Cleaned up alpha support and disabled screen edge clipping.

Change 3362478 on 2017/03/24 by Guillaume.Abadie

	Cherry pick 3316084's PostProcessing.cpp: Fixes a bug in Circle DOF where the apply pass was no longer using the downres DOF's TAA output.

	#author Brian.Karis

	#jira UE-42920

Change 3362738 on 2017/03/24 by Rolando.Caloca

	DR - Hide scene capture on IF

Change 3362890 on 2017/03/24 by Guillaume.Abadie

	Renames r.SceneAlpha to r.PostProcessing.PropagateAlpha

Change 3363665 on 2017/03/24 by Mark.Satterthwaite

	PR #3414: Add command line option "-noheartbeatthread" to disable heart beat thread (Contributed by JeffRous)

Change 3363866 on 2017/03/24 by Arne.Schober

	DR - Updated NVAPI
	#RB Marcus.Wassmer

Change 3364300 on 2017/03/24 by Brian.Karis

	SSR use dynamic velocity

Change 3364372 on 2017/03/24 by Brian.Karis

	Fix changing off axis projection velocities.

Change 3364373 on 2017/03/24 by Brian.Karis

	Enabled velocity drawing in scene captures

Change 3365531 on 2017/03/27 by Guillaume.Abadie

	Computes the material's screen position material expression directly from the pixel shader SvPosition

Change 3365764 on 2017/03/27 by Chris.Bunner

	Lowering severity of crash for missing values in scalability.ini.

	#jira UE-41331

Change 3365916 on 2017/03/27 by Guillaume.Abadie

	Exposes the viewport offset within the view property material expression

Change 3365979 on 2017/03/27 by Brian.Karis

	Fixed skylight intensity from double applying

Change 3365987 on 2017/03/27 by Brian.Karis

	Stopped post process indirect lighting intensity from scaling skylight reflections

Change 3365991 on 2017/03/27 by Brian.Karis

	Fix for static analysis

Change 3366028 on 2017/03/27 by Daniel.Wright

	Volumetric fog supports static shadowing from Stationary lights
	* Using bilinear on static shadowmap depths + 1 PCF to smooth out results

Change 3366029 on 2017/03/27 by Daniel.Wright

	Static shadow depth maps for Stationary point and spot lights are 2x higher res by default (4x more texels), which is more appropriate for volumetric fog

Change 3366055 on 2017/03/27 by Guillaume.Abadie

	Cherry picks 3251469: Implements scene capture component's CaptureSortPriority to control GPU execution order in order to manage inter dependencies.

Change 3366447 on 2017/03/27 by Simon.Tourangeau

	Fix IES light profile importer.
	- Bug in the LM-63-1986 format importer.

Change 3366836 on 2017/03/27 by Brian.Karis

	ClearUAV now supports int types

Change 3367435 on 2017/03/28 by Benjamin.Hyder

	Submitting Decal Automation map for initial approval

Change 3367572 on 2017/03/28 by Chris.Bunner

	Changed ClampedPow {max(abs(x),0.00001)} to PositiveClampedPow {max(x,0)} to give more expected results to Power node in material graphs.

	#jira UE-42989

Change 3367756 on 2017/03/28 by Olaf.Piesche

	Niagara material usage flags

Change 3367835 on 2017/03/28 by Marcus.Wassmer

	Fix crash when TileRenderer runs before anything else.  Make explicit behavior when rendering at a time when there is no valid scene.

Change 3367837 on 2017/03/28 by Marcus.Wassmer

	Missed a file.

Change 3367838 on 2017/03/28 by Richard.Wallis

	Updated items from original shelved version by Mark Satt:

	- Added MetalBackend.cpp to change main function string to have an initial crc + code length zero's

	**Description below taken from Mark Satt's original verison of this in CL3343280**

	Updated for Dev-Rendering's PSOs & integrates Richard's work on RHI shader libraries.

	Replace the FShaderCache's cook-time binary shader cache with Dmitriy Dyomin's standalone FShaderCodeLibrary that saves all shader byte-code arrays to files named by the FSHAHash. This de-duplicates shaders so we only ever store the byte code once. Includes optional support for generating a platform specific library file - which Metal implements to provide a single Metal library. The platform-native implementation can perform more de-duplication and in the case of Metal has lower file overheads and will compress more efficiently.

	- All of the support code for the FShaderCache's cook caching is gone, which affects all platforms. The FShaderCodeLibrary is currently  supported by Cook-By-The-Book but can be used with iterate or child cookers - only DLC cooking requires further work.
	- With further modifications it should be possible to support Cook-on-the-Fly as well (output directories would be needed in FShaderCodeLibrary::InitForCooking) and the file-access pattern should be changed to use async. IO so that Material loading is not considered complete until all required byte-code arrays are loaded into the FShaderCodeLibrary.
	- For Metal archiving shaders this way will compile with debug information and the FShaderCodeLibrary, with some help from extensions to IShaderFormat, will save the debug information out into separate files during cooking - these can then be used to debug the game without having to locally recompile, recook & repackage but the shipped byte-code is stripped. Global shader caches are also subject to de-duplication in the library in order to support Metal's shader stripping.
	- File Move operations need to respect the 'Replace' flag - for FShaderCodeLibrary to work we need Move to be atomic.
	- This bumps the object version and will cause all content to recook.
	- Native library support is optional - only Metal currently implements one, but so could Vulkan and D3D12. For Metal the big advantages are further de-duplication where different materials generate the same MetalSL text but a different FSHAHash, that the single Metal library has lower overhead and that as a single file it all compresses far better (esp. with LZMA - 5x smaller).

Change 3367854 on 2017/03/28 by Mark.Satterthwaite

	Don't track or record draw call resources for non-OpenGL shader platforms in the shader-cache as it is unnecessary and makes it slower on the CPU than it needs to be.

Change 3367877 on 2017/03/28 by Brian.Karis

	Fixed linux build hopefully

Change 3368001 on 2017/03/28 by Mark.Satterthwaite

	Compile fixes from Richard's checkin caused by not having visibility to all platforms from my original shelves.

Change 3368019 on 2017/03/28 by Mark.Satterthwaite

	And another fix for Windows compilation of MetalShaderFormat.

Change 3368042 on 2017/03/28 by Mark.Satterthwaite

	And a couple of simpler MSVC errors.

Change 3368271 on 2017/03/28 by Mark.Satterthwaite

	Make SceneRenderTargets compile again.

Change 3368691 on 2017/03/28 by Daniel.Wright

	[Copy from BenW] Renamed r.Shadow.MaxCSMShadowResolution to r.Shadow.MaxCSMResolution to match scalability inis

Change 3369689 on 2017/03/29 by Marcus.Wassmer

	Fix non editor compile for now

Change 3369862 on 2017/03/29 by Marcus.Wassmer

	Get the rest of the things compiling again.

Change 3369896 on 2017/03/29 by Chris.Bunner

	Enabling AMD HDR support by default.

	#jira UE-42113

Change 3370535 on 2017/03/29 by Marcus.Wassmer

	DR - Fix template explicit instantiation for ClearUAV permutations
	#RB Brian.Karis, Arne.Schober

Change 3370704 on 2017/03/29 by Rolando.Caloca

	DR - Rewrote GPU Skin Cache
	- Per section buffers
	- Limited memory per non-editor worlds (control with r.SkinCache.SceneMemoryLimitInMB)
	Copied from 3370529

Change 3371389 on 2017/03/30 by Richard.Wallis

	Remove temp working directories after archive packages built.

Change 3371641 on 2017/03/30 by Rolando.Caloca

	DR - Copy 3371640 (fix mem leak)

Change 3372436 on 2017/03/30 by Uriel.Doyon

	Added flags in UPrimitiveComponent to keep track of its state in the streaming manager.
	This allows to avoid unnecessary callback and processing in begin destroy reattach and being destroy logic.

	Removed the limitation of only processing UMeshComponent when handling spawed primitive.
	This releases the level manager from having to manage dynamic primitives.
	This improves performance by not having to manage dynamic references in the level manager.

	Primitives managed as dynamic now have a callback when ever their proxy is udpated, handling
	many cases automatically where previously a manual callback to notify would have been required.

	Fixed an issue where primitives with no reference to streaming textures would loose they dynamic state
	because of lack of references in the streamer.

Change 3372740 on 2017/03/30 by Chris.Bunner

	[Experimental] Partial compute post process pipeline (r.PostProcess.PreferCompute).
	StencilSceneTexture added to deferred list.
	A few known issues to be fixed in a follow-up CL.

Change 3372765 on 2017/03/30 by Uriel.Doyon

	Disabled concurrent call to NotifyPrimitiveUpdated while we don't have a safe concurrent update

Change 3372979 on 2017/03/30 by Richard.Hinckley

	#jira UE-43501
	The stencil buffer can now use single-channel bitmasks that ignore depth. This makes it possible to detect overlaps between stencil objects.

Change 3373053 on 2017/03/30 by Simon.Tourangeau

	LPV Fade support
	- mostly integrated from CL 2959511

Change 3373272 on 2017/03/30 by Uriel.Doyon

	Added support for the concurrent update of dynamic primitives by the streaming manager.

Change 3373450 on 2017/03/30 by Rolando.Caloca

	DR - FNT - Fix bad data for odd texcoord channels used on skin cache passthrough factory
	Copy 3373364

	#jira UE-43492

Change 3373470 on 2017/03/30 by Marcus.Wassmer

	Nvidia Aftermath support

Change 3374187 on 2017/03/31 by Chris.Bunner

	Volume texture support for CombineLUTs/Tonemap compute pass.
	Refactored common param code to shared sub-class in CombineLUTs and Tonemap PS/CS.
	Skip compute post process out-of-bounds writes.
	Unsigned type conversion fixes.
	Trimmed compute post process shader inputs.

Change 3374233 on 2017/03/31 by Chris.Bunner

	Removed several redundant post process compute fences and resource transitions.
	Added testing CVar to force compute post processes to async (r.PostProcess.ForceAsyncDispatch).

Change 3374412 on 2017/03/31 by Rolando.Caloca

	DR - Fix static analysis

Change 3374544 on 2017/03/31 by Richard.Wallis

	FShaderCache Parallel-Context-Aware Merged with FShaderCache Single Library.

	Future Work
	- This was done before Engine PSO were in so this now needs a refector in the recording and playback on pipeline states instead an emulate PSO in OpenGL Driver.
	- Remove FShaderCacheState and replace the logic with FGraphicsPipelineStateInitializer which should be able to record from the RHI current pipeline state
	- This would reduce the Locking required as it's naturally per thread/context and only the final record would need a lock

Change 3374588 on 2017/03/31 by Richard.Wallis

	Windows Compile Fixes

Change 3374810 on 2017/03/31 by Benjamin.Hyder

	updating recommended GPU drivers

Change 3375207 on 2017/03/31 by Rolando.Caloca

	DR - vk - Fixed swapchain format selection for some Linux platforms

Change 3375248 on 2017/03/31 by Rolando.Caloca

	DR - vk - Prefer D32S8

Change 3375495 on 2017/03/31 by Rolando.Caloca

	DR - vk - Update to sdk 1.0.42.2

Change 3375496 on 2017/03/31 by Rolando.Caloca

	DR - Force compiling with updated Vulkan SDK

Change 3375636 on 2017/03/31 by Mark.Satterthwaite

	Copying Metal improvements from task stream, with some modifications:
	- Off-by-default implementations for MTLFence & MTLHeap, including some small changes to the RHI interface for parallel contexts.
	- Support for Apple's Instruments "Points of Interest" tool.
	- Consolidation of some Mac & iOS compiler, memory and thread handling code.
	- Fixes for Metal not having implicit buffer SRV typecasting for DistanceField effects.
	- Improvements to the internal FMetalDebug layer, still off by default.
	- Limited support for Xcode automatic code-signing for iOS/tvOS.
	- Minimisation of render-target changes in some rendering code, esp. SceneOcclusion, DBufferDecals.
	- Added RHISetResourceAliasability_RenderThread to FDynamicRHI for RHIs to implement simple render-target aliasing.
	- Added FApplePlatformObject, a custom block allocator for Objective-C types (with NSZombie support) which is now used in MetalRHI to decrease allocation costs of Objective-C types.
	- Smattering of lesser fixes.

Change 3375654 on 2017/03/31 by Mark.Satterthwaite

	Incremental Windows build fix.

Change 3375656 on 2017/04/01 by Mark.Satterthwaite

	Correct extern declaration, including the module export macro which Mac unhelpfully doesn't enforce (for now...).

Change 3375797 on 2017/04/01 by Mark.Satterthwaite

	Nullability qualifiers to fix Mac build-farm compilation: perversely this is not a problem for local builds...

Change 3375798 on 2017/04/01 by Mark.Satterthwaite

	Fix the first mis-merge in ParticleGpuSimulation - these changes clearly weren't properly resolved in the task-stream.

Change 3375835 on 2017/04/01 by Mark.Satterthwaite

	Try again with nullability and fix the occlusion changes as the PSO work wasn't merged correctly.

Change 3376143 on 2017/04/02 by Mark.Satterthwaite

	Switch back to flat dSYMs for Dev-Rendering - they don't work with Instruments etc. but they are required by our build system.

Change 3376324 on 2017/04/03 by Chris.Bunner

	Fixed cvar re-registration log spam and flagged a testing-only cvar as such.

Change 3376726 on 2017/04/03 by Benjamin.Hyder

	Submitting initial HDR test map (WIP)

Change 3376756 on 2017/04/03 by Guillaume.Abadie

	Fixes scene captures ordering's backward compatibility.

	Before, 2d scene captures were rendered before cube scene captures. The CaptureSortPriority broke backward compatibility by settings this new member to 0 in the USceneCaptureComponent's constructor. Since it is a higher come first policy, this CL set the default of this value to 1 in USceneCaptureComponent2D's constructor.

Change 3377378 on 2017/04/03 by Arne.Schober

	DR - Fix ShaderRecompiling over and over again
	#RB Chris.Bunner

Change 3377512 on 2017/04/03 by Daniel.Wright

	[Copy] Fixed profilegpu in d3d12 - initialize FLongGPUTaskPS when it is safe to do so, and fixed FSlateRHIRenderer's incorrect usage of draw events

Change 3377518 on 2017/04/03 by Daniel.Wright

	[Copy] Distance field atlas coalesces updates to reduce RHIUpdateTexture3D memory overhead on d3d12

Change 3377526 on 2017/04/03 by Daniel.Wright

	[Copy] "Ran out of GPU queries!" log only happens once

Change 3377535 on 2017/04/03 by Daniel.Wright

	[Copy] Fixed unreferenced local variable

Change 3377539 on 2017/04/03 by Daniel.Wright

	[Copy] Xbox One RHIGetResourceInfo takes ESRAM into account - fixes render target pool 'VRamInKB request failed' messages

Change 3377546 on 2017/04/03 by Daniel.Wright

	[Copy] Added r.LightMaxDrawDistanceScale for local light scalability

Change 3377553 on 2017/04/03 by Daniel.Wright

	[Copy] Removed NEW_ESRAM_ALLOCATOR define and old unused path

Change 3377560 on 2017/04/03 by Daniel.Wright

	[Copy] Fixed two d3d12 refcounting bugs causing -norhithread crashes

Change 3377565 on 2017/04/03 by Daniel.Wright

	[Copy] Fixed Xbox One deleting GPU resources before the GPU is done reading from them (GRHINeedsExtraDeletionLatency was false)

Change 3377572 on 2017/04/03 by Daniel.Wright

	[Copy] Disabled point / spot lights with MaxDrawDistance on LowPC

Change 3377586 on 2017/04/03 by Daniel.Wright

	Fixed compile error

Change 3377699 on 2017/04/03 by David.Hill

	FFT Code.  Moved over from raven and refactored

	#review-3374589 @guillaume.abadie

Change 3377910 on 2017/04/03 by David.Hill

	GPU FFT: Fix Linux Build
	adding a missing template<> to an IMPLEMENT_SHADER_TYPE

Change 3378751 on 2017/04/04 by Marcus.Wassmer

	HQ particle lights now spawn attached to the same socket as their parent module.

Change 3378819 on 2017/04/04 by Richard.Wallis

	Should be no need to protect shader cache against RHI thread now.

Change 3378823 on 2017/04/04 by Richard.Wallis

	FRHIShaderLibrary Opaque Type

	- Base FRHIShaderLibrary has no Create*Shader functions and is passed to Overloaded RHICreate*Shader functions instead of creation directly through the library.
	- Assumed that only Native libraries will end up in the RHICreate*Shader functions.
	- ShaderCache and ShaderCode Libraries now inherit from a common factory interface.

Change 3378883 on 2017/04/04 by Arne.Schober

	DR - Fix DCC build

Change 3378885 on 2017/04/04 by Richard.Wallis

	Metal resource cast compile fix post merge.

Change 3378946 on 2017/04/04 by Chris.Bunner

	SM4 assert fix.

Change 3378953 on 2017/04/04 by Chris.Bunner

	Fixed type-correctness on legacy BreakMA material nodes and set more flexible formats to global attributes which should result in much more forgiving graphs for users.
	Allowed material nodes to opt out of mask-based pin coloration.
	#tests Compiled most Paragon materials + QAGame test maps.

	#jira UE-39885

Change 3379189 on 2017/04/04 by Arne.Schober

	DR - Fix aftermath staging

Change 3379229 on 2017/04/04 by Arne.Schober

	DR - Fix missing include

Change 3379374 on 2017/04/04 by Mark.Satterthwaite

	Revert an accidentally merged change in MacPlatformProcess that relies on further changes from the Metal task stream.

Change 3379505 on 2017/04/04 by Rolando.Caloca

	DR - Fix mismatched interpolators

Change 3379539 on 2017/04/04 by Mark.Satterthwaite

	No FFT for any hlslcc platform - the IR for one or more RWTexture2D isn't quite right...

	#jira UE-43626

Change 3379561 on 2017/04/04 by Rolando.Caloca

	DR - Fix root signature issues on D3D12 PC

Change 3379590 on 2017/04/04 by Mark.Satterthwaite

	Back out changelist 3379539 & change the shader slightly instead, the HLSLCC library generates bogus IR when you have an inout RWTexture.

	#jira UE-43626

Change 3379917 on 2017/04/04 by Uriel.Doyon

	Fix to input mismatch

Change 3380578 on 2017/04/05 by Chris.Bunner

	Shader type fixes.

	#jira UE-43652

Change 3380639 on 2017/04/05 by Rolando.Caloca

	DR - Expose GetOrCreate PSO and document

Change 3380821 on 2017/04/05 by Guillaume.Abadie

	Fixes a crash in USceneCaptureComponent::UpdateDeferredCaptures()

	#jira UE-43642

Change 3381092 on 2017/04/05 by Guillaume.Abadie

	Cherry pick 3362517: Implements TAA's scene color unpremultiplication from alpha channel to reduce DOF alpha channel temporal ghosting.

	This CL take the oportunity to transform AA_ALPHA to an compile time enumeration, and add a basic TAA compile time configuration validation to improve readability of the different TAA passes' configurations.

Change 3381300 on 2017/04/05 by Mark.Satterthwaite

	Quick fix for changes to MetalRHI's render-thread safe texture creation not correctly handling AVFoundation video player handing us an IOSurface.

	#jira UE-43597

Change 3381359 on 2017/04/05 by Guillaume.Abadie

	Back out changelist 3381092

Change 3381421 on 2017/04/05 by Mark.Satterthwaite

	Amended CL #3380995 from Richard Wallis to address crash in the Material Editor under the validation layer - when there are no textures bound the default pass descriptor assigns store actions, which means we can't override them with our deferred store actions.

	#jira UE-43689

Change 3381422 on 2017/04/05 by Mark.Satterthwaite

	Absolute time queries can't be batched in Metal but I also can't rely on them being started with a call to BeginQuery - only EndQuery.

	#jira UE-43691

Change 3381503 on 2017/04/05 by Daniel.Wright

	More intuitive controls for Volumetric Fog
	* Removed ScatteringScale / AbsorptionScale on Exponential Height Fog and added Albedo / Extinction
	* InscatteringColorCubemap is now supported by Volumetric Fog
	* Particle lights have a default VolumetricScatteringIntensity of 0 to avoid trailing
	* Tweaked GVolumetricFogDepthDistributionScale better for nearby details
	* Volume Materials have twice the interpolators available

Change 3381527 on 2017/04/05 by Mark.Satterthwaite

	Disable Private GPU storage for PVRTC texture formats on iOS Metal - these require more changes to the blit-encoder usage as PVRTC has strange requirements.

Change 3381671 on 2017/04/05 by Mark.Satterthwaite

	Better error message for failure to compile shaders remotely from PC for Metal.

Change 3381769 on 2017/04/05 by Rolando.Caloca

	DR - Added lock texture array 2d on Vulkan

Change 3382003 on 2017/04/05 by Mark.Satterthwaite

	Remove the automatic Metal aliasing/re-use when releasing some resource types as it doesn't work as intended.

Change 3382030 on 2017/04/05 by Zachary.Wilson

	Fix compiling Metal text shaders from PC broken in merge from task stream.
	#submitter mark.satterthwaite

	#jira UE-43652

Change 3382880 on 2017/04/06 by Mark.Satterthwaite

	Michael Trepka's CL #3379927:
	VolumetricFogVoxelization implementation for Mac

Change 3383315 on 2017/04/06 by Mark.Satterthwaite

	Partially revert CL #3382003 - the emulated Metal heaps require invoking makeAliasable in order to reclaim memory.

	#jira UE-43739

Change 3384639 on 2017/04/07 by Marcus.Wassmer

	Move ShaderResource version bump to RenderingObjectVersion

Change 3384704 on 2017/04/07 by Mark.Satterthwaite

	Compile fix for merge.

Change 3384933 on 2017/04/07 by Rolando.Caloca

	DR - Fix skin cache crash with BP (copy 3384714)

Change 3385104 on 2017/04/07 by Mark.Satterthwaite

	Fix MetalRHI's abs(int2) handling - it can't be translated to fabs(int2) as that won't compile. Also rebuild hlslcc for my sanity.

	#jira UE-43783

Change 3385105 on 2017/04/07 by Mark.Satterthwaite

	Force a shader rebuild to ensure that everybody picks up the fix for #jira UE-43783

	#jira UE-43783

Change 3385118 on 2017/04/07 by Arne.Schober

	DR - [OR-37359] - Fix disapearing Decals when StencilLod Fade is enabled
	#RB none

Change 3385149 on 2017/04/07 by Marcus.Wassmer

	Fix skincache motion blur

Change 3385189 on 2017/04/07 by Rolando.Caloca

	DR - Fix swapchain format for editor on Vulkan

Change 3385287 on 2017/04/07 by Mark.Satterthwaite

	Enable SM5 on Intel as of 10.12.4 and later.

Change 3385347 on 2017/04/07 by Rolando.Caloca

	DR - Temp fix for GL4 corruption on editor
	#jira UE-43785

Change 3385363 on 2017/04/07 by Rolando.Caloca

	DR - Actually fix all win platforms for GL bug
	#jira UE-43785

Change 3385557 on 2017/04/07 by Arne.Schober

	DR - [UE-43205] - Fix mesh paint
	#RB none

Change 3385608 on 2017/04/07 by Daniel.Wright

	Fixed SampleCmp being used on a non-depth texture, causing a d3d error

Change 3385980 on 2017/04/10 by Rolando.Caloca

	DR - Remove transition functions RHIClearColor* RHIClearDepthStencilTexture

Change 3386042 on 2017/04/10 by Rolando.Caloca

	DR - Fix metal merge issue

Change 3386157 on 2017/04/10 by Rolando.Caloca

	DR - Remove VS2013 libs generation off hlslcc & glslang (to match main)

Change 3386356 on 2017/04/10 by Chris.Bunner

	Resolving merge errors.

Change 3386414 on 2017/04/10 by Chris.Bunner

	Resolved merge issue in RendererScene.cpp.

Change 3386700 on 2017/04/10 by Mark.Satterthwaite

	Silence documentation warnings.

Change 3387178 on 2017/04/10 by Chris.Bunner

	Removed invalid mask correction on MakeMA material nodes.

Change 3388177 on 2017/04/11 by Marcus.Wassmer

	Disable ensure that is no longer relevant now that we bind clear colors on texture creation

Change 3388261 on 2017/04/11 by Chris.Bunner

	Static analysis fix.

[CL 3388266 by Chris Bunner in Main branch]
2017-04-11 10:32:07 -04:00

1960 lines
62 KiB
Plaintext

// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
/*=============================================================================
Common.usf: Common shader code
=============================================================================*/
#pragma once
#if PS4_PROFILE
// always #include PS4Common.usf so it can #define override anything in any other included file.
#include "PS4/PS4Common.usf"
#endif
#if XBOXONE_PROFILE
#include "XboxOne/XboxOneCommon.usf"
#endif
#if COMPILER_METAL
// Helps with iteration when changing Metal shader code generation backend.
#include "MetalCommon.usf"
#endif
#if VULKAN_PROFILE
// Helps with iteration when changing Vulkan shader code generation backend.
#include "VulkanCommon.usf"
#endif
#ifndef FORCE_FLOATS
#define FORCE_FLOATS 0
#endif
#if (!(COMPILER_GLSL_ES2 || COMPILER_GLSL_ES3_1) && !METAL_PROFILE) || FORCE_FLOATS
// Always use floats when not using the ES2 compiler, because low precision modifiers are currently only tweaked for ES2,
// And we don't want potential side effects on other platforms
#define half float
#define half1 float1
#define half2 float2
#define half3 float3
#define half4 float4
#define half3x3 float3x3
#define half4x4 float4x4
#define half4x3 float4x3
#define fixed float
#define fixed1 float1
#define fixed2 float2
#define fixed3 float3
#define fixed4 float4
#define fixed3x3 float3x3
#define fixed4x4 float4x4
#define fixed4x3 float4x3
#endif
// These types are used for material translator generated code, or any functions the translated code can call
#if PIXELSHADER
#define MaterialFloat half
#define MaterialFloat2 half2
#define MaterialFloat3 half3
#define MaterialFloat4 half4
#define MaterialFloat3x3 half3x3
#define MaterialFloat4x4 half4x4
#define MaterialFloat4x3 half4x3
#else
// Material translated vertex shader code always uses floats,
// Because it's used for things like world position and UVs
#define MaterialFloat float
#define MaterialFloat2 float2
#define MaterialFloat3 float3
#define MaterialFloat4 float4
#define MaterialFloat3x3 float3x3
#define MaterialFloat4x4 float4x4
#define MaterialFloat4x3 float4x3
#endif
#if COMPILER_GLSL_ES2 || COMPILER_METAL || COMPILER_SWITCH
float determinant(float3x3 M)
{
return
M[0][0] * (M[1][1] * M[2][2] - M[1][2] * M[2][1]) -
M[1][0] * (M[0][1] * M[2][2] - M[0][2] * M[2][1]) +
M[2][0] * (M[0][1] * M[1][2] - M[0][2] * M[1][1]);
}
#endif
#if POST_PROCESS_ALPHA
#define SceneColorLayout float4
#define CastFloat4ToSceneColorLayout(x) (x)
#define SetSceneColorLayoutToFloat4(dest,value) dest = (value)
#else
#define SceneColorLayout float3
#define CastFloat4ToSceneColorLayout(x) ((x).rgb)
#define SetSceneColorLayoutToFloat4(dest,value) dest.rgb = (value)
#endif
// Generated file that contains uniform buffer declarations needed by the shader being compiled
#include "GeneratedUniformBuffers.usf"
// to support the console command "r.InvalidateShaderCache"
#include "ShaderVersion.usf"
// uniform buffers specifics
#include "CommonViewUniformBuffer.usf"
#include "InstancedStereo.usf"
#include "Definitions.usf"
#define FEATURE_LEVEL_ES2 1
#define FEATURE_LEVEL_ES3_1 2
#define FEATURE_LEVEL_SM3 3
#define FEATURE_LEVEL_SM4 4
#define FEATURE_LEVEL_SM5 5
#define FEATURE_LEVEL_MAX 6
// A8 textures when sampled either have their component in R or A. D3D uses A and that is the default.
#define A8_SAMPLE_MASK .a
#if PS4_PROFILE
#define FEATURE_LEVEL FEATURE_LEVEL_SM5
#elif SM5_PROFILE
// SM5 = full dx11 features (high end UE4 rendering)
#define FEATURE_LEVEL FEATURE_LEVEL_SM5
#elif SM4_PROFILE
#define FEATURE_LEVEL FEATURE_LEVEL_SM4
#elif SWITCH_PROFILE || SWITCH_PROFILE_FORWARD
#undef ES3_1_PROFILE
#if SWITCH_PROFILE
#define FEATURE_LEVEL FEATURE_LEVEL_SM5
#else
#define FEATURE_LEVEL FEATURE_LEVEL_ES3_1
// @todo switch: maybe all uses of this should check feature level not profile?
#define ES3_1_PROFILE 1
#endif
// A8 textures when sampled have their component in R.
#undef A8_SAMPLE_MASK
#define A8_SAMPLE_MASK .r
// switch doesn't have vertex attribute swizzling
#define FCOLOR_COMPONENT_SWIZZLE .bgra
#elif VULKAN_PROFILE
#define FEATURE_LEVEL FEATURE_LEVEL_ES3_1
#elif VULKAN_PROFILE_SM4
#define FEATURE_LEVEL FEATURE_LEVEL_SM4
#elif VULKAN_PROFILE_SM5
#define FEATURE_LEVEL FEATURE_LEVEL_SM5
#elif GL3_PROFILE || GL4_PROFILE
#if GL3_PROFILE
#define FEATURE_LEVEL FEATURE_LEVEL_SM4
#elif GL4_PROFILE
#define FEATURE_LEVEL FEATURE_LEVEL_SM5
#endif
// A8 textures when sampled have their component in R.
#undef A8_SAMPLE_MASK
#define A8_SAMPLE_MASK .r
// hacks until the shader compiler supports those
#if GL4_PROFILE
#define class struct
#endif
#elif METAL_PROFILE
#define FEATURE_LEVEL FEATURE_LEVEL_ES3_1
// @todo metal: remove this and make sure all uses handle METAL_PROFILE
#undef ES3_1_PROFILE
#define ES3_1_PROFILE 1
#define FCOLOR_COMPONENT_SWIZZLE .bgra
#define STENCIL_COMPONENT_SWIZZLE .x
#elif METAL_ES2_PROFILE
#define FEATURE_LEVEL FEATURE_LEVEL_ES2
// @todo metal: remove this and make sure all uses handle METAL_ES2_PROFILE
#undef ES2_PROFILE
#define ES2_PROFILE 1
#define FCOLOR_COMPONENT_SWIZZLE .bgra
#define STENCIL_COMPONENT_SWIZZLE .x
#elif METAL_MRT_PROFILE
#define FEATURE_LEVEL FEATURE_LEVEL_SM4
#define FCOLOR_COMPONENT_SWIZZLE .bgra
#define STENCIL_COMPONENT_SWIZZLE .x
#elif METAL_SM4_PROFILE
#define FEATURE_LEVEL FEATURE_LEVEL_SM4
#define FCOLOR_COMPONENT_SWIZZLE .bgra
#define STENCIL_COMPONENT_SWIZZLE .x
#elif METAL_SM5_PROFILE
#define FEATURE_LEVEL FEATURE_LEVEL_SM5
#define FCOLOR_COMPONENT_SWIZZLE .bgra
#define STENCIL_COMPONENT_SWIZZLE .x
#elif ES2_PROFILE || ES3_1_PROFILE
#if ES3_1_PROFILE
#define FEATURE_LEVEL FEATURE_LEVEL_ES3_1
#else
//@todo ES3_1 GL
#define FEATURE_LEVEL FEATURE_LEVEL_ES2
#endif
#if COMPILER_GLSL_ES2 || COMPILER_GLSL_ES3_1
// Swizzle as we only support GL_BGRA on non-ES2 platforms that have that extension
#define FCOLOR_COMPONENT_SWIZZLE .bgra
#else
#define FCOLOR_COMPONENT_SWIZZLE
#if COMPILER_GLSL
// A8 textures when sampled have their component in R
#undef A8_SAMPLE_MASK
#define A8_SAMPLE_MASK .r
#endif
#endif
#else
#error Add your platform here
#define FEATURE_LEVEL FEATURE_LEVEL_MAX
#endif
#define METAL_TESSELLATION_PROFILE ((METAL_PROFILE || METAL_SM5_PROFILE) && USING_TESSELLATION)
// non-editor platforms generally never want development/editor features.
#define PLATFORM_SUPPORTS_DEVELOPMENT_SHADERS (!PS4_PROFILE && !XBOXONE_PROFILE && !ESDEFERRED_PROFILE && !SWITCH_PROFILE)
#define USE_DEVELOPMENT_SHADERS (COMPILE_SHADERS_FOR_DEVELOPMENT && PLATFORM_SUPPORTS_DEVELOPMENT_SHADERS)
#ifndef MOBILE_EMULATION
#define MOBILE_EMULATION ((FEATURE_LEVEL == FEATURE_LEVEL_ES2 || FEATURE_LEVEL == FEATURE_LEVEL_ES3_1) && (!(COMPILER_GLSL_ES3_1 || COMPILER_GLSL_ES2) && USE_DEVELOPMENT_SHADERS && (!(METAL_PROFILE) || MAC) && !VULKAN_PROFILE && !SWITCH_PROFILE))
#endif
#ifndef MOBILE_FORCE_DEPTH_TEXTURE_READS
#define MOBILE_FORCE_DEPTH_TEXTURE_READS 0
#endif
// If we didn't request color component swizzling, just make it empty
#ifndef FCOLOR_COMPONENT_SWIZZLE
#define FCOLOR_COMPONENT_SWIZZLE
#endif
#ifndef STENCIL_COMPONENT_SWIZZLE
#define STENCIL_COMPONENT_SWIZZLE .g
#endif
// Whether the platform supports independent texture and samplers
// When enabled, different texture lookups can share samplers to allow more artist samplers in the base pass
// Ideally this would just be enabled for all SM4 and above feature level platforms
// @todo metal mrt: No reason this can't work with Metal, once cross compiler is fixed
#define SUPPORTS_INDEPENDENT_SAMPLERS (PS4_PROFILE || SM5_PROFILE || SM4_PROFILE || METAL_MRT_PROFILE || METAL_SM4_PROFILE || METAL_SM5_PROFILE)
// Whether the platform supports a global clip plane through SV_ClipDistance
// Ideally this would just be enabled for all SM4 and above feature level platforms, but not tested everywhere yet
#define PLATFORM_SUPPORTS_GLOBAL_CLIP_PLANE (PS4_PROFILE || SM5_PROFILE || SM4_PROFILE || METAL_PROFILE || METAL_MRT_PROFILE || METAL_SM4_PROFILE || METAL_SM5_PROFILE || GL4_PROFILE || GL3_PROFILE || VULKAN_PROFILE_SM4 || VULKAN_PROFILE_SM5)
// Using SV_ClipDistance has overhead (15% slower base pass in triangle bound test scene on PS4) so projects have to opt-in
#define USE_GLOBAL_CLIP_PLANE (PLATFORM_SUPPORTS_GLOBAL_CLIP_PLANE && PROJECT_ALLOW_GLOBAL_CLIP_PLANE)
// Whether the platform support pixel coverage on MSAA targets (SV_Coverage).
#define SUPPORTS_PIXEL_COVERAGE (FEATURE_LEVEL >= FEATURE_LEVEL_SM5 && !COMPILER_GLSL && !MOBILE_EMULATION)
// This would need to be a #define in GLSL to ignore the SamplerState, however, it is currently a function call in HLSL
// for type checking of the parameters - ironically the type checking is really only needed in GLSL!
MaterialFloat4 Texture1DSample(Texture1D Tex, SamplerState Sampler, float UV)
{
#if COMPUTESHADER
return Tex.SampleLevel(Sampler, UV, 0);
#else
return Tex.Sample(Sampler, UV);
#endif
}
MaterialFloat4 Texture2DSample(Texture2D Tex, SamplerState Sampler, float2 UV)
{
#if COMPUTESHADER
return Tex.SampleLevel(Sampler, UV, 0);
#else
return Tex.Sample(Sampler, UV);
#endif
}
MaterialFloat Texture2DSample_A8(Texture2D Tex, SamplerState Sampler, float2 UV)
{
#if COMPUTESHADER
return Tex.SampleLevel(Sampler, UV, 0) A8_SAMPLE_MASK;
#else
return Tex.Sample(Sampler, UV) A8_SAMPLE_MASK;
#endif
}
MaterialFloat4 Texture3DSample(Texture3D Tex, SamplerState Sampler, float3 UV)
{
#if COMPUTESHADER
return Tex.SampleLevel(Sampler, UV, 0);
#else
return Tex.Sample(Sampler, UV);
#endif
}
MaterialFloat4 TextureCubeSample(TextureCube Tex, SamplerState Sampler, float3 UV)
{
#if COMPUTESHADER
return Tex.SampleLevel(Sampler, UV, 0);
#else
return Tex.Sample(Sampler, UV);
#endif
}
MaterialFloat4 Texture1DSampleLevel(Texture1D Tex, SamplerState Sampler, float UV, MaterialFloat Mip)
{
return Tex.SampleLevel(Sampler, UV, Mip);
}
MaterialFloat4 Texture2DSampleLevel(Texture2D Tex, SamplerState Sampler, float2 UV, MaterialFloat Mip)
{
return Tex.SampleLevel(Sampler, UV, Mip);
}
MaterialFloat4 Texture2DSampleBias(Texture2D Tex, SamplerState Sampler, float2 UV, MaterialFloat MipBias)
{
#if COMPUTESHADER
return Tex.SampleLevel(Sampler, UV, 0);
#else
return Tex.SampleBias(Sampler, UV, MipBias);
#endif
}
MaterialFloat4 Texture2DSampleGrad(Texture2D Tex, SamplerState Sampler, float2 UV, MaterialFloat2 DDX, MaterialFloat2 DDY)
{
return Tex.SampleGrad(Sampler, UV, DDX, DDY);
}
MaterialFloat4 Texture3DSampleLevel(Texture3D Tex, SamplerState Sampler, float3 UV, MaterialFloat Mip)
{
return Tex.SampleLevel(Sampler, UV, Mip);
}
MaterialFloat4 TextureCubeSampleLevel(TextureCube Tex, SamplerState Sampler, float3 UV, MaterialFloat Mip)
{
return Tex.SampleLevel(Sampler, UV, Mip);
}
MaterialFloat4 TextureCubeSampleBias(TextureCube Tex, SamplerState Sampler, float3 UV, MaterialFloat MipBias)
{
#if COMPUTESHADER
return Tex.SampleLevel(Sampler, UV, 0);
#else
return Tex.SampleBias(Sampler, UV, MipBias);
#endif
}
MaterialFloat4 TextureCubeSampleGrad(TextureCube Tex, SamplerState Sampler, float3 UV, MaterialFloat3 DDX, MaterialFloat3 DDY)
{
return Tex.SampleGrad(Sampler, UV, DDX, DDY);
}
//converts an input 1d to 2d position. Useful for locating z frames that have been laid out in a 2d grid like a flipbook.
float2 Tile1Dto2D(float xsize, float idx)
{
float2 xyidx = 0;
xyidx.y = floor(idx / xsize);
xyidx.x = idx - xsize * xyidx.y;
return xyidx;
}
// return a pseudovolume texture sample.
// useful for simulating 3D texturing with a 2D texture or as a texture flipbook with lerped transitions
// treats 2d layout of frames a 3d texture and performs bilinear filtering by blending with an offset Z frame.
// @param Tex = Input Texture Object storing Volume Data
// @param inPos = Input float3 for Position, 0-1
// @param xsize = Input float for num frames in x,y directions
// @param numFrames = Input float for num total frames
// @param mipmode = Sampling mode: 0 = use miplevel, 1 = use UV computed gradients, 2 = Use gradients (default=0)
// @param miplevel = MIP level to use in mipmode=0 (default 0)
// @param InDDX, InDDY = Texture gradients in mipmode=2
float4 PseudoVolumeTexture(Texture2D Tex, SamplerState TexSampler, float3 inPos, float xsize, float numframes,
uint mipmode = 0, float miplevel = 0, float2 InDDX = 0, float2 InDDY = 0)
{
float zframe = ceil(inPos.z * numframes);
float zphase = frac(inPos.z * numframes);
float2 uv = frac(inPos.xy) / xsize;
float2 curframe = Tile1Dto2D(xsize, zframe) / xsize;
float2 nextframe = Tile1Dto2D(xsize, zframe + 1) / xsize;
float4 sampleA = 0, sampleB = 0;
switch (mipmode)
{
case 0: // Mip level
sampleA = Tex.SampleLevel(TexSampler, uv + curframe, miplevel);
sampleB = Tex.SampleLevel(TexSampler, uv + nextframe, miplevel);
break;
case 1: // Gradients automatic from UV
sampleA = Texture2DSample(Tex, TexSampler, uv + curframe);
sampleB = Texture2DSample(Tex, TexSampler, uv + nextframe);
break;
case 2: // Deriviatives provided
sampleA = Tex.SampleGrad(TexSampler, uv + curframe, InDDX, InDDY);
sampleB = Tex.SampleGrad(TexSampler, uv + nextframe, InDDX, InDDY);
break;
default:
break;
}
return lerp(sampleA, sampleB, zphase);
}
#if FEATURE_LEVEL >= FEATURE_LEVEL_SM5 // Cubemap arrays are not supported in SM4 feature level
MaterialFloat4 TextureCubeArraySampleLevel(TextureCubeArray Tex, SamplerState Sampler, float3 UV, float ArrayIndex, MaterialFloat Mip)
{
return Tex.SampleLevel(Sampler, float4(UV, ArrayIndex), Mip);
}
#endif
void Bicubic2DCatmullRom( in float2 UV, in float2 Size, out float2 Sample[3], out float2 Weight[3] )
{
const float2 InvSize = 1.0 / Size;
UV *= Size;
float2 tc = floor( UV - 0.5 ) + 0.5;
float2 f = UV - tc;
float2 f2 = f * f;
float2 f3 = f2 * f;
float2 w0 = f2 - 0.5 * (f3 + f);
float2 w1 = 1.5 * f3 - 2.5 * f2 + 1;
float2 w3 = 0.5 * (f3 - f2);
float2 w2 = 1 - w0 - w1 - w3;
Weight[0] = w0;
Weight[1] = w1 + w2;
Weight[2] = w3;
Sample[0] = tc - 1;
Sample[1] = tc + w2 / Weight[1];
Sample[2] = tc + 2;
Sample[0] *= InvSize;
Sample[1] *= InvSize;
Sample[2] *= InvSize;
}
MaterialFloat4 Texture2DSampleBicubic( Texture2D Tex, SamplerState Sampler, float2 UV, float2 Size )
{
float2 Weight[3];
float2 Sample[3];
Bicubic2DCatmullRom( UV, Size, Sample, Weight );
MaterialFloat4 OutColor;
#if 0
OutColor = Tex.SampleLevel( Sampler, float2( Sample[0].x, Sample[0].y ), 0 ) * Weight[0].x * Weight[0].y;
OutColor += Tex.SampleLevel( Sampler, float2( Sample[1].x, Sample[0].y ), 0 ) * Weight[1].x * Weight[0].y;
OutColor += Tex.SampleLevel( Sampler, float2( Sample[2].x, Sample[0].y ), 0 ) * Weight[2].x * Weight[0].y;
OutColor += Tex.SampleLevel( Sampler, float2( Sample[0].x, Sample[1].y ), 0 ) * Weight[0].x * Weight[1].y;
OutColor += Tex.SampleLevel( Sampler, float2( Sample[1].x, Sample[1].y ), 0 ) * Weight[1].x * Weight[1].y;
OutColor += Tex.SampleLevel( Sampler, float2( Sample[2].x, Sample[1].y ), 0 ) * Weight[2].x * Weight[1].y;
OutColor += Tex.SampleLevel( Sampler, float2( Sample[0].x, Sample[2].y ), 0 ) * Weight[0].x * Weight[2].y;
OutColor += Tex.SampleLevel( Sampler, float2( Sample[1].x, Sample[2].y ), 0 ) * Weight[1].x * Weight[2].y;
OutColor += Tex.SampleLevel( Sampler, float2( Sample[2].x, Sample[2].y ), 0 ) * Weight[2].x * Weight[2].y;
#else
// Optimized by removing corner samples
OutColor = Tex.SampleLevel( Sampler, float2( Sample[1].x, Sample[0].y ), 0 ) * Weight[1].x * Weight[0].y;
OutColor += Tex.SampleLevel( Sampler, float2( Sample[0].x, Sample[1].y ), 0 ) * Weight[0].x * Weight[1].y;
OutColor += Tex.SampleLevel( Sampler, float2( Sample[1].x, Sample[1].y ), 0 ) * Weight[1].x * Weight[1].y;
OutColor += Tex.SampleLevel( Sampler, float2( Sample[2].x, Sample[1].y ), 0 ) * Weight[2].x * Weight[1].y;
OutColor += Tex.SampleLevel( Sampler, float2( Sample[1].x, Sample[2].y ), 0 ) * Weight[1].x * Weight[2].y;
// Reweight after removing the corners
float CornerWeights;
CornerWeights = Weight[1].x * Weight[0].y;
CornerWeights += Weight[0].x * Weight[1].y;
CornerWeights += Weight[1].x * Weight[1].y;
CornerWeights += Weight[2].x * Weight[1].y;
CornerWeights += Weight[1].x * Weight[2].y;
OutColor /= CornerWeights;
#endif
return OutColor;
}
// TANGENTTOWORLD0 is the first row of the tangent to world matrix, w might be needed for padding and is not used yet.
// TANGENTTOWORLD2 is the last row of the tangent to world matrix, determinant of tangent basis in w
#if FEATURE_LEVEL >= FEATURE_LEVEL_SM5
#define TANGENTTOWORLD0 TEXCOORD10
#define TANGENTTOWORLD2 TEXCOORD11
// _centroid is needed to get better quality with MSAA
// The D3D shader compiler combines _centroid and non controid. Using float3 would results in a internal
// shader compiler error. This block is using float4 to prevent that.
#define TANGENTTOWORLD_INTERPOLATOR_BLOCK float4 TangentToWorld0 : TEXCOORD10_centroid; float4 TangentToWorld2 : TEXCOORD11_centroid;
#else
#define TANGENTTOWORLD0 TEXCOORD10
#define TANGENTTOWORLD2 TEXCOORD11
// TangentToWorld0 is float4 only to match D3D11
#if METAL_PROFILE || COMPILER_GLSL_ES3_1
//@todo-rco: FIXME!
#define TANGENTTOWORLD_INTERPOLATOR_BLOCK float4 TangentToWorld0 : TANGENTTOWORLD0; float4 TangentToWorld2 : TANGENTTOWORLD2;
#else
#define TANGENTTOWORLD_INTERPOLATOR_BLOCK MaterialFloat4 TangentToWorld0 : TANGENTTOWORLD0; MaterialFloat4 TangentToWorld2 : TANGENTTOWORLD2;
#endif
#endif
MaterialFloat Luminance( MaterialFloat3 LinearColor )
{
return dot( LinearColor, MaterialFloat3( 0.3, 0.59, 0.11 ) );
}
const static MaterialFloat PI = 3.1415926535897932f;
MaterialFloat length2(MaterialFloat2 v)
{
return dot(v, v);
}
MaterialFloat length2(MaterialFloat3 v)
{
return dot(v, v);
}
MaterialFloat length2(MaterialFloat4 v)
{
return dot(v, v);
}
uint Mod(uint a, uint b)
{
#if FEATURE_LEVEL >= FEATURE_LEVEL_ES3_1
return a % b;
#else
return a - (b * (uint)((float)a / (float)b));
#endif
}
uint2 Mod(uint2 a, uint2 b)
{
#if FEATURE_LEVEL >= FEATURE_LEVEL_ES3_1
return a % b;
#else
return a - (b * (uint2)((float2)a / (float2)b));
#endif
}
uint3 Mod(uint3 a, uint3 b)
{
#if FEATURE_LEVEL >= FEATURE_LEVEL_ES3_1
return a % b;
#else
return a - (b * (uint3)((float3)a / (float3)b));
#endif
}
MaterialFloat UnClampedPow(MaterialFloat X, MaterialFloat Y)
{
return pow(X, Y);
}
MaterialFloat2 UnClampedPow(MaterialFloat2 X, MaterialFloat2 Y)
{
return pow(X, Y);
}
MaterialFloat3 UnClampedPow(MaterialFloat3 X, MaterialFloat3 Y)
{
return pow(X, Y);
}
MaterialFloat4 UnClampedPow(MaterialFloat4 X, MaterialFloat4 Y)
{
return pow(X, Y);
}
// Clamp the base, so it's never <= 0.0f (INF/NaN).
MaterialFloat ClampedPow(MaterialFloat X,MaterialFloat Y)
{
return pow(max(abs(X),0.000001f),Y);
}
MaterialFloat2 ClampedPow(MaterialFloat2 X,MaterialFloat2 Y)
{
return pow(max(abs(X),MaterialFloat2(0.000001f,0.000001f)),Y);
}
MaterialFloat3 ClampedPow(MaterialFloat3 X,MaterialFloat3 Y)
{
return pow(max(abs(X),MaterialFloat3(0.000001f,0.000001f,0.000001f)),Y);
}
MaterialFloat4 ClampedPow(MaterialFloat4 X,MaterialFloat4 Y)
{
return pow(max(abs(X),MaterialFloat4(0.000001f,0.000001f,0.000001f,0.000001f)),Y);
}
MaterialFloat PositiveClampedPow(MaterialFloat X,MaterialFloat Y)
{
return pow(max(X,0.0f),Y);
}
MaterialFloat2 PositiveClampedPow(MaterialFloat2 X,MaterialFloat2 Y)
{
return pow(max(X,MaterialFloat2(0.0f,0.0f)),Y);
}
MaterialFloat3 PositiveClampedPow(MaterialFloat3 X,MaterialFloat3 Y)
{
return pow(max(X,MaterialFloat3(0.0f,0.0f,0.0f)),Y);
}
MaterialFloat4 PositiveClampedPow(MaterialFloat4 X,MaterialFloat4 Y)
{
return pow(max(X,MaterialFloat4(0.0f,0.0f,0.0f,0.0f)),Y);
}
float DDX(float Input)
{
#if COMPUTESHADER
return 0;
#else
return ddx(Input);
#endif
}
float2 DDX(float2 Input)
{
#if COMPUTESHADER
return 0;
#else
return ddx(Input);
#endif
}
float3 DDX(float3 Input)
{
#if COMPUTESHADER
return 0;
#else
return ddx(Input);
#endif
}
float4 DDX(float4 Input)
{
#if COMPUTESHADER
return 0;
#else
return ddx(Input);
#endif
}
float DDY(float Input)
{
#if COMPUTESHADER
return 0;
#else
return ddy(Input);
#endif
}
float2 DDY(float2 Input)
{
#if COMPUTESHADER
return 0;
#else
return ddy(Input);
#endif
}
float3 DDY(float3 Input)
{
#if COMPUTESHADER
return 0;
#else
return ddy(Input);
#endif
}
float4 DDY(float4 Input)
{
#if COMPUTESHADER
return 0;
#else
return ddy(Input);
#endif
}
#include "FastMath.usf"
#include "Random.usf" // used by MaterialExpressionNoise
/**
* Use this function to compute the pow() in the specular computation.
* This allows to change the implementation depending on platform or it easily can be replaced by some approxmation.
*/
MaterialFloat PhongShadingPow(MaterialFloat X, MaterialFloat Y)
{
// The following clamping is done to prevent NaN being the result of the specular power computation.
// Clamping has a minor performance cost.
// In HLSL pow(a, b) is implemented as exp2(log2(a) * b).
// For a=0 this becomes exp2(-inf * 0) = exp2(NaN) = NaN.
// As seen in #TTP 160394 "QA Regression: PS3: Some maps have black pixelated artifacting."
// this can cause severe image artifacts (problem was caused by specular power of 0, lightshafts propagated this to other pixels).
// The problem appeared on PlayStation 3 but can also happen on similar PC NVidia hardware.
// In order to avoid platform differences and rarely occuring image atrifacts we clamp the base.
// Note: Clamping the exponent seemed to fix the issue mentioned TTP but we decided to fix the root and accept the
// minor performance cost.
return ClampedPow(X, Y);
}
#if FEATURE_LEVEL < FEATURE_LEVEL_ES3_1 && !COMPILER_METAL
// DX11 (feature levels >= 10) feature sets natively supports uints in shaders; we just use floats on other platforms.
#define uint4 int4
#endif
// Optional VertexID - used by tessellation to uniquely identify control points.
#if USING_TESSELLATION && DISPLACEMENT_ANTICRACK
#define OPTIONAL_VertexID uint VertexID : SV_VertexID,
#define OPTIONAL_VertexID_PARAM VertexID,
#define OPTIONAL_VertexID_VS_To_DS uint VertexID : VS_To_DS_VertexID;
#define OutputVertexID( Out ) Out.VertexID = VertexID
#else // #if USING_TESSELLATION && DISPLACEMENT_ANTICRACK
#define OPTIONAL_VertexID
#define OPTIONAL_VertexID_PARAM
#define OPTIONAL_VertexID_VS_To_DS
#define OutputVertexID( Out )
#endif // #if USING_TESSELLATION && DISPLACEMENT_ANTICRACK
// Helper macro used to interpolate the given member
#define TESSELLATION_INTERPOLATE_MEMBER(member) O.member = a.member * aInterp + b.member * bInterp
#if FEATURE_LEVEL >= FEATURE_LEVEL_SM4
/**
* Number of MSAA samples supported by deferred passes in D3D11.
* This is hardcoded because it allows deferred passes to optimize for the given value (for example, unrolling a loop).
*/
#define NumMSAASamples 4
#endif
// depth in the red channel in DeviceZ
Texture2D SceneDepthTexture;
SamplerState SceneDepthTextureSampler;
Texture2D CustomDepthTexture;
SamplerState CustomDepthTextureSampler;
// Custom Stencil texture used for mobile platforms
Texture2D MobileCustomStencilTexture;
SamplerState MobileCustomStencilTextureSampler;
// scene HDR color
Texture2D SceneColorTexture;
SamplerState SceneColorTextureSampler;
// copy of scene alpha for PC ES2 emulation
Texture2D SceneAlphaCopyTexture;
SamplerState SceneAlphaCopyTextureSampler;
// shadow and light function
Texture2D LightAttenuationTexture;
SamplerState LightAttenuationTextureSampler;
#if FEATURE_LEVEL >= FEATURE_LEVEL_SM4
// Scene depth-stencil buffer used for stencil reads
Texture2D<uint2> SceneStencilTexture;
#endif
// We don't use an inline function so we can avoid type promotion/ coercion.
#define RETURN_COLOR( Color ) ( Color )
// Tangent space bias
// We don't use a function so we can avoid type promotion/ coercion.
#define TangentBias(X) (X * 2.0f - 1.0f)
float Square( float x )
{
return x*x;
}
float2 Square( float2 x )
{
return x*x;
}
float3 Square( float3 x )
{
return x*x;
}
float4 Square( float4 x )
{
return x*x;
}
float Pow2( float x )
{
return x*x;
}
float2 Pow2( float2 x )
{
return x*x;
}
float3 Pow2( float3 x )
{
return x*x;
}
float4 Pow2( float4 x )
{
return x*x;
}
float Pow3( float x )
{
return x*x*x;
}
float2 Pow3( float2 x )
{
return x*x*x;
}
float3 Pow3( float3 x )
{
return x*x*x;
}
float4 Pow3( float4 x )
{
return x*x*x;
}
float Pow4( float x )
{
float xx = x*x;
return xx * xx;
}
float2 Pow4( float2 x )
{
float2 xx = x*x;
return xx * xx;
}
float3 Pow4( float3 x )
{
float3 xx = x*x;
return xx * xx;
}
float4 Pow4( float4 x )
{
float4 xx = x*x;
return xx * xx;
}
float Pow5( float x )
{
float xx = x*x;
return xx * xx * x;
}
float2 Pow5( float2 x )
{
float2 xx = x*x;
return xx * xx * x;
}
float3 Pow5( float3 x )
{
float3 xx = x*x;
return xx * xx * x;
}
float4 Pow5( float4 x )
{
float4 xx = x*x;
return xx * xx * x;
}
float Pow6( float x )
{
float xx = x*x;
return xx * xx * xx;
}
float2 Pow6( float2 x )
{
float2 xx = x*x;
return xx * xx * xx;
}
float3 Pow6( float3 x )
{
float3 xx = x*x;
return xx * xx * xx;
}
float4 Pow6( float4 x )
{
float4 xx = x*x;
return xx * xx * xx;
}
// Only valid for x >= 0
MaterialFloat AtanFast( MaterialFloat x )
{
// Minimax 3 approximation
MaterialFloat3 A = x < 1 ? MaterialFloat3( x, 0, 1 ) : MaterialFloat3( 1/x, 0.5 * PI, -1 );
return A.y + A.z * ( ( ( -0.130234 * A.x - 0.0954105 ) * A.x + 1.00712 ) * A.x - 0.00001203333 );
}
/**
* Returns the upper 3x3 portion of the LocalToWorld matrix.
*/
MaterialFloat3x3 GetLocalToWorld3x3()
{
return (MaterialFloat3x3)Primitive.LocalToWorld;
}
/** Converts a linear input value into a value to be stored in the light attenuation buffer. */
MaterialFloat EncodeLightAttenuation(MaterialFloat InColor)
{
// Apply a 1/2 power to the input, which allocates more bits for the darks and prevents banding
// Similar to storing colors in gamma space, except this uses less instructions than a pow(x, 1/2.2)
return sqrt(InColor);
}
/** Converts a linear input value into a value to be stored in the light attenuation buffer. */
MaterialFloat4 EncodeLightAttenuation(MaterialFloat4 InColor)
{
return sqrt(InColor);
}
/** return the scene lighting texture */
MaterialFloat3 CalcSceneColor(MaterialFloat2 ScreenUV)
{
#if SCENE_TEXTURES_DISABLED
return MaterialFloat3(0.0f,0.0f,0.0f);
#else
return Texture2DSampleLevel(SceneColorTexture, SceneColorTextureSampler, ScreenUV, 0).rgb;
#endif
}
/** return all channels of the scene lighting texture */
MaterialFloat4 CalcFullSceneColor(MaterialFloat2 ScreenUV)
{
#if SCENE_TEXTURES_DISABLED
return MaterialFloat4(0.0f, 0.0f, 0.0f, 0.0f);
#else
return Texture2DSample(SceneColorTexture, SceneColorTextureSampler,ScreenUV);
#endif
}
/** Encodes HDR linear scene color for storage in the 8 bit light attenuation texture. */
MaterialFloat3 EncodeSceneColorForMaterialNode(MaterialFloat3 LinearSceneColor)
{
// Preserving a range from [0, 10]
// Remap values to get more bits of precision in the darks
return pow(LinearSceneColor * .1f, .25f);
}
//
// MOBILE WITHOUT FP16 SUPPORT: 8-BIT/CHANNEL MOSAICING FOR LINEAR HDR
//
// This provides linear blending and a {0 to 2} dynamic range.
// This works by splitting the image into a checkerboard of dark and light pixels.
// DLDL
// LDLD
// DLDL
// LDLD
//
// There is an extra scan line dither pattern to increase precision by 1-bit.
// The HdrMosaic() function computes the exposure level per pixel for the forward render pass.
// The HdrDemosaic() function reconstructs the proper image in the tonemapping pass.
//
// These have been carefully tuned and should probably not be adjusted.
// Dark pixel range is {0 to 1/DRK_MUL}.
#define DRK_MUL 6.0
// Support {0 to 2} in dynamic range.
#define HDR_MUL (1.0/2.0)
#define MOSAIC_ADD HDR_MUL
// The blend factor causes a smooth blend between the light and dark pixels.
#define MOSAIC_BLEND ((255.0-(DRK_MUL/HDR_MUL))/255.0)
#define MOSAIC_MUL ((DRK_MUL * MOSAIC_BLEND - MOSAIC_ADD) * 2.0)
// Done during forward shading pass before blending.
MaterialFloat3 HdrMosaic(MaterialFloat3 LinearColor, float2 VPos)
{
float2 V;
V = VPos.xy * 0.5;
V.y += V.x;
V = frac(V);
MaterialFloat2 C;
C = (V * MaterialFloat2(2.0 * (-0.5/255.0), MOSAIC_MUL)) + MaterialFloat2(-0.5/255.0, MOSAIC_ADD);
return (LinearColor * C.y) + C.x;
}
#define DEMOSAIC_MUL (((1.0/DRK_MUL) - (1.0/HDR_MUL)) * 2.0)
#define DEMOSAIC_ADD (1.0/HDR_MUL)
// Resolve pass to remove mosaic and restore color.
MaterialFloat3 HdrDemosaic(MaterialFloat3 Pixel, MaterialFloat3 OtherPixel, float2 VPos)
{
MaterialFloat A = frac(dot(VPos + View.DemosaicVposOffset, float2(0.5, 0.5)));
MaterialFloat B = 0.5 - A;
A = A * DEMOSAIC_MUL + DEMOSAIC_ADD;
B = B * DEMOSAIC_MUL + DEMOSAIC_ADD;
// On ES2 devices we demosaic during the tonemapping pass which renders upside down, account for that here.
#if COMPILER_GLSL_ES2 || COMPILER_GLSL_ES3_1
return max((Pixel * B), (OtherPixel * A));
#else
return max((Pixel * A), (OtherPixel * B));
#endif
}
// Like RGBM but this can be interpolated.
MaterialFloat4 RGBTEncode(MaterialFloat3 Color)
{
MaterialFloat4 RGBT;
MaterialFloat Max = max(max(Color.r, Color.g), max(Color.b, 1e-6));
MaterialFloat RcpMax = rcp(Max);
RGBT.rgb = Color.rgb * RcpMax;
RGBT.a = Max * rcp(1.0 + Max);
return RGBT;
}
MaterialFloat3 RGBTDecode(MaterialFloat4 RGBT)
{
RGBT.a = RGBT.a * rcp(1.0 - RGBT.a);
return RGBT.rgb * RGBT.a;
}
MaterialFloat4 RGBMEncode( MaterialFloat3 Color )
{
Color *= 1.0 / 64.0;
float4 rgbm;
rgbm.a = saturate( max( max( Color.r, Color.g ), max( Color.b, 1e-6 ) ) );
rgbm.a = ceil( rgbm.a * 255.0 ) / 255.0;
rgbm.rgb = Color / rgbm.a;
return rgbm;
}
MaterialFloat4 RGBMEncodeFast( MaterialFloat3 Color )
{
// 0/0 result written to fixed point buffer goes to zero
MaterialFloat4 rgbm;
rgbm.a = dot( Color, 255.0 / 64.0 );
rgbm.a = ceil( rgbm.a );
rgbm.rgb = Color / rgbm.a;
rgbm *= MaterialFloat4( 255.0 / 64.0, 255.0 / 64.0, 255.0 / 64.0, 1.0 / 255.0 );
return rgbm;
}
MaterialFloat3 RGBMDecode( MaterialFloat4 rgbm, MaterialFloat MaxValue )
{
return rgbm.rgb * (rgbm.a * MaxValue);
}
MaterialFloat3 RGBMDecode( MaterialFloat4 rgbm )
{
return rgbm.rgb * (rgbm.a * 64.0f);
}
MaterialFloat4 RGBTEncode8BPC(MaterialFloat3 Color, MaterialFloat Range)
{
MaterialFloat Max = max(max(Color.r, Color.g), max(Color.b, 1e-6));
Max = min(Max, Range);
MaterialFloat4 RGBT;
RGBT.a = (Range + 1) / Range * Max / (1 + Max);
// quantise alpha to 8 bit.
RGBT.a = ceil(RGBT.a*255.0) / 255.0;
Max = RGBT.a / (1 + 1 / Range - RGBT.a);
MaterialFloat RcpMax = rcp(Max);
RGBT.rgb = Color.rgb * RcpMax;
return RGBT;
}
MaterialFloat3 RGBTDecode8BPC(MaterialFloat4 RGBT, MaterialFloat Range)
{
RGBT.a = RGBT.a / (1 + 1 / Range - RGBT.a);
return RGBT.rgb * RGBT.a;
}
#define HDR_ENCODE_NONE 0.0 // 64bpp HDR
#define HDR_ENCODE_MOSAIC 1.0 // 32bpp HDR using Mosaic encoding
#define HDR_ENCODE_RGBA 2.0 // 32bpp HDR using RGBA encoding
half GetHDR32bppEncodeMode()
{
#if ES2_PROFILE
#if COMPILER_GLSL_ES2 && !IOS && !WEBGL // ANDROID
return intrinsic_GetHDR32bppEncodeModeES2();
#endif
#if MOBILE_EMULATION
// To enable editor runtime change without recompile, PC always eats the encode cost.
return View.HDR32bppEncodingMode;
#endif
#endif
return HDR_ENCODE_NONE;
}
#define DEFAULT_32BPPHDR_ENCODED_RANGE 1024.0
MaterialFloat4 Encode32BPPHDR(MaterialFloat4 Color, float2 SvPosition)
{
half Mode = GetHDR32bppEncodeMode();
if (Mode == HDR_ENCODE_MOSAIC)
{
return MaterialFloat4(HdrMosaic(Color.rgb, SvPosition), Color.a);
}
else if (Mode == HDR_ENCODE_RGBA)
{
return RGBTEncode8BPC(Color.rgb, DEFAULT_32BPPHDR_ENCODED_RANGE);
}
else
{
return Color;
}
}
MaterialFloat4 Decode32BPPHDR(MaterialFloat4 Encoded, MaterialFloat3 OtherEncoded = MaterialFloat3(0, 0, 0), float2 SvPosition = float2(0, 0))
{
half Mode = GetHDR32bppEncodeMode();
if (Mode == HDR_ENCODE_MOSAIC)
{
return MaterialFloat4(HdrDemosaic(Encoded.rgb, OtherEncoded, SvPosition), 0.0f);
}
if (Mode == HDR_ENCODE_RGBA)
{
return MaterialFloat4(RGBTDecode8BPC(Encoded, DEFAULT_32BPPHDR_ENCODED_RANGE), 0.0f);
}
else
{
return Encoded;
}
}
// Demosaic capable 32bpp hdr decode.
float4 Decode32BPPHDR(float4 Pixel, float2 SvPosition, Texture2D SourceTexture, SamplerState SourceSampler, float2 InTexCoords[4])
{
half3 PixelColorN = SourceTexture.Sample(SourceSampler, InTexCoords[0].xy).rgb;
half3 PixelColorE = SourceTexture.Sample(SourceSampler, InTexCoords[1].xy).rgb;
half3 PixelColorW = SourceTexture.Sample(SourceSampler, InTexCoords[2].xy).rgb;
half3 PixelColorS = SourceTexture.Sample(SourceSampler, InTexCoords[3].xy).rgb;
half3 PixelColorV = PixelColorN * 0.5 + PixelColorS * 0.5;
half3 PixelColorH = PixelColorW * 0.5 + PixelColorE * 0.5;
if(abs(PixelColorN.g - PixelColorS.g) < abs(PixelColorW.g - PixelColorE.g))
{
PixelColorH = PixelColorV;
}
Pixel.rgb = Decode32BPPHDR(Pixel, PixelColorH, SvPosition.xy).rgb;
Pixel.a = 0.0;
return Pixel;
}
/** Get render target write mask value
* This gets a bit from a write mask texture created with FRTWriteMaskDecodeCS. Only supprted on some platforms.
*/
#if PLATFORM_SUPPORTS_RENDERTARGET_WRITE_MASK
uint DecodeRTWriteMaskTexture(in float2 ScreenPosition, in Texture2D<uint> RTWriteMaskTexture)
{
int2 IntPosition = int2(ScreenPosition.xy);
uint RTWriteMaskValue = RTWriteMaskTexture.Load( int3(IntPosition.x/8, IntPosition.y/8, 0) );
int2 BitCoord = ((IntPosition / int2(4, 4)) % int2(2, 2));
uint BitIdx = BitCoord.x + (BitCoord.y*2);
uint RTWriteMaskBit = RTWriteMaskValue & (1<<BitIdx);
return RTWriteMaskBit;
}
#endif
/** Calculates the ScreenUV given the screen position and an offset fraction. */
float2 CalcScreenUVFromOffsetFraction(float4 ScreenPosition, float2 OffsetFraction)
{
float2 NDC = ScreenPosition.xy / ScreenPosition.w;
// Apply the offset in NDC space so that it is consistent regardless of scene color buffer size
// Clamp to valid area of the screen to avoid reading garbage
//@todo - soft clamp
float2 OffsetNDC = clamp(NDC + OffsetFraction * float2(2, -2), -.999f, .999f);
return float2(OffsetNDC * View.ScreenPositionScaleBias.xy + View.ScreenPositionScaleBias.wz);
}
Texture2D SceneColorCopyTexture;
SamplerState SceneColorCopyTextureSampler;
/** Applies an offset to the scene texture lookup and decodes the HDR linear space color. */
float3 DecodeSceneColorForMaterialNode(float2 ScreenUV)
{
#if HIT_PROXY_SHADER || SCENE_TEXTURES_DISABLED
// Hit proxies rendering pass doesn't have access to valid render buffers
return float3(0.0f, 0.0f, 0.0f);
#else
float4 EncodedSceneColor = Texture2DSample(SceneColorCopyTexture, SceneColorCopyTextureSampler, ScreenUV);
// Undo the function in EncodeSceneColorForMaterialNode
return pow(EncodedSceneColor.rgb, 4) * 10;
#endif
}
float4 GetPerPixelLightAttenuation(float2 UV)
{
return Square(Texture2DSampleLevel(LightAttenuationTexture, LightAttenuationTextureSampler, UV, 0));
}
// also see ConvertToDeviceZ()
// @param DeviceZ value that is stored in the depth buffer (Z/W)
// @return SceneDepth (linear in world units, W)
float ConvertFromDeviceZ(float DeviceZ)
{
// Supports ortho and perspective, see CreateInvDeviceZToWorldZTransform()
return DeviceZ * View.InvDeviceZToWorldZTransform[0] + View.InvDeviceZToWorldZTransform[1] + 1.0f / (DeviceZ * View.InvDeviceZToWorldZTransform[2] - View.InvDeviceZToWorldZTransform[3]);
}
// inverse operation of ConvertFromDeviceZ()
// @param SceneDepth (linear in world units, W)
// @return DeviceZ (Z/W)
float ConvertToDeviceZ(float SceneDepth)
{
FLATTEN
if (View.ViewToClip[3][3] < 1.0f)
{
// Perspective
return 1.0f / ((SceneDepth + View.InvDeviceZToWorldZTransform[3]) * View.InvDeviceZToWorldZTransform[2]);
}
else
{
// Ortho
return SceneDepth * View.ViewToClip[2][2] + View.ViewToClip[3][2];
}
}
/** Returns clip space W, which is world space distance along the View Z axis. Note if you need DeviceZ LookupDeviceZ() is the faster option */
float CalcSceneDepth(float2 ScreenUV)
{
#if SCENE_TEXTURES_DISABLED
return 0.0f;
#else
#if FEATURE_LEVEL > FEATURE_LEVEL_ES3_1 || MOBILE_FORCE_DEPTH_TEXTURE_READS
return ConvertFromDeviceZ(Texture2DSampleLevel(SceneDepthTexture, SceneDepthTextureSampler, ScreenUV, 0).r);
#else
#if COMPILER_GLSL_ES2
#if IOS
// Only call FramebufferFetch when actually compiling for IOS ES2.
return FramebufferFetchES2().w;
#elif WEBGL
return Texture2DSampleLevel(SceneAlphaCopyTexture, SceneAlphaCopyTextureSampler, ScreenUV, 0).r;
#else
float SceneW = ConvertFromDeviceZ(Texture2DSampleLevel(SceneDepthTexture, SceneDepthTextureSampler, ScreenUV, 0).r);
return DepthbufferFetchES2(SceneW, View.InvDeviceZToWorldZTransform[2], View.InvDeviceZToWorldZTransform[3]);
#endif
#elif METAL_PROFILE && !MAC
return FramebufferFetchES2().w;
#else
return ConvertFromDeviceZ(Texture2DSampleLevel(SceneDepthTexture, SceneDepthTextureSampler, ScreenUV, 0).r);
#endif
#endif
#endif
}
/** Returns DeviceZ which is the z value stored in the depth buffer. */
float LookupDeviceZ( float2 ScreenUV )
{
#if FEATURE_LEVEL > FEATURE_LEVEL_ES3_1
// native Depth buffer lookup
return Texture2DSampleLevel(SceneDepthTexture, SceneDepthTextureSampler, ScreenUV, 0).r;
#else
#if COMPILER_GLSL_ES2
// todo: can be optimized
return ConvertToDeviceZ(CalcSceneDepth(ScreenUV));
#elif METAL_PROFILE
// todo: can be optimized
return ConvertToDeviceZ(CalcSceneDepth(ScreenUV));
#else
// native Depth buffer lookup
return Texture2DSampleLevel(SceneDepthTexture, SceneDepthTextureSampler, ScreenUV, 0).r;
#endif
#endif
}
#if FEATURE_LEVEL >= FEATURE_LEVEL_SM4 || MOBILE_EMULATION
// depth in in DeviceZ
Texture2D<float> SceneDepthTextureNonMS;
/** Returns clip space W, which is world space distance along the View Z axis. */
float CalcSceneDepth(uint2 PixelPos)
{
#if SCENE_TEXTURES_DISABLED
return 0.0f;
#else
float DeviceZ = SceneDepthTextureNonMS.Load(int3(PixelPos, 0));
// Fetch the depth buffer Z / W value, solve for W
return ConvertFromDeviceZ(DeviceZ);
#endif
}
#endif
/**
* Returns scene color in rgb, depth in a
*/
float4 CalcSceneColorAndDepth( float2 ScreenUV )
{
return float4(CalcSceneColor(ScreenUV), CalcSceneDepth(ScreenUV));
}
#if FEATURE_LEVEL >= FEATURE_LEVEL_SM4
#if FEATURE_LEVEL == FEATURE_LEVEL_SM4
// SM4 requires texture size to be explicitly stated and expressed as the number of samples.
Texture2DMS<float4, NumMSAASamples> SceneColorSurface;
Texture2DMS<float, NumMSAASamples> SceneDepthSurface;
#else
Texture2DMS<float4> SceneColorSurface;
Texture2DMS<float> SceneDepthSurface;
#endif // FEATURE_LEVEL
float CalcSceneDepthMSAA(float2 ScreenUV,uint SampleIndex)
{
int2 IntUV = int2(trunc(ScreenUV * View.BufferSizeAndInvSize.xy));
float DeviceZ = SceneDepthSurface.Load(IntUV,SampleIndex);
return ConvertFromDeviceZ(DeviceZ);
}
#endif
// ----------------------------
float2 ScreenPositionToBufferUV(float4 ScreenPosition)
{
return float2(ScreenPosition.xy / ScreenPosition.w * View.ScreenPositionScaleBias.xy + View.ScreenPositionScaleBias.wz);
}
float2 SvPositionToBufferUV(float4 SvPosition)
{
return SvPosition.xy * View.BufferSizeAndInvSize.zw;
}
// Used for post process shaders which don't need to resolve the view
float3 SvPositionToTranslatedWorld(float4 SvPosition)
{
float4 HomWorldPos = mul(float4(SvPosition.xyz, 1), View.SVPositionToTranslatedWorld);
return HomWorldPos.xyz / HomWorldPos.w;
}
// Used for vertex factory shaders which need to use the resolved view
float3 SvPositionToResolvedTranslatedWorld(float4 SvPosition)
{
float4 HomWorldPos = mul(float4(SvPosition.xyz, 1), ResolvedView.SVPositionToTranslatedWorld);
return HomWorldPos.xyz / HomWorldPos.w;
}
// prefer to use SvPositionToTranslatedWorld() for better quality
float3 SvPositionToWorld(float4 SvPosition)
{
return SvPositionToTranslatedWorld(SvPosition) - View.PreViewTranslation;
}
// investigate: doesn't work for usage with View.ScreenToWorld, see SvPositionToScreenPosition2()
float4 SvPositionToScreenPosition(float4 SvPosition)
{
// todo: is already in .w or needs to be reconstructed like this:
// SvPosition.w = ConvertFromDeviceZ(SvPosition.z);
float2 PixelPos = SvPosition.xy - View.ViewRectMin.xy;
// NDC (NormalizedDeviceCoordinates, after the perspective divide)
float3 NDCPos = float3( (PixelPos * View.ViewSizeAndInvSize.zw - 0.5f) * float2(2, -2), SvPosition.z);
// SvPosition.w: so .w has the SceneDepth, some mobile code and the DepthFade material expression wants that
return float4(NDCPos.xyz, 1) * SvPosition.w;
}
// Used for vertex factory shaders which need to use the resolved view
float4 SvPositionToResolvedScreenPosition(float4 SvPosition)
{
float2 PixelPos = SvPosition.xy - ResolvedView.ViewRectMin.xy;
// NDC (NormalizedDeviceCoordinates, after the perspective divide)
float3 NDCPos = float3( (PixelPos * ResolvedView.ViewSizeAndInvSize.zw - 0.5f) * float2(2, -2), SvPosition.z);
// SvPosition.w: so .w has the SceneDepth, some mobile code and the DepthFade material expression wants that
return float4(NDCPos.xyz, 1) * SvPosition.w;
}
float2 SvPositionToViewportUV(float4 SvPosition)
{
// can be optimized from 2SUB+2MUL to 2MAD
float2 PixelPos = SvPosition.xy - View.ViewRectMin.xy;
return PixelPos.xy * View.ViewSizeAndInvSize.zw;
}
float2 BufferUVToViewportUV(float2 BufferUV)
{
float2 PixelPos = BufferUV.xy * View.BufferSizeAndInvSize.xy - View.ViewRectMin.xy;
return PixelPos.xy * View.ViewSizeAndInvSize.zw;
}
// ----------------------------
/**
* aligns the clip space position so that it can be used as a texture coordinate
* to properly align in screen space
*/
MaterialFloat2 ScreenAlignedPosition( float4 ScreenPosition )
{
return MaterialFloat2(ScreenPositionToBufferUV(ScreenPosition));
}
/**
* Aligns the [0,1] UV to match the view within the backbuffer
*/
MaterialFloat2 ScreenAlignedUV( MaterialFloat2 UV )
{
return (UV*MaterialFloat2(2,-2) + MaterialFloat2(-1,1))*View.ScreenPositionScaleBias.xy + View.ScreenPositionScaleBias.wz;
}
/**
* Compute viewport coordinates from the given fragment coordinates.
*/
MaterialFloat2 GetViewportCoordinates(MaterialFloat2 InFragmentCoordinates)
{
return InFragmentCoordinates;
}
/**
* Unpack a normal stored in a normal map. The X and Y components are rescaled from [0,1] to [-1,1] and Z is reconstructed.
*/
MaterialFloat4 UnpackNormalMap( MaterialFloat4 TextureSample )
{
#if COMPILER_GLSL_ES2 && IOS
return MaterialFloat4(TextureSample.rgb * 2 - 1, 1);
#else
#if DXT5_NORMALMAPS
MaterialFloat2 NormalXY = TextureSample.ag;
#else
MaterialFloat2 NormalXY = TextureSample.rg;
#endif
NormalXY = NormalXY * MaterialFloat2(2.0f,2.0f) - MaterialFloat2(1.0f,1.0f);
MaterialFloat NormalZ = sqrt( saturate( 1.0f - dot( NormalXY, NormalXY ) ) );
return MaterialFloat4( NormalXY.xy, NormalZ, 1.0f );
#endif
}
// Antialiased version of a binary comparison between ThresholdConst and a texture channel.
float AntialiasedTextureMask( Texture2D Tex, SamplerState Sampler, float2 UV, float ThresholdConst, int Channel )
{
// By setting MaskConst to 0001, 0010, 0100 or 1000 individual channels can be chosen (the compiler should be able to optimize that).
MaterialFloat4 MaskConst = MaterialFloat4(Channel == 0, Channel == 1, Channel == 2, Channel == 3);
// border width in pixels, for antialiasing 1 .. 1.5 is good but 1.0 is good for optimizations
const float WidthConst = 1.0f;
float InvWidthConst = 1 / WidthConst;
// Problem:
// A simple texture lookup with a comparison against some thresold value allows to get a mask useful
// for many purposes (e.g. text rendering, signs, oil/water/paint). Antialiased masks look much better
// and mip mapping provides that but only for minification. So when the texture resolution is lower than
// the rendering size results get blurry.
// Idea:
// We compute the distance to the threshold line in pixels (with subpixel precision). We can visualize
// the problem as a heightmap that intersects a axis aligned plane at the threshold height. Only surface
// above the threshold plane contributes to the mask. Looking at one pixel the heightmap can be approximated
// by a plane. We can easily get the plane center value form a texture lookup and get the plane equation from
// ddx and ddy of that value (only one value per 2x2 block) or some other more precise method. We can reduce the
// 3d problem to 2d (looking at the steepest angle only) and the resulting value tells us how much the texture value
// changes for one pixel. This allows us to scale and bias (threshold) the texture value the so it maps to the
// distance function. We rescaling the distance to 0.5 coverage at the line, >1 MaterialFloat a pixel inside and <0 MaterialFloat
// a pixel outside. Clamping this value in the range from 0..1 gives us a good approximation of the pixel coverage.
// We tried multiple possible implementations - this is the cheapest and looks ok is most cases.
// If quality improvements are needed we can add an option to the node later on.
float Result;
{
// optimized, ddx/ddy only for every 2x2 block (bad for distant stuff)
float Sample1 = dot(MaskConst, Texture2DSample(Tex, Sampler, UV));
// compute the derivatives of the texture content
float2 TexDD = float2(DDX(Sample1), DDY(Sample1));
float TexDDLength = max(abs(TexDD.x), abs(TexDD.y));
float Top = InvWidthConst * (Sample1 - ThresholdConst);
Result = Top / TexDDLength + ThresholdConst;
}
Result = saturate(Result); // no always needed (e.g. DX9 framebuffer blending)
return Result;
}
float Noise3D_Multiplexer(int Function, float3 Position, int Quality, bool bTiling, uint RepeatSize)
{
// verified, HLSL compiled out the switch if Function is a constant
switch(Function)
{
case 0:
return SimplexNoise3D_TEX(Position);
case 1:
return GradientNoise3D_TEX(Position, bTiling, RepeatSize);
case 2:
return FastGradientPerlinNoise3D_TEX(Position);
case 3:
return GradientNoise3D_ALU(Position, bTiling, RepeatSize);
case 4:
return ValueNoise3D_ALU(Position, bTiling, RepeatSize);
default:
return VoronoiNoise3D_ALU(Position, Quality, bTiling, RepeatSize, true).w * 2. - 1.;
}
return 0;
}
// @param LevelScale usually 2 but higher values allow efficient use of few levels
// @return in user defined range (OutputMin..OutputMax)
MaterialFloat MaterialExpressionNoise(float3 Position, float Scale, int Quality, int Function, bool bTurbulence, uint Levels, float OutputMin, float OutputMax, float LevelScale, float FilterWidth, bool bTiling, float RepeatSize)
{
Position *= Scale;
FilterWidth *= Scale;
float Out = 0.0f;
float OutScale = 1.0f;
float InvLevelScale = 1.0f / LevelScale;
LOOP for(uint i = 0; i < Levels; ++i)
{
// fade out noise level that are too high frequent (not done through dynamic branching as it usually requires gradient instructions)
OutScale *= saturate(1.0 - FilterWidth);
if(bTurbulence)
{
Out += abs(Noise3D_Multiplexer(Function, Position, Quality, bTiling, RepeatSize)) * OutScale;
}
else
{
Out += Noise3D_Multiplexer(Function, Position, Quality, bTiling, RepeatSize) * OutScale;
}
Position *= LevelScale;
RepeatSize *= LevelScale;
OutScale *= InvLevelScale;
FilterWidth *= LevelScale;
}
if(!bTurbulence)
{
// bring -1..1 to 0..1 range
Out = Out * 0.5f + 0.5f;
}
// Out is in 0..1 range
return lerp(OutputMin, OutputMax, Out);
}
// Material node for noise functions returning a vector value
// @param LevelScale usually 2 but higher values allow efficient use of few levels
// @return in user defined range (OutputMin..OutputMax)
MaterialFloat4 MaterialExpressionVectorNoise(MaterialFloat3 Position, int Quality, int Function, bool bTiling, float TileSize)
{
float4 result = float4(0,0,0,1);
float3x4 Jacobian = JacobianSimplex_ALU(Position, bTiling, TileSize); // compiled out if not used
// verified, HLSL compiled out the switch if Function is a constant
switch (Function)
{
case 0: // Cellnoise
result.xyz = float3(Rand3DPCG16(int3(floor(NoiseTileWrap(Position, bTiling, TileSize))))) / 0xffff;
break;
case 1: // Color noise
result.xyz = float3(Jacobian[0].w, Jacobian[1].w, Jacobian[2].w);
break;
case 2: // Gradient
result = Jacobian[0];
break;
case 3: // Curl
result.xyz = float3(Jacobian[2][1] - Jacobian[1][2], Jacobian[0][2] - Jacobian[2][0], Jacobian[1][0] - Jacobian[0][1]);
break;
default: // Voronoi
result = VoronoiNoise3D_ALU(Position, Quality, bTiling, TileSize, false);
break;
}
return result;
}
/*
* Clips a ray to an AABB. Does not handle rays parallel to any of the planes.
*
* @param RayOrigin - The origin of the ray in world space.
* @param RayEnd - The end of the ray in world space.
* @param BoxMin - The minimum extrema of the box.
* @param BoxMax - The maximum extrema of the box.
* @return - Returns the closest intersection along the ray in x, and furthest in y.
* If the ray did not intersect the box, then the furthest intersection <= the closest intersection.
* The intersections will always be in the range [0,1], which corresponds to [RayOrigin, RayEnd] in worldspace.
* To find the world space position of either intersection, simply plug it back into the ray equation:
* WorldPos = RayOrigin + (RayEnd - RayOrigin) * Intersection;
*/
float2 LineBoxIntersect(float3 RayOrigin, float3 RayEnd, float3 BoxMin, float3 BoxMax)
{
float3 InvRayDir = 1.0f / (RayEnd - RayOrigin);
//find the ray intersection with each of the 3 planes defined by the minimum extrema.
float3 FirstPlaneIntersections = (BoxMin - RayOrigin) * InvRayDir;
//find the ray intersection with each of the 3 planes defined by the maximum extrema.
float3 SecondPlaneIntersections = (BoxMax - RayOrigin) * InvRayDir;
//get the closest of these intersections along the ray
float3 ClosestPlaneIntersections = min(FirstPlaneIntersections, SecondPlaneIntersections);
//get the furthest of these intersections along the ray
float3 FurthestPlaneIntersections = max(FirstPlaneIntersections, SecondPlaneIntersections);
float2 BoxIntersections;
//find the furthest near intersection
BoxIntersections.x = max(ClosestPlaneIntersections.x, max(ClosestPlaneIntersections.y, ClosestPlaneIntersections.z));
//find the closest far intersection
BoxIntersections.y = min(FurthestPlaneIntersections.x, min(FurthestPlaneIntersections.y, FurthestPlaneIntersections.z));
//clamp the intersections to be between RayOrigin and RayEnd on the ray
return saturate(BoxIntersections);
}
/** Computes distance from an AABB to a point in space. */
MaterialFloat ComputeDistanceFromBoxToPoint(MaterialFloat3 Mins, MaterialFloat3 Maxs, MaterialFloat3 InPoint)
{
MaterialFloat3 DistancesToMin = InPoint < Mins ? abs(InPoint - Mins) : 0;
MaterialFloat3 DistancesToMax = InPoint > Maxs ? abs(InPoint - Maxs) : 0;
//@todo - this is actually incorrect, it gives manhattan distance
MaterialFloat Distance = dot(DistancesToMin, 1);
Distance += dot(DistancesToMax, 1);
return Distance;
}
/** Computes squared distance from a point in space to an AABB. */
MaterialFloat ComputeSquaredDistanceFromBoxToPoint(MaterialFloat3 BoxCenter, MaterialFloat3 BoxExtent, MaterialFloat3 InPoint)
{
MaterialFloat3 AxisDistances = max(abs(InPoint - BoxCenter) - BoxExtent, 0);
return dot(AxisDistances, AxisDistances);
}
/** Computes distance from point inside an AABB to the AABB's surface. */
float ComputeDistanceFromBoxToPointInside(float3 BoxCenter, float3 BoxExtent, float3 InPoint)
{
float3 DistancesToMin = max(InPoint - BoxCenter + BoxExtent, 0);
float3 DistancesToMax = max(BoxCenter + BoxExtent - InPoint, 0);
float3 ClosestDistances = min(DistancesToMin, DistancesToMax);
return min(ClosestDistances.x, min(ClosestDistances.y, ClosestDistances.z));
}
bool RayHitSphere(float3 RayOrigin, float3 UnitRayDirection, float3 SphereCenter, float SphereRadius)
{
float3 ClosestPointOnRay = max(0, dot(SphereCenter - RayOrigin, UnitRayDirection)) * UnitRayDirection;
float3 CenterToRay = RayOrigin + ClosestPointOnRay - SphereCenter;
return dot(CenterToRay, CenterToRay) <= Square(SphereRadius);
}
bool RaySegmentHitSphere(float3 RayOrigin, float3 UnitRayDirection, float RayLength, float3 SphereCenter, float SphereRadius)
{
float DistanceAlongRay = dot(SphereCenter - RayOrigin, UnitRayDirection);
float3 ClosestPointOnRay = DistanceAlongRay * UnitRayDirection;
float3 CenterToRay = RayOrigin + ClosestPointOnRay - SphereCenter;
return dot(CenterToRay, CenterToRay) <= Square(SphereRadius) && DistanceAlongRay > -SphereRadius && DistanceAlongRay - SphereRadius < RayLength;
}
/** Transforms a vector from tangent space to world space */
MaterialFloat3 TransformTangentVectorToWorld(MaterialFloat3x3 TangentToWorld, MaterialFloat3 InTangentVector)
{
// Transform directly to world space
// The vector transform is optimized for this case, only one vector-matrix multiply is needed
return mul(InTangentVector, TangentToWorld);
}
/** Transforms a vector from world space to tangent space */
MaterialFloat3 TransformWorldVectorToTangent(MaterialFloat3x3 TangentToWorld, MaterialFloat3 InWorldVector)
{
// Transform from world to tangent space with the transpose of TangentToWorld (achieved by swapping vector / matrix multiply order)
// Note that the transpose is only equal to the inverse for orthonormal matrices - aka only uniform scaling
return mul(TangentToWorld, InWorldVector);
}
float3 TransformWorldVectorToView(float3 InTangentVector)
{
// Transform from world to view space
return mul(InTangentVector, (float3x3)ResolvedView.TranslatedWorldToView);
}
/** Computes the distance from the center to the edge of an AABB with the given extents in the given direction. */
MaterialFloat GetBoxPushout(MaterialFloat3 Normal,MaterialFloat3 Extent)
{
return dot(abs(Normal * Extent), MaterialFloat3(1.0f, 1.0f, 1.0f));
}
/** Generates arbitrary but valid perpendicular unit vectors to ZAxis. ZAxis should be unit length. */
void GenerateCoordinateSystem(float3 ZAxis, out float3 XAxis, out float3 YAxis)
{
if (abs(ZAxis.x) > abs(ZAxis.y))
{
float InverseLength = 1.0f / sqrt(dot(ZAxis.xz, ZAxis.xz));
XAxis = float3(-ZAxis.z * InverseLength, 0.0f, ZAxis.x * InverseLength);
}
else
{
float InverseLength = 1.0f / sqrt(dot(ZAxis.yz, ZAxis.yz));
XAxis = float3(0.0f, ZAxis.z * InverseLength, -ZAxis.y * InverseLength);
}
YAxis = cross(ZAxis, XAxis);
}
// Define passthrough implementations of EvaluateAttributeAtSample for non-D3D11 platforms.
#if !SM5_PROFILE
float EvaluateAttributeAtSample(float Attribute,uint SampleIndex) { return Attribute; }
float2 EvaluateAttributeAtSample(float2 Attribute,uint SampleIndex) { return Attribute; }
float3 EvaluateAttributeAtSample(float3 Attribute,uint SampleIndex) { return Attribute; }
float4 EvaluateAttributeAtSample(float4 Attribute,uint SampleIndex) { return Attribute; }
#endif
/** Output of the screen vertex shader. */
struct FScreenVertexOutput
{
#if METAL_PROFILE || COMPILER_GLSL_ES3_1
//@todo-rco: FIXME!
noperspective float2 UV : TEXCOORD0;
#else
noperspective MaterialFloat2 UV : TEXCOORD0;
#endif
float4 Position : SV_POSITION;
};
// for velocity rendering, motionblur and temporal AA
// velocity needs to support -2..2 screen space range for x and y
// texture is 16bit 0..1 range per channel
float2 EncodeVelocityToTexture(float2 In)
{
// 0.499f is a value smaller than 0.5f to avoid using the full range to use the clear color (0,0) as special value
// 0.5f to allow for a range of -2..2 instead of -1..1 for really fast motions for temporal AA
return In * (0.499f * 0.5f) + 32767.0f / 65535.0f;
}
// see EncodeVelocityToTexture()
float2 DecodeVelocityFromTexture(float2 In)
{
const float InvDiv = 1.0f / (0.499f * 0.5f);
// reference
// return (In - 32767.0f / 65535.0f ) / (0.499f * 0.5f);
// MAD layout to help compiler
return In * InvDiv - 32767.0f / 65535.0f * InvDiv;
}
// Used for the Global Illumination in the GIReplace material expression
bool GetGIReplaceState()
{
#if REFLECTIVE_SHADOW_MAP
return true;
#else
return false;
#endif
}
#if FEATURE_LEVEL >= FEATURE_LEVEL_SM4
struct FWriteToSliceGeometryOutput
{
FScreenVertexOutput Vertex;
uint LayerIndex : SV_RenderTargetArrayIndex;
};
#endif
// Helper macro to globally ignore requests for non-offset world positions in materials when lower than shader model 4. We do this
// because we are using an extra interpolator for this second world position, and in < ES31 there may not be enough
#define USE_WORLD_POSITION_EXCLUDING_SHADER_OFFSETS (NEEDS_WORLD_POSITION_EXCLUDING_SHADER_OFFSETS && FEATURE_LEVEL >= FEATURE_LEVEL_ES3_1)
/** Used for calculating vertex positions and UVs when drawing with DrawRectangle */
void DrawRectangle( in float4 InPosition, in float2 InTexCoord, out float4 OutPosition, out float2 OutTexCoord)
{
OutPosition = InPosition;
OutPosition.xy = -1.0f + 2.0f * (DrawRectangleParameters.PosScaleBias.zw + (InPosition.xy * DrawRectangleParameters.PosScaleBias.xy)) * DrawRectangleParameters.InvTargetSizeAndTextureSize.xy;
OutPosition.xy *= float2( 1, -1 );
OutTexCoord.xy = (DrawRectangleParameters.UVScaleBias.zw + (InTexCoord.xy * DrawRectangleParameters.UVScaleBias.xy)) * DrawRectangleParameters.InvTargetSizeAndTextureSize.zw;
}
/** Used for calculating vertex positions when drawing with DrawRectangle */
void DrawRectangle( in float4 InPosition, out float4 OutPosition)
{
OutPosition = InPosition;
OutPosition.xy = -1.0f + 2.0f * (DrawRectangleParameters.PosScaleBias.zw + (InPosition.xy * DrawRectangleParameters.PosScaleBias.xy)) * DrawRectangleParameters.InvTargetSizeAndTextureSize.xy;
OutPosition.xy *= float2( 1, -1 );
}
//Since some platforms don't remove Nans in saturate calls,
//SafeSaturate function will remove nan/inf.
//Can be expensive, only call when there's a good reason to expect Nans.
//D3D saturate actually turns Nans -> 1 since it does the min(x, 1) first, and D3D nan rules specify the non-nand operand wins in such a case.
//See: http://msdn.microsoft.com/en-us/library/windows/desktop/jj218760(v=vs.85).aspx
#define SafeSaturate_Def(type)\
type SafeSaturate(type In) \
{\
return saturate(In);\
}
SafeSaturate_Def(float)
SafeSaturate_Def(float2)
SafeSaturate_Def(float3)
SafeSaturate_Def(float4)
// Experimental way to allow adjusting the OpacityMask for shadow map rendering of masked materials.
// Can be accessed with a Custom material node. If this turns out to be very useful we can expose as MaterialFunction
// and potentially expose other queries as well (e.g. SkeletalMesh, HitProxy, ).
// @return 0:no, 1:yes
float IsShadowDepthShader()
{
#ifdef SHADOW_DEPTH_SHADER
return 1;
#else
return 0;
#endif
}
#define TERRAIN_ZSCALE (1.0f/128.0f)
// Decodes a value which was packed into two 8 bit channels
float DecodePackedTwoChannelValue(float2 PackedHeight)
{
return PackedHeight.x * 255.0 * 256.0 + PackedHeight.y * 255.0;
}
float DecodeHeightValue(float InValue)
{
return (InValue - 32768.0) * TERRAIN_ZSCALE;
}
float DecodePackedHeight(float2 PackedHeight)
{
return DecodeHeightValue(DecodePackedTwoChannelValue(PackedHeight));
}
uint ReverseBits32( uint bits )
{
#if SM5_PROFILE && !METAL_SM5_PROFILE
return reversebits( bits );
#else
bits = ( bits << 16) | ( bits >> 16);
bits = ( (bits & 0x00ff00ff) << 8 ) | ( (bits & 0xff00ff00) >> 8 );
bits = ( (bits & 0x0f0f0f0f) << 4 ) | ( (bits & 0xf0f0f0f0) >> 4 );
bits = ( (bits & 0x33333333) << 2 ) | ( (bits & 0xcccccccc) >> 2 );
bits = ( (bits & 0x55555555) << 1 ) | ( (bits & 0xaaaaaaaa) >> 1 );
return bits;
#endif
}
SamplerState GetBilinearWrappedSampler()
{
#if PS4_PROFILE
sce::Gnm::Sampler BuiltinSampler = GetDefaultBuiltinSampler();
BuiltinSampler.setWrapMode(sce::Gnm::kWrapModeWrap, sce::Gnm::kWrapModeWrap, sce::Gnm::kWrapModeWrap);
BuiltinSampler.setXyFilterMode(sce::Gnm::kFilterModeBilinear, sce::Gnm::kFilterModeBilinear);
BuiltinSampler.setZFilterMode(sce::Gnm::kZFilterModeLinear);
BuiltinSampler.setMipFilterMode(sce::Gnm::kMipFilterModePoint);
return SamplerState(BuiltinSampler);
#else
return BuiltinSamplers.Bilinear;
#endif
}
SamplerState GetBilinearClampedSampler()
{
#if PS4_PROFILE
sce::Gnm::Sampler BuiltinSampler = GetDefaultBuiltinSampler();
BuiltinSampler.setXyFilterMode(sce::Gnm::kFilterModeBilinear, sce::Gnm::kFilterModeBilinear);
BuiltinSampler.setMipFilterMode(sce::Gnm::kMipFilterModePoint);
BuiltinSampler.setZFilterMode(sce::Gnm::kZFilterModeLinear);
BuiltinSampler.setWrapMode(sce::Gnm::kWrapModeClampLastTexel, sce::Gnm::kWrapModeClampLastTexel, sce::Gnm::kWrapModeClampLastTexel);
return SamplerState(BuiltinSampler);
#else
return BuiltinSamplers.BilinearClamped;
#endif
}
SamplerState GetPointWrappedSampler()
{
#if PS4_PROFILE
sce::Gnm::Sampler BuiltinSampler = GetDefaultBuiltinSampler();
BuiltinSampler.setXyFilterMode(sce::Gnm::kFilterModePoint, sce::Gnm::kFilterModePoint);
BuiltinSampler.setMipFilterMode(sce::Gnm::kMipFilterModePoint);
BuiltinSampler.setZFilterMode(sce::Gnm::kZFilterModePoint);
BuiltinSampler.setWrapMode(sce::Gnm::kWrapModeWrap, sce::Gnm::kWrapModeWrap, sce::Gnm::kWrapModeWrap);
return SamplerState(BuiltinSampler);
#else
return BuiltinSamplers.Point;
#endif
}
SamplerState GetPointClampedSampler()
{
#if PS4_PROFILE
sce::Gnm::Sampler BuiltinSampler = GetDefaultBuiltinSampler();
BuiltinSampler.setXyFilterMode(sce::Gnm::kFilterModePoint, sce::Gnm::kFilterModePoint);
BuiltinSampler.setMipFilterMode(sce::Gnm::kMipFilterModePoint);
BuiltinSampler.setZFilterMode(sce::Gnm::kZFilterModePoint);
BuiltinSampler.setWrapMode(sce::Gnm::kWrapModeClampLastTexel, sce::Gnm::kWrapModeClampLastTexel, sce::Gnm::kWrapModeClampLastTexel);
return SamplerState(BuiltinSampler);
#else
return BuiltinSamplers.PointClamped;
#endif
}
SamplerState GetTrilinearWrappedSampler()
{
#if PS4_PROFILE
sce::Gnm::Sampler BuiltinSampler = GetDefaultBuiltinSampler();
BuiltinSampler.setXyFilterMode(sce::Gnm::kFilterModeBilinear, sce::Gnm::kFilterModeBilinear);
BuiltinSampler.setMipFilterMode(sce::Gnm::kMipFilterModeLinear);
BuiltinSampler.setZFilterMode(sce::Gnm::kZFilterModeLinear);
BuiltinSampler.setWrapMode(sce::Gnm::kWrapModeWrap, sce::Gnm::kWrapModeWrap, sce::Gnm::kWrapModeWrap);
return SamplerState(BuiltinSampler);
#else
return BuiltinSamplers.Trilinear;
#endif
}
SamplerState GetTrilinearClampedSampler()
{
#if PS4_PROFILE
sce::Gnm::Sampler BuiltinSampler = GetDefaultBuiltinSampler();
BuiltinSampler.setXyFilterMode(sce::Gnm::kFilterModeBilinear, sce::Gnm::kFilterModeBilinear);
BuiltinSampler.setMipFilterMode(sce::Gnm::kMipFilterModeLinear);
BuiltinSampler.setZFilterMode(sce::Gnm::kZFilterModeLinear);
BuiltinSampler.setWrapMode(sce::Gnm::kWrapModeClampLastTexel, sce::Gnm::kWrapModeClampLastTexel, sce::Gnm::kWrapModeClampLastTexel);
return SamplerState(BuiltinSampler);
#else
return BuiltinSamplers.TrilinearClamped;
#endif
}
// see PixelShaderOutputCommon
struct FPixelShaderIn
{
// read only
float4 SvPosition;
// Pixel Shader InCoverage, only usable if PIXELSHADEROUTPUT_COVERAGE is 1
uint Coverage;
//
bool bIsFrontFace;
};
// see PixelShaderOutputCommon
struct FPixelShaderOut
{
// [0..7], only usable if PIXELSHADEROUTPUT_MRT0, PIXELSHADEROUTPUT_MRT1, ... is 1
float4 MRT[8];
// Pixel Shader OutCoverage, only usable if PIXELSHADEROUTPUT_COVERAGE is 1
uint Coverage;
// Pixel Shader OutDepth
float Depth;
};