Crafter.Graphics/interfaces/Crafter.Graphics-RenderingElement2DVulkan.cppm

127 lines
6.8 KiB
Text
Raw Normal View History

2026-04-05 22:53:59 +02:00
/*
Crafter®.Graphics
Copyright (C) 2026 Catcrafts®
catcrafts.net
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License version 3.0 as published by the Free Software Foundation;
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
module;
#include "../lib/stb_truetype.h"
2026-04-09 00:15:09 +02:00
#ifdef CRAFTER_GRAPHICS_RENDERER_VULKAN
#include <vulkan/vulkan.h>
#endif
2026-04-05 22:53:59 +02:00
export module Crafter.Graphics:RenderingElement2DVulkan;
2026-04-09 00:15:09 +02:00
#ifdef CRAFTER_GRAPHICS_RENDERER_VULKAN
2026-04-05 22:53:59 +02:00
import Crafter.Asset;
import std;
import :Transform2D;
2026-04-09 00:15:09 +02:00
import :VulkanBuffer;
2026-04-05 22:53:59 +02:00
import :Types;
import :Window;
2026-04-10 22:26:15 +02:00
import :DescriptorHeapVulkan;
2026-04-05 22:53:59 +02:00
export namespace Crafter {
2026-04-09 00:15:09 +02:00
struct RenderingElement2DVulkanBase : Transform2D {
std::uint16_t index;
std::uint16_t bufferX;
std::uint16_t bufferY;
2026-04-10 22:26:15 +02:00
std::array<VulkanBufferBase*, Window::numFrames> buffers;
RenderingElement2DVulkanBase(Anchor2D anchor) : Transform2D(anchor) {
}
2026-04-09 00:15:09 +02:00
RenderingElement2DVulkanBase(Anchor2D anchor, std::uint16_t bufferX, std::uint16_t bufferY) : bufferX(bufferX), bufferY(bufferY), Transform2D(anchor) {
2026-04-10 22:26:15 +02:00
}
RenderingElement2DVulkanBase(Anchor2D anchor, std::uint16_t bufferX, std::uint16_t bufferY, std::array<VulkanBufferBase*, Window::numFrames>&& buffers) : bufferX(bufferX), bufferY(bufferY), buffers(std::move(buffers)), Transform2D(anchor) {
2026-04-09 00:15:09 +02:00
}
};
2026-04-05 22:53:59 +02:00
template<bool Owning, bool Mapped>
2026-04-09 00:15:09 +02:00
struct RenderingElement2DVulkan : RenderingElement2DVulkanBase {
2026-04-11 18:48:00 +02:00
RenderingElement2DVulkan(Anchor2D anchor, RendertargetBase& target, Transform2D& parent) requires(Owning) : RenderingElement2DVulkanBase(anchor) {
GetScale(target, parent);
this->bufferX = this->scaled.size.x;
this->bufferY = this->scaled.size.y;
for(std::uint8_t i = 0; i < Window::numFrames; i++) {
buffers[i] = new VulkanBuffer<Vector<_Float16, 4, 4>, Mapped>();
static_cast<VulkanBuffer<Vector<_Float16, 4, 4>, Mapped>*>(buffers[i])->Create(VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_2_SHADER_DEVICE_ADDRESS_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, bufferX * bufferY);
}
}
2026-04-09 00:15:09 +02:00
RenderingElement2DVulkan(Anchor2D anchor, std::uint16_t bufferX, std::uint16_t bufferY) requires(Owning) : RenderingElement2DVulkanBase(anchor, bufferX, bufferY) {
for(std::uint8_t i = 0; i < Window::numFrames; i++) {
2026-04-10 22:26:15 +02:00
buffers[i] = new VulkanBuffer<Vector<_Float16, 4, 4>, Mapped>();
static_cast<VulkanBuffer<Vector<_Float16, 4, 4>, Mapped>*>(buffers[i])->Create(VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_2_SHADER_DEVICE_ADDRESS_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, bufferX * bufferY);
2026-04-09 00:15:09 +02:00
}
2026-04-05 22:53:59 +02:00
}
2026-04-10 22:26:15 +02:00
RenderingElement2DVulkan(Anchor2D anchor, std::uint16_t bufferX, std::uint16_t bufferY, std::array<VulkanBufferBase*, Window::numFrames>&& buffers) requires(!Owning) : RenderingElement2DVulkanBase(anchor, bufferX, bufferY, std::move(buffers)) {
2026-04-11 18:48:00 +02:00
2026-04-05 22:53:59 +02:00
}
RenderingElement2DVulkan(Anchor2D anchor, const std::filesystem::path& assetPath, bool single) requires(Owning && Mapped) : RenderingElement2DVulkanBase(anchor) {
TextureAssetInfo info = TextureAsset<_Float16>::LoadInfo(assetPath);
this->bufferX = info.sizeX;
this->bufferY = info.sizeY;
if(single) {
buffers[0] = new VulkanBuffer<Vector<_Float16, 4, 4>, Mapped>();
static_cast<VulkanBuffer<Vector<_Float16, 4, 4>, Mapped>*>(buffers[0])->Create(VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_2_SHADER_DEVICE_ADDRESS_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, bufferX * bufferY);
for(std::uint8_t i = 1; i < Window::numFrames; i++) {
buffers[i] = buffers[0];
}
2026-04-11 18:48:00 +02:00
TextureAsset<_Float16>::Load(assetPath, reinterpret_cast<_Float16*>(static_cast<VulkanBuffer<Vector<_Float16, 4, 4>, Mapped>*>(buffers[0])->value), this->bufferX, this->bufferY);
} else {
for(std::uint8_t i = 0; i < Window::numFrames; i++) {
buffers[i] = new VulkanBuffer<Vector<_Float16, 4, 4>, Mapped>();
static_cast<VulkanBuffer<Vector<_Float16, 4, 4>, Mapped>*>(buffers[i])->Create(VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_2_SHADER_DEVICE_ADDRESS_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, bufferX * bufferY);
}
2026-04-11 18:48:00 +02:00
TextureAsset<_Float16>::Load(assetPath, reinterpret_cast<_Float16*>(static_cast<VulkanBuffer<Vector<_Float16, 4, 4>, Mapped>*>(buffers[0])->value), this->bufferX, this->bufferY);
for(std::uint8_t i = 1; i < Window::numFrames; i++) {
2026-04-11 18:48:00 +02:00
std::memcpy(static_cast<VulkanBuffer<Vector<_Float16, 4, 4>, Mapped>*>(buffers[i])->value, static_cast<VulkanBuffer<Vector<_Float16, 4, 4>, Mapped>*>(buffers[0])->value, this->bufferX * this->bufferY * sizeof(_Float16));
}
}
}
2026-04-05 22:53:59 +02:00
~RenderingElement2DVulkan() {
if constexpr(Owning) {
2026-04-10 22:26:15 +02:00
for(VulkanBufferBase* buffer : buffers) {
delete static_cast<VulkanBuffer<Vector<_Float16, 4, 4>, Mapped>*>(buffer);
2026-04-05 22:53:59 +02:00
}
}
}
RenderingElement2DVulkan(RenderingElement2DVulkan&) = delete;
RenderingElement2DVulkan& operator=(RenderingElement2DVulkan&) = delete;
2026-04-11 18:48:00 +02:00
void UpdatePosition(RendertargetBase& window2, Transform2D& parent) override {
RendertargetVulkan& window = static_cast<RendertargetVulkan&>(window2);
2026-04-05 22:53:59 +02:00
this->ScaleElement(parent);
2026-04-11 18:48:00 +02:00
RenderingElement2DVulkanTransformInfo* val = reinterpret_cast<RenderingElement2DVulkanTransformInfo*>(reinterpret_cast<char*>(window.transformBuffer[window.frame].value) + sizeof(RenderingElement2DVulkanTransformInfo));
2026-04-09 00:15:09 +02:00
val[index].scaled = this->scaled;
2026-04-05 22:53:59 +02:00
for(Transform2D* child : this->children) {
child->UpdatePosition(window, *this);
}
}
2026-04-11 18:48:00 +02:00
void GetScale(RendertargetBase& window, Transform2D& parent) {
this->ScaleElement(parent);
for(Transform2D* child : this->children) {
child->UpdatePosition(window, *this);
2026-04-09 00:15:09 +02:00
}
}
2026-04-11 18:48:00 +02:00
};
2026-04-09 00:15:09 +02:00
}
#endif