blob: 37fdf4db0b388ec0390dc87e76c8b0ceea118533 [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
49 IMPLEMENTATION_MAX_TEXTURE_SIZE = 16384,
50 IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE = 16384,
51
52 IMPLEMENTATION_MAX_TEXTURE_LEVELS = 15 // 1+log2 of MAX_TEXTURE_SIZE
53};
54
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000055class Texture : public RefCountObject
56{
57 public:
daniel@transgaming.com370482e2012-11-28 19:32:13 +000058 Texture(rx::Renderer *renderer, GLuint id);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000059
60 virtual ~Texture();
61
62 virtual void addProxyRef(const Renderbuffer *proxy) = 0;
63 virtual void releaseProxy(const Renderbuffer *proxy) = 0;
64
65 virtual GLenum getTarget() const = 0;
66
67 bool setMinFilter(GLenum filter);
68 bool setMagFilter(GLenum filter);
69 bool setWrapS(GLenum wrap);
70 bool setWrapT(GLenum wrap);
daniel@transgaming.com07ab8412012-07-12 15:17:09 +000071 bool setMaxAnisotropy(float textureMaxAnisotropy, float contextMaxAnisotropy);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000072 bool setUsage(GLenum usage);
73
74 GLenum getMinFilter() const;
75 GLenum getMagFilter() const;
76 GLenum getWrapS() const;
77 GLenum getWrapT() const;
daniel@transgaming.com07ab8412012-07-12 15:17:09 +000078 float getMaxAnisotropy() const;
daniel@transgaming.comebf139f2012-10-31 18:07:32 +000079 int getLodOffset();
80 void getSamplerState(SamplerState *sampler);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000081 GLenum getUsage() const;
daniel@transgaming.comca9a3c82012-10-26 18:55:07 +000082 bool isMipmapFiltered() const;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000083
84 virtual bool isSamplerComplete() const = 0;
85
daniel@transgaming.com87705f82012-12-20 21:10:45 +000086 rx::TextureStorageInterface *getNativeTexture();
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000087 virtual Renderbuffer *getRenderbuffer(GLenum target) = 0;
88
89 virtual void generateMipmaps() = 0;
90 virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source) = 0;
91
92 bool hasDirtyParameters() const;
93 bool hasDirtyImages() const;
94 void resetDirty();
95 unsigned int getTextureSerial();
96 unsigned int getRenderTargetSerial(GLenum target);
97
98 bool isImmutable() const;
99
100 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.
101
102 protected:
daniel@transgaming.com31b13e12012-11-28 19:34:30 +0000103 void setImage(GLint unpackAlignment, const void *pixels, rx::Image *image);
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000104 bool subImage(GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth,
105 GLenum format, GLenum type, GLint unpackAlignment, const void *pixels, rx::Image *image);
daniel@transgaming.com31b13e12012-11-28 19:34:30 +0000106 void setCompressedImage(GLsizei imageSize, const void *pixels, rx::Image *image);
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000107 bool subImageCompressed(GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth,
108 GLenum format, GLsizei imageSize, const void *pixels, rx::Image *image);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000109
110 GLint creationLevels(GLsizei width, GLsizei height) const;
111 GLint creationLevels(GLsizei size) const;
112
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000113 virtual void createTexture() = 0;
114 virtual void updateTexture() = 0;
115 virtual void convertToRenderTarget() = 0;
daniel@transgaming.comd186dc72012-11-28 19:40:16 +0000116 virtual rx::RenderTarget *getRenderTarget(GLenum target) = 0;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000117
daniel@transgaming.com690d8ae2012-10-31 19:52:08 +0000118 virtual int levelCount() = 0;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000119
daniel@transgaming.com370482e2012-11-28 19:32:13 +0000120 rx::Renderer *mRenderer;
121
daniel@transgaming.comebf139f2012-10-31 18:07:32 +0000122 SamplerState mSamplerState;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000123 GLenum mUsage;
124
125 bool mDirtyImages;
126
127 bool mImmutable;
128
129 private:
130 DISALLOW_COPY_AND_ASSIGN(Texture);
131
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000132 virtual rx::TextureStorageInterface *getStorage(bool renderTarget) = 0;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000133};
134
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000135class Texture2D : public Texture
136{
137 public:
daniel@transgaming.com370482e2012-11-28 19:32:13 +0000138 Texture2D(rx::Renderer *renderer, GLuint id);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000139
140 ~Texture2D();
141
142 void addProxyRef(const Renderbuffer *proxy);
143 void releaseProxy(const Renderbuffer *proxy);
144
145 virtual GLenum getTarget() const;
146
147 GLsizei getWidth(GLint level) const;
148 GLsizei getHeight(GLint level) const;
149 GLenum getInternalFormat(GLint level) const;
daniel@transgaming.com20d36662012-10-31 19:51:43 +0000150 GLenum getActualFormat(GLint level) const;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000151 bool isCompressed(GLint level) const;
152 bool isDepth(GLint level) const;
153
154 void setImage(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
155 void setCompressedImage(GLint level, GLenum format, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels);
156 void subImage(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
157 void subImageCompressed(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels);
158 void copyImage(GLint level, GLenum format, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
159 virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
160 void storage(GLsizei levels, GLenum internalformat, GLsizei width, GLsizei height);
161
162 virtual bool isSamplerComplete() const;
163 virtual void bindTexImage(egl::Surface *surface);
164 virtual void releaseTexImage();
165
166 virtual void generateMipmaps();
167
168 virtual Renderbuffer *getRenderbuffer(GLenum target);
169
170 protected:
171 friend class RenderbufferTexture2D;
daniel@transgaming.comd186dc72012-11-28 19:40:16 +0000172 virtual rx::RenderTarget *getRenderTarget(GLenum target);
173 virtual rx::RenderTarget *getDepthStencil(GLenum target);
daniel@transgaming.com690d8ae2012-10-31 19:52:08 +0000174 virtual int levelCount();
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000175
176 private:
177 DISALLOW_COPY_AND_ASSIGN(Texture2D);
178
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000179 virtual void createTexture();
180 virtual void updateTexture();
181 virtual void convertToRenderTarget();
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000182 virtual rx::TextureStorageInterface *getStorage(bool renderTarget);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000183
184 bool isMipmapComplete() const;
185
shannon.woods@transgaming.come2e97982013-02-28 23:18:50 +0000186 void redefineImage(GLint level, GLint internalformat, GLsizei width, GLsizei height);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000187 void commitRect(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height);
188
daniel@transgaming.comd9ec9022012-12-20 20:52:16 +0000189 rx::Image *mImageArray[IMPLEMENTATION_MAX_TEXTURE_LEVELS];
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000190
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000191 rx::TextureStorageInterface2D *mTexStorage;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000192 egl::Surface *mSurface;
193
194 // A specific internal reference count is kept for colorbuffer proxy references,
195 // because, as the renderbuffer acting as proxy will maintain a binding pointer
196 // back to this texture, there would be a circular reference if we used a binding
197 // pointer here. This reference count will cause the pointer to be set to NULL if
198 // the count drops to zero, but will not cause deletion of the Renderbuffer.
199 Renderbuffer *mColorbufferProxy;
200 unsigned int mProxyRefs;
201};
202
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000203class TextureCubeMap : public Texture
204{
205 public:
daniel@transgaming.com370482e2012-11-28 19:32:13 +0000206 TextureCubeMap(rx::Renderer *renderer, GLuint id);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000207
208 ~TextureCubeMap();
209
210 void addProxyRef(const Renderbuffer *proxy);
211 void releaseProxy(const Renderbuffer *proxy);
212
213 virtual GLenum getTarget() const;
214
215 GLsizei getWidth(GLenum target, GLint level) const;
216 GLsizei getHeight(GLenum target, GLint level) const;
217 GLenum getInternalFormat(GLenum target, GLint level) const;
daniel@transgaming.com20d36662012-10-31 19:51:43 +0000218 GLenum getActualFormat(GLenum target, GLint level) const;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000219 bool isCompressed(GLenum target, GLint level) const;
220
221 void setImagePosX(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
222 void setImageNegX(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
223 void setImagePosY(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
224 void setImageNegY(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
225 void setImagePosZ(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
226 void setImageNegZ(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
227
228 void setCompressedImage(GLenum face, GLint level, GLenum format, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels);
229
230 void subImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
231 void subImageCompressed(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels);
232 void copyImage(GLenum target, GLint level, GLenum format, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
233 virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
234 void storage(GLsizei levels, GLenum internalformat, GLsizei size);
235
236 virtual bool isSamplerComplete() const;
237
238 virtual void generateMipmaps();
239
240 virtual Renderbuffer *getRenderbuffer(GLenum target);
241
242 static unsigned int faceIndex(GLenum face);
243
244 protected:
245 friend class RenderbufferTextureCubeMap;
daniel@transgaming.comd186dc72012-11-28 19:40:16 +0000246 virtual rx::RenderTarget *getRenderTarget(GLenum target);
daniel@transgaming.com690d8ae2012-10-31 19:52:08 +0000247 virtual int levelCount();
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000248
249 private:
250 DISALLOW_COPY_AND_ASSIGN(TextureCubeMap);
251
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000252 virtual void createTexture();
253 virtual void updateTexture();
254 virtual void convertToRenderTarget();
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000255 virtual rx::TextureStorageInterface *getStorage(bool renderTarget);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000256
257 bool isCubeComplete() const;
258 bool isMipmapCubeComplete() const;
259
260 void setImage(int faceIndex, GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
261 void commitRect(int faceIndex, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height);
shannon.woods@transgaming.come2e97982013-02-28 23:18:50 +0000262 void redefineImage(int faceIndex, GLint level, GLint internalformat, GLsizei width, GLsizei height);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000263
daniel@transgaming.comd9ec9022012-12-20 20:52:16 +0000264 rx::Image *mImageArray[6][IMPLEMENTATION_MAX_TEXTURE_LEVELS];
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000265
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000266 rx::TextureStorageInterfaceCube *mTexStorage;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000267
268 // A specific internal reference count is kept for colorbuffer proxy references,
269 // because, as the renderbuffer acting as proxy will maintain a binding pointer
270 // back to this texture, there would be a circular reference if we used a binding
271 // pointer here. This reference count will cause the pointer to be set to NULL if
272 // the count drops to zero, but will not cause deletion of the Renderbuffer.
273 Renderbuffer *mFaceProxies[6];
274 unsigned int *mFaceProxyRefs[6];
275};
276}
277
jbauman@chromium.org68715282012-07-12 23:28:41 +0000278#endif // LIBGLESV2_TEXTURE_H_