Blender V5.3
eevee_material.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_time.hh"
10#include "DNA_material_types.h"
11
12#include "BKE_lib_id.hh"
13#include "BKE_material.hh"
14#include "BKE_node.hh"
17#include "BKE_scene.hh"
18
19#include "eevee_instance.hh"
20#include "eevee_material.hh"
21
22namespace blender::eevee {
23
24/* -------------------------------------------------------------------- */
28
30{
31 {
32 diffuse_mat = BKE_id_new_nomain<blender::Material>("EEVEE default diffuse");
33 bNodeTree *ntree = diffuse_mat->nodetree;
34 diffuse_mat->surface_render_method = MA_SURFACE_METHOD_FORWARD;
35
36 /* Use 0.18 as it is close to middle gray. Middle gray is typically defined as 18% reflectance
37 * of visible light and commonly used for VFX balls. */
39 bNodeSocket *base_color = bke::node_find_socket(*bsdf, SOCK_IN, "Color"_ustr);
40 copy_v3_fl((static_cast<bNodeSocketValueRGBA *>(base_color->default_value))->value, 0.18f);
41
43
44 bke::node_add_link(*ntree,
45 *bsdf,
46 *bke::node_find_socket(*bsdf, SOCK_OUT, "BSDF"_ustr),
47 *output,
48 *bke::node_find_socket(*output, SOCK_IN, "Surface"_ustr));
49
52 }
53 {
55 bNodeTree *ntree = metallic_mat->nodetree;
56 metallic_mat->surface_render_method = MA_SURFACE_METHOD_FORWARD;
57
58 bNode *bsdf = bke::node_add_static_node(nullptr, *ntree, SH_NODE_BSDF_GLOSSY);
59 bNodeSocket *base_color = bke::node_find_socket(*bsdf, SOCK_IN, "Color"_ustr);
60 copy_v3_fl((static_cast<bNodeSocketValueRGBA *>(base_color->default_value))->value, 1.0f);
61 bNodeSocket *roughness = bke::node_find_socket(*bsdf, SOCK_IN, "Roughness"_ustr);
62 (static_cast<bNodeSocketValueFloat *>(roughness->default_value))->value = 0.0f;
63
65
66 bke::node_add_link(*ntree,
67 *bsdf,
68 *bke::node_find_socket(*bsdf, SOCK_OUT, "BSDF"_ustr),
69 *output,
70 *bke::node_find_socket(*output, SOCK_IN, "Surface"_ustr));
71
74 }
75 {
77 nullptr, &BKE_material_default_surface()->id, nullptr, LIB_ID_COPY_LOCALIZE));
79 nullptr, &BKE_material_default_volume()->id, nullptr, LIB_ID_COPY_LOCALIZE));
80 }
81 {
82 error_mat_ = BKE_id_new_nomain<blender::Material>("EEVEE default error");
83 bNodeTree *ntree = error_mat_->nodetree;
84
85 /* Use emission and output material to be compatible with both World and Material. */
86 bNode *bsdf = bke::node_add_static_node(nullptr, *ntree, SH_NODE_EMISSION);
87 bNodeSocket *color = bke::node_find_socket(*bsdf, SOCK_IN, "Color"_ustr);
89 (static_cast<bNodeSocketValueRGBA *>(color->default_value))->value, 1.0f, 0.0f, 1.0f);
90
92
93 bke::node_add_link(*ntree,
94 *bsdf,
95 *bke::node_find_socket(*bsdf, SOCK_OUT, "Emission"_ustr),
96 *output,
97 *bke::node_find_socket(*output, SOCK_IN, "Surface"_ustr));
98
101 }
102}
103
105{
106 BKE_id_free(nullptr, metallic_mat);
107 BKE_id_free(nullptr, diffuse_mat);
109 BKE_id_free(nullptr, default_volume);
110 BKE_id_free(nullptr, error_mat_);
111}
112
114{
115 float frame = BKE_scene_frame_get(inst_.scene);
116
117 Scene *scene = inst_.scene;
118 bool frame_change = assign_if_different(material_frame, frame);
119 bool time_change = assign_if_different(material_time, float(FRA2TIME(frame)));
120
121 material_time_changed = (time_change || frame_change);
122
123 inst_.uniform_data.data.scene.time = material_time;
124 inst_.uniform_data.data.scene.frame = material_frame;
125
129
130 material_override = DEG_get_evaluated(inst_.depsgraph, inst_.view_layer->mat_override);
131
133 gpu_pass_last_update_ = gpu_pass_next_update_;
134 gpu_pass_next_update_ = next_update;
135
136 material_map_.clear();
137 shader_map_.clear();
138}
139
140MaterialPass MaterialModule::material_pass_get(Object *ob,
141 blender::Material *blender_mat,
142 eMaterialPipeline pipeline_type,
143 eMaterialGeometry geometry_type,
144 eMaterialProbe probe_capture)
145{
146 bNodeTree *ntree = (blender_mat->nodetree != nullptr) ? blender_mat->nodetree :
147 default_surface->nodetree;
148
149 /* We can't defer compilation in viewport image render, since we can't re-sync.(See #130235) */
150 bool use_deferred_compilation = !inst_.is_viewport_image_render;
151
152 const bool is_volume = ELEM(pipeline_type, MAT_PIPE_VOLUME_OCCUPANCY, MAT_PIPE_VOLUME_MATERIAL);
153 blender::Material *default_mat = is_volume ? default_volume : default_surface;
154
155 MaterialPass matpass = MaterialPass();
156 matpass.gpumat = inst_.shaders.material_shader_get(
157 blender_mat, ntree, pipeline_type, geometry_type, use_deferred_compilation, default_mat);
158
159 const bool is_forward = ELEM(pipeline_type,
164
165 switch (GPU_material_status(matpass.gpumat)) {
166 case GPU_MAT_SUCCESS: {
167 /* Determine optimization status for remaining compilations counter. */
168 int optimization_status = GPU_material_optimization_status(matpass.gpumat);
169 if (optimization_status == GPU_MAT_OPTIMIZATION_QUEUED) {
171 }
172 break;
173 }
174 case GPU_MAT_QUEUED:
176 matpass.gpumat = inst_.shaders.material_shader_get(
177 default_mat, default_mat->nodetree, pipeline_type, geometry_type, false, nullptr);
178 break;
179 case GPU_MAT_FAILED:
180 default:
181 matpass.gpumat = inst_.shaders.material_shader_get(
182 error_mat_, error_mat_->nodetree, pipeline_type, geometry_type, false, nullptr);
183 break;
184 }
185 /* Returned material should be ready to be drawn. */
187
188 inst_.manager->register_layer_attributes(matpass.gpumat);
189
190 const bool is_transparent = GPU_material_flag_get(matpass.gpumat, GPU_MATFLAG_TRANSPARENT) ||
191 geometry_type == MAT_GEOM_GSPLAT;
192
193 bool pass_updated = GPU_material_compilation_timestamp(matpass.gpumat) > gpu_pass_last_update_;
194
195 if (inst_.is_viewport() && use_deferred_compilation && pass_updated) {
196 inst_.sampling.reset();
197
198 const bool has_displacement = GPU_material_has_displacement_output(matpass.gpumat) &&
199 (blender_mat->displacement_method != MA_DISPLACEMENT_BUMP);
200 const bool has_volume = GPU_material_has_volume_output(matpass.gpumat);
201
202 if (((pipeline_type == MAT_PIPE_SHADOW) && (is_transparent || has_displacement)) || has_volume)
203 {
204 /* WORKAROUND: This is to avoid lingering shadows from default material.
205 * Ideally, we should tag the caster object to update only the needed areas but that's a bit
206 * more involved. */
207 inst_.shadows.reset();
208 }
209 }
210
211 if (is_volume || (is_forward && is_transparent)) {
212 /* Sub pass is generated later. */
213 matpass.sub_pass = nullptr;
214 }
215 else {
216 const bool hide_from_raycast = ob->visibility_flag & OB_HIDE_RAYCAST;
217 ShaderKey shader_key(matpass.gpumat, blender_mat, probe_capture, hide_from_raycast);
218
219 PassMain::Sub *shader_sub = shader_map_.lookup_or_add_cb(shader_key, [&]() {
220 /* First time encountering this shader. Create a sub that will contain materials using it. */
221 return inst_.pipelines.material_add(
222 ob, blender_mat, matpass.gpumat, pipeline_type, probe_capture);
223 });
224
225 if (shader_sub != nullptr) {
226 /* Create a sub for this material as `shader_sub` is for sharing shader between materials. */
227 matpass.sub_pass = &shader_sub->sub(GPU_material_get_name(matpass.gpumat));
228 matpass.sub_pass->material_set(
229 *inst_.manager, matpass.gpumat, true, inst_.anisotropic_filtering);
230 }
231 else {
232 matpass.sub_pass = nullptr;
233 }
234 }
235
236 return matpass;
237}
238
239Material &MaterialModule::material_sync(const ObjectHandle &ob_handle,
240 blender::Material *blender_mat,
241 eMaterialGeometry geometry_type,
242 bool has_motion)
243{
244 Object *ob = ob_handle.object;
245 bool hide_on_camera = ob->visibility_flag & OB_HIDE_CAMERA;
246
247 if (geometry_type == MAT_GEOM_VOLUME) {
248 MaterialKey material_key(
249 blender_mat, geometry_type, MAT_PIPE_VOLUME_MATERIAL, ob->visibility_flag);
250 Material &mat = material_map_.lookup_or_add_cb(material_key, [&]() {
251 Material mat = {};
252 mat.volume_occupancy = material_pass_get(
254 mat.volume_material = material_pass_get(
255 ob, blender_mat, MAT_PIPE_VOLUME_MATERIAL, MAT_GEOM_VOLUME);
257 return mat;
258 });
259 return mat;
260 }
261
262 const bool use_forward_pipeline = (blender_mat->surface_render_method ==
264 eMaterialPipeline surface_pipe, prepass_pipe;
265 if (use_forward_pipeline) {
266 surface_pipe = MAT_PIPE_FORWARD;
268 }
269 else {
270 surface_pipe = MAT_PIPE_DEFERRED;
272 }
273
282 MaterialKey material_key(blender_mat, geometry_type, prepass_pipe, ob->visibility_flag);
283
284 Material &mat = material_map_.lookup_or_add_cb(material_key, [&]() {
285 Material mat;
286 if (inst_.is_baking()) {
287 if (!(ob->visibility_flag & OB_HIDE_PROBE_VOLUME)) {
288 mat.capture = material_pass_get(ob, blender_mat, MAT_PIPE_CAPTURE, geometry_type);
289 }
290 /* TODO(fclem): Still need the shading pass for correct attribute extraction. Would be better
291 * to avoid this shader compilation in another context. */
292 mat.shading = material_pass_get(ob, blender_mat, surface_pipe, geometry_type);
293 mat.has_volume = false; /* TODO */
295 }
296 else {
297 if (!hide_on_camera) {
298 mat.prepass = material_pass_get(ob, blender_mat, prepass_pipe, geometry_type);
299 }
300
301 mat.shading = material_pass_get(ob, blender_mat, surface_pipe, geometry_type);
302 if (hide_on_camera) {
303 /* Only null the sub_pass.
304 * `mat.shading.gpumat` is always needed for using the GPU_material API. */
305 mat.shading.sub_pass = nullptr;
306 }
307
308 if (inst_.needs_lightprobe_sphere_passes() && !(ob->visibility_flag & OB_HIDE_PROBE_CUBEMAP))
309 {
310 mat.lightprobe_sphere_prepass = material_pass_get(
311 ob, blender_mat, MAT_PIPE_PREPASS_DEFERRED, geometry_type, MAT_PROBE_REFLECTION);
312 mat.lightprobe_sphere_shading = material_pass_get(
313 ob, blender_mat, MAT_PIPE_DEFERRED, geometry_type, MAT_PROBE_REFLECTION);
314 }
315
316 if (inst_.needs_planar_probe_passes() && !(ob->visibility_flag & OB_HIDE_PROBE_PLANAR)) {
317 mat.planar_probe_prepass = material_pass_get(
318 ob, blender_mat, MAT_PIPE_PREPASS_PLANAR, geometry_type, MAT_PROBE_PLANAR);
319 mat.planar_probe_shading = material_pass_get(
320 ob, blender_mat, MAT_PIPE_DEFERRED, geometry_type, MAT_PROBE_PLANAR);
321 }
322
325 if (mat.has_volume && !hide_on_camera) {
326 mat.volume_occupancy = material_pass_get(
327 ob, blender_mat, MAT_PIPE_VOLUME_OCCUPANCY, geometry_type);
328 mat.volume_material = material_pass_get(
329 ob, blender_mat, MAT_PIPE_VOLUME_MATERIAL, geometry_type);
330 }
331 }
332
333 if (!(ob->visibility_flag & OB_HIDE_SHADOW)) {
334 mat.shadow = material_pass_get(ob, blender_mat, MAT_PIPE_SHADOW, geometry_type);
335 }
336
337 mat.is_alpha_blend_transparent = use_forward_pipeline &&
340 mat.has_transparent_shadows = blender_mat->blend_flag & MA_BL_TRANSPARENT_SHADOW &&
343
344 return mat;
345 });
346
347 return mat;
348}
349
350blender::Material *MaterialModule::material_from_slot(Object *ob, int slot)
351{
353 if (ma == nullptr) {
354 if (ob->type == OB_VOLUME) {
356 }
357 if (ob->type == OB_POINTCLOUD) {
359 if (pointcloud.type == PointCloudType::GSplat) {
361 }
362 }
364 }
365 return ma;
366}
367
369{
370 Object *ob = ob_handle.object;
371
372 material_array_.materials.clear();
373 material_array_.gpu_materials.clear();
374
375 const int materials_len = BKE_object_material_used_with_fallback_eval(*ob);
376
377 for (auto i : IndexRange(materials_len)) {
379 material_from_slot(ob, i);
380 Material &mat = material_sync(ob_handle, blender_mat, to_material_geometry(ob), has_motion);
381
382 /* \note Perform a whole copy since next material_sync() can move the Material memory location
383 * (i.e: because of its container growing) */
384 material_array_.materials.append(mat);
385 material_array_.gpu_materials.append(mat.shading.gpumat);
386 }
387 return material_array_;
388}
389
391 bool has_motion,
392 int mat_nr,
393 eMaterialGeometry geometry_type)
394{
395 blender::Material *blender_mat = (material_override) ?
397 material_from_slot(ob_handle.object, mat_nr);
398
399 return material_sync(ob_handle, blender_mat, geometry_type, has_motion);
400}
401
402ShaderGroups MaterialModule::default_materials_load(bool block_until_ready)
403{
404 bool shaders_are_ready = true;
405 auto request_shader =
406 [&](blender::Material *mat, eMaterialPipeline pipeline, eMaterialGeometry geom) {
407 GPUMaterial *gpu_mat = inst_.shaders.material_shader_get(
408 mat, mat->nodetree, pipeline, geom, !block_until_ready, nullptr);
409 shaders_are_ready = shaders_are_ready && GPU_material_status(gpu_mat) == GPU_MAT_SUCCESS;
410 };
411
416
417 return shaders_are_ready ? DEFAULT_MATERIALS : NONE;
418}
419
421
422} // namespace blender::eevee
General operations, lookup, etc. for materials.
#define SH_NODE_EMISSION
#define SH_NODE_BSDF_DIFFUSE
#define SH_NODE_OUTPUT_MATERIAL
#define SH_NODE_BSDF_GLOSSY
Platform independent time functions.
#define FRA2TIME(a)
#define BLI_assert(a)
#define ELEM(...)
#define output
constexpr IndexRange()=default
void material_set(Manager &manager, GPUMaterial *material, bool deferred_texture_loading=false, GPUSamplerFiltering anisotropic_filtering=GPU_SAMPLER_FILTERING_DEFAULT)
PassBase< DrawCommandBufType > & sub(const char *name)
Definition draw_pass.hh:701
detail::PassBase< command::DrawMultiBuf > Sub
Definition draw_pass.hh:509
A running instance of the engine.
Material material_get(const ObjectHandle &ob_handle, bool has_motion, int mat_nr, eMaterialGeometry geometry_type)
MaterialArray & material_array_get(const ObjectHandle &ob_handle, bool has_motion)
blender::Material * material_override
blender::Material * default_volume
blender::Material * default_surface
GPUMaterial * material_shader_get(blender::Material *blender_mat, bNodeTree *nodetree, eMaterialPipeline pipeline_type, eMaterialGeometry geometry_type, bool deferred_compilation, blender::Material *default_mat)
bNode * node_add_static_node(const bContext *C, bNodeTree &ntree, int type)
Definition node.cc:4283
bNodeLink & node_add_link(bNodeTree &ntree, bNode &fromnode, bNodeSocket &fromsock, bNode &tonode, bNodeSocket &tosock)
Definition node.cc:4638
bNodeSocket * node_find_socket(bNode &node, eNodeSocketInOut in_out, UString identifier)
Definition node.cc:3036
void node_set_active(bNodeTree &ntree, bNode &node)
Definition node.cc:5484
unsigned __int64 uint64_t
Definition BKE_text.h:91
int BKE_object_material_used_with_fallback_eval(const Object &ob)
bool GPU_material_has_volume_output(GPUMaterial *mat)
bool GPU_material_has_displacement_output(GPUMaterial *mat)
bool assign_if_different(T &old_value, T &&new_value)
Material * BKE_material_default_surface()
@ GPU_MATFLAG_TRANSPARENT
@ GPU_MAT_SUCCESS
bool GPU_material_flag_get(const GPUMaterial *mat, eGPUMaterialFlag flag)
Material * BKE_material_default_volume()
@ LIB_ID_COPY_LOCALIZE
T & DRW_object_get_data_for_drawing(const Object &object)
float BKE_scene_frame_get(const Scene *scene)
Definition scene.cc:2595
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
bool GPU_material_has_surface_output(GPUMaterial *mat)
void BKE_id_free(Main *bmain, void *idv)
void BKE_ntree_update_without_main(bNodeTree &tree)
void * BKE_id_new_nomain(short type, const char *name)
Definition lib_id.cc:1617
MINLINE void copy_v3_fl3(float v[3], float x, float y, float z)
MINLINE void copy_v3_fl(float r[3], float f)
Material * BKE_object_material_get_eval(Object *ob, short act)
uint64_t GPU_material_compilation_timestamp(GPUMaterial *mat)
uint64_t GPU_pass_global_compilation_count()
Definition gpu_pass.cc:195
@ GPU_MAT_OPTIMIZATION_QUEUED
ID * BKE_id_copy_ex(Main *bmain, const ID *id, ID **new_id_p, int flag)
Definition lib_id.cc:851
Material * BKE_material_default_gsplat()
eGPUMaterialOptimizationStatus GPU_material_optimization_status(GPUMaterial *mat)
const char * GPU_material_get_name(GPUMaterial *material)
GPUMaterialStatus GPU_material_status(GPUMaterial *mat)
bool is_alpha_blend_transparent
MaterialPass volume_material
MaterialPass planar_probe_prepass
MaterialPass prepass
bool has_transparent_shadows
MaterialPass shadow
MaterialPass planar_probe_shading
MaterialPass volume_occupancy
MaterialPass capture
MaterialPass lightprobe_sphere_shading
MaterialPass shading
MaterialPass lightprobe_sphere_prepass
const NodeType * type
Definition graph/node.h:179
eObject_VisibilityFlag visibility_flag