0029915: Porting to VC 2017 : Regressions in Modeling Algorithms on VC 2017
[occt.git] / src / OpenGl / OpenGl_Texture.hxx
CommitLineData
bf75be98 1// Created by: Kirill GAVRILOV
d5f74e42 2// Copyright (c) 2013-2014 OPEN CASCADE SAS
bf75be98 3//
973c2be1 4// This file is part of Open CASCADE Technology software library.
bf75be98 5//
d5f74e42 6// This library is free software; you can redistribute it and/or modify it under
7// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 8// by the Free Software Foundation, with special exception defined in the file
9// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10// distribution for complete text of the license and disclaimer of any warranty.
bf75be98 11//
973c2be1 12// Alternatively, this file may be used under the terms of Open CASCADE
13// commercial license or contractual agreement.
bf75be98 14
15#ifndef _OpenGl_Texture_H__
16#define _OpenGl_Texture_H__
17
18#include <OpenGl_GlCore13.hxx>
cc8cbabe 19#include <OpenGl_NamedResource.hxx>
20#include <OpenGl_Sampler.hxx>
bf75be98 21#include <Graphic3d_TypeOfTexture.hxx>
cc8cbabe 22#include <Graphic3d_TextureUnit.hxx>
bf75be98 23
c04c30b3 24class Graphic3d_TextureParams;
bf75be98 25class Image_PixMap;
26
68333c8f 27//! Selects preferable texture format for specified parameters.
28template<class T>
29struct OpenGl_TextureFormatSelector
30{
31 // Not implemented
32};
33
34template<>
35struct OpenGl_TextureFormatSelector<GLubyte>
36{
37 static GLint Internal (GLuint theChannels)
38 {
39 switch (theChannels)
40 {
41 case 1:
42 return GL_R8;
43 case 2:
44 return GL_RG8;
45 case 3:
46 return GL_RGB8;
47 case 4:
48 return GL_RGBA8;
49 default:
50 return GL_NONE;
51 }
52 }
fbef84f9 53
54 static GLint DataType()
55 {
56 return GL_UNSIGNED_BYTE;
57 }
68333c8f 58};
59
60template<>
61struct OpenGl_TextureFormatSelector<GLushort>
62{
63 static GLint Internal (GLuint theChannels)
64 {
65 switch (theChannels)
66 {
67 case 1:
68 return GL_R16;
69 case 2:
70 return GL_RG16;
71 case 3:
72 return GL_RGB16;
73 case 4:
74 return GL_RGBA16;
75 default:
76 return GL_NONE;
77 }
78 }
fbef84f9 79
80 static GLint DataType()
81 {
82 return GL_UNSIGNED_SHORT;
83 }
68333c8f 84};
85
86template<>
87struct OpenGl_TextureFormatSelector<GLfloat>
88{
89 static GLint Internal (GLuint theChannels)
90 {
91 switch (theChannels)
92 {
93 case 1:
94 return GL_R32F;
95 case 2:
96 return GL_RG32F;
97 case 3:
98 return GL_RGB32F;
99 case 4:
100 return GL_RGBA32F;
101 default:
102 return GL_NONE;
103 }
104 }
fbef84f9 105
106 static GLint DataType()
107 {
108 return GL_FLOAT;
109 }
110};
111
112template<>
113struct OpenGl_TextureFormatSelector<GLuint>
114{
115 static GLint Internal (GLuint theChannels)
116 {
117 switch (theChannels)
118 {
119 case 1:
120 return GL_RED;
121 case 2:
122 return GL_RG;
123 case 3:
124 return GL_RGB;
125 case 4:
126 return GL_RGBA;
127 default:
128 return GL_NONE;
129 }
130 }
131
132 static GLint DataType()
133 {
134 return GL_UNSIGNED_INT;
135 }
136};
137
138//! Only unsigned formats are available in OpenGL ES 2.0
139#if !defined(GL_ES_VERSION_2_0)
140template<>
141struct OpenGl_TextureFormatSelector<GLbyte>
142{
143 static GLint Internal (GLuint theChannels)
144 {
145 switch (theChannels)
146 {
147 case 1:
148 return GL_R8_SNORM;
149 case 2:
150 return GL_RG8_SNORM;
151 case 3:
152 return GL_RGB8_SNORM;
153 case 4:
154 return GL_RGBA8_SNORM;
155 default:
156 return GL_NONE;
157 }
158 }
159
160 static GLint DataType()
161 {
162 return GL_BYTE;
163 }
164};
165
166template<>
167struct OpenGl_TextureFormatSelector<GLshort>
168{
169 static GLint Internal (GLuint theChannels)
170 {
171 switch (theChannels)
172 {
173 case 1:
174 return GL_R16_SNORM;
175 case 2:
176 return GL_RG16_SNORM;
177 case 3:
178 return GL_RGB16_SNORM;
179 case 4:
180 return GL_RGBA16_SNORM;
181 default:
182 return GL_NONE;
183 }
184 }
185
186 static GLint DataType()
187 {
188 return GL_SHORT;
189 }
190};
191
192template<>
193struct OpenGl_TextureFormatSelector<GLint>
194{
195 static GLint Internal (GLuint theChannels)
196 {
197 switch (theChannels)
198 {
199 case 1:
200 return GL_RED_SNORM;
201 case 2:
202 return GL_RG_SNORM;
203 case 3:
204 return GL_RGB_SNORM;
205 case 4:
206 return GL_RGBA_SNORM;
207 default:
208 return GL_NONE;
209 }
210 }
211
212 static GLint DataType()
213 {
214 return GL_INT;
215 }
68333c8f 216};
fbef84f9 217#endif
68333c8f 218
219//! Stores parameters of OpenGL texture format.
220class OpenGl_TextureFormat
221{
222 friend class OpenGl_Texture;
223
224public:
225
226 //! Returns OpenGL format of the pixel data.
227 inline GLenum Format() const
228 {
229 switch (myChannels)
230 {
231 case 1:
232 return GL_RED;
233 case 2:
234 return GL_RG;
235 case 3:
236 return GL_RGB;
237 case 4:
238 return GL_RGBA;
239 default:
240 return GL_NONE;
241 }
242 }
243
244 //! Returns OpenGL internal format of the pixel data.
245 inline GLint Internal() const
246 {
247 return myInternal;
248 }
249
fbef84f9 250 //! Returns OpenGL data type of the pixel data.
251 inline GLint DataType() const
252 {
253 return myDataType;
254 }
255
68333c8f 256 //! Returns texture format for specified type and number of channels.
257 template<class T, int N>
258 static OpenGl_TextureFormat Create()
259 {
fbef84f9 260 return OpenGl_TextureFormat (N,
261 OpenGl_TextureFormatSelector<T>::Internal(N),
262 OpenGl_TextureFormatSelector<T>::DataType());
68333c8f 263 }
264
265private:
266
267 //! Creates new texture format.
5322131b 268 OpenGl_TextureFormat (const GLint theChannels,
fbef84f9 269 const GLint theInternal,
270 const GLint theDataType)
5322131b 271 : myInternal (theInternal),
fbef84f9 272 myChannels (theChannels),
273 myDataType (theDataType) {}
68333c8f 274
275private:
276
5322131b 277 GLint myInternal; //!< OpenGL internal format of the pixel data
278 GLint myChannels; //!< Number of channels for each pixel (from 1 to 4)
fbef84f9 279 GLint myDataType; //!< OpenGL data type of input pixel data
68333c8f 280
68333c8f 281};
282
bf75be98 283//! Texture resource.
cc8cbabe 284class OpenGl_Texture : public OpenGl_NamedResource
bf75be98 285{
cc8cbabe 286 DEFINE_STANDARD_RTTIEXT(OpenGl_Texture, OpenGl_NamedResource)
bf75be98 287public:
288
289 //! Helpful constants
290 static const GLuint NO_TEXTURE = 0;
291
15669413 292 //! Return pixel size of pixel format in bytes.
293 //! Note that this method considers that OpenGL natively supports this pixel format,
294 //! which might be not the case - in the latter case, actual pixel size might differ!
295 Standard_EXPORT static Standard_Size PixelSizeOfPixelFormat (Standard_Integer theInternalFormat);
296
bf75be98 297public:
298
cc8cbabe 299 //! Create uninitialized texture.
300 Standard_EXPORT OpenGl_Texture (const TCollection_AsciiString& theResourceId = TCollection_AsciiString(),
301 const Handle(Graphic3d_TextureParams)& theParams = Handle(Graphic3d_TextureParams)());
bf75be98 302
303 //! Destroy object.
304 Standard_EXPORT virtual ~OpenGl_Texture();
305
306 //! @return true if current object was initialized
307 inline bool IsValid() const
308 {
309 return myTextureId != NO_TEXTURE;
310 }
311
312 //! @return target to which the texture is bound (GL_TEXTURE_1D, GL_TEXTURE_2D)
313 inline GLenum GetTarget() const
314 {
315 return myTarget;
316 }
317
a174a3c5 318 //! @return texture width (0 LOD)
319 inline GLsizei SizeX() const
320 {
321 return mySizeX;
322 }
323
324 //! @return texture height (0 LOD)
325 inline GLsizei SizeY() const
326 {
327 return mySizeY;
328 }
329
330 //! @return texture ID
331 inline GLuint TextureId() const
332 {
333 return myTextureId;
334 }
335
8625ef7e 336 //! @return texture format (not sized)
337 inline GLenum GetFormat() const
a577aaab 338 {
339 return myTextFormat;
340 }
341
4e1523ef 342 //! Return true for GL_RED and GL_ALPHA formats.
343 bool IsAlpha() const
344 {
345 return myIsAlpha;
346 }
347
348 //! Setup to interprete the format as Alpha by Shader Manager
349 //! (should be GL_ALPHA within compatible context or GL_RED otherwise).
350 void SetAlpha (const bool theValue)
351 {
352 myIsAlpha = theValue;
353 }
354
bf75be98 355 //! Creates Texture id if not yet generated.
356 //! Data should be initialized by another method.
357 Standard_EXPORT bool Create (const Handle(OpenGl_Context)& theCtx);
358
359 //! Destroy object - will release GPU memory if any.
79104795 360 Standard_EXPORT virtual void Release (OpenGl_Context* theCtx) Standard_OVERRIDE;
bf75be98 361
cc8cbabe 362 //! Return texture sampler.
363 const Handle(OpenGl_Sampler)& Sampler() const { return mySampler; }
364
365 //! Set texture sampler.
366 void SetSampler (const Handle(OpenGl_Sampler)& theSampler) { mySampler = theSampler; }
367
368 //! Initialize the Sampler Object (as OpenGL object).
369 //! @param theCtx currently bound OpenGL context
370 Standard_EXPORT bool InitSamplerObject (const Handle(OpenGl_Context)& theCtx);
371
372 //! Bind this Texture to the unit specified in sampler parameters.
373 //! Also binds Sampler Object if it is allocated.
374 void Bind (const Handle(OpenGl_Context)& theCtx) const
375 {
376 Bind (theCtx, mySampler->Parameters()->TextureUnit());
377 }
378
379 //! Unbind texture from the unit specified in sampler parameters.
380 //! Also unbinds Sampler Object if it is allocated.
381 void Unbind (const Handle(OpenGl_Context)& theCtx) const
382 {
383 Unbind (theCtx, mySampler->Parameters()->TextureUnit());
384 }
385
bf75be98 386 //! Bind this Texture to specified unit.
cc8cbabe 387 //! Also binds Sampler Object if it is allocated.
bf75be98 388 Standard_EXPORT void Bind (const Handle(OpenGl_Context)& theCtx,
cc8cbabe 389 const Graphic3d_TextureUnit theTextureUnit) const;
bf75be98 390
391 //! Unbind texture from specified unit.
cc8cbabe 392 //! Also unbinds Sampler Object if it is allocated.
bf75be98 393 Standard_EXPORT void Unbind (const Handle(OpenGl_Context)& theCtx,
cc8cbabe 394 const Graphic3d_TextureUnit theTextureUnit) const;
bf75be98 395
d2edda76 396 //! Revision of associated data source.
397 Standard_Size Revision() const { return myRevision; }
398
399 //! Set revision of associated data source.
400 void SetRevision (const Standard_Size theRevision) { myRevision = theRevision; }
401
bf75be98 402 //! Notice that texture will be unbound after this call.
403 Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theCtx,
404 const Image_PixMap& theImage,
405 const Graphic3d_TypeOfTexture theType);
406
18f4e8e2 407 //! Initialize the texture with specified format, size and texture type.
408 //! If theImage is empty the texture data will contain trash.
409 //! Notice that texture will be unbound after this call.
410 Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theCtx,
411 const GLint theTextFormat,
412 const GLenum thePixelFormat,
413 const GLenum theDataType,
414 const GLsizei theSizeX,
415 const GLsizei theSizeY,
416 const Graphic3d_TypeOfTexture theType,
417 const Image_PixMap* theImage = NULL);
418
3c4b62a4 419 //! Initialize the 2D multisampling texture using glTexImage2DMultisample().
420 Standard_EXPORT bool Init2DMultisample (const Handle(OpenGl_Context)& theCtx,
421 const GLsizei theNbSamples,
422 const GLint theTextFormat,
423 const GLsizei theSizeX,
424 const GLsizei theSizeY);
425
68333c8f 426 //! Allocates texture rectangle with specified format and size.
427 //! \note Texture data is not initialized (will contain trash).
428 Standard_EXPORT bool InitRectangle (const Handle(OpenGl_Context)& theCtx,
429 const Standard_Integer theSizeX,
430 const Standard_Integer theSizeY,
431 const OpenGl_TextureFormat& theFormat);
432
74fb257d 433 //! Initializes 3D texture rectangle with specified format and size.
434 Standard_EXPORT bool Init3D (const Handle(OpenGl_Context)& theCtx,
435 const GLint theTextFormat,
436 const GLenum thePixelFormat,
437 const GLenum theDataType,
438 const Standard_Integer theSizeX,
439 const Standard_Integer theSizeY,
440 const Standard_Integer theSizeZ,
441 const void* thePixels);
442
bf75be98 443 //! @return true if texture was generated within mipmaps
cc8cbabe 444 Standard_Boolean HasMipmaps() const { return myHasMipmaps; }
bf75be98 445
18f4e8e2 446 //! Return texture type and format by Image_PixMap data format.
447 Standard_EXPORT static bool GetDataFormat (const Handle(OpenGl_Context)& theCtx,
448 const Image_PixMap& theData,
449 GLint& theTextFormat,
450 GLenum& thePixelFormat,
451 GLenum& theDataType);
452
15669413 453 //! Returns estimated GPU memory usage for holding data without considering overheads and allocation alignment rules.
454 Standard_EXPORT virtual Standard_Size EstimatedDataSize() const Standard_OVERRIDE;
455
bf75be98 456protected:
457
cc8cbabe 458 //! Apply default sampler parameters after texture creation.
459 Standard_EXPORT void applyDefaultSamplerParams (const Handle(OpenGl_Context)& theCtx);
460
461protected:
462
463 Handle(OpenGl_Sampler) mySampler; //!< texture sampler
d2edda76 464 Standard_Size myRevision; //!< revision of associated data source
bf75be98 465 GLuint myTextureId; //!< GL resource ID
74fb257d 466 GLenum myTarget; //!< GL_TEXTURE_1D/GL_TEXTURE_2D/GL_TEXTURE_3D
bf75be98 467 GLsizei mySizeX; //!< texture width
468 GLsizei mySizeY; //!< texture height
74fb257d 469 GLsizei mySizeZ; //!< texture depth
8625ef7e 470 GLenum myTextFormat; //!< texture format - GL_RGB, GL_RGBA,...
15669413 471 GLint mySizedFormat;//!< internal (sized) texture format
472 Standard_Integer myNbSamples; //!< number of MSAA samples
bf75be98 473 Standard_Boolean myHasMipmaps; //!< flag indicates that texture was uploaded with mipmaps
4e1523ef 474 bool myIsAlpha; //!< indicates alpha format
bf75be98 475
bf75be98 476};
477
cc8cbabe 478DEFINE_STANDARD_HANDLE(OpenGl_Texture, OpenGl_NamedResource)
479
bf75be98 480#endif // _OpenGl_Texture_H__