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 RasterizationState rasterizationState;
49 VertexInputState vertexInputState;
50 InputAssemblyState inputAssemblyState;
52 PipelineCache* pipelineCache{};
55 PipelineImpl::PipelineImpl(const Graphics::PipelineCreateInfo& createInfo, Graphics::EglGraphicsController& controller, PipelineCache& pipelineCache)
56 : mController(controller)
58 // the creation is deferred so it's needed to copy certain parts of the CreateInfo structure
59 mPipelineState = std::make_unique<PipelineImpl::PipelineState>();
61 // Make copies of structured pass by pointers and replace
62 // stored create info structure fields
63 CopyStateIfSet(createInfo.inputAssemblyState, mPipelineState->inputAssemblyState, &mCreateInfo.inputAssemblyState);
64 CopyStateIfSet(createInfo.vertexInputState, mPipelineState->vertexInputState, &mCreateInfo.vertexInputState);
65 CopyStateIfSet(createInfo.rasterizationState, mPipelineState->rasterizationState, &mCreateInfo.rasterizationState);
66 CopyStateIfSet(createInfo.programState, mPipelineState->programState, &mCreateInfo.programState);
67 CopyStateIfSet(createInfo.colorBlendState, mPipelineState->colorBlendState, &mCreateInfo.colorBlendState);
68 CopyStateIfSet(createInfo.depthStencilState, mPipelineState->depthStencilState, &mCreateInfo.depthStencilState);
69 CopyStateIfSet(createInfo.programState, mPipelineState->programState, &mCreateInfo.programState);
70 CopyStateIfSet(createInfo.viewportState, mPipelineState->viewportState, &mCreateInfo.viewportState);
72 // This program doesn't need custom deleter
73 auto programImpl = static_cast<const GLES::Program*>(createInfo.programState->program)->GetImplementation();
74 mPipelineState->program = MakeUnique<GLES::Program>(programImpl);
76 // To make sure the program is alive as long as the pipeline is!
77 mCreateInfo.programState->program = mPipelineState->program.get();
80 mPipelineState->pipelineCache = &pipelineCache;
83 const PipelineCreateInfo& PipelineImpl::GetCreateInfo() const
88 auto& PipelineImpl::GetController() const
93 void PipelineImpl::Bind(const uint32_t glProgram) const
95 auto& gl = *GetController().GetGL();
96 gl.UseProgram(glProgram);
99 void PipelineImpl::Retain()
104 void PipelineImpl::Release()
109 uint32_t PipelineImpl::GetRefCount() const
114 PipelineImpl::~PipelineImpl() = default;
118 const PipelineCreateInfo& Pipeline::GetCreateInfo() const
120 return mPipeline.GetCreateInfo();
123 EglGraphicsController& Pipeline::GetController() const
125 return mPipeline.GetController();
128 Pipeline::~Pipeline()
131 if(mPipeline.GetRefCount())
137 void Pipeline::DiscardResource()
139 // Send pipeline to discard queue if refcount is 0
140 GetController().DiscardResource(this);
143 } // namespace Dali::Graphics::GLES