2022-01-13 17:26:59 +00:00
|
|
|
// Copyright (c) 2022- PPSSPP Project.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official git repository and contact information can be found at
|
|
|
|
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2022-01-15 04:19:32 +00:00
|
|
|
#include <atomic>
|
2022-01-16 17:07:33 +00:00
|
|
|
#include <unordered_map>
|
2022-01-13 17:26:59 +00:00
|
|
|
#include "GPU/Software/Rasterizer.h"
|
|
|
|
|
2022-01-14 06:17:12 +00:00
|
|
|
struct BinWaitable;
|
2022-01-16 17:30:02 +00:00
|
|
|
class DrawBinItemsTask;
|
2022-01-14 06:17:12 +00:00
|
|
|
|
2022-01-14 03:07:41 +00:00
|
|
|
enum class BinItemType {
|
|
|
|
TRIANGLE,
|
|
|
|
CLEAR_RECT,
|
2022-02-09 07:11:12 +00:00
|
|
|
RECT,
|
2022-01-14 03:07:41 +00:00
|
|
|
SPRITE,
|
|
|
|
LINE,
|
|
|
|
POINT,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct BinCoords {
|
|
|
|
int x1;
|
|
|
|
int y1;
|
|
|
|
int x2;
|
|
|
|
int y2;
|
2022-01-14 06:17:12 +00:00
|
|
|
|
|
|
|
bool Invalid() const {
|
|
|
|
return x2 < x1 || y2 < y1;
|
|
|
|
}
|
|
|
|
|
|
|
|
BinCoords Intersect(const BinCoords &range) const;
|
2022-01-14 03:07:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct BinItem {
|
|
|
|
BinItemType type;
|
|
|
|
int stateIndex;
|
|
|
|
BinCoords range;
|
|
|
|
VertexData v0;
|
|
|
|
VertexData v1;
|
|
|
|
VertexData v2;
|
|
|
|
};
|
|
|
|
|
2022-01-14 06:17:12 +00:00
|
|
|
template <typename T, size_t N>
|
|
|
|
struct BinQueue {
|
|
|
|
BinQueue() {
|
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
~BinQueue() {
|
2022-03-13 20:33:19 +00:00
|
|
|
FreeAlignedMemory(items_);
|
2022-01-14 06:17:12 +00:00
|
|
|
}
|
|
|
|
|
2022-01-16 19:49:41 +00:00
|
|
|
void Setup() {
|
2022-03-13 20:33:19 +00:00
|
|
|
items_ = (T *)AllocateAlignedMemory(sizeof(T) * N, 16);
|
2022-01-16 19:49:41 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 06:17:12 +00:00
|
|
|
void Reset() {
|
|
|
|
head_ = 0;
|
|
|
|
tail_ = 0;
|
|
|
|
size_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t Push(const T &item) {
|
|
|
|
size_t i = tail_++;
|
2022-01-15 08:02:31 +00:00
|
|
|
if (i + 1 == N)
|
|
|
|
tail_ -= N;
|
2022-01-14 06:17:12 +00:00
|
|
|
items_[i] = item;
|
2022-01-15 04:19:32 +00:00
|
|
|
size_++;
|
2022-01-14 06:17:12 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
T Pop() {
|
|
|
|
size_t i = head_++;
|
2022-01-15 08:02:31 +00:00
|
|
|
if (i + 1 == N)
|
|
|
|
head_ -= N;
|
2022-01-15 04:19:32 +00:00
|
|
|
T item = items_[i];
|
2022-01-14 06:17:12 +00:00
|
|
|
size_--;
|
2022-01-15 04:19:32 +00:00
|
|
|
return item;
|
2022-01-14 06:17:12 +00:00
|
|
|
}
|
|
|
|
|
2022-01-16 02:43:44 +00:00
|
|
|
// Only safe if you're the only one reading.
|
|
|
|
T &PeekNext() {
|
|
|
|
return items_[head_];
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkipNext() {
|
|
|
|
size_t i = head_++;
|
|
|
|
if (i + 1 == N)
|
|
|
|
head_ -= N;
|
|
|
|
size_--;
|
|
|
|
}
|
|
|
|
|
2022-01-21 02:17:23 +00:00
|
|
|
// Only safe if you're the only one reading.
|
|
|
|
const T &Peek(size_t offset) const {
|
|
|
|
size_t i = head_ + offset;
|
|
|
|
if (i >= N)
|
|
|
|
i -= N;
|
|
|
|
return items_[i];
|
|
|
|
}
|
|
|
|
|
2022-01-16 02:43:44 +00:00
|
|
|
// Only safe if you're the only one writing.
|
|
|
|
T &PeekPush() {
|
|
|
|
return items_[tail_];
|
|
|
|
}
|
|
|
|
|
|
|
|
void PushPeeked() {
|
|
|
|
size_t i = tail_++;
|
|
|
|
if (i + 1 == N)
|
|
|
|
tail_ -= N;
|
|
|
|
size_++;
|
|
|
|
}
|
|
|
|
|
2022-01-14 06:17:12 +00:00
|
|
|
size_t Size() const {
|
|
|
|
return size_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Full() const {
|
2022-01-16 05:59:23 +00:00
|
|
|
return size_ == N - 1;
|
2022-01-14 06:17:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Empty() const {
|
|
|
|
return size_ == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
T &operator[](size_t index) {
|
|
|
|
return items_[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
const T &operator[](size_t index) const {
|
|
|
|
return items_[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
T *items_ = nullptr;
|
2022-01-15 04:19:32 +00:00
|
|
|
std::atomic<size_t> head_;
|
|
|
|
std::atomic<size_t> tail_ ;
|
|
|
|
std::atomic<size_t> size_;
|
2022-01-14 06:17:12 +00:00
|
|
|
};
|
|
|
|
|
2022-01-16 07:12:03 +00:00
|
|
|
union BinClut {
|
|
|
|
uint8_t readable[1024];
|
|
|
|
};
|
|
|
|
|
2022-01-21 00:58:43 +00:00
|
|
|
struct BinTaskList {
|
|
|
|
// We shouldn't ever need more than two at once, since we use an atomic to run one at a time.
|
|
|
|
// A second could run due to overlap during teardown.
|
|
|
|
static constexpr int N = 2;
|
|
|
|
|
|
|
|
DrawBinItemsTask *tasks[N]{};
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
DrawBinItemsTask *Next() {
|
|
|
|
return tasks[count % N];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-01-21 02:17:23 +00:00
|
|
|
struct BinDirtyRange {
|
|
|
|
uint32_t base;
|
|
|
|
uint32_t strideBytes;
|
|
|
|
uint32_t widthBytes;
|
|
|
|
uint32_t height;
|
|
|
|
|
|
|
|
void Expand(uint32_t newBase, uint32_t bpp, uint32_t stride, DrawingCoords &tl, DrawingCoords &br);
|
|
|
|
};
|
|
|
|
|
2022-01-13 17:26:59 +00:00
|
|
|
class BinManager {
|
|
|
|
public:
|
2022-01-14 03:07:41 +00:00
|
|
|
BinManager();
|
2022-01-14 06:17:12 +00:00
|
|
|
~BinManager();
|
2022-01-14 03:07:41 +00:00
|
|
|
|
|
|
|
void UpdateState();
|
2022-01-16 07:24:58 +00:00
|
|
|
void UpdateClut(const void *src);
|
2022-01-13 17:26:59 +00:00
|
|
|
|
|
|
|
const Rasterizer::RasterizerState &State() {
|
2022-01-14 06:17:12 +00:00
|
|
|
return states_[stateIndex_];
|
2022-01-13 17:26:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AddTriangle(const VertexData &v0, const VertexData &v1, const VertexData &v2);
|
|
|
|
void AddClearRect(const VertexData &v0, const VertexData &v1);
|
2022-02-09 07:11:12 +00:00
|
|
|
void AddRect(const VertexData &v0, const VertexData &v1);
|
2022-01-13 17:26:59 +00:00
|
|
|
void AddSprite(const VertexData &v0, const VertexData &v1);
|
|
|
|
void AddLine(const VertexData &v0, const VertexData &v1);
|
|
|
|
void AddPoint(const VertexData &v0);
|
|
|
|
|
2022-01-14 06:17:12 +00:00
|
|
|
void Drain();
|
2022-01-16 17:07:33 +00:00
|
|
|
void Flush(const char *reason);
|
2022-01-21 02:17:23 +00:00
|
|
|
bool HasPendingWrite(uint32_t start, uint32_t stride, uint32_t w, uint32_t h);
|
2022-01-16 17:07:33 +00:00
|
|
|
|
|
|
|
void GetStats(char *buffer, size_t bufsize);
|
|
|
|
void ResetStats();
|
2022-01-13 17:26:59 +00:00
|
|
|
|
2022-01-23 06:41:41 +00:00
|
|
|
void SetDirty(SoftDirty flags) {
|
|
|
|
dirty_ |= flags;
|
|
|
|
}
|
2022-01-23 15:09:25 +00:00
|
|
|
void ClearDirty(SoftDirty flags) {
|
|
|
|
dirty_ &= ~flags;
|
|
|
|
}
|
2022-01-23 06:41:41 +00:00
|
|
|
SoftDirty GetDirty() {
|
|
|
|
return dirty_;
|
|
|
|
}
|
2022-01-23 15:09:25 +00:00
|
|
|
bool HasDirty(SoftDirty flags) {
|
|
|
|
return dirty_ & flags;
|
|
|
|
}
|
2022-01-23 06:41:41 +00:00
|
|
|
|
2022-01-16 17:30:02 +00:00
|
|
|
protected:
|
2022-03-13 20:34:14 +00:00
|
|
|
#if PPSSPP_ARCH(32BIT)
|
|
|
|
// Use less memory and less address space. We're unlikely to have 32 cores on a 32-bit CPU.
|
|
|
|
static constexpr int MAX_POSSIBLE_TASKS = 16;
|
|
|
|
#else
|
2022-01-15 04:19:32 +00:00
|
|
|
static constexpr int MAX_POSSIBLE_TASKS = 64;
|
2022-03-13 20:34:14 +00:00
|
|
|
#endif
|
2022-01-16 17:30:02 +00:00
|
|
|
// This is about 1MB of state data.
|
|
|
|
static constexpr int QUEUED_STATES = 4096;
|
|
|
|
// These are 1KB each, so half an MB.
|
|
|
|
static constexpr int QUEUED_CLUTS = 512;
|
2022-01-16 19:49:41 +00:00
|
|
|
// About 320 KB, but we have usually 16 or less of them, so 5 MB - 20 MB.
|
2022-01-16 17:30:02 +00:00
|
|
|
static constexpr int QUEUED_PRIMS = 1024;
|
|
|
|
|
|
|
|
typedef BinQueue<Rasterizer::RasterizerState, QUEUED_STATES> BinStateQueue;
|
|
|
|
typedef BinQueue<BinClut, QUEUED_CLUTS> BinClutQueue;
|
|
|
|
typedef BinQueue<BinItem, QUEUED_PRIMS> BinItemQueue;
|
2022-01-15 04:19:32 +00:00
|
|
|
|
2022-01-16 17:30:02 +00:00
|
|
|
private:
|
|
|
|
BinStateQueue states_;
|
2022-01-14 03:07:41 +00:00
|
|
|
int stateIndex_;
|
2022-01-16 17:30:02 +00:00
|
|
|
BinClutQueue cluts_;
|
2022-01-16 07:12:03 +00:00
|
|
|
int clutIndex_;
|
2022-01-14 03:07:41 +00:00
|
|
|
BinCoords scissor_;
|
2022-01-16 17:30:02 +00:00
|
|
|
BinItemQueue queue_;
|
2022-01-14 03:07:41 +00:00
|
|
|
BinCoords queueRange_;
|
2022-01-23 15:09:25 +00:00
|
|
|
SoftDirty dirty_ = SoftDirty::NONE;
|
2022-01-14 03:07:41 +00:00
|
|
|
|
2022-01-14 06:17:12 +00:00
|
|
|
int maxTasks_ = 1;
|
|
|
|
bool tasksSplit_ = false;
|
|
|
|
std::vector<BinCoords> taskRanges_;
|
2022-01-16 17:30:02 +00:00
|
|
|
BinItemQueue taskQueues_[MAX_POSSIBLE_TASKS];
|
2022-01-21 00:58:43 +00:00
|
|
|
BinTaskList taskLists_[MAX_POSSIBLE_TASKS];
|
2022-01-15 04:19:32 +00:00
|
|
|
std::atomic<bool> taskStatus_[MAX_POSSIBLE_TASKS];
|
2022-01-14 06:17:12 +00:00
|
|
|
BinWaitable *waitable_ = nullptr;
|
|
|
|
|
2022-01-21 02:17:23 +00:00
|
|
|
BinDirtyRange pendingWrites_[2]{};
|
2022-01-23 15:09:25 +00:00
|
|
|
bool pendingOverlap_ = false;
|
2022-01-21 02:17:23 +00:00
|
|
|
|
2022-01-16 17:07:33 +00:00
|
|
|
std::unordered_map<const char *, double> flushReasonTimes_;
|
2022-01-16 17:30:02 +00:00
|
|
|
std::unordered_map<const char *, double> lastFlushReasonTimes_;
|
2022-01-16 17:07:33 +00:00
|
|
|
const char *slowestFlushReason_ = nullptr;
|
|
|
|
double slowestFlushTime_ = 0.0;
|
2022-01-17 04:19:51 +00:00
|
|
|
int lastFlipstats_ = 0;
|
|
|
|
int enqueues_ = 0;
|
|
|
|
int mostThreads_ = 0;
|
2022-01-16 17:07:33 +00:00
|
|
|
|
2022-01-21 02:17:23 +00:00
|
|
|
bool HasTextureWrite(const Rasterizer::RasterizerState &state);
|
2022-01-14 03:07:41 +00:00
|
|
|
BinCoords Scissor(BinCoords range);
|
|
|
|
BinCoords Range(const VertexData &v0, const VertexData &v1, const VertexData &v2);
|
|
|
|
BinCoords Range(const VertexData &v0, const VertexData &v1);
|
|
|
|
BinCoords Range(const VertexData &v0);
|
|
|
|
void Expand(const BinCoords &range);
|
2022-01-16 17:30:02 +00:00
|
|
|
|
|
|
|
friend class DrawBinItemsTask;
|
2022-01-13 17:26:59 +00:00
|
|
|
};
|