Blender V5.3
gpu_py_compute.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_capabilities.hh"
15#include "GPU_compute.hh"
16#include "GPU_state.hh"
17
18#include "../generic/python_compat.hh" /* IWYU pragma: keep. */
19
20#include "gpu_py.hh"
21#include "gpu_py_compute.hh" /* own include */
22#include "gpu_py_shader.hh"
23
24namespace blender {
25
27 /* Wrap. */
28 pygpu_compute_dispatch_doc,
29 ".. function:: dispatch(shader, groups_x_len, groups_y_len, groups_z_len)\n"
30 "\n"
31 " Dispatches GPU compute.\n"
32 "\n"
33 " :param shader: The shader that you want to dispatch.\n"
34 " :type shader: :class:`gpu.types.GPUShader`\n"
35 " :param groups_x_len: Int for group x length:\n"
36 " :type groups_x_len: int\n"
37 " :param groups_y_len: Int for group y length:\n"
38 " :type groups_y_len: int\n"
39 " :param groups_z_len: Int for group z length:\n"
40 " :type groups_z_len: int\n");
41static PyObject *pygpu_compute_dispatch(PyObject * /*self*/, PyObject *args, PyObject *kwds)
42{
44
45 BPyGPUShader *py_shader;
46 int groups_x_len;
47 int groups_y_len;
48 int groups_z_len;
49
50 static const char *_keywords[] = {
51 "shader", "groups_x_len", "groups_y_len", "groups_z_len", nullptr};
52 static _PyArg_Parser _parser = {
53 "O" /* `shader` */
54 "i" /* `groups_x_len` */
55 "i" /* `groups_y_len` */
56 "i" /* `groups_z_len` */
57 ":dispatch",
58 _keywords,
59 nullptr,
60 };
61 if (_PyArg_ParseTupleAndKeywordsFast(
62 args, kwds, &_parser, &py_shader, &groups_x_len, &groups_y_len, &groups_z_len))
63 {
64 if (!BPyGPUShader_Check(py_shader)) {
65 PyErr_Format(PyExc_TypeError, "Expected a GPUShader, got %s", Py_TYPE(py_shader)->tp_name);
66 return nullptr;
67 }
68
69 /* Check that groups do not exceed #GPU_max_work_group_count(). */
70 const int max_work_group_count_x = GPU_max_work_group_count(0);
71 const int max_work_group_count_y = GPU_max_work_group_count(1);
72 const int max_work_group_count_z = GPU_max_work_group_count(2);
73
74 /* Report back to the user both the requested and the maximum supported value. */
75 if (groups_x_len > GPU_max_work_group_count(0)) {
76 PyErr_Format(PyExc_ValueError,
77 "groups_x_len (%d) exceeds maximum supported value (max work group count: %d)",
78 groups_x_len,
79 max_work_group_count_x);
80 return nullptr;
81 }
82 if (groups_y_len > GPU_max_work_group_count(1)) {
83 PyErr_Format(PyExc_ValueError,
84 "groups_y_len (%d) exceeds maximum supported value (max work group count: %d)",
85 groups_y_len,
86 max_work_group_count_y);
87 return nullptr;
88 }
89 if (groups_z_len > GPU_max_work_group_count(2)) {
90 PyErr_Format(PyExc_ValueError,
91 "groups_z_len (%d) exceeds maximum supported value (max work group count: %d)",
92 groups_z_len,
93 max_work_group_count_z);
94 return nullptr;
95 }
96
97 gpu::Shader *shader = py_shader->shader;
98 GPU_compute_dispatch(shader, groups_x_len, groups_y_len, groups_z_len);
101 }
102 Py_RETURN_NONE;
103}
104
105/* -------------------------------------------------------------------- */
108
109#ifdef __GNUC__
110# ifdef __clang__
111# pragma clang diagnostic push
112# pragma clang diagnostic ignored "-Wcast-function-type"
113# else
114# pragma GCC diagnostic push
115# pragma GCC diagnostic ignored "-Wcast-function-type"
116# endif
117#endif
118
119static PyMethodDef pygpu_compute__tp_methods[] = {
120 {"dispatch",
121 reinterpret_cast<PyCFunction>(pygpu_compute_dispatch),
122 METH_VARARGS | METH_KEYWORDS,
123 pygpu_compute_dispatch_doc},
124 {nullptr, nullptr, 0, nullptr},
125};
126
127#ifdef __GNUC__
128# ifdef __clang__
129# pragma clang diagnostic pop
130# else
131# pragma GCC diagnostic pop
132# endif
133#endif
134
136 /* Wrap. */
137 pygpu_compute__tp_doc,
138 "This module provides access to the global GPU compute functions.");
139static PyModuleDef pygpu_compute_module_def = {
140 /*m_base*/ PyModuleDef_HEAD_INIT,
141 /*m_name*/ "gpu.compute",
142 /*m_doc*/ pygpu_compute__tp_doc,
143 /*m_size*/ 0,
144 /*m_methods*/ pygpu_compute__tp_methods,
145 /*m_slots*/ nullptr,
146 /*m_traverse*/ nullptr,
147 /*m_clear*/ nullptr,
148 /*m_free*/ nullptr,
149};
150
152{
153 PyObject *submodule;
154
155 submodule = PyModule_Create(&pygpu_compute_module_def);
156
157 return submodule;
158}
159
161
162} // namespace blender
#define BPYGPU_IS_INIT_OR_ERROR_OBJ
Definition gpu_py.hh:45
#define BPyGPUShader_Check(v)
BMesh Iterators.
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")
void GPU_compute_dispatch(gpu::Shader *shader, uint groups_x_len, uint groups_y_len, uint groups_z_len, const gpu::shader::SpecializationConstants *constants_state=nullptr)
static PyMethodDef pygpu_compute__tp_methods[]
int GPU_max_work_group_count(int index)
void GPU_memory_barrier(GPUBarrier barrier)
Definition gpu_state.cc:305
static PyObject * pygpu_compute_dispatch(PyObject *, PyObject *args, PyObject *kwds)
@ GPU_BARRIER_TEXTURE_FETCH
Definition GPU_state.hh:40
@ GPU_BARRIER_SHADER_STORAGE
Definition GPU_state.hh:51
@ GPU_BARRIER_SHADER_IMAGE_ACCESS
Definition GPU_state.hh:38
static PyModuleDef pygpu_compute_module_def
PyObject * bpygpu_compute_init()
header-only compatibility defines.
PyObject_HEAD gpu::Shader * shader