Cogs.Core
ContextCommon.cpp
1#include "ContextCommon.h"
2
3#include "../IBuffers.h"
4
5#include "Foundation/Logging/Logger.h"
6
7namespace
8{
9 static Cogs::Logging::Log logger = Cogs::Logging::getLogger("ContextCommon");
10}
11
12const Cogs::FrameStatistics& Cogs::ContextCommon::getLastFrameStatistics()
13{
14 return prevStats;
15}
16
17const Cogs::UploadStatistics& Cogs::ContextCommon::getLastUploadStatistics()
18{
19 return prevUploadStats;
20}
21
23 prevStats = currStats;
24 currStats = FrameStatistics{};
25
26 prevUploadStats = currUploadStats;
27 currUploadStats = UploadStatistics{};
28}
29
30void Cogs::ContextCommon::frameStatisticsAccountDrawCall(size_t count, bool indexed)
31{
32 // bucket is position of most significant bit, zero is zero.
33 unsigned bucket = 0;
34 size_t t = count;
35 if ((t & 0xFFFF0000) != 0) { bucket += 16; t = t >> 16u; }
36 if ((t & 0x0000FF00) != 0) { bucket += 8; t = t >> 8u; }
37 if ((t & 0x000000F0) != 0) { bucket += 4; t = t >> 4u; }
38 if ((t & 0x0000000C) != 0) { bucket += 2; t = t >> 2u; }
39 if ((t & 0x00000002) != 0) { bucket += 1; }
40
41#ifdef _DEBUG
42 assert(uint64_t(count) < (uint64_t(1) << (bucket + 1)));
43 if (count) {
44 assert((uint64_t(1) << bucket) <= uint64_t(count));
45 }
46 else {
47 assert(bucket == 0);
48 }
49#endif
50
51 currStats.drawCallHistogram[bucket]++;
52 currStats.vertices += count;
53 if (indexed) currStats.indices += count;
54}
55
56void Cogs::ContextCommon::uploadStatisticsBufferUpload(size_t size)
57{
58 currUploadStats.bufferUploads++;
59 currUploadStats.bufferUploadSize += size;
60}
61
62void Cogs::ContextCommon::uploadStatisticsTextureUpload(size_t size)
63{
64 currUploadStats.textureUploads++;
65 currUploadStats.textureUploadSize += size;
66}
67
68void Cogs::ContextCommon::setMatrixVariable(const EffectVariableHandle variableHandle, const float * value)
69{
70 setVariable(variableHandle, reinterpret_cast<const unsigned char *>(value), sizeof(float) * 16);
71}
72
73void Cogs::ContextCommon::setMatrixVariable(const EffectVariableHandle variableHandle, const float * value, size_t count)
74{
75 setVariable(variableHandle, reinterpret_cast<const unsigned char *>(value), sizeof(float) * 16 * count);
76}
77
78void Cogs::ContextCommon::setScalarVariable(const EffectVariableHandle variableHandle, const float value)
79{
80 setVariable(variableHandle, reinterpret_cast<const unsigned char *>(&value), sizeof(float) * 1);
81}
82
83void Cogs::ContextCommon::setScalarVariable(const EffectVariableHandle variableHandle, const float* value, size_t count)
84{
85 setVariable(variableHandle, reinterpret_cast<const unsigned char *>(value), sizeof(float) * count);
86}
87
89{
90 setVariable(variableHandle, reinterpret_cast<const unsigned char *>(&value), sizeof(int) * 1);
91}
92
93void Cogs::ContextCommon::setScalarVariable(const EffectVariableHandle variableHandle, const int* value, size_t count)
94{
95 setVariable(variableHandle, reinterpret_cast<const unsigned char *>(value), sizeof(int) * count);
96}
97
98void Cogs::ContextCommon::setVector2Variable(const EffectVariableHandle variableHandle, const float * value, size_t count)
99{
100 setVariable(variableHandle, reinterpret_cast<const unsigned char *>(value), sizeof(float) * 2 * count);
101}
102
103void Cogs::ContextCommon::setVector3Variable(const EffectVariableHandle variableHandle, const float * value, size_t count)
104{
105 setVariable(variableHandle, reinterpret_cast<const unsigned char *>(value), sizeof(float) * 3 * count);
106}
107
108void Cogs::ContextCommon::setVector4Variable(const EffectVariableHandle variableHandle, const float * value, size_t count)
109{
110 setVariable(variableHandle, reinterpret_cast<const unsigned char *>(value), sizeof(float) * 4 * count);
111}
112
113void Cogs::ContextCommon::setVector4Variable(const EffectVariableHandle variableHandle, const int * value, size_t count)
114{
115 setVariable(variableHandle, reinterpret_cast<const unsigned char *>(value), sizeof(float) * 4 * count);
116}
117
118void Cogs::ContextCommon::setConstantBuffer(const StringView& name, const BufferHandle bufferHandle, const uint32_t offset, const uint32_t size)
119{
120 setConstantBuffer(getEffects()->getConstantBufferBinding(getCurrentEffect(), name), bufferHandle, offset, size);
121}
123{
124 auto binding = getEffects()->getBufferBinding(getCurrentEffect(), name);
125 setBuffer(binding, bufferHandle);
126 getEffects()->releaseBufferBinding(binding);
127}
128void Cogs::ContextCommon::setTexture(const StringView& name, unsigned int unit, TextureHandle textureHandle)
129{
130 setTexture(getEffects()->getTextureBinding(getCurrentEffect(), name, unit), textureHandle);
131}
132void Cogs::ContextCommon::setTexture(const StringView& name, TextureViewHandle textureViewHandle)
133{
134 setTexture(getEffects()->getTextureBinding(getCurrentEffect(), name, 0), textureViewHandle);
135}
136void Cogs::ContextCommon::setSamplerState(const StringView& name, unsigned int unit, SamplerStateHandle handle)
137{
138 setSamplerState(getEffects()->getSamplerStateBinding(getCurrentEffect(), name, unit), handle);
139}
140
141void Cogs::ContextCommon::setCurrentEffect(Effect * currentEffect)
142{
143 this->effect = currentEffect;
144}
145
146void Cogs::ContextCommon::updateConstantBuffers()
147{
148 if (!effect) return;
149
150 auto updateConstantBuffer = [&](ShaderConstantBuffer & constantBuffer) {
151 if (!constantBuffer.dirty || constantBuffer.manual) return;
152
153 MappedBuffer<uint8_t> mappedBuffer(this, constantBuffer.buffer, MapMode::WriteDiscard);
154
155 if (mappedBuffer) {
156 std::memcpy(mappedBuffer, constantBuffer.memoryBuffer.data(), constantBuffer.memoryBuffer.size());
157 }
158 constantBuffer.dirty = false;
159 };
160
161 for (auto & shader : effect->shaders) {
162 for (auto & constantBuffer : shader.reflection.constantBuffers) {
163 if (HandleIsValid(constantBuffer.buffer)) {
164 updateConstantBuffer(constantBuffer);
165 }
166 }
167 }
168}
169
170void Cogs::ContextCommon::updateBuffer(BufferHandle bufferHandle, const void* data, size_t size)
171{
172 MappedBuffer<uint8_t> mappedBuffer(this, bufferHandle, MapMode::WriteDiscard);
173 if (mappedBuffer) {
174 std::memcpy(mappedBuffer, data, size);
175 }
176 else {
177 LOG_ERROR_ONCE(logger, "updateBuffer: Faild to map buffer");
178 }
179}
180
181void Cogs::ContextCommon::setMatrixVariable(const StringView& name, const float * value)
182{
183 setVariable(name, reinterpret_cast<const unsigned char *>(value), sizeof(float) * 16);
184}
185
186void Cogs::ContextCommon::setVariable(const EffectVariableHandle handle, const uint8_t * data, size_t size)
187{
188 if (!HandleIsValid(handle)) return;
189
190 auto effectVariable = reinterpret_cast<EffectVariable *>(handle.handle);
191
192 for (auto & variable : effectVariable->variables) {
193 std::memcpy(variable.buffer->memoryBuffer.data() + variable.offset, data, size);
194 variable.buffer->dirty = true;
195 }
196
197 constantBuffersUpdated = false;
198}
199
200void Cogs::ContextCommon::setVariable(const StringView& name, const uint8_t * data, size_t size)
201{
202 setVariable(getEffects()->getEffectVariable(getCurrentEffect(), name), data, size);
203}
204
206{
207 LOG_ERROR_ONCE(logger, "Rendering context does not support vertex array objects.");
208}
Log implementation class.
Definition: LogManager.h:139
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
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
void setVector4Variable(const StringView &name, const float *value) override
Sets the vector variable with the given name to the given four-component value.
Definition: ContextCommon.h:52
void frameStatisticsBeginFrame()
void setVector3Variable(const StringView &name, const float *value) override
Sets the vector variable with the given name to the given three-component value.
Definition: ContextCommon.h:48
void setScalarVariable(const StringView &name, const float value) override
Sets the scalar floating point variable with the given name to the given value.
Definition: ContextCommon.h:36
void setConstantBuffer(const StringView &name, const BufferHandle bufferHandle, const uint32_t offset, const uint32_t size) final
Sets a constant buffer to be bound to the given name and slot.
void setSamplerState(const StringView &name, unsigned int unit, SamplerStateHandle handle) final
Sets the sampler slot given by unit with the given name to contain the given sampler state.
void setVertexArrayObject(VertexArrayObjectHandle vertexArrayObject) override
void updateBuffer(BufferHandle bufferHandle, const void *data, size_t size) override
Replace contents of buffer with new data.
void setTexture(const StringView &name, unsigned int unit, TextureHandle textureHandle) final
Sets the texture slot given by unit with the given name to contain the given texture.
void setBuffer(const StringView &name, BufferHandle bufferHandle) final
Sets the given buffer to the buffer binding slot with the given name.
void setVector2Variable(const StringView &name, const float *value) override
Sets the vector variable with the given name to the given two-component value.
Definition: ContextCommon.h:44
void setMatrixVariable(const StringView &name, const float *value) override
Sets the matrix variable with the given name in the current effect to value.
handle_type handle
Internal resource handle.
Definition: Common.h:74
@ WriteDiscard
Write access. When unmapping the graphics system will discard the old contents of the resource.
Definition: Flags.h:103
Provides RAII style mapping of a buffer resource.
Definition: IBuffers.h:160