Blender V4.3
gpu_shader_builder.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
11#include <iostream>
12
13#include "GHOST_C-api.h"
14
15#include "GPU_context.hh"
16#include "GPU_init_exit.hh"
18
19#include "BLI_string_ref.hh"
20#include "BLI_vector.hh"
21
22#include "CLG_log.h"
23
25
27 private:
28 GHOST_SystemHandle ghost_system_;
29 GHOST_ContextHandle ghost_context_ = nullptr;
30 GPUContext *gpu_context_ = nullptr;
31
32 public:
33 void init_system();
34 bool init_context();
35 bool bake_create_infos(const char *name_starts_with_filter);
36 void exit_context();
37 void exit_system();
38};
39
40bool ShaderBuilder::bake_create_infos(const char *name_starts_with_filter)
41{
42 return gpu_shader_create_info_compile(name_starts_with_filter);
43}
44
46{
47 CLG_init();
48 ghost_system_ = GHOST_CreateSystemBackground();
49 GPU_backend_ghost_system_set(ghost_system_);
50}
51
53{
54 BLI_assert(ghost_system_);
55 BLI_assert(ghost_context_ == nullptr);
56 BLI_assert(gpu_context_ == nullptr);
57
58 GHOST_GPUSettings gpuSettings = {0};
60#ifdef WITH_OPENGL_BACKEND
62 gpuSettings.context_type = GHOST_kDrawingContextTypeOpenGL;
63 break;
64#endif
65
66#ifdef WITH_METAL_BACKEND
68 gpuSettings.context_type = GHOST_kDrawingContextTypeMetal;
69 break;
70#endif
71
72#ifdef WITH_VULKAN_BACKEND
74 gpuSettings.context_type = GHOST_kDrawingContextTypeVulkan;
75 break;
76#endif
77
78 default:
80 break;
81 }
82
83 ghost_context_ = GHOST_CreateGPUContext(ghost_system_, gpuSettings);
84 if (ghost_context_ == nullptr) {
85 GHOST_DisposeSystem(ghost_system_);
86 return false;
87 }
88
89 GHOST_ActivateGPUContext(ghost_context_);
90
91 gpu_context_ = GPU_context_create(nullptr, ghost_context_);
92 GPU_init();
93 return true;
94}
95
97{
98 BLI_assert(ghost_context_);
99 BLI_assert(gpu_context_);
100 GPU_exit();
101 GPU_context_discard(gpu_context_);
102 GHOST_DisposeGPUContext(ghost_system_, ghost_context_);
103 gpu_context_ = nullptr;
104 ghost_context_ = nullptr;
105}
106
108{
109 GHOST_DisposeSystem(ghost_system_);
110 CLG_exit();
111}
112
113} // namespace blender::gpu::shader_builder
114
116int main(int argc, const char *argv[])
117{
118 std::string gpu_backend_arg;
119 std::string shader_name_starts_with_filter_arg;
120 std::string result_file_arg;
121
122 int arg = 1;
123 while (arg < argc) {
124 if (arg < argc - 2) {
125 blender::StringRefNull argument = argv[arg];
126 if (argument == "--gpu-backend") {
127 gpu_backend_arg = std::string(argv[arg + 1]);
128 arg += 2;
129 }
130 else if (argument == "--gpu-shader-filter") {
131 shader_name_starts_with_filter_arg = std::string(argv[arg + 1]);
132 arg += 2;
133 }
134 else {
135 break;
136 }
137 }
138 else if (arg == argc - 1) {
139 result_file_arg = argv[arg];
140 arg += 1;
141 }
142 else {
143 break;
144 }
145 }
146
147 if (result_file_arg.empty() || (!ELEM(gpu_backend_arg, "", "vulkan", "metal", "opengl"))) {
148 std::cout << "Usage: " << argv[0];
149 std::cout << " [--gpu-backend ";
150#ifdef WITH_METAL_BACKEND
151 std::cout << "metal";
152#endif
153#ifdef WITH_OPENGL_BACKEND
154 std::cout << "opengl";
155#endif
156#ifdef WITH_VULKAN_BACKEND
157 std::cout << ",vulkan";
158#endif
159 std::cout << "]";
160 std::cout << " [--gpu-shader-filter <shader-name>]";
161 std::cout << " <data_file_out>\n";
162 exit(1);
163 }
164
165 int exit_code = 0;
166
168 builder.init_system();
169
170 struct NamedBackend {
171 std::string name;
172 eGPUBackendType backend;
173 };
174
175 blender::Vector<NamedBackend> backends_to_validate;
176#ifdef WITH_OPENGL_BACKEND
177 if (ELEM(gpu_backend_arg, "", "opengl")) {
178 backends_to_validate.append({"OpenGL", GPU_BACKEND_OPENGL});
179 }
180#endif
181#ifdef WITH_METAL_BACKEND
182 if (ELEM(gpu_backend_arg, "", "metal")) {
183 backends_to_validate.append({"Metal", GPU_BACKEND_METAL});
184 }
185#endif
186#ifdef WITH_VULKAN_BACKEND
187 if (ELEM(gpu_backend_arg, "", "vulkan")) {
188 backends_to_validate.append({"Vulkan", GPU_BACKEND_VULKAN});
189 }
190#endif
191
192 for (NamedBackend &backend : backends_to_validate) {
193 GPU_backend_type_selection_set(backend.backend);
194 if (!GPU_backend_supported()) {
195 printf("%s isn't supported on this platform. Shader compilation is skipped\n",
196 backend.name.c_str());
197 continue;
198 }
199 if (builder.init_context()) {
200 if (!builder.bake_create_infos(shader_name_starts_with_filter_arg.c_str())) {
201 printf("Shader compilation failed for %s backend\n", backend.name.c_str());
202 exit_code = 1;
203 }
204 else {
205 printf("%s backend shader compilation succeeded.\n", backend.name.c_str());
206 }
207 builder.exit_context();
208 }
209 else {
210 printf("Shader compilation skipped for %s backend. Context could not be created.\n",
211 backend.name.c_str());
212 }
213 }
214
215 builder.exit_system();
216
217 exit(exit_code);
218 return exit_code;
219}
#define BLI_assert_unreachable()
Definition BLI_assert.h:97
#define BLI_assert(a)
Definition BLI_assert.h:50
#define ELEM(...)
void CLG_exit(void)
Definition clog.c:706
void CLG_init(void)
Definition clog.c:699
GHOST C-API function and type declarations.
GHOST_ContextHandle GHOST_CreateGPUContext(GHOST_SystemHandle systemhandle, GHOST_GPUSettings gpuSettings)
GHOST_SystemHandle GHOST_CreateSystemBackground(void)
GHOST_TSuccess GHOST_ActivateGPUContext(GHOST_ContextHandle contexthandle)
GHOST_TSuccess GHOST_DisposeGPUContext(GHOST_SystemHandle systemhandle, GHOST_ContextHandle contexthandle)
GHOST_TSuccess GHOST_DisposeSystem(GHOST_SystemHandle systemhandle)
GPUContext * GPU_context_create(void *ghost_window, void *ghost_context)
void GPU_backend_type_selection_set(const eGPUBackendType backend)
bool GPU_backend_supported()
eGPUBackendType GPU_backend_type_selection_get()
void GPU_context_discard(GPUContext *)
void GPU_backend_ghost_system_set(void *ghost_system_handle)
void GPU_init()
void GPU_exit()
struct GPUContext GPUContext
void append(const T &value)
bool bake_create_infos(const char *name_starts_with_filter)
#define printf
bool gpu_shader_create_info_compile(const char *name_starts_with_filter)
int main()
GHOST_TDrawingContextType context_type