blob: 305620c3948cfcabb71027415ef50f7561cc4d9d [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
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000011#include "libGLESv2/Texture.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000012
daniel@transgaming.com16973022010-03-11 19:22:19 +000013#include <algorithm>
14
alokp@chromium.orgea0e1af2010-03-22 19:33:14 +000015#include "common/debug.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000016
17#include "libGLESv2/main.h"
18#include "libGLESv2/mathutil.h"
19#include "libGLESv2/utilities.h"
20#include "libGLESv2/Blit.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000021
22namespace gl
23{
daniel@transgaming.com842f7a42010-03-21 04:31:03 +000024
25Texture::Image::Image()
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +000026 : width(0), height(0), dirty(false), surface(NULL)
daniel@transgaming.com842f7a42010-03-21 04:31:03 +000027{
28}
29
30Texture::Image::~Image()
31{
32 if (surface) surface->Release();
33}
34
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +000035Texture::Texture()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000036{
37 mMinFilter = GL_NEAREST_MIPMAP_LINEAR;
38 mMagFilter = GL_LINEAR;
39 mWrapS = GL_REPEAT;
40 mWrapT = GL_REPEAT;
daniel@transgaming.com29d27002010-03-11 19:41:22 +000041
daniel@transgaming.com00c75962010-03-11 20:36:15 +000042 mDirtyMetaData = true;
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +000043 mDirty = true;
daniel@transgaming.com93a81472010-04-20 18:52:58 +000044 mIsRenderable = false;
daniel@transgaming.com0a311a42010-05-17 09:58:33 +000045 mBaseTexture = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000046}
47
48Texture::~Texture()
49{
50}
51
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +000052Blit *Texture::getBlitter()
53{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +000054 Context *context = getContext();
55 return context->getBlitter();
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +000056}
57
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000058// Returns true on successful filter state update (valid enum parameter)
59bool Texture::setMinFilter(GLenum filter)
60{
61 switch (filter)
62 {
63 case GL_NEAREST:
64 case GL_LINEAR:
65 case GL_NEAREST_MIPMAP_NEAREST:
66 case GL_LINEAR_MIPMAP_NEAREST:
67 case GL_NEAREST_MIPMAP_LINEAR:
68 case GL_LINEAR_MIPMAP_LINEAR:
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +000069 {
70 if (mMinFilter != filter)
71 {
72 mMinFilter = filter;
73 mDirty = true;
74 }
75 return true;
76 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000077 default:
78 return false;
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +000079 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000080}
81
82// Returns true on successful filter state update (valid enum parameter)
83bool Texture::setMagFilter(GLenum filter)
84{
85 switch (filter)
86 {
87 case GL_NEAREST:
88 case GL_LINEAR:
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +000089 {
90 if (mMagFilter != filter)
91 {
92 mMagFilter = filter;
93 mDirty = true;
94 }
95 return true;
96 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000097 default:
98 return false;
99 }
100}
101
102// Returns true on successful wrap state update (valid enum parameter)
103bool Texture::setWrapS(GLenum wrap)
104{
105 switch (wrap)
106 {
107 case GL_REPEAT:
108 case GL_CLAMP_TO_EDGE:
109 case GL_MIRRORED_REPEAT:
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +0000110 {
111 if (mWrapS != wrap)
112 {
113 mWrapS = wrap;
114 mDirty = true;
115 }
116 return true;
117 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000118 default:
119 return false;
120 }
121}
122
123// Returns true on successful wrap state update (valid enum parameter)
124bool Texture::setWrapT(GLenum wrap)
125{
126 switch (wrap)
127 {
128 case GL_REPEAT:
129 case GL_CLAMP_TO_EDGE:
130 case GL_MIRRORED_REPEAT:
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +0000131 {
132 if (mWrapT != wrap)
133 {
134 mWrapT = wrap;
135 mDirty = true;
136 }
137 return true;
138 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000139 default:
140 return false;
141 }
142}
143
daniel@transgaming.com00c75962010-03-11 20:36:15 +0000144GLenum Texture::getMinFilter() const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000145{
146 return mMinFilter;
147}
148
daniel@transgaming.com00c75962010-03-11 20:36:15 +0000149GLenum Texture::getMagFilter() const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000150{
151 return mMagFilter;
152}
153
daniel@transgaming.com00c75962010-03-11 20:36:15 +0000154GLenum Texture::getWrapS() const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000155{
156 return mWrapS;
157}
158
daniel@transgaming.com00c75962010-03-11 20:36:15 +0000159GLenum Texture::getWrapT() const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000160{
161 return mWrapT;
162}
163
daniel@transgaming.com93a81472010-04-20 18:52:58 +0000164GLuint Texture::getWidth() const
165{
166 return mWidth;
167}
168
169GLuint Texture::getHeight() const
170{
171 return mHeight;
172}
173
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000174// Selects an internal Direct3D 9 format for storing an Image
daniel@transgaming.com842f7a42010-03-21 04:31:03 +0000175D3DFORMAT Texture::selectFormat(GLenum format)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000176{
daniel@transgaming.com00c75962010-03-11 20:36:15 +0000177 return D3DFMT_A8R8G8B8;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000178}
179
daniel@transgaming.com00c75962010-03-11 20:36:15 +0000180int Texture::imagePitch(const Image &img) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000181{
daniel@transgaming.com00c75962010-03-11 20:36:15 +0000182 return img.width * 4;
183}
daniel@transgaming.com29d27002010-03-11 19:41:22 +0000184
daniel@transgaming.com00c75962010-03-11 20:36:15 +0000185// Store the pixel rectangle designated by xoffset,yoffset,width,height with pixels stored as format/type at input
186// into the BGRA8 pixel rectangle at output with outputPitch bytes in between each line.
187void Texture::loadImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type,
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000188 GLint unpackAlignment, const void *input, size_t outputPitch, void *output) const
daniel@transgaming.com00c75962010-03-11 20:36:15 +0000189{
daniel@transgaming.com713914b2010-05-04 03:35:17 +0000190 GLsizei inputPitch = ComputePitch(width, format, type, unpackAlignment);
daniel@transgaming.com00c75962010-03-11 20:36:15 +0000191
192 for (int y = 0; y < height; y++)
193 {
194 const unsigned char *source = static_cast<const unsigned char*>(input) + y * inputPitch;
195 const unsigned short *source16 = reinterpret_cast<const unsigned short*>(source);
196 unsigned char *dest = static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 4;
197
198 for (int x = 0; x < width; x++)
199 {
200 unsigned char r;
201 unsigned char g;
202 unsigned char b;
203 unsigned char a;
204
205 switch (format)
206 {
207 case GL_ALPHA:
208 a = source[x];
209 r = 0;
210 g = 0;
211 b = 0;
212 break;
213
214 case GL_LUMINANCE:
215 r = source[x];
216 g = source[x];
217 b = source[x];
218 a = 0xFF;
219 break;
220
221 case GL_LUMINANCE_ALPHA:
222 r = source[2*x+0];
223 g = source[2*x+0];
224 b = source[2*x+0];
225 a = source[2*x+1];
226 break;
227
228 case GL_RGB:
229 switch (type)
230 {
231 case GL_UNSIGNED_BYTE:
232 r = source[x * 3 + 0];
daniel@transgaming.com5ac52152010-04-13 19:53:38 +0000233 g = source[x * 3 + 1];
234 b = source[x * 3 + 2];
daniel@transgaming.com00c75962010-03-11 20:36:15 +0000235 a = 0xFF;
236 break;
237
238 case GL_UNSIGNED_SHORT_5_6_5:
239 {
240 unsigned short rgba = source16[x];
241
242 a = 0xFF;
243 b = ((rgba & 0x001F) << 3) | ((rgba & 0x001F) >> 2);
244 g = ((rgba & 0x07E0) >> 3) | ((rgba & 0x07E0) >> 9);
245 r = ((rgba & 0xF800) >> 8) | ((rgba & 0xF800) >> 13);
246 }
247 break;
248
249 default: UNREACHABLE();
250 }
251 break;
252
253 case GL_RGBA:
254 switch (type)
255 {
256 case GL_UNSIGNED_BYTE:
257 r = source[x * 4 + 0];
258 g = source[x * 4 + 1];
259 b = source[x * 4 + 2];
260 a = source[x * 4 + 3];
261 break;
262
263 case GL_UNSIGNED_SHORT_4_4_4_4:
264 {
265 unsigned short rgba = source16[x];
266
267 a = ((rgba & 0x000F) << 4) | ((rgba & 0x000F) >> 0);
268 b = ((rgba & 0x00F0) << 0) | ((rgba & 0x00F0) >> 4);
269 g = ((rgba & 0x0F00) >> 4) | ((rgba & 0x0F00) >> 8);
270 r = ((rgba & 0xF000) >> 8) | ((rgba & 0xF000) >> 12);
271 }
272 break;
273
274 case GL_UNSIGNED_SHORT_5_5_5_1:
275 {
276 unsigned short rgba = source16[x];
277
278 a = (rgba & 0x0001) ? 0xFF : 0;
daniel@transgaming.com842f7a42010-03-21 04:31:03 +0000279 b = ((rgba & 0x003E) << 2) | ((rgba & 0x003E) >> 3);
daniel@transgaming.com00c75962010-03-11 20:36:15 +0000280 g = ((rgba & 0x07C0) >> 3) | ((rgba & 0x07C0) >> 8);
daniel@transgaming.com842f7a42010-03-21 04:31:03 +0000281 r = ((rgba & 0xF800) >> 8) | ((rgba & 0xF800) >> 13);
daniel@transgaming.com00c75962010-03-11 20:36:15 +0000282 }
283 break;
284
285 default: UNREACHABLE();
286 }
287 break;
288 default: UNREACHABLE();
289 }
290
291 dest[4 * x + 0] = b;
292 dest[4 * x + 1] = g;
293 dest[4 * x + 2] = r;
294 dest[4 * x + 3] = a;
295 }
296 }
297}
298
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000299void Texture::setImage(GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels, Image *img)
daniel@transgaming.com00c75962010-03-11 20:36:15 +0000300{
daniel@transgaming.com842f7a42010-03-21 04:31:03 +0000301 IDirect3DSurface9 *newSurface = NULL;
daniel@transgaming.com842f7a42010-03-21 04:31:03 +0000302
daniel@transgaming.com34dc3e82010-04-15 20:45:02 +0000303 if (width != 0 && height != 0)
daniel@transgaming.com842f7a42010-03-21 04:31:03 +0000304 {
daniel@transgaming.com34dc3e82010-04-15 20:45:02 +0000305 HRESULT result = getDevice()->CreateOffscreenPlainSurface(width, height, selectFormat(format), D3DPOOL_SYSTEMMEM, &newSurface, NULL);
306
307 if (FAILED(result))
308 {
309 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
310 return error(GL_OUT_OF_MEMORY);
311 }
daniel@transgaming.com842f7a42010-03-21 04:31:03 +0000312 }
313
314 if (img->surface) img->surface->Release();
315 img->surface = newSurface;
316
daniel@transgaming.com29d27002010-03-11 19:41:22 +0000317 img->width = width;
318 img->height = height;
319 img->format = format;
daniel@transgaming.com29d27002010-03-11 19:41:22 +0000320
daniel@transgaming.com34dc3e82010-04-15 20:45:02 +0000321 if (pixels != NULL && newSurface != NULL)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000322 {
daniel@transgaming.com842f7a42010-03-21 04:31:03 +0000323 D3DLOCKED_RECT locked;
324 HRESULT result = newSurface->LockRect(&locked, NULL, 0);
325
326 ASSERT(SUCCEEDED(result));
327
328 if (SUCCEEDED(result))
329 {
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000330 loadImageData(0, 0, width, height, format, type, unpackAlignment, pixels, locked.Pitch, locked.pBits);
daniel@transgaming.com842f7a42010-03-21 04:31:03 +0000331 newSurface->UnlockRect();
332 }
333
334 img->dirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000335 }
336
daniel@transgaming.com00c75962010-03-11 20:36:15 +0000337 mDirtyMetaData = true;
338}
339
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000340void Texture::subImage(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels, Image *img)
daniel@transgaming.com00c75962010-03-11 20:36:15 +0000341{
342 if (width + xoffset > img->width || height + yoffset > img->height) return error(GL_INVALID_VALUE);
343
daniel@transgaming.com842f7a42010-03-21 04:31:03 +0000344 D3DLOCKED_RECT locked;
345 HRESULT result = img->surface->LockRect(&locked, NULL, 0);
daniel@transgaming.com00c75962010-03-11 20:36:15 +0000346
daniel@transgaming.com842f7a42010-03-21 04:31:03 +0000347 ASSERT(SUCCEEDED(result));
348
349 if (SUCCEEDED(result))
350 {
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000351 loadImageData(xoffset, yoffset, width, height, format, type, unpackAlignment, pixels, locked.Pitch, locked.pBits);
daniel@transgaming.com842f7a42010-03-21 04:31:03 +0000352 img->surface->UnlockRect();
353 }
354
355 img->dirty = true;
daniel@transgaming.com29d27002010-03-11 19:41:22 +0000356}
357
358IDirect3DBaseTexture9 *Texture::getTexture()
359{
360 if (!isComplete())
361 {
362 return NULL;
363 }
364
daniel@transgaming.com00c75962010-03-11 20:36:15 +0000365 if (mDirtyMetaData)
366 {
daniel@transgaming.com00c75962010-03-11 20:36:15 +0000367 mBaseTexture = createTexture();
daniel@transgaming.com93a81472010-04-20 18:52:58 +0000368 mIsRenderable = false;
daniel@transgaming.com00c75962010-03-11 20:36:15 +0000369 }
370
daniel@transgaming.com842f7a42010-03-21 04:31:03 +0000371 if (mDirtyMetaData || dirtyImageData())
daniel@transgaming.com29d27002010-03-11 19:41:22 +0000372 {
daniel@transgaming.com00c75962010-03-11 20:36:15 +0000373 updateTexture();
daniel@transgaming.com29d27002010-03-11 19:41:22 +0000374 }
375
daniel@transgaming.com00c75962010-03-11 20:36:15 +0000376 mDirtyMetaData = false;
daniel@transgaming.com842f7a42010-03-21 04:31:03 +0000377 ASSERT(!dirtyImageData());
daniel@transgaming.com00c75962010-03-11 20:36:15 +0000378
daniel@transgaming.com29d27002010-03-11 19:41:22 +0000379 return mBaseTexture;
380}
381
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +0000382bool Texture::isDirty() const
383{
384 return (mDirty || mDirtyMetaData || dirtyImageData());
385}
386
daniel@transgaming.com7051b972010-03-21 04:31:07 +0000387// Returns the top-level texture surface as a render target
daniel@transgaming.com93a81472010-04-20 18:52:58 +0000388void Texture::needRenderTarget()
daniel@transgaming.com7051b972010-03-21 04:31:07 +0000389{
daniel@transgaming.com93a81472010-04-20 18:52:58 +0000390 if (!mIsRenderable)
daniel@transgaming.com7051b972010-03-21 04:31:07 +0000391 {
392 mBaseTexture = convertToRenderTarget();
daniel@transgaming.com93a81472010-04-20 18:52:58 +0000393 mIsRenderable = true;
daniel@transgaming.com7051b972010-03-21 04:31:07 +0000394 }
395
396 if (dirtyImageData())
397 {
398 updateTexture();
399 }
400
401 mDirtyMetaData = false;
daniel@transgaming.com7051b972010-03-21 04:31:07 +0000402}
403
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000404void Texture::dropTexture()
405{
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000406 if (mBaseTexture)
407 {
408 mBaseTexture = NULL;
409 }
daniel@transgaming.com93a81472010-04-20 18:52:58 +0000410
411 mIsRenderable = false;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000412}
413
daniel@transgaming.com93a81472010-04-20 18:52:58 +0000414void Texture::pushTexture(IDirect3DBaseTexture9 *newTexture, bool renderable)
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000415{
416 mBaseTexture = newTexture;
417 mDirtyMetaData = false;
daniel@transgaming.com93a81472010-04-20 18:52:58 +0000418 mIsRenderable = renderable;
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +0000419 mDirty = true;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000420}
421
422
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000423GLint Texture::creationLevels(GLsizei width, GLsizei height, GLint maxlevel) const
424{
425 if (isPow2(width) && isPow2(height))
426 {
427 return maxlevel;
428 }
429 else
430 {
daniel@transgaming.com4c03fa62010-06-24 13:02:16 +0000431 // OpenGL ES 2.0 without GL_OES_texture_npot does not permit NPOT mipmaps.
432 return 1;
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000433 }
434}
435
436GLint Texture::creationLevels(GLsizei size, GLint maxlevel) const
437{
438 return creationLevels(size, size, maxlevel);
439}
440
441int Texture::levelCount() const
442{
443 return mBaseTexture ? mBaseTexture->GetLevelCount() : 0;
444}
445
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000446Texture2D::Texture2D()
daniel@transgaming.com29d27002010-03-11 19:41:22 +0000447{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000448 mTexture = NULL;
daniel@transgaming.com93a81472010-04-20 18:52:58 +0000449 mColorbufferProxy = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000450}
451
452Texture2D::~Texture2D()
453{
daniel@transgaming.com93a81472010-04-20 18:52:58 +0000454 delete mColorbufferProxy;
455
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000456 if (mTexture)
457 {
458 mTexture->Release();
459 mTexture = NULL;
460 }
461}
462
463GLenum Texture2D::getTarget() const
464{
465 return GL_TEXTURE_2D;
466}
467
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000468// While OpenGL doesn't check texture consistency until draw-time, D3D9 requires a complete texture
469// for render-to-texture (such as CopyTexImage). We have no way of keeping individual inconsistent levels.
470// Call this when a particular level of the texture must be defined with a specific format, width and height.
471//
472// Returns true if the existing texture was unsuitable had to be destroyed. If so, it will also set
473// a new height and width for the texture by working backwards from the given width and height.
474bool Texture2D::redefineTexture(GLint level, GLenum internalFormat, GLsizei width, GLsizei height)
475{
476 bool widthOkay = (mWidth >> level == width);
477 bool heightOkay = (mHeight >> level == height);
478
479 bool sizeOkay = ((widthOkay && heightOkay)
480 || (widthOkay && mHeight >> level == 0 && height == 1)
481 || (heightOkay && mWidth >> level == 0 && width == 1));
482
483 bool textureOkay = (sizeOkay && internalFormat == mImageArray[0].format);
484
485 if (!textureOkay)
486 {
487 TRACE("Redefining 2D texture (%d, 0x%04X, %d, %d => 0x%04X, %d, %d).", level,
488 mImageArray[0].format, mWidth, mHeight,
489 internalFormat, width, height);
490
491 // Purge all the levels and the texture.
492
493 for (int i = 0; i < MAX_TEXTURE_LEVELS; i++)
494 {
495 if (mImageArray[i].surface != NULL)
496 {
497 mImageArray[i].dirty = false;
498
499 mImageArray[i].surface->Release();
500 mImageArray[i].surface = NULL;
501 }
502 }
503
504 if (mTexture != NULL)
505 {
506 mTexture->Release();
507 mTexture = NULL;
508 dropTexture();
509 }
510
511 mWidth = width << level;
512 mHeight = height << level;
513 mImageArray[0].format = internalFormat;
514 }
515
516 return !textureOkay;
517}
518
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000519void Texture2D::setImage(GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000520{
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000521 redefineTexture(level, internalFormat, width, height);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000522
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000523 Texture::setImage(width, height, format, type, unpackAlignment, pixels, &mImageArray[level]);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000524}
525
daniel@transgaming.com842f7a42010-03-21 04:31:03 +0000526void Texture2D::commitRect(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height)
527{
528 ASSERT(mImageArray[level].surface != NULL);
529
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000530 if (level < levelCount())
daniel@transgaming.com842f7a42010-03-21 04:31:03 +0000531 {
532 IDirect3DSurface9 *destLevel = NULL;
daniel@transgaming.com7051b972010-03-21 04:31:07 +0000533 HRESULT result = mTexture->GetSurfaceLevel(level, &destLevel);
daniel@transgaming.com842f7a42010-03-21 04:31:03 +0000534
daniel@transgaming.com7051b972010-03-21 04:31:07 +0000535 ASSERT(SUCCEEDED(result));
daniel@transgaming.com842f7a42010-03-21 04:31:03 +0000536
daniel@transgaming.com7051b972010-03-21 04:31:07 +0000537 if (SUCCEEDED(result))
538 {
539 Image *img = &mImageArray[level];
daniel@transgaming.com842f7a42010-03-21 04:31:03 +0000540
daniel@transgaming.com7051b972010-03-21 04:31:07 +0000541 RECT sourceRect;
542 sourceRect.left = xoffset;
543 sourceRect.top = yoffset;
544 sourceRect.right = xoffset + width;
545 sourceRect.bottom = yoffset + height;
daniel@transgaming.com842f7a42010-03-21 04:31:03 +0000546
daniel@transgaming.com7051b972010-03-21 04:31:07 +0000547 POINT destPoint;
548 destPoint.x = xoffset;
549 destPoint.y = yoffset;
daniel@transgaming.com842f7a42010-03-21 04:31:03 +0000550
daniel@transgaming.com7051b972010-03-21 04:31:07 +0000551 result = getDevice()->UpdateSurface(img->surface, &sourceRect, destLevel, &destPoint);
552 ASSERT(SUCCEEDED(result));
daniel@transgaming.com842f7a42010-03-21 04:31:03 +0000553
daniel@transgaming.com7051b972010-03-21 04:31:07 +0000554 destLevel->Release();
555
556 img->dirty = false;
557 }
daniel@transgaming.com842f7a42010-03-21 04:31:03 +0000558 }
559}
560
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000561void Texture2D::subImage(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000562{
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000563 Texture::subImage(xoffset, yoffset, width, height, format, type, unpackAlignment, pixels, &mImageArray[level]);
daniel@transgaming.com842f7a42010-03-21 04:31:03 +0000564 commitRect(level, xoffset, yoffset, width, height);
daniel@transgaming.com00c75962010-03-11 20:36:15 +0000565}
566
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000567void Texture2D::copyImage(GLint level, GLenum internalFormat, GLint x, GLint y, GLsizei width, GLsizei height, Renderbuffer *source)
568{
569 if (redefineTexture(level, internalFormat, width, height))
570 {
571 convertToRenderTarget();
daniel@transgaming.com93a81472010-04-20 18:52:58 +0000572 pushTexture(mTexture, true);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000573 }
574
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000575 if (width != 0 && height != 0 && level < levelCount())
daniel@transgaming.com34dc3e82010-04-15 20:45:02 +0000576 {
577 RECT sourceRect;
578 sourceRect.left = x;
daniel@transgaming.com34dc3e82010-04-15 20:45:02 +0000579 sourceRect.right = x + width;
daniel@transgaming.com18b426b2010-04-20 18:52:44 +0000580 sourceRect.top = y;
581 sourceRect.bottom = y + height;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000582
daniel@transgaming.com34dc3e82010-04-15 20:45:02 +0000583 IDirect3DSurface9 *dest;
584 HRESULT hr = mTexture->GetSurfaceLevel(level, &dest);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000585
daniel@transgaming.com34dc3e82010-04-15 20:45:02 +0000586 getBlitter()->formatConvert(source->getRenderTarget(), sourceRect, internalFormat, 0, 0, dest);
587 dest->Release();
588 }
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000589
590 mImageArray[level].width = width;
591 mImageArray[level].height = height;
592 mImageArray[level].format = internalFormat;
593}
594
595void Texture2D::copySubImage(GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Renderbuffer *source)
596{
daniel@transgaming.com34dc3e82010-04-15 20:45:02 +0000597 if (xoffset + width > mImageArray[level].width || yoffset + height > mImageArray[level].height)
598 {
599 return error(GL_INVALID_VALUE);
600 }
601
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000602 if (redefineTexture(0, mImageArray[0].format, mImageArray[0].width, mImageArray[0].height))
603 {
604 convertToRenderTarget();
daniel@transgaming.com93a81472010-04-20 18:52:58 +0000605 pushTexture(mTexture, true);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000606 }
607 else
608 {
daniel@transgaming.comfc23fe22010-05-05 18:48:17 +0000609 needRenderTarget();
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000610 }
611
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000612 if (level < levelCount())
613 {
614 RECT sourceRect;
615 sourceRect.left = x;
616 sourceRect.right = x + width;
617 sourceRect.top = y;
618 sourceRect.bottom = y + height;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000619
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000620 IDirect3DSurface9 *dest;
621 HRESULT hr = mTexture->GetSurfaceLevel(level, &dest);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000622
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000623 getBlitter()->formatConvert(source->getRenderTarget(), sourceRect, mImageArray[0].format, xoffset, yoffset, dest);
624 dest->Release();
625 }
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000626}
627
daniel@transgaming.com00c75962010-03-11 20:36:15 +0000628// Tests for GL texture object completeness. [OpenGL ES 2.0.24] section 3.7.10 page 81.
629bool Texture2D::isComplete() const
630{
daniel@transgaming.com93a81472010-04-20 18:52:58 +0000631 GLsizei width = mImageArray[0].width;
632 GLsizei height = mImageArray[0].height;
daniel@transgaming.com00c75962010-03-11 20:36:15 +0000633
daniel@transgaming.com93a81472010-04-20 18:52:58 +0000634 if (width <= 0 || height <= 0)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000635 {
636 return false;
637 }
638
daniel@transgaming.com4c03fa62010-06-24 13:02:16 +0000639 bool mipmapping = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000640
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000641 switch (mMinFilter)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000642 {
643 case GL_NEAREST:
644 case GL_LINEAR:
645 mipmapping = false;
646 break;
647 case GL_NEAREST_MIPMAP_NEAREST:
648 case GL_LINEAR_MIPMAP_NEAREST:
649 case GL_NEAREST_MIPMAP_LINEAR:
650 case GL_LINEAR_MIPMAP_LINEAR:
651 mipmapping = true;
652 break;
653 default: UNREACHABLE();
654 }
655
daniel@transgaming.com4c03fa62010-06-24 13:02:16 +0000656 if ((getWrapS() != GL_CLAMP_TO_EDGE && !isPow2(width))
657 || (getWrapT() != GL_CLAMP_TO_EDGE && !isPow2(height)))
658 {
659 return false;
660 }
661
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000662 if (mipmapping)
663 {
daniel@transgaming.com4c03fa62010-06-24 13:02:16 +0000664 if (!isPow2(width) || !isPow2(height))
daniel@transgaming.comd99bd452010-04-22 13:35:25 +0000665 {
666 return false;
667 }
668
daniel@transgaming.com93a81472010-04-20 18:52:58 +0000669 int q = log2(std::max(width, height));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000670
671 for (int level = 1; level <= q; level++)
672 {
673 if (mImageArray[level].format != mImageArray[0].format)
674 {
675 return false;
676 }
677
daniel@transgaming.comd99bd452010-04-22 13:35:25 +0000678 if (mImageArray[level].width != std::max(1, width >> level))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000679 {
680 return false;
681 }
682
daniel@transgaming.comd99bd452010-04-22 13:35:25 +0000683 if (mImageArray[level].height != std::max(1, height >> level))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000684 {
685 return false;
686 }
687 }
688 }
689
690 return true;
691}
692
693// Constructs a Direct3D 9 texture resource from the texture images, or returns an existing one
daniel@transgaming.com29d27002010-03-11 19:41:22 +0000694IDirect3DBaseTexture9 *Texture2D::createTexture()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000695{
daniel@transgaming.com29d27002010-03-11 19:41:22 +0000696 IDirect3DTexture9 *texture;
697
698 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com842f7a42010-03-21 04:31:03 +0000699 D3DFORMAT format = selectFormat(mImageArray[0].format);
daniel@transgaming.com29d27002010-03-11 19:41:22 +0000700
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000701 HRESULT result = device->CreateTexture(mWidth, mHeight, creationLevels(mWidth, mHeight, 0), 0, format, D3DPOOL_DEFAULT, &texture, NULL);
daniel@transgaming.com29d27002010-03-11 19:41:22 +0000702
daniel@transgaming.com7051b972010-03-21 04:31:07 +0000703 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000704 {
daniel@transgaming.com7051b972010-03-21 04:31:07 +0000705 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com29d27002010-03-11 19:41:22 +0000706 return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000707 }
708
daniel@transgaming.com00c75962010-03-11 20:36:15 +0000709 if (mTexture) mTexture->Release();
710 mTexture = texture;
711 return texture;
712}
713
714void Texture2D::updateTexture()
715{
716 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000717
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000718 int levels = levelCount();
daniel@transgaming.com29d27002010-03-11 19:41:22 +0000719
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000720 for (int level = 0; level < levels; level++)
daniel@transgaming.com29d27002010-03-11 19:41:22 +0000721 {
daniel@transgaming.com842f7a42010-03-21 04:31:03 +0000722 if (mImageArray[level].dirty)
723 {
724 IDirect3DSurface9 *levelSurface = NULL;
daniel@transgaming.com7051b972010-03-21 04:31:07 +0000725 HRESULT result = mTexture->GetSurfaceLevel(level, &levelSurface);
daniel@transgaming.com29d27002010-03-11 19:41:22 +0000726
daniel@transgaming.com7051b972010-03-21 04:31:07 +0000727 ASSERT(SUCCEEDED(result));
daniel@transgaming.com29d27002010-03-11 19:41:22 +0000728
daniel@transgaming.com7051b972010-03-21 04:31:07 +0000729 if (SUCCEEDED(result))
730 {
731 result = device->UpdateSurface(mImageArray[level].surface, NULL, levelSurface, NULL);
732 ASSERT(SUCCEEDED(result));
daniel@transgaming.com842f7a42010-03-21 04:31:03 +0000733
daniel@transgaming.com7051b972010-03-21 04:31:07 +0000734 levelSurface->Release();
735
736 mImageArray[level].dirty = false;
737 }
daniel@transgaming.com842f7a42010-03-21 04:31:03 +0000738 }
daniel@transgaming.com29d27002010-03-11 19:41:22 +0000739 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000740}
741
daniel@transgaming.com7051b972010-03-21 04:31:07 +0000742IDirect3DBaseTexture9 *Texture2D::convertToRenderTarget()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000743{
daniel@transgaming.com34dc3e82010-04-15 20:45:02 +0000744 IDirect3DTexture9 *texture = NULL;
daniel@transgaming.com7051b972010-03-21 04:31:07 +0000745
daniel@transgaming.com34dc3e82010-04-15 20:45:02 +0000746 if (mWidth != 0 && mHeight != 0)
daniel@transgaming.com29d27002010-03-11 19:41:22 +0000747 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +0000748 egl::Display *display = getDisplay();
daniel@transgaming.com34dc3e82010-04-15 20:45:02 +0000749 IDirect3DDevice9 *device = getDevice();
750 D3DFORMAT format = selectFormat(mImageArray[0].format);
751
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000752 HRESULT result = device->CreateTexture(mWidth, mHeight, creationLevels(mWidth, mHeight, 0), D3DUSAGE_RENDERTARGET, format, D3DPOOL_DEFAULT, &texture, NULL);
daniel@transgaming.com34dc3e82010-04-15 20:45:02 +0000753
754 if (FAILED(result))
755 {
756 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
757 return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL);
758 }
759
760 if (mTexture != NULL)
761 {
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000762 int levels = levelCount();
daniel@transgaming.com34dc3e82010-04-15 20:45:02 +0000763 for (int i = 0; i < levels; i++)
764 {
765 IDirect3DSurface9 *source;
766 result = mTexture->GetSurfaceLevel(i, &source);
767
768 if (FAILED(result))
769 {
770 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
771
772 texture->Release();
773
774 return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL);
775 }
776
777 IDirect3DSurface9 *dest;
778 result = texture->GetSurfaceLevel(i, &dest);
779
780 if (FAILED(result))
781 {
782 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
783
784 texture->Release();
785 source->Release();
786
787 return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL);
788 }
789
daniel@transgaming.comae072af2010-05-05 18:47:28 +0000790 display->endScene();
daniel@transgaming.com34dc3e82010-04-15 20:45:02 +0000791 result = device->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
792
793 if (FAILED(result))
794 {
795 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
796
797 texture->Release();
798 source->Release();
799 dest->Release();
800
801 return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL);
802 }
803
804 source->Release();
805 dest->Release();
806 }
807 }
daniel@transgaming.com29d27002010-03-11 19:41:22 +0000808 }
809
daniel@transgaming.com7051b972010-03-21 04:31:07 +0000810 if (mTexture != NULL)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000811 {
daniel@transgaming.com7051b972010-03-21 04:31:07 +0000812 mTexture->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000813 }
814
daniel@transgaming.com7051b972010-03-21 04:31:07 +0000815 mTexture = texture;
816 return mTexture;
817}
818
daniel@transgaming.com842f7a42010-03-21 04:31:03 +0000819bool Texture2D::dirtyImageData() const
820{
821 int q = log2(std::max(mWidth, mHeight));
822
823 for (int i = 0; i <= q; i++)
824 {
825 if (mImageArray[i].dirty) return true;
826 }
827
828 return false;
829}
830
daniel@transgaming.com8fd99e22010-04-20 18:52:00 +0000831void Texture2D::generateMipmaps()
832{
833 if (!isPow2(mImageArray[0].width) || !isPow2(mImageArray[0].height))
834 {
835 return error(GL_INVALID_OPERATION);
836 }
837
838 // Purge array levels 1 through q and reset them to represent the generated mipmap levels.
839 unsigned int q = log2(std::max(mWidth, mHeight));
840 for (unsigned int i = 1; i <= q; i++)
841 {
842 if (mImageArray[i].surface != NULL)
843 {
844 mImageArray[i].surface->Release();
845 mImageArray[i].surface = NULL;
846 }
847
848 mImageArray[i].dirty = false;
849
850 mImageArray[i].format = mImageArray[0].format;
851 mImageArray[i].width = std::max(mImageArray[0].width >> i, 1);
852 mImageArray[i].height = std::max(mImageArray[0].height >> i, 1);
853 }
854
daniel@transgaming.com93a81472010-04-20 18:52:58 +0000855 needRenderTarget();
daniel@transgaming.com8fd99e22010-04-20 18:52:00 +0000856
857 for (unsigned int i = 1; i <= q; i++)
858 {
859 IDirect3DSurface9 *upper = NULL;
860 IDirect3DSurface9 *lower = NULL;
861
862 mTexture->GetSurfaceLevel(i-1, &upper);
863 mTexture->GetSurfaceLevel(i, &lower);
864
865 if (upper != NULL && lower != NULL)
866 {
867 getBlitter()->boxFilter(upper, lower);
868 }
869
870 if (upper != NULL) upper->Release();
871 if (lower != NULL) lower->Release();
872 }
873}
874
daniel@transgaming.com93a81472010-04-20 18:52:58 +0000875Colorbuffer *Texture2D::getColorbuffer(GLenum target)
876{
877 if (target != GL_TEXTURE_2D)
878 {
879 return error(GL_INVALID_OPERATION, (Colorbuffer *)NULL);
880 }
881
882 if (mColorbufferProxy == NULL)
883 {
884 mColorbufferProxy = new TextureColorbufferProxy(this, target);
885 }
886
887 return mColorbufferProxy;
888}
889
890IDirect3DSurface9 *Texture2D::getRenderTarget(GLenum target)
891{
892 ASSERT(target == GL_TEXTURE_2D);
893
894 needRenderTarget();
895
896 IDirect3DSurface9 *renderTarget = NULL;
897 mTexture->GetSurfaceLevel(0, &renderTarget);
898
899 return renderTarget;
900}
901
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000902TextureCubeMap::TextureCubeMap()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000903{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000904 mTexture = NULL;
daniel@transgaming.com93a81472010-04-20 18:52:58 +0000905
906 for (int i = 0; i < 6; i++)
907 {
908 mFaceProxies[i] = NULL;
909 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000910}
911
912TextureCubeMap::~TextureCubeMap()
913{
daniel@transgaming.com93a81472010-04-20 18:52:58 +0000914 for (int i = 0; i < 6; i++)
915 {
916 delete mFaceProxies[i];
917 }
918
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000919 if (mTexture)
920 {
921 mTexture->Release();
922 mTexture = NULL;
923 }
924}
925
926GLenum TextureCubeMap::getTarget() const
927{
928 return GL_TEXTURE_CUBE_MAP;
929}
930
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000931void TextureCubeMap::setImagePosX(GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000932{
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000933 setImage(0, level, internalFormat, width, height, format, type, unpackAlignment, pixels);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000934}
935
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000936void TextureCubeMap::setImageNegX(GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000937{
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000938 setImage(1, level, internalFormat, width, height, format, type, unpackAlignment, pixels);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000939}
940
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000941void TextureCubeMap::setImagePosY(GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000942{
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000943 setImage(2, level, internalFormat, width, height, format, type, unpackAlignment, pixels);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000944}
945
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000946void TextureCubeMap::setImageNegY(GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000947{
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000948 setImage(3, level, internalFormat, width, height, format, type, unpackAlignment, pixels);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000949}
950
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000951void TextureCubeMap::setImagePosZ(GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000952{
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000953 setImage(4, level, internalFormat, width, height, format, type, unpackAlignment, pixels);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000954}
955
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000956void TextureCubeMap::setImageNegZ(GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000957{
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000958 setImage(5, level, internalFormat, width, height, format, type, unpackAlignment, pixels);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000959}
960
daniel@transgaming.com842f7a42010-03-21 04:31:03 +0000961void TextureCubeMap::commitRect(GLenum faceTarget, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height)
962{
963 int face = faceIndex(faceTarget);
964
965 ASSERT(mImageArray[face][level].surface != NULL);
966
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000967 if (level < levelCount())
daniel@transgaming.com842f7a42010-03-21 04:31:03 +0000968 {
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000969 IDirect3DSurface9 *destLevel = getCubeMapSurface(face, level);
970 ASSERT(destLevel != NULL);
daniel@transgaming.com842f7a42010-03-21 04:31:03 +0000971
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000972 if (destLevel != NULL)
daniel@transgaming.com7051b972010-03-21 04:31:07 +0000973 {
974 Image *img = &mImageArray[face][level];
daniel@transgaming.com842f7a42010-03-21 04:31:03 +0000975
daniel@transgaming.com7051b972010-03-21 04:31:07 +0000976 RECT sourceRect;
977 sourceRect.left = xoffset;
978 sourceRect.top = yoffset;
979 sourceRect.right = xoffset + width;
980 sourceRect.bottom = yoffset + height;
daniel@transgaming.com842f7a42010-03-21 04:31:03 +0000981
daniel@transgaming.com7051b972010-03-21 04:31:07 +0000982 POINT destPoint;
983 destPoint.x = xoffset;
984 destPoint.y = yoffset;
daniel@transgaming.com842f7a42010-03-21 04:31:03 +0000985
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000986 HRESULT result = getDevice()->UpdateSurface(img->surface, &sourceRect, destLevel, &destPoint);
daniel@transgaming.com7051b972010-03-21 04:31:07 +0000987 ASSERT(SUCCEEDED(result));
daniel@transgaming.com842f7a42010-03-21 04:31:03 +0000988
daniel@transgaming.com7051b972010-03-21 04:31:07 +0000989 destLevel->Release();
990
991 img->dirty = false;
992 }
daniel@transgaming.com842f7a42010-03-21 04:31:03 +0000993 }
994}
995
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000996void TextureCubeMap::subImage(GLenum face, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels)
daniel@transgaming.com00c75962010-03-11 20:36:15 +0000997{
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000998 Texture::subImage(xoffset, yoffset, width, height, format, type, unpackAlignment, pixels, &mImageArray[faceIndex(face)][level]);
daniel@transgaming.com842f7a42010-03-21 04:31:03 +0000999 commitRect(face, level, xoffset, yoffset, width, height);
daniel@transgaming.com00c75962010-03-11 20:36:15 +00001000}
1001
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001002// Tests for GL texture object completeness. [OpenGL ES 2.0.24] section 3.7.10 page 81.
daniel@transgaming.com00c75962010-03-11 20:36:15 +00001003bool TextureCubeMap::isComplete() const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001004{
daniel@transgaming.com93a81472010-04-20 18:52:58 +00001005 int size = mImageArray[0][0].width;
1006
1007 if (size <= 0)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001008 {
1009 return false;
1010 }
1011
1012 bool mipmapping;
1013
daniel@transgaming.com12d54072010-03-16 06:23:26 +00001014 switch (mMinFilter)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001015 {
1016 case GL_NEAREST:
1017 case GL_LINEAR:
1018 mipmapping = false;
1019 break;
1020 case GL_NEAREST_MIPMAP_NEAREST:
1021 case GL_LINEAR_MIPMAP_NEAREST:
1022 case GL_NEAREST_MIPMAP_LINEAR:
1023 case GL_LINEAR_MIPMAP_LINEAR:
1024 mipmapping = true;
1025 break;
1026 default: UNREACHABLE();
1027 }
1028
1029 for (int face = 0; face < 6; face++)
1030 {
daniel@transgaming.com93a81472010-04-20 18:52:58 +00001031 if (mImageArray[face][0].width != size || mImageArray[face][0].height != size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001032 {
1033 return false;
1034 }
1035 }
1036
1037 if (mipmapping)
1038 {
daniel@transgaming.comd99bd452010-04-22 13:35:25 +00001039 if (!isPow2(size) && (getWrapS() != GL_CLAMP_TO_EDGE || getWrapT() != GL_CLAMP_TO_EDGE))
1040 {
1041 return false;
1042 }
1043
daniel@transgaming.com93a81472010-04-20 18:52:58 +00001044 int q = log2(size);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001045
1046 for (int face = 0; face < 6; face++)
1047 {
1048 for (int level = 1; level <= q; level++)
1049 {
1050 if (mImageArray[face][level].format != mImageArray[0][0].format)
1051 {
1052 return false;
1053 }
1054
daniel@transgaming.comd99bd452010-04-22 13:35:25 +00001055 if (mImageArray[face][level].width != std::max(1, size >> level))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001056 {
1057 return false;
1058 }
1059
daniel@transgaming.comd99bd452010-04-22 13:35:25 +00001060 ASSERT(mImageArray[face][level].height == mImageArray[face][level].width);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001061 }
1062 }
1063 }
1064
1065 return true;
1066}
1067
1068// Constructs a Direct3D 9 texture resource from the texture images, or returns an existing one
daniel@transgaming.com29d27002010-03-11 19:41:22 +00001069IDirect3DBaseTexture9 *TextureCubeMap::createTexture()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001070{
daniel@transgaming.com29d27002010-03-11 19:41:22 +00001071 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com842f7a42010-03-21 04:31:03 +00001072 D3DFORMAT format = selectFormat(mImageArray[0][0].format);
daniel@transgaming.com29d27002010-03-11 19:41:22 +00001073
1074 IDirect3DCubeTexture9 *texture;
1075
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +00001076 HRESULT result = device->CreateCubeTexture(mWidth, creationLevels(mWidth, 0), 0, format, D3DPOOL_DEFAULT, &texture, NULL);
daniel@transgaming.com29d27002010-03-11 19:41:22 +00001077
daniel@transgaming.com7051b972010-03-21 04:31:07 +00001078 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001079 {
daniel@transgaming.com7051b972010-03-21 04:31:07 +00001080 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com29d27002010-03-11 19:41:22 +00001081 return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001082 }
1083
daniel@transgaming.com00c75962010-03-11 20:36:15 +00001084 if (mTexture) mTexture->Release();
1085
1086 mTexture = texture;
1087 return mTexture;
1088}
1089
1090void TextureCubeMap::updateTexture()
1091{
1092 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001093
daniel@transgaming.com29d27002010-03-11 19:41:22 +00001094 for (int face = 0; face < 6; face++)
1095 {
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +00001096 int levels = levelCount();
1097 for (int level = 0; level < levels; level++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001098 {
daniel@transgaming.com842f7a42010-03-21 04:31:03 +00001099 Image *img = &mImageArray[face][level];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001100
daniel@transgaming.com842f7a42010-03-21 04:31:03 +00001101 if (img->dirty)
1102 {
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +00001103 IDirect3DSurface9 *levelSurface = getCubeMapSurface(face, level);
1104 ASSERT(levelSurface != NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001105
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +00001106 if (levelSurface != NULL)
daniel@transgaming.com7051b972010-03-21 04:31:07 +00001107 {
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +00001108 HRESULT result = device->UpdateSurface(img->surface, NULL, levelSurface, NULL);
daniel@transgaming.com7051b972010-03-21 04:31:07 +00001109 ASSERT(SUCCEEDED(result));
daniel@transgaming.com842f7a42010-03-21 04:31:03 +00001110
daniel@transgaming.com7051b972010-03-21 04:31:07 +00001111 levelSurface->Release();
1112
1113 img->dirty = false;
1114 }
daniel@transgaming.com842f7a42010-03-21 04:31:03 +00001115 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001116 }
1117 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001118}
1119
daniel@transgaming.com7051b972010-03-21 04:31:07 +00001120IDirect3DBaseTexture9 *TextureCubeMap::convertToRenderTarget()
1121{
daniel@transgaming.com34dc3e82010-04-15 20:45:02 +00001122 IDirect3DCubeTexture9 *texture = NULL;
daniel@transgaming.com7051b972010-03-21 04:31:07 +00001123
daniel@transgaming.com34dc3e82010-04-15 20:45:02 +00001124 if (mWidth != 0)
daniel@transgaming.com7051b972010-03-21 04:31:07 +00001125 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00001126 egl::Display *display = getDisplay();
daniel@transgaming.com34dc3e82010-04-15 20:45:02 +00001127 IDirect3DDevice9 *device = getDevice();
1128 D3DFORMAT format = selectFormat(mImageArray[0][0].format);
1129
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +00001130 HRESULT result = device->CreateCubeTexture(mWidth, creationLevels(mWidth, 0), D3DUSAGE_RENDERTARGET, format, D3DPOOL_DEFAULT, &texture, NULL);
daniel@transgaming.com34dc3e82010-04-15 20:45:02 +00001131
1132 if (FAILED(result))
1133 {
1134 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
1135 return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL);
1136 }
1137
1138 if (mTexture != NULL)
1139 {
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +00001140 int levels = levelCount();
daniel@transgaming.com34dc3e82010-04-15 20:45:02 +00001141 for (int f = 0; f < 6; f++)
1142 {
1143 for (int i = 0; i < levels; i++)
1144 {
1145 IDirect3DSurface9 *source;
1146 result = mTexture->GetCubeMapSurface(static_cast<D3DCUBEMAP_FACES>(f), i, &source);
1147
1148 if (FAILED(result))
1149 {
1150 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
1151
1152 texture->Release();
1153
1154 return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL);
1155 }
1156
1157 IDirect3DSurface9 *dest;
1158 result = texture->GetCubeMapSurface(static_cast<D3DCUBEMAP_FACES>(f), i, &dest);
1159
1160 if (FAILED(result))
1161 {
1162 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
1163
1164 texture->Release();
1165 source->Release();
1166
1167 return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL);
1168 }
1169
daniel@transgaming.comae072af2010-05-05 18:47:28 +00001170 display->endScene();
daniel@transgaming.com34dc3e82010-04-15 20:45:02 +00001171 result = device->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
1172
1173 if (FAILED(result))
1174 {
1175 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
1176
1177 texture->Release();
1178 source->Release();
1179 dest->Release();
1180
1181 return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL);
1182 }
1183 }
1184 }
1185 }
daniel@transgaming.com7051b972010-03-21 04:31:07 +00001186 }
1187
1188 if (mTexture != NULL)
1189 {
daniel@transgaming.com7051b972010-03-21 04:31:07 +00001190 mTexture->Release();
1191 }
1192
1193 mTexture = texture;
1194 return mTexture;
1195}
1196
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00001197void TextureCubeMap::setImage(int face, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001198{
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +00001199 redefineTexture(level, internalFormat, width);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001200
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +00001201 Texture::setImage(width, height, format, type, unpackAlignment, pixels, &mImageArray[face][level]);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001202}
daniel@transgaming.com00c75962010-03-11 20:36:15 +00001203
1204unsigned int TextureCubeMap::faceIndex(GLenum face)
1205{
1206 META_ASSERT(GL_TEXTURE_CUBE_MAP_NEGATIVE_X - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 1);
1207 META_ASSERT(GL_TEXTURE_CUBE_MAP_POSITIVE_Y - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 2);
1208 META_ASSERT(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 3);
1209 META_ASSERT(GL_TEXTURE_CUBE_MAP_POSITIVE_Z - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 4);
1210 META_ASSERT(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 5);
1211
1212 return face - GL_TEXTURE_CUBE_MAP_POSITIVE_X;
1213}
1214
daniel@transgaming.com842f7a42010-03-21 04:31:03 +00001215bool TextureCubeMap::dirtyImageData() const
1216{
1217 int q = log2(mWidth);
1218
1219 for (int f = 0; f < 6; f++)
1220 {
1221 for (int i = 0; i <= q; i++)
1222 {
1223 if (mImageArray[f][i].dirty) return true;
1224 }
1225 }
1226
1227 return false;
1228}
1229
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +00001230// While OpenGL doesn't check texture consistency until draw-time, D3D9 requires a complete texture
1231// for render-to-texture (such as CopyTexImage). We have no way of keeping individual inconsistent levels & faces.
1232// Call this when a particular level of the texture must be defined with a specific format, width and height.
1233//
1234// Returns true if the existing texture was unsuitable had to be destroyed. If so, it will also set
1235// a new size for the texture by working backwards from the given size.
1236bool TextureCubeMap::redefineTexture(GLint level, GLenum internalFormat, GLsizei width)
1237{
1238 // Are these settings compatible with level 0?
1239 bool sizeOkay = (mImageArray[0][0].width >> level == width);
1240
1241 bool textureOkay = (sizeOkay && internalFormat == mImageArray[0][0].format);
1242
1243 if (!textureOkay)
1244 {
1245 TRACE("Redefining cube texture (%d, 0x%04X, %d => 0x%04X, %d).", level,
1246 mImageArray[0][0].format, mImageArray[0][0].width,
1247 internalFormat, width);
1248
1249 // Purge all the levels and the texture.
1250 for (int i = 0; i < MAX_TEXTURE_LEVELS; i++)
1251 {
1252 for (int f = 0; f < 6; f++)
1253 {
1254 if (mImageArray[f][i].surface != NULL)
1255 {
1256 mImageArray[f][i].dirty = false;
1257
1258 mImageArray[f][i].surface->Release();
1259 mImageArray[f][i].surface = NULL;
1260 }
1261 }
1262 }
1263
1264 if (mTexture != NULL)
1265 {
1266 mTexture->Release();
1267 mTexture = NULL;
1268 dropTexture();
1269 }
1270
1271 mWidth = width << level;
1272 mImageArray[0][0].width = width << level;
1273 mHeight = width << level;
1274 mImageArray[0][0].height = width << level;
1275
1276 mImageArray[0][0].format = internalFormat;
1277 }
1278
1279 return !textureOkay;
1280}
1281
1282void TextureCubeMap::copyImage(GLenum face, GLint level, GLenum internalFormat, GLint x, GLint y, GLsizei width, GLsizei height, Renderbuffer *source)
1283{
1284 unsigned int faceindex = faceIndex(face);
1285
1286 if (redefineTexture(level, internalFormat, width))
1287 {
1288 convertToRenderTarget();
daniel@transgaming.com93a81472010-04-20 18:52:58 +00001289 pushTexture(mTexture, true);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +00001290 }
1291
daniel@transgaming.com34dc3e82010-04-15 20:45:02 +00001292 ASSERT(width == height);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +00001293
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +00001294 if (width > 0 && level < levelCount())
daniel@transgaming.com34dc3e82010-04-15 20:45:02 +00001295 {
1296 RECT sourceRect;
1297 sourceRect.left = x;
daniel@transgaming.com34dc3e82010-04-15 20:45:02 +00001298 sourceRect.right = x + width;
daniel@transgaming.com18b426b2010-04-20 18:52:44 +00001299 sourceRect.top = y;
1300 sourceRect.bottom = y + height;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +00001301
daniel@transgaming.com34dc3e82010-04-15 20:45:02 +00001302 IDirect3DSurface9 *dest = getCubeMapSurface(face, level);
1303
1304 getBlitter()->formatConvert(source->getRenderTarget(), sourceRect, internalFormat, 0, 0, dest);
1305 dest->Release();
1306 }
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +00001307
1308 mImageArray[faceindex][level].width = width;
1309 mImageArray[faceindex][level].height = height;
1310 mImageArray[faceindex][level].format = internalFormat;
1311}
1312
1313IDirect3DSurface9 *TextureCubeMap::getCubeMapSurface(unsigned int faceIdentifier, unsigned int level)
1314{
1315 unsigned int faceIndex;
1316
1317 if (faceIdentifier < 6)
1318 {
1319 faceIndex = faceIdentifier;
1320 }
1321 else if (faceIdentifier >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && faceIdentifier <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z)
1322 {
1323 faceIndex = faceIdentifier - GL_TEXTURE_CUBE_MAP_POSITIVE_X;
1324 }
1325 else
1326 {
1327 UNREACHABLE();
1328 faceIndex = 0;
1329 }
1330
1331 if (mTexture == NULL)
1332 {
1333 UNREACHABLE();
1334 return NULL;
1335 }
1336
1337 IDirect3DSurface9 *surface = NULL;
1338
1339 HRESULT hr = mTexture->GetCubeMapSurface(static_cast<D3DCUBEMAP_FACES>(faceIndex), level, &surface);
1340
1341 return (SUCCEEDED(hr)) ? surface : NULL;
1342}
1343
1344void TextureCubeMap::copySubImage(GLenum face, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Renderbuffer *source)
1345{
daniel@transgaming.com34dc3e82010-04-15 20:45:02 +00001346 GLsizei size = mImageArray[faceIndex(face)][level].width;
1347
1348 if (xoffset + width > size || yoffset + height > size)
1349 {
1350 return error(GL_INVALID_VALUE);
1351 }
1352
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +00001353 if (redefineTexture(0, mImageArray[0][0].format, mImageArray[0][0].width))
1354 {
1355 convertToRenderTarget();
daniel@transgaming.com93a81472010-04-20 18:52:58 +00001356 pushTexture(mTexture, true);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +00001357 }
1358 else
1359 {
1360 getRenderTarget(face);
1361 }
1362
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +00001363 if (level < levelCount())
1364 {
1365 RECT sourceRect;
1366 sourceRect.left = x;
1367 sourceRect.right = x + width;
1368 sourceRect.top = y;
1369 sourceRect.bottom = y + height;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +00001370
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +00001371 IDirect3DSurface9 *dest = getCubeMapSurface(face, level);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +00001372
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +00001373 getBlitter()->formatConvert(source->getRenderTarget(), sourceRect, mImageArray[0][0].format, xoffset, yoffset, dest);
1374 dest->Release();
1375 }
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +00001376}
1377
daniel@transgaming.com8fd99e22010-04-20 18:52:00 +00001378bool TextureCubeMap::isCubeComplete() const
1379{
1380 if (mImageArray[0][0].width == 0)
1381 {
1382 return false;
1383 }
1384
1385 for (unsigned int f = 1; f < 6; f++)
1386 {
1387 if (mImageArray[f][0].width != mImageArray[0][0].width
1388 || mImageArray[f][0].format != mImageArray[0][0].format)
1389 {
1390 return false;
1391 }
1392 }
1393
1394 return true;
1395}
1396
1397void TextureCubeMap::generateMipmaps()
1398{
1399 if (!isPow2(mImageArray[0][0].width) || !isCubeComplete())
1400 {
1401 return error(GL_INVALID_OPERATION);
1402 }
1403
1404 // Purge array levels 1 through q and reset them to represent the generated mipmap levels.
1405 unsigned int q = log2(mImageArray[0][0].width);
1406 for (unsigned int f = 0; f < 6; f++)
1407 {
1408 for (unsigned int i = 1; i <= q; i++)
1409 {
1410 if (mImageArray[f][i].surface != NULL)
1411 {
1412 mImageArray[f][i].surface->Release();
1413 mImageArray[f][i].surface = NULL;
1414 }
1415
1416 mImageArray[f][i].dirty = false;
1417
1418 mImageArray[f][i].format = mImageArray[f][0].format;
1419 mImageArray[f][i].width = std::max(mImageArray[f][0].width >> i, 1);
1420 mImageArray[f][i].height = mImageArray[f][i].width;
1421 }
1422 }
1423
daniel@transgaming.com93a81472010-04-20 18:52:58 +00001424 needRenderTarget();
daniel@transgaming.com8fd99e22010-04-20 18:52:00 +00001425
1426 for (unsigned int f = 0; f < 6; f++)
1427 {
1428 for (unsigned int i = 1; i <= q; i++)
1429 {
1430 IDirect3DSurface9 *upper = getCubeMapSurface(f, i-1);
1431 IDirect3DSurface9 *lower = getCubeMapSurface(f, i);
1432
1433 if (upper != NULL && lower != NULL)
1434 {
1435 getBlitter()->boxFilter(upper, lower);
1436 }
1437
1438 if (upper != NULL) upper->Release();
1439 if (lower != NULL) lower->Release();
1440 }
1441 }
1442}
1443
daniel@transgaming.com93a81472010-04-20 18:52:58 +00001444Colorbuffer *TextureCubeMap::getColorbuffer(GLenum target)
1445{
daniel@transgaming.com19ffc242010-05-04 03:35:21 +00001446 if (!IsCubemapTextureTarget(target))
daniel@transgaming.com93a81472010-04-20 18:52:58 +00001447 {
1448 return error(GL_INVALID_OPERATION, (Colorbuffer *)NULL);
1449 }
1450
1451 unsigned int face = faceIndex(target);
1452
1453 if (mFaceProxies[face] == NULL)
1454 {
1455 mFaceProxies[face] = new TextureColorbufferProxy(this, target);
1456 }
1457
1458 return mFaceProxies[face];
1459}
1460
1461IDirect3DSurface9 *TextureCubeMap::getRenderTarget(GLenum target)
1462{
daniel@transgaming.com19ffc242010-05-04 03:35:21 +00001463 ASSERT(IsCubemapTextureTarget(target));
daniel@transgaming.com93a81472010-04-20 18:52:58 +00001464
1465 needRenderTarget();
1466
1467 IDirect3DSurface9 *renderTarget = NULL;
1468 mTexture->GetCubeMapSurface(static_cast<D3DCUBEMAP_FACES>(faceIndex(target)), 0, &renderTarget);
1469
1470 return renderTarget;
1471}
1472
1473Texture::TextureColorbufferProxy::TextureColorbufferProxy(Texture *texture, GLenum target)
1474 : Colorbuffer(NULL), mTexture(texture), mTarget(target)
1475{
daniel@transgaming.com19ffc242010-05-04 03:35:21 +00001476 ASSERT(target == GL_TEXTURE_2D || IsCubemapTextureTarget(target));
daniel@transgaming.com93a81472010-04-20 18:52:58 +00001477}
1478
1479IDirect3DSurface9 *Texture::TextureColorbufferProxy::getRenderTarget()
1480{
daniel@transgaming.com93a81472010-04-20 18:52:58 +00001481 if (mRenderTarget) mRenderTarget->Release();
1482
1483 mRenderTarget = mTexture->getRenderTarget(mTarget);
1484
1485 return mRenderTarget;
1486}
1487
daniel@transgaming.com866f3182010-05-20 19:28:22 +00001488int Texture::TextureColorbufferProxy::getWidth()
daniel@transgaming.com93a81472010-04-20 18:52:58 +00001489{
daniel@transgaming.com866f3182010-05-20 19:28:22 +00001490 return mTexture->getWidth();
1491}
1492
1493int Texture::TextureColorbufferProxy::getHeight()
1494{
1495 return mTexture->getHeight();
daniel@transgaming.com93a81472010-04-20 18:52:58 +00001496}
1497
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001498}