Blender  V3.3
eevee_material.hh
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2021 Blender Foundation.
3  */
4 
9 #pragma once
10 
11 #include "DRW_render.h"
12 
13 #include "BLI_map.hh"
14 #include "BLI_vector.hh"
15 #include "GPU_material.h"
16 
17 #include "eevee_sync.hh"
18 
19 namespace blender::eevee {
20 
21 class Instance;
22 
23 /* -------------------------------------------------------------------- */
37 };
38 
45 };
46 
47 static inline void material_type_from_shader_uuid(uint64_t shader_uuid,
48  eMaterialPipeline &pipeline_type,
49  eMaterialGeometry &geometry_type)
50 {
51  const uint64_t geometry_mask = ((1u << 3u) - 1u);
52  const uint64_t pipeline_mask = ((1u << 3u) - 1u);
53  geometry_type = static_cast<eMaterialGeometry>(shader_uuid & geometry_mask);
54  pipeline_type = static_cast<eMaterialPipeline>((shader_uuid >> 3u) & pipeline_mask);
55 }
56 
58  eMaterialGeometry geometry_type)
59 {
60  return geometry_type | (pipeline_type << 3);
61 }
62 
63 ENUM_OPERATORS(eClosureBits, CLOSURE_AMBIENT_OCCLUSION)
64 
65 static inline eClosureBits shader_closure_bits_from_flag(const GPUMaterial *gpumat)
66 {
67  eClosureBits closure_bits = eClosureBits(0);
69  closure_bits |= CLOSURE_DIFFUSE;
70  }
72  closure_bits |= CLOSURE_TRANSPARENCY;
73  }
75  closure_bits |= CLOSURE_EMISSION;
76  }
78  closure_bits |= CLOSURE_REFLECTION;
79  }
81  closure_bits |= CLOSURE_SSS;
82  }
84  closure_bits |= CLOSURE_REFRACTION;
85  }
87  closure_bits |= CLOSURE_HOLDOUT;
88  }
90  closure_bits |= CLOSURE_AMBIENT_OCCLUSION;
91  }
92  return closure_bits;
93 }
94 
96 {
97  switch (ob->type) {
98  case OB_CURVES:
99  return MAT_GEOM_CURVES;
100  case OB_VOLUME:
101  return MAT_GEOM_VOLUME;
102  case OB_GPENCIL:
103  return MAT_GEOM_GPENCIL;
104  default:
105  return MAT_GEOM_MESH;
106  }
107 }
108 
110 struct MaterialKey {
113 
114  MaterialKey(::Material *mat_, eMaterialGeometry geometry, eMaterialPipeline surface_pipeline)
115  : mat(mat_)
116  {
117  options = shader_uuid_from_material_type(surface_pipeline, geometry);
118  }
119 
120  uint64_t hash() const
121  {
122  BLI_assert(options < sizeof(*mat));
123  return (uint64_t)mat + options;
124  }
125 
126  bool operator<(const MaterialKey &k) const
127  {
128  return (mat < k.mat) || (options < k.options);
129  }
130 
131  bool operator==(const MaterialKey &k) const
132  {
133  return (mat == k.mat) && (options == k.options);
134  }
135 };
136 
139 /* -------------------------------------------------------------------- */
144 struct ShaderKey {
147 
149  {
151  options = shader_uuid_from_material_type(pipeline, geometry);
152  options = (options << 16u) | shader_closure_bits_from_flag(gpumat);
153  }
154 
155  uint64_t hash() const
156  {
157  return (uint64_t)shader + options;
158  }
159 
160  bool operator<(const ShaderKey &k) const
161  {
162  return (shader == k.shader) ? (options < k.options) : (shader < k.shader);
163  }
164 
165  bool operator==(const ShaderKey &k) const
166  {
167  return (shader == k.shader) && (options == k.options);
168  }
169 };
170 
173 /* -------------------------------------------------------------------- */
183  private:
184  bNodeTree *ntree_;
185  bNodeSocketValueRGBA *color_socket_;
186  bNodeSocketValueFloat *metallic_socket_;
187  bNodeSocketValueFloat *roughness_socket_;
188  bNodeSocketValueFloat *specular_socket_;
189 
190  public:
193 
196 };
197 
200 /* -------------------------------------------------------------------- */
205 struct MaterialPass {
206  GPUMaterial *gpumat = nullptr;
207  DRWShadingGroup *shgrp = nullptr;
208 };
209 
210 struct Material {
211  bool init = false;
214 };
215 
219 };
220 
222  public:
225 
227 
228  private:
229  Instance &inst_;
230 
231  Map<MaterialKey, Material *> material_map_;
233 
234  MaterialArray material_array_;
235 
236  DefaultSurfaceNodeTree default_surface_ntree_;
237 
238  ::Material *error_mat_;
239 
240  public:
241  MaterialModule(Instance &inst);
242  ~MaterialModule();
243 
244  void begin_sync();
245 
249  MaterialArray &material_array_get(Object *ob, bool has_motion);
254  Material &material_get(Object *ob, bool has_motion, int mat_nr, eMaterialGeometry geometry_type);
255 
256  private:
257  Material &material_sync(::Material *blender_mat,
258  eMaterialGeometry geometry_type,
259  bool has_motion);
260 
262  ::Material *material_from_slot(Object *ob, int slot);
263  MaterialPass material_pass_get(::Material *blender_mat,
264  eMaterialPipeline pipeline_type,
265  eMaterialGeometry geometry_type);
266 };
267 
270 } // namespace blender::eevee
#define BLI_assert(a)
Definition: BLI_assert.h:46
#define ENUM_OPERATORS(_type, _max)
@ OB_VOLUME
@ OB_CURVES
@ OB_GPENCIL
struct GPUShader * GPU_material_get_shader(GPUMaterial *material)
Definition: gpu_material.c:191
bool GPU_material_flag_get(const GPUMaterial *mat, eGPUMaterialFlag flag)
Definition: gpu_material.c:601
@ GPU_MATFLAG_EMISSION
Definition: GPU_material.h:75
@ GPU_MATFLAG_GLOSSY
Definition: GPU_material.h:73
@ GPU_MATFLAG_AO
Definition: GPU_material.h:79
@ GPU_MATFLAG_REFRACT
Definition: GPU_material.h:74
@ GPU_MATFLAG_HOLDOUT
Definition: GPU_material.h:77
@ GPU_MATFLAG_DIFFUSE
Definition: GPU_material.h:71
@ GPU_MATFLAG_TRANSPARENT
Definition: GPU_material.h:76
@ GPU_MATFLAG_SUBSURFACE
Definition: GPU_material.h:72
struct GPUShader GPUShader
Definition: GPU_shader.h:20
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 void material_type_from_shader_uuid(uint64_t shader_uuid, eMaterialPipeline &pipeline_type, eMaterialGeometry &geometry_type)
static uint64_t shader_uuid_from_material_type(eMaterialPipeline pipeline_type, eMaterialGeometry geometry_type)
static eMaterialGeometry to_material_geometry(const Object *ob)
static eClosureBits shader_closure_bits_from_flag(const GPUMaterial *gpumat)
@ MAT_PIPE_DEFERRED_PREPASS_VELOCITY
@ MAT_PIPE_FORWARD_PREPASS_VELOCITY
__int64 int64_t
Definition: stdint.h:89
unsigned __int64 uint64_t
Definition: stdint.h:90
Vector< Material * > materials
Vector< GPUMaterial * > gpu_materials
MaterialKey(::Material *mat_, eMaterialGeometry geometry, eMaterialPipeline surface_pipeline)
bool operator<(const MaterialKey &k) const
bool operator==(const MaterialKey &k) const
bool operator<(const ShaderKey &k) const
ShaderKey(GPUMaterial *gpumat, eMaterialGeometry geometry, eMaterialPipeline pipeline)
bool operator==(const ShaderKey &k) const