Ignore:
Timestamp:
Apr 17, 2019, 12:58:41 AM (5 years ago)
Author:
chronos
Message:
  • Modified: Propagate project build mode options to used packages.
  • Added: Check memory leaks using heaptrc.
  • Modified: Update BGRABitmap package.
Location:
GraphicTest
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • GraphicTest

    • Property svn:ignore
      •  

        old new  
        88GraphicTest.lps
        99GraphicTest.dbg
         10heaptrclog.trc
  • GraphicTest/Packages/bgrabitmap/bgraopengl.pas

    r494 r521  
    99  Classes, SysUtils, FPimage, BGRAGraphics,
    1010  BGRAOpenGLType, BGRASpriteGL, BGRACanvasGL, GL, GLext, GLU, BGRABitmapTypes,
    11   BGRAFontGL, BGRASSE;
     11  BGRAFontGL, BGRASSE, BGRAMatrix3D;
    1212
    1313type
     
    4343    property Width: integer read GetWidth;
    4444    property Height: integer read GetHeight;
     45  end;
     46
     47  { TBGLFrameBuffer }
     48
     49  TBGLFrameBuffer = class(TBGLCustomFrameBuffer)
     50  protected
     51    FHeight: integer;
     52    FMatrix: TAffineMatrix;
     53    FProjectionMatrix: TMatrix4D;
     54    FTexture: IBGLTexture;
     55    FFrameBufferId, FRenderBufferId: GLuint;
     56    FWidth: integer;
     57    FSettingMatrices: boolean;
     58    function GetTexture: IBGLTexture; override;
     59    function GetHandle: pointer; override;
     60    function GetHeight: integer; override;
     61    function GetMatrix: TAffineMatrix; override;
     62    function GetProjectionMatrix: TMatrix4D; override;
     63    function GetWidth: integer; override;
     64    procedure SetMatrix(AValue: TAffineMatrix); override;
     65    procedure SetProjectionMatrix(AValue: TMatrix4D); override;
     66  public
     67    constructor Create(AWidth,AHeight: integer);
     68    function MakeTextureAndFree: IBGLTexture; override;
     69    destructor Destroy; override;
    4570  end;
    4671
     
    120145implementation
    121146
    122 uses BGRATransform{$IFDEF BGRABITMAP_USE_LCL}, BGRAText, BGRATextFX{$ENDIF}
    123     ,BGRAMatrix3D;
     147uses BGRABlurGL, BGRATransform{$IFDEF BGRABITMAP_USE_LCL}, BGRAText, BGRATextFX{$ENDIF};
    124148
    125149type
     
    210234    procedure ToggleFlipY; override;
    211235    procedure Bind(ATextureNumber: integer); override;
     236    function FilterBlurMotion(ARadius: single; ABlurType: TRadialBlurType; ADirection: TPointF): IBGLTexture; override;
     237    function FilterBlurRadial(ARadius: single; ABlurType: TRadialBlurType): IBGLTexture; override;
    212238
    213239  end;
     
    247273    procedure InternalStartPolygon(const pt: TPointF); override;
    248274    procedure InternalStartTriangleFan(const pt: TPointF); override;
    249     procedure InternalContinueShape(const pt: TPointF); override;
    250 
    251     procedure InternalContinueShape(const pt: TPoint3D); override;
    252     procedure InternalContinueShape(const pt: TPoint3D_128); override;
    253     procedure InternalContinueShape(const pt, normal: TPoint3D_128); override;
     275    procedure InternalContinueShape(const pt: TPointF); overload; override;
     276
     277    procedure InternalContinueShape(const pt: TPoint3D); overload; override;
     278    procedure InternalContinueShape(const pt: TPoint3D_128); overload; override;
     279    procedure InternalContinueShape(const pt, normal: TPoint3D_128); overload; override;
    254280
    255281    procedure InternalEndShape; override;
     
    268294    function GetBlendMode: TOpenGLBlendMode; override;
    269295    procedure SetBlendMode(AValue: TOpenGLBlendMode); override;
     296
     297    procedure SetActiveFrameBuffer(AValue: TBGLCustomFrameBuffer); override;
    270298  public
    271299    destructor Destroy; override;
     
    274302    procedure EndZBuffer; override;
    275303    procedure WaitForGPU(AOption: TWaitForGPUOption); override;
     304    function GetImage(x, y, w, h: integer): TBGRACustomBitmap; override;
     305    function CreateFrameBuffer(AWidth, AHeight: integer): TBGLCustomFrameBuffer; override;
    276306  end;
    277307
     
    307337    function GetUniformVariable(AProgram: DWord; AName: string): DWord; override;
    308338    function GetAttribVariable(AProgram: DWord; AName: string): DWord; override;
    309     procedure SetUniformSingle(AVariable: DWord; const AValue; ACount: integer); override;
    310     procedure SetUniformInteger(AVariable: DWord; const AValue; ACount: integer); override;
     339    procedure SetUniformSingle(AVariable: DWord; const AValue; AElementCount, AComponentCount: integer); override;
     340    procedure SetUniformInteger(AVariable: DWord; const AValue; AElementCount, AComponentCount: integer); override;
    311341    procedure BindAttribute(AAttribute: TAttributeVariable); override;
    312342    procedure UnbindAttribute(AAttribute: TAttributeVariable); override;
    313343  end;
     344
     345{ TBGLFrameBuffer }
     346
     347procedure TBGLFrameBuffer.SetMatrix(AValue: TAffineMatrix);
     348begin
     349  if FSettingMatrices then Exit;
     350  FSettingMatrices := true;
     351  FMatrix:=AValue;
     352  if FCanvas <> nil then
     353    TBGLCustomCanvas(FCanvas).Matrix := AValue;
     354  FSettingMatrices := false;
     355end;
     356
     357function TBGLFrameBuffer.GetMatrix: TAffineMatrix;
     358begin
     359  result := FMatrix;
     360end;
     361
     362function TBGLFrameBuffer.GetTexture: IBGLTexture;
     363begin
     364  result := FTexture.FlipY;
     365end;
     366
     367function TBGLFrameBuffer.GetHandle: pointer;
     368begin
     369  result := @FFrameBufferId;
     370end;
     371
     372function TBGLFrameBuffer.GetHeight: integer;
     373begin
     374  result := FHeight;
     375end;
     376
     377function TBGLFrameBuffer.GetProjectionMatrix: TMatrix4D;
     378begin
     379  result := FProjectionMatrix;
     380end;
     381
     382function TBGLFrameBuffer.GetWidth: integer;
     383begin
     384  result := FWidth;
     385end;
     386
     387procedure TBGLFrameBuffer.SetProjectionMatrix(AValue: TMatrix4D);
     388begin
     389  if FSettingMatrices then Exit;
     390  FSettingMatrices := true;
     391  FProjectionMatrix:= AValue;
     392  if FCanvas <> nil then
     393    TBGLCustomCanvas(FCanvas).ProjectionMatrix := AValue;
     394  FSettingMatrices := false;
     395end;
     396
     397constructor TBGLFrameBuffer.Create(AWidth, AHeight: integer);
     398var frameBufferStatus: GLenum;
     399begin
     400  if not Load_GL_version_3_0 then
     401      raise exception.Create('Cannot load OpenGL 3.0');
     402
     403  FWidth := AWidth;
     404  FHeight := AHeight;
     405
     406  FTexture := BGLTextureFactory.Create(nil,AWidth,AHeight,AWidth,AHeight);
     407
     408  //depth and stencil
     409  glGenRenderbuffers(1, @FRenderBufferId);
     410  glBindRenderbuffer(GL_RENDERBUFFER, FRenderBufferId);
     411  glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, AWidth,AHeight);
     412  glBindRenderbuffer(GL_RENDERBUFFER, 0);
     413
     414  glGenFramebuffers(1, @FFrameBufferId);
     415  glBindFramebuffer(GL_FRAMEBUFFER, FFrameBufferId);
     416
     417  glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, PGLuint(FTexture.Handle)^, 0);
     418  glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, FFrameBufferId);
     419
     420  frameBufferStatus:= glCheckFramebufferStatus(GL_FRAMEBUFFER);
     421  glBindFramebuffer(GL_FRAMEBUFFER, 0);
     422
     423  if frameBufferStatus <> GL_FRAMEBUFFER_COMPLETE then
     424  begin
     425    glDeleteFramebuffers(1, @FFrameBufferId);
     426    glDeleteRenderbuffers(1, @FRenderBufferId);
     427    FTexture := nil;
     428    raise exception.Create('Error ' + inttostr(frameBufferStatus) + ' while initializing frame buffer');
     429  end;
     430
     431  UseOrthoProjection;
     432  Matrix := AffineMatrixIdentity;
     433end;
     434
     435function TBGLFrameBuffer.MakeTextureAndFree: IBGLTexture;
     436begin
     437  result := FTexture;
     438  FTexture := nil;
     439  Free;
     440end;
     441
     442destructor TBGLFrameBuffer.Destroy;
     443begin
     444  glDeleteFramebuffers(1, @FFrameBufferId);
     445  glDeleteRenderbuffers(1, @FRenderBufferId);
     446  FTexture := nil;
     447
     448  inherited Destroy;
     449end;
    314450
    315451procedure ApplyBlendMode(ABlendMode: TOpenGLBlendMode);
     
    776912
    777913procedure TBGLLighting.SetUniformSingle(AVariable: DWord;
    778   const AValue; ACount: integer);
     914  const AValue; AElementCount, AComponentCount: integer);
    779915begin
    780916  NeedOpenGL2_0;
    781   glUniform1fv(AVariable, ACount, @AValue);
     917  case AComponentCount of
     918    1: glUniform1fv(AVariable, AElementCount, @AValue);
     919    2: glUniform2fv(AVariable, AElementCount, @AValue);
     920    3: glUniform3fv(AVariable, AElementCount, @AValue);
     921    4: glUniform4fv(AVariable, AElementCount, @AValue);
     922    9: glUniformMatrix3fv(AVariable, AElementCount, GL_FALSE, @AValue);
     923    16: glUniformMatrix4fv(AVariable, AElementCount, GL_FALSE, @AValue);
     924  else
     925    raise exception.Create('Unexpected number of components');
     926  end;
    782927end;
    783928
    784929procedure TBGLLighting.SetUniformInteger(AVariable: DWord;
    785   const AValue; ACount: integer);
     930  const AValue; AElementCount, AComponentCount: integer);
    786931begin
    787932  NeedOpenGL2_0;
    788   glUniform1iv(AVariable, ACount, @AValue);
     933  case AComponentCount of
     934    1: glUniform1iv(AVariable, AElementCount, @AValue);
     935    2: glUniform2iv(AVariable, AElementCount, @AValue);
     936    3: glUniform3iv(AVariable, AElementCount, @AValue);
     937    4: glUniform4iv(AVariable, AElementCount, @AValue);
     938  else
     939    raise exception.Create('Unexpected number of components');
     940  end;
    789941end;
    790942
     
    8491001function TBGLCanvas.GetMatrix: TAffineMatrix;
    8501002begin
    851   result := FMatrix;
     1003  if ActiveFrameBuffer <> nil then
     1004    result := ActiveFrameBuffer.Matrix
     1005  else
     1006    result := FMatrix;
    8521007end;
    8531008
     
    8581013  m := AffineMatrixToMatrix4D(AValue);
    8591014  glLoadMatrixf(@m);
    860   FMatrix := AValue;
     1015
     1016  if ActiveFrameBuffer <> nil then
     1017    ActiveFrameBuffer.Matrix := AValue
     1018  else
     1019    FMatrix := AValue;
    8611020end;
    8621021
    8631022function TBGLCanvas.GetProjectionMatrix: TMatrix4D;
    8641023begin
    865   result := FProjectionMatrix;
     1024  if ActiveFrameBuffer <> nil then
     1025    result := ActiveFrameBuffer.ProjectionMatrix
     1026  else
     1027    result := FProjectionMatrix;
    8661028end;
    8671029
    8681030procedure TBGLCanvas.SetProjectionMatrix(const AValue: TMatrix4D);
    8691031begin
    870   FProjectionMatrix := AValue;
    8711032  glMatrixMode(GL_PROJECTION);
    8721033  glLoadMatrixf(@AValue);
    8731034  glMatrixMode(GL_MODELVIEW);
     1035
     1036  if ActiveFrameBuffer <> nil then
     1037    ActiveFrameBuffer.ProjectionMatrix := AValue
     1038  else
     1039    FProjectionMatrix := AValue;
    8741040end;
    8751041
     
    10151181end;
    10161182
     1183function TBGLCanvas.GetImage(x, y, w, h: integer): TBGRACustomBitmap;
     1184begin
     1185  NeedOpenGL2_0;
     1186  result := BGRABitmapFactory.Create(w,h);
     1187  if TBGRAPixel_RGBAOrder then
     1188    glReadPixels(x,self.Height-y-h, w,h, GL_RGBA, GL_UNSIGNED_BYTE, result.Data)
     1189  else
     1190    glReadPixels(x,self.Height-y-h, w,h, GL_BGRA, GL_UNSIGNED_BYTE, result.Data);
     1191end;
     1192
     1193function TBGLCanvas.CreateFrameBuffer(AWidth, AHeight: integer): TBGLCustomFrameBuffer;
     1194begin
     1195  Result:= TBGLFrameBuffer.Create(AWidth,AHeight);
     1196end;
     1197
    10171198procedure TBGLCanvas.EnableScissor(AValue: TRect);
    10181199begin
     
    10341215begin
    10351216  FBlendMode := AValue;
     1217end;
     1218
     1219procedure TBGLCanvas.SetActiveFrameBuffer(AValue: TBGLCustomFrameBuffer);
     1220var
     1221  m: TMatrix4D;
     1222begin
     1223  if AValue = ActiveFrameBuffer then exit;
     1224  inherited SetActiveFrameBuffer(AValue);
     1225  if AValue = nil then
     1226    glBindFramebuffer(GL_FRAMEBUFFER, 0)
     1227  else
     1228    glBindFramebuffer(GL_FRAMEBUFFER, PGLuint(AValue.Handle)^);
     1229
     1230  glViewPort(0,0,Width,Height);
     1231
     1232  glMatrixMode(GL_PROJECTION);
     1233  m := ProjectionMatrix;
     1234  glLoadMatrixf(@m);
     1235
     1236  glMatrixMode(GL_MODELVIEW);
     1237  m := AffineMatrixToMatrix4D(Matrix);
     1238  glLoadMatrixf(@m);
    10361239end;
    10371240
     
    13771580procedure TBGLTexture.ToggleFlipY;
    13781581begin
    1379   FFlipX := not FFlipY;
     1582  FFlipY := not FFlipY;
    13801583end;
    13811584
     
    13951598end;
    13961599
     1600function TBGLTexture.FilterBlurMotion(ARadius: single; ABlurType: TRadialBlurType; ADirection: TPointF): IBGLTexture;
     1601var shader: TBGLCustomShader;
     1602  blurName: string;
     1603begin
     1604  blurName := 'TBGLBlurShader(' + RadialBlurTypeToStr[ABlurType] + ')';
     1605  shader := BGLCanvas.Lighting.Shader[blurName];
     1606  if shader = nil then
     1607  begin
     1608    shader := TBGLBlurShader.Create(BGLCanvas, ABlurType);
     1609    BGLCanvas.Lighting.Shader[blurName] := shader;
     1610  end;
     1611  with (shader as TBGLBlurShader) do
     1612  begin
     1613    Radius := ARadius;
     1614    Direction := ADirection;
     1615    result := FilterBlurMotion(self);
     1616  end;
     1617end;
     1618
     1619function TBGLTexture.FilterBlurRadial(ARadius: single; ABlurType: TRadialBlurType): IBGLTexture;
     1620var shader: TBGLCustomShader;
     1621  blurName: String;
     1622begin
     1623  blurName := 'TBGLBlurShader(' + RadialBlurTypeToStr[ABlurType] + ')';
     1624  shader := BGLCanvas.Lighting.Shader[blurName];
     1625  if shader = nil then
     1626  begin
     1627    shader := TBGLBlurShader.Create(BGLCanvas, ABlurType);
     1628    BGLCanvas.Lighting.Shader[blurName] := shader;
     1629  end;
     1630  with (shader as TBGLBlurShader) do
     1631  begin
     1632    Radius := ARadius;
     1633    result := FilterBlurRadial(self);
     1634  end;
     1635end;
     1636
    13971637procedure TBGLTexture.Init(ATexture: TBGLTextureHandle; AWidth,
    13981638  AHeight: integer; AOwned: boolean);
Note: See TracChangeset for help on using the changeset viewer.