blob: 71b98787fb0973bc9151bc42271d85cd241d5ce9 [file] [log] [blame]
daniel@transgaming.com95a758f2012-07-12 15:17:06 +00001//
shannon.woods@transgaming.come2e97982013-02-28 23:18:50 +00002// Copyright (c) 2002-2013 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
shannon.woods%transgaming.com@gtempaccount.comf26ddae2013-04-13 03:29:13 +000017#include <GLES3/gl3.h>
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000018#include <GLES2/gl2.h>
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000019
20#include "common/debug.h"
21#include "common/RefCountObject.h"
daniel@transgaming.com8bc304a2013-01-11 04:07:42 +000022#include "libGLESv2/angletypes.h"
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000023
24namespace egl
25{
26class Surface;
27}
28
daniel@transgaming.com370482e2012-11-28 19:32:13 +000029namespace rx
30{
31class Renderer;
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000032class TextureStorageInterface;
33class TextureStorageInterface2D;
34class TextureStorageInterfaceCube;
35class RenderTarget;
36class Image;
daniel@transgaming.com370482e2012-11-28 19:32:13 +000037}
38
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000039namespace gl
40{
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000041class Framebuffer;
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000042class Renderbuffer;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000043
44enum
45{
46 // These are the maximums the implementation can support
47 // The actual GL caps are limited by the device caps
48 // and should be queried from the Context
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +000049 IMPLEMENTATION_MAX_2D_TEXTURE_SIZE = 16384,
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000050 IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE = 16384,
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +000051 IMPLEMENTATION_MAX_3D_TEXTURE_SIZE = 2048,
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000052
53 IMPLEMENTATION_MAX_TEXTURE_LEVELS = 15 // 1+log2 of MAX_TEXTURE_SIZE
54};
55
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000056class Texture : public RefCountObject
57{
58 public:
daniel@transgaming.com370482e2012-11-28 19:32:13 +000059 Texture(rx::Renderer *renderer, GLuint id);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000060
61 virtual ~Texture();
62
63 virtual void addProxyRef(const Renderbuffer *proxy) = 0;
64 virtual void releaseProxy(const Renderbuffer *proxy) = 0;
65
66 virtual GLenum getTarget() const = 0;
67
68 bool setMinFilter(GLenum filter);
69 bool setMagFilter(GLenum filter);
70 bool setWrapS(GLenum wrap);
71 bool setWrapT(GLenum wrap);
daniel@transgaming.com07ab8412012-07-12 15:17:09 +000072 bool setMaxAnisotropy(float textureMaxAnisotropy, float contextMaxAnisotropy);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000073 bool setUsage(GLenum usage);
74
75 GLenum getMinFilter() const;
76 GLenum getMagFilter() const;
77 GLenum getWrapS() const;
78 GLenum getWrapT() const;
daniel@transgaming.com07ab8412012-07-12 15:17:09 +000079 float getMaxAnisotropy() const;
daniel@transgaming.comebf139f2012-10-31 18:07:32 +000080 int getLodOffset();
81 void getSamplerState(SamplerState *sampler);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000082 GLenum getUsage() const;
daniel@transgaming.comca9a3c82012-10-26 18:55:07 +000083 bool isMipmapFiltered() const;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000084
85 virtual bool isSamplerComplete() const = 0;
86
daniel@transgaming.com87705f82012-12-20 21:10:45 +000087 rx::TextureStorageInterface *getNativeTexture();
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000088 virtual Renderbuffer *getRenderbuffer(GLenum target) = 0;
89
90 virtual void generateMipmaps() = 0;
91 virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source) = 0;
92
93 bool hasDirtyParameters() const;
94 bool hasDirtyImages() const;
95 void resetDirty();
96 unsigned int getTextureSerial();
97 unsigned int getRenderTargetSerial(GLenum target);
98
99 bool isImmutable() const;
100
101 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.
102
103 protected:
daniel@transgaming.com31b13e12012-11-28 19:34:30 +0000104 void setImage(GLint unpackAlignment, const void *pixels, rx::Image *image);
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000105 bool subImage(GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth,
106 GLenum format, GLenum type, GLint unpackAlignment, const void *pixels, rx::Image *image);
daniel@transgaming.com31b13e12012-11-28 19:34:30 +0000107 void setCompressedImage(GLsizei imageSize, const void *pixels, rx::Image *image);
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000108 bool subImageCompressed(GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth,
109 GLenum format, GLsizei imageSize, const void *pixels, rx::Image *image);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000110
111 GLint creationLevels(GLsizei width, GLsizei height) const;
112 GLint creationLevels(GLsizei size) const;
113
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000114 virtual void createTexture() = 0;
115 virtual void updateTexture() = 0;
116 virtual void convertToRenderTarget() = 0;
daniel@transgaming.comd186dc72012-11-28 19:40:16 +0000117 virtual rx::RenderTarget *getRenderTarget(GLenum target) = 0;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000118
daniel@transgaming.com690d8ae2012-10-31 19:52:08 +0000119 virtual int levelCount() = 0;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000120
daniel@transgaming.com370482e2012-11-28 19:32:13 +0000121 rx::Renderer *mRenderer;
122
daniel@transgaming.comebf139f2012-10-31 18:07:32 +0000123 SamplerState mSamplerState;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000124 GLenum mUsage;
125
126 bool mDirtyImages;
127
128 bool mImmutable;
129
130 private:
131 DISALLOW_COPY_AND_ASSIGN(Texture);
132
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000133 virtual rx::TextureStorageInterface *getStorage(bool renderTarget) = 0;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000134};
135
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000136class Texture2D : public Texture
137{
138 public:
daniel@transgaming.com370482e2012-11-28 19:32:13 +0000139 Texture2D(rx::Renderer *renderer, GLuint id);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000140
141 ~Texture2D();
142
143 void addProxyRef(const Renderbuffer *proxy);
144 void releaseProxy(const Renderbuffer *proxy);
145
146 virtual GLenum getTarget() const;
147
148 GLsizei getWidth(GLint level) const;
149 GLsizei getHeight(GLint level) const;
150 GLenum getInternalFormat(GLint level) const;
daniel@transgaming.com20d36662012-10-31 19:51:43 +0000151 GLenum getActualFormat(GLint level) const;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000152 bool isCompressed(GLint level) const;
153 bool isDepth(GLint level) const;
154
155 void setImage(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
156 void setCompressedImage(GLint level, GLenum format, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels);
157 void subImage(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
158 void subImageCompressed(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels);
159 void copyImage(GLint level, GLenum format, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
160 virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
161 void storage(GLsizei levels, GLenum internalformat, GLsizei width, GLsizei height);
162
163 virtual bool isSamplerComplete() const;
164 virtual void bindTexImage(egl::Surface *surface);
165 virtual void releaseTexImage();
166
167 virtual void generateMipmaps();
168
169 virtual Renderbuffer *getRenderbuffer(GLenum target);
170
171 protected:
172 friend class RenderbufferTexture2D;
daniel@transgaming.comd186dc72012-11-28 19:40:16 +0000173 virtual rx::RenderTarget *getRenderTarget(GLenum target);
174 virtual rx::RenderTarget *getDepthStencil(GLenum target);
daniel@transgaming.com690d8ae2012-10-31 19:52:08 +0000175 virtual int levelCount();
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000176
177 private:
178 DISALLOW_COPY_AND_ASSIGN(Texture2D);
179
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000180 virtual void createTexture();
181 virtual void updateTexture();
182 virtual void convertToRenderTarget();
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000183 virtual rx::TextureStorageInterface *getStorage(bool renderTarget);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000184
185 bool isMipmapComplete() const;
186
shannon.woods@transgaming.come2e97982013-02-28 23:18:50 +0000187 void redefineImage(GLint level, GLint internalformat, GLsizei width, GLsizei height);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000188 void commitRect(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height);
189
daniel@transgaming.comd9ec9022012-12-20 20:52:16 +0000190 rx::Image *mImageArray[IMPLEMENTATION_MAX_TEXTURE_LEVELS];
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000191
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000192 rx::TextureStorageInterface2D *mTexStorage;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000193 egl::Surface *mSurface;
194
195 // A specific internal reference count is kept for colorbuffer proxy references,
196 // because, as the renderbuffer acting as proxy will maintain a binding pointer
197 // back to this texture, there would be a circular reference if we used a binding
198 // pointer here. This reference count will cause the pointer to be set to NULL if
199 // the count drops to zero, but will not cause deletion of the Renderbuffer.
200 Renderbuffer *mColorbufferProxy;
201 unsigned int mProxyRefs;
202};
203
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000204class TextureCubeMap : public Texture
205{
206 public:
daniel@transgaming.com370482e2012-11-28 19:32:13 +0000207 TextureCubeMap(rx::Renderer *renderer, GLuint id);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000208
209 ~TextureCubeMap();
210
211 void addProxyRef(const Renderbuffer *proxy);
212 void releaseProxy(const Renderbuffer *proxy);
213
214 virtual GLenum getTarget() const;
215
216 GLsizei getWidth(GLenum target, GLint level) const;
217 GLsizei getHeight(GLenum target, GLint level) const;
218 GLenum getInternalFormat(GLenum target, GLint level) const;
daniel@transgaming.com20d36662012-10-31 19:51:43 +0000219 GLenum getActualFormat(GLenum target, GLint level) const;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000220 bool isCompressed(GLenum target, GLint level) const;
221
222 void setImagePosX(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
223 void setImageNegX(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
224 void setImagePosY(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
225 void setImageNegY(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
226 void setImagePosZ(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
227 void setImageNegZ(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
228
229 void setCompressedImage(GLenum face, GLint level, GLenum format, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels);
230
231 void subImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
232 void subImageCompressed(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels);
233 void copyImage(GLenum target, GLint level, GLenum format, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
234 virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
235 void storage(GLsizei levels, GLenum internalformat, GLsizei size);
236
237 virtual bool isSamplerComplete() const;
238
239 virtual void generateMipmaps();
240
241 virtual Renderbuffer *getRenderbuffer(GLenum target);
242
243 static unsigned int faceIndex(GLenum face);
244
245 protected:
246 friend class RenderbufferTextureCubeMap;
daniel@transgaming.comd186dc72012-11-28 19:40:16 +0000247 virtual rx::RenderTarget *getRenderTarget(GLenum target);
daniel@transgaming.com690d8ae2012-10-31 19:52:08 +0000248 virtual int levelCount();
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000249
250 private:
251 DISALLOW_COPY_AND_ASSIGN(TextureCubeMap);
252
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000253 virtual void createTexture();
254 virtual void updateTexture();
255 virtual void convertToRenderTarget();
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000256 virtual rx::TextureStorageInterface *getStorage(bool renderTarget);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000257
258 bool isCubeComplete() const;
259 bool isMipmapCubeComplete() const;
260
261 void setImage(int faceIndex, GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
262 void commitRect(int faceIndex, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height);
shannon.woods@transgaming.come2e97982013-02-28 23:18:50 +0000263 void redefineImage(int faceIndex, GLint level, GLint internalformat, GLsizei width, GLsizei height);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000264
daniel@transgaming.comd9ec9022012-12-20 20:52:16 +0000265 rx::Image *mImageArray[6][IMPLEMENTATION_MAX_TEXTURE_LEVELS];
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000266
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000267 rx::TextureStorageInterfaceCube *mTexStorage;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000268
269 // A specific internal reference count is kept for colorbuffer proxy references,
270 // because, as the renderbuffer acting as proxy will maintain a binding pointer
271 // back to this texture, there would be a circular reference if we used a binding
272 // pointer here. This reference count will cause the pointer to be set to NULL if
273 // the count drops to zero, but will not cause deletion of the Renderbuffer.
274 Renderbuffer *mFaceProxies[6];
275 unsigned int *mFaceProxyRefs[6];
276};
277}
278
jbauman@chromium.org68715282012-07-12 23:28:41 +0000279#endif // LIBGLESV2_TEXTURE_H_