blob: 0efedfa621fc15a024eecc89253325a6a20c77d1 [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
32namespace gl
33{
34class Blit;
35class Framebuffer;
36
37enum
38{
39 // These are the maximums the implementation can support
40 // The actual GL caps are limited by the device caps
41 // and should be queried from the Context
42 IMPLEMENTATION_MAX_TEXTURE_SIZE = 16384,
43 IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE = 16384,
44
45 IMPLEMENTATION_MAX_TEXTURE_LEVELS = 15 // 1+log2 of MAX_TEXTURE_SIZE
46};
47
daniel@transgaming.comebf139f2012-10-31 18:07:32 +000048struct SamplerState
49{
50 GLenum minFilter;
51 GLenum magFilter;
52 GLenum wrapS;
53 GLenum wrapT;
54 float maxAnisotropy;
55 int lodOffset;
56};
57
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000058class Texture : public RefCountObject
59{
60 public:
61 explicit Texture(GLuint id);
62
63 virtual ~Texture();
64
65 virtual void addProxyRef(const Renderbuffer *proxy) = 0;
66 virtual void releaseProxy(const Renderbuffer *proxy) = 0;
67
68 virtual GLenum getTarget() const = 0;
69
70 bool setMinFilter(GLenum filter);
71 bool setMagFilter(GLenum filter);
72 bool setWrapS(GLenum wrap);
73 bool setWrapT(GLenum wrap);
daniel@transgaming.com07ab8412012-07-12 15:17:09 +000074 bool setMaxAnisotropy(float textureMaxAnisotropy, float contextMaxAnisotropy);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000075 bool setUsage(GLenum usage);
76
77 GLenum getMinFilter() const;
78 GLenum getMagFilter() const;
79 GLenum getWrapS() const;
80 GLenum getWrapT() const;
daniel@transgaming.com07ab8412012-07-12 15:17:09 +000081 float getMaxAnisotropy() const;
daniel@transgaming.comebf139f2012-10-31 18:07:32 +000082 int getLodOffset();
83 void getSamplerState(SamplerState *sampler);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000084 GLenum getUsage() const;
daniel@transgaming.comca9a3c82012-10-26 18:55:07 +000085 bool isMipmapFiltered() const;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000086
87 virtual bool isSamplerComplete() const = 0;
88
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +000089 TextureStorage *getNativeTexture();
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000090 virtual Renderbuffer *getRenderbuffer(GLenum target) = 0;
91
92 virtual void generateMipmaps() = 0;
93 virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source) = 0;
94
95 bool hasDirtyParameters() const;
96 bool hasDirtyImages() const;
97 void resetDirty();
98 unsigned int getTextureSerial();
99 unsigned int getRenderTargetSerial(GLenum target);
100
101 bool isImmutable() const;
102
103 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.
104
105 protected:
106 void setImage(GLint unpackAlignment, const void *pixels, Image *image);
107 bool subImage(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels, Image *image);
108 void setCompressedImage(GLsizei imageSize, const void *pixels, Image *image);
109 bool subImageCompressed(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels, Image *image);
110
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;
117 virtual IDirect3DSurface9 *getRenderTarget(GLenum target) = 0;
118
daniel@transgaming.com690d8ae2012-10-31 19:52:08 +0000119 virtual int levelCount() = 0;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000120
121 static Blit *getBlitter();
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000122
daniel@transgaming.comebf139f2012-10-31 18:07:32 +0000123 SamplerState mSamplerState;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000124 bool mDirtyParameters;
125 GLenum mUsage;
126
127 bool mDirtyImages;
128
129 bool mImmutable;
130
131 private:
132 DISALLOW_COPY_AND_ASSIGN(Texture);
133
134 virtual TextureStorage *getStorage(bool renderTarget) = 0;
135};
136
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000137class Texture2D : public Texture
138{
139 public:
140 explicit Texture2D(GLuint id);
141
142 ~Texture2D();
143
144 void addProxyRef(const Renderbuffer *proxy);
145 void releaseProxy(const Renderbuffer *proxy);
146
147 virtual GLenum getTarget() const;
148
149 GLsizei getWidth(GLint level) const;
150 GLsizei getHeight(GLint level) const;
151 GLenum getInternalFormat(GLint level) const;
daniel@transgaming.com20d36662012-10-31 19:51:43 +0000152 GLenum getActualFormat(GLint level) const;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000153 bool isCompressed(GLint level) const;
154 bool isDepth(GLint level) const;
155
156 void setImage(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
157 void setCompressedImage(GLint level, GLenum format, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels);
158 void subImage(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
159 void subImageCompressed(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels);
160 void copyImage(GLint level, GLenum format, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
161 virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
162 void storage(GLsizei levels, GLenum internalformat, GLsizei width, GLsizei height);
163
164 virtual bool isSamplerComplete() const;
165 virtual void bindTexImage(egl::Surface *surface);
166 virtual void releaseTexImage();
167
168 virtual void generateMipmaps();
169
170 virtual Renderbuffer *getRenderbuffer(GLenum target);
171
172 protected:
173 friend class RenderbufferTexture2D;
174 virtual IDirect3DSurface9 *getRenderTarget(GLenum target);
175 virtual IDirect3DSurface9 *getDepthStencil(GLenum target);
daniel@transgaming.com690d8ae2012-10-31 19:52:08 +0000176 virtual int levelCount();
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000177
178 private:
179 DISALLOW_COPY_AND_ASSIGN(Texture2D);
180
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000181 virtual void createTexture();
182 virtual void updateTexture();
183 virtual void convertToRenderTarget();
184 virtual TextureStorage *getStorage(bool renderTarget);
185
186 bool isMipmapComplete() const;
187
daniel@transgaming.com6452adf2012-10-17 18:22:35 +0000188 void redefineImage(GLint level, GLint internalformat, GLsizei width, GLsizei height);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000189 void commitRect(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height);
190
191 Image mImageArray[IMPLEMENTATION_MAX_TEXTURE_LEVELS];
192
193 TextureStorage2D *mTexStorage;
194 egl::Surface *mSurface;
195
196 // A specific internal reference count is kept for colorbuffer proxy references,
197 // because, as the renderbuffer acting as proxy will maintain a binding pointer
198 // back to this texture, there would be a circular reference if we used a binding
199 // pointer here. This reference count will cause the pointer to be set to NULL if
200 // the count drops to zero, but will not cause deletion of the Renderbuffer.
201 Renderbuffer *mColorbufferProxy;
202 unsigned int mProxyRefs;
203};
204
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000205class TextureCubeMap : public Texture
206{
207 public:
208 explicit TextureCubeMap(GLuint id);
209
210 ~TextureCubeMap();
211
212 void addProxyRef(const Renderbuffer *proxy);
213 void releaseProxy(const Renderbuffer *proxy);
214
215 virtual GLenum getTarget() const;
216
217 GLsizei getWidth(GLenum target, GLint level) const;
218 GLsizei getHeight(GLenum target, GLint level) const;
219 GLenum getInternalFormat(GLenum target, GLint level) const;
daniel@transgaming.com20d36662012-10-31 19:51:43 +0000220 GLenum getActualFormat(GLenum target, GLint level) const;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000221 bool isCompressed(GLenum target, GLint level) const;
222
223 void setImagePosX(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
224 void setImageNegX(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
225 void setImagePosY(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
226 void setImageNegY(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
227 void setImagePosZ(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
228 void setImageNegZ(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
229
230 void setCompressedImage(GLenum face, GLint level, GLenum format, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels);
231
232 void subImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
233 void subImageCompressed(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels);
234 void copyImage(GLenum target, GLint level, GLenum format, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
235 virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
236 void storage(GLsizei levels, GLenum internalformat, GLsizei size);
237
238 virtual bool isSamplerComplete() const;
239
240 virtual void generateMipmaps();
241
242 virtual Renderbuffer *getRenderbuffer(GLenum target);
243
244 static unsigned int faceIndex(GLenum face);
245
246 protected:
247 friend class RenderbufferTextureCubeMap;
248 virtual IDirect3DSurface9 *getRenderTarget(GLenum target);
daniel@transgaming.com690d8ae2012-10-31 19:52:08 +0000249 virtual int levelCount();
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000250
251 private:
252 DISALLOW_COPY_AND_ASSIGN(TextureCubeMap);
253
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000254 virtual void createTexture();
255 virtual void updateTexture();
256 virtual void convertToRenderTarget();
257 virtual TextureStorage *getStorage(bool renderTarget);
258
259 bool isCubeComplete() const;
260 bool isMipmapCubeComplete() const;
261
262 void setImage(int faceIndex, GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
263 void commitRect(int faceIndex, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height);
daniel@transgaming.com6452adf2012-10-17 18:22:35 +0000264 void redefineImage(int faceIndex, GLint level, GLint internalformat, GLsizei width, GLsizei height);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000265
266 Image mImageArray[6][IMPLEMENTATION_MAX_TEXTURE_LEVELS];
267
268 TextureStorageCubeMap *mTexStorage;
269
270 // A specific internal reference count is kept for colorbuffer proxy references,
271 // because, as the renderbuffer acting as proxy will maintain a binding pointer
272 // back to this texture, there would be a circular reference if we used a binding
273 // pointer here. This reference count will cause the pointer to be set to NULL if
274 // the count drops to zero, but will not cause deletion of the Renderbuffer.
275 Renderbuffer *mFaceProxies[6];
276 unsigned int *mFaceProxyRefs[6];
277};
278}
279
jbauman@chromium.org68715282012-07-12 23:28:41 +0000280#endif // LIBGLESV2_TEXTURE_H_