FCanvas and UTextureRenderTarget2D: Draw functions not working

Greetings.

I have rigged up a FCanvas with UTextureRenderTarget2D to draw texture in realtime via code. Render target is applied to dynamic material instance. Problem is: canvas->clear function is working(can change clear color at any moment), but drawing functions(DrawItem, DrawNGon) produce no visual result.

Canvas and RenderTarget initialization:

 material = UMaterialInstanceDynamic::Create(gridMesh->GetMaterial(0), GetWorld());
    gridMesh->SetMaterial(0, material);
    
    gridTexture = (UTextureRenderTarget2D*)StaticConstructObject(UTextureRenderTarget2D::StaticClass());
    gridTexture->bNeedsTwoCopies = false;
    gridTexture->bHDR = false;
    gridTexture->ClearColor = FLinearColor::Black;
    gridTexture->InitAutoFormat(1024, 1024);
    gridTexture->UpdateResourceImmediate();
    
    FTextureRenderTarget2DResource* gridTexResource = (FTextureRenderTarget2DResource*)gridTexture->Resource;
    gridCanvas = new FCanvas(gridTexResource, NULL, 0, 0, 0, ERHIFeatureLevel::ES2);
    gridCanvas->Clear(FLinearColor::Black);
    gridTexture->UpdateResourceImmediate(false);
    material->SetTextureParameterValue(TEXT("TTarget2D"), gridTexture);

And drawing:

 void ASoliField::UpdateField(){
    
    	gridCanvas->Clear(FLinearColor(0.5,0.5,0.5,1)); //Works fine
    
    	FCanvasBoxItem box(FVector2D(512, 512), FVector2D(256, 256));
    	box.LineThickness = 10;
    	box.SetColor(FLinearColor::Red);
    	box.BlendMode = SE_BLEND_Additive;
    
    	FCanvasLineItem line(FVector2D(0, 0), FVector2D(1024, 1024));
    	line.LineThickness = 50;
    	line.SetColor(FLinearColor::Green);
    	line.BlendMode = SE_BLEND_Opaque;
    
    	FCanvasLineItem line2(FVector2D(0, 1024), FVector2D(1024, 0));
    	line2.LineThickness = 2;
    	line2.SetColor(FLinearColor::Blue);
    	line2.BlendMode = SE_BLEND_Translucent;
    
    	gridCanvas->DrawItem(box); //Doesnt draw
    	gridCanvas->DrawItem(line);  //Doesnt draw
    	gridCanvas->DrawItem(line2);  //Doesnt draw
    
    }

Any insight on this problem will be appreciated. Thank you.

You should change your init code from :
gridCanvas = new FCanvas(gridTexResource, NULL, 0, 0, 0, ERHIFeatureLevel::ES2);
to :
gridCanvas = new FCanvas(gridTexResource, NULL, 0, 0, 0, GMaxRHIFeatureLevel);

AND

add the following code at the end of UpdateField():
gridCanvas->Flush_GameThread();

I had the same problem as you, now it works.

My great thanks to you. The root of this problem was in wrong feature level, because with ES2 enqueuing draws onto rendering thread with Flush_GameThread() caused crash. Setting feature level to auto(GMaxRHIFeatureLevel) solved this problem