Ensuring vertex attributes are uploaded once per buffer
[platform/core/uifw/dali-adaptor.git] / dali / internal / graphics / gles-impl / gles-context.h
1 #ifndef DALI_GRAPHICS_GLES_CONTEXT_H
2 #define DALI_GRAPHICS_GLES_CONTEXT_H
3
4 /*
5  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 #include <dali/graphics-api/graphics-command-buffer.h>
22 #include "gles-context-state-cache.h"
23 #include "gles-graphics-types.h"
24
25 namespace Dali::Graphics
26 {
27 class EglGraphicsController;
28 namespace GLES
29 {
30 class Pipeline;
31 class RenderPass;
32 class RenderTarget;
33 class Texture;
34 class TextureDependencyChecker;
35
36 /**
37  * @brief Context represents single GLES context
38  */
39 class Context
40 {
41 public:
42   explicit Context(EglGraphicsController& controller);
43
44   ~Context();
45
46   /**
47    * @brief Flushes the context
48    *
49    * Flushes the context by issuing GL calls to set the required
50    * state. Causes a glWaitSync if any drawn textures are dependent
51    * on another context.
52    *
53    * @param[in] reset If true then state is reset unconditionally
54    * @param[in] drawCall the draws that need flushing
55    * @param[in] dependencyChecker The texture dependecy checker
56    */
57   void Flush(bool reset, const GLES::DrawCallDescriptor& drawCall, GLES::TextureDependencyChecker& dependencyChecker);
58
59   /**
60    * @brief Returns context Id
61    * @return
62    */
63   uint32_t GetContextId() const
64   {
65     return 0;
66   }
67
68   /**
69    * @brief Binds textures to the context
70    *
71    * Bindings are merged, they are resolved at later time
72    * when the draw call is issued. Then the texture binding
73    * slots (GL active texture binding) are enabled/disabled accordingly
74    * to the uniform layout.
75    *
76    * @param[in] bindings List of bindings
77    *
78    */
79   void BindTextures(const Graphics::TextureBinding* bindings, uint32_t count);
80
81   /**
82    * @brief Vertex buffers to bind
83    *
84    * The bindings are taken from a command buffer being
85    * currently processed and copied into the local storage.
86    */
87
88   void BindVertexBuffers(const GLES::VertexBufferBindingDescriptor* bindings, uint32_t count);
89
90   /**
91    * @brief Binds index buffer
92    *
93    * @param indexBufferBinding
94    */
95   void BindIndexBuffer(const IndexBufferBindingDescriptor& indexBufferBinding);
96
97   /**
98    * @brief Binds pipeline to the context
99    * @param newPipeline
100    */
101   void BindPipeline(const GLES::Pipeline* newPipeline);
102
103   /**
104    * @brief Binds uniform buffers to the context
105    *
106    * @param[in] uboBindings real UBO binfins
107    * @param[in] standaloneBindings emulated (legacy) UBO object
108    */
109   void BindUniformBuffers(const UniformBufferBindingDescriptor* uboBindings, uint32_t uboCount, const UniformBufferBindingDescriptor& standaloneBindings);
110
111   /**
112    * @brief Resolves blend state on the currently attached pipeline
113    */
114   void ResolveBlendState();
115
116   /**
117    * @brief Resolves rasterization state on the currently attached pipeline
118    */
119   void ResolveRasterizationState();
120
121   /**
122    * @brief Resolves uniform buffers and binds data to the pipeline
123    */
124   void ResolveUniformBuffers();
125
126   /**
127    * @brief Special usecase for legacy shaders, called by ResolveUniformBuffers()
128    */
129   void ResolveStandaloneUniforms();
130
131   /**
132    * @brief Begins render pass for specified render target
133    *
134    * @param[in] renderPass render pass object to begin
135    */
136   void BeginRenderPass(const BeginRenderPassDescriptor& renderPassBegin);
137
138   /**
139    * @brief Ends render pass
140    *
141    * Ending render pass is necessary in order to ensure
142    * proper explicit synchronization is in place
143    */
144   void EndRenderPass(TextureDependencyChecker& checker);
145
146   /**
147    * @brief Returns the cache of GL state in the context
148    * @return the reference of GL state cache (which can be modified)
149    */
150   GLStateCache& GetGLStateCache();
151
152   /**
153    * @brief Called when the GL context has been created.
154    */
155   void GlContextCreated();
156
157   /**
158    * @brief Called when the GL context has been destroyed.
159    */
160   void GlContextDestroyed();
161
162   /**
163    * @brief Invalidates the cached pipeline object in the context if it matches
164    * This is called before the pipeline is deleted
165    *
166    * @param[in] pipeline The pipeline
167    */
168   void InvalidateCachedPipeline(GLES::Pipeline* pipeline);
169
170   /**
171    * @brief Sets up EGL context for native rendering
172    *
173    * - The native rendering uses dedicated context
174    * - There is one EGL native rendering context per GLES::Context object
175    * - Native rendering context is compatible with the window/surface context
176    * - Native rendering context dies with GLES::Context object
177    *
178    * When native rendering is about to be executed, the dedicated EGL context
179    * is acquired (created or reused) and made current. The Window/Surface context
180    * is cached to be restored afterwards.
181    */
182   void PrepareForNativeRendering();
183
184   /**
185    * @brief Restores window/surface context after native rendering.
186    */
187   void RestoreFromNativeRendering();
188
189   void ActiveTexture(uint32_t textureBindingIndex);
190   void BindTexture(GLenum target, BoundTextureType textureTypeId, uint32_t textureId);
191   void GenerateMipmap(GLenum target);
192
193   /**
194    * Binds the buffer.
195    * @return true if the buffer was actually bound, false if it's cached
196    */
197   bool BindBuffer(GLenum target, uint32_t bufferId);
198
199   void DrawBuffers(uint32_t count, const GLenum* buffers);
200   void BindFrameBuffer(GLenum target, uint32_t bufferId);
201   void GenFramebuffers(uint32_t count, uint32_t* framebuffers);
202   void DeleteFramebuffers(uint32_t count, uint32_t* framebuffers);
203   void ColorMask(bool enabled);
204   void ClearStencilBuffer();
205   void ClearDepthBuffer();
206   void ClearBuffer(uint32_t mask, bool forceClear);
207   void InvalidateDepthStencilBuffers();
208   void SetScissorTestEnabled(bool scissorEnabled);
209   void SetStencilTestEnable(bool stencilEnable);
210   void StencilMask(uint32_t writeMask);
211   void StencilFunc(Graphics::CompareOp compareOp,
212                    uint32_t            reference,
213                    uint32_t            compareMask);
214   void StencilOp(Graphics::StencilOp failOp,
215                  Graphics::StencilOp depthFailOp,
216                  Graphics::StencilOp passOp);
217   void SetDepthCompareOp(Graphics::CompareOp compareOp);
218   void SetDepthTestEnable(bool depthTestEnable);
219   void SetDepthWriteEnable(bool depthWriteEnable);
220
221 private:
222   /**
223    * @brief Clear current state
224    */
225   void ClearState();
226
227 private:
228   struct Impl;
229   std::unique_ptr<Impl> mImpl;
230 };
231 } // namespace GLES
232 } // namespace Dali::Graphics
233 #endif //DALI_GRAPHICS_GLES_CONTEXT_H