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