UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
MaterialCacheAttribute.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "PixelFormat.h"
6#include "VirtualTexturing.h"
7#include "MaterialValueType.h"
8#include "Containers/Array.h"
9#include "MaterialCacheAttribute.generated.h"
10
12static constexpr uint32 MaterialCacheMaxRuntimeLayers = VIRTUALTEXTURE_SPACE_MAXLAYERS;
13
15static constexpr uint32 MaterialCacheMaxTagsPerPrimitive = 4u;
16
18static constexpr bool MaterialCacheDebugUseIdentities = true;
19
21UENUM(BlueprintType)
23{
29 Normal,
33 Opacity,
35
39
41 Mask,
42
44 Float
45};
46
48UENUM(BlueprintType)
56
58static EMaterialCacheAttribute DefaultMaterialCacheAttributes[] = {
65};
66
67static bool IsMaterialAttribute(EMaterialCacheAttribute Attribute)
68{
69 switch (Attribute)
70 {
71 default:
72 return false;
80 return true;
81 }
82}
83
84USTRUCT()
86{
88
89
90 UPROPERTY(VisibleAnywhere, Category = Layer)
92
94 UPROPERTY(VisibleAnywhere, Category = Layer)
95 TEnumAsByte<EPixelFormat> CompressedFormat = PF_Unknown;
96
98 UPROPERTY()
99 uint8 ComponentCount = 0;
100
102 UPROPERTY(VisibleAnywhere, Category = Layer)
103 bool bIsSRGB = false;
104
106 UPROPERTY(VisibleAnywhere, Category = Layer)
108
110 UPROPERTY(VisibleAnywhere, Category = Layer)
112};
113
115
125
127static bool MaterialCacheAttributeIdentityTest(TArray<EMaterialCacheAttribute>& Attributes, const TConstArrayView<EMaterialCacheAttribute>& IdentityAttributes)
128{
129 // Check if the current attribute set contains all identity attributes
131 {
132 int32 Index = Attributes.Find(IdentityAttribute);
133 if (Index == INDEX_NONE)
134 {
135 // Attribute missing, identity not valid
136 return false;
137 }
138 }
139
140 // Identity valid, remove all identity attributes
142 {
143 Attributes.Remove(IdentityAttribute);
144 }
145
146 return true;
147}
148
149static uint8 GetMaterialCacheAttributeComponentCount(EMaterialCacheAttribute Attribute, bool bIsStore)
150{
151 switch (Attribute)
152 {
154 return 3;
156 // Store's in either tangent-space or encoded world-space, which is .xy
157 // TODO[MP]: Optionally store in world-space
158 return bIsStore ? 2 : 3;
160 return 1;
162 return 1;
164 return 1;
166 return 1;
168 return 3;
170 return 1;
172 return 1;
174 return 1;
175 }
176
177 checkNoEntry();
178 return 0;
179}
180
181static uint8 GetMaterialCacheLayerAttributeSwizzleOffset(const FMaterialCacheLayer& Layer, EMaterialCacheAttribute Attribute, bool bIsStore)
182{
183 uint8 Offset = 0;
184
185 for (EMaterialCacheAttribute Contained : Layer.Attributes)
186 {
187 if (Attribute == Contained)
188 {
189 return Offset;
190 }
191
192 Offset += GetMaterialCacheAttributeComponentCount(Contained, bIsStore);
193 }
194
195 checkf(false, TEXT("Attribute not present in layer"));
196 return 0;
197}
198
199static EMaterialValueType GetMaterialCacheAttributeValueType(EMaterialCacheAttribute Attribute)
200{
201 switch (Attribute)
202 {
204 return MCT_Float3;
206 return MCT_Float3;
208 return MCT_Float1;
210 return MCT_Float1;
212 return MCT_Float1;
214 return MCT_Float1;
216 return MCT_Float3;
218 return MCT_Float1;
220 return MCT_Float1;
222 return MCT_Float1;
223 }
224
225 checkNoEntry();
226 return MCT_Unknown;
227}
228
229static FString GetMaterialCacheAttributeDecoration(EMaterialCacheAttribute Attribute)
230{
231 switch (Attribute)
232 {
234 return TEXT("BaseColor");
236 return TEXT("Normal");
238 return TEXT("Roughness");
240 return TEXT("Specular");
242 return TEXT("Metallic");
244 return TEXT("Opacity");
246 return TEXT("WorldPosition");
248 return TEXT("WorldHeight");
250 return TEXT("Mask");
252 return TEXT("Float");
253 }
254
255 checkNoEntry();
256 return TEXT("");
257}
258
259static FString GetMaterialCacheLayerDecoration(const FMaterialCacheLayer& Layer)
260{
261 switch (Layer.Identity)
262 {
264 break;
266 return TEXT("BaseColorSpecular");
268 return TEXT("NormalSpecularOpacity");
270 return TEXT("MetallicWorldPositionOffset");
271 }
272
273 FString Composite;
274
275 for (EMaterialCacheAttribute Attribute : Layer.Attributes)
276 {
277 switch (Attribute)
278 {
280 Composite += TEXT("BaseColor");
281 break;
283 Composite += TEXT("Normal");
284 break;
286 Composite += TEXT("Roughness");
287 break;
289 Composite += TEXT("Specular");
290 break;
292 Composite += TEXT("Metallic");
293 break;
295 Composite += TEXT("Opacity");
296 break;
298 Composite += TEXT("WorldPosition");
299 break;
301 Composite += TEXT("WorldHeight");
302 break;
304 Composite += TEXT("Mask");
305 break;
307 Composite += TEXT("Float");
308 break;
309 }
310 }
311
312 return Composite;
313}
314
316template<typename A>
317static void PackMaterialCacheAttributeLayers(TArray<EMaterialCacheAttribute>& Attributes, TArray<FMaterialCacheLayer, A>& Out)
318{
321 if (MaterialCacheDebugUseIdentities)
322 {
323 // BaseColor.xyz Roughness.w
325 {
331 Layer.ComponentCount = 4;
332 Layer.bIsSRGB = true;
333 Out.Add(Layer);
334 }
335
336 // Normal.xy Specular.z Opacity.w
338 {
344 Layer.ComponentCount = 4;
345 Out.Add(Layer);
346 }
347
348 // Metallic.x WorldPosition.yzw
350 {
356 Layer.ComponentCount = 4;
357 Out.Add(Layer);
358 }
359 }
360
363 // TODO[MP]: We're currently allocating a separate layer for each attribute
364 // This is temporary of course, we can pack similar attributes down to the same
365 // layer which avoids VT limitations. One problem at a time.
366
367 for (EMaterialCacheAttribute Attribute : Attributes)
368 {
370 Layer.Attributes.Add(Attribute);
371
372 switch (Attribute)
373 {
375 Layer.RenderFormat = PF_R8G8B8;
377 Layer.bIsSRGB = true;
378 break;
382 break;
387 Layer.RenderFormat = PF_R8;
389 break;
391 Layer.RenderFormat = PF_R8G8B8;
393 break;
395 Layer.RenderFormat = PF_R16F;
397 break;
399 Layer.RenderFormat = PF_R8;
401 break;
405 break;
406 }
407
408 Layer.ComponentCount = GetMaterialCacheAttributeComponentCount(Attribute, true);
409 Out.Add(Layer);
410 }
411}
412
413template<typename A>
414static void PackMaterialCacheAttributeLayers(const TArrayView<EMaterialCacheAttribute>& Attributes, TArray<FMaterialCacheLayer, A>& Out)
415{
417 PackMaterialCacheAttributeLayers(AttributesCopy, Out);
418}
#define checkNoEntry()
Definition AssertionMacros.h:316
#define checkf(expr, format,...)
Definition AssertionMacros.h:315
@ INDEX_NONE
Definition CoreMiscDefines.h:150
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
EMaterialCacheAttribute
Definition MaterialCacheAttribute.h:23
EMaterialCacheAttributeIdentity
Definition MaterialCacheAttribute.h:50
EMaterialValueType
Definition MaterialValueType.h:15
@ MCT_Unknown
Definition MaterialValueType.h:36
@ MCT_Float1
Definition MaterialValueType.h:21
@ MCT_Float3
Definition MaterialValueType.h:23
#define UPROPERTY(...)
UObject definition macros.
Definition ObjectMacros.h:744
#define GENERATED_BODY(...)
Definition ObjectMacros.h:765
#define UENUM(...)
Definition ObjectMacros.h:749
#define USTRUCT(...)
Definition ObjectMacros.h:746
EPixelFormat
Definition PixelFormat.h:16
@ PF_Unknown
Definition PixelFormat.h:17
@ PF_R16F
Definition PixelFormat.h:38
@ PF_R32_FLOAT
Definition PixelFormat.h:30
@ PF_R8G8B8
Definition PixelFormat.h:110
@ PF_A2B10G10R10
Definition PixelFormat.h:35
@ PF_DXT1
Definition PixelFormat.h:22
@ PF_DXT5
Definition PixelFormat.h:24
@ PF_R8
Definition PixelFormat.h:88
@ PF_R8G8B8A8
Definition PixelFormat.h:54
#define VIRTUALTEXTURE_SPACE_MAXLAYERS
Definition VirtualTexturing.h:57
uint32 Offset
Definition VulkanMemory.cpp:4033
uint8_t uint8
Definition binka_ue_file_header.h:8
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition ArrayView.h:139
Definition Array.h:670
SizeType Remove(const ElementType &Item)
Definition Array.h:3091
UE_NODEBUG UE_FORCEINLINE_HINT SizeType Add(ElementType &&Item)
Definition Array.h:2696
UE_NODEBUG UE_FORCEINLINE_HINT bool Find(const ElementType &Item, SizeType &Index) const
Definition Array.h:1302
Definition EnumAsByte.h:22
Definition ContainerAllocationPolicies.h:894
@ false
Definition radaudio_common.h:23
U16 Index
Definition radfft.cpp:71
Definition Guid.h:109
Definition MaterialCacheAttribute.h:86
EMaterialCacheAttributeIdentity Identity
Definition MaterialCacheAttribute.h:107
bool bIsSRGB
Definition MaterialCacheAttribute.h:103
TEnumAsByte< EPixelFormat > CompressedFormat
Definition MaterialCacheAttribute.h:95
TArray< EMaterialCacheAttribute > Attributes
Definition MaterialCacheAttribute.h:111
uint8 ComponentCount
Definition MaterialCacheAttribute.h:99
TEnumAsByte< EPixelFormat > RenderFormat
Definition MaterialCacheAttribute.h:91
Definition MaterialCacheAttribute.h:118
FGuid Guid
Definition MaterialCacheAttribute.h:120
FMaterialCacheLayerArray Layers
Definition MaterialCacheAttribute.h:123