Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void ViewportSortParentSpritesSegtree10(ParentSpriteToSortVector *psdv)
- {
- if (psdv->size() < 2) return;
- auto t = getticks();
- struct Node {
- int32 x = INT32_MAX;
- int32 y = INT32_MAX;
- int32 z = INT32_MAX;
- Node() {}
- Node(int32 x, int32 y, int32 z)
- :x(x), y(y), z(z) {}
- };
- auto n = psdv->size();
- std::vector<Node> nodes(2 * n);
- uint32 next_order = 0;
- for (auto p = psdv->rbegin(); p != psdv->rend(); p++) {
- (*p)->order = next_order++;
- (*p)->comparison_done = false;
- (*p)->comparison_fin = false;
- }
- auto sprites = *psdv;
- std::stable_sort(sprites.begin(), sprites.end(), [](const ParentSpriteToDraw *a, const ParentSpriteToDraw *b) {
- // return std::tie(a->ymin, a->xmin, a->zmin) > std::tie(b->ymin, b->xmin, b->zmin);
- // return a->xmin + a->ymin + a->zmin < b->xmin + b->ymin + b->zmin;
- return a->ymin > b->ymin;
- });
- std::vector<size_t> sprite_order(n);
- sprite_order.reserve(2 * n);
- auto &v = *psdv;
- for (auto i = n; i < 2 * n; i++) {
- auto p = sprites[i - n];
- sprite_order[p->order] = i;
- auto &c = nodes[i];
- c.x = p->xmin;
- c.y = p->ymin;
- c.z = p->zmin;
- // this->nodes[i] = Node(p->xmin, p->ymin, p->zmin, p->order, p);
- }
- for(auto i = n - 1; i > 0; i--) {
- auto &a = nodes[i * 2];
- auto &b = nodes[i * 2 + 1];
- auto &c = nodes[i];
- c.x = min(a.x, b.x);
- c.y = min(a.y, b.y);
- c.z = min(a.z, b.z);
- // this->nodes[i] = Node(min(a.x, b.x), min(a.y, b.y), min(a.z, b.z), min(a.order, b.order));
- }
- std::vector<size_t> moving;
- std::stack<ParentSpriteToDraw *> compared;
- auto out = psdv->begin();
- std::queue<size_t> q;
- double s1=elapsed(getticks(), t), s2=0, s3 = 0, s4=0, s5=0, s6=0;
- int ii=0, jj=0, kk=0;
- while (!sprite_order.empty()) {
- t = getticks();
- // std::cout << "REMOVE" << *s << std::endl;
- auto i = sprite_order.back();
- auto s = sprites[i - n];
- if (s->comparison_fin) {
- sprite_order.pop_back();
- continue;
- }
- if (s->comparison_done) {
- *(out++) = s;
- s->comparison_fin = true;
- sprite_order.pop_back();
- continue;
- }
- auto &c = nodes[i];
- // this->nodes[index] = Node(x, y, z, order, nullptr);
- c.x = INT32_MAX;
- c.y = INT32_MAX;
- c.z = INT32_MAX;
- for(; i > 1; i >>= 1) {
- auto &a = nodes[i];
- auto &b = nodes[i ^ 1];
- auto x = min(a.x, b.x), y = min(a.y, b.y), z = min(a.z, b.z);
- auto &c = nodes[i >> 1];
- c.x = x;
- c.y = y;
- c.z = z;
- }
- s2 += elapsed(getticks(), t); t = getticks();
- ii++;
- moving.clear();
- // auto &a = nodes[1];
- // if (a.x <= s->xmax && a.y <= s->ymax && a.z <= s->zmax) {
- q.push(2);
- // }
- // std::cout << "QUERY " << *s << std::endl;
- while (!q.empty()) {
- auto i = q.front();
- q.pop();
- jj++;
- for (auto j = 0; j < 2; j++, i++) {
- auto &a = nodes[i];
- // std::cout << "TEST " << a.x << " " << a.y << " " << a.z << " " << !(a.x > s->xmax || a.y > s->ymax || a.z > s->zmax) << " " << i << std::endl;
- if (a.x > s->xmax || a.y > s->ymax || a.z > s->zmax) continue;
- if (i < n) {
- q.push(i * 2);
- continue;
- }
- // std::cout << "FOUND " << a.x << " " << a.y << " " << a.z << " " << i << std::endl;
- kk++;
- auto p = sprites[i - n];
- if (s->xmin <= p->xmax && // overlap in X?
- s->ymin <= p->ymax && // overlap in Y?
- s->zmin <= p->zmax) { // overlap in Z?
- if (s->xmin + s->xmax + s->ymin + s->ymax + s->zmin + s->zmax <=
- p->xmin + p->xmax + p->ymin + p->ymax + p->zmin + p->zmax) {
- continue;
- }
- }
- // __m128i s_min = LOAD_128((__m128i*) &s->xmin);
- // __m128i p_max = LOAD_128((__m128i*) &p->xmax);
- // __m128i r = _mm_cmplt_epi32(p_max, s_min);
- // if (_mm_testz_si128(mask_ptest, r)) {
- // if (s->xmin + s->xmax + s->ymin + s->ymax + s->zmin + s->zmax <=
- // p->xmin + p->xmax + p->ymin + p->ymax + p->zmin + p->zmax) {
- // continue;
- // }
- // }
- moving.push_back(i);
- }
- }
- s3 += elapsed(getticks(), t); t = getticks();
- if (moving.empty()) {
- *(out++) = s;
- s->comparison_fin = true;
- sprite_order.pop_back();
- continue;
- }
- s4 += elapsed(getticks(), t); t = getticks();
- std::sort(moving.begin(), moving.end(), [&sprites, n](size_t a, size_t b) {
- return sprites[a - n]->order > sprites[b - n]->order;
- });
- // std::cout << "SPLIT " << *s << " " << moving.size() << std::endl;
- // std::cout << "SPLIT " << *s << " " << moving.size() << " " << **moving.begin() << " " << **moving.rbegin() << std::endl;
- s->order = next_order++;
- s->comparison_done = true;
- for (auto i: moving) {
- auto p = sprites[i - n];
- p->order = next_order++;
- sprite_order.push_back(i);
- }
- s5 += elapsed(getticks(), t); t = getticks();
- }
- s6 += elapsed(getticks(), t); t = getticks();
- std::cout << "segtree10_T 1: " << s1 / 100000 << " 2: " << s2 / 100000 << " 3: " << s3 / 100000 << " 4: " << s4 / 100000 << " 5: " << s5 / 100000 << " 6: " << s6 / 100000 << std::endl;
- std::cout << "segtree10_i " << ii << " " << jj << " " << kk << std::endl;
- }
- segtree10_T 1: 38.0 2: 28.9 3: 164.6 4: 0.5 5: 1.0 6: 0.0
- segtree10_i 27940 670653 3135
- 24.3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement