blob: 2cae9f24caef67efba6a897d4b69fba80e235a19 [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.com90cfcc92011-11-09 17:45:48 +00001027// This implements glCopyTex[Sub]Image2D for non-renderable internal texture formats and incomplete textures
1028void Image::copy(GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, IDirect3DSurface9 *renderTarget)
1029{
1030 IDirect3DDevice9 *device = getDevice();
1031 IDirect3DSurface9 *renderTargetData = NULL;
1032 D3DSURFACE_DESC description;
1033 renderTarget->GetDesc(&description);
1034
1035 HRESULT result = device->CreateOffscreenPlainSurface(description.Width, description.Height, description.Format, D3DPOOL_SYSTEMMEM, &renderTargetData, NULL);
1036
1037 if (FAILED(result))
1038 {
1039 ERR("Could not create matching destination surface.");
1040 return error(GL_OUT_OF_MEMORY);
1041 }
1042
1043 result = device->GetRenderTargetData(renderTarget, renderTargetData);
1044
1045 if (FAILED(result))
1046 {
1047 ERR("GetRenderTargetData unexpectedly failed.");
1048 renderTargetData->Release();
1049 return error(GL_OUT_OF_MEMORY);
1050 }
1051
1052 RECT sourceRect = transformPixelRect(x, y, width, height, description.Height);
1053 int destYOffset = transformPixelYOffset(yoffset, height, mHeight);
1054 RECT destRect = {xoffset, destYOffset, xoffset + width, destYOffset + height};
1055
1056 if (isRenderable())
1057 {
1058 result = D3DXLoadSurfaceFromSurface(getSurface(), NULL, &destRect, renderTargetData, NULL, &sourceRect, D3DX_FILTER_BOX, 0);
1059
1060 if (FAILED(result))
1061 {
1062 ERR("Copying surfaces unexpectedly failed.");
1063 renderTargetData->Release();
1064 return error(GL_OUT_OF_MEMORY);
1065 }
1066 }
1067 else
1068 {
1069 D3DLOCKED_RECT sourceLock = {0};
1070 result = renderTargetData->LockRect(&sourceLock, &sourceRect, 0);
1071
1072 if (FAILED(result))
1073 {
1074 ERR("Failed to lock the source surface (rectangle might be invalid).");
1075 renderTargetData->Release();
1076 return error(GL_OUT_OF_MEMORY);
1077 }
1078
1079 D3DLOCKED_RECT destLock = {0};
1080 result = lock(&destLock, &destRect);
1081
1082 if (FAILED(result))
1083 {
1084 ERR("Failed to lock the destination surface (rectangle might be invalid).");
1085 renderTargetData->UnlockRect();
1086 renderTargetData->Release();
1087 return error(GL_OUT_OF_MEMORY);
1088 }
1089
1090 if (destLock.pBits && sourceLock.pBits)
1091 {
1092 unsigned char *source = (unsigned char*)sourceLock.pBits;
1093 unsigned char *dest = (unsigned char*)destLock.pBits;
1094
1095 switch (description.Format)
1096 {
1097 case D3DFMT_X8R8G8B8:
1098 case D3DFMT_A8R8G8B8:
1099 switch(getD3DFormat())
1100 {
1101 case D3DFMT_L8:
1102 for(int y = 0; y < height; y++)
1103 {
1104 for(int x = 0; x < width; x++)
1105 {
1106 dest[x] = source[x * 4 + 2];
1107 }
1108
1109 source += sourceLock.Pitch;
1110 dest += destLock.Pitch;
1111 }
1112 break;
1113 case D3DFMT_A8L8:
1114 for(int y = 0; y < height; y++)
1115 {
1116 for(int x = 0; x < width; x++)
1117 {
1118 dest[x * 2 + 0] = source[x * 4 + 2];
1119 dest[x * 2 + 1] = source[x * 4 + 3];
1120 }
1121
1122 source += sourceLock.Pitch;
1123 dest += destLock.Pitch;
1124 }
1125 break;
1126 default:
1127 UNREACHABLE();
1128 }
1129 break;
1130 case D3DFMT_R5G6B5:
1131 switch(getD3DFormat())
1132 {
1133 case D3DFMT_L8:
1134 for(int y = 0; y < height; y++)
1135 {
1136 for(int x = 0; x < width; x++)
1137 {
1138 unsigned char red = source[x * 2 + 1] & 0xF8;
1139 dest[x] = red | (red >> 5);
1140 }
1141
1142 source += sourceLock.Pitch;
1143 dest += destLock.Pitch;
1144 }
1145 break;
1146 default:
1147 UNREACHABLE();
1148 }
1149 break;
1150 case D3DFMT_A1R5G5B5:
1151 switch(getD3DFormat())
1152 {
1153 case D3DFMT_L8:
1154 for(int y = 0; y < height; y++)
1155 {
1156 for(int x = 0; x < width; x++)
1157 {
1158 unsigned char red = source[x * 2 + 1] & 0x7C;
1159 dest[x] = (red << 1) | (red >> 4);
1160 }
1161
1162 source += sourceLock.Pitch;
1163 dest += destLock.Pitch;
1164 }
1165 break;
1166 case D3DFMT_A8L8:
1167 for(int y = 0; y < height; y++)
1168 {
1169 for(int x = 0; x < width; x++)
1170 {
1171 unsigned char red = source[x * 2 + 1] & 0x7C;
1172 dest[x * 2 + 0] = (red << 1) | (red >> 4);
1173 dest[x * 2 + 1] = (signed char)source[x * 2 + 1] >> 7;
1174 }
1175
1176 source += sourceLock.Pitch;
1177 dest += destLock.Pitch;
1178 }
1179 break;
1180 default:
1181 UNREACHABLE();
1182 }
1183 break;
1184 default:
1185 UNREACHABLE();
1186 }
1187 }
1188
1189 unlock();
1190 renderTargetData->UnlockRect();
1191 }
1192
1193 renderTargetData->Release();
1194
1195 mDirty = true;
1196}
1197
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00001198Texture::Texture(GLuint id) : RefCountObject(id), mSerial(issueSerial())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001199{
1200 mMinFilter = GL_NEAREST_MIPMAP_LINEAR;
1201 mMagFilter = GL_LINEAR;
1202 mWrapS = GL_REPEAT;
1203 mWrapT = GL_REPEAT;
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00001204 mDirtyParameters = true;
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00001205
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00001206 mDirtyImages = true;
daniel@transgaming.comaed18322011-03-21 16:38:13 +00001207
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001208 mIsRenderable = false;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001209}
1210
1211Texture::~Texture()
1212{
1213}
1214
1215Blit *Texture::getBlitter()
1216{
1217 Context *context = getContext();
1218 return context->getBlitter();
1219}
1220
1221// Returns true on successful filter state update (valid enum parameter)
1222bool Texture::setMinFilter(GLenum filter)
1223{
1224 switch (filter)
1225 {
1226 case GL_NEAREST:
1227 case GL_LINEAR:
1228 case GL_NEAREST_MIPMAP_NEAREST:
1229 case GL_LINEAR_MIPMAP_NEAREST:
1230 case GL_NEAREST_MIPMAP_LINEAR:
1231 case GL_LINEAR_MIPMAP_LINEAR:
1232 {
1233 if (mMinFilter != filter)
1234 {
1235 mMinFilter = filter;
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00001236 mDirtyParameters = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001237 }
1238 return true;
1239 }
1240 default:
1241 return false;
1242 }
1243}
1244
1245// Returns true on successful filter state update (valid enum parameter)
1246bool Texture::setMagFilter(GLenum filter)
1247{
1248 switch (filter)
1249 {
1250 case GL_NEAREST:
1251 case GL_LINEAR:
1252 {
1253 if (mMagFilter != filter)
1254 {
1255 mMagFilter = filter;
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00001256 mDirtyParameters = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001257 }
1258 return true;
1259 }
1260 default:
1261 return false;
1262 }
1263}
1264
1265// Returns true on successful wrap state update (valid enum parameter)
1266bool Texture::setWrapS(GLenum wrap)
1267{
1268 switch (wrap)
1269 {
1270 case GL_REPEAT:
1271 case GL_CLAMP_TO_EDGE:
1272 case GL_MIRRORED_REPEAT:
1273 {
1274 if (mWrapS != wrap)
1275 {
1276 mWrapS = wrap;
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00001277 mDirtyParameters = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001278 }
1279 return true;
1280 }
1281 default:
1282 return false;
1283 }
1284}
1285
1286// Returns true on successful wrap state update (valid enum parameter)
1287bool Texture::setWrapT(GLenum wrap)
1288{
1289 switch (wrap)
1290 {
1291 case GL_REPEAT:
1292 case GL_CLAMP_TO_EDGE:
1293 case GL_MIRRORED_REPEAT:
1294 {
1295 if (mWrapT != wrap)
1296 {
1297 mWrapT = wrap;
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00001298 mDirtyParameters = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001299 }
1300 return true;
1301 }
1302 default:
1303 return false;
1304 }
1305}
1306
1307GLenum Texture::getMinFilter() const
1308{
1309 return mMinFilter;
1310}
1311
1312GLenum Texture::getMagFilter() const
1313{
1314 return mMagFilter;
1315}
1316
1317GLenum Texture::getWrapS() const
1318{
1319 return mWrapS;
1320}
1321
1322GLenum Texture::getWrapT() const
1323{
1324 return mWrapT;
1325}
1326
daniel@transgaming.com61208202011-03-21 16:38:50 +00001327void Texture::setImage(GLint unpackAlignment, const void *pixels, Image *image)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001328{
daniel@transgaming.com73de05a2011-11-09 17:45:24 +00001329 if (pixels != NULL)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001330 {
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001331 D3DLOCKED_RECT locked;
daniel@transgaming.com5cce3ff2011-11-09 17:45:14 +00001332 HRESULT result = image->lock(&locked, NULL);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001333
1334 if (SUCCEEDED(result))
1335 {
daniel@transgaming.com0c67f3c2011-11-09 17:45:38 +00001336 image->loadData(0, 0, image->getWidth(), image->getHeight(), image->getType(), unpackAlignment, pixels, locked.Pitch, locked.pBits);
daniel@transgaming.com5cce3ff2011-11-09 17:45:14 +00001337 image->unlock();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001338 }
1339
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00001340 mDirtyImages = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001341 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001342}
1343
daniel@transgaming.com61208202011-03-21 16:38:50 +00001344void Texture::setCompressedImage(GLsizei imageSize, const void *pixels, Image *image)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001345{
daniel@transgaming.com73de05a2011-11-09 17:45:24 +00001346 if (pixels != NULL)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001347 {
1348 D3DLOCKED_RECT locked;
daniel@transgaming.com5cce3ff2011-11-09 17:45:14 +00001349 HRESULT result = image->lock(&locked, NULL);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001350
1351 if (SUCCEEDED(result))
1352 {
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001353 int inputPitch = ComputeCompressedPitch(image->getWidth(), image->getFormat());
1354 int inputSize = ComputeCompressedSize(image->getWidth(), image->getHeight(), image->getFormat());
daniel@transgaming.comf749f0e2011-11-09 17:45:34 +00001355 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 +00001356 image->unlock();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001357 }
1358
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00001359 mDirtyImages = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001360 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001361}
1362
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001363bool 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 +00001364{
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001365 if (width + xoffset > image->getWidth() || height + yoffset > image->getHeight())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001366 {
1367 error(GL_INVALID_VALUE);
1368 return false;
1369 }
1370
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001371 if (IsCompressed(image->getFormat()))
jbauman@chromium.orge2f954c2011-05-03 20:45:27 +00001372 {
1373 error(GL_INVALID_OPERATION);
1374 return false;
1375 }
1376
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001377 if (format != image->getFormat())
jbauman@chromium.orge2f954c2011-05-03 20:45:27 +00001378 {
1379 error(GL_INVALID_OPERATION);
1380 return false;
1381 }
1382
daniel@transgaming.com73de05a2011-11-09 17:45:24 +00001383 if (pixels != NULL)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001384 {
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001385 D3DLOCKED_RECT locked;
daniel@transgaming.com5cce3ff2011-11-09 17:45:14 +00001386 HRESULT result = image->lock(&locked, NULL);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001387
1388 if (SUCCEEDED(result))
1389 {
daniel@transgaming.com0c67f3c2011-11-09 17:45:38 +00001390 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 +00001391 image->unlock();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001392 }
1393
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00001394 mDirtyImages = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001395 }
1396
1397 return true;
1398}
1399
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001400bool 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 +00001401{
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001402 if (width + xoffset > image->getWidth() || height + yoffset > image->getHeight())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001403 {
1404 error(GL_INVALID_VALUE);
1405 return false;
1406 }
1407
1408 if (format != getInternalFormat())
1409 {
1410 error(GL_INVALID_OPERATION);
1411 return false;
1412 }
1413
daniel@transgaming.com5cce3ff2011-11-09 17:45:14 +00001414 if (pixels != NULL)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001415 {
1416 RECT updateRegion;
1417 updateRegion.left = xoffset;
1418 updateRegion.right = xoffset + width;
1419 updateRegion.bottom = yoffset + height;
1420 updateRegion.top = yoffset;
1421
1422 D3DLOCKED_RECT locked;
daniel@transgaming.com5cce3ff2011-11-09 17:45:14 +00001423 HRESULT result = image->lock(&locked, &updateRegion);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001424
1425 if (SUCCEEDED(result))
1426 {
1427 int inputPitch = ComputeCompressedPitch(width, format);
1428 int inputSize = ComputeCompressedSize(width, height, format);
daniel@transgaming.comf749f0e2011-11-09 17:45:34 +00001429 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 +00001430 image->unlock();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001431 }
1432
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00001433 mDirtyImages = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001434 }
1435
1436 return true;
1437}
1438
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001439IDirect3DBaseTexture9 *Texture::getTexture()
1440{
1441 if (!isComplete())
1442 {
1443 return NULL;
1444 }
1445
daniel@transgaming.comaed18322011-03-21 16:38:13 +00001446 if (!getBaseTexture())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001447 {
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001448 createTexture();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001449 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001450
daniel@transgaming.comc50edcb2011-03-21 16:38:40 +00001451 updateTexture();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001452
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001453 return getBaseTexture();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001454}
1455
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00001456bool Texture::hasDirtyParameters() const
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001457{
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00001458 return mDirtyParameters;
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00001459}
1460
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00001461bool Texture::hasDirtyImages() const
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00001462{
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00001463 return mDirtyImages;
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00001464}
1465
1466void Texture::resetDirty()
1467{
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00001468 mDirtyParameters = false;
1469 mDirtyImages = false;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001470}
1471
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00001472unsigned int Texture::getSerial() const
1473{
1474 return mSerial;
1475}
1476
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001477GLint Texture::creationLevels(GLsizei width, GLsizei height, GLint maxlevel) const
1478{
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00001479 if ((isPow2(width) && isPow2(height)) || getContext()->supportsNonPower2Texture())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001480 {
1481 return maxlevel;
1482 }
1483 else
1484 {
1485 // OpenGL ES 2.0 without GL_OES_texture_npot does not permit NPOT mipmaps.
1486 return 1;
1487 }
1488}
1489
1490GLint Texture::creationLevels(GLsizei size, GLint maxlevel) const
1491{
1492 return creationLevels(size, size, maxlevel);
1493}
1494
1495int Texture::levelCount() const
1496{
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001497 return getBaseTexture() ? getBaseTexture()->GetLevelCount() : 0;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001498}
1499
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00001500unsigned int Texture::issueSerial()
1501{
1502 return mCurrentSerial++;
1503}
1504
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001505Texture2D::Texture2D(GLuint id) : Texture(id)
1506{
1507 mTexture = NULL;
jbauman@chromium.orgae345802011-03-30 22:04:25 +00001508 mSurface = NULL;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001509}
1510
1511Texture2D::~Texture2D()
1512{
1513 mColorbufferProxy.set(NULL);
1514
1515 if (mTexture)
1516 {
1517 mTexture->Release();
1518 mTexture = NULL;
1519 }
jbauman@chromium.orgae345802011-03-30 22:04:25 +00001520
1521 if (mSurface)
1522 {
1523 mSurface->setBoundTexture(NULL);
1524 mSurface = NULL;
1525 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001526}
1527
1528GLenum Texture2D::getTarget() const
1529{
1530 return GL_TEXTURE_2D;
1531}
1532
daniel@transgaming.com61208202011-03-21 16:38:50 +00001533GLsizei Texture2D::getWidth() const
1534{
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001535 return mImageArray[0].getWidth();
daniel@transgaming.com61208202011-03-21 16:38:50 +00001536}
1537
1538GLsizei Texture2D::getHeight() const
1539{
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001540 return mImageArray[0].getHeight();
daniel@transgaming.com61208202011-03-21 16:38:50 +00001541}
1542
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001543GLenum Texture2D::getInternalFormat() const
1544{
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001545 return mImageArray[0].getFormat();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001546}
1547
daniel@transgaming.com61208202011-03-21 16:38:50 +00001548GLenum Texture2D::getType() const
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001549{
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001550 return mImageArray[0].getType();
daniel@transgaming.com61208202011-03-21 16:38:50 +00001551}
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001552
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00001553D3DFORMAT Texture2D::getD3DFormat() const
1554{
1555 return mImageArray[0].getD3DFormat();
1556}
1557
daniel@transgaming.com68ae2992011-11-09 17:44:49 +00001558void Texture2D::redefineImage(GLint level, GLenum format, GLsizei width, GLsizei height, GLenum type, bool forceRedefine)
daniel@transgaming.com61208202011-03-21 16:38:50 +00001559{
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001560 GLsizei textureWidth = mImageArray[0].getWidth();
1561 GLsizei textureHeight = mImageArray[0].getHeight();
1562 GLenum textureFormat = mImageArray[0].getFormat();
1563 GLenum textureType = mImageArray[0].getType();
daniel@transgaming.com94a4f032011-03-21 16:38:55 +00001564
daniel@transgaming.com68ae2992011-11-09 17:44:49 +00001565 mImageArray[level].redefine(format, width, height, type);
daniel@transgaming.comc9ba4ad2011-11-09 17:44:35 +00001566
daniel@transgaming.com61208202011-03-21 16:38:50 +00001567 if (!mTexture)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001568 {
daniel@transgaming.com61208202011-03-21 16:38:50 +00001569 return;
1570 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001571
daniel@transgaming.com94a4f032011-03-21 16:38:55 +00001572 bool widthOkay = (textureWidth >> level == width) || (textureWidth >> level == 0 && width == 1);
1573 bool heightOkay = (textureHeight >> level == height) || (textureHeight >> level == 0 && height == 1);
1574 bool textureOkay = (widthOkay && heightOkay && textureFormat == format && textureType == type);
daniel@transgaming.com61208202011-03-21 16:38:50 +00001575
daniel@transgaming.comc9ba4ad2011-11-09 17:44:35 +00001576 if (!textureOkay || forceRedefine || mSurface)
daniel@transgaming.com61208202011-03-21 16:38:50 +00001577 {
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001578 for (int i = 0; i < IMPLEMENTATION_MAX_TEXTURE_LEVELS; i++)
1579 {
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001580 mImageArray[i].markDirty();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001581 }
1582
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00001583 mTexture->Release();
1584 mTexture = NULL;
1585 mDirtyImages = true;
1586 mIsRenderable = false;
jbauman@chromium.orgae345802011-03-30 22:04:25 +00001587
1588 if (mSurface)
1589 {
1590 mSurface->setBoundTexture(NULL);
1591 mSurface = NULL;
1592 }
apatrick@chromium.org57a2cd62011-06-08 00:04:07 +00001593
1594 mColorbufferProxy.set(NULL);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001595 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001596}
1597
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00001598void Texture2D::setImage(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001599{
daniel@transgaming.com68ae2992011-11-09 17:44:49 +00001600 redefineImage(level, format, width, height, type, false);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001601
daniel@transgaming.com61208202011-03-21 16:38:50 +00001602 Texture::setImage(unpackAlignment, pixels, &mImageArray[level]);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001603}
1604
jbauman@chromium.orgae345802011-03-30 22:04:25 +00001605void Texture2D::bindTexImage(egl::Surface *surface)
1606{
1607 GLenum format;
1608
1609 switch(surface->getFormat())
1610 {
1611 case D3DFMT_A8R8G8B8:
1612 format = GL_RGBA;
1613 break;
1614 case D3DFMT_X8R8G8B8:
1615 format = GL_RGB;
1616 break;
1617 default:
1618 UNIMPLEMENTED();
1619 return;
1620 }
1621
daniel@transgaming.com68ae2992011-11-09 17:44:49 +00001622 redefineImage(0, format, surface->getWidth(), surface->getHeight(), GL_UNSIGNED_BYTE, true);
jbauman@chromium.orgae345802011-03-30 22:04:25 +00001623
1624 IDirect3DTexture9 *texture = surface->getOffscreenTexture();
1625
1626 mTexture = texture;
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00001627 mDirtyImages = true;
jbauman@chromium.orgae345802011-03-30 22:04:25 +00001628 mIsRenderable = true;
1629 mSurface = surface;
1630 mSurface->setBoundTexture(this);
1631}
1632
1633void Texture2D::releaseTexImage()
1634{
daniel@transgaming.com68ae2992011-11-09 17:44:49 +00001635 redefineImage(0, GL_RGB, 0, 0, GL_UNSIGNED_BYTE, true);
jbauman@chromium.orgae345802011-03-30 22:04:25 +00001636}
1637
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00001638void Texture2D::setCompressedImage(GLint level, GLenum format, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001639{
daniel@transgaming.com68ae2992011-11-09 17:44:49 +00001640 redefineImage(level, format, width, height, GL_UNSIGNED_BYTE, false);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001641
daniel@transgaming.com61208202011-03-21 16:38:50 +00001642 Texture::setCompressedImage(imageSize, pixels, &mImageArray[level]);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001643}
1644
1645void Texture2D::commitRect(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height)
1646{
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001647 ASSERT(mImageArray[level].getSurface() != NULL);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001648
1649 if (level < levelCount())
1650 {
1651 IDirect3DSurface9 *destLevel = NULL;
1652 HRESULT result = mTexture->GetSurfaceLevel(level, &destLevel);
1653
1654 ASSERT(SUCCEEDED(result));
1655
1656 if (SUCCEEDED(result))
1657 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001658 Image *image = &mImageArray[level];
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001659
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001660 RECT sourceRect = transformPixelRect(xoffset, yoffset, width, height, image->getHeight());;
daniel@transgaming.comb612f882011-11-09 17:44:31 +00001661 POINT destPoint = {sourceRect.left, sourceRect.top};
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001662
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001663 result = getDevice()->UpdateSurface(image->getSurface(), &sourceRect, destLevel, &destPoint);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001664 ASSERT(SUCCEEDED(result));
1665
1666 destLevel->Release();
1667
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001668 image->markClean();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001669 }
1670 }
1671}
1672
1673void Texture2D::subImage(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels)
1674{
1675 if (Texture::subImage(xoffset, yoffset, width, height, format, type, unpackAlignment, pixels, &mImageArray[level]))
1676 {
1677 commitRect(level, xoffset, yoffset, width, height);
1678 }
1679}
1680
1681void Texture2D::subImageCompressed(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels)
1682{
1683 if (Texture::subImageCompressed(xoffset, yoffset, width, height, format, imageSize, pixels, &mImageArray[level]))
1684 {
1685 commitRect(level, xoffset, yoffset, width, height);
1686 }
1687}
1688
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00001689void Texture2D::copyImage(GLint level, GLenum format, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001690{
1691 IDirect3DSurface9 *renderTarget = source->getRenderTarget();
1692
1693 if (!renderTarget)
1694 {
1695 ERR("Failed to retrieve the render target.");
1696 return error(GL_OUT_OF_MEMORY);
1697 }
1698
daniel@transgaming.com68ae2992011-11-09 17:44:49 +00001699 redefineImage(level, format, width, height, GL_UNSIGNED_BYTE, false);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001700
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00001701 if (!mImageArray[level].isRenderable())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001702 {
daniel@transgaming.com90cfcc92011-11-09 17:45:48 +00001703 mImageArray[level].copy(0, 0, x, y, width, height, renderTarget);
1704 mDirtyImages = true;
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.com90cfcc92011-11-09 17:45:48 +00001751 mImageArray[level].copy(xoffset, yoffset, x, y, width, height, renderTarget);
1752 mDirtyImages = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001753 }
1754 else
1755 {
daniel@transgaming.com61208202011-03-21 16:38:50 +00001756 if (!mTexture || !mIsRenderable)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001757 {
1758 convertToRenderTarget();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001759 }
daniel@transgaming.com61208202011-03-21 16:38:50 +00001760
1761 updateTexture();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001762
1763 if (level < levelCount())
1764 {
1765 RECT sourceRect = transformPixelRect(x, y, width, height, source->getColorbuffer()->getHeight());
1766 sourceRect.left = clamp(sourceRect.left, 0, source->getColorbuffer()->getWidth());
1767 sourceRect.top = clamp(sourceRect.top, 0, source->getColorbuffer()->getHeight());
1768 sourceRect.right = clamp(sourceRect.right, 0, source->getColorbuffer()->getWidth());
1769 sourceRect.bottom = clamp(sourceRect.bottom, 0, source->getColorbuffer()->getHeight());
1770
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001771 GLint destYOffset = transformPixelYOffset(yoffset, height, mImageArray[level].getHeight());
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001772
1773 IDirect3DSurface9 *dest;
1774 HRESULT hr = mTexture->GetSurfaceLevel(level, &dest);
1775
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001776 getBlitter()->copy(source->getRenderTarget(), sourceRect, mImageArray[0].getFormat(), xoffset, destYOffset, dest);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001777 dest->Release();
1778 }
1779 }
1780}
1781
1782// Tests for GL texture object completeness. [OpenGL ES 2.0.24] section 3.7.10 page 81.
1783bool Texture2D::isComplete() const
1784{
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001785 GLsizei width = mImageArray[0].getWidth();
1786 GLsizei height = mImageArray[0].getHeight();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001787
1788 if (width <= 0 || height <= 0)
1789 {
1790 return false;
1791 }
1792
1793 bool mipmapping = false;
1794
1795 switch (mMinFilter)
1796 {
1797 case GL_NEAREST:
1798 case GL_LINEAR:
1799 mipmapping = false;
1800 break;
1801 case GL_NEAREST_MIPMAP_NEAREST:
1802 case GL_LINEAR_MIPMAP_NEAREST:
1803 case GL_NEAREST_MIPMAP_LINEAR:
1804 case GL_LINEAR_MIPMAP_LINEAR:
1805 mipmapping = true;
1806 break;
1807 default: UNREACHABLE();
1808 }
1809
1810 if ((getInternalFormat() == GL_FLOAT && !getContext()->supportsFloatLinearFilter()) ||
1811 (getInternalFormat() == GL_HALF_FLOAT_OES && !getContext()->supportsHalfFloatLinearFilter()))
1812 {
1813 if (mMagFilter != GL_NEAREST || (mMinFilter != GL_NEAREST && mMinFilter != GL_NEAREST_MIPMAP_NEAREST))
1814 {
1815 return false;
1816 }
1817 }
1818
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00001819 bool npot = getContext()->supportsNonPower2Texture();
1820
1821 if (!npot)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001822 {
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00001823 if ((getWrapS() != GL_CLAMP_TO_EDGE && !isPow2(width)) ||
1824 (getWrapT() != GL_CLAMP_TO_EDGE && !isPow2(height)))
1825 {
1826 return false;
1827 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001828 }
1829
1830 if (mipmapping)
1831 {
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00001832 if (!npot)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001833 {
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00001834 if (!isPow2(width) || !isPow2(height))
1835 {
1836 return false;
1837 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001838 }
1839
1840 int q = log2(std::max(width, height));
1841
1842 for (int level = 1; level <= q; level++)
1843 {
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001844 if (mImageArray[level].getFormat() != mImageArray[0].getFormat())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001845 {
1846 return false;
1847 }
1848
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001849 if (mImageArray[level].getType() != mImageArray[0].getType())
daniel@transgaming.com0bd22bc2011-03-21 16:38:26 +00001850 {
1851 return false;
1852 }
1853
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001854 if (mImageArray[level].getWidth() != std::max(1, width >> level))
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001855 {
1856 return false;
1857 }
1858
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001859 if (mImageArray[level].getHeight() != std::max(1, height >> level))
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001860 {
1861 return false;
1862 }
1863 }
1864 }
1865
1866 return true;
1867}
1868
1869bool Texture2D::isCompressed() const
1870{
1871 return IsCompressed(getInternalFormat());
1872}
1873
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001874IDirect3DBaseTexture9 *Texture2D::getBaseTexture() const
1875{
1876 return mTexture;
1877}
1878
1879// Constructs a Direct3D 9 texture resource from the texture images
1880void Texture2D::createTexture()
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001881{
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001882 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00001883 D3DFORMAT format = mImageArray[0].getD3DFormat();
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001884 GLint levels = creationLevels(mImageArray[0].getWidth(), mImageArray[0].getHeight(), 0);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001885
daniel@transgaming.com61208202011-03-21 16:38:50 +00001886 IDirect3DTexture9 *texture = NULL;
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001887 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 +00001888
1889 if (FAILED(result))
1890 {
1891 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001892 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001893 }
1894
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001895 if (mTexture)
1896 {
1897 mTexture->Release();
1898 }
1899
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001900 mTexture = texture;
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00001901 mDirtyImages = true;
daniel@transgaming.comaed18322011-03-21 16:38:13 +00001902 mIsRenderable = false;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001903}
1904
1905void Texture2D::updateTexture()
1906{
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001907 int levels = levelCount();
1908
1909 for (int level = 0; level < levels; level++)
1910 {
daniel@transgaming.comb612f882011-11-09 17:44:31 +00001911 Image *image = &mImageArray[level];
1912
daniel@transgaming.com5cce3ff2011-11-09 17:45:14 +00001913 if (image->isDirty())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001914 {
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001915 commitRect(level, 0, 0, mImageArray[level].getWidth(), mImageArray[level].getHeight());
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001916 }
1917 }
1918}
1919
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001920void Texture2D::convertToRenderTarget()
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001921{
1922 IDirect3DTexture9 *texture = NULL;
1923
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001924 if (mImageArray[0].getWidth() != 0 && mImageArray[0].getHeight() != 0)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001925 {
1926 egl::Display *display = getDisplay();
1927 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00001928 D3DFORMAT format = mImageArray[0].getD3DFormat();
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001929 GLint levels = creationLevels(mImageArray[0].getWidth(), mImageArray[0].getHeight(), 0);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001930
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00001931 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 +00001932
1933 if (FAILED(result))
1934 {
1935 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001936 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001937 }
1938
1939 if (mTexture != NULL)
1940 {
1941 int levels = levelCount();
1942 for (int i = 0; i < levels; i++)
1943 {
1944 IDirect3DSurface9 *source;
1945 result = mTexture->GetSurfaceLevel(i, &source);
1946
1947 if (FAILED(result))
1948 {
1949 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
1950
1951 texture->Release();
1952
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001953 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001954 }
1955
1956 IDirect3DSurface9 *dest;
1957 result = texture->GetSurfaceLevel(i, &dest);
1958
1959 if (FAILED(result))
1960 {
1961 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
1962
1963 texture->Release();
1964 source->Release();
1965
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001966 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001967 }
1968
1969 display->endScene();
1970 result = device->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
1971
1972 if (FAILED(result))
1973 {
1974 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
1975
1976 texture->Release();
1977 source->Release();
1978 dest->Release();
1979
daniel@transgaming.com68076a02011-03-21 16:38:09 +00001980 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001981 }
1982
1983 source->Release();
1984 dest->Release();
1985 }
1986 }
1987 }
1988
1989 if (mTexture != NULL)
1990 {
1991 mTexture->Release();
1992 }
1993
1994 mTexture = texture;
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00001995 mDirtyImages = true;
daniel@transgaming.comaed18322011-03-21 16:38:13 +00001996 mIsRenderable = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001997}
1998
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00001999void Texture2D::generateMipmaps()
2000{
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00002001 if (!getContext()->supportsNonPower2Texture())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002002 {
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002003 if (!isPow2(mImageArray[0].getWidth()) || !isPow2(mImageArray[0].getHeight()))
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00002004 {
2005 return error(GL_INVALID_OPERATION);
2006 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002007 }
2008
2009 // Purge array levels 1 through q and reset them to represent the generated mipmap levels.
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002010 unsigned int q = log2(std::max(mImageArray[0].getWidth(), mImageArray[0].getHeight()));
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002011 for (unsigned int i = 1; i <= q; i++)
2012 {
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002013 mImageArray[i].redefine(mImageArray[0].getFormat(),
2014 std::max(mImageArray[0].getWidth() >> i, 1),
2015 std::max(mImageArray[0].getHeight() >> i, 1),
2016 mImageArray[0].getType());
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002017 }
2018
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002019 if (mIsRenderable)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002020 {
2021 if (mTexture == NULL)
2022 {
2023 ERR(" failed because mTexture was null.");
2024 return;
2025 }
2026
2027 for (unsigned int i = 1; i <= q; i++)
2028 {
2029 IDirect3DSurface9 *upper = NULL;
2030 IDirect3DSurface9 *lower = NULL;
2031
2032 mTexture->GetSurfaceLevel(i-1, &upper);
2033 mTexture->GetSurfaceLevel(i, &lower);
2034
2035 if (upper != NULL && lower != NULL)
2036 {
2037 getBlitter()->boxFilter(upper, lower);
2038 }
2039
2040 if (upper != NULL) upper->Release();
2041 if (lower != NULL) lower->Release();
daniel@transgaming.com61208202011-03-21 16:38:50 +00002042
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002043 mImageArray[i].markClean();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002044 }
2045 }
2046 else
2047 {
2048 for (unsigned int i = 1; i <= q; i++)
2049 {
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002050 if (mImageArray[i].getSurface() == NULL)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002051 {
2052 return error(GL_OUT_OF_MEMORY);
2053 }
2054
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002055 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 +00002056 {
2057 ERR(" failed to load filter %d to %d.", i - 1, i);
2058 }
2059
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002060 mImageArray[i].markDirty();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002061 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002062 }
2063}
2064
2065Renderbuffer *Texture2D::getRenderbuffer(GLenum target)
2066{
2067 if (target != GL_TEXTURE_2D)
2068 {
2069 return error(GL_INVALID_OPERATION, (Renderbuffer *)NULL);
2070 }
2071
2072 if (mColorbufferProxy.get() == NULL)
2073 {
2074 mColorbufferProxy.set(new Renderbuffer(id(), new Colorbuffer(this, target)));
2075 }
2076
2077 return mColorbufferProxy.get();
2078}
2079
2080IDirect3DSurface9 *Texture2D::getRenderTarget(GLenum target)
2081{
2082 ASSERT(target == GL_TEXTURE_2D);
2083
daniel@transgaming.com61208202011-03-21 16:38:50 +00002084 if (!mIsRenderable)
2085 {
2086 convertToRenderTarget();
2087 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002088
2089 if (mTexture == NULL)
2090 {
2091 return NULL;
2092 }
daniel@transgaming.com61208202011-03-21 16:38:50 +00002093
2094 updateTexture();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002095
2096 IDirect3DSurface9 *renderTarget = NULL;
2097 mTexture->GetSurfaceLevel(0, &renderTarget);
2098
2099 return renderTarget;
2100}
2101
2102TextureCubeMap::TextureCubeMap(GLuint id) : Texture(id)
2103{
2104 mTexture = NULL;
2105}
2106
2107TextureCubeMap::~TextureCubeMap()
2108{
2109 for (int i = 0; i < 6; i++)
2110 {
2111 mFaceProxies[i].set(NULL);
2112 }
2113
2114 if (mTexture)
2115 {
2116 mTexture->Release();
2117 mTexture = NULL;
2118 }
2119}
2120
2121GLenum TextureCubeMap::getTarget() const
2122{
2123 return GL_TEXTURE_CUBE_MAP;
2124}
2125
daniel@transgaming.com61208202011-03-21 16:38:50 +00002126GLsizei TextureCubeMap::getWidth() const
2127{
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002128 return mImageArray[0][0].getWidth();
daniel@transgaming.com61208202011-03-21 16:38:50 +00002129}
2130
2131GLsizei TextureCubeMap::getHeight() const
2132{
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002133 return mImageArray[0][0].getHeight();
daniel@transgaming.com61208202011-03-21 16:38:50 +00002134}
2135
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002136GLenum TextureCubeMap::getInternalFormat() const
2137{
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002138 return mImageArray[0][0].getFormat();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002139}
2140
daniel@transgaming.com61208202011-03-21 16:38:50 +00002141GLenum TextureCubeMap::getType() const
2142{
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002143 return mImageArray[0][0].getType();
daniel@transgaming.com61208202011-03-21 16:38:50 +00002144}
2145
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00002146D3DFORMAT TextureCubeMap::getD3DFormat() const
2147{
2148 return mImageArray[0][0].getD3DFormat();
2149}
2150
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002151void 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 +00002152{
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002153 setImage(0, level, width, height, format, type, unpackAlignment, pixels);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002154}
2155
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002156void 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 +00002157{
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002158 setImage(1, level, width, height, format, type, unpackAlignment, pixels);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002159}
2160
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002161void 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 +00002162{
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002163 setImage(2, level, width, height, format, type, unpackAlignment, pixels);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002164}
2165
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002166void 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 +00002167{
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002168 setImage(3, level, width, height, format, type, unpackAlignment, pixels);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002169}
2170
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002171void 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 +00002172{
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002173 setImage(4, level, width, height, format, type, unpackAlignment, pixels);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002174}
2175
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002176void 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 +00002177{
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002178 setImage(5, level, width, height, format, type, unpackAlignment, pixels);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002179}
2180
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002181void 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 +00002182{
daniel@transgaming.com68ae2992011-11-09 17:44:49 +00002183 redefineImage(faceIndex(face), level, format, width, height, GL_UNSIGNED_BYTE);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002184
daniel@transgaming.com61208202011-03-21 16:38:50 +00002185 Texture::setCompressedImage(imageSize, pixels, &mImageArray[faceIndex(face)][level]);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002186}
2187
daniel@transgaming.comb612f882011-11-09 17:44:31 +00002188void TextureCubeMap::commitRect(int face, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002189{
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002190 ASSERT(mImageArray[face][level].getSurface() != NULL);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002191
2192 if (level < levelCount())
2193 {
daniel@transgaming.comb612f882011-11-09 17:44:31 +00002194 IDirect3DSurface9 *destLevel = getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002195 ASSERT(destLevel != NULL);
2196
2197 if (destLevel != NULL)
2198 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002199 Image *image = &mImageArray[face][level];
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002200
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002201 RECT sourceRect = transformPixelRect(xoffset, yoffset, width, height, image->getHeight());;
daniel@transgaming.comb612f882011-11-09 17:44:31 +00002202 POINT destPoint = {sourceRect.left, sourceRect.top};
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002203
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002204 HRESULT result = getDevice()->UpdateSurface(image->getSurface(), &sourceRect, destLevel, &destPoint);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002205 ASSERT(SUCCEEDED(result));
2206
2207 destLevel->Release();
2208
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002209 image->markClean();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002210 }
2211 }
2212}
2213
2214void TextureCubeMap::subImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels)
2215{
2216 if (Texture::subImage(xoffset, yoffset, width, height, format, type, unpackAlignment, pixels, &mImageArray[faceIndex(target)][level]))
2217 {
daniel@transgaming.comb612f882011-11-09 17:44:31 +00002218 commitRect(faceIndex(target), level, xoffset, yoffset, width, height);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002219 }
2220}
2221
2222void TextureCubeMap::subImageCompressed(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels)
2223{
2224 if (Texture::subImageCompressed(xoffset, yoffset, width, height, format, imageSize, pixels, &mImageArray[faceIndex(target)][level]))
2225 {
daniel@transgaming.comb612f882011-11-09 17:44:31 +00002226 commitRect(faceIndex(target), level, xoffset, yoffset, width, height);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002227 }
2228}
2229
2230// Tests for GL texture object completeness. [OpenGL ES 2.0.24] section 3.7.10 page 81.
2231bool TextureCubeMap::isComplete() const
2232{
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002233 int size = mImageArray[0][0].getWidth();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002234
2235 if (size <= 0)
2236 {
2237 return false;
2238 }
2239
2240 bool mipmapping;
2241
2242 switch (mMinFilter)
2243 {
2244 case GL_NEAREST:
2245 case GL_LINEAR:
2246 mipmapping = false;
2247 break;
2248 case GL_NEAREST_MIPMAP_NEAREST:
2249 case GL_LINEAR_MIPMAP_NEAREST:
2250 case GL_NEAREST_MIPMAP_LINEAR:
2251 case GL_LINEAR_MIPMAP_LINEAR:
2252 mipmapping = true;
2253 break;
2254 default: UNREACHABLE();
2255 }
2256
2257 for (int face = 0; face < 6; face++)
2258 {
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002259 if (mImageArray[face][0].getWidth() != size || mImageArray[face][0].getHeight() != size)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002260 {
2261 return false;
2262 }
2263 }
2264
2265 if ((getInternalFormat() == GL_FLOAT && !getContext()->supportsFloatLinearFilter()) ||
2266 (getInternalFormat() == GL_HALF_FLOAT_OES && !getContext()->supportsHalfFloatLinearFilter()))
2267 {
2268 if (mMagFilter != GL_NEAREST || (mMinFilter != GL_NEAREST && mMinFilter != GL_NEAREST_MIPMAP_NEAREST))
2269 {
2270 return false;
2271 }
2272 }
2273
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00002274 bool npot = getContext()->supportsNonPower2Texture();
2275
2276 if (!npot)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002277 {
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00002278 if ((getWrapS() != GL_CLAMP_TO_EDGE || getWrapT() != GL_CLAMP_TO_EDGE) && !isPow2(size))
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002279 {
2280 return false;
2281 }
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00002282 }
2283
2284 if (mipmapping)
2285 {
2286 if (!npot)
2287 {
2288 if (!isPow2(size))
2289 {
2290 return false;
2291 }
2292 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002293
2294 int q = log2(size);
2295
2296 for (int face = 0; face < 6; face++)
2297 {
2298 for (int level = 1; level <= q; level++)
2299 {
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002300 if (mImageArray[face][level].getFormat() != mImageArray[0][0].getFormat())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002301 {
2302 return false;
2303 }
2304
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002305 if (mImageArray[face][level].getType() != mImageArray[0][0].getType())
daniel@transgaming.com0bd22bc2011-03-21 16:38:26 +00002306 {
2307 return false;
2308 }
2309
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002310 if (mImageArray[face][level].getWidth() != std::max(1, size >> level))
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002311 {
2312 return false;
2313 }
2314
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002315 ASSERT(mImageArray[face][level].getHeight() == mImageArray[face][level].getWidth());
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002316 }
2317 }
2318 }
2319
2320 return true;
2321}
2322
2323bool TextureCubeMap::isCompressed() const
2324{
2325 return IsCompressed(getInternalFormat());
2326}
2327
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002328IDirect3DBaseTexture9 *TextureCubeMap::getBaseTexture() const
2329{
2330 return mTexture;
2331}
2332
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002333// Constructs a Direct3D 9 texture resource from the texture images, or returns an existing one
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002334void TextureCubeMap::createTexture()
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002335{
2336 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00002337 D3DFORMAT format = mImageArray[0][0].getD3DFormat();
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002338 GLint levels = creationLevels(mImageArray[0][0].getWidth(), 0);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002339
daniel@transgaming.com0bd22bc2011-03-21 16:38:26 +00002340 IDirect3DCubeTexture9 *texture = NULL;
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002341 HRESULT result = device->CreateCubeTexture(mImageArray[0][0].getWidth(), levels, 0, format, D3DPOOL_DEFAULT, &texture, NULL);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002342
2343 if (FAILED(result))
2344 {
2345 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002346 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002347 }
2348
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002349 if (mTexture)
2350 {
2351 mTexture->Release();
2352 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002353
2354 mTexture = texture;
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00002355 mDirtyImages = true;
daniel@transgaming.comaed18322011-03-21 16:38:13 +00002356 mIsRenderable = false;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002357}
2358
2359void TextureCubeMap::updateTexture()
2360{
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002361 for (int face = 0; face < 6; face++)
2362 {
2363 int levels = levelCount();
2364 for (int level = 0; level < levels; level++)
2365 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002366 Image *image = &mImageArray[face][level];
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002367
daniel@transgaming.com5cce3ff2011-11-09 17:45:14 +00002368 if (image->isDirty())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002369 {
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002370 commitRect(face, level, 0, 0, image->getWidth(), image->getHeight());
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002371 }
2372 }
2373 }
2374}
2375
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002376void TextureCubeMap::convertToRenderTarget()
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002377{
2378 IDirect3DCubeTexture9 *texture = NULL;
2379
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002380 if (mImageArray[0][0].getWidth() != 0)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002381 {
2382 egl::Display *display = getDisplay();
2383 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00002384 D3DFORMAT format = mImageArray[0][0].getD3DFormat();
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002385 GLint levels = creationLevels(mImageArray[0][0].getWidth(), 0);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002386
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002387 HRESULT result = device->CreateCubeTexture(mImageArray[0][0].getWidth(), levels, D3DUSAGE_RENDERTARGET, format, D3DPOOL_DEFAULT, &texture, NULL);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002388
2389 if (FAILED(result))
2390 {
2391 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002392 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002393 }
2394
2395 if (mTexture != NULL)
2396 {
2397 int levels = levelCount();
2398 for (int f = 0; f < 6; f++)
2399 {
2400 for (int i = 0; i < levels; i++)
2401 {
2402 IDirect3DSurface9 *source;
2403 result = mTexture->GetCubeMapSurface(static_cast<D3DCUBEMAP_FACES>(f), i, &source);
2404
2405 if (FAILED(result))
2406 {
2407 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
2408
2409 texture->Release();
2410
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002411 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002412 }
2413
2414 IDirect3DSurface9 *dest;
2415 result = texture->GetCubeMapSurface(static_cast<D3DCUBEMAP_FACES>(f), i, &dest);
2416
2417 if (FAILED(result))
2418 {
2419 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
2420
2421 texture->Release();
2422 source->Release();
2423
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002424 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002425 }
2426
2427 display->endScene();
2428 result = device->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
2429
2430 if (FAILED(result))
2431 {
2432 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
2433
2434 texture->Release();
2435 source->Release();
2436 dest->Release();
2437
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002438 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002439 }
daniel@transgaming.coma1a86202011-08-09 13:41:08 +00002440
2441 source->Release();
2442 dest->Release();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002443 }
2444 }
2445 }
2446 }
2447
2448 if (mTexture != NULL)
2449 {
2450 mTexture->Release();
2451 }
2452
2453 mTexture = texture;
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00002454 mDirtyImages = true;
daniel@transgaming.comaed18322011-03-21 16:38:13 +00002455 mIsRenderable = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002456}
2457
daniel@transgaming.com61208202011-03-21 16:38:50 +00002458void 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 +00002459{
daniel@transgaming.com68ae2992011-11-09 17:44:49 +00002460 redefineImage(faceIndex, level, format, width, height, type);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002461
daniel@transgaming.com61208202011-03-21 16:38:50 +00002462 Texture::setImage(unpackAlignment, pixels, &mImageArray[faceIndex][level]);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002463}
2464
2465unsigned int TextureCubeMap::faceIndex(GLenum face)
2466{
2467 META_ASSERT(GL_TEXTURE_CUBE_MAP_NEGATIVE_X - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 1);
2468 META_ASSERT(GL_TEXTURE_CUBE_MAP_POSITIVE_Y - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 2);
2469 META_ASSERT(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 3);
2470 META_ASSERT(GL_TEXTURE_CUBE_MAP_POSITIVE_Z - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 4);
2471 META_ASSERT(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 5);
2472
2473 return face - GL_TEXTURE_CUBE_MAP_POSITIVE_X;
2474}
2475
daniel@transgaming.com68ae2992011-11-09 17:44:49 +00002476void TextureCubeMap::redefineImage(int face, GLint level, GLenum format, GLsizei width, GLsizei height, GLenum type)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002477{
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002478 GLsizei textureWidth = mImageArray[0][0].getWidth();
2479 GLsizei textureHeight = mImageArray[0][0].getHeight();
2480 GLenum textureFormat = mImageArray[0][0].getFormat();
2481 GLenum textureType = mImageArray[0][0].getType();
daniel@transgaming.com94a4f032011-03-21 16:38:55 +00002482
daniel@transgaming.com68ae2992011-11-09 17:44:49 +00002483 mImageArray[face][level].redefine(format, width, height, type);
daniel@transgaming.comc9ba4ad2011-11-09 17:44:35 +00002484
daniel@transgaming.com61208202011-03-21 16:38:50 +00002485 if (!mTexture)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002486 {
daniel@transgaming.com61208202011-03-21 16:38:50 +00002487 return;
2488 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002489
daniel@transgaming.com94a4f032011-03-21 16:38:55 +00002490 bool sizeOkay = (textureWidth >> level == width);
2491 bool textureOkay = (sizeOkay && textureFormat == format && textureType == type);
daniel@transgaming.com61208202011-03-21 16:38:50 +00002492
daniel@transgaming.comc9ba4ad2011-11-09 17:44:35 +00002493 if (!textureOkay)
daniel@transgaming.com61208202011-03-21 16:38:50 +00002494 {
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002495 for (int i = 0; i < IMPLEMENTATION_MAX_TEXTURE_LEVELS; i++)
2496 {
2497 for (int f = 0; f < 6; f++)
2498 {
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002499 mImageArray[f][i].markDirty();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002500 }
2501 }
2502
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00002503 mTexture->Release();
2504 mTexture = NULL;
2505 mDirtyImages = true;
2506 mIsRenderable = false;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002507 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002508}
2509
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00002510void 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 +00002511{
2512 IDirect3DSurface9 *renderTarget = source->getRenderTarget();
2513
2514 if (!renderTarget)
2515 {
2516 ERR("Failed to retrieve the render target.");
2517 return error(GL_OUT_OF_MEMORY);
2518 }
2519
2520 unsigned int faceindex = faceIndex(target);
daniel@transgaming.com68ae2992011-11-09 17:44:49 +00002521 redefineImage(faceindex, level, format, width, height, GL_UNSIGNED_BYTE);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002522
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00002523 if (!mImageArray[faceindex][level].isRenderable())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002524 {
daniel@transgaming.com90cfcc92011-11-09 17:45:48 +00002525 mImageArray[faceindex][level].copy(0, 0, x, y, width, height, renderTarget);
2526 mDirtyImages = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002527 }
2528 else
2529 {
daniel@transgaming.com61208202011-03-21 16:38:50 +00002530 if (!mTexture || !mIsRenderable)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002531 {
2532 convertToRenderTarget();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002533 }
daniel@transgaming.com3b3c1d42011-06-08 20:38:09 +00002534
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002535 mImageArray[faceindex][level].markClean();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002536
2537 ASSERT(width == height);
2538
2539 if (width > 0 && level < levelCount())
2540 {
2541 RECT sourceRect = transformPixelRect(x, y, width, height, source->getColorbuffer()->getHeight());
2542 sourceRect.left = clamp(sourceRect.left, 0, source->getColorbuffer()->getWidth());
2543 sourceRect.top = clamp(sourceRect.top, 0, source->getColorbuffer()->getHeight());
2544 sourceRect.right = clamp(sourceRect.right, 0, source->getColorbuffer()->getWidth());
2545 sourceRect.bottom = clamp(sourceRect.bottom, 0, source->getColorbuffer()->getHeight());
2546
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002547 GLint destYOffset = transformPixelYOffset(0, height, mImageArray[faceindex][level].getWidth());
daniel@transgaming.comeef864a2011-04-22 11:33:27 +00002548
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002549 IDirect3DSurface9 *dest = getCubeMapSurface(target, level);
2550
daniel@transgaming.comeef864a2011-04-22 11:33:27 +00002551 getBlitter()->copy(source->getRenderTarget(), sourceRect, format, 0, destYOffset, dest);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002552 dest->Release();
2553 }
2554 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002555}
2556
daniel@transgaming.comb612f882011-11-09 17:44:31 +00002557IDirect3DSurface9 *TextureCubeMap::getCubeMapSurface(GLenum target, unsigned int level)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002558{
2559 if (mTexture == NULL)
2560 {
2561 UNREACHABLE();
2562 return NULL;
2563 }
2564
2565 IDirect3DSurface9 *surface = NULL;
2566
daniel@transgaming.comb612f882011-11-09 17:44:31 +00002567 HRESULT hr = mTexture->GetCubeMapSurface(es2dx::ConvertCubeFace(target), level, &surface);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002568
2569 return (SUCCEEDED(hr)) ? surface : NULL;
2570}
2571
2572void TextureCubeMap::copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source)
2573{
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002574 GLsizei size = mImageArray[faceIndex(target)][level].getWidth();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002575
2576 if (xoffset + width > size || yoffset + height > size)
2577 {
2578 return error(GL_INVALID_VALUE);
2579 }
2580
2581 IDirect3DSurface9 *renderTarget = source->getRenderTarget();
2582
2583 if (!renderTarget)
2584 {
2585 ERR("Failed to retrieve the render target.");
2586 return error(GL_OUT_OF_MEMORY);
2587 }
2588
2589 unsigned int faceindex = faceIndex(target);
daniel@transgaming.com01dae852011-11-09 17:44:53 +00002590
daniel@transgaming.com549bdef2011-03-29 00:57:01 +00002591 if (!mImageArray[faceindex][level].isRenderable() || (!mTexture && !isComplete()))
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002592 {
daniel@transgaming.com90cfcc92011-11-09 17:45:48 +00002593 mImageArray[faceindex][level].copy(0, 0, x, y, width, height, renderTarget);
2594 mDirtyImages = true;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002595 }
2596 else
2597 {
daniel@transgaming.com61208202011-03-21 16:38:50 +00002598 if (!mTexture || !mIsRenderable)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002599 {
2600 convertToRenderTarget();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002601 }
daniel@transgaming.com61208202011-03-21 16:38:50 +00002602
2603 updateTexture();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002604
2605 if (level < levelCount())
2606 {
2607 RECT sourceRect = transformPixelRect(x, y, width, height, source->getColorbuffer()->getHeight());
2608 sourceRect.left = clamp(sourceRect.left, 0, source->getColorbuffer()->getWidth());
2609 sourceRect.top = clamp(sourceRect.top, 0, source->getColorbuffer()->getHeight());
2610 sourceRect.right = clamp(sourceRect.right, 0, source->getColorbuffer()->getWidth());
2611 sourceRect.bottom = clamp(sourceRect.bottom, 0, source->getColorbuffer()->getHeight());
2612
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002613 GLint destYOffset = transformPixelYOffset(yoffset, height, mImageArray[faceindex][level].getWidth());
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002614
2615 IDirect3DSurface9 *dest = getCubeMapSurface(target, level);
2616
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002617 getBlitter()->copy(source->getRenderTarget(), sourceRect, mImageArray[0][0].getFormat(), xoffset, destYOffset, dest);
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002618 dest->Release();
2619 }
2620 }
2621}
2622
2623bool TextureCubeMap::isCubeComplete() const
2624{
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002625 if (mImageArray[0][0].getWidth() == 0)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002626 {
2627 return false;
2628 }
2629
2630 for (unsigned int f = 1; f < 6; f++)
2631 {
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002632 if (mImageArray[f][0].getWidth() != mImageArray[0][0].getWidth()
2633 || mImageArray[f][0].getFormat() != mImageArray[0][0].getFormat())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002634 {
2635 return false;
2636 }
2637 }
2638
2639 return true;
2640}
2641
2642void TextureCubeMap::generateMipmaps()
2643{
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00002644 if (!isCubeComplete())
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002645 {
2646 return error(GL_INVALID_OPERATION);
2647 }
2648
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00002649 if (!getContext()->supportsNonPower2Texture())
2650 {
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002651 if (!isPow2(mImageArray[0][0].getWidth()))
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00002652 {
2653 return error(GL_INVALID_OPERATION);
2654 }
2655 }
2656
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002657 // Purge array levels 1 through q and reset them to represent the generated mipmap levels.
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002658 unsigned int q = log2(mImageArray[0][0].getWidth());
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002659 for (unsigned int f = 0; f < 6; f++)
2660 {
2661 for (unsigned int i = 1; i <= q; i++)
2662 {
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002663 mImageArray[f][i].redefine(mImageArray[f][0].getFormat(),
2664 std::max(mImageArray[f][0].getWidth() >> i, 1),
2665 std::max(mImageArray[f][0].getWidth() >> i, 1),
2666 mImageArray[f][0].getType());
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002667 }
2668 }
2669
daniel@transgaming.com68076a02011-03-21 16:38:09 +00002670 if (mIsRenderable)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002671 {
2672 if (mTexture == NULL)
2673 {
2674 return;
2675 }
2676
2677 for (unsigned int f = 0; f < 6; f++)
2678 {
2679 for (unsigned int i = 1; i <= q; i++)
2680 {
2681 IDirect3DSurface9 *upper = getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i-1);
2682 IDirect3DSurface9 *lower = getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i);
2683
2684 if (upper != NULL && lower != NULL)
2685 {
2686 getBlitter()->boxFilter(upper, lower);
2687 }
2688
2689 if (upper != NULL) upper->Release();
2690 if (lower != NULL) lower->Release();
daniel@transgaming.com61208202011-03-21 16:38:50 +00002691
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002692 mImageArray[f][i].markClean();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002693 }
2694 }
2695 }
2696 else
2697 {
2698 for (unsigned int f = 0; f < 6; f++)
2699 {
2700 for (unsigned int i = 1; i <= q; i++)
2701 {
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002702 if (mImageArray[f][i].getSurface() == NULL)
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002703 {
2704 return error(GL_OUT_OF_MEMORY);
2705 }
2706
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002707 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 +00002708 {
2709 ERR(" failed to load filter %d to %d.", i - 1, i);
2710 }
2711
daniel@transgaming.comdff362f2011-11-09 17:45:08 +00002712 mImageArray[f][i].markDirty();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002713 }
2714 }
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002715 }
2716}
2717
2718Renderbuffer *TextureCubeMap::getRenderbuffer(GLenum target)
2719{
2720 if (!IsCubemapTextureTarget(target))
2721 {
2722 return error(GL_INVALID_OPERATION, (Renderbuffer *)NULL);
2723 }
2724
2725 unsigned int face = faceIndex(target);
2726
2727 if (mFaceProxies[face].get() == NULL)
2728 {
2729 mFaceProxies[face].set(new Renderbuffer(id(), new Colorbuffer(this, target)));
2730 }
2731
2732 return mFaceProxies[face].get();
2733}
2734
2735IDirect3DSurface9 *TextureCubeMap::getRenderTarget(GLenum target)
2736{
2737 ASSERT(IsCubemapTextureTarget(target));
2738
daniel@transgaming.com61208202011-03-21 16:38:50 +00002739 if (!mIsRenderable)
2740 {
2741 convertToRenderTarget();
2742 }
2743
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002744 if (mTexture == NULL)
2745 {
2746 return NULL;
2747 }
daniel@transgaming.com61208202011-03-21 16:38:50 +00002748
2749 updateTexture();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002750
2751 IDirect3DSurface9 *renderTarget = NULL;
2752 mTexture->GetCubeMapSurface(es2dx::ConvertCubeFace(target), 0, &renderTarget);
2753
2754 return renderTarget;
2755}
2756
daniel@transgaming.comb612f882011-11-09 17:44:31 +00002757}