Debian Codegraph @

src/base_rendering/castleglutils_vertex_array_object.inc

raw
1 {%MainUnit castleglutils.pas}
2 {
3 Copyright 2023-2023 Michalis Kamburelis.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 ----------------------------------------------------------------------------
15 }
16
17 {$ifdef read_interface}
18
19 type
20 { Vertex array object.
21 Wrapper over VAO, as defined by OpenGL >= 3.
22
23 Note that you can use this from multiple OpenGL(ES) contexts,
24 it will automatically create and destroy VAOs specific to the current context.
25 (As VAOs are not shared between OpenGL(ES) contexts,
26 see https://stackoverflow.com/questions/55885139/what-is-shareable-between-opengl-contexts-and-how-to-enable-sharing .)
27
28 This does nothing if current OpenGL(ES) version doesn't support VAO.
29
30 Change the current VAO by @link(TRenderContext.CurrentVao RenderContext.CurrentVao).
31 It is done done automatically by @link(TGLSLAttribute.EnableArray),
32 remember that VBOs (including GL_ELEMENT_ARRAY_BUFFER) must be bound within this VAO. }
33 TVertexArrayObject = class(TComponent)
34 strict private
35 type
36 TConvextVaoDictionary = {$ifdef FPC}specialize{$endif} TDictionary<TComponent, TGLuint>;
37 var
38 ConvextsVao: TConvextVaoDictionary;
39 protected
40 procedure Notification(AComponent: TComponent; Operation: TOperation); override;
41 public
42 constructor Create(AOwner: TComponent); override;
43 destructor Destroy; override;
44 { OpenGL VAO identifier, for given context (must be @link(TRenderContext)). }
45 function InternalHandle(const Context: TComponent): TGLuint;
46 end;
47
48 {$endif read_interface}
49
50 {$ifdef read_implementation}
51
52 constructor TVertexArrayObject.Create(AOwner: TComponent);
53 begin
54 inherited;
55 ConvextsVao := TConvextVaoDictionary.Create;
56 end;
57
58 destructor TVertexArrayObject.Destroy;
59 var
60 Pair: {$ifdef FPC}TConvextVaoDictionary.TDictionaryPair{$else}
61 TPair<TComponent, TGLuint>{$endif};
62 begin
63 if ConvextsVao <> nil then
64 begin
65 for Pair in ConvextsVao do
66 begin
67 Pair.Key.RemoveFreeNotification(Self);
68 if GLFeatures.VertexArrayObject then
69 begin
70 Assert(Pair.Value <> 0);
71 glDeleteVertexArrays(1, @Pair.Value);
72 end;
73 end;
74 FreeAndNil(ConvextsVao);
75 end;
76 inherited;
77 end;
78
79 procedure TVertexArrayObject.Notification(AComponent: TComponent; Operation: TOperation);
80 var
81 Vao: TGLuint;
82 begin
83 inherited;
84
85 { When TRenderContext is freed, we must free VAOs associated with it. }
86 if Operation = opRemove then
87 if ConvextsVao.TryGetValue(AComponent, Vao) then
88 begin
89 glFreeVertexArray(Vao);
90 ConvextsVao.Remove(AComponent);
91 end;
92 end;
93
94 function TVertexArrayObject.InternalHandle(const Context: TComponent): TGLuint;
95 begin
96 Assert(Context is TRenderContext);
97
98 if not ConvextsVao.TryGetValue(Context, Result) then
99 begin
100 if GLFeatures.VertexArrayObject then
101 glGenVertexArrays(1, @Result)
102 else
103 Result := 0;
104 Context.FreeNotification(Self);
105 ConvextsVao.Add(Context, Result);
106 end;
107 end;
108
109 {$endif read_implementation}