给定同一个 Direct2D 工厂的多个渲染目标
Multiple render targets given the same Direct2D factory
我必须使用 Direct2D 绘制很多形状。我使用工厂来创建我用来绘制的渲染目标。在这些形状之上我需要在不改变之前的情况下添加其他形状(徒手画),但是如果我使用相同的渲染目标我必须刷新整个canvas(即重绘所有形状),这是不可行,因为它太慢了。
我需要我的解决方案,允许我在 static 形状上绘制,而无需连续清除绘制所有 canvas。我想通过使用相同的工厂创建一个新的渲染目标,但这个解决方案对我不起作用(即新形状不显示在屏幕上)。
是否有可以解决此问题的解决方案?例如在位图上绘制 static 形状?
好的..就在这里。我现在没有安装 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() 方法中渲染。
编辑:
我尝试进行缩放并了解您的问题。我想解决方案是每次更改缩放系数时(以及每次调整 window 大小时)都重新创建位图渲染目标。离屏位图的大小应该是:
OffscreenBitmap.Width/Height = HwndRT.Width/Height * ScaleFactor;
绘制形状时,必须使用相对于屏幕外位图大小的坐标。例如:而不是画线 (50,50, 100,100),你应该画它 (50*K, 50*K, 100*K, 100*K ) 其中 K = 比例因子;
以下是我创建位图 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;
我必须使用 Direct2D 绘制很多形状。我使用工厂来创建我用来绘制的渲染目标。在这些形状之上我需要在不改变之前的情况下添加其他形状(徒手画),但是如果我使用相同的渲染目标我必须刷新整个canvas(即重绘所有形状),这是不可行,因为它太慢了。
我需要我的解决方案,允许我在 static 形状上绘制,而无需连续清除绘制所有 canvas。我想通过使用相同的工厂创建一个新的渲染目标,但这个解决方案对我不起作用(即新形状不显示在屏幕上)。
是否有可以解决此问题的解决方案?例如在位图上绘制 static 形状?
好的..就在这里。我现在没有安装 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() 方法中渲染。
编辑: 我尝试进行缩放并了解您的问题。我想解决方案是每次更改缩放系数时(以及每次调整 window 大小时)都重新创建位图渲染目标。离屏位图的大小应该是:
OffscreenBitmap.Width/Height = HwndRT.Width/Height * ScaleFactor;
绘制形状时,必须使用相对于屏幕外位图大小的坐标。例如:而不是画线 (50,50, 100,100),你应该画它 (50*K, 50*K, 100*K, 100*K ) 其中 K = 比例因子;
以下是我创建位图 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;