UE4 RectLight LTCMat LTCAmp 初始化

DefferredShadingRender.cpp

void FDeferredShadingSceneRenderer::Render(FRHICommandListImmediate& RHICmdList)
{
	{
		SCOPE_CYCLE_COUNTER(STAT_FDeferredShadingSceneRenderer_Render_Init);
		SCOPED_GPU_STAT(RHICmdList, AllocateRendertargets);

		// Initialize global system textures (pass-through if already initialized).
		GSystemTextures.InitializeTextures(RHICmdList, FeatureLevel);

		// Allocate the maximum scene render target space for the current view family.
		SceneContext.SetKeepDepthContent(true);
		SceneContext.Allocate(RHICmdList, this);
	}
}

SystemTextures.h

// Copyright Epic Games, Inc. All Rights Reserved.

/*=============================================================================
	SystemTextures.h: System textures definitions.
=============================================================================*/

#pragma once

#include "RenderGraph.h"

/**
 * Encapsulates the system textures used for scene rendering.
 */
class FSystemTextures : public FRenderResource
{
public:
	FSystemTextures()
		: FRenderResource()
		, FeatureLevelInitializedTo(ERHIFeatureLevel::Num)
	{}

	/**
	 * Initialize/allocate textures if not already.
	 */
	inline void InitializeTextures(FRHICommandListImmediate& RHICmdList, const ERHIFeatureLevel::Type InFeatureLevel)
	{
		// When we render to system textures it should occur on all GPUs since this only
		// happens once on startup (or when the feature level changes).
		SCOPED_GPU_MASK(RHICmdList, FRHIGPUMask::All());

		// if this is the first call initialize everything
		if (FeatureLevelInitializedTo == ERHIFeatureLevel::Num)
		{
			InitializeCommonTextures(RHICmdList);
			InitializeFeatureLevelDependentTextures(RHICmdList, InFeatureLevel);
		}
		// otherwise, if we request a higher feature level, we might need to initialize those textures that depend on the feature level
		else if (InFeatureLevel > FeatureLevelInitializedTo)
		{
			InitializeFeatureLevelDependentTextures(RHICmdList, InFeatureLevel);
		}
		// there's no needed setup for those feature levels lower or identical to the current one
	}

	// FRenderResource interface.
	/**
	 * Release textures when device is lost/destroyed.
	 */
	virtual void ReleaseDynamicRHI();

	// -----------

	/**
		Any Textures added here MUST be explicitly released on ReleaseDynamicRHI()!
		Some RHIs need all their references released during destruction!
	*/

	// float4(1,1,1,1) can be used in case a light is not shadow casting
	TRefCountPtr WhiteDummy;
	// float4(0,0,0,0) can be used in additive postprocessing to avoid a shader combination
	TRefCountPtr BlackDummy;
	// float4(0,0,0,1)
	TRefCountPtr BlackAlphaOneDummy;
	// used by the material expression Noise
	TRefCountPtr PerlinNoiseGradient;
	// used by the material expression Noise (faster version, should replace old version), todo: move out of SceneRenderTargets
	TRefCountPtr PerlinNoise3D;
	// Sobol sampling texture, the first sample points for four sobol dimensions in RGBA
	TRefCountPtr SobolSampling;
	/** SSAO randomization */
	TRefCountPtr SSAORandomization;
	/** GTAO randomization */
	TRefCountPtr GTAORandomization;
	/** GTAO PreIntegrated */
	TRefCountPtr GTAOPreIntegrated;

