Line data Source code
1 : /** 2 : Copyright (c) 2023-2024 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_XL2D_H_ 24 : #define XENOLITH_RENDERER_BASIC2D_XL2D_H_ 25 : 26 : #include "XLCommon.h" 27 : #include "XLCoreMaterial.h" 28 : #include "XLNodeInfo.h" 29 : #include "XLSceneConfig.h" 30 : #include "XL2dConfig.h" 31 : 32 : #include "glsl/include/XL2dGlslVertexData.h" 33 : #include "glsl/include/XL2dGlslShadowData.h" 34 : #include "glsl/include/XL2dGlslSdfData.h" 35 : 36 : namespace STAPPLER_VERSIONIZED stappler::xenolith::basic2d { 37 : 38 : using Vertex = glsl::Vertex; 39 : using MaterialData = glsl::MaterialData; 40 : using TransformData = glsl::TransformData; 41 : using ShadowData = glsl::ShadowData; 42 : 43 : using DataAtlasIndex = glsl::DataAtlasIndex; 44 : using DataAtlasValue = glsl::DataAtlasValue; 45 : using AmbientLightData = glsl::AmbientLightData; 46 : using DirectLightData = glsl::DirectLightData; 47 : 48 : using Sdf2DObjectData = glsl::Sdf2DObjectData; 49 : using Circle2DIndex = glsl::Circle2DIndex; 50 : using Triangle2DIndex = glsl::Triangle2DIndex; 51 : using Rect2DIndex = glsl::Rect2DIndex; 52 : using RoundedRect2DIndex = glsl::RoundedRect2DIndex; 53 : using Polygon2DIndex = glsl::Polygon2DIndex; 54 : 55 : struct Triangle { 56 : Vertex a; 57 : Vertex b; 58 : Vertex c; 59 : }; 60 : 61 : struct Quad { 62 : Vertex tl; 63 : Vertex bl; 64 : Vertex tr; 65 : Vertex br; 66 : }; 67 : 68 : struct VertexSpan { 69 : core::MaterialId material = 0; 70 : uint32_t indexCount = 0; 71 : uint32_t instanceCount = 0; 72 : uint32_t firstIndex = 0; 73 : StateId state = 0; 74 : uint32_t gradientOffset = 0; 75 : uint32_t gradientCount = 0; 76 : }; 77 : 78 : struct alignas(16) VertexData : public Ref { 79 : Vector<Vertex> data; 80 : Vector<uint32_t> indexes; 81 : }; 82 : 83 : struct alignas(16) TransformVertexData { 84 : Mat4 transform; 85 : Rc<VertexData> data; 86 : }; 87 : 88 : enum class SdfShape { 89 : Circle2D, 90 : Rect2D, 91 : RoundedRect2D, 92 : Triangle2D, 93 : Polygon2D, 94 : }; 95 : 96 : struct SdfPrimitive2D { 97 : Vec2 origin; 98 : }; 99 : 100 : struct SdfCircle2D : SdfPrimitive2D { 101 : float radius; 102 : }; 103 : 104 : struct SdfRect2D : SdfPrimitive2D { 105 : Size2 size; 106 : }; 107 : 108 : struct SdfRoundedRect2D : SdfPrimitive2D { 109 : Size2 size; 110 : Vec4 radius; 111 : }; 112 : 113 : struct SdfTriangle2D : SdfPrimitive2D { 114 : Vec2 a; 115 : Vec2 b; 116 : Vec2 c; 117 : }; 118 : 119 : struct SdfPolygon2D { 120 : SpanView<Vec2> points; 121 : }; 122 : 123 : struct SdfPrimitive2DHeader { 124 : SdfShape type; 125 : BytesView bytes; 126 : }; 127 : 128 : class DeferredVertexResult : public Ref { 129 : public: 130 28480 : virtual ~DeferredVertexResult() { } 131 : 132 : virtual SpanView<TransformVertexData> getData() = 0; 133 : 134 104533 : bool isReady() const { return _isReady.load(); } 135 82681 : bool isWaitOnReady() const { return _waitOnReady; } 136 : 137 30034 : virtual void handleReady() { _isReady.store(true); } 138 : 139 : protected: 140 : bool _waitOnReady = true; 141 : std::atomic<bool> _isReady; 142 : }; 143 : 144 : struct ShadowLightInput { 145 : Color4F globalColor = Color4F::BLACK; 146 : uint32_t ambientLightCount = 0; 147 : uint32_t directLightCount = 0; 148 : float sceneDensity = 1.0f; 149 : float shadowDensity = 1.0f; 150 : float luminosity = nan(); 151 : float padding0 = 0.0f; 152 : AmbientLightData ambientLights[config::MaxAmbientLights]; 153 : DirectLightData directLights[config::MaxDirectLights]; 154 : 155 : bool addAmbientLight(const Vec4 &, const Color4F &, bool softShadow); 156 : bool addDirectLight(const Vec4 &, const Color4F &, const Vec4 &); 157 : 158 : Extent2 getShadowExtent(Size2 frameSize) const; 159 : Size2 getShadowSize(Size2 frameSize) const; 160 : }; 161 : 162 : } 163 : 164 : #endif /* XENOLITH_RENDERER_BASIC2D_XL2D_H_ */