Showing the changes to libraries/render-utils/src/TextureCache.cpp (1 of 13 files). Show all
Unified · Split
| @@ -32,13 +32,6 @@ TextureCache::TextureCache() : · 31 unmodified lines | |||
| 32 | _permutationNormalTexture(0), | 32 | _permutationNormalTexture(0), |
| 33 | _whiteTexture(0), | 33 | _whiteTexture(0), |
| 34 | _blueTexture(0), | 34 | _blueTexture(0), |
| 35 | _primaryDepthTextureID(0), | ||
| 36 | _primaryNormalTextureID(0), | ||
| 37 | _primarySpecularTextureID(0), | ||
| 38 | _primaryFramebufferObject(NULL), | ||
| 39 | _secondaryFramebufferObject(NULL), | ||
| 40 | _tertiaryFramebufferObject(NULL), | ||
| 41 | _shadowFramebufferObject(NULL), | ||
| 42 | _frameBufferSize(100, 100), | 35 | _frameBufferSize(100, 100), |
| 43 | _associatedWidget(NULL) | 36 | _associatedWidget(NULL) |
| 44 | { | 37 | { |
| @@ -47,24 +40,6 @@ TextureCache::TextureCache() : · 2 unmodified lines | |||
| 47 | } | 40 | } |
| 48 | 41 | ||
| 49 | TextureCache::~TextureCache() { | 42 | TextureCache::~TextureCache() { |
| 50 | |||
| 51 | if (_primaryFramebufferObject) { | ||
| 52 | glDeleteTextures(1, &_primaryDepthTextureID); | ||
| 53 | glDeleteTextures(1, &_primaryNormalTextureID); | ||
| 54 | glDeleteTextures(1, &_primarySpecularTextureID); | ||
| 55 | } | ||
| 56 | |||
| 57 | if (_primaryFramebufferObject) { | ||
| 58 | delete _primaryFramebufferObject; | ||
| 59 | } | ||
| 60 | |||
| 61 | if (_secondaryFramebufferObject) { | ||
| 62 | delete _secondaryFramebufferObject; | ||
| 63 | } | ||
| 64 | |||
| 65 | if (_tertiaryFramebufferObject) { | ||
| 66 | delete _tertiaryFramebufferObject; | ||
| 67 | } | ||
| 68 | } | 43 | } |
| 69 | 44 | ||
| 70 | void TextureCache::setFrameBufferSize(QSize frameBufferSize) { | 45 | void TextureCache::setFrameBufferSize(QSize frameBufferSize) { |
| @@ -72,35 +47,15 @@ void TextureCache::setFrameBufferSize(QSize frameBufferSize) { · 1 unmodified line | |||
| 72 | if (_frameBufferSize != frameBufferSize) { | 47 | if (_frameBufferSize != frameBufferSize) { |
| 73 | _frameBufferSize = frameBufferSize; | 48 | _frameBufferSize = frameBufferSize; |
| 74 | 49 | ||
| 75 | if (_primaryOpaqueFramebuffer) { | 50 | _primaryFramebuffer.reset(); |
| 76 | _primaryOpaqueFramebuffer.reset(); | 51 | _primaryDepthTexture.reset(); |
| 77 | _primaryTransparentFramebuffer.reset(); | 52 | _primaryColorTexture.reset(); |
| 78 | _primaryDepthTexture.reset(); | 53 | _primaryNormalTexture.reset(); |
| 79 | _primaryColorTexture.reset(); | 54 | _primarySpecularTexture.reset(); |
| 80 | _primaryNormalTexture.reset(); | ||
| 81 | _primarySpecularTexture.reset(); | ||
| 82 | } | ||
| 83 | |||
| 84 | if (_primaryFramebufferObject) { | ||
| 85 | delete _primaryFramebufferObject; | ||
| 86 | _primaryFramebufferObject = NULL; | ||
| 87 | glDeleteTextures(1, &_primaryDepthTextureID); | ||
| 88 | _primaryDepthTextureID = 0; | ||
| 89 | glDeleteTextures(1, &_primaryNormalTextureID); | ||
| 90 | _primaryNormalTextureID = 0; | ||
| 91 | glDeleteTextures(1, &_primarySpecularTextureID); | ||
| 92 | _primarySpecularTextureID = 0; | ||
| 93 | } | ||
| 94 | 55 | ||
| 95 | if (_secondaryFramebufferObject) { | 56 | _secondaryFramebuffer.reset(); |
| 96 | delete _secondaryFramebufferObject; | ||
| 97 | _secondaryFramebufferObject = NULL; | ||
| 98 | } | ||
| 99 | 57 | ||
| 100 | if (_tertiaryFramebufferObject) { | 58 | _tertiaryFramebuffer.reset(); |
| 101 | delete _tertiaryFramebufferObject; | ||
| 102 | _tertiaryFramebufferObject = NULL; | ||
| 103 | } | ||
| 104 | } | 59 | } |
| 105 | } | 60 | } |
| 106 | 61 | ||
| @@ -215,46 +170,8 @@ NetworkTexturePointer TextureCache::getTexture(const QUrl& url, TextureType type · 108 unmodified lines | |||
| 215 | return texture; | 170 | return texture; |
| 216 | } | 171 | } |
| 217 | 172 | ||
| 218 | QOpenGLFramebufferObject* TextureCache::getPrimaryFramebufferObject() { | ||
| 219 | |||
| 220 | if (!_primaryFramebufferObject) { | ||
| 221 | _primaryFramebufferObject = createFramebufferObject(); | ||
| 222 | |||
| 223 | glGenTextures(1, &_primaryDepthTextureID); | ||
| 224 | glBindTexture(GL_TEXTURE_2D, _primaryDepthTextureID); | ||
| 225 | glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, _frameBufferSize.width(), _frameBufferSize.height(), | ||
| 226 | 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0); | ||
| 227 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | ||
| 228 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | ||
| 229 | |||
| 230 | glGenTextures(1, &_primaryNormalTextureID); | ||
| 231 | glBindTexture(GL_TEXTURE_2D, _primaryNormalTextureID); | ||
| 232 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, _frameBufferSize.width(), _frameBufferSize.height(), | ||
| 233 | 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); | ||
| 234 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | ||
| 235 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | ||
| 236 | glBindTexture(GL_TEXTURE_2D, 0); | ||
| 237 | |||
| 238 | glGenTextures(1, &_primarySpecularTextureID); | ||
| 239 | glBindTexture(GL_TEXTURE_2D, _primarySpecularTextureID); | ||
| 240 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, _frameBufferSize.width(), _frameBufferSize.height(), | ||
| 241 | 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); | ||
| 242 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | ||
| 243 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | ||
| 244 | glBindTexture(GL_TEXTURE_2D, 0); | ||
| 245 | |||
| 246 | _primaryFramebufferObject->bind(); | ||
| 247 | glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, _primaryDepthTextureID, 0); | ||
| 248 | glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, _primaryNormalTextureID, 0); | ||
| 249 | glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, _primarySpecularTextureID, 0); | ||
| 250 | _primaryFramebufferObject->release(); | ||
| 251 | } | ||
| 252 | return _primaryFramebufferObject; | ||
| 253 | } | ||
| 254 | |||
| 255 | void TextureCache::createPrimaryFramebuffer() { | 173 | void TextureCache::createPrimaryFramebuffer() { |
| 256 | _primaryOpaqueFramebuffer = gpu::FramebufferPointer(gpu::Framebuffer::create()); | 174 | _primaryFramebuffer = gpu::FramebufferPointer(gpu::Framebuffer::create()); |
| 257 | _primaryTransparentFramebuffer = gpu::FramebufferPointer(gpu::Framebuffer::create()); | ||
| 258 | 175 | ||
| 259 | auto colorFormat = gpu::Element(gpu::VEC4, gpu::NUINT8, gpu::RGBA); | 176 | auto colorFormat = gpu::Element(gpu::VEC4, gpu::NUINT8, gpu::RGBA); |
| 260 | auto width = _frameBufferSize.width(); | 177 | auto width = _frameBufferSize.width(); |
| @@ -265,32 +182,22 @@ void TextureCache::createPrimaryFramebuffer() { · 4 unmodified lines | |||
| 265 | _primaryNormalTexture = gpu::TexturePointer(gpu::Texture::create2D(colorFormat, width, height, defaultSampler)); | 182 | _primaryNormalTexture = gpu::TexturePointer(gpu::Texture::create2D(colorFormat, width, height, defaultSampler)); |
| 266 | _primarySpecularTexture = gpu::TexturePointer(gpu::Texture::create2D(colorFormat, width, height, defaultSampler)); | 183 | _primarySpecularTexture = gpu::TexturePointer(gpu::Texture::create2D(colorFormat, width, height, defaultSampler)); |
| 267 | 184 | ||
| 268 | _primaryOpaqueFramebuffer->setRenderBuffer(0, _primaryColorTexture); | 185 | _primaryFramebuffer->setRenderBuffer(0, _primaryColorTexture); |
| 269 | _primaryOpaqueFramebuffer->setRenderBuffer(1, _primaryNormalTexture); | 186 | _primaryFramebuffer->setRenderBuffer(1, _primaryNormalTexture); |
| 270 | _primaryOpaqueFramebuffer->setRenderBuffer(2, _primarySpecularTexture); | 187 | _primaryFramebuffer->setRenderBuffer(2, _primarySpecularTexture); |
| 271 | 188 | ||
| 272 | _primaryTransparentFramebuffer->setRenderBuffer(0, _primaryColorTexture); | ||
| 273 | 189 | ||
| 274 | auto depthFormat = gpu::Element(gpu::SCALAR, gpu::FLOAT, gpu::DEPTH); | 190 | auto depthFormat = gpu::Element(gpu::SCALAR, gpu::FLOAT, gpu::DEPTH); |
| 275 | _primaryDepthTexture = gpu::TexturePointer(gpu::Texture::create2D(depthFormat, width, height, defaultSampler)); | 191 | _primaryDepthTexture = gpu::TexturePointer(gpu::Texture::create2D(depthFormat, width, height, defaultSampler)); |
| 276 | 192 | ||
| 277 | _primaryOpaqueFramebuffer->setDepthStencilBuffer(_primaryDepthTexture, depthFormat); | 193 | _primaryFramebuffer->setDepthStencilBuffer(_primaryDepthTexture, depthFormat); |
| 278 | |||
| 279 | _primaryTransparentFramebuffer->setDepthStencilBuffer(_primaryDepthTexture, depthFormat); | ||
| 280 | } | 194 | } |
| 281 | 195 | ||
| 282 | gpu::FramebufferPointer TextureCache::getPrimaryOpaqueFramebuffer() { | 196 | gpu::FramebufferPointer TextureCache::getPrimaryFramebuffer() { |
| 283 | if (!_primaryOpaqueFramebuffer) { | 197 | if (!_primaryFramebuffer) { |
| 284 | createPrimaryFramebuffer(); | 198 | createPrimaryFramebuffer(); |
| 285 | } | 199 | } |
| 286 | return _primaryOpaqueFramebuffer; | 200 | return _primaryFramebuffer; |
| 287 | } | ||
| 288 | |||
| 289 | gpu::FramebufferPointer TextureCache::getPrimaryTransparentFramebuffer() { | ||
| 290 | if (!_primaryTransparentFramebuffer) { | ||
| 291 | createPrimaryFramebuffer(); | ||
| 292 | } | ||
| 293 | return _primaryTransparentFramebuffer; | ||
| 294 | } | 201 | } |
| 295 | 202 | ||
| 296 | gpu::TexturePointer TextureCache::getPrimaryDepthTexture() { | 203 | gpu::TexturePointer TextureCache::getPrimaryDepthTexture() { |
| @@ -322,28 +229,19 @@ gpu::TexturePointer TextureCache::getPrimarySpecularTexture() { · 25 unmodified lines | |||
| 322 | } | 229 | } |
| 323 | 230 | ||
| 324 | GLuint TextureCache::getPrimaryDepthTextureID() { | 231 | GLuint TextureCache::getPrimaryDepthTextureID() { |
| 325 | // ensure that the primary framebuffer object is initialized before returning the depth texture id | 232 | return gpu::GLBackend::getTextureID(getPrimaryDepthTexture()); |
| 326 | //getPrimaryFramebufferObject(); | ||
| 327 | |||
| 328 | _primaryDepthTextureID = gpu::GLBackend::getTextureID(_primaryDepthTexture); | ||
| 329 | return _primaryDepthTextureID; | ||
| 330 | } | 233 | } |
| 331 | 234 | ||
| 332 | GLuint TextureCache::getPrimaryColorTextureID() { | 235 | GLuint TextureCache::getPrimaryColorTextureID() { |
| 333 | return gpu::GLBackend::getTextureID(_primaryColorTexture); | 236 | return gpu::GLBackend::getTextureID(getPrimaryColorTexture()); |
| 334 | } | 237 | } |
| 335 | 238 | ||
| 336 | GLuint TextureCache::getPrimaryNormalTextureID() { | 239 | GLuint TextureCache::getPrimaryNormalTextureID() { |
| 337 | // ensure that the primary framebuffer object is initialized before returning the normal texture id | 240 | return gpu::GLBackend::getTextureID(getPrimaryNormalTexture()); |
| 338 | //getPrimaryFramebufferObject(); | ||
| 339 | _primaryNormalTextureID = gpu::GLBackend::getTextureID(_primaryNormalTexture); | ||
| 340 | return _primaryNormalTextureID; | ||
| 341 | } | 241 | } |
| 342 | 242 | ||
| 343 | GLuint TextureCache::getPrimarySpecularTextureID() { | 243 | GLuint TextureCache::getPrimarySpecularTextureID() { |
| 344 | //getPrimaryFramebufferObject(); | 244 | return gpu::GLBackend::getTextureID(getPrimarySpecularTexture()); |
| 345 | _primarySpecularTextureID = gpu::GLBackend::getTextureID(_primarySpecularTexture); | ||
| 346 | return _primarySpecularTextureID; | ||
| 347 | } | 245 | } |
| 348 | 246 | ||
| 349 | void TextureCache::setPrimaryDrawBuffers(bool color, bool normal, bool specular) { | 247 | void TextureCache::setPrimaryDrawBuffers(bool color, bool normal, bool specular) { |
| @@ -361,18 +259,18 @@ void TextureCache::setPrimaryDrawBuffers(bool color, bool normal, bool specular) · 11 unmodified lines | |||
| 361 | glDrawBuffers(bufferCount, buffers); | 259 | glDrawBuffers(bufferCount, buffers); |
| 362 | } | 260 | } |
| 363 | 261 | ||
| 364 | QOpenGLFramebufferObject* TextureCache::getSecondaryFramebufferObject() { | 262 | gpu::FramebufferPointer TextureCache::getSecondaryFramebuffer() { |
| 365 | if (!_secondaryFramebufferObject) { | 263 | if (!_secondaryFramebuffer) { |
| 366 | _secondaryFramebufferObject = createFramebufferObject(); | 264 | _secondaryFramebuffer = gpu::FramebufferPointer(gpu::Framebuffer::create(gpu::Element::COLOR_RGBA_32, _frameBufferSize.width(), _frameBufferSize.height())); |
| 367 | } | 265 | } |
| 368 | return _secondaryFramebufferObject; | 266 | return _secondaryFramebuffer; |
| 369 | } | 267 | } |
| 370 | 268 | ||
| 371 | QOpenGLFramebufferObject* TextureCache::getTertiaryFramebufferObject() { | 269 | gpu::FramebufferPointer TextureCache::getTertiaryFramebuffer() { |
| 372 | if (!_tertiaryFramebufferObject) { | 270 | if (!_tertiaryFramebuffer) { |
| 373 | _tertiaryFramebufferObject = createFramebufferObject(); | 271 | _tertiaryFramebuffer = gpu::FramebufferPointer(gpu::Framebuffer::create(gpu::Element::COLOR_RGBA_32, _frameBufferSize.width(), _frameBufferSize.height())); |
| 374 | } | 272 | } |
| 375 | return _tertiaryFramebufferObject; | 273 | return _tertiaryFramebuffer; |
| 376 | } | 274 | } |
| 377 | 275 | ||
| 378 | 276 | ||
| @@ -386,58 +284,25 @@ gpu::FramebufferPointer TextureCache::getShadowFramebuffer() { · 7 unmodified lines | |||
| 386 | return _shadowFramebuffer; | 284 | return _shadowFramebuffer; |
| 387 | } | 285 | } |
| 388 | 286 | ||
| 389 | QOpenGLFramebufferObject* TextureCache::getShadowFramebufferObject() { | ||
| 390 | if (!_shadowFramebufferObject) { | ||
| 391 | const int SHADOW_MAP_SIZE = 2048; | ||
| 392 | _shadowFramebufferObject = new QOpenGLFramebufferObject(SHADOW_MAP_SIZE, SHADOW_MAP_SIZE, | ||
| 393 | QOpenGLFramebufferObject::NoAttachment, GL_TEXTURE_2D, GL_RGB); | ||
| 394 | |||
| 395 | glGenTextures(1, &_shadowDepthTextureID); | ||
| 396 | glBindTexture(GL_TEXTURE_2D, _shadowDepthTextureID); | ||
| 397 | glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, SHADOW_MAP_SIZE, SHADOW_MAP_SIZE, | ||
| 398 | 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0); | ||
| 399 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | ||
| 400 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | ||
| 401 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); | ||
| 402 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); | ||
| 403 | const float DISTANT_BORDER[] = { 1.0f, 1.0f, 1.0f, 1.0f }; | ||
| 404 | glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, DISTANT_BORDER); | ||
| 405 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE); | ||
| 406 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL); | ||
| 407 | glBindTexture(GL_TEXTURE_2D, 0); | ||
| 408 | |||
| 409 | _shadowFramebufferObject->bind(); | ||
| 410 | glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, _shadowDepthTextureID, 0); | ||
| 411 | _shadowFramebufferObject->release(); | ||
| 412 | } | ||
| 413 | return _shadowFramebufferObject; | ||
| 414 | } | ||
| 415 | |||
| 416 | GLuint TextureCache::getShadowDepthTextureID() { | 287 | GLuint TextureCache::getShadowDepthTextureID() { |
| 417 | // ensure that the shadow framebuffer object is initialized before returning the depth texture id | 288 | // ensure that the shadow framebuffer object is initialized before returning the depth texture id |
| 418 | //getShadowFramebufferObject(); | 289 | getShadowFramebuffer(); |
| 419 | _shadowDepthTextureID = gpu::GLBackend::getTextureID(_shadowTexture); | 290 | return gpu::GLBackend::getTextureID(_shadowTexture); |
| 420 | |||
| 421 | return _shadowDepthTextureID; | ||
| 422 | } | 291 | } |
| 423 | 292 | ||
| 424 | bool TextureCache::eventFilter(QObject* watched, QEvent* event) { | 293 | bool TextureCache::eventFilter(QObject* watched, QEvent* event) { |
| 425 | if (event->type() == QEvent::Resize) { | 294 | if (event->type() == QEvent::Resize) { |
| 426 | QSize size = static_cast<QResizeEvent*>(event)->size(); | 295 | QSize size = static_cast<QResizeEvent*>(event)->size(); |
| 427 | if (_primaryFramebufferObject && _primaryFramebufferObject->size() != size) { | 296 | if (_frameBufferSize != size) { |
| 428 | delete _primaryFramebufferObject; | 297 | _primaryFramebuffer.reset(); |
| 429 | _primaryFramebufferObject = NULL; | 298 | _primaryColorTexture.reset(); |
| 430 | glDeleteTextures(1, &_primaryDepthTextureID); | 299 | _primaryDepthTexture.reset(); |
| 431 | glDeleteTextures(1, &_primaryNormalTextureID); | 300 | _primaryNormalTexture.reset(); |
| 432 | glDeleteTextures(1, &_primarySpecularTextureID); | 301 | _primarySpecularTexture.reset(); |
| 433 | } | 302 | |
| 434 | if (_secondaryFramebufferObject && _secondaryFramebufferObject->size() != size) { | 303 | _secondaryFramebuffer.reset(); |
| 435 | delete _secondaryFramebufferObject; | 304 | |
| 436 | _secondaryFramebufferObject = NULL; | 305 | _tertiaryFramebuffer.reset(); |
| 437 | } | ||
| 438 | if (_tertiaryFramebufferObject && _tertiaryFramebufferObject->size() != size) { | ||
| 439 | delete _tertiaryFramebufferObject; | ||
| 440 | _tertiaryFramebufferObject = NULL; | ||
| 441 | } | 306 | } |
| 442 | } | 307 | } |
| 443 | return false; | 308 | return false; |
| @@ -458,17 +323,6 @@ void TextureCache::associateWithWidget(QGLWidget* widget) { · 14 unmodified lines | |||
| 458 | _associatedWidget->installEventFilter(this); | 323 | _associatedWidget->installEventFilter(this); |
| 459 | } | 324 | } |
| 460 | 325 | ||
| 461 | QOpenGLFramebufferObject* TextureCache::createFramebufferObject() { | ||
| 462 | QOpenGLFramebufferObject* fbo = new QOpenGLFramebufferObject(_frameBufferSize); | ||
| 463 | |||
| 464 | glBindTexture(GL_TEXTURE_2D, fbo->texture()); | ||
| 465 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | ||
| 466 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | ||
| 467 | glBindTexture(GL_TEXTURE_2D, 0); | ||
| 468 | |||
| 469 | return fbo; | ||
| 470 | } | ||
| 471 | |||
| 472 | Texture::Texture() { | 326 | Texture::Texture() { |
| 473 | } | 327 | } |
| 474 | 328 | ||