Blender V4.3
cached_shader.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
5#include <cstdint>
6#include <memory>
7#include <string>
8
9#include "BLI_hash.hh"
10
11#include "GPU_shader.hh"
12#include "GPU_texture.hh"
13
14#include "COM_cached_shader.hh"
15#include "COM_result.hh"
16
18
20
21/* --------------------------------------------------------------------
22 * Cached Shader Key.
23 */
24
25CachedShaderKey::CachedShaderKey(const char *info_name, ResultPrecision precision)
26 : info_name(info_name), precision(precision)
27{
28}
29
34
36{
37 return a.info_name == b.info_name && a.precision == b.precision;
38}
39
40/* --------------------------------------------------------------------
41 * Cached Shader.
42 */
43
44CachedShader::CachedShader(const char *info_name, ResultPrecision precision)
45{
46 using namespace gpu::shader;
47 ShaderCreateInfo info = *reinterpret_cast<const ShaderCreateInfo *>(
49
50 /* Finalize first in case the create info had additional info. */
51 info.finalize();
52
53 /* Change the format of image resource to the target precision. */
54 for (ShaderCreateInfo::Resource &resource : info.pass_resources_) {
55 if (resource.bind_type != ShaderCreateInfo::Resource::BindType::IMAGE) {
56 continue;
57 }
58 resource.image.format = Result::gpu_texture_format(resource.image.format, precision);
59 }
60
61 shader_ = GPU_shader_create_from_info(reinterpret_cast<const GPUShaderCreateInfo *>(&info));
62}
63
68
70{
71 return shader_;
72}
73
74/* --------------------------------------------------------------------
75 * Cached Shader Container.
76 */
77
79{
80 /* First, delete all resources that are no longer needed. */
81 map_.remove_if([](auto item) { return !item.value->needed; });
82
83 /* Second, reset the needed status of the remaining resources to false to ready them to track
84 * their needed status for the next evaluation. */
85 for (auto &value : map_.values()) {
86 value->needed = false;
87 }
88}
89
90GPUShader *CachedShaderContainer::get(const char *info_name, ResultPrecision precision)
91{
92 const CachedShaderKey key(info_name, precision);
93
94 auto &cached_shader = *map_.lookup_or_add_cb(
95 key, [&]() { return std::make_unique<CachedShader>(info_name, precision); });
96
97 cached_shader.needed = true;
98 return cached_shader.shader();
99}
100
101} // namespace blender::realtime_compositor
const GPUShaderCreateInfo * GPU_shader_create_info_get(const char *info_name)
GPUShader * GPU_shader_create_from_info(const GPUShaderCreateInfo *_info)
void GPU_shader_free(GPUShader *shader)
struct GPUShader GPUShader
GPUShader * get(const char *info_name, ResultPrecision precision)
CachedShaderKey(const char *info_name, ResultPrecision precision)
CachedShader(const char *info_name, ResultPrecision precision)
static eGPUTextureFormat gpu_texture_format(ResultType type, ResultPrecision precision)
Definition result.cc:29
local_group_size(16, 16) .push_constant(Type b
bool operator==(const BokehKernelKey &a, const BokehKernelKey &b)
uint64_t get_default_hash(const T &v)
Definition BLI_hash.hh:219
unsigned __int64 uint64_t
Definition stdint.h:90
Describe inputs & outputs, stage interfaces, resources and sources of a shader. If all data is correc...
void finalize(const bool recursive=false)