blob: 293d5e248f9a097cc6ae5a5bd64dd198bcac368f [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
daniel@transgaming.comb9d7e6f2012-10-31 19:08:32 +000065 static D3DFORMAT ConvertTextureInternalFormat(GLint internalformat);
66 static bool IsTextureFormatRenderable(D3DFORMAT format);
67
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000068 virtual void addProxyRef(const Renderbuffer *proxy) = 0;
69 virtual void releaseProxy(const Renderbuffer *proxy) = 0;
70
71 virtual GLenum getTarget() const = 0;
72
73 bool setMinFilter(GLenum filter);
74 bool setMagFilter(GLenum filter);
75 bool setWrapS(GLenum wrap);
76 bool setWrapT(GLenum wrap);
daniel@transgaming.com07ab8412012-07-12 15:17:09 +000077 bool setMaxAnisotropy(float textureMaxAnisotropy, float contextMaxAnisotropy);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000078 bool setUsage(GLenum usage);
79
80 GLenum getMinFilter() const;
81 GLenum getMagFilter() const;
82 GLenum getWrapS() const;
83 GLenum getWrapT() const;
daniel@transgaming.com07ab8412012-07-12 15:17:09 +000084 float getMaxAnisotropy() const;
daniel@transgaming.comebf139f2012-10-31 18:07:32 +000085 int getLodOffset();
86 void getSamplerState(SamplerState *sampler);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000087 GLenum getUsage() const;
daniel@transgaming.comca9a3c82012-10-26 18:55:07 +000088 bool isMipmapFiltered() const;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000089
90 virtual bool isSamplerComplete() const = 0;
91
daniel@transgaming.coma734f272012-10-31 18:07:48 +000092 IDirect3DBaseTexture9 *getD3DTexture(); // D3D9_REPLACE
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000093 virtual Renderbuffer *getRenderbuffer(GLenum target) = 0;
94
95 virtual void generateMipmaps() = 0;
96 virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source) = 0;
97
98 bool hasDirtyParameters() const;
99 bool hasDirtyImages() const;
100 void resetDirty();
101 unsigned int getTextureSerial();
102 unsigned int getRenderTargetSerial(GLenum target);
103
104 bool isImmutable() const;
105
106 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.
107
108 protected:
109 void setImage(GLint unpackAlignment, const void *pixels, Image *image);
110 bool subImage(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels, Image *image);
111 void setCompressedImage(GLsizei imageSize, const void *pixels, Image *image);
112 bool subImageCompressed(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels, Image *image);
113
114 GLint creationLevels(GLsizei width, GLsizei height) const;
115 GLint creationLevels(GLsizei size) const;
116
117 virtual IDirect3DBaseTexture9 *getBaseTexture() const = 0;
118 virtual void createTexture() = 0;
119 virtual void updateTexture() = 0;
120 virtual void convertToRenderTarget() = 0;
121 virtual IDirect3DSurface9 *getRenderTarget(GLenum target) = 0;
122
jbauman@chromium.org6bc4a142012-09-06 21:28:30 +0000123 int levelCount();
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000124
125 static Blit *getBlitter();
126 static bool copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged);
127
daniel@transgaming.comebf139f2012-10-31 18:07:32 +0000128 SamplerState mSamplerState;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000129 bool mDirtyParameters;
130 GLenum mUsage;
131
132 bool mDirtyImages;
133
134 bool mImmutable;
135
136 private:
137 DISALLOW_COPY_AND_ASSIGN(Texture);
138
139 virtual TextureStorage *getStorage(bool renderTarget) = 0;
140};
141
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000142class Texture2D : public Texture
143{
144 public:
145 explicit Texture2D(GLuint id);
146
147 ~Texture2D();
148
149 void addProxyRef(const Renderbuffer *proxy);
150 void releaseProxy(const Renderbuffer *proxy);
151
152 virtual GLenum getTarget() const;
153
154 GLsizei getWidth(GLint level) const;
155 GLsizei getHeight(GLint level) const;
156 GLenum getInternalFormat(GLint level) const;
157 D3DFORMAT getD3DFormat(GLint level) const;
158 bool isCompressed(GLint level) const;
159 bool isDepth(GLint level) const;
160
161 void setImage(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
162 void setCompressedImage(GLint level, GLenum format, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels);
163 void subImage(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
164 void subImageCompressed(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels);
165 void copyImage(GLint level, GLenum format, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
166 virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
167 void storage(GLsizei levels, GLenum internalformat, GLsizei width, GLsizei height);
168
169 virtual bool isSamplerComplete() const;
170 virtual void bindTexImage(egl::Surface *surface);
171 virtual void releaseTexImage();
172
173 virtual void generateMipmaps();
174
175 virtual Renderbuffer *getRenderbuffer(GLenum target);
176
177 protected:
178 friend class RenderbufferTexture2D;
179 virtual IDirect3DSurface9 *getRenderTarget(GLenum target);
180 virtual IDirect3DSurface9 *getDepthStencil(GLenum target);
181
182 private:
183 DISALLOW_COPY_AND_ASSIGN(Texture2D);
184
185 virtual IDirect3DBaseTexture9 *getBaseTexture() const;
186 virtual void createTexture();
187 virtual void updateTexture();
188 virtual void convertToRenderTarget();
189 virtual TextureStorage *getStorage(bool renderTarget);
190
191 bool isMipmapComplete() const;
192
daniel@transgaming.com6452adf2012-10-17 18:22:35 +0000193 void redefineImage(GLint level, GLint internalformat, GLsizei width, GLsizei height);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000194 void commitRect(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height);
195
196 Image mImageArray[IMPLEMENTATION_MAX_TEXTURE_LEVELS];
197
198 TextureStorage2D *mTexStorage;
199 egl::Surface *mSurface;
200
201 // A specific internal reference count is kept for colorbuffer proxy references,
202 // because, as the renderbuffer acting as proxy will maintain a binding pointer
203 // back to this texture, there would be a circular reference if we used a binding
204 // pointer here. This reference count will cause the pointer to be set to NULL if
205 // the count drops to zero, but will not cause deletion of the Renderbuffer.
206 Renderbuffer *mColorbufferProxy;
207 unsigned int mProxyRefs;
208};
209
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000210class TextureCubeMap : public Texture
211{
212 public:
213 explicit TextureCubeMap(GLuint id);
214
215 ~TextureCubeMap();
216
217 void addProxyRef(const Renderbuffer *proxy);
218 void releaseProxy(const Renderbuffer *proxy);
219
220 virtual GLenum getTarget() const;
221
222 GLsizei getWidth(GLenum target, GLint level) const;
223 GLsizei getHeight(GLenum target, GLint level) const;
224 GLenum getInternalFormat(GLenum target, GLint level) const;
225 D3DFORMAT getD3DFormat(GLenum target, GLint level) const;
226 bool isCompressed(GLenum target, GLint level) const;
227
228 void setImagePosX(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
229 void setImageNegX(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
230 void setImagePosY(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
231 void setImageNegY(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
232 void setImagePosZ(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
233 void setImageNegZ(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
234
235 void setCompressedImage(GLenum face, GLint level, GLenum format, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels);
236
237 void subImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
238 void subImageCompressed(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels);
239 void copyImage(GLenum target, GLint level, GLenum format, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
240 virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
241 void storage(GLsizei levels, GLenum internalformat, GLsizei size);
242
243 virtual bool isSamplerComplete() const;
244
245 virtual void generateMipmaps();
246
247 virtual Renderbuffer *getRenderbuffer(GLenum target);
248
249 static unsigned int faceIndex(GLenum face);
250
251 protected:
252 friend class RenderbufferTextureCubeMap;
253 virtual IDirect3DSurface9 *getRenderTarget(GLenum target);
254
255 private:
256 DISALLOW_COPY_AND_ASSIGN(TextureCubeMap);
257
258 virtual IDirect3DBaseTexture9 *getBaseTexture() const;
259 virtual void createTexture();
260 virtual void updateTexture();
261 virtual void convertToRenderTarget();
262 virtual TextureStorage *getStorage(bool renderTarget);
263
264 bool isCubeComplete() const;
265 bool isMipmapCubeComplete() const;
266
267 void setImage(int faceIndex, GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
268 void commitRect(int faceIndex, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height);
daniel@transgaming.com6452adf2012-10-17 18:22:35 +0000269 void redefineImage(int faceIndex, GLint level, GLint internalformat, GLsizei width, GLsizei height);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000270
271 Image mImageArray[6][IMPLEMENTATION_MAX_TEXTURE_LEVELS];
272
273 TextureStorageCubeMap *mTexStorage;
274
275 // A specific internal reference count is kept for colorbuffer proxy references,
276 // because, as the renderbuffer acting as proxy will maintain a binding pointer
277 // back to this texture, there would be a circular reference if we used a binding
278 // pointer here. This reference count will cause the pointer to be set to NULL if
279 // the count drops to zero, but will not cause deletion of the Renderbuffer.
280 Renderbuffer *mFaceProxies[6];
281 unsigned int *mFaceProxyRefs[6];
282};
283}
284
jbauman@chromium.org68715282012-07-12 23:28:41 +0000285#endif // LIBGLESV2_TEXTURE_H_