File: BinaryShaderCache.cpp

package info (click to toggle)
jazz2-native 3.5.0-1
  • links: PTS, VCS
  • area: contrib
  • in suites:
  • size: 16,836 kB
  • sloc: cpp: 172,557; xml: 113; python: 36; makefile: 5; sh: 2
file content (225 lines) | stat: -rw-r--r-- 7,191 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include "BinaryShaderCache.h"
#include "IGfxCapabilities.h"
#include "../ServiceLocator.h"
#include "../Base/Algorithms.h"
#include "../Base/HashFunctions.h"
#include "../../Main.h"

#include <IO/FileSystem.h>
#include <IO/Stream.h>
#include <IO/Compression/DeflateStream.h>

using namespace Death::Containers::Literals;
using namespace Death::IO;
using namespace Death::IO::Compression;

static constexpr std::uint8_t Signature[] = { 0xEF, 0xBB, 0xBF, 0xF0, 0x9F, 0x8C, 0xAA, 0x20 };
static constexpr std::uint16_t Version = 1 | 0x1000;

namespace nCine
{
	namespace
	{
		std::uint32_t bufferSize = 0;
		std::unique_ptr<std::uint8_t[]> bufferPtr;
	}

	BinaryShaderCache::BinaryShaderCache(StringView path)
		: isAvailable_(false), platformHash_(0)
	{
		if (path.empty()) {
			LOGD("Binary shader cache is disabled");
			return;
		}

		const IGfxCapabilities& gfxCaps = theServiceLocator().GetGfxCapabilities();
#if defined(WITH_OPENGLES) && !defined(DEATH_TARGET_EMSCRIPTEN) && !defined(DEATH_TARGET_SWITCH) && !defined(DEATH_TARGET_UNIX)
		const bool isSupported = gfxCaps.HasExtension(IGfxCapabilities::GLExtensions::ARB_GET_PROGRAM_BINARY) ||
								 gfxCaps.HasExtension(IGfxCapabilities::GLExtensions::OES_GET_PROGRAM_BINARY);
#else
		const bool isSupported = gfxCaps.HasExtension(IGfxCapabilities::GLExtensions::ARB_GET_PROGRAM_BINARY);
#endif
		if (!isSupported) {
			LOGW("GL_ARB_get_program_binary extensions not supported, binary shader cache is disabled");
			return;
		}

#if defined(WITH_OPENGLES) && !defined(DEATH_TARGET_EMSCRIPTEN) && !defined(DEATH_TARGET_SWITCH) && !defined(DEATH_TARGET_UNIX) && (!defined(DEATH_TARGET_WINDOWS_RT) || defined(WITH_ANGLE))
		if (gfxCaps.HasExtension(IGfxCapabilities::GLExtensions::OES_GET_PROGRAM_BINARY)) {
			_glGetProgramBinary = glGetProgramBinaryOES;
			_glProgramBinary = glProgramBinaryOES;
			_glProgramBinaryLength = GL_PROGRAM_BINARY_LENGTH_OES;
		} else
#endif
		{
			_glGetProgramBinary = glGetProgramBinary;
			_glProgramBinary = glProgramBinary;
			_glProgramBinaryLength = GL_PROGRAM_BINARY_LENGTH;
		}

		const auto& infoStrings = gfxCaps.GetGLInfoStrings();

		platformHash_ =
			xxHash3(infoStrings.renderer, std::strlen(infoStrings.renderer),
				xxHash3(infoStrings.glVersion, std::strlen(infoStrings.glVersion)));

		char platformHashString[24];
		std::size_t platformHashLength = formatInto(platformHashString, "{:.16x}", platformHash_);
		path_ = fs::CombinePath(path, { platformHashString, platformHashLength });
		fs::CreateDirectories(path_);

		bufferSize = 64 * 1024;
		bufferPtr = std::make_unique<std::uint8_t[]>(bufferSize);

		const bool pathExists = fs::DirectoryExists(path_);
		isAvailable_ = (isSupported && pathExists);
	}

	String BinaryShaderCache::GetCachedShaderPath(const char* shaderName)
	{
		if (!isAvailable_ || shaderName == nullptr || shaderName[0] == '\0') {
			return {};
		}

		std::uint64_t shaderNameHash = xxHash3(shaderName, std::strlen(shaderName));

		char filename[32];
		std::size_t filenameLength = formatInto(filename, "{:.16x}.shader", shaderNameHash);
		return fs::CombinePath(path_, { filename, filenameLength });
	}

