Blender V4.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
9#pragma once
10
11#include "DRW_render.hh"
12
13#include "BLI_map.hh"
14#include "BLI_vector.hh"
15#include "GPU_material.hh"
16
17#include "eevee_sync.hh"
18
19namespace blender::eevee {
20
21class Instance;
22
23/* -------------------------------------------------------------------- */
44
46 /* These maps directly to object types. */
52
53 /* These maps to special shader. */
55};
56
57static inline bool geometry_type_has_surface(eMaterialGeometry geometry_type)
58{
59 return geometry_type < MAT_GEOM_VOLUME;
60}
61
66
67static inline eMaterialDisplacement to_displacement_type(int displacement_method)
68{
69 switch (displacement_method) {
71 /* Currently unsupported. Revert to vertex displacement + bump. */
75 default:
77 }
78}
79
81 /* These maps directly to thickness mode. */
84};
85
86static inline eMaterialThickness to_thickness_type(int thickness_mode)
87{
88 switch (thickness_mode) {
90 return MAT_THICKNESS_SLAB;
91 default:
93 }
94}
95
101
102static inline void material_type_from_shader_uuid(uint64_t shader_uuid,
103 eMaterialPipeline &pipeline_type,
104 eMaterialGeometry &geometry_type,
105 eMaterialDisplacement &displacement_type,
106 eMaterialThickness &thickness_type,
107 bool &transparent_shadows)
108{
109 const uint64_t geometry_mask = ((1u << 4u) - 1u);
110 const uint64_t pipeline_mask = ((1u << 4u) - 1u);
111 const uint64_t thickness_mask = ((1u << 1u) - 1u);
112 const uint64_t displacement_mask = ((1u << 1u) - 1u);
113 geometry_type = static_cast<eMaterialGeometry>(shader_uuid & geometry_mask);
114 pipeline_type = static_cast<eMaterialPipeline>((shader_uuid >> 4u) & pipeline_mask);
115 displacement_type = static_cast<eMaterialDisplacement>((shader_uuid >> 8u) & displacement_mask);
116 thickness_type = static_cast<eMaterialThickness>((shader_uuid >> 9u) & thickness_mask);
117 transparent_shadows = (shader_uuid >> 10u) & 1u;
118}
119
121 eMaterialPipeline pipeline_type,
122 eMaterialGeometry geometry_type,
125 char blend_flags = 0)
126{
127 BLI_assert(displacement_type < (1 << 1));
128 BLI_assert(thickness_type < (1 << 1));
129 BLI_assert(geometry_type < (1 << 4));
130 BLI_assert(pipeline_type < (1 << 4));
131 uint64_t transparent_shadows = blend_flags & MA_BL_TRANSPARENT_SHADOW ? 1 : 0;
132
133 uint64_t uuid;
134 uuid = geometry_type;
135 uuid |= pipeline_type << 4;
136 uuid |= displacement_type << 8;
137 uuid |= thickness_type << 9;
138 uuid |= transparent_shadows << 10;
139 return uuid;
140}
141
143
145{
146 eClosureBits closure_bits = eClosureBits(0);
148 closure_bits |= CLOSURE_DIFFUSE;
149 }
151 closure_bits |= CLOSURE_TRANSPARENCY;
152 }
154 closure_bits |= CLOSURE_TRANSLUCENT;
155 }
157 closure_bits |= CLOSURE_EMISSION;
158 }
160 closure_bits |= CLOSURE_REFLECTION;
161 }
163 closure_bits |= CLOSURE_CLEARCOAT;
164 }
166 closure_bits |= CLOSURE_SSS;
167 }
169 closure_bits |= CLOSURE_REFRACTION;
170 }
172 closure_bits |= CLOSURE_HOLDOUT;
173 }
175 closure_bits |= CLOSURE_AMBIENT_OCCLUSION;
176 }
178 closure_bits |= CLOSURE_SHADER_TO_RGBA;
179 }
180 return closure_bits;
181}
182
184{
185 switch (ob->type) {
186 case OB_CURVES:
187 return MAT_GEOM_CURVES;
188 case OB_VOLUME:
189 return MAT_GEOM_VOLUME;
191 return MAT_GEOM_GPENCIL;
192 case OB_POINTCLOUD:
194 default:
195 return MAT_GEOM_MESH;
196 }
197}
198
206
208 eMaterialGeometry geometry,
209 eMaterialPipeline pipeline,
210 short visibility_flags)
211 : mat(mat_)
212 {
214 geometry,
217 mat_->blend_flag);
218 options = (options << 1) | (visibility_flags & OB_HIDE_CAMERA ? 0 : 1);
219 options = (options << 1) | (visibility_flags & OB_HIDE_SHADOW ? 0 : 1);
220 options = (options << 1) | (visibility_flags & OB_HIDE_PROBE_CUBEMAP ? 0 : 1);
221 options = (options << 1) | (visibility_flags & OB_HIDE_PROBE_PLANAR ? 0 : 1);
222 }
223
225 {
226 return uint64_t(mat) + options;
227 }
228
229 bool operator<(const MaterialKey &k) const
230 {
231 if (mat == k.mat) {
232 return options < k.options;
233 }
234 return mat < k.mat;
235 }
236
237 bool operator==(const MaterialKey &k) const
238 {
239 return (mat == k.mat) && (options == k.options);
240 }
241};
242
245/* -------------------------------------------------------------------- */
256struct ShaderKey {
259
260 ShaderKey(GPUMaterial *gpumat, ::Material *blender_mat, eMaterialProbe probe_capture)
261 {
262 shader = GPU_material_get_shader(gpumat);
264 options = (options << 8) | blender_mat->blend_flag;
265 options = (options << 2) | uint64_t(probe_capture);
266 }
267
269 {
270 return uint64_t(shader) + options;
271 }
272
273 bool operator<(const ShaderKey &k) const
274 {
275 return (shader == k.shader) ? (options < k.options) : (shader < k.shader);
276 }
277
278 bool operator==(const ShaderKey &k) const
279 {
280 return (shader == k.shader) && (options == k.options);
281 }
282};
283
286/* -------------------------------------------------------------------- */
296 private:
297 bNodeTree *ntree_;
298 bNodeSocketValueRGBA *color_socket_;
299 bNodeSocketValueFloat *metallic_socket_;
300 bNodeSocketValueFloat *roughness_socket_;
301 bNodeSocketValueFloat *specular_socket_;
302
303 public:
306
309};
310
313/* -------------------------------------------------------------------- */
322
340
345
347 public:
350
353
354 private:
355 Instance &inst_;
356
357 Map<MaterialKey, Material> material_map_;
359
360 MaterialArray material_array_;
361
362 DefaultSurfaceNodeTree default_surface_ntree_;
363
364 ::Material *error_mat_;
365
366 public:
369
370 void begin_sync();
371
375 MaterialArray &material_array_get(Object *ob, bool has_motion);
380 Material &material_get(Object *ob, bool has_motion, int mat_nr, eMaterialGeometry geometry_type);
381
382 private:
383 Material &material_sync(Object *ob,
384 ::Material *blender_mat,
385 eMaterialGeometry geometry_type,
386 bool has_motion);
387
389 ::Material *material_from_slot(Object *ob, int slot);
390 MaterialPass material_pass_get(Object *ob,
391 ::Material *blender_mat,
392 eMaterialPipeline pipeline_type,
393 eMaterialGeometry geometry_type,
394 eMaterialProbe probe_capture = MAT_PROBE_NONE);
395};
396
399} // namespace blender::eevee
#define BLI_assert(a)
Definition BLI_assert.h:50
#define ATTR_FALLTHROUGH
#define ENUM_OPERATORS(_type, _max)
@ MA_BL_TRANSPARENT_SHADOW
@ MA_DISPLACEMENT_BOTH
@ MA_DISPLACEMENT_DISPLACE
@ MA_THICKNESS_SLAB
@ OB_HIDE_CAMERA
@ OB_HIDE_PROBE_PLANAR
@ OB_HIDE_PROBE_CUBEMAP
@ OB_HIDE_SHADOW
@ OB_POINTCLOUD
@ OB_VOLUME
@ OB_GPENCIL_LEGACY
@ OB_CURVES
GPUShader * GPU_material_get_shader(GPUMaterial *material)
bool GPU_material_flag_get(const GPUMaterial *mat, eGPUMaterialFlag flag)
@ GPU_MATFLAG_SHADER_TO_RGBA
@ GPU_MATFLAG_EMISSION
@ GPU_MATFLAG_GLOSSY
@ GPU_MATFLAG_COAT
@ GPU_MATFLAG_AO
@ GPU_MATFLAG_REFRACT
@ GPU_MATFLAG_TRANSLUCENT
@ GPU_MATFLAG_HOLDOUT
@ GPU_MATFLAG_DIFFUSE
@ GPU_MATFLAG_TRANSPARENT
@ GPU_MATFLAG_SUBSURFACE
struct GPUShader GPUShader
bNodeTree * nodetree_get(::Material *ma)
A running instance of the engine.
Material & material_get(Object *ob, bool has_motion, int mat_nr, eMaterialGeometry geometry_type)
MaterialArray & material_array_get(Object *ob, bool has_motion)
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 eMaterialThickness to_thickness_type(int thickness_mode)
@ MAT_DISPLACEMENT_VERTEX_WITH_BUMP
@ MAT_PIPE_PREPASS_FORWARD_VELOCITY
@ MAT_PIPE_PREPASS_DEFERRED_VELOCITY
__int64 int64_t
Definition stdint.h:89
unsigned __int64 uint64_t
Definition stdint.h:90
Vector< GPUMaterial * > gpu_materials
bool operator<(const MaterialKey &k) const
MaterialKey(::Material *mat_, eMaterialGeometry geometry, eMaterialPipeline pipeline, short visibility_flags)
bool operator==(const MaterialKey &k) const
MaterialPass lightprobe_sphere_shading
MaterialPass lightprobe_sphere_prepass
ShaderKey(GPUMaterial *gpumat, ::Material *blender_mat, eMaterialProbe probe_capture)
bool operator<(const ShaderKey &k) const
bool operator==(const ShaderKey &k) const