blob: 80e288d1c66becd38c822e23cadd5485776131f8 [file] [log] [blame]
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001//
2// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
3// 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
52 bool redefine(GLenum format, GLsizei width, GLsizei height, GLenum type, bool forceRelease);
53 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;}
61 GLenum getFormat() const {return mFormat;}
62 GLenum getType() const {return mType;}
63 bool isDirty() const {return mSurface && mDirty;}
64 IDirect3DSurface9 *getSurface();
65
66 void setManagedSurface(IDirect3DSurface9 *surface);
67 void updateSurface(IDirect3DSurface9 *dest, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height);
68
69 void loadData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum type,
70 GLint unpackAlignment, const void *input);
71
72 void loadAlphaData(GLsizei width, GLsizei height,
73 int inputPitch, const void *input, size_t outputPitch, void *output) const;
74 void loadAlphaDataSSE2(GLsizei width, GLsizei height,
75 int inputPitch, const void *input, size_t outputPitch, void *output) const;
76 void loadAlphaFloatData(GLsizei width, GLsizei height,
77 int inputPitch, const void *input, size_t outputPitch, void *output) const;
78 void loadAlphaHalfFloatData(GLsizei width, GLsizei height,
79 int inputPitch, const void *input, size_t outputPitch, void *output) const;
80 void loadLuminanceData(GLsizei width, GLsizei height,
81 int inputPitch, const void *input, size_t outputPitch, void *output, bool native) const;
82 void loadLuminanceFloatData(GLsizei width, GLsizei height,
83 int inputPitch, const void *input, size_t outputPitch, void *output) const;
84 void loadLuminanceHalfFloatData(GLsizei width, GLsizei height,
85 int inputPitch, const void *input, size_t outputPitch, void *output) const;
86 void loadLuminanceAlphaData(GLsizei width, GLsizei height,
87 int inputPitch, const void *input, size_t outputPitch, void *output, bool native) const;
88 void loadLuminanceAlphaFloatData(GLsizei width, GLsizei height,
89 int inputPitch, const void *input, size_t outputPitch, void *output) const;
90 void loadLuminanceAlphaHalfFloatData(GLsizei width, GLsizei height,
91 int inputPitch, const void *input, size_t outputPitch, void *output) const;
92 void loadRGBUByteData(GLsizei width, GLsizei height,
93 int inputPitch, const void *input, size_t outputPitch, void *output) const;
94 void loadRGB565Data(GLsizei width, GLsizei height,
95 int inputPitch, const void *input, size_t outputPitch, void *output) const;
96 void loadRGBFloatData(GLsizei width, GLsizei height,
97 int inputPitch, const void *input, size_t outputPitch, void *output) const;
98 void loadRGBHalfFloatData(GLsizei width, GLsizei height,
99 int inputPitch, const void *input, size_t outputPitch, void *output) const;
100 void loadRGBAUByteDataSSE2(GLsizei width, GLsizei height,
101 int inputPitch, const void *input, size_t outputPitch, void *output) const;
102 void loadRGBAUByteData(GLsizei width, GLsizei height,
103 int inputPitch, const void *input, size_t outputPitch, void *output) const;
104 void loadRGBA4444Data(GLsizei width, GLsizei height,
105 int inputPitch, const void *input, size_t outputPitch, void *output) const;
106 void loadRGBA5551Data(GLsizei width, GLsizei height,
107 int inputPitch, const void *input, size_t outputPitch, void *output) const;
108 void loadRGBAFloatData(GLsizei width, GLsizei height,
109 int inputPitch, const void *input, size_t outputPitch, void *output) const;
110 void loadRGBAHalfFloatData(GLsizei width, GLsizei height,
111 int inputPitch, const void *input, size_t outputPitch, void *output) const;
112 void loadBGRAData(GLsizei width, GLsizei height,
113 int inputPitch, const void *input, size_t outputPitch, void *output) const;
114 void loadCompressedData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
115 const void *input);
116
117 void copy(GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, IDirect3DSurface9 *renderTarget);
118
119 private:
120 DISALLOW_COPY_AND_ASSIGN(Image);
121
122 void createSurface();
123
124 HRESULT lock(D3DLOCKED_RECT *lockedRect, const RECT *rect);
125 void unlock();
126
127 GLsizei mWidth;
128 GLsizei mHeight;
129 GLenum mFormat;
130 GLenum mType;
131
132 bool mDirty;
133
134 D3DPOOL mD3DPool; // can only be D3DPOOL_SYSTEMMEM or D3DPOOL_MANAGED since it needs to be lockable.
135 D3DFORMAT mD3DFormat;
136
137 IDirect3DSurface9 *mSurface;
138};
139
140class TextureStorage
141{
142 public:
143 explicit TextureStorage(DWORD usage);
144
145 virtual ~TextureStorage();
146
147 bool isRenderTarget() const;
148 bool isManaged() const;
149 D3DPOOL getPool() const;
150 DWORD getUsage() const;
151 unsigned int getTextureSerial() const;
152 virtual unsigned int getRenderTargetSerial(GLenum target) const = 0;
153
154 private:
155 DISALLOW_COPY_AND_ASSIGN(TextureStorage);
156
157 const DWORD mD3DUsage;
158 const D3DPOOL mD3DPool;
159
160 const unsigned int mTextureSerial;
161 static unsigned int issueTextureSerial();
162
163 static unsigned int mCurrentTextureSerial;
164};
165
166class Texture : public RefCountObject
167{
168 public:
169 explicit Texture(GLuint id);
170
171 virtual ~Texture();
172
173 virtual void addProxyRef(const Renderbuffer *proxy) = 0;
174 virtual void releaseProxy(const Renderbuffer *proxy) = 0;
175
176 virtual GLenum getTarget() const = 0;
177
178 bool setMinFilter(GLenum filter);
179 bool setMagFilter(GLenum filter);
180 bool setWrapS(GLenum wrap);
181 bool setWrapT(GLenum wrap);
182 bool setUsage(GLenum usage);
183
184 GLenum getMinFilter() const;
185 GLenum getMagFilter() const;
186 GLenum getWrapS() const;
187 GLenum getWrapT() const;
188 GLenum getUsage() const;
189
190 virtual bool isSamplerComplete() const = 0;
191
192 IDirect3DBaseTexture9 *getTexture();
193 virtual Renderbuffer *getRenderbuffer(GLenum target) = 0;
194
195 virtual void generateMipmaps() = 0;
196 virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source) = 0;
197
198 bool hasDirtyParameters() const;
199 bool hasDirtyImages() const;
200 void resetDirty();
201 unsigned int getTextureSerial();
202 unsigned int getRenderTargetSerial(GLenum target);
203
204 bool isImmutable() const;
205
206 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.
207
208 protected:
209 void setImage(GLint unpackAlignment, const void *pixels, Image *image);
210 bool subImage(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels, Image *image);
211 void setCompressedImage(GLsizei imageSize, const void *pixels, Image *image);
212 bool subImageCompressed(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels, Image *image);
213
214 GLint creationLevels(GLsizei width, GLsizei height) const;
215 GLint creationLevels(GLsizei size) const;
216
217 virtual IDirect3DBaseTexture9 *getBaseTexture() const = 0;
218 virtual void createTexture() = 0;
219 virtual void updateTexture() = 0;
220 virtual void convertToRenderTarget() = 0;
221 virtual IDirect3DSurface9 *getRenderTarget(GLenum target) = 0;
222
223 int levelCount() const;
224
225 static Blit *getBlitter();
226 static bool copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged);
227
228 GLenum mMinFilter;
229 GLenum mMagFilter;
230 GLenum mWrapS;
231 GLenum mWrapT;
232 bool mDirtyParameters;
233 GLenum mUsage;
234
235 bool mDirtyImages;
236
237 bool mImmutable;
238
239 private:
240 DISALLOW_COPY_AND_ASSIGN(Texture);
241
242 virtual TextureStorage *getStorage(bool renderTarget) = 0;
243};
244
245class TextureStorage2D : public TextureStorage
246{
247 public:
248 explicit TextureStorage2D(IDirect3DTexture9 *surfaceTexture);
249 TextureStorage2D(int levels, D3DFORMAT format, DWORD usage, int width, int height);
250
251 virtual ~TextureStorage2D();
252
253 IDirect3DSurface9 *getSurfaceLevel(int level);
254 IDirect3DBaseTexture9 *getBaseTexture() const;
255
256 virtual unsigned int getRenderTargetSerial(GLenum target) const;
257
258 private:
259 DISALLOW_COPY_AND_ASSIGN(TextureStorage2D);
260
261 IDirect3DTexture9 *mTexture;
262 const unsigned int mRenderTargetSerial;
263};
264
265class Texture2D : public Texture
266{
267 public:
268 explicit Texture2D(GLuint id);
269
270 ~Texture2D();
271
272 void addProxyRef(const Renderbuffer *proxy);
273 void releaseProxy(const Renderbuffer *proxy);
274
275 virtual GLenum getTarget() const;
276
277 GLsizei getWidth(GLint level) const;
278 GLsizei getHeight(GLint level) const;
279 GLenum getInternalFormat(GLint level) const;
280 D3DFORMAT getD3DFormat(GLint level) const;
281 bool isCompressed(GLint level) const;
282 bool isDepth(GLint level) const;
283
284 void setImage(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
285 void setCompressedImage(GLint level, GLenum format, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels);
286 void subImage(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
287 void subImageCompressed(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels);
288 void copyImage(GLint level, GLenum format, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
289 virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
290 void storage(GLsizei levels, GLenum internalformat, GLsizei width, GLsizei height);
291
292 virtual bool isSamplerComplete() const;
293 virtual void bindTexImage(egl::Surface *surface);
294 virtual void releaseTexImage();
295
296 virtual void generateMipmaps();
297
298 virtual Renderbuffer *getRenderbuffer(GLenum target);
299
300 protected:
301 friend class RenderbufferTexture2D;
302 virtual IDirect3DSurface9 *getRenderTarget(GLenum target);
303 virtual IDirect3DSurface9 *getDepthStencil(GLenum target);
304
305 private:
306 DISALLOW_COPY_AND_ASSIGN(Texture2D);
307
308 virtual IDirect3DBaseTexture9 *getBaseTexture() const;
309 virtual void createTexture();
310 virtual void updateTexture();
311 virtual void convertToRenderTarget();
312 virtual TextureStorage *getStorage(bool renderTarget);
313
314 bool isMipmapComplete() const;
315
316 void redefineImage(GLint level, GLenum format, GLsizei width, GLsizei height, GLenum type);
317 void commitRect(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height);
318
319 Image mImageArray[IMPLEMENTATION_MAX_TEXTURE_LEVELS];
320
321 TextureStorage2D *mTexStorage;
322 egl::Surface *mSurface;
323
324 // A specific internal reference count is kept for colorbuffer proxy references,
325 // because, as the renderbuffer acting as proxy will maintain a binding pointer
326 // back to this texture, there would be a circular reference if we used a binding
327 // pointer here. This reference count will cause the pointer to be set to NULL if
328 // the count drops to zero, but will not cause deletion of the Renderbuffer.
329 Renderbuffer *mColorbufferProxy;
330 unsigned int mProxyRefs;
331};
332
333class TextureStorageCubeMap : public TextureStorage
334{
335 public:
336 TextureStorageCubeMap(int levels, D3DFORMAT format, DWORD usage, int size);
337
338 virtual ~TextureStorageCubeMap();
339
340 IDirect3DSurface9 *getCubeMapSurface(GLenum faceTarget, int level);
341 IDirect3DBaseTexture9 *getBaseTexture() const;
342
343 virtual unsigned int getRenderTargetSerial(GLenum target) const;
344
345 private:
346 DISALLOW_COPY_AND_ASSIGN(TextureStorageCubeMap);
347
348 IDirect3DCubeTexture9 *mTexture;
349 const unsigned int mFirstRenderTargetSerial;
350};
351
352class TextureCubeMap : public Texture
353{
354 public:
355 explicit TextureCubeMap(GLuint id);
356
357 ~TextureCubeMap();
358
359 void addProxyRef(const Renderbuffer *proxy);
360 void releaseProxy(const Renderbuffer *proxy);
361
362 virtual GLenum getTarget() const;
363
364 GLsizei getWidth(GLenum target, GLint level) const;
365 GLsizei getHeight(GLenum target, GLint level) const;
366 GLenum getInternalFormat(GLenum target, GLint level) const;
367 D3DFORMAT getD3DFormat(GLenum target, GLint level) const;
368 bool isCompressed(GLenum target, GLint level) const;
369
370 void setImagePosX(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
371 void setImageNegX(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
372 void setImagePosY(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
373 void setImageNegY(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
374 void setImagePosZ(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
375 void setImageNegZ(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
376
377 void setCompressedImage(GLenum face, GLint level, GLenum format, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels);
378
379 void subImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
380 void subImageCompressed(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels);
381 void copyImage(GLenum target, GLint level, GLenum format, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
382 virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
383 void storage(GLsizei levels, GLenum internalformat, GLsizei size);
384
385 virtual bool isSamplerComplete() const;
386
387 virtual void generateMipmaps();
388
389 virtual Renderbuffer *getRenderbuffer(GLenum target);
390
391 static unsigned int faceIndex(GLenum face);
392
393 protected:
394 friend class RenderbufferTextureCubeMap;
395 virtual IDirect3DSurface9 *getRenderTarget(GLenum target);
396
397 private:
398 DISALLOW_COPY_AND_ASSIGN(TextureCubeMap);
399
400 virtual IDirect3DBaseTexture9 *getBaseTexture() const;
401 virtual void createTexture();
402 virtual void updateTexture();
403 virtual void convertToRenderTarget();
404 virtual TextureStorage *getStorage(bool renderTarget);
405
406 bool isCubeComplete() const;
407 bool isMipmapCubeComplete() const;
408
409 void setImage(int faceIndex, GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
410 void commitRect(int faceIndex, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height);
411 void redefineImage(int faceIndex, GLint level, GLenum format, GLsizei width, GLsizei height, GLenum type);
412
413 Image mImageArray[6][IMPLEMENTATION_MAX_TEXTURE_LEVELS];
414
415 TextureStorageCubeMap *mTexStorage;
416
417 // A specific internal reference count is kept for colorbuffer proxy references,
418 // because, as the renderbuffer acting as proxy will maintain a binding pointer
419 // back to this texture, there would be a circular reference if we used a binding
420 // pointer here. This reference count will cause the pointer to be set to NULL if
421 // the count drops to zero, but will not cause deletion of the Renderbuffer.
422 Renderbuffer *mFaceProxies[6];
423 unsigned int *mFaceProxyRefs[6];
424};
425}
426
daniel@transgaming.comafa8ef32011-11-11 04:10:13 +0000427#endif // LIBGLESV2_TEXTURE_H_