daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
| 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 | |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 11 | #include "libGLESv2/Texture.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 12 | |
daniel@transgaming.com | 1697302 | 2010-03-11 19:22:19 +0000 | [diff] [blame] | 13 | #include <algorithm> |
| 14 | |
alokp@chromium.org | ea0e1af | 2010-03-22 19:33:14 +0000 | [diff] [blame] | 15 | #include "common/debug.h" |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 16 | |
| 17 | #include "libGLESv2/main.h" |
| 18 | #include "libGLESv2/mathutil.h" |
| 19 | #include "libGLESv2/utilities.h" |
| 20 | #include "libGLESv2/Blit.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 21 | |
| 22 | namespace gl |
| 23 | { |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 24 | |
| 25 | Texture::Image::Image() |
daniel@transgaming.com | 0186813 | 2010-08-24 19:21:17 +0000 | [diff] [blame^] | 26 | : width(0), height(0), dirty(false), surface(NULL), format(GL_NONE) |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 27 | { |
| 28 | } |
| 29 | |
| 30 | Texture::Image::~Image() |
| 31 | { |
| 32 | if (surface) surface->Release(); |
| 33 | } |
| 34 | |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 35 | Texture::Texture(GLuint id) : RefCountObject(id) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 36 | { |
| 37 | mMinFilter = GL_NEAREST_MIPMAP_LINEAR; |
| 38 | mMagFilter = GL_LINEAR; |
| 39 | mWrapS = GL_REPEAT; |
| 40 | mWrapT = GL_REPEAT; |
daniel@transgaming.com | 29d2700 | 2010-03-11 19:41:22 +0000 | [diff] [blame] | 41 | |
daniel@transgaming.com | 3127355 | 2010-08-04 13:42:44 +0000 | [diff] [blame] | 42 | mWidth = 0; |
| 43 | mHeight = 0; |
| 44 | |
daniel@transgaming.com | 00c7596 | 2010-03-11 20:36:15 +0000 | [diff] [blame] | 45 | mDirtyMetaData = true; |
daniel@transgaming.com | 5a0b0a8 | 2010-05-12 03:45:07 +0000 | [diff] [blame] | 46 | mDirty = true; |
daniel@transgaming.com | 93a8147 | 2010-04-20 18:52:58 +0000 | [diff] [blame] | 47 | mIsRenderable = false; |
daniel@transgaming.com | 0a311a4 | 2010-05-17 09:58:33 +0000 | [diff] [blame] | 48 | mBaseTexture = NULL; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | Texture::~Texture() |
| 52 | { |
| 53 | } |
| 54 | |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 55 | Blit *Texture::getBlitter() |
| 56 | { |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 57 | Context *context = getContext(); |
| 58 | return context->getBlitter(); |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 59 | } |
| 60 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 61 | // Returns true on successful filter state update (valid enum parameter) |
| 62 | bool Texture::setMinFilter(GLenum filter) |
| 63 | { |
| 64 | switch (filter) |
| 65 | { |
| 66 | case GL_NEAREST: |
| 67 | case GL_LINEAR: |
| 68 | case GL_NEAREST_MIPMAP_NEAREST: |
| 69 | case GL_LINEAR_MIPMAP_NEAREST: |
| 70 | case GL_NEAREST_MIPMAP_LINEAR: |
| 71 | case GL_LINEAR_MIPMAP_LINEAR: |
daniel@transgaming.com | 5a0b0a8 | 2010-05-12 03:45:07 +0000 | [diff] [blame] | 72 | { |
| 73 | if (mMinFilter != filter) |
| 74 | { |
| 75 | mMinFilter = filter; |
| 76 | mDirty = true; |
| 77 | } |
| 78 | return true; |
| 79 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 80 | default: |
| 81 | return false; |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 82 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | // Returns true on successful filter state update (valid enum parameter) |
| 86 | bool Texture::setMagFilter(GLenum filter) |
| 87 | { |
| 88 | switch (filter) |
| 89 | { |
| 90 | case GL_NEAREST: |
| 91 | case GL_LINEAR: |
daniel@transgaming.com | 5a0b0a8 | 2010-05-12 03:45:07 +0000 | [diff] [blame] | 92 | { |
| 93 | if (mMagFilter != filter) |
| 94 | { |
| 95 | mMagFilter = filter; |
| 96 | mDirty = true; |
| 97 | } |
| 98 | return true; |
| 99 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 100 | default: |
| 101 | return false; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // Returns true on successful wrap state update (valid enum parameter) |
| 106 | bool Texture::setWrapS(GLenum wrap) |
| 107 | { |
| 108 | switch (wrap) |
| 109 | { |
| 110 | case GL_REPEAT: |
| 111 | case GL_CLAMP_TO_EDGE: |
| 112 | case GL_MIRRORED_REPEAT: |
daniel@transgaming.com | 5a0b0a8 | 2010-05-12 03:45:07 +0000 | [diff] [blame] | 113 | { |
| 114 | if (mWrapS != wrap) |
| 115 | { |
| 116 | mWrapS = wrap; |
| 117 | mDirty = true; |
| 118 | } |
| 119 | return true; |
| 120 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 121 | default: |
| 122 | return false; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // Returns true on successful wrap state update (valid enum parameter) |
| 127 | bool Texture::setWrapT(GLenum wrap) |
| 128 | { |
| 129 | switch (wrap) |
| 130 | { |
| 131 | case GL_REPEAT: |
| 132 | case GL_CLAMP_TO_EDGE: |
| 133 | case GL_MIRRORED_REPEAT: |
daniel@transgaming.com | 5a0b0a8 | 2010-05-12 03:45:07 +0000 | [diff] [blame] | 134 | { |
| 135 | if (mWrapT != wrap) |
| 136 | { |
| 137 | mWrapT = wrap; |
| 138 | mDirty = true; |
| 139 | } |
| 140 | return true; |
| 141 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 142 | default: |
| 143 | return false; |
| 144 | } |
| 145 | } |
| 146 | |
daniel@transgaming.com | 00c7596 | 2010-03-11 20:36:15 +0000 | [diff] [blame] | 147 | GLenum Texture::getMinFilter() const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 148 | { |
| 149 | return mMinFilter; |
| 150 | } |
| 151 | |
daniel@transgaming.com | 00c7596 | 2010-03-11 20:36:15 +0000 | [diff] [blame] | 152 | GLenum Texture::getMagFilter() const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 153 | { |
| 154 | return mMagFilter; |
| 155 | } |
| 156 | |
daniel@transgaming.com | 00c7596 | 2010-03-11 20:36:15 +0000 | [diff] [blame] | 157 | GLenum Texture::getWrapS() const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 158 | { |
| 159 | return mWrapS; |
| 160 | } |
| 161 | |
daniel@transgaming.com | 00c7596 | 2010-03-11 20:36:15 +0000 | [diff] [blame] | 162 | GLenum Texture::getWrapT() const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 163 | { |
| 164 | return mWrapT; |
| 165 | } |
| 166 | |
daniel@transgaming.com | 93a8147 | 2010-04-20 18:52:58 +0000 | [diff] [blame] | 167 | GLuint Texture::getWidth() const |
| 168 | { |
| 169 | return mWidth; |
| 170 | } |
| 171 | |
| 172 | GLuint Texture::getHeight() const |
| 173 | { |
| 174 | return mHeight; |
| 175 | } |
| 176 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 177 | // Selects an internal Direct3D 9 format for storing an Image |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 178 | D3DFORMAT Texture::selectFormat(GLenum format) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 179 | { |
daniel@transgaming.com | 0186813 | 2010-08-24 19:21:17 +0000 | [diff] [blame^] | 180 | if (format == GL_COMPRESSED_RGB_S3TC_DXT1_EXT || |
| 181 | format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) |
| 182 | { |
| 183 | return D3DFMT_DXT1; |
| 184 | } |
| 185 | else |
| 186 | { |
| 187 | return D3DFMT_A8R8G8B8; |
| 188 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 189 | } |
| 190 | |
daniel@transgaming.com | 00c7596 | 2010-03-11 20:36:15 +0000 | [diff] [blame] | 191 | int Texture::imagePitch(const Image &img) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 192 | { |
daniel@transgaming.com | 00c7596 | 2010-03-11 20:36:15 +0000 | [diff] [blame] | 193 | return img.width * 4; |
| 194 | } |
daniel@transgaming.com | 29d2700 | 2010-03-11 19:41:22 +0000 | [diff] [blame] | 195 | |
daniel@transgaming.com | 00c7596 | 2010-03-11 20:36:15 +0000 | [diff] [blame] | 196 | // Store the pixel rectangle designated by xoffset,yoffset,width,height with pixels stored as format/type at input |
| 197 | // into the BGRA8 pixel rectangle at output with outputPitch bytes in between each line. |
| 198 | void Texture::loadImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, |
daniel@transgaming.com | 3489e3a | 2010-03-21 04:31:11 +0000 | [diff] [blame] | 199 | GLint unpackAlignment, const void *input, size_t outputPitch, void *output) const |
daniel@transgaming.com | 00c7596 | 2010-03-11 20:36:15 +0000 | [diff] [blame] | 200 | { |
daniel@transgaming.com | 713914b | 2010-05-04 03:35:17 +0000 | [diff] [blame] | 201 | GLsizei inputPitch = ComputePitch(width, format, type, unpackAlignment); |
daniel@transgaming.com | 00c7596 | 2010-03-11 20:36:15 +0000 | [diff] [blame] | 202 | |
| 203 | for (int y = 0; y < height; y++) |
| 204 | { |
| 205 | const unsigned char *source = static_cast<const unsigned char*>(input) + y * inputPitch; |
| 206 | const unsigned short *source16 = reinterpret_cast<const unsigned short*>(source); |
| 207 | unsigned char *dest = static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 4; |
| 208 | |
daniel@transgaming.com | a9198d9 | 2010-08-08 04:49:56 +0000 | [diff] [blame] | 209 | // fast path for EXT_texture_format_BGRA8888 |
| 210 | if (format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE) { |
| 211 | memcpy(dest, source, width*4); |
| 212 | continue; |
| 213 | } |
| 214 | |
daniel@transgaming.com | 00c7596 | 2010-03-11 20:36:15 +0000 | [diff] [blame] | 215 | for (int x = 0; x < width; x++) |
| 216 | { |
| 217 | unsigned char r; |
| 218 | unsigned char g; |
| 219 | unsigned char b; |
| 220 | unsigned char a; |
| 221 | |
| 222 | switch (format) |
| 223 | { |
| 224 | case GL_ALPHA: |
| 225 | a = source[x]; |
| 226 | r = 0; |
| 227 | g = 0; |
| 228 | b = 0; |
| 229 | break; |
| 230 | |
| 231 | case GL_LUMINANCE: |
| 232 | r = source[x]; |
| 233 | g = source[x]; |
| 234 | b = source[x]; |
| 235 | a = 0xFF; |
| 236 | break; |
| 237 | |
| 238 | case GL_LUMINANCE_ALPHA: |
| 239 | r = source[2*x+0]; |
| 240 | g = source[2*x+0]; |
| 241 | b = source[2*x+0]; |
| 242 | a = source[2*x+1]; |
| 243 | break; |
| 244 | |
| 245 | case GL_RGB: |
| 246 | switch (type) |
| 247 | { |
| 248 | case GL_UNSIGNED_BYTE: |
| 249 | r = source[x * 3 + 0]; |
daniel@transgaming.com | 5ac5215 | 2010-04-13 19:53:38 +0000 | [diff] [blame] | 250 | g = source[x * 3 + 1]; |
| 251 | b = source[x * 3 + 2]; |
daniel@transgaming.com | 00c7596 | 2010-03-11 20:36:15 +0000 | [diff] [blame] | 252 | a = 0xFF; |
| 253 | break; |
| 254 | |
| 255 | case GL_UNSIGNED_SHORT_5_6_5: |
| 256 | { |
| 257 | unsigned short rgba = source16[x]; |
| 258 | |
| 259 | a = 0xFF; |
| 260 | b = ((rgba & 0x001F) << 3) | ((rgba & 0x001F) >> 2); |
| 261 | g = ((rgba & 0x07E0) >> 3) | ((rgba & 0x07E0) >> 9); |
| 262 | r = ((rgba & 0xF800) >> 8) | ((rgba & 0xF800) >> 13); |
| 263 | } |
| 264 | break; |
| 265 | |
| 266 | default: UNREACHABLE(); |
| 267 | } |
| 268 | break; |
| 269 | |
| 270 | case GL_RGBA: |
| 271 | switch (type) |
| 272 | { |
| 273 | case GL_UNSIGNED_BYTE: |
| 274 | r = source[x * 4 + 0]; |
| 275 | g = source[x * 4 + 1]; |
| 276 | b = source[x * 4 + 2]; |
| 277 | a = source[x * 4 + 3]; |
| 278 | break; |
| 279 | |
| 280 | case GL_UNSIGNED_SHORT_4_4_4_4: |
| 281 | { |
| 282 | unsigned short rgba = source16[x]; |
| 283 | |
| 284 | a = ((rgba & 0x000F) << 4) | ((rgba & 0x000F) >> 0); |
| 285 | b = ((rgba & 0x00F0) << 0) | ((rgba & 0x00F0) >> 4); |
| 286 | g = ((rgba & 0x0F00) >> 4) | ((rgba & 0x0F00) >> 8); |
| 287 | r = ((rgba & 0xF000) >> 8) | ((rgba & 0xF000) >> 12); |
| 288 | } |
| 289 | break; |
| 290 | |
| 291 | case GL_UNSIGNED_SHORT_5_5_5_1: |
| 292 | { |
| 293 | unsigned short rgba = source16[x]; |
| 294 | |
| 295 | a = (rgba & 0x0001) ? 0xFF : 0; |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 296 | b = ((rgba & 0x003E) << 2) | ((rgba & 0x003E) >> 3); |
daniel@transgaming.com | 00c7596 | 2010-03-11 20:36:15 +0000 | [diff] [blame] | 297 | g = ((rgba & 0x07C0) >> 3) | ((rgba & 0x07C0) >> 8); |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 298 | r = ((rgba & 0xF800) >> 8) | ((rgba & 0xF800) >> 13); |
daniel@transgaming.com | 00c7596 | 2010-03-11 20:36:15 +0000 | [diff] [blame] | 299 | } |
| 300 | break; |
| 301 | |
| 302 | default: UNREACHABLE(); |
| 303 | } |
| 304 | break; |
| 305 | default: UNREACHABLE(); |
| 306 | } |
| 307 | |
| 308 | dest[4 * x + 0] = b; |
| 309 | dest[4 * x + 1] = g; |
| 310 | dest[4 * x + 2] = r; |
| 311 | dest[4 * x + 3] = a; |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | |
daniel@transgaming.com | 0186813 | 2010-08-24 19:21:17 +0000 | [diff] [blame^] | 316 | void Texture::createSurface(GLsizei width, GLsizei height, GLenum format, Image *img) |
daniel@transgaming.com | 00c7596 | 2010-03-11 20:36:15 +0000 | [diff] [blame] | 317 | { |
daniel@transgaming.com | f5cde48 | 2010-08-24 19:21:11 +0000 | [diff] [blame] | 318 | IDirect3DTexture9 *newTexture = NULL; |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 319 | IDirect3DSurface9 *newSurface = NULL; |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 320 | |
daniel@transgaming.com | 34dc3e8 | 2010-04-15 20:45:02 +0000 | [diff] [blame] | 321 | if (width != 0 && height != 0) |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 322 | { |
daniel@transgaming.com | f5cde48 | 2010-08-24 19:21:11 +0000 | [diff] [blame] | 323 | HRESULT result = getDevice()->CreateTexture(width, height, 1, NULL, selectFormat(format), D3DPOOL_SYSTEMMEM, &newTexture, NULL); |
daniel@transgaming.com | 34dc3e8 | 2010-04-15 20:45:02 +0000 | [diff] [blame] | 324 | |
| 325 | if (FAILED(result)) |
| 326 | { |
| 327 | ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); |
| 328 | return error(GL_OUT_OF_MEMORY); |
| 329 | } |
daniel@transgaming.com | f5cde48 | 2010-08-24 19:21:11 +0000 | [diff] [blame] | 330 | |
| 331 | newTexture->GetSurfaceLevel(0, &newSurface); |
| 332 | newTexture->Release(); |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | if (img->surface) img->surface->Release(); |
| 336 | img->surface = newSurface; |
| 337 | |
daniel@transgaming.com | 29d2700 | 2010-03-11 19:41:22 +0000 | [diff] [blame] | 338 | img->width = width; |
| 339 | img->height = height; |
| 340 | img->format = format; |
daniel@transgaming.com | 0186813 | 2010-08-24 19:21:17 +0000 | [diff] [blame^] | 341 | } |
daniel@transgaming.com | 29d2700 | 2010-03-11 19:41:22 +0000 | [diff] [blame] | 342 | |
daniel@transgaming.com | 0186813 | 2010-08-24 19:21:17 +0000 | [diff] [blame^] | 343 | void Texture::setImage(GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels, Image *img) |
| 344 | { |
| 345 | createSurface(width, height, format, img); |
| 346 | |
| 347 | if (pixels != NULL && img->surface != NULL) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 348 | { |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 349 | D3DLOCKED_RECT locked; |
daniel@transgaming.com | 0186813 | 2010-08-24 19:21:17 +0000 | [diff] [blame^] | 350 | HRESULT result = img->surface->LockRect(&locked, NULL, 0); |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 351 | |
| 352 | ASSERT(SUCCEEDED(result)); |
| 353 | |
| 354 | if (SUCCEEDED(result)) |
| 355 | { |
daniel@transgaming.com | 3489e3a | 2010-03-21 04:31:11 +0000 | [diff] [blame] | 356 | loadImageData(0, 0, width, height, format, type, unpackAlignment, pixels, locked.Pitch, locked.pBits); |
daniel@transgaming.com | 0186813 | 2010-08-24 19:21:17 +0000 | [diff] [blame^] | 357 | img->surface->UnlockRect(); |
| 358 | } |
| 359 | |
| 360 | img->dirty = true; |
| 361 | } |
| 362 | |
| 363 | mDirtyMetaData = true; |
| 364 | } |
| 365 | |
| 366 | void Texture::setCompressedImage(GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels, Image *img) |
| 367 | { |
| 368 | createSurface(width, height, format, img); |
| 369 | |
| 370 | if (pixels != NULL && img->surface != NULL) |
| 371 | { |
| 372 | D3DLOCKED_RECT locked; |
| 373 | HRESULT result = img->surface->LockRect(&locked, NULL, 0); |
| 374 | |
| 375 | ASSERT(SUCCEEDED(result)); |
| 376 | |
| 377 | if (SUCCEEDED(result)) |
| 378 | { |
| 379 | memcpy(locked.pBits, pixels, imageSize); |
| 380 | img->surface->UnlockRect(); |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | img->dirty = true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 384 | } |
| 385 | |
daniel@transgaming.com | 00c7596 | 2010-03-11 20:36:15 +0000 | [diff] [blame] | 386 | mDirtyMetaData = true; |
| 387 | } |
| 388 | |
daniel@transgaming.com | 3127355 | 2010-08-04 13:42:44 +0000 | [diff] [blame] | 389 | bool Texture::subImage(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels, Image *img) |
daniel@transgaming.com | 00c7596 | 2010-03-11 20:36:15 +0000 | [diff] [blame] | 390 | { |
daniel@transgaming.com | 3127355 | 2010-08-04 13:42:44 +0000 | [diff] [blame] | 391 | if (width + xoffset > img->width || height + yoffset > img->height) |
| 392 | { |
| 393 | error(GL_INVALID_VALUE); |
| 394 | return false; |
| 395 | } |
daniel@transgaming.com | 00c7596 | 2010-03-11 20:36:15 +0000 | [diff] [blame] | 396 | |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 397 | D3DLOCKED_RECT locked; |
| 398 | HRESULT result = img->surface->LockRect(&locked, NULL, 0); |
daniel@transgaming.com | 00c7596 | 2010-03-11 20:36:15 +0000 | [diff] [blame] | 399 | |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 400 | ASSERT(SUCCEEDED(result)); |
| 401 | |
| 402 | if (SUCCEEDED(result)) |
| 403 | { |
daniel@transgaming.com | 3489e3a | 2010-03-21 04:31:11 +0000 | [diff] [blame] | 404 | loadImageData(xoffset, yoffset, width, height, format, type, unpackAlignment, pixels, locked.Pitch, locked.pBits); |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 405 | img->surface->UnlockRect(); |
| 406 | } |
| 407 | |
| 408 | img->dirty = true; |
daniel@transgaming.com | 3127355 | 2010-08-04 13:42:44 +0000 | [diff] [blame] | 409 | return true; |
daniel@transgaming.com | 29d2700 | 2010-03-11 19:41:22 +0000 | [diff] [blame] | 410 | } |
| 411 | |
daniel@transgaming.com | 0186813 | 2010-08-24 19:21:17 +0000 | [diff] [blame^] | 412 | bool Texture::subImageCompressed(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels, Image *img) |
| 413 | { |
| 414 | if (width + xoffset > img->width || height + yoffset > img->height) |
| 415 | { |
| 416 | error(GL_INVALID_VALUE); |
| 417 | return false; |
| 418 | } |
| 419 | |
| 420 | if (format != getFormat()) |
| 421 | { |
| 422 | error(GL_INVALID_OPERATION); |
| 423 | return false; |
| 424 | } |
| 425 | |
| 426 | RECT updateRegion; |
| 427 | updateRegion.left = xoffset; |
| 428 | updateRegion.right = xoffset + width; |
| 429 | updateRegion.bottom = yoffset + height; |
| 430 | updateRegion.top = yoffset; |
| 431 | |
| 432 | D3DLOCKED_RECT locked; |
| 433 | HRESULT result = img->surface->LockRect(&locked, &updateRegion, 0); |
| 434 | |
| 435 | ASSERT(SUCCEEDED(result)); |
| 436 | |
| 437 | if (SUCCEEDED(result)) |
| 438 | { |
| 439 | GLsizei inputPitch = ComputeCompressedPitch(width, format); |
| 440 | int rows = imageSize / inputPitch; |
| 441 | for (int i = 0; i < rows; ++i) |
| 442 | { |
| 443 | memcpy((void*)((BYTE*)locked.pBits + i * locked.Pitch), (void*)((BYTE*)pixels + i * inputPitch), inputPitch); |
| 444 | } |
| 445 | img->surface->UnlockRect(); |
| 446 | } |
| 447 | |
| 448 | img->dirty = true; |
| 449 | return true; |
| 450 | } |
| 451 | |
daniel@transgaming.com | 29d2700 | 2010-03-11 19:41:22 +0000 | [diff] [blame] | 452 | IDirect3DBaseTexture9 *Texture::getTexture() |
| 453 | { |
| 454 | if (!isComplete()) |
| 455 | { |
| 456 | return NULL; |
| 457 | } |
| 458 | |
daniel@transgaming.com | 00c7596 | 2010-03-11 20:36:15 +0000 | [diff] [blame] | 459 | if (mDirtyMetaData) |
| 460 | { |
daniel@transgaming.com | 00c7596 | 2010-03-11 20:36:15 +0000 | [diff] [blame] | 461 | mBaseTexture = createTexture(); |
daniel@transgaming.com | 93a8147 | 2010-04-20 18:52:58 +0000 | [diff] [blame] | 462 | mIsRenderable = false; |
daniel@transgaming.com | 00c7596 | 2010-03-11 20:36:15 +0000 | [diff] [blame] | 463 | } |
| 464 | |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 465 | if (mDirtyMetaData || dirtyImageData()) |
daniel@transgaming.com | 29d2700 | 2010-03-11 19:41:22 +0000 | [diff] [blame] | 466 | { |
daniel@transgaming.com | 00c7596 | 2010-03-11 20:36:15 +0000 | [diff] [blame] | 467 | updateTexture(); |
daniel@transgaming.com | 29d2700 | 2010-03-11 19:41:22 +0000 | [diff] [blame] | 468 | } |
| 469 | |
daniel@transgaming.com | 00c7596 | 2010-03-11 20:36:15 +0000 | [diff] [blame] | 470 | mDirtyMetaData = false; |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 471 | ASSERT(!dirtyImageData()); |
daniel@transgaming.com | 00c7596 | 2010-03-11 20:36:15 +0000 | [diff] [blame] | 472 | |
daniel@transgaming.com | 29d2700 | 2010-03-11 19:41:22 +0000 | [diff] [blame] | 473 | return mBaseTexture; |
| 474 | } |
| 475 | |
daniel@transgaming.com | 5a0b0a8 | 2010-05-12 03:45:07 +0000 | [diff] [blame] | 476 | bool Texture::isDirty() const |
| 477 | { |
| 478 | return (mDirty || mDirtyMetaData || dirtyImageData()); |
| 479 | } |
| 480 | |
daniel@transgaming.com | 7051b97 | 2010-03-21 04:31:07 +0000 | [diff] [blame] | 481 | // Returns the top-level texture surface as a render target |
daniel@transgaming.com | 93a8147 | 2010-04-20 18:52:58 +0000 | [diff] [blame] | 482 | void Texture::needRenderTarget() |
daniel@transgaming.com | 7051b97 | 2010-03-21 04:31:07 +0000 | [diff] [blame] | 483 | { |
daniel@transgaming.com | 93a8147 | 2010-04-20 18:52:58 +0000 | [diff] [blame] | 484 | if (!mIsRenderable) |
daniel@transgaming.com | 7051b97 | 2010-03-21 04:31:07 +0000 | [diff] [blame] | 485 | { |
| 486 | mBaseTexture = convertToRenderTarget(); |
daniel@transgaming.com | 93a8147 | 2010-04-20 18:52:58 +0000 | [diff] [blame] | 487 | mIsRenderable = true; |
daniel@transgaming.com | 7051b97 | 2010-03-21 04:31:07 +0000 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | if (dirtyImageData()) |
| 491 | { |
| 492 | updateTexture(); |
| 493 | } |
| 494 | |
| 495 | mDirtyMetaData = false; |
daniel@transgaming.com | 7051b97 | 2010-03-21 04:31:07 +0000 | [diff] [blame] | 496 | } |
| 497 | |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 498 | void Texture::dropTexture() |
| 499 | { |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 500 | if (mBaseTexture) |
| 501 | { |
| 502 | mBaseTexture = NULL; |
| 503 | } |
daniel@transgaming.com | 93a8147 | 2010-04-20 18:52:58 +0000 | [diff] [blame] | 504 | |
| 505 | mIsRenderable = false; |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 506 | } |
| 507 | |
daniel@transgaming.com | 93a8147 | 2010-04-20 18:52:58 +0000 | [diff] [blame] | 508 | void Texture::pushTexture(IDirect3DBaseTexture9 *newTexture, bool renderable) |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 509 | { |
| 510 | mBaseTexture = newTexture; |
| 511 | mDirtyMetaData = false; |
daniel@transgaming.com | 93a8147 | 2010-04-20 18:52:58 +0000 | [diff] [blame] | 512 | mIsRenderable = renderable; |
daniel@transgaming.com | 5a0b0a8 | 2010-05-12 03:45:07 +0000 | [diff] [blame] | 513 | mDirty = true; |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | |
daniel@transgaming.com | c808c5a | 2010-05-14 17:31:01 +0000 | [diff] [blame] | 517 | GLint Texture::creationLevels(GLsizei width, GLsizei height, GLint maxlevel) const |
| 518 | { |
| 519 | if (isPow2(width) && isPow2(height)) |
| 520 | { |
| 521 | return maxlevel; |
| 522 | } |
| 523 | else |
| 524 | { |
daniel@transgaming.com | 4c03fa6 | 2010-06-24 13:02:16 +0000 | [diff] [blame] | 525 | // OpenGL ES 2.0 without GL_OES_texture_npot does not permit NPOT mipmaps. |
| 526 | return 1; |
daniel@transgaming.com | c808c5a | 2010-05-14 17:31:01 +0000 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | |
| 530 | GLint Texture::creationLevels(GLsizei size, GLint maxlevel) const |
| 531 | { |
| 532 | return creationLevels(size, size, maxlevel); |
| 533 | } |
| 534 | |
| 535 | int Texture::levelCount() const |
| 536 | { |
| 537 | return mBaseTexture ? mBaseTexture->GetLevelCount() : 0; |
| 538 | } |
| 539 | |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 540 | Texture2D::Texture2D(GLuint id) : Texture(id) |
daniel@transgaming.com | 29d2700 | 2010-03-11 19:41:22 +0000 | [diff] [blame] | 541 | { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 542 | mTexture = NULL; |
daniel@transgaming.com | 93a8147 | 2010-04-20 18:52:58 +0000 | [diff] [blame] | 543 | mColorbufferProxy = NULL; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | Texture2D::~Texture2D() |
| 547 | { |
daniel@transgaming.com | 93a8147 | 2010-04-20 18:52:58 +0000 | [diff] [blame] | 548 | delete mColorbufferProxy; |
| 549 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 550 | if (mTexture) |
| 551 | { |
| 552 | mTexture->Release(); |
| 553 | mTexture = NULL; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | GLenum Texture2D::getTarget() const |
| 558 | { |
| 559 | return GL_TEXTURE_2D; |
| 560 | } |
| 561 | |
daniel@transgaming.com | 0186813 | 2010-08-24 19:21:17 +0000 | [diff] [blame^] | 562 | GLenum Texture2D::getFormat() const |
| 563 | { |
| 564 | return mImageArray[0].format; |
| 565 | } |
| 566 | |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 567 | // While OpenGL doesn't check texture consistency until draw-time, D3D9 requires a complete texture |
| 568 | // for render-to-texture (such as CopyTexImage). We have no way of keeping individual inconsistent levels. |
| 569 | // Call this when a particular level of the texture must be defined with a specific format, width and height. |
| 570 | // |
| 571 | // Returns true if the existing texture was unsuitable had to be destroyed. If so, it will also set |
| 572 | // a new height and width for the texture by working backwards from the given width and height. |
| 573 | bool Texture2D::redefineTexture(GLint level, GLenum internalFormat, GLsizei width, GLsizei height) |
| 574 | { |
| 575 | bool widthOkay = (mWidth >> level == width); |
| 576 | bool heightOkay = (mHeight >> level == height); |
| 577 | |
| 578 | bool sizeOkay = ((widthOkay && heightOkay) |
| 579 | || (widthOkay && mHeight >> level == 0 && height == 1) |
| 580 | || (heightOkay && mWidth >> level == 0 && width == 1)); |
| 581 | |
| 582 | bool textureOkay = (sizeOkay && internalFormat == mImageArray[0].format); |
| 583 | |
| 584 | if (!textureOkay) |
| 585 | { |
| 586 | TRACE("Redefining 2D texture (%d, 0x%04X, %d, %d => 0x%04X, %d, %d).", level, |
| 587 | mImageArray[0].format, mWidth, mHeight, |
| 588 | internalFormat, width, height); |
| 589 | |
| 590 | // Purge all the levels and the texture. |
| 591 | |
| 592 | for (int i = 0; i < MAX_TEXTURE_LEVELS; i++) |
| 593 | { |
| 594 | if (mImageArray[i].surface != NULL) |
| 595 | { |
| 596 | mImageArray[i].dirty = false; |
| 597 | |
| 598 | mImageArray[i].surface->Release(); |
| 599 | mImageArray[i].surface = NULL; |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | if (mTexture != NULL) |
| 604 | { |
| 605 | mTexture->Release(); |
| 606 | mTexture = NULL; |
| 607 | dropTexture(); |
| 608 | } |
| 609 | |
| 610 | mWidth = width << level; |
| 611 | mHeight = height << level; |
| 612 | mImageArray[0].format = internalFormat; |
| 613 | } |
| 614 | |
| 615 | return !textureOkay; |
| 616 | } |
| 617 | |
daniel@transgaming.com | 3489e3a | 2010-03-21 04:31:11 +0000 | [diff] [blame] | 618 | void Texture2D::setImage(GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 619 | { |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 620 | redefineTexture(level, internalFormat, width, height); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 621 | |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 622 | Texture::setImage(width, height, format, type, unpackAlignment, pixels, &mImageArray[level]); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 623 | } |
| 624 | |
daniel@transgaming.com | 0186813 | 2010-08-24 19:21:17 +0000 | [diff] [blame^] | 625 | void Texture2D::setCompressedImage(GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels) |
| 626 | { |
| 627 | redefineTexture(level, internalFormat, width, height); |
| 628 | |
| 629 | Texture::setCompressedImage(width, height, internalFormat, imageSize, pixels, &mImageArray[level]); |
| 630 | } |
| 631 | |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 632 | void Texture2D::commitRect(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height) |
| 633 | { |
| 634 | ASSERT(mImageArray[level].surface != NULL); |
| 635 | |
daniel@transgaming.com | c808c5a | 2010-05-14 17:31:01 +0000 | [diff] [blame] | 636 | if (level < levelCount()) |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 637 | { |
| 638 | IDirect3DSurface9 *destLevel = NULL; |
daniel@transgaming.com | 7051b97 | 2010-03-21 04:31:07 +0000 | [diff] [blame] | 639 | HRESULT result = mTexture->GetSurfaceLevel(level, &destLevel); |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 640 | |
daniel@transgaming.com | 7051b97 | 2010-03-21 04:31:07 +0000 | [diff] [blame] | 641 | ASSERT(SUCCEEDED(result)); |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 642 | |
daniel@transgaming.com | 7051b97 | 2010-03-21 04:31:07 +0000 | [diff] [blame] | 643 | if (SUCCEEDED(result)) |
| 644 | { |
| 645 | Image *img = &mImageArray[level]; |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 646 | |
daniel@transgaming.com | 7051b97 | 2010-03-21 04:31:07 +0000 | [diff] [blame] | 647 | RECT sourceRect; |
| 648 | sourceRect.left = xoffset; |
| 649 | sourceRect.top = yoffset; |
| 650 | sourceRect.right = xoffset + width; |
| 651 | sourceRect.bottom = yoffset + height; |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 652 | |
daniel@transgaming.com | 7051b97 | 2010-03-21 04:31:07 +0000 | [diff] [blame] | 653 | POINT destPoint; |
| 654 | destPoint.x = xoffset; |
| 655 | destPoint.y = yoffset; |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 656 | |
daniel@transgaming.com | 7051b97 | 2010-03-21 04:31:07 +0000 | [diff] [blame] | 657 | result = getDevice()->UpdateSurface(img->surface, &sourceRect, destLevel, &destPoint); |
| 658 | ASSERT(SUCCEEDED(result)); |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 659 | |
daniel@transgaming.com | 7051b97 | 2010-03-21 04:31:07 +0000 | [diff] [blame] | 660 | destLevel->Release(); |
| 661 | |
| 662 | img->dirty = false; |
| 663 | } |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | |
daniel@transgaming.com | 3489e3a | 2010-03-21 04:31:11 +0000 | [diff] [blame] | 667 | void Texture2D::subImage(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 668 | { |
daniel@transgaming.com | 3127355 | 2010-08-04 13:42:44 +0000 | [diff] [blame] | 669 | if (Texture::subImage(xoffset, yoffset, width, height, format, type, unpackAlignment, pixels, &mImageArray[level])) |
| 670 | { |
| 671 | commitRect(level, xoffset, yoffset, width, height); |
| 672 | } |
daniel@transgaming.com | 00c7596 | 2010-03-11 20:36:15 +0000 | [diff] [blame] | 673 | } |
| 674 | |
daniel@transgaming.com | 0186813 | 2010-08-24 19:21:17 +0000 | [diff] [blame^] | 675 | void Texture2D::subImageCompressed(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels) |
| 676 | { |
| 677 | if (Texture::subImageCompressed(xoffset, yoffset, width, height, format, imageSize, pixels, &mImageArray[level])) |
| 678 | { |
| 679 | commitRect(level, xoffset, yoffset, width, height); |
| 680 | } |
| 681 | } |
| 682 | |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 683 | void Texture2D::copyImage(GLint level, GLenum internalFormat, GLint x, GLint y, GLsizei width, GLsizei height, RenderbufferStorage *source) |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 684 | { |
| 685 | if (redefineTexture(level, internalFormat, width, height)) |
| 686 | { |
| 687 | convertToRenderTarget(); |
daniel@transgaming.com | 93a8147 | 2010-04-20 18:52:58 +0000 | [diff] [blame] | 688 | pushTexture(mTexture, true); |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 689 | } |
daniel@transgaming.com | bc3699d | 2010-08-05 14:48:49 +0000 | [diff] [blame] | 690 | else |
| 691 | { |
| 692 | needRenderTarget(); |
| 693 | } |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 694 | |
daniel@transgaming.com | c808c5a | 2010-05-14 17:31:01 +0000 | [diff] [blame] | 695 | if (width != 0 && height != 0 && level < levelCount()) |
daniel@transgaming.com | 34dc3e8 | 2010-04-15 20:45:02 +0000 | [diff] [blame] | 696 | { |
| 697 | RECT sourceRect; |
| 698 | sourceRect.left = x; |
daniel@transgaming.com | 34dc3e8 | 2010-04-15 20:45:02 +0000 | [diff] [blame] | 699 | sourceRect.right = x + width; |
daniel@transgaming.com | 18b426b | 2010-04-20 18:52:44 +0000 | [diff] [blame] | 700 | sourceRect.top = y; |
| 701 | sourceRect.bottom = y + height; |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 702 | |
daniel@transgaming.com | 34dc3e8 | 2010-04-15 20:45:02 +0000 | [diff] [blame] | 703 | IDirect3DSurface9 *dest; |
| 704 | HRESULT hr = mTexture->GetSurfaceLevel(level, &dest); |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 705 | |
daniel@transgaming.com | 34dc3e8 | 2010-04-15 20:45:02 +0000 | [diff] [blame] | 706 | getBlitter()->formatConvert(source->getRenderTarget(), sourceRect, internalFormat, 0, 0, dest); |
| 707 | dest->Release(); |
| 708 | } |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 709 | |
| 710 | mImageArray[level].width = width; |
| 711 | mImageArray[level].height = height; |
| 712 | mImageArray[level].format = internalFormat; |
| 713 | } |
| 714 | |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 715 | void Texture2D::copySubImage(GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, RenderbufferStorage *source) |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 716 | { |
daniel@transgaming.com | 34dc3e8 | 2010-04-15 20:45:02 +0000 | [diff] [blame] | 717 | if (xoffset + width > mImageArray[level].width || yoffset + height > mImageArray[level].height) |
| 718 | { |
| 719 | return error(GL_INVALID_VALUE); |
| 720 | } |
| 721 | |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 722 | if (redefineTexture(0, mImageArray[0].format, mImageArray[0].width, mImageArray[0].height)) |
| 723 | { |
| 724 | convertToRenderTarget(); |
daniel@transgaming.com | 93a8147 | 2010-04-20 18:52:58 +0000 | [diff] [blame] | 725 | pushTexture(mTexture, true); |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 726 | } |
| 727 | else |
| 728 | { |
daniel@transgaming.com | fc23fe2 | 2010-05-05 18:48:17 +0000 | [diff] [blame] | 729 | needRenderTarget(); |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 730 | } |
| 731 | |
daniel@transgaming.com | c808c5a | 2010-05-14 17:31:01 +0000 | [diff] [blame] | 732 | if (level < levelCount()) |
| 733 | { |
| 734 | RECT sourceRect; |
| 735 | sourceRect.left = x; |
| 736 | sourceRect.right = x + width; |
| 737 | sourceRect.top = y; |
| 738 | sourceRect.bottom = y + height; |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 739 | |
daniel@transgaming.com | c808c5a | 2010-05-14 17:31:01 +0000 | [diff] [blame] | 740 | IDirect3DSurface9 *dest; |
| 741 | HRESULT hr = mTexture->GetSurfaceLevel(level, &dest); |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 742 | |
daniel@transgaming.com | c808c5a | 2010-05-14 17:31:01 +0000 | [diff] [blame] | 743 | getBlitter()->formatConvert(source->getRenderTarget(), sourceRect, mImageArray[0].format, xoffset, yoffset, dest); |
| 744 | dest->Release(); |
| 745 | } |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 746 | } |
| 747 | |
daniel@transgaming.com | 00c7596 | 2010-03-11 20:36:15 +0000 | [diff] [blame] | 748 | // Tests for GL texture object completeness. [OpenGL ES 2.0.24] section 3.7.10 page 81. |
| 749 | bool Texture2D::isComplete() const |
| 750 | { |
daniel@transgaming.com | 93a8147 | 2010-04-20 18:52:58 +0000 | [diff] [blame] | 751 | GLsizei width = mImageArray[0].width; |
| 752 | GLsizei height = mImageArray[0].height; |
daniel@transgaming.com | 00c7596 | 2010-03-11 20:36:15 +0000 | [diff] [blame] | 753 | |
daniel@transgaming.com | 93a8147 | 2010-04-20 18:52:58 +0000 | [diff] [blame] | 754 | if (width <= 0 || height <= 0) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 755 | { |
| 756 | return false; |
| 757 | } |
| 758 | |
daniel@transgaming.com | 4c03fa6 | 2010-06-24 13:02:16 +0000 | [diff] [blame] | 759 | bool mipmapping = false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 760 | |
daniel@transgaming.com | 12d5407 | 2010-03-16 06:23:26 +0000 | [diff] [blame] | 761 | switch (mMinFilter) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 762 | { |
| 763 | case GL_NEAREST: |
| 764 | case GL_LINEAR: |
| 765 | mipmapping = false; |
| 766 | break; |
| 767 | case GL_NEAREST_MIPMAP_NEAREST: |
| 768 | case GL_LINEAR_MIPMAP_NEAREST: |
| 769 | case GL_NEAREST_MIPMAP_LINEAR: |
| 770 | case GL_LINEAR_MIPMAP_LINEAR: |
| 771 | mipmapping = true; |
| 772 | break; |
| 773 | default: UNREACHABLE(); |
| 774 | } |
| 775 | |
daniel@transgaming.com | 4c03fa6 | 2010-06-24 13:02:16 +0000 | [diff] [blame] | 776 | if ((getWrapS() != GL_CLAMP_TO_EDGE && !isPow2(width)) |
| 777 | || (getWrapT() != GL_CLAMP_TO_EDGE && !isPow2(height))) |
| 778 | { |
| 779 | return false; |
| 780 | } |
| 781 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 782 | if (mipmapping) |
| 783 | { |
daniel@transgaming.com | 4c03fa6 | 2010-06-24 13:02:16 +0000 | [diff] [blame] | 784 | if (!isPow2(width) || !isPow2(height)) |
daniel@transgaming.com | d99bd45 | 2010-04-22 13:35:25 +0000 | [diff] [blame] | 785 | { |
| 786 | return false; |
| 787 | } |
| 788 | |
daniel@transgaming.com | 93a8147 | 2010-04-20 18:52:58 +0000 | [diff] [blame] | 789 | int q = log2(std::max(width, height)); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 790 | |
| 791 | for (int level = 1; level <= q; level++) |
| 792 | { |
| 793 | if (mImageArray[level].format != mImageArray[0].format) |
| 794 | { |
| 795 | return false; |
| 796 | } |
| 797 | |
daniel@transgaming.com | d99bd45 | 2010-04-22 13:35:25 +0000 | [diff] [blame] | 798 | if (mImageArray[level].width != std::max(1, width >> level)) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 799 | { |
| 800 | return false; |
| 801 | } |
| 802 | |
daniel@transgaming.com | d99bd45 | 2010-04-22 13:35:25 +0000 | [diff] [blame] | 803 | if (mImageArray[level].height != std::max(1, height >> level)) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 804 | { |
| 805 | return false; |
| 806 | } |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | return true; |
| 811 | } |
| 812 | |
daniel@transgaming.com | 0186813 | 2010-08-24 19:21:17 +0000 | [diff] [blame^] | 813 | bool Texture2D::isCompressed() const |
| 814 | { |
| 815 | return IsCompressed(getFormat()); |
| 816 | } |
| 817 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 818 | // Constructs a Direct3D 9 texture resource from the texture images, or returns an existing one |
daniel@transgaming.com | 29d2700 | 2010-03-11 19:41:22 +0000 | [diff] [blame] | 819 | IDirect3DBaseTexture9 *Texture2D::createTexture() |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 820 | { |
daniel@transgaming.com | 29d2700 | 2010-03-11 19:41:22 +0000 | [diff] [blame] | 821 | IDirect3DTexture9 *texture; |
| 822 | |
| 823 | IDirect3DDevice9 *device = getDevice(); |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 824 | D3DFORMAT format = selectFormat(mImageArray[0].format); |
daniel@transgaming.com | 29d2700 | 2010-03-11 19:41:22 +0000 | [diff] [blame] | 825 | |
daniel@transgaming.com | c808c5a | 2010-05-14 17:31:01 +0000 | [diff] [blame] | 826 | HRESULT result = device->CreateTexture(mWidth, mHeight, creationLevels(mWidth, mHeight, 0), 0, format, D3DPOOL_DEFAULT, &texture, NULL); |
daniel@transgaming.com | 29d2700 | 2010-03-11 19:41:22 +0000 | [diff] [blame] | 827 | |
daniel@transgaming.com | 7051b97 | 2010-03-21 04:31:07 +0000 | [diff] [blame] | 828 | if (FAILED(result)) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 829 | { |
daniel@transgaming.com | 7051b97 | 2010-03-21 04:31:07 +0000 | [diff] [blame] | 830 | ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); |
daniel@transgaming.com | 29d2700 | 2010-03-11 19:41:22 +0000 | [diff] [blame] | 831 | return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 832 | } |
| 833 | |
daniel@transgaming.com | 00c7596 | 2010-03-11 20:36:15 +0000 | [diff] [blame] | 834 | if (mTexture) mTexture->Release(); |
| 835 | mTexture = texture; |
| 836 | return texture; |
| 837 | } |
| 838 | |
| 839 | void Texture2D::updateTexture() |
| 840 | { |
| 841 | IDirect3DDevice9 *device = getDevice(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 842 | |
daniel@transgaming.com | c808c5a | 2010-05-14 17:31:01 +0000 | [diff] [blame] | 843 | int levels = levelCount(); |
daniel@transgaming.com | 29d2700 | 2010-03-11 19:41:22 +0000 | [diff] [blame] | 844 | |
daniel@transgaming.com | c808c5a | 2010-05-14 17:31:01 +0000 | [diff] [blame] | 845 | for (int level = 0; level < levels; level++) |
daniel@transgaming.com | 29d2700 | 2010-03-11 19:41:22 +0000 | [diff] [blame] | 846 | { |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 847 | if (mImageArray[level].dirty) |
| 848 | { |
| 849 | IDirect3DSurface9 *levelSurface = NULL; |
daniel@transgaming.com | 7051b97 | 2010-03-21 04:31:07 +0000 | [diff] [blame] | 850 | HRESULT result = mTexture->GetSurfaceLevel(level, &levelSurface); |
daniel@transgaming.com | 29d2700 | 2010-03-11 19:41:22 +0000 | [diff] [blame] | 851 | |
daniel@transgaming.com | 7051b97 | 2010-03-21 04:31:07 +0000 | [diff] [blame] | 852 | ASSERT(SUCCEEDED(result)); |
daniel@transgaming.com | 29d2700 | 2010-03-11 19:41:22 +0000 | [diff] [blame] | 853 | |
daniel@transgaming.com | 7051b97 | 2010-03-21 04:31:07 +0000 | [diff] [blame] | 854 | if (SUCCEEDED(result)) |
| 855 | { |
| 856 | result = device->UpdateSurface(mImageArray[level].surface, NULL, levelSurface, NULL); |
| 857 | ASSERT(SUCCEEDED(result)); |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 858 | |
daniel@transgaming.com | 7051b97 | 2010-03-21 04:31:07 +0000 | [diff] [blame] | 859 | levelSurface->Release(); |
| 860 | |
| 861 | mImageArray[level].dirty = false; |
| 862 | } |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 863 | } |
daniel@transgaming.com | 29d2700 | 2010-03-11 19:41:22 +0000 | [diff] [blame] | 864 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 865 | } |
| 866 | |
daniel@transgaming.com | 7051b97 | 2010-03-21 04:31:07 +0000 | [diff] [blame] | 867 | IDirect3DBaseTexture9 *Texture2D::convertToRenderTarget() |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 868 | { |
daniel@transgaming.com | 34dc3e8 | 2010-04-15 20:45:02 +0000 | [diff] [blame] | 869 | IDirect3DTexture9 *texture = NULL; |
daniel@transgaming.com | 7051b97 | 2010-03-21 04:31:07 +0000 | [diff] [blame] | 870 | |
daniel@transgaming.com | 34dc3e8 | 2010-04-15 20:45:02 +0000 | [diff] [blame] | 871 | if (mWidth != 0 && mHeight != 0) |
daniel@transgaming.com | 29d2700 | 2010-03-11 19:41:22 +0000 | [diff] [blame] | 872 | { |
daniel@transgaming.com | ae072af | 2010-05-05 18:47:28 +0000 | [diff] [blame] | 873 | egl::Display *display = getDisplay(); |
daniel@transgaming.com | 34dc3e8 | 2010-04-15 20:45:02 +0000 | [diff] [blame] | 874 | IDirect3DDevice9 *device = getDevice(); |
| 875 | D3DFORMAT format = selectFormat(mImageArray[0].format); |
| 876 | |
daniel@transgaming.com | c808c5a | 2010-05-14 17:31:01 +0000 | [diff] [blame] | 877 | HRESULT result = device->CreateTexture(mWidth, mHeight, creationLevels(mWidth, mHeight, 0), D3DUSAGE_RENDERTARGET, format, D3DPOOL_DEFAULT, &texture, NULL); |
daniel@transgaming.com | 34dc3e8 | 2010-04-15 20:45:02 +0000 | [diff] [blame] | 878 | |
| 879 | if (FAILED(result)) |
| 880 | { |
| 881 | ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); |
| 882 | return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL); |
| 883 | } |
| 884 | |
| 885 | if (mTexture != NULL) |
| 886 | { |
daniel@transgaming.com | c808c5a | 2010-05-14 17:31:01 +0000 | [diff] [blame] | 887 | int levels = levelCount(); |
daniel@transgaming.com | 34dc3e8 | 2010-04-15 20:45:02 +0000 | [diff] [blame] | 888 | for (int i = 0; i < levels; i++) |
| 889 | { |
| 890 | IDirect3DSurface9 *source; |
| 891 | result = mTexture->GetSurfaceLevel(i, &source); |
| 892 | |
| 893 | if (FAILED(result)) |
| 894 | { |
| 895 | ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); |
| 896 | |
| 897 | texture->Release(); |
| 898 | |
| 899 | return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL); |
| 900 | } |
| 901 | |
| 902 | IDirect3DSurface9 *dest; |
| 903 | result = texture->GetSurfaceLevel(i, &dest); |
| 904 | |
| 905 | if (FAILED(result)) |
| 906 | { |
| 907 | ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); |
| 908 | |
| 909 | texture->Release(); |
| 910 | source->Release(); |
| 911 | |
| 912 | return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL); |
| 913 | } |
| 914 | |
daniel@transgaming.com | ae072af | 2010-05-05 18:47:28 +0000 | [diff] [blame] | 915 | display->endScene(); |
daniel@transgaming.com | 34dc3e8 | 2010-04-15 20:45:02 +0000 | [diff] [blame] | 916 | result = device->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE); |
| 917 | |
| 918 | if (FAILED(result)) |
| 919 | { |
| 920 | ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); |
| 921 | |
| 922 | texture->Release(); |
| 923 | source->Release(); |
| 924 | dest->Release(); |
| 925 | |
| 926 | return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL); |
| 927 | } |
| 928 | |
| 929 | source->Release(); |
| 930 | dest->Release(); |
| 931 | } |
| 932 | } |
daniel@transgaming.com | 29d2700 | 2010-03-11 19:41:22 +0000 | [diff] [blame] | 933 | } |
| 934 | |
daniel@transgaming.com | 7051b97 | 2010-03-21 04:31:07 +0000 | [diff] [blame] | 935 | if (mTexture != NULL) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 936 | { |
daniel@transgaming.com | 7051b97 | 2010-03-21 04:31:07 +0000 | [diff] [blame] | 937 | mTexture->Release(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 938 | } |
| 939 | |
daniel@transgaming.com | 7051b97 | 2010-03-21 04:31:07 +0000 | [diff] [blame] | 940 | mTexture = texture; |
| 941 | return mTexture; |
| 942 | } |
| 943 | |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 944 | bool Texture2D::dirtyImageData() const |
| 945 | { |
| 946 | int q = log2(std::max(mWidth, mHeight)); |
| 947 | |
| 948 | for (int i = 0; i <= q; i++) |
| 949 | { |
| 950 | if (mImageArray[i].dirty) return true; |
| 951 | } |
| 952 | |
| 953 | return false; |
| 954 | } |
| 955 | |
daniel@transgaming.com | 8fd99e2 | 2010-04-20 18:52:00 +0000 | [diff] [blame] | 956 | void Texture2D::generateMipmaps() |
| 957 | { |
| 958 | if (!isPow2(mImageArray[0].width) || !isPow2(mImageArray[0].height)) |
| 959 | { |
| 960 | return error(GL_INVALID_OPERATION); |
| 961 | } |
| 962 | |
| 963 | // Purge array levels 1 through q and reset them to represent the generated mipmap levels. |
| 964 | unsigned int q = log2(std::max(mWidth, mHeight)); |
| 965 | for (unsigned int i = 1; i <= q; i++) |
| 966 | { |
| 967 | if (mImageArray[i].surface != NULL) |
| 968 | { |
| 969 | mImageArray[i].surface->Release(); |
| 970 | mImageArray[i].surface = NULL; |
| 971 | } |
| 972 | |
| 973 | mImageArray[i].dirty = false; |
| 974 | |
| 975 | mImageArray[i].format = mImageArray[0].format; |
| 976 | mImageArray[i].width = std::max(mImageArray[0].width >> i, 1); |
| 977 | mImageArray[i].height = std::max(mImageArray[0].height >> i, 1); |
| 978 | } |
| 979 | |
daniel@transgaming.com | 93a8147 | 2010-04-20 18:52:58 +0000 | [diff] [blame] | 980 | needRenderTarget(); |
daniel@transgaming.com | 8fd99e2 | 2010-04-20 18:52:00 +0000 | [diff] [blame] | 981 | |
| 982 | for (unsigned int i = 1; i <= q; i++) |
| 983 | { |
| 984 | IDirect3DSurface9 *upper = NULL; |
| 985 | IDirect3DSurface9 *lower = NULL; |
| 986 | |
| 987 | mTexture->GetSurfaceLevel(i-1, &upper); |
| 988 | mTexture->GetSurfaceLevel(i, &lower); |
| 989 | |
| 990 | if (upper != NULL && lower != NULL) |
| 991 | { |
| 992 | getBlitter()->boxFilter(upper, lower); |
| 993 | } |
| 994 | |
| 995 | if (upper != NULL) upper->Release(); |
| 996 | if (lower != NULL) lower->Release(); |
| 997 | } |
| 998 | } |
| 999 | |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 1000 | Renderbuffer *Texture2D::getColorbuffer(GLenum target) |
daniel@transgaming.com | 93a8147 | 2010-04-20 18:52:58 +0000 | [diff] [blame] | 1001 | { |
| 1002 | if (target != GL_TEXTURE_2D) |
| 1003 | { |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 1004 | return error(GL_INVALID_OPERATION, (Renderbuffer *)NULL); |
daniel@transgaming.com | 93a8147 | 2010-04-20 18:52:58 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | if (mColorbufferProxy == NULL) |
| 1008 | { |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 1009 | mColorbufferProxy = new Renderbuffer(id(), new TextureColorbufferProxy(this, target)); |
| 1010 | mColorbufferProxy->addRef(); |
daniel@transgaming.com | 93a8147 | 2010-04-20 18:52:58 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | return mColorbufferProxy; |
| 1014 | } |
| 1015 | |
| 1016 | IDirect3DSurface9 *Texture2D::getRenderTarget(GLenum target) |
| 1017 | { |
| 1018 | ASSERT(target == GL_TEXTURE_2D); |
| 1019 | |
| 1020 | needRenderTarget(); |
| 1021 | |
| 1022 | IDirect3DSurface9 *renderTarget = NULL; |
| 1023 | mTexture->GetSurfaceLevel(0, &renderTarget); |
| 1024 | |
| 1025 | return renderTarget; |
| 1026 | } |
| 1027 | |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 1028 | TextureCubeMap::TextureCubeMap(GLuint id) : Texture(id) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1029 | { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1030 | mTexture = NULL; |
daniel@transgaming.com | 93a8147 | 2010-04-20 18:52:58 +0000 | [diff] [blame] | 1031 | |
| 1032 | for (int i = 0; i < 6; i++) |
| 1033 | { |
| 1034 | mFaceProxies[i] = NULL; |
| 1035 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | TextureCubeMap::~TextureCubeMap() |
| 1039 | { |
daniel@transgaming.com | 93a8147 | 2010-04-20 18:52:58 +0000 | [diff] [blame] | 1040 | for (int i = 0; i < 6; i++) |
| 1041 | { |
| 1042 | delete mFaceProxies[i]; |
| 1043 | } |
| 1044 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1045 | if (mTexture) |
| 1046 | { |
| 1047 | mTexture->Release(); |
| 1048 | mTexture = NULL; |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | GLenum TextureCubeMap::getTarget() const |
| 1053 | { |
| 1054 | return GL_TEXTURE_CUBE_MAP; |
| 1055 | } |
| 1056 | |
daniel@transgaming.com | 0186813 | 2010-08-24 19:21:17 +0000 | [diff] [blame^] | 1057 | GLenum TextureCubeMap::getFormat() const |
| 1058 | { |
| 1059 | return mImageArray[0][0].format; |
| 1060 | } |
| 1061 | |
daniel@transgaming.com | 3489e3a | 2010-03-21 04:31:11 +0000 | [diff] [blame] | 1062 | void TextureCubeMap::setImagePosX(GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1063 | { |
daniel@transgaming.com | 3489e3a | 2010-03-21 04:31:11 +0000 | [diff] [blame] | 1064 | setImage(0, level, internalFormat, width, height, format, type, unpackAlignment, pixels); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
daniel@transgaming.com | 3489e3a | 2010-03-21 04:31:11 +0000 | [diff] [blame] | 1067 | void TextureCubeMap::setImageNegX(GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1068 | { |
daniel@transgaming.com | 3489e3a | 2010-03-21 04:31:11 +0000 | [diff] [blame] | 1069 | setImage(1, level, internalFormat, width, height, format, type, unpackAlignment, pixels); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
daniel@transgaming.com | 3489e3a | 2010-03-21 04:31:11 +0000 | [diff] [blame] | 1072 | void TextureCubeMap::setImagePosY(GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1073 | { |
daniel@transgaming.com | 3489e3a | 2010-03-21 04:31:11 +0000 | [diff] [blame] | 1074 | setImage(2, level, internalFormat, width, height, format, type, unpackAlignment, pixels); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
daniel@transgaming.com | 3489e3a | 2010-03-21 04:31:11 +0000 | [diff] [blame] | 1077 | void TextureCubeMap::setImageNegY(GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1078 | { |
daniel@transgaming.com | 3489e3a | 2010-03-21 04:31:11 +0000 | [diff] [blame] | 1079 | setImage(3, level, internalFormat, width, height, format, type, unpackAlignment, pixels); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
daniel@transgaming.com | 3489e3a | 2010-03-21 04:31:11 +0000 | [diff] [blame] | 1082 | void TextureCubeMap::setImagePosZ(GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1083 | { |
daniel@transgaming.com | 3489e3a | 2010-03-21 04:31:11 +0000 | [diff] [blame] | 1084 | setImage(4, level, internalFormat, width, height, format, type, unpackAlignment, pixels); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
daniel@transgaming.com | 3489e3a | 2010-03-21 04:31:11 +0000 | [diff] [blame] | 1087 | void TextureCubeMap::setImageNegZ(GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1088 | { |
daniel@transgaming.com | 3489e3a | 2010-03-21 04:31:11 +0000 | [diff] [blame] | 1089 | setImage(5, level, internalFormat, width, height, format, type, unpackAlignment, pixels); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
daniel@transgaming.com | 0186813 | 2010-08-24 19:21:17 +0000 | [diff] [blame^] | 1092 | void TextureCubeMap::setCompressedImage(GLenum face, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels) |
| 1093 | { |
| 1094 | redefineTexture(level, internalFormat, width); |
| 1095 | |
| 1096 | Texture::setCompressedImage(width, height, internalFormat, imageSize, pixels, &mImageArray[faceIndex(face)][level]); |
| 1097 | } |
| 1098 | |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 1099 | void TextureCubeMap::commitRect(GLenum faceTarget, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height) |
| 1100 | { |
| 1101 | int face = faceIndex(faceTarget); |
| 1102 | |
| 1103 | ASSERT(mImageArray[face][level].surface != NULL); |
| 1104 | |
daniel@transgaming.com | c808c5a | 2010-05-14 17:31:01 +0000 | [diff] [blame] | 1105 | if (level < levelCount()) |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 1106 | { |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 1107 | IDirect3DSurface9 *destLevel = getCubeMapSurface(face, level); |
| 1108 | ASSERT(destLevel != NULL); |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 1109 | |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 1110 | if (destLevel != NULL) |
daniel@transgaming.com | 7051b97 | 2010-03-21 04:31:07 +0000 | [diff] [blame] | 1111 | { |
| 1112 | Image *img = &mImageArray[face][level]; |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 1113 | |
daniel@transgaming.com | 7051b97 | 2010-03-21 04:31:07 +0000 | [diff] [blame] | 1114 | RECT sourceRect; |
| 1115 | sourceRect.left = xoffset; |
| 1116 | sourceRect.top = yoffset; |
| 1117 | sourceRect.right = xoffset + width; |
| 1118 | sourceRect.bottom = yoffset + height; |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 1119 | |
daniel@transgaming.com | 7051b97 | 2010-03-21 04:31:07 +0000 | [diff] [blame] | 1120 | POINT destPoint; |
| 1121 | destPoint.x = xoffset; |
| 1122 | destPoint.y = yoffset; |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 1123 | |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 1124 | HRESULT result = getDevice()->UpdateSurface(img->surface, &sourceRect, destLevel, &destPoint); |
daniel@transgaming.com | 7051b97 | 2010-03-21 04:31:07 +0000 | [diff] [blame] | 1125 | ASSERT(SUCCEEDED(result)); |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 1126 | |
daniel@transgaming.com | 7051b97 | 2010-03-21 04:31:07 +0000 | [diff] [blame] | 1127 | destLevel->Release(); |
| 1128 | |
| 1129 | img->dirty = false; |
| 1130 | } |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 1131 | } |
| 1132 | } |
| 1133 | |
daniel@transgaming.com | 3489e3a | 2010-03-21 04:31:11 +0000 | [diff] [blame] | 1134 | void TextureCubeMap::subImage(GLenum face, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels) |
daniel@transgaming.com | 00c7596 | 2010-03-11 20:36:15 +0000 | [diff] [blame] | 1135 | { |
daniel@transgaming.com | 3127355 | 2010-08-04 13:42:44 +0000 | [diff] [blame] | 1136 | if (Texture::subImage(xoffset, yoffset, width, height, format, type, unpackAlignment, pixels, &mImageArray[faceIndex(face)][level])) |
| 1137 | { |
| 1138 | commitRect(face, level, xoffset, yoffset, width, height); |
| 1139 | } |
daniel@transgaming.com | 00c7596 | 2010-03-11 20:36:15 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
daniel@transgaming.com | 0186813 | 2010-08-24 19:21:17 +0000 | [diff] [blame^] | 1142 | void TextureCubeMap::subImageCompressed(GLenum face, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels) |
| 1143 | { |
| 1144 | if (Texture::subImageCompressed(xoffset, yoffset, width, height, format, imageSize, pixels, &mImageArray[faceIndex(face)][level])) |
| 1145 | { |
| 1146 | commitRect(face, level, xoffset, yoffset, width, height); |
| 1147 | } |
| 1148 | } |
| 1149 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1150 | // Tests for GL texture object completeness. [OpenGL ES 2.0.24] section 3.7.10 page 81. |
daniel@transgaming.com | 00c7596 | 2010-03-11 20:36:15 +0000 | [diff] [blame] | 1151 | bool TextureCubeMap::isComplete() const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1152 | { |
daniel@transgaming.com | 93a8147 | 2010-04-20 18:52:58 +0000 | [diff] [blame] | 1153 | int size = mImageArray[0][0].width; |
| 1154 | |
| 1155 | if (size <= 0) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1156 | { |
| 1157 | return false; |
| 1158 | } |
| 1159 | |
| 1160 | bool mipmapping; |
| 1161 | |
daniel@transgaming.com | 12d5407 | 2010-03-16 06:23:26 +0000 | [diff] [blame] | 1162 | switch (mMinFilter) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1163 | { |
| 1164 | case GL_NEAREST: |
| 1165 | case GL_LINEAR: |
| 1166 | mipmapping = false; |
| 1167 | break; |
| 1168 | case GL_NEAREST_MIPMAP_NEAREST: |
| 1169 | case GL_LINEAR_MIPMAP_NEAREST: |
| 1170 | case GL_NEAREST_MIPMAP_LINEAR: |
| 1171 | case GL_LINEAR_MIPMAP_LINEAR: |
| 1172 | mipmapping = true; |
| 1173 | break; |
| 1174 | default: UNREACHABLE(); |
| 1175 | } |
| 1176 | |
| 1177 | for (int face = 0; face < 6; face++) |
| 1178 | { |
daniel@transgaming.com | 93a8147 | 2010-04-20 18:52:58 +0000 | [diff] [blame] | 1179 | if (mImageArray[face][0].width != size || mImageArray[face][0].height != size) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1180 | { |
| 1181 | return false; |
| 1182 | } |
| 1183 | } |
| 1184 | |
| 1185 | if (mipmapping) |
| 1186 | { |
daniel@transgaming.com | d99bd45 | 2010-04-22 13:35:25 +0000 | [diff] [blame] | 1187 | if (!isPow2(size) && (getWrapS() != GL_CLAMP_TO_EDGE || getWrapT() != GL_CLAMP_TO_EDGE)) |
| 1188 | { |
| 1189 | return false; |
| 1190 | } |
| 1191 | |
daniel@transgaming.com | 93a8147 | 2010-04-20 18:52:58 +0000 | [diff] [blame] | 1192 | int q = log2(size); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1193 | |
| 1194 | for (int face = 0; face < 6; face++) |
| 1195 | { |
| 1196 | for (int level = 1; level <= q; level++) |
| 1197 | { |
| 1198 | if (mImageArray[face][level].format != mImageArray[0][0].format) |
| 1199 | { |
| 1200 | return false; |
| 1201 | } |
| 1202 | |
daniel@transgaming.com | d99bd45 | 2010-04-22 13:35:25 +0000 | [diff] [blame] | 1203 | if (mImageArray[face][level].width != std::max(1, size >> level)) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1204 | { |
| 1205 | return false; |
| 1206 | } |
| 1207 | |
daniel@transgaming.com | d99bd45 | 2010-04-22 13:35:25 +0000 | [diff] [blame] | 1208 | ASSERT(mImageArray[face][level].height == mImageArray[face][level].width); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1209 | } |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | return true; |
| 1214 | } |
| 1215 | |
daniel@transgaming.com | 0186813 | 2010-08-24 19:21:17 +0000 | [diff] [blame^] | 1216 | bool TextureCubeMap::isCompressed() const |
| 1217 | { |
| 1218 | return IsCompressed(getFormat()); |
| 1219 | } |
| 1220 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1221 | // Constructs a Direct3D 9 texture resource from the texture images, or returns an existing one |
daniel@transgaming.com | 29d2700 | 2010-03-11 19:41:22 +0000 | [diff] [blame] | 1222 | IDirect3DBaseTexture9 *TextureCubeMap::createTexture() |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1223 | { |
daniel@transgaming.com | 29d2700 | 2010-03-11 19:41:22 +0000 | [diff] [blame] | 1224 | IDirect3DDevice9 *device = getDevice(); |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 1225 | D3DFORMAT format = selectFormat(mImageArray[0][0].format); |
daniel@transgaming.com | 29d2700 | 2010-03-11 19:41:22 +0000 | [diff] [blame] | 1226 | |
| 1227 | IDirect3DCubeTexture9 *texture; |
| 1228 | |
daniel@transgaming.com | c808c5a | 2010-05-14 17:31:01 +0000 | [diff] [blame] | 1229 | HRESULT result = device->CreateCubeTexture(mWidth, creationLevels(mWidth, 0), 0, format, D3DPOOL_DEFAULT, &texture, NULL); |
daniel@transgaming.com | 29d2700 | 2010-03-11 19:41:22 +0000 | [diff] [blame] | 1230 | |
daniel@transgaming.com | 7051b97 | 2010-03-21 04:31:07 +0000 | [diff] [blame] | 1231 | if (FAILED(result)) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1232 | { |
daniel@transgaming.com | 7051b97 | 2010-03-21 04:31:07 +0000 | [diff] [blame] | 1233 | ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); |
daniel@transgaming.com | 29d2700 | 2010-03-11 19:41:22 +0000 | [diff] [blame] | 1234 | return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1235 | } |
| 1236 | |
daniel@transgaming.com | 00c7596 | 2010-03-11 20:36:15 +0000 | [diff] [blame] | 1237 | if (mTexture) mTexture->Release(); |
| 1238 | |
| 1239 | mTexture = texture; |
| 1240 | return mTexture; |
| 1241 | } |
| 1242 | |
| 1243 | void TextureCubeMap::updateTexture() |
| 1244 | { |
| 1245 | IDirect3DDevice9 *device = getDevice(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1246 | |
daniel@transgaming.com | 29d2700 | 2010-03-11 19:41:22 +0000 | [diff] [blame] | 1247 | for (int face = 0; face < 6; face++) |
| 1248 | { |
daniel@transgaming.com | c808c5a | 2010-05-14 17:31:01 +0000 | [diff] [blame] | 1249 | int levels = levelCount(); |
| 1250 | for (int level = 0; level < levels; level++) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1251 | { |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 1252 | Image *img = &mImageArray[face][level]; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1253 | |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 1254 | if (img->dirty) |
| 1255 | { |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 1256 | IDirect3DSurface9 *levelSurface = getCubeMapSurface(face, level); |
| 1257 | ASSERT(levelSurface != NULL); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1258 | |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 1259 | if (levelSurface != NULL) |
daniel@transgaming.com | 7051b97 | 2010-03-21 04:31:07 +0000 | [diff] [blame] | 1260 | { |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 1261 | HRESULT result = device->UpdateSurface(img->surface, NULL, levelSurface, NULL); |
daniel@transgaming.com | 7051b97 | 2010-03-21 04:31:07 +0000 | [diff] [blame] | 1262 | ASSERT(SUCCEEDED(result)); |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 1263 | |
daniel@transgaming.com | 7051b97 | 2010-03-21 04:31:07 +0000 | [diff] [blame] | 1264 | levelSurface->Release(); |
| 1265 | |
| 1266 | img->dirty = false; |
| 1267 | } |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 1268 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1269 | } |
| 1270 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
daniel@transgaming.com | 7051b97 | 2010-03-21 04:31:07 +0000 | [diff] [blame] | 1273 | IDirect3DBaseTexture9 *TextureCubeMap::convertToRenderTarget() |
| 1274 | { |
daniel@transgaming.com | 34dc3e8 | 2010-04-15 20:45:02 +0000 | [diff] [blame] | 1275 | IDirect3DCubeTexture9 *texture = NULL; |
daniel@transgaming.com | 7051b97 | 2010-03-21 04:31:07 +0000 | [diff] [blame] | 1276 | |
daniel@transgaming.com | 34dc3e8 | 2010-04-15 20:45:02 +0000 | [diff] [blame] | 1277 | if (mWidth != 0) |
daniel@transgaming.com | 7051b97 | 2010-03-21 04:31:07 +0000 | [diff] [blame] | 1278 | { |
daniel@transgaming.com | ae072af | 2010-05-05 18:47:28 +0000 | [diff] [blame] | 1279 | egl::Display *display = getDisplay(); |
daniel@transgaming.com | 34dc3e8 | 2010-04-15 20:45:02 +0000 | [diff] [blame] | 1280 | IDirect3DDevice9 *device = getDevice(); |
| 1281 | D3DFORMAT format = selectFormat(mImageArray[0][0].format); |
| 1282 | |
daniel@transgaming.com | c808c5a | 2010-05-14 17:31:01 +0000 | [diff] [blame] | 1283 | HRESULT result = device->CreateCubeTexture(mWidth, creationLevels(mWidth, 0), D3DUSAGE_RENDERTARGET, format, D3DPOOL_DEFAULT, &texture, NULL); |
daniel@transgaming.com | 34dc3e8 | 2010-04-15 20:45:02 +0000 | [diff] [blame] | 1284 | |
| 1285 | if (FAILED(result)) |
| 1286 | { |
| 1287 | ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); |
| 1288 | return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL); |
| 1289 | } |
| 1290 | |
| 1291 | if (mTexture != NULL) |
| 1292 | { |
daniel@transgaming.com | c808c5a | 2010-05-14 17:31:01 +0000 | [diff] [blame] | 1293 | int levels = levelCount(); |
daniel@transgaming.com | 34dc3e8 | 2010-04-15 20:45:02 +0000 | [diff] [blame] | 1294 | for (int f = 0; f < 6; f++) |
| 1295 | { |
| 1296 | for (int i = 0; i < levels; i++) |
| 1297 | { |
| 1298 | IDirect3DSurface9 *source; |
| 1299 | result = mTexture->GetCubeMapSurface(static_cast<D3DCUBEMAP_FACES>(f), i, &source); |
| 1300 | |
| 1301 | if (FAILED(result)) |
| 1302 | { |
| 1303 | ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); |
| 1304 | |
| 1305 | texture->Release(); |
| 1306 | |
| 1307 | return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL); |
| 1308 | } |
| 1309 | |
| 1310 | IDirect3DSurface9 *dest; |
| 1311 | result = texture->GetCubeMapSurface(static_cast<D3DCUBEMAP_FACES>(f), i, &dest); |
| 1312 | |
| 1313 | if (FAILED(result)) |
| 1314 | { |
| 1315 | ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); |
| 1316 | |
| 1317 | texture->Release(); |
| 1318 | source->Release(); |
| 1319 | |
| 1320 | return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL); |
| 1321 | } |
| 1322 | |
daniel@transgaming.com | ae072af | 2010-05-05 18:47:28 +0000 | [diff] [blame] | 1323 | display->endScene(); |
daniel@transgaming.com | 34dc3e8 | 2010-04-15 20:45:02 +0000 | [diff] [blame] | 1324 | result = device->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE); |
| 1325 | |
| 1326 | if (FAILED(result)) |
| 1327 | { |
| 1328 | ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); |
| 1329 | |
| 1330 | texture->Release(); |
| 1331 | source->Release(); |
| 1332 | dest->Release(); |
| 1333 | |
| 1334 | return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL); |
| 1335 | } |
| 1336 | } |
| 1337 | } |
| 1338 | } |
daniel@transgaming.com | 7051b97 | 2010-03-21 04:31:07 +0000 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | if (mTexture != NULL) |
| 1342 | { |
daniel@transgaming.com | 7051b97 | 2010-03-21 04:31:07 +0000 | [diff] [blame] | 1343 | mTexture->Release(); |
| 1344 | } |
| 1345 | |
| 1346 | mTexture = texture; |
| 1347 | return mTexture; |
| 1348 | } |
| 1349 | |
daniel@transgaming.com | 3489e3a | 2010-03-21 04:31:11 +0000 | [diff] [blame] | 1350 | void TextureCubeMap::setImage(int face, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1351 | { |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 1352 | redefineTexture(level, internalFormat, width); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1353 | |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 1354 | Texture::setImage(width, height, format, type, unpackAlignment, pixels, &mImageArray[face][level]); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1355 | } |
daniel@transgaming.com | 00c7596 | 2010-03-11 20:36:15 +0000 | [diff] [blame] | 1356 | |
| 1357 | unsigned int TextureCubeMap::faceIndex(GLenum face) |
| 1358 | { |
| 1359 | META_ASSERT(GL_TEXTURE_CUBE_MAP_NEGATIVE_X - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 1); |
| 1360 | META_ASSERT(GL_TEXTURE_CUBE_MAP_POSITIVE_Y - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 2); |
| 1361 | META_ASSERT(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 3); |
| 1362 | META_ASSERT(GL_TEXTURE_CUBE_MAP_POSITIVE_Z - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 4); |
| 1363 | META_ASSERT(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 5); |
| 1364 | |
| 1365 | return face - GL_TEXTURE_CUBE_MAP_POSITIVE_X; |
| 1366 | } |
| 1367 | |
daniel@transgaming.com | 842f7a4 | 2010-03-21 04:31:03 +0000 | [diff] [blame] | 1368 | bool TextureCubeMap::dirtyImageData() const |
| 1369 | { |
| 1370 | int q = log2(mWidth); |
| 1371 | |
| 1372 | for (int f = 0; f < 6; f++) |
| 1373 | { |
| 1374 | for (int i = 0; i <= q; i++) |
| 1375 | { |
| 1376 | if (mImageArray[f][i].dirty) return true; |
| 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | return false; |
| 1381 | } |
| 1382 | |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 1383 | // While OpenGL doesn't check texture consistency until draw-time, D3D9 requires a complete texture |
| 1384 | // for render-to-texture (such as CopyTexImage). We have no way of keeping individual inconsistent levels & faces. |
| 1385 | // Call this when a particular level of the texture must be defined with a specific format, width and height. |
| 1386 | // |
| 1387 | // Returns true if the existing texture was unsuitable had to be destroyed. If so, it will also set |
| 1388 | // a new size for the texture by working backwards from the given size. |
| 1389 | bool TextureCubeMap::redefineTexture(GLint level, GLenum internalFormat, GLsizei width) |
| 1390 | { |
| 1391 | // Are these settings compatible with level 0? |
| 1392 | bool sizeOkay = (mImageArray[0][0].width >> level == width); |
| 1393 | |
| 1394 | bool textureOkay = (sizeOkay && internalFormat == mImageArray[0][0].format); |
| 1395 | |
| 1396 | if (!textureOkay) |
| 1397 | { |
| 1398 | TRACE("Redefining cube texture (%d, 0x%04X, %d => 0x%04X, %d).", level, |
| 1399 | mImageArray[0][0].format, mImageArray[0][0].width, |
| 1400 | internalFormat, width); |
| 1401 | |
| 1402 | // Purge all the levels and the texture. |
| 1403 | for (int i = 0; i < MAX_TEXTURE_LEVELS; i++) |
| 1404 | { |
| 1405 | for (int f = 0; f < 6; f++) |
| 1406 | { |
| 1407 | if (mImageArray[f][i].surface != NULL) |
| 1408 | { |
| 1409 | mImageArray[f][i].dirty = false; |
| 1410 | |
| 1411 | mImageArray[f][i].surface->Release(); |
| 1412 | mImageArray[f][i].surface = NULL; |
| 1413 | } |
| 1414 | } |
| 1415 | } |
| 1416 | |
| 1417 | if (mTexture != NULL) |
| 1418 | { |
| 1419 | mTexture->Release(); |
| 1420 | mTexture = NULL; |
| 1421 | dropTexture(); |
| 1422 | } |
| 1423 | |
| 1424 | mWidth = width << level; |
| 1425 | mImageArray[0][0].width = width << level; |
| 1426 | mHeight = width << level; |
| 1427 | mImageArray[0][0].height = width << level; |
| 1428 | |
| 1429 | mImageArray[0][0].format = internalFormat; |
| 1430 | } |
| 1431 | |
| 1432 | return !textureOkay; |
| 1433 | } |
| 1434 | |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 1435 | void TextureCubeMap::copyImage(GLenum face, GLint level, GLenum internalFormat, GLint x, GLint y, GLsizei width, GLsizei height, RenderbufferStorage *source) |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 1436 | { |
| 1437 | unsigned int faceindex = faceIndex(face); |
| 1438 | |
| 1439 | if (redefineTexture(level, internalFormat, width)) |
| 1440 | { |
| 1441 | convertToRenderTarget(); |
daniel@transgaming.com | 93a8147 | 2010-04-20 18:52:58 +0000 | [diff] [blame] | 1442 | pushTexture(mTexture, true); |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 1443 | } |
daniel@transgaming.com | bc3699d | 2010-08-05 14:48:49 +0000 | [diff] [blame] | 1444 | else |
| 1445 | { |
| 1446 | needRenderTarget(); |
| 1447 | } |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 1448 | |
daniel@transgaming.com | 34dc3e8 | 2010-04-15 20:45:02 +0000 | [diff] [blame] | 1449 | ASSERT(width == height); |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 1450 | |
daniel@transgaming.com | c808c5a | 2010-05-14 17:31:01 +0000 | [diff] [blame] | 1451 | if (width > 0 && level < levelCount()) |
daniel@transgaming.com | 34dc3e8 | 2010-04-15 20:45:02 +0000 | [diff] [blame] | 1452 | { |
| 1453 | RECT sourceRect; |
| 1454 | sourceRect.left = x; |
daniel@transgaming.com | 34dc3e8 | 2010-04-15 20:45:02 +0000 | [diff] [blame] | 1455 | sourceRect.right = x + width; |
daniel@transgaming.com | 18b426b | 2010-04-20 18:52:44 +0000 | [diff] [blame] | 1456 | sourceRect.top = y; |
| 1457 | sourceRect.bottom = y + height; |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 1458 | |
daniel@transgaming.com | 34dc3e8 | 2010-04-15 20:45:02 +0000 | [diff] [blame] | 1459 | IDirect3DSurface9 *dest = getCubeMapSurface(face, level); |
| 1460 | |
| 1461 | getBlitter()->formatConvert(source->getRenderTarget(), sourceRect, internalFormat, 0, 0, dest); |
| 1462 | dest->Release(); |
| 1463 | } |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 1464 | |
| 1465 | mImageArray[faceindex][level].width = width; |
| 1466 | mImageArray[faceindex][level].height = height; |
| 1467 | mImageArray[faceindex][level].format = internalFormat; |
| 1468 | } |
| 1469 | |
| 1470 | IDirect3DSurface9 *TextureCubeMap::getCubeMapSurface(unsigned int faceIdentifier, unsigned int level) |
| 1471 | { |
| 1472 | unsigned int faceIndex; |
| 1473 | |
| 1474 | if (faceIdentifier < 6) |
| 1475 | { |
| 1476 | faceIndex = faceIdentifier; |
| 1477 | } |
| 1478 | else if (faceIdentifier >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && faceIdentifier <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z) |
| 1479 | { |
| 1480 | faceIndex = faceIdentifier - GL_TEXTURE_CUBE_MAP_POSITIVE_X; |
| 1481 | } |
| 1482 | else |
| 1483 | { |
| 1484 | UNREACHABLE(); |
| 1485 | faceIndex = 0; |
| 1486 | } |
| 1487 | |
| 1488 | if (mTexture == NULL) |
| 1489 | { |
| 1490 | UNREACHABLE(); |
| 1491 | return NULL; |
| 1492 | } |
| 1493 | |
| 1494 | IDirect3DSurface9 *surface = NULL; |
| 1495 | |
| 1496 | HRESULT hr = mTexture->GetCubeMapSurface(static_cast<D3DCUBEMAP_FACES>(faceIndex), level, &surface); |
| 1497 | |
| 1498 | return (SUCCEEDED(hr)) ? surface : NULL; |
| 1499 | } |
| 1500 | |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 1501 | void TextureCubeMap::copySubImage(GLenum face, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, RenderbufferStorage *source) |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 1502 | { |
daniel@transgaming.com | 34dc3e8 | 2010-04-15 20:45:02 +0000 | [diff] [blame] | 1503 | GLsizei size = mImageArray[faceIndex(face)][level].width; |
| 1504 | |
| 1505 | if (xoffset + width > size || yoffset + height > size) |
| 1506 | { |
| 1507 | return error(GL_INVALID_VALUE); |
| 1508 | } |
| 1509 | |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 1510 | if (redefineTexture(0, mImageArray[0][0].format, mImageArray[0][0].width)) |
| 1511 | { |
| 1512 | convertToRenderTarget(); |
daniel@transgaming.com | 93a8147 | 2010-04-20 18:52:58 +0000 | [diff] [blame] | 1513 | pushTexture(mTexture, true); |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 1514 | } |
| 1515 | else |
| 1516 | { |
daniel@transgaming.com | bc3699d | 2010-08-05 14:48:49 +0000 | [diff] [blame] | 1517 | needRenderTarget(); |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
daniel@transgaming.com | c808c5a | 2010-05-14 17:31:01 +0000 | [diff] [blame] | 1520 | if (level < levelCount()) |
| 1521 | { |
| 1522 | RECT sourceRect; |
| 1523 | sourceRect.left = x; |
| 1524 | sourceRect.right = x + width; |
| 1525 | sourceRect.top = y; |
| 1526 | sourceRect.bottom = y + height; |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 1527 | |
daniel@transgaming.com | c808c5a | 2010-05-14 17:31:01 +0000 | [diff] [blame] | 1528 | IDirect3DSurface9 *dest = getCubeMapSurface(face, level); |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 1529 | |
daniel@transgaming.com | c808c5a | 2010-05-14 17:31:01 +0000 | [diff] [blame] | 1530 | getBlitter()->formatConvert(source->getRenderTarget(), sourceRect, mImageArray[0][0].format, xoffset, yoffset, dest); |
| 1531 | dest->Release(); |
| 1532 | } |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 1533 | } |
| 1534 | |
daniel@transgaming.com | 8fd99e2 | 2010-04-20 18:52:00 +0000 | [diff] [blame] | 1535 | bool TextureCubeMap::isCubeComplete() const |
| 1536 | { |
| 1537 | if (mImageArray[0][0].width == 0) |
| 1538 | { |
| 1539 | return false; |
| 1540 | } |
| 1541 | |
| 1542 | for (unsigned int f = 1; f < 6; f++) |
| 1543 | { |
| 1544 | if (mImageArray[f][0].width != mImageArray[0][0].width |
| 1545 | || mImageArray[f][0].format != mImageArray[0][0].format) |
| 1546 | { |
| 1547 | return false; |
| 1548 | } |
| 1549 | } |
| 1550 | |
| 1551 | return true; |
| 1552 | } |
| 1553 | |
| 1554 | void TextureCubeMap::generateMipmaps() |
| 1555 | { |
| 1556 | if (!isPow2(mImageArray[0][0].width) || !isCubeComplete()) |
| 1557 | { |
| 1558 | return error(GL_INVALID_OPERATION); |
| 1559 | } |
| 1560 | |
| 1561 | // Purge array levels 1 through q and reset them to represent the generated mipmap levels. |
| 1562 | unsigned int q = log2(mImageArray[0][0].width); |
| 1563 | for (unsigned int f = 0; f < 6; f++) |
| 1564 | { |
| 1565 | for (unsigned int i = 1; i <= q; i++) |
| 1566 | { |
| 1567 | if (mImageArray[f][i].surface != NULL) |
| 1568 | { |
| 1569 | mImageArray[f][i].surface->Release(); |
| 1570 | mImageArray[f][i].surface = NULL; |
| 1571 | } |
| 1572 | |
| 1573 | mImageArray[f][i].dirty = false; |
| 1574 | |
| 1575 | mImageArray[f][i].format = mImageArray[f][0].format; |
| 1576 | mImageArray[f][i].width = std::max(mImageArray[f][0].width >> i, 1); |
| 1577 | mImageArray[f][i].height = mImageArray[f][i].width; |
| 1578 | } |
| 1579 | } |
| 1580 | |
daniel@transgaming.com | 93a8147 | 2010-04-20 18:52:58 +0000 | [diff] [blame] | 1581 | needRenderTarget(); |
daniel@transgaming.com | 8fd99e2 | 2010-04-20 18:52:00 +0000 | [diff] [blame] | 1582 | |
| 1583 | for (unsigned int f = 0; f < 6; f++) |
| 1584 | { |
| 1585 | for (unsigned int i = 1; i <= q; i++) |
| 1586 | { |
| 1587 | IDirect3DSurface9 *upper = getCubeMapSurface(f, i-1); |
| 1588 | IDirect3DSurface9 *lower = getCubeMapSurface(f, i); |
| 1589 | |
| 1590 | if (upper != NULL && lower != NULL) |
| 1591 | { |
| 1592 | getBlitter()->boxFilter(upper, lower); |
| 1593 | } |
| 1594 | |
| 1595 | if (upper != NULL) upper->Release(); |
| 1596 | if (lower != NULL) lower->Release(); |
| 1597 | } |
| 1598 | } |
| 1599 | } |
| 1600 | |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 1601 | Renderbuffer *TextureCubeMap::getColorbuffer(GLenum target) |
daniel@transgaming.com | 93a8147 | 2010-04-20 18:52:58 +0000 | [diff] [blame] | 1602 | { |
daniel@transgaming.com | 19ffc24 | 2010-05-04 03:35:21 +0000 | [diff] [blame] | 1603 | if (!IsCubemapTextureTarget(target)) |
daniel@transgaming.com | 93a8147 | 2010-04-20 18:52:58 +0000 | [diff] [blame] | 1604 | { |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 1605 | return error(GL_INVALID_OPERATION, (Renderbuffer *)NULL); |
daniel@transgaming.com | 93a8147 | 2010-04-20 18:52:58 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | unsigned int face = faceIndex(target); |
| 1609 | |
| 1610 | if (mFaceProxies[face] == NULL) |
| 1611 | { |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 1612 | mFaceProxies[face] = new Renderbuffer(id(), new TextureColorbufferProxy(this, target)); |
| 1613 | mFaceProxies[face]->addRef(); |
daniel@transgaming.com | 93a8147 | 2010-04-20 18:52:58 +0000 | [diff] [blame] | 1614 | } |
| 1615 | |
| 1616 | return mFaceProxies[face]; |
| 1617 | } |
| 1618 | |
| 1619 | IDirect3DSurface9 *TextureCubeMap::getRenderTarget(GLenum target) |
| 1620 | { |
daniel@transgaming.com | 19ffc24 | 2010-05-04 03:35:21 +0000 | [diff] [blame] | 1621 | ASSERT(IsCubemapTextureTarget(target)); |
daniel@transgaming.com | 93a8147 | 2010-04-20 18:52:58 +0000 | [diff] [blame] | 1622 | |
| 1623 | needRenderTarget(); |
| 1624 | |
| 1625 | IDirect3DSurface9 *renderTarget = NULL; |
| 1626 | mTexture->GetCubeMapSurface(static_cast<D3DCUBEMAP_FACES>(faceIndex(target)), 0, &renderTarget); |
| 1627 | |
| 1628 | return renderTarget; |
| 1629 | } |
| 1630 | |
| 1631 | Texture::TextureColorbufferProxy::TextureColorbufferProxy(Texture *texture, GLenum target) |
| 1632 | : Colorbuffer(NULL), mTexture(texture), mTarget(target) |
| 1633 | { |
daniel@transgaming.com | 19ffc24 | 2010-05-04 03:35:21 +0000 | [diff] [blame] | 1634 | ASSERT(target == GL_TEXTURE_2D || IsCubemapTextureTarget(target)); |
daniel@transgaming.com | 93a8147 | 2010-04-20 18:52:58 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 1637 | void Texture::TextureColorbufferProxy::addRef() const |
| 1638 | { |
| 1639 | mTexture->addRef(); |
| 1640 | } |
| 1641 | |
| 1642 | void Texture::TextureColorbufferProxy::release() const |
| 1643 | { |
| 1644 | mTexture->release(); |
| 1645 | } |
| 1646 | |
daniel@transgaming.com | 93a8147 | 2010-04-20 18:52:58 +0000 | [diff] [blame] | 1647 | IDirect3DSurface9 *Texture::TextureColorbufferProxy::getRenderTarget() |
| 1648 | { |
daniel@transgaming.com | 93a8147 | 2010-04-20 18:52:58 +0000 | [diff] [blame] | 1649 | if (mRenderTarget) mRenderTarget->Release(); |
| 1650 | |
| 1651 | mRenderTarget = mTexture->getRenderTarget(mTarget); |
| 1652 | |
| 1653 | return mRenderTarget; |
| 1654 | } |
| 1655 | |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 1656 | int Texture::TextureColorbufferProxy::getWidth() const |
daniel@transgaming.com | 93a8147 | 2010-04-20 18:52:58 +0000 | [diff] [blame] | 1657 | { |
daniel@transgaming.com | 866f318 | 2010-05-20 19:28:22 +0000 | [diff] [blame] | 1658 | return mTexture->getWidth(); |
| 1659 | } |
| 1660 | |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 1661 | int Texture::TextureColorbufferProxy::getHeight() const |
daniel@transgaming.com | 866f318 | 2010-05-20 19:28:22 +0000 | [diff] [blame] | 1662 | { |
| 1663 | return mTexture->getHeight(); |
daniel@transgaming.com | 93a8147 | 2010-04-20 18:52:58 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
daniel@transgaming.com | 0186813 | 2010-08-24 19:21:17 +0000 | [diff] [blame^] | 1666 | GLenum Texture::TextureColorbufferProxy::getFormat() const |
| 1667 | { |
| 1668 | return mTexture->getFormat(); |
| 1669 | } |
| 1670 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1671 | } |