Blender V5.3
eevee_material.hh
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#pragma once
10
11#include "DNA_material_types.h"
13
14#include "DRW_render.hh"
15
16#include "BLI_enum_flags.hh"
17#include "BLI_map.hh"
18#include "BLI_vector.hh"
19
20#include "GPU_material.hh"
21
22#include "draw_pass.hh"
23
25#include "eevee_shader.hh"
26#include "eevee_sync.hh"
27
28namespace blender {
29
32
33namespace eevee {
34
35class Instance;
36
37/* -------------------------------------------------------------------- */
41
42static inline bool geometry_type_has_surface(eMaterialGeometry geometry_type)
43{
44 return geometry_type < MAT_GEOM_VOLUME;
45}
46
51
52static inline eMaterialDisplacement to_displacement_type(int displacement_method)
53{
54 switch (displacement_method) {
56 /* Currently unsupported. Revert to vertex displacement + bump. */
60 default:
62 }
63}
64
66 /* These maps directly to thickness mode. */
69};
70
71static inline eMaterialThickness to_thickness_type(int thickness_mode)
72{
73 switch (thickness_mode) {
75 return MAT_THICKNESS_SLAB;
76 default:
78 }
79}
80
86
87static inline void material_type_from_shader_uuid(uint64_t shader_uuid,
88 eMaterialPipeline &pipeline_type,
89 eMaterialGeometry &geometry_type,
90 eMaterialDisplacement &displacement_type,
91 eMaterialThickness &thickness_type,
92 bool &transparent_shadows)
93{
94 const uint64_t geometry_mask = ((1u << 4u) - 1u);
95 const uint64_t pipeline_mask = ((1u << 4u) - 1u);
96 const uint64_t thickness_mask = ((1u << 1u) - 1u);
97 const uint64_t displacement_mask = ((1u << 1u) - 1u);
98 geometry_type = static_cast<eMaterialGeometry>(shader_uuid & geometry_mask);
99 pipeline_type = static_cast<eMaterialPipeline>((shader_uuid >> 4u) & pipeline_mask);
100 displacement_type = static_cast<eMaterialDisplacement>((shader_uuid >> 8u) & displacement_mask);
101 thickness_type = static_cast<eMaterialThickness>((shader_uuid >> 9u) & thickness_mask);
102 transparent_shadows = (shader_uuid >> 10u) & 1u;
103}
104
106 eMaterialPipeline pipeline_type,
107 eMaterialGeometry geometry_type,
110 char blend_flags = 0)
111{
112 BLI_assert(int64_t(displacement_type) < (1 << 1));
113 BLI_assert(int64_t(thickness_type) < (1 << 1));
114 BLI_assert(int64_t(geometry_type) < (1 << 4));
115 BLI_assert(int64_t(pipeline_type) < (1 << 4));
116 uint64_t transparent_shadows = blend_flags & MA_BL_TRANSPARENT_SHADOW ? 1 : 0;
117
118 uint64_t uuid;
119 uuid = geometry_type;
120 uuid |= pipeline_type << 4;
121 uuid |= displacement_type << 8;
122 uuid |= thickness_type << 9;
123 uuid |= transparent_shadows << 10;
124 return uuid;
125}
126
145
147{
148 eClosureBits closure_bits = eClosureBits(0);
150 closure_bits |= CLOSURE_DIFFUSE;
151 }
153 closure_bits |= CLOSURE_TRANSPARENCY;
154 }
156 closure_bits |= CLOSURE_TRANSLUCENT;
157 }
159 closure_bits |= CLOSURE_EMISSION;
160 }
162 closure_bits |= CLOSURE_REFLECTION;
163 }
165 closure_bits |= CLOSURE_CLEARCOAT;
166 }
168 closure_bits |= CLOSURE_SSS;
169 }
171 closure_bits |= CLOSURE_REFRACTION;
172 }
174 closure_bits |= CLOSURE_HOLDOUT;
175 }
177 closure_bits |= CLOSURE_AMBIENT_OCCLUSION;
178 }
180 closure_bits |= CLOSURE_SHADER_TO_RGBA;
181 }
182 return closure_bits;
183}
184
185/* Count the number of closure bins required for the given combination of closure types. */
186static inline int to_gbuffer_bin_count(const eClosureBits closure_bits)
187{
188 int closure_data_slots = 0;
189 if (closure_bits & CLOSURE_DIFFUSE) {
190 if ((closure_bits & CLOSURE_TRANSLUCENT) && !(closure_bits & CLOSURE_CLEARCOAT)) {
191 /* Special case to allow translucent with diffuse without noise.
192 * Revert back to noise if clear coat is present. */
193 closure_data_slots |= (1 << 2);
194 }
195 else {
196 closure_data_slots |= (1 << 0);
197 }
198 }
199 if (closure_bits & CLOSURE_SSS) {
200 closure_data_slots |= (1 << 0);
201 }
202 if (closure_bits & CLOSURE_REFRACTION) {
203 closure_data_slots |= (1 << 0);
204 }
205 if (closure_bits & CLOSURE_TRANSLUCENT) {
206 closure_data_slots |= (1 << 0);
207 }
208 if (closure_bits & CLOSURE_REFLECTION) {
209 closure_data_slots |= (1 << 1);
210 }
211 if (closure_bits & CLOSURE_CLEARCOAT) {
212 closure_data_slots |= (1 << 2);
213 }
214 return count_bits_i(closure_data_slots);
215};
216
223
225{
226 switch (ob->type) {
227 case OB_CURVES:
228 return MAT_GEOM_CURVES;
229 case OB_VOLUME:
230 return MAT_GEOM_VOLUME;
231 case OB_POINTCLOUD:
232 /* Special case: GSplats have their own shader under the same object type for now. */
234 default:
235 return MAT_GEOM_MESH;
236 }
237}
238
246
249 eMaterialPipeline pipeline,
250 short visibility_flags)
251 : mat(mat_)
252 {
254 geometry,
255 to_displacement_type(mat_->displacement_method),
256 to_thickness_type(mat_->thickness_mode),
257 mat_->blend_flag);
258 options = (options << 1) | (visibility_flags & OB_HIDE_CAMERA ? 0 : 1);
259 options = (options << 1) | (visibility_flags & OB_HIDE_SHADOW ? 0 : 1);
260 options = (options << 1) | (visibility_flags & OB_HIDE_PROBE_CUBEMAP ? 0 : 1);
261 options = (options << 1) | (visibility_flags & OB_HIDE_PROBE_PLANAR ? 0 : 1);
262 options = (options << 1) | (visibility_flags & OB_HIDE_RAYCAST ? 0 : 1);
263 }
264
266 {
267 return uint64_t(mat) + options;
268 }
269
270 bool operator==(const MaterialKey &k) const
271 {
272 return (mat == k.mat) && (options == k.options);
273 }
274};
275
277
278/* -------------------------------------------------------------------- */
282
289struct ShaderKey {
292
294 blender::Material *blender_mat,
295 eMaterialProbe probe_capture,
296 bool hide_from_raycast)
297 {
300 options = (options << 8) | blender_mat->blend_flag;
301 options = (options << 2) | uint64_t(probe_capture);
302 options = (options << 1) | (hide_from_raycast ? 1 : 0);
303 }
304
306 {
307 return uint64_t(shader) + options;
308 }
309
310 bool operator==(const ShaderKey &k) const
311 {
312 return (shader == k.shader) && (options == k.options);
313 }
314};
315
317
318/* -------------------------------------------------------------------- */
322
324 GPUMaterial *gpumat = nullptr;
326};
327
328struct Material {
341 /* These pipelines need a sub-pass per object/instance, so the returned sub_pass for these are
342 * always null and the sub-pass creation is handled directly by the SyncModule.
343 * Note that this also applies to the shading MaterialPass in the case of alpha-blended
344 * materials. */
348};
349
354
356 public:
361
363
367
369 float material_frame = 0;
370 float material_time = 0;
371
372 private:
373 Instance &inst_;
374
375 Map<MaterialKey, Material> material_map_;
377
378 MaterialArray material_array_;
379
380 blender::Material *error_mat_;
381
382 uint64_t gpu_pass_last_update_ = 0;
383 uint64_t gpu_pass_next_update_ = 0;
384
385 public:
388
389 void begin_sync();
390
394 MaterialArray &material_array_get(const ObjectHandle &ob_handle, bool has_motion);
399 Material material_get(const ObjectHandle &ob_handle,
400 bool has_motion,
401 int mat_nr,
402 eMaterialGeometry geometry_type);
403
404 /* Request default materials and return DEFAULT_MATERIALS if they are compiled. */
406 {
407 return default_materials_load(false);
408 }
410 {
411 return default_materials_load(true);
412 }
413
414 private:
415 Material &material_sync(const ObjectHandle &ob_handle,
416 blender::Material *blender_mat,
417 eMaterialGeometry geometry_type,
418 bool has_motion);
419
421 blender::Material *material_from_slot(Object *ob, int slot);
422 MaterialPass material_pass_get(Object *ob,
423 blender::Material *blender_mat,
424 eMaterialPipeline pipeline_type,
425 eMaterialGeometry geometry_type,
426 eMaterialProbe probe_capture = MAT_PROBE_NONE);
427
428 ShaderGroups default_materials_load(bool block_until_ready = false);
429};
430
432
433} // namespace eevee
434} // namespace blender
#define ATTR_FALLTHROUGH
detail::PassBase< command::DrawMultiBuf > Sub
Definition draw_pass.hh:509
A running instance of the engine.
ShaderGroups default_materials_load_async()
Material material_get(const ObjectHandle &ob_handle, bool has_motion, int mat_nr, eMaterialGeometry geometry_type)
ShaderGroups default_materials_wait_ready()
MaterialArray & material_array_get(const ObjectHandle &ob_handle, bool has_motion)
blender::Material * material_override
blender::Material * default_volume
blender::Material * default_surface
ENUM_OPERATORS(Layer::Flags)
static eMaterialDisplacement to_displacement_type(int displacement_method)
static bool geometry_type_has_surface(eMaterialGeometry geometry_type)
static eMaterialGeometry to_material_geometry(const Object *ob)
static eClosureBits shader_closure_bits_from_flag(const GPUMaterial *gpumat)
static void material_type_from_shader_uuid(uint64_t shader_uuid, eMaterialPipeline &pipeline_type, eMaterialGeometry &geometry_type, eMaterialDisplacement &displacement_type, eMaterialThickness &thickness_type, bool &transparent_shadows)
static uint64_t shader_uuid_from_material_type(eMaterialPipeline pipeline_type, eMaterialGeometry geometry_type, eMaterialDisplacement displacement_type=MAT_DISPLACEMENT_BUMP, eMaterialThickness thickness_type=MAT_THICKNESS_SPHERE, char blend_flags=0)
static int to_gbuffer_bin_count(const eClosureBits closure_bits)
static eMaterialGeometry to_pointcloud_material_geometry(const Object *ob)
static eMaterialThickness to_thickness_type(int thickness_mode)
@ MAT_DISPLACEMENT_VERTEX_WITH_BUMP
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
Definition BKE_text.h:91
__int64 int64_t
Definition BKE_text.h:90
MINLINE int count_bits_i(unsigned int n)
@ GPU_MATFLAG_SHADER_TO_RGBA
@ GPU_MATFLAG_COAT
@ GPU_MATFLAG_HOLDOUT
@ GPU_MATFLAG_SUBSURFACE
@ GPU_MATFLAG_TRANSLUCENT
@ GPU_MATFLAG_GLOSSY
@ GPU_MATFLAG_REFRACT
@ GPU_MATFLAG_EMISSION
@ GPU_MATFLAG_DIFFUSE
@ GPU_MATFLAG_TRANSPARENT
gpu::Shader * GPU_material_get_shader(GPUMaterial *material)
bool GPU_material_flag_get(const GPUMaterial *mat, eGPUMaterialFlag flag)
unsigned int uint32_t
Definition BKE_text.h:81
T & DRW_object_get_data_for_drawing(const Object &object)
Vector< GPUMaterial * > gpu_materials
bool operator==(const MaterialKey &k) const
MaterialKey(blender::Material *mat_, eMaterialGeometry geometry, eMaterialPipeline pipeline, short visibility_flags)
MaterialPass lightprobe_sphere_shading
MaterialPass lightprobe_sphere_prepass
ShaderKey(GPUMaterial *gpumat, blender::Material *blender_mat, eMaterialProbe probe_capture, bool hide_from_raycast)
bool operator==(const ShaderKey &k) const