Blender V5.3
workbench_resources.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
8
9#include "../eevee/eevee_lut.hh" /* TODO: find somewhere to share blue noise Table. */
10
11#include "BKE_studiolight.h"
12
13#include "BLI_math_matrix_c.hh"
15#include "BLI_math_vector_c.hh"
16
17#include "GPU_batch_utils.hh"
19#include "IMB_imbuf.hh"
20#include "IMB_imbuf_types.hh"
21
22#include "draw_common_c.hh"
23#include "workbench_private.hh"
24
25namespace blender::workbench {
26
27static bool get_matcap_tx(Texture &matcap_tx, StudioLight &studio_light)
28{
29 BKE_studiolight_ensure_flag(&studio_light,
32 ImBuf *matcap_diffuse = studio_light.matcap_diffuse.ibuf;
33 ImBuf *matcap_specular = studio_light.matcap_specular.ibuf;
34 if (matcap_diffuse && matcap_diffuse->float_data()) {
35 int layers = 1;
36 const float *buffer = matcap_diffuse->float_data();
37 Vector<float> combined_buffer;
38
39 if (matcap_specular && matcap_specular->float_data()) {
40 int size = matcap_diffuse->x * matcap_diffuse->y * 4;
41 combined_buffer.extend(matcap_diffuse->float_data(), size);
42 combined_buffer.extend(matcap_specular->float_data(), size);
43 buffer = combined_buffer.begin();
44 layers++;
45 }
46
47 matcap_tx = Texture(studio_light.name,
48 gpu::TextureFormat::SFLOAT_16_16_16_16,
50 int2(matcap_diffuse->x, matcap_diffuse->y),
51 layers,
52 buffer);
53 return true;
54 }
55 return false;
56}
57
58static float4x4 get_world_shading_rotation_matrix(float studiolight_rot_z)
59{
61 float R[4][4];
62 axis_angle_to_mat4_single(R, 'Z', -studiolight_rot_z);
63 mul_m4_m4m4(R, V.ptr(), R);
64 swap_v3_v3(R[2], R[1]);
65 negate_v3(R[2]);
66 return float4x4(R);
67}
68
70 const float4x4 &world_shading_rotation)
71{
72 SolidLightData light = {};
73 if (sl && sl->flag) {
74 float3 direction = math::transform_direction(world_shading_rotation, float3(sl->vec));
75 light.direction = float4(direction, 0.0f);
76 /* We should pre-divide the power by PI but that makes the lights really dim. */
77 light.specular_color = float4(float3(sl->spec), 0.0f);
78 light.diffuse_color_wrap = float4(float3(sl->col), sl->smooth);
79 }
80 else {
81 light.direction = float4(1.0f, 0.0f, 0.0f, 0.0f);
82 light.specular_color = float4(0.0f);
83 light.diffuse_color_wrap = float4(0.0f);
84 }
85 return light;
86}
87
88void SceneResources::load_jitter_tx(int total_samples)
89{
91
92 const float total_samples_inv = 1.0f / total_samples;
93
94 /* Create blue noise jitter texture */
95 for (int x = 0; x < 64; x++) {
96 for (int y = 0; y < 64; y++) {
97 float phi = eevee::lut::blue_noise[y][x][0] * 2.0f * M_PI;
98 /* This rotate the sample per pixels */
99 jitter[y][x].x = math::cos(phi);
100 jitter[y][x].y = math::sin(phi);
101 /* This offset the sample along its direction axis (reduce banding) */
102 float bn = eevee::lut::blue_noise[y][x][1] - 0.5f;
103 bn = clamp_f(bn, -0.499f, 0.499f); /* fix fireflies */
104 jitter[y][x].z = bn * total_samples_inv;
105 jitter[y][x].w = eevee::lut::blue_noise[y][x][1];
106 }
107 }
108
109 jitter_tx.free();
110 jitter_tx.ensure_2d(gpu::TextureFormat::SFLOAT_16_16_16_16,
113 jitter[0][0]);
114}
115
116void SceneResources::init(const SceneState &scene_state, const DRWContext *ctx)
117{
118 const View3DShading &shading = scene_state.shading;
119
122 world_buf.xray_mode = scene_state.xray_mode;
126 world_buf.ui_scale = ctx->is_image_render() ? 1.0f : U.pixelsize;
128
129 StudioLight *studio_light = nullptr;
130 if (U.edit_studio_light && shading.light == V3D_LIGHTING_STUDIO) {
131 /* Do not use this for MATCAP.
132 * matcap is also stored as StudioLight data but it needs its selected matcap texture loaded
133 * through the normal matcap path. */
134 studio_light = BKE_studiolight_studio_edit_get();
135 }
136 else {
137 if (shading.light == V3D_LIGHTING_MATCAP) {
138 studio_light = BKE_studiolight_find(shading.matcap, STUDIOLIGHT_TYPE_MATCAP);
140 if (studio_light &&
141 (studio_light->name != current_matcap || working_space != current_matcap_working_space))
142 {
143 if (get_matcap_tx(matcap_tx, *studio_light)) {
144 current_matcap = studio_light->name;
145 current_matcap_working_space = working_space;
146 }
147 }
148 }
149 /* If matcaps are missing, use this as fallback. */
150 if (studio_light == nullptr) {
152 }
153 }
154 if (!matcap_tx.is_valid()) {
156 gpu::TextureFormat::SFLOAT_16_16_16_16, int2(1), 1, GPU_TEXTURE_USAGE_SHADER_READ);
157 }
158
159 float4x4 world_shading_rotation = float4x4::identity();
160 if (shading.flag & V3D_SHADING_WORLD_ORIENTATION) {
161 world_shading_rotation = get_world_shading_rotation_matrix(shading.studiolight_rot_z);
162 }
163
164 for (int i = 0; i < 4; i++) {
165 SolidLight *sl = (studio_light) ? &studio_light->light[i] : nullptr;
166 world_buf.lights[i] = get_light_data_from_studio_solidlight(sl, world_shading_rotation);
167 }
168
169 if (studio_light != nullptr) {
170 world_buf.ambient_color = float4(float3(studio_light->light_ambient), 0.0f);
173 }
174 else {
175 world_buf.ambient_color = float4(1.0f, 1.0f, 1.0f, 0.0f);
176 world_buf.use_specular = false;
177 }
178
179 /* TODO(@pragma37): volumes_do */
180
181 cavity.init(scene_state, *this);
182
183 if (scene_state.draw_dof && !jitter_tx.is_valid()) {
184 /* We don't care about total_samples in this case */
186 }
187
189
190 for (int i : IndexRange(6)) {
191 if (i < scene_state.clip_planes.size()) {
192 clip_planes_buf[i] = scene_state.clip_planes[i];
193 }
194 else {
195 clip_planes_buf[i] = float4(0);
196 }
197 }
198
199 clip_planes_buf.push_update();
200
201 if (missing_texture.gpu.texture == nullptr) {
203 missing_texture.name = "Missing Texture";
204 }
205
206 dummy_texture_tx.ensure_2d(gpu::TextureFormat::UNORM_8_8_8_8,
207 int2(1),
209 float4(0.0f, 0.0f, 0.0f, 0.0f));
210 dummy_tile_array_tx.ensure_2d_array(gpu::TextureFormat::UNORM_8_8_8_8,
211 int2(1),
212 1,
214 float4(0.0f, 0.0f, 0.0f, 0.0f));
215 dummy_tile_data_tx.ensure_1d_array(gpu::TextureFormat::UNORM_8_8_8_8,
216 1,
217 1,
219 float4(0.0f, 0.0f, 0.0f, 0.0f));
220
221 if (volume_cube_batch == nullptr) {
223 }
224}
225
226} // namespace blender::workbench
#define M_PI
constexpr IndexRange()=default
void extend(Span< T > array)
bool ensure_2d(gpu::TextureFormat format, int2 extent, eGPUTextureUsage usage=GPU_TEXTURE_USAGE_GENERAL, const float *data=nullptr, int mip_len=1)
Texture(const char *name="gpu::Texture")
bool ensure_1d_array(gpu::TextureFormat format, int extent, int layers, eGPUTextureUsage usage=GPU_TEXTURE_USAGE_GENERAL, const float *data=nullptr, int mip_len=1)
bool ensure_2d_array(gpu::TextureFormat format, int2 extent, int layers, eGPUTextureUsage usage=GPU_TEXTURE_USAGE_GENERAL, const float *data=nullptr, int mip_len=1)
static View & default_get()
Definition draw_view.cc:317
const float4x4 & viewmat(int view_id=0) const
Definition draw_view.hh:136
void init(const SceneState &scene_state, SceneResources &resources)
#define R
const float blue_noise[64][64][4]
T cos(const AngleRadianBase< T > &a)
T sin(const AngleRadianBase< T > &a)
VecBase< T, 3 > transform_direction(const MatBase< T, 3, 3 > &mat, const VecBase< T, 3 > &direction)
static bool get_matcap_tx(Texture &matcap_tx, StudioLight &studio_light)
static float4x4 get_world_shading_rotation_matrix(float studiolight_rot_z)
static SolidLightData get_light_data_from_studio_solidlight(const SolidLight *sl, const float4x4 &world_shading_rotation)
UserDef U
Definition blender.cc:58
@ STUDIOLIGHT_TYPE_MATCAP
@ STUDIOLIGHT_MATCAP_SPECULAR_GPUTEXTURE
@ STUDIOLIGHT_TYPE_STUDIO
@ STUDIOLIGHT_MATCAP_DIFFUSE_GPUTEXTURE
@ STUDIOLIGHT_SPECULAR_HIGHLIGHT_PASS
void BKE_studiolight_ensure_flag(StudioLight *sl, int flag)
gpu::Batch * GPU_batch_unit_cube() ATTR_WARN_UNUSED_RESULT
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
MatBase< float, 4, 4 > float4x4
@ V3D_SHADING_WORLD_ORIENTATION
@ V3D_SHADING_MATCAP_FLIP_X
@ V3D_SHADING_SPECULAR_HIGHLIGHT
struct StudioLight * BKE_studiolight_find(const char *name, int flag)
VecBase< float, 4 > float4
VecBase< int32_t, 2 > int2
@ GPU_TEXTURE_USAGE_SHADER_READ
MINLINE void swap_v3_v3(float a[3], float b[3])
MINLINE void negate_v3(float r[3])
void axis_angle_to_mat4_single(float R[4][4], char axis, float angle)
gpu::Texture * GPU_texture_create_error(int dimension, bool array)
VecBase< float, 3 > float3
const char * IMB_colormanagement_working_space_get()
StudioLight * BKE_studiolight_studio_edit_get()
MINLINE float clamp_f(float value, float min, float max)
struct SolidLightData lights[4]
bool is_image_render() const
float2 viewport_size_get() const
const float * float_data() const
StudioLightImage matcap_specular
StudioLightImage matcap_diffuse
char name[FILE_MAXFILE]
SolidLight light[STUDIOLIGHT_MAX_LIGHT]
eView3DShading_Flag flag
eV3DShadingLightingMode light
UniformBuffer< WorldData > world_buf
void init(const SceneState &scene_state, const DRWContext *ctx)
UniformArrayBuffer< float4, 6 > clip_planes_buf
CCL_NAMESPACE_BEGIN struct Window V