blob: 86a987443ec27e02795f86ea40594a3ca3d3363b [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{
daniel@transgaming.comc9ba4ad2011-11-09 17:44:35 +00001055 if(image->surface)
1056 {
1057 return;
1058 }
1059
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001060 IDirect3DTexture9 *newTexture = NULL;
1061 IDirect3DSurface9 *newSurface = NULL;
1062
daniel@transgaming.com61208202011-03-21 16:38:50 +00001063 if (image->width != 0 && image->height != 0)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001064 {
1065 int levelToFetch = 0;
daniel@transgaming.com61208202011-03-21 16:38:50 +00001066 GLsizei requestWidth = image->width;
1067 GLsizei requestHeight = image->height;
1068 if (IsCompressed(image->format) && (image->width % 4 != 0 || image->height % 4 != 0))
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001069 {
1070 bool isMult4 = false;
1071 int upsampleCount = 0;
1072 while (!isMult4)
1073 {
1074 requestWidth <<= 1;
1075 requestHeight <<= 1;
1076 upsampleCount++;
1077 if (requestWidth % 4 == 0 && requestHeight % 4 == 0)
1078 {
1079 isMult4 = true;
1080 }
1081 }
1082 levelToFetch = upsampleCount;
1083 }
1084
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00001085 HRESULT result = getDevice()->CreateTexture(requestWidth, requestHeight, levelToFetch + 1, NULL, image->getD3DFormat(),
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001086 D3DPOOL_SYSTEMMEM, &newTexture, NULL);
1087
1088 if (FAILED(result))
1089 {
1090 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
1091 return error(GL_OUT_OF_MEMORY);
1092 }
1093
1094 newTexture->GetSurfaceLevel(levelToFetch, &newSurface);
1095 newTexture->Release();
1096 }
1097
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.comc9ba4ad2011-11-09 17:44:35 +00001559 if (mImageArray[level].surface != NULL)
1560 {
1561 mImageArray[level].surface->Release();
1562 mImageArray[level].surface = NULL;
1563 mImageArray[level].dirty = true;
1564 }
1565
1566 createSurface(&mImageArray[level]);
1567
daniel@transgaming.com61208202011-03-21 16:38:50 +00001568 if (!mTexture)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001569 {
daniel@transgaming.com61208202011-03-21 16:38:50 +00001570 return;
1571 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001572
daniel@transgaming.com94a4f032011-03-21 16:38:55 +00001573 bool widthOkay = (textureWidth >> level == width) || (textureWidth >> level == 0 && width == 1);
1574 bool heightOkay = (textureHeight >> level == height) || (textureHeight >> level == 0 && height == 1);
1575 bool textureOkay = (widthOkay && heightOkay && textureFormat == format && textureType == type);
daniel@transgaming.com61208202011-03-21 16:38:50 +00001576
daniel@transgaming.comc9ba4ad2011-11-09 17:44:35 +00001577 if (!textureOkay || forceRedefine || mSurface)
daniel@transgaming.com61208202011-03-21 16:38:50 +00001578 {
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001579 for (int i = 0; i < IMPLEMENTATION_MAX_TEXTURE_LEVELS; i++)
1580 {
daniel@transgaming.comc9ba4ad2011-11-09 17:44:35 +00001581 mImageArray[i].dirty = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001582 }
1583
1584 if (mTexture != NULL)
1585 {
1586 mTexture->Release();
1587 mTexture = NULL;
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00001588 mDirtyImage = true;
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001589 mIsRenderable = false;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001590 }
jbauman@chromium.orgae345802011-03-30 22:04:25 +00001591
1592 if (mSurface)
1593 {
1594 mSurface->setBoundTexture(NULL);
1595 mSurface = NULL;
1596 }
apatrick@chromium.org57a2cd62011-06-08 00:04:07 +00001597
1598 mColorbufferProxy.set(NULL);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001599 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001600}
1601
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00001602void 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 +00001603{
jbauman@chromium.orgae345802011-03-30 22:04:25 +00001604 redefineTexture(level, format, width, height, type, false);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001605
daniel@transgaming.com61208202011-03-21 16:38:50 +00001606 Texture::setImage(unpackAlignment, pixels, &mImageArray[level]);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001607}
1608
jbauman@chromium.orgae345802011-03-30 22:04:25 +00001609void Texture2D::bindTexImage(egl::Surface *surface)
1610{
1611 GLenum format;
1612
1613 switch(surface->getFormat())
1614 {
1615 case D3DFMT_A8R8G8B8:
1616 format = GL_RGBA;
1617 break;
1618 case D3DFMT_X8R8G8B8:
1619 format = GL_RGB;
1620 break;
1621 default:
1622 UNIMPLEMENTED();
1623 return;
1624 }
1625
1626 redefineTexture(0, format, surface->getWidth(), surface->getHeight(), GL_UNSIGNED_BYTE, true);
1627
1628 IDirect3DTexture9 *texture = surface->getOffscreenTexture();
1629
1630 mTexture = texture;
1631 mDirtyImage = true;
1632 mIsRenderable = true;
1633 mSurface = surface;
1634 mSurface->setBoundTexture(this);
1635}
1636
1637void Texture2D::releaseTexImage()
1638{
1639 redefineTexture(0, GL_RGB, 0, 0, GL_UNSIGNED_BYTE, true);
1640}
1641
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00001642void Texture2D::setCompressedImage(GLint level, GLenum format, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001643{
jbauman@chromium.orgae345802011-03-30 22:04:25 +00001644 redefineTexture(level, format, width, height, GL_UNSIGNED_BYTE, false);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001645
daniel@transgaming.com61208202011-03-21 16:38:50 +00001646 Texture::setCompressedImage(imageSize, pixels, &mImageArray[level]);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001647}
1648
1649void Texture2D::commitRect(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height)
1650{
1651 ASSERT(mImageArray[level].surface != NULL);
1652
1653 if (level < levelCount())
1654 {
1655 IDirect3DSurface9 *destLevel = NULL;
1656 HRESULT result = mTexture->GetSurfaceLevel(level, &destLevel);
1657
1658 ASSERT(SUCCEEDED(result));
1659
1660 if (SUCCEEDED(result))
1661 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001662 Image *image = &mImageArray[level];
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001663
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001664 RECT sourceRect = transformPixelRect(xoffset, yoffset, width, height, image->height);;
daniel@transgaming.comb612f882011-11-09 17:44:31 +00001665 POINT destPoint = {sourceRect.left, sourceRect.top};
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001666
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001667 result = getDevice()->UpdateSurface(image->surface, &sourceRect, destLevel, &destPoint);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001668 ASSERT(SUCCEEDED(result));
1669
1670 destLevel->Release();
1671
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001672 image->dirty = false;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001673 }
1674 }
1675}
1676
1677void Texture2D::subImage(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels)
1678{
1679 if (Texture::subImage(xoffset, yoffset, width, height, format, type, unpackAlignment, pixels, &mImageArray[level]))
1680 {
1681 commitRect(level, xoffset, yoffset, width, height);
1682 }
1683}
1684
1685void Texture2D::subImageCompressed(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels)
1686{
1687 if (Texture::subImageCompressed(xoffset, yoffset, width, height, format, imageSize, pixels, &mImageArray[level]))
1688 {
1689 commitRect(level, xoffset, yoffset, width, height);
1690 }
1691}
1692
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00001693void Texture2D::copyImage(GLint level, GLenum format, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001694{
1695 IDirect3DSurface9 *renderTarget = source->getRenderTarget();
1696
1697 if (!renderTarget)
1698 {
1699 ERR("Failed to retrieve the render target.");
1700 return error(GL_OUT_OF_MEMORY);
1701 }
1702
jbauman@chromium.orgae345802011-03-30 22:04:25 +00001703 redefineTexture(level, format, width, height, GL_UNSIGNED_BYTE, false);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001704
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00001705 if (!mImageArray[level].isRenderable())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001706 {
daniel@transgaming.comb6276992011-03-29 00:58:18 +00001707 copyToImage(&mImageArray[level], 0, 0, x, y, width, height, renderTarget);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001708 }
1709 else
1710 {
daniel@transgaming.com61208202011-03-21 16:38:50 +00001711 if (!mTexture || !mIsRenderable)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001712 {
1713 convertToRenderTarget();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001714 }
daniel@transgaming.com3b3c1d42011-06-08 20:38:09 +00001715
1716 updateTexture();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001717
1718 if (width != 0 && height != 0 && level < levelCount())
1719 {
1720 RECT sourceRect = transformPixelRect(x, y, width, height, source->getColorbuffer()->getHeight());
1721 sourceRect.left = clamp(sourceRect.left, 0, source->getColorbuffer()->getWidth());
1722 sourceRect.top = clamp(sourceRect.top, 0, source->getColorbuffer()->getHeight());
1723 sourceRect.right = clamp(sourceRect.right, 0, source->getColorbuffer()->getWidth());
1724 sourceRect.bottom = clamp(sourceRect.bottom, 0, source->getColorbuffer()->getHeight());
daniel@transgaming.comeef864a2011-04-22 11:33:27 +00001725
1726 GLint destYOffset = transformPixelYOffset(0, height, mImageArray[level].height);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001727
1728 IDirect3DSurface9 *dest;
1729 HRESULT hr = mTexture->GetSurfaceLevel(level, &dest);
1730
daniel@transgaming.comeef864a2011-04-22 11:33:27 +00001731 getBlitter()->copy(source->getRenderTarget(), sourceRect, format, 0, destYOffset, dest);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001732 dest->Release();
1733 }
1734 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001735}
1736
1737void Texture2D::copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source)
1738{
1739 if (xoffset + width > mImageArray[level].width || yoffset + height > mImageArray[level].height)
1740 {
1741 return error(GL_INVALID_VALUE);
1742 }
1743
1744 IDirect3DSurface9 *renderTarget = source->getRenderTarget();
1745
1746 if (!renderTarget)
1747 {
1748 ERR("Failed to retrieve the render target.");
1749 return error(GL_OUT_OF_MEMORY);
1750 }
1751
jbauman@chromium.orgae345802011-03-30 22:04:25 +00001752 redefineTexture(level, mImageArray[level].format, mImageArray[level].width, mImageArray[level].height, GL_UNSIGNED_BYTE, false);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001753
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00001754 if (!mImageArray[level].isRenderable() || (!mTexture && !isComplete()))
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001755 {
daniel@transgaming.comb6276992011-03-29 00:58:18 +00001756 copyToImage(&mImageArray[level], xoffset, yoffset, x, y, width, height, renderTarget);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001757 }
1758 else
1759 {
daniel@transgaming.com61208202011-03-21 16:38:50 +00001760 if (!mTexture || !mIsRenderable)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001761 {
1762 convertToRenderTarget();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001763 }
daniel@transgaming.com61208202011-03-21 16:38:50 +00001764
1765 updateTexture();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001766
1767 if (level < levelCount())
1768 {
1769 RECT sourceRect = transformPixelRect(x, y, width, height, source->getColorbuffer()->getHeight());
1770 sourceRect.left = clamp(sourceRect.left, 0, source->getColorbuffer()->getWidth());
1771 sourceRect.top = clamp(sourceRect.top, 0, source->getColorbuffer()->getHeight());
1772 sourceRect.right = clamp(sourceRect.right, 0, source->getColorbuffer()->getWidth());
1773 sourceRect.bottom = clamp(sourceRect.bottom, 0, source->getColorbuffer()->getHeight());
1774
1775 GLint destYOffset = transformPixelYOffset(yoffset, height, mImageArray[level].height);
1776
1777 IDirect3DSurface9 *dest;
1778 HRESULT hr = mTexture->GetSurfaceLevel(level, &dest);
1779
daniel@transgaming.comeef864a2011-04-22 11:33:27 +00001780 getBlitter()->copy(source->getRenderTarget(), sourceRect, mImageArray[0].format, xoffset, destYOffset, dest);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001781 dest->Release();
1782 }
1783 }
1784}
1785
1786// Tests for GL texture object completeness. [OpenGL ES 2.0.24] section 3.7.10 page 81.
1787bool Texture2D::isComplete() const
1788{
1789 GLsizei width = mImageArray[0].width;
1790 GLsizei height = mImageArray[0].height;
1791
1792 if (width <= 0 || height <= 0)
1793 {
1794 return false;
1795 }
1796
1797 bool mipmapping = false;
1798
1799 switch (mMinFilter)
1800 {
1801 case GL_NEAREST:
1802 case GL_LINEAR:
1803 mipmapping = false;
1804 break;
1805 case GL_NEAREST_MIPMAP_NEAREST:
1806 case GL_LINEAR_MIPMAP_NEAREST:
1807 case GL_NEAREST_MIPMAP_LINEAR:
1808 case GL_LINEAR_MIPMAP_LINEAR:
1809 mipmapping = true;
1810 break;
1811 default: UNREACHABLE();
1812 }
1813
1814 if ((getInternalFormat() == GL_FLOAT && !getContext()->supportsFloatLinearFilter()) ||
1815 (getInternalFormat() == GL_HALF_FLOAT_OES && !getContext()->supportsHalfFloatLinearFilter()))
1816 {
1817 if (mMagFilter != GL_NEAREST || (mMinFilter != GL_NEAREST && mMinFilter != GL_NEAREST_MIPMAP_NEAREST))
1818 {
1819 return false;
1820 }
1821 }
1822
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00001823 bool npot = getContext()->supportsNonPower2Texture();
1824
1825 if (!npot)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001826 {
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00001827 if ((getWrapS() != GL_CLAMP_TO_EDGE && !isPow2(width)) ||
1828 (getWrapT() != GL_CLAMP_TO_EDGE && !isPow2(height)))
1829 {
1830 return false;
1831 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001832 }
1833
1834 if (mipmapping)
1835 {
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00001836 if (!npot)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001837 {
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00001838 if (!isPow2(width) || !isPow2(height))
1839 {
1840 return false;
1841 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001842 }
1843
1844 int q = log2(std::max(width, height));
1845
1846 for (int level = 1; level <= q; level++)
1847 {
1848 if (mImageArray[level].format != mImageArray[0].format)
1849 {
1850 return false;
1851 }
1852
daniel@transgaming.com0bd22bc2011-03-21 16:38:26 +00001853 if (mImageArray[level].type != mImageArray[0].type)
1854 {
1855 return false;
1856 }
1857
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001858 if (mImageArray[level].width != std::max(1, width >> level))
1859 {
1860 return false;
1861 }
1862
1863 if (mImageArray[level].height != std::max(1, height >> level))
1864 {
1865 return false;
1866 }
1867 }
1868 }
1869
1870 return true;
1871}
1872
1873bool Texture2D::isCompressed() const
1874{
1875 return IsCompressed(getInternalFormat());
1876}
1877
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001878IDirect3DBaseTexture9 *Texture2D::getBaseTexture() const
1879{
1880 return mTexture;
1881}
1882
1883// Constructs a Direct3D 9 texture resource from the texture images
1884void Texture2D::createTexture()
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001885{
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001886 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00001887 D3DFORMAT format = mImageArray[0].getD3DFormat();
daniel@transgaming.com61208202011-03-21 16:38:50 +00001888 GLint levels = creationLevels(mImageArray[0].width, mImageArray[0].height, 0);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001889
daniel@transgaming.com61208202011-03-21 16:38:50 +00001890 IDirect3DTexture9 *texture = NULL;
1891 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 +00001892
1893 if (FAILED(result))
1894 {
1895 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001896 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001897 }
1898
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001899 if (mTexture)
1900 {
1901 mTexture->Release();
1902 }
1903
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001904 mTexture = texture;
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00001905 mDirtyImage = true;
daniel@transgaming.comaed18322011-03-21 16:38:13 +00001906 mIsRenderable = false;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001907}
1908
1909void Texture2D::updateTexture()
1910{
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001911 int levels = levelCount();
1912
1913 for (int level = 0; level < levels; level++)
1914 {
daniel@transgaming.comb612f882011-11-09 17:44:31 +00001915 Image *image = &mImageArray[level];
1916
1917 if (image->surface && image->dirty)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001918 {
daniel@transgaming.comb612f882011-11-09 17:44:31 +00001919 commitRect(level, 0, 0, mImageArray[level].width, mImageArray[level].height);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001920 }
1921 }
1922}
1923
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001924void Texture2D::convertToRenderTarget()
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001925{
1926 IDirect3DTexture9 *texture = NULL;
1927
daniel@transgaming.com61208202011-03-21 16:38:50 +00001928 if (mImageArray[0].width != 0 && mImageArray[0].height != 0)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001929 {
1930 egl::Display *display = getDisplay();
1931 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00001932 D3DFORMAT format = mImageArray[0].getD3DFormat();
daniel@transgaming.com61208202011-03-21 16:38:50 +00001933 GLint levels = creationLevels(mImageArray[0].width, mImageArray[0].height, 0);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001934
daniel@transgaming.com61208202011-03-21 16:38:50 +00001935 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 +00001936
1937 if (FAILED(result))
1938 {
1939 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001940 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001941 }
1942
1943 if (mTexture != NULL)
1944 {
1945 int levels = levelCount();
1946 for (int i = 0; i < levels; i++)
1947 {
1948 IDirect3DSurface9 *source;
1949 result = mTexture->GetSurfaceLevel(i, &source);
1950
1951 if (FAILED(result))
1952 {
1953 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
1954
1955 texture->Release();
1956
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001957 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001958 }
1959
1960 IDirect3DSurface9 *dest;
1961 result = texture->GetSurfaceLevel(i, &dest);
1962
1963 if (FAILED(result))
1964 {
1965 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
1966
1967 texture->Release();
1968 source->Release();
1969
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001970 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001971 }
1972
1973 display->endScene();
1974 result = device->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
1975
1976 if (FAILED(result))
1977 {
1978 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
1979
1980 texture->Release();
1981 source->Release();
1982 dest->Release();
1983
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001984 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001985 }
1986
1987 source->Release();
1988 dest->Release();
1989 }
1990 }
1991 }
1992
1993 if (mTexture != NULL)
1994 {
1995 mTexture->Release();
1996 }
1997
1998 mTexture = texture;
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00001999 mDirtyImage = true;
daniel@transgaming.comaed18322011-03-21 16:38:13 +00002000 mIsRenderable = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002001}
2002
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002003void Texture2D::generateMipmaps()
2004{
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00002005 if (!getContext()->supportsNonPower2Texture())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002006 {
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00002007 if (!isPow2(mImageArray[0].width) || !isPow2(mImageArray[0].height))
2008 {
2009 return error(GL_INVALID_OPERATION);
2010 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002011 }
2012
2013 // Purge array levels 1 through q and reset them to represent the generated mipmap levels.
daniel@transgaming.com61208202011-03-21 16:38:50 +00002014 unsigned int q = log2(std::max(mImageArray[0].width, mImageArray[0].height));
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002015 for (unsigned int i = 1; i <= q; i++)
2016 {
2017 if (mImageArray[i].surface != NULL)
2018 {
2019 mImageArray[i].surface->Release();
2020 mImageArray[i].surface = NULL;
2021 }
2022
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002023 mImageArray[i].width = std::max(mImageArray[0].width >> i, 1);
2024 mImageArray[i].height = std::max(mImageArray[0].height >> i, 1);
daniel@transgaming.com0bd22bc2011-03-21 16:38:26 +00002025 mImageArray[i].format = mImageArray[0].format;
2026 mImageArray[i].type = mImageArray[0].type;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002027 }
2028
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002029 if (mIsRenderable)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002030 {
2031 if (mTexture == NULL)
2032 {
2033 ERR(" failed because mTexture was null.");
2034 return;
2035 }
2036
2037 for (unsigned int i = 1; i <= q; i++)
2038 {
2039 IDirect3DSurface9 *upper = NULL;
2040 IDirect3DSurface9 *lower = NULL;
2041
2042 mTexture->GetSurfaceLevel(i-1, &upper);
2043 mTexture->GetSurfaceLevel(i, &lower);
2044
2045 if (upper != NULL && lower != NULL)
2046 {
2047 getBlitter()->boxFilter(upper, lower);
2048 }
2049
2050 if (upper != NULL) upper->Release();
2051 if (lower != NULL) lower->Release();
daniel@transgaming.com61208202011-03-21 16:38:50 +00002052
2053 mImageArray[i].dirty = false;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002054 }
2055 }
2056 else
2057 {
2058 for (unsigned int i = 1; i <= q; i++)
2059 {
daniel@transgaming.com61208202011-03-21 16:38:50 +00002060 createSurface(&mImageArray[i]);
2061
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002062 if (mImageArray[i].surface == NULL)
2063 {
2064 return error(GL_OUT_OF_MEMORY);
2065 }
2066
2067 if (FAILED(D3DXLoadSurfaceFromSurface(mImageArray[i].surface, NULL, NULL, mImageArray[i - 1].surface, NULL, NULL, D3DX_FILTER_BOX, 0)))
2068 {
2069 ERR(" failed to load filter %d to %d.", i - 1, i);
2070 }
2071
2072 mImageArray[i].dirty = true;
2073 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002074 }
2075}
2076
2077Renderbuffer *Texture2D::getRenderbuffer(GLenum target)
2078{
2079 if (target != GL_TEXTURE_2D)
2080 {
2081 return error(GL_INVALID_OPERATION, (Renderbuffer *)NULL);
2082 }
2083
2084 if (mColorbufferProxy.get() == NULL)
2085 {
2086 mColorbufferProxy.set(new Renderbuffer(id(), new Colorbuffer(this, target)));
2087 }
2088
2089 return mColorbufferProxy.get();
2090}
2091
2092IDirect3DSurface9 *Texture2D::getRenderTarget(GLenum target)
2093{
2094 ASSERT(target == GL_TEXTURE_2D);
2095
daniel@transgaming.com61208202011-03-21 16:38:50 +00002096 if (!mIsRenderable)
2097 {
2098 convertToRenderTarget();
2099 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002100
2101 if (mTexture == NULL)
2102 {
2103 return NULL;
2104 }
daniel@transgaming.com61208202011-03-21 16:38:50 +00002105
2106 updateTexture();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002107
2108 IDirect3DSurface9 *renderTarget = NULL;
2109 mTexture->GetSurfaceLevel(0, &renderTarget);
2110
2111 return renderTarget;
2112}
2113
2114TextureCubeMap::TextureCubeMap(GLuint id) : Texture(id)
2115{
2116 mTexture = NULL;
2117}
2118
2119TextureCubeMap::~TextureCubeMap()
2120{
2121 for (int i = 0; i < 6; i++)
2122 {
2123 mFaceProxies[i].set(NULL);
2124 }
2125
2126 if (mTexture)
2127 {
2128 mTexture->Release();
2129 mTexture = NULL;
2130 }
2131}
2132
2133GLenum TextureCubeMap::getTarget() const
2134{
2135 return GL_TEXTURE_CUBE_MAP;
2136}
2137
daniel@transgaming.com61208202011-03-21 16:38:50 +00002138GLsizei TextureCubeMap::getWidth() const
2139{
2140 return mImageArray[0][0].width;
2141}
2142
2143GLsizei TextureCubeMap::getHeight() const
2144{
2145 return mImageArray[0][0].height;
2146}
2147
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002148GLenum TextureCubeMap::getInternalFormat() const
2149{
2150 return mImageArray[0][0].format;
2151}
2152
daniel@transgaming.com61208202011-03-21 16:38:50 +00002153GLenum TextureCubeMap::getType() const
2154{
2155 return mImageArray[0][0].type;
2156}
2157
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00002158D3DFORMAT TextureCubeMap::getD3DFormat() const
2159{
2160 return mImageArray[0][0].getD3DFormat();
2161}
2162
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002163void 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 +00002164{
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002165 setImage(0, level, width, height, format, type, unpackAlignment, pixels);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002166}
2167
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002168void 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 +00002169{
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002170 setImage(1, level, width, height, format, type, unpackAlignment, pixels);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002171}
2172
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002173void 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 +00002174{
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002175 setImage(2, level, width, height, format, type, unpackAlignment, pixels);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002176}
2177
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002178void 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 +00002179{
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002180 setImage(3, level, width, height, format, type, unpackAlignment, pixels);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002181}
2182
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002183void 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 +00002184{
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002185 setImage(4, level, width, height, format, type, unpackAlignment, pixels);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002186}
2187
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002188void 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 +00002189{
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002190 setImage(5, level, width, height, format, type, unpackAlignment, pixels);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002191}
2192
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002193void 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 +00002194{
daniel@transgaming.com61208202011-03-21 16:38:50 +00002195 redefineTexture(faceIndex(face), level, format, width, height, GL_UNSIGNED_BYTE);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002196
daniel@transgaming.com61208202011-03-21 16:38:50 +00002197 Texture::setCompressedImage(imageSize, pixels, &mImageArray[faceIndex(face)][level]);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002198}
2199
daniel@transgaming.comb612f882011-11-09 17:44:31 +00002200void TextureCubeMap::commitRect(int face, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002201{
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002202 ASSERT(mImageArray[face][level].surface != NULL);
2203
2204 if (level < levelCount())
2205 {
daniel@transgaming.comb612f882011-11-09 17:44:31 +00002206 IDirect3DSurface9 *destLevel = getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002207 ASSERT(destLevel != NULL);
2208
2209 if (destLevel != NULL)
2210 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002211 Image *image = &mImageArray[face][level];
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002212
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002213 RECT sourceRect = transformPixelRect(xoffset, yoffset, width, height, image->height);;
daniel@transgaming.comb612f882011-11-09 17:44:31 +00002214 POINT destPoint = {sourceRect.left, sourceRect.top};
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002215
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002216 HRESULT result = getDevice()->UpdateSurface(image->surface, &sourceRect, destLevel, &destPoint);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002217 ASSERT(SUCCEEDED(result));
2218
2219 destLevel->Release();
2220
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002221 image->dirty = false;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002222 }
2223 }
2224}
2225
2226void TextureCubeMap::subImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels)
2227{
2228 if (Texture::subImage(xoffset, yoffset, width, height, format, type, unpackAlignment, pixels, &mImageArray[faceIndex(target)][level]))
2229 {
daniel@transgaming.comb612f882011-11-09 17:44:31 +00002230 commitRect(faceIndex(target), level, xoffset, yoffset, width, height);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002231 }
2232}
2233
2234void TextureCubeMap::subImageCompressed(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels)
2235{
2236 if (Texture::subImageCompressed(xoffset, yoffset, width, height, format, imageSize, pixels, &mImageArray[faceIndex(target)][level]))
2237 {
daniel@transgaming.comb612f882011-11-09 17:44:31 +00002238 commitRect(faceIndex(target), level, xoffset, yoffset, width, height);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002239 }
2240}
2241
2242// Tests for GL texture object completeness. [OpenGL ES 2.0.24] section 3.7.10 page 81.
2243bool TextureCubeMap::isComplete() const
2244{
2245 int size = mImageArray[0][0].width;
2246
2247 if (size <= 0)
2248 {
2249 return false;
2250 }
2251
2252 bool mipmapping;
2253
2254 switch (mMinFilter)
2255 {
2256 case GL_NEAREST:
2257 case GL_LINEAR:
2258 mipmapping = false;
2259 break;
2260 case GL_NEAREST_MIPMAP_NEAREST:
2261 case GL_LINEAR_MIPMAP_NEAREST:
2262 case GL_NEAREST_MIPMAP_LINEAR:
2263 case GL_LINEAR_MIPMAP_LINEAR:
2264 mipmapping = true;
2265 break;
2266 default: UNREACHABLE();
2267 }
2268
2269 for (int face = 0; face < 6; face++)
2270 {
2271 if (mImageArray[face][0].width != size || mImageArray[face][0].height != size)
2272 {
2273 return false;
2274 }
2275 }
2276
2277 if ((getInternalFormat() == GL_FLOAT && !getContext()->supportsFloatLinearFilter()) ||
2278 (getInternalFormat() == GL_HALF_FLOAT_OES && !getContext()->supportsHalfFloatLinearFilter()))
2279 {
2280 if (mMagFilter != GL_NEAREST || (mMinFilter != GL_NEAREST && mMinFilter != GL_NEAREST_MIPMAP_NEAREST))
2281 {
2282 return false;
2283 }
2284 }
2285
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00002286 bool npot = getContext()->supportsNonPower2Texture();
2287
2288 if (!npot)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002289 {
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00002290 if ((getWrapS() != GL_CLAMP_TO_EDGE || getWrapT() != GL_CLAMP_TO_EDGE) && !isPow2(size))
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002291 {
2292 return false;
2293 }
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00002294 }
2295
2296 if (mipmapping)
2297 {
2298 if (!npot)
2299 {
2300 if (!isPow2(size))
2301 {
2302 return false;
2303 }
2304 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002305
2306 int q = log2(size);
2307
2308 for (int face = 0; face < 6; face++)
2309 {
2310 for (int level = 1; level <= q; level++)
2311 {
2312 if (mImageArray[face][level].format != mImageArray[0][0].format)
2313 {
2314 return false;
2315 }
2316
daniel@transgaming.com0bd22bc2011-03-21 16:38:26 +00002317 if (mImageArray[face][level].type != mImageArray[0][0].type)
2318 {
2319 return false;
2320 }
2321
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002322 if (mImageArray[face][level].width != std::max(1, size >> level))
2323 {
2324 return false;
2325 }
2326
2327 ASSERT(mImageArray[face][level].height == mImageArray[face][level].width);
2328 }
2329 }
2330 }
2331
2332 return true;
2333}
2334
2335bool TextureCubeMap::isCompressed() const
2336{
2337 return IsCompressed(getInternalFormat());
2338}
2339
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002340IDirect3DBaseTexture9 *TextureCubeMap::getBaseTexture() const
2341{
2342 return mTexture;
2343}
2344
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002345// Constructs a Direct3D 9 texture resource from the texture images, or returns an existing one
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002346void TextureCubeMap::createTexture()
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002347{
2348 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00002349 D3DFORMAT format = mImageArray[0][0].getD3DFormat();
daniel@transgaming.com61208202011-03-21 16:38:50 +00002350 GLint levels = creationLevels(mImageArray[0][0].width, 0);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002351
daniel@transgaming.com0bd22bc2011-03-21 16:38:26 +00002352 IDirect3DCubeTexture9 *texture = NULL;
daniel@transgaming.com61208202011-03-21 16:38:50 +00002353 HRESULT result = device->CreateCubeTexture(mImageArray[0][0].width, levels, 0, format, D3DPOOL_DEFAULT, &texture, NULL);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002354
2355 if (FAILED(result))
2356 {
2357 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002358 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002359 }
2360
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002361 if (mTexture)
2362 {
2363 mTexture->Release();
2364 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002365
2366 mTexture = texture;
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002367 mDirtyImage = true;
daniel@transgaming.comaed18322011-03-21 16:38:13 +00002368 mIsRenderable = false;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002369}
2370
2371void TextureCubeMap::updateTexture()
2372{
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002373 for (int face = 0; face < 6; face++)
2374 {
2375 int levels = levelCount();
2376 for (int level = 0; level < levels; level++)
2377 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002378 Image *image = &mImageArray[face][level];
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002379
daniel@transgaming.com61208202011-03-21 16:38:50 +00002380 if (image->surface && image->dirty)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002381 {
daniel@transgaming.comb612f882011-11-09 17:44:31 +00002382 commitRect(face, level, 0, 0, image->width, image->height);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002383 }
2384 }
2385 }
2386}
2387
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002388void TextureCubeMap::convertToRenderTarget()
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002389{
2390 IDirect3DCubeTexture9 *texture = NULL;
2391
daniel@transgaming.com61208202011-03-21 16:38:50 +00002392 if (mImageArray[0][0].width != 0)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002393 {
2394 egl::Display *display = getDisplay();
2395 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00002396 D3DFORMAT format = mImageArray[0][0].getD3DFormat();
daniel@transgaming.com61208202011-03-21 16:38:50 +00002397 GLint levels = creationLevels(mImageArray[0][0].width, 0);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002398
daniel@transgaming.com61208202011-03-21 16:38:50 +00002399 HRESULT result = device->CreateCubeTexture(mImageArray[0][0].width, levels, D3DUSAGE_RENDERTARGET, format, D3DPOOL_DEFAULT, &texture, NULL);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002400
2401 if (FAILED(result))
2402 {
2403 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002404 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002405 }
2406
2407 if (mTexture != NULL)
2408 {
2409 int levels = levelCount();
2410 for (int f = 0; f < 6; f++)
2411 {
2412 for (int i = 0; i < levels; i++)
2413 {
2414 IDirect3DSurface9 *source;
2415 result = mTexture->GetCubeMapSurface(static_cast<D3DCUBEMAP_FACES>(f), i, &source);
2416
2417 if (FAILED(result))
2418 {
2419 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
2420
2421 texture->Release();
2422
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002423 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002424 }
2425
2426 IDirect3DSurface9 *dest;
2427 result = texture->GetCubeMapSurface(static_cast<D3DCUBEMAP_FACES>(f), i, &dest);
2428
2429 if (FAILED(result))
2430 {
2431 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
2432
2433 texture->Release();
2434 source->Release();
2435
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002436 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002437 }
2438
2439 display->endScene();
2440 result = device->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
2441
2442 if (FAILED(result))
2443 {
2444 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
2445
2446 texture->Release();
2447 source->Release();
2448 dest->Release();
2449
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002450 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002451 }
daniel@transgaming.coma1a86202011-08-09 13:41:08 +00002452
2453 source->Release();
2454 dest->Release();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002455 }
2456 }
2457 }
2458 }
2459
2460 if (mTexture != NULL)
2461 {
2462 mTexture->Release();
2463 }
2464
2465 mTexture = texture;
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002466 mDirtyImage = true;
daniel@transgaming.comaed18322011-03-21 16:38:13 +00002467 mIsRenderable = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002468}
2469
daniel@transgaming.com61208202011-03-21 16:38:50 +00002470void 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 +00002471{
daniel@transgaming.com61208202011-03-21 16:38:50 +00002472 redefineTexture(faceIndex, level, format, width, height, type);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002473
daniel@transgaming.com61208202011-03-21 16:38:50 +00002474 Texture::setImage(unpackAlignment, pixels, &mImageArray[faceIndex][level]);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002475}
2476
2477unsigned int TextureCubeMap::faceIndex(GLenum face)
2478{
2479 META_ASSERT(GL_TEXTURE_CUBE_MAP_NEGATIVE_X - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 1);
2480 META_ASSERT(GL_TEXTURE_CUBE_MAP_POSITIVE_Y - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 2);
2481 META_ASSERT(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 3);
2482 META_ASSERT(GL_TEXTURE_CUBE_MAP_POSITIVE_Z - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 4);
2483 META_ASSERT(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 5);
2484
2485 return face - GL_TEXTURE_CUBE_MAP_POSITIVE_X;
2486}
2487
daniel@transgaming.com61208202011-03-21 16:38:50 +00002488void TextureCubeMap::redefineTexture(int face, GLint level, GLenum format, GLsizei width, GLsizei height, GLenum type)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002489{
daniel@transgaming.com94a4f032011-03-21 16:38:55 +00002490 GLsizei textureWidth = mImageArray[0][0].width;
2491 GLsizei textureHeight = mImageArray[0][0].height;
2492 GLenum textureFormat = mImageArray[0][0].format;
2493 GLenum textureType = mImageArray[0][0].type;
2494
daniel@transgaming.com61208202011-03-21 16:38:50 +00002495 mImageArray[face][level].width = width;
2496 mImageArray[face][level].height = height;
2497 mImageArray[face][level].format = format;
2498 mImageArray[face][level].type = type;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002499
daniel@transgaming.comc9ba4ad2011-11-09 17:44:35 +00002500 if (mImageArray[face][level].surface != NULL)
2501 {
2502 mImageArray[face][level].surface->Release();
2503 mImageArray[face][level].surface = NULL;
2504 mImageArray[face][level].dirty = true;
2505 }
2506
2507 createSurface(&mImageArray[face][level]);
2508
daniel@transgaming.com61208202011-03-21 16:38:50 +00002509 if (!mTexture)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002510 {
daniel@transgaming.com61208202011-03-21 16:38:50 +00002511 return;
2512 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002513
daniel@transgaming.com94a4f032011-03-21 16:38:55 +00002514 bool sizeOkay = (textureWidth >> level == width);
2515 bool textureOkay = (sizeOkay && textureFormat == format && textureType == type);
daniel@transgaming.com61208202011-03-21 16:38:50 +00002516
daniel@transgaming.comc9ba4ad2011-11-09 17:44:35 +00002517 if (!textureOkay)
daniel@transgaming.com61208202011-03-21 16:38:50 +00002518 {
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002519 for (int i = 0; i < IMPLEMENTATION_MAX_TEXTURE_LEVELS; i++)
2520 {
2521 for (int f = 0; f < 6; f++)
2522 {
daniel@transgaming.comc9ba4ad2011-11-09 17:44:35 +00002523 mImageArray[f][i].dirty = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002524 }
2525 }
2526
2527 if (mTexture != NULL)
2528 {
2529 mTexture->Release();
2530 mTexture = NULL;
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002531 mDirtyImage = true;
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002532 mIsRenderable = false;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002533 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002534 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002535}
2536
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002537void 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 +00002538{
2539 IDirect3DSurface9 *renderTarget = source->getRenderTarget();
2540
2541 if (!renderTarget)
2542 {
2543 ERR("Failed to retrieve the render target.");
2544 return error(GL_OUT_OF_MEMORY);
2545 }
2546
2547 unsigned int faceindex = faceIndex(target);
daniel@transgaming.com61208202011-03-21 16:38:50 +00002548 redefineTexture(faceindex, level, format, width, height, GL_UNSIGNED_BYTE);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002549
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00002550 if (!mImageArray[faceindex][level].isRenderable())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002551 {
daniel@transgaming.comb6276992011-03-29 00:58:18 +00002552 copyToImage(&mImageArray[faceindex][level], 0, 0, x, y, width, height, renderTarget);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002553 }
2554 else
2555 {
daniel@transgaming.com61208202011-03-21 16:38:50 +00002556 if (!mTexture || !mIsRenderable)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002557 {
2558 convertToRenderTarget();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002559 }
daniel@transgaming.com3b3c1d42011-06-08 20:38:09 +00002560
2561 updateTexture();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002562
2563 ASSERT(width == height);
2564
2565 if (width > 0 && level < levelCount())
2566 {
2567 RECT sourceRect = transformPixelRect(x, y, width, height, source->getColorbuffer()->getHeight());
2568 sourceRect.left = clamp(sourceRect.left, 0, source->getColorbuffer()->getWidth());
2569 sourceRect.top = clamp(sourceRect.top, 0, source->getColorbuffer()->getHeight());
2570 sourceRect.right = clamp(sourceRect.right, 0, source->getColorbuffer()->getWidth());
2571 sourceRect.bottom = clamp(sourceRect.bottom, 0, source->getColorbuffer()->getHeight());
2572
daniel@transgaming.comeef864a2011-04-22 11:33:27 +00002573 GLint destYOffset = transformPixelYOffset(0, height, mImageArray[faceindex][level].width);
2574
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002575 IDirect3DSurface9 *dest = getCubeMapSurface(target, level);
2576
daniel@transgaming.comeef864a2011-04-22 11:33:27 +00002577 getBlitter()->copy(source->getRenderTarget(), sourceRect, format, 0, destYOffset, dest);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002578 dest->Release();
2579 }
2580 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002581}
2582
daniel@transgaming.comb612f882011-11-09 17:44:31 +00002583IDirect3DSurface9 *TextureCubeMap::getCubeMapSurface(GLenum target, unsigned int level)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002584{
2585 if (mTexture == NULL)
2586 {
2587 UNREACHABLE();
2588 return NULL;
2589 }
2590
2591 IDirect3DSurface9 *surface = NULL;
2592
daniel@transgaming.comb612f882011-11-09 17:44:31 +00002593 HRESULT hr = mTexture->GetCubeMapSurface(es2dx::ConvertCubeFace(target), level, &surface);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002594
2595 return (SUCCEEDED(hr)) ? surface : NULL;
2596}
2597
2598void TextureCubeMap::copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source)
2599{
2600 GLsizei size = mImageArray[faceIndex(target)][level].width;
2601
2602 if (xoffset + width > size || yoffset + height > size)
2603 {
2604 return error(GL_INVALID_VALUE);
2605 }
2606
2607 IDirect3DSurface9 *renderTarget = source->getRenderTarget();
2608
2609 if (!renderTarget)
2610 {
2611 ERR("Failed to retrieve the render target.");
2612 return error(GL_OUT_OF_MEMORY);
2613 }
2614
2615 unsigned int faceindex = faceIndex(target);
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00002616 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 +00002617
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00002618 if (!mImageArray[faceindex][level].isRenderable() || (!mTexture && !isComplete()))
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002619 {
daniel@transgaming.comb6276992011-03-29 00:58:18 +00002620 copyToImage(&mImageArray[faceindex][level], 0, 0, x, y, width, height, renderTarget);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002621 }
2622 else
2623 {
daniel@transgaming.com61208202011-03-21 16:38:50 +00002624 if (!mTexture || !mIsRenderable)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002625 {
2626 convertToRenderTarget();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002627 }
daniel@transgaming.com61208202011-03-21 16:38:50 +00002628
2629 updateTexture();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002630
2631 if (level < levelCount())
2632 {
2633 RECT sourceRect = transformPixelRect(x, y, width, height, source->getColorbuffer()->getHeight());
2634 sourceRect.left = clamp(sourceRect.left, 0, source->getColorbuffer()->getWidth());
2635 sourceRect.top = clamp(sourceRect.top, 0, source->getColorbuffer()->getHeight());
2636 sourceRect.right = clamp(sourceRect.right, 0, source->getColorbuffer()->getWidth());
2637 sourceRect.bottom = clamp(sourceRect.bottom, 0, source->getColorbuffer()->getHeight());
2638
2639 GLint destYOffset = transformPixelYOffset(yoffset, height, mImageArray[faceindex][level].width);
2640
2641 IDirect3DSurface9 *dest = getCubeMapSurface(target, level);
2642
daniel@transgaming.comeef864a2011-04-22 11:33:27 +00002643 getBlitter()->copy(source->getRenderTarget(), sourceRect, mImageArray[0][0].format, xoffset, destYOffset, dest);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002644 dest->Release();
2645 }
2646 }
2647}
2648
2649bool TextureCubeMap::isCubeComplete() const
2650{
2651 if (mImageArray[0][0].width == 0)
2652 {
2653 return false;
2654 }
2655
2656 for (unsigned int f = 1; f < 6; f++)
2657 {
2658 if (mImageArray[f][0].width != mImageArray[0][0].width
2659 || mImageArray[f][0].format != mImageArray[0][0].format)
2660 {
2661 return false;
2662 }
2663 }
2664
2665 return true;
2666}
2667
2668void TextureCubeMap::generateMipmaps()
2669{
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00002670 if (!isCubeComplete())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002671 {
2672 return error(GL_INVALID_OPERATION);
2673 }
2674
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00002675 if (!getContext()->supportsNonPower2Texture())
2676 {
2677 if (!isPow2(mImageArray[0][0].width))
2678 {
2679 return error(GL_INVALID_OPERATION);
2680 }
2681 }
2682
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002683 // Purge array levels 1 through q and reset them to represent the generated mipmap levels.
2684 unsigned int q = log2(mImageArray[0][0].width);
2685 for (unsigned int f = 0; f < 6; f++)
2686 {
2687 for (unsigned int i = 1; i <= q; i++)
2688 {
2689 if (mImageArray[f][i].surface != NULL)
2690 {
2691 mImageArray[f][i].surface->Release();
2692 mImageArray[f][i].surface = NULL;
2693 }
2694
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002695 mImageArray[f][i].width = std::max(mImageArray[f][0].width >> i, 1);
2696 mImageArray[f][i].height = mImageArray[f][i].width;
daniel@transgaming.com0bd22bc2011-03-21 16:38:26 +00002697 mImageArray[f][i].format = mImageArray[f][0].format;
2698 mImageArray[f][i].type = mImageArray[f][0].type;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002699 }
2700 }
2701
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002702 if (mIsRenderable)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002703 {
2704 if (mTexture == NULL)
2705 {
2706 return;
2707 }
2708
2709 for (unsigned int f = 0; f < 6; f++)
2710 {
2711 for (unsigned int i = 1; i <= q; i++)
2712 {
2713 IDirect3DSurface9 *upper = getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i-1);
2714 IDirect3DSurface9 *lower = getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i);
2715
2716 if (upper != NULL && lower != NULL)
2717 {
2718 getBlitter()->boxFilter(upper, lower);
2719 }
2720
2721 if (upper != NULL) upper->Release();
2722 if (lower != NULL) lower->Release();
daniel@transgaming.com61208202011-03-21 16:38:50 +00002723
2724 mImageArray[f][i].dirty = false;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002725 }
2726 }
2727 }
2728 else
2729 {
2730 for (unsigned int f = 0; f < 6; f++)
2731 {
2732 for (unsigned int i = 1; i <= q; i++)
2733 {
daniel@transgaming.com61208202011-03-21 16:38:50 +00002734 createSurface(&mImageArray[f][i]);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002735 if (mImageArray[f][i].surface == NULL)
2736 {
2737 return error(GL_OUT_OF_MEMORY);
2738 }
2739
2740 if (FAILED(D3DXLoadSurfaceFromSurface(mImageArray[f][i].surface, NULL, NULL, mImageArray[f][i - 1].surface, NULL, NULL, D3DX_FILTER_BOX, 0)))
2741 {
2742 ERR(" failed to load filter %d to %d.", i - 1, i);
2743 }
2744
2745 mImageArray[f][i].dirty = true;
2746 }
2747 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002748 }
2749}
2750
2751Renderbuffer *TextureCubeMap::getRenderbuffer(GLenum target)
2752{
2753 if (!IsCubemapTextureTarget(target))
2754 {
2755 return error(GL_INVALID_OPERATION, (Renderbuffer *)NULL);
2756 }
2757
2758 unsigned int face = faceIndex(target);
2759
2760 if (mFaceProxies[face].get() == NULL)
2761 {
2762 mFaceProxies[face].set(new Renderbuffer(id(), new Colorbuffer(this, target)));
2763 }
2764
2765 return mFaceProxies[face].get();
2766}
2767
2768IDirect3DSurface9 *TextureCubeMap::getRenderTarget(GLenum target)
2769{
2770 ASSERT(IsCubemapTextureTarget(target));
2771
daniel@transgaming.com61208202011-03-21 16:38:50 +00002772 if (!mIsRenderable)
2773 {
2774 convertToRenderTarget();
2775 }
2776
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002777 if (mTexture == NULL)
2778 {
2779 return NULL;
2780 }
daniel@transgaming.com61208202011-03-21 16:38:50 +00002781
2782 updateTexture();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002783
2784 IDirect3DSurface9 *renderTarget = NULL;
2785 mTexture->GetCubeMapSurface(es2dx::ConvertCubeFace(target), 0, &renderTarget);
2786
2787 return renderTarget;
2788}
2789
daniel@transgaming.comb612f882011-11-09 17:44:31 +00002790}