blob: 8f3fee57bb0939906cfcee27a2457baee189640d [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
daniel@transgaming.comde631782011-11-09 17:45:04 +000032Image::Image()
daniel@transgaming.comdff362f2011-11-09 17:45:08 +000033 : mWidth(0), mHeight(0), mDirty(false), mSurface(NULL), mFormat(GL_NONE), mType(GL_UNSIGNED_BYTE)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +000034{
35}
36
daniel@transgaming.comde631782011-11-09 17:45:04 +000037Image::~Image()
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +000038{
daniel@transgaming.comdff362f2011-11-09 17:45:08 +000039 if (mSurface)
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +000040 {
daniel@transgaming.comdff362f2011-11-09 17:45:08 +000041 mSurface->Release();
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +000042 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +000043}
44
daniel@transgaming.comdff362f2011-11-09 17:45:08 +000045void Image::redefine(GLenum format, GLsizei width, GLsizei height, GLenum type)
46{
daniel@transgaming.com4c0a7712011-11-09 17:45:19 +000047 if (mWidth != width ||
48 mHeight != height ||
49 mFormat != format ||
50 mType != type)
51 {
52 if (mSurface)
53 {
54 mSurface->Release();
55 mSurface = NULL;
56 }
57 }
58
daniel@transgaming.comdff362f2011-11-09 17:45:08 +000059 mWidth = width;
60 mHeight = height;
61 mFormat = format;
62 mType = type;
daniel@transgaming.comdff362f2011-11-09 17:45:08 +000063}
64
65void Image::createSurface()
66{
67 if(mSurface)
68 {
69 return;
70 }
71
72 IDirect3DTexture9 *newTexture = NULL;
73 IDirect3DSurface9 *newSurface = NULL;
74
75 if (mWidth != 0 && mHeight != 0)
76 {
77 int levelToFetch = 0;
78 GLsizei requestWidth = mWidth;
79 GLsizei requestHeight = mHeight;
80 if (IsCompressed(mFormat) && (mWidth % 4 != 0 || mHeight % 4 != 0))
81 {
82 bool isMult4 = false;
83 int upsampleCount = 0;
84 while (!isMult4)
85 {
86 requestWidth <<= 1;
87 requestHeight <<= 1;
88 upsampleCount++;
89 if (requestWidth % 4 == 0 && requestHeight % 4 == 0)
90 {
91 isMult4 = true;
92 }
93 }
94 levelToFetch = upsampleCount;
95 }
96
97 HRESULT result = getDevice()->CreateTexture(requestWidth, requestHeight, levelToFetch + 1, NULL, getD3DFormat(),
98 D3DPOOL_SYSTEMMEM, &newTexture, NULL);
99
100 if (FAILED(result))
101 {
102 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com73de05a2011-11-09 17:45:24 +0000103 ERR("Creating image surface failed.");
daniel@transgaming.comdff362f2011-11-09 17:45:08 +0000104 return error(GL_OUT_OF_MEMORY);
105 }
106
107 newTexture->GetSurfaceLevel(levelToFetch, &newSurface);
108 newTexture->Release();
109 }
110
111 mSurface = newSurface;
daniel@transgaming.com839fb9b2011-11-09 17:45:43 +0000112 mDirty = false;
daniel@transgaming.comdff362f2011-11-09 17:45:08 +0000113}
114
daniel@transgaming.com5cce3ff2011-11-09 17:45:14 +0000115HRESULT Image::lock(D3DLOCKED_RECT *lockedRect, const RECT *rect)
116{
117 createSurface();
118
119 HRESULT result = D3DERR_INVALIDCALL;
120
121 if (mSurface)
122 {
123 result = mSurface->LockRect(lockedRect, rect, 0);
124 ASSERT(SUCCEEDED(result));
125
126 mDirty = true;
127 }
128
129 return result;
130}
131
132void Image::unlock()
133{
134 if (mSurface)
135 {
136 HRESULT result = mSurface->UnlockRect();
137 ASSERT(SUCCEEDED(result));
138 }
139}
140
daniel@transgaming.comde631782011-11-09 17:45:04 +0000141bool Image::isRenderable() const
daniel@transgaming.com549bdef2011-03-29 00:57:01 +0000142{
143 switch(getD3DFormat())
144 {
145 case D3DFMT_L8:
146 case D3DFMT_A8L8:
147 case D3DFMT_DXT1:
gman@chromium.org50c526d2011-08-10 05:19:44 +0000148 case D3DFMT_DXT3:
149 case D3DFMT_DXT5:
daniel@transgaming.com549bdef2011-03-29 00:57:01 +0000150 return false;
151 case D3DFMT_A8R8G8B8:
152 case D3DFMT_X8R8G8B8:
153 case D3DFMT_A16B16G16R16F:
154 case D3DFMT_A32B32G32R32F:
155 return true;
156 default:
157 UNREACHABLE();
158 }
159
160 return false;
161}
162
daniel@transgaming.comde631782011-11-09 17:45:04 +0000163D3DFORMAT Image::getD3DFormat() const
daniel@transgaming.com549bdef2011-03-29 00:57:01 +0000164{
daniel@transgaming.comdff362f2011-11-09 17:45:08 +0000165 if (mFormat == GL_COMPRESSED_RGB_S3TC_DXT1_EXT ||
166 mFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT)
daniel@transgaming.com549bdef2011-03-29 00:57:01 +0000167 {
168 return D3DFMT_DXT1;
169 }
daniel@transgaming.comdff362f2011-11-09 17:45:08 +0000170 else if (mFormat == GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE)
gman@chromium.org50c526d2011-08-10 05:19:44 +0000171 {
172 return D3DFMT_DXT3;
173 }
daniel@transgaming.comdff362f2011-11-09 17:45:08 +0000174 else if (mFormat == GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE)
gman@chromium.org50c526d2011-08-10 05:19:44 +0000175 {
176 return D3DFMT_DXT5;
177 }
daniel@transgaming.comdff362f2011-11-09 17:45:08 +0000178 else if (mType == GL_FLOAT)
daniel@transgaming.com549bdef2011-03-29 00:57:01 +0000179 {
180 return D3DFMT_A32B32G32R32F;
181 }
daniel@transgaming.comdff362f2011-11-09 17:45:08 +0000182 else if (mType == GL_HALF_FLOAT_OES)
daniel@transgaming.com549bdef2011-03-29 00:57:01 +0000183 {
184 return D3DFMT_A16B16G16R16F;
185 }
daniel@transgaming.comdff362f2011-11-09 17:45:08 +0000186 else if (mType == GL_UNSIGNED_BYTE)
daniel@transgaming.com549bdef2011-03-29 00:57:01 +0000187 {
daniel@transgaming.comdff362f2011-11-09 17:45:08 +0000188 if (mFormat == GL_LUMINANCE && getContext()->supportsLuminanceTextures())
daniel@transgaming.com549bdef2011-03-29 00:57:01 +0000189 {
190 return D3DFMT_L8;
191 }
daniel@transgaming.comdff362f2011-11-09 17:45:08 +0000192 else if (mFormat == GL_LUMINANCE_ALPHA && getContext()->supportsLuminanceAlphaTextures())
daniel@transgaming.com549bdef2011-03-29 00:57:01 +0000193 {
194 return D3DFMT_A8L8;
195 }
daniel@transgaming.comdff362f2011-11-09 17:45:08 +0000196 else if (mFormat == GL_RGB)
daniel@transgaming.com549bdef2011-03-29 00:57:01 +0000197 {
198 return D3DFMT_X8R8G8B8;
199 }
200
201 return D3DFMT_A8R8G8B8;
202 }
203
204 return D3DFMT_A8R8G8B8;
205}
206
daniel@transgaming.com73de05a2011-11-09 17:45:24 +0000207IDirect3DSurface9 *Image::getSurface()
208{
209 createSurface();
210
211 return mSurface;
212}
213
daniel@transgaming.comf749f0e2011-11-09 17:45:34 +0000214// Store the pixel rectangle designated by xoffset,yoffset,width,height with pixels stored as format/type at input
215// into the target pixel rectangle at output with outputPitch bytes in between each line.
daniel@transgaming.com0c67f3c2011-11-09 17:45:38 +0000216void Image::loadData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum type,
daniel@transgaming.comf749f0e2011-11-09 17:45:34 +0000217 GLint unpackAlignment, const void *input, size_t outputPitch, void *output) const
218{
daniel@transgaming.com0c67f3c2011-11-09 17:45:38 +0000219 GLsizei inputPitch = -ComputePitch(width, mFormat, type, unpackAlignment);
daniel@transgaming.comf749f0e2011-11-09 17:45:34 +0000220 input = ((char*)input) - inputPitch * (height - 1);
221
222 switch (type)
223 {
224 case GL_UNSIGNED_BYTE:
daniel@transgaming.com0c67f3c2011-11-09 17:45:38 +0000225 switch (mFormat)
daniel@transgaming.comf749f0e2011-11-09 17:45:34 +0000226 {
227 case GL_ALPHA:
228 loadAlphaData(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
229 break;
230 case GL_LUMINANCE:
231 loadLuminanceData(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output, getD3DFormat() == D3DFMT_L8);
232 break;
233 case GL_LUMINANCE_ALPHA:
234 loadLuminanceAlphaData(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output, getD3DFormat() == D3DFMT_A8L8);
235 break;
236 case GL_RGB:
237 loadRGBUByteData(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
238 break;
239 case GL_RGBA:
240 if (supportsSSE2())
241 {
242 loadRGBAUByteDataSSE2(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
243 }
244 else
245 {
246 loadRGBAUByteData(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
247 }
248 break;
249 case GL_BGRA_EXT:
250 loadBGRAData(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
251 break;
252 default: UNREACHABLE();
253 }
254 break;
255 case GL_UNSIGNED_SHORT_5_6_5:
daniel@transgaming.com0c67f3c2011-11-09 17:45:38 +0000256 switch (mFormat)
daniel@transgaming.comf749f0e2011-11-09 17:45:34 +0000257 {
258 case GL_RGB:
259 loadRGB565Data(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
260 break;
261 default: UNREACHABLE();
262 }
263 break;
264 case GL_UNSIGNED_SHORT_4_4_4_4:
daniel@transgaming.com0c67f3c2011-11-09 17:45:38 +0000265 switch (mFormat)
daniel@transgaming.comf749f0e2011-11-09 17:45:34 +0000266 {
267 case GL_RGBA:
268 loadRGBA4444Data(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
269 break;
270 default: UNREACHABLE();
271 }
272 break;
273 case GL_UNSIGNED_SHORT_5_5_5_1:
daniel@transgaming.com0c67f3c2011-11-09 17:45:38 +0000274 switch (mFormat)
daniel@transgaming.comf749f0e2011-11-09 17:45:34 +0000275 {
276 case GL_RGBA:
277 loadRGBA5551Data(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
278 break;
279 default: UNREACHABLE();
280 }
281 break;
282 case GL_FLOAT:
daniel@transgaming.com0c67f3c2011-11-09 17:45:38 +0000283 switch (mFormat)
daniel@transgaming.comf749f0e2011-11-09 17:45:34 +0000284 {
285 // float textures are converted to RGBA, not BGRA, as they're stored that way in D3D
286 case GL_ALPHA:
287 loadAlphaFloatData(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
288 break;
289 case GL_LUMINANCE:
290 loadLuminanceFloatData(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
291 break;
292 case GL_LUMINANCE_ALPHA:
293 loadLuminanceAlphaFloatData(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
294 break;
295 case GL_RGB:
296 loadRGBFloatData(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
297 break;
298 case GL_RGBA:
299 loadRGBAFloatData(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
300 break;
301 default: UNREACHABLE();
302 }
303 break;
304 case GL_HALF_FLOAT_OES:
daniel@transgaming.com0c67f3c2011-11-09 17:45:38 +0000305 switch (mFormat)
daniel@transgaming.comf749f0e2011-11-09 17:45:34 +0000306 {
307 // float textures are converted to RGBA, not BGRA, as they're stored that way in D3D
308 case GL_ALPHA:
309 loadAlphaHalfFloatData(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
310 break;
311 case GL_LUMINANCE:
312 loadLuminanceHalfFloatData(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
313 break;
314 case GL_LUMINANCE_ALPHA:
315 loadLuminanceAlphaHalfFloatData(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
316 break;
317 case GL_RGB:
318 loadRGBHalfFloatData(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
319 break;
320 case GL_RGBA:
321 loadRGBAHalfFloatData(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
322 break;
323 default: UNREACHABLE();
324 }
325 break;
326 default: UNREACHABLE();
327 }
328}
329
330void Image::loadAlphaData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
331 int inputPitch, const void *input, size_t outputPitch, void *output) const
332{
333 const unsigned char *source = NULL;
334 unsigned char *dest = NULL;
335
336 for (int y = 0; y < height; y++)
337 {
338 source = static_cast<const unsigned char*>(input) + y * inputPitch;
339 dest = static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 4;
340 for (int x = 0; x < width; x++)
341 {
342 dest[4 * x + 0] = 0;
343 dest[4 * x + 1] = 0;
344 dest[4 * x + 2] = 0;
345 dest[4 * x + 3] = source[x];
346 }
347 }
348}
349
350void Image::loadAlphaFloatData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
351 int inputPitch, const void *input, size_t outputPitch, void *output) const
352{
353 const float *source = NULL;
354 float *dest = NULL;
355
356 for (int y = 0; y < height; y++)
357 {
358 source = reinterpret_cast<const float*>(static_cast<const unsigned char*>(input) + y * inputPitch);
359 dest = reinterpret_cast<float*>(static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 16);
360 for (int x = 0; x < width; x++)
361 {
362 dest[4 * x + 0] = 0;
363 dest[4 * x + 1] = 0;
364 dest[4 * x + 2] = 0;
365 dest[4 * x + 3] = source[x];
366 }
367 }
368}
369
370void Image::loadAlphaHalfFloatData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
371 int inputPitch, const void *input, size_t outputPitch, void *output) const
372{
373 const unsigned short *source = NULL;
374 unsigned short *dest = NULL;
375
376 for (int y = 0; y < height; y++)
377 {
378 source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch);
379 dest = reinterpret_cast<unsigned short*>(static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 8);
380 for (int x = 0; x < width; x++)
381 {
382 dest[4 * x + 0] = 0;
383 dest[4 * x + 1] = 0;
384 dest[4 * x + 2] = 0;
385 dest[4 * x + 3] = source[x];
386 }
387 }
388}
389
390void Image::loadLuminanceData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
391 int inputPitch, const void *input, size_t outputPitch, void *output, bool native) const
392{
393 const int destBytesPerPixel = native? 1: 4;
394 const unsigned char *source = NULL;
395 unsigned char *dest = NULL;
396
397 for (int y = 0; y < height; y++)
398 {
399 source = static_cast<const unsigned char*>(input) + y * inputPitch;
400 dest = static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * destBytesPerPixel;
401
402 if (!native) // BGRA8 destination format
403 {
404 for (int x = 0; x < width; x++)
405 {
406 dest[4 * x + 0] = source[x];
407 dest[4 * x + 1] = source[x];
408 dest[4 * x + 2] = source[x];
409 dest[4 * x + 3] = 0xFF;
410 }
411 }
412 else // L8 destination format
413 {
414 memcpy(dest, source, width);
415 }
416 }
417}
418
419void Image::loadLuminanceFloatData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
420 int inputPitch, const void *input, size_t outputPitch, void *output) const
421{
422 const float *source = NULL;
423 float *dest = NULL;
424
425 for (int y = 0; y < height; y++)
426 {
427 source = reinterpret_cast<const float*>(static_cast<const unsigned char*>(input) + y * inputPitch);
428 dest = reinterpret_cast<float*>(static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 16);
429 for (int x = 0; x < width; x++)
430 {
431 dest[4 * x + 0] = source[x];
432 dest[4 * x + 1] = source[x];
433 dest[4 * x + 2] = source[x];
434 dest[4 * x + 3] = 1.0f;
435 }
436 }
437}
438
439void Image::loadLuminanceHalfFloatData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
440 int inputPitch, const void *input, size_t outputPitch, void *output) const
441{
442 const unsigned short *source = NULL;
443 unsigned short *dest = NULL;
444
445 for (int y = 0; y < height; y++)
446 {
447 source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch);
448 dest = reinterpret_cast<unsigned short*>(static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 8);
449 for (int x = 0; x < width; x++)
450 {
451 dest[4 * x + 0] = source[x];
452 dest[4 * x + 1] = source[x];
453 dest[4 * x + 2] = source[x];
454 dest[4 * x + 3] = 0x3C00; // SEEEEEMMMMMMMMMM, S = 0, E = 15, M = 0: 16bit flpt representation of 1
455 }
456 }
457}
458
459void Image::loadLuminanceAlphaData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
460 int inputPitch, const void *input, size_t outputPitch, void *output, bool native) const
461{
462 const int destBytesPerPixel = native? 2: 4;
463 const unsigned char *source = NULL;
464 unsigned char *dest = NULL;
465
466 for (int y = 0; y < height; y++)
467 {
468 source = static_cast<const unsigned char*>(input) + y * inputPitch;
469 dest = static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * destBytesPerPixel;
470
471 if (!native) // BGRA8 destination format
472 {
473 for (int x = 0; x < width; x++)
474 {
475 dest[4 * x + 0] = source[2*x+0];
476 dest[4 * x + 1] = source[2*x+0];
477 dest[4 * x + 2] = source[2*x+0];
478 dest[4 * x + 3] = source[2*x+1];
479 }
480 }
481 else
482 {
483 memcpy(dest, source, width * 2);
484 }
485 }
486}
487
488void Image::loadLuminanceAlphaFloatData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
489 int inputPitch, const void *input, size_t outputPitch, void *output) const
490{
491 const float *source = NULL;
492 float *dest = NULL;
493
494 for (int y = 0; y < height; y++)
495 {
496 source = reinterpret_cast<const float*>(static_cast<const unsigned char*>(input) + y * inputPitch);
497 dest = reinterpret_cast<float*>(static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 16);
498 for (int x = 0; x < width; x++)
499 {
500 dest[4 * x + 0] = source[2*x+0];
501 dest[4 * x + 1] = source[2*x+0];
502 dest[4 * x + 2] = source[2*x+0];
503 dest[4 * x + 3] = source[2*x+1];
504 }
505 }
506}
507
508void Image::loadLuminanceAlphaHalfFloatData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
509 int inputPitch, const void *input, size_t outputPitch, void *output) const
510{
511 const unsigned short *source = NULL;
512 unsigned short *dest = NULL;
513
514 for (int y = 0; y < height; y++)
515 {
516 source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch);
517 dest = reinterpret_cast<unsigned short*>(static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 8);
518 for (int x = 0; x < width; x++)
519 {
520 dest[4 * x + 0] = source[2*x+0];
521 dest[4 * x + 1] = source[2*x+0];
522 dest[4 * x + 2] = source[2*x+0];
523 dest[4 * x + 3] = source[2*x+1];
524 }
525 }
526}
527
528void Image::loadRGBUByteData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
529 int inputPitch, const void *input, size_t outputPitch, void *output) const
530{
531 const unsigned char *source = NULL;
532 unsigned char *dest = NULL;
533
534 for (int y = 0; y < height; y++)
535 {
536 source = static_cast<const unsigned char*>(input) + y * inputPitch;
537 dest = static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 4;
538 for (int x = 0; x < width; x++)
539 {
540 dest[4 * x + 0] = source[x * 3 + 2];
541 dest[4 * x + 1] = source[x * 3 + 1];
542 dest[4 * x + 2] = source[x * 3 + 0];
543 dest[4 * x + 3] = 0xFF;
544 }
545 }
546}
547
548void Image::loadRGB565Data(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
549 int inputPitch, const void *input, size_t outputPitch, void *output) const
550{
551 const unsigned short *source = NULL;
552 unsigned char *dest = NULL;
553
554 for (int y = 0; y < height; y++)
555 {
556 source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch);
557 dest = static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 4;
558 for (int x = 0; x < width; x++)
559 {
560 unsigned short rgba = source[x];
561 dest[4 * x + 0] = ((rgba & 0x001F) << 3) | ((rgba & 0x001F) >> 2);
562 dest[4 * x + 1] = ((rgba & 0x07E0) >> 3) | ((rgba & 0x07E0) >> 9);
563 dest[4 * x + 2] = ((rgba & 0xF800) >> 8) | ((rgba & 0xF800) >> 13);
564 dest[4 * x + 3] = 0xFF;
565 }
566 }
567}
568
569void Image::loadRGBFloatData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
570 int inputPitch, const void *input, size_t outputPitch, void *output) const
571{
572 const float *source = NULL;
573 float *dest = NULL;
574
575 for (int y = 0; y < height; y++)
576 {
577 source = reinterpret_cast<const float*>(static_cast<const unsigned char*>(input) + y * inputPitch);
578 dest = reinterpret_cast<float*>(static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 16);
579 for (int x = 0; x < width; x++)
580 {
581 dest[4 * x + 0] = source[x * 3 + 0];
582 dest[4 * x + 1] = source[x * 3 + 1];
583 dest[4 * x + 2] = source[x * 3 + 2];
584 dest[4 * x + 3] = 1.0f;
585 }
586 }
587}
588
589void Image::loadRGBHalfFloatData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
590 int inputPitch, const void *input, size_t outputPitch, void *output) const
591{
592 const unsigned short *source = NULL;
593 unsigned short *dest = NULL;
594
595 for (int y = 0; y < height; y++)
596 {
597 source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch);
598 dest = reinterpret_cast<unsigned short*>(static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 8);
599 for (int x = 0; x < width; x++)
600 {
601 dest[4 * x + 0] = source[x * 3 + 0];
602 dest[4 * x + 1] = source[x * 3 + 1];
603 dest[4 * x + 2] = source[x * 3 + 2];
604 dest[4 * x + 3] = 0x3C00; // SEEEEEMMMMMMMMMM, S = 0, E = 15, M = 0: 16bit flpt representation of 1
605 }
606 }
607}
608
609void Image::loadRGBAUByteDataSSE2(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
610 int inputPitch, const void *input, size_t outputPitch, void *output) const
611{
612 const unsigned int *source = NULL;
613 unsigned int *dest = NULL;
614 __m128i brMask = _mm_set1_epi32(0x00ff00ff);
615
616 for (int y = 0; y < height; y++)
617 {
618 source = reinterpret_cast<const unsigned int*>(static_cast<const unsigned char*>(input) + y * inputPitch);
619 dest = reinterpret_cast<unsigned int*>(static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 4);
620 int x = 0;
621
622 // Make output writes aligned
623 for (x = 0; ((reinterpret_cast<intptr_t>(&dest[x]) & 15) != 0) && x < width; x++)
624 {
625 unsigned int rgba = source[x];
626 dest[x] = (_rotl(rgba, 16) & 0x00ff00ff) | (rgba & 0xff00ff00);
627 }
628
629 for (; x + 3 < width; x += 4)
630 {
631 __m128i sourceData = _mm_loadu_si128(reinterpret_cast<const __m128i*>(&source[x]));
632 // Mask out g and a, which don't change
633 __m128i gaComponents = _mm_andnot_si128(brMask, sourceData);
634 // Mask out b and r
635 __m128i brComponents = _mm_and_si128(sourceData, brMask);
636 // Swap b and r
637 __m128i brSwapped = _mm_shufflehi_epi16(_mm_shufflelo_epi16(brComponents, _MM_SHUFFLE(2, 3, 0, 1)), _MM_SHUFFLE(2, 3, 0, 1));
638 __m128i result = _mm_or_si128(gaComponents, brSwapped);
639 _mm_store_si128(reinterpret_cast<__m128i*>(&dest[x]), result);
640 }
641
642 // Perform leftover writes
643 for (; x < width; x++)
644 {
645 unsigned int rgba = source[x];
646 dest[x] = (_rotl(rgba, 16) & 0x00ff00ff) | (rgba & 0xff00ff00);
647 }
648 }
649}
650
651void Image::loadRGBAUByteData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
652 int inputPitch, const void *input, size_t outputPitch, void *output) const
653{
654 const unsigned int *source = NULL;
655 unsigned int *dest = NULL;
656 for (int y = 0; y < height; y++)
657 {
658 source = reinterpret_cast<const unsigned int*>(static_cast<const unsigned char*>(input) + y * inputPitch);
659 dest = reinterpret_cast<unsigned int*>(static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 4);
660
661 for (int x = 0; x < width; x++)
662 {
663 unsigned int rgba = source[x];
664 dest[x] = (_rotl(rgba, 16) & 0x00ff00ff) | (rgba & 0xff00ff00);
665 }
666 }
667}
668
669void Image::loadRGBA4444Data(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
670 int inputPitch, const void *input, size_t outputPitch, void *output) const
671{
672 const unsigned short *source = NULL;
673 unsigned char *dest = NULL;
674
675 for (int y = 0; y < height; y++)
676 {
677 source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch);
678 dest = static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 4;
679 for (int x = 0; x < width; x++)
680 {
681 unsigned short rgba = source[x];
682 dest[4 * x + 0] = ((rgba & 0x00F0) << 0) | ((rgba & 0x00F0) >> 4);
683 dest[4 * x + 1] = ((rgba & 0x0F00) >> 4) | ((rgba & 0x0F00) >> 8);
684 dest[4 * x + 2] = ((rgba & 0xF000) >> 8) | ((rgba & 0xF000) >> 12);
685 dest[4 * x + 3] = ((rgba & 0x000F) << 4) | ((rgba & 0x000F) >> 0);
686 }
687 }
688}
689
690void Image::loadRGBA5551Data(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
691 int inputPitch, const void *input, size_t outputPitch, void *output) const
692{
693 const unsigned short *source = NULL;
694 unsigned char *dest = NULL;
695
696 for (int y = 0; y < height; y++)
697 {
698 source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch);
699 dest = static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 4;
700 for (int x = 0; x < width; x++)
701 {
702 unsigned short rgba = source[x];
703 dest[4 * x + 0] = ((rgba & 0x003E) << 2) | ((rgba & 0x003E) >> 3);
704 dest[4 * x + 1] = ((rgba & 0x07C0) >> 3) | ((rgba & 0x07C0) >> 8);
705 dest[4 * x + 2] = ((rgba & 0xF800) >> 8) | ((rgba & 0xF800) >> 13);
706 dest[4 * x + 3] = (rgba & 0x0001) ? 0xFF : 0;
707 }
708 }
709}
710
711void Image::loadRGBAFloatData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
712 int inputPitch, const void *input, size_t outputPitch, void *output) const
713{
714 const float *source = NULL;
715 float *dest = NULL;
716
717 for (int y = 0; y < height; y++)
718 {
719 source = reinterpret_cast<const float*>(static_cast<const unsigned char*>(input) + y * inputPitch);
720 dest = reinterpret_cast<float*>(static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 16);
721 memcpy(dest, source, width * 16);
722 }
723}
724
725void Image::loadRGBAHalfFloatData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
726 int inputPitch, const void *input, size_t outputPitch, void *output) const
727{
728 const unsigned char *source = NULL;
729 unsigned char *dest = NULL;
730
731 for (int y = 0; y < height; y++)
732 {
733 source = static_cast<const unsigned char*>(input) + y * inputPitch;
734 dest = static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 8;
735 memcpy(dest, source, width * 8);
736 }
737}
738
739void Image::loadBGRAData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
740 int inputPitch, const void *input, size_t outputPitch, void *output) const
741{
742 const unsigned char *source = NULL;
743 unsigned char *dest = NULL;
744
745 for (int y = 0; y < height; y++)
746 {
747 source = static_cast<const unsigned char*>(input) + y * inputPitch;
748 dest = static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 4;
749 memcpy(dest, source, width*4);
750 }
751}
752
753void Image::loadCompressedData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
754 int inputPitch, const void *input, size_t outputPitch, void *output) const {
755 switch (getD3DFormat())
756 {
757 case D3DFMT_DXT1:
758 loadDXT1Data(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
759 break;
760 case D3DFMT_DXT3:
761 loadDXT3Data(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
762 break;
763 case D3DFMT_DXT5:
764 loadDXT5Data(xoffset, yoffset, width, height, inputPitch, input, outputPitch, output);
765 break;
766 }
767}
768
769static void FlipCopyDXT1BlockFull(const unsigned int* source, unsigned int* dest) {
770 // A DXT1 block layout is:
771 // [0-1] color0.
772 // [2-3] color1.
773 // [4-7] color bitmap, 2 bits per pixel.
774 // So each of the 4-7 bytes represents one line, flipping a block is just
775 // flipping those bytes.
776
777 // First 32-bits is two RGB565 colors shared by tile and does not need to be modified.
778 dest[0] = source[0];
779
780 // Second 32-bits contains 4 rows of 4 2-bit interpolants between the colors. All rows should be flipped.
781 dest[1] = (source[1] >> 24) |
782 ((source[1] << 8) & 0x00FF0000) |
783 ((source[1] >> 8) & 0x0000FF00) |
784 (source[1] << 24);
785}
786
787// Flips the first 2 lines of a DXT1 block in the y direction.
788static void FlipCopyDXT1BlockHalf(const unsigned int* source, unsigned int* dest) {
789 // See layout above.
790 dest[0] = source[0];
791 dest[1] = ((source[1] << 8) & 0x0000FF00) |
792 ((source[1] >> 8) & 0x000000FF);
793}
794
795// Flips a full DXT3 block in the y direction.
796static void FlipCopyDXT3BlockFull(const unsigned int* source, unsigned int* dest) {
797 // A DXT3 block layout is:
798 // [0-7] alpha bitmap, 4 bits per pixel.
799 // [8-15] a DXT1 block.
800
801 // First and Second 32 bits are 4bit per pixel alpha and need to be flipped.
802 dest[0] = (source[1] >> 16) | (source[1] << 16);
803 dest[1] = (source[0] >> 16) | (source[0] << 16);
804
805 // And flip the DXT1 block using the above function.
806 FlipCopyDXT1BlockFull(source + 2, dest + 2);
807}
808
809// Flips the first 2 lines of a DXT3 block in the y direction.
810static void FlipCopyDXT3BlockHalf(const unsigned int* source, unsigned int* dest) {
811 // See layout above.
812 dest[0] = (source[1] >> 16) | (source[1] << 16);
813 FlipCopyDXT1BlockHalf(source + 2, dest + 2);
814}
815
816// Flips a full DXT5 block in the y direction.
817static void FlipCopyDXT5BlockFull(const unsigned int* source, unsigned int* dest) {
818 // A DXT5 block layout is:
819 // [0] alpha0.
820 // [1] alpha1.
821 // [2-7] alpha bitmap, 3 bits per pixel.
822 // [8-15] a DXT1 block.
823
824 // The alpha bitmap doesn't easily map lines to bytes, so we have to
825 // interpret it correctly. Extracted from
826 // http://www.opengl.org/registry/specs/EXT/texture_compression_s3tc.txt :
827 //
828 // The 6 "bits" bytes of the block are decoded into one 48-bit integer:
829 //
830 // bits = bits_0 + 256 * (bits_1 + 256 * (bits_2 + 256 * (bits_3 +
831 // 256 * (bits_4 + 256 * bits_5))))
832 //
833 // bits is a 48-bit unsigned integer, from which a three-bit control code
834 // is extracted for a texel at location (x,y) in the block using:
835 //
836 // code(x,y) = bits[3*(4*y+x)+1..3*(4*y+x)+0]
837 //
838 // where bit 47 is the most significant and bit 0 is the least
839 // significant bit.
840 const unsigned char* sourceBytes = static_cast<const unsigned char*>(static_cast<const void*>(source));
841 unsigned char* destBytes = static_cast<unsigned char*>(static_cast<void*>(dest));
842 unsigned int line_0_1 = sourceBytes[2] + 256 * (sourceBytes[3] + 256 * sourceBytes[4]);
843 unsigned int line_2_3 = sourceBytes[5] + 256 * (sourceBytes[6] + 256 * sourceBytes[7]);
844 // swap lines 0 and 1 in line_0_1.
845 unsigned int line_1_0 = ((line_0_1 & 0x000fff) << 12) |
846 ((line_0_1 & 0xfff000) >> 12);
847 // swap lines 2 and 3 in line_2_3.
848 unsigned int line_3_2 = ((line_2_3 & 0x000fff) << 12) |
849 ((line_2_3 & 0xfff000) >> 12);
850 destBytes[0] = sourceBytes[0];
851 destBytes[1] = sourceBytes[1];
852 destBytes[2] = line_3_2 & 0xff;
853 destBytes[3] = (line_3_2 & 0xff00) >> 8;
854 destBytes[4] = (line_3_2 & 0xff0000) >> 16;
855 destBytes[5] = line_1_0 & 0xff;
856 destBytes[6] = (line_1_0 & 0xff00) >> 8;
857 destBytes[7] = (line_1_0 & 0xff0000) >> 16;
858
859 // And flip the DXT1 block using the above function.
860 FlipCopyDXT1BlockFull(source + 2, dest + 2);
861}
862
863// Flips the first 2 lines of a DXT5 block in the y direction.
864static void FlipCopyDXT5BlockHalf(const unsigned int* source, unsigned int* dest) {
865 // See layout above.
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_1_0 = ((line_0_1 & 0x000fff) << 12) |
870 ((line_0_1 & 0xfff000) >> 12);
871 destBytes[0] = sourceBytes[0];
872 destBytes[1] = sourceBytes[1];
873 destBytes[2] = line_1_0 & 0xff;
874 destBytes[3] = (line_1_0 & 0xff00) >> 8;
875 destBytes[4] = (line_1_0 & 0xff0000) >> 16;
876 FlipCopyDXT1BlockHalf(source + 2, dest + 2);
877}
878
879void Image::loadDXT1Data(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
880 int inputPitch, const void *input, size_t outputPitch, void *output) const
881{
882 ASSERT(xoffset % 4 == 0);
883 ASSERT(yoffset % 4 == 0);
884 ASSERT(width % 4 == 0 || width == 2 || width == 1);
885 ASSERT(inputPitch % 8 == 0);
886 ASSERT(outputPitch % 8 == 0);
887
888 const unsigned int *source = reinterpret_cast<const unsigned int*>(input);
889 unsigned int *dest = reinterpret_cast<unsigned int*>(output);
890
891 // Round width up in case it is less than 4.
892 int blocksAcross = (width + 3) / 4;
893 int intsAcross = blocksAcross * 2;
894
895 switch (height)
896 {
897 case 1:
898 for (int x = 0; x < intsAcross; x += 2)
899 {
900 // just copy the block
901 dest[x] = source[x];
902 dest[x + 1] = source[x + 1];
903 }
904 break;
905 case 2:
906 for (int x = 0; x < intsAcross; x += 2)
907 {
908 FlipCopyDXT1BlockHalf(source + x, dest + x);
909 }
910 break;
911 default:
912 ASSERT(height % 4 == 0);
913 for (int y = 0; y < height / 4; ++y)
914 {
915 const unsigned int *source = reinterpret_cast<const unsigned int*>(static_cast<const unsigned char*>(input) + y * inputPitch);
916 unsigned int *dest = reinterpret_cast<unsigned int*>(static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 8);
917
918 for (int x = 0; x < intsAcross; x += 2)
919 {
920 FlipCopyDXT1BlockFull(source + x, dest + x);
921 }
922 }
923 break;
924 }
925}
926
927void Image::loadDXT3Data(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
928 int inputPitch, const void *input, size_t outputPitch, void *output) const
929{
930 ASSERT(xoffset % 4 == 0);
931 ASSERT(yoffset % 4 == 0);
932 ASSERT(width % 4 == 0 || width == 2 || width == 1);
933 ASSERT(inputPitch % 16 == 0);
934 ASSERT(outputPitch % 16 == 0);
935
936 const unsigned int *source = reinterpret_cast<const unsigned int*>(input);
937 unsigned int *dest = reinterpret_cast<unsigned int*>(output);
938
939 // Round width up in case it is less than 4.
940 int blocksAcross = (width + 3) / 4;
941 int intsAcross = blocksAcross * 4;
942
943 switch (height)
944 {
945 case 1:
946 for (int x = 0; x < intsAcross; x += 4)
947 {
948 // just copy the block
949 dest[x] = source[x];
950 dest[x + 1] = source[x + 1];
951 dest[x + 2] = source[x + 2];
952 dest[x + 3] = source[x + 3];
953 }
954 break;
955 case 2:
956 for (int x = 0; x < intsAcross; x += 4)
957 {
958 FlipCopyDXT3BlockHalf(source + x, dest + x);
959 }
960 break;
961 default:
962 ASSERT(height % 4 == 0);
963 for (int y = 0; y < height / 4; ++y)
964 {
965 const unsigned int *source = reinterpret_cast<const unsigned int*>(static_cast<const unsigned char*>(input) + y * inputPitch);
966 unsigned int *dest = reinterpret_cast<unsigned int*>(static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 16);
967
968 for (int x = 0; x < intsAcross; x += 4)
969 {
970 FlipCopyDXT3BlockFull(source + x, dest + x);
971 }
972 }
973 break;
974 }
975}
976
977void Image::loadDXT5Data(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
978 int inputPitch, const void *input, size_t outputPitch, void *output) const
979{
980 ASSERT(xoffset % 4 == 0);
981 ASSERT(yoffset % 4 == 0);
982 ASSERT(width % 4 == 0 || width == 2 || width == 1);
983 ASSERT(inputPitch % 16 == 0);
984 ASSERT(outputPitch % 16 == 0);
985
986 const unsigned int *source = reinterpret_cast<const unsigned int*>(input);
987 unsigned int *dest = reinterpret_cast<unsigned int*>(output);
988
989 // Round width up in case it is less than 4.
990 int blocksAcross = (width + 3) / 4;
991 int intsAcross = blocksAcross * 4;
992
993 switch (height)
994 {
995 case 1:
996 for (int x = 0; x < intsAcross; x += 4)
997 {
998 // just copy the block
999 dest[x] = source[x];
1000 dest[x + 1] = source[x + 1];
1001 dest[x + 2] = source[x + 2];
1002 dest[x + 3] = source[x + 3];
1003 }
1004 break;
1005 case 2:
1006 for (int x = 0; x < intsAcross; x += 4)
1007 {
1008 FlipCopyDXT5BlockHalf(source + x, dest + x);
1009 }
1010 break;
1011 default:
1012 ASSERT(height % 4 == 0);
1013 for (int y = 0; y < height / 4; ++y)
1014 {
1015 const unsigned int *source = reinterpret_cast<const unsigned int*>(static_cast<const unsigned char*>(input) + y * inputPitch);
1016 unsigned int *dest = reinterpret_cast<unsigned int*>(static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 16);
1017
1018 for (int x = 0; x < intsAcross; x += 4)
1019 {
1020 FlipCopyDXT5BlockFull(source + x, dest + x);
1021 }
1022 }
1023 break;
1024 }
1025}
1026
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00001027Texture::Texture(GLuint id) : RefCountObject(id), mSerial(issueSerial())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001028{
1029 mMinFilter = GL_NEAREST_MIPMAP_LINEAR;
1030 mMagFilter = GL_LINEAR;
1031 mWrapS = GL_REPEAT;
1032 mWrapT = GL_REPEAT;
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00001033 mDirtyParameters = true;
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00001034
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00001035 mDirtyImages = true;
daniel@transgaming.comaed18322011-03-21 16:38:13 +00001036
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001037 mIsRenderable = false;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001038}
1039
1040Texture::~Texture()
1041{
1042}
1043
1044Blit *Texture::getBlitter()
1045{
1046 Context *context = getContext();
1047 return context->getBlitter();
1048}
1049
1050// Returns true on successful filter state update (valid enum parameter)
1051bool Texture::setMinFilter(GLenum filter)
1052{
1053 switch (filter)
1054 {
1055 case GL_NEAREST:
1056 case GL_LINEAR:
1057 case GL_NEAREST_MIPMAP_NEAREST:
1058 case GL_LINEAR_MIPMAP_NEAREST:
1059 case GL_NEAREST_MIPMAP_LINEAR:
1060 case GL_LINEAR_MIPMAP_LINEAR:
1061 {
1062 if (mMinFilter != filter)
1063 {
1064 mMinFilter = filter;
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00001065 mDirtyParameters = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001066 }
1067 return true;
1068 }
1069 default:
1070 return false;
1071 }
1072}
1073
1074// Returns true on successful filter state update (valid enum parameter)
1075bool Texture::setMagFilter(GLenum filter)
1076{
1077 switch (filter)
1078 {
1079 case GL_NEAREST:
1080 case GL_LINEAR:
1081 {
1082 if (mMagFilter != filter)
1083 {
1084 mMagFilter = filter;
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00001085 mDirtyParameters = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001086 }
1087 return true;
1088 }
1089 default:
1090 return false;
1091 }
1092}
1093
1094// Returns true on successful wrap state update (valid enum parameter)
1095bool Texture::setWrapS(GLenum wrap)
1096{
1097 switch (wrap)
1098 {
1099 case GL_REPEAT:
1100 case GL_CLAMP_TO_EDGE:
1101 case GL_MIRRORED_REPEAT:
1102 {
1103 if (mWrapS != wrap)
1104 {
1105 mWrapS = wrap;
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00001106 mDirtyParameters = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001107 }
1108 return true;
1109 }
1110 default:
1111 return false;
1112 }
1113}
1114
1115// Returns true on successful wrap state update (valid enum parameter)
1116bool Texture::setWrapT(GLenum wrap)
1117{
1118 switch (wrap)
1119 {
1120 case GL_REPEAT:
1121 case GL_CLAMP_TO_EDGE:
1122 case GL_MIRRORED_REPEAT:
1123 {
1124 if (mWrapT != wrap)
1125 {
1126 mWrapT = wrap;
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00001127 mDirtyParameters = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001128 }
1129 return true;
1130 }
1131 default:
1132 return false;
1133 }
1134}
1135
1136GLenum Texture::getMinFilter() const
1137{
1138 return mMinFilter;
1139}
1140
1141GLenum Texture::getMagFilter() const
1142{
1143 return mMagFilter;
1144}
1145
1146GLenum Texture::getWrapS() const
1147{
1148 return mWrapS;
1149}
1150
1151GLenum Texture::getWrapT() const
1152{
1153 return mWrapT;
1154}
1155
daniel@transgaming.com61208202011-03-21 16:38:50 +00001156void Texture::setImage(GLint unpackAlignment, const void *pixels, Image *image)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001157{
daniel@transgaming.com73de05a2011-11-09 17:45:24 +00001158 if (pixels != NULL)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001159 {
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001160 D3DLOCKED_RECT locked;
daniel@transgaming.com5cce3ff2011-11-09 17:45:14 +00001161 HRESULT result = image->lock(&locked, NULL);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001162
1163 if (SUCCEEDED(result))
1164 {
daniel@transgaming.com0c67f3c2011-11-09 17:45:38 +00001165 image->loadData(0, 0, image->getWidth(), image->getHeight(), image->getType(), unpackAlignment, pixels, locked.Pitch, locked.pBits);
daniel@transgaming.com5cce3ff2011-11-09 17:45:14 +00001166 image->unlock();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001167 }
1168
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00001169 mDirtyImages = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001170 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001171}
1172
daniel@transgaming.com61208202011-03-21 16:38:50 +00001173void Texture::setCompressedImage(GLsizei imageSize, const void *pixels, Image *image)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001174{
daniel@transgaming.com73de05a2011-11-09 17:45:24 +00001175 if (pixels != NULL)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001176 {
1177 D3DLOCKED_RECT locked;
daniel@transgaming.com5cce3ff2011-11-09 17:45:14 +00001178 HRESULT result = image->lock(&locked, NULL);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001179
1180 if (SUCCEEDED(result))
1181 {
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001182 int inputPitch = ComputeCompressedPitch(image->getWidth(), image->getFormat());
1183 int inputSize = ComputeCompressedSize(image->getWidth(), image->getHeight(), image->getFormat());
daniel@transgaming.comf749f0e2011-11-09 17:45:34 +00001184 image->loadCompressedData(0, 0, image->getWidth(), image->getHeight(), -inputPitch, static_cast<const char*>(pixels) + inputSize - inputPitch, locked.Pitch, locked.pBits);
daniel@transgaming.com5cce3ff2011-11-09 17:45:14 +00001185 image->unlock();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001186 }
1187
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00001188 mDirtyImages = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001189 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001190}
1191
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001192bool 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 +00001193{
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001194 if (width + xoffset > image->getWidth() || height + yoffset > image->getHeight())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001195 {
1196 error(GL_INVALID_VALUE);
1197 return false;
1198 }
1199
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001200 if (IsCompressed(image->getFormat()))
jbauman@chromium.orge2f954c2011-05-03 20:45:27 +00001201 {
1202 error(GL_INVALID_OPERATION);
1203 return false;
1204 }
1205
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001206 if (format != image->getFormat())
jbauman@chromium.orge2f954c2011-05-03 20:45:27 +00001207 {
1208 error(GL_INVALID_OPERATION);
1209 return false;
1210 }
1211
daniel@transgaming.com73de05a2011-11-09 17:45:24 +00001212 if (pixels != NULL)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001213 {
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001214 D3DLOCKED_RECT locked;
daniel@transgaming.com5cce3ff2011-11-09 17:45:14 +00001215 HRESULT result = image->lock(&locked, NULL);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001216
1217 if (SUCCEEDED(result))
1218 {
daniel@transgaming.com0c67f3c2011-11-09 17:45:38 +00001219 image->loadData(xoffset, transformPixelYOffset(yoffset, height, image->getHeight()), width, height, type, unpackAlignment, pixels, locked.Pitch, locked.pBits);
daniel@transgaming.com5cce3ff2011-11-09 17:45:14 +00001220 image->unlock();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001221 }
1222
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00001223 mDirtyImages = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001224 }
1225
1226 return true;
1227}
1228
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001229bool 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 +00001230{
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001231 if (width + xoffset > image->getWidth() || height + yoffset > image->getHeight())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001232 {
1233 error(GL_INVALID_VALUE);
1234 return false;
1235 }
1236
1237 if (format != getInternalFormat())
1238 {
1239 error(GL_INVALID_OPERATION);
1240 return false;
1241 }
1242
daniel@transgaming.com5cce3ff2011-11-09 17:45:14 +00001243 if (pixels != NULL)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001244 {
1245 RECT updateRegion;
1246 updateRegion.left = xoffset;
1247 updateRegion.right = xoffset + width;
1248 updateRegion.bottom = yoffset + height;
1249 updateRegion.top = yoffset;
1250
1251 D3DLOCKED_RECT locked;
daniel@transgaming.com5cce3ff2011-11-09 17:45:14 +00001252 HRESULT result = image->lock(&locked, &updateRegion);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001253
1254 if (SUCCEEDED(result))
1255 {
1256 int inputPitch = ComputeCompressedPitch(width, format);
1257 int inputSize = ComputeCompressedSize(width, height, format);
daniel@transgaming.comf749f0e2011-11-09 17:45:34 +00001258 image->loadCompressedData(xoffset, transformPixelYOffset(yoffset, height, image->getHeight()), width, height, -inputPitch, static_cast<const char*>(pixels) + inputSize - inputPitch, locked.Pitch, locked.pBits);
daniel@transgaming.com5cce3ff2011-11-09 17:45:14 +00001259 image->unlock();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001260 }
1261
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00001262 mDirtyImages = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001263 }
1264
1265 return true;
1266}
1267
daniel@transgaming.comb6276992011-03-29 00:58:18 +00001268// This implements glCopyTex[Sub]Image2D for non-renderable internal texture formats and incomplete textures
1269void 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 +00001270{
1271 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.comb6276992011-03-29 00:58:18 +00001272 IDirect3DSurface9 *renderTargetData = NULL;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001273 D3DSURFACE_DESC description;
1274 renderTarget->GetDesc(&description);
1275
daniel@transgaming.comb6276992011-03-29 00:58:18 +00001276 HRESULT result = device->CreateOffscreenPlainSurface(description.Width, description.Height, description.Format, D3DPOOL_SYSTEMMEM, &renderTargetData, NULL);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001277
daniel@transgaming.comb6276992011-03-29 00:58:18 +00001278 if (FAILED(result))
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001279 {
1280 ERR("Could not create matching destination surface.");
1281 return error(GL_OUT_OF_MEMORY);
1282 }
1283
daniel@transgaming.comb6276992011-03-29 00:58:18 +00001284 result = device->GetRenderTargetData(renderTarget, renderTargetData);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001285
daniel@transgaming.comb6276992011-03-29 00:58:18 +00001286 if (FAILED(result))
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001287 {
1288 ERR("GetRenderTargetData unexpectedly failed.");
daniel@transgaming.comb6276992011-03-29 00:58:18 +00001289 renderTargetData->Release();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001290 return error(GL_OUT_OF_MEMORY);
1291 }
1292
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001293 RECT sourceRect = transformPixelRect(x, y, width, height, description.Height);
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001294 int destYOffset = transformPixelYOffset(yoffset, height, image->getHeight());
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001295 RECT destRect = {xoffset, destYOffset, xoffset + width, destYOffset + height};
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001296
daniel@transgaming.comb6276992011-03-29 00:58:18 +00001297 if (image->isRenderable())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001298 {
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001299 result = D3DXLoadSurfaceFromSurface(image->getSurface(), NULL, &destRect, renderTargetData, NULL, &sourceRect, D3DX_FILTER_BOX, 0);
daniel@transgaming.comb6276992011-03-29 00:58:18 +00001300
1301 if (FAILED(result))
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001302 {
daniel@transgaming.comb6276992011-03-29 00:58:18 +00001303 ERR("Copying surfaces unexpectedly failed.");
1304 renderTargetData->Release();
1305 return error(GL_OUT_OF_MEMORY);
1306 }
1307 }
1308 else
1309 {
1310 D3DLOCKED_RECT sourceLock = {0};
1311 result = renderTargetData->LockRect(&sourceLock, &sourceRect, 0);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001312
daniel@transgaming.comb6276992011-03-29 00:58:18 +00001313 if (FAILED(result))
1314 {
1315 ERR("Failed to lock the source surface (rectangle might be invalid).");
1316 renderTargetData->Release();
1317 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001318 }
1319
daniel@transgaming.comb6276992011-03-29 00:58:18 +00001320 D3DLOCKED_RECT destLock = {0};
daniel@transgaming.com5cce3ff2011-11-09 17:45:14 +00001321 result = image->lock(&destLock, &destRect);
daniel@transgaming.comb6276992011-03-29 00:58:18 +00001322
1323 if (FAILED(result))
1324 {
1325 ERR("Failed to lock the destination surface (rectangle might be invalid).");
1326 renderTargetData->UnlockRect();
1327 renderTargetData->Release();
1328 return error(GL_OUT_OF_MEMORY);
1329 }
1330
1331 if (destLock.pBits && sourceLock.pBits)
1332 {
1333 unsigned char *source = (unsigned char*)sourceLock.pBits;
1334 unsigned char *dest = (unsigned char*)destLock.pBits;
1335
1336 switch (description.Format)
1337 {
1338 case D3DFMT_X8R8G8B8:
1339 case D3DFMT_A8R8G8B8:
1340 switch(image->getD3DFormat())
1341 {
1342 case D3DFMT_L8:
1343 for(int y = 0; y < height; y++)
1344 {
1345 for(int x = 0; x < width; x++)
1346 {
1347 dest[x] = source[x * 4 + 2];
1348 }
1349
1350 source += sourceLock.Pitch;
1351 dest += destLock.Pitch;
1352 }
1353 break;
1354 case D3DFMT_A8L8:
1355 for(int y = 0; y < height; y++)
1356 {
1357 for(int x = 0; x < width; x++)
1358 {
1359 dest[x * 2 + 0] = source[x * 4 + 2];
1360 dest[x * 2 + 1] = source[x * 4 + 3];
1361 }
1362
1363 source += sourceLock.Pitch;
1364 dest += destLock.Pitch;
1365 }
1366 break;
1367 default:
1368 UNREACHABLE();
1369 }
1370 break;
1371 case D3DFMT_R5G6B5:
1372 switch(image->getD3DFormat())
1373 {
1374 case D3DFMT_L8:
1375 for(int y = 0; y < height; y++)
1376 {
1377 for(int x = 0; x < width; x++)
1378 {
1379 unsigned char red = source[x * 2 + 1] & 0xF8;
1380 dest[x] = red | (red >> 5);
1381 }
1382
1383 source += sourceLock.Pitch;
1384 dest += destLock.Pitch;
1385 }
1386 break;
1387 default:
1388 UNREACHABLE();
1389 }
1390 break;
1391 case D3DFMT_A1R5G5B5:
1392 switch(image->getD3DFormat())
1393 {
1394 case D3DFMT_L8:
1395 for(int y = 0; y < height; y++)
1396 {
1397 for(int x = 0; x < width; x++)
1398 {
1399 unsigned char red = source[x * 2 + 1] & 0x7C;
1400 dest[x] = (red << 1) | (red >> 4);
1401 }
1402
1403 source += sourceLock.Pitch;
1404 dest += destLock.Pitch;
1405 }
1406 break;
1407 case D3DFMT_A8L8:
1408 for(int y = 0; y < height; y++)
1409 {
1410 for(int x = 0; x < width; x++)
1411 {
1412 unsigned char red = source[x * 2 + 1] & 0x7C;
1413 dest[x * 2 + 0] = (red << 1) | (red >> 4);
1414 dest[x * 2 + 1] = (signed char)source[x * 2 + 1] >> 7;
1415 }
1416
1417 source += sourceLock.Pitch;
1418 dest += destLock.Pitch;
1419 }
1420 break;
1421 default:
1422 UNREACHABLE();
1423 }
1424 break;
1425 default:
1426 UNREACHABLE();
1427 }
1428 }
1429
daniel@transgaming.com5cce3ff2011-11-09 17:45:14 +00001430 image->unlock();
daniel@transgaming.comb6276992011-03-29 00:58:18 +00001431 renderTargetData->UnlockRect();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001432 }
1433
daniel@transgaming.comb6276992011-03-29 00:58:18 +00001434 renderTargetData->Release();
1435
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001436 image->markDirty();
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00001437 mDirtyImages = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001438}
1439
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001440IDirect3DBaseTexture9 *Texture::getTexture()
1441{
1442 if (!isComplete())
1443 {
1444 return NULL;
1445 }
1446
daniel@transgaming.comaed18322011-03-21 16:38:13 +00001447 if (!getBaseTexture())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001448 {
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001449 createTexture();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001450 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001451
daniel@transgaming.comc50edcb2011-03-21 16:38:40 +00001452 updateTexture();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001453
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001454 return getBaseTexture();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001455}
1456
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00001457bool Texture::hasDirtyParameters() const
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001458{
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00001459 return mDirtyParameters;
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00001460}
1461
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00001462bool Texture::hasDirtyImages() const
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00001463{
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00001464 return mDirtyImages;
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00001465}
1466
1467void Texture::resetDirty()
1468{
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00001469 mDirtyParameters = false;
1470 mDirtyImages = false;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001471}
1472
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00001473unsigned int Texture::getSerial() const
1474{
1475 return mSerial;
1476}
1477
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001478GLint Texture::creationLevels(GLsizei width, GLsizei height, GLint maxlevel) const
1479{
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00001480 if ((isPow2(width) && isPow2(height)) || getContext()->supportsNonPower2Texture())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001481 {
1482 return maxlevel;
1483 }
1484 else
1485 {
1486 // OpenGL ES 2.0 without GL_OES_texture_npot does not permit NPOT mipmaps.
1487 return 1;
1488 }
1489}
1490
1491GLint Texture::creationLevels(GLsizei size, GLint maxlevel) const
1492{
1493 return creationLevels(size, size, maxlevel);
1494}
1495
1496int Texture::levelCount() const
1497{
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001498 return getBaseTexture() ? getBaseTexture()->GetLevelCount() : 0;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001499}
1500
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00001501unsigned int Texture::issueSerial()
1502{
1503 return mCurrentSerial++;
1504}
1505
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001506Texture2D::Texture2D(GLuint id) : Texture(id)
1507{
1508 mTexture = NULL;
jbauman@chromium.orgae345802011-03-30 22:04:25 +00001509 mSurface = NULL;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001510}
1511
1512Texture2D::~Texture2D()
1513{
1514 mColorbufferProxy.set(NULL);
1515
1516 if (mTexture)
1517 {
1518 mTexture->Release();
1519 mTexture = NULL;
1520 }
jbauman@chromium.orgae345802011-03-30 22:04:25 +00001521
1522 if (mSurface)
1523 {
1524 mSurface->setBoundTexture(NULL);
1525 mSurface = NULL;
1526 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001527}
1528
1529GLenum Texture2D::getTarget() const
1530{
1531 return GL_TEXTURE_2D;
1532}
1533
daniel@transgaming.com61208202011-03-21 16:38:50 +00001534GLsizei Texture2D::getWidth() const
1535{
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001536 return mImageArray[0].getWidth();
daniel@transgaming.com61208202011-03-21 16:38:50 +00001537}
1538
1539GLsizei Texture2D::getHeight() const
1540{
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001541 return mImageArray[0].getHeight();
daniel@transgaming.com61208202011-03-21 16:38:50 +00001542}
1543
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001544GLenum Texture2D::getInternalFormat() const
1545{
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001546 return mImageArray[0].getFormat();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001547}
1548
daniel@transgaming.com61208202011-03-21 16:38:50 +00001549GLenum Texture2D::getType() const
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001550{
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001551 return mImageArray[0].getType();
daniel@transgaming.com61208202011-03-21 16:38:50 +00001552}
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001553
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00001554D3DFORMAT Texture2D::getD3DFormat() const
1555{
1556 return mImageArray[0].getD3DFormat();
1557}
1558
daniel@transgaming.com68ae2992011-11-09 17:44:49 +00001559void Texture2D::redefineImage(GLint level, GLenum format, GLsizei width, GLsizei height, GLenum type, bool forceRedefine)
daniel@transgaming.com61208202011-03-21 16:38:50 +00001560{
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001561 GLsizei textureWidth = mImageArray[0].getWidth();
1562 GLsizei textureHeight = mImageArray[0].getHeight();
1563 GLenum textureFormat = mImageArray[0].getFormat();
1564 GLenum textureType = mImageArray[0].getType();
daniel@transgaming.com94a4f032011-03-21 16:38:55 +00001565
daniel@transgaming.com68ae2992011-11-09 17:44:49 +00001566 mImageArray[level].redefine(format, width, height, type);
daniel@transgaming.comc9ba4ad2011-11-09 17:44:35 +00001567
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.comdff362f2011-11-09 17:45:08 +00001581 mImageArray[i].markDirty();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001582 }
1583
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00001584 mTexture->Release();
1585 mTexture = NULL;
1586 mDirtyImages = true;
1587 mIsRenderable = false;
jbauman@chromium.orgae345802011-03-30 22:04:25 +00001588
1589 if (mSurface)
1590 {
1591 mSurface->setBoundTexture(NULL);
1592 mSurface = NULL;
1593 }
apatrick@chromium.org57a2cd62011-06-08 00:04:07 +00001594
1595 mColorbufferProxy.set(NULL);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001596 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001597}
1598
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00001599void 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 +00001600{
daniel@transgaming.com68ae2992011-11-09 17:44:49 +00001601 redefineImage(level, format, width, height, type, false);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001602
daniel@transgaming.com61208202011-03-21 16:38:50 +00001603 Texture::setImage(unpackAlignment, pixels, &mImageArray[level]);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001604}
1605
jbauman@chromium.orgae345802011-03-30 22:04:25 +00001606void Texture2D::bindTexImage(egl::Surface *surface)
1607{
1608 GLenum format;
1609
1610 switch(surface->getFormat())
1611 {
1612 case D3DFMT_A8R8G8B8:
1613 format = GL_RGBA;
1614 break;
1615 case D3DFMT_X8R8G8B8:
1616 format = GL_RGB;
1617 break;
1618 default:
1619 UNIMPLEMENTED();
1620 return;
1621 }
1622
daniel@transgaming.com68ae2992011-11-09 17:44:49 +00001623 redefineImage(0, format, surface->getWidth(), surface->getHeight(), GL_UNSIGNED_BYTE, true);
jbauman@chromium.orgae345802011-03-30 22:04:25 +00001624
1625 IDirect3DTexture9 *texture = surface->getOffscreenTexture();
1626
1627 mTexture = texture;
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00001628 mDirtyImages = true;
jbauman@chromium.orgae345802011-03-30 22:04:25 +00001629 mIsRenderable = true;
1630 mSurface = surface;
1631 mSurface->setBoundTexture(this);
1632}
1633
1634void Texture2D::releaseTexImage()
1635{
daniel@transgaming.com68ae2992011-11-09 17:44:49 +00001636 redefineImage(0, GL_RGB, 0, 0, GL_UNSIGNED_BYTE, true);
jbauman@chromium.orgae345802011-03-30 22:04:25 +00001637}
1638
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00001639void Texture2D::setCompressedImage(GLint level, GLenum format, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001640{
daniel@transgaming.com68ae2992011-11-09 17:44:49 +00001641 redefineImage(level, format, width, height, GL_UNSIGNED_BYTE, false);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001642
daniel@transgaming.com61208202011-03-21 16:38:50 +00001643 Texture::setCompressedImage(imageSize, pixels, &mImageArray[level]);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001644}
1645
1646void Texture2D::commitRect(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height)
1647{
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001648 ASSERT(mImageArray[level].getSurface() != NULL);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001649
1650 if (level < levelCount())
1651 {
1652 IDirect3DSurface9 *destLevel = NULL;
1653 HRESULT result = mTexture->GetSurfaceLevel(level, &destLevel);
1654
1655 ASSERT(SUCCEEDED(result));
1656
1657 if (SUCCEEDED(result))
1658 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001659 Image *image = &mImageArray[level];
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001660
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001661 RECT sourceRect = transformPixelRect(xoffset, yoffset, width, height, image->getHeight());;
daniel@transgaming.comb612f882011-11-09 17:44:31 +00001662 POINT destPoint = {sourceRect.left, sourceRect.top};
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001663
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001664 result = getDevice()->UpdateSurface(image->getSurface(), &sourceRect, destLevel, &destPoint);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001665 ASSERT(SUCCEEDED(result));
1666
1667 destLevel->Release();
1668
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001669 image->markClean();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001670 }
1671 }
1672}
1673
1674void Texture2D::subImage(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels)
1675{
1676 if (Texture::subImage(xoffset, yoffset, width, height, format, type, unpackAlignment, pixels, &mImageArray[level]))
1677 {
1678 commitRect(level, xoffset, yoffset, width, height);
1679 }
1680}
1681
1682void Texture2D::subImageCompressed(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels)
1683{
1684 if (Texture::subImageCompressed(xoffset, yoffset, width, height, format, imageSize, pixels, &mImageArray[level]))
1685 {
1686 commitRect(level, xoffset, yoffset, width, height);
1687 }
1688}
1689
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00001690void Texture2D::copyImage(GLint level, GLenum format, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001691{
1692 IDirect3DSurface9 *renderTarget = source->getRenderTarget();
1693
1694 if (!renderTarget)
1695 {
1696 ERR("Failed to retrieve the render target.");
1697 return error(GL_OUT_OF_MEMORY);
1698 }
1699
daniel@transgaming.com68ae2992011-11-09 17:44:49 +00001700 redefineImage(level, format, width, height, GL_UNSIGNED_BYTE, false);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001701
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00001702 if (!mImageArray[level].isRenderable())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001703 {
daniel@transgaming.comb6276992011-03-29 00:58:18 +00001704 copyToImage(&mImageArray[level], 0, 0, x, y, width, height, renderTarget);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001705 }
1706 else
1707 {
daniel@transgaming.com61208202011-03-21 16:38:50 +00001708 if (!mTexture || !mIsRenderable)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001709 {
1710 convertToRenderTarget();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001711 }
daniel@transgaming.com3b3c1d42011-06-08 20:38:09 +00001712
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001713 mImageArray[level].markClean();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001714
1715 if (width != 0 && height != 0 && level < levelCount())
1716 {
1717 RECT sourceRect = transformPixelRect(x, y, width, height, source->getColorbuffer()->getHeight());
1718 sourceRect.left = clamp(sourceRect.left, 0, source->getColorbuffer()->getWidth());
1719 sourceRect.top = clamp(sourceRect.top, 0, source->getColorbuffer()->getHeight());
1720 sourceRect.right = clamp(sourceRect.right, 0, source->getColorbuffer()->getWidth());
1721 sourceRect.bottom = clamp(sourceRect.bottom, 0, source->getColorbuffer()->getHeight());
daniel@transgaming.comeef864a2011-04-22 11:33:27 +00001722
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001723 GLint destYOffset = transformPixelYOffset(0, height, mImageArray[level].getHeight());
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001724
1725 IDirect3DSurface9 *dest;
1726 HRESULT hr = mTexture->GetSurfaceLevel(level, &dest);
1727
daniel@transgaming.comeef864a2011-04-22 11:33:27 +00001728 getBlitter()->copy(source->getRenderTarget(), sourceRect, format, 0, destYOffset, dest);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001729 dest->Release();
1730 }
1731 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001732}
1733
1734void Texture2D::copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source)
1735{
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001736 if (xoffset + width > mImageArray[level].getWidth() || yoffset + height > mImageArray[level].getHeight())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001737 {
1738 return error(GL_INVALID_VALUE);
1739 }
1740
1741 IDirect3DSurface9 *renderTarget = source->getRenderTarget();
1742
1743 if (!renderTarget)
1744 {
1745 ERR("Failed to retrieve the render target.");
1746 return error(GL_OUT_OF_MEMORY);
1747 }
1748
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00001749 if (!mImageArray[level].isRenderable() || (!mTexture && !isComplete()))
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001750 {
daniel@transgaming.comb6276992011-03-29 00:58:18 +00001751 copyToImage(&mImageArray[level], xoffset, yoffset, x, y, width, height, renderTarget);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001752 }
1753 else
1754 {
daniel@transgaming.com61208202011-03-21 16:38:50 +00001755 if (!mTexture || !mIsRenderable)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001756 {
1757 convertToRenderTarget();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001758 }
daniel@transgaming.com61208202011-03-21 16:38:50 +00001759
1760 updateTexture();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001761
1762 if (level < levelCount())
1763 {
1764 RECT sourceRect = transformPixelRect(x, y, width, height, source->getColorbuffer()->getHeight());
1765 sourceRect.left = clamp(sourceRect.left, 0, source->getColorbuffer()->getWidth());
1766 sourceRect.top = clamp(sourceRect.top, 0, source->getColorbuffer()->getHeight());
1767 sourceRect.right = clamp(sourceRect.right, 0, source->getColorbuffer()->getWidth());
1768 sourceRect.bottom = clamp(sourceRect.bottom, 0, source->getColorbuffer()->getHeight());
1769
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001770 GLint destYOffset = transformPixelYOffset(yoffset, height, mImageArray[level].getHeight());
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001771
1772 IDirect3DSurface9 *dest;
1773 HRESULT hr = mTexture->GetSurfaceLevel(level, &dest);
1774
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001775 getBlitter()->copy(source->getRenderTarget(), sourceRect, mImageArray[0].getFormat(), xoffset, destYOffset, dest);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001776 dest->Release();
1777 }
1778 }
1779}
1780
1781// Tests for GL texture object completeness. [OpenGL ES 2.0.24] section 3.7.10 page 81.
1782bool Texture2D::isComplete() const
1783{
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001784 GLsizei width = mImageArray[0].getWidth();
1785 GLsizei height = mImageArray[0].getHeight();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001786
1787 if (width <= 0 || height <= 0)
1788 {
1789 return false;
1790 }
1791
1792 bool mipmapping = false;
1793
1794 switch (mMinFilter)
1795 {
1796 case GL_NEAREST:
1797 case GL_LINEAR:
1798 mipmapping = false;
1799 break;
1800 case GL_NEAREST_MIPMAP_NEAREST:
1801 case GL_LINEAR_MIPMAP_NEAREST:
1802 case GL_NEAREST_MIPMAP_LINEAR:
1803 case GL_LINEAR_MIPMAP_LINEAR:
1804 mipmapping = true;
1805 break;
1806 default: UNREACHABLE();
1807 }
1808
1809 if ((getInternalFormat() == GL_FLOAT && !getContext()->supportsFloatLinearFilter()) ||
1810 (getInternalFormat() == GL_HALF_FLOAT_OES && !getContext()->supportsHalfFloatLinearFilter()))
1811 {
1812 if (mMagFilter != GL_NEAREST || (mMinFilter != GL_NEAREST && mMinFilter != GL_NEAREST_MIPMAP_NEAREST))
1813 {
1814 return false;
1815 }
1816 }
1817
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00001818 bool npot = getContext()->supportsNonPower2Texture();
1819
1820 if (!npot)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001821 {
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00001822 if ((getWrapS() != GL_CLAMP_TO_EDGE && !isPow2(width)) ||
1823 (getWrapT() != GL_CLAMP_TO_EDGE && !isPow2(height)))
1824 {
1825 return false;
1826 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001827 }
1828
1829 if (mipmapping)
1830 {
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00001831 if (!npot)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001832 {
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00001833 if (!isPow2(width) || !isPow2(height))
1834 {
1835 return false;
1836 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001837 }
1838
1839 int q = log2(std::max(width, height));
1840
1841 for (int level = 1; level <= q; level++)
1842 {
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001843 if (mImageArray[level].getFormat() != mImageArray[0].getFormat())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001844 {
1845 return false;
1846 }
1847
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001848 if (mImageArray[level].getType() != mImageArray[0].getType())
daniel@transgaming.com0bd22bc2011-03-21 16:38:26 +00001849 {
1850 return false;
1851 }
1852
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001853 if (mImageArray[level].getWidth() != std::max(1, width >> level))
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001854 {
1855 return false;
1856 }
1857
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001858 if (mImageArray[level].getHeight() != std::max(1, height >> level))
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001859 {
1860 return false;
1861 }
1862 }
1863 }
1864
1865 return true;
1866}
1867
1868bool Texture2D::isCompressed() const
1869{
1870 return IsCompressed(getInternalFormat());
1871}
1872
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001873IDirect3DBaseTexture9 *Texture2D::getBaseTexture() const
1874{
1875 return mTexture;
1876}
1877
1878// Constructs a Direct3D 9 texture resource from the texture images
1879void Texture2D::createTexture()
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001880{
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001881 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00001882 D3DFORMAT format = mImageArray[0].getD3DFormat();
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001883 GLint levels = creationLevels(mImageArray[0].getWidth(), mImageArray[0].getHeight(), 0);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001884
daniel@transgaming.com61208202011-03-21 16:38:50 +00001885 IDirect3DTexture9 *texture = NULL;
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001886 HRESULT result = device->CreateTexture(mImageArray[0].getWidth(), mImageArray[0].getHeight(), levels, 0, format, D3DPOOL_DEFAULT, &texture, NULL);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001887
1888 if (FAILED(result))
1889 {
1890 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001891 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001892 }
1893
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001894 if (mTexture)
1895 {
1896 mTexture->Release();
1897 }
1898
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001899 mTexture = texture;
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00001900 mDirtyImages = true;
daniel@transgaming.comaed18322011-03-21 16:38:13 +00001901 mIsRenderable = false;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001902}
1903
1904void Texture2D::updateTexture()
1905{
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001906 int levels = levelCount();
1907
1908 for (int level = 0; level < levels; level++)
1909 {
daniel@transgaming.comb612f882011-11-09 17:44:31 +00001910 Image *image = &mImageArray[level];
1911
daniel@transgaming.com5cce3ff2011-11-09 17:45:14 +00001912 if (image->isDirty())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001913 {
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001914 commitRect(level, 0, 0, mImageArray[level].getWidth(), mImageArray[level].getHeight());
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001915 }
1916 }
1917}
1918
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001919void Texture2D::convertToRenderTarget()
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001920{
1921 IDirect3DTexture9 *texture = NULL;
1922
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001923 if (mImageArray[0].getWidth() != 0 && mImageArray[0].getHeight() != 0)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001924 {
1925 egl::Display *display = getDisplay();
1926 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00001927 D3DFORMAT format = mImageArray[0].getD3DFormat();
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001928 GLint levels = creationLevels(mImageArray[0].getWidth(), mImageArray[0].getHeight(), 0);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001929
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001930 HRESULT result = device->CreateTexture(mImageArray[0].getWidth(), mImageArray[0].getHeight(), levels, D3DUSAGE_RENDERTARGET, format, D3DPOOL_DEFAULT, &texture, NULL);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001931
1932 if (FAILED(result))
1933 {
1934 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001935 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001936 }
1937
1938 if (mTexture != NULL)
1939 {
1940 int levels = levelCount();
1941 for (int i = 0; i < levels; i++)
1942 {
1943 IDirect3DSurface9 *source;
1944 result = mTexture->GetSurfaceLevel(i, &source);
1945
1946 if (FAILED(result))
1947 {
1948 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
1949
1950 texture->Release();
1951
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001952 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001953 }
1954
1955 IDirect3DSurface9 *dest;
1956 result = texture->GetSurfaceLevel(i, &dest);
1957
1958 if (FAILED(result))
1959 {
1960 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
1961
1962 texture->Release();
1963 source->Release();
1964
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001965 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001966 }
1967
1968 display->endScene();
1969 result = device->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
1970
1971 if (FAILED(result))
1972 {
1973 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
1974
1975 texture->Release();
1976 source->Release();
1977 dest->Release();
1978
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001979 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001980 }
1981
1982 source->Release();
1983 dest->Release();
1984 }
1985 }
1986 }
1987
1988 if (mTexture != NULL)
1989 {
1990 mTexture->Release();
1991 }
1992
1993 mTexture = texture;
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00001994 mDirtyImages = true;
daniel@transgaming.comaed18322011-03-21 16:38:13 +00001995 mIsRenderable = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001996}
1997
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001998void Texture2D::generateMipmaps()
1999{
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00002000 if (!getContext()->supportsNonPower2Texture())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002001 {
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002002 if (!isPow2(mImageArray[0].getWidth()) || !isPow2(mImageArray[0].getHeight()))
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00002003 {
2004 return error(GL_INVALID_OPERATION);
2005 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002006 }
2007
2008 // Purge array levels 1 through q and reset them to represent the generated mipmap levels.
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002009 unsigned int q = log2(std::max(mImageArray[0].getWidth(), mImageArray[0].getHeight()));
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002010 for (unsigned int i = 1; i <= q; i++)
2011 {
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002012 mImageArray[i].redefine(mImageArray[0].getFormat(),
2013 std::max(mImageArray[0].getWidth() >> i, 1),
2014 std::max(mImageArray[0].getHeight() >> i, 1),
2015 mImageArray[0].getType());
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002016 }
2017
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002018 if (mIsRenderable)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002019 {
2020 if (mTexture == NULL)
2021 {
2022 ERR(" failed because mTexture was null.");
2023 return;
2024 }
2025
2026 for (unsigned int i = 1; i <= q; i++)
2027 {
2028 IDirect3DSurface9 *upper = NULL;
2029 IDirect3DSurface9 *lower = NULL;
2030
2031 mTexture->GetSurfaceLevel(i-1, &upper);
2032 mTexture->GetSurfaceLevel(i, &lower);
2033
2034 if (upper != NULL && lower != NULL)
2035 {
2036 getBlitter()->boxFilter(upper, lower);
2037 }
2038
2039 if (upper != NULL) upper->Release();
2040 if (lower != NULL) lower->Release();
daniel@transgaming.com61208202011-03-21 16:38:50 +00002041
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002042 mImageArray[i].markClean();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002043 }
2044 }
2045 else
2046 {
2047 for (unsigned int i = 1; i <= q; i++)
2048 {
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002049 if (mImageArray[i].getSurface() == NULL)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002050 {
2051 return error(GL_OUT_OF_MEMORY);
2052 }
2053
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002054 if (FAILED(D3DXLoadSurfaceFromSurface(mImageArray[i].getSurface(), NULL, NULL, mImageArray[i - 1].getSurface(), NULL, NULL, D3DX_FILTER_BOX, 0)))
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002055 {
2056 ERR(" failed to load filter %d to %d.", i - 1, i);
2057 }
2058
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002059 mImageArray[i].markDirty();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002060 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002061 }
2062}
2063
2064Renderbuffer *Texture2D::getRenderbuffer(GLenum target)
2065{
2066 if (target != GL_TEXTURE_2D)
2067 {
2068 return error(GL_INVALID_OPERATION, (Renderbuffer *)NULL);
2069 }
2070
2071 if (mColorbufferProxy.get() == NULL)
2072 {
2073 mColorbufferProxy.set(new Renderbuffer(id(), new Colorbuffer(this, target)));
2074 }
2075
2076 return mColorbufferProxy.get();
2077}
2078
2079IDirect3DSurface9 *Texture2D::getRenderTarget(GLenum target)
2080{
2081 ASSERT(target == GL_TEXTURE_2D);
2082
daniel@transgaming.com61208202011-03-21 16:38:50 +00002083 if (!mIsRenderable)
2084 {
2085 convertToRenderTarget();
2086 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002087
2088 if (mTexture == NULL)
2089 {
2090 return NULL;
2091 }
daniel@transgaming.com61208202011-03-21 16:38:50 +00002092
2093 updateTexture();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002094
2095 IDirect3DSurface9 *renderTarget = NULL;
2096 mTexture->GetSurfaceLevel(0, &renderTarget);
2097
2098 return renderTarget;
2099}
2100
2101TextureCubeMap::TextureCubeMap(GLuint id) : Texture(id)
2102{
2103 mTexture = NULL;
2104}
2105
2106TextureCubeMap::~TextureCubeMap()
2107{
2108 for (int i = 0; i < 6; i++)
2109 {
2110 mFaceProxies[i].set(NULL);
2111 }
2112
2113 if (mTexture)
2114 {
2115 mTexture->Release();
2116 mTexture = NULL;
2117 }
2118}
2119
2120GLenum TextureCubeMap::getTarget() const
2121{
2122 return GL_TEXTURE_CUBE_MAP;
2123}
2124
daniel@transgaming.com61208202011-03-21 16:38:50 +00002125GLsizei TextureCubeMap::getWidth() const
2126{
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002127 return mImageArray[0][0].getWidth();
daniel@transgaming.com61208202011-03-21 16:38:50 +00002128}
2129
2130GLsizei TextureCubeMap::getHeight() const
2131{
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002132 return mImageArray[0][0].getHeight();
daniel@transgaming.com61208202011-03-21 16:38:50 +00002133}
2134
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002135GLenum TextureCubeMap::getInternalFormat() const
2136{
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002137 return mImageArray[0][0].getFormat();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002138}
2139
daniel@transgaming.com61208202011-03-21 16:38:50 +00002140GLenum TextureCubeMap::getType() const
2141{
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002142 return mImageArray[0][0].getType();
daniel@transgaming.com61208202011-03-21 16:38:50 +00002143}
2144
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00002145D3DFORMAT TextureCubeMap::getD3DFormat() const
2146{
2147 return mImageArray[0][0].getD3DFormat();
2148}
2149
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002150void 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 +00002151{
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002152 setImage(0, level, width, height, format, type, unpackAlignment, pixels);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002153}
2154
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002155void 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 +00002156{
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002157 setImage(1, level, width, height, format, type, unpackAlignment, pixels);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002158}
2159
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002160void 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 +00002161{
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002162 setImage(2, level, width, height, format, type, unpackAlignment, pixels);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002163}
2164
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002165void 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 +00002166{
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002167 setImage(3, level, width, height, format, type, unpackAlignment, pixels);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002168}
2169
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002170void 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 +00002171{
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002172 setImage(4, level, width, height, format, type, unpackAlignment, pixels);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002173}
2174
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002175void 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 +00002176{
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002177 setImage(5, level, width, height, format, type, unpackAlignment, pixels);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002178}
2179
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002180void TextureCubeMap::setCompressedImage(GLenum face, GLint level, GLenum format, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002181{
daniel@transgaming.com68ae2992011-11-09 17:44:49 +00002182 redefineImage(faceIndex(face), level, format, width, height, GL_UNSIGNED_BYTE);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002183
daniel@transgaming.com61208202011-03-21 16:38:50 +00002184 Texture::setCompressedImage(imageSize, pixels, &mImageArray[faceIndex(face)][level]);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002185}
2186
daniel@transgaming.comb612f882011-11-09 17:44:31 +00002187void TextureCubeMap::commitRect(int face, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002188{
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002189 ASSERT(mImageArray[face][level].getSurface() != NULL);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002190
2191 if (level < levelCount())
2192 {
daniel@transgaming.comb612f882011-11-09 17:44:31 +00002193 IDirect3DSurface9 *destLevel = getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002194 ASSERT(destLevel != NULL);
2195
2196 if (destLevel != NULL)
2197 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002198 Image *image = &mImageArray[face][level];
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002199
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002200 RECT sourceRect = transformPixelRect(xoffset, yoffset, width, height, image->getHeight());;
daniel@transgaming.comb612f882011-11-09 17:44:31 +00002201 POINT destPoint = {sourceRect.left, sourceRect.top};
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002202
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002203 HRESULT result = getDevice()->UpdateSurface(image->getSurface(), &sourceRect, destLevel, &destPoint);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002204 ASSERT(SUCCEEDED(result));
2205
2206 destLevel->Release();
2207
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002208 image->markClean();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002209 }
2210 }
2211}
2212
2213void TextureCubeMap::subImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels)
2214{
2215 if (Texture::subImage(xoffset, yoffset, width, height, format, type, unpackAlignment, pixels, &mImageArray[faceIndex(target)][level]))
2216 {
daniel@transgaming.comb612f882011-11-09 17:44:31 +00002217 commitRect(faceIndex(target), level, xoffset, yoffset, width, height);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002218 }
2219}
2220
2221void TextureCubeMap::subImageCompressed(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels)
2222{
2223 if (Texture::subImageCompressed(xoffset, yoffset, width, height, format, imageSize, pixels, &mImageArray[faceIndex(target)][level]))
2224 {
daniel@transgaming.comb612f882011-11-09 17:44:31 +00002225 commitRect(faceIndex(target), level, xoffset, yoffset, width, height);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002226 }
2227}
2228
2229// Tests for GL texture object completeness. [OpenGL ES 2.0.24] section 3.7.10 page 81.
2230bool TextureCubeMap::isComplete() const
2231{
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002232 int size = mImageArray[0][0].getWidth();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002233
2234 if (size <= 0)
2235 {
2236 return false;
2237 }
2238
2239 bool mipmapping;
2240
2241 switch (mMinFilter)
2242 {
2243 case GL_NEAREST:
2244 case GL_LINEAR:
2245 mipmapping = false;
2246 break;
2247 case GL_NEAREST_MIPMAP_NEAREST:
2248 case GL_LINEAR_MIPMAP_NEAREST:
2249 case GL_NEAREST_MIPMAP_LINEAR:
2250 case GL_LINEAR_MIPMAP_LINEAR:
2251 mipmapping = true;
2252 break;
2253 default: UNREACHABLE();
2254 }
2255
2256 for (int face = 0; face < 6; face++)
2257 {
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002258 if (mImageArray[face][0].getWidth() != size || mImageArray[face][0].getHeight() != size)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002259 {
2260 return false;
2261 }
2262 }
2263
2264 if ((getInternalFormat() == GL_FLOAT && !getContext()->supportsFloatLinearFilter()) ||
2265 (getInternalFormat() == GL_HALF_FLOAT_OES && !getContext()->supportsHalfFloatLinearFilter()))
2266 {
2267 if (mMagFilter != GL_NEAREST || (mMinFilter != GL_NEAREST && mMinFilter != GL_NEAREST_MIPMAP_NEAREST))
2268 {
2269 return false;
2270 }
2271 }
2272
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00002273 bool npot = getContext()->supportsNonPower2Texture();
2274
2275 if (!npot)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002276 {
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00002277 if ((getWrapS() != GL_CLAMP_TO_EDGE || getWrapT() != GL_CLAMP_TO_EDGE) && !isPow2(size))
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002278 {
2279 return false;
2280 }
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00002281 }
2282
2283 if (mipmapping)
2284 {
2285 if (!npot)
2286 {
2287 if (!isPow2(size))
2288 {
2289 return false;
2290 }
2291 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002292
2293 int q = log2(size);
2294
2295 for (int face = 0; face < 6; face++)
2296 {
2297 for (int level = 1; level <= q; level++)
2298 {
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002299 if (mImageArray[face][level].getFormat() != mImageArray[0][0].getFormat())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002300 {
2301 return false;
2302 }
2303
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002304 if (mImageArray[face][level].getType() != mImageArray[0][0].getType())
daniel@transgaming.com0bd22bc2011-03-21 16:38:26 +00002305 {
2306 return false;
2307 }
2308
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002309 if (mImageArray[face][level].getWidth() != std::max(1, size >> level))
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002310 {
2311 return false;
2312 }
2313
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002314 ASSERT(mImageArray[face][level].getHeight() == mImageArray[face][level].getWidth());
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002315 }
2316 }
2317 }
2318
2319 return true;
2320}
2321
2322bool TextureCubeMap::isCompressed() const
2323{
2324 return IsCompressed(getInternalFormat());
2325}
2326
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002327IDirect3DBaseTexture9 *TextureCubeMap::getBaseTexture() const
2328{
2329 return mTexture;
2330}
2331
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002332// Constructs a Direct3D 9 texture resource from the texture images, or returns an existing one
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002333void TextureCubeMap::createTexture()
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002334{
2335 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00002336 D3DFORMAT format = mImageArray[0][0].getD3DFormat();
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002337 GLint levels = creationLevels(mImageArray[0][0].getWidth(), 0);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002338
daniel@transgaming.com0bd22bc2011-03-21 16:38:26 +00002339 IDirect3DCubeTexture9 *texture = NULL;
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002340 HRESULT result = device->CreateCubeTexture(mImageArray[0][0].getWidth(), levels, 0, format, D3DPOOL_DEFAULT, &texture, NULL);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002341
2342 if (FAILED(result))
2343 {
2344 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002345 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002346 }
2347
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002348 if (mTexture)
2349 {
2350 mTexture->Release();
2351 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002352
2353 mTexture = texture;
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00002354 mDirtyImages = true;
daniel@transgaming.comaed18322011-03-21 16:38:13 +00002355 mIsRenderable = false;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002356}
2357
2358void TextureCubeMap::updateTexture()
2359{
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002360 for (int face = 0; face < 6; face++)
2361 {
2362 int levels = levelCount();
2363 for (int level = 0; level < levels; level++)
2364 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002365 Image *image = &mImageArray[face][level];
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002366
daniel@transgaming.com5cce3ff2011-11-09 17:45:14 +00002367 if (image->isDirty())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002368 {
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002369 commitRect(face, level, 0, 0, image->getWidth(), image->getHeight());
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002370 }
2371 }
2372 }
2373}
2374
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002375void TextureCubeMap::convertToRenderTarget()
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002376{
2377 IDirect3DCubeTexture9 *texture = NULL;
2378
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002379 if (mImageArray[0][0].getWidth() != 0)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002380 {
2381 egl::Display *display = getDisplay();
2382 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00002383 D3DFORMAT format = mImageArray[0][0].getD3DFormat();
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002384 GLint levels = creationLevels(mImageArray[0][0].getWidth(), 0);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002385
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002386 HRESULT result = device->CreateCubeTexture(mImageArray[0][0].getWidth(), levels, D3DUSAGE_RENDERTARGET, format, D3DPOOL_DEFAULT, &texture, NULL);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002387
2388 if (FAILED(result))
2389 {
2390 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002391 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002392 }
2393
2394 if (mTexture != NULL)
2395 {
2396 int levels = levelCount();
2397 for (int f = 0; f < 6; f++)
2398 {
2399 for (int i = 0; i < levels; i++)
2400 {
2401 IDirect3DSurface9 *source;
2402 result = mTexture->GetCubeMapSurface(static_cast<D3DCUBEMAP_FACES>(f), i, &source);
2403
2404 if (FAILED(result))
2405 {
2406 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
2407
2408 texture->Release();
2409
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002410 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002411 }
2412
2413 IDirect3DSurface9 *dest;
2414 result = texture->GetCubeMapSurface(static_cast<D3DCUBEMAP_FACES>(f), i, &dest);
2415
2416 if (FAILED(result))
2417 {
2418 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
2419
2420 texture->Release();
2421 source->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 display->endScene();
2427 result = device->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
2428
2429 if (FAILED(result))
2430 {
2431 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
2432
2433 texture->Release();
2434 source->Release();
2435 dest->Release();
2436
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002437 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002438 }
daniel@transgaming.coma1a86202011-08-09 13:41:08 +00002439
2440 source->Release();
2441 dest->Release();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002442 }
2443 }
2444 }
2445 }
2446
2447 if (mTexture != NULL)
2448 {
2449 mTexture->Release();
2450 }
2451
2452 mTexture = texture;
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00002453 mDirtyImages = true;
daniel@transgaming.comaed18322011-03-21 16:38:13 +00002454 mIsRenderable = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002455}
2456
daniel@transgaming.com61208202011-03-21 16:38:50 +00002457void 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 +00002458{
daniel@transgaming.com68ae2992011-11-09 17:44:49 +00002459 redefineImage(faceIndex, level, format, width, height, type);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002460
daniel@transgaming.com61208202011-03-21 16:38:50 +00002461 Texture::setImage(unpackAlignment, pixels, &mImageArray[faceIndex][level]);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002462}
2463
2464unsigned int TextureCubeMap::faceIndex(GLenum face)
2465{
2466 META_ASSERT(GL_TEXTURE_CUBE_MAP_NEGATIVE_X - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 1);
2467 META_ASSERT(GL_TEXTURE_CUBE_MAP_POSITIVE_Y - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 2);
2468 META_ASSERT(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 3);
2469 META_ASSERT(GL_TEXTURE_CUBE_MAP_POSITIVE_Z - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 4);
2470 META_ASSERT(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 5);
2471
2472 return face - GL_TEXTURE_CUBE_MAP_POSITIVE_X;
2473}
2474
daniel@transgaming.com68ae2992011-11-09 17:44:49 +00002475void TextureCubeMap::redefineImage(int face, GLint level, GLenum format, GLsizei width, GLsizei height, GLenum type)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002476{
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002477 GLsizei textureWidth = mImageArray[0][0].getWidth();
2478 GLsizei textureHeight = mImageArray[0][0].getHeight();
2479 GLenum textureFormat = mImageArray[0][0].getFormat();
2480 GLenum textureType = mImageArray[0][0].getType();
daniel@transgaming.com94a4f032011-03-21 16:38:55 +00002481
daniel@transgaming.com68ae2992011-11-09 17:44:49 +00002482 mImageArray[face][level].redefine(format, width, height, type);
daniel@transgaming.comc9ba4ad2011-11-09 17:44:35 +00002483
daniel@transgaming.com61208202011-03-21 16:38:50 +00002484 if (!mTexture)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002485 {
daniel@transgaming.com61208202011-03-21 16:38:50 +00002486 return;
2487 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002488
daniel@transgaming.com94a4f032011-03-21 16:38:55 +00002489 bool sizeOkay = (textureWidth >> level == width);
2490 bool textureOkay = (sizeOkay && textureFormat == format && textureType == type);
daniel@transgaming.com61208202011-03-21 16:38:50 +00002491
daniel@transgaming.comc9ba4ad2011-11-09 17:44:35 +00002492 if (!textureOkay)
daniel@transgaming.com61208202011-03-21 16:38:50 +00002493 {
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002494 for (int i = 0; i < IMPLEMENTATION_MAX_TEXTURE_LEVELS; i++)
2495 {
2496 for (int f = 0; f < 6; f++)
2497 {
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002498 mImageArray[f][i].markDirty();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002499 }
2500 }
2501
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00002502 mTexture->Release();
2503 mTexture = NULL;
2504 mDirtyImages = true;
2505 mIsRenderable = false;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002506 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002507}
2508
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002509void 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 +00002510{
2511 IDirect3DSurface9 *renderTarget = source->getRenderTarget();
2512
2513 if (!renderTarget)
2514 {
2515 ERR("Failed to retrieve the render target.");
2516 return error(GL_OUT_OF_MEMORY);
2517 }
2518
2519 unsigned int faceindex = faceIndex(target);
daniel@transgaming.com68ae2992011-11-09 17:44:49 +00002520 redefineImage(faceindex, level, format, width, height, GL_UNSIGNED_BYTE);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002521
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00002522 if (!mImageArray[faceindex][level].isRenderable())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002523 {
daniel@transgaming.comb6276992011-03-29 00:58:18 +00002524 copyToImage(&mImageArray[faceindex][level], 0, 0, x, y, width, height, renderTarget);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002525 }
2526 else
2527 {
daniel@transgaming.com61208202011-03-21 16:38:50 +00002528 if (!mTexture || !mIsRenderable)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002529 {
2530 convertToRenderTarget();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002531 }
daniel@transgaming.com3b3c1d42011-06-08 20:38:09 +00002532
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002533 mImageArray[faceindex][level].markClean();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002534
2535 ASSERT(width == height);
2536
2537 if (width > 0 && level < levelCount())
2538 {
2539 RECT sourceRect = transformPixelRect(x, y, width, height, source->getColorbuffer()->getHeight());
2540 sourceRect.left = clamp(sourceRect.left, 0, source->getColorbuffer()->getWidth());
2541 sourceRect.top = clamp(sourceRect.top, 0, source->getColorbuffer()->getHeight());
2542 sourceRect.right = clamp(sourceRect.right, 0, source->getColorbuffer()->getWidth());
2543 sourceRect.bottom = clamp(sourceRect.bottom, 0, source->getColorbuffer()->getHeight());
2544
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002545 GLint destYOffset = transformPixelYOffset(0, height, mImageArray[faceindex][level].getWidth());
daniel@transgaming.comeef864a2011-04-22 11:33:27 +00002546
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002547 IDirect3DSurface9 *dest = getCubeMapSurface(target, level);
2548
daniel@transgaming.comeef864a2011-04-22 11:33:27 +00002549 getBlitter()->copy(source->getRenderTarget(), sourceRect, format, 0, destYOffset, dest);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002550 dest->Release();
2551 }
2552 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002553}
2554
daniel@transgaming.comb612f882011-11-09 17:44:31 +00002555IDirect3DSurface9 *TextureCubeMap::getCubeMapSurface(GLenum target, unsigned int level)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002556{
2557 if (mTexture == NULL)
2558 {
2559 UNREACHABLE();
2560 return NULL;
2561 }
2562
2563 IDirect3DSurface9 *surface = NULL;
2564
daniel@transgaming.comb612f882011-11-09 17:44:31 +00002565 HRESULT hr = mTexture->GetCubeMapSurface(es2dx::ConvertCubeFace(target), level, &surface);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002566
2567 return (SUCCEEDED(hr)) ? surface : NULL;
2568}
2569
2570void TextureCubeMap::copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source)
2571{
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002572 GLsizei size = mImageArray[faceIndex(target)][level].getWidth();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002573
2574 if (xoffset + width > size || yoffset + height > size)
2575 {
2576 return error(GL_INVALID_VALUE);
2577 }
2578
2579 IDirect3DSurface9 *renderTarget = source->getRenderTarget();
2580
2581 if (!renderTarget)
2582 {
2583 ERR("Failed to retrieve the render target.");
2584 return error(GL_OUT_OF_MEMORY);
2585 }
2586
2587 unsigned int faceindex = faceIndex(target);
daniel@transgaming.com01dae852011-11-09 17:44:53 +00002588
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00002589 if (!mImageArray[faceindex][level].isRenderable() || (!mTexture && !isComplete()))
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002590 {
daniel@transgaming.comb6276992011-03-29 00:58:18 +00002591 copyToImage(&mImageArray[faceindex][level], 0, 0, x, y, width, height, renderTarget);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002592 }
2593 else
2594 {
daniel@transgaming.com61208202011-03-21 16:38:50 +00002595 if (!mTexture || !mIsRenderable)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002596 {
2597 convertToRenderTarget();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002598 }
daniel@transgaming.com61208202011-03-21 16:38:50 +00002599
2600 updateTexture();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002601
2602 if (level < levelCount())
2603 {
2604 RECT sourceRect = transformPixelRect(x, y, width, height, source->getColorbuffer()->getHeight());
2605 sourceRect.left = clamp(sourceRect.left, 0, source->getColorbuffer()->getWidth());
2606 sourceRect.top = clamp(sourceRect.top, 0, source->getColorbuffer()->getHeight());
2607 sourceRect.right = clamp(sourceRect.right, 0, source->getColorbuffer()->getWidth());
2608 sourceRect.bottom = clamp(sourceRect.bottom, 0, source->getColorbuffer()->getHeight());
2609
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002610 GLint destYOffset = transformPixelYOffset(yoffset, height, mImageArray[faceindex][level].getWidth());
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002611
2612 IDirect3DSurface9 *dest = getCubeMapSurface(target, level);
2613
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002614 getBlitter()->copy(source->getRenderTarget(), sourceRect, mImageArray[0][0].getFormat(), xoffset, destYOffset, dest);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002615 dest->Release();
2616 }
2617 }
2618}
2619
2620bool TextureCubeMap::isCubeComplete() const
2621{
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002622 if (mImageArray[0][0].getWidth() == 0)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002623 {
2624 return false;
2625 }
2626
2627 for (unsigned int f = 1; f < 6; f++)
2628 {
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002629 if (mImageArray[f][0].getWidth() != mImageArray[0][0].getWidth()
2630 || mImageArray[f][0].getFormat() != mImageArray[0][0].getFormat())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002631 {
2632 return false;
2633 }
2634 }
2635
2636 return true;
2637}
2638
2639void TextureCubeMap::generateMipmaps()
2640{
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00002641 if (!isCubeComplete())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002642 {
2643 return error(GL_INVALID_OPERATION);
2644 }
2645
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00002646 if (!getContext()->supportsNonPower2Texture())
2647 {
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002648 if (!isPow2(mImageArray[0][0].getWidth()))
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00002649 {
2650 return error(GL_INVALID_OPERATION);
2651 }
2652 }
2653
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002654 // Purge array levels 1 through q and reset them to represent the generated mipmap levels.
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002655 unsigned int q = log2(mImageArray[0][0].getWidth());
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002656 for (unsigned int f = 0; f < 6; f++)
2657 {
2658 for (unsigned int i = 1; i <= q; i++)
2659 {
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002660 mImageArray[f][i].redefine(mImageArray[f][0].getFormat(),
2661 std::max(mImageArray[f][0].getWidth() >> i, 1),
2662 std::max(mImageArray[f][0].getWidth() >> i, 1),
2663 mImageArray[f][0].getType());
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002664 }
2665 }
2666
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002667 if (mIsRenderable)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002668 {
2669 if (mTexture == NULL)
2670 {
2671 return;
2672 }
2673
2674 for (unsigned int f = 0; f < 6; f++)
2675 {
2676 for (unsigned int i = 1; i <= q; i++)
2677 {
2678 IDirect3DSurface9 *upper = getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i-1);
2679 IDirect3DSurface9 *lower = getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i);
2680
2681 if (upper != NULL && lower != NULL)
2682 {
2683 getBlitter()->boxFilter(upper, lower);
2684 }
2685
2686 if (upper != NULL) upper->Release();
2687 if (lower != NULL) lower->Release();
daniel@transgaming.com61208202011-03-21 16:38:50 +00002688
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002689 mImageArray[f][i].markClean();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002690 }
2691 }
2692 }
2693 else
2694 {
2695 for (unsigned int f = 0; f < 6; f++)
2696 {
2697 for (unsigned int i = 1; i <= q; i++)
2698 {
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002699 if (mImageArray[f][i].getSurface() == NULL)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002700 {
2701 return error(GL_OUT_OF_MEMORY);
2702 }
2703
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002704 if (FAILED(D3DXLoadSurfaceFromSurface(mImageArray[f][i].getSurface(), NULL, NULL, mImageArray[f][i - 1].getSurface(), NULL, NULL, D3DX_FILTER_BOX, 0)))
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002705 {
2706 ERR(" failed to load filter %d to %d.", i - 1, i);
2707 }
2708
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002709 mImageArray[f][i].markDirty();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002710 }
2711 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002712 }
2713}
2714
2715Renderbuffer *TextureCubeMap::getRenderbuffer(GLenum target)
2716{
2717 if (!IsCubemapTextureTarget(target))
2718 {
2719 return error(GL_INVALID_OPERATION, (Renderbuffer *)NULL);
2720 }
2721
2722 unsigned int face = faceIndex(target);
2723
2724 if (mFaceProxies[face].get() == NULL)
2725 {
2726 mFaceProxies[face].set(new Renderbuffer(id(), new Colorbuffer(this, target)));
2727 }
2728
2729 return mFaceProxies[face].get();
2730}
2731
2732IDirect3DSurface9 *TextureCubeMap::getRenderTarget(GLenum target)
2733{
2734 ASSERT(IsCubemapTextureTarget(target));
2735
daniel@transgaming.com61208202011-03-21 16:38:50 +00002736 if (!mIsRenderable)
2737 {
2738 convertToRenderTarget();
2739 }
2740
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002741 if (mTexture == NULL)
2742 {
2743 return NULL;
2744 }
daniel@transgaming.com61208202011-03-21 16:38:50 +00002745
2746 updateTexture();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002747
2748 IDirect3DSurface9 *renderTarget = NULL;
2749 mTexture->GetCubeMapSurface(es2dx::ConvertCubeFace(target), 0, &renderTarget);
2750
2751 return renderTarget;
2752}
2753
daniel@transgaming.comb612f882011-11-09 17:44:31 +00002754}