UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
RawIndexBuffer.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3/*=============================================================================
4 RawIndexBuffer.h: Raw index buffer definitions.
5=============================================================================*/
6
7#pragma once
8
9#include "CoreMinimal.h"
10#include "RHI.h"
11#include "RHICommandList.h"
12#include "RenderResource.h"
14
15
17{
18public:
19
21
25 void CacheOptimize();
26
27 // FRenderResource interface.
28 virtual void InitRHI(FRHICommandListBase& RHICmdList) override;
29
30 // Serialization.
32};
33
34
36{
37public:
39 : b32Bit(true)
40 {
41 }
42
44
48 void CacheOptimize();
49
53 void ComputeIndexWidth();
54
59 void ForceUse32Bit(bool bIn32Bit) { b32Bit = bIn32Bit; }
60
61 // FRenderResource interface.
62 virtual void InitRHI(FRHICommandListBase& RHICmdList) override;
63
64 // Serialization.
66
67private:
68 bool b32Bit;
69};
70
75{
76 enum Type
77 {
79 Force16Bit = 1,
81 Force32Bit = 2,
83 AutoDetect = 3
84 };
85}
86
92{
93public:
96 : UntypedIndexData(nullptr)
97 , NumIndices(0)
98 , b32Bit(false)
99 {
100 }
101
109 : UntypedIndexData(InIndexData)
110 , NumIndices(InNumIndices)
111 , b32Bit(bIn32Bit)
112 {
113 }
114
116 uint32 operator[](int32 i) { return (uint32)(b32Bit ? ((const uint32*)UntypedIndexData)[i] : ((const uint16*)UntypedIndexData)[i]); }
117 uint32 operator[](int32 i) const { return (uint32)(b32Bit ? ((const uint32*)UntypedIndexData)[i] : ((const uint16*)UntypedIndexData)[i]); }
118 inline int32 Num() const { return NumIndices; }
119
120private:
122 const void* UntypedIndexData;
124 int32 NumIndices;
126 bool b32Bit;
127};
128
130{
131public:
138
145 bool TrySetAllowCPUAccess(bool bAllowCPUAccess)
146 {
147 IndexStorage.SetAllowCPUAccess(bAllowCPUAccess);
148
149 return !bAllowCPUAccess || CachedNumIndices == 0 || IndexStorage.Num() > 0;
150 }
151
157 inline void SetIndex( const uint32 At, const uint32 NewIndexValue )
158 {
159 check( At >= 0 && At < (uint32)IndexStorage.Num() );
160
161 if( b32Bit )
162 {
163 uint32* Indices32Bit = (uint32*)IndexStorage.GetData();
165 }
166 else
167 {
168 uint16* Indices16Bit = (uint16*)IndexStorage.GetData();
170 }
171 }
172
178 ENGINE_API void SetIndices(const TArray<uint32>& InIndices, EIndexBufferStride::Type DesiredStride);
179
187
194
196 inline uint32 GetIndex( const uint32 At ) const
197 {
198 check( At >= 0 && At < (uint32)IndexStorage.Num() );
199 uint32 IndexValue;
200 if( b32Bit )
201 {
202 const uint32* SrcIndices32Bit = (const uint32*)IndexStorage.GetData();
203 IndexValue = SrcIndices32Bit[ At ];
204 }
205 else
206 {
207 const uint16* SrcIndices16Bit = (const uint16*)IndexStorage.GetData();
208 IndexValue = SrcIndices16Bit[ At ];
209 }
210
211 return IndexValue;
212 }
213
214
222
229
231 void ExpandTo32Bit();
232
237 ENGINE_API const uint16* AccessStream16() const;
238
243 ENGINE_API const uint32* AccessStream32() const;
244
254
258 inline int32 GetNumIndices() const
259 {
260 return CachedNumIndices >= 0 ? CachedNumIndices : (b32Bit ? (IndexStorage.Num()/4) : (IndexStorage.Num()/2));
261 }
262
267 {
268 return IndexStorage.GetAllocatedSize();
269 }
270
271 inline bool GetAllowCPUAccess() const
272 {
273 return IndexStorage.GetAllowCPUAccess();
274 }
275
277 int32 GetIndexDataSize() const { return IndexStorage.Num(); }
278
281
284
287
293 ENGINE_API void Serialize(FArchive& Ar, bool bNeedsCPUAccess);
294
296 void SerializeMetaData(FArchive& Ar);
297
298 void ClearMetaData();
299
304 void Discard();
305
306 // FRenderResource interface.
307 ENGINE_API virtual void InitRHI(FRHICommandListBase& RHICmdList) override;
308
309 inline bool Is32Bit() const { return b32Bit; }
310
311private:
314
316 int32 CachedNumIndices;
317
319 bool b32Bit;
320};
321
326{
327public:
328 virtual void Serialize( FArchive& Ar ) = 0;
329
330 virtual void SerializeMetaData(FArchive& Ar) = 0;
331
333
339 virtual bool GetNeedsCPUAccess() const = 0;
340 // number of indices (e.g. 4 triangles would result in 12 elements)
341 virtual int32 Num() const = 0;
342 virtual int32 AddItem(uint32 Val) = 0;
343 virtual uint32 Get(uint32 Idx) const = 0;
344 virtual void* GetPointerTo(uint32 Idx) = 0;
345 virtual void Insert(int32 Idx, int32 Num = 1) = 0;
346 virtual void Remove(int32 Idx, int32 Num = 1) = 0;
347 virtual void Empty(int32 Slack = 0) = 0;
348 virtual int32 GetResourceDataSize() const = 0;
349
350 // @param guaranteed only to be valid if the vertex buffer is valid and the buffer was created with the SRV flags
352 {
353 return SRVValue;
354 }
355
356 // Allows for creating SRV on demand
357 // Note: consider cache the SRV locally if multiple compute tasks all need access to one in a single frame
359
360protected:
361 ENGINE_API bool IsSRVCreatedByDefault(bool bAllowCPUAccess) const;
362 ENGINE_API bool IsShaderResource(bool bAllowCPUAccess) const;
363
364 UE_DEPRECATED(5.7, "Use IsSRVCreatedByDefault or IsShaderResource instead, which gives more granular control over platform specific behavior")
365 ENGINE_API bool IsSRVNeeded(bool bAllowCPUAccess) const;
366
370
372 FRHICommandListBase& RHICmdList,
373 const TCHAR* InDebugName,
374 const FName& InOwnerName,
375 int32 IndexCount,
376 size_t IndexSize,
378 bool bIsShaderResource
379 );
380
381 // guaranteed only to be valid if the vertex buffer is valid and the buffer was created with the SRV flags
383};
384
387{
388public:
394 : Indices(InNeedsCPUAccess)
395 , CachedNumIndices(0)
396 {
397 static_assert(sizeof(INDEX_TYPE) == 2 || sizeof(INDEX_TYPE) == 4, "FRawStaticIndexBuffer16or32 must have a stride of 2 or 4 bytes.");
398 }
399
403 virtual void InitRHI(FRHICommandListBase& RHICmdList) override
404 {
405 IndexBufferRHI = CreateRHIBuffer(RHICmdList);
406
407 if (IsSRVCreatedByDefault(Indices.GetAllowCPUAccess()))
408 {
409 SRVValue = GetOrCreateSRV(RHICmdList);
410 }
411 }
412
413 virtual void ReleaseRHI() override
414 {
416
418 }
419
421 {
422 if (SRVValue.IsValid())
423 {
424 return SRVValue;
425 }
426
427 if (!IndexBufferRHI)
428 {
429 return {};
430 }
431
432 if (!IsShaderResource(Indices.GetAllowCPUAccess()))
433 {
434 return {};
435 }
436
441 .SetFormat(sizeof(INDEX_TYPE) == 2 ? PF_R16_UINT : PF_R32_UINT));
442
443 return ExternalSRV;
444 }
445
451 virtual void Serialize( FArchive& Ar ) override
452 {
453 Indices.BulkSerialize(Ar);
454 CachedNumIndices = Indices.Num();
455 }
456
457 virtual void SerializeMetaData(FArchive& Ar) override
458 {
459 Ar << CachedNumIndices;
460 }
461
462 virtual void SetMetaData(int32 InCachedNumIndices) override
463 {
464 CachedNumIndices = InCachedNumIndices;
465 }
466
470 void CacheOptimize();
471
472
477 virtual bool GetNeedsCPUAccess() const override { return Indices.GetAllowCPUAccess(); }
478
479 virtual int32 Num() const override
480 {
481 return CachedNumIndices;
482 }
483
484 virtual int32 AddItem(uint32 Val) override
485 {
486 ++CachedNumIndices;
487 return Indices.Add((INDEX_TYPE)Val);
488 }
489
490 virtual uint32 Get(uint32 Idx) const override
491 {
492 return (uint32)Indices[Idx];
493 }
494
495 virtual void* GetPointerTo(uint32 Idx) override
496 {
497 return (void*)(&Indices[Idx]);
498 }
499
500 virtual void Insert(int32 Idx, int32 Num) override
501 {
502 CachedNumIndices += Num;
503 Indices.InsertUninitialized(Idx, Num);
504 check(CachedNumIndices == Indices.Num());
505 }
506
507 virtual void Remove(int32 Idx, int32 Num) override
508 {
509 CachedNumIndices -= Num;
510 Indices.RemoveAt(Idx, Num);
511 check(CachedNumIndices == Indices.Num());
512 }
513
514 virtual void Empty(int32 Slack) override
515 {
516 Indices.Empty(Slack);
517 CachedNumIndices = 0;
518 }
519
520 virtual int32 GetResourceDataSize() const override
521 {
522 return Indices.GetResourceDataSize();
523 }
524
526 {
528 Indices = IndexBufferType(Buffer);
529 CachedNumIndices = Indices.Num();
530 }
531
534 {
535 if (CachedNumIndices)
536 {
537 // Need to cache number of indices from the source array *before* RHICreateBuffer is called
538 // because it will empty the source array.
539 CachedNumIndices = Indices.Num();
540
542 RHICmdList,
543 sizeof(INDEX_TYPE) == 4 ? TEXT("FRawStaticIndexBuffer32") : TEXT("FRawStaticIndexBuffer16"),
544 GetOwnerName(),
545 Indices.Num(),
546 sizeof(INDEX_TYPE),
547 &Indices,
548 IsShaderResource(Indices.GetAllowCPUAccess())
549 );
550 }
551 return nullptr;
552 }
553
559
564
565private:
567
568 int32 CachedNumIndices;
569
570};
#define check(expr)
Definition AssertionMacros.h:314
#define UE_DEPRECATED(Version, Message)
Definition CoreMiscDefines.h:302
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::SIZE_T SIZE_T
An unsigned integer the same size as a pointer, the same as UPTRINT.
Definition Platform.h:1150
FPlatformTypes::TCHAR TCHAR
Either ANSICHAR or WIDECHAR, depending on whether the platform supports wide characters or the requir...
Definition Platform.h:1135
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
return true
Definition ExternalRpcRegistry.cpp:601
@ PF_R32_UINT
Definition PixelFormat.h:45
@ PF_R16_UINT
Definition PixelFormat.h:49
float Val(const FString &Value)
Definition UnrealMath.cpp:3163
uint16_t uint16
Definition binka_ue_file_header.h:7
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition Archive.h:1208
Definition RawIndexBuffer.h:92
FIndexArrayView(const void *InIndexData, int32 InNumIndices, bool bIn32Bit)
Definition RawIndexBuffer.h:108
uint32 operator[](int32 i) const
Definition RawIndexBuffer.h:117
FIndexArrayView()
Definition RawIndexBuffer.h:95
int32 Num() const
Definition RawIndexBuffer.h:118
uint32 operator[](int32 i)
Definition RawIndexBuffer.h:116
Definition RenderResource.h:509
FBufferRHIRef IndexBufferRHI
Definition RenderResource.h:524
Definition NameTypes.h:617
Definition RHIResources.h:1581
Definition RHICommandList.h:455
FShaderResourceViewRHIRef CreateShaderResourceView(FRHIBuffer *Buffer, FRHIViewDesc::FBufferSRV::FInitializer const &ViewDesc)
Definition RHICommandList.h:975
Definition RHIResourceReplace.h:55
Definition RHIResources.h:3304
Definition RawIndexBuffer.h:36
void ForceUse32Bit(bool bIn32Bit)
Definition RawIndexBuffer.h:59
virtual void InitRHI(FRHICommandListBase &RHICmdList) override
Definition RawIndexBuffer.cpp:110
friend FArchive & operator<<(FArchive &Ar, FRawIndexBuffer16or32 &I)
Definition RawIndexBuffer.cpp:153
void ComputeIndexWidth()
Definition RawIndexBuffer.cpp:89
void CacheOptimize()
Definition RawIndexBuffer.cpp:82
TArray< uint32 > Indices
Definition RawIndexBuffer.h:43
FRawIndexBuffer16or32()
Definition RawIndexBuffer.h:38
Definition RawIndexBuffer.h:17
virtual void InitRHI(FRHICommandListBase &RHICmdList) override
Definition RawIndexBuffer.cpp:50
friend FArchive & operator<<(FArchive &Ar, FRawIndexBuffer &I)
Definition RawIndexBuffer.cpp:69
TArray< uint16 > Indices
Definition RawIndexBuffer.h:20
void CacheOptimize()
Definition RawIndexBuffer.cpp:43
Definition RawIndexBuffer.h:326
virtual void Empty(int32 Slack=0)=0
ENGINE_API bool IsShaderResource(bool bAllowCPUAccess) const
Definition RawIndexBuffer.cpp:466
void ReleaseRHIForStreaming(FRHIResourceReplaceBatcher &Batcher)
Definition RawIndexBuffer.cpp:498
virtual void * GetPointerTo(uint32 Idx)=0
virtual int32 Num() const =0
ENGINE_API bool IsSRVNeeded(bool bAllowCPUAccess) const
Definition RawIndexBuffer.cpp:479
virtual void SetMetaData(int32 InCachedNumIndices)=0
virtual void Remove(int32 Idx, int32 Num=1)=0
virtual void Insert(int32 Idx, int32 Num=1)=0
void InitRHIForStreaming(FRHIBuffer *IntermediateBuffer, size_t IndexSize, FRHIResourceReplaceBatcher &Batcher)
Definition RawIndexBuffer.cpp:490
virtual int32 GetResourceDataSize() const =0
virtual void Serialize(FArchive &Ar)=0
virtual void SerializeMetaData(FArchive &Ar)=0
static ENGINE_API FBufferRHIRef CreateRHIIndexBufferInternal(FRHICommandListBase &RHICmdList, const TCHAR *InDebugName, const FName &InOwnerName, int32 IndexCount, size_t IndexSize, FResourceArrayInterface *ResourceArray, bool bIsShaderResource)
Definition RawIndexBuffer.cpp:506
virtual FShaderResourceViewRHIRef GetOrCreateSRV(FRHICommandListBase &RHICmdList) const =0
virtual bool GetNeedsCPUAccess() const =0
virtual uint32 Get(uint32 Idx) const =0
ENGINE_API bool IsSRVCreatedByDefault(bool bAllowCPUAccess) const
Definition RawIndexBuffer.cpp:455
virtual int32 AddItem(uint32 Val)=0
FShaderResourceViewRHIRef SRVValue
Definition RawIndexBuffer.h:382
FRHIShaderResourceView * GetSRV() const
Definition RawIndexBuffer.h:351
Definition RawIndexBuffer.h:387
void ReleaseRHIForStreaming(FRHIResourceReplaceBatcher &Batcher)
Definition RawIndexBuffer.h:560
virtual int32 AddItem(uint32 Val) override
Definition RawIndexBuffer.h:484
virtual void * GetPointerTo(uint32 Idx) override
Definition RawIndexBuffer.h:495
void InitRHIForStreaming(FRHIBuffer *IntermediateBuffer, FRHIResourceReplaceBatcher &Batcher)
Definition RawIndexBuffer.h:555
virtual void Serialize(FArchive &Ar) override
Definition RawIndexBuffer.h:451
virtual int32 Num() const override
Definition RawIndexBuffer.h:479
virtual FShaderResourceViewRHIRef GetOrCreateSRV(FRHICommandListBase &RHICmdList) const override
Definition RawIndexBuffer.h:420
virtual void ReleaseRHI() override
Definition RawIndexBuffer.h:413
FBufferRHIRef CreateRHIBuffer(FRHICommandListBase &RHICmdList)
Definition RawIndexBuffer.h:533
virtual void InitRHI(FRHICommandListBase &RHICmdList) override
Definition RawIndexBuffer.h:403
virtual void Remove(int32 Idx, int32 Num) override
Definition RawIndexBuffer.h:507
FRawStaticIndexBuffer16or32(bool InNeedsCPUAccess=false)
Definition RawIndexBuffer.h:393
virtual void AssignNewBuffer(const TArray< INDEX_TYPE > &Buffer)
Definition RawIndexBuffer.h:525
virtual void SetMetaData(int32 InCachedNumIndices) override
Definition RawIndexBuffer.h:462
virtual void Insert(int32 Idx, int32 Num) override
Definition RawIndexBuffer.h:500
virtual void Empty(int32 Slack) override
Definition RawIndexBuffer.h:514
virtual bool GetNeedsCPUAccess() const override
Definition RawIndexBuffer.h:477
virtual uint32 Get(uint32 Idx) const override
Definition RawIndexBuffer.h:490
virtual int32 GetResourceDataSize() const override
Definition RawIndexBuffer.h:520
virtual void SerializeMetaData(FArchive &Ar) override
Definition RawIndexBuffer.h:457
Definition RawIndexBuffer.h:130
void ReleaseRHIForStreaming(FRHIResourceReplaceBatcher &Batcher)
Definition RawIndexBuffer.cpp:385
bool Is32Bit() const
Definition RawIndexBuffer.h:309
int32 GetIndexDataSize() const
Definition RawIndexBuffer.h:277
void ExpandTo32Bit()
Definition RawIndexBuffer.cpp:293
void InitRHIForStreaming(FRHIBuffer *IntermediateBuffer, FRHIResourceReplaceBatcher &Batcher)
Definition RawIndexBuffer.cpp:377
void SerializeMetaData(FArchive &Ar)
Definition RawIndexBuffer.cpp:439
uint32 GetIndex(const uint32 At) const
Definition RawIndexBuffer.h:196
virtual ENGINE_API ~FRawStaticIndexBuffer()
int32 GetNumIndices() const
Definition RawIndexBuffer.h:258
SIZE_T GetAllocatedSize() const
Definition RawIndexBuffer.h:266
void Discard()
Definition RawIndexBuffer.cpp:449
bool GetAllowCPUAccess() const
Definition RawIndexBuffer.h:271
void ClearMetaData()
Definition RawIndexBuffer.cpp:444
ENGINE_API void RemoveIndicesAt(const uint32 At, const uint32 NumIndicesToRemove)
Definition RawIndexBuffer.cpp:259
ENGINE_API void AppendIndices(const uint32 *IndicesToAppend, const uint32 NumIndicesToAppend)
Definition RawIndexBuffer.cpp:254
ENGINE_API const uint32 * AccessStream32() const
Definition RawIndexBuffer.cpp:324
void SetIndex(const uint32 At, const uint32 NewIndexValue)
Definition RawIndexBuffer.h:157
ENGINE_API void SetIndices(const TArray< uint32 > &InIndices, EIndexBufferStride::Type DesiredStride)
Definition RawIndexBuffer.cpp:172
bool TrySetAllowCPUAccess(bool bAllowCPUAccess)
Definition RawIndexBuffer.h:145
ENGINE_API const uint16 * AccessStream16() const
Definition RawIndexBuffer.cpp:315
ENGINE_API FIndexArrayView GetArrayView() const
Definition RawIndexBuffer.cpp:333
FBufferRHIRef CreateRHIBuffer(FRHICommandListBase &RHICmdList)
Definition RawIndexBuffer.cpp:339
ENGINE_API void GetCopy(TArray< uint32 > &OutIndices) const
Definition RawIndexBuffer.cpp:269
virtual ENGINE_API void InitRHI(FRHICommandListBase &RHICmdList) override
Definition RawIndexBuffer.cpp:393
ENGINE_API void InsertIndices(const uint32 At, const uint32 *IndicesToAppend, const uint32 NumIndicesToAppend)
Definition RawIndexBuffer.cpp:219
virtual void ReleaseRHI()
Definition RenderResource.h:90
FName GetOwnerName() const
Definition RenderResource.h:127
FBufferRHIRef CreateRHIBuffer(FRHICommandListBase &RHICmdList, T &InOutResourceObject, uint32 ResourceCount, EBufferUsageFlags InBufferUsageFlags, const TCHAR *InDebugName)
Definition RenderResource.h:160
Definition ResourceArray.h:77
Definition Array.h:670
UE_REWRITE SizeType Num() const
Definition Array.h:1144
UE_NODEBUG UE_FORCEINLINE_HINT ElementType * GetData() UE_LIFETIMEBOUND
Definition Array.h:1027
UE_NODEBUG UE_FORCEINLINE_HINT SIZE_T GetAllocatedSize(void) const
Definition Array.h:1059
UE_FORCEINLINE_HINT bool IsValid() const
Definition RefCounting.h:594
UE_FORCEINLINE_HINT void SafeRelease()
Definition RefCounting.h:599
Definition DynamicRHIResourceArray.h:31
virtual bool GetAllowCPUAccess() const override
Definition DynamicRHIResourceArray.h:101
virtual void SetAllowCPUAccess(bool bInNeedsCPUAccess) override
Definition DynamicRHIResourceArray.h:109
Definition RawIndexBuffer.h:75
@ false
Definition radaudio_common.h:23
static FBufferSRV::FInitializer CreateBufferSRV()
Definition RHIResources.h:3124