Blender V4.5
smaa_precomputed_textures.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 <memory>
6
7#include "BLI_smaa_textures.h"
8
9#include "GPU_shader.hh"
10#include "GPU_texture.hh"
11
12#include "COM_context.hh"
13#include "COM_result.hh"
15#include "COM_utilities.hh"
16
17namespace blender::compositor {
18
19/* ------------------------------------------------------------------------------------------------
20 * SMAA Precomputed Textures.
21 */
22
24 : search_texture(context.create_result(ResultType::Float)),
25 area_texture(context.create_result(ResultType::Float2))
26{
27 if (context.use_gpu()) {
28 this->compute_gpu();
29 }
30 else {
31 this->compute_cpu();
32 }
33}
34
36{
37 GPU_TEXTURE_FREE_SAFE(search_texture_);
38 GPU_TEXTURE_FREE_SAFE(area_texture_);
39 this->search_texture.release();
40 this->area_texture.release();
41}
42
44 const char *sampler_name) const
45{
46 const int texture_image_unit = GPU_shader_get_sampler_binding(shader, sampler_name);
47 GPU_texture_bind(search_texture_, texture_image_unit);
48}
49
54
55void SMAAPrecomputedTextures::bind_area_texture(GPUShader *shader, const char *sampler_name) const
56{
57 const int texture_image_unit = GPU_shader_get_sampler_binding(shader, sampler_name);
58 GPU_texture_bind(area_texture_, texture_image_unit);
59}
60
65
66void SMAAPrecomputedTextures::compute_gpu()
67{
68 search_texture_ = GPU_texture_create_2d("SMAA Search",
71 1,
72 GPU_R8,
74 nullptr);
76 GPU_texture_filter_mode(search_texture_, true);
77
78 area_texture_ = GPU_texture_create_2d("SMAA Area",
81 1,
82 GPU_RG8,
84 nullptr);
86 GPU_texture_filter_mode(area_texture_, true);
87}
88
89void SMAAPrecomputedTextures::compute_cpu()
90{
91 const int2 search_texture_size = int2(SEARCHTEX_WIDTH, SEARCHTEX_HEIGHT);
92 search_texture.allocate_texture(Domain(search_texture_size), false);
93 parallel_for(search_texture_size, [&](const int2 texel) {
94 const float value = searchTexBytes[int64_t(texel.y) * search_texture_size.x + texel.x] /
95 255.0f;
96 search_texture.store_pixel(texel, value);
97 });
98
99 const int2 area_texture_size = int2(AREATEX_WIDTH, AREATEX_HEIGHT);
100 area_texture.allocate_texture(Domain(area_texture_size), false);
101 parallel_for(area_texture_size, [&](const int2 texel) {
102 const float2 value = float2(uchar2(areaTexBytes +
103 (int64_t(texel.y) * area_texture_size.x + texel.x) * 2)) /
104 255.0f;
105 area_texture.store_pixel(texel, value);
106 });
107}
108
109/* ------------------------------------------------------------------------------------------------
110 * SMAA Precomputed Textures Container.
111 */
112
114{
115 /* First, delete the textures if they are no longer needed. */
116 if (textures_ && !textures_->needed) {
117 textures_.reset();
118 }
119
120 /* Second, if they were not deleted, reset their needed status to false to ready them to track
121 * their needed status for the next evaluation. */
122 if (textures_) {
123 textures_->needed = false;
124 }
125}
126
128{
129 if (!textures_) {
130 textures_ = std::make_unique<SMAAPrecomputedTextures>(context);
131 }
132
133 textures_->needed = true;
134 return *textures_;
135}
136
137} // namespace blender::compositor
#define SEARCHTEX_HEIGHT
const unsigned char searchTexBytes[]
#define SEARCHTEX_WIDTH
#define AREATEX_HEIGHT
const unsigned char areaTexBytes[]
#define AREATEX_WIDTH
int GPU_shader_get_sampler_binding(GPUShader *shader, const char *name)
void GPU_texture_bind(GPUTexture *texture, int unit)
GPUTexture * GPU_texture_create_2d(const char *name, int width, int height, int mip_len, eGPUTextureFormat format, eGPUTextureUsage usage, const float *data)
void GPU_texture_unbind(GPUTexture *texture)
@ GPU_DATA_UBYTE
@ GPU_TEXTURE_USAGE_SHADER_READ
void GPU_texture_filter_mode(GPUTexture *texture, bool use_filter)
#define GPU_TEXTURE_FREE_SAFE(texture)
@ GPU_RG8
@ GPU_R8
void GPU_texture_update(GPUTexture *texture, eGPUDataFormat data_format, const void *data)
long long int int64_t
void allocate_texture(Domain domain, bool from_pool=true)
Definition result.cc:309
void store_pixel(const int2 &texel, const T &pixel_value)
void bind_area_texture(GPUShader *shader, const char *sampler_name) const
void bind_search_texture(GPUShader *shader, const char *sampler_name) const
void parallel_for(const int2 range, const Function &function)
blender::VecBase< uint8_t, 2 > uchar2
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
int x
Definition types_int2.h:13
int y
Definition types_int2.h:13