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 "Common/Math/lin/matrix4x4.h"
19 #include "Common/GPU/thin3d.h"
20
21 #include "Common/Data/Convert/ColorConv.h"
22 #include "Core/MemMap.h"
23 #include "Core/Config.h"
24 #include "Core/ConfigValues.h"
25 #include "Core/System.h"
26 #include "Core/Reporting.h"
27 #include "GPU/ge_constants.h"
28 #include "GPU/GPUState.h"
29 #include "GPU/Debugger/Stepping.h"
30
31 #include "Common/GPU/D3D9/D3D9StateCache.h"
32 #include "GPU/Common/FramebufferManagerCommon.h"
33 #include "GPU/Common/PresentationCommon.h"
34 #include "GPU/Common/TextureDecoder.h"
35 #include "GPU/Directx9/FramebufferManagerDX9.h"
36 #include "GPU/Directx9/ShaderManagerDX9.h"
37 #include "GPU/Directx9/TextureCacheDX9.h"
38 #include "GPU/Directx9/DrawEngineDX9.h"
39
40 #include "Common/GPU/thin3d.h"
41
42 #include <algorithm>
43
44 #ifdef _M_SSE
45 #include <emmintrin.h>
46 #endif
47
48 namespace DX9 {
49
50 static const char *vscode = R"(
51 struct VS_IN {
52 float4 ObjPos : POSITION;
53 float2 Uv : TEXCOORD0;
54 };
55 struct VS_OUT {
56 float4 ProjPos : POSITION;
57 float2 Uv : TEXCOORD0;
VertexArrayInfoDX9()58 };
59 VS_OUT main( VS_IN In ) {
60 VS_OUT Out;
61 Out.ProjPos = In.ObjPos;
62 Out.Uv = In.Uv;
63 return Out;
64 }
65 )";
66
67 static const char *pscode = R"(
68 sampler s: register(s0);
69 struct PS_IN {
70 float2 Uv : TEXCOORD0;
71 };
72 float4 main( PS_IN In ) : COLOR {
73 float4 c = tex2D(s, In.Uv);
74 return c;
75 }
76 )";
77
78 static const D3DVERTEXELEMENT9 g_FramebufferVertexElements[] = {
79 { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
80 { 0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
81 D3DDECL_END()
82 };
83
84 FramebufferManagerDX9::FramebufferManagerDX9(Draw::DrawContext *draw)
85 : FramebufferManagerCommon(draw) {
86
87 device_ = (LPDIRECT3DDEVICE9)draw->GetNativeObject(Draw::NativeObject::DEVICE);
88 deviceEx_ = (LPDIRECT3DDEVICE9)draw->GetNativeObject(Draw::NativeObject::DEVICE_EX);
89 std::string errorMsg;
90 if (!CompileVertexShaderD3D9(device_, vscode, &pFramebufferVertexShader, &errorMsg)) {
91 OutputDebugStringA(errorMsg.c_str());
92 }
93
94 if (!CompilePixelShaderD3D9(device_, pscode, &pFramebufferPixelShader, &errorMsg)) {
95 OutputDebugStringA(errorMsg.c_str());
96 if (pFramebufferVertexShader) {
97 pFramebufferVertexShader->Release();
98 }
99 }
100
TessellationDataTransferDX9()101 device_->CreateVertexDeclaration(g_FramebufferVertexElements, &pFramebufferVertexDecl);
~TessellationDataTransferDX9()102
103
104 int usage = 0;
105 D3DPOOL pool = D3DPOOL_MANAGED;
106 if (deviceEx_) {
107 pool = D3DPOOL_DEFAULT;
108 usage = D3DUSAGE_DYNAMIC;
109 }
110 HRESULT hr = device_->CreateTexture(1, 1, 1, usage, D3DFMT_A8R8G8B8, pool, &nullTex_, nullptr);
111 D3DLOCKED_RECT rect;
SetShaderManager(ShaderManagerDX9 * shaderManager)112 nullTex_->LockRect(0, &rect, nullptr, D3DLOCK_DISCARD);
113 memset(rect.pBits, 0, 4);
114 nullTex_->UnlockRect(0);
SetTextureCache(TextureCacheDX9 * textureCache)115
116 presentation_->SetLanguage(HLSL_D3D9);
117 preferredPixelsFormat_ = Draw::DataFormat::B8G8R8A8_UNORM;
SetFramebufferManager(FramebufferManagerDX9 * fbManager)118 }
119
120 FramebufferManagerDX9::~FramebufferManagerDX9() {
121 if (pFramebufferVertexShader) {
122 pFramebufferVertexShader->Release();
123 pFramebufferVertexShader = nullptr;
124 }
125 if (pFramebufferPixelShader) {
126 pFramebufferPixelShader->Release();
127 pFramebufferPixelShader = nullptr;
128 }
129 pFramebufferVertexDecl->Release();
130 for (auto &it : offscreenSurfaces_) {
131 it.second.surface->Release();
132 }
133 if (stencilUploadPS_) {
134 stencilUploadPS_->Release();
135 }
136 if (stencilUploadVS_) {
137 stencilUploadVS_->Release();
138 }
139 if (nullTex_)
140 nullTex_->Release();
141 }
142
143 void FramebufferManagerDX9::SetTextureCache(TextureCacheDX9 *tc) {
144 textureCacheDX9_ = tc;
UpdateUseHWTessellation(bool enable)145 textureCache_ = tc;
146 }
147
148 void FramebufferManagerDX9::SetShaderManager(ShaderManagerDX9 *sm) {
149 shaderManagerDX9_ = sm;
150 shaderManager_ = sm;
151 }
152
153 void FramebufferManagerDX9::SetDrawEngine(DrawEngineDX9 *td) {
154 drawEngineD3D9_ = td;
155 drawEngine_ = td;
156 }
157
158 void FramebufferManagerDX9::DrawActiveTexture(float x, float y, float w, float h, float destW, float destH, float u0, float v0, float u1, float v1, int uvRotation, int flags) {
159 // TODO: StretchRect instead when possible?
160 float coord[20] = {
161 x,y,0, u0,v0,
162 x+w,y,0, u1,v0,
163 x+w,y+h,0, u1,v1,
164 x,y+h,0, u0,v1,
165 };
166
167 if (uvRotation != ROTATION_LOCKED_HORIZONTAL) {
168 float temp[8];
169 int rotation = 0;
170 switch (uvRotation) {
171 case ROTATION_LOCKED_HORIZONTAL180: rotation = 2; break;
172 case ROTATION_LOCKED_VERTICAL: rotation = 1; break;
173 case ROTATION_LOCKED_VERTICAL180: rotation = 3; break;
174 }
175
176 for (int i = 0; i < 4; i++) {
177 temp[i * 2] = coord[((i + rotation) & 3) * 5 + 3];
178 temp[i * 2 + 1] = coord[((i + rotation) & 3) * 5 + 4];
179 }
180
181 for (int i = 0; i < 4; i++) {
182 coord[i * 5 + 3] = temp[i * 2];
183 coord[i * 5 + 4] = temp[i * 2 + 1];
184 }
185 }
186
187 float invDestW = 1.0f / (destW * 0.5f);
188 float invDestH = 1.0f / (destH * 0.5f);
189 float halfPixelX = invDestW * 0.5f;
190 float halfPixelY = invDestH * 0.5f;
191 for (int i = 0; i < 4; i++) {
192 coord[i * 5] = coord[i * 5] * invDestW - 1.0f - halfPixelX;
193 coord[i * 5 + 1] = -(coord[i * 5 + 1] * invDestH - 1.0f - halfPixelY);
194 }
195
196 if (flags & DRAWTEX_LINEAR) {
197 dxstate.texMagFilter.set(D3DTEXF_LINEAR);
198 dxstate.texMinFilter.set(D3DTEXF_LINEAR);
199 } else {
200 dxstate.texMagFilter.set(D3DTEXF_POINT);
201 dxstate.texMinFilter.set(D3DTEXF_POINT);
202 }
203 dxstate.texMipLodBias.set(0.0f);
204 dxstate.texMaxMipLevel.set(0);
205 dxstate.blend.disable();
206 dxstate.cullMode.set(false, false);
207 dxstate.depthTest.disable();
208 dxstate.scissorTest.disable();
209 dxstate.stencilTest.disable();
210 dxstate.colorMask.set(true, true, true, true);
211 dxstate.stencilMask.set(0xFF);
212 HRESULT hr = device_->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, coord, 5 * sizeof(float));
213 if (FAILED(hr)) {
214 ERROR_LOG_REPORT(G3D, "DrawActiveTexture() failed: %08x", (uint32_t)hr);
215 }
216 }
217
218 void FramebufferManagerDX9::Bind2DShader() {
219 device_->SetVertexDeclaration(pFramebufferVertexDecl);
220 device_->SetPixelShader(pFramebufferPixelShader);
221 device_->SetVertexShader(pFramebufferVertexShader);
222 }
223
224 LPDIRECT3DSURFACE9 FramebufferManagerDX9::GetOffscreenSurface(LPDIRECT3DSURFACE9 similarSurface, VirtualFramebuffer *vfb) {
225 D3DSURFACE_DESC desc = {};
226 HRESULT hr = similarSurface->GetDesc(&desc);
227 if (FAILED(hr)) {
228 ERROR_LOG_REPORT(G3D, "Unable to get size for offscreen surface at %08x", vfb->fb_address);
229 return nullptr;
230 }
231
232 return GetOffscreenSurface(desc.Format, desc.Width, desc.Height);
233 }
234
235 LPDIRECT3DSURFACE9 FramebufferManagerDX9::GetOffscreenSurface(D3DFORMAT fmt, u32 w, u32 h) {
236 u64 key = ((u64)fmt << 32) | (w << 16) | h;
237 auto it = offscreenSurfaces_.find(key);
238 if (it != offscreenSurfaces_.end()) {
239 it->second.last_frame_used = gpuStats.numFlips;
240 return it->second.surface;
241 }
242
243 textureCacheDX9_->ForgetLastTexture();
244 LPDIRECT3DSURFACE9 offscreen = nullptr;
245 HRESULT hr = device_->CreateOffscreenPlainSurface(w, h, fmt, D3DPOOL_SYSTEMMEM, &offscreen, NULL);
246 if (FAILED(hr) || !offscreen) {
247 ERROR_LOG_REPORT(G3D, "Unable to create offscreen surface %dx%d @%d", w, h, fmt);
248 return nullptr;
249 }
250 const OffscreenSurface info = {offscreen, gpuStats.numFlips};
251 offscreenSurfaces_[key] = info;
252 return offscreen;
253 }
254
255 void FramebufferManagerDX9::BlitFramebuffer(VirtualFramebuffer *dst, int dstX, int dstY, VirtualFramebuffer *src, int srcX, int srcY, int w, int h, int bpp, const char *tag) {
256 if (!dst->fbo || !src->fbo || !useBufferedRendering_) {
257 // This can happen if we recently switched from non-buffered.
258 if (useBufferedRendering_)
259 draw_->BindFramebufferAsRenderTarget(nullptr, { Draw::RPAction::KEEP, Draw::RPAction::KEEP, Draw::RPAction::KEEP }, "BlitFramebuffer_Fail");
260 return;
261 }
262
263 float srcXFactor = (float)src->renderScaleFactor;
264 float srcYFactor = (float)src->renderScaleFactor;
265 const int srcBpp = src->format == GE_FORMAT_8888 ? 4 : 2;
266 if (srcBpp != bpp && bpp != 0) {
267 srcXFactor = (srcXFactor * bpp) / srcBpp;
268 }
269 int srcX1 = srcX * srcXFactor;
270 int srcX2 = (srcX + w) * srcXFactor;
271 int srcY1 = srcY * srcYFactor;
272 int srcY2 = (srcY + h) * srcYFactor;
273
274 float dstXFactor = (float)dst->renderScaleFactor;
275 float dstYFactor = (float)dst->renderScaleFactor;
276 const int dstBpp = dst->format == GE_FORMAT_8888 ? 4 : 2;
277 if (dstBpp != bpp && bpp != 0) {
278 dstXFactor = (dstXFactor * bpp) / dstBpp;
279 }
280 int dstX1 = dstX * dstXFactor;
281 int dstX2 = (dstX + w) * dstXFactor;
282 int dstY1 = dstY * dstYFactor;
283 int dstY2 = (dstY + h) * dstYFactor;
284
285 // Direct3D 9 doesn't support rect -> self.
286 Draw::Framebuffer *srcFBO = src->fbo;
287 if (src == dst) {
288 Draw::Framebuffer *tempFBO = GetTempFBO(TempFBO::BLIT, src->renderWidth, src->renderHeight);
289 bool result = draw_->BlitFramebuffer(
290 src->fbo, srcX1, srcY1, srcX2, srcY2,
291 tempFBO, dstX1, dstY1, dstX2, dstY2,
292 Draw::FB_COLOR_BIT, Draw::FB_BLIT_NEAREST, tag);
293 if (result) {
294 srcFBO = tempFBO;
295 }
296 }
297 bool result = draw_->BlitFramebuffer(
298 srcFBO, srcX1, srcY1, srcX2, srcY2,
299 dst->fbo, dstX1, dstY1, dstX2, dstY2,
300 Draw::FB_COLOR_BIT, Draw::FB_BLIT_NEAREST, tag);
301 if (!result) {
302 ERROR_LOG_REPORT(G3D, "fbo_blit_color failed in blit (%08x -> %08x)", src->fb_address, dst->fb_address);
303 }
304 }
305
306 void ConvertFromBGRA8888(u8 *dst, u8 *src, u32 dstStride, u32 srcStride, u32 width, u32 height, GEBufferFormat format) {
307 // Must skip stride in the cases below. Some games pack data into the cracks, like MotoGP.
308 const u32 *src32 = (const u32 *)src;
309
310 if (format == GE_FORMAT_8888) {
311 ConvertFromBGRA8888(dst, src, dstStride, srcStride, width, height, Draw::DataFormat::R8G8B8A8_UNORM);
312 } else {
313 // But here it shouldn't matter if they do intersect
314 u16 *dst16 = (u16 *)dst;
315 switch (format) {
316 case GE_FORMAT_565: // BGR 565
317 for (u32 y = 0; y < height; ++y) {
318 ConvertBGRA8888ToRGB565(dst16, src32, width);
319 src32 += srcStride;
320 dst16 += dstStride;
321 }
322 break;
323 case GE_FORMAT_5551: // ABGR 1555
324 for (u32 y = 0; y < height; ++y) {
325 ConvertBGRA8888ToRGBA5551(dst16, src32, width);
326 src32 += srcStride;
327 dst16 += dstStride;
328 }
329 break;
330 case GE_FORMAT_4444: // ABGR 4444
331 for (u32 y = 0; y < height; ++y) {
332 ConvertBGRA8888ToRGBA4444(dst16, src32, width);
333 src32 += srcStride;
334 dst16 += dstStride;
335 }
336 break;
337 case GE_FORMAT_8888:
338 case GE_FORMAT_INVALID:
339 // Not possible.
340 break;
341 }
342 }
343 }
344
345 void FramebufferManagerDX9::PackFramebufferSync_(VirtualFramebuffer *vfb, int x, int y, int w, int h) {
346 if (!vfb->fbo) {
347 ERROR_LOG_REPORT_ONCE(vfbfbozero, SCEGE, "PackFramebufferDirectx9_: vfb->fbo == 0");
348 return;
349 }
350
351 const u32 fb_address = vfb->fb_address & 0x3FFFFFFF;
352 const int dstBpp = vfb->format == GE_FORMAT_8888 ? 4 : 2;
353
354 // We always need to convert from the framebuffer native format.
355 // Right now that's always 8888.
356 DEBUG_LOG(G3D, "Reading framebuffer to mem, fb_address = %08x", fb_address);
357
358 LPDIRECT3DSURFACE9 renderTarget = (LPDIRECT3DSURFACE9)draw_->GetFramebufferAPITexture(vfb->fbo, Draw::FB_COLOR_BIT | Draw::FB_SURFACE_BIT, 0);
359 D3DSURFACE_DESC desc;
360 renderTarget->GetDesc(&desc);
361
362 LPDIRECT3DSURFACE9 offscreen = GetOffscreenSurface(renderTarget, vfb);
363 if (offscreen) {
364 HRESULT hr = device_->GetRenderTargetData(renderTarget, offscreen);
365 if (SUCCEEDED(hr)) {
366 D3DLOCKED_RECT locked;
367 u32 widthFactor = vfb->renderWidth / vfb->bufferWidth;
368 u32 heightFactor = vfb->renderHeight / vfb->bufferHeight;
369 RECT rect = {(LONG)(x * widthFactor), (LONG)(y * heightFactor), (LONG)((x + w) * widthFactor), (LONG)((y + h) * heightFactor)};
370 hr = offscreen->LockRect(&locked, &rect, D3DLOCK_READONLY);
371 if (SUCCEEDED(hr)) {
372 // TODO: Handle the other formats? We don't currently create them, I think.
373 const int dstByteOffset = (y * vfb->fb_stride + x) * dstBpp;
374 // Pixel size always 4 here because we always request BGRA8888.
375 ConvertFromBGRA8888(Memory::GetPointer(fb_address + dstByteOffset), (u8 *)locked.pBits, vfb->fb_stride, locked.Pitch / 4, w, h, vfb->format);
376 offscreen->UnlockRect();
377 } else {
378 ERROR_LOG_REPORT(G3D, "Unable to lock rect from %08x: %d,%d %dx%d of %dx%d", fb_address, (int)rect.left, (int)rect.top, (int)rect.right, (int)rect.bottom, vfb->renderWidth, vfb->renderHeight);
379 }
380 } else {
381 ERROR_LOG_REPORT(G3D, "Unable to download render target data from %08x", fb_address);
382 }
383 }
384 }
385
386 void FramebufferManagerDX9::PackDepthbuffer(VirtualFramebuffer *vfb, int x, int y, int w, int h) {
387 if (!vfb->fbo) {
388 ERROR_LOG_REPORT_ONCE(vfbfbozero, SCEGE, "PackDepthbuffer: vfb->fbo == 0");
389 return;
390 }
391
392 // We always read the depth buffer in 24_8 format.
393 const u32 z_address = vfb->z_address;
394
395 DEBUG_LOG(FRAMEBUF, "Reading depthbuffer to mem at %08x for vfb=%08x", z_address, vfb->fb_address);
396
397 LPDIRECT3DTEXTURE9 tex = (LPDIRECT3DTEXTURE9)draw_->GetFramebufferAPITexture(vfb->fbo, Draw::FB_DEPTH_BIT, 0);
398 if (tex) {
399 D3DSURFACE_DESC desc;
400 D3DLOCKED_RECT locked;
401 tex->GetLevelDesc(0, &desc);
402 RECT rect = {0, 0, (LONG)desc.Width, (LONG)desc.Height};
403 HRESULT hr = tex->LockRect(0, &locked, &rect, D3DLOCK_READONLY);
404
405 if (SUCCEEDED(hr)) {
406 const u32 *packed = (const u32 *)locked.pBits;
407 u16 *depth = (u16 *)Memory::GetPointer(z_address);
408
409 DepthScaleFactors depthScale = GetDepthScaleFactors();
410 // TODO: Optimize.
411 for (int yp = 0; yp < h; ++yp) {
412 for (int xp = 0; xp < w; ++xp) {
413 const int offset = (yp + y) * vfb->z_stride + x + xp;
414
415 float scaled = depthScale.Apply((packed[offset] & 0x00FFFFFF) * (1.0f / 16777215.0f));
416 if (scaled <= 0.0f) {
417 depth[offset] = 0;
418 } else if (scaled >= 65535.0f) {
419 depth[offset] = 65535;
420 } else {
421 depth[offset] = (int)scaled;
422 }
423 }
424 }
425
426 tex->UnlockRect(0);
427 } else {
428 ERROR_LOG_REPORT(G3D, "Unable to lock rect from depth %08x: %d,%d %dx%d of %dx%d", vfb->fb_address, (int)rect.left, (int)rect.top, (int)rect.right, (int)rect.bottom, vfb->renderWidth, vfb->renderHeight);
429 }
430 } else {
431 ERROR_LOG_REPORT(G3D, "Unable to download render target depth from %08x", vfb->fb_address);
432 }
433 }
434
435 void FramebufferManagerDX9::EndFrame() {
436 }
437
438 void FramebufferManagerDX9::DecimateFBOs() {
439 FramebufferManagerCommon::DecimateFBOs();
440 for (auto it = offscreenSurfaces_.begin(); it != offscreenSurfaces_.end(); ) {
441 int age = frameLastFramebufUsed_ - it->second.last_frame_used;
442 if (age > FBO_OLD_AGE) {
443 it->second.surface->Release();
444 it = offscreenSurfaces_.erase(it);
445 } else {
446 ++it;
447 }
448 }
449 }
450
451 void FramebufferManagerDX9::DestroyAllFBOs() {
452 FramebufferManagerCommon::DestroyAllFBOs();
453
454 for (auto &it : offscreenSurfaces_) {
455 it.second.surface->Release();
456 }
457 offscreenSurfaces_.clear();
458 }
459
460 bool FramebufferManagerDX9::GetFramebuffer(u32 fb_address, int fb_stride, GEBufferFormat fb_format, GPUDebugBuffer &buffer, int maxRes) {
461 VirtualFramebuffer *vfb = currentRenderVfb_;
462 if (!vfb) {
463 vfb = GetVFBAt(fb_address);
464 }
465
466 if (!vfb) {
467 if (!Memory::IsValidAddress(fb_address))
468 return false;
469 // If there's no vfb and we're drawing there, must be memory?
470 buffer = GPUDebugBuffer(Memory::GetPointer(fb_address), fb_stride, 512, fb_format);
471 return true;
472 }
473 LPDIRECT3DSURFACE9 renderTarget = vfb->fbo ? (LPDIRECT3DSURFACE9)draw_->GetFramebufferAPITexture(vfb->fbo, Draw::FB_COLOR_BIT | Draw::FB_SURFACE_BIT, 0) : nullptr;
474 bool success = false;
475 if (renderTarget) {
476 Draw::Framebuffer *tempFBO = nullptr;
477 int w = vfb->renderWidth, h = vfb->renderHeight;
478
479 if (maxRes > 0 && vfb->renderWidth > vfb->width * maxRes) {
480 // Let's resize. We must stretch to a render target first.
481 w = vfb->width * maxRes;
482 h = vfb->height * maxRes;
483 tempFBO = draw_->CreateFramebuffer({ w, h, 1, 1, false });
484 if (draw_->BlitFramebuffer(vfb->fbo, 0, 0, vfb->renderWidth, vfb->renderHeight, tempFBO, 0, 0, w, h, Draw::FB_COLOR_BIT, g_Config.iBufFilter == SCALE_LINEAR ? Draw::FB_BLIT_LINEAR : Draw::FB_BLIT_NEAREST, "GetFramebuffer")) {
485 renderTarget = (LPDIRECT3DSURFACE9)draw_->GetFramebufferAPITexture(tempFBO, Draw::FB_COLOR_BIT | Draw::FB_SURFACE_BIT, 0);
486 }
487 }
488
489 LPDIRECT3DSURFACE9 offscreen = GetOffscreenSurface(renderTarget, vfb);
490 if (offscreen) {
491 success = GetRenderTargetFramebuffer(renderTarget, offscreen, w, h, buffer);
492 }
493 }
494
495 return success;
496 }
497
498 bool FramebufferManagerDX9::GetOutputFramebuffer(GPUDebugBuffer &buffer) {
499 LPDIRECT3DSURFACE9 renderTarget = nullptr;
500 HRESULT hr = device_->GetRenderTarget(0, &renderTarget);
501 bool success = false;
502 if (renderTarget && SUCCEEDED(hr)) {
503 D3DSURFACE_DESC desc;
504 renderTarget->GetDesc(&desc);
505
506 LPDIRECT3DSURFACE9 offscreen = nullptr;
507 HRESULT hr = device_->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &offscreen, NULL);
508 if (offscreen && SUCCEEDED(hr)) {
509 success = GetRenderTargetFramebuffer(renderTarget, offscreen, PSP_CoreParameter().pixelWidth, PSP_CoreParameter().pixelHeight, buffer);
510 offscreen->Release();
511 }
512 renderTarget->Release();
513 }
514 return success;
515 }
516
517 bool FramebufferManagerDX9::GetRenderTargetFramebuffer(LPDIRECT3DSURFACE9 renderTarget, LPDIRECT3DSURFACE9 offscreen, int w, int h, GPUDebugBuffer &buffer) {
518 D3DSURFACE_DESC desc;
519 renderTarget->GetDesc(&desc);
520
521 bool success = false;
522 HRESULT hr = device_->GetRenderTargetData(renderTarget, offscreen);
523 if (SUCCEEDED(hr)) {
524 D3DLOCKED_RECT locked;
525 RECT rect = {0, 0, w, h};
526 hr = offscreen->LockRect(&locked, &rect, D3DLOCK_READONLY);
527 if (SUCCEEDED(hr)) {
528 // TODO: Handle the other formats? We don't currently create them, I think.
529 buffer.Allocate(locked.Pitch / 4, desc.Height, GPU_DBG_FORMAT_8888_BGRA, false);
530 memcpy(buffer.GetData(), locked.pBits, locked.Pitch * desc.Height);
531 offscreen->UnlockRect();
532 success = true;
533 }
534 }
535
536 return success;
537 }
538
539 bool FramebufferManagerDX9::GetDepthbuffer(u32 fb_address, int fb_stride, u32 z_address, int z_stride, GPUDebugBuffer &buffer) {
540 VirtualFramebuffer *vfb = currentRenderVfb_;
541 if (!vfb) {
542 vfb = GetVFBAt(fb_address);
543 }
544
545 if (!vfb) {
546 // If there's no vfb and we're drawing there, must be memory?
547 buffer = GPUDebugBuffer(Memory::GetPointer(z_address), z_stride, 512, GPU_DBG_FORMAT_16BIT);
548 return true;
549 }
550
551 bool success = false;
552 LPDIRECT3DTEXTURE9 tex = (LPDIRECT3DTEXTURE9)draw_->GetFramebufferAPITexture(vfb->fbo, Draw::FB_DEPTH_BIT, 0);
553 if (tex) {
554 D3DSURFACE_DESC desc;
555 D3DLOCKED_RECT locked;
556 tex->GetLevelDesc(0, &desc);
557 RECT rect = {0, 0, (LONG)desc.Width, (LONG)desc.Height};
558 HRESULT hr = tex->LockRect(0, &locked, &rect, D3DLOCK_READONLY);
559
560 if (SUCCEEDED(hr)) {
561 GPUDebugBufferFormat fmt = GPU_DBG_FORMAT_24BIT_8X;
562 if (gstate_c.Supports(GPU_SCALE_DEPTH_FROM_24BIT_TO_16BIT)) {
563 fmt = GPU_DBG_FORMAT_24BIT_8X_DIV_256;
564 }
565 int pixelSize = 4;
566
567 buffer.Allocate(locked.Pitch / pixelSize, desc.Height, fmt, false);
568 memcpy(buffer.GetData(), locked.pBits, locked.Pitch * desc.Height);
569 success = true;
570 tex->UnlockRect(0);
571 }
572 }
573
574 return success;
575 }
576
577 bool FramebufferManagerDX9::GetStencilbuffer(u32 fb_address, int fb_stride, GPUDebugBuffer &buffer) {
578 VirtualFramebuffer *vfb = currentRenderVfb_;
579 if (!vfb) {
580 vfb = GetVFBAt(fb_address);
581 }
582
583 if (!vfb) {
584 // If there's no vfb and we're drawing there, must be memory?
585 buffer = GPUDebugBuffer(Memory::GetPointer(vfb->z_address), vfb->z_stride, 512, GPU_DBG_FORMAT_16BIT);
586 return true;
587 }
588
589 bool success = false;
590 LPDIRECT3DTEXTURE9 tex = (LPDIRECT3DTEXTURE9)draw_->GetFramebufferAPITexture(vfb->fbo, Draw::FB_DEPTH_BIT, 0);
591 if (tex) {
592 D3DSURFACE_DESC desc;
593 D3DLOCKED_RECT locked;
594 tex->GetLevelDesc(0, &desc);
595 RECT rect = {0, 0, (LONG)desc.Width, (LONG)desc.Height};
596 HRESULT hr = tex->LockRect(0, &locked, &rect, D3DLOCK_READONLY);
597
598 if (SUCCEEDED(hr)) {
599 GPUDebugBufferFormat fmt = GPU_DBG_FORMAT_24X_8BIT;
600 int pixelSize = 4;
601
602 buffer.Allocate(locked.Pitch / pixelSize, desc.Height, fmt, false);
603 memcpy(buffer.GetData(), locked.pBits, locked.Pitch * desc.Height);
604 success = true;
605 tex->UnlockRect(0);
606 }
607 }
608
609 return success;
610 }
611
612 } // namespace DX9
613