Line data Source code
1 : /**
2 : Copyright (c) 2023 Stappler LLC <admin@stappler.dev>
3 :
4 : Permission is hereby granted, free of charge, to any person obtaining a copy
5 : of this software and associated documentation files (the "Software"), to deal
6 : in the Software without restriction, including without limitation the rights
7 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 : copies of the Software, and to permit persons to whom the Software is
9 : furnished to do so, subject to the following conditions:
10 :
11 : The above copyright notice and this permission notice shall be included in
12 : all copies or substantial portions of the Software.
13 :
14 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 : THE SOFTWARE.
21 : **/
22 :
23 : #ifndef XENOLITH_RENDERER_BASIC2D_XL2DCOMMANDLIST_H_
24 : #define XENOLITH_RENDERER_BASIC2D_XL2DCOMMANDLIST_H_
25 :
26 : #include "XL2dVertexArray.h"
27 : #include "XLNodeInfo.h"
28 : #include "XLFrameContext.h"
29 :
30 : namespace STAPPLER_VERSIONIZED stappler::xenolith::basic2d {
31 :
32 : enum class CommandType : uint16_t {
33 : CommandGroup,
34 : VertexArray,
35 : Deferred,
36 :
37 : ShadowArray,
38 : ShadowDeferred,
39 :
40 : SdfGroup2D
41 : };
42 :
43 : struct CmdGeneral {
44 : SpanView<ZOrder> zPath;
45 : core::MaterialId material = 0;
46 : StateId state = StateIdNone;
47 : RenderingLevel renderingLevel = RenderingLevel::Solid;
48 : float depthValue = 0.0f;
49 : };
50 :
51 : struct CmdVertexArray : CmdGeneral {
52 : SpanView<TransformVertexData> vertexes;
53 : };
54 :
55 : struct CmdDeferred : CmdGeneral {
56 : Rc<DeferredVertexResult> deferred;
57 : Mat4 viewTransform;
58 : Mat4 modelTransform;
59 : bool normalized = false;
60 : };
61 :
62 : struct CmdShadow {
63 : StateId state = 0;
64 : float value = 0.0f;
65 : };
66 :
67 : struct CmdShadowArray : CmdShadow {
68 : SpanView<TransformVertexData> vertexes;
69 : };
70 :
71 : struct CmdShadowDeferred : CmdShadow {
72 : Rc<DeferredVertexResult> deferred;
73 : Mat4 viewTransform;
74 : Mat4 modelTransform;
75 : bool normalized = false;
76 : };
77 :
78 : struct CmdSdfGroup2D {
79 : Mat4 modelTransform;
80 : StateId state = 0;
81 : float value = 0.0f;
82 : float opacity = 1.0f;
83 :
84 : memory::vector<SdfPrimitive2DHeader> data;
85 :
86 : void addCircle2D(Vec2 origin, float r);
87 : void addRect2D(Rect rect);
88 : void addRoundedRect2D(Rect rect, float r);
89 : void addRoundedRect2D(Rect rect, Vec4 r);
90 : void addTriangle2D(Vec2 origin, Vec2 a, Vec2 b, Vec2 c);
91 : void addPolygon2D(SpanView<Vec2>);
92 : };
93 :
94 : struct Command {
95 : static Command *create(memory::pool_t *, CommandType t, CommandFlags = CommandFlags::None);
96 :
97 : void release();
98 :
99 : Command *next;
100 : CommandType type;
101 : CommandFlags flags = CommandFlags::None;
102 : void *data;
103 : };
104 :
105 : class CommandList : public Ref {
106 : public:
107 : virtual ~CommandList();
108 : bool init(const Rc<PoolRef> &);
109 :
110 : void pushVertexArray(Rc<VertexData> &&, const Mat4 &,
111 : SpanView<ZOrder> zPath, core::MaterialId material, StateId, RenderingLevel, float depthValue, CommandFlags = CommandFlags::None);
112 :
113 : // data should be preallocated from frame's pool
114 : void pushVertexArray(SpanView<TransformVertexData>,
115 : SpanView<ZOrder> zPath, core::MaterialId material, StateId, RenderingLevel, float depthValue, CommandFlags = CommandFlags::None);
116 :
117 : void pushDeferredVertexResult(const Rc<DeferredVertexResult> &, const Mat4 &view, const Mat4 &model, bool normalized,
118 : SpanView<ZOrder> zPath, core::MaterialId material, StateId, RenderingLevel, float depthValue, CommandFlags = CommandFlags::None);
119 :
120 : void pushShadowArray(Rc<VertexData> &&, const Mat4 &, StateId state, float value);
121 : void pushShadowArray(SpanView<TransformVertexData>, StateId state, float value);
122 : void pushDeferredShadow(const Rc<DeferredVertexResult> &, const Mat4 &view, const Mat4 &model, StateId state, bool normalized, float value);
123 :
124 : void pushSdfGroup(const Mat4 &model, StateId state, float value, const Callback<void(CmdSdfGroup2D &)> &cb);
125 :
126 9280 : const Command *getFirst() const { return _first; }
127 : const Command *getLast() const { return _last; }
128 :
129 : bool empty() const { return _first == nullptr; }
130 :
131 : size_t size() const { return _size; }
132 :
133 : protected:
134 : void addCommand(Command *);
135 :
136 : Rc<PoolRef> _pool;
137 : Command *_first = nullptr;
138 : Command *_last = nullptr;
139 : size_t _size = 0;
140 : };
141 :
142 : struct FrameContextHandle2d : public FrameContextHandle {
143 : ShadowLightInput lights;
144 : Rc<CommandList> commands;
145 : Rc<CommandList> shadows;
146 : };
147 :
148 : }
149 :
150 : #endif /* XENOLITH_RENDERER_BASIC2D_XL2DCOMMANDLIST_H_ */
|