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/D3D11/GPU_D3D11.cpp
Views: 1401
1
// Copyright (c) 2017- 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/Log.h"
21
#include "Common/Serialize/Serializer.h"
22
#include "Common/GraphicsContext.h"
23
#include "Common/System/System.h"
24
#include "Common/Profiler/Profiler.h"
25
#include "Common/Data/Text/I18n.h"
26
#include "Core/Debugger/Breakpoints.h"
27
#include "Core/MemMapHelpers.h"
28
#include "Core/MIPS/MIPS.h"
29
#include "Core/Config.h"
30
#include "Core/System.h"
31
32
#include "GPU/GPUState.h"
33
#include "GPU/ge_constants.h"
34
#include "GPU/GeDisasm.h"
35
36
#include "GPU/Common/FramebufferManagerCommon.h"
37
#include "GPU/D3D11/ShaderManagerD3D11.h"
38
#include "GPU/D3D11/GPU_D3D11.h"
39
#include "GPU/D3D11/FramebufferManagerD3D11.h"
40
#include "GPU/D3D11/DrawEngineD3D11.h"
41
#include "GPU/D3D11/TextureCacheD3D11.h"
42
#include "GPU/D3D11/D3D11Util.h"
43
44
GPU_D3D11::GPU_D3D11(GraphicsContext *gfxCtx, Draw::DrawContext *draw)
45
: GPUCommonHW(gfxCtx, draw), drawEngine_(draw,
46
(ID3D11Device *)draw->GetNativeObject(Draw::NativeObject::DEVICE),
47
(ID3D11DeviceContext *)draw->GetNativeObject(Draw::NativeObject::CONTEXT)) {
48
device_ = (ID3D11Device *)draw->GetNativeObject(Draw::NativeObject::DEVICE);
49
context_ = (ID3D11DeviceContext *)draw->GetNativeObject(Draw::NativeObject::CONTEXT);
50
D3D_FEATURE_LEVEL featureLevel = (D3D_FEATURE_LEVEL)draw->GetNativeObject(Draw::NativeObject::FEATURE_LEVEL);
51
52
stockD3D11.Create(device_);
53
54
shaderManagerD3D11_ = new ShaderManagerD3D11(draw, device_, context_, featureLevel);
55
framebufferManagerD3D11_ = new FramebufferManagerD3D11(draw);
56
framebufferManager_ = framebufferManagerD3D11_;
57
textureCacheD3D11_ = new TextureCacheD3D11(draw, framebufferManager_->GetDraw2D());
58
textureCache_ = textureCacheD3D11_;
59
drawEngineCommon_ = &drawEngine_;
60
shaderManager_ = shaderManagerD3D11_;
61
drawEngine_.SetShaderManager(shaderManagerD3D11_);
62
drawEngine_.SetTextureCache(textureCacheD3D11_);
63
drawEngine_.SetFramebufferManager(framebufferManagerD3D11_);
64
drawEngine_.Init();
65
framebufferManagerD3D11_->SetTextureCache(textureCacheD3D11_);
66
framebufferManagerD3D11_->SetShaderManager(shaderManagerD3D11_);
67
framebufferManagerD3D11_->SetDrawEngine(&drawEngine_);
68
framebufferManagerD3D11_->Init(msaaLevel_);
69
textureCacheD3D11_->SetFramebufferManager(framebufferManagerD3D11_);
70
textureCacheD3D11_->SetShaderManager(shaderManagerD3D11_);
71
72
// Sanity check gstate
73
if ((int *)&gstate.transferstart - (int *)&gstate != 0xEA) {
74
ERROR_LOG(Log::G3D, "gstate has drifted out of sync!");
75
}
76
77
// No need to flush before the tex scale/offset commands if we are baking
78
// the tex scale/offset into the vertices anyway.
79
UpdateCmdInfo();
80
gstate_c.SetUseFlags(CheckGPUFeatures());
81
82
BuildReportingInfo();
83
84
// Some of our defaults are different from hw defaults, let's assert them.
85
// We restore each frame anyway, but here is convenient for tests.
86
textureCache_->NotifyConfigChanged();
87
}
88
89
GPU_D3D11::~GPU_D3D11() {
90
stockD3D11.Destroy();
91
}
92
93
u32 GPU_D3D11::CheckGPUFeatures() const {
94
u32 features = GPUCommonHW::CheckGPUFeatures();
95
96
// Accurate depth is required because the Direct3D API does not support inverse Z.
97
// So we cannot incorrectly use the viewport transform as the depth range on Direct3D.
98
features |= GPU_USE_ACCURATE_DEPTH;
99
100
features |= GPU_USE_TEXTURE_FLOAT;
101
features |= GPU_USE_INSTANCE_RENDERING;
102
features |= GPU_USE_TEXTURE_LOD_CONTROL;
103
104
uint32_t fmt4444 = draw_->GetDataFormatSupport(Draw::DataFormat::A4R4G4B4_UNORM_PACK16);
105
uint32_t fmt1555 = draw_->GetDataFormatSupport(Draw::DataFormat::A1R5G5B5_UNORM_PACK16);
106
uint32_t fmt565 = draw_->GetDataFormatSupport(Draw::DataFormat::R5G6B5_UNORM_PACK16);
107
if ((fmt4444 & Draw::FMT_TEXTURE) && (fmt565 & Draw::FMT_TEXTURE) && (fmt1555 & Draw::FMT_TEXTURE)) {
108
features |= GPU_USE_16BIT_FORMATS;
109
}
110
111
return CheckGPUFeaturesLate(features);
112
}
113
114
void GPU_D3D11::DeviceLost() {
115
draw_->Invalidate(InvalidationFlags::CACHED_RENDER_STATE);
116
// Simply drop all caches and textures.
117
// FBOs appear to survive? Or no?
118
shaderManager_->ClearShaders();
119
drawEngine_.ClearInputLayoutMap();
120
textureCache_->Clear(false);
121
122
GPUCommonHW::DeviceLost();
123
}
124
125
void GPU_D3D11::DeviceRestore(Draw::DrawContext *draw) {
126
GPUCommonHW::DeviceRestore(draw);
127
// Nothing needed.
128
}
129
130
void GPU_D3D11::BeginHostFrame() {
131
GPUCommonHW::BeginHostFrame();
132
133
textureCache_->StartFrame();
134
drawEngine_.BeginFrame();
135
136
shaderManager_->DirtyLastShader();
137
138
framebufferManager_->BeginFrame();
139
gstate_c.Dirty(DIRTY_PROJTHROUGHMATRIX);
140
141
if (gstate_c.useFlagsChanged) {
142
// TODO: It'd be better to recompile them in the background, probably?
143
// This most likely means that saw equal depth changed.
144
WARN_LOG(Log::G3D, "Shader use flags changed, clearing all shaders and depth buffers");
145
shaderManager_->ClearShaders();
146
framebufferManager_->ClearAllDepthBuffers();
147
drawEngine_.ClearInputLayoutMap();
148
gstate_c.useFlagsChanged = false;
149
}
150
}
151
152
void GPU_D3D11::FinishDeferred() {
153
// This finishes reading any vertex data that is pending.
154
drawEngine_.FinishDeferred();
155
}
156
157
void GPU_D3D11::GetStats(char *buffer, size_t bufsize) {
158
size_t offset = FormatGPUStatsCommon(buffer, bufsize);
159
buffer += offset;
160
bufsize -= offset;
161
if ((int)bufsize < 0)
162
return;
163
snprintf(buffer, bufsize,
164
"Vertex, Fragment shaders loaded: %d, %d\n",
165
shaderManagerD3D11_->GetNumVertexShaders(),
166
shaderManagerD3D11_->GetNumFragmentShaders()
167
);
168
}
169
170