Skip to content

Commit 6d208fc

Browse files
committed
Added ability to configure texture filter on construction
1 parent 9f26bba commit 6d208fc

File tree

15 files changed

+75
-31
lines changed

15 files changed

+75
-31
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ export compileFlags := -Wall -std=c++17
5454
export linkFlags += -L $(libDir)
5555
buildFlagsFile:=.buildflags
5656

57-
.PHONY: all testapp gameapp renderapp tilemapapp package-gameapp package-renderapp package-tilemap buildFlags clean format
57+
.PHONY: all testapp gameapp renderapp tilemapapp package-gameapp package-renderapp package-tilemapapp buildFlags clean format
5858

59-
all: testapp package-gameapp package-renderapp package-tilemap
59+
all: testapp package-gameapp package-renderapp package-tilemapapp
6060

6161
$(utilsLib): buildFlags
6262
"$(MAKE)" -C $(engineDir)/utils CXXFLAGS="$(CXXFLAGS)"
@@ -96,7 +96,7 @@ package-gameapp: gameapp
9696
package-renderapp: renderapp
9797
"$(MAKE)" package -C $(examplesDir)/render CXXFLAGS="$(CXXFLAGS)"
9898

99-
package-tilemap: tilemapapp
99+
package-tilemapapp: tilemapapp
100100
"$(MAKE)" package -C $(examplesDir)/tilemap CXXFLAGS="$(CXXFLAGS)"
101101

102102
# Check to invalidate the build if flags have changed

