Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/GPU/Common/TextureShaderCommon.cpp
5663 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 <algorithm>
19
#include <map>
20
#include <vector>
21
#include <string>
22
23
#include "Common/StringUtils.h"
24
#include "GPU/GPUState.h"
25
#include "Common/GPU/Shader.h"
26
#include "Common/GPU/ShaderWriter.h"
27
#include "Common/Data/Convert/ColorConv.h"
28
#include "GPU/Common/Draw2D.h"
29
#include "GPU/Common/TextureShaderCommon.h"
30
#include "GPU/Common/DepalettizeShaderCommon.h"
31
32
static const VaryingDef varyings[1] = {
33
{ "vec2", "v_texcoord", Draw::SEM_TEXCOORD0, 0, "highp" },
34
};
35
36
static const SamplerDef samplers[2] = {
37
{ 0, "tex", SamplerFlags::ARRAY_ON_VULKAN },
38
{ 1, "pal" },
39
};
40
41
TextureShaderCache::TextureShaderCache(Draw::DrawContext *draw, Draw2D *draw2D) : draw_(draw), draw2D_(draw2D) { }
42
43
TextureShaderCache::~TextureShaderCache() {
44
DeviceLost();
45
}
46
47
void TextureShaderCache::DeviceRestore(Draw::DrawContext *draw) {
48
draw_ = draw;
49
}
50
51
void TextureShaderCache::DeviceLost() {
52
Clear();
53
draw_ = nullptr;
54
}
55
56
ClutTexture TextureShaderCache::GetClutTexture(GEPaletteFormat clutFormat, const u32 clutHash, const u32 *rawClut) {
57
// Simplistic, but works well enough.
58
u32 clutId = clutHash ^ (uint32_t)clutFormat;
59
60
auto oldtex = texCache_.find(clutId);
61
if (oldtex != texCache_.end()) {
62
oldtex->second->lastFrame = gpuStats.numFlips;
63
return *oldtex->second;
64
}
65
66
int maxClutEntries = clutFormat == GE_CMODE_32BIT_ABGR8888 ? 256 : 512;
67
68
ClutTexture *tex = new ClutTexture();
69
70
Draw::TextureDesc desc{};
71
desc.width = 512; // We always use 512-sized textures here for simplicity, though the most common is that only up to 256 entries are used.
72
desc.height = 1;
73
desc.depth = 1;
74
desc.mipLevels = 1;
75
desc.tag = "clut";
76
desc.type = Draw::TextureType::LINEAR2D; // TODO: Try LINEAR1D?
77
desc.format = Draw::DataFormat::R8G8B8A8_UNORM; // TODO: Also support an BGR format. We won't bother with the 16-bit formats here.
78
79
uint8_t convTemp[2048]{};
80
81
switch (clutFormat) {
82
case GEPaletteFormat::GE_CMODE_32BIT_ABGR8888:
83
desc.initData.push_back((const uint8_t *)rawClut);
84
break;
85
case GEPaletteFormat::GE_CMODE_16BIT_BGR5650:
86
ConvertRGB565ToRGBA8888((u32 *)convTemp, (const u16 *)rawClut, maxClutEntries);
87
desc.initData.push_back(convTemp);
88
break;
89
case GEPaletteFormat::GE_CMODE_16BIT_ABGR5551:
90
ConvertRGBA5551ToRGBA8888((u32 *)convTemp, (const u16 *)rawClut, maxClutEntries);
91
desc.initData.push_back(convTemp);
92
break;
93
case GEPaletteFormat::GE_CMODE_16BIT_ABGR4444:
94
ConvertRGBA4444ToRGBA8888((u32 *)convTemp, (const u16 *)rawClut, maxClutEntries);
95
desc.initData.push_back(convTemp);
96
break;
97
}
98
99
100
for (int i = 0; i < 3; i++) {
101
tex->rampLengths[i] = 0;
102
tex->rampStarts[i] = 0;
103
}
104
// Quick check for how many continuously growing entries we have at the start.
105
// Bilinearly filtering CLUTs only really makes sense for this kind of ramp.
106
int i = 0;
107
for (int j = 0; j < ClutTexture::MAX_RAMPS; j++) {
108
tex->rampStarts[j] = i;
109
int lastR = 0;
110
int lastG = 0;
111
int lastB = 0;
112
int lastA = 0;
113
for (; i < maxClutEntries; i++) {
114
int r = desc.initData[0][i * 4];
115
int g = desc.initData[0][i * 4 + 1];
116
int b = desc.initData[0][i * 4 + 2];
117
int a = desc.initData[0][i * 4 + 3];
118
if (r < lastR || g < lastG || b < lastB || a < lastA) {
119
lastR = r; lastG = g; lastB = b; lastA = a;
120
break;
121
} else {
122
lastR = r;
123
lastG = g;
124
lastB = b;
125
lastA = a;
126
}
127
}
128
tex->rampLengths[j] = i - tex->rampStarts[j];
129
if (i >= maxClutEntries) {
130
break;
131
}
132
}
133
134
tex->texture = draw_->CreateTexture(desc);
135
tex->lastFrame = gpuStats.numFlips;
136
137
texCache_[clutId] = tex;
138
return *tex;
139
}
140
141
void TextureShaderCache::Clear() {
142
for (auto shader = depalCache_.begin(); shader != depalCache_.end(); ++shader) {
143
if (shader->second->pipeline) {
144
shader->second->pipeline->Release();
145
}
146
delete shader->second;
147
}
148
depalCache_.clear();
149
for (auto tex = texCache_.begin(); tex != texCache_.end(); ++tex) {
150
tex->second->texture->Release();
151
delete tex->second;
152
}
153
texCache_.clear();
154
if (nearestSampler_) {
155
nearestSampler_->Release();
156
nearestSampler_ = nullptr;
157
}
158
if (linearSampler_) {
159
linearSampler_->Release();
160
linearSampler_ = nullptr;
161
}
162
}
163
164
Draw::SamplerState *TextureShaderCache::GetSampler(bool linearFilter) {
165
if (linearFilter) {
166
if (!linearSampler_) {
167
Draw::SamplerStateDesc desc{};
168
desc.magFilter = Draw::TextureFilter::LINEAR;
169
desc.minFilter = Draw::TextureFilter::LINEAR;
170
desc.wrapU = Draw::TextureAddressMode::CLAMP_TO_EDGE;
171
desc.wrapV = Draw::TextureAddressMode::CLAMP_TO_EDGE;
172
desc.wrapW = Draw::TextureAddressMode::CLAMP_TO_EDGE;
173
linearSampler_ = draw_->CreateSamplerState(desc);
174
}
175
return linearSampler_;
176
} else {
177
if (!nearestSampler_) {
178
Draw::SamplerStateDesc desc{};
179
desc.wrapU = Draw::TextureAddressMode::CLAMP_TO_EDGE;
180
desc.wrapV = Draw::TextureAddressMode::CLAMP_TO_EDGE;
181
desc.wrapW = Draw::TextureAddressMode::CLAMP_TO_EDGE;
182
nearestSampler_ = draw_->CreateSamplerState(desc);
183
}
184
return nearestSampler_;
185
}
186
}
187
188
void TextureShaderCache::Decimate() {
189
for (auto tex = texCache_.begin(); tex != texCache_.end(); ) {
190
if (tex->second->lastFrame + DEPAL_TEXTURE_OLD_AGE < gpuStats.numFlips) {
191
tex->second->texture->Release();
192
delete tex->second;
193
texCache_.erase(tex++);
194
} else {
195
++tex;
196
}
197
}
198
gpuStats.numClutTextures = (int)texCache_.size();
199
}
200
201
Draw2DPipeline *TextureShaderCache::GetDepalettizeShader(uint32_t clutMode, GETextureFormat textureFormat, GEBufferFormat bufferFormat, bool smoothedDepal, u32 depthUpperBits) {
202
using namespace Draw;
203
204
// Generate an ID for depal shaders.
205
u64 id = ((u64)depthUpperBits << 32) | (clutMode & 0xFFFFFF) | (textureFormat << 24) | (bufferFormat << 28);
206
207
auto shader = depalCache_.find(id);
208
if (shader != depalCache_.end()) {
209
return shader->second;
210
}
211
212
// TODO: Parse these out of clutMode some nice way, to become a bit more stateless.
213
DepalConfig config;
214
config.clutFormat = gstate.getClutPaletteFormat();
215
config.startPos = gstate.getClutIndexStartPos();
216
config.shift = gstate.getClutIndexShift();
217
config.mask = gstate.getClutIndexMask();
218
config.bufferFormat = bufferFormat;
219
config.textureFormat = textureFormat;
220
config.smoothedDepal = smoothedDepal;
221
config.depthUpperBits = depthUpperBits;
222
223
Draw2DPipeline *ts = draw2D_->Create2DPipeline([=](ShaderWriter &writer) -> Draw2DPipelineInfo {
224
GenerateDepalFs(writer, config);
225
return Draw2DPipelineInfo{
226
"depal",
227
config.bufferFormat == GE_FORMAT_DEPTH16 ? RASTER_DEPTH : RASTER_COLOR,
228
RASTER_COLOR,
229
samplers
230
};
231
});
232
233
depalCache_[id] = ts;
234
235
return ts->pipeline ? ts : nullptr;
236
}
237
238
std::vector<std::string> TextureShaderCache::DebugGetShaderIDs(DebugShaderType type) {
239
std::vector<std::string> ids;
240
for (auto &iter : depalCache_) {
241
ids.push_back(StringFromFormat("%08x", iter.first));
242
}
243
return ids;
244
}
245
246
std::string TextureShaderCache::DebugGetShaderString(const std::string &idstr, DebugShaderType type, DebugShaderStringType stringType) {
247
uint32_t id = 0;
248
sscanf(idstr.c_str(), "%08x", &id);
249
auto iter = depalCache_.find(id);
250
if (iter == depalCache_.end())
251
return "";
252
switch (stringType) {
253
case SHADER_STRING_SHORT_DESC:
254
return idstr;
255
case SHADER_STRING_SOURCE_CODE:
256
return iter->second->code;
257
default:
258
return "";
259
}
260
}
261
262