	/** Preintegrated GF for single sample IBL */
	TRefCountPtr PreintegratedGF;
	/** Hair BSDF LUT texture */
	TRefCountPtr HairLUT0;
	TRefCountPtr HairLUT1;
	TRefCountPtr HairLUT2;
	/** Linearly Transformed Cosines LUTs */
	TRefCountPtr LTCMat;
	TRefCountPtr LTCAmp;
	/** Texture that holds a single value containing the maximum depth that can be stored as FP16. */
	TRefCountPtr MaxFP16Depth;
	/** Depth texture that holds a single depth value */
	TRefCountPtr DepthDummy;
	/** Stencil texture that holds a single stencil value. */
	TRefCountPtr StencilDummy;
	// float4(0,1,0,1)
	TRefCountPtr GreenDummy;
	// float4(0.5,0.5,0.5,1)
	TRefCountPtr DefaultNormal8Bit;
	// float4(0.5,0.5,0.5,0.5)
	TRefCountPtr MidGreyDummy;

	/** float4(0,0,0,0) volumetric texture. */
	TRefCountPtr VolumetricBlackDummy;
	

	// Dummy 0 Uint texture for RHIs that need explicit overloads
	TRefCountPtr ZeroUIntDummy;

	// SRV for WhiteDummy Texture.
	TRefCountPtr WhiteDummySRV;
	// SRV for StencilDummy Texture.
	TRefCountPtr StencilDummySRV;

	FRDGTextureRef GetWhiteDummy(FRDGBuilder& GraphBuilder) const;
	FRDGTextureRef GetBlackDummy(FRDGBuilder& GraphBuilder) const;
	FRDGTextureRef GetZeroUIntDummy(FRDGBuilder& GraphBuilder) const;
	FRDGTextureRef GetBlackAlphaOneDummy(FRDGBuilder& GraphBuilder) const;
	FRDGTextureRef GetPerlinNoiseGradient(FRDGBuilder& GraphBuilder) const;
	FRDGTextureRef GetPerlinNoise3D(FRDGBuilder& GraphBuilder) const;
	FRDGTextureRef GetSobolSampling(FRDGBuilder& GraphBuilder) const;
	FRDGTextureRef GetSSAORandomization(FRDGBuilder& GraphBuilder) const;
	FRDGTextureRef GetPreintegratedGF(FRDGBuilder& GraphBuilder) const;
	FRDGTextureRef GetLTCMat(FRDGBuilder& GraphBuilder) const;
	FRDGTextureRef GetLTCAmp(FRDGBuilder& GraphBuilder) const;
	FRDGTextureRef GetMaxFP16Depth(FRDGBuilder& GraphBuilder) const;
	FRDGTextureRef GetDepthDummy(FRDGBuilder& GraphBuilder) const;
	FRDGTextureRef GetStencilDummy(FRDGBuilder& GraphBuilder) const;
	FRDGTextureRef GetGreenDummy(FRDGBuilder& GraphBuilder) const;
	FRDGTextureRef GetDefaultNormal8Bit(FRDGBuilder& GraphBuilder) const;
	FRDGTextureRef GetMidGreyDummy(FRDGBuilder& GraphBuilder) const;
	FRDGTextureRef GetVolumetricBlackDummy(FRDGBuilder& GraphBuilder) const;

protected:
	/** Maximum feature level that the textures have been initialized up to */
	ERHIFeatureLevel::Type FeatureLevelInitializedTo;

	void InitializeCommonTextures(FRHICommandListImmediate& RHICmdList);
	void InitializeFeatureLevelDependentTextures(FRHICommandListImmediate& RHICmdList, const ERHIFeatureLevel::Type InFeatureLevel);
};

/** The global system textures used for scene rendering. */
RENDERER_API extern TGlobalResource GSystemTextures;

LTC.h

#pragma once

static const int LTC_Size = 64;

static const float LTC_Mat[ 4 * LTC_Size * LTC_Size ] =
{
1, 0, 0, 2e-05,
1, 0, 0, 0.000503905,
1, 0, 0, 0.00201562,
1, 0, 0, 0.00453516,
1, 0, 0, 0.00806253,
1, 0, 0, 0.0125978,
1, 0, 0, 0.018141,
1, 0, 0, 0.0246924,
1, 0, 0, 0.0322525,
....
};

