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