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