Cogs.Core
Texture.h
1#pragma once
2
3#include "ResourceBase.h"
4
5#include "Rendering/TextureData.h"
6
7#include "Foundation/UniqueValue.h"
8
9#include <glm/glm.hpp>
10
11namespace Cogs
12{
13 namespace Core
14 {
15 template<typename Element>
16 inline TextureFormat getFormat() { return TextureFormat::Unknown; }
17
18 template<>
19 inline TextureFormat getFormat<glm::u8vec4>() { return TextureFormat::R8G8B8A8_UNORM; }
20
21 template<>
22 inline TextureFormat getFormat<glm::vec3>() { return TextureFormat::R32G32B32_FLOAT; }
23
24 template<>
25 inline TextureFormat getFormat<glm::vec4>() { return TextureFormat::R32G32B32A32_FLOAT; }
26
27 struct Texture;
28
32 template<typename Element>
34 {
35 public:
43
45 MappedTexture(MappedTexture & other) = delete;
46
48 MappedTexture(MappedTexture && other) = default;
49
55
57 Element & operator[](const size_t index)
58 {
59 return elements[index];
60 }
61
63 explicit operator Element *() { return data; }
64
66 Element * data() { return elements; }
67
68 private:
70 Element * elements;
71
74 };
75
76
77
78
90 struct Texture : public ResourceBase
91 {
93 Texture() = default;
94
96 Texture(const Texture & other) = delete;
97
104 Texture(Texture && other) = default;
105
109 ~Texture();
110
118 Texture & operator=(Texture && other) = default;
119
129 MappedTexture<uint8_t> map(uint16_t width, uint16_t height, TextureFormat format, bool generateMipMap)
130 {
131 return MappedTexture<uint8_t>(this, static_cast<uint8_t *>(mapInternal(width, height, format, generateMipMap)));
132 }
133
144 template<typename Element>
145 MappedTexture<Element> map(uint16_t width, uint16_t height, bool generateMipMap)
146 {
147 auto format = getFormat<Element>();
148
149 assert(Cogs::getBlockSize(format) == sizeof(Element) && "Compatible format not found for element type.");
150
151 return MappedTexture<Element>(this, static_cast<Element *>(mapInternal(width, height, format, generateMipMap)));
152 }
153
166 template<typename Element>
167 MappedTexture<Element> map(uint16_t width, uint16_t height, TextureFormat format, bool generateMipMap)
168 {
169 assert(Cogs::getBlockSize(format) == sizeof(Element) && "Format not size compatible with element type.");
170
171 return MappedTexture<Element>(this, static_cast<Element *>(mapInternal(width, height, format, generateMipMap)));
172 }
173
177 void unmap()
178 {
179 setChanged();
180 }
181
185 void setData(ResourceDimensions target, const void * data, size_t size, int width, int height, TextureFormat format, bool generateMipMap);
186
187 void setData(ResourceDimensions target, const void * data, size_t size, int width, int height, int levels, TextureFormat format, bool generateMipMap);
188
189 void COGSCORE_DLL_API setData(ResourceDimensions target, const void * data, size_t size, uint32_t width, uint32_t height, uint32_t depth, uint32_t layers, uint32_t faces, uint32_t levels, TextureFormat format, bool generateMipMap);
190
191 void clearData();
192
193 bool empty() { return storage.data.size() == 0; }
194
195 void * mapInternal(uint16_t width, uint16_t height, TextureFormat format, bool mipMap);
196
197 TextureDescription description;
198 TextureData storage;
199 intptr_t externalHandle = 0;
200 UniqueValue<bool> ownsExternalTexture{ false };
201 bool hasAlpha = false;
202 };
203
204 template<typename Element>
206 {
207 if (texture) {
208 texture->unmap();
209 }
210 }
211 }
212}
Wrapper for mapped texture data, ensuring RAII behavior of stream map/unmap operations.
Definition: Texture.h:34
~MappedTexture()
Destructs the texture wrapper.
Definition: Texture.h:205
MappedTexture(MappedTexture &other)=delete
Deleted copy constructor.
Element * elements
Pointer to the mapped data.
Definition: Texture.h:70
MappedTexture(Texture *texture, Element *data)
Constructs a texture data wrapper.
Definition: Texture.h:42
Element & operator[](const size_t index)
Access the element at offset index.
Definition: Texture.h:57
MappedTexture(MappedTexture &&other)=default
Move construct the wrapper from other.
Element * data()
Get the pointer to the mapped data.
Definition: Texture.h:66
Texture * texture
Pointer to the texture the data was mapped from.
Definition: Texture.h:73
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
Base class for engine resources.
Definition: ResourceBase.h:107
Texture resources contain raster bitmap data to use for texturing.
Definition: Texture.h:91
MappedTexture< Element > map(uint16_t width, uint16_t height, TextureFormat format, bool generateMipMap)
Map the texture data, ensuring the data is sized to hold width * height * sizeof(Element) bytes.
Definition: Texture.h:167
Texture()=default
Construct a texture resource.
MappedTexture< Element > map(uint16_t width, uint16_t height, bool generateMipMap)
Map the texture data, ensuring the data is sized to hold width * height * sizeof(Element) bytes.
Definition: Texture.h:145
Texture(Texture &&other)=default
Move construct a texture from the given texture other.
MappedTexture< uint8_t > map(uint16_t width, uint16_t height, TextureFormat format, bool generateMipMap)
Map the texture data, ensuring the data is sized to hold width * height * bpp of the format bytes.
Definition: Texture.h:129
void unmap()
Unmap the currently mapped texture data.
Definition: Texture.h:177
Texture & operator=(Texture &&other)=default
Move assign a texture from the given texture other.
void setData(ResourceDimensions target, const void *data, size_t size, int width, int height, TextureFormat format, bool generateMipMap)
Set the texture data.
Definition: Texture.cpp:54
~Texture()
Destroy a texture.
Definition: Texture.cpp:47
Texture(const Texture &other)=delete
Copying disabled since copying a texture resource might be a resource-intensive operation.
A value that is cleared when it is moved from.
Definition: UniqueValue.h:13