OpenGothic
Open source reimplementation of Gothic I and II
Loading...
Searching...
No Matches
resources.h
Go to the documentation of this file.
1#pragma once
2
3#include <Tempest/Font>
4#include <Tempest/Texture2d>
5#include <Tempest/Device>
6#include <Tempest/SoundDevice>
7
8#include <zenkit/Vfs.hh>
9#include <zenkit/world/VobTree.hh>
10
11#include <tuple>
12#include <string_view>
13#include <map>
14
15#include "graphics/material.h"
16#include "sound/soundfx.h"
17
18struct DmSegment;
19struct DmLoader;
20
21class StaticMesh;
22class ProtoMesh;
23class Skeleton;
24class Animation;
25class AttachBinder;
26class PfxEmitterMesh;
27class GthFont;
28
29namespace Dx8 {
30class DirectMusic;
31class PatternList;
32}
33
34class Resources final {
35 public:
36 explicit Resources(Tempest::Device& device);
37 ~Resources();
38
39 enum class FontType : uint8_t {
40 Normal,
41 Hi,
43 Yellow,
44 Red
45 };
46
47 enum {
50 };
51
52 static const size_t MAX_NUM_SKELETAL_NODES = 96;
53 static const size_t MAX_MORPH_LAYERS = 4;
54
55 struct Vertex {
56 float pos[3];
57 float norm[3];
58 float uv[2];
59 uint32_t color;
60 };
61
62 struct VertexA {
63 float norm[3];
64 float uv[2];
65 uint32_t color/*unused*/;
66 float pos[4][3];
67 uint8_t boneId[4];
68 float weights[4];
69 };
70
71 struct VertexFsq {
72 float pos[2];
73 };
74
75 struct VertexL {
76 Tempest::Vec3 pos;
77 Tempest::Vec4 cen;
78 Tempest::Vec3 color;
79 };
80
81 using VobTree = std::vector<std::shared_ptr<zenkit::VirtualObject>>;
82
83 static Tempest::Device& device() { return inst->dev; }
84 static const char* renderer();
85 static void mountWork(const std::filesystem::path& path);
86 static void loadVdfs(const std::vector<std::u16string> &modvdfs, bool modFilter);
87
88 static const Tempest::Sampler& shadowSampler();
89
90 static const GthFont& dialogFont(const float scale);
91 static const GthFont& font(const float scale);
92 static const GthFont& font(FontType type, const float scale);
93 static const GthFont& font(std::string_view fname, FontType type, const float scale);
94
95 static const Tempest::Texture2d& fallbackTexture();
96 static const Tempest::Texture2d& fallbackBlack();
97 static auto fallbackImage() -> const Tempest::StorageImage&;
98 static Tempest::Texture2d loadTextureUncached(std::string_view name, bool forceMips = false);
99 static const Tempest::Texture2d* loadTexture(std::string_view name, bool forceMips = false);
100 static const Tempest::Texture2d* loadTexture(Tempest::Color color);
101 static const Tempest::Texture2d* loadTexture(std::string_view name, int32_t v, int32_t c);
102 static Tempest::Texture2d loadTexturePm(const Tempest::Pixmap& pm);
103 static auto loadTextureAnim(std::string_view name) -> std::vector<const Tempest::Texture2d*>;
104 static Material loadMaterial(const zenkit::Material& src, bool enableAlphaTest);
105
106 static const AttachBinder* bindMesh (const ProtoMesh& anim, const Skeleton& s);
107 static const ProtoMesh* loadMesh (std::string_view name);
108 static const PfxEmitterMesh* loadEmiterMesh (std::string_view name);
109 static const Skeleton* loadSkeleton (std::string_view name);
110 static const Animation* loadAnimation (std::string_view name);
111 static Tempest::Sound loadSoundBuffer(std::string_view name);
112
113 static Dx8::PatternList loadDxMusic(std::string_view name);
114 static DmSegment* loadMusicSegment(char const* name);
115 static const ProtoMesh* decalMesh(const zenkit::VisualDecal& decal);
116
117 static const VobTree* loadVobBundle(std::string_view name);
118
119 template<class V>
120 static Tempest::VertexBuffer<V> vbo(const V* data,size_t sz){ return inst->dev.vbo(data,sz); }
121
122 template<class I>
123 static Tempest::IndexBuffer<I> ibo(const I* data,size_t sz){ return inst->dev.ibo(data,sz); }
124
125 static Tempest::StorageBuffer ssbo(const void* data, size_t size) { return inst->dev.ssbo(data,size); }
126 static Tempest::StorageBuffer ssbo(Tempest::Uninitialized_t, size_t size) { return inst->dev.ssbo(Tempest::Uninitialized,size); }
127
128 template<class V, class I>
129 static Tempest::AccelerationStructure
130 blas(const Tempest::VertexBuffer<V>& b,
131 const Tempest::IndexBuffer<I>& i,
132 size_t offset, size_t size){
133 if(!inst->dev.properties().raytracing.rayQuery)
134 return Tempest::AccelerationStructure();
135 return inst->dev.blas(b,i,offset,size);
136 }
137
138 static void resetRecycled(uint8_t fId);
139 static void recycle(Tempest::DescriptorArray&& arr);
140 static void recycle(Tempest::StorageBuffer&& ssbo);
141 static void recycle(Tempest::StorageImage&& img);
142 static void recycle(Tempest::Attachment&& img);
143 static void recycle(Tempest::ZBuffer&& img);
144 static void recycle(Tempest::AccelerationStructure&& rtas);
145
146 static std::vector<uint8_t> getFileData(std::string_view name);
147 static bool getFileData(std::string_view name, std::vector<uint8_t>& dat);
148 static std::unique_ptr<zenkit::Read> getFileBuffer(std::string_view name);
149 static auto openReader(std::string_view name, std::unique_ptr<zenkit::Read>& read) -> std::unique_ptr<zenkit::ReadArchive>;
150 static bool hasFile (std::string_view fname);
151
152 static const zenkit::Vfs& vdfsIndex();
153
154 static const Tempest::IndexBuffer<uint16_t>& cubeIbo();
155
156 private:
157 static Resources* inst;
158
159 struct Archive {
160 std::u16string name;
161 int64_t time=0;
162 uint16_t ord=0;
163 bool isMod=false;
164 };
165
166 struct DecalK {
167 Material mat;
168 float sX = 1;
169 float sY = 1;
170 bool decal2Sided = false;
171 bool operator == (const DecalK& other) const {
172 return mat ==other.mat &&
173 sX ==other.sX &&
174 sY ==other.sY &&
175 decal2Sided==other.decal2Sided;
176 }
177 };
178
179 using TextureCache = std::unordered_map<std::string,std::unique_ptr<Tempest::Texture2d>>;
180
181 int64_t vdfTimestamp(const std::u16string& name);
182 void detectVdf(std::vector<Archive>& ret, const std::u16string& root);
183
184 Tempest::Texture2d* implLoadTexture(std::string_view cname, bool forceMips);
185 Tempest::Texture2d implLoadTextureUncached(std::string_view name, bool forceMips);
186 Tempest::Texture2d implLoadTextureUncached(std::string_view name, zenkit::Read& data, bool forceMips);
187 ProtoMesh* implLoadMesh(std::string_view name);
188 std::unique_ptr<ProtoMesh> implLoadMeshMain(std::string name);
189 std::unique_ptr<Animation> implLoadAnimation(std::string name);
190 ProtoMesh* implDecalMesh(const zenkit::VisualDecal& decal);
191 Tempest::Sound implLoadSoundBuffer(std::string_view name);
192 Dx8::PatternList implLoadDxMusic(std::string_view name);
193 DmSegment* implLoadMusicSegment(char const* name);
194 GthFont& implLoadFont(std::string_view fname, FontType type, const float scale);
195 PfxEmitterMesh* implLoadEmiterMesh(std::string_view name);
196 const VobTree* implLoadVobBundle(std::string_view name);
197
198 Tempest::VertexBuffer<Vertex> sphere(int passCount, float R);
199
200 Tempest::Texture2d fallback, fbZero;
201 Tempest::StorageImage fbImg;
202
203 using BindK = std::tuple<const Skeleton*,const ProtoMesh*>;
204 using FontK = std::tuple<const std::string,FontType,float>;
205
206 struct Hash {
207 size_t operator()(const BindK& b) const {
208 return std::uintptr_t(std::get<0>(b));
209 }
210 size_t operator()(const DecalK& b) const {
211 return std::uintptr_t(b.mat.tex);
212 }
213 size_t operator()(const FontK& b) const {
214 std::hash<std::string> h;
215 return h(std::get<0>(b));
216 }
217 };
218
219 struct Less {
220 size_t operator()(const Tempest::Color& a, const Tempest::Color& b) const {
221 auto ta = std::make_tuple(a.r(),a.g(),a.b(),a.a());
222 auto tb = std::make_tuple(b.r(),b.g(),b.b(),b.a());
223 return ta<tb;
224 }
225 };
226
227 Tempest::Device& dev;
228 Tempest::SoundDevice sound;
229
230 std::recursive_mutex sync;
231 std::unique_ptr<Dx8::DirectMusic> dxMusic;
232 DmLoader* dmLoader = nullptr;
233 zenkit::Vfs gothicAssets;
234
235 std::vector<uint8_t> fBuff, ddsBuf;
236 Tempest::IndexBuffer<uint16_t> cube;
237
238 struct DeleteQueue {
239 std::vector<Tempest::StorageBuffer> ssbo;
240 std::vector<Tempest::StorageImage> img;
241 std::vector<Tempest::Attachment> att;
242 std::vector<Tempest::ZBuffer> zb;
243 std::vector<Tempest::DescriptorArray> arr;
244 std::vector<Tempest::AccelerationStructure> rtas;
245 };
246 DeleteQueue recycled[MaxFramesInFlight];
247 uint8_t recycledId = 0;
248
249 TextureCache texCache;
250 std::map<Tempest::Color,std::unique_ptr<Tempest::Texture2d>,Less> pixCache;
251 std::unordered_map<std::string,std::unique_ptr<ProtoMesh>> aniMeshCache;
252 std::unordered_map<DecalK,std::unique_ptr<ProtoMesh>,Hash> decalMeshCache;
253 std::unordered_map<std::string,std::unique_ptr<Skeleton>> skeletonCache;
254 std::unordered_map<std::string,std::unique_ptr<Animation>> animCache;
255 std::unordered_map<BindK,std::unique_ptr<AttachBinder>,Hash> bindCache;
256 std::unordered_map<std::string,std::unique_ptr<PfxEmitterMesh>> emiMeshCache;
257 std::unordered_map<std::string,std::unique_ptr<VobTree>> zenCache;
258
259 std::recursive_mutex syncFont;
260 std::unordered_map<FontK,std::unique_ptr<GthFont>,Hash> gothicFnt;
261 };
static void loadVdfs(const std::vector< std::u16string > &modvdfs, bool modFilter)
static const PfxEmitterMesh * loadEmiterMesh(std::string_view name)
static Tempest::StorageBuffer ssbo(const void *data, size_t size)
Definition resources.h:125
std::vector< std::shared_ptr< zenkit::VirtualObject > > VobTree
Definition resources.h:81
static const Tempest::Texture2d & fallbackTexture()
static void resetRecycled(uint8_t fId)
static Tempest::StorageBuffer ssbo(Tempest::Uninitialized_t, size_t size)
Definition resources.h:126
static auto openReader(std::string_view name, std::unique_ptr< zenkit::Read > &read) -> std::unique_ptr< zenkit::ReadArchive >
static Tempest::IndexBuffer< I > ibo(const I *data, size_t sz)
Definition resources.h:123
static const Animation * loadAnimation(std::string_view name)
static const Skeleton * loadSkeleton(std::string_view name)
static const Tempest::IndexBuffer< uint16_t > & cubeIbo()
@ MaxFramesInFlight
Definition resources.h:48
@ ShadowLayers
Definition resources.h:49
static const Tempest::Sampler & shadowSampler()
static Tempest::Texture2d loadTextureUncached(std::string_view name, bool forceMips=false)
static const GthFont & font(const float scale)
static const GthFont & dialogFont(const float scale)
static bool hasFile(std::string_view fname)
static const VobTree * loadVobBundle(std::string_view name)
static auto fallbackImage() -> const Tempest::StorageImage &
static Material loadMaterial(const zenkit::Material &src, bool enableAlphaTest)
static std::vector< uint8_t > getFileData(std::string_view name)
static const ProtoMesh * loadMesh(std::string_view name)
static Tempest::Device & device()
Definition resources.h:83
static Dx8::PatternList loadDxMusic(std::string_view name)
static const Tempest::Texture2d * loadTexture(std::string_view name, bool forceMips=false)
static std::unique_ptr< zenkit::Read > getFileBuffer(std::string_view name)
static const char * renderer()
static Tempest::Sound loadSoundBuffer(std::string_view name)
static const Tempest::Texture2d & fallbackBlack()
static DmSegment * loadMusicSegment(char const *name)
static const zenkit::Vfs & vdfsIndex()
static const size_t MAX_MORPH_LAYERS
Definition resources.h:53
static const AttachBinder * bindMesh(const ProtoMesh &anim, const Skeleton &s)
static const size_t MAX_NUM_SKELETAL_NODES
Definition resources.h:52
static auto loadTextureAnim(std::string_view name) -> std::vector< const Tempest::Texture2d * >
static Tempest::Texture2d loadTexturePm(const Tempest::Pixmap &pm)
static void recycle(Tempest::DescriptorArray &&arr)
static void mountWork(const std::filesystem::path &path)
static Tempest::VertexBuffer< V > vbo(const V *data, size_t sz)
Definition resources.h:120
static Tempest::AccelerationStructure blas(const Tempest::VertexBuffer< V > &b, const Tempest::IndexBuffer< I > &i, size_t offset, size_t size)
Definition resources.h:130
static const ProtoMesh * decalMesh(const zenkit::VisualDecal &decal)
Definition band.h:10
uint8_t boneId[4]
Definition resources.h:67
float pos[4][3]
Definition resources.h:66
Tempest::Vec4 cen
Definition resources.h:77
Tempest::Vec3 pos
Definition resources.h:76
Tempest::Vec3 color
Definition resources.h:78