blob: ccddf5792cb250532de7bccdeb7d9314a880b4b9 [file] [log] [blame]
daniel@transgaming.com95a758f2012-07-12 15:17:06 +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);
daniel@transgaming.com07ab8412012-07-12 15:17:09 +0000182 bool setMaxAnisotropy(float textureMaxAnisotropy, float contextMaxAnisotropy);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000183 bool setUsage(GLenum usage);
184
185 GLenum getMinFilter() const;
186 GLenum getMagFilter() const;
187 GLenum getWrapS() const;
188 GLenum getWrapT() const;
daniel@transgaming.com07ab8412012-07-12 15:17:09 +0000189 float getMaxAnisotropy() const;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000190 GLenum getUsage() const;
191
192 virtual bool isSamplerComplete() const = 0;
193
194 IDirect3DBaseTexture9 *getTexture();
195 virtual Renderbuffer *getRenderbuffer(GLenum target) = 0;
196
197 virtual void generateMipmaps() = 0;
198 virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source) = 0;
199
200 bool hasDirtyParameters() const;
201 bool hasDirtyImages() const;
202 void resetDirty();
203 unsigned int getTextureSerial();
204 unsigned int getRenderTargetSerial(GLenum target);
205
206 bool isImmutable() const;
207
208 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.
209
210 protected:
211 void setImage(GLint unpackAlignment, const void *pixels, Image *image);
212 bool subImage(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels, Image *image);
213 void setCompressedImage(GLsizei imageSize, const void *pixels, Image *image);
214 bool subImageCompressed(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels, Image *image);
215
216 GLint creationLevels(GLsizei width, GLsizei height) const;
217 GLint creationLevels(GLsizei size) const;
218
219 virtual IDirect3DBaseTexture9 *getBaseTexture() const = 0;
220 virtual void createTexture() = 0;
221 virtual void updateTexture() = 0;
222 virtual void convertToRenderTarget() = 0;
223 virtual IDirect3DSurface9 *getRenderTarget(GLenum target) = 0;
224
225 int levelCount() const;
226
227 static Blit *getBlitter();
228 static bool copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged);
229
230 GLenum mMinFilter;
231 GLenum mMagFilter;
232 GLenum mWrapS;
233 GLenum mWrapT;
daniel@transgaming.com07ab8412012-07-12 15:17:09 +0000234 float mMaxAnisotropy;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000235 bool mDirtyParameters;
236 GLenum mUsage;
237
238 bool mDirtyImages;
239
240 bool mImmutable;
241
242 private:
243 DISALLOW_COPY_AND_ASSIGN(Texture);
244
245 virtual TextureStorage *getStorage(bool renderTarget) = 0;
246};
247
248class TextureStorage2D : public TextureStorage
249{
250 public:
251 explicit TextureStorage2D(IDirect3DTexture9 *surfaceTexture);
252 TextureStorage2D(int levels, D3DFORMAT format, DWORD usage, int width, int height);
253
254 virtual ~TextureStorage2D();
255
256 IDirect3DSurface9 *getSurfaceLevel(int level);
257 IDirect3DBaseTexture9 *getBaseTexture() const;
258
259 virtual unsigned int getRenderTargetSerial(GLenum target) const;
260
261 private:
262 DISALLOW_COPY_AND_ASSIGN(TextureStorage2D);
263
264 IDirect3DTexture9 *mTexture;
265 const unsigned int mRenderTargetSerial;
266};
267
268class Texture2D : public Texture
269{
270 public:
271 explicit Texture2D(GLuint id);
272
273 ~Texture2D();
274
275 void addProxyRef(const Renderbuffer *proxy);
276 void releaseProxy(const Renderbuffer *proxy);
277
278 virtual GLenum getTarget() const;
279
280 GLsizei getWidth(GLint level) const;
281 GLsizei getHeight(GLint level) const;
282 GLenum getInternalFormat(GLint level) const;
283 D3DFORMAT getD3DFormat(GLint level) const;
284 bool isCompressed(GLint level) const;
285 bool isDepth(GLint level) const;
286
287 void setImage(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
288 void setCompressedImage(GLint level, GLenum format, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels);
289 void subImage(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
290 void subImageCompressed(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels);
291 void copyImage(GLint level, GLenum format, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
292 virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
293 void storage(GLsizei levels, GLenum internalformat, GLsizei width, GLsizei height);
294
295 virtual bool isSamplerComplete() const;
296 virtual void bindTexImage(egl::Surface *surface);
297 virtual void releaseTexImage();
298
299 virtual void generateMipmaps();
300
301 virtual Renderbuffer *getRenderbuffer(GLenum target);
302
303 protected:
304 friend class RenderbufferTexture2D;
305 virtual IDirect3DSurface9 *getRenderTarget(GLenum target);
306 virtual IDirect3DSurface9 *getDepthStencil(GLenum target);
307
308 private:
309 DISALLOW_COPY_AND_ASSIGN(Texture2D);
310
311 virtual IDirect3DBaseTexture9 *getBaseTexture() const;
312 virtual void createTexture();
313 virtual void updateTexture();
314 virtual void convertToRenderTarget();
315 virtual TextureStorage *getStorage(bool renderTarget);
316
317 bool isMipmapComplete() const;
318
319 void redefineImage(GLint level, GLenum format, GLsizei width, GLsizei height, GLenum type);
320 void commitRect(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height);
321
322 Image mImageArray[IMPLEMENTATION_MAX_TEXTURE_LEVELS];
323
324 TextureStorage2D *mTexStorage;
325 egl::Surface *mSurface;
326
327 // A specific internal reference count is kept for colorbuffer proxy references,
328 // because, as the renderbuffer acting as proxy will maintain a binding pointer
329 // back to this texture, there would be a circular reference if we used a binding
330 // pointer here. This reference count will cause the pointer to be set to NULL if
331 // the count drops to zero, but will not cause deletion of the Renderbuffer.
332 Renderbuffer *mColorbufferProxy;
333 unsigned int mProxyRefs;
334};
335
336class TextureStorageCubeMap : public TextureStorage
337{
338 public:
339 TextureStorageCubeMap(int levels, D3DFORMAT format, DWORD usage, int size);
340
341 virtual ~TextureStorageCubeMap();
342
343 IDirect3DSurface9 *getCubeMapSurface(GLenum faceTarget, int level);
344 IDirect3DBaseTexture9 *getBaseTexture() const;
345
346 virtual unsigned int getRenderTargetSerial(GLenum target) const;
347
348 private:
349 DISALLOW_COPY_AND_ASSIGN(TextureStorageCubeMap);
350
351 IDirect3DCubeTexture9 *mTexture;
352 const unsigned int mFirstRenderTargetSerial;
353};
354
355class TextureCubeMap : public Texture
356{
357 public:
358 explicit TextureCubeMap(GLuint id);
359
360 ~TextureCubeMap();
361
362 void addProxyRef(const Renderbuffer *proxy);
363 void releaseProxy(const Renderbuffer *proxy);
364
365 virtual GLenum getTarget() const;
366
367 GLsizei getWidth(GLenum target, GLint level) const;
368 GLsizei getHeight(GLenum target, GLint level) const;
369 GLenum getInternalFormat(GLenum target, GLint level) const;
370 D3DFORMAT getD3DFormat(GLenum target, GLint level) const;
371 bool isCompressed(GLenum target, GLint level) const;
372
373 void setImagePosX(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
374 void setImageNegX(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
375 void setImagePosY(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
376 void setImageNegY(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
377 void setImagePosZ(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
378 void setImageNegZ(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
379
380 void setCompressedImage(GLenum face, GLint level, GLenum format, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels);
381
382 void subImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
383 void subImageCompressed(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels);
384 void copyImage(GLenum target, GLint level, GLenum format, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
385 virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
386 void storage(GLsizei levels, GLenum internalformat, GLsizei size);
387
388 virtual bool isSamplerComplete() const;
389
390 virtual void generateMipmaps();
391
392 virtual Renderbuffer *getRenderbuffer(GLenum target);
393
394 static unsigned int faceIndex(GLenum face);
395
396 protected:
397 friend class RenderbufferTextureCubeMap;
398 virtual IDirect3DSurface9 *getRenderTarget(GLenum target);
399
400 private:
401 DISALLOW_COPY_AND_ASSIGN(TextureCubeMap);
402
403 virtual IDirect3DBaseTexture9 *getBaseTexture() const;
404 virtual void createTexture();
405 virtual void updateTexture();
406 virtual void convertToRenderTarget();
407 virtual TextureStorage *getStorage(bool renderTarget);
408
409 bool isCubeComplete() const;
410 bool isMipmapCubeComplete() const;
411
412 void setImage(int faceIndex, GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
413 void commitRect(int faceIndex, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height);
414 void redefineImage(int faceIndex, GLint level, GLenum format, GLsizei width, GLsizei height, GLenum type);
415
416 Image mImageArray[6][IMPLEMENTATION_MAX_TEXTURE_LEVELS];
417
418 TextureStorageCubeMap *mTexStorage;
419
420 // A specific internal reference count is kept for colorbuffer proxy references,
421 // because, as the renderbuffer acting as proxy will maintain a binding pointer
422 // back to this texture, there would be a circular reference if we used a binding
423 // pointer here. This reference count will cause the pointer to be set to NULL if
424 // the count drops to zero, but will not cause deletion of the Renderbuffer.
425 Renderbuffer *mFaceProxies[6];
426 unsigned int *mFaceProxyRefs[6];
427};
428}
429
daniel@transgaming.comafa8ef32011-11-11 04:10:13 +0000430#endif // LIBGLESV2_TEXTURE_H_