Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/GPU/Vulkan/ShaderManagerVulkan.h
5672 views
1
// Copyright (c) 2016- 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 <cstdio>
21
#include <cstdint>
22
#include <mutex>
23
24
#include "Common/Thread/Promise.h"
25
#include "Common/Data/Collections/Hashmaps.h"
26
#include "Common/GPU/Vulkan/VulkanMemory.h"
27
#include "GPU/Common/ShaderCommon.h"
28
#include "GPU/Common/ShaderId.h"
29
#include "GPU/Common/VertexShaderGenerator.h"
30
#include "GPU/Common/FragmentShaderGenerator.h"
31
#include "GPU/Vulkan/VulkanUtil.h"
32
#include "Common/Math/lin/matrix4x4.h"
33
#include "GPU/Common/ShaderUniforms.h"
34
35
#include "Common/File/FileUtil.h"
36
37
class VulkanContext;
38
class DrawEngineVulkan;
39
class VulkanPushPool;
40
41
class VulkanFragmentShader {
42
public:
43
VulkanFragmentShader(VulkanContext *vulkan, FShaderID id, FragmentShaderFlags flags, const char *code);
44
~VulkanFragmentShader();
45
46
const std::string &source() const { return source_; }
47
48
std::string GetShaderString(DebugShaderStringType type) const;
49
Promise<VkShaderModule> *GetModule() { return module_; }
50
const FShaderID &GetID() const { return id_; }
51
52
FragmentShaderFlags Flags() const { return flags_; }
53
54
protected:
55
Promise<VkShaderModule> *module_ = nullptr;
56
57
VulkanContext *vulkan_;
58
std::string source_;
59
bool failed_ = false;
60
FShaderID id_;
61
FragmentShaderFlags flags_;
62
};
63
64
class VulkanVertexShader {
65
public:
66
VulkanVertexShader(VulkanContext *vulkan, VShaderID id, VertexShaderFlags flags, const char *code, bool useHWTransform);
67
~VulkanVertexShader();
68
69
const std::string &source() const { return source_; }
70
71
bool UseHWTransform() const { return useHWTransform_; } // TODO: Roll into flags
72
VertexShaderFlags Flags() const { return flags_; }
73
74
std::string GetShaderString(DebugShaderStringType type) const;
75
Promise<VkShaderModule> *GetModule() { return module_; }
76
const VShaderID &GetID() const { return id_; }
77
78
protected:
79
Promise<VkShaderModule> *module_ = nullptr;
80
81
VulkanContext *vulkan_;
82
std::string source_;
83
bool useHWTransform_;
84
VShaderID id_;
85
VertexShaderFlags flags_;
86
};
87
88
class VulkanGeometryShader {
89
public:
90
VulkanGeometryShader(VulkanContext *vulkan, GShaderID id, const char *code);
91
~VulkanGeometryShader();
92
93
const std::string &source() const { return source_; }
94
95
std::string GetShaderString(DebugShaderStringType type) const;
96
97
Promise<VkShaderModule> *GetModule() const { return module_; }
98
const GShaderID &GetID() { return id_; }
99
100
protected:
101
Promise<VkShaderModule> *module_ = nullptr;
102
103
VulkanContext *vulkan_;
104
std::string source_;
105
GShaderID id_;
106
};
107
108
struct Uniforms {
109
// Uniform block scratchpad. These (the relevant ones) are copied to the current pushbuffer at draw time.
110
UB_VS_FS_Base ub_base{};
111
UB_VS_Lights ub_lights{};
112
UB_VS_Bones ub_bones{};
113
};
114
115
class ShaderManagerVulkan : public ShaderManagerCommon {
116
public:
117
ShaderManagerVulkan(Draw::DrawContext *draw);
118
~ShaderManagerVulkan();
119
120
void DeviceLost() override;
121
void DeviceRestore(Draw::DrawContext *draw) override;
122
123
void GetShaders(int prim, u32 vertexType, VulkanVertexShader **vshader, VulkanFragmentShader **fshader, VulkanGeometryShader **gshader, const ComputedPipelineState &pipelineState, bool useHWTransform, bool useHWTessellation, bool weightsAsFloat, bool useSkinInDecode);
124
void ClearShaders() override;
125
void DirtyLastShader() override;
126
127
int GetNumVertexShaders() const { return (int)vsCache_.size(); }
128
int GetNumFragmentShaders() const { return (int)fsCache_.size(); }
129
int GetNumGeometryShaders() const { return (int)gsCache_.size(); }
130
131
// Used for saving/loading the cache. Don't need to be particularly fast.
132
VulkanVertexShader *GetVertexShaderFromID(VShaderID id) { return vsCache_.GetOrNull(id); }
133
VulkanFragmentShader *GetFragmentShaderFromID(FShaderID id) { return fsCache_.GetOrNull(id); }
134
VulkanGeometryShader *GetGeometryShaderFromID(GShaderID id) { return gsCache_.GetOrNull(id); }
135
136
VulkanVertexShader *GetVertexShaderFromModule(VkShaderModule module);
137
VulkanFragmentShader *GetFragmentShaderFromModule(VkShaderModule module);
138
VulkanGeometryShader *GetGeometryShaderFromModule(VkShaderModule module);
139
140
std::vector<std::string> DebugGetShaderIDs(DebugShaderType type) override;
141
std::string DebugGetShaderString(std::string id, DebugShaderType type, DebugShaderStringType stringType) override;
142
143
uint64_t UpdateUniforms(bool useBufferedRendering);
144
145
// TODO: Avoid copying these buffers if same as last draw, can still point to it assuming we're still in the same pushbuffer.
146
// Applies dirty changes and copies the buffer.
147
bool IsBaseDirty() { return true; }
148
bool IsLightDirty() { return true; }
149
bool IsBoneDirty() { return true; }
150
151
uint32_t PushBaseBuffer(VulkanPushPool *dest, VkBuffer *buf) const {
152
return dest->Push(&uniforms_->ub_base, sizeof(uniforms_->ub_base), uboAlignment_, buf);
153
}
154
uint32_t PushLightBuffer(VulkanPushPool *dest, VkBuffer *buf) const {
155
return dest->Push(&uniforms_->ub_lights, sizeof(uniforms_->ub_lights), uboAlignment_, buf);
156
}
157
// TODO: Only push half the bone buffer if we only have four bones.
158
uint32_t PushBoneBuffer(VulkanPushPool *dest, VkBuffer *buf) const {
159
return dest->Push(&uniforms_->ub_bones, sizeof(uniforms_->ub_bones), uboAlignment_, buf);
160
}
161
162
static bool LoadCacheFlags(FILE *f, DrawEngineVulkan *drawEngine);
163
bool LoadCache(FILE *f);
164
void SaveCache(FILE *f, DrawEngineVulkan *drawEngine);
165
166
private:
167
void Clear();
168
169
ShaderLanguageDesc compat_;
170
171
typedef DenseHashMap<FShaderID, VulkanFragmentShader *> FSCache;
172
FSCache fsCache_;
173
174
typedef DenseHashMap<VShaderID, VulkanVertexShader *> VSCache;
175
VSCache vsCache_;
176
177
typedef DenseHashMap<GShaderID, VulkanGeometryShader *> GSCache;
178
GSCache gsCache_;
179
180
char *codeBuffer_;
181
182
uint64_t uboAlignment_;
183
184
Uniforms *uniforms_;
185
186
VulkanFragmentShader *lastFShader_ = nullptr;
187
VulkanVertexShader *lastVShader_ = nullptr;
188
VulkanGeometryShader *lastGShader_ = nullptr;
189
190
FShaderID lastFSID_;
191
VShaderID lastVSID_;
192
GShaderID lastGSID_;
193
};
194
195