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