UTexture2D::UpdateTextureRegions has no effect

Hi!

I’m running UE4.18.1 PCD3D_SM5 on Win10 x64 Fall Creators Update (16299), GPU is GTX 750 Ti.

I develop yet another fog of war and have following code:

void AFogOfWar::BeginPlay()
{
    Super::BeginPlay();
    this->FogMapTexture = UTexture2D::CreateTransient(30, 60, EPixelFormat::PF_A8);
    FogMapTexture->UpdateResource();

    if (this->TargetVolume != nullptr)
    {

        UMaterialInstanceDynamic* MaterialInstance = UMaterialInstanceDynamic::Create(this->PostProcessMaterial, this);
        if (MaterialInstance != nullptr)
        {
            MaterialInstance->SetTextureParameterValue(TEXT("FogMapTexture"), FogMapTexture);
            TargetVolume->AddOrUpdateBlendable(MaterialInstance, 1.f);
        }

    }

}

void AFogOfWar::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);

    static struct FPayload {
        uint8 Data[30 * 60];
        FPayload() { memset(Data, 255, 30 * 60); }
    } Payload;
    
    static FUpdateTextureRegion2D region(0, 0, 0, 0, 30, 60);
    FogMapTexture->UpdateTextureRegions(0, 1, &region, 30, 1, Payload.Data);
    FogMapTexture->UpdateResource();
}

The problem is, UpdateTextureRegions has no effect on FogMapTexture, but direct modification of texture mip’s BulkData works nice although is not efficient:

void AFogOfWar::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);

    void* TextureData = FogMapTexture->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);

    for (uint32 iY = 0; iY < 60; ++iY)
    {
        FMemory::Memset((uint8*)TextureData + (iY * 30), 255, 30);
    }

    FogMapTexture->PlatformData->Mips[0].BulkData.Unlock();
    FogMapTexture->UpdateResource();
}

I came up with similar questions on AnswerHub and forums, but no proposed solutions seems working for me. How to use UpdateTextureRegions properly?

Finally found the solution and related pitfall on Setting Up a Dynamic Texture - #4 by anonymous_user_4827d1c0 - C++ - Epic Developer Community Forums

Before updating the regions, we must make sure the resource exists and is updated. I call UpdateResource() right after UTexture2D::CreateTransient as seen above, but UTexture2D::UpdateResource has no effect within AActor::BeginPlay!

We must UpdateResource within AActor::Tick, before first UpdateTextureRegions. This way the texture is created and initialized properly and we can modify/update it further.