blob: 1ed053d298c63a9c650292008d5d8b3012c8e775 [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.com95a758f2012-07-12 15:17:06 +000023#include "libGLESv2/Renderbuffer.h"
24#include "libGLESv2/utilities.h"
25
26namespace egl
27{
28class Surface;
29}
30
31namespace gl
32{
33class Blit;
34class Framebuffer;
35
36enum
37{
38 // These are the maximums the implementation can support
39 // The actual GL caps are limited by the device caps
40 // and should be queried from the Context
41 IMPLEMENTATION_MAX_TEXTURE_SIZE = 16384,
42 IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE = 16384,
43
44 IMPLEMENTATION_MAX_TEXTURE_LEVELS = 15 // 1+log2 of MAX_TEXTURE_SIZE
45};
46
daniel@transgaming.comebf139f2012-10-31 18:07:32 +000047struct SamplerState
48{
49 GLenum minFilter;
50 GLenum magFilter;
51 GLenum wrapS;
52 GLenum wrapT;
53 float maxAnisotropy;
54 int lodOffset;
55};
56
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000057
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000058
59class TextureStorage
60{
61 public:
62 explicit TextureStorage(DWORD usage);
63
64 virtual ~TextureStorage();
65
66 bool isRenderTarget() const;
67 bool isManaged() const;
68 D3DPOOL getPool() const;
69 DWORD getUsage() const;
70 unsigned int getTextureSerial() const;
71 virtual unsigned int getRenderTargetSerial(GLenum target) const = 0;
jbauman@chromium.org68715282012-07-12 23:28:41 +000072 int getLodOffset() const;
73
74 protected:
75 int mLodOffset;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000076
77 private:
78 DISALLOW_COPY_AND_ASSIGN(TextureStorage);
79
80 const DWORD mD3DUsage;
81 const D3DPOOL mD3DPool;
82
83 const unsigned int mTextureSerial;
84 static unsigned int issueTextureSerial();
85
86 static unsigned int mCurrentTextureSerial;
87};
88
89class Texture : public RefCountObject
90{
91 public:
92 explicit Texture(GLuint id);
93
94 virtual ~Texture();
95
daniel@transgaming.comb9d7e6f2012-10-31 19:08:32 +000096 static D3DFORMAT ConvertTextureInternalFormat(GLint internalformat);
97 static bool IsTextureFormatRenderable(D3DFORMAT format);
98
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000099 virtual void addProxyRef(const Renderbuffer *proxy) = 0;
100 virtual void releaseProxy(const Renderbuffer *proxy) = 0;
101
102 virtual GLenum getTarget() const = 0;
103
104 bool setMinFilter(GLenum filter);
105 bool setMagFilter(GLenum filter);
106 bool setWrapS(GLenum wrap);
107 bool setWrapT(GLenum wrap);
daniel@transgaming.com07ab8412012-07-12 15:17:09 +0000108 bool setMaxAnisotropy(float textureMaxAnisotropy, float contextMaxAnisotropy);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000109 bool setUsage(GLenum usage);
110
111 GLenum getMinFilter() const;
112 GLenum getMagFilter() const;
113 GLenum getWrapS() const;
114 GLenum getWrapT() const;
daniel@transgaming.com07ab8412012-07-12 15:17:09 +0000115 float getMaxAnisotropy() const;
daniel@transgaming.comebf139f2012-10-31 18:07:32 +0000116 int getLodOffset();
117 void getSamplerState(SamplerState *sampler);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000118 GLenum getUsage() const;
daniel@transgaming.comca9a3c82012-10-26 18:55:07 +0000119 bool isMipmapFiltered() const;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000120
121 virtual bool isSamplerComplete() const = 0;
122
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000123 IDirect3DBaseTexture9 *getD3DTexture(); // D3D9_REPLACE
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000124 virtual Renderbuffer *getRenderbuffer(GLenum target) = 0;
125
126 virtual void generateMipmaps() = 0;
127 virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source) = 0;
128
129 bool hasDirtyParameters() const;
130 bool hasDirtyImages() const;
131 void resetDirty();
132 unsigned int getTextureSerial();
133 unsigned int getRenderTargetSerial(GLenum target);
134
135 bool isImmutable() const;
136
137 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.
138
139 protected:
140 void setImage(GLint unpackAlignment, const void *pixels, Image *image);
141 bool subImage(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels, Image *image);
142 void setCompressedImage(GLsizei imageSize, const void *pixels, Image *image);
143 bool subImageCompressed(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels, Image *image);
144
145 GLint creationLevels(GLsizei width, GLsizei height) const;
146 GLint creationLevels(GLsizei size) const;
147
148 virtual IDirect3DBaseTexture9 *getBaseTexture() const = 0;
149 virtual void createTexture() = 0;
150 virtual void updateTexture() = 0;
151 virtual void convertToRenderTarget() = 0;
152 virtual IDirect3DSurface9 *getRenderTarget(GLenum target) = 0;
153
jbauman@chromium.org6bc4a142012-09-06 21:28:30 +0000154 int levelCount();
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000155
156 static Blit *getBlitter();
157 static bool copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged);
158
daniel@transgaming.comebf139f2012-10-31 18:07:32 +0000159 SamplerState mSamplerState;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000160 bool mDirtyParameters;
161 GLenum mUsage;
162
163 bool mDirtyImages;
164
165 bool mImmutable;
166
167 private:
168 DISALLOW_COPY_AND_ASSIGN(Texture);
169
170 virtual TextureStorage *getStorage(bool renderTarget) = 0;
171};
172
173class TextureStorage2D : public TextureStorage
174{
175 public:
176 explicit TextureStorage2D(IDirect3DTexture9 *surfaceTexture);
177 TextureStorage2D(int levels, D3DFORMAT format, DWORD usage, int width, int height);
178
179 virtual ~TextureStorage2D();
180
daniel@transgaming.com2b5af7b2012-09-27 17:46:15 +0000181 IDirect3DSurface9 *getSurfaceLevel(int level, bool dirty);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000182 IDirect3DBaseTexture9 *getBaseTexture() const;
183
184 virtual unsigned int getRenderTargetSerial(GLenum target) const;
185
186 private:
187 DISALLOW_COPY_AND_ASSIGN(TextureStorage2D);
188
189 IDirect3DTexture9 *mTexture;
190 const unsigned int mRenderTargetSerial;
191};
192
193class Texture2D : public Texture
194{
195 public:
196 explicit Texture2D(GLuint id);
197
198 ~Texture2D();
199
200 void addProxyRef(const Renderbuffer *proxy);
201 void releaseProxy(const Renderbuffer *proxy);
202
203 virtual GLenum getTarget() const;
204
205 GLsizei getWidth(GLint level) const;
206 GLsizei getHeight(GLint level) const;
207 GLenum getInternalFormat(GLint level) const;
208 D3DFORMAT getD3DFormat(GLint level) const;
209 bool isCompressed(GLint level) const;
210 bool isDepth(GLint level) const;
211
212 void setImage(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
213 void setCompressedImage(GLint level, GLenum format, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels);
214 void subImage(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
215 void subImageCompressed(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels);
216 void copyImage(GLint level, GLenum format, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
217 virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
218 void storage(GLsizei levels, GLenum internalformat, GLsizei width, GLsizei height);
219
220 virtual bool isSamplerComplete() const;
221 virtual void bindTexImage(egl::Surface *surface);
222 virtual void releaseTexImage();
223
224 virtual void generateMipmaps();
225
226 virtual Renderbuffer *getRenderbuffer(GLenum target);
227
228 protected:
229 friend class RenderbufferTexture2D;
230 virtual IDirect3DSurface9 *getRenderTarget(GLenum target);
231 virtual IDirect3DSurface9 *getDepthStencil(GLenum target);
232
233 private:
234 DISALLOW_COPY_AND_ASSIGN(Texture2D);
235
236 virtual IDirect3DBaseTexture9 *getBaseTexture() const;
237 virtual void createTexture();
238 virtual void updateTexture();
239 virtual void convertToRenderTarget();
240 virtual TextureStorage *getStorage(bool renderTarget);
241
242 bool isMipmapComplete() const;
243
daniel@transgaming.com6452adf2012-10-17 18:22:35 +0000244 void redefineImage(GLint level, GLint internalformat, GLsizei width, GLsizei height);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000245 void commitRect(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height);
246
247 Image mImageArray[IMPLEMENTATION_MAX_TEXTURE_LEVELS];
248
249 TextureStorage2D *mTexStorage;
250 egl::Surface *mSurface;
251
252 // A specific internal reference count is kept for colorbuffer proxy references,
253 // because, as the renderbuffer acting as proxy will maintain a binding pointer
254 // back to this texture, there would be a circular reference if we used a binding
255 // pointer here. This reference count will cause the pointer to be set to NULL if
256 // the count drops to zero, but will not cause deletion of the Renderbuffer.
257 Renderbuffer *mColorbufferProxy;
258 unsigned int mProxyRefs;
259};
260
261class TextureStorageCubeMap : public TextureStorage
262{
263 public:
264 TextureStorageCubeMap(int levels, D3DFORMAT format, DWORD usage, int size);
265
266 virtual ~TextureStorageCubeMap();
267
daniel@transgaming.com2b5af7b2012-09-27 17:46:15 +0000268 IDirect3DSurface9 *getCubeMapSurface(GLenum faceTarget, int level, bool dirty);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000269 IDirect3DBaseTexture9 *getBaseTexture() const;
270
271 virtual unsigned int getRenderTargetSerial(GLenum target) const;
272
273 private:
274 DISALLOW_COPY_AND_ASSIGN(TextureStorageCubeMap);
275
276 IDirect3DCubeTexture9 *mTexture;
277 const unsigned int mFirstRenderTargetSerial;
278};
279
280class TextureCubeMap : public Texture
281{
282 public:
283 explicit TextureCubeMap(GLuint id);
284
285 ~TextureCubeMap();
286
287 void addProxyRef(const Renderbuffer *proxy);
288 void releaseProxy(const Renderbuffer *proxy);
289
290 virtual GLenum getTarget() const;
291
292 GLsizei getWidth(GLenum target, GLint level) const;
293 GLsizei getHeight(GLenum target, GLint level) const;
294 GLenum getInternalFormat(GLenum target, GLint level) const;
295 D3DFORMAT getD3DFormat(GLenum target, GLint level) const;
296 bool isCompressed(GLenum target, GLint level) const;
297
298 void setImagePosX(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
299 void setImageNegX(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
300 void setImagePosY(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
301 void setImageNegY(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
302 void setImagePosZ(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
303 void setImageNegZ(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
304
305 void setCompressedImage(GLenum face, GLint level, GLenum format, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels);
306
307 void subImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
308 void subImageCompressed(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels);
309 void copyImage(GLenum target, GLint level, GLenum format, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
310 virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
311 void storage(GLsizei levels, GLenum internalformat, GLsizei size);
312
313 virtual bool isSamplerComplete() const;
314
315 virtual void generateMipmaps();
316
317 virtual Renderbuffer *getRenderbuffer(GLenum target);
318
319 static unsigned int faceIndex(GLenum face);
320
321 protected:
322 friend class RenderbufferTextureCubeMap;
323 virtual IDirect3DSurface9 *getRenderTarget(GLenum target);
324
325 private:
326 DISALLOW_COPY_AND_ASSIGN(TextureCubeMap);
327
328 virtual IDirect3DBaseTexture9 *getBaseTexture() const;
329 virtual void createTexture();
330 virtual void updateTexture();
331 virtual void convertToRenderTarget();
332 virtual TextureStorage *getStorage(bool renderTarget);
333
334 bool isCubeComplete() const;
335 bool isMipmapCubeComplete() const;
336
337 void setImage(int faceIndex, GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
338 void commitRect(int faceIndex, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height);
daniel@transgaming.com6452adf2012-10-17 18:22:35 +0000339 void redefineImage(int faceIndex, GLint level, GLint internalformat, GLsizei width, GLsizei height);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000340
341 Image mImageArray[6][IMPLEMENTATION_MAX_TEXTURE_LEVELS];
342
343 TextureStorageCubeMap *mTexStorage;
344
345 // A specific internal reference count is kept for colorbuffer proxy references,
346 // because, as the renderbuffer acting as proxy will maintain a binding pointer
347 // back to this texture, there would be a circular reference if we used a binding
348 // pointer here. This reference count will cause the pointer to be set to NULL if
349 // the count drops to zero, but will not cause deletion of the Renderbuffer.
350 Renderbuffer *mFaceProxies[6];
351 unsigned int *mFaceProxyRefs[6];
352};
353}
354
jbauman@chromium.org68715282012-07-12 23:28:41 +0000355#endif // LIBGLESV2_TEXTURE_H_