2 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <dali/internal/render/renderers/pipeline-cache.h>
21 #include <dali/graphics-api/graphics-types.h>
22 #include <dali/integration-api/debug.h>
23 #include <dali/internal/render/common/render-instruction.h>
24 #include <dali/internal/render/renderers/render-renderer.h>
25 #include <dali/internal/render/renderers/render-vertex-buffer.h>
26 #include <dali/internal/render/shaders/program.h>
28 namespace Dali::Internal::Render
32 // Helper to get the vertex input format
33 Dali::Graphics::VertexInputFormat GetPropertyVertexFormat(Property::Type propertyType)
35 Dali::Graphics::VertexInputFormat type{};
39 case Property::BOOLEAN:
41 type = Dali::Graphics::VertexInputFormat::UNDEFINED; // type = GL_BYTE; @todo new type for this?
44 case Property::INTEGER:
46 type = Dali::Graphics::VertexInputFormat::INTEGER; // (short)
51 type = Dali::Graphics::VertexInputFormat::FLOAT;
54 case Property::VECTOR2:
56 type = Dali::Graphics::VertexInputFormat::FVECTOR2;
59 case Property::VECTOR3:
61 type = Dali::Graphics::VertexInputFormat::FVECTOR3;
64 case Property::VECTOR4:
66 type = Dali::Graphics::VertexInputFormat::FVECTOR4;
71 type = Dali::Graphics::VertexInputFormat::UNDEFINED;
78 constexpr Graphics::CullMode ConvertCullFace(Dali::FaceCullingMode::Type mode)
82 case Dali::FaceCullingMode::NONE:
84 return Graphics::CullMode::NONE;
86 case Dali::FaceCullingMode::FRONT:
88 return Graphics::CullMode::FRONT;
90 case Dali::FaceCullingMode::BACK:
92 return Graphics::CullMode::BACK;
94 case Dali::FaceCullingMode::FRONT_AND_BACK:
96 return Graphics::CullMode::FRONT_AND_BACK;
100 return Graphics::CullMode::NONE;
105 constexpr Graphics::BlendFactor ConvertBlendFactor(BlendFactor::Type blendFactor)
109 case BlendFactor::ZERO:
110 return Graphics::BlendFactor::ZERO;
111 case BlendFactor::ONE:
112 return Graphics::BlendFactor::ONE;
113 case BlendFactor::SRC_COLOR:
114 return Graphics::BlendFactor::SRC_COLOR;
115 case BlendFactor::ONE_MINUS_SRC_COLOR:
116 return Graphics::BlendFactor::ONE_MINUS_SRC_COLOR;
117 case BlendFactor::SRC_ALPHA:
118 return Graphics::BlendFactor::SRC_ALPHA;
119 case BlendFactor::ONE_MINUS_SRC_ALPHA:
120 return Graphics::BlendFactor::ONE_MINUS_SRC_ALPHA;
121 case BlendFactor::DST_ALPHA:
122 return Graphics::BlendFactor::DST_ALPHA;
123 case BlendFactor::ONE_MINUS_DST_ALPHA:
124 return Graphics::BlendFactor::ONE_MINUS_DST_ALPHA;
125 case BlendFactor::DST_COLOR:
126 return Graphics::BlendFactor::DST_COLOR;
127 case BlendFactor::ONE_MINUS_DST_COLOR:
128 return Graphics::BlendFactor::ONE_MINUS_DST_COLOR;
129 case BlendFactor::SRC_ALPHA_SATURATE:
130 return Graphics::BlendFactor::SRC_ALPHA_SATURATE;
131 case BlendFactor::CONSTANT_COLOR:
132 return Graphics::BlendFactor::CONSTANT_COLOR;
133 case BlendFactor::ONE_MINUS_CONSTANT_COLOR:
134 return Graphics::BlendFactor::ONE_MINUS_CONSTANT_COLOR;
135 case BlendFactor::CONSTANT_ALPHA:
136 return Graphics::BlendFactor::CONSTANT_ALPHA;
137 case BlendFactor::ONE_MINUS_CONSTANT_ALPHA:
138 return Graphics::BlendFactor::ONE_MINUS_CONSTANT_ALPHA;
140 return Graphics::BlendFactor();
144 constexpr Graphics::BlendOp ConvertBlendEquation(DevelBlendEquation::Type blendEquation)
146 switch(blendEquation)
148 case DevelBlendEquation::ADD:
149 return Graphics::BlendOp::ADD;
150 case DevelBlendEquation::SUBTRACT:
151 return Graphics::BlendOp::SUBTRACT;
152 case DevelBlendEquation::REVERSE_SUBTRACT:
153 return Graphics::BlendOp::REVERSE_SUBTRACT;
154 case DevelBlendEquation::COLOR:
155 return Graphics::BlendOp::COLOR;
156 case DevelBlendEquation::COLOR_BURN:
157 return Graphics::BlendOp::COLOR_BURN;
158 case DevelBlendEquation::COLOR_DODGE:
159 return Graphics::BlendOp::COLOR_DODGE;
160 case DevelBlendEquation::DARKEN:
161 return Graphics::BlendOp::DARKEN;
162 case DevelBlendEquation::DIFFERENCE:
163 return Graphics::BlendOp::DIFFERENCE;
164 case DevelBlendEquation::EXCLUSION:
165 return Graphics::BlendOp::EXCLUSION;
166 case DevelBlendEquation::HARD_LIGHT:
167 return Graphics::BlendOp::HARD_LIGHT;
168 case DevelBlendEquation::HUE:
169 return Graphics::BlendOp::HUE;
170 case DevelBlendEquation::LIGHTEN:
171 return Graphics::BlendOp::LIGHTEN;
172 case DevelBlendEquation::LUMINOSITY:
173 return Graphics::BlendOp::LUMINOSITY;
174 case DevelBlendEquation::MAX:
175 return Graphics::BlendOp::MAX;
176 case DevelBlendEquation::MIN:
177 return Graphics::BlendOp::MIN;
178 case DevelBlendEquation::MULTIPLY:
179 return Graphics::BlendOp::MULTIPLY;
180 case DevelBlendEquation::OVERLAY:
181 return Graphics::BlendOp::OVERLAY;
182 case DevelBlendEquation::SATURATION:
183 return Graphics::BlendOp::SATURATION;
184 case DevelBlendEquation::SCREEN:
185 return Graphics::BlendOp::SCREEN;
186 case DevelBlendEquation::SOFT_LIGHT:
187 return Graphics::BlendOp::SOFT_LIGHT;
189 return Graphics::BlendOp{};
193 PipelineCacheL0* PipelineCache::GetPipelineCacheL0(std::size_t hash, Program* program, Render::Geometry* geometry)
195 auto it = std::find_if(level0nodes.begin(), level0nodes.end(), [hash, program, geometry](PipelineCacheL0& item) {
196 return ((item.hash == hash && item.program == program && item.geometry == geometry));
199 // Add new node to cache
200 if(it == level0nodes.end())
202 uint32_t bindingIndex{0u};
203 auto& reflection = graphicsController->GetProgramReflection(program->GetGraphicsProgram());
205 Graphics::VertexInputState vertexInputState{};
208 for(auto&& vertexBuffer : geometry->GetVertexBuffers())
210 const VertexBuffer::Format& vertexFormat = *vertexBuffer->GetFormat();
212 vertexInputState.bufferBindings.emplace_back(vertexFormat.size, // stride
213 Graphics::VertexInputRate::PER_VERTEX);
215 const uint32_t attributeCount = vertexBuffer->GetAttributeCount();
216 uint32_t lastBoundAttributeIndex = 0;
217 for(uint32_t i = 0; i < attributeCount; ++i)
219 auto attributeName = vertexBuffer->GetAttributeName(i);
220 int32_t pLocation = reflection.GetVertexAttributeLocation(std::string(attributeName.GetStringView()));
223 auto location = static_cast<uint32_t>(pLocation);
224 vertexInputState.attributes.emplace_back(location,
226 vertexFormat.components[i].offset,
227 GetPropertyVertexFormat(vertexFormat.components[i].type));
228 ++lastBoundAttributeIndex;
232 DALI_LOG_WARNING("Attribute not found in the shader: %s\n", attributeName.GetCString());
233 // Don't bind unused attributes.
236 base += lastBoundAttributeIndex;
239 PipelineCacheL0 level0;
241 level0.program = program;
242 level0.geometry = geometry;
243 level0.inputState = vertexInputState;
244 level0nodes.emplace_back(std::move(level0));
245 it = level0nodes.end() - 1;
251 PipelineCacheL1* PipelineCacheL0::GetPipelineCacheL1(Render::Renderer* renderer, bool usingReflection)
253 // hash must be collision free
255 auto topo = (uint32_t(geometry->GetTopology()) & 0xffu);
256 auto cull = (uint32_t(renderer->GetFaceCullMode()) & 0xffu);
258 static const Graphics::PolygonMode polyTable[] = {
259 Graphics::PolygonMode::POINT,
260 Graphics::PolygonMode::LINE,
261 Graphics::PolygonMode::LINE,
262 Graphics::PolygonMode::LINE,
263 Graphics::PolygonMode::FILL,
264 Graphics::PolygonMode::FILL,
265 Graphics::PolygonMode::FILL};
267 auto poly = polyTable[topo];
269 static const FaceCullingMode::Type adjFaceCullingMode[4] =
271 FaceCullingMode::NONE,
272 FaceCullingMode::BACK,
273 FaceCullingMode::FRONT,
274 FaceCullingMode::FRONT_AND_BACK,
277 static const FaceCullingMode::Type normalFaceCullingMode[4] =
279 FaceCullingMode::NONE,
280 FaceCullingMode::FRONT,
281 FaceCullingMode::BACK,
282 FaceCullingMode::FRONT_AND_BACK,
285 static const FaceCullingMode::Type* cullModeTable[2] = {
286 normalFaceCullingMode,
289 // Retrieve cull mode
290 auto cullModeTableIndex = uint32_t(usingReflection) & 1u;
291 cull = cullModeTable[cullModeTableIndex][renderer->GetFaceCullMode()];
293 hash = (topo & 0xffu) | ((cull << 8u) & 0xff00u) | ((uint32_t(poly) << 16u) & 0xff0000u);
295 // If L1 not found by hash, create rasterization state describing pipeline and store it
296 auto it = std::find_if(level1nodes.begin(), level1nodes.end(), [hash](PipelineCacheL1& item) {
297 return item.hashCode == hash;
300 PipelineCacheL1* retval = nullptr;
301 if(it == level1nodes.end())
303 PipelineCacheL1 item;
304 item.hashCode = hash;
305 item.rs.cullMode = ConvertCullFace(FaceCullingMode::Type(cull));
306 item.rs.frontFace = Graphics::FrontFace::COUNTER_CLOCKWISE;
307 item.rs.polygonMode = poly; // not in use
308 item.ia.topology = geometry->GetTopology();
309 level1nodes.emplace_back(std::move(item));
310 retval = &level1nodes.back();
319 PipelineCacheL2* PipelineCacheL1::GetPipelineCacheL2(bool blend, bool premul, BlendingOptions& blendingOptions)
324 if(noBlend.pipeline == nullptr)
326 // reset all before returning if pipeline has never been created for that case
328 memset(&noBlend.colorBlendState, 0, sizeof(Graphics::ColorBlendState));
333 auto bitmask = uint32_t(blendingOptions.GetBitmask());
335 // Find by bitmask (L2 entries must be sorted by bitmask)
336 auto it = std::find_if(level2nodes.begin(), level2nodes.end(), [bitmask](PipelineCacheL2& item) {
337 return item.hash == bitmask;
340 // TODO: find better way of blend constants lookup
341 PipelineCacheL2* retval = nullptr;
342 if(it != level2nodes.end())
344 bool hasBlendColor = blendingOptions.GetBlendColor();
345 while(hasBlendColor && it != level2nodes.end() && (*it).hash == bitmask)
347 Vector4 v(it->colorBlendState.blendConstants);
348 if(v == *blendingOptions.GetBlendColor())
362 // create new entry and return it with null pipeline
364 l2.pipeline = nullptr;
365 auto& colorBlendState = l2.colorBlendState;
366 colorBlendState.SetBlendEnable(true);
367 Graphics::BlendOp rgbOp = ConvertBlendEquation(blendingOptions.GetBlendEquationRgb());
368 Graphics::BlendOp alphaOp = ConvertBlendEquation(blendingOptions.GetBlendEquationAlpha());
369 if(blendingOptions.IsAdvancedBlendEquationApplied() && premul)
373 DALI_LOG_ERROR("Advanced Blend Equation MUST be applied by using BlendEquation.\n");
379 .SetSrcColorBlendFactor(ConvertBlendFactor(blendingOptions.GetBlendSrcFactorRgb()))
380 .SetSrcAlphaBlendFactor(ConvertBlendFactor(blendingOptions.GetBlendSrcFactorAlpha()))
381 .SetDstColorBlendFactor(ConvertBlendFactor(blendingOptions.GetBlendDestFactorRgb()))
382 .SetDstAlphaBlendFactor(ConvertBlendFactor(blendingOptions.GetBlendDestFactorAlpha()))
383 .SetColorBlendOp(rgbOp)
384 .SetAlphaBlendOp(alphaOp);
386 // Blend color is optional and rarely used
387 auto* blendColor = const_cast<Vector4*>(blendingOptions.GetBlendColor());
390 colorBlendState.SetBlendConstants(blendColor->AsFloat());
393 l2.hash = blendingOptions.GetBitmask();
394 level2nodes.emplace_back(std::move(l2));
396 std::sort(level2nodes.begin(), level2nodes.end(), [](PipelineCacheL2& lhs, PipelineCacheL2& rhs) {
397 return lhs.hash < rhs.hash;
400 // run same function to retrieve retval
401 retval = GetPipelineCacheL2(blend, premul, blendingOptions);
407 void PipelineCacheQueryInfo::GenerateHash()
409 // Lightweight hash value generation.
410 hash = (reinterpret_cast<std::size_t>(program) >> Dali::Log<sizeof(decltype(*program))>::value) ^
411 (reinterpret_cast<std::size_t>(geometry) >> Dali::Log<sizeof(decltype(*geometry))>::value) ^
412 ((blendingEnabled ? 1u : 0u) << 0u) ^
413 ((alphaPremultiplied ? 1u : 0u) << 1u) ^
414 (static_cast<std::size_t>(geometry->GetTopology()) << 2u) ^
415 (static_cast<std::size_t>(renderer->GetFaceCullMode()) << 5u) ^
416 ((cameraUsingReflection ? 1u : 0u) << 8u) ^
417 (blendingEnabled ? static_cast<std::size_t>(blendingOptions->GetBitmask()) : 0xDA11u);
420 bool PipelineCacheQueryInfo::Equal(const PipelineCacheQueryInfo& lhs, const PipelineCacheQueryInfo& rhs) noexcept
422 // Naive equal check.
423 const bool ret = (lhs.hash == rhs.hash) && // Check hash value first
424 (lhs.program == rhs.program) &&
425 (lhs.geometry == rhs.geometry) &&
426 (lhs.blendingEnabled == rhs.blendingEnabled) &&
427 (lhs.alphaPremultiplied == rhs.alphaPremultiplied) &&
428 (lhs.geometry->GetTopology() == rhs.geometry->GetTopology()) &&
429 (lhs.renderer->GetFaceCullMode() == rhs.renderer->GetFaceCullMode()) &&
430 (lhs.cameraUsingReflection == rhs.cameraUsingReflection) &&
431 (!lhs.blendingEnabled ||
432 (lhs.blendingOptions->GetBitmask() == rhs.blendingOptions->GetBitmask() &&
433 ((lhs.blendingOptions->GetBlendColor() == nullptr && rhs.blendingOptions->GetBlendColor() == nullptr) ||
434 (lhs.blendingOptions->GetBlendColor() &&
435 rhs.blendingOptions->GetBlendColor() &&
436 (*lhs.blendingOptions->GetBlendColor() == *rhs.blendingOptions->GetBlendColor())))));
441 PipelineCache::PipelineCache(Graphics::Controller& controller)
442 : graphicsController(&controller)
444 // Clean up cache first
445 CleanLatestUsedCache();
448 PipelineResult PipelineCache::GetPipeline(const PipelineCacheQueryInfo& queryInfo, bool createNewIfNotFound)
450 // Seperate branch whether query use blending or not.
451 const int latestUsedCacheIndex = queryInfo.blendingEnabled ? 0 : 1;
453 // If we can reuse latest bound pipeline, Fast return.
454 if(ReuseLatestBoundPipeline(latestUsedCacheIndex, queryInfo))
456 return mLatestResult[latestUsedCacheIndex];
459 auto* level0 = GetPipelineCacheL0(queryInfo.hash, queryInfo.program, queryInfo.geometry);
460 auto* level1 = level0->GetPipelineCacheL1(queryInfo.renderer, queryInfo.cameraUsingReflection);
461 auto* level2 = level1->GetPipelineCacheL2(queryInfo.blendingEnabled, queryInfo.alphaPremultiplied, *queryInfo.blendingOptions);
463 // Create new pipeline at level2 if requested
464 if(level2->pipeline == nullptr && createNewIfNotFound)
466 Graphics::ProgramState programState{};
467 programState.program = &queryInfo.program->GetGraphicsProgram();
468 // Create the pipeline
469 Graphics::PipelineCreateInfo createInfo;
471 .SetInputAssemblyState(&level1->ia)
472 .SetVertexInputState(&level0->inputState)
473 .SetRasterizationState(&level1->rs)
474 .SetColorBlendState(&level2->colorBlendState)
475 .SetProgramState(&programState);
477 // Store a pipeline per renderer per render (renderer can be owned by multiple nodes,
478 // and re-drawn in multiple instructions).
479 level2->pipeline = graphicsController->CreatePipeline(createInfo, nullptr);
482 PipelineResult result{};
484 result.pipeline = level2->pipeline.get();
485 result.level0 = level0;
486 result.level1 = level1;
487 result.level2 = level2;
489 // Copy query and result
490 mLatestQuery[latestUsedCacheIndex] = queryInfo;
491 mLatestResult[latestUsedCacheIndex] = result;
496 bool PipelineCache::ReuseLatestBoundPipeline(const int latestUsedCacheIndex, const PipelineCacheQueryInfo& queryInfo) const
498 return mLatestResult[latestUsedCacheIndex].pipeline != nullptr && PipelineCacheQueryInfo::Equal(queryInfo, mLatestQuery[latestUsedCacheIndex]);
501 } // namespace Dali::Internal::Render