KiCad PCB EDA Suite
Loading...
Searching...
No Matches
gal_modern.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef GAL_MODERN_H
21#define GAL_MODERN_H
22
24#include <gal/definitions.h>
25#include <rhi/rhi_handle.h>
26#include <rhi/rhi_instance.h>
27#include <rhi/frame_packet.h>
28
29#include <array>
30#include <map>
31#include <memory>
32#include <optional>
33#include <unordered_map>
34#include <vector>
35
36class SHAPE_ELLIPSE;
37
38namespace KIGFX
39{
40
41class RENDER_THREAD;
43
49class GAL_API GAL_MODERN : public GAL
50{
51public:
52 GAL_MODERN( GAL_DISPLAY_OPTIONS& aOptions, RHI_DEVICE_PROXY& aDeviceProxy );
53 ~GAL_MODERN() override;
54
55 bool IsInitialized() const override { return m_rhiInitialized; }
56 bool IsDrawingMode() const override { return m_isDrawing; }
57 bool HasStaticRebuildPending() const override { return m_staticDirty; }
58 bool HasRetainedScene() const override { return true; }
59 bool RefillsNonCachedTargets() const override { return !m_persistentTargets; }
60 bool SupportsTypedPads() const override { return true; }
61 bool UsesPolygonTriangulationCache() const override { return true; }
62
63 void BeginDrawing() override;
64 void EndDrawing() override;
65
66 void ResizeScreen( int aWidth, int aHeight ) override;
67 void Flush() override;
68 void ClearScreen() override;
69
70 void SetTarget( RENDER_TARGET aTarget ) override;
71 RENDER_TARGET GetTarget() const override;
72 void ClearTarget( RENDER_TARGET aTarget ) override;
73
74 void StartDiffLayer() override;
75 void EndDiffLayer() override;
76 void StartNegativesLayer() override;
77 void EndNegativesLayer() override;
78 void SetNegativeDrawMode( bool aSetting ) override;
79 void SetScreenSpaceMode( bool aEnabled ) override { m_screenSpaceMode = aEnabled; }
80
81 bool SupportsStrokePixelPad() const override { return true; }
82
83 bool SupportsStrokeLengthPad() const override { return true; }
84
85 void SetStrokeLengthPad( double aPixels ) override { m_strokeLengthPad = static_cast<float>( aPixels ); }
86
87 void SetStrokePixelPad( double aPixels ) override
88 {
89 m_strokePixelPad = static_cast<uint16_t>( std::clamp( std::lround( aPixels * 16.0 ), 0L, 65535L ) );
90 }
91
92 bool SupportsStrokeLevelOfDetail() const override { return true; }
93
94 void SetStrokeLevelOfDetail( double aLod ) override { m_strokeLod = aLod; }
95
96 void BitmapText( const wxString& aText, const VECTOR2I& aPosition, const EDA_ANGLE& aAngle ) override;
97
99 void SetBitmapGlyphs( bool aEnable ) { m_bitmapGlyphs = aEnable; }
100
101 void DrawGrid() override;
102 void DrawCursor( const VECTOR2D& aCursorPosition ) override;
103
104 int BeginGroup() override;
105 void EndGroup() override;
106 std::array<uint64_t, 2> GroupContentFingerprint( int aGroupNumber ) const override;
107 void DrawGroup( int aGroupNumber ) override;
108 void ChangeGroupColor( int aGroupNumber, const COLOR4D& aNewColor ) override;
109 void ChangeGroupDepth( int aGroupNumber, int aDepth ) override;
110 void ChangeGroupUndimmed( int aGroupNumber, bool aUndimmed ) override;
111 void DeleteGroup( int aGroupNumber ) override;
112 void ClearCache() override;
113
114 void SetSceneLayerState( int aLayer, int aOrder, bool aVisible, bool aHasNegatives ) override;
115 void SetSceneLayerDim( int aLayer, const COLOR4D& aToward, double aKeep ) override;
116 void SetSceneLayerOverlayAlpha( int aLayer ) override;
117 void SetSceneDrawOrdered( bool aOrdered ) override;
118 void BeginSceneLayer( int aLayer ) override;
119 void SetGroupPlacement( int aGroup, int aLayer, double aLodScale, bool aShown,
120 const RETAINED_GROUP_ORDER& aOrder ) override;
121 void SetSceneScale( double aScale ) override;
122
129 bool SyncScene();
130
131 void Save() override;
132 void Restore() override;
133 void Transform( const MATRIX3x3D& aTransformation ) override;
134 void Rotate( double aAngle ) override;
135 void Translate( const VECTOR2D& aTranslation ) override;
136 void Scale( const VECTOR2D& aScale ) override;
137
138 void SetLayerDepth( double aLayerDepth ) override;
139
140 int GetSwapInterval() const override;
141
143 void SetRenderThread( RENDER_THREAD* aThread ) { m_renderThread = aThread; }
144
149 void SetDpiScale( float aScale ) { m_dpiScale = aScale; }
150
155 void SetCentringPad( int aPad ) { m_centringPad = aPad; }
156
158 {
160 }
161
169 static std::shared_ptr<RENDER_THREAD> AcquireRenderThread( bool aEnableValidation );
170
172 static uint32_t AllocateWindowId();
173
178 void DestroyBuffers();
179
182 {
183 auto release = []( GEOMETRY_BUFFER_SET& bs )
184 {
185 bs.buffer.fill( {} );
186 bs.capacity.fill( 0 );
187
188 for( auto& bq : bs.bitmapQuads )
189 {
190 bq.texture = {};
191 bq.vertexBuffer = {};
192 }
193 };
194
195 for( auto& bs : m_bufferSets )
196 release( bs );
197
198 for( auto& bs : m_overlaySets )
199 release( bs );
200
201 release( m_staticPool );
202 release( m_gridPool );
203
204 for( auto& [id, layer] : m_sceneLayers )
205 {
206 for( SCENE_ARENA<RHI_INSTANCE>& arena : layer.instances )
207 {
208 arena.buffer = {};
209 arena.capacity = 0;
210 }
211
212 layer.polygons.buffer = {};
213 layer.polygons.capacity = 0;
214 }
215
216 m_retiredBuffers.clear();
217 m_retiredTextures.clear();
218 m_bitmapTextureCache.clear();
219 }
220
221 void DrawLine( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint ) override;
222 void DrawSegment( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint,
223 double aWidth ) override;
224 void DrawTrack( const VECTOR2D& aStart, const VECTOR2D& aEnd,
225 double aWidth, const COLOR4D& aColor ) override;
226 void DrawPad( const VECTOR2D& aCenter, double aHalfWidth, double aHalfHeight,
227 double aCornerRadius, float aShapeType, double aRotation,
228 const COLOR4D& aColor ) override;
229 void DrawSegmentChain( const std::vector<VECTOR2D>& aPointList, double aWidth ) override;
230 void DrawSegmentChain( const SHAPE_LINE_CHAIN& aLineChain, double aWidth ) override;
231
232 void DrawArc( const VECTOR2D& aCenterPoint, double aRadius,
233 const EDA_ANGLE& aStartAngle, const EDA_ANGLE& aAngle ) override;
234 void DrawArcSegment( const VECTOR2D& aCenterPoint, double aRadius,
235 const EDA_ANGLE& aStartAngle, const EDA_ANGLE& aAngle,
236 double aWidth, double aMaxError ) override;
237
238 void DrawCircle( const VECTOR2D& aCenterPoint, double aRadius ) override;
239 void DrawEllipse( const VECTOR2D& aCenterPoint, double aMajorRadius, double aMinorRadius,
240 const EDA_ANGLE& aRotation ) override;
241 void DrawEllipseArc( const VECTOR2D& aCenterPoint, double aMajorRadius, double aMinorRadius,
242 const EDA_ANGLE& aRotation, const EDA_ANGLE& aStartAngle,
243 const EDA_ANGLE& aEndAngle ) override;
244 void DrawHoleWall( const VECTOR2D& aCenterPoint, double aHoleRadius,
245 double aWallWidth ) override;
246 void DrawRectangle( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint ) override;
247 void DrawPolygon( const std::deque<VECTOR2D>& aPointList ) override;
248 void DrawPolygon( const VECTOR2D aPointList[], int aListSize ) override;
249 void DrawPolygon( const SHAPE_POLY_SET& aPolySet, bool aStrokeTriangulation = false ) override;
250 void DrawPolygon( const SHAPE_LINE_CHAIN& aPolySet ) override;
251
252 void DrawCurve( const VECTOR2D& aStartPoint, const VECTOR2D& aControlPointA,
253 const VECTOR2D& aControlPointB, const VECTOR2D& aEndPoint,
254 double aFilterValue = 0.0 ) override;
255
256 void DrawBitmap( const BITMAP_BASE& aBitmap, double alphaBlend = 1.0 ) override;
257
258 void DrawPolyline( const std::deque<VECTOR2D>& aPointList ) override;
259 void DrawPolyline( const std::vector<VECTOR2D>& aPointList ) override;
260 void DrawPolyline( const VECTOR2D aPointList[], int aListSize ) override;
261 void DrawPolyline( const SHAPE_LINE_CHAIN& aLineChain ) override;
262 void DrawPolylines( const std::vector<std::vector<VECTOR2D>>& aPointLists ) override;
263
264 void DrawGlyph( const KIFONT::GLYPH& aGlyph, int aNth = 0, int aTotal = 1 ) override;
265
267 {
269 float alpha = 1.0f;
272 bool ownsResources = true;
273 bool ownsTexture = true;
274 std::array<uint64_t, 2> contentHash{};
275 };
276
278 using SHAPE_INSTANCES = std::array<std::vector<RHI_INSTANCE>, RHI_INSTANCED_SHAPES>;
279
284 static constexpr int ARENA_INSTANCE_COUNT = 2 * RHI_INSTANCED_SHAPES;
285 static constexpr int ARENA_POLYGON = ARENA_INSTANCE_COUNT;
286 static constexpr int ARENA_COUNT = ARENA_POLYGON + 1;
287
288 static constexpr int arenaOf( int aShape, bool aNegative ) { return 2 * aShape + ( aNegative ? 1 : 0 ); }
289
291 {
293 std::vector<RHI_VERTEX> polygonVertices;
294 std::vector<BITMAP_QUAD> bitmapQuads;
295 mutable std::array<uint64_t, 2> contentFingerprint{};
296 mutable bool contentFingerprintValid = false;
297
299 bool negative = false;
300
302 bool undimmed = false;
303
305 uint64_t placeSeq = 0;
306
308 float baseDepth = 0.0f;
309
311 int sceneLayer = -1;
312 double lodScale = 0.0;
313 bool shown = false;
316 std::optional<std::multimap<double, int>::iterator> lodEntry;
317
319 bool resident = false;
320 uint32_t residentIndex = 0;
321 uint32_t slotStart[ARENA_COUNT] = {};
322 uint32_t slotCount[ARENA_COUNT] = {};
323 };
324
331 void ReleaseRetiredBuffers( uint64_t aConsumedGeneration );
332
334 KIGFX::FRAME_DRAW_DESC GetDrawDesc() const;
335
337 void PopulateCursorUBO( KIGFX::FRAME_DRAW_DESC& aDesc ) const;
338
343 void SetPersistentTargets( bool aPersistent ) { m_persistentTargets = aPersistent; }
344
346 void MarkStaticDirty() override { m_staticDirty = true; }
347
349 KIGFX::FRAME_DRAW_DESC GetStaticDrawDesc() const;
350
352 KIGFX::FRAME_DRAW_DESC GetDynamicDrawDesc() const;
353
356 {
359 return v;
360 }
361
366 void PopulateCameraUBOs( KIGFX::FRAME_DRAW_DESC& aDesc ) const;
367
368protected:
369 void beginUpdate() override;
370 void endUpdate() override;
371
372private:
375
376 bool m_rhiInitialized = false;
377 bool m_isDrawing = false;
378 bool m_isUpdating = false;
380
383 float m_dpiScale = 1.0f;
385
386 bool m_negativeMode = false;
387 bool m_screenSpaceMode = false;
388 uint16_t m_strokePixelPad = 0;
389 float m_strokeLengthPad = 0.0f;
390 double m_strokeLod = 0.0;
391 double m_bitmapTextHeight = 0.0;
392 bool m_bitmapGlyphs = false;
393 bool m_diffLayerActive = false;
394 bool m_drawGrid = false;
395 bool m_drawCursor = false;
396
399
402 {
403 const auto& m = m_currentTransform.m_data;
404 m_transformIsIdentity = m[0][0] == 1.0 && m[0][1] == 0.0 && m[0][2] == 0.0
405 && m[1][0] == 0.0 && m[1][1] == 1.0 && m[1][2] == 0.0
406 && m[2][0] == 0.0 && m[2][1] == 0.0 && m[2][2] == 1.0;
407 }
408
409 VECTOR2D xform( const VECTOR2D& aPt ) const
410 {
411 return m_transformIsIdentity ? aPt : VECTOR2D( m_currentTransform * aPt );
412 }
413
414 double xformScale() const
415 {
416 return m_transformIsIdentity ? 1.0
417 : std::sqrt( std::abs( m_currentTransform.Determinant() ) );
418 }
419
420 double xformAngle() const
421 {
422 return m_transformIsIdentity ? 0.0 : std::atan2( m_currentTransform.m_data[1][0],
423 m_currentTransform.m_data[0][0] );
424 }
425
427 RHI_INSTANCE makeInstance( const COLOR4D& aColor );
428
430 void emitInstance( RHI_SHAPE aShape, const RHI_INSTANCE& aInstance )
431 {
432 poolForCurrentTarget().instances[aShape].push_back( aInstance );
433 }
434
436 void releaseBitmapQuads( std::vector<BITMAP_QUAD>& aQuads, bool aOwnedOnly );
437
439 bool drawBitmapGlyphs( const wxString& aText, const VECTOR2I& aPosition, const EDA_ANGLE& aAngle );
440
441 void addCapsuleInstance( const VECTOR2D& aStart, const VECTOR2D& aEnd,
442 double aWidth, const COLOR4D& aColor );
443
445 RHI_INSTANCE& addArcInstance( const VECTOR2D& aCenter, double aRadius, double aStartAngle,
446 double aSweepAngle, double aWidth, const COLOR4D& aColor );
447
448 RHI_INSTANCE& addPadInstance( const VECTOR2D& aCenter, double aHalfWidth, double aHalfHeight,
449 double aCornerRadius, float aShapeType, double aRotation,
450 const COLOR4D& aColor );
451
453 void drawEllipseShape( const SHAPE_ELLIPSE& aEllipse );
454
456 void fillOutline( SHAPE_LINE_CHAIN aOutline, const COLOR4D& aColor );
457
458 void emitTriangulatedPolygon( const SHAPE_POLY_SET& aPolySet, const COLOR4D& aColor );
459
461 RHI_VERTEX fillVertex( const COLOR4D& aColor );
462
464 void emitTriangle( const VECTOR2D& aA, const VECTOR2D& aB, const VECTOR2D& aC, const RHI_VERTEX& aFill );
465
467 void strokeLineChain( const SHAPE_LINE_CHAIN& aChain );
468
473 bool ensureBuffer( RHI_BufferHandle& aBuffer, size_t& aCapacity,
474 size_t aNeeded, size_t aStride );
475
476 static constexpr int BUFFER_RING_SIZE = 2;
477
485
487 {
489 std::vector<RHI_VERTEX> polygonVertices;
490 std::vector<BITMAP_QUAD> bitmapQuads;
491
492 std::vector<POLARITY_MARK> polarityMarks;
493 std::vector<POLARITY_RUN> polarityRuns;
494 bool drawsNegative = false;
495
497 {
498 RHI_SHAPE_COUNTS sizes;
499
500 for( int shape = 0; shape < RHI_INSTANCED_SHAPES; ++shape )
501 sizes[shape] = static_cast<uint32_t>( instances[shape].size() );
502
503 sizes[RHI_SHAPE_POLYGON] = static_cast<uint32_t>( polygonVertices.size() );
504 return sizes;
505 }
506
507 bool Empty() const
508 {
509 return std::ranges::all_of( Sizes(), []( uint32_t aSize ) { return aSize == 0; } )
510 && bitmapQuads.empty();
511 }
512
514 {
515 for( std::vector<RHI_INSTANCE>& shape : instances )
516 shape.clear();
517
518 polygonVertices.clear();
519 polarityMarks.clear();
520 polarityRuns.clear();
521 drawsNegative = false;
522 lastCount = {};
523 lastNegCount = {};
524 }
525
526 std::array<RHI_BufferHandle, RHI_SHAPES> buffer;
527 std::array<size_t, RHI_SHAPES> capacity{};
528
532
534 std::shared_ptr<const RHI_RUN_TABLE> cullRuns;
535 };
536
543
546
548 bool m_nonCachedRefilled = false;
549 bool m_overlayRefilled = false;
550
552 void refillDynamicTarget( RENDER_TARGET aTarget );
553
562
568 bool m_staticDirty = true;
569
575 std::vector<LAYER_RANGE> m_gridLayerRanges;
576 bool m_gridPoolRefilled = false;
577 bool m_drawingGridSources = false;
578 std::array<uint64_t, 2> m_gridPoolHash{};
579
581 void drawGridSources();
582 void drawGridSource( const GRID_SOURCE& aSrc );
583
585 double screenPixelSize() const;
586
589
594 uint64_t m_bufferGeneration = 1;
595
596 std::vector<std::pair<uint64_t, RHI_BufferHandle>> m_retiredBuffers;
597 std::vector<std::pair<uint64_t, RHI_TextureHandle>> m_retiredTextures;
598
599 void retireBuffer( RHI_BufferHandle aBuffer );
600 void retireTexture( RHI_TextureHandle aTexture );
601
611
613 std::unordered_map<int, std::vector<STATIC_GROUP_SLOT>> m_staticGroupSlots;
614
617 {
618 uint32_t begin = UINT32_MAX;
619 uint32_t end = 0;
620
621 void add( uint32_t aStart, uint32_t aCount )
622 {
623 if( aCount == 0 )
624 return;
625
626 begin = std::min( begin, aStart );
627 end = std::max( end, aStart + aCount );
628 }
629
630 bool empty() const { return end <= begin; }
631 void clear() { begin = UINT32_MAX; end = 0; }
632 };
633
635 template <typename T>
636 void submitSpan( const std::vector<T>& aElements, RHI_BufferHandle aBuffer, const PATCH_SPAN& aSpan );
637
638 std::array<PATCH_SPAN, RHI_SHAPES> m_staticPatches;
639
642 {
643 m_staticGroupSlots.clear();
644
645 for( PATCH_SPAN& span : m_staticPatches )
646 span.clear();
647 }
648
655 template <typename INSTANCE_FN, typename VERTEX_FN>
656 bool patchStaticGroup( int aGroup, INSTANCE_FN aInstance, VERTEX_FN aVertex );
657
659 void rewriteResidentGroup( int aGroupNumber, GAL_GROUP& aGroup );
660
666 template <typename T>
668 {
669 std::vector<T> elements;
671 size_t capacity = 0;
672 uint32_t dead = 0;
673
678 std::unordered_map<uint32_t, std::vector<uint32_t>> freeSlots;
679
680 std::vector<PATCH_SPAN> dirty;
681 bool uploadAll = false;
682 };
683
685 {
686 float depth = 0.0f;
687 bool hasNegatives = false;
688 bool inOrder = false;
689 bool visible = true;
690
692 bool overlayAlpha = false;
693
696
697 std::vector<int> residents;
698 std::vector<int> bitmapResidents;
699 std::optional<std::unordered_multimap<size_t, int>> retainedPrefixIndex;
700
705 uint32_t alphaCount[256] = {};
706 int bakedAlpha = -1;
707 bool alphaStale = false;
708 bool translucent = false;
709
714 std::vector<POLARITY_RUN> runs;
715 bool runsStale = false;
716 uint32_t knockoutGroups = 0;
717
719 std::shared_ptr<const RHI_RUN_TABLE> cullRuns;
720
723 };
724
730 static std::shared_ptr<const RHI_RUN_TABLE> updateSceneCullRuns( SCENE_LAYER& aLayer );
731
733 void buildSceneRuns( SCENE_LAYER& aLayer );
734
735 bool gridShown() const { return m_gridVisibility && m_gridSize.x != 0 && m_gridSize.y != 0; }
736
741 static bool sceneLayerComposited( const SCENE_LAYER& aLayer ) { return !aLayer.runs.empty(); }
742
743 uint64_t m_placeSeq = 0;
744 bool m_sceneDrawOrdered = false;
745
746 std::unordered_map<int, SCENE_LAYER> m_sceneLayers;
747 std::vector<int> m_sceneOrder;
748 std::multimap<double, int> m_lodGroups;
751 double m_sceneScale = 1.0;
752 bool m_sceneChanged = false;
753 bool m_sceneOrderStale = false;
754
756 void sortSceneOrder();
757 bool m_negativesLayer = false;
758
759 template <typename T>
760 uint32_t allocSceneSlot( SCENE_ARENA<T>& aArena, uint32_t aCount );
761
762 template <typename T>
763 void freeSceneSlot( SCENE_ARENA<T>& aArena, uint32_t aStart, uint32_t aCount );
764
765 template <typename T>
766 void compactSceneArena( SCENE_LAYER& aLayer, SCENE_ARENA<T>& aArena, int aArenaId );
767
769 template <typename T>
770 bool uploadSceneArena( SCENE_ARENA<T>& aArena );
771
772 void placeGroupInScene( int aGroupId, GAL_GROUP& aGroup, SCENE_LAYER& aLayer );
773 void removeGroupFromScene( int aGroupId, GAL_GROUP& aGroup );
774
776 void trackGroupLod( int aGroupId, GAL_GROUP& aGroup, bool aTrack );
777
779 void writeGroupToScene( const GAL_GROUP& aGroup, SCENE_LAYER& aLayer );
780 bool retainedTieOrderChanged( int aGroupId, GAL_GROUP& aGroup, SCENE_LAYER& aLayer );
781 static std::array<uint64_t, 2> computeGroupFingerprint( const GAL_GROUP& aGroup );
782
783 void countGroupAlpha( const GAL_GROUP& aGroup, SCENE_LAYER& aLayer, int aDelta );
784
786 static void countGroupArenas( const GAL_GROUP& aGroup, uint32_t* aCounts );
787
789 void resetScene();
790
792 bool flushScene();
793
794 void appendSceneRanges( FRAME_DRAW_DESC& aDesc ) const;
795
798
801
820
822 {
824 return m_gridPool;
825
827 return m_staticPool;
828
829 return m_currentTarget == TARGET_OVERLAY ? obuf() : buf();
830 }
831
837 {
839 size_t bytes = 0;
840 uint64_t lastUse = 0;
841 uint32_t groupRefs = 0;
842 std::array<uint64_t, 2> contentHash{};
843 };
844
849 void countBitmapRefs( const GAL_GROUP& aGroup, int aDelta );
850
855 bool assignSlicesByOrder( std::vector<LAYER_RANGE>& aRanges, size_t aFirst ) const;
856
858 bool keepsRelativeDepth( const GEOMETRY_BUFFER_SET& aPool ) const
859 {
860 return m_persistentTargets && &aPool != &m_staticPool && &aPool != &m_gridPool;
861 }
862
864 void notePolarity( bool aNegative );
865
866 bool m_groupNegative = false;
867
868 static constexpr size_t BITMAP_CACHE_MAX_ENTRIES = 64;
869
870 std::unordered_map<KIID, BITMAP_CACHE_ENTRY> m_bitmapTextureCache;
871 uint64_t m_bitmapUseCounter = 0;
873
879
880 std::unordered_map<int, GAL_GROUP> m_groups;
884
891 std::vector<RHI_VERTEX> m_groupScratchPolygonVertices;
892 std::vector<BITMAP_QUAD> m_groupScratchBitmapQuads;
893
895 std::vector<MATRIX3x3D> m_transformStack;
896
898 std::vector<LAYER_RANGE> m_staticLayerRanges;
899 std::vector<LAYER_RANGE> m_dynamicLayerRanges;
900 std::vector<LAYER_RANGE> m_overlayLayerRanges;
901
902 std::vector<LAYER_RANGE>& layerRangesForCurrentTarget()
903 {
905 return m_gridLayerRanges;
906
908 return m_staticLayerRanges;
909
911 }
912
913 const std::vector<LAYER_RANGE>& layerRangesForCurrentTarget() const
914 {
916 return m_gridLayerRanges;
917
919 return m_staticLayerRanges;
920
922 }
923
924 void beginLayerRange( float aDepth );
925};
926
927
928} // namespace KIGFX
929
930#endif // GAL_MODERN_H
This class handle bitmap images in KiCad.
Definition bitmap_base.h:47
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
GEOMETRY_BUFFER_SET m_gridPool
The grids of the grid items, which the static target draws over the grid and under all else.
Definition gal_modern.h:574
static void countGroupArenas(const GAL_GROUP &aGroup, uint32_t *aCounts)
Fill aCounts with how many of a group's instances go in each instance arena.
bool keepsRelativeDepth(const GEOMETRY_BUFFER_SET &aPool) const
The pool is a kept target whose ranges are drawn in depth slices, see Flush()
Definition gal_modern.h:858
double xformAngle() const
Definition gal_modern.h:420
void countGroupAlpha(const GAL_GROUP &aGroup, SCENE_LAYER &aLayer, int aDelta)
void SetScreenSpaceMode(bool aEnabled) override
Store sizes of what is drawn next in pixels rather than world units, so it keeps its pixel size throu...
Definition gal_modern.h:79
static std::array< uint64_t, 2 > computeGroupFingerprint(const GAL_GROUP &aGroup)
double m_bitmapTextHeight
glyph height while bitmap text is stroked, else 0
Definition gal_modern.h:391
std::vector< std::pair< uint64_t, RHI_TextureHandle > > m_retiredTextures
Definition gal_modern.h:597
void writeGroupToScene(const GAL_GROUP &aGroup, SCENE_LAYER &aLayer)
Copy a resident group's elements into its slots, applying the layer's hoisted alpha.
void SetCentringPad(int aPad)
The other canvases lay the view out this many pixels larger than the client area.
Definition gal_modern.h:155
std::multimap< double, int > m_lodGroups
groups with a scale threshold, by it
Definition gal_modern.h:748
float m_strokeLengthPad
see SetStrokeLengthPad(), 0 when off
Definition gal_modern.h:389
void SetPersistentTargets(bool aPersistent)
Keep the non-cached and overlay pools across frames until ClearTarget() empties one,...
Definition gal_modern.h:343
void emitInstance(RHI_SHAPE aShape, const RHI_INSTANCE &aInstance)
Append to the current target's pool.
Definition gal_modern.h:430
const GEOMETRY_BUFFER_SET & poolForCurrentTarget() const
Definition gal_modern.h:821
void refillDynamicTarget(RENDER_TARGET aTarget)
Rotate a dynamic target's ring, empty the new slot and open a range at the current depth.
bool m_nonCachedRefilled
non-cached pool was emptied for the frame being drawn
Definition gal_modern.h:548
void evictBitmapCacheEntries()
Evict least recently used entries down to the entry and byte caps, sparing those used this frame sinc...
uint64_t m_bufferGeneration
Bumped whenever a resource is retired.
Definition gal_modern.h:594
bool HasStaticRebuildPending() const override
Return true if the backend dropped its cached geometry and VIEW must draw the cached layers again,...
Definition gal_modern.h:57
std::array< PATCH_SPAN, RHI_SHAPES > m_staticPatches
Definition gal_modern.h:638
static bool sceneLayerComposited(const SCENE_LAYER &aLayer)
Whether a scene layer goes through the temp target and is composited with its alpha.
Definition gal_modern.h:741
GEOMETRY_BUFFER_SET m_overlaySets[BUFFER_RING_SIZE]
Definition gal_modern.h:544
bool retainedTieOrderChanged(int aGroupId, GAL_GROUP &aGroup, SCENE_LAYER &aLayer)
void retireBuffer(RHI_BufferHandle aBuffer)
void retireTexture(RHI_TextureHandle aTexture)
bool gridShown() const
Definition gal_modern.h:735
static constexpr int BUFFER_RING_SIZE
Definition gal_modern.h:476
bool uploadSceneArena(SCENE_ARENA< T > &aArena)
Upload the arena's pending spans, growing its buffer first.
uint64_t m_bitmapFrameStartSeq
use counter at frame start, entries used since are pinned
Definition gal_modern.h:872
MATRIX3x3D m_currentTransform
Definition gal_modern.h:894
GAL_MODERN(GAL_DISPLAY_OPTIONS &aOptions, RHI_DEVICE_PROXY &aDeviceProxy)
std::vector< LAYER_RANGE > m_gridLayerRanges
Definition gal_modern.h:575
bool m_transformIsIdentity
The GPU applies world to screen, so most geometry skips the matrix math on the hot path.
Definition gal_modern.h:398
void SetStrokeLevelOfDetail(double aLod) override
Show every stroke drawn from now on only while the view scale is above aLod, as VIEW::IsShownAtScale(...
Definition gal_modern.h:94
std::array< std::vector< RHI_INSTANCE >, RHI_INSTANCED_SHAPES > SHAPE_INSTANCES
Instances of each instanced shape, indexed by RHI_SHAPE.
Definition gal_modern.h:278
GEOMETRY_BUFFER_SET m_bufferSets[BUFFER_RING_SIZE]
The non-cached pool.
Definition gal_modern.h:541
uint64_t m_bitmapUseCounter
bumped on every cache touch
Definition gal_modern.h:871
SCENE_LAYER * m_openSceneLayer
Definition gal_modern.h:749
bool UsesPolygonTriangulationCache() const override
Return true if filled polygons are drawn from the SHAPE_POLY_SET triangulation cache,...
Definition gal_modern.h:61
void SetBitmapGlyphs(bool aEnable)
Draw bitmap text as one font atlas glyph per quad, for editors whose track shader supports it.
Definition gal_modern.h:99
void SetRenderThread(RENDER_THREAD *aThread)
Render thread used for fence-based synchronization.
Definition gal_modern.h:143
std::vector< BITMAP_QUAD > m_groupScratchBitmapQuads
Definition gal_modern.h:892
void sortSceneOrder()
Put the declared layers back in decreasing depth order after one was added or moved.
std::unordered_map< int, std::vector< STATIC_GROUP_SLOT > > m_staticGroupSlots
A group may be drawn into the static pool more than once per build, so a patch touches every slot.
Definition gal_modern.h:613
uint64_t m_placeSeq
Definition gal_modern.h:743
const std::vector< LAYER_RANGE > & layerRangesForCurrentTarget() const
Definition gal_modern.h:913
static constexpr int ARENA_INSTANCE_COUNT
Arenas of one retained scene layer.
Definition gal_modern.h:284
RENDER_THREAD * m_renderThread
Definition gal_modern.h:374
bool SupportsStrokePixelPad() const override
Tell whether strokes can carry part of their width in pixels, see SetStrokePixelPad().
Definition gal_modern.h:81
static constexpr int arenaOf(int aShape, bool aNegative)
Definition gal_modern.h:288
std::vector< std::pair< uint64_t, RHI_BufferHandle > > m_retiredBuffers
Definition gal_modern.h:596
bool IsInitialized() const override
Return the initialization status for the canvas.
Definition gal_modern.h:55
void appendSceneRanges(FRAME_DRAW_DESC &aDesc) const
uint16_t m_strokePixelPad
see SetStrokePixelPad(), in sixteenths of a pixel
Definition gal_modern.h:388
std::vector< MATRIX3x3D > m_transformStack
Definition gal_modern.h:895
static constexpr int ARENA_POLYGON
Definition gal_modern.h:285
RENDER_TARGET m_currentTarget
Definition gal_modern.h:379
bool m_groupNegative
the group being recorded has drawn a knockout
Definition gal_modern.h:866
bool m_bitmapGlyphs
see SetBitmapGlyphs()
Definition gal_modern.h:392
std::unordered_map< KIID, BITMAP_CACHE_ENTRY > m_bitmapTextureCache
Definition gal_modern.h:870
void compactSceneArena(SCENE_LAYER &aLayer, SCENE_ARENA< T > &aArena, int aArenaId)
float m_dpiScale
see SetDpiScale()
Definition gal_modern.h:383
const GEOMETRY_BUFFER_SET & obuf() const
Definition gal_modern.h:800
RHI_DEVICE_PROXY & m_deviceProxy
Definition gal_modern.h:373
std::vector< int > m_sceneOrder
layer ids in draw order
Definition gal_modern.h:747
static constexpr int ARENA_COUNT
Definition gal_modern.h:286
double m_strokeLod
see SetStrokeLevelOfDetail(), 0 when off
Definition gal_modern.h:390
void freeSceneSlot(SCENE_ARENA< T > &aArena, uint32_t aStart, uint32_t aCount)
static constexpr size_t BITMAP_CACHE_MAX_ENTRIES
Definition gal_modern.h:868
void SetStrokeLengthPad(double aPixels) override
Extend every stroke drawn from now on by aPixels at each end.
Definition gal_modern.h:85
std::array< uint64_t, 2 > m_gridPoolHash
hash of what the GPU buffers hold
Definition gal_modern.h:578
SHAPE_INSTANCES m_groupScratchInstances
BeginGroup() swaps these with the current pool's vectors and EndGroup() swaps them back.
Definition gal_modern.h:890
bool TakeStaticChanged()
Whether the last Flush() rebuilt the static pool.
Definition gal_modern.h:355
bool currentTargetOpen() const
Whether drawing to the current target lands in a pool that this frame has emptied.
Definition gal_modern.h:555
std::vector< LAYER_RANGE > m_staticLayerRanges
Kept per pool so static ranges survive BeginDrawing when m_staticDirty is false.
Definition gal_modern.h:898
GEOMETRY_BUFFER_SET m_staticPool
Pool for TARGET_CACHED.
Definition gal_modern.h:567
const GEOMETRY_BUFFER_SET & buf() const
Definition gal_modern.h:797
GEOMETRY_BUFFER_SET & poolForCurrentTarget()
TARGET_CACHED draws to the static pool and every other target to its dynamic ring slot.
Definition gal_modern.h:803
void resetScene()
Empty every arena and forget residency ahead of a full rebuild.
void recomputeTransformIdentity()
Exact comparison is enough since a false negative only takes the slow path.
Definition gal_modern.h:401
void placeGroupInScene(int aGroupId, GAL_GROUP &aGroup, SCENE_LAYER &aLayer)
VECTOR2D xform(const VECTOR2D &aPt) const
Definition gal_modern.h:409
std::vector< LAYER_RANGE > m_overlayLayerRanges
Definition gal_modern.h:900
uint32_t allocSceneSlot(SCENE_ARENA< T > &aArena, uint32_t aCount)
void MarkStaticDirty() override
Force the next Flush() to rebuild the static pool from scratch.
Definition gal_modern.h:346
GEOMETRY_BUFFER_SET & obuf()
Definition gal_modern.h:799
bool HasRetainedScene() const override
Return true if the backend keeps cached groups in a persistent scene that it patches in place,...
Definition gal_modern.h:58
int m_centringPad
see SetCentringPad()
Definition gal_modern.h:384
bool RefillsNonCachedTargets() const override
Return true if the backend empties every non-cached target each frame, so VIEW must emit them again e...
Definition gal_modern.h:59
VECTOR2I GetLayoutPixelSize() const override
Return the size the view is centred in and panned by.
Definition gal_modern.h:157
std::unordered_map< int, SCENE_LAYER > m_sceneLayers
by VIEW layer id
Definition gal_modern.h:746
bool flushScene()
Settle hoisted alpha, compact and upload.
std::vector< RHI_VERTEX > m_groupScratchPolygonVertices
Definition gal_modern.h:891
void SetStrokePixelPad(double aPixels) override
Widen every stroke, circle and pad drawn from now on by aPixels.
Definition gal_modern.h:87
bool IsDrawingMode() const override
Return true if the GAL is inside a BeginDrawing/EndDrawing pair.
Definition gal_modern.h:56
void clearStaticSlots()
Forget where groups sit in the static pool and the patches pending for it.
Definition gal_modern.h:641
void trackGroupLod(int aGroupId, GAL_GROUP &aGroup, bool aTrack)
Keep m_lodGroups to the shown groups whose threshold a zoom can actually cross.
bool m_drawingGridSources
emissions go to m_gridPool
Definition gal_modern.h:577
GEOMETRY_BUFFER_SET & buf()
Definition gal_modern.h:796
bool SupportsStrokeLevelOfDetail() const override
Tell whether strokes can carry a level of detail, see SetStrokeLevelOfDetail().
Definition gal_modern.h:92
double xformScale() const
Definition gal_modern.h:414
std::vector< LAYER_RANGE > & layerRangesForCurrentTarget()
Definition gal_modern.h:902
bool SupportsStrokeLengthPad() const override
Tell whether strokes can be longer by a number of pixels, see SetStrokeLengthPad().
Definition gal_modern.h:83
void SetDpiScale(float aScale)
Backing-store scale factor, 2.0 on Retina.
Definition gal_modern.h:149
void ReleaseBuffers()
Drop all GPU handles without destroying them.
Definition gal_modern.h:181
std::unordered_map< int, GAL_GROUP > m_groups
Definition gal_modern.h:880
std::vector< LAYER_RANGE > m_dynamicLayerRanges
Definition gal_modern.h:899
void removeGroupFromScene(int aGroupId, GAL_GROUP &aGroup)
bool m_staticRebuiltThisFlush
see TakeStaticChanged()
Definition gal_modern.h:588
bool SupportsTypedPads() const override
Return true if DrawPad() renders every standard pad shape.
Definition gal_modern.h:60
VECTOR2I m_screenSize
Screen size in screen (wx logical) coordinates.
VECTOR2D m_gridSize
The grid size.
bool m_gridVisibility
Should the grid be shown.
GAL(GAL_DISPLAY_OPTIONS &aOptions)
Thread that owns the RHI device.
UI-thread access to GPU resources without touching the device, which only the render thread may mutat...
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
Represent a set of closed polygons.
#define GAL_API
Definition gal.h:27
MATRIX3x3< double > MATRIX3x3D
Definition matrix3x3.h:469
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:30
RHI_SHAPE
Shapes a frame draws, each with its own array and pipelines.
@ RHI_SHAPE_POLYGON
RENDER_TARGET
RENDER_TARGET: Possible rendering targets.
Definition definitions.h:32
@ TARGET_CACHED
Main rendering target (cached)
Definition definitions.h:33
@ TARGET_OVERLAY
Items that may change while the view stays the same (noncached)
Definition definitions.h:35
std::array< uint32_t, RHI_SHAPES > RHI_SHAPE_COUNTS
Elements of each shape, indexed by RHI_SHAPE.
constexpr int RHI_INSTANCED_SHAPES
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:437
void DrawSegment(const ROUND_SEGMENT_2D &aSegment, unsigned int aNrSidesPerCircle)
Draw a thick line segment with rounded ends.
RHI_HANDLE< RHI_TAG_TEXTURE > RHI_TextureHandle
Definition rhi_handle.h:84
RHI_HANDLE< RHI_TAG_BUFFER > RHI_BufferHandle
Definition rhi_handle.h:83
GPU geometry of one frame, built by GAL_MODERN after EndDrawing() and passed through the FRAME_MAILBO...
GPU texture per bitmap, keyed on the image KIID so redrawing reuses the upload.
Definition gal_modern.h:837
uint32_t groupRefs
quads of cached groups drawing this texture
Definition gal_modern.h:841
std::array< uint64_t, 2 > contentHash
Definition gal_modern.h:842
RHI_TextureHandle texture
Definition gal_modern.h:838
size_t bytes
texture memory, which can be less than the image takes
Definition gal_modern.h:839
uint64_t lastUse
use counter for LRU eviction
Definition gal_modern.h:840
bool ownsTexture
false when the shared cache owns the texture
Definition gal_modern.h:273
std::array< uint64_t, 2 > contentHash
Definition gal_modern.h:274
RHI_BufferHandle vertexBuffer
Definition gal_modern.h:271
bool ownsResources
false for group copies, which own nothing
Definition gal_modern.h:272
uint32_t slotCount[ARENA_COUNT]
Definition gal_modern.h:322
std::vector< RHI_VERTEX > polygonVertices
Definition gal_modern.h:293
RETAINED_GROUP_ORDER order
Definition gal_modern.h:314
float baseDepth
Layer depth the group was cached at.
Definition gal_modern.h:308
uint64_t placeSeq
When the group was placed in the scene, which orders a layer's polarity runs.
Definition gal_modern.h:305
std::vector< BITMAP_QUAD > bitmapQuads
Definition gal_modern.h:294
bool undimmed
Keeps its colour when its layer is dimmed, see ChangeGroupUndimmed.
Definition gal_modern.h:302
uint32_t slotStart[ARENA_COUNT]
Definition gal_modern.h:321
bool negative
Recorded as a knockout, so it erases instead of drawing.
Definition gal_modern.h:299
bool resident
Slot held in each arena of sceneLayer while resident.
Definition gal_modern.h:319
int sceneLayer
Placement in the retained scene, set by SetGroupPlacement.
Definition gal_modern.h:311
std::array< uint64_t, 2 > contentFingerprint
Definition gal_modern.h:295
std::optional< std::multimap< double, int >::iterator > lodEntry
Entry in m_lodGroups while a zoom can show or hide the group.
Definition gal_modern.h:316
bool drawsNegative
polarity of the last element drawn
Definition gal_modern.h:494
std::vector< POLARITY_RUN > polarityRuns
built from the marks by Flush()
Definition gal_modern.h:493
std::array< size_t, RHI_SHAPES > capacity
Definition gal_modern.h:527
std::shared_ptr< const RHI_RUN_TABLE > cullRuns
Bounds of the arrays above as last processed, for culling off-screen runs.
Definition gal_modern.h:534
RHI_SHAPE_COUNTS Sizes() const
Definition gal_modern.h:496
RHI_SHAPE_COUNTS lastCount
Elements of each shape as last processed, normal and negative.
Definition gal_modern.h:530
std::vector< RHI_VERTEX > polygonVertices
Definition gal_modern.h:489
std::array< RHI_BufferHandle, RHI_SHAPES > buffer
Definition gal_modern.h:526
std::vector< POLARITY_MARK > polarityMarks
Definition gal_modern.h:492
std::vector< BITMAP_QUAD > bitmapQuads
Definition gal_modern.h:490
Half-open index span touched by patches since the last upload, empty when end <= begin.
Definition gal_modern.h:617
void add(uint32_t aStart, uint32_t aCount)
Definition gal_modern.h:621
A point in a pool's drawing order where the polarity of what is drawn changes.
Definition gal_modern.h:480
int range
index of the layer range that was open
Definition gal_modern.h:482
RHI_SHAPE_COUNTS at
elements of each shape drawn before it
Definition gal_modern.h:483
Retained scene arena holding one slot per placed group, patched in place so changing one item uploads...
Definition gal_modern.h:668
std::vector< PATCH_SPAN > dirty
spans to upload at the next Flush
Definition gal_modern.h:680
std::unordered_map< uint32_t, std::vector< uint32_t > > freeSlots
Freed slot starts by slot length, reused last-in first-out so a group re-cached with the same size ge...
Definition gal_modern.h:678
uint32_t dead
elements sitting in freed slots
Definition gal_modern.h:672
std::vector< T > elements
CPU mirror of the GPU buffer, dead slots included.
Definition gal_modern.h:669
size_t capacity
elements the GPU buffer can hold
Definition gal_modern.h:671
bool translucent
some element is drawn with less than full alpha
Definition gal_modern.h:708
bool visible
hidden layers keep their groups but are not uploaded
Definition gal_modern.h:689
bool overlayAlpha
see SetSceneLayerOverlayAlpha
Definition gal_modern.h:692
bool inOrder
declared, so it has a place in the draw order
Definition gal_modern.h:688
std::vector< POLARITY_RUN > runs
The layer's content in the order and polarity it was placed in.
Definition gal_modern.h:714
int bakedAlpha
hoisted alpha, or -1 when elements keep their own
Definition gal_modern.h:706
uint32_t alphaCount[256]
Elements per alpha value.
Definition gal_modern.h:705
std::vector< int > residents
groups holding a slot here
Definition gal_modern.h:697
SCENE_ARENA< RHI_VERTEX > polygons
Definition gal_modern.h:695
uint32_t knockoutGroups
residents recorded as knockouts
Definition gal_modern.h:716
size_t cullCounted[ARENA_COUNT]
Elements each arena held when cullRuns was counted.
Definition gal_modern.h:722
std::shared_ptr< const RHI_RUN_TABLE > cullRuns
Bounds of the arenas as last uploaded, for culling off-screen runs.
Definition gal_modern.h:719
SCENE_ARENA< RHI_INSTANCE > instances[ARENA_INSTANCE_COUNT]
Definition gal_modern.h:694
std::vector< int > bitmapResidents
the residents that carry bitmap quads
Definition gal_modern.h:698
std::optional< std::unordered_multimap< size_t, int > > retainedPrefixIndex
Definition gal_modern.h:699
Where a cached group landed in the static pool at the last full build, so a colour or depth change is...
Definition gal_modern.h:607
Render-time projection of a grid: GRID_GEOMETRY (kind/origin/pitch/orientation/extent) plus rendering...
Fade of a whole layer towards a colour, laid out as the push constants of layer_dim....
Per-instance data for instanced 2D rendering, packed into 40 bytes.
Position is in world coordinates, color is premultiplied alpha and u, v hold half floats.
Definition rhi_vertex.h:37
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708
VECTOR2< double > VECTOR2D
Definition vector2d.h:707