2021-09-23 10:10:39 +02:00
|
|
|
#include "UIPass.h"
|
2024-06-09 12:20:04 +02:00
|
|
|
#include "Graphics/Command.h"
|
2024-04-15 13:48:34 +02:00
|
|
|
#include "Graphics/Enums.h"
|
2021-09-23 10:10:39 +02:00
|
|
|
#include "Graphics/Graphics.h"
|
2023-10-26 18:37:29 +02:00
|
|
|
#include "Graphics/RenderTarget.h"
|
2024-06-09 12:20:04 +02:00
|
|
|
#include "RenderGraph.h"
|
|
|
|
|
|
2021-09-23 10:10:39 +02:00
|
|
|
using namespace Seele;
|
|
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
UIPass::UIPass(Gfx::PGraphics graphics, PScene scene) : RenderPass(graphics, scene) {}
|
2021-09-23 10:10:39 +02:00
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
UIPass::~UIPass() {}
|
2021-09-23 10:10:39 +02:00
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
void UIPass::beginFrame(const Component::Camera& cam) {
|
2023-11-05 10:36:01 +01:00
|
|
|
RenderPass::beginFrame(cam);
|
2022-04-18 18:08:38 +02:00
|
|
|
VertexBufferCreateInfo info = {
|
2024-09-27 15:57:37 +02:00
|
|
|
.sourceData =
|
|
|
|
|
{
|
|
|
|
|
.size = (uint32)(sizeof(UI::RenderElementStyle) * renderElements.size()),
|
|
|
|
|
.data = (uint8*)renderElements.data(),
|
|
|
|
|
},
|
2022-04-18 18:08:38 +02:00
|
|
|
.vertexSize = sizeof(UI::RenderElementStyle),
|
2023-10-26 18:37:29 +02:00
|
|
|
.numVertices = (uint32)renderElements.size(),
|
2022-04-18 18:08:38 +02:00
|
|
|
};
|
|
|
|
|
elementBuffer = graphics->createVertexBuffer(info);
|
2023-10-26 18:37:29 +02:00
|
|
|
uint32 numTextures = static_cast<uint32>(usedTextures.size());
|
2024-08-08 22:14:25 +02:00
|
|
|
numTexturesBuffer->updateContents(0, sizeof(uint32), &numTextures);
|
2024-09-27 15:57:37 +02:00
|
|
|
descriptorSet->updateBuffer(2, 0, numTexturesBuffer);
|
|
|
|
|
for (uint32 i = 0; i < usedTextures.size(); ++i) {
|
|
|
|
|
descriptorSet->updateTexture(3, i, usedTextures[i]);
|
|
|
|
|
}
|
2022-04-18 18:08:38 +02:00
|
|
|
descriptorSet->writeChanges();
|
2024-06-09 12:20:04 +02:00
|
|
|
// co_return;
|
2021-09-23 10:10:39 +02:00
|
|
|
}
|
|
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
void UIPass::render() {
|
2021-09-23 10:10:39 +02:00
|
|
|
graphics->beginRenderPass(renderPass);
|
2024-04-10 10:23:06 +02:00
|
|
|
Gfx::ORenderCommand command = graphics->createRenderCommand("UIPassCommand");
|
2021-09-23 10:10:39 +02:00
|
|
|
command->setViewport(viewport);
|
|
|
|
|
command->bindPipeline(pipeline);
|
2023-10-26 18:37:29 +02:00
|
|
|
command->bindVertexBuffer({elementBuffer});
|
2022-04-18 18:08:38 +02:00
|
|
|
command->bindDescriptor(descriptorSet);
|
2023-10-26 18:37:29 +02:00
|
|
|
command->draw(4, static_cast<uint32>(renderElements.size()), 0, 0);
|
2024-04-11 12:38:42 +02:00
|
|
|
Array<Gfx::ORenderCommand> commands;
|
|
|
|
|
commands.add(std::move(command));
|
|
|
|
|
graphics->executeCommands(std::move(commands));
|
2021-09-23 10:10:39 +02:00
|
|
|
graphics->endRenderPass();
|
2024-06-09 12:20:04 +02:00
|
|
|
|
|
|
|
|
// co_return;
|
2021-09-23 10:10:39 +02:00
|
|
|
}
|
|
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
void UIPass::endFrame() {
|
|
|
|
|
// co_return;
|
2021-09-23 10:10:39 +02:00
|
|
|
}
|
|
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
void UIPass::publishOutputs() {
|
2022-11-17 16:47:42 +01:00
|
|
|
TextureCreateInfo depthBufferInfo = {
|
2023-11-15 17:42:57 +01:00
|
|
|
.format = Gfx::SE_FORMAT_D32_SFLOAT,
|
2023-11-15 00:06:00 +01:00
|
|
|
.width = viewport->getWidth(),
|
|
|
|
|
.height = viewport->getHeight(),
|
2022-11-17 16:47:42 +01:00
|
|
|
.usage = Gfx::SE_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
|
|
|
|
|
};
|
2021-09-23 10:10:39 +02:00
|
|
|
|
|
|
|
|
depthBuffer = graphics->createTexture2D(depthBufferInfo);
|
2024-06-09 12:20:04 +02:00
|
|
|
depthAttachment =
|
|
|
|
|
Gfx::RenderTargetAttachment(depthBuffer, Gfx::SE_IMAGE_LAYOUT_UNDEFINED, Gfx::SE_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
|
|
|
|
|
Gfx::SE_ATTACHMENT_LOAD_OP_CLEAR, Gfx::SE_ATTACHMENT_STORE_OP_STORE);
|
2021-10-01 19:55:04 +02:00
|
|
|
resources->registerRenderPassOutput("UIPASS_DEPTH", depthAttachment);
|
2022-11-17 16:47:42 +01:00
|
|
|
|
|
|
|
|
TextureCreateInfo colorBufferInfo = {
|
2023-11-15 17:42:57 +01:00
|
|
|
.format = Gfx::SE_FORMAT_R16G16B16A16_SFLOAT,
|
2023-11-15 00:06:00 +01:00
|
|
|
.width = viewport->getWidth(),
|
|
|
|
|
.height = viewport->getHeight(),
|
2022-11-17 16:47:42 +01:00
|
|
|
.usage = Gfx::SE_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
|
|
|
|
|
};
|
|
|
|
|
colorBuffer = graphics->createTexture2D(colorBufferInfo);
|
2024-06-09 12:20:04 +02:00
|
|
|
renderTarget = Gfx::RenderTargetAttachment(colorBuffer, Gfx::SE_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
|
|
|
|
|
Gfx::SE_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, Gfx::SE_ATTACHMENT_LOAD_OP_CLEAR,
|
|
|
|
|
Gfx::SE_ATTACHMENT_STORE_OP_STORE);
|
|
|
|
|
renderTarget.clear.color = {{0.0f, 0.0f, 0.0f, 1.0f}};
|
2022-11-17 16:47:42 +01:00
|
|
|
resources->registerRenderPassOutput("UIPASS_COLOR", renderTarget);
|
2021-09-23 10:10:39 +02:00
|
|
|
}
|
|
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
void UIPass::createRenderPass() {
|
2024-07-10 21:07:10 +02:00
|
|
|
ShaderCompilationInfo createInfo = {
|
2024-04-26 19:32:38 +02:00
|
|
|
.name = "UIVertex",
|
2024-07-10 21:07:10 +02:00
|
|
|
.modules = {"UIPass"},
|
|
|
|
|
.entryPoints =
|
|
|
|
|
{
|
|
|
|
|
{"vertexMain", "UIPass"},
|
|
|
|
|
{"fragmentMain", "UIFragment"},
|
|
|
|
|
},
|
2024-04-26 19:32:38 +02:00
|
|
|
};
|
2024-07-10 21:07:10 +02:00
|
|
|
graphics->beginShaderCompilation(createInfo);
|
|
|
|
|
vertexShader = graphics->createVertexShader({0});
|
|
|
|
|
fragmentShader = graphics->createFragmentShader({1});
|
2022-04-18 18:08:38 +02:00
|
|
|
|
2024-04-19 18:23:36 +02:00
|
|
|
descriptorLayout = graphics->createDescriptorLayout("pParams");
|
2024-06-09 12:20:04 +02:00
|
|
|
descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
|
|
|
|
|
.binding = 0,
|
|
|
|
|
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
2024-11-01 21:04:06 +01:00
|
|
|
.uniformLength = sizeof(Matrix4),
|
2024-06-09 12:20:04 +02:00
|
|
|
});
|
|
|
|
|
descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
|
|
|
|
|
.binding = 1,
|
|
|
|
|
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLER,
|
|
|
|
|
});
|
|
|
|
|
descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
|
|
|
|
|
.binding = 2,
|
|
|
|
|
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
2024-11-01 21:04:06 +01:00
|
|
|
.uniformLength = sizeof(uint32)
|
2024-06-09 12:20:04 +02:00
|
|
|
});
|
|
|
|
|
descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
|
|
|
|
|
.binding = 3,
|
|
|
|
|
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
|
|
|
|
|
.textureType = Gfx::SE_IMAGE_VIEW_TYPE_2D_ARRAY,
|
|
|
|
|
.descriptorCount = 256,
|
|
|
|
|
.bindingFlags = Gfx::SE_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT | Gfx::SE_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT,
|
|
|
|
|
});
|
2022-04-18 18:08:38 +02:00
|
|
|
descriptorLayout->create();
|
|
|
|
|
|
2023-01-21 18:43:21 +01:00
|
|
|
Matrix4 projectionMatrix = glm::ortho(0, 1, 1, 0);
|
2024-10-01 11:15:38 +02:00
|
|
|
|
|
|
|
|
Gfx::OUniformBuffer uniformBuffer = graphics->createUniformBuffer(UniformBufferCreateInfo{
|
2024-06-09 12:20:04 +02:00
|
|
|
.sourceData =
|
|
|
|
|
{
|
|
|
|
|
.size = sizeof(Matrix4),
|
|
|
|
|
.data = (uint8*)&projectionMatrix,
|
|
|
|
|
},
|
2024-10-01 11:15:38 +02:00
|
|
|
});
|
2023-11-15 17:42:57 +01:00
|
|
|
Gfx::OSampler backgroundSampler = graphics->createSampler({});
|
2022-04-18 18:08:38 +02:00
|
|
|
|
2024-10-01 11:15:38 +02:00
|
|
|
numTexturesBuffer = graphics->createUniformBuffer(UniformBufferCreateInfo{
|
|
|
|
|
.sourceData =
|
|
|
|
|
{
|
|
|
|
|
.size = sizeof(uint32),
|
|
|
|
|
.data = nullptr,
|
|
|
|
|
},
|
|
|
|
|
});
|
2022-04-18 18:08:38 +02:00
|
|
|
|
|
|
|
|
descriptorSet = descriptorLayout->allocateDescriptorSet();
|
2024-09-27 15:57:37 +02:00
|
|
|
descriptorSet->updateBuffer(0, 0, uniformBuffer);
|
|
|
|
|
descriptorSet->updateSampler(1, 0, backgroundSampler);
|
2022-04-18 18:08:38 +02:00
|
|
|
descriptorSet->writeChanges();
|
|
|
|
|
|
2024-04-23 08:11:44 +02:00
|
|
|
pipelineLayout = graphics->createPipelineLayout();
|
2024-04-19 18:23:36 +02:00
|
|
|
pipelineLayout->addDescriptorLayout(descriptorLayout);
|
2021-09-23 10:10:39 +02:00
|
|
|
pipelineLayout->create();
|
|
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
Gfx::RenderTargetLayout layout = Gfx::RenderTargetLayout{.colorAttachments = {renderTarget}, .depthAttachment = depthAttachment};
|
2024-01-31 09:49:53 +01:00
|
|
|
renderPass = graphics->createRenderPass(std::move(layout), {}, viewport);
|
2024-06-09 12:20:04 +02:00
|
|
|
|
2023-10-26 18:37:29 +02:00
|
|
|
Gfx::LegacyPipelineCreateInfo pipelineInfo;
|
2021-09-23 10:10:39 +02:00
|
|
|
pipelineInfo.vertexShader = vertexShader;
|
|
|
|
|
pipelineInfo.fragmentShader = fragmentShader;
|
|
|
|
|
pipelineInfo.renderPass = renderPass;
|
2024-04-23 08:11:44 +02:00
|
|
|
pipelineInfo.pipelineLayout = pipelineLayout;
|
2021-09-23 10:10:39 +02:00
|
|
|
pipelineInfo.rasterizationState.cullMode = Gfx::SE_CULL_MODE_NONE;
|
|
|
|
|
pipelineInfo.topology = Gfx::SE_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
|
|
|
|
|
|
2023-11-09 22:15:51 +01:00
|
|
|
pipeline = graphics->createGraphicsPipeline(std::move(pipelineInfo));
|
2021-09-23 10:10:39 +02:00
|
|
|
}
|