Blender V5.3
shader_eval.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
6
8
9#include "device/device.h"
10#include "device/queue.h"
11
12#include "device/cpu/kernel.h"
13
14#include "util/log.h"
15#include "util/progress.h"
16#include "util/scoped_defer.h"
17#include "util/tbb.h"
18
20
21ShaderEval::ShaderEval(Device *device, Progress &progress) : device_(device), progress_(progress)
22{
23 DCHECK_NE(device_, nullptr);
24}
25
27 const int max_num_inputs,
28 const int num_channels,
29 const std::function<int(device_vector<KernelShaderEvalInput> &)> &fill_input,
30 const std::function<void(device_vector<float> &)> &read_output)
31{
32 bool first_device = true;
33 bool success = true;
34
35 device_->foreach_device([&](Device *device) {
36 if (!first_device) {
37 LOG_DEBUG << "Multi-devices are not yet fully implemented, will evaluate shader on a "
38 "single device.";
39 return;
40 }
41 first_device = false;
42
43 device_vector<KernelShaderEvalInput> input(device, "ShaderEval input", MEM_READ_ONLY);
44 device_vector<float> output(device, "ShaderEval output", MEM_READ_WRITE);
45
46 /* Allocate and copy device buffers. */
47 DCHECK_EQ(input.device, device);
48 DCHECK_EQ(output.device, device);
49 DCHECK_LE(output.size(), input.size());
50
51 input.alloc(max_num_inputs);
52 int const num_points = fill_input(input);
53 if (num_points == 0) {
54 return;
55 }
56
57 input.copy_to_device();
58 output.alloc(num_points * num_channels);
59 output.zero_to_device();
60
61 /* Evaluate on CPU or GPU. */
62 success = (device->info.type == DEVICE_CPU) ?
63 eval_cpu(device, type, input, output, num_points) :
64 eval_gpu(device, type, input, output, num_points);
65
66 /* Copy data back from device if not canceled. */
67 if (success) {
68 output.copy_from_device(0, 1, output.size());
69 read_output(output);
70 }
71
72 input.free();
73 output.free();
74 });
75
76 return success;
77}
78
80 const ShaderEvalType type,
83 const int64_t work_size)
84{
85 vector<ThreadKernelGlobalsCPU> *kernel_thread_globals =
88
89 /* Find required kernel function. */
90 const CPUKernels &kernels = Device::get_cpu_kernels();
91
92 /* Simple parallel_for over all work items. */
93 KernelShaderEvalInput *input_data = input.data();
94 float *output_data = output.data();
95 bool success = true;
96
97 tbb::task_arena local_arena(device->info.cpu_threads);
98 local_arena.execute([&]() {
99 parallel_for(int64_t(0), work_size, [&](int64_t work_index) {
100 /* TODO: is this fast enough? */
101 if (progress_.get_cancel()) {
102 success = false;
103 return;
104 }
105
106 const int thread_index = tbb::this_task_arena::current_thread_index();
107 const ThreadKernelGlobalsCPU *kg = &(*kernel_thread_globals)[thread_index];
108
109 switch (type) {
111 kernels.shader_eval_displace(kg, input_data, output_data, work_index);
112 break;
114 kernels.shader_eval_background(kg, input_data, output_data, work_index);
115 break;
117 kernels.shader_eval_curve_shadow_transparency(kg, input_data, output_data, work_index);
118 break;
120 kernels.shader_eval_volume_density(kg, input_data, output_data, work_index);
121 break;
122 }
123 });
124 });
125
126 return success;
127}
128
130 const ShaderEvalType type,
133 const int64_t work_size)
134{
135 /* Find required kernel function. */
136 DeviceKernel kernel;
137 switch (type) {
140 break;
143 break;
146 break;
149 };
150
151 /* Create device queue. */
153 queue->init_execution();
154
155 device_vector<uint> cache_miss(device, "ShaderEval cache_miss", MEM_READ_WRITE);
156 cache_miss.alloc(1);
157 cache_miss[0] = false;
158
159 /* Execute work on GPU in chunk, so we can cancel.
160 * TODO: query appropriate size from device. */
161 const int32_t chunk_size = 1 << 21;
162
163 const device_ptr d_input = input.device_pointer;
164 device_ptr d_output = output.device_pointer;
165
166 assert(work_size <= 0x7fffffff);
167 for (int32_t d_offset = 0; d_offset < int32_t(work_size); d_offset += chunk_size) {
168 int32_t d_work_size = std::min(chunk_size, int32_t(work_size) - d_offset);
169
170 do {
171 if (device->have_error() || progress_.get_cancel()) {
172 return false;
173 }
174
175 if (cache_miss[0]) {
176 /* Update image cache if needed. */
177 device->image_load_requested_gpu(*queue);
178 cache_miss[0] = false;
179
180 if (device->have_error() || progress_.get_cancel()) {
181 return false;
182 }
183 }
184
185 /* Execute shaders. */
186 queue->copy_to_device(cache_miss);
187 const DeviceKernelArguments args(
188 &d_input, &d_output, &cache_miss.device_pointer, &d_offset, &d_work_size);
189 queue->enqueue(kernel, d_work_size, args);
190 queue->copy_from_device(cache_miss);
191
192 if (!queue->synchronize()) {
193 return false;
194 }
195
196 /* Keep trying until there is no more cache miss. We could try to only re-execute
197 * items with cache misses, however all work items use the same shader and so
198 * likely the same tiled images textures. So it's unlikely for there to be much
199 * divergence as probably all or none have a cache miss. */
200 } while (cache_miss[0]);
201 }
202
203 return true;
204}
205
long long int int64_t
#define output
ShaderEvalFunction shader_eval_background
ShaderEvalFunction shader_eval_volume_density
ShaderEvalFunction shader_eval_displace
ShaderEvalFunction shader_eval_curve_shadow_transparency
DeviceType type
static const CPUKernels & get_cpu_kernels()
void image_load_requested_gpu(DeviceQueue &queue)
virtual unique_ptr< DeviceQueue > gpu_queue_create()
virtual vector< ThreadKernelGlobalsCPU > * acquire_cpu_kernel_thread_globals()
DeviceInfo info
virtual void foreach_device(const std::function< void(Device *)> &callback)
virtual void release_cpu_kernel_thread_globals()
bool have_error()
bool get_cancel() const
Definition progress.h:77
ShaderEval(Device *device, Progress &progress)
bool eval_cpu(Device *device, const ShaderEvalType type, device_vector< KernelShaderEvalInput > &input, device_vector< float > &output, const int64_t work_size)
bool eval(const ShaderEvalType type, const int max_num_inputs, const int num_channels, const std::function< int(device_vector< KernelShaderEvalInput > &)> &fill_input, const std::function< void(device_vector< float > &)> &read_output)
Progress & progress_
Definition shader_eval.h:51
Device * device_
Definition shader_eval.h:50
bool eval_gpu(Device *device, const ShaderEvalType type, device_vector< KernelShaderEvalInput > &input, device_vector< float > &output, const int64_t work_size)
size_t size() const
T * alloc(const size_t width, const size_t height=0)
@ MEM_READ_WRITE
@ MEM_READ_ONLY
#define CCL_NAMESPACE_END
@ DEVICE_CPU
#define assert(assertion)
ccl_gpu_kernel_postfix const ccl_global int ccl_global float const int work_size
DeviceKernel
@ DEVICE_KERNEL_SHADER_EVAL_DISPLACE
@ DEVICE_KERNEL_SHADER_EVAL_VOLUME_DENSITY
@ DEVICE_KERNEL_SHADER_EVAL_BACKGROUND
@ DEVICE_KERNEL_SHADER_EVAL_CURVE_SHADOW_TRANSPARENCY
#define DCHECK_EQ(a, b)
Definition log.h:144
#define LOG_DEBUG
Definition log.h:107
#define DCHECK_LE(a, b)
Definition log.h:147
#define DCHECK_NE(a, b)
Definition log.h:143
#define SCOPED_DEFER(code)
ShaderEvalType
Definition shader_eval.h:18
@ SHADER_EVAL_VOLUME_DENSITY
Definition shader_eval.h:22
@ SHADER_EVAL_CURVE_SHADOW_TRANSPARENCY
Definition shader_eval.h:21
@ SHADER_EVAL_BACKGROUND
Definition shader_eval.h:20
@ SHADER_EVAL_DISPLACE
Definition shader_eval.h:19
uint64_t device_ptr
Definition types_base.h:44