Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/GPU/GLES/FragmentTestCacheGLES.cpp
3187 views
1
// Copyright (c) 2014- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#include "Common/GPU/thin3d.h"
19
#include "Common/GPU/OpenGL/GLDebugLog.h"
20
#include "GPU/GLES/FragmentTestCacheGLES.h"
21
#include "GPU/GPUState.h"
22
#include "GPU/Common/GPUStateUtils.h"
23
24
// These are small, let's give them plenty of frames.
25
static const int FRAGTEST_TEXTURE_OLD_AGE = 307;
26
static const int FRAGTEST_DECIMATION_INTERVAL = 113;
27
28
FragmentTestCacheGLES::FragmentTestCacheGLES(Draw::DrawContext *draw) {
29
render_ = (GLRenderManager *)draw->GetNativeObject(Draw::NativeObject::RENDER_MANAGER);
30
}
31
32
FragmentTestCacheGLES::~FragmentTestCacheGLES() {
33
Clear();
34
}
35
36
void FragmentTestCacheGLES::DeviceRestore(Draw::DrawContext *draw) {
37
render_ = (GLRenderManager *)draw->GetNativeObject(Draw::NativeObject::RENDER_MANAGER);
38
}
39
40
void FragmentTestCacheGLES::BindTestTexture(int slot) {
41
bool alphaNeedsTexture = gstate.isAlphaTestEnabled() && !IsAlphaTestAgainstZero() && !IsAlphaTestTriviallyTrue();
42
bool colorNeedsTexture = gstate.isColorTestEnabled() && !IsColorTestAgainstZero() && !IsColorTestTriviallyTrue();
43
if (!alphaNeedsTexture && !colorNeedsTexture) {
44
// Common case: testing against zero. Just skip it, faster not to bind anything.
45
return;
46
}
47
48
const FragmentTestID id = GenerateTestID();
49
const auto cached = cache_.find(id);
50
if (cached != cache_.end()) {
51
cached->second.lastFrame = gpuStats.numFlips;
52
GLRTexture *tex = cached->second.texture;
53
if (tex == lastTexture_) {
54
// Already bound, hurray.
55
return;
56
}
57
render_->BindTexture(slot, tex);
58
lastTexture_ = tex;
59
return;
60
}
61
62
const u8 rRef = (gstate.getColorTestRef() >> 0) & 0xFF;
63
const u8 rMask = (gstate.getColorTestMask() >> 0) & 0xFF;
64
const u8 gRef = (gstate.getColorTestRef() >> 8) & 0xFF;
65
const u8 gMask = (gstate.getColorTestMask() >> 8) & 0xFF;
66
const u8 bRef = (gstate.getColorTestRef() >> 16) & 0xFF;
67
const u8 bMask = (gstate.getColorTestMask() >> 16) & 0xFF;
68
const u8 aRef = gstate.getAlphaTestRef();
69
const u8 aMask = gstate.getAlphaTestMask();
70
const u8 refs[4] = {rRef, gRef, bRef, aRef};
71
const u8 masks[4] = {rMask, gMask, bMask, aMask};
72
const GEComparison funcs[4] = {gstate.getColorTestFunction(), gstate.getColorTestFunction(), gstate.getColorTestFunction(), gstate.getAlphaTestFunction()};
73
const bool valid[4] = {gstate.isColorTestEnabled(), gstate.isColorTestEnabled(), gstate.isColorTestEnabled(), gstate.isAlphaTestEnabled()};
74
75
GLRTexture *tex = CreateTestTexture(funcs, refs, masks, valid);
76
lastTexture_ = tex;
77
render_->BindTexture(slot, tex);
78
// We only need to do this once for the texture.
79
render_->SetTextureSampler(slot, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, GL_NEAREST, GL_NEAREST, 0.0f);
80
FragmentTestTexture item;
81
item.lastFrame = gpuStats.numFlips;
82
item.texture = tex;
83
cache_[id] = item;
84
}
85
86
FragmentTestID FragmentTestCacheGLES::GenerateTestID() {
87
FragmentTestID id;
88
// Let's just keep it simple, it's all in here.
89
id.alpha = gstate.isAlphaTestEnabled() ? gstate.alphatest : 0;
90
if (gstate.isColorTestEnabled()) {
91
id.colorRefFunc = gstate.getColorTestFunction() | (gstate.getColorTestRef() << 8);
92
id.colorMask = gstate.getColorTestMask();
93
} else {
94
id.colorRefFunc = 0;
95
id.colorMask = 0;
96
}
97
return id;
98
}
99
100
GLRTexture *FragmentTestCacheGLES::CreateTestTexture(const GEComparison funcs[4], const u8 refs[4], const u8 masks[4], const bool valid[4]) {
101
u8 *data = new u8[256 * 4];
102
// TODO: Might it be better to use GL_ALPHA for simple textures?
103
// TODO: Experiment with 4-bit/etc. textures.
104
105
// Build the logic map.
106
for (int color = 0; color < 256; ++color) {
107
for (int i = 0; i < 4; ++i) {
108
bool res = true;
109
if (valid[i]) {
110
switch (funcs[i]) {
111
case GE_COMP_NEVER:
112
res = false;
113
break;
114
case GE_COMP_ALWAYS:
115
res = true;
116
break;
117
case GE_COMP_EQUAL:
118
res = (color & masks[i]) == (refs[i] & masks[i]);
119
break;
120
case GE_COMP_NOTEQUAL:
121
res = (color & masks[i]) != (refs[i] & masks[i]);
122
break;
123
case GE_COMP_LESS:
124
res = (color & masks[i]) < (refs[i] & masks[i]);
125
break;
126
case GE_COMP_LEQUAL:
127
res = (color & masks[i]) <= (refs[i] & masks[i]);
128
break;
129
case GE_COMP_GREATER:
130
res = (color & masks[i]) > (refs[i] & masks[i]);
131
break;
132
case GE_COMP_GEQUAL:
133
res = (color & masks[i]) >= (refs[i] & masks[i]);
134
break;
135
}
136
}
137
data[color * 4 + i] = res ? 0xFF : 0;
138
}
139
}
140
141
GLRTexture *tex = render_->CreateTexture(GL_TEXTURE_2D, 256, 1, 1, 1);
142
render_->TextureImage(tex, 0, 256, 1, 1, Draw::DataFormat::R8G8B8A8_UNORM, data);
143
return tex;
144
}
145
146
void FragmentTestCacheGLES::Clear(bool deleteThem) {
147
if (deleteThem) {
148
for (const auto &[_, v] : cache_) {
149
render_->DeleteTexture(v.texture);
150
}
151
}
152
cache_.clear();
153
lastTexture_ = nullptr;
154
}
155
156
void FragmentTestCacheGLES::Decimate() {
157
if (--decimationCounter_ <= 0) {
158
for (auto tex = cache_.begin(); tex != cache_.end(); ) {
159
if (tex->second.lastFrame + FRAGTEST_TEXTURE_OLD_AGE < gpuStats.numFlips) {
160
render_->DeleteTexture(tex->second.texture);
161
cache_.erase(tex++);
162
} else {
163
++tex;
164
}
165
}
166
167
decimationCounter_ = FRAGTEST_DECIMATION_INTERVAL;
168
}
169
170
lastTexture_ = nullptr;
171
}
172
173