Blender V5.3
gpu_py_platform.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
11
12#include <Python.h>
13
14#include "GPU_context.hh"
15#include "GPU_platform.hh"
16
17#include "gpu_py.hh"
18#include "gpu_py_platform.hh" /* Own include. */
19
20namespace blender {
21
22/* -------------------------------------------------------------------- */
25
26PyDoc_STRVAR(pygpu_device_index_doc,
27 "Device index.\n"
28 "\n"
29 ":type: int\n");
30static PyObject *pygpu_device_index_get(BPyGPUDevice *self, void * /*closure*/)
31{
32 return PyLong_FromLong(self->index);
33}
34
35PyDoc_STRVAR(pygpu_device_identifier_doc,
36 "Device identifier.\n"
37 "\n"
38 ":type: str\n");
39static PyObject *pygpu_device_identifier_get(BPyGPUDevice *self, void * /*closure*/)
40{
41 return PyUnicode_FromString(self->identifier);
42}
43
44PyDoc_STRVAR(pygpu_device_name_doc,
45 "Device name.\n"
46 "\n"
47 ":type: str\n");
48static PyObject *pygpu_device_name_get(BPyGPUDevice *self, void * /*closure*/)
49{
50 return PyUnicode_FromString(self->name);
51}
52
53/* Property descriptors */
54static PyGetSetDef pygpu_device_getseters[] = {
55 {"index",
56 reinterpret_cast<getter>(pygpu_device_index_get),
57 nullptr,
58 pygpu_device_index_doc,
59 nullptr},
60 {"identifier",
61 reinterpret_cast<getter>(pygpu_device_identifier_get),
62 nullptr,
63 pygpu_device_identifier_doc,
64 nullptr},
65 {"name",
66 reinterpret_cast<getter>(pygpu_device_name_get),
67 nullptr,
68 pygpu_device_name_doc,
69 nullptr},
70 {nullptr, nullptr, nullptr, nullptr, nullptr},
71};
72
73/* Representation */
74static PyObject *pygpu_device__tp_repr(BPyGPUDevice *self)
75{
76 return PyUnicode_FromFormat("<GPUDevice index=%d identifier=\"%s\" name=\"%s\">",
77 self->index,
78 self->identifier,
79 self->name);
80}
81
83static PyObject *pygpu_device__tp_richcmp(BPyGPUDevice *self, PyObject *other, int op)
84{
85 if (!Py_IS_TYPE(other, &BPyGPU_DeviceType)) {
86 Py_RETURN_NOTIMPLEMENTED;
87 }
88 BPyGPUDevice *other_device = reinterpret_cast<BPyGPUDevice *>(other);
89 switch (op) {
90 case Py_LT:
91 return PyBool_FromLong(self->index < other_device->index);
92 case Py_LE:
93 return PyBool_FromLong(self->index <= other_device->index);
94 case Py_EQ:
95 return PyBool_FromLong(self->index == other_device->index);
96 case Py_NE:
97 return PyBool_FromLong(self->index != other_device->index);
98 case Py_GT:
99 return PyBool_FromLong(self->index > other_device->index);
100 case Py_GE:
101 return PyBool_FromLong(self->index >= other_device->index);
102 }
103 Py_RETURN_NOTIMPLEMENTED;
104}
105
106/* Type definition */
107PyDoc_STRVAR(pygpu_device__tp_doc,
108 ".. class:: GPUDevice\n"
109 "\n"
110 " Represents a GPU device.\n"
111 "\n"
112 " :ivar index: Device index.\n"
113 " :type index: int\n"
114 " :ivar identifier: Device identifier.\n"
115 " :type identifier: str\n"
116 " :ivar name: Device name.\n"
117 " :type name: str\n");
118
119PyTypeObject BPyGPU_DeviceType = {
120 /*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
121 /*tp_name*/ "gpu.platform.GPUDevice",
122 /*tp_basicsize*/ sizeof(BPyGPUDevice),
123 /*tp_itemsize*/ 0,
124 /*tp_dealloc*/ nullptr,
125 /*tp_vectorcall_offset*/ 0,
126 /*tp_getattr*/ nullptr,
127 /*tp_setattr*/ nullptr,
128 /*tp_compare*/ nullptr,
129 /*tp_repr*/ reinterpret_cast<reprfunc>(pygpu_device__tp_repr),
130 /*tp_as_number*/ nullptr,
131 /*tp_as_sequence*/ nullptr,
132 /*tp_as_mapping*/ nullptr,
133 /*tp_hash*/ nullptr,
134 /*tp_call*/ nullptr,
135 /*tp_str*/ nullptr,
136 /*tp_getattro*/ nullptr,
137 /*tp_setattro*/ nullptr,
138 /*tp_as_buffer*/ nullptr,
139 /*tp_flags*/ Py_TPFLAGS_DEFAULT,
140 /*tp_doc*/ pygpu_device__tp_doc,
141 /*tp_traverse*/ nullptr,
142 /*tp_clear*/ nullptr,
143 /*tp_richcompare*/ reinterpret_cast<richcmpfunc>(pygpu_device__tp_richcmp),
144 /*tp_weaklistoffset*/ 0,
145 /*tp_iter*/ nullptr,
146 /*tp_iternext*/ nullptr,
147 /*tp_methods*/ nullptr,
148 /*tp_members*/ nullptr,
149 /*tp_getset*/ pygpu_device_getseters,
150 /*tp_base*/ nullptr,
151 /*tp_dict*/ nullptr,
152 /*tp_descr_get*/ nullptr,
153 /*tp_descr_set*/ nullptr,
154 /*tp_dictoffset*/ 0,
155 /*tp_init*/ nullptr,
156 /*tp_alloc*/ nullptr,
157 /*tp_new*/ nullptr,
158 /*tp_free*/ nullptr,
159 /*tp_is_gc*/ nullptr,
160 /*tp_bases*/ nullptr,
161 /*tp_mro*/ nullptr,
162 /*tp_cache*/ nullptr,
163 /*tp_subclasses*/ nullptr,
164 /*tp_weaklist*/ nullptr,
165 /*tp_del*/ nullptr,
166 /*tp_version_tag*/ 0,
167 /*tp_finalize*/ nullptr,
168 /*tp_vectorcall*/ nullptr,
169};
170
171static BPyGPUDevice *pygpu_device_new(int index, const char *identifier, const char *name)
172{
173 BPyGPUDevice *self = reinterpret_cast<BPyGPUDevice *>(
175 if (self != nullptr) {
176 self->index = index;
177 self->identifier = identifier;
178 self->name = name;
179 }
180 return self;
181}
182
184
185/* -------------------------------------------------------------------- */
188
190 /* Wrap. */
191 pygpu_platform_vendor_get_doc,
192 ".. function:: vendor_get()\n"
193 "\n"
194 " Get GPU vendor.\n"
195 "\n"
196 " :return: Vendor name.\n"
197 " :rtype: str\n");
198static PyObject *pygpu_platform_vendor_get(PyObject * /*self*/)
199{
201
202 return PyUnicode_FromString(GPU_platform_vendor());
203}
204
206 /* Wrap. */
207 pygpu_platform_renderer_get_doc,
208 ".. function:: renderer_get()\n"
209 "\n"
210 " Get GPU to be used for rendering.\n"
211 "\n"
212 " :return: GPU name.\n"
213 " :rtype: str\n");
214static PyObject *pygpu_platform_renderer_get(PyObject * /*self*/)
215{
217
218 return PyUnicode_FromString(GPU_platform_renderer());
219}
220
222 /* Wrap. */
223 pygpu_platform_version_get_doc,
224 ".. function:: version_get()\n"
225 "\n"
226 " Get GPU driver version.\n"
227 "\n"
228 " :return: Driver version.\n"
229 " :rtype: str\n");
230static PyObject *pygpu_platform_version_get(PyObject * /*self*/)
231{
233
234 return PyUnicode_FromString(GPU_platform_version());
235}
236
238 /* Wrap. */
239 pygpu_platform_device_type_get_doc,
240 ".. function:: device_type_get()\n"
241 "\n"
242 " Get GPU device type.\n"
243 "\n"
244 " :return: Device type ('APPLE', 'NVIDIA', 'AMD', 'INTEL', 'SOFTWARE', 'QUALCOMM', "
245 "'UNKNOWN').\n"
246 " :rtype: str\n");
247static PyObject *pygpu_platform_device_type_get(PyObject * /*self*/)
248{
250
251 const char *device;
253 device = "APPLE";
254 }
256 device = "NVIDIA";
257 }
259 device = "AMD";
260 }
262 device = "INTEL";
263 }
265 device = "SOFTWARE";
266 }
267 /* Right now we can only detect Qualcomm GPUs on Windows, not other OSes */
269 device = "QUALCOMM";
270 }
271 else {
272 device = "UNKNOWN";
273 }
274 return PyUnicode_FromString(device);
275}
276
278 /* Wrap. */
279 pygpu_platform_backend_type_get_doc,
280 ".. function:: backend_type_get()\n"
281 "\n"
282 " Get active GPU backend.\n"
283 "\n"
284 " :return: Backend type ('OPENGL', 'VULKAN', 'METAL', 'NONE', 'UNKNOWN').\n"
285 " :rtype: str\n");
286static PyObject *pygpu_platform_backend_type_get(PyObject * /*self*/)
287{
289
290 const char *backend = "UNKNOWN";
291 switch (GPU_backend_get_type()) {
292 case GPU_BACKEND_VULKAN: {
293 backend = "VULKAN";
294 break;
295 }
296 case GPU_BACKEND_METAL: {
297 backend = "METAL";
298 break;
299 }
300 case GPU_BACKEND_NONE: {
301 backend = "NONE";
302 break;
303 }
304 case GPU_BACKEND_OPENGL: {
305 backend = "OPENGL";
306 break;
307 }
308 case GPU_BACKEND_ANY:
309 break;
310 }
311 return PyUnicode_FromString(backend);
312}
313
315 /* Wrap. */
316 pygpu_platform_devices_get_doc,
317 ".. function:: devices_get()\n"
318 "\n"
319 " Get all available GPU devices.\n"
320 "\n"
321 " :return: List of :class:`GPUDevice` objects for each device.\n"
322 " :rtype: list\n");
323static PyObject *pygpu_platform_devices_get(PyObject * /*self*/)
324{
326
328 PyObject *list = PyList_New(devices.size());
329 for (int i = 0; i < devices.size(); i++) {
330 const GPUDevice &dev = devices[i];
331 PyObject *item = reinterpret_cast<PyObject *>(
332 pygpu_device_new(dev.index, dev.identifier.c_str(), dev.name.c_str()));
333 PyList_SET_ITEM(list, i, item);
334 }
335 /* Sort by index (first attribute) for deterministic ordering. */
336 PyList_Sort(list);
337 return list;
338}
339
341
342/* -------------------------------------------------------------------- */
345
346#ifdef __GNUC__
347# ifdef __clang__
348# pragma clang diagnostic push
349# pragma clang diagnostic ignored "-Wcast-function-type"
350# else
351# pragma GCC diagnostic push
352# pragma GCC diagnostic ignored "-Wcast-function-type"
353# endif
354#endif
355
356static PyMethodDef pygpu_platform__tp_methods[] = {
357 {"vendor_get",
358 reinterpret_cast<PyCFunction>(pygpu_platform_vendor_get),
359 METH_NOARGS,
360 pygpu_platform_vendor_get_doc},
361 {"renderer_get",
362 reinterpret_cast<PyCFunction>(pygpu_platform_renderer_get),
363 METH_NOARGS,
364 pygpu_platform_renderer_get_doc},
365 {"version_get",
366 reinterpret_cast<PyCFunction>(pygpu_platform_version_get),
367 METH_NOARGS,
368 pygpu_platform_version_get_doc},
369 {"device_type_get",
370 reinterpret_cast<PyCFunction>(pygpu_platform_device_type_get),
371 METH_NOARGS,
372 pygpu_platform_device_type_get_doc},
373 {"backend_type_get",
374 reinterpret_cast<PyCFunction>(pygpu_platform_backend_type_get),
375 METH_NOARGS,
376 pygpu_platform_backend_type_get_doc},
377 {"devices_get",
378 reinterpret_cast<PyCFunction>(pygpu_platform_devices_get),
379 METH_NOARGS,
380 pygpu_platform_devices_get_doc},
381 {nullptr, nullptr, 0, nullptr},
382};
383
384#ifdef __GNUC__
385# ifdef __clang__
386# pragma clang diagnostic pop
387# else
388# pragma GCC diagnostic pop
389# endif
390#endif
391
393 /* Wrap. */
394 pygpu_platform__tp_doc,
395 "This module provides access to GPU Platform definitions.");
396static PyModuleDef pygpu_platform_module_def = {
397 /*m_base*/ PyModuleDef_HEAD_INIT,
398 /*m_name*/ "gpu.platform",
399 /*m_doc*/ pygpu_platform__tp_doc,
400 /*m_size*/ 0,
401 /*m_methods*/ pygpu_platform__tp_methods,
402 /*m_slots*/ nullptr,
403 /*m_traverse*/ nullptr,
404 /*m_clear*/ nullptr,
405 /*m_free*/ nullptr,
406};
407
409{
410 PyObject *submodule;
411
412 submodule = PyModule_Create(&pygpu_platform_module_def);
413
414 return submodule;
415}
416
418
419} // namespace blender
constexpr int64_t size() const
Definition BLI_span.hh:256
#define BPYGPU_IS_INIT_OR_ERROR_OBJ
Definition gpu_py.hh:45
BMesh Iterators.
static PyObject * pygpu_device_index_get(BPyGPUDevice *self, void *)
const char * GPU_platform_version()
static PyObject * pygpu_device__tp_repr(BPyGPUDevice *self)
static PyMethodDef pygpu_platform__tp_methods[]
Span< GPUDevice > GPU_platform_devices_list()
GPUBackendType GPU_backend_get_type()
PyDoc_STRVAR(bpy_bm_new_doc, ".. function:: new(*, use_operators=True)\n" "\n" " :param use_operators: Support calling operators in :mod:`bmesh.ops` (uses some " "extra memory per vert/edge/face).\n" " :type use_operators: bool\n" " :return: Return a new, empty BMesh.\n" " :rtype: :class:`bmesh.types.BMesh`\n")
static PyGetSetDef pygpu_device_getseters[]
static PyModuleDef pygpu_platform_module_def
static PyObject * pygpu_platform_devices_get(PyObject *)
bool GPU_type_matches(GPUDeviceType device, GPUOSType os, GPUDriverType driver)
static PyObject * pygpu_platform_renderer_get(PyObject *)
const char * GPU_platform_renderer()
PyTypeObject BPyGPU_DeviceType
static PyObject * pygpu_platform_device_type_get(PyObject *)
const char * GPU_platform_vendor()
static PyObject * pygpu_device__tp_richcmp(BPyGPUDevice *self, PyObject *other, int op)
static PyObject * pygpu_platform_vendor_get(PyObject *)
static PyObject * pygpu_platform_backend_type_get(PyObject *)
static BPyGPUDevice * pygpu_device_new(int index, const char *identifier, const char *name)
PyObject * bpygpu_platform_init()
static PyObject * pygpu_device_name_get(BPyGPUDevice *self, void *)
static PyObject * pygpu_device_identifier_get(BPyGPUDevice *self, void *)
@ GPU_DEVICE_QUALCOMM
@ GPU_DEVICE_SOFTWARE
@ GPU_DEVICE_INTEL_UHD
@ GPU_DEVICE_INTEL
@ GPU_DEVICE_NVIDIA
@ GPU_DEVICE_APPLE
static PyObject * pygpu_platform_version_get(PyObject *)
PyObject_HEAD int index
std::string identifier