| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // Copyright (C) 2016 Jolla Ltd, author: <gunnar.sletta@jollamobile.com> |
| 3 | // Copyright (C) 2016 Robin Burchell <robin.burchell@viroteck.net> |
| 4 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 5 | |
| 6 | #ifndef QSGBATCHRENDERER_P_H |
| 7 | #define QSGBATCHRENDERER_P_H |
| 8 | |
| 9 | // |
| 10 | // W A R N I N G |
| 11 | // ------------- |
| 12 | // |
| 13 | // This file is not part of the Qt API. It exists purely as an |
| 14 | // implementation detail. This header file may change from version to |
| 15 | // version without notice, or even be removed. |
| 16 | // |
| 17 | // We mean it. |
| 18 | // |
| 19 | |
| 20 | #include <private/qsgrenderer_p.h> |
| 21 | #include <private/qsgdefaultrendercontext_p.h> |
| 22 | #include <private/qsgnodeupdater_p.h> |
| 23 | #include <private/qsgrendernode_p.h> |
| 24 | #include <private/qdatabuffer_p.h> |
| 25 | #include <private/qsgtexture_p.h> |
| 26 | |
| 27 | #include <QtCore/QBitArray> |
| 28 | #include <QtCore/QStack> |
| 29 | |
| 30 | #include <rhi/qrhi.h> |
| 31 | |
| 32 | QT_BEGIN_NAMESPACE |
| 33 | |
| 34 | namespace QSGBatchRenderer |
| 35 | { |
| 36 | |
| 37 | #define QSG_RENDERER_COORD_LIMIT 1000000.0f |
| 38 | |
| 39 | struct Vec; |
| 40 | struct Rect; |
| 41 | struct Buffer; |
| 42 | struct Chunk; |
| 43 | struct Batch; |
| 44 | struct Node; |
| 45 | class Updater; |
| 46 | class Renderer; |
| 47 | class ShaderManager; |
| 48 | |
| 49 | template <typename Type, int PageSize> class AllocatorPage |
| 50 | { |
| 51 | public: |
| 52 | // The memory used by this allocator |
| 53 | char data[sizeof(Type) * PageSize]; |
| 54 | |
| 55 | // 'blocks' contains a list of free indices which can be allocated. |
| 56 | // The first available index is found in PageSize - available. |
| 57 | int blocks[PageSize]; |
| 58 | |
| 59 | // 'available' is the number of available instances this page has left to allocate. |
| 60 | int available; |
| 61 | |
| 62 | // This is not strictly needed, but useful for sanity checking and anyway |
| 63 | // pretty small.. |
| 64 | QBitArray allocated; |
| 65 | |
| 66 | AllocatorPage() |
| 67 | : available(PageSize) |
| 68 | , allocated(PageSize) |
| 69 | { |
| 70 | for (int i=0; i<PageSize; ++i) |
| 71 | blocks[i] = i; |
| 72 | |
| 73 | // Zero out all new pages. |
| 74 | memset(data, 0, sizeof(data)); |
| 75 | } |
| 76 | |
| 77 | const Type *at(uint index) const |
| 78 | { |
| 79 | return (Type *) &data[index * sizeof(Type)]; |
| 80 | } |
| 81 | |
| 82 | Type *at(uint index) |
| 83 | { |
| 84 | return (Type *) &data[index * sizeof(Type)]; |
| 85 | } |
| 86 | }; |
| 87 | |
| 88 | template <typename Type, int PageSize> class Allocator |
| 89 | { |
| 90 | public: |
| 91 | Allocator() |
| 92 | { |
| 93 | pages.push_back(new AllocatorPage<Type, PageSize>()); |
| 94 | } |
| 95 | |
| 96 | ~Allocator() |
| 97 | { |
| 98 | qDeleteAll(pages); |
| 99 | } |
| 100 | |
| 101 | Type *allocate() |
| 102 | { |
| 103 | AllocatorPage<Type, PageSize> *p = 0; |
| 104 | for (int i = m_freePage; i < pages.size(); i++) { |
| 105 | if (pages.at(i)->available > 0) { |
| 106 | p = pages.at(i); |
| 107 | m_freePage = i; |
| 108 | break; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // we couldn't find a free page from m_freePage to the last page. |
| 113 | // either there is no free pages, or there weren't any in the area we |
| 114 | // scanned: rescanning is expensive, so let's just assume there isn't |
| 115 | // one. when an item is released, we'll reset m_freePage anyway. |
| 116 | if (!p) { |
| 117 | p = new AllocatorPage<Type, PageSize>(); |
| 118 | m_freePage = pages.size(); |
| 119 | pages.push_back(p); |
| 120 | } |
| 121 | uint pos = p->blocks[PageSize - p->available]; |
| 122 | void *mem = p->at(pos); |
| 123 | p->available--; |
| 124 | p->allocated.setBit(pos); |
| 125 | Type *t = (Type*)mem; |
| 126 | return t; |
| 127 | } |
| 128 | |
| 129 | void releaseExplicit(uint pageIndex, uint index) |
| 130 | { |
| 131 | AllocatorPage<Type, PageSize> *page = pages.at(pageIndex); |
| 132 | if (!page->allocated.testBit(index)) |
| 133 | qFatal(msg: "Double delete in allocator: page=%d, index=%d" , pageIndex , index); |
| 134 | |
| 135 | // Zero this instance as we're done with it. |
| 136 | void *mem = page->at(index); |
| 137 | memset(s: mem, c: 0, n: sizeof(Type)); |
| 138 | |
| 139 | page->allocated[index] = false; |
| 140 | page->available++; |
| 141 | page->blocks[PageSize - page->available] = index; |
| 142 | |
| 143 | // Remove the pages if they are empty and they are the last ones. We need to keep the |
| 144 | // order of pages since we have references to their index, so we can only remove |
| 145 | // from the end. |
| 146 | while (page->available == PageSize && pages.size() > 1 && pages.back() == page) { |
| 147 | pages.pop_back(); |
| 148 | delete page; |
| 149 | page = pages.back(); |
| 150 | } |
| 151 | |
| 152 | // Reset the free page to force a scan for a new free point. |
| 153 | m_freePage = 0; |
| 154 | } |
| 155 | |
| 156 | void release(Type *t) |
| 157 | { |
| 158 | int pageIndex = -1; |
| 159 | for (int i=0; i<pages.size(); ++i) { |
| 160 | AllocatorPage<Type, PageSize> *p = pages.at(i); |
| 161 | if ((Type *) (&p->data[0]) <= t && (Type *) (&p->data[PageSize * sizeof(Type)]) > t) { |
| 162 | pageIndex = i; |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | Q_ASSERT(pageIndex >= 0); |
| 167 | |
| 168 | AllocatorPage<Type, PageSize> *page = pages.at(pageIndex); |
| 169 | int index = (quint64(t) - quint64(&page->data[0])) / sizeof(Type); |
| 170 | |
| 171 | releaseExplicit(pageIndex, index); |
| 172 | } |
| 173 | |
| 174 | QVector<AllocatorPage<Type, PageSize> *> pages; |
| 175 | int m_freePage = 0; |
| 176 | }; |
| 177 | |
| 178 | |
| 179 | inline bool hasMaterialWithBlending(QSGGeometryNode *n) |
| 180 | { |
| 181 | return (n->opaqueMaterial() ? n->opaqueMaterial()->flags() & QSGMaterial::Blending |
| 182 | : n->material()->flags() & QSGMaterial::Blending); |
| 183 | } |
| 184 | |
| 185 | struct Pt { |
| 186 | float x, y; |
| 187 | |
| 188 | void map(const QMatrix4x4 &mat) { |
| 189 | Pt r; |
| 190 | const float *m = mat.constData(); |
| 191 | r.x = x * m[0] + y * m[4] + m[12]; |
| 192 | r.y = x * m[1] + y * m[5] + m[13]; |
| 193 | x = r.x; |
| 194 | y = r.y; |
| 195 | } |
| 196 | |
| 197 | void set(float nx, float ny) { |
| 198 | x = nx; |
| 199 | y = ny; |
| 200 | } |
| 201 | }; |
| 202 | |
| 203 | inline QDebug operator << (QDebug d, const Pt &p) { |
| 204 | d << "Pt(" << p.x << p.y << ")" ; |
| 205 | return d; |
| 206 | } |
| 207 | |
| 208 | |
| 209 | |
| 210 | struct Rect { |
| 211 | Pt tl, br; // Top-Left (min) and Bottom-Right (max) |
| 212 | |
| 213 | void operator |= (const Pt &pt) { |
| 214 | if (pt.x < tl.x) |
| 215 | tl.x = pt.x; |
| 216 | if (pt.x > br.x) |
| 217 | br.x = pt.x; |
| 218 | if (pt.y < tl.y) |
| 219 | tl.y = pt.y; |
| 220 | if (pt.y > br.y) |
| 221 | br.y = pt.y; |
| 222 | } |
| 223 | |
| 224 | void operator |= (const Rect &r) { |
| 225 | if (r.tl.x < tl.x) |
| 226 | tl.x = r.tl.x; |
| 227 | if (r.tl.y < tl.y) |
| 228 | tl.y = r.tl.y; |
| 229 | if (r.br.x > br.x) |
| 230 | br.x = r.br.x; |
| 231 | if (r.br.y > br.y) |
| 232 | br.y = r.br.y; |
| 233 | } |
| 234 | |
| 235 | void map(const QMatrix4x4 &m); |
| 236 | |
| 237 | void set(float left, float top, float right, float bottom) { |
| 238 | tl.set(nx: left, ny: top); |
| 239 | br.set(nx: right, ny: bottom); |
| 240 | } |
| 241 | |
| 242 | bool intersects(const Rect &r) { |
| 243 | bool xOverlap = r.tl.x < br.x && r.br.x > tl.x; |
| 244 | bool yOverlap = r.tl.y < br.y && r.br.y > tl.y; |
| 245 | return xOverlap && yOverlap; |
| 246 | } |
| 247 | |
| 248 | bool isOutsideFloatRange() const { |
| 249 | return tl.x < -QSG_RENDERER_COORD_LIMIT |
| 250 | || tl.y < -QSG_RENDERER_COORD_LIMIT |
| 251 | || br.x > QSG_RENDERER_COORD_LIMIT |
| 252 | || br.y > QSG_RENDERER_COORD_LIMIT; |
| 253 | } |
| 254 | }; |
| 255 | |
| 256 | inline QDebug operator << (QDebug d, const Rect &r) { |
| 257 | d << "Rect(" << r.tl.x << r.tl.y << r.br.x << r.br.y << ")" ; |
| 258 | return d; |
| 259 | } |
| 260 | |
| 261 | struct Buffer { |
| 262 | quint32 size; |
| 263 | // Data is only valid while preparing the upload. Exception is if we are using the |
| 264 | // broken IBO workaround or we are using a visualization mode. |
| 265 | char *data; |
| 266 | QRhiBuffer *buf; |
| 267 | uint nonDynamicChangeCount; |
| 268 | }; |
| 269 | |
| 270 | struct Element { |
| 271 | Element() |
| 272 | : boundsComputed(false) |
| 273 | , boundsOutsideFloatRange(false) |
| 274 | , translateOnlyToRoot(false) |
| 275 | , removed(false) |
| 276 | , orphaned(false) |
| 277 | , isRenderNode(false) |
| 278 | , isMaterialBlended(false) |
| 279 | { |
| 280 | } |
| 281 | |
| 282 | void setNode(QSGGeometryNode *n) { |
| 283 | node = n; |
| 284 | isMaterialBlended = hasMaterialWithBlending(n); |
| 285 | } |
| 286 | |
| 287 | inline void ensureBoundsValid() { |
| 288 | if (!boundsComputed) |
| 289 | computeBounds(); |
| 290 | } |
| 291 | void computeBounds(); |
| 292 | |
| 293 | QSGGeometryNode *node = nullptr; |
| 294 | Batch *batch = nullptr; |
| 295 | Element *nextInBatch = nullptr; |
| 296 | Node *root = nullptr; |
| 297 | |
| 298 | Rect bounds; // in device coordinates |
| 299 | |
| 300 | int order = 0; |
| 301 | QRhiShaderResourceBindings *srb = nullptr; |
| 302 | QRhiGraphicsPipeline *ps = nullptr; |
| 303 | QRhiGraphicsPipeline *depthPostPassPs = nullptr; |
| 304 | |
| 305 | uint boundsComputed : 1; |
| 306 | uint boundsOutsideFloatRange : 1; |
| 307 | uint translateOnlyToRoot : 1; |
| 308 | uint removed : 1; |
| 309 | uint orphaned : 1; |
| 310 | uint isRenderNode : 1; |
| 311 | uint isMaterialBlended : 1; |
| 312 | }; |
| 313 | |
| 314 | struct RenderNodeElement : public Element { |
| 315 | |
| 316 | RenderNodeElement(QSGRenderNode *rn) |
| 317 | : renderNode(rn) |
| 318 | { |
| 319 | isRenderNode = true; |
| 320 | } |
| 321 | |
| 322 | QSGRenderNode *renderNode; |
| 323 | }; |
| 324 | |
| 325 | struct BatchRootInfo { |
| 326 | BatchRootInfo() {} |
| 327 | QSet<Node *> subRoots; |
| 328 | Node *parentRoot = nullptr; |
| 329 | int lastOrder = -1; |
| 330 | int firstOrder = -1; |
| 331 | int availableOrders = 0; |
| 332 | }; |
| 333 | |
| 334 | struct ClipBatchRootInfo : public BatchRootInfo |
| 335 | { |
| 336 | QMatrix4x4 matrix; |
| 337 | }; |
| 338 | |
| 339 | struct DrawSet |
| 340 | { |
| 341 | DrawSet(int v, int z, int i) |
| 342 | : vertices(v) |
| 343 | , zorders(z) |
| 344 | , indices(i) |
| 345 | { |
| 346 | } |
| 347 | DrawSet() {} |
| 348 | int vertices = 0; |
| 349 | int zorders = 0; |
| 350 | int indices = 0; |
| 351 | int indexCount = 0; |
| 352 | }; |
| 353 | |
| 354 | enum BatchCompatibility |
| 355 | { |
| 356 | BatchBreaksOnCompare, |
| 357 | BatchIsCompatible |
| 358 | }; |
| 359 | |
| 360 | struct ClipState |
| 361 | { |
| 362 | enum ClipTypeBit |
| 363 | { |
| 364 | NoClip = 0x00, |
| 365 | ScissorClip = 0x01, |
| 366 | StencilClip = 0x02 |
| 367 | }; |
| 368 | Q_DECLARE_FLAGS(ClipType, ClipTypeBit) |
| 369 | |
| 370 | const QSGClipNode *clipList; |
| 371 | ClipType type; |
| 372 | QRhiScissor scissor; |
| 373 | int stencilRef; |
| 374 | |
| 375 | inline void reset(); |
| 376 | }; |
| 377 | |
| 378 | struct StencilClipState |
| 379 | { |
| 380 | StencilClipState() : drawCalls(1) { } |
| 381 | |
| 382 | bool updateStencilBuffer = false; |
| 383 | QRhiShaderResourceBindings *srb = nullptr; |
| 384 | QRhiBuffer *vbuf = nullptr; |
| 385 | QRhiBuffer *ibuf = nullptr; |
| 386 | QRhiBuffer *ubuf = nullptr; |
| 387 | |
| 388 | struct StencilDrawCall { |
| 389 | int stencilRef; |
| 390 | int vertexCount; |
| 391 | int indexCount; |
| 392 | QRhiCommandBuffer::IndexFormat indexFormat; |
| 393 | quint32 vbufOffset; |
| 394 | quint32 ibufOffset; |
| 395 | quint32 ubufOffset; |
| 396 | }; |
| 397 | QDataBuffer<StencilDrawCall> drawCalls; |
| 398 | |
| 399 | inline void reset(); |
| 400 | }; |
| 401 | |
| 402 | struct Batch |
| 403 | { |
| 404 | Batch() : drawSets(1) {} |
| 405 | bool geometryWasChanged(QSGGeometryNode *gn); |
| 406 | BatchCompatibility isMaterialCompatible(Element *e) const; |
| 407 | void invalidate(); |
| 408 | void cleanupRemovedElements(); |
| 409 | |
| 410 | bool isTranslateOnlyToRoot() const; |
| 411 | bool isSafeToBatch() const; |
| 412 | |
| 413 | // pseudo-constructor... |
| 414 | void init() { |
| 415 | // Only non-reusable members are reset here. See Renderer::newBatch(). |
| 416 | first = nullptr; |
| 417 | root = nullptr; |
| 418 | vertexCount = 0; |
| 419 | indexCount = 0; |
| 420 | isOpaque = false; |
| 421 | needsUpload = false; |
| 422 | merged = false; |
| 423 | positionAttribute = -1; |
| 424 | uploadedThisFrame = false; |
| 425 | isRenderNode = false; |
| 426 | ubufDataValid = false; |
| 427 | needsPurge = false; |
| 428 | clipState.reset(); |
| 429 | blendConstant = QColor(); |
| 430 | } |
| 431 | |
| 432 | Element *first; |
| 433 | Node *root; |
| 434 | |
| 435 | int positionAttribute; |
| 436 | |
| 437 | int vertexCount; |
| 438 | int indexCount; |
| 439 | |
| 440 | int lastOrderInBatch; |
| 441 | |
| 442 | uint isOpaque : 1; |
| 443 | uint needsUpload : 1; |
| 444 | uint merged : 1; |
| 445 | uint isRenderNode : 1; |
| 446 | uint ubufDataValid : 1; |
| 447 | uint needsPurge : 1; |
| 448 | |
| 449 | mutable uint uploadedThisFrame : 1; // solely for debugging purposes |
| 450 | |
| 451 | Buffer vbo; |
| 452 | Buffer ibo; |
| 453 | QRhiBuffer *ubuf; |
| 454 | ClipState clipState; |
| 455 | StencilClipState stencilClipState; |
| 456 | QColor blendConstant; |
| 457 | |
| 458 | QDataBuffer<DrawSet> drawSets; |
| 459 | }; |
| 460 | |
| 461 | // NOTE: Node is zero-initialized by the Allocator. |
| 462 | struct Node |
| 463 | { |
| 464 | QSGNode *sgNode; |
| 465 | void *data; |
| 466 | |
| 467 | Node *m_parent; |
| 468 | Node *m_child; |
| 469 | Node *m_next; |
| 470 | Node *m_prev; |
| 471 | |
| 472 | Node *parent() const { return m_parent; } |
| 473 | |
| 474 | void append(Node *child) { |
| 475 | Q_ASSERT(child); |
| 476 | Q_ASSERT(!hasChild(child)); |
| 477 | Q_ASSERT(child->m_parent == nullptr); |
| 478 | Q_ASSERT(child->m_next == nullptr); |
| 479 | Q_ASSERT(child->m_prev == nullptr); |
| 480 | |
| 481 | if (!m_child) { |
| 482 | child->m_next = child; |
| 483 | child->m_prev = child; |
| 484 | m_child = child; |
| 485 | } else { |
| 486 | m_child->m_prev->m_next = child; |
| 487 | child->m_prev = m_child->m_prev; |
| 488 | m_child->m_prev = child; |
| 489 | child->m_next = m_child; |
| 490 | } |
| 491 | child->setParent(this); |
| 492 | } |
| 493 | |
| 494 | void remove(Node *child) { |
| 495 | Q_ASSERT(child); |
| 496 | Q_ASSERT(hasChild(child)); |
| 497 | |
| 498 | // only child.. |
| 499 | if (child->m_next == child) { |
| 500 | m_child = nullptr; |
| 501 | } else { |
| 502 | if (m_child == child) |
| 503 | m_child = child->m_next; |
| 504 | child->m_next->m_prev = child->m_prev; |
| 505 | child->m_prev->m_next = child->m_next; |
| 506 | } |
| 507 | child->m_next = nullptr; |
| 508 | child->m_prev = nullptr; |
| 509 | child->setParent(nullptr); |
| 510 | } |
| 511 | |
| 512 | Node *firstChild() const { return m_child; } |
| 513 | |
| 514 | Node *sibling() const { |
| 515 | Q_ASSERT(m_parent); |
| 516 | return m_next == m_parent->m_child ? nullptr : m_next; |
| 517 | } |
| 518 | |
| 519 | void setParent(Node *p) { |
| 520 | Q_ASSERT(m_parent == nullptr || p == nullptr); |
| 521 | m_parent = p; |
| 522 | } |
| 523 | |
| 524 | bool hasChild(Node *child) const { |
| 525 | Node *n = m_child; |
| 526 | while (n && n != child) |
| 527 | n = n->sibling(); |
| 528 | return n; |
| 529 | } |
| 530 | |
| 531 | |
| 532 | |
| 533 | QSGNode::DirtyState dirtyState; |
| 534 | |
| 535 | uint isOpaque : 1; |
| 536 | uint isBatchRoot : 1; |
| 537 | uint becameBatchRoot : 1; |
| 538 | |
| 539 | inline QSGNode::NodeType type() const { return sgNode->type(); } |
| 540 | |
| 541 | inline Element *element() const { |
| 542 | Q_ASSERT(sgNode->type() == QSGNode::GeometryNodeType); |
| 543 | return (Element *) data; |
| 544 | } |
| 545 | |
| 546 | inline RenderNodeElement *renderNodeElement() const { |
| 547 | Q_ASSERT(sgNode->type() == QSGNode::RenderNodeType); |
| 548 | return (RenderNodeElement *) data; |
| 549 | } |
| 550 | |
| 551 | inline ClipBatchRootInfo *clipInfo() const { |
| 552 | Q_ASSERT(sgNode->type() == QSGNode::ClipNodeType); |
| 553 | return (ClipBatchRootInfo *) data; |
| 554 | } |
| 555 | |
| 556 | inline BatchRootInfo *rootInfo() const { |
| 557 | Q_ASSERT(sgNode->type() == QSGNode::ClipNodeType |
| 558 | || (sgNode->type() == QSGNode::TransformNodeType && isBatchRoot)); |
| 559 | return (BatchRootInfo *) data; |
| 560 | } |
| 561 | }; |
| 562 | |
| 563 | class Updater : public QSGNodeUpdater |
| 564 | { |
| 565 | public: |
| 566 | Updater(Renderer *r); |
| 567 | |
| 568 | void visitOpacityNode(Node *n); |
| 569 | void visitTransformNode(Node *n); |
| 570 | void visitGeometryNode(Node *n); |
| 571 | void visitClipNode(Node *n); |
| 572 | void updateRootTransforms(Node *n); |
| 573 | void updateRootTransforms(Node *n, Node *root, const QMatrix4x4 &combined); |
| 574 | |
| 575 | void updateStates(QSGNode *n) override; |
| 576 | void visitNode(Node *n); |
| 577 | void registerWithParentRoot(QSGNode *subRoot, QSGNode *parentRoot); |
| 578 | |
| 579 | private: |
| 580 | Renderer *renderer; |
| 581 | |
| 582 | QDataBuffer<Node *> m_roots; |
| 583 | QDataBuffer<QMatrix4x4> m_rootMatrices; |
| 584 | |
| 585 | int m_added; |
| 586 | int m_transformChange; |
| 587 | int m_opacityChange; |
| 588 | |
| 589 | QMatrix4x4 m_identityMatrix; |
| 590 | }; |
| 591 | |
| 592 | struct GraphicsState |
| 593 | { |
| 594 | bool depthTest = false; |
| 595 | bool depthWrite = false; |
| 596 | QRhiGraphicsPipeline::CompareOp depthFunc = QRhiGraphicsPipeline::Less; |
| 597 | bool blending = false; |
| 598 | QRhiGraphicsPipeline::BlendFactor srcColor = QRhiGraphicsPipeline::One; |
| 599 | QRhiGraphicsPipeline::BlendFactor dstColor = QRhiGraphicsPipeline::OneMinusSrcAlpha; |
| 600 | QRhiGraphicsPipeline::BlendFactor srcAlpha = QRhiGraphicsPipeline::One; |
| 601 | QRhiGraphicsPipeline::BlendFactor dstAlpha = QRhiGraphicsPipeline::OneMinusSrcAlpha; |
| 602 | QRhiGraphicsPipeline::BlendOp opColor = QRhiGraphicsPipeline::Add; |
| 603 | QRhiGraphicsPipeline::BlendOp opAlpha = QRhiGraphicsPipeline::Add; |
| 604 | QRhiGraphicsPipeline::ColorMask colorWrite = QRhiGraphicsPipeline::ColorMask(0xF); |
| 605 | QRhiGraphicsPipeline::CullMode cullMode = QRhiGraphicsPipeline::None; |
| 606 | bool usesScissor = false; |
| 607 | bool stencilTest = false; |
| 608 | int sampleCount = 1; |
| 609 | QSGGeometry::DrawingMode drawMode = QSGGeometry::DrawTriangles; |
| 610 | float lineWidth = 1.0f; |
| 611 | QRhiGraphicsPipeline::PolygonMode polygonMode = QRhiGraphicsPipeline::Fill; |
| 612 | int multiViewCount = 0; |
| 613 | }; |
| 614 | |
| 615 | bool operator==(const GraphicsState &a, const GraphicsState &b) noexcept; |
| 616 | bool operator!=(const GraphicsState &a, const GraphicsState &b) noexcept; |
| 617 | size_t qHash(const GraphicsState &s, size_t seed = 0) noexcept; |
| 618 | |
| 619 | struct ShaderManagerShader; |
| 620 | |
| 621 | struct GraphicsPipelineStateKey |
| 622 | { |
| 623 | GraphicsState state; |
| 624 | const ShaderManagerShader *sms; |
| 625 | QVector<quint32> renderTargetDescription; |
| 626 | QVector<quint32> srbLayoutDescription; |
| 627 | struct { |
| 628 | size_t renderTargetDescriptionHash; |
| 629 | size_t srbLayoutDescriptionHash; |
| 630 | } ; |
| 631 | static GraphicsPipelineStateKey create(const GraphicsState &state, |
| 632 | const ShaderManagerShader *sms, |
| 633 | const QRhiRenderPassDescriptor *rpDesc, |
| 634 | const QRhiShaderResourceBindings *srb) |
| 635 | { |
| 636 | const QVector<quint32> rtDesc = rpDesc->serializedFormat(); |
| 637 | const QVector<quint32> srbDesc = srb->serializedLayoutDescription(); |
| 638 | return { .state: state, .sms: sms, .renderTargetDescription: rtDesc, .srbLayoutDescription: srbDesc, .extra: { .renderTargetDescriptionHash: qHash(key: rtDesc), .srbLayoutDescriptionHash: qHash(key: srbDesc) } }; |
| 639 | } |
| 640 | }; |
| 641 | |
| 642 | bool operator==(const GraphicsPipelineStateKey &a, const GraphicsPipelineStateKey &b) noexcept; |
| 643 | bool operator!=(const GraphicsPipelineStateKey &a, const GraphicsPipelineStateKey &b) noexcept; |
| 644 | size_t qHash(const GraphicsPipelineStateKey &k, size_t seed = 0) noexcept; |
| 645 | |
| 646 | struct ShaderKey |
| 647 | { |
| 648 | QSGMaterialType *type; |
| 649 | QSGRendererInterface::RenderMode renderMode; |
| 650 | int multiViewCount; |
| 651 | }; |
| 652 | |
| 653 | bool operator==(const ShaderKey &a, const ShaderKey &b) noexcept; |
| 654 | bool operator!=(const ShaderKey &a, const ShaderKey &b) noexcept; |
| 655 | size_t qHash(const ShaderKey &k, size_t seed = 0) noexcept; |
| 656 | |
| 657 | struct ShaderManagerShader |
| 658 | { |
| 659 | ~ShaderManagerShader() { |
| 660 | delete materialShader; |
| 661 | } |
| 662 | QSGMaterialShader *materialShader = nullptr; |
| 663 | QRhiVertexInputLayout inputLayout; |
| 664 | QVarLengthArray<QRhiShaderStage, 2> stages; |
| 665 | float lastOpacity; |
| 666 | }; |
| 667 | |
| 668 | class ShaderManager : public QObject |
| 669 | { |
| 670 | Q_OBJECT |
| 671 | public: |
| 672 | using Shader = ShaderManagerShader; |
| 673 | |
| 674 | ShaderManager(QSGDefaultRenderContext *ctx) : context(ctx) { } |
| 675 | ~ShaderManager() { |
| 676 | qDeleteAll(c: rewrittenShaders); |
| 677 | qDeleteAll(c: stockShaders); |
| 678 | } |
| 679 | |
| 680 | void clearCachedRendererData(); |
| 681 | |
| 682 | QHash<GraphicsPipelineStateKey, QRhiGraphicsPipeline *> pipelineCache; |
| 683 | |
| 684 | QMultiHash<QVector<quint32>, QRhiShaderResourceBindings *> srbPool; |
| 685 | QVector<quint32> srbLayoutDescSerializeWorkspace; |
| 686 | |
| 687 | public Q_SLOTS: |
| 688 | void invalidated(); |
| 689 | |
| 690 | public: |
| 691 | Shader *prepareMaterial(QSGMaterial *material, |
| 692 | const QSGGeometry *geometry = nullptr, |
| 693 | QSGRendererInterface::RenderMode renderMode = QSGRendererInterface::RenderMode2D, |
| 694 | int multiViewCount = 0); |
| 695 | Shader *prepareMaterialNoRewrite(QSGMaterial *material, |
| 696 | const QSGGeometry *geometry = nullptr, |
| 697 | QSGRendererInterface::RenderMode renderMode = QSGRendererInterface::RenderMode2D, |
| 698 | int multiViewCount = 0); |
| 699 | |
| 700 | private: |
| 701 | QHash<ShaderKey, Shader *> rewrittenShaders; |
| 702 | QHash<ShaderKey, Shader *> stockShaders; |
| 703 | |
| 704 | QSGDefaultRenderContext *context; |
| 705 | }; |
| 706 | |
| 707 | struct RenderPassState |
| 708 | { |
| 709 | QRhiViewport viewport; |
| 710 | QColor clearColor; |
| 711 | QRhiDepthStencilClearValue dsClear; |
| 712 | bool viewportSet; |
| 713 | bool scissorSet; |
| 714 | }; |
| 715 | |
| 716 | class Visualizer |
| 717 | { |
| 718 | public: |
| 719 | enum VisualizeMode { |
| 720 | VisualizeNothing, |
| 721 | VisualizeBatches, |
| 722 | VisualizeClipping, |
| 723 | VisualizeChanges, |
| 724 | VisualizeOverdraw |
| 725 | }; |
| 726 | |
| 727 | Visualizer(Renderer *renderer); |
| 728 | virtual ~Visualizer(); |
| 729 | |
| 730 | VisualizeMode mode() const { return m_visualizeMode; } |
| 731 | void setMode(VisualizeMode mode) { m_visualizeMode = mode; } |
| 732 | |
| 733 | virtual void visualizeChangesPrepare(Node *n, uint parentChanges = 0); |
| 734 | virtual void prepareVisualize() = 0; |
| 735 | virtual void visualize() = 0; |
| 736 | |
| 737 | virtual void releaseResources() = 0; |
| 738 | |
| 739 | protected: |
| 740 | Renderer *m_renderer; |
| 741 | VisualizeMode m_visualizeMode; |
| 742 | QHash<Node *, uint> m_visualizeChangeSet; |
| 743 | }; |
| 744 | |
| 745 | class Q_QUICK_EXPORT Renderer : public QSGRenderer |
| 746 | { |
| 747 | public: |
| 748 | Renderer(QSGDefaultRenderContext *ctx, QSGRendererInterface::RenderMode renderMode = QSGRendererInterface::RenderMode2D); |
| 749 | ~Renderer(); |
| 750 | |
| 751 | protected: |
| 752 | void nodeChanged(QSGNode *node, QSGNode::DirtyState state) override; |
| 753 | void render() override; |
| 754 | void prepareInline() override; |
| 755 | void renderInline() override; |
| 756 | void releaseCachedResources() override; |
| 757 | |
| 758 | struct PreparedRenderBatch { |
| 759 | const Batch *batch; |
| 760 | ShaderManager::Shader *sms; |
| 761 | }; |
| 762 | |
| 763 | struct RenderPassContext { |
| 764 | bool valid = false; |
| 765 | QVarLengthArray<PreparedRenderBatch, 64> opaqueRenderBatches; |
| 766 | QVarLengthArray<PreparedRenderBatch, 64> alphaRenderBatches; |
| 767 | QElapsedTimer timer; |
| 768 | quint64 timeRenderLists; |
| 769 | quint64 timePrepareOpaque; |
| 770 | quint64 timePrepareAlpha; |
| 771 | quint64 timeSorting; |
| 772 | quint64 timeUploadOpaque; |
| 773 | quint64 timeUploadAlpha; |
| 774 | }; |
| 775 | |
| 776 | // update batches and queue and commit rhi resource updates |
| 777 | void prepareRenderPass(RenderPassContext *ctx); |
| 778 | // records the beginPass() |
| 779 | void beginRenderPass(RenderPassContext *ctx); |
| 780 | // records the draw calls, must be preceded by a prepareRenderPass at minimum, |
| 781 | // and also surrounded by begin/endRenderPass unless we are recording inside an |
| 782 | // already started pass. |
| 783 | void recordRenderPass(RenderPassContext *ctx); |
| 784 | // does visualizing if enabled and records the endPass() |
| 785 | void endRenderPass(RenderPassContext *ctx); |
| 786 | |
| 787 | private: |
| 788 | enum RebuildFlag { |
| 789 | BuildRenderListsForTaggedRoots = 0x0001, |
| 790 | BuildRenderLists = 0x0002, |
| 791 | BuildBatches = 0x0004, |
| 792 | FullRebuild = 0xffff |
| 793 | }; |
| 794 | |
| 795 | friend class Updater; |
| 796 | friend class RhiVisualizer; |
| 797 | |
| 798 | void destroyGraphicsResources(); |
| 799 | void map(Buffer *buffer, quint32 byteSize, bool isIndexBuf = false); |
| 800 | void unmap(Buffer *buffer, bool isIndexBuf = false); |
| 801 | |
| 802 | void buildRenderListsFromScratch(); |
| 803 | void buildRenderListsForTaggedRoots(); |
| 804 | void tagSubRoots(Node *node); |
| 805 | void buildRenderLists(QSGNode *node); |
| 806 | |
| 807 | void deleteRemovedElements(); |
| 808 | void cleanupBatches(QDataBuffer<Batch *> *batches); |
| 809 | void prepareOpaqueBatches(); |
| 810 | bool checkOverlap(int first, int last, const Rect &bounds); |
| 811 | void prepareAlphaBatches(); |
| 812 | void invalidateBatchAndOverlappingRenderOrders(Batch *batch); |
| 813 | |
| 814 | void uploadBatch(Batch *b); |
| 815 | void uploadMergedElement(Element *e, int vaOffset, char **vertexData, char **zData, char **indexData, void *iBasePtr, int *indexCount); |
| 816 | |
| 817 | bool ensurePipelineState(Element *e, const ShaderManager::Shader *sms, bool depthPostPass = false); |
| 818 | QRhiTexture *dummyTexture(); |
| 819 | void updateMaterialDynamicData(ShaderManager::Shader *sms, QSGMaterialShader::RenderState &renderState, |
| 820 | QSGMaterial *material, const Batch *batch, Element *e, int ubufOffset, int ubufRegionSize, |
| 821 | char *directUpdatePtr); |
| 822 | void updateMaterialStaticData(ShaderManager::Shader *sms, QSGMaterialShader::RenderState &renderState, |
| 823 | QSGMaterial *material, Batch *batch, bool *gstateChanged); |
| 824 | void checkLineWidth(QSGGeometry *g); |
| 825 | bool prepareRenderMergedBatch(Batch *batch, PreparedRenderBatch *renderBatch); |
| 826 | void renderMergedBatch(PreparedRenderBatch *renderBatch, bool depthPostPass = false); |
| 827 | bool prepareRenderUnmergedBatch(Batch *batch, PreparedRenderBatch *renderBatch); |
| 828 | void renderUnmergedBatch(PreparedRenderBatch *renderBatch, bool depthPostPass = false); |
| 829 | void setGraphicsPipeline(QRhiCommandBuffer *cb, const Batch *batch, Element *e, bool depthPostPass = false); |
| 830 | ClipState::ClipType updateStencilClip(const QSGClipNode *clip); |
| 831 | void updateClip(const QSGClipNode *clipList, const Batch *batch); |
| 832 | void applyClipStateToGraphicsState(); |
| 833 | QRhiGraphicsPipeline *buildStencilPipeline(const Batch *batch, bool firstStencilClipInBatch); |
| 834 | void updateClipState(const QSGClipNode *clipList, Batch *batch); |
| 835 | void enqueueStencilDraw(const Batch *batch); |
| 836 | const QMatrix4x4 &matrixForRoot(Node *node); |
| 837 | void renderRenderNode(Batch *batch); |
| 838 | bool prepareRhiRenderNode(Batch *batch, PreparedRenderBatch *renderBatch); |
| 839 | void renderRhiRenderNode(const Batch *batch); |
| 840 | void setActiveShader(QSGMaterialShader *program, ShaderManager::Shader *shader); |
| 841 | void setActiveRhiShader(QSGMaterialShader *program, ShaderManager::Shader *shader); |
| 842 | |
| 843 | bool changeBatchRoot(Node *node, Node *newRoot); |
| 844 | void registerBatchRoot(Node *childRoot, Node *parentRoot); |
| 845 | void removeBatchRootFromParent(Node *childRoot); |
| 846 | void nodeChangedBatchRoot(Node *node, Node *root); |
| 847 | void turnNodeIntoBatchRoot(Node *node); |
| 848 | void nodeWasTransformed(Node *node, int *vertexCount); |
| 849 | void nodeWasRemoved(Node *node); |
| 850 | void nodeWasAdded(QSGNode *node, Node *shadowParent); |
| 851 | BatchRootInfo *batchRootInfo(Node *node); |
| 852 | void updateLineWidth(QSGGeometry *g); |
| 853 | |
| 854 | inline Batch *newBatch(); |
| 855 | void invalidateAndRecycleBatch(Batch *b); |
| 856 | void releaseElement(Element *e, bool inDestructor = false); |
| 857 | |
| 858 | void setVisualizationMode(const QByteArray &mode) override; |
| 859 | bool hasVisualizationModeWithContinuousUpdate() const override; |
| 860 | |
| 861 | QSGDefaultRenderContext *m_context; |
| 862 | QSGRendererInterface::RenderMode m_renderMode; |
| 863 | QSet<Node *> m_taggedRoots; |
| 864 | QDataBuffer<Element *> m_opaqueRenderList; |
| 865 | QDataBuffer<Element *> m_alphaRenderList; |
| 866 | int m_nextRenderOrder; |
| 867 | bool m_partialRebuild; |
| 868 | QSGNode *m_partialRebuildRoot; |
| 869 | bool m_forceNoDepthBuffer; |
| 870 | |
| 871 | QHash<QSGRenderNode *, RenderNodeElement *> m_renderNodeElements; |
| 872 | QDataBuffer<Batch *> m_opaqueBatches; |
| 873 | QDataBuffer<Batch *> m_alphaBatches; |
| 874 | QHash<QSGNode *, Node *> m_nodes; |
| 875 | |
| 876 | QDataBuffer<Batch *> m_batchPool; |
| 877 | QDataBuffer<Element *> m_elementsToDelete; |
| 878 | QDataBuffer<Element *> m_tmpAlphaElements; |
| 879 | QDataBuffer<Element *> m_tmpOpaqueElements; |
| 880 | |
| 881 | QDataBuffer<QRhiBuffer *> m_vboPool; |
| 882 | QDataBuffer<QRhiBuffer *> m_iboPool; |
| 883 | quint32 m_vboPoolCost; |
| 884 | quint32 m_iboPoolCost; |
| 885 | |
| 886 | uint m_rebuild; |
| 887 | qreal m_zRange; |
| 888 | #if defined(QSGBATCHRENDERER_INVALIDATE_WEDGED_NODES) |
| 889 | int m_renderOrderRebuildLower; |
| 890 | int m_renderOrderRebuildUpper; |
| 891 | #endif |
| 892 | |
| 893 | int m_batchNodeThreshold; |
| 894 | int m_batchVertexThreshold; |
| 895 | int m_srbPoolThreshold; |
| 896 | int m_bufferPoolSizeLimit; |
| 897 | |
| 898 | Visualizer *m_visualizer; |
| 899 | |
| 900 | ShaderManager *m_shaderManager; // per rendercontext, shared |
| 901 | QSGMaterial *m_currentMaterial; |
| 902 | QSGMaterialShader *m_currentProgram; |
| 903 | ShaderManager::Shader *m_currentShader; |
| 904 | ClipState m_currentClipState; |
| 905 | |
| 906 | QDataBuffer<char> m_vertexUploadPool; |
| 907 | QDataBuffer<char> m_indexUploadPool; |
| 908 | |
| 909 | Allocator<Node, 256> m_nodeAllocator; |
| 910 | Allocator<Element, 64> m_elementAllocator; |
| 911 | |
| 912 | RenderPassContext m_mainRenderPassContext; |
| 913 | QRhiResourceUpdateBatch *m_resourceUpdates = nullptr; |
| 914 | uint m_ubufAlignment; |
| 915 | bool m_uint32IndexForRhi; |
| 916 | GraphicsState m_gstate; |
| 917 | RenderPassState m_pstate; |
| 918 | QStack<GraphicsState> m_gstateStack; |
| 919 | QHash<QSGSamplerDescription, QRhiSampler *> m_samplers; |
| 920 | QRhiTexture *m_dummyTexture = nullptr; |
| 921 | |
| 922 | struct StencilClipCommonData { |
| 923 | QRhiGraphicsPipeline *replacePs = nullptr; |
| 924 | QRhiGraphicsPipeline *incrPs = nullptr; |
| 925 | QShader vs; |
| 926 | QShader fs; |
| 927 | QRhiVertexInputLayout inputLayout; |
| 928 | QRhiGraphicsPipeline::Topology topology; |
| 929 | inline void reset(); |
| 930 | } m_stencilClipCommon; |
| 931 | |
| 932 | inline int mergedIndexElemSize() const; |
| 933 | inline bool useDepthBuffer() const; |
| 934 | inline void setStateForDepthPostPass(); |
| 935 | }; |
| 936 | |
| 937 | Batch *Renderer::newBatch() |
| 938 | { |
| 939 | Batch *b; |
| 940 | int size = m_batchPool.size(); |
| 941 | if (size) { |
| 942 | b = m_batchPool.at(i: size - 1); |
| 943 | // vbo, ibo, ubuf, stencil-related buffers are reused |
| 944 | m_batchPool.resize(size: size - 1); |
| 945 | } else { |
| 946 | b = new Batch(); |
| 947 | Q_ASSERT(offsetof(Batch, ibo) == sizeof(Buffer) + offsetof(Batch, vbo)); |
| 948 | memset(s: &b->vbo, c: 0, n: sizeof(Buffer) * 2); // Clear VBO & IBO |
| 949 | b->ubuf = nullptr; |
| 950 | b->stencilClipState.reset(); |
| 951 | } |
| 952 | // initialize (when new batch) or reset (when reusing a batch) the non-reusable fields |
| 953 | b->init(); |
| 954 | return b; |
| 955 | } |
| 956 | |
| 957 | int Renderer::mergedIndexElemSize() const |
| 958 | { |
| 959 | return m_uint32IndexForRhi ? sizeof(quint32) : sizeof(quint16); |
| 960 | } |
| 961 | |
| 962 | // "use" here means that both depth test and write is wanted (the latter for |
| 963 | // opaque batches only). Therefore neither RenderMode2DNoDepthBuffer nor |
| 964 | // RenderMode3D must result in true. So while RenderMode3D requires a depth |
| 965 | // buffer, this here must say false. In addition, m_forceNoDepthBuffer is a |
| 966 | // dynamic override relevant with QSGRenderNode. |
| 967 | // |
| 968 | bool Renderer::useDepthBuffer() const |
| 969 | { |
| 970 | return !m_forceNoDepthBuffer && m_renderMode == QSGRendererInterface::RenderMode2D; |
| 971 | } |
| 972 | |
| 973 | void Renderer::setStateForDepthPostPass() |
| 974 | { |
| 975 | m_gstate.colorWrite = {}; |
| 976 | m_gstate.depthWrite = true; |
| 977 | m_gstate.depthTest = true; |
| 978 | m_gstate.depthFunc = QRhiGraphicsPipeline::Less; |
| 979 | } |
| 980 | |
| 981 | void Renderer::StencilClipCommonData::reset() |
| 982 | { |
| 983 | delete replacePs; |
| 984 | replacePs = nullptr; |
| 985 | |
| 986 | delete incrPs; |
| 987 | incrPs = nullptr; |
| 988 | |
| 989 | vs = QShader(); |
| 990 | fs = QShader(); |
| 991 | } |
| 992 | |
| 993 | void ClipState::reset() |
| 994 | { |
| 995 | clipList = nullptr; |
| 996 | type = NoClip; |
| 997 | stencilRef = 0; |
| 998 | } |
| 999 | |
| 1000 | void StencilClipState::reset() |
| 1001 | { |
| 1002 | updateStencilBuffer = false; |
| 1003 | |
| 1004 | delete srb; |
| 1005 | srb = nullptr; |
| 1006 | |
| 1007 | delete vbuf; |
| 1008 | vbuf = nullptr; |
| 1009 | |
| 1010 | delete ibuf; |
| 1011 | ibuf = nullptr; |
| 1012 | |
| 1013 | delete ubuf; |
| 1014 | ubuf = nullptr; |
| 1015 | |
| 1016 | drawCalls.reset(); |
| 1017 | } |
| 1018 | |
| 1019 | } |
| 1020 | |
| 1021 | Q_DECLARE_TYPEINFO(QSGBatchRenderer::GraphicsState, Q_RELOCATABLE_TYPE); |
| 1022 | Q_DECLARE_TYPEINFO(QSGBatchRenderer::GraphicsPipelineStateKey, Q_RELOCATABLE_TYPE); |
| 1023 | Q_DECLARE_TYPEINFO(QSGBatchRenderer::RenderPassState, Q_RELOCATABLE_TYPE); |
| 1024 | Q_DECLARE_TYPEINFO(QSGBatchRenderer::DrawSet, Q_PRIMITIVE_TYPE); |
| 1025 | |
| 1026 | QT_END_NAMESPACE |
| 1027 | |
| 1028 | #endif // QSGBATCHRENDERER_P_H |
| 1029 | |