blob: 9e8f454efda557a68758d7bccb39d07531156181 [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.comb612f882011-11-09 17:44:31 +00001661 POINT destPoint = {sourceRect.left, sourceRect.top};
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001662
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001663 result = getDevice()->UpdateSurface(image->surface, &sourceRect, destLevel, &destPoint);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001664 ASSERT(SUCCEEDED(result));
1665
1666 destLevel->Release();
1667
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001668 image->dirty = false;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001669 }
1670 }
1671}
1672
1673void Texture2D::subImage(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels)
1674{
1675 if (Texture::subImage(xoffset, yoffset, width, height, format, type, unpackAlignment, pixels, &mImageArray[level]))
1676 {
1677 commitRect(level, xoffset, yoffset, width, height);
1678 }
1679}
1680
1681void Texture2D::subImageCompressed(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels)
1682{
1683 if (Texture::subImageCompressed(xoffset, yoffset, width, height, format, imageSize, pixels, &mImageArray[level]))
1684 {
1685 commitRect(level, xoffset, yoffset, width, height);
1686 }
1687}
1688
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00001689void Texture2D::copyImage(GLint level, GLenum format, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001690{
1691 IDirect3DSurface9 *renderTarget = source->getRenderTarget();
1692
1693 if (!renderTarget)
1694 {
1695 ERR("Failed to retrieve the render target.");
1696 return error(GL_OUT_OF_MEMORY);
1697 }
1698
jbauman@chromium.orgae345802011-03-30 22:04:25 +00001699 redefineTexture(level, format, width, height, GL_UNSIGNED_BYTE, false);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001700
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00001701 if (!mImageArray[level].isRenderable())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001702 {
daniel@transgaming.comb6276992011-03-29 00:58:18 +00001703 copyToImage(&mImageArray[level], 0, 0, x, y, width, height, renderTarget);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001704 }
1705 else
1706 {
daniel@transgaming.com61208202011-03-21 16:38:50 +00001707 if (!mTexture || !mIsRenderable)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001708 {
1709 convertToRenderTarget();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001710 }
daniel@transgaming.com3b3c1d42011-06-08 20:38:09 +00001711
1712 updateTexture();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001713
1714 if (width != 0 && height != 0 && level < levelCount())
1715 {
1716 RECT sourceRect = transformPixelRect(x, y, width, height, source->getColorbuffer()->getHeight());
1717 sourceRect.left = clamp(sourceRect.left, 0, source->getColorbuffer()->getWidth());
1718 sourceRect.top = clamp(sourceRect.top, 0, source->getColorbuffer()->getHeight());
1719 sourceRect.right = clamp(sourceRect.right, 0, source->getColorbuffer()->getWidth());
1720 sourceRect.bottom = clamp(sourceRect.bottom, 0, source->getColorbuffer()->getHeight());
daniel@transgaming.comeef864a2011-04-22 11:33:27 +00001721
1722 GLint destYOffset = transformPixelYOffset(0, height, mImageArray[level].height);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001723
1724 IDirect3DSurface9 *dest;
1725 HRESULT hr = mTexture->GetSurfaceLevel(level, &dest);
1726
daniel@transgaming.comeef864a2011-04-22 11:33:27 +00001727 getBlitter()->copy(source->getRenderTarget(), sourceRect, format, 0, destYOffset, dest);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001728 dest->Release();
1729 }
1730 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001731}
1732
1733void Texture2D::copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source)
1734{
1735 if (xoffset + width > mImageArray[level].width || yoffset + height > mImageArray[level].height)
1736 {
1737 return error(GL_INVALID_VALUE);
1738 }
1739
1740 IDirect3DSurface9 *renderTarget = source->getRenderTarget();
1741
1742 if (!renderTarget)
1743 {
1744 ERR("Failed to retrieve the render target.");
1745 return error(GL_OUT_OF_MEMORY);
1746 }
1747
jbauman@chromium.orgae345802011-03-30 22:04:25 +00001748 redefineTexture(level, mImageArray[level].format, mImageArray[level].width, mImageArray[level].height, GL_UNSIGNED_BYTE, false);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001749
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00001750 if (!mImageArray[level].isRenderable() || (!mTexture && !isComplete()))
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001751 {
daniel@transgaming.comb6276992011-03-29 00:58:18 +00001752 copyToImage(&mImageArray[level], xoffset, yoffset, x, y, width, height, renderTarget);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001753 }
1754 else
1755 {
daniel@transgaming.com61208202011-03-21 16:38:50 +00001756 if (!mTexture || !mIsRenderable)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001757 {
1758 convertToRenderTarget();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001759 }
daniel@transgaming.com61208202011-03-21 16:38:50 +00001760
1761 updateTexture();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001762
1763 if (level < levelCount())
1764 {
1765 RECT sourceRect = transformPixelRect(x, y, width, height, source->getColorbuffer()->getHeight());
1766 sourceRect.left = clamp(sourceRect.left, 0, source->getColorbuffer()->getWidth());
1767 sourceRect.top = clamp(sourceRect.top, 0, source->getColorbuffer()->getHeight());
1768 sourceRect.right = clamp(sourceRect.right, 0, source->getColorbuffer()->getWidth());
1769 sourceRect.bottom = clamp(sourceRect.bottom, 0, source->getColorbuffer()->getHeight());
1770
1771 GLint destYOffset = transformPixelYOffset(yoffset, height, mImageArray[level].height);
1772
1773 IDirect3DSurface9 *dest;
1774 HRESULT hr = mTexture->GetSurfaceLevel(level, &dest);
1775
daniel@transgaming.comeef864a2011-04-22 11:33:27 +00001776 getBlitter()->copy(source->getRenderTarget(), sourceRect, mImageArray[0].format, xoffset, destYOffset, dest);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001777 dest->Release();
1778 }
1779 }
1780}
1781
1782// Tests for GL texture object completeness. [OpenGL ES 2.0.24] section 3.7.10 page 81.
1783bool Texture2D::isComplete() const
1784{
1785 GLsizei width = mImageArray[0].width;
1786 GLsizei height = mImageArray[0].height;
1787
1788 if (width <= 0 || height <= 0)
1789 {
1790 return false;
1791 }
1792
1793 bool mipmapping = false;
1794
1795 switch (mMinFilter)
1796 {
1797 case GL_NEAREST:
1798 case GL_LINEAR:
1799 mipmapping = false;
1800 break;
1801 case GL_NEAREST_MIPMAP_NEAREST:
1802 case GL_LINEAR_MIPMAP_NEAREST:
1803 case GL_NEAREST_MIPMAP_LINEAR:
1804 case GL_LINEAR_MIPMAP_LINEAR:
1805 mipmapping = true;
1806 break;
1807 default: UNREACHABLE();
1808 }
1809
1810 if ((getInternalFormat() == GL_FLOAT && !getContext()->supportsFloatLinearFilter()) ||
1811 (getInternalFormat() == GL_HALF_FLOAT_OES && !getContext()->supportsHalfFloatLinearFilter()))
1812 {
1813 if (mMagFilter != GL_NEAREST || (mMinFilter != GL_NEAREST && mMinFilter != GL_NEAREST_MIPMAP_NEAREST))
1814 {
1815 return false;
1816 }
1817 }
1818
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00001819 bool npot = getContext()->supportsNonPower2Texture();
1820
1821 if (!npot)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001822 {
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00001823 if ((getWrapS() != GL_CLAMP_TO_EDGE && !isPow2(width)) ||
1824 (getWrapT() != GL_CLAMP_TO_EDGE && !isPow2(height)))
1825 {
1826 return false;
1827 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001828 }
1829
1830 if (mipmapping)
1831 {
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00001832 if (!npot)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001833 {
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00001834 if (!isPow2(width) || !isPow2(height))
1835 {
1836 return false;
1837 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001838 }
1839
1840 int q = log2(std::max(width, height));
1841
1842 for (int level = 1; level <= q; level++)
1843 {
1844 if (mImageArray[level].format != mImageArray[0].format)
1845 {
1846 return false;
1847 }
1848
daniel@transgaming.com0bd22bc2011-03-21 16:38:26 +00001849 if (mImageArray[level].type != mImageArray[0].type)
1850 {
1851 return false;
1852 }
1853
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001854 if (mImageArray[level].width != std::max(1, width >> level))
1855 {
1856 return false;
1857 }
1858
1859 if (mImageArray[level].height != std::max(1, height >> level))
1860 {
1861 return false;
1862 }
1863 }
1864 }
1865
1866 return true;
1867}
1868
1869bool Texture2D::isCompressed() const
1870{
1871 return IsCompressed(getInternalFormat());
1872}
1873
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001874IDirect3DBaseTexture9 *Texture2D::getBaseTexture() const
1875{
1876 return mTexture;
1877}
1878
1879// Constructs a Direct3D 9 texture resource from the texture images
1880void Texture2D::createTexture()
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001881{
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001882 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00001883 D3DFORMAT format = mImageArray[0].getD3DFormat();
daniel@transgaming.com61208202011-03-21 16:38:50 +00001884 GLint levels = creationLevels(mImageArray[0].width, mImageArray[0].height, 0);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001885
daniel@transgaming.com61208202011-03-21 16:38:50 +00001886 IDirect3DTexture9 *texture = NULL;
1887 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 +00001888
1889 if (FAILED(result))
1890 {
1891 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001892 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001893 }
1894
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001895 if (mTexture)
1896 {
1897 mTexture->Release();
1898 }
1899
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001900 mTexture = texture;
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00001901 mDirtyImage = true;
daniel@transgaming.comaed18322011-03-21 16:38:13 +00001902 mIsRenderable = false;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001903}
1904
1905void Texture2D::updateTexture()
1906{
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001907 int levels = levelCount();
1908
1909 for (int level = 0; level < levels; level++)
1910 {
daniel@transgaming.comb612f882011-11-09 17:44:31 +00001911 Image *image = &mImageArray[level];
1912
1913 if (image->surface && image->dirty)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001914 {
daniel@transgaming.comb612f882011-11-09 17:44:31 +00001915 commitRect(level, 0, 0, mImageArray[level].width, mImageArray[level].height);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001916 }
1917 }
1918}
1919
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001920void Texture2D::convertToRenderTarget()
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001921{
1922 IDirect3DTexture9 *texture = NULL;
1923
daniel@transgaming.com61208202011-03-21 16:38:50 +00001924 if (mImageArray[0].width != 0 && mImageArray[0].height != 0)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001925 {
1926 egl::Display *display = getDisplay();
1927 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00001928 D3DFORMAT format = mImageArray[0].getD3DFormat();
daniel@transgaming.com61208202011-03-21 16:38:50 +00001929 GLint levels = creationLevels(mImageArray[0].width, mImageArray[0].height, 0);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001930
daniel@transgaming.com61208202011-03-21 16:38:50 +00001931 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 +00001932
1933 if (FAILED(result))
1934 {
1935 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001936 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001937 }
1938
1939 if (mTexture != NULL)
1940 {
1941 int levels = levelCount();
1942 for (int i = 0; i < levels; i++)
1943 {
1944 IDirect3DSurface9 *source;
1945 result = mTexture->GetSurfaceLevel(i, &source);
1946
1947 if (FAILED(result))
1948 {
1949 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
1950
1951 texture->Release();
1952
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001953 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001954 }
1955
1956 IDirect3DSurface9 *dest;
1957 result = texture->GetSurfaceLevel(i, &dest);
1958
1959 if (FAILED(result))
1960 {
1961 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
1962
1963 texture->Release();
1964 source->Release();
1965
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001966 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001967 }
1968
1969 display->endScene();
1970 result = device->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
1971
1972 if (FAILED(result))
1973 {
1974 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
1975
1976 texture->Release();
1977 source->Release();
1978 dest->Release();
1979
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001980 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001981 }
1982
1983 source->Release();
1984 dest->Release();
1985 }
1986 }
1987 }
1988
1989 if (mTexture != NULL)
1990 {
1991 mTexture->Release();
1992 }
1993
1994 mTexture = texture;
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00001995 mDirtyImage = true;
daniel@transgaming.comaed18322011-03-21 16:38:13 +00001996 mIsRenderable = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001997}
1998
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001999void Texture2D::generateMipmaps()
2000{
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00002001 if (!getContext()->supportsNonPower2Texture())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002002 {
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00002003 if (!isPow2(mImageArray[0].width) || !isPow2(mImageArray[0].height))
2004 {
2005 return error(GL_INVALID_OPERATION);
2006 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002007 }
2008
2009 // Purge array levels 1 through q and reset them to represent the generated mipmap levels.
daniel@transgaming.com61208202011-03-21 16:38:50 +00002010 unsigned int q = log2(std::max(mImageArray[0].width, mImageArray[0].height));
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002011 for (unsigned int i = 1; i <= q; i++)
2012 {
2013 if (mImageArray[i].surface != NULL)
2014 {
2015 mImageArray[i].surface->Release();
2016 mImageArray[i].surface = NULL;
2017 }
2018
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002019 mImageArray[i].width = std::max(mImageArray[0].width >> i, 1);
2020 mImageArray[i].height = std::max(mImageArray[0].height >> i, 1);
daniel@transgaming.com0bd22bc2011-03-21 16:38:26 +00002021 mImageArray[i].format = mImageArray[0].format;
2022 mImageArray[i].type = mImageArray[0].type;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002023 }
2024
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002025 if (mIsRenderable)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002026 {
2027 if (mTexture == NULL)
2028 {
2029 ERR(" failed because mTexture was null.");
2030 return;
2031 }
2032
2033 for (unsigned int i = 1; i <= q; i++)
2034 {
2035 IDirect3DSurface9 *upper = NULL;
2036 IDirect3DSurface9 *lower = NULL;
2037
2038 mTexture->GetSurfaceLevel(i-1, &upper);
2039 mTexture->GetSurfaceLevel(i, &lower);
2040
2041 if (upper != NULL && lower != NULL)
2042 {
2043 getBlitter()->boxFilter(upper, lower);
2044 }
2045
2046 if (upper != NULL) upper->Release();
2047 if (lower != NULL) lower->Release();
daniel@transgaming.com61208202011-03-21 16:38:50 +00002048
2049 mImageArray[i].dirty = false;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002050 }
2051 }
2052 else
2053 {
2054 for (unsigned int i = 1; i <= q; i++)
2055 {
daniel@transgaming.com61208202011-03-21 16:38:50 +00002056 createSurface(&mImageArray[i]);
2057
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002058 if (mImageArray[i].surface == NULL)
2059 {
2060 return error(GL_OUT_OF_MEMORY);
2061 }
2062
2063 if (FAILED(D3DXLoadSurfaceFromSurface(mImageArray[i].surface, NULL, NULL, mImageArray[i - 1].surface, NULL, NULL, D3DX_FILTER_BOX, 0)))
2064 {
2065 ERR(" failed to load filter %d to %d.", i - 1, i);
2066 }
2067
2068 mImageArray[i].dirty = true;
2069 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002070 }
2071}
2072
2073Renderbuffer *Texture2D::getRenderbuffer(GLenum target)
2074{
2075 if (target != GL_TEXTURE_2D)
2076 {
2077 return error(GL_INVALID_OPERATION, (Renderbuffer *)NULL);
2078 }
2079
2080 if (mColorbufferProxy.get() == NULL)
2081 {
2082 mColorbufferProxy.set(new Renderbuffer(id(), new Colorbuffer(this, target)));
2083 }
2084
2085 return mColorbufferProxy.get();
2086}
2087
2088IDirect3DSurface9 *Texture2D::getRenderTarget(GLenum target)
2089{
2090 ASSERT(target == GL_TEXTURE_2D);
2091
daniel@transgaming.com61208202011-03-21 16:38:50 +00002092 if (!mIsRenderable)
2093 {
2094 convertToRenderTarget();
2095 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002096
2097 if (mTexture == NULL)
2098 {
2099 return NULL;
2100 }
daniel@transgaming.com61208202011-03-21 16:38:50 +00002101
2102 updateTexture();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002103
2104 IDirect3DSurface9 *renderTarget = NULL;
2105 mTexture->GetSurfaceLevel(0, &renderTarget);
2106
2107 return renderTarget;
2108}
2109
2110TextureCubeMap::TextureCubeMap(GLuint id) : Texture(id)
2111{
2112 mTexture = NULL;
2113}
2114
2115TextureCubeMap::~TextureCubeMap()
2116{
2117 for (int i = 0; i < 6; i++)
2118 {
2119 mFaceProxies[i].set(NULL);
2120 }
2121
2122 if (mTexture)
2123 {
2124 mTexture->Release();
2125 mTexture = NULL;
2126 }
2127}
2128
2129GLenum TextureCubeMap::getTarget() const
2130{
2131 return GL_TEXTURE_CUBE_MAP;
2132}
2133
daniel@transgaming.com61208202011-03-21 16:38:50 +00002134GLsizei TextureCubeMap::getWidth() const
2135{
2136 return mImageArray[0][0].width;
2137}
2138
2139GLsizei TextureCubeMap::getHeight() const
2140{
2141 return mImageArray[0][0].height;
2142}
2143
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002144GLenum TextureCubeMap::getInternalFormat() const
2145{
2146 return mImageArray[0][0].format;
2147}
2148
daniel@transgaming.com61208202011-03-21 16:38:50 +00002149GLenum TextureCubeMap::getType() const
2150{
2151 return mImageArray[0][0].type;
2152}
2153
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00002154D3DFORMAT TextureCubeMap::getD3DFormat() const
2155{
2156 return mImageArray[0][0].getD3DFormat();
2157}
2158
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002159void 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 +00002160{
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002161 setImage(0, level, width, height, format, type, unpackAlignment, pixels);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002162}
2163
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002164void 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 +00002165{
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002166 setImage(1, level, width, height, format, type, unpackAlignment, pixels);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002167}
2168
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002169void 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 +00002170{
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002171 setImage(2, level, width, height, format, type, unpackAlignment, pixels);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002172}
2173
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002174void 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 +00002175{
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002176 setImage(3, level, width, height, format, type, unpackAlignment, pixels);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002177}
2178
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002179void 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 +00002180{
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002181 setImage(4, level, width, height, format, type, unpackAlignment, pixels);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002182}
2183
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002184void 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 +00002185{
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002186 setImage(5, level, width, height, format, type, unpackAlignment, pixels);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002187}
2188
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002189void 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 +00002190{
daniel@transgaming.com61208202011-03-21 16:38:50 +00002191 redefineTexture(faceIndex(face), level, format, width, height, GL_UNSIGNED_BYTE);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002192
daniel@transgaming.com61208202011-03-21 16:38:50 +00002193 Texture::setCompressedImage(imageSize, pixels, &mImageArray[faceIndex(face)][level]);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002194}
2195
daniel@transgaming.comb612f882011-11-09 17:44:31 +00002196void TextureCubeMap::commitRect(int face, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002197{
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002198 ASSERT(mImageArray[face][level].surface != NULL);
2199
2200 if (level < levelCount())
2201 {
daniel@transgaming.comb612f882011-11-09 17:44:31 +00002202 IDirect3DSurface9 *destLevel = getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002203 ASSERT(destLevel != NULL);
2204
2205 if (destLevel != NULL)
2206 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002207 Image *image = &mImageArray[face][level];
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002208
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002209 RECT sourceRect = transformPixelRect(xoffset, yoffset, width, height, image->height);;
daniel@transgaming.comb612f882011-11-09 17:44:31 +00002210 POINT destPoint = {sourceRect.left, sourceRect.top};
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002211
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002212 HRESULT result = getDevice()->UpdateSurface(image->surface, &sourceRect, destLevel, &destPoint);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002213 ASSERT(SUCCEEDED(result));
2214
2215 destLevel->Release();
2216
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002217 image->dirty = false;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002218 }
2219 }
2220}
2221
2222void TextureCubeMap::subImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels)
2223{
2224 if (Texture::subImage(xoffset, yoffset, width, height, format, type, unpackAlignment, pixels, &mImageArray[faceIndex(target)][level]))
2225 {
daniel@transgaming.comb612f882011-11-09 17:44:31 +00002226 commitRect(faceIndex(target), level, xoffset, yoffset, width, height);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002227 }
2228}
2229
2230void TextureCubeMap::subImageCompressed(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels)
2231{
2232 if (Texture::subImageCompressed(xoffset, yoffset, width, height, format, imageSize, pixels, &mImageArray[faceIndex(target)][level]))
2233 {
daniel@transgaming.comb612f882011-11-09 17:44:31 +00002234 commitRect(faceIndex(target), level, xoffset, yoffset, width, height);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002235 }
2236}
2237
2238// Tests for GL texture object completeness. [OpenGL ES 2.0.24] section 3.7.10 page 81.
2239bool TextureCubeMap::isComplete() const
2240{
2241 int size = mImageArray[0][0].width;
2242
2243 if (size <= 0)
2244 {
2245 return false;
2246 }
2247
2248 bool mipmapping;
2249
2250 switch (mMinFilter)
2251 {
2252 case GL_NEAREST:
2253 case GL_LINEAR:
2254 mipmapping = false;
2255 break;
2256 case GL_NEAREST_MIPMAP_NEAREST:
2257 case GL_LINEAR_MIPMAP_NEAREST:
2258 case GL_NEAREST_MIPMAP_LINEAR:
2259 case GL_LINEAR_MIPMAP_LINEAR:
2260 mipmapping = true;
2261 break;
2262 default: UNREACHABLE();
2263 }
2264
2265 for (int face = 0; face < 6; face++)
2266 {
2267 if (mImageArray[face][0].width != size || mImageArray[face][0].height != size)
2268 {
2269 return false;
2270 }
2271 }
2272
2273 if ((getInternalFormat() == GL_FLOAT && !getContext()->supportsFloatLinearFilter()) ||
2274 (getInternalFormat() == GL_HALF_FLOAT_OES && !getContext()->supportsHalfFloatLinearFilter()))
2275 {
2276 if (mMagFilter != GL_NEAREST || (mMinFilter != GL_NEAREST && mMinFilter != GL_NEAREST_MIPMAP_NEAREST))
2277 {
2278 return false;
2279 }
2280 }
2281
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00002282 bool npot = getContext()->supportsNonPower2Texture();
2283
2284 if (!npot)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002285 {
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00002286 if ((getWrapS() != GL_CLAMP_TO_EDGE || getWrapT() != GL_CLAMP_TO_EDGE) && !isPow2(size))
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002287 {
2288 return false;
2289 }
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00002290 }
2291
2292 if (mipmapping)
2293 {
2294 if (!npot)
2295 {
2296 if (!isPow2(size))
2297 {
2298 return false;
2299 }
2300 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002301
2302 int q = log2(size);
2303
2304 for (int face = 0; face < 6; face++)
2305 {
2306 for (int level = 1; level <= q; level++)
2307 {
2308 if (mImageArray[face][level].format != mImageArray[0][0].format)
2309 {
2310 return false;
2311 }
2312
daniel@transgaming.com0bd22bc2011-03-21 16:38:26 +00002313 if (mImageArray[face][level].type != mImageArray[0][0].type)
2314 {
2315 return false;
2316 }
2317
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002318 if (mImageArray[face][level].width != std::max(1, size >> level))
2319 {
2320 return false;
2321 }
2322
2323 ASSERT(mImageArray[face][level].height == mImageArray[face][level].width);
2324 }
2325 }
2326 }
2327
2328 return true;
2329}
2330
2331bool TextureCubeMap::isCompressed() const
2332{
2333 return IsCompressed(getInternalFormat());
2334}
2335
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002336IDirect3DBaseTexture9 *TextureCubeMap::getBaseTexture() const
2337{
2338 return mTexture;
2339}
2340
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002341// Constructs a Direct3D 9 texture resource from the texture images, or returns an existing one
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002342void TextureCubeMap::createTexture()
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002343{
2344 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00002345 D3DFORMAT format = mImageArray[0][0].getD3DFormat();
daniel@transgaming.com61208202011-03-21 16:38:50 +00002346 GLint levels = creationLevels(mImageArray[0][0].width, 0);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002347
daniel@transgaming.com0bd22bc2011-03-21 16:38:26 +00002348 IDirect3DCubeTexture9 *texture = NULL;
daniel@transgaming.com61208202011-03-21 16:38:50 +00002349 HRESULT result = device->CreateCubeTexture(mImageArray[0][0].width, levels, 0, format, D3DPOOL_DEFAULT, &texture, NULL);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002350
2351 if (FAILED(result))
2352 {
2353 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002354 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002355 }
2356
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002357 if (mTexture)
2358 {
2359 mTexture->Release();
2360 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002361
2362 mTexture = texture;
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002363 mDirtyImage = true;
daniel@transgaming.comaed18322011-03-21 16:38:13 +00002364 mIsRenderable = false;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002365}
2366
2367void TextureCubeMap::updateTexture()
2368{
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002369 for (int face = 0; face < 6; face++)
2370 {
2371 int levels = levelCount();
2372 for (int level = 0; level < levels; level++)
2373 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002374 Image *image = &mImageArray[face][level];
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002375
daniel@transgaming.com61208202011-03-21 16:38:50 +00002376 if (image->surface && image->dirty)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002377 {
daniel@transgaming.comb612f882011-11-09 17:44:31 +00002378 commitRect(face, level, 0, 0, image->width, image->height);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002379 }
2380 }
2381 }
2382}
2383
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002384void TextureCubeMap::convertToRenderTarget()
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002385{
2386 IDirect3DCubeTexture9 *texture = NULL;
2387
daniel@transgaming.com61208202011-03-21 16:38:50 +00002388 if (mImageArray[0][0].width != 0)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002389 {
2390 egl::Display *display = getDisplay();
2391 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00002392 D3DFORMAT format = mImageArray[0][0].getD3DFormat();
daniel@transgaming.com61208202011-03-21 16:38:50 +00002393 GLint levels = creationLevels(mImageArray[0][0].width, 0);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002394
daniel@transgaming.com61208202011-03-21 16:38:50 +00002395 HRESULT result = device->CreateCubeTexture(mImageArray[0][0].width, levels, D3DUSAGE_RENDERTARGET, format, D3DPOOL_DEFAULT, &texture, NULL);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002396
2397 if (FAILED(result))
2398 {
2399 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002400 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002401 }
2402
2403 if (mTexture != NULL)
2404 {
2405 int levels = levelCount();
2406 for (int f = 0; f < 6; f++)
2407 {
2408 for (int i = 0; i < levels; i++)
2409 {
2410 IDirect3DSurface9 *source;
2411 result = mTexture->GetCubeMapSurface(static_cast<D3DCUBEMAP_FACES>(f), i, &source);
2412
2413 if (FAILED(result))
2414 {
2415 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
2416
2417 texture->Release();
2418
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002419 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002420 }
2421
2422 IDirect3DSurface9 *dest;
2423 result = texture->GetCubeMapSurface(static_cast<D3DCUBEMAP_FACES>(f), i, &dest);
2424
2425 if (FAILED(result))
2426 {
2427 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
2428
2429 texture->Release();
2430 source->Release();
2431
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002432 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002433 }
2434
2435 display->endScene();
2436 result = device->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
2437
2438 if (FAILED(result))
2439 {
2440 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
2441
2442 texture->Release();
2443 source->Release();
2444 dest->Release();
2445
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002446 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002447 }
daniel@transgaming.coma1a86202011-08-09 13:41:08 +00002448
2449 source->Release();
2450 dest->Release();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002451 }
2452 }
2453 }
2454 }
2455
2456 if (mTexture != NULL)
2457 {
2458 mTexture->Release();
2459 }
2460
2461 mTexture = texture;
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002462 mDirtyImage = true;
daniel@transgaming.comaed18322011-03-21 16:38:13 +00002463 mIsRenderable = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002464}
2465
daniel@transgaming.com61208202011-03-21 16:38:50 +00002466void 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 +00002467{
daniel@transgaming.com61208202011-03-21 16:38:50 +00002468 redefineTexture(faceIndex, level, format, width, height, type);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002469
daniel@transgaming.com61208202011-03-21 16:38:50 +00002470 Texture::setImage(unpackAlignment, pixels, &mImageArray[faceIndex][level]);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002471}
2472
2473unsigned int TextureCubeMap::faceIndex(GLenum face)
2474{
2475 META_ASSERT(GL_TEXTURE_CUBE_MAP_NEGATIVE_X - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 1);
2476 META_ASSERT(GL_TEXTURE_CUBE_MAP_POSITIVE_Y - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 2);
2477 META_ASSERT(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 3);
2478 META_ASSERT(GL_TEXTURE_CUBE_MAP_POSITIVE_Z - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 4);
2479 META_ASSERT(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 5);
2480
2481 return face - GL_TEXTURE_CUBE_MAP_POSITIVE_X;
2482}
2483
daniel@transgaming.com61208202011-03-21 16:38:50 +00002484void TextureCubeMap::redefineTexture(int face, GLint level, GLenum format, GLsizei width, GLsizei height, GLenum type)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002485{
daniel@transgaming.com94a4f032011-03-21 16:38:55 +00002486 GLsizei textureWidth = mImageArray[0][0].width;
2487 GLsizei textureHeight = mImageArray[0][0].height;
2488 GLenum textureFormat = mImageArray[0][0].format;
2489 GLenum textureType = mImageArray[0][0].type;
2490
daniel@transgaming.com61208202011-03-21 16:38:50 +00002491 mImageArray[face][level].width = width;
2492 mImageArray[face][level].height = height;
2493 mImageArray[face][level].format = format;
2494 mImageArray[face][level].type = type;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002495
daniel@transgaming.com61208202011-03-21 16:38:50 +00002496 if (!mTexture)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002497 {
daniel@transgaming.com61208202011-03-21 16:38:50 +00002498 return;
2499 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002500
daniel@transgaming.com94a4f032011-03-21 16:38:55 +00002501 bool sizeOkay = (textureWidth >> level == width);
2502 bool textureOkay = (sizeOkay && textureFormat == format && textureType == type);
daniel@transgaming.com61208202011-03-21 16:38:50 +00002503
2504 if (!textureOkay) // Purge all the levels and the texture.
2505 {
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002506 for (int i = 0; i < IMPLEMENTATION_MAX_TEXTURE_LEVELS; i++)
2507 {
2508 for (int f = 0; f < 6; f++)
2509 {
2510 if (mImageArray[f][i].surface != NULL)
2511 {
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002512 mImageArray[f][i].surface->Release();
2513 mImageArray[f][i].surface = NULL;
daniel@transgaming.com61208202011-03-21 16:38:50 +00002514 mImageArray[f][i].dirty = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002515 }
2516 }
2517 }
2518
2519 if (mTexture != NULL)
2520 {
2521 mTexture->Release();
2522 mTexture = NULL;
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002523 mDirtyImage = true;
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002524 mIsRenderable = false;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002525 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002526 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002527}
2528
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002529void 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 +00002530{
2531 IDirect3DSurface9 *renderTarget = source->getRenderTarget();
2532
2533 if (!renderTarget)
2534 {
2535 ERR("Failed to retrieve the render target.");
2536 return error(GL_OUT_OF_MEMORY);
2537 }
2538
2539 unsigned int faceindex = faceIndex(target);
daniel@transgaming.com61208202011-03-21 16:38:50 +00002540 redefineTexture(faceindex, level, format, width, height, GL_UNSIGNED_BYTE);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002541
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00002542 if (!mImageArray[faceindex][level].isRenderable())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002543 {
daniel@transgaming.comb6276992011-03-29 00:58:18 +00002544 copyToImage(&mImageArray[faceindex][level], 0, 0, x, y, width, height, renderTarget);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002545 }
2546 else
2547 {
daniel@transgaming.com61208202011-03-21 16:38:50 +00002548 if (!mTexture || !mIsRenderable)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002549 {
2550 convertToRenderTarget();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002551 }
daniel@transgaming.com3b3c1d42011-06-08 20:38:09 +00002552
2553 updateTexture();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002554
2555 ASSERT(width == height);
2556
2557 if (width > 0 && level < levelCount())
2558 {
2559 RECT sourceRect = transformPixelRect(x, y, width, height, source->getColorbuffer()->getHeight());
2560 sourceRect.left = clamp(sourceRect.left, 0, source->getColorbuffer()->getWidth());
2561 sourceRect.top = clamp(sourceRect.top, 0, source->getColorbuffer()->getHeight());
2562 sourceRect.right = clamp(sourceRect.right, 0, source->getColorbuffer()->getWidth());
2563 sourceRect.bottom = clamp(sourceRect.bottom, 0, source->getColorbuffer()->getHeight());
2564
daniel@transgaming.comeef864a2011-04-22 11:33:27 +00002565 GLint destYOffset = transformPixelYOffset(0, height, mImageArray[faceindex][level].width);
2566
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002567 IDirect3DSurface9 *dest = getCubeMapSurface(target, level);
2568
daniel@transgaming.comeef864a2011-04-22 11:33:27 +00002569 getBlitter()->copy(source->getRenderTarget(), sourceRect, format, 0, destYOffset, dest);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002570 dest->Release();
2571 }
2572 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002573}
2574
daniel@transgaming.comb612f882011-11-09 17:44:31 +00002575IDirect3DSurface9 *TextureCubeMap::getCubeMapSurface(GLenum target, unsigned int level)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002576{
2577 if (mTexture == NULL)
2578 {
2579 UNREACHABLE();
2580 return NULL;
2581 }
2582
2583 IDirect3DSurface9 *surface = NULL;
2584
daniel@transgaming.comb612f882011-11-09 17:44:31 +00002585 HRESULT hr = mTexture->GetCubeMapSurface(es2dx::ConvertCubeFace(target), level, &surface);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002586
2587 return (SUCCEEDED(hr)) ? surface : NULL;
2588}
2589
2590void TextureCubeMap::copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source)
2591{
2592 GLsizei size = mImageArray[faceIndex(target)][level].width;
2593
2594 if (xoffset + width > size || yoffset + height > size)
2595 {
2596 return error(GL_INVALID_VALUE);
2597 }
2598
2599 IDirect3DSurface9 *renderTarget = source->getRenderTarget();
2600
2601 if (!renderTarget)
2602 {
2603 ERR("Failed to retrieve the render target.");
2604 return error(GL_OUT_OF_MEMORY);
2605 }
2606
2607 unsigned int faceindex = faceIndex(target);
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00002608 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 +00002609
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00002610 if (!mImageArray[faceindex][level].isRenderable() || (!mTexture && !isComplete()))
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002611 {
daniel@transgaming.comb6276992011-03-29 00:58:18 +00002612 copyToImage(&mImageArray[faceindex][level], 0, 0, x, y, width, height, renderTarget);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002613 }
2614 else
2615 {
daniel@transgaming.com61208202011-03-21 16:38:50 +00002616 if (!mTexture || !mIsRenderable)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002617 {
2618 convertToRenderTarget();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002619 }
daniel@transgaming.com61208202011-03-21 16:38:50 +00002620
2621 updateTexture();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002622
2623 if (level < levelCount())
2624 {
2625 RECT sourceRect = transformPixelRect(x, y, width, height, source->getColorbuffer()->getHeight());
2626 sourceRect.left = clamp(sourceRect.left, 0, source->getColorbuffer()->getWidth());
2627 sourceRect.top = clamp(sourceRect.top, 0, source->getColorbuffer()->getHeight());
2628 sourceRect.right = clamp(sourceRect.right, 0, source->getColorbuffer()->getWidth());
2629 sourceRect.bottom = clamp(sourceRect.bottom, 0, source->getColorbuffer()->getHeight());
2630
2631 GLint destYOffset = transformPixelYOffset(yoffset, height, mImageArray[faceindex][level].width);
2632
2633 IDirect3DSurface9 *dest = getCubeMapSurface(target, level);
2634
daniel@transgaming.comeef864a2011-04-22 11:33:27 +00002635 getBlitter()->copy(source->getRenderTarget(), sourceRect, mImageArray[0][0].format, xoffset, destYOffset, dest);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002636 dest->Release();
2637 }
2638 }
2639}
2640
2641bool TextureCubeMap::isCubeComplete() const
2642{
2643 if (mImageArray[0][0].width == 0)
2644 {
2645 return false;
2646 }
2647
2648 for (unsigned int f = 1; f < 6; f++)
2649 {
2650 if (mImageArray[f][0].width != mImageArray[0][0].width
2651 || mImageArray[f][0].format != mImageArray[0][0].format)
2652 {
2653 return false;
2654 }
2655 }
2656
2657 return true;
2658}
2659
2660void TextureCubeMap::generateMipmaps()
2661{
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00002662 if (!isCubeComplete())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002663 {
2664 return error(GL_INVALID_OPERATION);
2665 }
2666
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00002667 if (!getContext()->supportsNonPower2Texture())
2668 {
2669 if (!isPow2(mImageArray[0][0].width))
2670 {
2671 return error(GL_INVALID_OPERATION);
2672 }
2673 }
2674
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002675 // Purge array levels 1 through q and reset them to represent the generated mipmap levels.
2676 unsigned int q = log2(mImageArray[0][0].width);
2677 for (unsigned int f = 0; f < 6; f++)
2678 {
2679 for (unsigned int i = 1; i <= q; i++)
2680 {
2681 if (mImageArray[f][i].surface != NULL)
2682 {
2683 mImageArray[f][i].surface->Release();
2684 mImageArray[f][i].surface = NULL;
2685 }
2686
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002687 mImageArray[f][i].width = std::max(mImageArray[f][0].width >> i, 1);
2688 mImageArray[f][i].height = mImageArray[f][i].width;
daniel@transgaming.com0bd22bc2011-03-21 16:38:26 +00002689 mImageArray[f][i].format = mImageArray[f][0].format;
2690 mImageArray[f][i].type = mImageArray[f][0].type;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002691 }
2692 }
2693
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002694 if (mIsRenderable)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002695 {
2696 if (mTexture == NULL)
2697 {
2698 return;
2699 }
2700
2701 for (unsigned int f = 0; f < 6; f++)
2702 {
2703 for (unsigned int i = 1; i <= q; i++)
2704 {
2705 IDirect3DSurface9 *upper = getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i-1);
2706 IDirect3DSurface9 *lower = getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i);
2707
2708 if (upper != NULL && lower != NULL)
2709 {
2710 getBlitter()->boxFilter(upper, lower);
2711 }
2712
2713 if (upper != NULL) upper->Release();
2714 if (lower != NULL) lower->Release();
daniel@transgaming.com61208202011-03-21 16:38:50 +00002715
2716 mImageArray[f][i].dirty = false;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002717 }
2718 }
2719 }
2720 else
2721 {
2722 for (unsigned int f = 0; f < 6; f++)
2723 {
2724 for (unsigned int i = 1; i <= q; i++)
2725 {
daniel@transgaming.com61208202011-03-21 16:38:50 +00002726 createSurface(&mImageArray[f][i]);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002727 if (mImageArray[f][i].surface == NULL)
2728 {
2729 return error(GL_OUT_OF_MEMORY);
2730 }
2731
2732 if (FAILED(D3DXLoadSurfaceFromSurface(mImageArray[f][i].surface, NULL, NULL, mImageArray[f][i - 1].surface, NULL, NULL, D3DX_FILTER_BOX, 0)))
2733 {
2734 ERR(" failed to load filter %d to %d.", i - 1, i);
2735 }
2736
2737 mImageArray[f][i].dirty = true;
2738 }
2739 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002740 }
2741}
2742
2743Renderbuffer *TextureCubeMap::getRenderbuffer(GLenum target)
2744{
2745 if (!IsCubemapTextureTarget(target))
2746 {
2747 return error(GL_INVALID_OPERATION, (Renderbuffer *)NULL);
2748 }
2749
2750 unsigned int face = faceIndex(target);
2751
2752 if (mFaceProxies[face].get() == NULL)
2753 {
2754 mFaceProxies[face].set(new Renderbuffer(id(), new Colorbuffer(this, target)));
2755 }
2756
2757 return mFaceProxies[face].get();
2758}
2759
2760IDirect3DSurface9 *TextureCubeMap::getRenderTarget(GLenum target)
2761{
2762 ASSERT(IsCubemapTextureTarget(target));
2763
daniel@transgaming.com61208202011-03-21 16:38:50 +00002764 if (!mIsRenderable)
2765 {
2766 convertToRenderTarget();
2767 }
2768
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002769 if (mTexture == NULL)
2770 {
2771 return NULL;
2772 }
daniel@transgaming.com61208202011-03-21 16:38:50 +00002773
2774 updateTexture();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002775
2776 IDirect3DSurface9 *renderTarget = NULL;
2777 mTexture->GetCubeMapSurface(es2dx::ConvertCubeFace(target), 0, &renderTarget);
2778
2779 return renderTarget;
2780}
2781
daniel@transgaming.comb612f882011-11-09 17:44:31 +00002782}