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::setConstantBuffer(const StringView& name, const BufferHandle bufferHandle, const uint32_t offset, const uint32_t size)
69{
70 setConstantBuffer(getEffects()->getConstantBufferBinding(getCurrentEffect(), name), bufferHandle, offset, size);
71}
73{
74 auto binding = getEffects()->getBufferBinding(getCurrentEffect(), name);
75 setBuffer(binding, bufferHandle);
76 getEffects()->releaseBufferBinding(binding);
77}
78void Cogs::ContextCommon::setTexture(const StringView& name, unsigned int unit, TextureHandle textureHandle)
79{
80 setTexture(getEffects()->getTextureBinding(getCurrentEffect(), name, unit), textureHandle);
81}
82void Cogs::ContextCommon::setTexture(const StringView& name, TextureViewHandle textureViewHandle)
83{
84 setTexture(getEffects()->getTextureBinding(getCurrentEffect(), name, 0), textureViewHandle);
85}
86void Cogs::ContextCommon::setSamplerState(const StringView& name, unsigned int unit, SamplerStateHandle handle)
87{
88 setSamplerState(getEffects()->getSamplerStateBinding(getCurrentEffect(), name, unit), handle);
89}
90
91void Cogs::ContextCommon::setCurrentEffect(Effect * currentEffect)
92{
93 this->effect = currentEffect;
94}
95
96void Cogs::ContextCommon::updateConstantBuffers()
97{
98 if (!effect) return;
99
100 auto updateConstantBuffer = [&](ShaderConstantBuffer & constantBuffer) {
101 if (!constantBuffer.dirty || constantBuffer.manual) return;
102
103 MappedBuffer<uint8_t> mappedBuffer(this, constantBuffer.buffer, MapMode::WriteDiscard);
104
105 if (mappedBuffer) {
106 std::memcpy(mappedBuffer, constantBuffer.memoryBuffer.data(), constantBuffer.memoryBuffer.size());
107 }
108 constantBuffer.dirty = false;
109 };
110
111 for (auto & shader : effect->shaders) {
112 for (auto & constantBuffer : shader.reflection.constantBuffers) {
113 if (HandleIsValid(constantBuffer.buffer)) {
114 updateConstantBuffer(constantBuffer);
115 }
116 }
117 }
118}
119
120void Cogs::ContextCommon::updateBuffer(BufferHandle bufferHandle, const void* data, size_t size)
121{
122 MappedBuffer<uint8_t> mappedBuffer(this, bufferHandle, MapMode::WriteDiscard);
123 if (mappedBuffer) {
124 std::memcpy(mappedBuffer, data, size);
125 }
126 else {
127 LOG_ERROR_ONCE(logger, "updateBuffer: Faild to map buffer");
128 }
129}
130
132{
133 LOG_ERROR_ONCE(logger, "Rendering context does not support vertex array objects.");
134}
Log implementation class.
Definition: LogManager.h:140
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:181
void frameStatisticsBeginFrame()
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.
@ 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