2 * Copyright (c) 2021 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.
19 #include "gles-graphics-pipeline.h"
22 #include <dali/integration-api/gl-defines.h>
26 #include "egl-graphics-controller.h"
27 #include "gles-graphics-pipeline-cache.h"
28 #include "gles-graphics-program.h"
30 namespace Dali::Graphics::GLES
33 * Copy of pipeline state, can be also used for internal caching
35 struct PipelineImpl::PipelineState
37 PipelineState() = default;
38 ~PipelineState() = default;
40 // for maintaining correct lifecycle, the owned program
41 // wrapper must be created
42 UniquePtr<Program> program;
44 ColorBlendState colorBlendState;
45 DepthStencilState depthStencilState;
46 ProgramState programState;
47 ViewportState viewportState;
48 FramebufferState framebufferState;
49 RasterizationState rasterizationState;
50 VertexInputState vertexInputState;
51 InputAssemblyState inputAssemblyState;
53 PipelineCache* pipelineCache{};
56 PipelineImpl::PipelineImpl(const Graphics::PipelineCreateInfo& createInfo, Graphics::EglGraphicsController& controller, PipelineCache& pipelineCache)
57 : mController(controller)
59 // the creation is deferred so it's needed to copy certain parts of the CreateInfo structure
60 mPipelineState = std::make_unique<PipelineImpl::PipelineState>();
62 // Make copies of structured pass by pointers and replace
63 // stored create info structure fields
64 CopyStateIfSet(createInfo.inputAssemblyState, mPipelineState->inputAssemblyState, &mCreateInfo.inputAssemblyState);
65 CopyStateIfSet(createInfo.vertexInputState, mPipelineState->vertexInputState, &mCreateInfo.vertexInputState);
66 CopyStateIfSet(createInfo.rasterizationState, mPipelineState->rasterizationState, &mCreateInfo.rasterizationState);
67 CopyStateIfSet(createInfo.programState, mPipelineState->programState, &mCreateInfo.programState);
68 CopyStateIfSet(createInfo.framebufferState, mPipelineState->framebufferState, &mCreateInfo.framebufferState);
69 CopyStateIfSet(createInfo.colorBlendState, mPipelineState->colorBlendState, &mCreateInfo.colorBlendState);
70 CopyStateIfSet(createInfo.depthStencilState, mPipelineState->depthStencilState, &mCreateInfo.depthStencilState);
71 CopyStateIfSet(createInfo.programState, mPipelineState->programState, &mCreateInfo.programState);
72 CopyStateIfSet(createInfo.viewportState, mPipelineState->viewportState, &mCreateInfo.viewportState);
74 // This program doesn't need custom deleter
75 auto programImpl = static_cast<const GLES::Program*>(createInfo.programState->program)->GetImplementation();
76 mPipelineState->program = MakeUnique<GLES::Program>(programImpl);
78 // To make sure the program is alive as long as the pipeline is!
79 mCreateInfo.programState->program = mPipelineState->program.get();
82 mPipelineState->pipelineCache = &pipelineCache;
85 const PipelineCreateInfo& PipelineImpl::GetCreateInfo() const
90 auto& PipelineImpl::GetController() const
95 void PipelineImpl::Bind(GLES::PipelineImpl* prevPipeline)
97 auto& gl = *GetController().GetGL();
98 auto glProgram = static_cast<const GLES::Program*>(GetCreateInfo().programState->program)->GetImplementation()->GetGlProgram();
99 gl.UseProgram(glProgram);
102 void PipelineImpl::Retain()
107 void PipelineImpl::Release()
112 uint32_t PipelineImpl::GetRefCount() const
117 PipelineImpl::~PipelineImpl() = default;
121 const PipelineCreateInfo& Pipeline::GetCreateInfo() const
123 return mPipeline.GetCreateInfo();
126 EglGraphicsController& Pipeline::GetController() const
128 return mPipeline.GetController();
131 Pipeline::~Pipeline()
134 if(mPipeline.GetRefCount())
140 void Pipeline::DiscardResource()
142 // Send pipeline to discard queue if refcount is 0
143 GetController().DiscardResource(this);
146 } // namespace Dali::Graphics::GLES