static const float LTC_Amp[ 4 * LTC_Size * LTC_Size ] =
{
1, 0, 0, 0,
1, 7.91421e-31, 0, 0,
1, 1.04392e-24, 0, 0,
1, 3.49405e-21, 0, 0,
1, 1.09923e-18, 0, 0,
1, 9.47414e-17, 0, 0,
1, 3.59627e-15, 0, 0,
1, 7.72053e-14, 0, 0,
1, 1.08799e-12, 0, 0,
1, 1.10655e-11, 0, 0,
1, 8.65818e-11, 0, 0,
0.999998, 5.45037e-10, 0, 0,
0.999994, 2.85095e-09, 0, 0,
0.999989, 1.26931e-08, 0, 0,
......
};

SystemTextures.cpp

void FSystemTextures::InitializeFeatureLevelDependentTextures(FRHICommandListImmediate& RHICmdList, const ERHIFeatureLevel::Type InFeatureLevel)
{
		    {
			    FPooledRenderTargetDesc Desc(FPooledRenderTargetDesc::Create2DDesc(FIntPoint(LTC_Size, LTC_Size), PF_FloatRGBA, FClearValueBinding::None, TexCreate_FastVRAM, TexCreate_ShaderResource, false));
			    Desc.AutoWritable = false;
    
			    GRenderTargetPool.FindFreeElement(RHICmdList, Desc, LTCMat, TEXT("LTCMat"));
			    // Write the contents of the texture.
			    uint32 DestStride;
			    uint8* DestBuffer = (uint8*)RHICmdList.LockTexture2D((FTexture2DRHIRef&)LTCMat->GetRenderTargetItem().ShaderResourceTexture, 0, RLM_WriteOnly, DestStride, false);
    
				    for (int32 y = 0; y < Desc.Extent.Y; ++y)
			    {
					    for (int32 x = 0; x < Desc.Extent.X; ++x)
				    {
					    uint16* Dest = (uint16*)(DestBuffer + x * 4 * sizeof(uint16) + y * DestStride);
    
						    for (int k = 0; k < 4; k++)
							    Dest[k] = FFloat16(LTC_Mat[4 * (x + y * LTC_Size) + k]).Encoded;
				    }
			    }
			    RHICmdList.UnlockTexture2D((FTexture2DRHIRef&)LTCMat->GetRenderTargetItem().ShaderResourceTexture, 0, false);
		    }
    
		    {
			    FPooledRenderTargetDesc Desc(FPooledRenderTargetDesc::Create2DDesc(FIntPoint(LTC_Size, LTC_Size), PF_G16R16F, FClearValueBinding::None, TexCreate_FastVRAM, TexCreate_ShaderResource, false));
			    Desc.AutoWritable = false;
    
			    GRenderTargetPool.FindFreeElement(RHICmdList, Desc, LTCAmp, TEXT("LTCAmp"));
			    // Write the contents of the texture.
			    uint32 DestStride;
			    uint8* DestBuffer = (uint8*)RHICmdList.LockTexture2D((FTexture2DRHIRef&)LTCAmp->GetRenderTargetItem().ShaderResourceTexture, 0, RLM_WriteOnly, DestStride, false);
    
				for (int32 y = 0; y < Desc.Extent.Y; ++y)
			    {
					for (int32 x = 0; x < Desc.Extent.X; ++x)
				    {
					    uint16* Dest = (uint16*)(DestBuffer + x * 2 * sizeof(uint16) + y * DestStride);
    
						for (int k = 0; k < 2; k++)
						    Dest[k] = FFloat16(LTC_Amp[4 * (x + y * LTC_Size) + k]).Encoded;
				    }
			    }
			    RHICmdList.UnlockTexture2D((FTexture2DRHIRef&)LTCAmp->GetRenderTargetItem().ShaderResourceTexture, 0, false);
		    }
}

你可能感兴趣的:(#,UE4之灯光渲染,ue4)