1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OGRE
4     (Object-oriented Graphics Rendering Engine)
5 For the latest info, see http://www.ogre3d.org/
6 
7 Copyright (c) 2000-2013 Torus Knot Software Ltd
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28 
29 #include "OgreGLES2TextureManager.h"
30 #include "OgreGLES2RenderTexture.h"
31 #include "OgreRoot.h"
32 #include "OgreRenderSystem.h"
33 #include "OgreGLES2StateCacheManager.h"
34 
35 namespace Ogre {
GLES2TextureManager(GLES2Support & support)36     GLES2TextureManager::GLES2TextureManager(GLES2Support& support)
37         : TextureManager(), mGLSupport(support), mWarningTextureID(0)
38     {
39         // Register with group manager
40         ResourceGroupManager::getSingleton()._registerResourceManager(mResourceType, this);
41     }
42 
~GLES2TextureManager()43     GLES2TextureManager::~GLES2TextureManager()
44     {
45         // Unregister with group manager
46         ResourceGroupManager::getSingleton()._unregisterResourceManager(mResourceType);
47 
48         // Delete warning texture
49         OGRE_CHECK_GL_ERROR(glDeleteTextures(1, &mWarningTextureID));
50     }
51 
createImpl(const String & name,ResourceHandle handle,const String & group,bool isManual,ManualResourceLoader * loader,const NameValuePairList * createParams)52     Resource* GLES2TextureManager::createImpl(const String& name, ResourceHandle handle,
53                                            const String& group, bool isManual,
54                                            ManualResourceLoader* loader,
55                                            const NameValuePairList* createParams)
56     {
57         return OGRE_NEW GLES2Texture(this, name, handle, group, isManual, loader, mGLSupport);
58     }
59 
60     //-----------------------------------------------------------------------------
createWarningTexture()61     void GLES2TextureManager::createWarningTexture()
62     {
63         // Generate warning texture
64         uint32 width = 8;
65         uint32 height = 8;
66 
67         uint16* data = new uint16[width * height];
68 
69         // Yellow/black stripes
70         for(size_t y = 0; y < height; ++y)
71         {
72             for(size_t x = 0; x < width; ++x)
73             {
74                 data[y * width + x] = (((x + y) % 8) < 4) ? 0x0000 : 0xFFF0;
75             }
76         }
77 
78         // Create GL resource
79         OGRE_CHECK_GL_ERROR(glGenTextures(1, &mWarningTextureID));
80         OGRE_CHECK_GL_ERROR(glBindTexture(GL_TEXTURE_2D, mWarningTextureID));
81         if(mGLSupport.checkExtension("GL_APPLE_texture_max_level") || gleswIsSupported(3, 0))
82             mGLSupport.getStateCacheManager()->setTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL_APPLE, 0);
83 
84         OGRE_CHECK_GL_ERROR(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
85                                          GL_UNSIGNED_SHORT_5_6_5, (void*)data));
86         // Free memory
87         delete [] data;
88     }
89 
getNativeFormat(TextureType ttype,PixelFormat format,int usage)90     PixelFormat GLES2TextureManager::getNativeFormat(TextureType ttype, PixelFormat format, int usage)
91     {
92         // Adjust requested parameters to capabilities
93         const RenderSystemCapabilities *caps = Root::getSingleton().getRenderSystem()->getCapabilities();
94 
95         // Check compressed texture support
96         // if a compressed format not supported, revert to PF_A8R8G8B8
97         if (PixelUtil::isCompressed(format) &&
98             !caps->hasCapability(RSC_TEXTURE_COMPRESSION_DXT) &&
99 			!caps->hasCapability(RSC_TEXTURE_COMPRESSION_PVRTC) &&
100 			!caps->hasCapability(RSC_TEXTURE_COMPRESSION_ATC) &&
101 			!caps->hasCapability(RSC_TEXTURE_COMPRESSION_ETC1))
102         {
103             return PF_A8R8G8B8;
104         }
105         // if floating point textures not supported, revert to PF_A8R8G8B8
106         if (PixelUtil::isFloatingPoint(format) &&
107             !caps->hasCapability(RSC_TEXTURE_FLOAT))
108         {
109             return PF_A8R8G8B8;
110         }
111 
112         // Check if this is a valid rendertarget format
113         if (usage & TU_RENDERTARGET)
114         {
115             /// Get closest supported alternative
116             /// If mFormat is supported it's returned
117             return GLES2RTTManager::getSingleton().getSupportedAlternative(format);
118         }
119 
120         // Supported
121         return format;
122     }
123 
isHardwareFilteringSupported(TextureType ttype,PixelFormat format,int usage,bool preciseFormatOnly)124     bool GLES2TextureManager::isHardwareFilteringSupported(TextureType ttype, PixelFormat format, int usage,
125             bool preciseFormatOnly)
126     {
127         if (format == PF_UNKNOWN)
128         {
129             return false;
130         }
131 
132         // Check native format
133         PixelFormat nativeFormat = getNativeFormat(ttype, format, usage);
134         if (preciseFormatOnly && format != nativeFormat)
135         {
136             return false;
137         }
138 
139         // Assume non-floating point is supported always
140         if (!PixelUtil::isFloatingPoint(nativeFormat))
141         {
142             return true;
143         }
144 
145         return false;
146     }
147 }
148