	bool BinaryShaderCache::LoadFromCache(const char* shaderName, std::uint64_t shaderVersion, GLShaderProgram* program, GLShaderProgram::Introspection introspection)
	{
		String cachePath = GetCachedShaderPath(shaderName);
		if (cachePath.empty()) {
			return false;
		}

		std::unique_ptr<Stream> s = fs::Open(cachePath, FileAccess::Read);
		std::int64_t fileSize = s->GetSize();
		if (fileSize <= 20 || fileSize > 16 * 1024 * 1024) {
			if (s->IsValid()) {
				LOGW("Invalid .shader file");
			}
			return false;
		}

		std::uint8_t signature[sizeof(Signature)];
		s->Read(signature, sizeof(signature));
		if (std::memcmp(signature, Signature, sizeof(Signature)) != 0) {
			LOGW("Invalid .shader file");
			return false;
		}

		std::uint16_t fileVersion = s->ReadValueAsLE<std::uint16_t>();
		if (fileVersion != Version) {
			LOGW("Unsupported .shader file version");
			return false;
		}

		std::uint64_t fileShaderVersion = s->ReadVariableUint64();
		// Shader version must be the same
		if (fileShaderVersion != shaderVersion) {
			return false;
		}

		std::uint32_t fileBinaryFormat = s->ReadVariableUint32();
		std::uint32_t fileBatchSize = s->ReadVariableUint32();
		std::uint32_t fileShaderLength = s->ReadVariableUint32();
		DEATH_ASSERT(fileShaderLength > 0, "Invalid .shader file", false);

		if (bufferSize < fileShaderLength) {
			bufferSize = fileShaderLength;
			bufferPtr = std::make_unique<std::uint8_t[]>(bufferSize);
		}

		DeflateStream ds(*s);
		std::int64_t bytesRead = ds.Read(bufferPtr.get(), std::int64_t(fileShaderLength));
		DEATH_ASSERT(bytesRead == fileShaderLength, "Failed to read binary shader data", false);

		_glProgramBinary(program->GetGLHandle(), fileBinaryFormat, bufferPtr.get(), std::int64_t(fileShaderLength));
		program->SetBatchSize(fileBatchSize);
		return program->FinalizeAfterLinking(introspection);
	}

	bool BinaryShaderCache::SaveToCache(const char* shaderName, std::uint64_t shaderVersion, GLShaderProgram* program)
	{
		String cachePath = GetCachedShaderPath(shaderName);
		if (cachePath.empty()) {
			return false;
		}

		GLint shaderLength = 0;
		glGetProgramiv(program->GetGLHandle(), _glProgramBinaryLength, &shaderLength);
		if (shaderLength <= 0) {
			return false;
		}

		if (bufferSize < std::uint32_t(shaderLength)) {
			bufferSize = std::uint32_t(shaderLength);
			bufferPtr = std::make_unique<std::uint8_t[]>(bufferSize);
		} 

		shaderLength = 0;
		unsigned int binaryFormat = 0;
		_glGetProgramBinary(program->GetGLHandle(), bufferSize, &shaderLength, &binaryFormat, bufferPtr.get());
		if (shaderLength <= 0 || std::uint32_t(shaderLength) > bufferSize) {
			return false;
		}

		std::unique_ptr<Stream> s = fs::Open(cachePath, FileAccess::Write);
		if (!s->IsValid()) {
			return false;
		}

		s->Write(Signature, sizeof(Signature));
		s->WriteValueAsLE<std::uint16_t>(Version);
		s->WriteVariableUint64(shaderVersion);
		s->WriteVariableUint32(binaryFormat);
		s->WriteVariableUint32(program->GetBatchSize());
		s->WriteVariableUint32(shaderLength);

		DeflateWriter dw(*s);
		dw.Write(bufferPtr.get(), shaderLength);

		return true;
	}

	std::uint32_t BinaryShaderCache::Prune()
	{
		auto platformHashString = fs::GetFileName(path_);

		std::uint32_t directoriesRemoved = 0;
		for (auto shaderPath : fs::Directory(fs::GetDirectoryName(path_))) {
			if (fs::DirectoryExists(shaderPath)) {
				StringView filename = fs::GetFileName(shaderPath);
				if (filename != platformHashString) {
					fs::RemoveDirectoryRecursive(shaderPath);
					directoriesRemoved++;
				}
			} else if (fs::GetExtension(shaderPath) == "shader"_s) {
				// Remove stray .shader files that are not in a platform directory
				fs::RemoveFile(shaderPath);
			}
		}

		return directoriesRemoved;
	}

	bool BinaryShaderCache::Clear()
	{
		bool success = fs::RemoveDirectoryRecursive(path_);
		fs::CreateDirectories(path_);
		return success;
	}

	bool BinaryShaderCache::SetPath(StringView path)
	{
		if (!fs::DirectoryExists(path) || !fs::IsWritable(path)) {
			return false;
		}

		path_ = path;
		return true;
	}
}