// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. #pragma once #include "IMaterial.h" #include "Engine/Graphics/GPUPipelineState.h" #include "Engine/Renderer/Config.h" /// /// Current materials shader version. /// #define MATERIAL_GRAPH_VERSION 146 class Material; class GPUShader; class MemoryReadStream; /// /// Represents material shader that can be used to render objects, visuals or effects. Contains a dedicated shader. /// class MaterialShader : public IMaterial { protected: struct PipelineStateCache { GPUPipelineState* PS[6]; GPUPipelineState::Description Desc; PipelineStateCache() { Platform::MemoryClear(PS, sizeof(PS)); } void Init(GPUPipelineState::Description& desc) { Desc = desc; } GPUPipelineState* GetPS(CullMode mode, bool wireframe); void Release() { for (int32 i = 0; i < ARRAY_COUNT(PS); i++) { SAFE_DELETE_GPU_RESOURCE(PS[i]); } } }; protected: bool _isLoaded; GPUShader* _shader; Array _cb0Data; Array _cb1Data; MaterialInfo _info; protected: /// /// Init /// /// Material resource name MaterialShader(const String& name); public: /// /// Finalizes an instance of the class. /// virtual ~MaterialShader(); public: /// /// Creates and loads the material from the data. /// /// Material resource name /// Stream with compiled shader data /// Loaded material info structure /// The created and loaded material or null if failed. static MaterialShader* Create(const String& name, MemoryReadStream& shaderCacheStream, const MaterialInfo& info); /// /// Creates the dummy material used by the Null rendering backend to mock object but not perform any rendering. /// /// The shader cache stream. /// The material information. /// The created and loaded material or null if failed. static MaterialShader* CreateDummy(MemoryReadStream& shaderCacheStream, const MaterialInfo& info); public: /// /// Clear loaded data /// virtual void Unload(); protected: bool Load(MemoryReadStream& shaderCacheStream, const MaterialInfo& info); virtual bool Load() = 0; public: // [IMaterial] const MaterialInfo& GetInfo() const override; bool IsReady() const override; };