blob: d9af22eef289d8b30db0b52e8f980a65eae4701e [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>
18#include <d3d9.h>
19
20#include "common/debug.h"
21#include "common/RefCountObject.h"
daniel@transgaming.comb9d7e6f2012-10-31 19:08:32 +000022#include "libGLESv2/renderer/Image.h"
daniel@transgaming.comb5e1a272012-10-31 19:10:00 +000023#include "libGLESv2/renderer/TextureStorage.h"
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000024#include "libGLESv2/Renderbuffer.h"
25#include "libGLESv2/utilities.h"
26
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.comebf139f2012-10-31 18:07:32 +000052struct SamplerState
53{
54 GLenum minFilter;
55 GLenum magFilter;
56 GLenum wrapS;
57 GLenum wrapT;
58 float maxAnisotropy;
59 int lodOffset;
60};
61
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000062class Texture : public RefCountObject
63{
64 public:
daniel@transgaming.com370482e2012-11-28 19:32:13 +000065 Texture(rx::Renderer *renderer, GLuint id);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000066
67 virtual ~Texture();
68
69 virtual void addProxyRef(const Renderbuffer *proxy) = 0;
70 virtual void releaseProxy(const Renderbuffer *proxy) = 0;
71
72 virtual GLenum getTarget() const = 0;
73
74 bool setMinFilter(GLenum filter);
75 bool setMagFilter(GLenum filter);
76 bool setWrapS(GLenum wrap);
77 bool setWrapT(GLenum wrap);
daniel@transgaming.com07ab8412012-07-12 15:17:09 +000078 bool setMaxAnisotropy(float textureMaxAnisotropy, float contextMaxAnisotropy);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000079 bool setUsage(GLenum usage);
80
81 GLenum getMinFilter() const;
82 GLenum getMagFilter() const;
83 GLenum getWrapS() const;
84 GLenum getWrapT() const;
daniel@transgaming.com07ab8412012-07-12 15:17:09 +000085 float getMaxAnisotropy() const;
daniel@transgaming.comebf139f2012-10-31 18:07:32 +000086 int getLodOffset();
87 void getSamplerState(SamplerState *sampler);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000088 GLenum getUsage() const;
daniel@transgaming.comca9a3c82012-10-26 18:55:07 +000089 bool isMipmapFiltered() const;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000090
91 virtual bool isSamplerComplete() const = 0;
92
daniel@transgaming.com31b13e12012-11-28 19:34:30 +000093 rx::TextureStorage *getNativeTexture();
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000094 virtual Renderbuffer *getRenderbuffer(GLenum target) = 0;
95
96 virtual void generateMipmaps() = 0;
97 virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source) = 0;
98
99 bool hasDirtyParameters() const;
100 bool hasDirtyImages() const;
101 void resetDirty();
102 unsigned int getTextureSerial();
103 unsigned int getRenderTargetSerial(GLenum target);
104
105 bool isImmutable() const;
106
107 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.
108
109 protected:
daniel@transgaming.com31b13e12012-11-28 19:34:30 +0000110 void setImage(GLint unpackAlignment, const void *pixels, rx::Image *image);
111 bool subImage(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels, rx::Image *image);
112 void setCompressedImage(GLsizei imageSize, const void *pixels, rx::Image *image);
113 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 +0000114
115 GLint creationLevels(GLsizei width, GLsizei height) const;
116 GLint creationLevels(GLsizei size) const;
117
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000118 virtual void createTexture() = 0;
119 virtual void updateTexture() = 0;
120 virtual void convertToRenderTarget() = 0;
121 virtual IDirect3DSurface9 *getRenderTarget(GLenum target) = 0;
122
daniel@transgaming.com690d8ae2012-10-31 19:52:08 +0000123 virtual int levelCount() = 0;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000124
daniel@transgaming.com370482e2012-11-28 19:32:13 +0000125 rx::Renderer *mRenderer;
126
daniel@transgaming.comebf139f2012-10-31 18:07:32 +0000127 SamplerState mSamplerState;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000128 bool mDirtyParameters;
129 GLenum mUsage;
130
131 bool mDirtyImages;
132
133 bool mImmutable;
134
135 private:
136 DISALLOW_COPY_AND_ASSIGN(Texture);
137
daniel@transgaming.com31b13e12012-11-28 19:34:30 +0000138 virtual rx::TextureStorage *getStorage(bool renderTarget) = 0;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000139};
140
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000141class Texture2D : public Texture
142{
143 public:
daniel@transgaming.com370482e2012-11-28 19:32:13 +0000144 Texture2D(rx::Renderer *renderer, GLuint id);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000145
146 ~Texture2D();
147
148 void addProxyRef(const Renderbuffer *proxy);
149 void releaseProxy(const Renderbuffer *proxy);
150
151 virtual GLenum getTarget() const;
152
153 GLsizei getWidth(GLint level) const;
154 GLsizei getHeight(GLint level) const;
155 GLenum getInternalFormat(GLint level) const;
daniel@transgaming.com20d36662012-10-31 19:51:43 +0000156 GLenum getActualFormat(GLint level) const;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000157 bool isCompressed(GLint level) const;
158 bool isDepth(GLint level) const;
159
160 void setImage(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
161 void setCompressedImage(GLint level, GLenum format, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels);
162 void subImage(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
163 void subImageCompressed(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels);
164 void copyImage(GLint level, GLenum format, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
165 virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
166 void storage(GLsizei levels, GLenum internalformat, GLsizei width, GLsizei height);
167
168 virtual bool isSamplerComplete() const;
169 virtual void bindTexImage(egl::Surface *surface);
170 virtual void releaseTexImage();
171
172 virtual void generateMipmaps();
173
174 virtual Renderbuffer *getRenderbuffer(GLenum target);
175
176 protected:
177 friend class RenderbufferTexture2D;
178 virtual IDirect3DSurface9 *getRenderTarget(GLenum target);
179 virtual IDirect3DSurface9 *getDepthStencil(GLenum target);
daniel@transgaming.com690d8ae2012-10-31 19:52:08 +0000180 virtual int levelCount();
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000181
182 private:
183 DISALLOW_COPY_AND_ASSIGN(Texture2D);
184
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000185 virtual void createTexture();
186 virtual void updateTexture();
187 virtual void convertToRenderTarget();
daniel@transgaming.com31b13e12012-11-28 19:34:30 +0000188 virtual rx::TextureStorage *getStorage(bool renderTarget);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000189
190 bool isMipmapComplete() const;
191
daniel@transgaming.com6452adf2012-10-17 18:22:35 +0000192 void redefineImage(GLint level, GLint internalformat, GLsizei width, GLsizei height);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000193 void commitRect(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height);
194
daniel@transgaming.com31b13e12012-11-28 19:34:30 +0000195 rx::Image mImageArray[IMPLEMENTATION_MAX_TEXTURE_LEVELS];
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000196
daniel@transgaming.com31b13e12012-11-28 19:34:30 +0000197 rx::TextureStorage2D *mTexStorage;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000198 egl::Surface *mSurface;
199
200 // A specific internal reference count is kept for colorbuffer proxy references,
201 // because, as the renderbuffer acting as proxy will maintain a binding pointer
202 // back to this texture, there would be a circular reference if we used a binding
203 // pointer here. This reference count will cause the pointer to be set to NULL if
204 // the count drops to zero, but will not cause deletion of the Renderbuffer.
205 Renderbuffer *mColorbufferProxy;
206 unsigned int mProxyRefs;
207};
208
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000209class TextureCubeMap : public Texture
210{
211 public:
daniel@transgaming.com370482e2012-11-28 19:32:13 +0000212 TextureCubeMap(rx::Renderer *renderer, GLuint id);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000213
214 ~TextureCubeMap();
215
216 void addProxyRef(const Renderbuffer *proxy);
217 void releaseProxy(const Renderbuffer *proxy);
218
219 virtual GLenum getTarget() const;
220
221 GLsizei getWidth(GLenum target, GLint level) const;
222 GLsizei getHeight(GLenum target, GLint level) const;
223 GLenum getInternalFormat(GLenum target, GLint level) const;
daniel@transgaming.com20d36662012-10-31 19:51:43 +0000224 GLenum getActualFormat(GLenum target, GLint level) const;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000225 bool isCompressed(GLenum target, GLint level) const;
226
227 void setImagePosX(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
228 void setImageNegX(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
229 void setImagePosY(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
230 void setImageNegY(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
231 void setImagePosZ(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
232 void setImageNegZ(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
233
234 void setCompressedImage(GLenum face, GLint level, GLenum format, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels);
235
236 void subImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
237 void subImageCompressed(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels);
238 void copyImage(GLenum target, GLint level, GLenum format, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
239 virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
240 void storage(GLsizei levels, GLenum internalformat, GLsizei size);
241
242 virtual bool isSamplerComplete() const;
243
244 virtual void generateMipmaps();
245
246 virtual Renderbuffer *getRenderbuffer(GLenum target);
247
248 static unsigned int faceIndex(GLenum face);
249
250 protected:
251 friend class RenderbufferTextureCubeMap;
252 virtual IDirect3DSurface9 *getRenderTarget(GLenum target);
daniel@transgaming.com690d8ae2012-10-31 19:52:08 +0000253 virtual int levelCount();
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000254
255 private:
256 DISALLOW_COPY_AND_ASSIGN(TextureCubeMap);
257
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000258 virtual void createTexture();
259 virtual void updateTexture();
260 virtual void convertToRenderTarget();
daniel@transgaming.com31b13e12012-11-28 19:34:30 +0000261 virtual rx::TextureStorage *getStorage(bool renderTarget);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000262
263 bool isCubeComplete() const;
264 bool isMipmapCubeComplete() const;
265
266 void setImage(int faceIndex, GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
267 void commitRect(int faceIndex, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height);
daniel@transgaming.com6452adf2012-10-17 18:22:35 +0000268 void redefineImage(int faceIndex, GLint level, GLint internalformat, GLsizei width, GLsizei height);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000269
daniel@transgaming.com31b13e12012-11-28 19:34:30 +0000270 rx::Image mImageArray[6][IMPLEMENTATION_MAX_TEXTURE_LEVELS];
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000271
daniel@transgaming.com31b13e12012-11-28 19:34:30 +0000272 rx::TextureStorageCubeMap *mTexStorage;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000273
274 // A specific internal reference count is kept for colorbuffer proxy references,
275 // because, as the renderbuffer acting as proxy will maintain a binding pointer
276 // back to this texture, there would be a circular reference if we used a binding
277 // pointer here. This reference count will cause the pointer to be set to NULL if
278 // the count drops to zero, but will not cause deletion of the Renderbuffer.
279 Renderbuffer *mFaceProxies[6];
280 unsigned int *mFaceProxyRefs[6];
281};
282}
283
jbauman@chromium.org68715282012-07-12 23:28:41 +0000284#endif // LIBGLESV2_TEXTURE_H_