Cogs.Core
Texture.cpp
1#include "Texture.h"
2
3#include "Services/Variables.h"
4#include "Context.h"
5
6#include "Foundation/BitTwiddling/PowerOfTwo.h"
7#include "Foundation/Logging/Logger.h"
8
9namespace {
11}
12
13namespace Cogs
14{
15 namespace Core
16 {
18 {
19 public:
21 {
22#if 0
23 assert(!allocationSize && "Texture memory leaked.");
24#endif
25 }
26
27 void * allocate(size_t size, size_t alignment = 0, MemBlockType type = MemBlockType::Block) override
28 {
29 allocationSize += size;
30
31 return Memory::Allocator::allocate(size, alignment, type);
32 }
33
34 void deallocate(void * ptr, size_t size, MemBlockType type = MemBlockType::Block) override
35 {
36 allocationSize -= size;
37
38 Memory::Allocator::deallocate(ptr, size, type);
39 }
40
41 private:
42 Atomic<size_t> allocationSize{ 0 };
43 } textureAllocator;
44 }
45}
46
48{
49 if (ownsExternalTexture.value) {
50 assert(externalHandle == 0);
51 }
52}
53
54void Cogs::Core::Texture::setData(ResourceDimensions target, const void * data, size_t size, int width, int height, TextureFormat format, bool generateMipMap)
55{
56 setData(target, data, size, width, height, 1, format, generateMipMap);
57}
58
59void Cogs::Core::Texture::setData(ResourceDimensions target, const void * data, size_t size, int width, int height, int levels, TextureFormat format, bool generateMipMap)
60{
61 setData(target, data, size, width, height, 1, 1, 1, levels, format, generateMipMap);
62}
63
64void COGSCORE_DLL_API Cogs::Core::Texture::setData(ResourceDimensions target,
65 const void * data,
66 size_t size,
67 uint32_t width,
68 uint32_t height,
69 uint32_t depth,
70 uint32_t layers,
71 uint32_t faces,
72 uint32_t levels,
73 TextureFormat format,
74 bool generateMipMap)
75{
76 debug_assert(layers >= 1 && "Invalid number of layers.");
77 debug_assert(faces == 1 || faces == 6 && "Invalid number of faces.");
78 debug_assert(levels >= 1 && "Invalid number of levels.");
79
80 description.target = target;
81 description.width = width;
82 description.height = height;
83 description.depth = depth;
84 description.layers = layers;
85 description.faces = faces;
86 description.levels = levels;
87 description.format = format;
89 if (generateMipMap) {
90 description.flags |= TextureFlags::GenerateMipMaps;
91 }
92 if (faces == 6) {
93 description.flags |= TextureFlags::CubeMap;
94 }
95
96 if(data){
97 storage.init({ width, height, depth }, layers, faces, levels, format, &textureAllocator);
98
99 auto textureData = storage.getData();
100
101 std::memcpy(textureData, data, size);
102 }
103
104 if (!isSet(ResourceFlags::Loading)) {
105 setChanged();
106 }
107}
108
109void * Cogs::Core::Texture::mapInternal(uint16_t width, uint16_t height, TextureFormat format, bool generateMipMap)
110{
111 description.width = width;
112 description.height = height;
113 description.format = format;
115 if (generateMipMap) {
116 description.flags |= TextureFlags::GenerateMipMaps;
117 }
118
119 storage.init({ width, height, 1 }, 1, 1, 1, format, &textureAllocator);
120
121 std::memset(storage.getData(), 0, storage.getSize());
122
123 return storage.getData();
124}
125
126void Cogs::Core::Texture::clearData(){
127 if (!keepStorage()) {
128 storage.clearData();
129 }
130}
void * allocate(size_t size, size_t alignment=0, MemBlockType type=MemBlockType::Block) override
Allocate raw memory.
Definition: Texture.cpp:27
void deallocate(void *ptr, size_t size, MemBlockType type=MemBlockType::Block) override
Deallocate the memory block at the given pointer, with the given size.
Definition: Texture.cpp:34
Log implementation class.
Definition: LogManager.h:139
Base allocator implementation.
Definition: Allocator.h:30
virtual void deallocate(void *ptr, size_t size, MemBlockType type=MemBlockType::Block)
Deallocate the memory block at the given pointer, with the given size.
Definition: Allocator.cpp:58
virtual void * allocate(size_t size, size_t alignment=0, MemBlockType type=MemBlockType::Block)
Allocate raw memory.
Definition: Allocator.cpp:29
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Definition: LogManager.h:180
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
void setChanged(Cogs::Core::Context *context, Cogs::ComponentModel::Component *component, Reflection::FieldId fieldId)
Must be Called after changing a Component field. Mark field changed. Request engine update.
Definition: FieldSetter.h:25
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
@ GenerateMipMaps
The texture supports automatic mipmap generation performed by the graphics device.
Definition: Flags.h:124
@ Texture
Texture usage, see Default.
Definition: Flags.h:118
@ CubeMap
The texture can be used as a cube map.
Definition: Flags.h:126
@ Default
Default usage, the texture can be loaded once and bound and sampled in shaders.
Definition: Flags.h:116