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 #pragma once
19 
20 #include <unordered_map>
21 
22 #include <d3d9.h>
23 
24 // Keeps track of allocated FBOs.
25 // Also provides facilities for drawing and later converting raw
26 // pixel data.
27 
28 #include "GPU/GPUCommon.h"
29 #include "GPU/Common/FramebufferManagerCommon.h"
30 
31 namespace DX9 {
32 
33 class TextureCacheDX9;
34 class DrawEngineDX9;
35 class ShaderManagerDX9;
36 
37 class FramebufferManagerDX9 : public FramebufferManagerCommon {
38 public:
39 	FramebufferManagerDX9(Draw::DrawContext *draw);
40 	~FramebufferManagerDX9();
41 
42 	void SetTextureCache(TextureCacheDX9 *tc);
43 	void SetShaderManager(ShaderManagerDX9 *sm);
44 	void SetDrawEngine(DrawEngineDX9 *td);
45 	void 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) override;
46 
47 	void DestroyAllFBOs() override;
48 
49 	void EndFrame();
50 
51 	virtual bool NotifyStencilUpload(u32 addr, int size, StencilUpload flags = StencilUpload::NEEDS_CLEAR) override;
52 
53 	bool GetFramebuffer(u32 fb_address, int fb_stride, GEBufferFormat format, GPUDebugBuffer &buffer, int maxRes) override;
54 	bool GetDepthbuffer(u32 fb_address, int fb_stride, u32 z_address, int z_stride, GPUDebugBuffer &buffer) override;
55 	bool GetStencilbuffer(u32 fb_address, int fb_stride, GPUDebugBuffer &buffer) override;
56 	bool GetOutputFramebuffer(GPUDebugBuffer &buffer) override;
57 
58 	LPDIRECT3DSURFACE9 GetOffscreenSurface(LPDIRECT3DSURFACE9 similarSurface, VirtualFramebuffer *vfb);
59 	LPDIRECT3DSURFACE9 GetOffscreenSurface(D3DFORMAT fmt, u32 w, u32 h);
60 
61 protected:
62 	void Bind2DShader() override;
63 	void DecimateFBOs() override;
64 
65 	// Used by ReadFramebufferToMemory and later framebuffer block copies
66 	void BlitFramebuffer(VirtualFramebuffer *dst, int dstX, int dstY, VirtualFramebuffer *src, int srcX, int srcY, int w, int h, int bpp, const char *tag) override;
67 
68 private:
69 	void PackFramebufferSync_(VirtualFramebuffer *vfb, int x, int y, int w, int h) override;
70 	void PackDepthbuffer(VirtualFramebuffer *vfb, int x, int y, int w, int h);
71 	bool GetRenderTargetFramebuffer(LPDIRECT3DSURFACE9 renderTarget, LPDIRECT3DSURFACE9 offscreen, int w, int h, GPUDebugBuffer &buffer);
72 
73 	LPDIRECT3DDEVICE9 device_;
74 	LPDIRECT3DDEVICE9 deviceEx_;
75 
76 	LPDIRECT3DVERTEXSHADER9 pFramebufferVertexShader = nullptr;
77 	LPDIRECT3DPIXELSHADER9 pFramebufferPixelShader = nullptr;
78 	LPDIRECT3DVERTEXDECLARATION9 pFramebufferVertexDecl = nullptr;
79 
80 	LPDIRECT3DPIXELSHADER9 stencilUploadPS_ = nullptr;
81 	LPDIRECT3DVERTEXSHADER9 stencilUploadVS_ = nullptr;
82 	bool stencilUploadFailed_ = false;
83 
84 	LPDIRECT3DTEXTURE9 nullTex_ = nullptr;
85 
86 	TextureCacheDX9 *textureCacheDX9_;
87 	ShaderManagerDX9 *shaderManagerDX9_;
88 	DrawEngineDX9 *drawEngineD3D9_;
89 
90 	struct OffscreenSurface {
91 		LPDIRECT3DSURFACE9 surface;
92 		int last_frame_used;
93 	};
94 
95 	std::unordered_map<u64, OffscreenSurface> offscreenSurfaces_;
96 };
97 
98 };
99