blob: b8b9bdf6a61b6d4d3635a1331bcfaf95120ce563 [file] [log] [blame]
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +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 "libGLESv2/Texture.h"
12
13#include <d3dx9tex.h>
14
15#include <algorithm>
jbauman@chromium.orgf1f28c82011-05-12 20:53:34 +000016#include <intrin.h>
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +000017
18#include "common/debug.h"
19
jbauman@chromium.orgae345802011-03-30 22:04:25 +000020#include "libEGL/Display.h"
21
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +000022#include "libGLESv2/main.h"
23#include "libGLESv2/mathutil.h"
24#include "libGLESv2/utilities.h"
25#include "libGLESv2/Blit.h"
26#include "libGLESv2/Framebuffer.h"
27
28namespace gl
29{
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +000030unsigned int Texture::mCurrentSerial = 1;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +000031
32Texture::Image::Image()
daniel@transgaming.comc50edcb2011-03-21 16:38:40 +000033 : width(0), height(0), dirty(false), surface(NULL), format(GL_NONE), type(GL_UNSIGNED_BYTE)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +000034{
35}
36
37Texture::Image::~Image()
38{
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +000039 if (surface)
40 {
41 surface->Release();
42 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +000043}
44
daniel@transgaming.com549bdef2011-03-29 00:57:01 +000045bool Texture::Image::isRenderable() const
46{
47 switch(getD3DFormat())
48 {
49 case D3DFMT_L8:
50 case D3DFMT_A8L8:
51 case D3DFMT_DXT1:
gman@chromium.org50c526d2011-08-10 05:19:44 +000052 case D3DFMT_DXT3:
53 case D3DFMT_DXT5:
daniel@transgaming.com549bdef2011-03-29 00:57:01 +000054 return false;
55 case D3DFMT_A8R8G8B8:
56 case D3DFMT_X8R8G8B8:
57 case D3DFMT_A16B16G16R16F:
58 case D3DFMT_A32B32G32R32F:
59 return true;
60 default:
61 UNREACHABLE();
62 }
63
64 return false;
65}
66
67D3DFORMAT Texture::Image::getD3DFormat() const
68{
69 if (format == GL_COMPRESSED_RGB_S3TC_DXT1_EXT ||
70 format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT)
71 {
72 return D3DFMT_DXT1;
73 }
gman@chromium.org2ac3e732011-08-10 07:59:47 +000074 else if (format == GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE)
gman@chromium.org50c526d2011-08-10 05:19:44 +000075 {
76 return D3DFMT_DXT3;
77 }
gman@chromium.org2ac3e732011-08-10 07:59:47 +000078 else if (format == GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE)
gman@chromium.org50c526d2011-08-10 05:19:44 +000079 {
80 return D3DFMT_DXT5;
81 }
daniel@transgaming.com549bdef2011-03-29 00:57:01 +000082 else if (type == GL_FLOAT)
83 {
84 return D3DFMT_A32B32G32R32F;
85 }
86 else if (type == GL_HALF_FLOAT_OES)
87 {
88 return D3DFMT_A16B16G16R16F;
89 }
90 else if (type == GL_UNSIGNED_BYTE)
91 {
92 if (format == GL_LUMINANCE && getContext()->supportsLuminanceTextures())
93 {
94 return D3DFMT_L8;
95 }
96 else if (format == GL_LUMINANCE_ALPHA && getContext()->supportsLuminanceAlphaTextures())
97 {
98 return D3DFMT_A8L8;
99 }
100 else if (format == GL_RGB)
101 {
102 return D3DFMT_X8R8G8B8;
103 }
104
105 return D3DFMT_A8R8G8B8;
106 }
107
108 return D3DFMT_A8R8G8B8;
109}
110
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +0000111Texture::Texture(GLuint id) : RefCountObject(id), mSerial(issueSerial())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000112{
113 mMinFilter = GL_NEAREST_MIPMAP_LINEAR;
114 mMagFilter = GL_LINEAR;
115 mWrapS = GL_REPEAT;
116 mWrapT = GL_REPEAT;
daniel@transgaming.coma06aa872011-03-21 17:22:21 +0000117 mDirtyParameter = true;
118
119 mDirtyImage = true;
daniel@transgaming.comaed18322011-03-21 16:38:13 +0000120
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000121 mIsRenderable = false;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000122}
123
124Texture::~Texture()
125{
126}
127
128Blit *Texture::getBlitter()
129{
130 Context *context = getContext();
131 return context->getBlitter();
132}
133
134// Returns true on successful filter state update (valid enum parameter)
135bool Texture::setMinFilter(GLenum filter)
136{
137 switch (filter)
138 {
139 case GL_NEAREST:
140 case GL_LINEAR:
141 case GL_NEAREST_MIPMAP_NEAREST:
142 case GL_LINEAR_MIPMAP_NEAREST:
143 case GL_NEAREST_MIPMAP_LINEAR:
144 case GL_LINEAR_MIPMAP_LINEAR:
145 {
146 if (mMinFilter != filter)
147 {
148 mMinFilter = filter;
daniel@transgaming.coma06aa872011-03-21 17:22:21 +0000149 mDirtyParameter = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000150 }
151 return true;
152 }
153 default:
154 return false;
155 }
156}
157
158// Returns true on successful filter state update (valid enum parameter)
159bool Texture::setMagFilter(GLenum filter)
160{
161 switch (filter)
162 {
163 case GL_NEAREST:
164 case GL_LINEAR:
165 {
166 if (mMagFilter != filter)
167 {
168 mMagFilter = filter;
daniel@transgaming.coma06aa872011-03-21 17:22:21 +0000169 mDirtyParameter = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000170 }
171 return true;
172 }
173 default:
174 return false;
175 }
176}
177
178// Returns true on successful wrap state update (valid enum parameter)
179bool Texture::setWrapS(GLenum wrap)
180{
181 switch (wrap)
182 {
183 case GL_REPEAT:
184 case GL_CLAMP_TO_EDGE:
185 case GL_MIRRORED_REPEAT:
186 {
187 if (mWrapS != wrap)
188 {
189 mWrapS = wrap;
daniel@transgaming.coma06aa872011-03-21 17:22:21 +0000190 mDirtyParameter = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000191 }
192 return true;
193 }
194 default:
195 return false;
196 }
197}
198
199// Returns true on successful wrap state update (valid enum parameter)
200bool Texture::setWrapT(GLenum wrap)
201{
202 switch (wrap)
203 {
204 case GL_REPEAT:
205 case GL_CLAMP_TO_EDGE:
206 case GL_MIRRORED_REPEAT:
207 {
208 if (mWrapT != wrap)
209 {
210 mWrapT = wrap;
daniel@transgaming.coma06aa872011-03-21 17:22:21 +0000211 mDirtyParameter = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000212 }
213 return true;
214 }
215 default:
216 return false;
217 }
218}
219
220GLenum Texture::getMinFilter() const
221{
222 return mMinFilter;
223}
224
225GLenum Texture::getMagFilter() const
226{
227 return mMagFilter;
228}
229
230GLenum Texture::getWrapS() const
231{
232 return mWrapS;
233}
234
235GLenum Texture::getWrapT() const
236{
237 return mWrapT;
238}
239
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000240// Store the pixel rectangle designated by xoffset,yoffset,width,height with pixels stored as format/type at input
241// into the target pixel rectangle at output with outputPitch bytes in between each line.
242void Texture::loadImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type,
243 GLint unpackAlignment, const void *input, size_t outputPitch, void *output, D3DSURFACE_DESC *description) const
244{
245 GLsizei inputPitch = -ComputePitch(width, format, type, unpackAlignment);
246 input = ((char*)input) - inputPitch * (height - 1);
247
248 switch (type)
249 {
250 case GL_UNSIGNED_BYTE:
251 switch (format)
252 {
253 case GL_ALPHA:
254 loadAlphaImageData(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
255 break;
256 case GL_LUMINANCE:
257 loadLuminanceImageData(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output, description->Format == D3DFMT_L8);
258 break;
259 case GL_LUMINANCE_ALPHA:
260 loadLuminanceAlphaImageData(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output, description->Format == D3DFMT_A8L8);
261 break;
262 case GL_RGB:
263 loadRGBUByteImageData(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
264 break;
265 case GL_RGBA:
jbauman@chromium.orgf1f28c82011-05-12 20:53:34 +0000266 if (supportsSSE2())
267 {
268 loadRGBAUByteImageDataSSE2(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
269 }
270 else
271 {
272 loadRGBAUByteImageData(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
273 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000274 break;
275 case GL_BGRA_EXT:
276 loadBGRAImageData(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
277 break;
278 default: UNREACHABLE();
279 }
280 break;
281 case GL_UNSIGNED_SHORT_5_6_5:
282 switch (format)
283 {
284 case GL_RGB:
285 loadRGB565ImageData(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
286 break;
287 default: UNREACHABLE();
288 }
289 break;
290 case GL_UNSIGNED_SHORT_4_4_4_4:
291 switch (format)
292 {
293 case GL_RGBA:
294 loadRGBA4444ImageData(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
295 break;
296 default: UNREACHABLE();
297 }
298 break;
299 case GL_UNSIGNED_SHORT_5_5_5_1:
300 switch (format)
301 {
302 case GL_RGBA:
303 loadRGBA5551ImageData(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
304 break;
305 default: UNREACHABLE();
306 }
307 break;
308 case GL_FLOAT:
309 switch (format)
310 {
311 // float textures are converted to RGBA, not BGRA, as they're stored that way in D3D
312 case GL_ALPHA:
313 loadAlphaFloatImageData(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
314 break;
315 case GL_LUMINANCE:
316 loadLuminanceFloatImageData(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
317 break;
318 case GL_LUMINANCE_ALPHA:
319 loadLuminanceAlphaFloatImageData(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
320 break;
321 case GL_RGB:
322 loadRGBFloatImageData(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
323 break;
324 case GL_RGBA:
325 loadRGBAFloatImageData(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
326 break;
327 default: UNREACHABLE();
328 }
329 break;
330 case GL_HALF_FLOAT_OES:
331 switch (format)
332 {
333 // float textures are converted to RGBA, not BGRA, as they're stored that way in D3D
334 case GL_ALPHA:
335 loadAlphaHalfFloatImageData(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
336 break;
337 case GL_LUMINANCE:
338 loadLuminanceHalfFloatImageData(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
339 break;
340 case GL_LUMINANCE_ALPHA:
341 loadLuminanceAlphaHalfFloatImageData(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
342 break;
343 case GL_RGB:
344 loadRGBHalfFloatImageData(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
345 break;
346 case GL_RGBA:
347 loadRGBAHalfFloatImageData(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
348 break;
349 default: UNREACHABLE();
350 }
351 break;
352 default: UNREACHABLE();
353 }
354}
355
356void Texture::loadAlphaImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
357 int inputPitch, const void *input, size_t outputPitch, void *output) const
358{
359 const unsigned char *source = NULL;
360 unsigned char *dest = NULL;
361
362 for (int y = 0; y < height; y++)
363 {
364 source = static_cast<const unsigned char*>(input) + y * inputPitch;
365 dest = static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 4;
366 for (int x = 0; x < width; x++)
367 {
368 dest[4 * x + 0] = 0;
369 dest[4 * x + 1] = 0;
370 dest[4 * x + 2] = 0;
371 dest[4 * x + 3] = source[x];
372 }
373 }
374}
375
376void Texture::loadAlphaFloatImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
377 int inputPitch, const void *input, size_t outputPitch, void *output) const
378{
379 const float *source = NULL;
380 float *dest = NULL;
381
382 for (int y = 0; y < height; y++)
383 {
384 source = reinterpret_cast<const float*>(static_cast<const unsigned char*>(input) + y * inputPitch);
385 dest = reinterpret_cast<float*>(static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 16);
386 for (int x = 0; x < width; x++)
387 {
388 dest[4 * x + 0] = 0;
389 dest[4 * x + 1] = 0;
390 dest[4 * x + 2] = 0;
391 dest[4 * x + 3] = source[x];
392 }
393 }
394}
395
396void Texture::loadAlphaHalfFloatImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
397 int inputPitch, const void *input, size_t outputPitch, void *output) const
398{
399 const unsigned short *source = NULL;
400 unsigned short *dest = NULL;
401
402 for (int y = 0; y < height; y++)
403 {
404 source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch);
405 dest = reinterpret_cast<unsigned short*>(static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 8);
406 for (int x = 0; x < width; x++)
407 {
408 dest[4 * x + 0] = 0;
409 dest[4 * x + 1] = 0;
410 dest[4 * x + 2] = 0;
411 dest[4 * x + 3] = source[x];
412 }
413 }
414}
415
416void Texture::loadLuminanceImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
417 int inputPitch, const void *input, size_t outputPitch, void *output, bool native) const
418{
419 const int destBytesPerPixel = native? 1: 4;
420 const unsigned char *source = NULL;
421 unsigned char *dest = NULL;
422
423 for (int y = 0; y < height; y++)
424 {
425 source = static_cast<const unsigned char*>(input) + y * inputPitch;
426 dest = static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * destBytesPerPixel;
427
428 if (!native) // BGRA8 destination format
429 {
430 for (int x = 0; x < width; x++)
431 {
432 dest[4 * x + 0] = source[x];
433 dest[4 * x + 1] = source[x];
434 dest[4 * x + 2] = source[x];
435 dest[4 * x + 3] = 0xFF;
436 }
437 }
438 else // L8 destination format
439 {
440 memcpy(dest, source, width);
441 }
442 }
443}
444
445void Texture::loadLuminanceFloatImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
446 int inputPitch, const void *input, size_t outputPitch, void *output) const
447{
448 const float *source = NULL;
449 float *dest = NULL;
450
451 for (int y = 0; y < height; y++)
452 {
453 source = reinterpret_cast<const float*>(static_cast<const unsigned char*>(input) + y * inputPitch);
454 dest = reinterpret_cast<float*>(static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 16);
455 for (int x = 0; x < width; x++)
456 {
457 dest[4 * x + 0] = source[x];
458 dest[4 * x + 1] = source[x];
459 dest[4 * x + 2] = source[x];
460 dest[4 * x + 3] = 1.0f;
461 }
462 }
463}
464
465void Texture::loadLuminanceHalfFloatImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
466 int inputPitch, const void *input, size_t outputPitch, void *output) const
467{
468 const unsigned short *source = NULL;
469 unsigned short *dest = NULL;
470
471 for (int y = 0; y < height; y++)
472 {
473 source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch);
474 dest = reinterpret_cast<unsigned short*>(static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 8);
475 for (int x = 0; x < width; x++)
476 {
477 dest[4 * x + 0] = source[x];
478 dest[4 * x + 1] = source[x];
479 dest[4 * x + 2] = source[x];
480 dest[4 * x + 3] = 0x3C00; // SEEEEEMMMMMMMMMM, S = 0, E = 15, M = 0: 16bit flpt representation of 1
481 }
482 }
483}
484
485void Texture::loadLuminanceAlphaImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
486 int inputPitch, const void *input, size_t outputPitch, void *output, bool native) const
487{
488 const int destBytesPerPixel = native? 2: 4;
489 const unsigned char *source = NULL;
490 unsigned char *dest = NULL;
491
492 for (int y = 0; y < height; y++)
493 {
494 source = static_cast<const unsigned char*>(input) + y * inputPitch;
495 dest = static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * destBytesPerPixel;
496
497 if (!native) // BGRA8 destination format
498 {
499 for (int x = 0; x < width; x++)
500 {
501 dest[4 * x + 0] = source[2*x+0];
502 dest[4 * x + 1] = source[2*x+0];
503 dest[4 * x + 2] = source[2*x+0];
504 dest[4 * x + 3] = source[2*x+1];
505 }
506 }
507 else
508 {
509 memcpy(dest, source, width * 2);
510 }
511 }
512}
513
514void Texture::loadLuminanceAlphaFloatImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
515 int inputPitch, const void *input, size_t outputPitch, void *output) const
516{
517 const float *source = NULL;
518 float *dest = NULL;
519
520 for (int y = 0; y < height; y++)
521 {
522 source = reinterpret_cast<const float*>(static_cast<const unsigned char*>(input) + y * inputPitch);
523 dest = reinterpret_cast<float*>(static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 16);
524 for (int x = 0; x < width; x++)
525 {
526 dest[4 * x + 0] = source[2*x+0];
527 dest[4 * x + 1] = source[2*x+0];
528 dest[4 * x + 2] = source[2*x+0];
529 dest[4 * x + 3] = source[2*x+1];
530 }
531 }
532}
533
534void Texture::loadLuminanceAlphaHalfFloatImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
535 int inputPitch, const void *input, size_t outputPitch, void *output) const
536{
537 const unsigned short *source = NULL;
538 unsigned short *dest = NULL;
539
540 for (int y = 0; y < height; y++)
541 {
542 source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch);
543 dest = reinterpret_cast<unsigned short*>(static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 8);
544 for (int x = 0; x < width; x++)
545 {
546 dest[4 * x + 0] = source[2*x+0];
547 dest[4 * x + 1] = source[2*x+0];
548 dest[4 * x + 2] = source[2*x+0];
549 dest[4 * x + 3] = source[2*x+1];
550 }
551 }
552}
553
554void Texture::loadRGBUByteImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
555 int inputPitch, const void *input, size_t outputPitch, void *output) const
556{
557 const unsigned char *source = NULL;
558 unsigned char *dest = NULL;
559
560 for (int y = 0; y < height; y++)
561 {
562 source = static_cast<const unsigned char*>(input) + y * inputPitch;
563 dest = static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 4;
564 for (int x = 0; x < width; x++)
565 {
566 dest[4 * x + 0] = source[x * 3 + 2];
567 dest[4 * x + 1] = source[x * 3 + 1];
568 dest[4 * x + 2] = source[x * 3 + 0];
569 dest[4 * x + 3] = 0xFF;
570 }
571 }
572}
573
574void Texture::loadRGB565ImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
575 int inputPitch, const void *input, size_t outputPitch, void *output) const
576{
577 const unsigned short *source = NULL;
578 unsigned char *dest = NULL;
579
580 for (int y = 0; y < height; y++)
581 {
582 source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch);
583 dest = static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 4;
584 for (int x = 0; x < width; x++)
585 {
586 unsigned short rgba = source[x];
587 dest[4 * x + 0] = ((rgba & 0x001F) << 3) | ((rgba & 0x001F) >> 2);
588 dest[4 * x + 1] = ((rgba & 0x07E0) >> 3) | ((rgba & 0x07E0) >> 9);
589 dest[4 * x + 2] = ((rgba & 0xF800) >> 8) | ((rgba & 0xF800) >> 13);
590 dest[4 * x + 3] = 0xFF;
591 }
592 }
593}
594
595void Texture::loadRGBFloatImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
596 int inputPitch, const void *input, size_t outputPitch, void *output) const
597{
598 const float *source = NULL;
599 float *dest = NULL;
600
601 for (int y = 0; y < height; y++)
602 {
603 source = reinterpret_cast<const float*>(static_cast<const unsigned char*>(input) + y * inputPitch);
604 dest = reinterpret_cast<float*>(static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 16);
605 for (int x = 0; x < width; x++)
606 {
607 dest[4 * x + 0] = source[x * 3 + 0];
608 dest[4 * x + 1] = source[x * 3 + 1];
609 dest[4 * x + 2] = source[x * 3 + 2];
610 dest[4 * x + 3] = 1.0f;
611 }
612 }
613}
614
615void Texture::loadRGBHalfFloatImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
616 int inputPitch, const void *input, size_t outputPitch, void *output) const
617{
618 const unsigned short *source = NULL;
619 unsigned short *dest = NULL;
620
621 for (int y = 0; y < height; y++)
622 {
623 source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch);
624 dest = reinterpret_cast<unsigned short*>(static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 8);
625 for (int x = 0; x < width; x++)
626 {
627 dest[4 * x + 0] = source[x * 3 + 0];
628 dest[4 * x + 1] = source[x * 3 + 1];
629 dest[4 * x + 2] = source[x * 3 + 2];
630 dest[4 * x + 3] = 0x3C00; // SEEEEEMMMMMMMMMM, S = 0, E = 15, M = 0: 16bit flpt representation of 1
631 }
632 }
633}
634
jbauman@chromium.orgf1f28c82011-05-12 20:53:34 +0000635void Texture::loadRGBAUByteImageDataSSE2(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
636 int inputPitch, const void *input, size_t outputPitch, void *output) const
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000637{
jbauman@chromium.orgf1f28c82011-05-12 20:53:34 +0000638 const unsigned int *source = NULL;
639 unsigned int *dest = NULL;
640 __m128i brMask = _mm_set1_epi32(0x00ff00ff);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000641
642 for (int y = 0; y < height; y++)
643 {
jbauman@chromium.orgf1f28c82011-05-12 20:53:34 +0000644 source = reinterpret_cast<const unsigned int*>(static_cast<const unsigned char*>(input) + y * inputPitch);
645 dest = reinterpret_cast<unsigned int*>(static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 4);
646 int x = 0;
647
648 // Make output writes aligned
649 for (x = 0; ((reinterpret_cast<intptr_t>(&dest[x]) & 15) != 0) && x < width; x++)
650 {
651 unsigned int rgba = source[x];
652 dest[x] = (_rotl(rgba, 16) & 0x00ff00ff) | (rgba & 0xff00ff00);
653 }
654
655 for (; x + 3 < width; x += 4)
656 {
657 __m128i sourceData = _mm_loadu_si128(reinterpret_cast<const __m128i*>(&source[x]));
658 // Mask out g and a, which don't change
659 __m128i gaComponents = _mm_andnot_si128(brMask, sourceData);
660 // Mask out b and r
661 __m128i brComponents = _mm_and_si128(sourceData, brMask);
662 // Swap b and r
663 __m128i brSwapped = _mm_shufflehi_epi16(_mm_shufflelo_epi16(brComponents, _MM_SHUFFLE(2, 3, 0, 1)), _MM_SHUFFLE(2, 3, 0, 1));
664 __m128i result = _mm_or_si128(gaComponents, brSwapped);
665 _mm_store_si128(reinterpret_cast<__m128i*>(&dest[x]), result);
666 }
667
668 // Perform leftover writes
669 for (; x < width; x++)
670 {
671 unsigned int rgba = source[x];
672 dest[x] = (_rotl(rgba, 16) & 0x00ff00ff) | (rgba & 0xff00ff00);
673 }
674 }
675}
676
677void Texture::loadRGBAUByteImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
678 int inputPitch, const void *input, size_t outputPitch, void *output) const
679{
680 const unsigned int *source = NULL;
681 unsigned int *dest = NULL;
682 for (int y = 0; y < height; y++)
683 {
684 source = reinterpret_cast<const unsigned int*>(static_cast<const unsigned char*>(input) + y * inputPitch);
685 dest = reinterpret_cast<unsigned int*>(static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 4);
686
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000687 for (int x = 0; x < width; x++)
688 {
jbauman@chromium.orgf1f28c82011-05-12 20:53:34 +0000689 unsigned int rgba = source[x];
690 dest[x] = (_rotl(rgba, 16) & 0x00ff00ff) | (rgba & 0xff00ff00);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000691 }
692 }
693}
694
695void Texture::loadRGBA4444ImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
696 int inputPitch, const void *input, size_t outputPitch, void *output) const
697{
698 const unsigned short *source = NULL;
699 unsigned char *dest = NULL;
700
701 for (int y = 0; y < height; y++)
702 {
703 source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch);
704 dest = static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 4;
705 for (int x = 0; x < width; x++)
706 {
707 unsigned short rgba = source[x];
708 dest[4 * x + 0] = ((rgba & 0x00F0) << 0) | ((rgba & 0x00F0) >> 4);
709 dest[4 * x + 1] = ((rgba & 0x0F00) >> 4) | ((rgba & 0x0F00) >> 8);
710 dest[4 * x + 2] = ((rgba & 0xF000) >> 8) | ((rgba & 0xF000) >> 12);
711 dest[4 * x + 3] = ((rgba & 0x000F) << 4) | ((rgba & 0x000F) >> 0);
712 }
713 }
714}
715
716void Texture::loadRGBA5551ImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
717 int inputPitch, const void *input, size_t outputPitch, void *output) const
718{
719 const unsigned short *source = NULL;
720 unsigned char *dest = NULL;
721
722 for (int y = 0; y < height; y++)
723 {
724 source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch);
725 dest = static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 4;
726 for (int x = 0; x < width; x++)
727 {
728 unsigned short rgba = source[x];
729 dest[4 * x + 0] = ((rgba & 0x003E) << 2) | ((rgba & 0x003E) >> 3);
730 dest[4 * x + 1] = ((rgba & 0x07C0) >> 3) | ((rgba & 0x07C0) >> 8);
731 dest[4 * x + 2] = ((rgba & 0xF800) >> 8) | ((rgba & 0xF800) >> 13);
732 dest[4 * x + 3] = (rgba & 0x0001) ? 0xFF : 0;
733 }
734 }
735}
736
737void Texture::loadRGBAFloatImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
738 int inputPitch, const void *input, size_t outputPitch, void *output) const
739{
740 const float *source = NULL;
741 float *dest = NULL;
742
743 for (int y = 0; y < height; y++)
744 {
745 source = reinterpret_cast<const float*>(static_cast<const unsigned char*>(input) + y * inputPitch);
746 dest = reinterpret_cast<float*>(static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 16);
747 memcpy(dest, source, width * 16);
748 }
749}
750
751void Texture::loadRGBAHalfFloatImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
752 int inputPitch, const void *input, size_t outputPitch, void *output) const
753{
754 const unsigned char *source = NULL;
755 unsigned char *dest = NULL;
756
757 for (int y = 0; y < height; y++)
758 {
759 source = static_cast<const unsigned char*>(input) + y * inputPitch;
760 dest = static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 8;
761 memcpy(dest, source, width * 8);
762 }
763}
764
765void Texture::loadBGRAImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
766 int inputPitch, const void *input, size_t outputPitch, void *output) const
767{
768 const unsigned char *source = NULL;
769 unsigned char *dest = NULL;
770
771 for (int y = 0; y < height; y++)
772 {
773 source = static_cast<const unsigned char*>(input) + y * inputPitch;
774 dest = static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 4;
775 memcpy(dest, source, width*4);
776 }
777}
778
779void Texture::loadCompressedImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
gman@chromium.org50c526d2011-08-10 05:19:44 +0000780 int inputPitch, const void *input, size_t outputPitch, void *output) const {
781 switch (getD3DFormat())
782 {
783 case D3DFMT_DXT1:
784 loadDXT1ImageData(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
785 break;
786 case D3DFMT_DXT3:
787 loadDXT3ImageData(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
788 break;
789 case D3DFMT_DXT5:
790 loadDXT5ImageData(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
791 break;
792 }
793}
794
795static void FlipCopyDXT1BlockFull(const unsigned int* source, unsigned int* dest) {
796 // A DXT1 block layout is:
797 // [0-1] color0.
798 // [2-3] color1.
799 // [4-7] color bitmap, 2 bits per pixel.
800 // So each of the 4-7 bytes represents one line, flipping a block is just
801 // flipping those bytes.
802
803 // First 32-bits is two RGB565 colors shared by tile and does not need to be modified.
804 dest[0] = source[0];
805
806 // Second 32-bits contains 4 rows of 4 2-bit interpolants between the colors. All rows should be flipped.
807 dest[1] = (source[1] >> 24) |
808 ((source[1] << 8) & 0x00FF0000) |
809 ((source[1] >> 8) & 0x0000FF00) |
810 (source[1] << 24);
811}
812
813// Flips the first 2 lines of a DXT1 block in the y direction.
814static void FlipCopyDXT1BlockHalf(const unsigned int* source, unsigned int* dest) {
815 // See layout above.
816 dest[0] = source[0];
817 dest[1] = ((source[1] << 8) & 0x0000FF00) |
818 ((source[1] >> 8) & 0x000000FF);
819}
820
821// Flips a full DXT3 block in the y direction.
822static void FlipCopyDXT3BlockFull(const unsigned int* source, unsigned int* dest) {
823 // A DXT3 block layout is:
824 // [0-7] alpha bitmap, 4 bits per pixel.
825 // [8-15] a DXT1 block.
826
827 // First and Second 32 bits are 4bit per pixel alpha and need to be flipped.
828 dest[0] = (source[1] >> 16) | (source[1] << 16);
829 dest[1] = (source[0] >> 16) | (source[0] << 16);
830
831 // And flip the DXT1 block using the above function.
832 FlipCopyDXT1BlockFull(source + 2, dest + 2);
833}
834
835// Flips the first 2 lines of a DXT3 block in the y direction.
836static void FlipCopyDXT3BlockHalf(const unsigned int* source, unsigned int* dest) {
837 // See layout above.
838 dest[0] = (source[1] >> 16) | (source[1] << 16);
839 FlipCopyDXT1BlockHalf(source + 2, dest + 2);
840}
841
842// Flips a full DXT5 block in the y direction.
843static void FlipCopyDXT5BlockFull(const unsigned int* source, unsigned int* dest) {
844 // A DXT5 block layout is:
845 // [0] alpha0.
846 // [1] alpha1.
847 // [2-7] alpha bitmap, 3 bits per pixel.
848 // [8-15] a DXT1 block.
849
850 // The alpha bitmap doesn't easily map lines to bytes, so we have to
851 // interpret it correctly. Extracted from
852 // http://www.opengl.org/registry/specs/EXT/texture_compression_s3tc.txt :
853 //
854 // The 6 "bits" bytes of the block are decoded into one 48-bit integer:
855 //
856 // bits = bits_0 + 256 * (bits_1 + 256 * (bits_2 + 256 * (bits_3 +
857 // 256 * (bits_4 + 256 * bits_5))))
858 //
859 // bits is a 48-bit unsigned integer, from which a three-bit control code
860 // is extracted for a texel at location (x,y) in the block using:
861 //
862 // code(x,y) = bits[3*(4*y+x)+1..3*(4*y+x)+0]
863 //
864 // where bit 47 is the most significant and bit 0 is the least
865 // significant bit.
866 const unsigned char* sourceBytes = static_cast<const unsigned char*>(static_cast<const void*>(source));
867 unsigned char* destBytes = static_cast<unsigned char*>(static_cast<void*>(dest));
868 unsigned int line_0_1 = sourceBytes[2] + 256 * (sourceBytes[3] + 256 * sourceBytes[4]);
869 unsigned int line_2_3 = sourceBytes[5] + 256 * (sourceBytes[6] + 256 * sourceBytes[7]);
870 // swap lines 0 and 1 in line_0_1.
871 unsigned int line_1_0 = ((line_0_1 & 0x000fff) << 12) |
872 ((line_0_1 & 0xfff000) >> 12);
873 // swap lines 2 and 3 in line_2_3.
874 unsigned int line_3_2 = ((line_2_3 & 0x000fff) << 12) |
875 ((line_2_3 & 0xfff000) >> 12);
876 destBytes[0] = sourceBytes[0];
877 destBytes[1] = sourceBytes[1];
878 destBytes[2] = line_3_2 & 0xff;
879 destBytes[3] = (line_3_2 & 0xff00) >> 8;
gman@chromium.org2ac3e732011-08-10 07:59:47 +0000880 destBytes[4] = (line_3_2 & 0xff0000) >> 16;
gman@chromium.org50c526d2011-08-10 05:19:44 +0000881 destBytes[5] = line_1_0 & 0xff;
882 destBytes[6] = (line_1_0 & 0xff00) >> 8;
gman@chromium.org2ac3e732011-08-10 07:59:47 +0000883 destBytes[7] = (line_1_0 & 0xff0000) >> 16;
gman@chromium.org50c526d2011-08-10 05:19:44 +0000884
885 // And flip the DXT1 block using the above function.
886 FlipCopyDXT1BlockFull(source + 2, dest + 2);
887}
888
889// Flips the first 2 lines of a DXT5 block in the y direction.
890static void FlipCopyDXT5BlockHalf(const unsigned int* source, unsigned int* dest) {
891 // See layout above.
892 const unsigned char* sourceBytes = static_cast<const unsigned char*>(static_cast<const void*>(source));
893 unsigned char* destBytes = static_cast<unsigned char*>(static_cast<void*>(dest));
894 unsigned int line_0_1 = sourceBytes[2] + 256 * (sourceBytes[3] + 256 * sourceBytes[4]);
895 unsigned int line_1_0 = ((line_0_1 & 0x000fff) << 12) |
896 ((line_0_1 & 0xfff000) >> 12);
897 destBytes[0] = sourceBytes[0];
898 destBytes[1] = sourceBytes[1];
899 destBytes[2] = line_1_0 & 0xff;
900 destBytes[3] = (line_1_0 & 0xff00) >> 8;
gman@chromium.org25c5cf62011-08-10 08:07:54 +0000901 destBytes[4] = (line_1_0 & 0xff0000) >> 16;
gman@chromium.org50c526d2011-08-10 05:19:44 +0000902 FlipCopyDXT1BlockHalf(source + 2, dest + 2);
903}
904
905void Texture::loadDXT1ImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000906 int inputPitch, const void *input, size_t outputPitch, void *output) const
907{
908 ASSERT(xoffset % 4 == 0);
909 ASSERT(yoffset % 4 == 0);
910 ASSERT(width % 4 == 0 || width == 2 || width == 1);
911 ASSERT(inputPitch % 8 == 0);
912 ASSERT(outputPitch % 8 == 0);
913
914 const unsigned int *source = reinterpret_cast<const unsigned int*>(input);
915 unsigned int *dest = reinterpret_cast<unsigned int*>(output);
916
gman@chromium.org50c526d2011-08-10 05:19:44 +0000917 // Round width up in case it is less than 4.
918 int blocksAcross = (width + 3) / 4;
919 int intsAcross = blocksAcross * 2;
920
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000921 switch (height)
922 {
923 case 1:
gman@chromium.org50c526d2011-08-10 05:19:44 +0000924 for (int x = 0; x < intsAcross; x += 2)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000925 {
gman@chromium.org50c526d2011-08-10 05:19:44 +0000926 // just copy the block
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000927 dest[x] = source[x];
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000928 dest[x + 1] = source[x + 1];
929 }
930 break;
931 case 2:
gman@chromium.org50c526d2011-08-10 05:19:44 +0000932 for (int x = 0; x < intsAcross; x += 2)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000933 {
gman@chromium.org50c526d2011-08-10 05:19:44 +0000934 FlipCopyDXT1BlockHalf(source + x, dest + x);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000935 }
936 break;
937 default:
938 ASSERT(height % 4 == 0);
939 for (int y = 0; y < height / 4; ++y)
940 {
941 const unsigned int *source = reinterpret_cast<const unsigned int*>(static_cast<const unsigned char*>(input) + y * inputPitch);
942 unsigned int *dest = reinterpret_cast<unsigned int*>(static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 8);
943
gman@chromium.org50c526d2011-08-10 05:19:44 +0000944 for (int x = 0; x < intsAcross; x += 2)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000945 {
gman@chromium.org50c526d2011-08-10 05:19:44 +0000946 FlipCopyDXT1BlockFull(source + x, dest + x);
947 }
948 }
949 break;
950 }
951}
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000952
gman@chromium.org50c526d2011-08-10 05:19:44 +0000953void Texture::loadDXT3ImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
954 int inputPitch, const void *input, size_t outputPitch, void *output) const
955{
956 ASSERT(xoffset % 4 == 0);
957 ASSERT(yoffset % 4 == 0);
958 ASSERT(width % 4 == 0 || width == 2 || width == 1);
959 ASSERT(inputPitch % 16 == 0);
960 ASSERT(outputPitch % 16 == 0);
961
962 const unsigned int *source = reinterpret_cast<const unsigned int*>(input);
963 unsigned int *dest = reinterpret_cast<unsigned int*>(output);
964
965 // Round width up in case it is less than 4.
966 int blocksAcross = (width + 3) / 4;
967 int intsAcross = blocksAcross * 4;
968
969 switch (height)
970 {
971 case 1:
972 for (int x = 0; x < intsAcross; x += 4)
973 {
974 // just copy the block
975 dest[x] = source[x];
976 dest[x + 1] = source[x + 1];
977 dest[x + 2] = source[x + 2];
978 dest[x + 3] = source[x + 3];
979 }
980 break;
981 case 2:
982 for (int x = 0; x < intsAcross; x += 4)
983 {
984 FlipCopyDXT3BlockHalf(source + x, dest + x);
985 }
986 break;
987 default:
988 ASSERT(height % 4 == 0);
989 for (int y = 0; y < height / 4; ++y)
990 {
991 const unsigned int *source = reinterpret_cast<const unsigned int*>(static_cast<const unsigned char*>(input) + y * inputPitch);
992 unsigned int *dest = reinterpret_cast<unsigned int*>(static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 16);
993
994 for (int x = 0; x < intsAcross; x += 4)
995 {
996 FlipCopyDXT3BlockFull(source + x, dest + x);
997 }
998 }
999 break;
1000 }
1001}
1002
1003void Texture::loadDXT5ImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
1004 int inputPitch, const void *input, size_t outputPitch, void *output) const
1005{
1006 ASSERT(xoffset % 4 == 0);
1007 ASSERT(yoffset % 4 == 0);
1008 ASSERT(width % 4 == 0 || width == 2 || width == 1);
1009 ASSERT(inputPitch % 16 == 0);
1010 ASSERT(outputPitch % 16 == 0);
1011
1012 const unsigned int *source = reinterpret_cast<const unsigned int*>(input);
1013 unsigned int *dest = reinterpret_cast<unsigned int*>(output);
1014
1015 // Round width up in case it is less than 4.
1016 int blocksAcross = (width + 3) / 4;
1017 int intsAcross = blocksAcross * 4;
1018
1019 switch (height)
1020 {
1021 case 1:
1022 for (int x = 0; x < intsAcross; x += 4)
1023 {
1024 // just copy the block
1025 dest[x] = source[x];
1026 dest[x + 1] = source[x + 1];
1027 dest[x + 2] = source[x + 2];
1028 dest[x + 3] = source[x + 3];
1029 }
1030 break;
1031 case 2:
1032 for (int x = 0; x < intsAcross; x += 4)
1033 {
1034 FlipCopyDXT5BlockHalf(source + x, dest + x);
1035 }
1036 break;
1037 default:
1038 ASSERT(height % 4 == 0);
gman@chromium.org2ac3e732011-08-10 07:59:47 +00001039 for (int y = 0; y < height / 4; ++y)
gman@chromium.org50c526d2011-08-10 05:19:44 +00001040 {
1041 const unsigned int *source = reinterpret_cast<const unsigned int*>(static_cast<const unsigned char*>(input) + y * inputPitch);
1042 unsigned int *dest = reinterpret_cast<unsigned int*>(static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 16);
1043
1044 for (int x = 0; x < intsAcross; x += 4)
1045 {
1046 FlipCopyDXT5BlockFull(source + x, dest + x);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001047 }
1048 }
1049 break;
1050 }
1051}
1052
daniel@transgaming.com61208202011-03-21 16:38:50 +00001053void Texture::createSurface(Image *image)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001054{
1055 IDirect3DTexture9 *newTexture = NULL;
1056 IDirect3DSurface9 *newSurface = NULL;
1057
daniel@transgaming.com61208202011-03-21 16:38:50 +00001058 if (image->width != 0 && image->height != 0)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001059 {
1060 int levelToFetch = 0;
daniel@transgaming.com61208202011-03-21 16:38:50 +00001061 GLsizei requestWidth = image->width;
1062 GLsizei requestHeight = image->height;
1063 if (IsCompressed(image->format) && (image->width % 4 != 0 || image->height % 4 != 0))
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001064 {
1065 bool isMult4 = false;
1066 int upsampleCount = 0;
1067 while (!isMult4)
1068 {
1069 requestWidth <<= 1;
1070 requestHeight <<= 1;
1071 upsampleCount++;
1072 if (requestWidth % 4 == 0 && requestHeight % 4 == 0)
1073 {
1074 isMult4 = true;
1075 }
1076 }
1077 levelToFetch = upsampleCount;
1078 }
1079
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00001080 HRESULT result = getDevice()->CreateTexture(requestWidth, requestHeight, levelToFetch + 1, NULL, image->getD3DFormat(),
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001081 D3DPOOL_SYSTEMMEM, &newTexture, NULL);
1082
1083 if (FAILED(result))
1084 {
1085 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
1086 return error(GL_OUT_OF_MEMORY);
1087 }
1088
1089 newTexture->GetSurfaceLevel(levelToFetch, &newSurface);
1090 newTexture->Release();
1091 }
1092
daniel@transgaming.com0bd22bc2011-03-21 16:38:26 +00001093 if (image->surface)
1094 {
1095 image->surface->Release();
1096 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001097
daniel@transgaming.com0bd22bc2011-03-21 16:38:26 +00001098 image->surface = newSurface;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001099}
1100
daniel@transgaming.com61208202011-03-21 16:38:50 +00001101void Texture::setImage(GLint unpackAlignment, const void *pixels, Image *image)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001102{
daniel@transgaming.com61208202011-03-21 16:38:50 +00001103 createSurface(image);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001104
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001105 if (pixels != NULL && image->surface != NULL)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001106 {
1107 D3DSURFACE_DESC description;
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001108 image->surface->GetDesc(&description);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001109
1110 D3DLOCKED_RECT locked;
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001111 HRESULT result = image->surface->LockRect(&locked, NULL, 0);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001112
1113 ASSERT(SUCCEEDED(result));
1114
1115 if (SUCCEEDED(result))
1116 {
daniel@transgaming.com61208202011-03-21 16:38:50 +00001117 loadImageData(0, 0, image->width, image->height, image->format, image->type, unpackAlignment, pixels, locked.Pitch, locked.pBits, &description);
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001118 image->surface->UnlockRect();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001119 }
1120
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001121 image->dirty = true;
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00001122 mDirtyImage = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001123 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001124}
1125
daniel@transgaming.com61208202011-03-21 16:38:50 +00001126void Texture::setCompressedImage(GLsizei imageSize, const void *pixels, Image *image)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001127{
daniel@transgaming.com61208202011-03-21 16:38:50 +00001128 createSurface(image);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001129
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001130 if (pixels != NULL && image->surface != NULL)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001131 {
1132 D3DLOCKED_RECT locked;
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001133 HRESULT result = image->surface->LockRect(&locked, NULL, 0);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001134
1135 ASSERT(SUCCEEDED(result));
1136
1137 if (SUCCEEDED(result))
1138 {
daniel@transgaming.com61208202011-03-21 16:38:50 +00001139 int inputPitch = ComputeCompressedPitch(image->width, image->format);
1140 int inputSize = ComputeCompressedSize(image->width, image->height, image->format);
1141 loadCompressedImageData(0, 0, image->width, image->height, -inputPitch, static_cast<const char*>(pixels) + inputSize - inputPitch, locked.Pitch, locked.pBits);
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001142 image->surface->UnlockRect();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001143 }
1144
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001145 image->dirty = true;
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00001146 mDirtyImage = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001147 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001148}
1149
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001150bool Texture::subImage(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels, Image *image)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001151{
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001152 if (width + xoffset > image->width || height + yoffset > image->height)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001153 {
1154 error(GL_INVALID_VALUE);
1155 return false;
1156 }
1157
jbauman@chromium.orge2f954c2011-05-03 20:45:27 +00001158 if (IsCompressed(image->format))
1159 {
1160 error(GL_INVALID_OPERATION);
1161 return false;
1162 }
1163
1164 if (format != image->format)
1165 {
1166 error(GL_INVALID_OPERATION);
1167 return false;
1168 }
1169
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001170 if (!image->surface)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001171 {
daniel@transgaming.com61208202011-03-21 16:38:50 +00001172 createSurface(image);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001173 }
1174
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001175 if (pixels != NULL && image->surface != NULL)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001176 {
1177 D3DSURFACE_DESC description;
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001178 image->surface->GetDesc(&description);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001179
1180 D3DLOCKED_RECT locked;
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001181 HRESULT result = image->surface->LockRect(&locked, NULL, 0);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001182
1183 ASSERT(SUCCEEDED(result));
1184
1185 if (SUCCEEDED(result))
1186 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001187 loadImageData(xoffset, transformPixelYOffset(yoffset, height, image->height), width, height, format, type, unpackAlignment, pixels, locked.Pitch, locked.pBits, &description);
1188 image->surface->UnlockRect();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001189 }
1190
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001191 image->dirty = true;
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00001192 mDirtyImage = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001193 }
1194
1195 return true;
1196}
1197
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001198bool Texture::subImageCompressed(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels, Image *image)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001199{
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001200 if (width + xoffset > image->width || height + yoffset > image->height)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001201 {
1202 error(GL_INVALID_VALUE);
1203 return false;
1204 }
1205
1206 if (format != getInternalFormat())
1207 {
1208 error(GL_INVALID_OPERATION);
1209 return false;
1210 }
1211
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001212 if (!image->surface)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001213 {
daniel@transgaming.com61208202011-03-21 16:38:50 +00001214 createSurface(image);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001215 }
1216
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001217 if (pixels != NULL && image->surface != NULL)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001218 {
1219 RECT updateRegion;
1220 updateRegion.left = xoffset;
1221 updateRegion.right = xoffset + width;
1222 updateRegion.bottom = yoffset + height;
1223 updateRegion.top = yoffset;
1224
1225 D3DLOCKED_RECT locked;
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001226 HRESULT result = image->surface->LockRect(&locked, &updateRegion, 0);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001227
1228 ASSERT(SUCCEEDED(result));
1229
1230 if (SUCCEEDED(result))
1231 {
1232 int inputPitch = ComputeCompressedPitch(width, format);
1233 int inputSize = ComputeCompressedSize(width, height, format);
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001234 loadCompressedImageData(xoffset, transformPixelYOffset(yoffset, height, image->height), width, height, -inputPitch, static_cast<const char*>(pixels) + inputSize - inputPitch, locked.Pitch, locked.pBits);
1235 image->surface->UnlockRect();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001236 }
1237
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001238 image->dirty = true;
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00001239 mDirtyImage = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001240 }
1241
1242 return true;
1243}
1244
daniel@transgaming.comb6276992011-03-29 00:58:18 +00001245// This implements glCopyTex[Sub]Image2D for non-renderable internal texture formats and incomplete textures
1246void Texture::copyToImage(Image *image, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, IDirect3DSurface9 *renderTarget)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001247{
daniel@transgaming.comb6276992011-03-29 00:58:18 +00001248 if (!image->surface)
1249 {
1250 createSurface(image);
1251
1252 if (!image->surface)
1253 {
1254 ERR("Failed to create an image surface.");
1255 return error(GL_OUT_OF_MEMORY);
1256 }
1257 }
1258
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001259 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.comb6276992011-03-29 00:58:18 +00001260 IDirect3DSurface9 *renderTargetData = NULL;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001261 D3DSURFACE_DESC description;
1262 renderTarget->GetDesc(&description);
1263
daniel@transgaming.comb6276992011-03-29 00:58:18 +00001264 HRESULT result = device->CreateOffscreenPlainSurface(description.Width, description.Height, description.Format, D3DPOOL_SYSTEMMEM, &renderTargetData, NULL);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001265
daniel@transgaming.comb6276992011-03-29 00:58:18 +00001266 if (FAILED(result))
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001267 {
1268 ERR("Could not create matching destination surface.");
1269 return error(GL_OUT_OF_MEMORY);
1270 }
1271
daniel@transgaming.comb6276992011-03-29 00:58:18 +00001272 result = device->GetRenderTargetData(renderTarget, renderTargetData);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001273
daniel@transgaming.comb6276992011-03-29 00:58:18 +00001274 if (FAILED(result))
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001275 {
1276 ERR("GetRenderTargetData unexpectedly failed.");
daniel@transgaming.comb6276992011-03-29 00:58:18 +00001277 renderTargetData->Release();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001278 return error(GL_OUT_OF_MEMORY);
1279 }
1280
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001281 RECT sourceRect = transformPixelRect(x, y, width, height, description.Height);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001282 int destYOffset = transformPixelYOffset(yoffset, height, image->height);
1283 RECT destRect = {xoffset, destYOffset, xoffset + width, destYOffset + height};
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001284
daniel@transgaming.comb6276992011-03-29 00:58:18 +00001285 if (image->isRenderable())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001286 {
daniel@transgaming.comb6276992011-03-29 00:58:18 +00001287 result = D3DXLoadSurfaceFromSurface(image->surface, NULL, &destRect, renderTargetData, NULL, &sourceRect, D3DX_FILTER_BOX, 0);
1288
1289 if (FAILED(result))
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001290 {
daniel@transgaming.comb6276992011-03-29 00:58:18 +00001291 ERR("Copying surfaces unexpectedly failed.");
1292 renderTargetData->Release();
1293 return error(GL_OUT_OF_MEMORY);
1294 }
1295 }
1296 else
1297 {
1298 D3DLOCKED_RECT sourceLock = {0};
1299 result = renderTargetData->LockRect(&sourceLock, &sourceRect, 0);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001300
daniel@transgaming.comb6276992011-03-29 00:58:18 +00001301 if (FAILED(result))
1302 {
1303 ERR("Failed to lock the source surface (rectangle might be invalid).");
1304 renderTargetData->Release();
1305 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001306 }
1307
daniel@transgaming.comb6276992011-03-29 00:58:18 +00001308 D3DLOCKED_RECT destLock = {0};
1309 result = image->surface->LockRect(&destLock, &destRect, 0);
1310
1311 if (FAILED(result))
1312 {
1313 ERR("Failed to lock the destination surface (rectangle might be invalid).");
1314 renderTargetData->UnlockRect();
1315 renderTargetData->Release();
1316 return error(GL_OUT_OF_MEMORY);
1317 }
1318
1319 if (destLock.pBits && sourceLock.pBits)
1320 {
1321 unsigned char *source = (unsigned char*)sourceLock.pBits;
1322 unsigned char *dest = (unsigned char*)destLock.pBits;
1323
1324 switch (description.Format)
1325 {
1326 case D3DFMT_X8R8G8B8:
1327 case D3DFMT_A8R8G8B8:
1328 switch(image->getD3DFormat())
1329 {
1330 case D3DFMT_L8:
1331 for(int y = 0; y < height; y++)
1332 {
1333 for(int x = 0; x < width; x++)
1334 {
1335 dest[x] = source[x * 4 + 2];
1336 }
1337
1338 source += sourceLock.Pitch;
1339 dest += destLock.Pitch;
1340 }
1341 break;
1342 case D3DFMT_A8L8:
1343 for(int y = 0; y < height; y++)
1344 {
1345 for(int x = 0; x < width; x++)
1346 {
1347 dest[x * 2 + 0] = source[x * 4 + 2];
1348 dest[x * 2 + 1] = source[x * 4 + 3];
1349 }
1350
1351 source += sourceLock.Pitch;
1352 dest += destLock.Pitch;
1353 }
1354 break;
1355 default:
1356 UNREACHABLE();
1357 }
1358 break;
1359 case D3DFMT_R5G6B5:
1360 switch(image->getD3DFormat())
1361 {
1362 case D3DFMT_L8:
1363 for(int y = 0; y < height; y++)
1364 {
1365 for(int x = 0; x < width; x++)
1366 {
1367 unsigned char red = source[x * 2 + 1] & 0xF8;
1368 dest[x] = red | (red >> 5);
1369 }
1370
1371 source += sourceLock.Pitch;
1372 dest += destLock.Pitch;
1373 }
1374 break;
1375 default:
1376 UNREACHABLE();
1377 }
1378 break;
1379 case D3DFMT_A1R5G5B5:
1380 switch(image->getD3DFormat())
1381 {
1382 case D3DFMT_L8:
1383 for(int y = 0; y < height; y++)
1384 {
1385 for(int x = 0; x < width; x++)
1386 {
1387 unsigned char red = source[x * 2 + 1] & 0x7C;
1388 dest[x] = (red << 1) | (red >> 4);
1389 }
1390
1391 source += sourceLock.Pitch;
1392 dest += destLock.Pitch;
1393 }
1394 break;
1395 case D3DFMT_A8L8:
1396 for(int y = 0; y < height; y++)
1397 {
1398 for(int x = 0; x < width; x++)
1399 {
1400 unsigned char red = source[x * 2 + 1] & 0x7C;
1401 dest[x * 2 + 0] = (red << 1) | (red >> 4);
1402 dest[x * 2 + 1] = (signed char)source[x * 2 + 1] >> 7;
1403 }
1404
1405 source += sourceLock.Pitch;
1406 dest += destLock.Pitch;
1407 }
1408 break;
1409 default:
1410 UNREACHABLE();
1411 }
1412 break;
1413 default:
1414 UNREACHABLE();
1415 }
1416 }
1417
1418 image->surface->UnlockRect();
1419 renderTargetData->UnlockRect();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001420 }
1421
daniel@transgaming.comb6276992011-03-29 00:58:18 +00001422 renderTargetData->Release();
1423
1424 image->dirty = true;
1425 mDirtyImage = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001426}
1427
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001428IDirect3DBaseTexture9 *Texture::getTexture()
1429{
1430 if (!isComplete())
1431 {
1432 return NULL;
1433 }
1434
daniel@transgaming.comaed18322011-03-21 16:38:13 +00001435 if (!getBaseTexture())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001436 {
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001437 createTexture();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001438 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001439
daniel@transgaming.comc50edcb2011-03-21 16:38:40 +00001440 updateTexture();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001441
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001442 return getBaseTexture();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001443}
1444
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00001445bool Texture::isDirtyParameter() const
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001446{
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00001447 return mDirtyParameter;
1448}
1449
1450bool Texture::isDirtyImage() const
1451{
1452 return mDirtyImage;
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00001453}
1454
1455void Texture::resetDirty()
1456{
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00001457 mDirtyParameter = false;
1458 mDirtyImage = false;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001459}
1460
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00001461unsigned int Texture::getSerial() const
1462{
1463 return mSerial;
1464}
1465
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001466GLint Texture::creationLevels(GLsizei width, GLsizei height, GLint maxlevel) const
1467{
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00001468 if ((isPow2(width) && isPow2(height)) || getContext()->supportsNonPower2Texture())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001469 {
1470 return maxlevel;
1471 }
1472 else
1473 {
1474 // OpenGL ES 2.0 without GL_OES_texture_npot does not permit NPOT mipmaps.
1475 return 1;
1476 }
1477}
1478
1479GLint Texture::creationLevels(GLsizei size, GLint maxlevel) const
1480{
1481 return creationLevels(size, size, maxlevel);
1482}
1483
1484int Texture::levelCount() const
1485{
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001486 return getBaseTexture() ? getBaseTexture()->GetLevelCount() : 0;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001487}
1488
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00001489unsigned int Texture::issueSerial()
1490{
1491 return mCurrentSerial++;
1492}
1493
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001494Texture2D::Texture2D(GLuint id) : Texture(id)
1495{
1496 mTexture = NULL;
jbauman@chromium.orgae345802011-03-30 22:04:25 +00001497 mSurface = NULL;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001498}
1499
1500Texture2D::~Texture2D()
1501{
1502 mColorbufferProxy.set(NULL);
1503
1504 if (mTexture)
1505 {
1506 mTexture->Release();
1507 mTexture = NULL;
1508 }
jbauman@chromium.orgae345802011-03-30 22:04:25 +00001509
1510 if (mSurface)
1511 {
1512 mSurface->setBoundTexture(NULL);
1513 mSurface = NULL;
1514 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001515}
1516
1517GLenum Texture2D::getTarget() const
1518{
1519 return GL_TEXTURE_2D;
1520}
1521
daniel@transgaming.com61208202011-03-21 16:38:50 +00001522GLsizei Texture2D::getWidth() const
1523{
1524 return mImageArray[0].width;
1525}
1526
1527GLsizei Texture2D::getHeight() const
1528{
1529 return mImageArray[0].height;
1530}
1531
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001532GLenum Texture2D::getInternalFormat() const
1533{
1534 return mImageArray[0].format;
1535}
1536
daniel@transgaming.com61208202011-03-21 16:38:50 +00001537GLenum Texture2D::getType() const
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001538{
daniel@transgaming.com61208202011-03-21 16:38:50 +00001539 return mImageArray[0].type;
1540}
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001541
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00001542D3DFORMAT Texture2D::getD3DFormat() const
1543{
1544 return mImageArray[0].getD3DFormat();
1545}
1546
jbauman@chromium.orgae345802011-03-30 22:04:25 +00001547void Texture2D::redefineTexture(GLint level, GLenum format, GLsizei width, GLsizei height, GLenum type, bool forceRedefine)
daniel@transgaming.com61208202011-03-21 16:38:50 +00001548{
daniel@transgaming.com94a4f032011-03-21 16:38:55 +00001549 GLsizei textureWidth = mImageArray[0].width;
1550 GLsizei textureHeight = mImageArray[0].height;
1551 GLenum textureFormat = mImageArray[0].format;
1552 GLenum textureType = mImageArray[0].type;
1553
daniel@transgaming.com61208202011-03-21 16:38:50 +00001554 mImageArray[level].width = width;
1555 mImageArray[level].height = height;
1556 mImageArray[level].format = format;
1557 mImageArray[level].type = type;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001558
daniel@transgaming.com61208202011-03-21 16:38:50 +00001559 if (!mTexture)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001560 {
daniel@transgaming.com61208202011-03-21 16:38:50 +00001561 return;
1562 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001563
daniel@transgaming.com94a4f032011-03-21 16:38:55 +00001564 bool widthOkay = (textureWidth >> level == width) || (textureWidth >> level == 0 && width == 1);
1565 bool heightOkay = (textureHeight >> level == height) || (textureHeight >> level == 0 && height == 1);
1566 bool textureOkay = (widthOkay && heightOkay && textureFormat == format && textureType == type);
daniel@transgaming.com61208202011-03-21 16:38:50 +00001567
jbauman@chromium.orgae345802011-03-30 22:04:25 +00001568 if (!textureOkay || forceRedefine || mSurface) // Purge all the levels and the texture.
daniel@transgaming.com61208202011-03-21 16:38:50 +00001569 {
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001570 for (int i = 0; i < IMPLEMENTATION_MAX_TEXTURE_LEVELS; i++)
1571 {
1572 if (mImageArray[i].surface != NULL)
1573 {
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001574 mImageArray[i].surface->Release();
1575 mImageArray[i].surface = NULL;
daniel@transgaming.com61208202011-03-21 16:38:50 +00001576 mImageArray[i].dirty = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001577 }
1578 }
1579
1580 if (mTexture != NULL)
1581 {
1582 mTexture->Release();
1583 mTexture = NULL;
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00001584 mDirtyImage = true;
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001585 mIsRenderable = false;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001586 }
jbauman@chromium.orgae345802011-03-30 22:04:25 +00001587
1588 if (mSurface)
1589 {
1590 mSurface->setBoundTexture(NULL);
1591 mSurface = NULL;
1592 }
apatrick@chromium.org57a2cd62011-06-08 00:04:07 +00001593
1594 mColorbufferProxy.set(NULL);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001595 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001596}
1597
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00001598void Texture2D::setImage(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001599{
jbauman@chromium.orgae345802011-03-30 22:04:25 +00001600 redefineTexture(level, format, width, height, type, false);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001601
daniel@transgaming.com61208202011-03-21 16:38:50 +00001602 Texture::setImage(unpackAlignment, pixels, &mImageArray[level]);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001603}
1604
jbauman@chromium.orgae345802011-03-30 22:04:25 +00001605void Texture2D::bindTexImage(egl::Surface *surface)
1606{
1607 GLenum format;
1608
1609 switch(surface->getFormat())
1610 {
1611 case D3DFMT_A8R8G8B8:
1612 format = GL_RGBA;
1613 break;
1614 case D3DFMT_X8R8G8B8:
1615 format = GL_RGB;
1616 break;
1617 default:
1618 UNIMPLEMENTED();
1619 return;
1620 }
1621
1622 redefineTexture(0, format, surface->getWidth(), surface->getHeight(), GL_UNSIGNED_BYTE, true);
1623
1624 IDirect3DTexture9 *texture = surface->getOffscreenTexture();
1625
1626 mTexture = texture;
1627 mDirtyImage = true;
1628 mIsRenderable = true;
1629 mSurface = surface;
1630 mSurface->setBoundTexture(this);
1631}
1632
1633void Texture2D::releaseTexImage()
1634{
1635 redefineTexture(0, GL_RGB, 0, 0, GL_UNSIGNED_BYTE, true);
1636}
1637
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00001638void Texture2D::setCompressedImage(GLint level, GLenum format, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001639{
jbauman@chromium.orgae345802011-03-30 22:04:25 +00001640 redefineTexture(level, format, width, height, GL_UNSIGNED_BYTE, false);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001641
daniel@transgaming.com61208202011-03-21 16:38:50 +00001642 Texture::setCompressedImage(imageSize, pixels, &mImageArray[level]);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001643}
1644
1645void Texture2D::commitRect(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height)
1646{
1647 ASSERT(mImageArray[level].surface != NULL);
1648
1649 if (level < levelCount())
1650 {
1651 IDirect3DSurface9 *destLevel = NULL;
1652 HRESULT result = mTexture->GetSurfaceLevel(level, &destLevel);
1653
1654 ASSERT(SUCCEEDED(result));
1655
1656 if (SUCCEEDED(result))
1657 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001658 Image *image = &mImageArray[level];
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001659
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001660 RECT sourceRect = transformPixelRect(xoffset, yoffset, width, height, image->height);;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001661
1662 POINT destPoint;
1663 destPoint.x = sourceRect.left;
1664 destPoint.y = sourceRect.top;
1665
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001666 result = getDevice()->UpdateSurface(image->surface, &sourceRect, destLevel, &destPoint);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001667 ASSERT(SUCCEEDED(result));
1668
1669 destLevel->Release();
1670
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001671 image->dirty = false;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001672 }
1673 }
1674}
1675
1676void Texture2D::subImage(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels)
1677{
1678 if (Texture::subImage(xoffset, yoffset, width, height, format, type, unpackAlignment, pixels, &mImageArray[level]))
1679 {
1680 commitRect(level, xoffset, yoffset, width, height);
1681 }
1682}
1683
1684void Texture2D::subImageCompressed(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels)
1685{
1686 if (Texture::subImageCompressed(xoffset, yoffset, width, height, format, imageSize, pixels, &mImageArray[level]))
1687 {
1688 commitRect(level, xoffset, yoffset, width, height);
1689 }
1690}
1691
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00001692void Texture2D::copyImage(GLint level, GLenum format, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001693{
1694 IDirect3DSurface9 *renderTarget = source->getRenderTarget();
1695
1696 if (!renderTarget)
1697 {
1698 ERR("Failed to retrieve the render target.");
1699 return error(GL_OUT_OF_MEMORY);
1700 }
1701
jbauman@chromium.orgae345802011-03-30 22:04:25 +00001702 redefineTexture(level, format, width, height, GL_UNSIGNED_BYTE, false);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001703
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00001704 if (!mImageArray[level].isRenderable())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001705 {
daniel@transgaming.comb6276992011-03-29 00:58:18 +00001706 copyToImage(&mImageArray[level], 0, 0, x, y, width, height, renderTarget);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001707 }
1708 else
1709 {
daniel@transgaming.com61208202011-03-21 16:38:50 +00001710 if (!mTexture || !mIsRenderable)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001711 {
1712 convertToRenderTarget();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001713 }
daniel@transgaming.com3b3c1d42011-06-08 20:38:09 +00001714
1715 updateTexture();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001716
1717 if (width != 0 && height != 0 && level < levelCount())
1718 {
1719 RECT sourceRect = transformPixelRect(x, y, width, height, source->getColorbuffer()->getHeight());
1720 sourceRect.left = clamp(sourceRect.left, 0, source->getColorbuffer()->getWidth());
1721 sourceRect.top = clamp(sourceRect.top, 0, source->getColorbuffer()->getHeight());
1722 sourceRect.right = clamp(sourceRect.right, 0, source->getColorbuffer()->getWidth());
1723 sourceRect.bottom = clamp(sourceRect.bottom, 0, source->getColorbuffer()->getHeight());
daniel@transgaming.comeef864a2011-04-22 11:33:27 +00001724
1725 GLint destYOffset = transformPixelYOffset(0, height, mImageArray[level].height);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001726
1727 IDirect3DSurface9 *dest;
1728 HRESULT hr = mTexture->GetSurfaceLevel(level, &dest);
1729
daniel@transgaming.comeef864a2011-04-22 11:33:27 +00001730 getBlitter()->copy(source->getRenderTarget(), sourceRect, format, 0, destYOffset, dest);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001731 dest->Release();
1732 }
1733 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001734}
1735
1736void Texture2D::copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source)
1737{
1738 if (xoffset + width > mImageArray[level].width || yoffset + height > mImageArray[level].height)
1739 {
1740 return error(GL_INVALID_VALUE);
1741 }
1742
1743 IDirect3DSurface9 *renderTarget = source->getRenderTarget();
1744
1745 if (!renderTarget)
1746 {
1747 ERR("Failed to retrieve the render target.");
1748 return error(GL_OUT_OF_MEMORY);
1749 }
1750
jbauman@chromium.orgae345802011-03-30 22:04:25 +00001751 redefineTexture(level, mImageArray[level].format, mImageArray[level].width, mImageArray[level].height, GL_UNSIGNED_BYTE, false);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001752
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00001753 if (!mImageArray[level].isRenderable() || (!mTexture && !isComplete()))
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001754 {
daniel@transgaming.comb6276992011-03-29 00:58:18 +00001755 copyToImage(&mImageArray[level], xoffset, yoffset, x, y, width, height, renderTarget);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001756 }
1757 else
1758 {
daniel@transgaming.com61208202011-03-21 16:38:50 +00001759 if (!mTexture || !mIsRenderable)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001760 {
1761 convertToRenderTarget();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001762 }
daniel@transgaming.com61208202011-03-21 16:38:50 +00001763
1764 updateTexture();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001765
1766 if (level < levelCount())
1767 {
1768 RECT sourceRect = transformPixelRect(x, y, width, height, source->getColorbuffer()->getHeight());
1769 sourceRect.left = clamp(sourceRect.left, 0, source->getColorbuffer()->getWidth());
1770 sourceRect.top = clamp(sourceRect.top, 0, source->getColorbuffer()->getHeight());
1771 sourceRect.right = clamp(sourceRect.right, 0, source->getColorbuffer()->getWidth());
1772 sourceRect.bottom = clamp(sourceRect.bottom, 0, source->getColorbuffer()->getHeight());
1773
1774 GLint destYOffset = transformPixelYOffset(yoffset, height, mImageArray[level].height);
1775
1776 IDirect3DSurface9 *dest;
1777 HRESULT hr = mTexture->GetSurfaceLevel(level, &dest);
1778
daniel@transgaming.comeef864a2011-04-22 11:33:27 +00001779 getBlitter()->copy(source->getRenderTarget(), sourceRect, mImageArray[0].format, xoffset, destYOffset, dest);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001780 dest->Release();
1781 }
1782 }
1783}
1784
1785// Tests for GL texture object completeness. [OpenGL ES 2.0.24] section 3.7.10 page 81.
1786bool Texture2D::isComplete() const
1787{
1788 GLsizei width = mImageArray[0].width;
1789 GLsizei height = mImageArray[0].height;
1790
1791 if (width <= 0 || height <= 0)
1792 {
1793 return false;
1794 }
1795
1796 bool mipmapping = false;
1797
1798 switch (mMinFilter)
1799 {
1800 case GL_NEAREST:
1801 case GL_LINEAR:
1802 mipmapping = false;
1803 break;
1804 case GL_NEAREST_MIPMAP_NEAREST:
1805 case GL_LINEAR_MIPMAP_NEAREST:
1806 case GL_NEAREST_MIPMAP_LINEAR:
1807 case GL_LINEAR_MIPMAP_LINEAR:
1808 mipmapping = true;
1809 break;
1810 default: UNREACHABLE();
1811 }
1812
1813 if ((getInternalFormat() == GL_FLOAT && !getContext()->supportsFloatLinearFilter()) ||
1814 (getInternalFormat() == GL_HALF_FLOAT_OES && !getContext()->supportsHalfFloatLinearFilter()))
1815 {
1816 if (mMagFilter != GL_NEAREST || (mMinFilter != GL_NEAREST && mMinFilter != GL_NEAREST_MIPMAP_NEAREST))
1817 {
1818 return false;
1819 }
1820 }
1821
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00001822 bool npot = getContext()->supportsNonPower2Texture();
1823
1824 if (!npot)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001825 {
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00001826 if ((getWrapS() != GL_CLAMP_TO_EDGE && !isPow2(width)) ||
1827 (getWrapT() != GL_CLAMP_TO_EDGE && !isPow2(height)))
1828 {
1829 return false;
1830 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001831 }
1832
1833 if (mipmapping)
1834 {
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00001835 if (!npot)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001836 {
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00001837 if (!isPow2(width) || !isPow2(height))
1838 {
1839 return false;
1840 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001841 }
1842
1843 int q = log2(std::max(width, height));
1844
1845 for (int level = 1; level <= q; level++)
1846 {
1847 if (mImageArray[level].format != mImageArray[0].format)
1848 {
1849 return false;
1850 }
1851
daniel@transgaming.com0bd22bc2011-03-21 16:38:26 +00001852 if (mImageArray[level].type != mImageArray[0].type)
1853 {
1854 return false;
1855 }
1856
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001857 if (mImageArray[level].width != std::max(1, width >> level))
1858 {
1859 return false;
1860 }
1861
1862 if (mImageArray[level].height != std::max(1, height >> level))
1863 {
1864 return false;
1865 }
1866 }
1867 }
1868
1869 return true;
1870}
1871
1872bool Texture2D::isCompressed() const
1873{
1874 return IsCompressed(getInternalFormat());
1875}
1876
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001877IDirect3DBaseTexture9 *Texture2D::getBaseTexture() const
1878{
1879 return mTexture;
1880}
1881
1882// Constructs a Direct3D 9 texture resource from the texture images
1883void Texture2D::createTexture()
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001884{
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001885 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00001886 D3DFORMAT format = mImageArray[0].getD3DFormat();
daniel@transgaming.com61208202011-03-21 16:38:50 +00001887 GLint levels = creationLevels(mImageArray[0].width, mImageArray[0].height, 0);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001888
daniel@transgaming.com61208202011-03-21 16:38:50 +00001889 IDirect3DTexture9 *texture = NULL;
1890 HRESULT result = device->CreateTexture(mImageArray[0].width, mImageArray[0].height, levels, 0, format, D3DPOOL_DEFAULT, &texture, NULL);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001891
1892 if (FAILED(result))
1893 {
1894 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001895 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001896 }
1897
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001898 if (mTexture)
1899 {
1900 mTexture->Release();
1901 }
1902
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001903 mTexture = texture;
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00001904 mDirtyImage = true;
daniel@transgaming.comaed18322011-03-21 16:38:13 +00001905 mIsRenderable = false;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001906}
1907
1908void Texture2D::updateTexture()
1909{
1910 IDirect3DDevice9 *device = getDevice();
1911
1912 int levels = levelCount();
1913
1914 for (int level = 0; level < levels; level++)
1915 {
daniel@transgaming.com61208202011-03-21 16:38:50 +00001916 if (mImageArray[level].surface && mImageArray[level].dirty)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001917 {
1918 IDirect3DSurface9 *levelSurface = NULL;
1919 HRESULT result = mTexture->GetSurfaceLevel(level, &levelSurface);
1920
1921 ASSERT(SUCCEEDED(result));
1922
1923 if (SUCCEEDED(result))
1924 {
1925 result = device->UpdateSurface(mImageArray[level].surface, NULL, levelSurface, NULL);
1926 ASSERT(SUCCEEDED(result));
1927
1928 levelSurface->Release();
1929
1930 mImageArray[level].dirty = false;
1931 }
1932 }
1933 }
1934}
1935
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001936void Texture2D::convertToRenderTarget()
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001937{
1938 IDirect3DTexture9 *texture = NULL;
1939
daniel@transgaming.com61208202011-03-21 16:38:50 +00001940 if (mImageArray[0].width != 0 && mImageArray[0].height != 0)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001941 {
1942 egl::Display *display = getDisplay();
1943 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00001944 D3DFORMAT format = mImageArray[0].getD3DFormat();
daniel@transgaming.com61208202011-03-21 16:38:50 +00001945 GLint levels = creationLevels(mImageArray[0].width, mImageArray[0].height, 0);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001946
daniel@transgaming.com61208202011-03-21 16:38:50 +00001947 HRESULT result = device->CreateTexture(mImageArray[0].width, mImageArray[0].height, levels, D3DUSAGE_RENDERTARGET, format, D3DPOOL_DEFAULT, &texture, NULL);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001948
1949 if (FAILED(result))
1950 {
1951 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001952 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001953 }
1954
1955 if (mTexture != NULL)
1956 {
1957 int levels = levelCount();
1958 for (int i = 0; i < levels; i++)
1959 {
1960 IDirect3DSurface9 *source;
1961 result = mTexture->GetSurfaceLevel(i, &source);
1962
1963 if (FAILED(result))
1964 {
1965 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
1966
1967 texture->Release();
1968
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001969 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001970 }
1971
1972 IDirect3DSurface9 *dest;
1973 result = texture->GetSurfaceLevel(i, &dest);
1974
1975 if (FAILED(result))
1976 {
1977 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
1978
1979 texture->Release();
1980 source->Release();
1981
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001982 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001983 }
1984
1985 display->endScene();
1986 result = device->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
1987
1988 if (FAILED(result))
1989 {
1990 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
1991
1992 texture->Release();
1993 source->Release();
1994 dest->Release();
1995
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001996 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001997 }
1998
1999 source->Release();
2000 dest->Release();
2001 }
2002 }
2003 }
2004
2005 if (mTexture != NULL)
2006 {
2007 mTexture->Release();
2008 }
2009
2010 mTexture = texture;
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002011 mDirtyImage = true;
daniel@transgaming.comaed18322011-03-21 16:38:13 +00002012 mIsRenderable = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002013}
2014
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002015void Texture2D::generateMipmaps()
2016{
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00002017 if (!getContext()->supportsNonPower2Texture())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002018 {
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00002019 if (!isPow2(mImageArray[0].width) || !isPow2(mImageArray[0].height))
2020 {
2021 return error(GL_INVALID_OPERATION);
2022 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002023 }
2024
2025 // Purge array levels 1 through q and reset them to represent the generated mipmap levels.
daniel@transgaming.com61208202011-03-21 16:38:50 +00002026 unsigned int q = log2(std::max(mImageArray[0].width, mImageArray[0].height));
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002027 for (unsigned int i = 1; i <= q; i++)
2028 {
2029 if (mImageArray[i].surface != NULL)
2030 {
2031 mImageArray[i].surface->Release();
2032 mImageArray[i].surface = NULL;
2033 }
2034
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002035 mImageArray[i].width = std::max(mImageArray[0].width >> i, 1);
2036 mImageArray[i].height = std::max(mImageArray[0].height >> i, 1);
daniel@transgaming.com0bd22bc2011-03-21 16:38:26 +00002037 mImageArray[i].format = mImageArray[0].format;
2038 mImageArray[i].type = mImageArray[0].type;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002039 }
2040
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002041 if (mIsRenderable)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002042 {
2043 if (mTexture == NULL)
2044 {
2045 ERR(" failed because mTexture was null.");
2046 return;
2047 }
2048
2049 for (unsigned int i = 1; i <= q; i++)
2050 {
2051 IDirect3DSurface9 *upper = NULL;
2052 IDirect3DSurface9 *lower = NULL;
2053
2054 mTexture->GetSurfaceLevel(i-1, &upper);
2055 mTexture->GetSurfaceLevel(i, &lower);
2056
2057 if (upper != NULL && lower != NULL)
2058 {
2059 getBlitter()->boxFilter(upper, lower);
2060 }
2061
2062 if (upper != NULL) upper->Release();
2063 if (lower != NULL) lower->Release();
daniel@transgaming.com61208202011-03-21 16:38:50 +00002064
2065 mImageArray[i].dirty = false;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002066 }
2067 }
2068 else
2069 {
2070 for (unsigned int i = 1; i <= q; i++)
2071 {
daniel@transgaming.com61208202011-03-21 16:38:50 +00002072 createSurface(&mImageArray[i]);
2073
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002074 if (mImageArray[i].surface == NULL)
2075 {
2076 return error(GL_OUT_OF_MEMORY);
2077 }
2078
2079 if (FAILED(D3DXLoadSurfaceFromSurface(mImageArray[i].surface, NULL, NULL, mImageArray[i - 1].surface, NULL, NULL, D3DX_FILTER_BOX, 0)))
2080 {
2081 ERR(" failed to load filter %d to %d.", i - 1, i);
2082 }
2083
2084 mImageArray[i].dirty = true;
2085 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002086 }
2087}
2088
2089Renderbuffer *Texture2D::getRenderbuffer(GLenum target)
2090{
2091 if (target != GL_TEXTURE_2D)
2092 {
2093 return error(GL_INVALID_OPERATION, (Renderbuffer *)NULL);
2094 }
2095
2096 if (mColorbufferProxy.get() == NULL)
2097 {
2098 mColorbufferProxy.set(new Renderbuffer(id(), new Colorbuffer(this, target)));
2099 }
2100
2101 return mColorbufferProxy.get();
2102}
2103
2104IDirect3DSurface9 *Texture2D::getRenderTarget(GLenum target)
2105{
2106 ASSERT(target == GL_TEXTURE_2D);
2107
daniel@transgaming.com61208202011-03-21 16:38:50 +00002108 if (!mIsRenderable)
2109 {
2110 convertToRenderTarget();
2111 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002112
2113 if (mTexture == NULL)
2114 {
2115 return NULL;
2116 }
daniel@transgaming.com61208202011-03-21 16:38:50 +00002117
2118 updateTexture();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002119
2120 IDirect3DSurface9 *renderTarget = NULL;
2121 mTexture->GetSurfaceLevel(0, &renderTarget);
2122
2123 return renderTarget;
2124}
2125
2126TextureCubeMap::TextureCubeMap(GLuint id) : Texture(id)
2127{
2128 mTexture = NULL;
2129}
2130
2131TextureCubeMap::~TextureCubeMap()
2132{
2133 for (int i = 0; i < 6; i++)
2134 {
2135 mFaceProxies[i].set(NULL);
2136 }
2137
2138 if (mTexture)
2139 {
2140 mTexture->Release();
2141 mTexture = NULL;
2142 }
2143}
2144
2145GLenum TextureCubeMap::getTarget() const
2146{
2147 return GL_TEXTURE_CUBE_MAP;
2148}
2149
daniel@transgaming.com61208202011-03-21 16:38:50 +00002150GLsizei TextureCubeMap::getWidth() const
2151{
2152 return mImageArray[0][0].width;
2153}
2154
2155GLsizei TextureCubeMap::getHeight() const
2156{
2157 return mImageArray[0][0].height;
2158}
2159
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002160GLenum TextureCubeMap::getInternalFormat() const
2161{
2162 return mImageArray[0][0].format;
2163}
2164
daniel@transgaming.com61208202011-03-21 16:38:50 +00002165GLenum TextureCubeMap::getType() const
2166{
2167 return mImageArray[0][0].type;
2168}
2169
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00002170D3DFORMAT TextureCubeMap::getD3DFormat() const
2171{
2172 return mImageArray[0][0].getD3DFormat();
2173}
2174
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002175void TextureCubeMap::setImagePosX(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002176{
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002177 setImage(0, level, width, height, format, type, unpackAlignment, pixels);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002178}
2179
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002180void TextureCubeMap::setImageNegX(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002181{
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002182 setImage(1, level, width, height, format, type, unpackAlignment, pixels);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002183}
2184
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002185void TextureCubeMap::setImagePosY(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002186{
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002187 setImage(2, level, width, height, format, type, unpackAlignment, pixels);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002188}
2189
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002190void TextureCubeMap::setImageNegY(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002191{
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002192 setImage(3, level, width, height, format, type, unpackAlignment, pixels);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002193}
2194
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002195void TextureCubeMap::setImagePosZ(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002196{
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002197 setImage(4, level, width, height, format, type, unpackAlignment, pixels);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002198}
2199
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002200void TextureCubeMap::setImageNegZ(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002201{
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002202 setImage(5, level, width, height, format, type, unpackAlignment, pixels);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002203}
2204
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002205void TextureCubeMap::setCompressedImage(GLenum face, GLint level, GLenum format, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002206{
daniel@transgaming.com61208202011-03-21 16:38:50 +00002207 redefineTexture(faceIndex(face), level, format, width, height, GL_UNSIGNED_BYTE);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002208
daniel@transgaming.com61208202011-03-21 16:38:50 +00002209 Texture::setCompressedImage(imageSize, pixels, &mImageArray[faceIndex(face)][level]);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002210}
2211
2212void TextureCubeMap::commitRect(GLenum faceTarget, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height)
2213{
2214 int face = faceIndex(faceTarget);
2215 ASSERT(mImageArray[face][level].surface != NULL);
2216
2217 if (level < levelCount())
2218 {
2219 IDirect3DSurface9 *destLevel = getCubeMapSurface(faceTarget, level);
2220 ASSERT(destLevel != NULL);
2221
2222 if (destLevel != NULL)
2223 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002224 Image *image = &mImageArray[face][level];
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002225
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002226 RECT sourceRect = transformPixelRect(xoffset, yoffset, width, height, image->height);;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002227
2228 POINT destPoint;
2229 destPoint.x = sourceRect.left;
2230 destPoint.y = sourceRect.top;
2231
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002232 HRESULT result = getDevice()->UpdateSurface(image->surface, &sourceRect, destLevel, &destPoint);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002233 ASSERT(SUCCEEDED(result));
2234
2235 destLevel->Release();
2236
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002237 image->dirty = false;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002238 }
2239 }
2240}
2241
2242void TextureCubeMap::subImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels)
2243{
2244 if (Texture::subImage(xoffset, yoffset, width, height, format, type, unpackAlignment, pixels, &mImageArray[faceIndex(target)][level]))
2245 {
2246 commitRect(target, level, xoffset, yoffset, width, height);
2247 }
2248}
2249
2250void TextureCubeMap::subImageCompressed(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels)
2251{
2252 if (Texture::subImageCompressed(xoffset, yoffset, width, height, format, imageSize, pixels, &mImageArray[faceIndex(target)][level]))
2253 {
2254 commitRect(target, level, xoffset, yoffset, width, height);
2255 }
2256}
2257
2258// Tests for GL texture object completeness. [OpenGL ES 2.0.24] section 3.7.10 page 81.
2259bool TextureCubeMap::isComplete() const
2260{
2261 int size = mImageArray[0][0].width;
2262
2263 if (size <= 0)
2264 {
2265 return false;
2266 }
2267
2268 bool mipmapping;
2269
2270 switch (mMinFilter)
2271 {
2272 case GL_NEAREST:
2273 case GL_LINEAR:
2274 mipmapping = false;
2275 break;
2276 case GL_NEAREST_MIPMAP_NEAREST:
2277 case GL_LINEAR_MIPMAP_NEAREST:
2278 case GL_NEAREST_MIPMAP_LINEAR:
2279 case GL_LINEAR_MIPMAP_LINEAR:
2280 mipmapping = true;
2281 break;
2282 default: UNREACHABLE();
2283 }
2284
2285 for (int face = 0; face < 6; face++)
2286 {
2287 if (mImageArray[face][0].width != size || mImageArray[face][0].height != size)
2288 {
2289 return false;
2290 }
2291 }
2292
2293 if ((getInternalFormat() == GL_FLOAT && !getContext()->supportsFloatLinearFilter()) ||
2294 (getInternalFormat() == GL_HALF_FLOAT_OES && !getContext()->supportsHalfFloatLinearFilter()))
2295 {
2296 if (mMagFilter != GL_NEAREST || (mMinFilter != GL_NEAREST && mMinFilter != GL_NEAREST_MIPMAP_NEAREST))
2297 {
2298 return false;
2299 }
2300 }
2301
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00002302 bool npot = getContext()->supportsNonPower2Texture();
2303
2304 if (!npot)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002305 {
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00002306 if ((getWrapS() != GL_CLAMP_TO_EDGE || getWrapT() != GL_CLAMP_TO_EDGE) && !isPow2(size))
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002307 {
2308 return false;
2309 }
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00002310 }
2311
2312 if (mipmapping)
2313 {
2314 if (!npot)
2315 {
2316 if (!isPow2(size))
2317 {
2318 return false;
2319 }
2320 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002321
2322 int q = log2(size);
2323
2324 for (int face = 0; face < 6; face++)
2325 {
2326 for (int level = 1; level <= q; level++)
2327 {
2328 if (mImageArray[face][level].format != mImageArray[0][0].format)
2329 {
2330 return false;
2331 }
2332
daniel@transgaming.com0bd22bc2011-03-21 16:38:26 +00002333 if (mImageArray[face][level].type != mImageArray[0][0].type)
2334 {
2335 return false;
2336 }
2337
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002338 if (mImageArray[face][level].width != std::max(1, size >> level))
2339 {
2340 return false;
2341 }
2342
2343 ASSERT(mImageArray[face][level].height == mImageArray[face][level].width);
2344 }
2345 }
2346 }
2347
2348 return true;
2349}
2350
2351bool TextureCubeMap::isCompressed() const
2352{
2353 return IsCompressed(getInternalFormat());
2354}
2355
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002356IDirect3DBaseTexture9 *TextureCubeMap::getBaseTexture() const
2357{
2358 return mTexture;
2359}
2360
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002361// Constructs a Direct3D 9 texture resource from the texture images, or returns an existing one
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002362void TextureCubeMap::createTexture()
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002363{
2364 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00002365 D3DFORMAT format = mImageArray[0][0].getD3DFormat();
daniel@transgaming.com61208202011-03-21 16:38:50 +00002366 GLint levels = creationLevels(mImageArray[0][0].width, 0);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002367
daniel@transgaming.com0bd22bc2011-03-21 16:38:26 +00002368 IDirect3DCubeTexture9 *texture = NULL;
daniel@transgaming.com61208202011-03-21 16:38:50 +00002369 HRESULT result = device->CreateCubeTexture(mImageArray[0][0].width, levels, 0, format, D3DPOOL_DEFAULT, &texture, NULL);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002370
2371 if (FAILED(result))
2372 {
2373 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002374 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002375 }
2376
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002377 if (mTexture)
2378 {
2379 mTexture->Release();
2380 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002381
2382 mTexture = texture;
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002383 mDirtyImage = true;
daniel@transgaming.comaed18322011-03-21 16:38:13 +00002384 mIsRenderable = false;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002385}
2386
2387void TextureCubeMap::updateTexture()
2388{
2389 IDirect3DDevice9 *device = getDevice();
2390
2391 for (int face = 0; face < 6; face++)
2392 {
2393 int levels = levelCount();
2394 for (int level = 0; level < levels; level++)
2395 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002396 Image *image = &mImageArray[face][level];
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002397
daniel@transgaming.com61208202011-03-21 16:38:50 +00002398 if (image->surface && image->dirty)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002399 {
2400 IDirect3DSurface9 *levelSurface = getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level);
2401 ASSERT(levelSurface != NULL);
2402
2403 if (levelSurface != NULL)
2404 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002405 HRESULT result = device->UpdateSurface(image->surface, NULL, levelSurface, NULL);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002406 ASSERT(SUCCEEDED(result));
2407
2408 levelSurface->Release();
2409
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002410 image->dirty = false;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002411 }
2412 }
2413 }
2414 }
2415}
2416
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002417void TextureCubeMap::convertToRenderTarget()
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002418{
2419 IDirect3DCubeTexture9 *texture = NULL;
2420
daniel@transgaming.com61208202011-03-21 16:38:50 +00002421 if (mImageArray[0][0].width != 0)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002422 {
2423 egl::Display *display = getDisplay();
2424 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00002425 D3DFORMAT format = mImageArray[0][0].getD3DFormat();
daniel@transgaming.com61208202011-03-21 16:38:50 +00002426 GLint levels = creationLevels(mImageArray[0][0].width, 0);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002427
daniel@transgaming.com61208202011-03-21 16:38:50 +00002428 HRESULT result = device->CreateCubeTexture(mImageArray[0][0].width, levels, D3DUSAGE_RENDERTARGET, format, D3DPOOL_DEFAULT, &texture, NULL);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002429
2430 if (FAILED(result))
2431 {
2432 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002433 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002434 }
2435
2436 if (mTexture != NULL)
2437 {
2438 int levels = levelCount();
2439 for (int f = 0; f < 6; f++)
2440 {
2441 for (int i = 0; i < levels; i++)
2442 {
2443 IDirect3DSurface9 *source;
2444 result = mTexture->GetCubeMapSurface(static_cast<D3DCUBEMAP_FACES>(f), i, &source);
2445
2446 if (FAILED(result))
2447 {
2448 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
2449
2450 texture->Release();
2451
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002452 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002453 }
2454
2455 IDirect3DSurface9 *dest;
2456 result = texture->GetCubeMapSurface(static_cast<D3DCUBEMAP_FACES>(f), i, &dest);
2457
2458 if (FAILED(result))
2459 {
2460 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
2461
2462 texture->Release();
2463 source->Release();
2464
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002465 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002466 }
2467
2468 display->endScene();
2469 result = device->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
2470
2471 if (FAILED(result))
2472 {
2473 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
2474
2475 texture->Release();
2476 source->Release();
2477 dest->Release();
2478
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002479 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002480 }
daniel@transgaming.coma1a86202011-08-09 13:41:08 +00002481
2482 source->Release();
2483 dest->Release();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002484 }
2485 }
2486 }
2487 }
2488
2489 if (mTexture != NULL)
2490 {
2491 mTexture->Release();
2492 }
2493
2494 mTexture = texture;
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002495 mDirtyImage = true;
daniel@transgaming.comaed18322011-03-21 16:38:13 +00002496 mIsRenderable = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002497}
2498
daniel@transgaming.com61208202011-03-21 16:38:50 +00002499void TextureCubeMap::setImage(int faceIndex, GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002500{
daniel@transgaming.com61208202011-03-21 16:38:50 +00002501 redefineTexture(faceIndex, level, format, width, height, type);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002502
daniel@transgaming.com61208202011-03-21 16:38:50 +00002503 Texture::setImage(unpackAlignment, pixels, &mImageArray[faceIndex][level]);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002504}
2505
2506unsigned int TextureCubeMap::faceIndex(GLenum face)
2507{
2508 META_ASSERT(GL_TEXTURE_CUBE_MAP_NEGATIVE_X - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 1);
2509 META_ASSERT(GL_TEXTURE_CUBE_MAP_POSITIVE_Y - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 2);
2510 META_ASSERT(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 3);
2511 META_ASSERT(GL_TEXTURE_CUBE_MAP_POSITIVE_Z - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 4);
2512 META_ASSERT(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 5);
2513
2514 return face - GL_TEXTURE_CUBE_MAP_POSITIVE_X;
2515}
2516
daniel@transgaming.com61208202011-03-21 16:38:50 +00002517void TextureCubeMap::redefineTexture(int face, GLint level, GLenum format, GLsizei width, GLsizei height, GLenum type)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002518{
daniel@transgaming.com94a4f032011-03-21 16:38:55 +00002519 GLsizei textureWidth = mImageArray[0][0].width;
2520 GLsizei textureHeight = mImageArray[0][0].height;
2521 GLenum textureFormat = mImageArray[0][0].format;
2522 GLenum textureType = mImageArray[0][0].type;
2523
daniel@transgaming.com61208202011-03-21 16:38:50 +00002524 mImageArray[face][level].width = width;
2525 mImageArray[face][level].height = height;
2526 mImageArray[face][level].format = format;
2527 mImageArray[face][level].type = type;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002528
daniel@transgaming.com61208202011-03-21 16:38:50 +00002529 if (!mTexture)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002530 {
daniel@transgaming.com61208202011-03-21 16:38:50 +00002531 return;
2532 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002533
daniel@transgaming.com94a4f032011-03-21 16:38:55 +00002534 bool sizeOkay = (textureWidth >> level == width);
2535 bool textureOkay = (sizeOkay && textureFormat == format && textureType == type);
daniel@transgaming.com61208202011-03-21 16:38:50 +00002536
2537 if (!textureOkay) // Purge all the levels and the texture.
2538 {
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002539 for (int i = 0; i < IMPLEMENTATION_MAX_TEXTURE_LEVELS; i++)
2540 {
2541 for (int f = 0; f < 6; f++)
2542 {
2543 if (mImageArray[f][i].surface != NULL)
2544 {
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002545 mImageArray[f][i].surface->Release();
2546 mImageArray[f][i].surface = NULL;
daniel@transgaming.com61208202011-03-21 16:38:50 +00002547 mImageArray[f][i].dirty = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002548 }
2549 }
2550 }
2551
2552 if (mTexture != NULL)
2553 {
2554 mTexture->Release();
2555 mTexture = NULL;
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002556 mDirtyImage = true;
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002557 mIsRenderable = false;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002558 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002559 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002560}
2561
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002562void TextureCubeMap::copyImage(GLenum target, GLint level, GLenum format, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002563{
2564 IDirect3DSurface9 *renderTarget = source->getRenderTarget();
2565
2566 if (!renderTarget)
2567 {
2568 ERR("Failed to retrieve the render target.");
2569 return error(GL_OUT_OF_MEMORY);
2570 }
2571
2572 unsigned int faceindex = faceIndex(target);
daniel@transgaming.com61208202011-03-21 16:38:50 +00002573 redefineTexture(faceindex, level, format, width, height, GL_UNSIGNED_BYTE);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002574
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00002575 if (!mImageArray[faceindex][level].isRenderable())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002576 {
daniel@transgaming.comb6276992011-03-29 00:58:18 +00002577 copyToImage(&mImageArray[faceindex][level], 0, 0, x, y, width, height, renderTarget);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002578 }
2579 else
2580 {
daniel@transgaming.com61208202011-03-21 16:38:50 +00002581 if (!mTexture || !mIsRenderable)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002582 {
2583 convertToRenderTarget();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002584 }
daniel@transgaming.com3b3c1d42011-06-08 20:38:09 +00002585
2586 updateTexture();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002587
2588 ASSERT(width == height);
2589
2590 if (width > 0 && level < levelCount())
2591 {
2592 RECT sourceRect = transformPixelRect(x, y, width, height, source->getColorbuffer()->getHeight());
2593 sourceRect.left = clamp(sourceRect.left, 0, source->getColorbuffer()->getWidth());
2594 sourceRect.top = clamp(sourceRect.top, 0, source->getColorbuffer()->getHeight());
2595 sourceRect.right = clamp(sourceRect.right, 0, source->getColorbuffer()->getWidth());
2596 sourceRect.bottom = clamp(sourceRect.bottom, 0, source->getColorbuffer()->getHeight());
2597
daniel@transgaming.comeef864a2011-04-22 11:33:27 +00002598 GLint destYOffset = transformPixelYOffset(0, height, mImageArray[faceindex][level].width);
2599
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002600 IDirect3DSurface9 *dest = getCubeMapSurface(target, level);
2601
daniel@transgaming.comeef864a2011-04-22 11:33:27 +00002602 getBlitter()->copy(source->getRenderTarget(), sourceRect, format, 0, destYOffset, dest);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002603 dest->Release();
2604 }
2605 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002606}
2607
2608IDirect3DSurface9 *TextureCubeMap::getCubeMapSurface(GLenum face, unsigned int level)
2609{
2610 if (mTexture == NULL)
2611 {
2612 UNREACHABLE();
2613 return NULL;
2614 }
2615
2616 IDirect3DSurface9 *surface = NULL;
2617
2618 HRESULT hr = mTexture->GetCubeMapSurface(es2dx::ConvertCubeFace(face), level, &surface);
2619
2620 return (SUCCEEDED(hr)) ? surface : NULL;
2621}
2622
2623void TextureCubeMap::copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source)
2624{
2625 GLsizei size = mImageArray[faceIndex(target)][level].width;
2626
2627 if (xoffset + width > size || yoffset + height > size)
2628 {
2629 return error(GL_INVALID_VALUE);
2630 }
2631
2632 IDirect3DSurface9 *renderTarget = source->getRenderTarget();
2633
2634 if (!renderTarget)
2635 {
2636 ERR("Failed to retrieve the render target.");
2637 return error(GL_OUT_OF_MEMORY);
2638 }
2639
2640 unsigned int faceindex = faceIndex(target);
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00002641 redefineTexture(faceindex, level, mImageArray[faceindex][level].format, mImageArray[faceindex][level].width, mImageArray[faceindex][level].height, GL_UNSIGNED_BYTE);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002642
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00002643 if (!mImageArray[faceindex][level].isRenderable() || (!mTexture && !isComplete()))
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002644 {
daniel@transgaming.comb6276992011-03-29 00:58:18 +00002645 copyToImage(&mImageArray[faceindex][level], 0, 0, x, y, width, height, renderTarget);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002646 }
2647 else
2648 {
daniel@transgaming.com61208202011-03-21 16:38:50 +00002649 if (!mTexture || !mIsRenderable)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002650 {
2651 convertToRenderTarget();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002652 }
daniel@transgaming.com61208202011-03-21 16:38:50 +00002653
2654 updateTexture();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002655
2656 if (level < levelCount())
2657 {
2658 RECT sourceRect = transformPixelRect(x, y, width, height, source->getColorbuffer()->getHeight());
2659 sourceRect.left = clamp(sourceRect.left, 0, source->getColorbuffer()->getWidth());
2660 sourceRect.top = clamp(sourceRect.top, 0, source->getColorbuffer()->getHeight());
2661 sourceRect.right = clamp(sourceRect.right, 0, source->getColorbuffer()->getWidth());
2662 sourceRect.bottom = clamp(sourceRect.bottom, 0, source->getColorbuffer()->getHeight());
2663
2664 GLint destYOffset = transformPixelYOffset(yoffset, height, mImageArray[faceindex][level].width);
2665
2666 IDirect3DSurface9 *dest = getCubeMapSurface(target, level);
2667
daniel@transgaming.comeef864a2011-04-22 11:33:27 +00002668 getBlitter()->copy(source->getRenderTarget(), sourceRect, mImageArray[0][0].format, xoffset, destYOffset, dest);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002669 dest->Release();
2670 }
2671 }
2672}
2673
2674bool TextureCubeMap::isCubeComplete() const
2675{
2676 if (mImageArray[0][0].width == 0)
2677 {
2678 return false;
2679 }
2680
2681 for (unsigned int f = 1; f < 6; f++)
2682 {
2683 if (mImageArray[f][0].width != mImageArray[0][0].width
2684 || mImageArray[f][0].format != mImageArray[0][0].format)
2685 {
2686 return false;
2687 }
2688 }
2689
2690 return true;
2691}
2692
2693void TextureCubeMap::generateMipmaps()
2694{
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00002695 if (!isCubeComplete())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002696 {
2697 return error(GL_INVALID_OPERATION);
2698 }
2699
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00002700 if (!getContext()->supportsNonPower2Texture())
2701 {
2702 if (!isPow2(mImageArray[0][0].width))
2703 {
2704 return error(GL_INVALID_OPERATION);
2705 }
2706 }
2707
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002708 // Purge array levels 1 through q and reset them to represent the generated mipmap levels.
2709 unsigned int q = log2(mImageArray[0][0].width);
2710 for (unsigned int f = 0; f < 6; f++)
2711 {
2712 for (unsigned int i = 1; i <= q; i++)
2713 {
2714 if (mImageArray[f][i].surface != NULL)
2715 {
2716 mImageArray[f][i].surface->Release();
2717 mImageArray[f][i].surface = NULL;
2718 }
2719
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002720 mImageArray[f][i].width = std::max(mImageArray[f][0].width >> i, 1);
2721 mImageArray[f][i].height = mImageArray[f][i].width;
daniel@transgaming.com0bd22bc2011-03-21 16:38:26 +00002722 mImageArray[f][i].format = mImageArray[f][0].format;
2723 mImageArray[f][i].type = mImageArray[f][0].type;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002724 }
2725 }
2726
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002727 if (mIsRenderable)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002728 {
2729 if (mTexture == NULL)
2730 {
2731 return;
2732 }
2733
2734 for (unsigned int f = 0; f < 6; f++)
2735 {
2736 for (unsigned int i = 1; i <= q; i++)
2737 {
2738 IDirect3DSurface9 *upper = getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i-1);
2739 IDirect3DSurface9 *lower = getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i);
2740
2741 if (upper != NULL && lower != NULL)
2742 {
2743 getBlitter()->boxFilter(upper, lower);
2744 }
2745
2746 if (upper != NULL) upper->Release();
2747 if (lower != NULL) lower->Release();
daniel@transgaming.com61208202011-03-21 16:38:50 +00002748
2749 mImageArray[f][i].dirty = false;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002750 }
2751 }
2752 }
2753 else
2754 {
2755 for (unsigned int f = 0; f < 6; f++)
2756 {
2757 for (unsigned int i = 1; i <= q; i++)
2758 {
daniel@transgaming.com61208202011-03-21 16:38:50 +00002759 createSurface(&mImageArray[f][i]);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002760 if (mImageArray[f][i].surface == NULL)
2761 {
2762 return error(GL_OUT_OF_MEMORY);
2763 }
2764
2765 if (FAILED(D3DXLoadSurfaceFromSurface(mImageArray[f][i].surface, NULL, NULL, mImageArray[f][i - 1].surface, NULL, NULL, D3DX_FILTER_BOX, 0)))
2766 {
2767 ERR(" failed to load filter %d to %d.", i - 1, i);
2768 }
2769
2770 mImageArray[f][i].dirty = true;
2771 }
2772 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002773 }
2774}
2775
2776Renderbuffer *TextureCubeMap::getRenderbuffer(GLenum target)
2777{
2778 if (!IsCubemapTextureTarget(target))
2779 {
2780 return error(GL_INVALID_OPERATION, (Renderbuffer *)NULL);
2781 }
2782
2783 unsigned int face = faceIndex(target);
2784
2785 if (mFaceProxies[face].get() == NULL)
2786 {
2787 mFaceProxies[face].set(new Renderbuffer(id(), new Colorbuffer(this, target)));
2788 }
2789
2790 return mFaceProxies[face].get();
2791}
2792
2793IDirect3DSurface9 *TextureCubeMap::getRenderTarget(GLenum target)
2794{
2795 ASSERT(IsCubemapTextureTarget(target));
2796
daniel@transgaming.com61208202011-03-21 16:38:50 +00002797 if (!mIsRenderable)
2798 {
2799 convertToRenderTarget();
2800 }
2801
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002802 if (mTexture == NULL)
2803 {
2804 return NULL;
2805 }
daniel@transgaming.com61208202011-03-21 16:38:50 +00002806
2807 updateTexture();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002808
2809 IDirect3DSurface9 *renderTarget = NULL;
2810 mTexture->GetCubeMapSurface(es2dx::ConvertCubeFace(target), 0, &renderTarget);
2811
2812 return renderTarget;
2813}
2814
2815}