Cogs.Core
ContextCommon.h
1#pragma once
2
3#include "../IContext.h"
4
5#include "../VertexFormat.h"
6#include "../Statistics.h"
7
8#include "EffectsCommon.h"
9
10namespace Cogs
11{
12 const int kMaxVertexInputSlots = 32;
13
15 {
16 size_t numVertexBuffers = 0;
17 const VertexFormat * formats[kMaxVertexInputSlots] = {};
18 uint32_t strides[kMaxVertexInputSlots] = {};
19 uint32_t offsets[kMaxVertexInputSlots] = {};
20 };
21
22 struct ContextCommon : public IContext
23 {
24 virtual InputAssemblerStateCommon * getIAState() = 0;
25
26 virtual EffectHandle getCurrentEffect() = 0;
27 virtual IEffects * getEffects() = 0;
28
29 const Cogs::FrameStatistics& getLastFrameStatistics() override;
30 const Cogs::UploadStatistics& getLastUploadStatistics() override;
31
32 void frameStatisticsConfigure(bool enable) override { frameStatisticsEnabled = enable; }
33
34 void setMatrixVariable(const StringView& name, const float * value) override;
35
36 void setScalarVariable(const StringView& name, const float value) override {
37 this->setVariable(name, reinterpret_cast<const unsigned char *>(&value), sizeof(float));
38 }
39
40 void setScalarVariable(const StringView& name, int value) override {
41 this->setVariable(name, reinterpret_cast<const unsigned char *>(&value), sizeof(int));
42 }
43
44 void setVector2Variable(const StringView& name, const float * value) override {
45 this->setVariable(name, reinterpret_cast<const unsigned char *>(value), sizeof(float) * 2);
46 }
47
48 void setVector3Variable(const StringView& name, const float * value) override {
49 this->setVariable(name, reinterpret_cast<const unsigned char *>(value), sizeof(float) * 3);
50 }
51
52 void setVector4Variable(const StringView& name, const float * value) override {
53 this->setVariable(name, reinterpret_cast<const unsigned char *>(value), sizeof(float) * 4);
54 }
55
56 void setVariable(const StringView& name, const uint8_t * data, size_t size);
57 void setVariable(const EffectVariableHandle index, const uint8_t * data, size_t size) override;
58
59 void setMatrixVariable(const EffectVariableHandle variableHandle, const float * value) override;
60 void setMatrixVariable(const EffectVariableHandle variableHandle, const float * value, size_t count) override;
61
62 void setScalarVariable(const EffectVariableHandle variableHandle, const float value) override;
63 void setScalarVariable(const EffectVariableHandle variableHandle, const float* value, size_t count) override;
64 void setScalarVariable(const EffectVariableHandle variableHandle, int value) override;
65 void setScalarVariable(const EffectVariableHandle variableHandle, const int* value, size_t count) override;
66
67 void setVector2Variable(const EffectVariableHandle variableHandle, const float * value, size_t count = 1) override;
68 void setVector3Variable(const EffectVariableHandle variableHandle, const float * value, size_t count = 1) override;
69 void setVector4Variable(const EffectVariableHandle variableHandle, const float * value, size_t count = 1) override;
70
71 void setVector4Variable(const EffectVariableHandle variableHandle, const int * value, size_t count = 1) override;
72
73 void setConstantBuffer(const StringView& name, const BufferHandle bufferHandle, const uint32_t offset, const uint32_t size) final;
74 void setBuffer(const StringView& name, BufferHandle bufferHandle) final;
75 void setTexture(const StringView& name, unsigned int unit, TextureHandle textureHandle) final;
76 void setTexture(const StringView& name, TextureViewHandle textureViewHandle) final;
77 void setSamplerState(const StringView& name, unsigned int unit, SamplerStateHandle handle) final;
78
83
85 void setVertexArrayObject(VertexArrayObjectHandle vertexArrayObject) override;
86
87 virtual void signal(FenceHandle /*fenceHandle*/) override {}
88
89 void updateBuffer(BufferHandle bufferHandle, const void* data, size_t size) override;
90
96
97 protected:
98 void setCurrentEffect(Effect * currentEffect);
99 void updateConstantBuffers();
100
101 // Invoked by draw call to account for submission.
102 void frameStatisticsAccountDrawCall(size_t count, bool indexed);
103
104 // Invoked by upload calls
105 void uploadStatisticsBufferUpload(size_t size);
106 void uploadStatisticsTextureUpload(size_t size);
107
108 FrameStatistics prevStats{};
109 FrameStatistics currStats{};
110
111 UploadStatistics prevUploadStats{};
112 UploadStatistics currUploadStats{};
113
115 {
116 uint32_t dirtySRV[(int)Cogs::ShaderType::NumShaderSlots] = {};
117 uint32_t dirtyCB[(int)Cogs::ShaderType::NumShaderSlots] = {};
118 BufferHandle vsCBs[255];
119 BufferHandle psCBs[255];
120 BufferHandle csCBs[255];
122 } state;
123
124 Effect * effect = nullptr;
125 bool constantBuffersUpdated = true;
126 bool frameStatisticsEnabled = false;
127 bool uploadStatisticsEnabled = true;
128
129 friend struct BuffersD3D11;
130 friend struct TexturesD3D11;
131 };
132}
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
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 setScalarVariable(const StringView &name, int value) override
Sets the scalar integer variable with the given name to the given value.
Definition: ContextCommon.h:40
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.
virtual void signal(FenceHandle) override
Insert a fence in the command stream that will signal when all commands before the fence are complete...
Definition: ContextCommon.h:87
Represents a graphics device context which can receive rendering commands.
Definition: IContext.h:43
virtual void setTexture(const StringView &name, unsigned int unit, TextureHandle textureHandle)=0
Sets the texture slot given by unit with the given name to contain the given texture.
virtual void setConstantBuffer(const StringView &name, const BufferHandle bufferHandle, const uint32_t offset=0, const uint32_t size=~0u)=0
Sets a constant buffer to be bound to the given name and slot.
virtual void setBuffer(const StringView &name, BufferHandle bufferHandle)=0
Sets the given buffer to the buffer binding slot with the given name.
virtual void setSamplerState(const StringView &name, unsigned int unit, SamplerStateHandle samplerStateHandle)=0
Sets the sampler slot given by unit with the given name to contain the given sampler state.
Provides effects and shader management functionality.
Definition: IEffects.h:148
EPrimitiveType
Primitive type enumeration.
Definition: Common.h:114
Vertex format structure used to describe a single vertex for the input assembler.
Definition: VertexFormat.h:60