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

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

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

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

	// SRV for WhiteDummy Texture.
	TRefCountPtr<FRHIShaderResourceView> WhiteDummySRV;
	// SRV for StencilDummy Texture.
	TRefCountPtr<FRHIShaderResourceView> 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<FSystemTextures> 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);
		    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值