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/GPU_DX9.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 <string>
19
20
#include "Common/Serialize/Serializer.h"
21
#include "Common/GraphicsContext.h"
22
#include "Common/System/OSD.h"
23
#include "Common/Profiler/Profiler.h"
24
#include "Common/Data/Text/I18n.h"
25
#include "Core/Debugger/Breakpoints.h"
26
#include "Core/MemMapHelpers.h"
27
#include "Core/MIPS/MIPS.h"
28
#include "Core/Config.h"
29
#include "Core/ConfigValues.h"
30
#include "Core/System.h"
31
32
#include "Common/GPU/D3D9/D3D9StateCache.h"
33
34
#include "GPU/GPUState.h"
35
#include "GPU/ge_constants.h"
36
#include "GPU/GeDisasm.h"
37
38
#include "GPU/Common/FramebufferManagerCommon.h"
39
#include "GPU/Directx9/ShaderManagerDX9.h"
40
#include "GPU/Directx9/GPU_DX9.h"
41
#include "GPU/Directx9/FramebufferManagerDX9.h"
42
#include "GPU/Directx9/DrawEngineDX9.h"
43
#include "GPU/Directx9/TextureCacheDX9.h"
44
45
GPU_DX9::GPU_DX9(GraphicsContext *gfxCtx, Draw::DrawContext *draw)
46
: GPUCommonHW(gfxCtx, draw),
47
drawEngine_(draw) {
48
device_ = (LPDIRECT3DDEVICE9)draw->GetNativeObject(Draw::NativeObject::DEVICE);
49
deviceEx_ = (LPDIRECT3DDEVICE9EX)draw->GetNativeObject(Draw::NativeObject::DEVICE_EX);
50
51
shaderManagerDX9_ = new ShaderManagerDX9(draw, device_);
52
framebufferManagerDX9_ = new FramebufferManagerDX9(draw);
53
framebufferManager_ = framebufferManagerDX9_;
54
textureCacheDX9_ = new TextureCacheDX9(draw, framebufferManager_->GetDraw2D());
55
textureCache_ = textureCacheDX9_;
56
drawEngineCommon_ = &drawEngine_;
57
shaderManager_ = shaderManagerDX9_;
58
59
drawEngine_.SetShaderManager(shaderManagerDX9_);
60
drawEngine_.SetTextureCache(textureCacheDX9_);
61
drawEngine_.SetFramebufferManager(framebufferManagerDX9_);
62
drawEngine_.Init();
63
framebufferManagerDX9_->SetTextureCache(textureCacheDX9_);
64
framebufferManagerDX9_->SetShaderManager(shaderManagerDX9_);
65
framebufferManagerDX9_->SetDrawEngine(&drawEngine_);
66
framebufferManagerDX9_->Init(msaaLevel_);
67
textureCacheDX9_->SetFramebufferManager(framebufferManagerDX9_);
68
textureCacheDX9_->SetShaderManager(shaderManagerDX9_);
69
70
// Sanity check gstate
71
if ((int *)&gstate.transferstart - (int *)&gstate != 0xEA) {
72
ERROR_LOG(Log::G3D, "gstate has drifted out of sync!");
73
}
74
75
// No need to flush before the tex scale/offset commands if we are baking
76
// the tex scale/offset into the vertices anyway.
77
UpdateCmdInfo();
78
gstate_c.SetUseFlags(CheckGPUFeatures());
79
80
BuildReportingInfo();
81
82
// Some of our defaults are different from hw defaults, let's assert them.
83
// We restore each frame anyway, but here is convenient for tests.
84
dxstate.Restore();
85
textureCache_->NotifyConfigChanged();
86
87
if (g_Config.bHardwareTessellation) {
88
// Disable hardware tessellation bacause DX9 is still unsupported.
89
ERROR_LOG(Log::G3D, "Hardware Tessellation is unsupported, falling back to software tessellation");
90
auto gr = GetI18NCategory(I18NCat::GRAPHICS);
91
// TODO: Badly formulated
92
g_OSD.Show(OSDType::MESSAGE_WARNING, gr->T("Turn off Hardware Tessellation - unsupported"));
93
}
94
}
95
96
u32 GPU_DX9::CheckGPUFeatures() const {
97
u32 features = GPUCommonHW::CheckGPUFeatures();
98
features |= GPU_USE_16BIT_FORMATS;
99
features |= GPU_USE_TEXTURE_LOD_CONTROL;
100
101
// Accurate depth is required because the Direct3D API does not support inverse Z.
102
// So we cannot incorrectly use the viewport transform as the depth range on Direct3D.
103
features |= GPU_USE_ACCURATE_DEPTH;
104
105
return CheckGPUFeaturesLate(features);
106
}
107
108
void GPU_DX9::DeviceLost() {
109
GPUCommonHW::DeviceLost();
110
}
111
112
void GPU_DX9::DeviceRestore(Draw::DrawContext *draw) {
113
GPUCommonHW::DeviceRestore(draw);
114
}
115
116
void GPU_DX9::ReapplyGfxState() {
117
dxstate.Restore();
118
GPUCommonHW::ReapplyGfxState();
119
}
120
121
void GPU_DX9::BeginHostFrame() {
122
GPUCommonHW::BeginHostFrame();
123
124
textureCache_->StartFrame();
125
drawEngine_.BeginFrame();
126
127
shaderManagerDX9_->DirtyLastShader();
128
129
framebufferManager_->BeginFrame();
130
131
if (gstate_c.useFlagsChanged) {
132
// TODO: It'd be better to recompile them in the background, probably?
133
// This most likely means that saw equal depth changed.
134
WARN_LOG(Log::G3D, "Shader use flags changed, clearing all shaders and depth buffers");
135
shaderManager_->ClearShaders();
136
framebufferManager_->ClearAllDepthBuffers();
137
gstate_c.useFlagsChanged = false;
138
}
139
}
140
141
void GPU_DX9::FinishDeferred() {
142
// This finishes reading any vertex data that is pending.
143
drawEngine_.FinishDeferred();
144
}
145
146
void GPU_DX9::GetStats(char *buffer, size_t bufsize) {
147
size_t offset = FormatGPUStatsCommon(buffer, bufsize);
148
buffer += offset;
149
bufsize -= offset;
150
if ((int)bufsize < 0)
151
return;
152
snprintf(buffer, bufsize,
153
"Vertex, Fragment shaders loaded: %i, %i\n",
154
shaderManagerDX9_->GetNumVertexShaders(),
155
shaderManagerDX9_->GetNumFragmentShaders()
156
);
157
}
158
159