Ocean
Loading...
Searching...
No Matches
GLESTexture2D.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_RENDERING_GLES_TEXTURE_2D_H
9#define META_OCEAN_RENDERING_GLES_TEXTURE_2D_H
10
14
17
18namespace Ocean
19{
20
21namespace Rendering
22{
23
24namespace GLESceneGraph
25{
26
27/**
28 * This class wraps a GLESceneGraph texture object.
29 * @ingroup renderinggles
30 */
31class OCEAN_RENDERING_GLES_EXPORT GLESTexture2D :
32 virtual public GLESTexture,
33 virtual public Texture2D,
34 virtual protected DynamicObject
35{
36 friend class GLESFactory;
37
38 public:
39
40 /**
41 * Definition of individual layouts of the secondary texture.
42 */
43 enum SecondaryLayout : uint32_t
44 {
45 /// The frame type does not need a secondary texture.
46 SL_NONE = 0u,
47 /// The second plane of the frame is copied into the secondary texture.
49 /// The second and the third plane of the frame are stacked into the secondary texture, the second plane in the upper half.
51 /// The second and the third plane of the frame are stacked into the secondary texture, the third plane in the upper half.
52 SL_PLANE_2_1
53 };
54
55 /**
56 * This class holds the texture properties of one pixel format.
57 * The GL properties always describe the internal pixel format, so they can be used without resolving the conversion first.
58 */
60 {
61 public:
62
63 /**
64 * Creates properties for a pixel format which needs one texture only.
65 * @param programType The type of the shader program which can sample the pixel format, PT_UNKNOWN if no shader exists for it
66 * @param primaryFormat The GL format of the primary texture, must be valid
67 * @param primaryType The GL type of the primary texture, must be valid
68 */
69 TextureProperties(const GLESAttribute::ProgramType programType, const GLenum primaryFormat, const GLenum primaryType);
70
71 /**
72 * Creates properties for a pixel format which needs a secondary texture as well.
73 * @param programType The type of the shader program which can sample the pixel format, must not be PT_UNKNOWN
74 * @param primaryFormat The GL format of the primary texture, must be valid
75 * @param primaryType The GL type of the primary texture, must be valid
76 * @param secondaryFormat The GL format of the secondary texture, must be valid
77 * @param secondaryType The GL type of the secondary texture, must be valid
78 * @param secondaryWidthDivisor The divisor between the width of the frame and the width of the secondary texture, with range [1, infinity)
79 * @param secondaryHeightDivisor The divisor between the height of the frame and the height of the secondary texture, with range [1, infinity)
80 * @param secondaryLayout The layout of the secondary texture, must not be SL_NONE
81 */
82 TextureProperties(const GLESAttribute::ProgramType programType, const GLenum primaryFormat, const GLenum primaryType, const GLenum secondaryFormat, const GLenum secondaryType, const unsigned int secondaryWidthDivisor, const unsigned int secondaryHeightDivisor, const SecondaryLayout secondaryLayout);
83
84 public:
85
86 /// The pixel format the frame needs to be converted to before it can be uploaded, FORMAT_UNDEFINED if the frame can be uploaded as it is.
87 /// The remaining properties belong to this pixel format and not to the pixel format the properties are stored for.
88 FrameType::PixelFormat internalPixelFormat_ = FrameType::FORMAT_UNDEFINED;
89
90 /// The type of the shader program which can sample the pixel format, PT_UNKNOWN if no shader exists for it.
91 GLESAttribute::ProgramType programType_ = GLESAttribute::PT_UNKNOWN;
92
93 /// The GL format of the primary texture.
94 GLenum primaryFormat_ = 0;
95
96 /// The GL type of the primary texture.
97 GLenum primaryType_ = 0;
98
99 /// The GL format of the secondary texture, 0 if the pixel format does not need a secondary texture.
100 GLenum secondaryFormat_ = 0;
101
102 /// The GL type of the secondary texture, 0 if the pixel format does not need a secondary texture.
103 GLenum secondaryType_ = 0;
104
105 /// The divisor between the width of the frame and the width of the secondary texture, 0 if the pixel format does not need a secondary texture.
106 unsigned int secondaryWidthDivisor_ = 0u;
107
108 /// The divisor between the height of the frame and the height of the secondary texture, 0 if the pixel format does not need a secondary texture.
109 unsigned int secondaryHeightDivisor_ = 0u;
110
111 /// The layout of the secondary texture.
112 SecondaryLayout secondaryLayout_ = SL_NONE;
113
114 public:
115
116 /**
117 * Returns whether a second texture is necessary.
118 * @return True, if so; False, if one texture is enough
119 */
120 inline bool needsSecondaryTextureObjects() const;
121
122 /**
123 * Returns the frame type of the internal frame for which a GL texture format exists.
124 * @param frameType The external (given) frame type for which the internal frame type will be determined, must be valid and must match these properties
125 * @return The resulting internal frame type
126 */
127 FrameType internalFrameType(const FrameType& frameType) const;
128
129 /**
130 * Determines the properties of the primary texture for a given frame type.
131 * @param frameType The frame type for which the properties will be determined, must be valid and must match these properties
132 * @param width The resulting width of the primary texture in pixel, with range [1, infinity)
133 * @param height The resulting height of the primary texture in pixel, with range [1, infinity)
134 * @param format The resulting GL format of the primary texture
135 * @param type The resulting GL type of the primary texture
136 */
137 void primaryTextureProperties(const FrameType& frameType, unsigned int& width, unsigned int& height, GLenum& format, GLenum& type) const;
138
139 /**
140 * Determines the properties of the secondary texture for a given frame type.
141 * @param frameType The frame type for which the properties will be determined, must be valid and must match these properties
142 * @param width The resulting width of the secondary texture in pixel, with range [1, infinity)
143 * @param height The resulting height of the secondary texture in pixel, with range [1, infinity)
144 * @param format The resulting GL format of the secondary texture
145 * @param type The resulting GL type of the secondary texture
146 * @return True, if succeeded; False, if the frame type does not need a secondary texture
147 */
148 bool secondaryTextureProperties(const FrameType& frameType, unsigned int& width, unsigned int& height, GLenum& format, GLenum& type) const;
149 };
150
151 protected:
152
153 /**
154 * Definition of a map mapping pixel formats to texture properties.
155 */
156 using TexturePropertiesMap = std::unordered_map<FrameType::PixelFormat, TextureProperties>;
157
158 public:
159
160 /**
161 * Returns the texture wrap type in s direction.
162 * @see Texture2D::wrapTypeS().
163 */
164 WrapType wrapTypeS() const override;
165
166 /**
167 * Returns the texture wrap type in t direction.
168 * @see Texture2D::wrapTypeT().
169 */
170 WrapType wrapTypeT() const override;
171
172 /**
173 * Sets the texture wrap type in s direction.
174 * @see Texture2D::setWrapTypeS().
175 */
176 bool setWrapTypeS(const WrapType type) override;
177
178 /**
179 * Sets the texture wrap type in t direction.
180 * @see Texture2D::setWrapTypeT().
181 */
182 bool setWrapTypeT(const WrapType type) override;
183
184 /**
185 * Returns the name of the texture e.g., in a shader.
186 * @return The name of the texture
187 */
188 std::string textureName() const override;
189
190 /**
191 * Sets the name of the texture e.g., in a shader.
192 * @param name The name to set
193 * @return True, if succeeded
194 */
195 bool setTextureName(const std::string& name) override;
196
197 /**
198 * Returns the texture id of the primary texture.
199 * @return The id of the primary texture, 0 if invalid
200 */
201 inline GLuint primaryTextureId() const;
202
203 /**
204 * Returns the texture id of the secondary texture.
205 * @return The id of the secondary texture, 0 if invalid
206 */
207 inline GLuint secondaryTextureId() const;
208
209 /**
210 * Binds this texture.
211 * @see GLESTexture::bindTexture().
212 */
213 unsigned int bindTexture(GLESShaderProgram& shaderProgram, const unsigned int id) override;
214
215 /**
216 * Returns the texture properties of a given pixel format.
217 * @param pixelFormat The pixel format for which the properties will be returned, must be valid
218 * @return The properties of the pixel format, nullptr if the pixel format is not supported
219 */
221
222 protected:
223
224 /**
225 * Creates a new GLESceneGraph texture 2D object.
226 */
228
229 /**
230 * Destructs a GLESceneGraph texture 2D object.
231 */
232 ~GLESTexture2D() override;
233
234 /**
235 * Updates the mipmap for this texture.
236 */
237 void createMipmap() override;
238
239 /**
240 * Creates a new GLESceneGraph texture object.
241 * @param properties The texture properties of the frame type, must match the frame type
242 * @param frameType Frame type to create the new texture object for
243 * @return True, if succeeded
244 */
245 bool defineTextureObject(const TextureProperties& properties, const FrameType& frameType);
246
247 /**
248 * Creates a primary GLESceneGraph texture object.
249 * @param properties The texture properties of the frame type, must match the frame type
250 * @param frameType Frame type to create the new texture object for
251 * @return True, if succeeded
252 */
253 bool definePrimaryTextureObject(const TextureProperties& properties, const FrameType& frameType);
254
255 /**
256 * Creates a secondary GLESceneGraph texture object.
257 * @param properties The texture properties of the frame type, must match the frame type, must need a secondary texture
258 * @param frameType Frame type to create the new texture object for
259 * @return True, if succeeded
260 */
261 bool defineSecondaryTextureObject(const TextureProperties& properties, const FrameType& frameType);
262
263 /**
264 * Determines the unpack parameters for a plane of a frame.
265 * @param frame The frame providing the plane, must be valid
266 * @param planeIndex The index of the plane, with range [0, frame.numberPlanes())
267 * @param rowLength The resulting row length in plane pixels, as GL_UNPACK_ROW_LENGTH expects, with range [1, infinity)
268 * @param byteAlignment The resulting byte alignment, either 1, 2, or 4
269 * @return True, if succeeded
270 */
271 static bool determineAlignment(const Frame& frame, const unsigned int planeIndex, unsigned int& rowLength, unsigned int& byteAlignment);
272
273 /**
274 * Creates the map mapping all supported pixel formats to their texture properties.
275 * @return The map with the properties of all supported pixel formats
276 */
278
279 /**
280 * Adds the texture properties of one pixel format to a map.
281 * @param map The map to which the properties will be added
282 * @param pixelFormat The pixel format for which the properties will be added, must not be in the map already
283 * @param properties The properties of the pixel format
284 */
285 static void addTextureProperties(TexturePropertiesMap& map, const FrameType::PixelFormat pixelFormat, const TextureProperties& properties);
286
287 /**
288 * Adds the texture properties of one pixel format which needs to be converted before it can be uploaded.
289 * The properties are the properties of the pixel format the frame is converted to.
290 * @param map The map to which the properties will be added
291 * @param pixelFormat The pixel format for which the properties will be added, must not be in the map already
292 * @param internalPixelFormat The pixel format the frame needs to be converted to, must be in the map already and must not need a conversion itself
293 */
294 static void addConvertedTextureProperties(TexturePropertiesMap& map, const FrameType::PixelFormat pixelFormat, const FrameType::PixelFormat internalPixelFormat);
295
296 /**
297 * Returns the name of the primary texture.
298 * The input must have the format '<PRIMARY>,<SECONDARY>'.
299 * @param names The names of all textures, must be valid
300 * @param name The resulting name of the primary texture
301 * @return True, if succeeded
302 */
303 static bool primaryTextureName(const std::string& names, std::string& name);
304
305 /**
306 * Returns the name of the secondary texture.
307 * The input must have the format '<PRIMARY>,<SECONDARY>'.
308 * @param names The names of all textures, must be valid
309 * @param name The resulting name of the secondary texture
310 * @return True, if succeeded
311 */
312 static bool secondaryTextureName(const std::string& names, std::string& name);
313
314 protected:
315
316 /**
317 * Updates the texture based on a given frame.
318 * @param frame The frame to be used to update the texture, must be valid
319 * @return True, if succeeded
320 */
321 bool updateTexture(const Frame& frame);
322
323 protected:
324
325 /// The texture wrap s type.
326 WrapType wrapTypeS_ = WRAP_CLAMP;
327
328 /// The texture wrap t type.
329 WrapType wrapTypeT_ = WRAP_CLAMP;
330
331 /// The OpenGL ES primary texture id.
332 GLuint primaryTextureId_ = 0u;
333
334 /// The OpenGL ES secondary texture id.
335 GLuint secondaryTextureId_ = 0u;
336
337 /// The name of the texture in the shader.
338 std::string textureName_ = std::string("primaryTexture,secondaryTexture");
339
340 /// Optional temp conversion frame.
342};
343
348
350{
351 return primaryTextureId_;
352}
353
355{
356 return secondaryTextureId_;
357}
358
359}
360
361}
362
363}
364
365#endif // META_OCEAN_RENDERING_GLES_TEXTURE_2D_H
This class implements Ocean's image class.
Definition Frame.h:1969
Definition of a frame type composed by the frame dimension, pixel format and pixel origin.
Definition Frame.h:30
PixelFormat
Definition of all pixel formats available in the Ocean framework.
Definition Frame.h:183
This class is the base class for all dynamic scene graph objects.
Definition DynamicObject.h:40
ProgramType
Definition of different shader functionalities.
Definition GLESAttribute.h:43
This class implements a class factory for all GLESceneGraph objects.
Definition GLESFactory.h:30
This class implements a container for an OpenGL ES shader program.
Definition GLESShaderProgram.h:53
This class holds the texture properties of one pixel format.
Definition GLESTexture2D.h:60
TextureProperties(const GLESAttribute::ProgramType programType, const GLenum primaryFormat, const GLenum primaryType, const GLenum secondaryFormat, const GLenum secondaryType, const unsigned int secondaryWidthDivisor, const unsigned int secondaryHeightDivisor, const SecondaryLayout secondaryLayout)
Creates properties for a pixel format which needs a secondary texture as well.
bool secondaryTextureProperties(const FrameType &frameType, unsigned int &width, unsigned int &height, GLenum &format, GLenum &type) const
Determines the properties of the secondary texture for a given frame type.
void primaryTextureProperties(const FrameType &frameType, unsigned int &width, unsigned int &height, GLenum &format, GLenum &type) const
Determines the properties of the primary texture for a given frame type.
TextureProperties(const GLESAttribute::ProgramType programType, const GLenum primaryFormat, const GLenum primaryType)
Creates properties for a pixel format which needs one texture only.
SecondaryLayout secondaryLayout_
The layout of the secondary texture.
Definition GLESTexture2D.h:112
FrameType internalFrameType(const FrameType &frameType) const
Returns the frame type of the internal frame for which a GL texture format exists.
bool needsSecondaryTextureObjects() const
Returns whether a second texture is necessary.
Definition GLESTexture2D.h:344
This class wraps a GLESceneGraph texture object.
Definition GLESTexture2D.h:35
unsigned int bindTexture(GLESShaderProgram &shaderProgram, const unsigned int id) override
Binds this texture.
std::string textureName() const override
Returns the name of the texture e.g., in a shader.
GLESTexture2D()
Creates a new GLESceneGraph texture 2D object.
static bool determineAlignment(const Frame &frame, const unsigned int planeIndex, unsigned int &rowLength, unsigned int &byteAlignment)
Determines the unpack parameters for a plane of a frame.
void createMipmap() override
Updates the mipmap for this texture.
static void addConvertedTextureProperties(TexturePropertiesMap &map, const FrameType::PixelFormat pixelFormat, const FrameType::PixelFormat internalPixelFormat)
Adds the texture properties of one pixel format which needs to be converted before it can be uploaded...
bool defineSecondaryTextureObject(const TextureProperties &properties, const FrameType &frameType)
Creates a secondary GLESceneGraph texture object.
static TexturePropertiesMap createTexturePropertiesMap()
Creates the map mapping all supported pixel formats to their texture properties.
GLuint primaryTextureId_
The OpenGL ES primary texture id.
Definition GLESTexture2D.h:332
bool setWrapTypeT(const WrapType type) override
Sets the texture wrap type in t direction.
std::unordered_map< FrameType::PixelFormat, TextureProperties > TexturePropertiesMap
Definition of a map mapping pixel formats to texture properties.
Definition GLESTexture2D.h:156
static void addTextureProperties(TexturePropertiesMap &map, const FrameType::PixelFormat pixelFormat, const TextureProperties &properties)
Adds the texture properties of one pixel format to a map.
static bool primaryTextureName(const std::string &names, std::string &name)
Returns the name of the primary texture.
Frame conversionFrame_
Optional temp conversion frame.
Definition GLESTexture2D.h:341
bool setTextureName(const std::string &name) override
Sets the name of the texture e.g., in a shader.
WrapType wrapTypeS() const override
Returns the texture wrap type in s direction.
GLuint secondaryTextureId_
The OpenGL ES secondary texture id.
Definition GLESTexture2D.h:335
static const TextureProperties * textureProperties(const FrameType::PixelFormat pixelFormat)
Returns the texture properties of a given pixel format.
bool updateTexture(const Frame &frame)
Updates the texture based on a given frame.
WrapType wrapTypeT() const override
Returns the texture wrap type in t direction.
GLuint secondaryTextureId() const
Returns the texture id of the secondary texture.
Definition GLESTexture2D.h:354
bool setWrapTypeS(const WrapType type) override
Sets the texture wrap type in s direction.
SecondaryLayout
Definition of individual layouts of the secondary texture.
Definition GLESTexture2D.h:44
@ SL_NONE
The frame type does not need a secondary texture.
Definition GLESTexture2D.h:46
@ SL_PLANE_1_2
The second and the third plane of the frame are stacked into the secondary texture,...
Definition GLESTexture2D.h:50
@ SL_PLANE_1
The second plane of the frame is copied into the secondary texture.
Definition GLESTexture2D.h:48
GLuint primaryTextureId() const
Returns the texture id of the primary texture.
Definition GLESTexture2D.h:349
bool definePrimaryTextureObject(const TextureProperties &properties, const FrameType &frameType)
Creates a primary GLESceneGraph texture object.
~GLESTexture2D() override
Destructs a GLESceneGraph texture 2D object.
bool defineTextureObject(const TextureProperties &properties, const FrameType &frameType)
Creates a new GLESceneGraph texture object.
static bool secondaryTextureName(const std::string &names, std::string &name)
Returns the name of the secondary texture.
This class is the base class for all GLESceneGraph texture objects.
Definition GLESTexture.h:37
This class is the base class for all 2D textures.
Definition Texture2D.h:39
WrapType
Definition of different texture wrapping types.
Definition rendering/Texture.h:96
The namespace covering the entire Ocean framework.
Definition Accessor.h:15