blob: 7d7378f88b03dcdb352955ec21c1dfa8e435840d [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"
22#include "libGLESv2/Renderbuffer.h"
23#include "libGLESv2/utilities.h"
24
25namespace egl
26{
27class Surface;
28}
29
30namespace gl
31{
32class Blit;
33class Framebuffer;
34
35enum
36{
37 // These are the maximums the implementation can support
38 // The actual GL caps are limited by the device caps
39 // and should be queried from the Context
40 IMPLEMENTATION_MAX_TEXTURE_SIZE = 16384,
41 IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE = 16384,
42
43 IMPLEMENTATION_MAX_TEXTURE_LEVELS = 15 // 1+log2 of MAX_TEXTURE_SIZE
44};
45
46class Image
47{
48 public:
49 Image();
50 ~Image();
51
daniel@transgaming.com6452adf2012-10-17 18:22:35 +000052 bool redefine(GLint internalformat, GLsizei width, GLsizei height, bool forceRelease);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000053 void markDirty() {mDirty = true;}
54 void markClean() {mDirty = false;}
55
56 bool isRenderableFormat() const;
57 D3DFORMAT getD3DFormat() const;
58
59 GLsizei getWidth() const {return mWidth;}
60 GLsizei getHeight() const {return mHeight;}
daniel@transgaming.com6452adf2012-10-17 18:22:35 +000061 GLenum getInternalFormat() const {return mInternalFormat;}
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000062 bool isDirty() const {return mSurface && mDirty;}
63 IDirect3DSurface9 *getSurface();
64
65 void setManagedSurface(IDirect3DSurface9 *surface);
66 void updateSurface(IDirect3DSurface9 *dest, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height);
67
daniel@transgaming.com6452adf2012-10-17 18:22:35 +000068 void loadData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000069 GLint unpackAlignment, const void *input);
70
71 void loadAlphaData(GLsizei width, GLsizei height,
72 int inputPitch, const void *input, size_t outputPitch, void *output) const;
73 void loadAlphaDataSSE2(GLsizei width, GLsizei height,
74 int inputPitch, const void *input, size_t outputPitch, void *output) const;
75 void loadAlphaFloatData(GLsizei width, GLsizei height,
76 int inputPitch, const void *input, size_t outputPitch, void *output) const;
77 void loadAlphaHalfFloatData(GLsizei width, GLsizei height,
78 int inputPitch, const void *input, size_t outputPitch, void *output) const;
79 void loadLuminanceData(GLsizei width, GLsizei height,
80 int inputPitch, const void *input, size_t outputPitch, void *output, bool native) const;
81 void loadLuminanceFloatData(GLsizei width, GLsizei height,
82 int inputPitch, const void *input, size_t outputPitch, void *output) const;
83 void loadLuminanceHalfFloatData(GLsizei width, GLsizei height,
84 int inputPitch, const void *input, size_t outputPitch, void *output) const;
85 void loadLuminanceAlphaData(GLsizei width, GLsizei height,
86 int inputPitch, const void *input, size_t outputPitch, void *output, bool native) const;
87 void loadLuminanceAlphaFloatData(GLsizei width, GLsizei height,
88 int inputPitch, const void *input, size_t outputPitch, void *output) const;
89 void loadLuminanceAlphaHalfFloatData(GLsizei width, GLsizei height,
90 int inputPitch, const void *input, size_t outputPitch, void *output) const;
91 void loadRGBUByteData(GLsizei width, GLsizei height,
92 int inputPitch, const void *input, size_t outputPitch, void *output) const;
93 void loadRGB565Data(GLsizei width, GLsizei height,
94 int inputPitch, const void *input, size_t outputPitch, void *output) const;
95 void loadRGBFloatData(GLsizei width, GLsizei height,
96 int inputPitch, const void *input, size_t outputPitch, void *output) const;
97 void loadRGBHalfFloatData(GLsizei width, GLsizei height,
98 int inputPitch, const void *input, size_t outputPitch, void *output) const;
99 void loadRGBAUByteDataSSE2(GLsizei width, GLsizei height,
100 int inputPitch, const void *input, size_t outputPitch, void *output) const;
101 void loadRGBAUByteData(GLsizei width, GLsizei height,
102 int inputPitch, const void *input, size_t outputPitch, void *output) const;
103 void loadRGBA4444Data(GLsizei width, GLsizei height,
104 int inputPitch, const void *input, size_t outputPitch, void *output) const;
105 void loadRGBA5551Data(GLsizei width, GLsizei height,
106 int inputPitch, const void *input, size_t outputPitch, void *output) const;
107 void loadRGBAFloatData(GLsizei width, GLsizei height,
108 int inputPitch, const void *input, size_t outputPitch, void *output) const;
109 void loadRGBAHalfFloatData(GLsizei width, GLsizei height,
110 int inputPitch, const void *input, size_t outputPitch, void *output) const;
111 void loadBGRAData(GLsizei width, GLsizei height,
112 int inputPitch, const void *input, size_t outputPitch, void *output) const;
113 void loadCompressedData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
114 const void *input);
115
116 void copy(GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, IDirect3DSurface9 *renderTarget);
117
118 private:
119 DISALLOW_COPY_AND_ASSIGN(Image);
120
121 void createSurface();
122
123 HRESULT lock(D3DLOCKED_RECT *lockedRect, const RECT *rect);
124 void unlock();
125
126 GLsizei mWidth;
127 GLsizei mHeight;
daniel@transgaming.com6452adf2012-10-17 18:22:35 +0000128 GLint mInternalFormat;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000129
130 bool mDirty;
131
132 D3DPOOL mD3DPool; // can only be D3DPOOL_SYSTEMMEM or D3DPOOL_MANAGED since it needs to be lockable.
133 D3DFORMAT mD3DFormat;
134
135 IDirect3DSurface9 *mSurface;
136};
137
138class TextureStorage
139{
140 public:
141 explicit TextureStorage(DWORD usage);
142
143 virtual ~TextureStorage();
144
145 bool isRenderTarget() const;
146 bool isManaged() const;
147 D3DPOOL getPool() const;
148 DWORD getUsage() const;
149 unsigned int getTextureSerial() const;
150 virtual unsigned int getRenderTargetSerial(GLenum target) const = 0;
jbauman@chromium.org68715282012-07-12 23:28:41 +0000151 int getLodOffset() const;
152
153 protected:
154 int mLodOffset;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000155
156 private:
157 DISALLOW_COPY_AND_ASSIGN(TextureStorage);
158
159 const DWORD mD3DUsage;
160 const D3DPOOL mD3DPool;
161
162 const unsigned int mTextureSerial;
163 static unsigned int issueTextureSerial();
164
165 static unsigned int mCurrentTextureSerial;
166};
167
168class Texture : public RefCountObject
169{
170 public:
171 explicit Texture(GLuint id);
172
173 virtual ~Texture();
174
175 virtual void addProxyRef(const Renderbuffer *proxy) = 0;
176 virtual void releaseProxy(const Renderbuffer *proxy) = 0;
177
178 virtual GLenum getTarget() const = 0;
179
180 bool setMinFilter(GLenum filter);
181 bool setMagFilter(GLenum filter);
182 bool setWrapS(GLenum wrap);
183 bool setWrapT(GLenum wrap);
daniel@transgaming.com07ab8412012-07-12 15:17:09 +0000184 bool setMaxAnisotropy(float textureMaxAnisotropy, float contextMaxAnisotropy);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000185 bool setUsage(GLenum usage);
186
187 GLenum getMinFilter() const;
188 GLenum getMagFilter() const;
189 GLenum getWrapS() const;
190 GLenum getWrapT() const;
daniel@transgaming.com07ab8412012-07-12 15:17:09 +0000191 float getMaxAnisotropy() const;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000192 GLenum getUsage() const;
193
194 virtual bool isSamplerComplete() const = 0;
195
196 IDirect3DBaseTexture9 *getTexture();
197 virtual Renderbuffer *getRenderbuffer(GLenum target) = 0;
198
199 virtual void generateMipmaps() = 0;
200 virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source) = 0;
201
202 bool hasDirtyParameters() const;
203 bool hasDirtyImages() const;
204 void resetDirty();
205 unsigned int getTextureSerial();
206 unsigned int getRenderTargetSerial(GLenum target);
207
208 bool isImmutable() const;
jbauman@chromium.org68715282012-07-12 23:28:41 +0000209 int getLodOffset();
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000210
211 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.
212
213 protected:
214 void setImage(GLint unpackAlignment, const void *pixels, Image *image);
215 bool subImage(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels, Image *image);
216 void setCompressedImage(GLsizei imageSize, const void *pixels, Image *image);
217 bool subImageCompressed(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels, Image *image);
218
219 GLint creationLevels(GLsizei width, GLsizei height) const;
220 GLint creationLevels(GLsizei size) const;
221
222 virtual IDirect3DBaseTexture9 *getBaseTexture() const = 0;
223 virtual void createTexture() = 0;
224 virtual void updateTexture() = 0;
225 virtual void convertToRenderTarget() = 0;
226 virtual IDirect3DSurface9 *getRenderTarget(GLenum target) = 0;
227
jbauman@chromium.org6bc4a142012-09-06 21:28:30 +0000228 int levelCount();
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000229
230 static Blit *getBlitter();
231 static bool copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged);
232
233 GLenum mMinFilter;
234 GLenum mMagFilter;
235 GLenum mWrapS;
236 GLenum mWrapT;
daniel@transgaming.com07ab8412012-07-12 15:17:09 +0000237 float mMaxAnisotropy;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000238 bool mDirtyParameters;
239 GLenum mUsage;
240
241 bool mDirtyImages;
242
243 bool mImmutable;
244
245 private:
246 DISALLOW_COPY_AND_ASSIGN(Texture);
247
248 virtual TextureStorage *getStorage(bool renderTarget) = 0;
249};
250
251class TextureStorage2D : public TextureStorage
252{
253 public:
254 explicit TextureStorage2D(IDirect3DTexture9 *surfaceTexture);
255 TextureStorage2D(int levels, D3DFORMAT format, DWORD usage, int width, int height);
256
257 virtual ~TextureStorage2D();
258
daniel@transgaming.com2b5af7b2012-09-27 17:46:15 +0000259 IDirect3DSurface9 *getSurfaceLevel(int level, bool dirty);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000260 IDirect3DBaseTexture9 *getBaseTexture() const;
261
262 virtual unsigned int getRenderTargetSerial(GLenum target) const;
263
264 private:
265 DISALLOW_COPY_AND_ASSIGN(TextureStorage2D);
266
267 IDirect3DTexture9 *mTexture;
268 const unsigned int mRenderTargetSerial;
269};
270
271class Texture2D : public Texture
272{
273 public:
274 explicit Texture2D(GLuint id);
275
276 ~Texture2D();
277
278 void addProxyRef(const Renderbuffer *proxy);
279 void releaseProxy(const Renderbuffer *proxy);
280
281 virtual GLenum getTarget() const;
282
283 GLsizei getWidth(GLint level) const;
284 GLsizei getHeight(GLint level) const;
285 GLenum getInternalFormat(GLint level) const;
286 D3DFORMAT getD3DFormat(GLint level) const;
287 bool isCompressed(GLint level) const;
288 bool isDepth(GLint level) const;
289
290 void setImage(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
291 void setCompressedImage(GLint level, GLenum format, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels);
292 void subImage(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
293 void subImageCompressed(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels);
294 void copyImage(GLint level, GLenum format, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
295 virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
296 void storage(GLsizei levels, GLenum internalformat, GLsizei width, GLsizei height);
297
298 virtual bool isSamplerComplete() const;
299 virtual void bindTexImage(egl::Surface *surface);
300 virtual void releaseTexImage();
301
302 virtual void generateMipmaps();
303
304 virtual Renderbuffer *getRenderbuffer(GLenum target);
305
306 protected:
307 friend class RenderbufferTexture2D;
308 virtual IDirect3DSurface9 *getRenderTarget(GLenum target);
309 virtual IDirect3DSurface9 *getDepthStencil(GLenum target);
310
311 private:
312 DISALLOW_COPY_AND_ASSIGN(Texture2D);
313
314 virtual IDirect3DBaseTexture9 *getBaseTexture() const;
315 virtual void createTexture();
316 virtual void updateTexture();
317 virtual void convertToRenderTarget();
318 virtual TextureStorage *getStorage(bool renderTarget);
319
320 bool isMipmapComplete() const;
321
daniel@transgaming.com6452adf2012-10-17 18:22:35 +0000322 void redefineImage(GLint level, GLint internalformat, GLsizei width, GLsizei height);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000323 void commitRect(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height);
324
325 Image mImageArray[IMPLEMENTATION_MAX_TEXTURE_LEVELS];
326
327 TextureStorage2D *mTexStorage;
328 egl::Surface *mSurface;
329
330 // A specific internal reference count is kept for colorbuffer proxy references,
331 // because, as the renderbuffer acting as proxy will maintain a binding pointer
332 // back to this texture, there would be a circular reference if we used a binding
333 // pointer here. This reference count will cause the pointer to be set to NULL if
334 // the count drops to zero, but will not cause deletion of the Renderbuffer.
335 Renderbuffer *mColorbufferProxy;
336 unsigned int mProxyRefs;
337};
338
339class TextureStorageCubeMap : public TextureStorage
340{
341 public:
342 TextureStorageCubeMap(int levels, D3DFORMAT format, DWORD usage, int size);
343
344 virtual ~TextureStorageCubeMap();
345
daniel@transgaming.com2b5af7b2012-09-27 17:46:15 +0000346 IDirect3DSurface9 *getCubeMapSurface(GLenum faceTarget, int level, bool dirty);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000347 IDirect3DBaseTexture9 *getBaseTexture() const;
348
349 virtual unsigned int getRenderTargetSerial(GLenum target) const;
350
351 private:
352 DISALLOW_COPY_AND_ASSIGN(TextureStorageCubeMap);
353
354 IDirect3DCubeTexture9 *mTexture;
355 const unsigned int mFirstRenderTargetSerial;
356};
357
358class TextureCubeMap : public Texture
359{
360 public:
361 explicit TextureCubeMap(GLuint id);
362
363 ~TextureCubeMap();
364
365 void addProxyRef(const Renderbuffer *proxy);
366 void releaseProxy(const Renderbuffer *proxy);
367
368 virtual GLenum getTarget() const;
369
370 GLsizei getWidth(GLenum target, GLint level) const;
371 GLsizei getHeight(GLenum target, GLint level) const;
372 GLenum getInternalFormat(GLenum target, GLint level) const;
373 D3DFORMAT getD3DFormat(GLenum target, GLint level) const;
374 bool isCompressed(GLenum target, GLint level) const;
375
376 void setImagePosX(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
377 void setImageNegX(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
378 void setImagePosY(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
379 void setImageNegY(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
380 void setImagePosZ(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
381 void setImageNegZ(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
382
383 void setCompressedImage(GLenum face, GLint level, GLenum format, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels);
384
385 void subImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
386 void subImageCompressed(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels);
387 void copyImage(GLenum target, GLint level, GLenum format, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
388 virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
389 void storage(GLsizei levels, GLenum internalformat, GLsizei size);
390
391 virtual bool isSamplerComplete() const;
392
393 virtual void generateMipmaps();
394
395 virtual Renderbuffer *getRenderbuffer(GLenum target);
396
397 static unsigned int faceIndex(GLenum face);
398
399 protected:
400 friend class RenderbufferTextureCubeMap;
401 virtual IDirect3DSurface9 *getRenderTarget(GLenum target);
402
403 private:
404 DISALLOW_COPY_AND_ASSIGN(TextureCubeMap);
405
406 virtual IDirect3DBaseTexture9 *getBaseTexture() const;
407 virtual void createTexture();
408 virtual void updateTexture();
409 virtual void convertToRenderTarget();
410 virtual TextureStorage *getStorage(bool renderTarget);
411
412 bool isCubeComplete() const;
413 bool isMipmapCubeComplete() const;
414
415 void setImage(int faceIndex, GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
416 void commitRect(int faceIndex, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height);
daniel@transgaming.com6452adf2012-10-17 18:22:35 +0000417 void redefineImage(int faceIndex, GLint level, GLint internalformat, GLsizei width, GLsizei height);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000418
419 Image mImageArray[6][IMPLEMENTATION_MAX_TEXTURE_LEVELS];
420
421 TextureStorageCubeMap *mTexStorage;
422
423 // A specific internal reference count is kept for colorbuffer proxy references,
424 // because, as the renderbuffer acting as proxy will maintain a binding pointer
425 // back to this texture, there would be a circular reference if we used a binding
426 // pointer here. This reference count will cause the pointer to be set to NULL if
427 // the count drops to zero, but will not cause deletion of the Renderbuffer.
428 Renderbuffer *mFaceProxies[6];
429 unsigned int *mFaceProxyRefs[6];
430};
431}
432
jbauman@chromium.org68715282012-07-12 23:28:41 +0000433#endif // LIBGLESV2_TEXTURE_H_