FastEngine 0.9.4
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_textureImage.hpp
1/*
2 * Copyright 2025 Guillaume Guillet
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _FGE_VULKAN_C_TEXTUREIMAGE_HPP_INCLUDED
18#define _FGE_VULKAN_C_TEXTUREIMAGE_HPP_INCLUDED
19
20#include "FastEngine/fge_extern.hpp"
21#include "FastEngine/C_rect.hpp"
22#include "FastEngine/C_vector.hpp"
23#include "FastEngine/vulkan/C_contextAware.hpp"
24#include "FastEngine/vulkan/C_descriptorSet.hpp"
25#include "FastEngine/vulkan/vulkanGlobal.hpp"
26#include "SDL_surface.h"
27
28#define FGE_TEXTURE_IMAGE_MIPMAPS_LEVELS_AUTO 0
29#define FGE_TEXTURE_IMAGE_BYTESPERPIXEL 4
30#define FGE_TEXTURE_IMAGE_FORMAT VK_FORMAT_R8G8B8A8_UNORM
31
32namespace fge::vulkan
33{
34
35class FGE_API TextureImage : public ContextAware
36{
37public:
38 explicit TextureImage(Context const& context);
39 TextureImage(TextureImage const& r);
40 TextureImage(TextureImage&& r) noexcept;
41 ~TextureImage() override;
42
43 TextureImage& operator=(TextureImage const& r) = delete;
44 TextureImage& operator=(TextureImage&& r) noexcept;
45
46 bool create(glm::vec<2, int> const& size, uint32_t levels = 1);
47 bool create(SDL_Surface* surface, uint32_t levels = 1);
48 bool create(TextureImage const& texture, uint32_t levels = FGE_TEXTURE_IMAGE_MIPMAPS_LEVELS_AUTO);
49 void destroy() final;
50 [[nodiscard]] bool isCreated() const;
51
52 [[nodiscard]] SDL_Surface* copyToSurface() const;
53
54 void update(SDL_Surface* surface, glm::vec<2, int> const& offset);
55 void update(TextureImage const& textureImage, glm::vec<2, int> const& offset);
56 void update(void* buffer, std::size_t bufferSize, glm::vec<2, int> const& size, glm::vec<2, int> const& offset);
57
58 void generateMipmaps(uint32_t levels = FGE_TEXTURE_IMAGE_MIPMAPS_LEVELS_AUTO);
59 [[nodiscard]] uint32_t getMipLevels() const;
60 void forceMipLod(float mipLodBias, float mipLodMin, float mipLodMax);
61
62 [[nodiscard]] glm::vec<2, int> const& getSize() const;
63 [[nodiscard]] VkExtent2D getExtent() const;
64 [[nodiscard]] int getBytesPerPixel() const;
65
66 [[nodiscard]] VkImage getTextureImage() const;
67 [[nodiscard]] VmaAllocation getTextureImageAllocation() const;
68
69 [[nodiscard]] VkImageView getTextureImageView() const;
70 [[nodiscard]] VkSampler getTextureSampler() const;
71
72 void setNormalizedCoordinates(bool normalized);
73 [[nodiscard]] bool getNormalizedCoordinates() const;
74
75 void setFilter(VkFilter filter);
76 [[nodiscard]] VkFilter getFilter() const;
77
78 [[nodiscard]] fge::vulkan::DescriptorSet const& getDescriptorSet() const;
79
80 [[nodiscard]] fge::Vector2f normalizeTextureCoords(fge::Vector2i const& coords) const;
81 [[nodiscard]] fge::RectFloat normalizeTextureRect(fge::RectInt const& rect) const;
82
83 [[nodiscard]] uint32_t getModificationCount() const;
84
85 [[nodiscard]] static uint32_t computeMipLevels(glm::vec<2, int> size);
86
87private:
88 void createTextureSampler(float mipLodBias, float mipLodMin, float mipLodMax);
89
90 ImageInfo g_imageInfo;
91
92 VkImageView g_textureImageView;
93 VkSampler g_textureSampler;
94
95 glm::vec<2, int> g_textureSize;
96 int g_textureBytesPerPixel;
97
98 VkFilter g_filter;
99 bool g_normalizedCoordinates;
100
101 fge::vulkan::DescriptorSet g_textureDescriptorSet;
102
103 uint32_t g_mipLevels;
104 uint32_t g_modificationCount;
105};
106
107} // namespace fge::vulkan
108
109#endif //_FGE_VULKAN_C_TEXTUREIMAGE_HPP_INCLUDED
Vulkan context.
Definition C_context.hpp:70
This class abstract the vulkan descriptor set for easier use.
Definition C_descriptorSet.hpp:41
Definition vulkanGlobal.hpp:60