blob: 7f2588f4279d21ac5db085118c71dffda8371778 [file] [log] [blame]
daniel@transgaming.com95a758f2012-07-12 15:17:06 +00001//
daniel@transgaming.com2b5af7b2012-09-27 17:46:15 +00002// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com95a758f2012-07-12 15:17:06 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Texture.h: Defines the abstract gl::Texture class and its concrete derived
8// classes Texture2D and TextureCubeMap. Implements GL texture objects and
9// related functionality. [OpenGL ES 2.0.24] section 3.7 page 63.
10
11#ifndef LIBGLESV2_TEXTURE_H_
12#define LIBGLESV2_TEXTURE_H_
13
14#include <vector>
15
16#define GL_APICALL
17#include <GLES2/gl2.h>
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000018
19#include "common/debug.h"
20#include "common/RefCountObject.h"
daniel@transgaming.comb9d7e6f2012-10-31 19:08:32 +000021#include "libGLESv2/renderer/Image.h"
daniel@transgaming.comb5e1a272012-10-31 19:10:00 +000022#include "libGLESv2/renderer/TextureStorage.h"
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000023#include "libGLESv2/Renderbuffer.h"
24#include "libGLESv2/utilities.h"
25
26namespace egl
27{
28class Surface;
29}
30
daniel@transgaming.com370482e2012-11-28 19:32:13 +000031namespace rx
32{
33class Renderer;
34}
35
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000036namespace gl
37{
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000038class Framebuffer;
39
40enum
41{
42 // These are the maximums the implementation can support
43 // The actual GL caps are limited by the device caps
44 // and should be queried from the Context
45 IMPLEMENTATION_MAX_TEXTURE_SIZE = 16384,
46 IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE = 16384,
47
48 IMPLEMENTATION_MAX_TEXTURE_LEVELS = 15 // 1+log2 of MAX_TEXTURE_SIZE
49};
50
daniel@transgaming.comebf139f2012-10-31 18:07:32 +000051struct SamplerState
52{
53 GLenum minFilter;
54 GLenum magFilter;
55 GLenum wrapS;
56 GLenum wrapT;
57 float maxAnisotropy;
58 int lodOffset;
59};
60
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000061class Texture : public RefCountObject
62{
63 public:
daniel@transgaming.com370482e2012-11-28 19:32:13 +000064 Texture(rx::Renderer *renderer, GLuint id);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000065
66 virtual ~Texture();
67
68 virtual void addProxyRef(const Renderbuffer *proxy) = 0;
69 virtual void releaseProxy(const Renderbuffer *proxy) = 0;
70
71 virtual GLenum getTarget() const = 0;
72
73 bool setMinFilter(GLenum filter);
74 bool setMagFilter(GLenum filter);
75 bool setWrapS(GLenum wrap);
76 bool setWrapT(GLenum wrap);
daniel@transgaming.com07ab8412012-07-12 15:17:09 +000077 bool setMaxAnisotropy(float textureMaxAnisotropy, float contextMaxAnisotropy);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000078 bool setUsage(GLenum usage);
79
80 GLenum getMinFilter() const;
81 GLenum getMagFilter() const;
82 GLenum getWrapS() const;
83 GLenum getWrapT() const;
daniel@transgaming.com07ab8412012-07-12 15:17:09 +000084 float getMaxAnisotropy() const;
daniel@transgaming.comebf139f2012-10-31 18:07:32 +000085 int getLodOffset();
86 void getSamplerState(SamplerState *sampler);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000087 GLenum getUsage() const;
daniel@transgaming.comca9a3c82012-10-26 18:55:07 +000088 bool isMipmapFiltered() const;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000089
90 virtual bool isSamplerComplete() const = 0;
91
daniel@transgaming.com87705f82012-12-20 21:10:45 +000092 rx::TextureStorageInterface *getNativeTexture();
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000093 virtual Renderbuffer *getRenderbuffer(GLenum target) = 0;
94
95 virtual void generateMipmaps() = 0;
96 virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source) = 0;
97
98 bool hasDirtyParameters() const;
99 bool hasDirtyImages() const;
100 void resetDirty();
101 unsigned int getTextureSerial();
102 unsigned int getRenderTargetSerial(GLenum target);
103
104 bool isImmutable() const;
105
106 static const GLuint INCOMPLETE_TEXTURE_ID = static_cast<GLuint>(-1); // Every texture takes an id at creation time. The value is arbitrary because it is never registered with the resource manager.
107
108 protected:
daniel@transgaming.com31b13e12012-11-28 19:34:30 +0000109 void setImage(GLint unpackAlignment, const void *pixels, rx::Image *image);
110 bool subImage(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels, rx::Image *image);
111 void setCompressedImage(GLsizei imageSize, const void *pixels, rx::Image *image);
112 bool subImageCompressed(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels, rx::Image *image);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000113
114 GLint creationLevels(GLsizei width, GLsizei height) const;
115 GLint creationLevels(GLsizei size) const;
116
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000117 virtual void createTexture() = 0;
118 virtual void updateTexture() = 0;
119 virtual void convertToRenderTarget() = 0;
daniel@transgaming.comd186dc72012-11-28 19:40:16 +0000120 virtual rx::RenderTarget *getRenderTarget(GLenum target) = 0;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000121
daniel@transgaming.com690d8ae2012-10-31 19:52:08 +0000122 virtual int levelCount() = 0;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000123
daniel@transgaming.com370482e2012-11-28 19:32:13 +0000124 rx::Renderer *mRenderer;
125
daniel@transgaming.comebf139f2012-10-31 18:07:32 +0000126 SamplerState mSamplerState;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000127 bool mDirtyParameters;
128 GLenum mUsage;
129
130 bool mDirtyImages;
131
132 bool mImmutable;
133
134 private:
135 DISALLOW_COPY_AND_ASSIGN(Texture);
136
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000137 virtual rx::TextureStorageInterface *getStorage(bool renderTarget) = 0;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000138};
139
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000140class Texture2D : public Texture
141{
142 public:
daniel@transgaming.com370482e2012-11-28 19:32:13 +0000143 Texture2D(rx::Renderer *renderer, GLuint id);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000144
145 ~Texture2D();
146
147 void addProxyRef(const Renderbuffer *proxy);
148 void releaseProxy(const Renderbuffer *proxy);
149
150 virtual GLenum getTarget() const;
151
152 GLsizei getWidth(GLint level) const;
153 GLsizei getHeight(GLint level) const;
154 GLenum getInternalFormat(GLint level) const;
daniel@transgaming.com20d36662012-10-31 19:51:43 +0000155 GLenum getActualFormat(GLint level) const;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000156 bool isCompressed(GLint level) const;
157 bool isDepth(GLint level) const;
158
159 void setImage(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
160 void setCompressedImage(GLint level, GLenum format, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels);
161 void subImage(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
162 void subImageCompressed(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels);
163 void copyImage(GLint level, GLenum format, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
164 virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
165 void storage(GLsizei levels, GLenum internalformat, GLsizei width, GLsizei height);
166
167 virtual bool isSamplerComplete() const;
168 virtual void bindTexImage(egl::Surface *surface);
169 virtual void releaseTexImage();
170
171 virtual void generateMipmaps();
172
173 virtual Renderbuffer *getRenderbuffer(GLenum target);
174
175 protected:
176 friend class RenderbufferTexture2D;
daniel@transgaming.comd186dc72012-11-28 19:40:16 +0000177 virtual rx::RenderTarget *getRenderTarget(GLenum target);
178 virtual rx::RenderTarget *getDepthStencil(GLenum target);
daniel@transgaming.com690d8ae2012-10-31 19:52:08 +0000179 virtual int levelCount();
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000180
181 private:
182 DISALLOW_COPY_AND_ASSIGN(Texture2D);
183
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000184 virtual void createTexture();
185 virtual void updateTexture();
186 virtual void convertToRenderTarget();
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000187 virtual rx::TextureStorageInterface *getStorage(bool renderTarget);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000188
189 bool isMipmapComplete() const;
190
daniel@transgaming.com6452adf2012-10-17 18:22:35 +0000191 void redefineImage(GLint level, GLint internalformat, GLsizei width, GLsizei height);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000192 void commitRect(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height);
193
daniel@transgaming.comd9ec9022012-12-20 20:52:16 +0000194 rx::Image *mImageArray[IMPLEMENTATION_MAX_TEXTURE_LEVELS];
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000195
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000196 rx::TextureStorageInterface2D *mTexStorage;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000197 egl::Surface *mSurface;
198
199 // A specific internal reference count is kept for colorbuffer proxy references,
200 // because, as the renderbuffer acting as proxy will maintain a binding pointer
201 // back to this texture, there would be a circular reference if we used a binding
202 // pointer here. This reference count will cause the pointer to be set to NULL if
203 // the count drops to zero, but will not cause deletion of the Renderbuffer.
204 Renderbuffer *mColorbufferProxy;
205 unsigned int mProxyRefs;
206};
207
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000208class TextureCubeMap : public Texture
209{
210 public:
daniel@transgaming.com370482e2012-11-28 19:32:13 +0000211 TextureCubeMap(rx::Renderer *renderer, GLuint id);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000212
213 ~TextureCubeMap();
214
215 void addProxyRef(const Renderbuffer *proxy);
216 void releaseProxy(const Renderbuffer *proxy);
217
218 virtual GLenum getTarget() const;
219
220 GLsizei getWidth(GLenum target, GLint level) const;
221 GLsizei getHeight(GLenum target, GLint level) const;
222 GLenum getInternalFormat(GLenum target, GLint level) const;
daniel@transgaming.com20d36662012-10-31 19:51:43 +0000223 GLenum getActualFormat(GLenum target, GLint level) const;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000224 bool isCompressed(GLenum target, GLint level) const;
225
226 void setImagePosX(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
227 void setImageNegX(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
228 void setImagePosY(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
229 void setImageNegY(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
230 void setImagePosZ(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
231 void setImageNegZ(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
232
233 void setCompressedImage(GLenum face, GLint level, GLenum format, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels);
234
235 void subImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
236 void subImageCompressed(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels);
237 void copyImage(GLenum target, GLint level, GLenum format, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
238 virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
239 void storage(GLsizei levels, GLenum internalformat, GLsizei size);
240
241 virtual bool isSamplerComplete() const;
242
243 virtual void generateMipmaps();
244
245 virtual Renderbuffer *getRenderbuffer(GLenum target);
246
247 static unsigned int faceIndex(GLenum face);
248
249 protected:
250 friend class RenderbufferTextureCubeMap;
daniel@transgaming.comd186dc72012-11-28 19:40:16 +0000251 virtual rx::RenderTarget *getRenderTarget(GLenum target);
daniel@transgaming.com690d8ae2012-10-31 19:52:08 +0000252 virtual int levelCount();
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000253
254 private:
255 DISALLOW_COPY_AND_ASSIGN(TextureCubeMap);
256
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000257 virtual void createTexture();
258 virtual void updateTexture();
259 virtual void convertToRenderTarget();
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000260 virtual rx::TextureStorageInterface *getStorage(bool renderTarget);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000261
262 bool isCubeComplete() const;
263 bool isMipmapCubeComplete() const;
264
265 void setImage(int faceIndex, GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
266 void commitRect(int faceIndex, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height);
daniel@transgaming.com6452adf2012-10-17 18:22:35 +0000267 void redefineImage(int faceIndex, GLint level, GLint internalformat, GLsizei width, GLsizei height);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000268
daniel@transgaming.comd9ec9022012-12-20 20:52:16 +0000269 rx::Image *mImageArray[6][IMPLEMENTATION_MAX_TEXTURE_LEVELS];
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000270
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000271 rx::TextureStorageInterfaceCube *mTexStorage;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000272
273 // A specific internal reference count is kept for colorbuffer proxy references,
274 // because, as the renderbuffer acting as proxy will maintain a binding pointer
275 // back to this texture, there would be a circular reference if we used a binding
276 // pointer here. This reference count will cause the pointer to be set to NULL if
277 // the count drops to zero, but will not cause deletion of the Renderbuffer.
278 Renderbuffer *mFaceProxies[6];
279 unsigned int *mFaceProxyRefs[6];
280};
281}
282
jbauman@chromium.org68715282012-07-12 23:28:41 +0000283#endif // LIBGLESV2_TEXTURE_H_