Cogs.Core
MaterialPropertyBuffer.h
1#pragma once
2
3#include "Resources.h"
4#include "Utilities/Strings.h"
5
6#include <algorithm>
7
8#include <glm/glm.hpp>
9
10#include <string>
11#include <vector>
12
13namespace Cogs::Core
14{
15 class Context;
16
18 typedef std::string PropertyName;
19
22 {
23 size_t offset = 0;
24 size_t size = 0;
25 };
26
42 {
55 COGSCORE_DLL_API MaterialPropertyDescriptor addProperty(size_t propertyAlignment, size_t propertySize, const uint8_t * data);
56
57 COGSCORE_DLL_API void finalize(Context* context);
58
59 // Overloads for supported property types.
60
62 {
63 uint32_t value = data ? 1 : 0;
64 return addProperty(sizeof(uint32_t), sizeof(uint32_t), reinterpret_cast<const uint8_t *>(&value));
65 }
66
67 MaterialPropertyDescriptor addProperty(const float & data)
68 {
69 return addProperty(sizeof(float), sizeof(float), reinterpret_cast<const uint8_t *>(&data));
70 }
71
73 {
74 return addProperty(sizeof(int), sizeof(int), reinterpret_cast<const uint8_t *>(&data));
75 }
76
77 MaterialPropertyDescriptor addProperty(const glm::ivec2 & data)
78 {
79 return addProperty(sizeof(glm::ivec2), sizeof(glm::ivec2), reinterpret_cast<const uint8_t *>(&data));
80 }
81
82 MaterialPropertyDescriptor addProperty(const glm::ivec3 & data)
83 {
84 return addProperty(sizeof(glm::ivec4), sizeof(glm::ivec3), reinterpret_cast<const uint8_t *>(&data));
85 }
86
87 MaterialPropertyDescriptor addProperty(const glm::ivec4 & data)
88 {
89 return addProperty(sizeof(glm::ivec4), sizeof(glm::ivec4), reinterpret_cast<const uint8_t *>(&data));
90 }
91
92 MaterialPropertyDescriptor addProperty(const glm::vec2 & data)
93 {
94 return addProperty(sizeof(glm::vec2), sizeof(glm::vec2), reinterpret_cast<const uint8_t *>(&data));
95 }
96
97 MaterialPropertyDescriptor addProperty(const glm::vec3 & data)
98 {
99 return addProperty(sizeof(glm::vec4), sizeof(glm::vec3), reinterpret_cast<const uint8_t *>(&data));
100 }
101
102 MaterialPropertyDescriptor addProperty(const glm::vec4 & data)
103 {
104 return addProperty(sizeof(glm::vec4), sizeof(glm::vec4), reinterpret_cast<const uint8_t *>(&data));
105 }
106
107 MaterialPropertyDescriptor addProperty(const glm::mat4 & data)
108 {
109 return addProperty(sizeof(glm::vec4), sizeof(glm::mat4), reinterpret_cast<const uint8_t *>(&data));
110 }
111
112 MaterialPropertyDescriptor addProperty(const uint32_t & data)
113 {
114 return addProperty(sizeof(uint32_t), sizeof(uint32_t), reinterpret_cast<const uint8_t *>(&data));
115 }
116
117 MaterialPropertyDescriptor addProperty(const glm::uvec2 & data)
118 {
119 return addProperty(sizeof(glm::uvec2), sizeof(glm::uvec2), reinterpret_cast<const uint8_t *>(&data));
120 }
121
122 MaterialPropertyDescriptor addProperty(const glm::uvec3 & data)
123 {
124 return addProperty(sizeof(glm::uvec4), sizeof(glm::uvec3), reinterpret_cast<const uint8_t *>(&data));
125 }
126
127 MaterialPropertyDescriptor addProperty(const glm::uvec4 & data)
128 {
129 return addProperty(sizeof(glm::uvec4), sizeof(glm::uvec4), reinterpret_cast<const uint8_t *>(&data));
130 }
131
140 COGSCORE_DLL_API void setValue(size_t offset, size_t propertySize, const uint8_t * data);
141
142 void setValue(const MaterialPropertyDescriptor & descriptor, const uint8_t * data)
143 {
144 setValue(descriptor.offset, descriptor.size, data);
145 }
146
156 template<typename T>
157 void setValue(size_t offset, const T & value)
158 {
159 setValue(offset, sizeof(T), reinterpret_cast<const uint8_t *>(&value));
160 }
161
171 template<typename T>
172 const T & getValue(size_t offset) const
173 {
174 return *reinterpret_cast<const T *>(&content[offset]);
175 }
176
178 std::vector<uint8_t> content;
179
181 ConstantBufferKey index;
182
185
187 size_t size = 0;
188
190 bool isPerInstance = true;
191
193 size_t generation = 1;
194 };
195
196 // Specialization for bool values, which needs some extra care.
197 template<>
198 inline void MaterialPropertyBuffer::setValue<bool>(size_t offset, const bool & boolValue)
199 {
200 uint32_t intValue = boolValue ? 1 : 0;
201 setValue(offset, sizeof(uint32_t), reinterpret_cast<const uint8_t *>(&intValue));
202 }
203
204 template<>
205 inline const bool & MaterialPropertyBuffer::getValue<bool>(size_t offset) const
206 {
207 static bool temp = false;
208 uint32_t intValue = getValue<uint32_t>(offset);
209 temp = intValue != 0;
210 return temp;
211 }
212
221 {
222
223 };
224}
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
std::string PropertyName
Typedef for property names.
Material property buffer instances are created from MaterialPropertyBuffers, and have the same set of...
Material property buffers contain properties for a material, such as color, reflectiveness or other v...
bool isPerInstance
If the buffer is updated per instance.
ConstantBufferKey index
Index of the buffer in the set of buffers owned by the same Material.
size_t size
Total size of the buffer in bytes.
COGSCORE_DLL_API void setValue(size_t offset, size_t propertySize, const uint8_t *data)
Set the property value for the property located at offset to that pointed to by data.
PropertyName name
Name used to refer to the buffer in shaders.
void setValue(size_t offset, const T &value)
Set the property value for the property located at offset to the value given.
std::vector< uint8_t > content
Default content for property buffer instances created from this buffer.
COGSCORE_DLL_API MaterialPropertyDescriptor addProperty(size_t propertyAlignment, size_t propertySize, const uint8_t *data)
Add the value pointed to by data, with the given propertySize, to the buffer.
const T & getValue(size_t offset) const
Get the property value of type T located at offset.
Contains location/size information for a property.