Changeset View
Changeset View
Standalone View
Standalone View
libkwineffects/kwinglutils.cpp
Show First 20 Lines • Show All 1089 Lines • ▼ Show 20 Line(s) | |||||
1090 | { | 1090 | { | ||
1091 | if (s_renderTargets.isEmpty()) { | 1091 | if (s_renderTargets.isEmpty()) { | ||
1092 | glGetIntegerv(GL_VIEWPORT, s_virtualScreenViewport); | 1092 | glGetIntegerv(GL_VIEWPORT, s_virtualScreenViewport); | ||
1093 | } | 1093 | } | ||
1094 | target->enable(); | 1094 | target->enable(); | ||
1095 | s_renderTargets.push(target); | 1095 | s_renderTargets.push(target); | ||
1096 | } | 1096 | } | ||
1097 | 1097 | | |||
1098 | void GLRenderTarget::pushRenderTargets(QStack <GLRenderTarget*> targets) | ||||
1099 | { | ||||
1100 | if (s_renderTargets.isEmpty()) { | ||||
1101 | glGetIntegerv(GL_VIEWPORT, s_virtualScreenViewport); | ||||
1102 | s_renderTargets = targets; | ||||
1103 | } else { | ||||
1104 | s_renderTargets.reserve(s_renderTargets.size() + targets.size()); | ||||
fredrik: The reserve() call should be in the else {} clause. | |||||
1105 | | ||||
1106 | /* | ||||
1107 | * Merging the two stacks. | ||||
1108 | * We cheat a little bit by using the inherited QVector functions. | ||||
1109 | * This is to not have the targets stack in reverse order without | ||||
1110 | * having to use a helper QStack first to reverse the order. | ||||
1111 | */ | ||||
1112 | while (!targets.isEmpty()) { | ||||
1113 | s_renderTargets.push(targets.first()); | ||||
1114 | targets.removeFirst(); | ||||
1115 | } | ||||
You could also check if s_renderTargets is empty, and just do s_renderTargets = targets when that's the case. fredrik: You could also check if s_renderTargets is empty, and just do s_renderTargets = targets when… | |||||
1116 | } | ||||
1117 | | ||||
1118 | s_renderTargets.top()->enable(); | ||||
1119 | } | ||||
1120 | | ||||
1098 | GLRenderTarget* GLRenderTarget::popRenderTarget() | 1121 | GLRenderTarget* GLRenderTarget::popRenderTarget() | ||
1099 | { | 1122 | { | ||
1100 | GLRenderTarget* ret = s_renderTargets.pop(); | 1123 | GLRenderTarget* ret = s_renderTargets.pop(); | ||
1101 | ret->disable(); | 1124 | ret->setTextureDirty(); | ||
1102 | 1125 | | |||
1103 | if (!s_renderTargets.isEmpty()) { | 1126 | if (!s_renderTargets.isEmpty()) { | ||
1104 | s_renderTargets.top()->enable(); | 1127 | s_renderTargets.top()->enable(); | ||
1105 | } else { | 1128 | } else { | ||
1129 | ret->disable(); | ||||
1106 | glViewport (s_virtualScreenViewport[0], s_virtualScreenViewport[1], s_virtualScreenViewport[2], s_virtualScreenViewport[3]); | 1130 | glViewport (s_virtualScreenViewport[0], s_virtualScreenViewport[1], s_virtualScreenViewport[2], s_virtualScreenViewport[3]); | ||
1107 | } | 1131 | } | ||
1132 | | ||||
1108 | return ret; | 1133 | return ret; | ||
1109 | } | 1134 | } | ||
1110 | 1135 | | |||
1136 | GLRenderTarget::GLRenderTarget() | ||||
1137 | { | ||||
1138 | // Reset variables | ||||
1139 | mValid = false; | ||||
1140 | mTexture = GLTexture(); | ||||
1141 | } | ||||
1142 | | ||||
1111 | GLRenderTarget::GLRenderTarget(const GLTexture& color) | 1143 | GLRenderTarget::GLRenderTarget(const GLTexture& color) | ||
1112 | { | 1144 | { | ||
1113 | // Reset variables | 1145 | // Reset variables | ||
1114 | mValid = false; | 1146 | mValid = false; | ||
1115 | 1147 | | |||
1116 | mTexture = color; | 1148 | mTexture = color; | ||
1117 | 1149 | | |||
1118 | // Make sure FBO is supported | 1150 | // Make sure FBO is supported | ||
1119 | if (sSupported && !mTexture.isNull()) { | 1151 | if (sSupported && !mTexture.isNull()) { | ||
1120 | initFBO(); | 1152 | initFBO(); | ||
1121 | } else | 1153 | } else | ||
1122 | qCCritical(LIBKWINGLUTILS) << "Render targets aren't supported!"; | 1154 | qCCritical(LIBKWINGLUTILS) << "Render targets aren't supported!"; | ||
1123 | } | 1155 | } | ||
1124 | 1156 | | |||
1125 | GLRenderTarget::~GLRenderTarget() | 1157 | GLRenderTarget::~GLRenderTarget() | ||
1126 | { | 1158 | { | ||
1127 | if (mValid) { | 1159 | if (mValid) { | ||
1128 | glDeleteFramebuffers(1, &mFramebuffer); | 1160 | glDeleteFramebuffers(1, &mFramebuffer); | ||
1129 | } | 1161 | } | ||
1130 | } | 1162 | } | ||
1131 | 1163 | | |||
1132 | bool GLRenderTarget::enable() | 1164 | bool GLRenderTarget::enable() | ||
1133 | { | 1165 | { | ||
1166 | if (!mValid) { | ||||
1167 | initFBO(); | ||||
1168 | } | ||||
1169 | | ||||
1134 | if (!valid()) { | 1170 | if (!valid()) { | ||
1135 | qCCritical(LIBKWINGLUTILS) << "Can't enable invalid render target!"; | 1171 | qCCritical(LIBKWINGLUTILS) << "Can't enable invalid render target!"; | ||
1136 | return false; | 1172 | return false; | ||
1137 | } | 1173 | } | ||
1138 | 1174 | | |||
1139 | glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer); | 1175 | glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer); | ||
1140 | glViewport(0, 0, mTexture.width(), mTexture.height()); | 1176 | glViewport(0, 0, mTexture.width(), mTexture.height()); | ||
1141 | mTexture.setDirty(); | 1177 | mTexture.setDirty(); | ||
1142 | 1178 | | |||
1143 | return true; | 1179 | return true; | ||
1144 | } | 1180 | } | ||
1145 | 1181 | | |||
1146 | bool GLRenderTarget::disable() | 1182 | bool GLRenderTarget::disable() | ||
1147 | { | 1183 | { | ||
1184 | if (!mValid) { | ||||
1185 | initFBO(); | ||||
1186 | } | ||||
1187 | | ||||
1148 | if (!valid()) { | 1188 | if (!valid()) { | ||
1149 | qCCritical(LIBKWINGLUTILS) << "Can't disable invalid render target!"; | 1189 | qCCritical(LIBKWINGLUTILS) << "Can't disable invalid render target!"; | ||
1150 | return false; | 1190 | return false; | ||
1151 | } | 1191 | } | ||
1152 | 1192 | | |||
1153 | glBindFramebuffer(GL_FRAMEBUFFER, 0); | 1193 | glBindFramebuffer(GL_FRAMEBUFFER, 0); | ||
1154 | mTexture.setDirty(); | 1194 | mTexture.setDirty(); | ||
1155 | 1195 | | |||
▲ Show 20 Lines • Show All 88 Lines • ▼ Show 20 Line(s) | 1268 | #endif | |||
1244 | mValid = true; | 1284 | mValid = true; | ||
1245 | } | 1285 | } | ||
1246 | 1286 | | |||
1247 | void GLRenderTarget::blitFromFramebuffer(const QRect &source, const QRect &destination, GLenum filter) | 1287 | void GLRenderTarget::blitFromFramebuffer(const QRect &source, const QRect &destination, GLenum filter) | ||
1248 | { | 1288 | { | ||
1249 | if (!GLRenderTarget::blitSupported()) { | 1289 | if (!GLRenderTarget::blitSupported()) { | ||
1250 | return; | 1290 | return; | ||
1251 | } | 1291 | } | ||
1292 | | ||||
1293 | if (!mValid) { | ||||
1294 | initFBO(); | ||||
1295 | } | ||||
1296 | | ||||
1252 | GLRenderTarget::pushRenderTarget(this); | 1297 | GLRenderTarget::pushRenderTarget(this); | ||
1253 | glBindFramebuffer(GL_DRAW_FRAMEBUFFER, mFramebuffer); | 1298 | glBindFramebuffer(GL_DRAW_FRAMEBUFFER, mFramebuffer); | ||
1254 | glBindFramebuffer(GL_READ_FRAMEBUFFER, 0); | 1299 | glBindFramebuffer(GL_READ_FRAMEBUFFER, 0); | ||
1255 | const QRect s = source.isNull() ? s_virtualScreenGeometry : source; | 1300 | const QRect s = source.isNull() ? s_virtualScreenGeometry : source; | ||
1256 | const QRect d = destination.isNull() ? QRect(0, 0, mTexture.width(), mTexture.height()) : destination; | 1301 | const QRect d = destination.isNull() ? QRect(0, 0, mTexture.width(), mTexture.height()) : destination; | ||
1257 | 1302 | | |||
1258 | glBlitFramebuffer((s.x() - s_virtualScreenGeometry.x()) * s_virtualScreenScale, | 1303 | glBlitFramebuffer((s.x() - s_virtualScreenGeometry.x()) * s_virtualScreenScale, | ||
1259 | (s_virtualScreenGeometry.height() - s_virtualScreenGeometry.y() + s.y() - s.height()) * s_virtualScreenScale, | 1304 | (s_virtualScreenGeometry.height() - s_virtualScreenGeometry.y() + s.y() - s.height()) * s_virtualScreenScale, | ||
1260 | (s.x() - s_virtualScreenGeometry.x() + s.width()) * s_virtualScreenScale, | 1305 | (s.x() - s_virtualScreenGeometry.x() + s.width()) * s_virtualScreenScale, | ||
1261 | (s_virtualScreenGeometry.height() - s_virtualScreenGeometry.y() + s.y()) * s_virtualScreenScale, | 1306 | (s_virtualScreenGeometry.height() - s_virtualScreenGeometry.y() + s.y()) * s_virtualScreenScale, | ||
1262 | d.x(), mTexture.height() - d.y() - d.height(), d.x() + d.width(), mTexture.height() - d.y(), | 1307 | d.x(), mTexture.height() - d.y() - d.height(), d.x() + d.width(), mTexture.height() - d.y(), | ||
1263 | GL_COLOR_BUFFER_BIT, filter); | 1308 | GL_COLOR_BUFFER_BIT, filter); | ||
1264 | GLRenderTarget::popRenderTarget(); | 1309 | GLRenderTarget::popRenderTarget(); | ||
1265 | } | 1310 | } | ||
1266 | 1311 | | |||
1267 | void GLRenderTarget::attachTexture(const GLTexture& target) | 1312 | void GLRenderTarget::attachTexture(const GLTexture& target) | ||
1268 | { | 1313 | { | ||
1269 | if (!mValid || mTexture.texture() == target.texture()) { | 1314 | if (!mValid) { | ||
1315 | initFBO(); | ||||
1316 | } | ||||
1317 | | ||||
1318 | if (mTexture.texture() == target.texture()) { | ||||
1270 | return; | 1319 | return; | ||
1271 | } | 1320 | } | ||
1272 | 1321 | | |||
1273 | pushRenderTarget(this); | 1322 | pushRenderTarget(this); | ||
1274 | 1323 | | |||
1275 | mTexture = target; | 1324 | mTexture = target; | ||
1276 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, | 1325 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, | ||
1277 | mTexture.target(), mTexture.texture(), 0); | 1326 | mTexture.target(), mTexture.texture(), 0); | ||
1278 | 1327 | | |||
1279 | popRenderTarget(); | 1328 | popRenderTarget(); | ||
1280 | } | 1329 | } | ||
1281 | 1330 | | |||
1331 | void GLRenderTarget::detachTexture() | ||||
1332 | { | ||||
1333 | if (mTexture.isNull()) { | ||||
1334 | return; | ||||
1335 | } | ||||
1336 | | ||||
1337 | pushRenderTarget(this); | ||||
1338 | | ||||
1339 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, | ||||
1340 | mTexture.target(), 0, 0); | ||||
1341 | | ||||
1342 | popRenderTarget(); | ||||
1343 | } | ||||
1344 | | ||||
1282 | 1345 | | |||
1283 | // ------------------------------------------------------------------ | 1346 | // ------------------------------------------------------------------ | ||
1284 | 1347 | | |||
1285 | static const uint16_t indices[] = { | 1348 | static const uint16_t indices[] = { | ||
1286 | 1, 0, 3, 3, 2, 1, 5, 4, 7, 7, 6, 5, 9, 8, 11, 11, 10, 9, | 1349 | 1, 0, 3, 3, 2, 1, 5, 4, 7, 7, 6, 5, 9, 8, 11, 11, 10, 9, | ||
1287 | 13, 12, 15, 15, 14, 13, 17, 16, 19, 19, 18, 17, 21, 20, 23, 23, 22, 21, | 1350 | 13, 12, 15, 15, 14, 13, 17, 16, 19, 19, 18, 17, 21, 20, 23, 23, 22, 21, | ||
1288 | 25, 24, 27, 27, 26, 25, 29, 28, 31, 31, 30, 29, 33, 32, 35, 35, 34, 33, | 1351 | 25, 24, 27, 27, 26, 25, 29, 28, 31, 31, 30, 29, 33, 32, 35, 35, 34, 33, | ||
1289 | 37, 36, 39, 39, 38, 37, 41, 40, 43, 43, 42, 41, 45, 44, 47, 47, 46, 45, | 1352 | 37, 36, 39, 39, 38, 37, 41, 40, 43, 43, 42, 41, 45, 44, 47, 47, 46, 45, | ||
▲ Show 20 Lines • Show All 981 Lines • Show Last 20 Lines |
The reserve() call should be in the else {} clause.