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