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