Cogs.Core
RenderMaterialInstance.cpp
1#include "RenderMaterialInstance.h"
2
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"
8
9#include "Context.h"
10
11#include "RenderStates.h"
12#include "RenderResources.h"
13#include "Resources/MaterialManager.h"
14
15#include "Renderer.h"
16#include "Engine.h"
17
18#include "Foundation/HashSequence.h"
19#include "Foundation/Logging/Logger.h"
20
21namespace
22{
23 using namespace Cogs::Core;
24
25 Cogs::Logging::Log logger = Cogs::Logging::getLogger("RenderMaterialInstance");
26
27 void clearBuffers(RenderMaterialInstance* that, Cogs::IBuffers* deviceBuffers)
28 {
29 for (auto& buffer : that->ownedBuffers) {
30 if (HandleIsValid(buffer)) {
31 deviceBuffers->releaseBuffer(buffer);
32 }
33 }
34
35 for (auto& buffer : that->buffers) {
36 buffer.handle = Cogs::BufferHandle::NoHandle;
37 buffer.generation = 0;
38 }
39
40 that->ownedBuffers.clear();
41 }
42
43 size_t getBindingCode(const MeshStreamsLayout& streamsLayout,
44 const EnginePermutation* permutation,
45 const RenderPassOptions& passOptions,
46 ClipShapeType clipShape)
47 {
48 if (streamsLayout.numStreams && streamsLayout.hash == 0) {
49 LOG_ERROR(logger, "MeshStreamsLayout has streams but hash is zero.");
50 }
51 return Cogs::hashSequence(streamsLayout.hash, permutation->getCode(), passOptions.hash, clipShape);
52 }
53
54 void updateBindings(RenderMaterialInstance* that,
55 MaterialInstance* materialInstance,
56 const Material* material,
58 EffectBinding* /*effectBinding*/)
59 {
60 Cogs::IBuffers* deviceBuffers = device->getBuffers();
61 Cogs::IContext* immediateContext = device->getImmediateContext();
62
63 if (that->context->variables->get("renderer.disableMaterialUpdates", false)) {
64 return;
65 }
66
67 if (that->buffersGeneration != materialInstance->buffersGeneration) {
68 that->buffersGeneration = materialInstance->buffersGeneration;
69
70 // If a binding permutation has been encountered earlier with valid bindings for only a
71 // subset of buffers, we may have both owned buffers and missing buffers.
72 clearBuffers(that, deviceBuffers);
73
74 that->buffers.resize(material->constantBuffers.buffers.size());
75
76 for (auto& buffer : material->constantBuffers.buffers) {
77
78 if (buffer.isPerInstance && buffer.size) {
79 that->buffers[buffer.index].handle = deviceBuffers->loadBuffer(nullptr, buffer.size, Cogs::Usage::Dynamic, Cogs::AccessMode::Write, Cogs::BindFlags::ConstantBuffer);
80 deviceBuffers->annotate(that->buffers[buffer.index].handle, material->definition.name + "_matinst_" + std::to_string(buffer.index));
81
82 that->ownedBuffers.push_back(that->buffers[buffer.index].handle);
83 that->buffers[buffer.index].bound = true;
84 }
85 }
86 }
87
88 for (auto& buffer : material->constantBuffers.buffers) {
89 if (!buffer.isPerInstance) continue;
90
91 auto& instanceBuffer = materialInstance->buffers[buffer.index];
92 RenderMaterialInstance::UpdateBuffer& updateBuffer = that->buffers[buffer.index];
93
94 bool dirty = updateBuffer.generation != instanceBuffer.generation;
95
96 if (dirty && HandleIsValid(updateBuffer.handle)) {
97 immediateContext->updateBuffer(updateBuffer.handle, instanceBuffer.content.data(), buffer.size);
98 updateBuffer.generation = instanceBuffer.generation;
99 }
100 }
101 }
102
103
104 void updateTextureProperties(RenderMaterialInstance* that,
105 MaterialInstance* materialInstance,
106 Cogs::IGraphicsDevice* /*device*/,
107 RenderStates* renderStates)
108 {
109 auto& settings = that->context->renderer->getSettings();
110
111 // Allocate enough room to store a sampler state per texture.
112 that->samplerStates.resize(materialInstance->textureVariables.size());
113
114 for (auto& t : materialInstance->textureVariables) {
115 if (t.dirty || !HandleIsValid(that->samplerStates[t.key])) {
116 const Cogs::SamplerState state = {
117 t.texture.sMode,
118 t.texture.tMode,
119 t.texture.uMode,
120 t.texture.filterMode,
121 Cogs::SamplerState::Never,
122 static_cast<unsigned int>(settings.anisotropicFiltering->getInt()),
123 { 0, 0, 0, 1 }
124 };
125
126 that->samplerStates[t.key] = renderStates->getSamplerState(state);
127
128 debug_assert(HandleIsValid(that->samplerStates[t.key]));
129
130 t.dirty = false;
131 }
132 }
133 }
134
135 ActivationResult checkMaterial(RenderMaterialInstance* instance, RenderMaterial* renderMaterial)
136 {
137 if (!renderMaterial) {
138 return ActivationResult::Postponed;
139 }
140
141 if (renderMaterial->hasFailed()) {
142 instance->setFailed();
143 return ActivationResult::Failure;
144 }
145 else if (renderMaterial->isReleased()) {
146 LOG_ERROR(logger, "Could not initialize material instance for released material.");
147 instance->setFailed();
148 return ActivationResult::Failure;
149 }
150 else if (renderMaterial->isDelayed()) {
151 instance->setDelayed();
152 return ActivationResult::Delayed;
153 }
154 else if (!renderMaterial->isActive()) {
155 return ActivationResult::Postponed;
156 }
157
158 return ActivationResult::Success;
159 }
160}
161
162Cogs::Core::ActivationResult Cogs::Core::RenderMaterialInstance::update(MaterialInstance* materialInstance,
163 IGraphicsDevice* device,
164 RenderResources* resources,
165 RenderStates* renderStates)
166{
167 context = resources->getContext();
168
169 renderMaterial = resources->getRenderMaterial(materialInstance->material);
170
171 auto checkResult = checkMaterial(this, renderMaterial);
172
173 if (checkResult != ActivationResult::Success) return checkResult;
174
175 updateTextureProperties(this, materialInstance, device, renderStates);
176
177 if (materialInstance->permutationIndex != permutationIndex || variantGeneration != materialInstance->variantGeneration) {
178 loadedBindings.clear();
179 }
180
181 permutationIndex = materialInstance->permutationIndex;
182 variantGeneration = materialInstance->variantGeneration;
183
184 std::vector<RenderEffectBinding> stillPending;
185
186 for (RenderEffectBinding& pending : pendingBindings) {
187
188 if (pending.variantGeneration != variantGeneration) continue;
189
190 EffectBinding* binding = renderMaterial->getBinding(permutationIndex,
191 materialInstance,
192 &pending.streamsLayout,
193 pending.enginePermutation,
194 pending.passOptions,
195 pending.clipShape);
196
197 if (binding->renderEffect &&
198 HandleIsValid(binding->renderEffect->effectHandle) &&
199 HandleIsValid(binding->renderEffect->inputHandle) &&
200 !binding->renderEffect->isDelayed())
201 {
202 loadedBindings.push_back(std::move(pending));
203 loadedBindings.back().variantGeneration = 0u;
204 }
205 else {
206 stillPending.push_back(std::move(pending));
207 materialInstance->setChanged();
208 }
209 }
210
211 pendingBindings = std::move(stillPending);
212
213 for (auto & loaded : loadedBindings) {
214 updateBindings(this, materialInstance, materialInstance->material, device, loaded.binding);
215 }
216
217 setActive();
218
220}
221
222
223void Cogs::Core::RenderMaterialInstance::release(Renderer * renderer)
224{
225 assert(!isReleased());
226
227 clearBuffers(this, renderer->getDevice()->getBuffers());
228
229 setReleased();
230}
231
233 const EnginePermutation* permutation,
234 const RenderPassOptions& passOptions,
235 ClipShapeType clipShape)
236{
237 MaterialInstance* materialInstance = getResource();
238
239 // We already have an binding
240 if (const EffectBinding* bindings = getBindings(streamsLayout, permutation, passOptions, clipShape); bindings) {
241
242 if (!bindings->renderEffect ||
243 bindings->renderEffect->isDelayed() ||
244 !HandleIsValid(bindings->renderEffect->effectHandle) ||
245 !HandleIsValid(bindings->renderEffect->inputHandle) ||
246 bindings->buffersGeneration != materialInstance->material->constantBuffers.buffersGeneration ||
247 buffersGeneration != materialInstance->buffersGeneration)
248 {
249 Material* material = materialInstance->material;
250
251 if (bindings->renderEffect && bindings->renderEffect->isDelayed()) {
252 bindings->renderEffect->requestLoad();
253 context->engine->setDirty();
254 }
255
256 else if (renderMaterial->isDelayed() || renderMaterial->pendingBindings.size()) {
257 // Ensure the material is queued for processing if it is either delayed or has pending bindings to set up.
258 material->setChanged();
259 }
260
261 if (materialInstance->hasFailedActivation()) {
262 materialInstance->setChanged();
263 materialInstance->material->setChanged();
264 }
265
266 return nullptr;
267 }
268
269 return bindings;
270 }
271
272 // We do not have any bindings, try to get one
273 else {
274
275 size_t permuationCode = getBindingCode(streamsLayout, permutation, passOptions, clipShape);
276
277 Material* material = materialInstance->material;
278
279 EffectBinding* binding = nullptr;
280 if (renderMaterial) {
281 binding = renderMaterial->getBinding(permutationIndex,
282 materialInstance,
283 &streamsLayout,
284 permutation,
285 passOptions,
286 clipShape);
287 }
288
289 if (binding && binding->renderEffect && binding->buffersGeneration == material->constantBuffers.buffersGeneration) {
290 auto device = context->renderer->getDevice();
291 updateBindings(this, materialInstance, material, device, binding);
292 updateTextureProperties(this, materialInstance, device, &context->renderer->getRenderStates());
293
294 loadedBindings.push_back(RenderEffectBinding{ permuationCode, streamsLayout, permutation, passOptions, clipShape, binding, 0 });
295
296 return checkReady(streamsLayout, permutation, passOptions, clipShape);
297 }
298
299 else {
300 pendingBindings.emplace_back(RenderEffectBinding{ permuationCode, streamsLayout, permutation, passOptions, clipShape, binding, variantGeneration });
301
302 materialInstance->setChanged();
303 context->engine->setDirty();
304 }
305
306
307 if (materialInstance->hasFailedActivation()) {
308 materialInstance->setChanged();
309 materialInstance->material->setChanged();
310 }
311
312 return nullptr;
313 }
314}
315
317 const EnginePermutation * permutation,
318 const RenderPassOptions& passOptions,
319 ClipShapeType clipShape) const
320{
321 permutation = context->renderer->getEnginePermutations().get(permutation->getIndex());
322
323 size_t code = getBindingCode(streamsLayout, permutation, passOptions, clipShape);
324
325 for (auto & binding : loadedBindings) {
326 if (binding.permutationCode == code) return binding.binding;
327 }
328
329 for (auto & binding : pendingBindings) {
330 if (binding.permutationCode == code) return binding.binding;
331 }
332
333 return nullptr;
334}
class IRenderer * renderer
Renderer.
Definition: Context.h:228
std::unique_ptr< class Variables > variables
Variables service instance.
Definition: Context.h:180
virtual const RenderSettings & getSettings() const =0
Get the settings of the renderer.
Contains render resources used by the renderer.
Core renderer system.
Definition: Renderer.h:28
IGraphicsDevice * getDevice() override
Get the graphics device used by the renderer.
Definition: Renderer.h:45
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.
Definition: LogManager.h:139
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.
Definition: ResourceBase.h:14
@ 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
Definition: LogManager.h:180
constexpr size_t hashSequence(const T &t, const U &u)
Hash the last two items in a sequence of objects.
Definition: HashSequence.h:8
@ Write
The buffer can be mapped and written to by the CPU after creation.
Definition: Flags.h:50
@ ConstantBuffer
The buffer can be bound as input to effects as a constant buffer.
Definition: Flags.h:72
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...
Definition: Material.h:82
const EffectBinding * getBindings(const MeshStreamsLayout &streamsLayout, const EnginePermutation *permutation, const RenderPassOptions &passOptions, ClipShapeType clipShape) const
const EffectBinding * checkReady(const MeshStreamsLayout &streamsLayout, const EnginePermutation *permutation, const RenderPassOptions &passOptions, ClipShapeType clipShape)
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.
Definition: Common.h:77
Provides buffer management functionality.
Definition: IBuffers.h:13
virtual void annotate(BufferHandle handle, const StringView &name)
Associate a name with an object for use in graphics debugging.
Definition: IBuffers.h:17
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.
Definition: IContext.h:43
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.
Definition: SamplerState.h:12
@ Dynamic
Buffer will be loaded and modified with some frequency.
Definition: Flags.h:30