1#include "ContextCommon.h"
3#include "../IBuffers.h"
5#include "Foundation/Logging/Logger.h"
19 return prevUploadStats;
23 prevStats = currStats;
26 prevUploadStats = currUploadStats;
30void Cogs::ContextCommon::frameStatisticsAccountDrawCall(
size_t count,
bool indexed)
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; }
42 assert(uint64_t(count) < (uint64_t(1) << (bucket + 1)));
44 assert((uint64_t(1) << bucket) <= uint64_t(count));
51 currStats.drawCallHistogram[bucket]++;
52 currStats.vertices += count;
53 if (indexed) currStats.indices += count;
56void Cogs::ContextCommon::uploadStatisticsBufferUpload(
size_t size)
58 currUploadStats.bufferUploads++;
59 currUploadStats.bufferUploadSize += size;
62void Cogs::ContextCommon::uploadStatisticsTextureUpload(
size_t size)
64 currUploadStats.textureUploads++;
65 currUploadStats.textureUploadSize += size;
70 setVariable(variableHandle,
reinterpret_cast<const unsigned char *
>(value),
sizeof(
float) * 16);
75 setVariable(variableHandle,
reinterpret_cast<const unsigned char *
>(value),
sizeof(
float) * 16 * count);
80 setVariable(variableHandle,
reinterpret_cast<const unsigned char *
>(&value),
sizeof(
float) * 1);
85 setVariable(variableHandle,
reinterpret_cast<const unsigned char *
>(value),
sizeof(
float) * count);
90 setVariable(variableHandle,
reinterpret_cast<const unsigned char *
>(&value),
sizeof(
int) * 1);
95 setVariable(variableHandle,
reinterpret_cast<const unsigned char *
>(value),
sizeof(
int) * count);
100 setVariable(variableHandle,
reinterpret_cast<const unsigned char *
>(value),
sizeof(
float) * 2 * count);
105 setVariable(variableHandle,
reinterpret_cast<const unsigned char *
>(value),
sizeof(
float) * 3 * count);
110 setVariable(variableHandle,
reinterpret_cast<const unsigned char *
>(value),
sizeof(
float) * 4 * count);
115 setVariable(variableHandle,
reinterpret_cast<const unsigned char *
>(value),
sizeof(
float) * 4 * count);
120 setConstantBuffer(getEffects()->getConstantBufferBinding(getCurrentEffect(), name), bufferHandle, offset, size);
124 auto binding = getEffects()->getBufferBinding(getCurrentEffect(), name);
125 setBuffer(binding, bufferHandle);
126 getEffects()->releaseBufferBinding(binding);
130 setTexture(getEffects()->getTextureBinding(getCurrentEffect(), name, unit), textureHandle);
134 setTexture(getEffects()->getTextureBinding(getCurrentEffect(), name, 0), textureViewHandle);
138 setSamplerState(getEffects()->getSamplerStateBinding(getCurrentEffect(), name, unit), handle);
141void Cogs::ContextCommon::setCurrentEffect(
Effect * currentEffect)
143 this->effect = currentEffect;
146void Cogs::ContextCommon::updateConstantBuffers()
150 auto updateConstantBuffer = [&](ShaderConstantBuffer & constantBuffer) {
151 if (!constantBuffer.dirty || constantBuffer.manual)
return;
156 std::memcpy(mappedBuffer, constantBuffer.memoryBuffer.data(), constantBuffer.memoryBuffer.size());
158 constantBuffer.dirty =
false;
161 for (
auto & shader : effect->shaders) {
162 for (
auto & constantBuffer : shader.reflection.constantBuffers) {
164 updateConstantBuffer(constantBuffer);
174 std::memcpy(mappedBuffer, data, size);
177 LOG_ERROR_ONCE(logger,
"updateBuffer: Faild to map buffer");
183 setVariable(name,
reinterpret_cast<const unsigned char *
>(value),
sizeof(
float) * 16);
188 if (!HandleIsValid(handle))
return;
192 for (
auto & variable : effectVariable->variables) {
193 std::memcpy(variable.buffer->memoryBuffer.data() + variable.offset, data, size);
194 variable.buffer->dirty =
true;
197 constantBuffersUpdated =
false;
200void Cogs::ContextCommon::setVariable(
const StringView& name,
const uint8_t * data,
size_t size)
202 setVariable(getEffects()->getEffectVariable(getCurrentEffect(), name), data, size);
207 LOG_ERROR_ONCE(logger,
"Rendering context does not support vertex array objects.");
Log implementation class.
Provides a weakly referenced view over the contents of a string.
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
void setVector4Variable(const StringView &name, const float *value) override
Sets the vector variable with the given name to the given four-component value.
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.
void setScalarVariable(const StringView &name, const float value) override
Sets the scalar floating point variable with the given name to the given value.
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.
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.
@ WriteDiscard
Write access. When unmapping the graphics system will discard the old contents of the resource.
Provides RAII style mapping of a buffer resource.