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