engine/render/renderer/Renderer.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ Renderer::Renderer(Window& window) : window {window}
4848
Renderer::~Renderer()
4949
{
5050
CC_LOG_INFO("Destroying renderer")
51-
Vulkan::Context::GetCurrentDevice()->WaitIdle();
5251
DescriptorPool::DestroyPool();
5352
Renderer3D::DestroyRenderer3D();
5453
Statics::Free();
@@ -62,7 +61,7 @@ void Renderer::DrawFrame()
6261

6362
void Renderer::RecreateSwapChain()
6463
{
65-
ClearDeviceQueue();
64+
ClearQueues();
6665
auto extent = window.GetExtents();
6766

6867
while (!window.IsVisible() || extent.width == 0.0 || extent.height == 0.0)
@@ -176,7 +175,7 @@ void Renderer::EndFrame()
176175
renderer2D.Flush();
177176
}
178177

179-
void Renderer::ClearDeviceQueue()
178+
void Renderer::ClearQueues()
180179
{
181180
Vulkan::Context::GetCurrentDevice()->WaitIdle();
182181
}

engine/render/renderer/Renderer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Renderer
4545
bool StartFrame();
4646
void EndFrame();
4747

48-
void ClearDeviceQueue();
48+
void ClearQueues();
4949

5050
void DrawQuad(const Vec2 position,
5151
const Vec2 scale = {1.f, 1.f},

engine/render/renderer/platform/vulkan/Font.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Font::Font(const char* filePath)
4242
sizeof(uint8_t) * extent.width * extent.height,
4343
extent.width,
4444
extent.height,
45+
Utils::TEXTURE_FILTER_LINEAR,
4546
Texture2D::Usage::TEX_USAGE_FONT);
4647

4748
PopulateTextureAtlas(buffer);

engine/render/renderer/platform/vulkan/Texture2D.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "Context.h"
1414
#include "render/renderer/buffer/Buffer.h"
1515
#include "utils/Descriptor.h"
16+
#include "utils/TypeAdaptor.h"
1617

1718
#define STB_IMAGE_IMPLEMENTATION
1819
#include <stb_image.h>
@@ -21,11 +22,12 @@
2122

2223
namespace Siege::Vulkan
2324
{
24-
Texture2D::Texture2D(const char* name, Usage texUsage)
25+
Texture2D::Texture2D(const char* name, Utils::TextureFilter filter, Usage texUsage)
2526
{
2627
LoadTexture(Constants::DEFAULT_TEXTURE_2D, Constants::DEFAULT_TEXTURE_SIZE, 16, 16, texUsage);
2728

28-
VkSamplerCreateInfo samplerInfo = Utils::Descriptor::SamplerCreateInfo(VK_FILTER_LINEAR);
29+
VkSamplerCreateInfo samplerInfo =
30+
Utils::Descriptor::SamplerCreateInfo(Utils::ToVkFilter(filter));
2931

3032
info = {image.GetInfo()};
3133

@@ -37,11 +39,12 @@ Texture2D::Texture2D(const char* name, Usage texUsage)
3739
info.usage = texUsage;
3840
}
3941

40-
Texture2D::Texture2D(const char* name, const char* filePath)
42+
Texture2D::Texture2D(const char* name, const char* filePath, Utils::TextureFilter filter)
4143
{
4244
LoadFromFile(filePath);
4345

44-
VkSamplerCreateInfo samplerInfo = Utils::Descriptor::SamplerCreateInfo(VK_FILTER_LINEAR);
46+
VkSamplerCreateInfo samplerInfo =
47+
Utils::Descriptor::SamplerCreateInfo(Utils::ToVkFilter(filter));
4548

4649
info = {image.GetInfo()};
4750

@@ -55,11 +58,13 @@ Texture2D::Texture2D(const char* name,
5558
size_t size,
5659
uint32_t width,
5760
uint32_t height,
61+
Utils::TextureFilter filter,
5862
Usage texUsage)
5963
{
6064
LoadTexture(pixels, size, width, height, texUsage);
6165

62-
VkSamplerCreateInfo samplerInfo = Utils::Descriptor::SamplerCreateInfo(VK_FILTER_LINEAR);
66+
VkSamplerCreateInfo samplerInfo =
67+
Utils::Descriptor::SamplerCreateInfo(Utils::ToVkFilter(filter));
6368

6469
info = {image.GetInfo()};
6570

@@ -91,6 +96,10 @@ void Texture2D::LoadFromFile(const char* filePath)
9196
int texWidth, texHeight, texChannels;
9297

9398
stbi_uc* pixels = stbi_load(filePath, &texWidth, &texHeight, &texChannels, STBI_rgb_alpha);
99+
defer([pixels] {
100+
stbi_image_free(pixels);
101+
;
102+
});
94103

95104
CC_ASSERT(pixels, "Failed to load image file!")
96105

@@ -104,6 +113,8 @@ void Texture2D::LoadFromFile(const char* filePath)
104113
memcpy(pixelPtr, pixels, sizeof(uint8_t) * imageSize);
105114

106115
Buffer::Buffer stagingBuffer;
116+
defer([&stagingBuffer] { Buffer::DestroyBuffer(stagingBuffer); });
117+
107118
Buffer::CreateBuffer(imageSize,
108119
VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
109120
// specifies that data is accessible on the CPU.
@@ -115,8 +126,6 @@ void Texture2D::LoadFromFile(const char* filePath)
115126

116127
Buffer::CopyData(stagingBuffer, imageSize, pixelPtr);
117128

118-
stbi_image_free(pixels);
119-
120129
extent = {static_cast<uint32_t>(texWidth), static_cast<uint32_t>(texHeight)};
121130

122131
Utils::Extent3D imageExtent {static_cast<uint32_t>(texWidth),
@@ -125,8 +134,6 @@ void Texture2D::LoadFromFile(const char* filePath)
125134
image = Image({Utils::RGBASRGB, imageExtent, Vulkan::Utils::USAGE_TEXTURE, 1, 1});
126135

127136
image.CopyBuffer(stagingBuffer.buffer, imageExtent);
128-
129-
Buffer::DestroyBuffer(stagingBuffer);
130137
}
131138

132139
void Texture2D::LoadTexture(const uint8_t* pixels,

engine/render/renderer/platform/vulkan/Texture2D.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,26 @@ class Texture2D
5757
* default white texture
5858
* @param name the name of the texture being created
5959
*/
60-
Texture2D(const char* name, Usage texUsage = TEX_USAGE_TEX2D);
60+
Texture2D(const char* name,
61+
Utils::TextureFilter filter = Utils::TEXTURE_FILTER_LINEAR,
62+
Usage texUsage = TEX_USAGE_TEX2D);
6163

6264
/**
6365
* The texture constructor used for loading a texture from file. This loads data from a given
6466
* file path and stores pixel data in the class
6567
* @param name the name of the texture
6668
* @param filePath the path of the PNG or JPG file
6769
*/
68-
Texture2D(const char* name, const char* filePath);
70+
Texture2D(const char* name,
71+
const char* filePath,
72+
Utils::TextureFilter filter = Utils::TEXTURE_FILTER_LINEAR);
6973

7074
Texture2D(const char* name,
7175
const uint8_t* pixels,
7276
size_t size,
7377
uint32_t width,
7478
uint32_t height,
79+
Utils::TextureFilter filter = Utils::TEXTURE_FILTER_LINEAR,
7580
Usage texUsage = Usage::TEX_USAGE_TEX2D);
7681

7782
/**

engine/render/renderer/platform/vulkan/utils/TypeAdaptor.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,21 @@ DECL_VULKAN_SWITCH_FUN(VertexInputRate,
355355
VK_VERTEX_INPUT_RATE_INSTANCE,
356356
INPUT_RATE_INSTANCE) SWITCH_DEFAULT(INPUT_RATE_VERTEX))
357357

358+
DECL_VULKAN_SWITCH_FUN(VkFilter,
359+
TextureFilter,
360+
SWITCH_MEM(TextureFilter, TEXTURE_FILTER_NEAREST, VK_FILTER_NEAREST)
361+
SWITCH_MEM(TextureFilter, TEXTURE_FILTER_LINEAR, VK_FILTER_LINEAR)
362+
SWITCH_MEM(TextureFilter,
363+
TEXTURE_FILTER_MAX_ENUM,
364+
VK_FILTER_MAX_ENUM) SWITCH_DEFAULT(VK_FILTER_CUBIC_EXT))
365+
366+
DECL_VULKAN_SWITCH_FUN(TextureFilter,
367+
VkFilter,
368+
SWITCH_MEM(VkFilter, VK_FILTER_NEAREST, TEXTURE_FILTER_NEAREST)
369+
SWITCH_MEM(VkFilter, VK_FILTER_LINEAR, TEXTURE_FILTER_LINEAR)
370+
SWITCH_MEM(VkFilter, VK_FILTER_MAX_ENUM, TEXTURE_FILTER_MAX_ENUM)
371+
SWITCH_DEFAULT(TEXTURE_FILTER_CUBIC_EXT))
372+
358373
//----------------------------------------- Structs -----------------------------------------------
359374

360375
DECL_CONVERSION_FUN(VkExtent2D, Extent2D, {GET_MEMBER(width), GET_MEMBER(height)})

engine/render/renderer/platform/vulkan/utils/Types.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,15 @@ enum MemoryProperty
226226
MEMORY_HOST_VISIBLE = 0x00000004
227227
};
228228

229+
enum TextureFilter
230+
{
231+
TEXTURE_FILTER_NEAREST = 0,
232+
TEXTURE_FILTER_LINEAR = 1,
233+
TEXTURE_FILTER_CUBIC_EXT = 1000015000,
234+
TEXTURE_FILTER_CUBIC_IMG = TEXTURE_FILTER_CUBIC_EXT,
235+
TEXTURE_FILTER_MAX_ENUM = 0x7FFFFFFF
236+
};
237+
229238
//----------------------------------------- Structs -----------------------------------------------
230239

231240
struct Extent2D

engine/utils/Macros.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,4 @@
2525
#define STRINGIFY(x) #x
2626
#define TOSTRING(x) STRINGIFY(x)
2727

28-
// Macro for declaring a binary operator
29-
#define DECL_BINARY_OP_NO_IMPL(op, returnType, lhsType, rhsType) \
30-
returnType operator op(lhsType lhs, rhsType rhs) _SEMICOLON
31-
3228
#endif // SIEGE_ENGINE_MACROS_H

engine/utils/collections/ArrayUtils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
#include <cstdint>
1818
#include <cstring>
1919
#include <initializer_list>
20-
#include <utility>
2120
#include <iterator>
21+
#include <utility>
2222

2323
namespace Siege
2424
{

0 commit comments

Comments
 (0)