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