Blender V5.0
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
13#include "COM_cached_shader.hh"
14#include "COM_result.hh"
15
17
18namespace blender::compositor {
19
20/* --------------------------------------------------------------------
21 * Cached Shader Key.
22 */
23
28
33
35{
36 return a.info_name == b.info_name && a.precision == b.precision;
37}
38
39/* --------------------------------------------------------------------
40 * Cached Shader.
41 */
42
43CachedShader::CachedShader(const char *info_name, ResultPrecision precision)
44{
45 using namespace gpu::shader;
46 ShaderCreateInfo info = *reinterpret_cast<const ShaderCreateInfo *>(
48
49 /* Finalize first in case the create info had additional info. */
50 info.finalize();
51
52 /* Change the format of image resource to the target precision. */
54 if (resource.bind_type != ShaderCreateInfo::Resource::BindType::IMAGE) {
55 continue;
56 }
57 resource.image.format = Result::gpu_texture_format(resource.image.format, precision);
58 }
59
60 shader_ = GPU_shader_create_from_info(reinterpret_cast<const GPUShaderCreateInfo *>(&info));
61}
62
67
69{
70 return shader_;
71}
72
73/* --------------------------------------------------------------------
74 * Cached Shader Container.
75 */
76
78{
79 /* First, delete all resources that are no longer needed. */
80 map_.remove_if([](auto item) { return !item.value->needed; });
81
82 /* Second, reset the needed status of the remaining resources to false to ready them to track
83 * their needed status for the next evaluation. */
84 for (auto &value : map_.values()) {
85 value->needed = false;
86 }
87}
88
89gpu::Shader *CachedShaderContainer::get(const char *info_name, ResultPrecision precision)
90{
91 const CachedShaderKey key(info_name, precision);
92
93 auto &cached_shader = *map_.lookup_or_add_cb(
94 key, [&]() { return std::make_unique<CachedShader>(info_name, precision); });
95
96 cached_shader.needed = true;
97 return cached_shader.shader();
98}
99
100} // namespace blender::compositor
void GPU_shader_free(blender::gpu::Shader *shader)
blender::gpu::Shader * GPU_shader_create_from_info(const GPUShaderCreateInfo *_info)
const GPUShaderCreateInfo * GPU_shader_create_info_get(const char *info_name)
unsigned long long int uint64_t
gpu::Shader * get(const char *info_name, ResultPrecision precision)
CachedShaderKey(const char *info_name, ResultPrecision precision)
CachedShader(const char *info_name, ResultPrecision precision)
static blender::gpu::TextureFormat gpu_texture_format(ResultType type, ResultPrecision precision)
Definition result.cc:65
#define resource
bool operator==(const BokehKernelKey &a, const BokehKernelKey &b)
uint64_t get_default_hash(const T &v, const Args &...args)
Definition BLI_hash.hh:233
Describe inputs & outputs, stage interfaces, resources and sources of a shader. If all data is correc...
void finalize(const bool recursive=false)