同一Direct2D工厂下的多个渲染目标

4

我需要使用Direct2D绘制许多形状。我使用工厂创建一个渲染目标来进行绘制。在这些形状之上,我需要添加其他形状而不改变之前的形状(手绘),但是如果我使用相同的渲染目标,我必须刷新整个画布(即重新绘制所有形状),这是不可行的,因为速度太慢。

我需要一种解决方案,可以让我在不连续清除和重绘整个画布的情况下,在静态形状上绘制。我考虑使用相同的工厂创建一个新的渲染目标,但这种解决方案对我不起作用(即新的形状无法在屏幕上显示)。

是否有解决此问题的方法?例如将静态形状绘制到位图上?


1
解决方案是将静态形状绘制到离屏位图上,并仅在形状更改时重新绘制它。在您的渲染函数中,您首先绘制离屏位图,然后在其上方绘制自由手绘图。您使用哪个渲染目标?hwnd 渲染目标吗? - Anton Angelov
@AntonAngelov 是的,我使用ID2D1HwndRenderTarget,你能提供一个针对这种渲染目标的解决方案吗? - Nick
1个回答

3
好的..这里是内容。我现在没有安装MS VS,所以我用Delphi写了一个简单的例子。函数名都是一样的,所以我希望你能理解它的意思。
这是我使用的变量:
FFactory: ID2D1Factory;             //ID2D1Factory* FFactory;
FHWNDRT: ID2D1HwndRenderTarget;     //ID2D1HwndRenderTarget* FHWNDRT;
FBitmapRT: ID2D1BitmapRenderTarget; //Etc..
FBrush: ID2D1SolidColorBrush;

这里是一个例子:

function TForm1.InitializeD2D: HRESULT;
var
  TargetRect: TRect;
  BrushProps: D2D1_BRUSH_PROPERTIES;
