blob: 72e9282d2882d19a8151a2c68994fcad633768ec [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +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.cpp: Implements the gl::Texture class and its derived classes
8// Texture2D and TextureCubeMap. Implements GL texture objects and related
9// functionality. [OpenGL ES 2.0.24] section 3.7 page 63.
10
11#include "Texture.h"
12
13#include "main.h"
14#include "mathutil.h"
15#include "debug.h"
16
17namespace gl
18{
19Texture::Texture() : Colorbuffer(0)
20{
21 mMinFilter = GL_NEAREST_MIPMAP_LINEAR;
22 mMagFilter = GL_LINEAR;
23 mWrapS = GL_REPEAT;
24 mWrapT = GL_REPEAT;
25}
26
27Texture::~Texture()
28{
29}
30
31// Returns true on successful filter state update (valid enum parameter)
32bool Texture::setMinFilter(GLenum filter)
33{
34 switch (filter)
35 {
36 case GL_NEAREST:
37 case GL_LINEAR:
38 case GL_NEAREST_MIPMAP_NEAREST:
39 case GL_LINEAR_MIPMAP_NEAREST:
40 case GL_NEAREST_MIPMAP_LINEAR:
41 case GL_LINEAR_MIPMAP_LINEAR:
42 mMinFilter = filter;
43 return true;
44 default:
45 return false;
46 }
47}
48
49// Returns true on successful filter state update (valid enum parameter)
50bool Texture::setMagFilter(GLenum filter)
51{
52 switch (filter)
53 {
54 case GL_NEAREST:
55 case GL_LINEAR:
56 mMagFilter = filter;
57 return true;
58 default:
59 return false;
60 }
61}
62
63// Returns true on successful wrap state update (valid enum parameter)
64bool Texture::setWrapS(GLenum wrap)
65{
66 switch (wrap)
67 {
68 case GL_REPEAT:
69 case GL_CLAMP_TO_EDGE:
70 case GL_MIRRORED_REPEAT:
71 mWrapS = wrap;
72 return true;
73 default:
74 return false;
75 }
76}
77
78// Returns true on successful wrap state update (valid enum parameter)
79bool Texture::setWrapT(GLenum wrap)
80{
81 switch (wrap)
82 {
83 case GL_REPEAT:
84 case GL_CLAMP_TO_EDGE:
85 case GL_MIRRORED_REPEAT:
86 mWrapT = wrap;
87 return true;
88 default:
89 return false;
90 }
91}
92
93GLenum Texture::getMinFilter()
94{
95 return mMinFilter;
96}
97
98GLenum Texture::getMagFilter()
99{
100 return mMagFilter;
101}
102
103GLenum Texture::getWrapS()
104{
105 return mWrapS;
106}
107
108GLenum Texture::getWrapT()
109{
110 return mWrapT;
111}
112
113// Copies an Image into an already locked Direct3D 9 surface, performing format conversions as necessary
114void Texture::copyImage(D3DLOCKED_RECT &lock, D3DFORMAT format, Image &image)
115{
116 if (lock.pBits && image.pixels)
117 {
118 for (int y = 0; y < image.height; y++)
119 {
120 unsigned char *source = (unsigned char*)image.pixels + y * image.width * pixelSize(image);
121 unsigned short *source16 = (unsigned short*)source;
122 unsigned char *dest = (unsigned char*)lock.pBits + y * lock.Pitch;
123
124 for (int x = 0; x < image.width; x++)
125 {
126 unsigned char r;
127 unsigned char g;
128 unsigned char b;
129 unsigned char a;
130
131 switch (image.format)
132 {
133 case GL_ALPHA:
134 UNIMPLEMENTED();
135 break;
136 case GL_LUMINANCE:
137 UNIMPLEMENTED();
138 break;
139 case GL_LUMINANCE_ALPHA:
140 UNIMPLEMENTED();
141 break;
142 case GL_RGB:
143 switch (image.type)
144 {
145 case GL_UNSIGNED_BYTE: UNIMPLEMENTED(); break;
146 case GL_UNSIGNED_SHORT_5_6_5:
147 {
148 unsigned short rgba = source16[x];
149
150 a = 0xFF;
151 b = ((rgba & 0x001F) << 3) | ((rgba & 0x001F) >> 2);
152 g = ((rgba & 0x07E0) >> 3) | ((rgba & 0x07E0) >> 9);
153 r = ((rgba & 0xF800) >> 8) | ((rgba & 0xF800) >> 13);
154 }
155 break;
156 default: UNREACHABLE();
157 }
158 break;
159 case GL_RGBA:
160 switch (image.type)
161 {
162 case GL_UNSIGNED_BYTE:
163 r = source[x * 4 + 0];
164 g = source[x * 4 + 1];
165 b = source[x * 4 + 2];
166 a = source[x * 4 + 3];
167 break;
168 case GL_UNSIGNED_SHORT_4_4_4_4:
169 {
170 unsigned short rgba = source16[x];
171
172 a = ((rgba & 0x000F) << 4) | ((rgba & 0x000F) >> 0);
173 b = ((rgba & 0x00F0) << 0) | ((rgba & 0x00F0) >> 4);
174 g = ((rgba & 0x0F00) >> 4) | ((rgba & 0x0F00) >> 8);
175 r = ((rgba & 0xF000) >> 8) | ((rgba & 0xF000) >> 12);
176 }
177 break;
178 case GL_UNSIGNED_SHORT_5_5_5_1: UNIMPLEMENTED(); break;
179 default: UNREACHABLE();
180 }
181 break;
182 default: UNREACHABLE();
183 }
184
185 switch (format)
186 {
187 case D3DFMT_A8R8G8B8:
188 dest[4 * x + 0] = b;
189 dest[4 * x + 1] = g;
190 dest[4 * x + 2] = r;
191 dest[4 * x + 3] = a;
192 break;
193 default: UNREACHABLE();
194 }
195 }
196 }
197 }
198}
199
200// Selects an internal Direct3D 9 format for storing an Image
201D3DFORMAT Texture::selectFormat(const Image &image)
202{
203 switch (image.format)
204 {
205 case GL_ALPHA:
206 UNIMPLEMENTED();
207 break;
208 case GL_LUMINANCE:
209 UNIMPLEMENTED();
210 break;
211 case GL_LUMINANCE_ALPHA:
212 UNIMPLEMENTED();
213 break;
214 case GL_RGB:
215 switch (image.type)
216 {
217 case GL_UNSIGNED_BYTE: UNIMPLEMENTED();
218 case GL_UNSIGNED_SHORT_5_6_5: return D3DFMT_A8R8G8B8;
219 default: UNREACHABLE();
220 }
221 break;
222 case GL_RGBA:
223 switch (image.type)
224 {
225 case GL_UNSIGNED_BYTE: return D3DFMT_A8R8G8B8;
226 case GL_UNSIGNED_SHORT_4_4_4_4: return D3DFMT_A8R8G8B8;
227 case GL_UNSIGNED_SHORT_5_5_5_1: UNIMPLEMENTED();
228 default: UNREACHABLE();
229 }
230 break;
231 default: UNREACHABLE();
232 }
233
234 return D3DFMT_UNKNOWN;
235}
236
237// Returns the size, in bytes, of a single texel in an Image
238int Texture::pixelSize(const Image &image)
239{
240 switch (image.type)
241 {
242 case GL_UNSIGNED_BYTE:
243 switch (image.format)
244 {
245 case GL_ALPHA: return sizeof(unsigned char);
246 case GL_LUMINANCE: return sizeof(unsigned char);
247 case GL_LUMINANCE_ALPHA: return sizeof(unsigned char) * 2;
248 case GL_RGB: return sizeof(unsigned char) * 3;
249 case GL_RGBA: return sizeof(unsigned char) * 4;
250 default: UNREACHABLE();
251 }
252 break;
253 case GL_UNSIGNED_SHORT_4_4_4_4:
254 case GL_UNSIGNED_SHORT_5_5_5_1:
255 case GL_UNSIGNED_SHORT_5_6_5:
256 return sizeof(unsigned short);
257 default: UNREACHABLE();
258 }
259
260 return 0;
261}
262
263Texture2D::Texture2D()
264{
265 for (int level = 0; level < MAX_TEXTURE_LEVELS; level++)
266 {
267 mImageArray[level].pixels = NULL;
268 }
269
270 mTexture = NULL;
271}
272
273Texture2D::~Texture2D()
274{
275 for (int level = 0; level < MAX_TEXTURE_LEVELS; level++)
276 {
277 delete[] mImageArray[level].pixels;
278 }
279
280 if (mTexture)
281 {
282 mTexture->Release();
283 mTexture = NULL;
284 }
285}
286
287GLenum Texture2D::getTarget() const
288{
289 return GL_TEXTURE_2D;
290}
291
292void Texture2D::setImage(GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels)
293{
294 if (level < 0 || level >= MAX_TEXTURE_LEVELS)
295 {
296 return;
297 }
298
299 mImageArray[level].internalFormat = internalFormat;
300 mImageArray[level].width = width;
301 mImageArray[level].height = height;
302 mImageArray[level].format = format;
303 mImageArray[level].type = type;
304
305 delete[] mImageArray[level].pixels;
306 mImageArray[level].pixels = NULL;
307
308 int imageSize = pixelSize(mImageArray[level]) * width * height;
309 mImageArray[level].pixels = new unsigned char[imageSize];
310
311 if(pixels)
312 {
313 memcpy(mImageArray[level].pixels, pixels, imageSize);
314 }
315
316 if (level == 0)
317 {
318 mWidth = width;
319 mHeight = height;
320 }
321}
322
323// Tests for GL texture object completeness. [OpenGL ES 2.0.24] section 3.7.10 page 81.
324bool Texture2D::isComplete()
325{
326 if (mWidth <= 0 || mHeight <= 0)
327 {
328 return false;
329 }
330
331 bool mipmapping;
332
333 switch (mMagFilter)
334 {
335 case GL_NEAREST:
336 case GL_LINEAR:
337 mipmapping = false;
338 break;
339 case GL_NEAREST_MIPMAP_NEAREST:
340 case GL_LINEAR_MIPMAP_NEAREST:
341 case GL_NEAREST_MIPMAP_LINEAR:
342 case GL_LINEAR_MIPMAP_LINEAR:
343 mipmapping = true;
344 break;
345 default: UNREACHABLE();
346 }
347
348 if (mipmapping)
349 {
350 int q = log2(max(mWidth, mHeight));
351
352 for (int level = 1; level <= q; level++)
353 {
354 if (mImageArray[level].format != mImageArray[0].format)
355 {
356 return false;
357 }
358
359 if (mImageArray[level].internalFormat != mImageArray[0].internalFormat)
360 {
361 return false;
362 }
363
364 if (mImageArray[level].type != mImageArray[0].type)
365 {
366 return false;
367 }
368
369 if (mImageArray[level].width != (mImageArray[level - 1].width + 1) / 2)
370 {
371 return false;
372 }
373
374 if (mImageArray[level].height != (mImageArray[level - 1].height + 1) / 2)
375 {
376 return false;
377 }
378 }
379 }
380
381 return true;
382}
383
384// Constructs a Direct3D 9 texture resource from the texture images, or returns an existing one
385IDirect3DBaseTexture9 *Texture2D::getTexture()
386{
387 if (!isComplete())
388 {
389 return NULL;
390 }
391
392 if (!mTexture) // FIXME: Recreate when changed (same for getRenderTarget)
393 {
394 IDirect3DDevice9 *device = getDevice();
395 D3DFORMAT format = selectFormat(mImageArray[0]);
396
397 HRESULT result = device->CreateTexture(mWidth, mHeight, 0, D3DUSAGE_RENDERTARGET, format, D3DPOOL_DEFAULT, &mTexture, NULL);
398
399 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
400 {
401 error(GL_OUT_OF_MEMORY, 0);
402 }
403
404 ASSERT(SUCCEEDED(result));
405
406 IDirect3DTexture9 *lockableTexture;
407 result = device->CreateTexture(mWidth, mHeight, 0, D3DUSAGE_DYNAMIC, format, D3DPOOL_SYSTEMMEM, &lockableTexture, NULL);
408
409 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
410 {
411 error(GL_OUT_OF_MEMORY,(IDirect3DBaseTexture9*)NULL);
412 }
413
414 ASSERT(SUCCEEDED(result));
415
416 if (mTexture) // FIXME: Handle failure
417 {
418 int levelCount = mTexture->GetLevelCount();
419
420 for (int level = 0; level < levelCount; level++)
421 {
422 D3DLOCKED_RECT lock = {0};
423 lockableTexture->LockRect(level, &lock, NULL, 0);
424
425 copyImage(lock, format, mImageArray[level]);
426
427 lockableTexture->UnlockRect(level);
428 }
429
430 device->UpdateTexture(lockableTexture, mTexture);
431 lockableTexture->Release();
432 }
433 }
434
435 return mTexture;
436}
437
438// Returns the top-level texture surface as a render target
439IDirect3DSurface9 *Texture2D::getRenderTarget()
440{
441 if (!mRenderTarget && getTexture())
442 {
443 mTexture->GetSurfaceLevel(0, &mRenderTarget);
444 }
445
446 return mRenderTarget;
447}
448
449TextureCubeMap::TextureCubeMap()
450{
451 for (int face = 0; face < 6; face++)
452 {
453 for (int level = 0; level < MAX_TEXTURE_LEVELS; level++)
454 {
455 mImageArray[face][level].pixels = NULL;
456 }
457 }
458
459 mTexture = NULL;
460}
461
462TextureCubeMap::~TextureCubeMap()
463{
464 for (int face = 0; face < 6; face++)
465 {
466 for (int level = 0; level < MAX_TEXTURE_LEVELS; level++)
467 {
468 delete[] mImageArray[face][level].pixels;
469 }
470 }
471
472 if (mTexture)
473 {
474 mTexture->Release();
475 mTexture = NULL;
476 }
477}
478
479GLenum TextureCubeMap::getTarget() const
480{
481 return GL_TEXTURE_CUBE_MAP;
482}
483
484void TextureCubeMap::setImagePosX(GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels)
485{
486 setImage(0, level, internalFormat, width, height, format, type, pixels);
487}
488
489void TextureCubeMap::setImageNegX(GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels)
490{
491 setImage(1, level, internalFormat, width, height, format, type, pixels);
492}
493
494void TextureCubeMap::setImagePosY(GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels)
495{
496 setImage(2, level, internalFormat, width, height, format, type, pixels);
497}
498
499void TextureCubeMap::setImageNegY(GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels)
500{
501 setImage(3, level, internalFormat, width, height, format, type, pixels);
502}
503
504void TextureCubeMap::setImagePosZ(GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels)
505{
506 setImage(4, level, internalFormat, width, height, format, type, pixels);
507}
508
509void TextureCubeMap::setImageNegZ(GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels)
510{
511 setImage(5, level, internalFormat, width, height, format, type, pixels);
512}
513
514// Tests for GL texture object completeness. [OpenGL ES 2.0.24] section 3.7.10 page 81.
515bool TextureCubeMap::isComplete()
516{
517 if (mWidth <= 0 || mHeight <= 0 || mWidth != mHeight)
518 {
519 return false;
520 }
521
522 bool mipmapping;
523
524 switch (mMagFilter)
525 {
526 case GL_NEAREST:
527 case GL_LINEAR:
528 mipmapping = false;
529 break;
530 case GL_NEAREST_MIPMAP_NEAREST:
531 case GL_LINEAR_MIPMAP_NEAREST:
532 case GL_NEAREST_MIPMAP_LINEAR:
533 case GL_LINEAR_MIPMAP_LINEAR:
534 mipmapping = true;
535 break;
536 default: UNREACHABLE();
537 }
538
539 for (int face = 0; face < 6; face++)
540 {
541 if (mImageArray[face][0].width != mWidth || mImageArray[face][0].height != mHeight)
542 {
543 return false;
544 }
545 }
546
547 if (mipmapping)
548 {
549 int q = log2(mWidth);
550
551 for (int face = 0; face < 6; face++)
552 {
553 for (int level = 1; level <= q; level++)
554 {
555 if (mImageArray[face][level].format != mImageArray[0][0].format)
556 {
557 return false;
558 }
559
560 if (mImageArray[face][level].internalFormat != mImageArray[0][0].internalFormat)
561 {
562 return false;
563 }
564
565 if (mImageArray[face][level].type != mImageArray[0][0].type)
566 {
567 return false;
568 }
569
570 if (mImageArray[face][level].width != (mImageArray[0][level - 1].width + 1) / 2)
571 {
572 return false;
573 }
574
575 if (mImageArray[face][level].height != (mImageArray[0][level - 1].height + 1) / 2)
576 {
577 return false;
578 }
579 }
580 }
581 }
582
583 return true;
584}
585
586// Constructs a Direct3D 9 texture resource from the texture images, or returns an existing one
587IDirect3DBaseTexture9 *TextureCubeMap::getTexture()
588{
589 if (!isComplete())
590 {
591 return NULL;
592 }
593
594 if (!mTexture) // FIXME: Recreate when changed (same for getRenderTarget)
595 {
596 IDirect3DDevice9 *device = getDevice();
597 D3DFORMAT format = selectFormat(mImageArray[0][0]);
598
599 HRESULT result = device->CreateCubeTexture(mWidth, 0, D3DUSAGE_RENDERTARGET, format, D3DPOOL_DEFAULT, &mTexture, NULL);
600
601 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
602 {
603 error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL);
604 }
605
606 ASSERT(SUCCEEDED(result));
607
608 IDirect3DCubeTexture9 *lockableTexture;
609 result = device->CreateCubeTexture(mWidth, 0, D3DUSAGE_DYNAMIC, format, D3DPOOL_SYSTEMMEM, &lockableTexture, NULL);
610
611 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
612 {
613 return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL);
614 }
615
616 ASSERT(SUCCEEDED(result));
617
618 if (mTexture)
619 {
620 for (int face = 0; face < 6; face++)
621 {
622 for (int level = 0; level < MAX_TEXTURE_LEVELS; level++)
623 {
624 D3DLOCKED_RECT lock = {0};
625 lockableTexture->LockRect((D3DCUBEMAP_FACES)face, level, &lock, NULL, 0);
626
627 copyImage(lock, format, mImageArray[face][level]);
628
629 lockableTexture->UnlockRect((D3DCUBEMAP_FACES)face, level);
630 }
631 }
632
633 device->UpdateTexture(lockableTexture, mTexture);
634 lockableTexture->Release();
635 }
636 }
637
638 return mTexture;
639}
640
641void TextureCubeMap::setImage(int face, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels)
642{
643 if (level < 0 || level >= MAX_TEXTURE_LEVELS)
644 {
645 return;
646 }
647
648 mImageArray[face][level].internalFormat = internalFormat;
649 mImageArray[face][level].width = width;
650 mImageArray[face][level].height = height;
651 mImageArray[face][level].format = format;
652 mImageArray[face][level].type = type;
653
654 delete[] mImageArray[face][level].pixels;
655 mImageArray[face][level].pixels = NULL;
656
657 int imageSize = pixelSize(mImageArray[face][level]) * width * height;
658 mImageArray[face][level].pixels = new unsigned char[imageSize];
659
660 if (pixels)
661 {
662 memcpy(mImageArray[face][level].pixels, pixels, imageSize);
663 }
664
665 if (face == 0 && level == 0)
666 {
667 mWidth = width;
668 mHeight = height;
669 }
670}
671}