FCanvas and UTextureRenderTarget2D: Draw functions not working


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);
    FTextureRenderTarget2DResource* gridTexResource = (FTextureRenderTarget2DResource*)gridTexture->Resource;
    gridCanvas = new FCanvas(gridTexResource, NULL, 0, 0, 0, ERHIFeatureLevel::ES2);
    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.BlendMode = SE_BLEND_Additive;
    	FCanvasLineItem line(FVector2D(0, 0), FVector2D(1024, 1024));
    	line.LineThickness = 50;
    	line.BlendMode = SE_BLEND_Opaque;
    	FCanvasLineItem line2(FVector2D(0, 1024), FVector2D(1024, 0));
    	line2.LineThickness = 2;
    	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);


add the following code at the end of UpdateField():

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