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