1 // Copyright (C) 2013 Patryk Nadrowski
2 // Heavily based on the OpenGL driver implemented by Nikolaus Gebhardt
3 // OpenGL ES driver implemented by Christian Stehno and first OpenGL ES 2.0
4 // driver implemented by Amundis.
5 // This file is part of the "Irrlicht Engine".
6 // For conditions of distribution and use, see copyright notice in Irrlicht.h
7 
8 #ifndef __C_OGLES2_TEXTURE_H_INCLUDED__
9 #define __C_OGLES2_TEXTURE_H_INCLUDED__
10 
11 #include "IrrCompileConfig.h"
12 
13 #ifdef _IRR_COMPILE_WITH_OGLES2_
14 
15 #ifdef IOS_STK
16 #include <OpenGLES/ES2/gl.h>
17 #else
18 #include <GLES2/gl2.h>
19 #endif
20 
21 #include "ITexture.h"
22 #include "IImage.h"
23 #include "SMaterialLayer.h"
24 
25 namespace irr
26 {
27 namespace video
28 {
29 
30 class COGLES2Driver;
31 
32 //! OpenGL ES 2.0 texture.
33 class COGLES2Texture : public ITexture
34 {
35 public:
36 
37 	//! Cache structure.
38 	struct SStatesCache
39 	{
SStatesCacheSStatesCache40 		SStatesCache() : WrapU(ETC_REPEAT), WrapV(ETC_REPEAT), BilinearFilter(false),
41 			TrilinearFilter(false), AnisotropicFilter(0), MipMapStatus(false), LODBias(0), IsCached(false)
42 		{
43 		}
44 
45 		u8 WrapU;
46 		u8 WrapV;
47 		bool BilinearFilter;
48 		bool TrilinearFilter;
49 		u8 AnisotropicFilter;
50 		bool MipMapStatus;
51 		s8 LODBias;
52 
53 		bool IsCached;
54 	};
55 
56 	//! constructor
57 	COGLES2Texture(IImage* surface, const io::path& name, void* mipmapData=0, COGLES2Driver* driver=0);
58 
59 	//! destructor
60 	virtual ~COGLES2Texture();
61 
62 	//! lock function
63 	virtual void* lock(E_TEXTURE_LOCK_MODE mode=ETLM_READ_WRITE, u32 mipmapLevel=0);
64 
65 	//! unlock function
66 	virtual void unlock();
67 
68 	//! Returns original size of the texture (image).
69 	virtual const core::dimension2d<u32>& getOriginalSize() const;
70 
71 	//! Returns size of the texture.
72 	virtual const core::dimension2d<u32>& getSize() const;
73 
74 	//! returns driver type of texture (=the driver, that created it)
75 	virtual E_DRIVER_TYPE getDriverType() const;
76 
77 	//! returns color format of texture
78 	virtual ECOLOR_FORMAT getColorFormat() const;
79 
80 	//! returns pitch of texture (in bytes)
81 	virtual u32 getPitch() const;
82 
83 	//! return open gl texture name
84 	virtual u32 getOpenGLTextureName() const;
85 
getHandle()86 	virtual u64 getHandle() { return 0; }
87 
88 	//! return whether this texture has mipmaps
89 	virtual bool hasMipMaps() const;
90 
91 	//! Regenerates the mip map levels of the texture.
92 	/** Useful after locking and modifying the texture
93 	\param mipmapData Pointer to raw mipmap data, including all necessary mip levels, in the same format as the main texture image. If not set the mipmaps are derived from the main image. */
94 	virtual void regenerateMipMapLevels(void* mipmapData=0);
95 
96 	//! Is it a render target?
97 	virtual bool isRenderTarget() const;
98 
99 	//! Is it a FrameBufferObject?
100 	virtual bool isFrameBufferObject() const;
101 
102 	//! Bind RenderTargetTexture
103 	virtual void bindRTT();
104 
105 	//! Unbind RenderTargetTexture
106 	virtual void unbindRTT();
107 
108 	//! sets whether this texture is intended to be used as a render target.
109 	void setIsRenderTarget(bool isTarget);
110 
111 	//! Get an access to texture states cache.
112 	SStatesCache& getStatesCache() const;
113 
setImage(IImage * new_image)114 	void setImage(IImage* new_image)
115 	{
116 		if (Image)
117 			Image->drop();
118 		Image = new_image;
119 	}
120 
121 protected:
122 
123 	//! protected constructor with basic setup, no GL texture name created, for derived classes
124 	COGLES2Texture(const io::path& name, COGLES2Driver* driver);
125 
126 	//! get the desired color format based on texture creation flags and the input format.
127 	ECOLOR_FORMAT getBestColorFormat(ECOLOR_FORMAT format);
128 
129 	//! get important numbers of the image and hw texture
130 	void getImageValues(IImage* image);
131 
132 	//! copies the texture into an OpenGL texture.
133 	/** \param newTexture True if method is called for a newly created texture for the first time. Otherwise call with false to improve memory handling.
134 	\param mipmapData Pointer to raw mipmap data, including all necessary mip levels, in the same format as the main texture image.
135 	\param mipLevel If set to non-zero, only that specific miplevel is updated, using the MipImage member. */
136 	void uploadTexture(bool newTexture=false, void* mipmapData=0, u32 mipLevel=0);
137 
138 	core::dimension2d<u32> ImageSize;
139 	core::dimension2d<u32> TextureSize;
140 	ECOLOR_FORMAT ColorFormat;
141 	COGLES2Driver* Driver;
142 	IImage* Image;
143 	IImage* MipImage;
144 
145 	GLuint TextureName;
146 	GLint InternalFormat;
147 	GLenum PixelFormat;
148 	GLenum PixelType;
149 
150 	u8 MipLevelStored;
151 	bool HasMipMaps;
152 	bool IsRenderTarget;
153 	bool AutomaticMipmapUpdate;
154 	bool ReadOnlyLock;
155 	bool KeepImage;
156 
157 	mutable SStatesCache StatesCache;
158 };
159 
160 //! OpenGL ES 2.0 FBO texture.
161 class COGLES2FBOTexture : public COGLES2Texture
162 {
163 public:
164 
165 	//! FrameBufferObject constructor
166 	COGLES2FBOTexture(const core::dimension2d<u32>& size, const io::path& name,
167 		COGLES2Driver* driver = 0, const ECOLOR_FORMAT format = ECF_UNKNOWN);
168 
169 	//! destructor
170 	virtual ~COGLES2FBOTexture();
171 
172 	//! Is it a FrameBufferObject?
173 	virtual bool isFrameBufferObject() const;
174 
175 	//! Bind RenderTargetTexture
176 	virtual void bindRTT();
177 
178 	//! Unbind RenderTargetTexture
179 	virtual void unbindRTT();
180 
181 	ITexture* DepthTexture;
182 	GLuint DepthBufferTexture;
183 protected:
184 	GLuint ColorFrameBuffer;
185 };
186 
187 
188 //! OpenGL ES 2.0 FBO depth texture.
189 class COGLES2FBODepthTexture : public COGLES2Texture
190 {
191 public:
192 	//! FrameBufferObject depth constructor
193 	COGLES2FBODepthTexture(const core::dimension2d<u32>& size, const io::path& name, COGLES2Driver* driver=0, bool useStencil=false);
194 
195 	//! destructor
196 	virtual ~COGLES2FBODepthTexture();
197 
198 	//! Bind RenderTargetTexture
199 	virtual void bindRTT();
200 
201 	//! Unbind RenderTargetTexture
202 	virtual void unbindRTT();
203 
204 	bool attach(ITexture*);
205 
206 protected:
207 	GLuint DepthRenderBuffer;
208 	GLuint StencilRenderBuffer;
209 	bool UseStencil;
210 };
211 
212 
213 } // end namespace video
214 } // end namespace irr
215 
216 #endif
217 #endif // _IRR_COMPILE_WITH_OGLES2_
218 
219