31#include <glm/vec4.hpp>
36#include <unordered_map>
37#include <unordered_set>
135 const auto param = [
this](
const std::string& n) ->
i32 {
136 if (n.empty())
return -1;
140 const auto texture = [
this](
const char* n) ->
i32 {
144 named.sceneColor = texture(
"u_sceneColor");
145 named.sceneDepth = texture(
"u_sceneDepthMap");
146 named.screenColor = texture(
"u_screenColor");
147 named.transmission = param(
"u_transmission");
148 named.blendDistance = param(
"u_blendDistance");
156 for (
const auto& p :
params) {
157 if (p.name ==
name)
return &p;
163 if (t.name ==
name)
return &t;
218 defaultWhite_ = white;
219 defaultBlack_ = black;
220 defaultFlatNormal_ = flatNormal;
221 defaultRipples_ = ripples;
229 defaultWhiteArray_ = white;
230 defaultBlackArray_ = black;
231 defaultFlatNormalArray_ = flatNormal;
238 return defaultWhiteArray_;
259 const bool mentionsGbuffer = source.find(
"ES_GBUFFER") != std::string::npos;
260 const bool writesAlbedo = mentionsGbuffer && source.find(
"fragAlbedo") != std::string::npos;
261 const bool takesLight = source.find(
"applyLighting") != std::string::npos;
262 const bool mentionsOpaque = source.find(
"ES_OPAQUE") != std::string::npos;
263 sources_[shader.
id()] = { std::move(source), std::move(features), mentionsGbuffer, writesAlbedo, takesLight, mentionsOpaque };
271 bool withNormals =
false,
bool skinned =
false,
272 bool envMapped =
false,
bool lightmapped =
false,
bool gbuffer =
false,
273 bool tangents =
false,
bool tipPass =
false,
bool opaque =
false)
const;
282 return gbuffer_programs_.count(programId) != 0;
286 return albedo_programs_.count(programId) != 0;
290 gbuffer_programs_.insert(programId);
291 if (albedo) albedo_programs_.insert(programId);
296 static const std::string fallback =
"default";
299 return layout && !layout->
name.empty() ? layout->
name : fallback;
320 const auto* material =
find(materialId);
321 const auto* layout = material ?
layoutFor(material->shaderRef) :
nullptr;
322 return layout && layout->vertexHook && layout->depthInputUnit < 0;
338 bool withNormals,
bool skinned,
bool tangents =
false)
const;
344 bool withNormals,
bool skinned,
bool tangents =
false)
const;
359 layouts_[shader.
id()] = std::move(layout);
361 reviseMaterialsOf(shader);
384 if (materialId == 0)
return;
385 auto it = materials_.find(materialId);
386 if (it == materials_.end()) {
395 std::vector<u8> bytes = std::move(rec.
uboBytes);
396 std::vector<MaterialTextureBinding> texs = std::move(rec.
textures);
413 auto it = materials_.find(materialId);
414 if (it == materials_.end())
return;
417 if (lit == layouts_.end())
return;
420 ensureDefaults(rec, lit->second);
421 const u32 n = std::min(arity, slot->
arity);
430 auto it = materials_.find(materialId);
431 if (it == materials_.end())
return;
432 auto lit = layouts_.find(it->second.shaderRef.id());
433 if (lit == layouts_.end())
return;
436 it->second.revision = revise();
437 for (
auto& b : it->second.textures) {
438 if (b.unit == slot->
unit) { b.texture = texture;
return; }
440 it->second.textures.push_back({ slot->
unit, texture });
447 if (materialId == 0)
return nullptr;
449 if (materialId == last_material_id_ && last_material_)
return last_material_;
450 auto it = materials_.find(materialId);
451 last_material_id_ = materialId;
452 last_material_ = it != materials_.end() ? &it->second :
nullptr;
453 return last_material_;
458 if (index < 0)
return 0.0f;
514 if (!layout)
return false;
523 if (!layout)
return false;
531 if (!layout)
return 0.0f;
533 return value > 0.0f ? value : 0.0f;
542 const u32 id = shader.
id();
543 if (
id == last_layout_id_ && last_layout_)
return last_layout_;
544 auto it = layouts_.find(
id);
545 last_layout_id_ = id;
546 last_layout_ = it != layouts_.end() ? &it->second :
nullptr;
564 const std::vector<std::pair<std::string, glm::vec4>>& overrides,
u32 scratch);
580 for (
const auto& p : layout.
params) {
582 defaultInUnits(p, world_unit_scale_, value);
583 std::memcpy(rec.
uboBytes.data() + p.offset, value, p.arity *
sizeof(
f32));
589 static void defaultInUnits(
const MaterialParamSlot& p,
f32 scale,
f32* out) {
590 const f32 k = p.worldUnits > 0 ? scale : p.worldUnits < 0 ? 1.0f / scale : 1.0f;
591 for (
u32 i = 0; i < 4; ++i) out[i] = p.defaults[i] * k;
595 f32 world_unit_scale_ = 1.0f;
597 std::unordered_map<u32, MaterialRecord> materials_;
598 std::unordered_map<u32, MaterialUniformLayout> layouts_;
600 std::vector<std::pair<BufferHandle, u32>> through_ubos_;
602 mutable u32 last_material_id_ = 0;
603 mutable const MaterialRecord* last_material_ =
nullptr;
604 mutable u32 last_layout_id_ = 0;
605 mutable const MaterialUniformLayout* last_layout_ =
nullptr;
606 void forgetLookups() {
607 last_material_ =
nullptr; last_material_id_ = 0; last_layout_ =
nullptr; last_layout_id_ = 0;
616 for (
auto& [
id, rec] : materials_)
if (rec.
shaderRef == shader) rec.
revision = revise();
621 struct ShaderSource {
623 std::string features;
626 bool mentionsGbuffer =
false;
629 bool writesAlbedo =
false;
630 bool takesLight =
false;
632 bool mentionsOpaque =
false;
634 std::unordered_map<u32, ShaderSource> sources_;
635 bool lighting_direct_ =
false;
641 mutable std::unordered_map<u32, std::unordered_map<u64, resource::ShaderHandle>> mesh_programs_;
642 mutable std::unordered_map<u32, std::unordered_map<u64, resource::ShaderHandle>> variant_sources_;
643 void releaseVariants(
const std::unordered_map<u64, resource::ShaderHandle>& variants);
644 static u64 sourceDigest(
const std::string& vertex,
const std::string& fragment);
646 mutable std::unordered_set<u32> gbuffer_programs_;
648 mutable std::unordered_set<u32> albedo_programs_;
658 for (
const auto& [shader, variants] : mesh_programs_) count +=
static_cast<u32>(variants.size());
667 u64 shape, std::vector<std::string> extraFeatures,
668 const char* what)
const;
Blend mode enumeration for custom rendering.
Backend-agnostic graphics enumerations.
Type-safe resource handle system.
What a resource's content is, as a number that changes whenever it does.
Core type definitions and utilities for ESEngine.
Abstract graphics device interface.
bool programWritesAlbedo(u32 programId) const
Of those, the ones that also write the surface's diffuse colour (location 2, fragAlbedo).
MaterialStore & operator=(const MaterialStore &)=delete
MaterialStore(const MaterialStore &)=delete
void setScreenColor(TextureHandle texture)
void registerLayout(resource::ShaderHandle shader, MaterialUniformLayout layout)
void markGBufferProgram(u32 programId, bool albedo) const
A program that is not a material's — the engine's own mesh shader — says it writes the record.
void bindForDraw(u32 materialId, u32 samplerUnits=0xFFFFFFFFu)
bool bindThrough(u32 materialId, resource::ShaderHandle shader, const std::vector< std::pair< std::string, glm::vec4 > > &overrides, u32 scratch)
Binds a material's values for a draw through another shader (a bake into a texture,...
const MaterialRecord * find(u32 materialId) const
The record for materialId, or nullptr if the handle is 0 / unregistered.
bool hasUntrackedDepthHook(u32 materialId) const
void setSceneColor(TextureHandle texture)
resource::ShaderHandle shaderOf(u32 materialId) const
void setBuiltinDefaults(TextureHandle white, TextureHandle black, TextureHandle flatNormal, TextureHandle ripples)
The context's built-in fallbacks.
static f32 paramValue(const MaterialRecord &m, const MaterialUniformLayout &layout, i32 index)
bool usesSceneColor(u32 materialId) const
u32 meshProgram(u32 materialId, resource::ResourceManager &resources, bool withNormals=false, bool skinned=false, bool envMapped=false, bool lightmapped=false, bool gbuffer=false, bool tangents=false, bool tipPass=false, bool opaque=false) const
u64 revision() const
Changes whenever any material does (revisionOf), or one is added or removed.
void setBuiltinDefaultArrays(TextureHandle white, TextureHandle black, TextureHandle flatNormal)
The context's layers of white, black and a flat normal: what an unset textureArray param binds.
void setWorldUnitsPerMeter(f32 unitsPerMeter)
void forgetShader(resource::ShaderHandle shader)
void rememberSource(resource::ShaderHandle shader, std::string source, std::string features)
float vertexBounds(u32 materialId) const
const std::string & shaderLabel(u32 materialId) const
The name of the shader materialId draws with, or "default" for the path's own batch shader.
TextureHandle builtinDefault(MaterialDefaultTexture which) const
const MaterialUniformLayout * layoutFor(resource::ShaderHandle shader) const
u32 shadowProgram(u32 materialId, resource::ResourceManager &resources, bool withNormals, bool skinned, bool tangents=false) const
void setDevice(GfxDevice *device)
The render path uses this device to create/upload/delete per-material UBOs.
bool lightingDirect() const
void setTexture(u32 materialId, const std::string &name, resource::TextureHandle texture)
TextureHandle builtinDefault(const MaterialTextureSlot &slot) const
What an unset param of slot binds: its declared default, as layers for an array (ripples are white th...
bool usesSceneDepth(u32 materialId) const
u32 prepassCoverageProgram(u32 materialId, resource::ResourceManager &resources, bool withNormals, bool skinned, bool tangents=false) const
u64 revisionOf(u32 materialId) const
What materialId is, as a number that changes whenever it does: its state, a param,...
u32 builtVariantCount() const
How many material program variants have been built. A prewarm counts what it caused by the difference...
bool hasTipPass(u32 materialId) const
void undefine(u32 materialId)
bool usesScreenColor(u32 materialId) const
u32 particleProgram(u32 materialId, resource::ResourceManager &resources) const
bool alphaIsCoverage(u32 materialId) const
bool programWritesGBuffer(u32 programId) const
bool castsCutoutShadow(u32 materialId) const
static MaterialUniformLayout layoutOf(const resource::ParsedShader &parsed)
void setUniform(u32 materialId, const std::string &name, const f32 *values, u32 arity)
bool needsScenePicture(u32 materialId) const
static void seedSamplers(Shader &shader, const resource::ParsedShader &parsed)
Point each of shader's texture params at its unit.
void setLightingDirect(bool on)
f32 hairShadowDensity(u32 materialId) const
The hair shadow density materialId holds (see ParsedShader::hairShadowParam), 0 when its shader has n...
void define(u32 materialId, const MaterialRecord &record)
void setResourceManager(resource::ResourceManager *resources)
Resolves texture handles at bind time; without one, texture params bind nothing.
bool hasVertexHook(u32 materialId) const
Whether materialId bends its vertices (so its shadow has to bend too).
void setSceneDepth(TextureHandle texture)
The copy of the opaque scene's distances the frame took, or invalid when it took none....
GPU shader program for rendering.
bool isValid() const
Checks if the handle references a valid resource.
IdType id() const
Gets the packed identifier (index + generation).
Central manager for GPU resources.
Handle< esengine::Shader > ShaderHandle
Handle to a shader resource.
Handle< esengine::Texture > TextureHandle
Handle to a texture resource.
std::int32_t i32
Signed 32-bit integer.
std::uint64_t u64
Unsigned 64-bit integer.
std::size_t usize
Size type for indexing and sizes.
std::int16_t i16
Signed 16-bit integer.
MaterialDefaultTexture
Which built-in texture a param falls back to when a material leaves it unset.
std::int8_t i8
Signed 8-bit integer.
MaterialDefaultTexture materialDefaultByName(const std::string &name)
Maps a shader's declared default(<name>) to the built-in it names.
std::uint32_t u32
Unsigned 32-bit integer.
BlendMode
Predefined blend modes for rendering.
std::uint8_t u8
Unsigned 8-bit integer.
CullMode
Triangle culling mode baked into a material's pipeline.
u64 nextRevision()
A revision no content has had before. Never 0, which reads as "none".
float f32
32-bit floating point
One scalar/vector param's slot in a shader's std140 MaterialConstants block.
i8 worldUnits
1 a length, -1 a rate per length: the default is at 100 units a metre (ShaderProperty::worldUnits).
u32 arity
Float component count (1..4); textures are not slots.
u32 offset
std140 byte offset within the block.
f32 defaults[4]
Shader-declared default(...); the block's initial value.
A material's resolved render state (P0) plus its packed std140 constants (P1).
resource::ShaderHandle shaderRef
std::vector< MaterialTextureBinding > textures
Texture params bound to sampler units, by handle (see MaterialTextureBinding).
u32 shader
Program id; 0 means "use the path's default batch shader".
u64 revision
Changes whenever anything above does, or its shader's layout: see core/Revision.hpp and MaterialStore...
std::vector< u8 > uboBytes
A material's bound texture at a sampler unit.
resource::TextureHandle texture
A texture param's sampler unit (>= MATERIAL_TEXTURE_UNIT_BASE, above the batch's 0....
MaterialDefaultTexture defaultTexture
Result of parsing an .esshader file.