I’m struggling to create a new texture at runtime and not have it contain gabrage data. I have simplified my problem into the following few lines of code:
// Create texture and set to all black
UTexture2D* Tex = UTexture2D::CreateTransient(16, 16);
auto BulkData = Tex->GetPlatformData()->Mips[0].BulkData;
auto Data = BulkData.Lock(LOCK_READ_WRITE);
FMemory::Memset(Data, 0, 4*16*16); // Default format is 32-bit BGRA
int breakpoint = 0;
BulkData.Unlock();
Tex->UpdateResource();
// Read data back
auto BulkData2 = Tex->GetPlatformData()->Mips[0].BulkData;
auto Data2 = BulkData2.Lock(LOCK_READ_ONLY);
int breakpoint2 = 0;
BulkData2.Unlock();
As you can see I am writing all 0’s into the texture’s mip 0, expecting it to come out all black. Indeed, setting a breakpoint at int breakpoint
line I can inspect the memory at the address Data
and confirm that it is all 0’s.
However when I then try to read the data back from the texture and examine it in the debugger at line int breakpoint2
, the debugger shows that not only is the address of Data
not equal to Data2
as I would expect, but indeed Data2
is filled with random garbage! And indeed looking at the texture in the editor, it has a bunch of random colors and is definitelly not all black. What am I missing?
Unreal Engine 5.1.1 in case that is relevant in any way.
Running on main thread inside an actors BeginPlay method.