1 #ifndef DALI_INTERNAL_RENDER_PIPELINE_CACHE_H
2 #define DALI_INTERNAL_RENDER_PIPELINE_CACHE_H
5 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 #include <dali/graphics-api/graphics-controller.h>
22 #include <dali/graphics-api/graphics-pipeline.h>
23 #include <dali/graphics-api/graphics-types.h>
24 #include <dali/internal/common/blending-options.h>
29 namespace Dali::Internal
38 * Cache Level 2 : Last level of cache, stores actual pipeline
40 struct PipelineCacheL2
43 uint32_t referenceCount{0u};
44 Graphics::ColorBlendState colorBlendState;
45 Graphics::UniquePtr<Graphics::Pipeline> pipeline;
49 * Cache Level 1 : Stores rasterization and input assembly states
51 struct PipelineCacheL1
53 PipelineCacheL2* GetPipelineCacheL2(bool blend, bool premul, BlendingOptions& blendingOptions);
56 * @brief Clear unused caches.
58 bool ClearUnusedCache();
60 uint32_t hashCode{}; // 1byte cull, 1byte poly, 1byte frontface
61 Graphics::RasterizationState rs{};
62 Graphics::InputAssemblyState ia{};
64 PipelineCacheL2 noBlend; // special case
65 std::vector<PipelineCacheL2> level2nodes;
69 * Cache Level 0 : Stores hash, geometry, program amd vertex input state
71 struct PipelineCacheL0 // L0 cache
73 PipelineCacheL1* GetPipelineCacheL1(Render::Renderer* renderer, bool usingReflection);
76 * @brief Clear unused caches.
78 void ClearUnusedCache();
83 Graphics::VertexInputState inputState;
85 std::vector<PipelineCacheL1> level1nodes;
88 struct PipelineCacheQueryInfo
95 bool cameraUsingReflection;
99 bool alphaPremultiplied;
100 BlendingOptions* blendingOptions;
102 // Lightweight hash value before compare each query.
103 std::size_t hash{0u};
105 // Generate hash value for this query.
108 // Value comparision between two query info.
109 static bool Equal(const PipelineCacheQueryInfo& lhs, const PipelineCacheQueryInfo& rhs) noexcept;
113 * Result of PipelineCache::GetPipeline() call
115 struct PipelineResult
117 Graphics::Pipeline* pipeline;
118 PipelineCacheL2* level2;
129 * @param[in] controller Graphics controller
131 explicit PipelineCache(Graphics::Controller& controller);
134 * Retrieves next cache level
136 PipelineCacheL0* GetPipelineCacheL0(std::size_t hash, Program* program, Render::Geometry* geometry);
139 * Retrieves pipeline matching queryInfo struct
141 * May retrieve existing pipeline or create one or return nullptr.
143 PipelineResult GetPipeline(const PipelineCacheQueryInfo& queryInfo, bool createNewIfNotFound);
146 * @brief Check whether we can reuse latest found PipelineResult.
147 * We can reuse latest pipeline only if query info is equal with latest query
148 * and we don't call CleanLatestUsedCache() before.
150 * @param[in] latestUsedCacheIndex Index of cache we want to compare.
151 * @param[in] queryInfo Query for current pipeline.
152 * @return True if we can reuse latest pipeline result. False otherwise
154 bool ReuseLatestBoundPipeline(const int latestUsedCacheIndex, const PipelineCacheQueryInfo& queryInfo) const;
157 * @brief This is called before rendering every frame.
162 * @brief Decrease the reference count of the pipeline cache.
163 * @param pipelineCache The pipeline cache to decrease the reference count
165 void ResetPipeline(PipelineCacheL2* pipelineCache);
169 * @brief Clear latest bound result.
171 void CleanLatestUsedCache()
173 // Set pipeline as nullptr is enough.
174 mLatestResult[0].pipeline = nullptr;
175 mLatestResult[1].pipeline = nullptr;
179 * @brief Clear unused caches.
181 void ClearUnusedCache();
184 Graphics::Controller* graphicsController{nullptr};
185 std::vector<PipelineCacheL0> level0nodes;
187 // Cache latest queries whether blend enabled or not.
188 // (Since most UI case (like Text and Image) enable blend, and most 3D case disable blend.)
189 PipelineCacheQueryInfo mLatestQuery[2]; ///< Latest requested query info. It will be invalidate after query's renderer / geometry / blendingOptions value changed.
190 PipelineResult mLatestResult[2]; ///< Latest used result. It will be invalidate when we call CleanLatestUsedCache() or some cache changed.
192 uint32_t mFrameCount{0u};
195 } // namespace Render
196 } // namespace Dali::Internal
198 #endif // DALI_INTERNAL_RENDER_PIPELINE_CACHE_H