forked from Shell4026/ShellEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderTexture.cpp
More file actions
143 lines (126 loc) · 4.17 KB
/
Copy pathRenderTexture.cpp
File metadata and controls
143 lines (126 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "pch.h"
#include "RenderTexture.h"
#include "Renderer.h"
#include "VulkanRenderer.h"
#include "VulkanTextureBuffer.h"
#include "VulkanImpl/VulkanFramebuffer.h"
namespace sh::render
{
RenderTexture::RenderTexture(Texture::TextureFormat format) :
Texture(format, 1024, 1024),
width(1024), height(1024)
{
}
RenderTexture::~RenderTexture()
{
}
RenderTexture::RenderTexture(RenderTexture&& other) noexcept :
Texture(std::move(other)),
width(other.width), height(other.height),
framebuffer(std::move(other.framebuffer))
{
}
SH_RENDER_API void RenderTexture::SetReadUsage(bool bReadUsage) noexcept
{
this->bReadUsage = bReadUsage;
}
SH_RENDER_API void RenderTexture::Build(Renderer& renderer)
{
this->renderer = &renderer;
if (renderer.apiType == RenderAPI::Vulkan)
{
auto& vkRenderer = static_cast<const vk::VulkanRenderer&>(renderer);
framebuffer[core::ThreadType::Game] = std::make_unique<vk::VulkanFramebuffer>(vkRenderer.GetDevice(), vkRenderer.GetGPU(), vkRenderer.GetAllocator());
framebuffer[core::ThreadType::Render] = std::make_unique<vk::VulkanFramebuffer>(vkRenderer.GetDevice(), vkRenderer.GetGPU(), vkRenderer.GetAllocator());
VkFormat format = VkFormat::VK_FORMAT_R8G8B8A8_SRGB;
switch (this->format)
{
case Texture::TextureFormat::SRGB24:
format = VkFormat::VK_FORMAT_R8G8B8_SRGB;
break;
case Texture::TextureFormat::RGB24:
format = VkFormat::VK_FORMAT_R8G8B8_UNORM;
break;
case Texture::TextureFormat::RGBA32:
format = VkFormat::VK_FORMAT_R8G8B8A8_UNORM;
break;
}
static_cast<vk::VulkanFramebuffer*>(framebuffer[core::ThreadType::Game].get())->CreateOffScreen(width, height, format, bReadUsage);
static_cast<vk::VulkanFramebuffer*>(framebuffer[core::ThreadType::Render].get())->CreateOffScreen(width, height, format, bReadUsage);
}
buffer[core::ThreadType::Game] = std::make_unique<vk::VulkanTextureBuffer>();
buffer[core::ThreadType::Game]->Create(*framebuffer[core::ThreadType::Game].get());
buffer[core::ThreadType::Render] = std::make_unique<vk::VulkanTextureBuffer>();
buffer[core::ThreadType::Render]->Create(*framebuffer[core::ThreadType::Render].get());
}
SH_RENDER_API auto RenderTexture::GetFramebuffer(core::ThreadType thr) const -> Framebuffer*
{
return framebuffer[thr].get();
}
SH_RENDER_API auto RenderTexture::GetPixelData() const -> const std::vector<Byte>&
{
assert(renderer->apiType == RenderAPI::Vulkan); // TODO
if (renderer->apiType == RenderAPI::Vulkan)
{
}
return pixels;
}
inline void RenderTexture::Resize(uint32_t width, uint32_t height)
{
if (this->renderer == nullptr)
return;
if (this->renderer->apiType == RenderAPI::Vulkan)
{
VkFormat format = VkFormat::VK_FORMAT_R8G8B8A8_SRGB;
switch (this->format)
{
case Texture::TextureFormat::SRGB24:
format = VkFormat::VK_FORMAT_R8G8B8_SRGB;
break;
case Texture::TextureFormat::RGB24:
format = VkFormat::VK_FORMAT_R8G8B8_UNORM;
break;
case Texture::TextureFormat::RGBA32:
format = VkFormat::VK_FORMAT_R8G8B8A8_UNORM;
break;
}
static_cast<vk::VulkanFramebuffer*>(framebuffer[core::ThreadType::Game].get())->Clean();
static_cast<vk::VulkanFramebuffer*>(framebuffer[core::ThreadType::Game].get())->CreateOffScreen(width, height, format, bReadUsage);
}
buffer[core::ThreadType::Game]->Clean();
buffer[core::ThreadType::Game]->Create(*framebuffer[core::ThreadType::Game].get());
}
SH_RENDER_API void RenderTexture::SetSize(uint32_t width, uint32_t height)
{
this->width = width;
this->height = height;
Resize(width, height);
bChangeSize = true;
SetDirty();
}
SH_RENDER_API auto RenderTexture::GetSize() const -> glm::vec2
{
return { width, height };
}
SH_RENDER_API void RenderTexture::Sync()
{
Super::Sync();
std::swap(framebuffer[core::ThreadType::Render], framebuffer[core::ThreadType::Game]);
if (bChangeSize)
{
Resize(width, height);
bChangeSize = false;
}
}
#if SH_EDITOR
SH_RENDER_API void RenderTexture::OnPropertyChanged(const core::reflection::Property& property)
{
if (this->renderer == nullptr)
return;
if (property.GetName() == "width" || property.GetName() == "height")
{
Build(*renderer);
}
}
#endif
}