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 Material* material = materialInstance->material;
170 assert(material);
171
172 renderMaterial = resources->getRenderMaterial(materialInstance->material);
173
174 auto checkResult = checkMaterial(this, renderMaterial);
175
176 if (checkResult != ActivationResult::Success) return checkResult;
177
178 updateTextureProperties(this, materialInstance, device, renderStates);
179
180 if ((permutationIndex != materialInstance->permutationIndex) ||
181 (materialVariantGeneration != material->variantGeneration) ||
182 (instanceVariantGeneration != materialInstance->variantGeneration))
183 {
184 loadedBindings.clear();
185 pendingBindings.clear();
186
187 permutationIndex = materialInstance->permutationIndex;
188 materialVariantGeneration = material->variantGeneration;
189 instanceVariantGeneration = materialInstance->variantGeneration;
190 }
191
192 std::vector<RenderEffectBinding> stillPending;
193
194 for (RenderEffectBinding& pending : pendingBindings) {
195
196 EffectBinding* binding = renderMaterial->getBinding(permutationIndex,
197 materialInstance,
198 &pending.streamsLayout,
199 pending.enginePermutation,
200 pending.passOptions,
201 pending.clipShape);
202
203 if (binding->renderEffect &&
204 HandleIsValid(binding->renderEffect->effectHandle) &&
205 HandleIsValid(binding->renderEffect->inputHandle) &&
206 !binding->renderEffect->isDelayed())
207 {
208 loadedBindings.push_back(std::move(pending));
209 }
210 else {
211 stillPending.push_back(std::move(pending));
212 materialInstance->setChanged();
213 }
214 }
215
216 pendingBindings = std::move(stillPending);
217
218 for (auto & loaded : loadedBindings) {
219 updateBindings(this, materialInstance, materialInstance->material, device, loaded.binding);
220 }
221
222 setActive();
223
225}
226
227
228void Cogs::Core::RenderMaterialInstance::release(Renderer * renderer)
229{
230 assert(!isReleased());
231
232 clearBuffers(this, renderer->getDevice()->getBuffers());
233
234 setReleased();
235}
236
238 const EnginePermutation* permutation,
239 const RenderPassOptions& passOptions,
240 ClipShapeType clipShape)
241{
242 MaterialInstance* materialInstance = getResource();
243
244 // We already have an binding
245 if (const EffectBinding* bindings = getBindings(streamsLayout, permutation, passOptions, clipShape); bindings) {
246
247 if (!bindings->renderEffect ||
248 bindings->renderEffect->isDelayed() ||
249 !HandleIsValid(bindings->renderEffect->effectHandle) ||
250 !HandleIsValid(bindings->renderEffect->inputHandle) ||
251 bindings->buffersGeneration != materialInstance->material->constantBuffers.buffersGeneration ||
252 buffersGeneration != materialInstance->buffersGeneration)
253 {
254 Material* material = materialInstance->material;
255
256 if (bindings->renderEffect && bindings->renderEffect->isDelayed()) {
257 bindings->renderEffect->requestLoad();
258 context->engine->setDirty();
259 }
260
261 else if (renderMaterial->isDelayed() || renderMaterial->pendingBindings.size()) {
262 // Ensure the material is queued for processing if it is either delayed or has pending bindings to set up.
263 material->setChanged();
264 }
265
266 if (materialInstance->hasFailedActivation()) {
267 materialInstance->setChanged();
268 materialInstance->material->setChanged();
269 }
270
271 return nullptr;
272 }
273
274 return bindings;
275 }
276
277 // We do not have any bindings, try to get one
278 else {
279
280 size_t permuationCode = getBindingCode(streamsLayout, permutation, passOptions, clipShape);
281
282 Material* material = materialInstance->material;
283
284 EffectBinding* binding = nullptr;
285 if (renderMaterial) {
286 binding = renderMaterial->getBinding(permutationIndex,
287 materialInstance,
288 &streamsLayout,
289 permutation,
290 passOptions,
291 clipShape);
292 }
293
294 if (binding && binding->renderEffect && binding->buffersGeneration == material->constantBuffers.buffersGeneration) {
295 auto device = context->renderer->getDevice();
296 updateBindings(this, materialInstance, material, device, binding);
297 updateTextureProperties(this, materialInstance, device, &context->renderer->getRenderStates());
298
299 loadedBindings.push_back(RenderEffectBinding{ permuationCode, streamsLayout, permutation, passOptions, clipShape, binding });
300
301 return checkReady(streamsLayout, permutation, passOptions, clipShape);
302 }
303
304 else {
305 pendingBindings.emplace_back(RenderEffectBinding{ permuationCode, streamsLayout, permutation, passOptions, clipShape, binding });
306
307 materialInstance->setChanged();
308 context->engine->setDirty();
309 }
310
311
312 if (materialInstance->hasFailedActivation()) {
313 materialInstance->setChanged();
314 materialInstance->material->setChanged();
315 }
316
317 return nullptr;
318 }
319}
320
322 const EnginePermutation * permutation,
323 const RenderPassOptions& passOptions,
324 ClipShapeType clipShape) const
325{
326 permutation = context->renderer->getEnginePermutations().get(permutation->getIndex());
327
328 size_t code = getBindingCode(streamsLayout, permutation, passOptions, clipShape);
329
330 for (auto & binding : loadedBindings) {
331 if (binding.permutationCode == code) return binding.binding;
332 }
333
334 for (auto & binding : pendingBindings) {
335 if (binding.permutationCode == code) return binding.binding;
336 }
337
338 return nullptr;
339}
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:140
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:181
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:78
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