blob: a78544eb520fc964f44af3dd1fefe140c480e706 [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"
daniel@transgaming.com8bc304a2013-01-11 04:07:42 +000025#include "libGLESv2/angletypes.h"
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000026
27namespace egl
28{
29class Surface;
30}
31
daniel@transgaming.com370482e2012-11-28 19:32:13 +000032namespace rx
33{
34class Renderer;
35}
36
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000037namespace gl
38{
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000039class Framebuffer;
40
41enum
42{
43 // These are the maximums the implementation can support
44 // The actual GL caps are limited by the device caps
45 // and should be queried from the Context
46 IMPLEMENTATION_MAX_TEXTURE_SIZE = 16384,
47 IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE = 16384,
48
49 IMPLEMENTATION_MAX_TEXTURE_LEVELS = 15 // 1+log2 of MAX_TEXTURE_SIZE
50};
51
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000052class Texture : public RefCountObject
53{
54 public:
daniel@transgaming.com370482e2012-11-28 19:32:13 +000055 Texture(rx::Renderer *renderer, GLuint id);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000056
57 virtual ~Texture();
58
59 virtual void addProxyRef(const Renderbuffer *proxy) = 0;
60 virtual void releaseProxy(const Renderbuffer *proxy) = 0;
61
62 virtual GLenum getTarget() const = 0;
63
64 bool setMinFilter(GLenum filter);
65 bool setMagFilter(GLenum filter);
66 bool setWrapS(GLenum wrap);
67 bool setWrapT(GLenum wrap);
daniel@transgaming.com07ab8412012-07-12 15:17:09 +000068 bool setMaxAnisotropy(float textureMaxAnisotropy, float contextMaxAnisotropy);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000069 bool setUsage(GLenum usage);
70
71 GLenum getMinFilter() const;
72 GLenum getMagFilter() const;
73 GLenum getWrapS() const;
74 GLenum getWrapT() const;
daniel@transgaming.com07ab8412012-07-12 15:17:09 +000075 float getMaxAnisotropy() const;
daniel@transgaming.comebf139f2012-10-31 18:07:32 +000076 int getLodOffset();
77 void getSamplerState(SamplerState *sampler);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000078 GLenum getUsage() const;
daniel@transgaming.comca9a3c82012-10-26 18:55:07 +000079 bool isMipmapFiltered() const;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000080
81 virtual bool isSamplerComplete() const = 0;
82
daniel@transgaming.com87705f82012-12-20 21:10:45 +000083 rx::TextureStorageInterface *getNativeTexture();
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000084 virtual Renderbuffer *getRenderbuffer(GLenum target) = 0;
85
86 virtual void generateMipmaps() = 0;
87 virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source) = 0;
88
89 bool hasDirtyParameters() const;
90 bool hasDirtyImages() const;
91 void resetDirty();
92 unsigned int getTextureSerial();
93 unsigned int getRenderTargetSerial(GLenum target);
94
95 bool isImmutable() const;
96
97 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.
98
99 protected:
daniel@transgaming.com31b13e12012-11-28 19:34:30 +0000100 void setImage(GLint unpackAlignment, const void *pixels, rx::Image *image);
101 bool subImage(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels, rx::Image *image);
102 void setCompressedImage(GLsizei imageSize, const void *pixels, rx::Image *image);
103 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 +0000104
105 GLint creationLevels(GLsizei width, GLsizei height) const;
106 GLint creationLevels(GLsizei size) const;
107
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000108 virtual void createTexture() = 0;
109 virtual void updateTexture() = 0;
110 virtual void convertToRenderTarget() = 0;
daniel@transgaming.comd186dc72012-11-28 19:40:16 +0000111 virtual rx::RenderTarget *getRenderTarget(GLenum target) = 0;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000112
daniel@transgaming.com690d8ae2012-10-31 19:52:08 +0000113 virtual int levelCount() = 0;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000114
daniel@transgaming.com370482e2012-11-28 19:32:13 +0000115 rx::Renderer *mRenderer;
116
daniel@transgaming.comebf139f2012-10-31 18:07:32 +0000117 SamplerState mSamplerState;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000118 bool mDirtyParameters;
119 GLenum mUsage;
120
121 bool mDirtyImages;
122
123 bool mImmutable;
124
125 private:
126 DISALLOW_COPY_AND_ASSIGN(Texture);
127
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000128 virtual rx::TextureStorageInterface *getStorage(bool renderTarget) = 0;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000129};
130
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000131class Texture2D : public Texture
132{
133 public:
daniel@transgaming.com370482e2012-11-28 19:32:13 +0000134 Texture2D(rx::Renderer *renderer, GLuint id);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000135
136 ~Texture2D();
137
138 void addProxyRef(const Renderbuffer *proxy);
139 void releaseProxy(const Renderbuffer *proxy);
140
141 virtual GLenum getTarget() const;
142
143 GLsizei getWidth(GLint level) const;
144 GLsizei getHeight(GLint level) const;
145 GLenum getInternalFormat(GLint level) const;
daniel@transgaming.com20d36662012-10-31 19:51:43 +0000146 GLenum getActualFormat(GLint level) const;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000147 bool isCompressed(GLint level) const;
148 bool isDepth(GLint level) const;
149
150 void setImage(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
151 void setCompressedImage(GLint level, GLenum format, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels);
152 void subImage(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
153 void subImageCompressed(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels);
154 void copyImage(GLint level, GLenum format, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
155 virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
156 void storage(GLsizei levels, GLenum internalformat, GLsizei width, GLsizei height);
157
158 virtual bool isSamplerComplete() const;
159 virtual void bindTexImage(egl::Surface *surface);
160 virtual void releaseTexImage();
161
162 virtual void generateMipmaps();
163
164 virtual Renderbuffer *getRenderbuffer(GLenum target);
165
166 protected:
167 friend class RenderbufferTexture2D;
daniel@transgaming.comd186dc72012-11-28 19:40:16 +0000168 virtual rx::RenderTarget *getRenderTarget(GLenum target);
169 virtual rx::RenderTarget *getDepthStencil(GLenum target);
daniel@transgaming.com690d8ae2012-10-31 19:52:08 +0000170 virtual int levelCount();
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000171
172 private:
173 DISALLOW_COPY_AND_ASSIGN(Texture2D);
174
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000175 virtual void createTexture();
176 virtual void updateTexture();
177 virtual void convertToRenderTarget();
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000178 virtual rx::TextureStorageInterface *getStorage(bool renderTarget);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000179
180 bool isMipmapComplete() const;
181
daniel@transgaming.com6452adf2012-10-17 18:22:35 +0000182 void redefineImage(GLint level, GLint internalformat, GLsizei width, GLsizei height);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000183 void commitRect(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height);
184
daniel@transgaming.comd9ec9022012-12-20 20:52:16 +0000185 rx::Image *mImageArray[IMPLEMENTATION_MAX_TEXTURE_LEVELS];
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000186
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000187 rx::TextureStorageInterface2D *mTexStorage;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000188 egl::Surface *mSurface;
189
190 // A specific internal reference count is kept for colorbuffer proxy references,
191 // because, as the renderbuffer acting as proxy will maintain a binding pointer
192 // back to this texture, there would be a circular reference if we used a binding
193 // pointer here. This reference count will cause the pointer to be set to NULL if
194 // the count drops to zero, but will not cause deletion of the Renderbuffer.
195 Renderbuffer *mColorbufferProxy;
196 unsigned int mProxyRefs;
197};
198
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000199class TextureCubeMap : public Texture
200{
201 public:
daniel@transgaming.com370482e2012-11-28 19:32:13 +0000202 TextureCubeMap(rx::Renderer *renderer, GLuint id);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000203
204 ~TextureCubeMap();
205
206 void addProxyRef(const Renderbuffer *proxy);
207 void releaseProxy(const Renderbuffer *proxy);
208
209 virtual GLenum getTarget() const;
210
211 GLsizei getWidth(GLenum target, GLint level) const;
212 GLsizei getHeight(GLenum target, GLint level) const;
213 GLenum getInternalFormat(GLenum target, GLint level) const;
daniel@transgaming.com20d36662012-10-31 19:51:43 +0000214 GLenum getActualFormat(GLenum target, GLint level) const;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000215 bool isCompressed(GLenum target, GLint level) const;
216
217 void setImagePosX(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
218 void setImageNegX(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
219 void setImagePosY(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
220 void setImageNegY(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
221 void setImagePosZ(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
222 void setImageNegZ(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
223
224 void setCompressedImage(GLenum face, GLint level, GLenum format, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels);
225
226 void subImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
227 void subImageCompressed(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels);
228 void copyImage(GLenum target, GLint level, GLenum format, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
229 virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
230 void storage(GLsizei levels, GLenum internalformat, GLsizei size);
231
232 virtual bool isSamplerComplete() const;
233
234 virtual void generateMipmaps();
235
236 virtual Renderbuffer *getRenderbuffer(GLenum target);
237
238 static unsigned int faceIndex(GLenum face);
239
240 protected:
241 friend class RenderbufferTextureCubeMap;
daniel@transgaming.comd186dc72012-11-28 19:40:16 +0000242 virtual rx::RenderTarget *getRenderTarget(GLenum target);
daniel@transgaming.com690d8ae2012-10-31 19:52:08 +0000243 virtual int levelCount();
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000244
245 private:
246 DISALLOW_COPY_AND_ASSIGN(TextureCubeMap);
247
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000248 virtual void createTexture();
249 virtual void updateTexture();
250 virtual void convertToRenderTarget();
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000251 virtual rx::TextureStorageInterface *getStorage(bool renderTarget);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000252
253 bool isCubeComplete() const;
254 bool isMipmapCubeComplete() const;
255
256 void setImage(int faceIndex, GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
257 void commitRect(int faceIndex, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height);
daniel@transgaming.com6452adf2012-10-17 18:22:35 +0000258 void redefineImage(int faceIndex, GLint level, GLint internalformat, GLsizei width, GLsizei height);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000259
daniel@transgaming.comd9ec9022012-12-20 20:52:16 +0000260 rx::Image *mImageArray[6][IMPLEMENTATION_MAX_TEXTURE_LEVELS];
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000261
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000262 rx::TextureStorageInterfaceCube *mTexStorage;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000263
264 // A specific internal reference count is kept for colorbuffer proxy references,
265 // because, as the renderbuffer acting as proxy will maintain a binding pointer
266 // back to this texture, there would be a circular reference if we used a binding
267 // pointer here. This reference count will cause the pointer to be set to NULL if
268 // the count drops to zero, but will not cause deletion of the Renderbuffer.
269 Renderbuffer *mFaceProxies[6];
270 unsigned int *mFaceProxyRefs[6];
271};
272}
273
jbauman@chromium.org68715282012-07-12 23:28:41 +0000274#endif // LIBGLESV2_TEXTURE_H_