1#include "RenderMaterialInstance.h"
3#include "Rendering/IEffects.h"
4#include "Rendering/IBuffers.h"
5#include "Rendering/IContext.h"
6#include "Rendering/ITextures.h"
7#include "Rendering/ICapabilities.h"
11#include "RenderStates.h"
12#include "RenderResources.h"
13#include "Resources/MaterialManager.h"
18#include "Foundation/HashSequence.h"
19#include "Foundation/Logging/Logger.h"
29 for (
auto& buffer : that->ownedBuffers) {
35 for (
auto& buffer : that->buffers) {
37 buffer.generation = 0;
40 that->ownedBuffers.clear();
49 LOG_ERROR(logger,
"MeshStreamsLayout has streams but hash is zero.");
63 if (that->context->
variables->get(
"renderer.disableMaterialUpdates",
false)) {
72 clearBuffers(that, deviceBuffers);
74 that->buffers.resize(material->constantBuffers.
buffers.size());
76 for (
auto& buffer : material->constantBuffers.
buffers) {
78 if (buffer.isPerInstance && buffer.size) {
80 deviceBuffers->
annotate(that->buffers[buffer.index].handle, material->definition.name +
"_matinst_" + std::to_string(buffer.index));
82 that->ownedBuffers.push_back(that->buffers[buffer.index].handle);
83 that->buffers[buffer.index].bound =
true;
88 for (
auto& buffer : material->constantBuffers.
buffers) {
89 if (!buffer.isPerInstance)
continue;
91 auto& instanceBuffer = materialInstance->
buffers[buffer.index];
94 bool dirty = updateBuffer.generation != instanceBuffer.generation;
97 immediateContext->
updateBuffer(updateBuffer.handle, instanceBuffer.content.data(), buffer.size);
98 updateBuffer.generation = instanceBuffer.generation;
116 unsigned int anisotropy = 1;
118 anisotropy =
static_cast<unsigned int>(settings.anisotropicFiltering->getInt());
124 t.texture.filterMode,
125 Cogs::SamplerState::Never,
130 that->samplerStates[t.key] = renderStates->getSamplerState(state);
141 if (!renderMaterial) {
142 return ActivationResult::Postponed;
145 if (renderMaterial->hasFailed()) {
146 instance->setFailed();
147 return ActivationResult::Failure;
149 else if (renderMaterial->isReleased()) {
150 LOG_ERROR(logger,
"Could not initialize material instance for released material.");
151 instance->setFailed();
152 return ActivationResult::Failure;
155 instance->setDelayed();
156 return ActivationResult::Delayed;
158 else if (!renderMaterial->
isActive()) {
159 return ActivationResult::Postponed;
162 return ActivationResult::Success;
171 uint16_t buffersGeneration)
173 if (effectBinding->buffersGeneration == material->constantBuffers.
buffersGeneration &&
176 return effectBinding;
179 if (renderMaterial->
isDelayed() || !renderMaterial->pendingBindings.empty()) {
180 material->setChanged();
183 if (materialInstance->hasFailedActivation()) {
184 materialInstance->setChanged();
185 material->setChanged();
188 context->
engine->setDirty();
199 context = resources->getContext();
204 renderMaterial = resources->getRenderMaterial(materialInstance->
material);
206 auto checkResult = checkMaterial(
this, renderMaterial);
210 updateTextureProperties(
this, materialInstance, device, renderStates);
213 (materialVariantGeneration != material->variantGeneration) ||
216 loadedBindings.clear();
217 pendingBindings.clear();
220 materialVariantGeneration = material->variantGeneration;
224 std::vector<RenderEffectBinding> stillPending;
226 for (RenderEffectBinding& pending : pendingBindings) {
230 &pending.streamsLayout,
231 pending.enginePermutation,
236 pending.binding = binding;
237 loadedBindings.push_back(std::move(pending));
240 stillPending.push_back(std::move(pending));
241 materialInstance->setChanged();
245 pendingBindings = std::move(stillPending);
247 for (
auto & loaded : loadedBindings) {
248 updateBindings(
this, materialInstance, materialInstance->
material, device, loaded.binding);
257void Cogs::Core::RenderMaterialInstance::release(
Renderer * renderer)
259 assert(!isReleased());
277 size_t bindingCode = getBindingCode(streamsLayout, permutation, passOptions, clipShape);
280 if (binding.permutationCode == bindingCode) {
281 return checkEffectBinding(context, binding.binding, renderMaterial, materialInstance, material, buffersGeneration);
285 bool pending =
false;
287 if (binding.permutationCode == bindingCode) {
294 if (renderMaterial) {
304 updateBindings(
this, materialInstance, material, device, bindings);
307 loadedBindings.push_back(
RenderEffectBinding { bindingCode, streamsLayout, permutation, passOptions, clipShape, bindings });
310 for (
size_t i = 0; i < pendingBindings.size(); ++i) {
311 if (pendingBindings[i].permutationCode == bindingCode) {
312 pendingBindings.erase(pendingBindings.begin() +
static_cast<ptrdiff_t
>(i));
317 return checkEffectBinding(context, bindings, renderMaterial, materialInstance, material, buffersGeneration);
322 pendingBindings.emplace_back(
RenderEffectBinding{ bindingCode, streamsLayout, permutation, passOptions, clipShape,
nullptr });
325 materialInstance->setChanged();
326 if (materialInstance->hasFailedActivation()) {
327 material->setChanged();
330 context->
engine->setDirty();
A Context instance contains all the services, systems and runtime components needed to use Cogs.
class IRenderer * renderer
Renderer.
std::unique_ptr< class Variables > variables
Variables service instance.
std::unique_ptr< class Engine > engine
Engine instance.
virtual RenderStates & getRenderStates()=0
Get the reference to the RenderStates structure.
virtual IGraphicsDevice * getDevice()=0
Get the graphics device used by the renderer.
virtual EnginePermutations & getEnginePermutations()=0
Get the reference to the EnginePermutations structure.
virtual const RenderSettings & getSettings() const =0
Get the settings of the renderer.
Contains render resources used by the renderer.
IGraphicsDevice * getDevice() override
Get the graphics device used by the renderer.
Represents a graphics device used to manage graphics resources and issue drawing commands.
virtual IContext * getImmediateContext()=0
Get a pointer to the immediate context used to issue commands to the graphics device.
virtual IBuffers * getBuffers()=0
Get a pointer to the buffer management interface.
Log implementation class.
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
ClipShapeType
Specifices what kind of shape a clip shape has.
ActivationResult
Defines results for resource activation.
@ Success
Resource activated successfully.
bool HandleIsValid(const ResourceHandle_t< T > &handle)
Check if the given resource is valid, that is not equal to NoHandle or InvalidHandle.
constexpr Log getLogger(const char(&name)[LEN]) noexcept
constexpr size_t hashSequence(const T &t, const U &u)
Hash the last two items in a sequence of objects.
@ Write
The buffer can be mapped and written to by the CPU after creation.
@ ConstantBuffer
The buffer can be bound as input to effects as a constant buffer.
std::vector< MaterialPropertyBuffer > buffers
Constant buffer instances.
uint16_t buffersGeneration
If the constant buffer bindings need updates.
Material instances represent a specialized Material combined with state for all its buffers and prope...
std::vector< TextureValue > textureVariables
Texture property values for this instance.
uint16_t buffersGeneration
If the material buffer bindings need updates.
std::vector< MaterialPropertyBufferInstance > buffers
Buffer instances matching the buffers and layout of the parent material.
size_t variantGeneration
If the variant or definitions need updates.
size_t permutationIndex
Index of material permutation to use.
Material * material
Material resource this MaterialInstance is created from.
Material resources define the how of geometry rendering (the what is defined by Mesh and Texture reso...
const EffectBinding * checkReady(const MeshStreamsLayout &streamsLayout, const EnginePermutation *permutation, const RenderPassOptions &passOptions, ClipShapeType clipShape, const RenderTarget *renderTarget)
EffectBinding * getBinding(const size_t permutationIndex, const MaterialInstance *materialInstance, const MeshStreamsLayout *streamsLayout, const EnginePermutation *enginePermutation, const RenderPassOptions &passOptions, const ClipShapeType clipShape)
bool isDelayed() const
Get if the render resource is in a delayed state.
bool isActive() const
Get if the render resource is active and can be used for rendering.
static const Handle_t NoHandle
Represents a handle to nothing.
Provides buffer management functionality.
virtual void annotate(BufferHandle handle, const StringView &name)
Associate a name with an object for use in graphics debugging.
virtual BufferHandle loadBuffer(const void *data, const size_t size, Usage::EUsage usage, uint32_t accessMode, uint32_t bindFlags, uint32_t stride=0)=0
Loads a new buffer using the given data to populate the buffer.
virtual void releaseBuffer(BufferHandle bufferHandle)=0
Releases the buffer with the given bufferHandle.
Represents a graphics device context which can receive rendering commands.
virtual void updateBuffer(BufferHandle bufferHandle, const void *data, size_t size)=0
Replace contents of buffer with new data.
Encapsulates state for texture sampling in a state object.
@ MinMagMipLinear
Linear sampling for both minification and magnification.
@ Dynamic
Buffer will be loaded and modified with some frequency.