begin
  { Create factory }
  Result := D2D1CreateFactory( D2D1_FACTORY_TYPE_SINGLE_THREADED, ID2D1Factory, nil, FFactory );
  If Failed(Result) then Exit;

  { Get form's client rect }
  Winapi.Windows.GetClientRect( Self.Handle, targetRect );

  { Create hwnd render target }
  Result := FFactory.CreateHwndRenderTarget(
      D2D1RenderTargetProperties( D2D1_RENDER_TARGET_TYPE_HARDWARE, D2D1PixelFormat( DXGI_FORMAT_R8G8B8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED ), 96, 96, D2D1_RENDER_TARGET_USAGE_NONE, D2D1_FEATURE_LEVEL_10 ),
      D2D1HwndRenderTargetProperties( Self.Handle, D2D1SizeU( TargetRect.Width, TargetRect.Height ) ),
      FHWNDRT
  );
  If Failed(Result) then Exit;

  { Create bitmap render target }
  Result := FHWNDRT.CreateCompatibleRenderTarget(
      nil,
      nil,
      nil,
      D2D1_COMPATIBLE_RENDER_TARGET_OPTIONS_NONE,
      FBitmapRT
  );
  If Failed(Result) then Exit;

  With BrushProps do Begin
    opacity := 1;
    transform := D2D1_MATRIX_3X2_F.Identity;
  End;

  { Create brush so we can draw }
  Result := FBitmapRT.CreateSolidColorBrush( D2D1ColorF( 0, 0.25, 0.75, 1 ), @BrushProps, FBrush );
end;

function TForm1.UpdateStaticShapes: HRESULT;
begin
  //Draw the static shapes on the offscreen bitmap
  FBitmapRT.BeginDraw;

  //Clear the offscreen bitmap
  FBitmapRT.Clear( D2D1ColorF(0, 0, 0, 1) );

  //Draw a line
  FBrush.SetColor( D2D1ColorF( 1, 0, 0, 1 ) );
  FBitmapRT.DrawLine( D2D1PointF( Random(10)+10, Random(10)+10 ), D2D1PointF( 50, 50 ), FBrush );

  //Draw a filled rect
  FBrush.SetColor( D2D1ColorF( 0, 0.25, 0.75, 1 ) );
  FBitmapRT.FillRectangle( D2D1RectF( Random(50), Random(50), Random(250)+50, Random(300) + 50 ), FBrush );

  //Etc.. (draw all your shapes)

  Result := FBitmapRT.EndDraw();
end;

function TForm1.Render: HRESULT;
var
  pBitmap: ID2D1Bitmap;
begin
  FHWNDRT.BeginDraw;

  FHWNDRT.Clear( D2D1ColorF( 0, 0, 0, 1 ) );

  { Draw the offscreen bitmap }
  FBitmapRT.GetBitmap( pBitmap );

  If pBitmap <> nil then Begin
    FHWNDRT.DrawBitmap( pBitmap );
    pBitmap := nil; //Equivalent to _Release()
  End;

  { Draw the additional free hand drawing here }
  FBrush.SetColor( D2D1ColorF( 1, 1, 1, 1 ) );
  FHWNDRT.DrawRectangle( D2D1RectF( 100, 100, 200, 200 ), FBrush );

  Result := FHWNDRT.EndDraw();
end;

离屏位图在UpdateStaticShapes()方法中被重新绘制。最终的帧在Render()方法中被渲染。

编辑: 我尝试了缩放并理解了你的问题。我猜解决方案是每次改变缩放因子时(也每次调整窗口大小时)重新创建位图渲染目标。离屏位图的大小应为:

  OffscreenBitmap.Width/Height = HwndRT.Width/Height * ScaleFactor;

在绘制形状时,您必须使用相对于屏幕外位图大小的坐标。例如:不要绘制线(50,50, 100,100),而应该绘制(50*K, 50*K, 100*K, 100*K)其中K = ScaleFactor;

以下是我创建位图 RT 的方法:

//In C++ this should look like:
//HRESULT TForm::CreateBitmapRT(float ScaleFactor) {}
function TForm1.CreateBitmapRT(ScaleFactor: Single): HRESULT;
var
  DesiredSize: D2D1_POINT_2F;
begin
  FBitmapRT := nil; //FBitmapRT->_Release(); FBitmapRT = NULL;

  { Decide offscreen bitmap's size }
  DesiredSize := D2D1PointF( FWindowRect.Width * ScaleFactor, FWindowRect.Height * ScaleFactor );

  { Create bitmap render target }
  Result := FHWNDRT.CreateCompatibleRenderTarget(
      @DesiredSize,
      nil,
      nil,
      D2D1_COMPATIBLE_RENDER_TARGET_OPTIONS_NONE,
      FBitmapRT
  );
end;

这是更新后的Render()方法:

function TForm1.Render: HRESULT;
var
  pBitmap: ID2D1Bitmap;
  SrcRect, DestRect: D2D1_RECT_F;
begin
  FHWNDRT.BeginDraw;

  FHWNDRT.Clear( D2D1ColorF( 0, 0, 0, 1 ) );

  If FBitmapRT <> nil then Begin
    { Draw the offscreen bitmap }
    FBitmapRT.GetBitmap( pBitmap );

    If pBitmap <> nil then Begin
      SrcRect := D2D1RectF( FZoomOffset.x, FZoomOffset.y, FZoomOffset.x + FWindowRect.Width, FZoomOffset.y + FWindowRect.Height );
      DestRect := D2D1RectF( 0, 0, FWindowRect.Width, FWindowRect.Height );

      FHWNDRT.DrawBitmap( pBitmap, @DestRect, 1, D2D1_BITMAP_INTERPOLATION_MODE_LINEAR, @SrcRect );
      pBitmap := nil; //pBitmap->_Release(); pBitmap = NULL;
    End;
  End;

  { Draw the additional free hand drawing here }
  FBrush.SetColor( D2D1ColorF( 1, 1, 1, 1 ) );
  FHWNDRT.DrawRectangle( D2D1RectF( 100, 100, 200, 200 ), FBrush );

  Result := FHWNDRT.EndDraw();
end;

这是当我改变缩放比例(放大或缩小)时发生的情况:
function TForm1.ApplyZoom(fScaleFactor: Single): HRESULT;
begin
  If fScaleFactor = FZoomFactor then Exit(S_OK);    
  If fScaleFactor = 0 then fScaleFactor := 0.1;

  { Recreate the offscreen bitmap }
  Result := CreateBitmapRT( fScaleFactor );
  If Failed(Result) then Exit;

  { Here you have to redraw the shapes once again }  
  Result := UpdateStaticShapes;
  If Failed(Result) then Exit;

  { I save the new Zoom Factor in a class field }
  FZoomFactor := fScaleFactor;
end;

问题在于显示的位图与不使用位图渲染目标绘制的形状的反锯齿版本不同(当我使用缩放变换即缩放时)。有没有办法创建一个看起来像Windows截图的位图?也就是说,一个看起来像反锯齿版本的位图。 - Nick
这种效果是由于您的形状在位图上被光栅化造成的。解决方法是使屏幕外位图变大(2、4倍或更大),以便它可以容纳更详细的光栅。 - Anton Angelov
你的解决方案是有效的,但我有以下问题:https://dev59.com/nIjca4cB1Zd3GeqPvVg2。请随意回答。谢谢。 - Nick
我已经编辑了答案。它不是完美的,但应该可以胜任工作。此外,离屏位图的大小有限制,因此如果您的位图变得非常大,则必须将其分解为小型位图。我测试了16320x8644大小的位图,看起来还可以。另一个注意点是,如果您进行非常接近的缩放,可以放弃离屏位图并直接绘制形状。 - Anton Angelov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接