CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/GPU/Directx9/TextureCacheDX9.cpp
Views: 1401
1
// Copyright (c) 2012- 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 <cstring>
20
#include <wrl/client.h>
21
22
#include "Common/TimeUtil.h"
23
#include "Core/MemMap.h"
24
#include "GPU/ge_constants.h"
25
26
#include "GPU/GPUState.h"
27
#include "GPU/Directx9/TextureCacheDX9.h"
28
#include "GPU/Directx9/FramebufferManagerDX9.h"
29
#include "GPU/Directx9/ShaderManagerDX9.h"
30
#include "Common/GPU/D3D9/D3D9StateCache.h"
31
#include "GPU/Common/TextureShaderCommon.h"
32
#include "GPU/Common/FramebufferManagerCommon.h"
33
#include "GPU/Common/TextureDecoder.h"
34
#include "Core/Config.h"
35
36
#include "ext/xxhash.h"
37
#include "Common/Math/math_util.h"
38
39
// NOTE: In the D3D backends, we flip R and B in the shaders, so while these look wrong, they're OK.
40
41
using Microsoft::WRL::ComPtr;
42
43
Draw::DataFormat FromD3D9Format(u32 fmt) {
44
switch (fmt) {
45
case D3DFMT_A4R4G4B4: return Draw::DataFormat::B4G4R4A4_UNORM_PACK16;
46
case D3DFMT_A1R5G5B5: return Draw::DataFormat::A1R5G5B5_UNORM_PACK16;
47
case D3DFMT_R5G6B5: return Draw::DataFormat::R5G6B5_UNORM_PACK16;
48
case D3DFMT_A8: return Draw::DataFormat::R8_UNORM;
49
case D3DFMT_A8R8G8B8: default: return Draw::DataFormat::R8G8B8A8_UNORM;
50
}
51
}
52
53
D3DFORMAT ToD3D9Format(Draw::DataFormat fmt) {
54
switch (fmt) {
55
case Draw::DataFormat::BC1_RGBA_UNORM_BLOCK: return D3DFMT_DXT1;
56
case Draw::DataFormat::BC2_UNORM_BLOCK: return D3DFMT_DXT3;
57
case Draw::DataFormat::BC3_UNORM_BLOCK: return D3DFMT_DXT5;
58
case Draw::DataFormat::R8G8B8A8_UNORM: return D3DFMT_A8R8G8B8;
59
default: _dbg_assert_(false); return D3DFMT_A8R8G8B8;
60
}
61
}
62
63
#define INVALID_TEX (LPDIRECT3DTEXTURE9)(-1)
64
65
static const D3DVERTEXELEMENT9 g_FramebufferVertexElements[] = {
66
{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
67
{ 0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
68
D3DDECL_END()
69
};
70
71
TextureCacheDX9::TextureCacheDX9(Draw::DrawContext *draw, Draw2D *draw2D)
72
: TextureCacheCommon(draw, draw2D) {
73
lastBoundTexture = INVALID_TEX;
74
device_ = (LPDIRECT3DDEVICE9)draw->GetNativeObject(Draw::NativeObject::DEVICE);
75
deviceEx_ = (LPDIRECT3DDEVICE9EX)draw->GetNativeObject(Draw::NativeObject::DEVICE_EX);
76
D3DCAPS9 pCaps;
77
ZeroMemory(&pCaps, sizeof(pCaps));
78
HRESULT result = 0;
79
if (deviceEx_) {
80
result = deviceEx_->GetDeviceCaps(&pCaps);
81
} else {
82
result = device_->GetDeviceCaps(&pCaps);
83
}
84
if (FAILED(result)) {
85
WARN_LOG(Log::G3D, "Failed to get the device caps!");
86
maxAnisotropyLevel = 16;
87
} else {
88
maxAnisotropyLevel = pCaps.MaxAnisotropy;
89
}
90
91
nextTexture_ = nullptr;
92
device_->CreateVertexDeclaration(g_FramebufferVertexElements, &pFramebufferVertexDecl);
93
}
94
95
TextureCacheDX9::~TextureCacheDX9() {
96
Clear(true);
97
}
98
99
void TextureCacheDX9::SetFramebufferManager(FramebufferManagerDX9 *fbManager) {
100
framebufferManager_ = fbManager;
101
}
102
103
void TextureCacheDX9::ReleaseTexture(TexCacheEntry *entry, bool delete_them) {
104
LPDIRECT3DBASETEXTURE9 &texture = DxTex(entry);
105
if (texture) {
106
texture->Release();
107
texture = nullptr;
108
}
109
}
110
111
void TextureCacheDX9::ForgetLastTexture() {
112
lastBoundTexture = INVALID_TEX;
113
}
114
115
D3DFORMAT getClutDestFormat(GEPaletteFormat format) {
116
switch (format) {
117
case GE_CMODE_16BIT_ABGR4444:
118
return D3DFMT_A4R4G4B4;
119
case GE_CMODE_16BIT_ABGR5551:
120
return D3DFMT_A1R5G5B5;
121
case GE_CMODE_16BIT_BGR5650:
122
return D3DFMT_R5G6B5;
123
case GE_CMODE_32BIT_ABGR8888:
124
return D3DFMT_A8R8G8B8;
125
}
126
// Should never be here !
127
return D3DFMT_A8R8G8B8;
128
}
129
130
void TextureCacheDX9::ApplySamplingParams(const SamplerCacheKey &key) {
131
D3DTEXTUREFILTERTYPE minFilt = (false ? D3DTEXF_ANISOTROPIC : D3DTEXF_LINEAR);
132
dxstate.texMinFilter.set(key.minFilt ? minFilt : D3DTEXF_POINT);
133
dxstate.texMipFilter.set(key.mipFilt ? D3DTEXF_LINEAR : D3DTEXF_POINT);
134
dxstate.texMagFilter.set(key.magFilt ? D3DTEXF_LINEAR : D3DTEXF_POINT);
135
136
// DX9 mip levels are .. odd. The "max level" sets the LARGEST mip to use.
137
// We can enforce only the top mip level by setting a massive negative lod bias.
138
139
if (!key.mipEnable) {
140
dxstate.texMaxMipLevel.set(0);
141
dxstate.texMipLodBias.set(-100.0f);
142
} else {
143
dxstate.texMipLodBias.set((float)key.lodBias / 256.0f);
144
dxstate.texMaxMipLevel.set(key.minLevel / 256);
145
}
146
147
dxstate.texAddressU.set(key.sClamp ? D3DTADDRESS_CLAMP : D3DTADDRESS_WRAP);
148
dxstate.texAddressV.set(key.tClamp ? D3DTADDRESS_CLAMP : D3DTADDRESS_WRAP);
149
}
150
151
void TextureCacheDX9::StartFrame() {
152
TextureCacheCommon::StartFrame();
153
154
if (gstate_c.Use(GPU_USE_ANISOTROPY)) {
155
// Just take the opportunity to set the global aniso level here, once per frame.
156
DWORD aniso = 1 << g_Config.iAnisotropyLevel;
157
DWORD anisotropyLevel = aniso > maxAnisotropyLevel ? maxAnisotropyLevel : aniso;
158
device_->SetSamplerState(0, D3DSAMP_MAXANISOTROPY, anisotropyLevel);
159
}
160
}
161
162
void TextureCacheDX9::UpdateCurrentClut(GEPaletteFormat clutFormat, u32 clutBase, bool clutIndexIsSimple) {
163
const u32 clutBaseBytes = clutBase * (clutFormat == GE_CMODE_32BIT_ABGR8888 ? sizeof(u32) : sizeof(u16));
164
// Technically, these extra bytes weren't loaded, but hopefully it was loaded earlier.
165
// If not, we're going to hash random data, which hopefully doesn't cause a performance issue.
166
//
167
// TODO: Actually, this seems like a hack. The game can upload part of a CLUT and reference other data.
168
// clutTotalBytes_ is the last amount uploaded. We should hash clutMaxBytes_, but this will often hash
169
// unrelated old entries for small palettes.
170
// Adding clutBaseBytes may just be mitigating this for some usage patterns.
171
const u32 clutExtendedBytes = std::min(clutTotalBytes_ + clutBaseBytes, clutMaxBytes_);
172
173
if (replacer_.Enabled())
174
clutHash_ = XXH32((const char *)clutBufRaw_, clutExtendedBytes, 0xC0108888);
175
else
176
clutHash_ = XXH3_64bits((const char *)clutBufRaw_, clutExtendedBytes) & 0xFFFFFFFF;
177
clutBuf_ = clutBufRaw_;
178
179
// Special optimization: fonts typically draw clut4 with just alpha values in a single color.
180
clutAlphaLinear_ = false;
181
clutAlphaLinearColor_ = 0;
182
if (clutFormat == GE_CMODE_16BIT_ABGR4444 && clutIndexIsSimple) {
183
const u16_le *clut = GetCurrentClut<u16_le>();
184
clutAlphaLinear_ = true;
185
clutAlphaLinearColor_ = clut[15] & 0x0FFF;
186
for (int i = 0; i < 16; ++i) {
187
u16 step = clutAlphaLinearColor_ | (i << 12);
188
if (clut[i] != step) {
189
clutAlphaLinear_ = false;
190
break;
191
}
192
}
193
}
194
195
clutLastFormat_ = gstate.clutformat;
196
}
197
198
void TextureCacheDX9::BindTexture(TexCacheEntry *entry) {
199
if (!entry) {
200
device_->SetTexture(0, nullptr);
201
return;
202
}
203
IDirect3DBaseTexture9 *texture = DxTex(entry);
204
if (texture != lastBoundTexture) {
205
device_->SetTexture(0, texture);
206
lastBoundTexture = texture;
207
}
208
int maxLevel = (entry->status & TexCacheEntry::STATUS_NO_MIPS) ? 0 : entry->maxLevel;
209
SamplerCacheKey samplerKey = GetSamplingParams(maxLevel, entry);
210
ApplySamplingParams(samplerKey);
211
}
212
213
void TextureCacheDX9::Unbind() {
214
device_->SetTexture(0, nullptr);
215
ForgetLastTexture();
216
}
217
218
void TextureCacheDX9::BindAsClutTexture(Draw::Texture *tex, bool smooth) {
219
LPDIRECT3DBASETEXTURE9 clutTexture = (LPDIRECT3DBASETEXTURE9)draw_->GetNativeObject(Draw::NativeObject::TEXTURE_VIEW, tex);
220
device_->SetTexture(1, clutTexture);
221
device_->SetSamplerState(1, D3DSAMP_MINFILTER, smooth ? D3DTEXF_LINEAR : D3DTEXF_POINT);
222
device_->SetSamplerState(1, D3DSAMP_MAGFILTER, smooth ? D3DTEXF_LINEAR : D3DTEXF_POINT);
223
device_->SetSamplerState(1, D3DSAMP_MIPFILTER, D3DTEXF_NONE);
224
}
225
226
void TextureCacheDX9::BuildTexture(TexCacheEntry *const entry) {
227
BuildTexturePlan plan;
228
if (!PrepareBuildTexture(plan, entry)) {
229
// We're screwed?
230
return;
231
}
232
233
D3DFORMAT dstFmt = GetDestFormat(GETextureFormat(entry->format), gstate.getClutPaletteFormat());
234
if (plan.doReplace) {
235
dstFmt = ToD3D9Format(plan.replaced->Format());
236
} else if (plan.scaleFactor > 1 || plan.saveTexture) {
237
dstFmt = D3DFMT_A8R8G8B8;
238
} else if (plan.decodeToClut8) {
239
dstFmt = D3DFMT_A8;
240
}
241
242
int levels;
243
244
LPDIRECT3DBASETEXTURE9 &texture = DxTex(entry);
245
D3DPOOL pool = D3DPOOL_DEFAULT;
246
int usage = D3DUSAGE_DYNAMIC;
247
248
int tw;
249
int th;
250
plan.GetMipSize(0, &tw, &th);
251
252
HRESULT hr;
253
if (plan.depth == 1) {
254
// We don't yet have mip generation, so clamp the number of levels to the ones we can load directly.
255
levels = std::min(plan.levelsToCreate, plan.levelsToLoad);
256
257
LPDIRECT3DTEXTURE9 tex;
258
hr = device_->CreateTexture(tw, th, levels, usage, dstFmt, pool, &tex, nullptr);
259
texture = tex;
260
} else {
261
LPDIRECT3DVOLUMETEXTURE9 tex;
262
hr = device_->CreateVolumeTexture(tw, th, plan.depth, 1, usage, dstFmt, pool, &tex, nullptr);
263
texture = tex;
264
265
levels = 1;
266
}
267
268
if (FAILED(hr)) {
269
INFO_LOG(Log::G3D, "Failed to create D3D texture: %dx%d", tw, th);
270
ReleaseTexture(entry, true);
271
return;
272
}
273
274
if (!texture) {
275
// What to do here?
276
return;
277
}
278
279
if (plan.depth == 1) {
280
// Regular loop.
281
for (int i = 0; i < levels; i++) {
282
int dstLevel = i;
283
HRESULT result;
284
uint32_t lockFlag = dstLevel == 0 ? D3DLOCK_DISCARD : 0; // Can only discard the top level
285
D3DLOCKED_RECT rect{};
286
287
result = ((LPDIRECT3DTEXTURE9)texture)->LockRect(dstLevel, &rect, NULL, lockFlag);
288
if (FAILED(result)) {
289
ERROR_LOG(Log::G3D, "Failed to lock D3D 2D texture at level %d: %dx%d", i, plan.w, plan.h);
290
return;
291
}
292
uint8_t *data = (uint8_t *)rect.pBits;
293
int stride = rect.Pitch;
294
LoadTextureLevel(*entry, data, 0, stride, plan, (i == 0) ? plan.baseLevelSrc : i, FromD3D9Format(dstFmt), TexDecodeFlags{});
295
((LPDIRECT3DTEXTURE9)texture)->UnlockRect(dstLevel);
296
}
297
} else {
298
// 3D loop.
299
D3DLOCKED_BOX box;
300
HRESULT result = ((LPDIRECT3DVOLUMETEXTURE9)texture)->LockBox(0, &box, nullptr, D3DLOCK_DISCARD);
301
if (FAILED(result)) {
302
ERROR_LOG(Log::G3D, "Failed to lock D3D 2D texture: %dx%dx%d", plan.w, plan.h, plan.depth);
303
return;
304
}
305
306
uint8_t *data = (uint8_t *)box.pBits;
307
int stride = box.RowPitch;
308
for (int i = 0; i < plan.depth; i++) {
309
LoadTextureLevel(*entry, data, 0, stride, plan, (i == 0) ? plan.baseLevelSrc : i, FromD3D9Format(dstFmt), TexDecodeFlags{});
310
data += box.SlicePitch;
311
}
312
((LPDIRECT3DVOLUMETEXTURE9)texture)->UnlockBox(0);
313
}
314
315
// Signal that we support depth textures so use it as one.
316
if (plan.depth > 1) {
317
entry->status |= TexCacheEntry::STATUS_3D;
318
}
319
320
if (plan.doReplace) {
321
entry->SetAlphaStatus(TexCacheEntry::TexStatus(plan.replaced->AlphaStatus()));
322
323
if (!Draw::DataFormatIsBlockCompressed(plan.replaced->Format(), nullptr)) {
324
entry->status |= TexCacheEntry::STATUS_BGRA;
325
}
326
} else {
327
entry->status |= TexCacheEntry::STATUS_BGRA;
328
}
329
}
330
331
D3DFORMAT TextureCacheDX9::GetDestFormat(GETextureFormat format, GEPaletteFormat clutFormat) const {
332
switch (format) {
333
case GE_TFMT_CLUT4:
334
case GE_TFMT_CLUT8:
335
case GE_TFMT_CLUT16:
336
case GE_TFMT_CLUT32:
337
return getClutDestFormat(clutFormat);
338
case GE_TFMT_4444:
339
return D3DFMT_A4R4G4B4;
340
case GE_TFMT_5551:
341
return D3DFMT_A1R5G5B5;
342
case GE_TFMT_5650:
343
return D3DFMT_R5G6B5;
344
case GE_TFMT_8888:
345
case GE_TFMT_DXT1:
346
case GE_TFMT_DXT3:
347
case GE_TFMT_DXT5:
348
default:
349
return D3DFMT_A8R8G8B8;
350
}
351
}
352
353
bool TextureCacheDX9::GetCurrentTextureDebug(GPUDebugBuffer &buffer, int level, bool *isFramebuffer) {
354
SetTexture();
355
if (!nextTexture_) {
356
return GetCurrentFramebufferTextureDebug(buffer, isFramebuffer);
357
}
358
359
ApplyTexture();
360
361
ComPtr<IDirect3DBaseTexture9> baseTex;
362
ComPtr<IDirect3DTexture9> tex;
363
ComPtr<IDirect3DSurface9> offscreen;
364
HRESULT hr;
365
366
bool success = false;
367
hr = device_->GetTexture(0, &baseTex);
368
if (SUCCEEDED(hr) && baseTex != NULL) {
369
hr = baseTex.As(&tex);
370
if (SUCCEEDED(hr)) {
371
D3DSURFACE_DESC desc;
372
D3DLOCKED_RECT locked;
373
tex->GetLevelDesc(level, &desc);
374
RECT rect = { 0, 0, (LONG)desc.Width, (LONG)desc.Height };
375
hr = tex->LockRect(level, &locked, &rect, D3DLOCK_READONLY);
376
377
// If it fails, this means it's a render-to-texture, so we have to get creative.
378
if (FAILED(hr)) {
379
ComPtr<IDirect3DSurface9> renderTarget;
380
hr = tex->GetSurfaceLevel(level, &renderTarget);
381
if (renderTarget && SUCCEEDED(hr)) {
382
hr = device_->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &offscreen, NULL);
383
if (SUCCEEDED(hr)) {
384
hr = device_->GetRenderTargetData(renderTarget.Get(), offscreen.Get());
385
if (SUCCEEDED(hr)) {
386
hr = offscreen->LockRect(&locked, &rect, D3DLOCK_READONLY);
387
}
388
}
389
}
390
*isFramebuffer = true;
391
} else {
392
*isFramebuffer = false;
393
}
394
395
if (SUCCEEDED(hr)) {
396
GPUDebugBufferFormat fmt;
397
int pixelSize;
398
switch (desc.Format) {
399
case D3DFMT_A1R5G5B5:
400
fmt = gstate_c.bgraTexture ? GPU_DBG_FORMAT_5551 : GPU_DBG_FORMAT_5551_BGRA;
401
pixelSize = 2;
402
break;
403
case D3DFMT_A4R4G4B4:
404
fmt = gstate_c.bgraTexture ? GPU_DBG_FORMAT_4444 : GPU_DBG_FORMAT_4444_BGRA;
405
pixelSize = 2;
406
break;
407
case D3DFMT_R5G6B5:
408
fmt = gstate_c.bgraTexture ? GPU_DBG_FORMAT_565 : GPU_DBG_FORMAT_565_BGRA;
409
pixelSize = 2;
410
break;
411
case D3DFMT_A8R8G8B8:
412
fmt = gstate_c.bgraTexture ? GPU_DBG_FORMAT_8888 : GPU_DBG_FORMAT_8888_BGRA;
413
pixelSize = 4;
414
break;
415
default:
416
fmt = GPU_DBG_FORMAT_INVALID;
417
break;
418
}
419
420
if (fmt != GPU_DBG_FORMAT_INVALID) {
421
buffer.Allocate(locked.Pitch / pixelSize, desc.Height, fmt, false);
422
memcpy(buffer.GetData(), locked.pBits, locked.Pitch * desc.Height);
423
success = true;
424
} else {
425
success = false;
426
}
427
if (offscreen) {
428
offscreen->UnlockRect();
429
} else {
430
tex->UnlockRect(level);
431
}
432
}
433
}
434
}
435
436
return success;
437
}
438
439
void *TextureCacheDX9::GetNativeTextureView(const TexCacheEntry *entry) {
440
LPDIRECT3DBASETEXTURE9 tex = DxTex(entry);
441
return (void *)tex;
442
}
443
444