Blender V4.5
eevee_lookdev.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 "BLI_rect.h"
10
11#include "BKE_image.hh"
12#include "BKE_lib_id.hh"
13#include "BKE_node.hh"
15#include "BKE_studiolight.h"
16
17#include "NOD_shader.h"
18
19#include "GPU_material.hh"
20
21#include "draw_cache.hh"
22#include "draw_view_data.hh"
23
24#include "eevee_instance.hh"
25
26namespace blender::eevee {
27
28/* -------------------------------------------------------------------- */
31
33{
34 /* Create a dummy World data block to hold the nodetree generated for studio-lights. */
35 world = BKE_id_new_nomain<::World>("Lookdev");
36
38 nullptr, &world->id, "Lookdev World Nodetree", ntreeType_Shader->idname);
39
40 bNode *coordinate = bke::node_add_static_node(nullptr, *ntree, SH_NODE_TEX_COORD);
41 bNodeSocket *coordinate_out = bke::node_find_socket(*coordinate, SOCK_OUT, "Generated");
42
45 bNodeSocket *rotate_vector_in = bke::node_find_socket(*rotate, SOCK_IN, "Vector");
46 angle_socket_ = static_cast<bNodeSocketValueFloat *>(
48 bNodeSocket *rotate_out = bke::node_find_socket(*rotate, SOCK_OUT, "Vector");
49
50 bNode *environment = bke::node_add_static_node(nullptr, *ntree, SH_NODE_TEX_ENVIRONMENT);
51 environment_node_ = environment;
52 NodeTexImage *environment_storage = static_cast<NodeTexImage *>(environment->storage);
53 bNodeSocket *environment_vector_in = bke::node_find_socket(*environment, SOCK_IN, "Vector");
54 bNodeSocket *environment_out = bke::node_find_socket(*environment, SOCK_OUT, "Color");
55
56 bNode *background = bke::node_add_static_node(nullptr, *ntree, SH_NODE_BACKGROUND);
57 bNodeSocket *background_out = bke::node_find_socket(*background, SOCK_OUT, "Background");
58 bNodeSocket *background_color_in = bke::node_find_socket(*background, SOCK_IN, "Color");
59 intensity_socket_ = static_cast<bNodeSocketValueFloat *>(
60 bke::node_find_socket(*background, SOCK_IN, "Strength")->default_value);
61
63 bNodeSocket *output_in = bke::node_find_socket(*output, SOCK_IN, "Surface");
64
65 bke::node_add_link(*ntree, *coordinate, *coordinate_out, *rotate, *rotate_vector_in);
66 bke::node_add_link(*ntree, *rotate, *rotate_out, *environment, *environment_vector_in);
67 bke::node_add_link(*ntree, *environment, *environment_out, *background, *background_color_in);
68 bke::node_add_link(*ntree, *background, *background_out, *output, *output_in);
70
71 world->use_nodes = true;
72 world->nodetree = ntree;
73
74 /* Create a dummy image data block to hold GPU textures generated by studio-lights. */
75 image = BKE_id_new_nomain<::Image>("Lookdev");
76 image->type = IMA_TYPE_IMAGE;
77 image->source = IMA_SRC_GENERATED;
78 ImageTile *base_tile = BKE_image_get_tile(image, 0);
79 base_tile->gen_x = 1;
80 base_tile->gen_y = 1;
81 base_tile->gen_type = IMA_GENTYPE_BLANK;
82 copy_v4_fl(base_tile->gen_color, 0.0f);
83 /* TODO: This works around the issue that the first time the texture is accessed the image would
84 * overwrite the set GPU texture. A better solution would be to use image data-blocks as part of
85 * the studio-lights, but that requires a larger refactoring. */
86 BKE_image_get_gpu_texture(image, &environment_storage->iuser);
87}
88
90{
91 BKE_id_free(nullptr, &image->id);
92 BKE_id_free(nullptr, &world->id);
93}
94
95bool LookdevWorld::sync(const LookdevParameters &new_parameters)
96{
97 const bool parameters_changed = assign_if_different(parameters_, new_parameters);
98
99 if (parameters_changed) {
100 intensity_socket_->value = parameters_.intensity;
101 angle_socket_->value = parameters_.rot_z;
102
103 GPU_TEXTURE_FREE_SAFE(image->gputexture[TEXTARGET_2D][0]);
104 environment_node_->id = nullptr;
105
106 StudioLight *sl = BKE_studiolight_find(parameters_.hdri.c_str(),
108 if (sl) {
110 GPUTexture *texture = sl->equirect_radiance_gputexture;
111 if (texture != nullptr) {
113 image->gputexture[TEXTARGET_2D][0] = texture;
114 environment_node_->id = &image->id;
115 }
116 }
117
118 GPU_material_free(&world->gpumaterial);
119 }
120 return parameters_changed;
121}
122
124
125/* -------------------------------------------------------------------- */
129
131
133{
134 for (gpu::Batch *batch : sphere_lod_) {
136 }
137}
138
139blender::gpu::Batch *LookdevModule::sphere_get(const SphereLOD level_of_detail)
140{
141 BLI_assert(level_of_detail >= SphereLOD::LOW && level_of_detail < SphereLOD::MAX);
142
143 if (sphere_lod_[level_of_detail] != nullptr) {
144 return sphere_lod_[level_of_detail];
145 }
146
147 int lat_res;
148 int lon_res;
149 switch (level_of_detail) {
150 case 2:
151 lat_res = 80;
152 lon_res = 60;
153 break;
154 case 1:
155 lat_res = 64;
156 lon_res = 48;
157 break;
158 default:
159 case 0:
160 lat_res = 32;
161 lon_res = 24;
162 break;
163 }
164
165 GPUVertFormat format = {0};
168 struct Vert {
169 float x, y, z;
170 float nor_x, nor_y, nor_z;
171 };
172
173 blender::gpu::VertBuf *vbo = GPU_vertbuf_create_with_format(format);
174 int v_len = (lat_res - 1) * lon_res * 6;
175 GPU_vertbuf_data_alloc(*vbo, v_len);
176
177 const float lon_inc = 2 * M_PI / lon_res;
178 const float lat_inc = M_PI / lat_res;
179 float lon, lat;
180
181 int v = 0;
182 lon = 0.0f;
183
184 auto sphere_lat_lon_vert = [&](float lat, float lon) {
185 Vert vert;
186 vert.nor_x = vert.x = sinf(lat) * cosf(lon);
187 vert.nor_y = vert.y = cosf(lat);
188 vert.nor_z = vert.z = sinf(lat) * sinf(lon);
189 GPU_vertbuf_vert_set(vbo, v, &vert);
190 v++;
191 };
192
193 for (int i = 0; i < lon_res; i++, lon += lon_inc) {
194 lat = 0.0f;
195 for (int j = 0; j < lat_res; j++, lat += lat_inc) {
196 if (j != lat_res - 1) { /* Pole */
197 sphere_lat_lon_vert(lat + lat_inc, lon + lon_inc);
198 sphere_lat_lon_vert(lat + lat_inc, lon);
199 sphere_lat_lon_vert(lat, lon);
200 }
201 if (j != 0) { /* Pole */
202 sphere_lat_lon_vert(lat, lon + lon_inc);
203 sphere_lat_lon_vert(lat + lat_inc, lon + lon_inc);
204 sphere_lat_lon_vert(lat, lon);
205 }
206 }
207 }
208
209 sphere_lod_[level_of_detail] = GPU_batch_create_ex(
210 GPU_PRIM_TRIS, vbo, nullptr, GPU_BATCH_OWNS_VBO);
211 return sphere_lod_[level_of_detail];
212}
213
214void LookdevModule::init(const rcti *visible_rect)
215{
216 visible_rect_ = *visible_rect;
217 enabled_ = inst_.is_viewport() && inst_.overlays_enabled() && inst_.use_lookdev_overlay();
218
219 if (enabled_) {
220 const int2 extent_dummy(1);
223 dummy_cryptomatte_tx_.ensure_2d(GPU_RGBA32F, extent_dummy, usage);
224 dummy_aov_color_tx_.ensure_2d_array(GPU_RGBA16F, extent_dummy, 1, usage);
225 dummy_aov_value_tx_.ensure_2d_array(GPU_R16F, extent_dummy, 1, usage);
226 }
227}
228
229float LookdevModule::calc_viewport_scale()
230{
231 const float viewport_scale = clamp_f(
232 BLI_rcti_size_x(&visible_rect_) / (2000.0f * UI_SCALE_FAC), 0.5f, 1.0f);
233 return viewport_scale;
234}
235
236LookdevModule::SphereLOD LookdevModule::calc_level_of_detail(const float viewport_scale)
237{
238 float res_scale = clamp_f(
239 (U.lookdev_sphere_size / 400.0f) * viewport_scale * UI_SCALE_FAC, 0.1f, 1.0f);
240
241 if (res_scale > 0.7f) {
242 return LookdevModule::SphereLOD::HIGH;
243 }
244 if (res_scale > 0.25f) {
245 return LookdevModule::SphereLOD::MEDIUM;
246 }
247 return LookdevModule::SphereLOD::LOW;
248}
249
250static int calc_sphere_extent(const float viewport_scale)
251{
252 const int sphere_radius = U.lookdev_sphere_size * UI_SCALE_FAC * viewport_scale;
253 return sphere_radius * 2;
254}
255
257{
258 if (!enabled_) {
259 return;
260 }
261 const float viewport_scale = calc_viewport_scale();
262 const int2 extent = int2(calc_sphere_extent(viewport_scale));
263
264 const eGPUTextureFormat color_format = GPU_RGBA16F;
265
266 for (int index : IndexRange(num_spheres)) {
267 if (spheres_[index].color_tx_.ensure_2d(color_format, extent)) {
268 /* Request redraw if the light-probe were off and the sampling was already finished. */
269 if (inst_.is_viewport() && inst_.sampling.finished_viewport()) {
270 inst_.sampling.reset();
271 }
272 }
273
274 spheres_[index].framebuffer.ensure(GPU_ATTACHMENT_NONE,
275 GPU_ATTACHMENT_TEXTURE(spheres_[index].color_tx_));
276 }
277
278 const Camera &cam = inst_.camera;
279 float sphere_distance = cam.data_get().clip_near;
280 int2 display_extent = inst_.film.display_extent_get();
281 float pixel_radius = ShadowModule::screen_pixel_radius(
282 cam.data_get().wininv, cam.is_perspective(), display_extent);
283
284 if (cam.is_perspective()) {
285 pixel_radius *= sphere_distance;
286 }
287
288 this->sphere_radius_ = (extent.x / 2) * pixel_radius;
289 this->sphere_position_ = cam.position() -
290 cam.forward() * (sphere_distance + this->sphere_radius_);
291
292 float4x4 model_m4 = float4x4(float3x3(cam.data_get().viewmat));
293 model_m4.location() = this->sphere_position_;
294 model_m4 = math::scale(model_m4, float3(this->sphere_radius_));
295
296 ResourceHandle handle = inst_.manager->resource_handle(model_m4);
297 gpu::Batch *geom = sphere_get(calc_level_of_detail(viewport_scale));
298
299 sync_pass(spheres_[0].pass, geom, inst_.materials.metallic_mat, handle);
300 sync_pass(spheres_[1].pass, geom, inst_.materials.diffuse_mat, handle);
301 sync_display();
302}
303
304void LookdevModule::sync_pass(PassSimple &pass,
305 gpu::Batch *geom,
306 ::Material *mat,
307 ResourceHandle res_handle)
308{
309 pass.init();
310 pass.clear_color_depth_stencil(float4(0.0, 0.0, 0.0, 1.0), inst_.film.depth.clear_value, 0);
311
313
316 pass.state_set(state);
317 pass.material_set(*inst_.manager, gpumat);
319 pass.bind_resources(inst_.uniform_data);
320 pass.bind_resources(inst_.lights);
321 pass.bind_resources(inst_.shadows);
322 pass.bind_resources(inst_.volume.result);
323 pass.bind_resources(inst_.sampling);
324 pass.bind_resources(inst_.hiz_buffer.front);
325 pass.bind_resources(inst_.volume_probes);
326 pass.bind_resources(inst_.sphere_probes);
327 pass.draw(geom, res_handle, 0);
328}
329
330void LookdevModule::sync_display()
331{
332 PassSimple &pass = display_ps_;
333
334 const float2 viewport_size = inst_.draw_ctx->viewport_size_get();
337 pass.init();
338 pass.state_set(state);
339 pass.shader_set(inst_.shaders.static_shader_get(LOOKDEV_DISPLAY));
340 pass.push_constant("viewportSize", viewport_size);
341 pass.push_constant("invertedViewportSize", 1.0f / viewport_size);
342 pass.push_constant("anchor", int2(visible_rect_.xmax, visible_rect_.ymin));
343 pass.bind_texture("metallic_tx", &spheres_[0].color_tx_);
344 pass.bind_texture("diffuse_tx", &spheres_[1].color_tx_);
345
346 pass.draw_procedural(GPU_PRIM_TRIS, 2, 6);
347}
348
350{
351 if (!enabled_) {
352 return;
353 }
354
355 inst_.volume_probes.set_view(view);
356 inst_.sphere_probes.set_view(view);
357
358 for (Sphere &sphere : spheres_) {
359 sphere.framebuffer.bind();
360 inst_.manager->submit(sphere.pass, view);
361 }
362}
363
365{
366 if (!enabled_) {
367 return;
368 }
369
370 BLI_assert(inst_.is_viewport());
371
372 DefaultFramebufferList *dfbl = inst_.draw_ctx->viewport_framebuffer_list_get();
373 /* The viewport of the framebuffer can be modified when border rendering is enabled. */
376 inst_.manager->submit(display_ps_);
377}
378
380
381/* -------------------------------------------------------------------- */
384
386
388{
389 if (v3d == nullptr) {
390 return;
391 }
392
393 const ::View3DShading &shading = v3d->shading;
394 show_scene_world = shading.type == OB_RENDER ? shading.flag & V3D_SHADING_SCENE_WORLD_RENDER :
395 shading.flag & V3D_SHADING_SCENE_WORLD;
396 if (!show_scene_world) {
397 rot_z = shading.studiolight_rot_z;
398 background_opacity = shading.studiolight_background;
399 blur = shading.studiolight_blur;
400 intensity = shading.studiolight_intensity;
401 hdri = StringRefNull(shading.lookdev_light);
402 }
403}
404
406{
407 return hdri == other.hdri && rot_z == other.rot_z &&
408 background_opacity == other.background_opacity && blur == other.blur &&
410}
411
413{
414 return !(*this == other);
415}
416
418
419} // namespace blender::eevee
GPUTexture * BKE_image_get_gpu_texture(Image *image, ImageUser *iuser)
Definition image_gpu.cc:486
void BKE_id_free(Main *bmain, void *idv)
void * BKE_id_new_nomain(short type, const char *name)
Definition lib_id.cc:1500
#define SH_NODE_OUTPUT_WORLD
#define SH_NODE_VECTOR_ROTATE
#define SH_NODE_TEX_COORD
#define SH_NODE_TEX_ENVIRONMENT
#define SH_NODE_BACKGROUND
@ STUDIOLIGHT_EQUIRECT_RADIANCE_GPUTEXTURE
struct StudioLight * BKE_studiolight_find(const char *name, int flag)
void BKE_studiolight_ensure_flag(StudioLight *sl, int flag)
#define STUDIOLIGHT_ORIENTATIONS_MATERIAL_MODE
#define BLI_assert(a)
Definition BLI_assert.h:46
MINLINE float clamp_f(float value, float min, float max)
#define M_PI
MINLINE void copy_v4_fl(float r[4], float f)
BLI_INLINE int BLI_rcti_size_x(const struct rcti *rct)
Definition BLI_rect.h:194
@ IMA_SRC_GENERATED
@ IMA_GENTYPE_BLANK
@ IMA_TYPE_IMAGE
@ TEXTARGET_2D
@ NODE_VECTOR_ROTATE_TYPE_AXIS_Z
@ SOCK_OUT
@ SOCK_IN
@ OB_RENDER
#define UI_SCALE_FAC
@ V3D_SHADING_SCENE_WORLD_RENDER
@ V3D_SHADING_SCENE_WORLD
static AppView * view
blender::gpu::Batch * GPU_batch_create_ex(GPUPrimType primitive_type, blender::gpu::VertBuf *vertex_buf, blender::gpu::IndexBuf *index_buf, eGPUBatchFlag owns_flag)
Definition gpu_batch.cc:51
#define GPU_BATCH_DISCARD_SAFE(batch)
Definition GPU_batch.hh:204
@ GPU_BATCH_OWNS_VBO
Definition GPU_batch.hh:41
#define GPU_ATTACHMENT_TEXTURE(_texture)
void GPU_framebuffer_viewport_reset(GPUFrameBuffer *fb)
#define GPU_ATTACHMENT_NONE
void GPU_framebuffer_bind(GPUFrameBuffer *fb)
void GPU_material_free(ListBase *gpumaterial)
@ GPU_PRIM_TRIS
void GPU_texture_ref(GPUTexture *texture)
eGPUTextureUsage
@ GPU_TEXTURE_USAGE_SHADER_READ
@ GPU_TEXTURE_USAGE_SHADER_WRITE
#define GPU_TEXTURE_FREE_SAFE(texture)
eGPUTextureFormat
@ GPU_R16F
@ GPU_RGBA32F
@ GPU_RGBA16F
#define GPU_vertbuf_create_with_format(format)
void GPU_vertbuf_vert_set(blender::gpu::VertBuf *verts, uint v_idx, const void *data)
void GPU_vertbuf_data_alloc(blender::gpu::VertBuf &verts, uint v_len)
@ GPU_FETCH_FLOAT
uint GPU_vertformat_attr_add(GPUVertFormat *, blender::StringRef name, GPUVertCompType, uint comp_len, GPUVertFetchMode)
@ GPU_COMP_F32
struct blender::bke::bNodeTreeType * ntreeType_Shader
#define U
ATTR_WARN_UNUSED_RESULT const BMVert * v
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
void bind_texture(const char *name, GPUTexture *texture, GPUSamplerState state=sampler_auto)
void bind_resources(U &resources)
Definition draw_pass.hh:440
void draw(gpu::Batch *batch, uint instance_len=-1, uint vertex_len=-1, uint vertex_first=-1, ResourceHandleRange handle={0}, uint custom_id=0)
Definition draw_pass.hh:884
void state_set(DRWState state, int clip_plane_count=0)
void clear_color_depth_stencil(float4 color, float depth, uint8_t stencil)
void material_set(Manager &manager, GPUMaterial *material, bool deferred_texture_loading=false)
const float3 & forward() const
const float3 & position() const
const CameraData & data_get() const
bool is_perspective() const
struct blender::eevee::Film::DepthState depth
struct blender::eevee::HiZBuffer::@117223074165324154125050272272211024005337204302 front
A running instance of the engine.
VolumeProbeModule volume_probes
SphereProbeModule sphere_probes
const DRWContext * draw_ctx
UniformDataModule uniform_data
void init(const rcti *visible_rect)
bool sync(const LookdevParameters &new_parameters)
GPUMaterial * material_shader_get(::Material *blender_mat, bNodeTree *nodetree, eMaterialPipeline pipeline_type, eMaterialGeometry geometry_type, bool deferred_compilation, ::Material *default_mat)
GPUShader * static_shader_get(eShaderType shader_type)
static float screen_pixel_radius(const float4x4 &wininv, bool is_perspective, const int2 &extent)
struct blender::eevee::VolumeModule::@215032374165261147333173176262124367334320352300 result
#define sinf(x)
#define cosf(x)
DRWState
Definition draw_state.hh:25
@ DRW_STATE_BLEND_ALPHA
Definition draw_state.hh:55
@ DRW_STATE_WRITE_DEPTH
Definition draw_state.hh:29
@ DRW_STATE_WRITE_COLOR
Definition draw_state.hh:30
@ DRW_STATE_CULL_BACK
Definition draw_state.hh:43
@ DRW_STATE_DEPTH_ALWAYS
Definition draw_state.hh:36
#define RBUFS_UTILITY_TEX_SLOT
struct @064345207361167251075330302113175271221317160336::@113254110077376341056327177062323111323010325277 batch
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
#define output
format
static ulong state[N]
bNodeTree * node_tree_add_tree_embedded(Main *bmain, ID *owner_id, StringRefNull name, StringRefNull idname)
Definition node.cc:4375
bNodeSocket * node_find_socket(bNode &node, eNodeSocketInOut in_out, StringRef identifier)
Definition node.cc:2864
bNode * node_add_static_node(const bContext *C, bNodeTree &ntree, int type)
Definition node.cc:3804
bNodeLink & node_add_link(bNodeTree &ntree, bNode &fromnode, bNodeSocket &fromsock, bNode &tonode, bNodeSocket &tosock)
Definition node.cc:4087
void node_set_active(bNodeTree &ntree, bNode &node)
Definition node.cc:4996
detail::Pass< command::DrawCommandBuf > PassSimple
static int calc_sphere_extent(const float viewport_scale)
MatBase< T, NumCol, NumRow > scale(const MatBase< T, NumCol, NumRow > &mat, const VectorT &scale)
MatBase< T, NumCol, NumRow > rotate(const MatBase< T, NumCol, NumRow > &mat, const RotationT &rotation)
bool assign_if_different(T &old_value, T new_value)
MatBase< float, 4, 4 > float4x4
VecBase< float, 4 > float4
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
MatBase< float, 3, 3 > float3x3
VecBase< float, 3 > float3
blender::float2 viewport_size_get() const
GPUFrameBuffer * default_fb
float gen_color[4]
struct bNodeTree * nodetree
struct GPUTexture * equirect_radiance_gputexture
void * default_value
void * storage
bool operator==(const LookdevParameters &other) const
bool operator!=(const LookdevParameters &other) const
int ymin
int xmax
void * BKE_image_get_tile
Definition stubs.c:36
i
Definition text_draw.cc:230