11#if BLI_SUBPROCESS_SUPPORT
41class SubprocessShader {
47 bool success_ =
false;
50 SubprocessShader(
const char *comp_src,
56 program_ = glCreateProgram();
58 auto compile_stage = [&](
const char *src, GLenum stage) -> GLuint {
65 GLuint shader = glCreateShader(stage);
66 glShaderSource(shader, 1, &src,
nullptr);
67 glCompileShader(shader);
68 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
69 glAttachShader(program_, shader);
73 comp_ = compile_stage(comp_src, GL_COMPUTE_SHADER);
78 vert_ = compile_stage(vert_src, GL_VERTEX_SHADER);
83 geom_ = compile_stage(geom_src, GL_GEOMETRY_SHADER);
88 frag_ = compile_stage(frag_src, GL_FRAGMENT_SHADER);
93 glLinkProgram(program_);
94 glGetProgramiv(program_, GL_LINK_STATUS, &status);
104 glDeleteShader(comp_);
105 glDeleteShader(vert_);
106 glDeleteShader(geom_);
107 glDeleteShader(frag_);
108 glDeleteProgram(program_);
111 ShaderBinaryHeader *get_binary(
void *memory)
113 ShaderBinaryHeader *bin =
reinterpret_cast<ShaderBinaryHeader *
>(memory);
118 glGetProgramiv(program_, GL_PROGRAM_BINARY_LENGTH, &bin->size);
119 if (bin->size >
sizeof(ShaderBinaryHeader::data)) {
123 glGetProgramBinary(program_, bin->size,
nullptr, &bin->format, bin->data);
131static bool validate_binary(
void *binary)
133 ShaderBinaryHeader *bin =
reinterpret_cast<ShaderBinaryHeader *
>(binary);
134 GLuint program = glCreateProgram();
135 glProgramBinary(program, bin->format, bin->data, bin->size);
137 glGetProgramiv(program, GL_LINK_STATUS, &status);
138 glDeleteProgram(program);
142std::string GL_shader_cache_dir_get()
144 static char tmp_dir_buffer[1024];
147 std::string cache_dir = std::string(tmp_dir_buffer) +
"gl-shader-cache" +
SEP_STR;
155void GPU_compilation_subprocess_run(
const char *subprocess_name)
157 using namespace blender::gpu;
161 pid_t ppid = getppid();
170 std::string name = subprocess_name;
171 SharedMemory shared_mem(name, compilation_subprocess_shared_memory_size,
false);
172 if (!shared_mem.get_data()) {
173 std::cerr <<
"Compilation Subprocess: Failed to open shared memory " << subprocess_name
177 SharedSemaphore start_semaphore(name +
"_START",
true);
178 SharedSemaphore end_semaphore(name +
"_END",
true);
179 SharedSemaphore close_semaphore(name +
"_CLOSE",
true);
185 GHOST_GPUSettings gpu_settings = {0};
186 gpu_settings.
context_type = GHOST_kDrawingContextTypeOpenGL;
188 if (ghost_context ==
nullptr) {
189 std::cerr <<
"Compilation Subprocess: Failed to initialize GHOST context for "
190 << subprocess_name <<
"\n";
198 std::string cache_dir = GL_shader_cache_dir_get();
206 start_semaphore.decrement();
208 bool lost_parent =
false;
209 while (!lost_parent && !start_semaphore.try_decrement(1000)) {
210 lost_parent = getppid() != ppid;
213 std::cerr <<
"Compilation Subprocess: Lost parent process\n";
218 if (close_semaphore.try_decrement()) {
222 ShaderSourceHeader *source =
reinterpret_cast<ShaderSourceHeader *
>(shared_mem.get_data());
223 const char *next_src = source->sources;
224 const char *comp_src =
nullptr;
225 const char *vert_src =
nullptr;
226 const char *geom_src =
nullptr;
227 const char *frag_src =
nullptr;
230 std::string hash_str =
"_";
232 auto get_src = [&]() {
233 const char *src = next_src;
234 next_src += strlen(src) +
sizeof(
'\0');
235 hash_str += std::to_string(hasher(src)) +
"_";
239 if (source->type == ShaderSourceHeader::Type::COMPUTE) {
240 comp_src = get_src();
243 vert_src = get_src();
244 if (source->type == ShaderSourceHeader::Type::GRAPHICS_WITH_GEOMETRY_STAGE) {
245 geom_src = get_src();
247 frag_src = get_src();
250 std::string cache_path = cache_dir +
SEP_STR + hash_str;
257 std::string source_hash =
"SOURCE_HASH:" + hash_str;
258 BLI_strncpy(
reinterpret_cast<char *
>(shared_mem.get_data()),
260 source_hash.size() + 1);
265 fstream file(cache_path, std::ios::binary | std::ios::in | std::ios::ate);
266 std::streamsize
size = file.tellg();
267 if (size <= compilation_subprocess_shared_memory_size) {
268 file.seekg(0, std::ios::beg);
270 static char tmp_mem[compilation_subprocess_shared_memory_size];
271 file.read(tmp_mem, size);
275 if (!validate_binary(tmp_mem)) {
276 std::cout <<
"Compilation Subprocess: Failed to load cached shader binary " << hash_str
285 memcpy(shared_mem.get_data(), tmp_mem, size);
286 end_semaphore.increment();
292 std::cerr <<
"Compilation Subprocess: Wrong size for cached shader binary " << hash_str
298 SubprocessShader
shader(comp_src, vert_src, geom_src, frag_src);
299 ShaderBinaryHeader *binary =
shader.get_binary(shared_mem.get_data());
302 fstream file(cache_path, std::ios::binary | std::ios::out);
303 file.write(
reinterpret_cast<char *
>(shared_mem.get_data()),
307 end_semaphore.increment();
317void GL_shader_cache_dir_clear_old()
319 std::string cache_dir = GL_shader_cache_dir_get();
321 direntry *entries =
nullptr;
324 direntry entry = entries[i];
328 const time_t ts_now =
time(
nullptr);
329 const time_t delete_threshold = 60 * 60 * 24 * 30 ;
330 if (entry.
s.st_mtime + delete_threshold < ts_now) {
File and directory operations.
Compatibility-like things for windows.
#define BLI_assert_unreachable()
virtual GHOST_TSuccess activateDrawingContext()=0
static GHOST_ISystem * getSystem()
virtual GHOST_IContext * createOffscreenContext(GHOST_GPUSettings gpu_settings)=0
virtual GHOST_TSuccess disposeContext(GHOST_IContext *context)=0
static GHOST_TSuccess disposeSystem()
virtual bool processEvents(bool waitForEvent)=0
static GHOST_TSuccess createSystemBackground()
constexpr IndexRange()=default
std::string time(const std::tm &date_time, TimeFormat format=TimeFormat::H24)
int size(const AssetLibraryReference *library_reference)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned int BLI_filelist_dir_contents(const char *dirname, struct direntry **r_filelist)
GPUContext * GPU_context_create(GHOST_IWindow *ghost_window, GHOST_IContext *ghost_context)
bool BLI_dir_create_recursive(const char *dirname) ATTR_NONNULL()
bool BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void BLI_threadapi_init()
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
BMesh const char void * data
int BLI_delete(const char *path, bool dir, bool recursive) ATTR_NONNULL()
bool BLI_file_touch(const char *filepath) ATTR_NONNULL(1)
void BLI_filelist_free(struct direntry *filelist, unsigned int nrentries)
void BKE_appdir_folder_caches(char *path, size_t path_maxncpy) ATTR_NONNULL(1)
void GPU_backend_ghost_system_set(GHOST_ISystem *ghost_system_handle)
void GPU_context_discard(GPUContext *)
GHOST_TDrawingContextType context_type
bool use_main_context_workaround