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 if(format == TextureFormat::B8G8R8 ||
77 format == TextureFormat::R8G8B8T ||
78 format == TextureFormat::R8G8B8_SINT ||
79 format == TextureFormat::R8G8B8_UINT ||
80 format == TextureFormat::R8G8B8_SNORM ||
81 format == TextureFormat::R8G8B8_UNORM ||
82 format == TextureFormat::R8G8B8_UNORM_SRGB){
83 LOG_WARNING_ONCE(logger, "Using R8G8B8_... texture format. Consider padding this out to R8G8B8A8_... for better platform support and performance.");
84 }
85 debug_assert(layers >= 1 && "Invalid number of layers.");
86 debug_assert(faces == 1 || faces == 6 && "Invalid number of faces.");
87 debug_assert(levels >= 1 && "Invalid number of levels.");
88
89 description.target = target;
90 description.width = width;
91 description.height = height;
92 description.depth = depth;
93 description.layers = layers;
94 description.faces = faces;
95 description.levels = levels;
96 description.format = format;
98 if (generateMipMap) {
99 description.flags |= TextureFlags::GenerateMipMaps;
100 }
101 if (faces == 6) {
102 description.flags |= TextureFlags::CubeMap;
103 }
104
105 if(data){
106 storage.init({ width, height, depth }, layers, faces, levels, format, &textureAllocator);
107
108 auto textureData = storage.getData();
109
110 std::memcpy(textureData, data, size);
111 }
112
113 if (!isSet(ResourceFlags::Loading)) {
114 setChanged();
115 }
116}
117
118void * Cogs::Core::Texture::mapInternal(uint16_t width, uint16_t height, TextureFormat format, bool generateMipMap)
119{
120 description.width = width;
121 description.height = height;
122 description.format = format;
124 if (generateMipMap) {
125 description.flags |= TextureFlags::GenerateMipMaps;
126 }
127
128 storage.init({ width, height, 1 }, 1, 1, 1, format, &textureAllocator);
129
130 std::memset(storage.getData(), 0, storage.getSize());
131
132 return storage.getData();
133}
134
135void Cogs::Core::Texture::clearData(){
136 if (!keepStorage()) {
137 storage.clearData();
138 }
139}
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:140
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:181
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